Skip to content

Commit

Permalink
Finishing Worhp
Browse files Browse the repository at this point in the history
  • Loading branch information
jgillis committed Feb 28, 2023
1 parent 8bac44b commit 63a6b94
Show file tree
Hide file tree
Showing 4 changed files with 2,193 additions and 5 deletions.
91 changes: 91 additions & 0 deletions worhp/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import os
import re
from collections import defaultdict
versions = {"1.15": "/home/jgillis/programs/worhp_1.15-0_linux"}

worhp_interface = "/home/jgillis/programs/casadi/casadi/interfaces/worhp/worhp_interface.cpp"


structs = ["Workspace", "Params", "OptVar", "Control"]

data = {}


used_fields = defaultdict(set)

# Retrieve all uses of Worhp structs in worhp_interface
with open(worhp_interface,"r") as inp:
contents = inp.read()
for t,e in re.findall(r"worhp_([owpc]).(\w+)", contents):
# Convert t to full struct name
t = structs["wpoc".index(t)]
used_fields[t].add(e)

print(used_fields)


def read_stucts(header_file):
data = {}
active_struct = None
with open(header_file,"r") as inp:
lines = inp.read()
# Multi-line c style comment removal
lines = re.sub(r"/\*.*?\*/", "", lines, flags=re.DOTALL)
for line in lines.split("\n"):
if line.startswith("typedef struct"):
struct_name = line.split()[2]
active_struct = struct_name
data[active_struct] = []
elif active_struct is not None:
# Remove c style comments
line = re.sub(r"/\*.*\*/", "", line)
if line.strip()=="":
continue
if line.startswith("}"):
active_struct = None
else:
d = data[active_struct]
d.append(line.strip()[:-1])
return data


for version, root in versions.items():
for k,v in read_stucts(os.path.join(root,"include","worhp","C_Worhp_Data.h")).items():
data[k[:-6]] = v
qp_data = read_stucts(os.path.join(root,"include","worhp","C_qp_data.h"))
leq_data = read_stucts(os.path.join(root,"include","worhp","C_leqsol_data.h"))

def write_struct(output,fields,struct_name,alias):
outp.write(f"typedef struct {struct_name} {{\n")
for line in fields:
outp.write(" "+line+";\n")
outp.write(f"}} {alias};\n")

def anonymize(fields,used_fields=None):
if used_fields is None:
used_fields = set()
count = 0
for line in fields:
data_type, name = line.rsplit(" ",1)
suffix = "".join(re.findall("\[\w+\]",name))
if name.startswith("*"):
name = name[1:]
data_type = data_type + "*"
if name in used_fields:
yield line
else:
yield f"{data_type} anon{count}"+suffix
count+=1

with open("include/worhp_data_structs.h","w") as outp:
with open(os.path.join(root,"include","worhp","worhp_version.h"),"r") as inp:
outp.write(inp.read())
write_struct(outp,qp_data["QPParamsStruct"],"QPParamsStruct","QPParams")
write_struct(outp,anonymize(leq_data["LeqsolWorkspaceStruct"]),"LeqsolWorkspaceStruct","LeqsolWorkspace")
write_struct(outp,anonymize(qp_data["QPWorkspaceStruct"]),"QPWorkspaceStruct","QPWorkspace")
for k,v in data.items():
v = anonymize(v,used_fields[k])
write_struct(outp,v,f"{k}Struct",k)


