Skip to content

Commit

Permalink
csvInput: input order by name
Browse files Browse the repository at this point in the history
  • Loading branch information
vruge authored and OpenModelica-Hudson committed Feb 2, 2016
1 parent 993bd3a commit 530076c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
14 changes: 14 additions & 0 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -150,6 +150,7 @@ end translateInitFile;
extern int <%symbolName(modelNamePrefixStr,"function_equationsSynchronous")%>(DATA * data, threadData_t *threadData, long i);
extern void <%symbolName(modelNamePrefixStr,"read_input_fmu")%>(MODEL_DATA* modelData, SIMULATION_INFO* simulationData);
extern void <%symbolName(modelNamePrefixStr,"function_savePreSynchronous")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"inputNames")%>(DATA* data, char ** names);
<%\n%>
>>
end match
Expand Down Expand Up @@ -972,6 +973,7 @@ template simulationFile(SimCode simCode, String guid, Boolean isModelExchangeFMU
,<%symbolName(modelNamePrefixStr,"functionODE_Partial")%>
,<%symbolName(modelNamePrefixStr,"functionFMIJacobian")%>
#endif
,<%symbolName(modelNamePrefixStr,"inputNames")%>
<%\n%>
};
Expand Down Expand Up @@ -1597,6 +1599,18 @@ template functionInput(ModelInfo modelInfo, String modelNamePrefix)
TRACE_POP
return 0;
}

int <%symbolName(modelNamePrefix,"inputNames")%>(DATA *data, char ** names){
TRACE_PUSH
<%vars.inputVars |> simVar as SIMVAR(__) hasindex i0 =>
'names[<%i0%>] = (char *) <%cref(name)%>__varInfo.name;'
;separator="\n"
%>
TRACE_POP
return 0;
}
>>
end match
end functionInput;
Expand Down
4 changes: 4 additions & 0 deletions SimulationRuntime/c/openmodelica_func.h
Expand Up @@ -319,6 +319,10 @@ int (*function_equationsSynchronous)(DATA *data, threadData_t *threadData, long
*/
void (*read_input_fmu)(MODEL_DATA* modelData, SIMULATION_INFO* simulationData);

/*
* return input names
*/
int (*inputNames)(DATA* modelData, char ** names);
#ifdef FMU_EXPERIMENTAL
/* functionODEPartial contains those equations that are needed
* to calculate the state derivative i-th */
Expand Down
59 changes: 47 additions & 12 deletions SimulationRuntime/c/simulation/solver/external_input.c
Expand Up @@ -65,12 +65,11 @@ int externalInputallocate(DATA* data)
pFile = fopen(cflags,"r");
if(pFile == NULL)
warningStreamPrint(LOG_STDOUT, 0, "OMC can't find the file %s.",cflags);
}else
}else{
pFile = fopen("externalInput.csv","r");
}
}



data->simulationInfo->external_input.active = (modelica_boolean) (pFile != NULL);
if(data->simulationInfo->external_input.active || useLibCsvH){
if(useLibCsvH){
Expand All @@ -97,27 +96,63 @@ int externalInputallocate(DATA* data)
return 0;
}

static inline void externalInputallocate2(DATA* data, char *filename){

int cmp_modelica_integer(const void *v1, const void *v2) {
return (*(modelica_integer*)v1 - *(modelica_integer*)v2);
}

void externalInputallocate2(DATA* data, char *filename){
int i, j, k;
struct csv_data *res = read_csv(filename);
data->modelData->nInputVars = res->numvars - 1;
char ** names;
int * indx;
const int nu = data->modelData->nInputVars;
const int nnu = modelica_integer_min(nu, res->numvars - 1);

data->modelData->nInputVars = nu;
data->simulationInfo->external_input.n = res->numsteps;
data->simulationInfo->external_input.N = data->simulationInfo->external_input.n;

data->simulationInfo->external_input.u = (modelica_real**)calloc(modelica_integer_max(1,res->numsteps),sizeof(modelica_real*));
for(i = 0; i<data->simulationInfo->external_input.n; ++i)
data->simulationInfo->external_input.u[i] = (modelica_real*)calloc(modelica_integer_max(1,data->modelData->nInputVars),sizeof(modelica_real));
data->simulationInfo->external_input.t = (modelica_real*)calloc(modelica_integer_max(1,data->simulationInfo->external_input.n),sizeof(modelica_real));
data->simulationInfo->external_input.u = (modelica_real**)calloc(data->simulationInfo->external_input.n+1, sizeof(modelica_real*));

names = (char**)malloc(nu * sizeof(char*));

for(i = 0; i<data->simulationInfo->external_input.n; ++i){
data->simulationInfo->external_input.u[i] = (modelica_real*)calloc(nnu, sizeof(modelica_real));
}

data->simulationInfo->external_input.t = (modelica_real*)calloc(data->simulationInfo->external_input.n+1, sizeof(modelica_real));

data->callback->inputNames(data, names);

indx = (int*)malloc(nu*sizeof(int));
for(i = 0; i < nu; ++i){
indx[i] = -1;
for(j = 0; j < (res->numvars - 1); ++j){
if(strcmp(names[i], res->variables[j]) == 0){
indx[i] = j;
break;
}
}
}

qsort((void*) indx, nu, sizeof(int), &cmp_modelica_integer);

for(i = 0, k= 0; i < data->simulationInfo->external_input.n; ++i)
data->simulationInfo->external_input.t[i] = res->data[k++];
for(j = 0; j < data->modelData->nInputVars; ++j){
for(i = 0; i < data->simulationInfo->external_input.n; ++i){
data->simulationInfo->external_input.u[i][j] = res->data[k++];

for(j = 0; j < nu; ++j){
if(indx[j] != -1){
k = (indx[j])*data->simulationInfo->external_input.n;
for(i = 0; i < data->simulationInfo->external_input.n; ++i){
data->simulationInfo->external_input.u[i][j] = res->data[k++];
}
}
}

omc_free_csv_reader(res);
free(names);
free(indx);
data->simulationInfo->external_input.active = data->simulationInfo->external_input.n > 0;
}

Expand Down

0 comments on commit 530076c

Please sign in to comment.