#print(data)
179 changes: 175 additions & 4 deletions worhp/include/worhp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,104 @@ extern "C" {
#include <stdbool.h>
#endif

typedef struct _OptVar OptVar;
typedef struct _Workspace Workspace;
typedef struct _Params Params;
typedef struct _Control Control;
#include <stdlib.h>

enum {
WorhpMatrix_Name_Length = 10
};

typedef int mat_int;
typedef int rwmt_index;
typedef int iwmt_index;
typedef int counter;

typedef struct WorhpMatrix {
double* val;
mat_int* row;
mat_int* col;
mat_int* slc;
mat_int* CCrow;
mat_int* CCcol;
mat_int* CCper;
mat_int* nGroups;
mat_int* Groups;
mat_int* nSubGroups;
mat_int* SubGroups;
mat_int* perm;

mat_int dim_val;
mat_int dim_row;
mat_int dim_col;
mat_int dim_slc;
mat_int dim_CCrow;
mat_int dim_CCcol;
mat_int dim_CCper;
mat_int dim_nGroups;
mat_int dim_Groups;
mat_int dim_nSubGroups;
mat_int dim_SubGroups;
mat_int dim_perm;

mat_int nnz;
mat_int nnzDefault;
mat_int nRow;
mat_int nRowDefault;
mat_int nCol;
mat_int nColDefault;

mat_int nGrp;

int kind;

char name[WorhpMatrix_Name_Length];
bool NeedStructure;
bool Dense;
} WorhpMatrix;

typedef struct {
double start;
double total;
int status;
int _shim;
} TimerType;

typedef struct {
int year;
int month;
int day;
int hour;
int minute;
int second;
} DateType;

struct FltNode{
double anon0;
double anon1;
bool anon2;
struct FltNode *anon3;
struct FltNode *anon4;
};

enum {
WorhpQueueSize = 10
};

typedef struct WorhpQueueStruct {
double val[WorhpQueueSize];
int idx;
} WorhpQueue;

typedef struct FltNode FilterNode;

enum {
WMTslices = 100,
WMTrows = 6,
WMTnames = 20,
NAres = 7,
NUserAction = 14,
NStageHistory = 10
};
#include "worhp_data_structs.h"

typedef void (*worhp_print_t) (int mode, const char s[]);

Expand Down Expand Up @@ -54,6 +148,83 @@ bool WorhpSetIntParam (Params *, const char*, int);
void Worhp(OptVar*, Workspace*, Params*, Control*);
/* U Worhp@@WORHP_1.0*/

void InitParams(int*, Params*); // @@WORHP_1.0
bool GetUserAction(const Control *, int); // @@WORHP_1.0
void DoneUserAction(Control *, int); // @@WORHP_1.0
void StatusMsg(OptVar*, Workspace*, Params*, Control*); // @@WORHP_1.0
void IterationOutput(OptVar*, Workspace*, Params*, Control*); // IterationOutput@@WORHP_1.0

enum {
TerminateSuccess = 1000,
OptimalSolution = 1001,
LowPassFilterOptimal = 1011,
OptimalSolutionConstantF = 1012,
FritzJohn = 1021,
NotDiffable = 1022,
Unbounded = 1031,
FeasibleSolution = 1041,
SearchDirectionZero = 1051,
SearchDirectionSmall = 1052,
AcceptableSolution = 1061,
AcceptablePrevious = 1062,
LowPassFilterAcceptable = 1063,
AcceptableSolutionConstantF = 1064,
AcceptablePreviousConstantF = 1065,
AcceptableSolutionSKKT = 1066,
AcceptableSolutionScaled = 1067,
AcceptablePreviousScaled = 1068,
/*-----------------------------------------------------------------
* (somehow) unsuccessful termination
*-----------------------------------------------------------------*/
TerminateError = -1000,
MaxCalls = -1001,
MaxIter = -1002,
Timeout = -1003,
LocalInfeasOptimal = -1011,
LocalInfeas = -1012,
GlobalInfeas = -1013,
TooBig = -1021,
evalsNaN = -1022,
DivergingPrimal = -1023,
DivergingDual = -1024,
MinimumStepsize = -1031,
RegularizationFailed = -1032,
InitError = -1041,
DataError = -1042,
RestartError = -1043,
FileOpenError = -1044,
QPerror = -1051,
LinearSolverFailed = -1052,
TerminatedByCheckFD = -1061,
LicenseError = -1071,
Debug = -1206,
};

enum {
WORHP_PRINT_MESSAGE = 1,
WORHP_PRINT_WARNING = 2,
WORHP_PRINT_ERROR = 4,
};

enum {
callWorhp = 1,
evalF = 2,
evalG = 3,
evalDF = 4,
evalDG = 5,
evalHM = 6,
evalZenDGp = 7,
evalZenDLxp = 8,
evalZenDLp = 9,
evalZenDLpp = 10,
iterOutput = 11,
fidif_DF = 12,
fidif_DG = 13,
fidif_HM = 14,
fidif = 15,
FirstCall = 20
};

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 63a6b94

Please sign in to comment.