Skip to content

Commit

Permalink
added flag: csvInput - use libcsv.lib for external input
Browse files Browse the repository at this point in the history
  • Loading branch information
vruge authored and OpenModelica-Hudson committed Jan 27, 2016
1 parent 10e85bd commit 152dfb1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 45 deletions.
132 changes: 88 additions & 44 deletions SimulationRuntime/c/simulation/solver/external_input.c
Expand Up @@ -37,21 +37,30 @@

#include "util/omc_error.h"
#include "util/memory_pool.h"
#include "util/read_csv.h"
#include "util/libcsv.h"
#include "util/read_matlab4.h"

#include "simulation/simulation_runtime.h"
#include "simulation/solver/solver_main.h"
#include "simulation/solver/model_help.h"
#include "simulation/options.h"

static inline void externalInputallocate1(DATA* data, FILE * pFile);
static inline void externalInputallocate2(DATA* data, char *filename);

int externalInputallocate(DATA* data)
{
FILE * pFile = NULL;
int n,m,c;
int i,j;
short useLibCsvH = 1;
char * cflags = NULL;


{
char * cflags = NULL;
cflags = (char*)omc_flagValue[FLAG_INPUT_CSV];
if(!cflags){
cflags = (char*)omc_flagValue[FLAG_INPUT_FILE];
useLibCsvH = 0;
if(cflags){
pFile = fopen(cflags,"r");
if(pFile == NULL)
Expand All @@ -61,67 +70,101 @@ int externalInputallocate(DATA* data)
}


data->simulationInfo->external_input.active = (modelica_boolean) (pFile != NULL);
n = 0;
if(data->simulationInfo->external_input.active){

while(1) {
c = fgetc(pFile);
if (c==EOF) break;
if (c=='\n') ++n;
}
// check if csv file is empty!
if (n == 0)
{
fprintf(stderr, "External input file: externalInput.csv is empty!\n"); fflush(NULL);
EXIT(1);
}

--n;
data->simulationInfo->external_input.n = n;
data->simulationInfo->external_input.N = data->simulationInfo->external_input.n;
rewind(pFile);

do{
c = fgetc(pFile);
if (c==EOF) break;
}while(c!='\n');

m = data->modelData->nInputVars;
data->simulationInfo->external_input.u = (modelica_real**)calloc(modelica_integer_max(1,n),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,m),sizeof(modelica_real));
data->simulationInfo->external_input.t = (modelica_real*)calloc(modelica_integer_max(1,data->simulationInfo->external_input.n),sizeof(modelica_real));

for(i = 0; i < data->simulationInfo->external_input.n; ++i){
c = fscanf(pFile, "%lf", &data->simulationInfo->external_input.t[i]);
for(j = 0; j < m; ++j){
c = fscanf(pFile, "%lf", &data->simulationInfo->external_input.u[i][j]);
}
if(c<0)
data->simulationInfo->external_input.n = i;
}
data->simulationInfo->external_input.active = (modelica_boolean) (pFile != NULL);
if(data->simulationInfo->external_input.active || useLibCsvH){
if(useLibCsvH){
externalInputallocate2(data, cflags);
}else
externalInputallocate1(data, pFile);

if(ACTIVE_STREAM(LOG_SIMULATION))
{
printf("\nExternal Input");
printf("\n========================================================");
for(i = 0; i < data->simulationInfo->external_input.n; ++i){
printf("\nInput: t=%f \t", data->simulationInfo->external_input.t[i]);
for(j = 0; j < m; ++j){
for(j = 0; j < data->modelData->nInputVars; ++j){
printf("u%d(t)= %f \t",j+1,data->simulationInfo->external_input.u[i][j]);
}
}
printf("\n========================================================\n");
}

fclose(pFile);
data->simulationInfo->external_input.i = 0;
}

return 0;
}

static inline void externalInputallocate2(DATA* data, char *filename){
int i, j, k;
struct csv_data *res = read_csv(filename);
data->modelData->nInputVars = res->numvars - 1;
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));


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++];
}
}
omc_free_csv_reader(res);
data->simulationInfo->external_input.active = data->simulationInfo->external_input.n > 0;
}

static inline void externalInputallocate1(DATA* data, FILE * pFile){
int n,m,c;
int i,j;
n = 0;

while(1) {
c = fgetc(pFile);
if (c==EOF) break;
if (c=='\n') ++n;
}
// check if csv file is empty!
if (n == 0)
{
fprintf(stderr, "External input file: externalInput.csv is empty!\n"); fflush(NULL);
EXIT(1);
}

--n;
data->simulationInfo->external_input.n = n;
data->simulationInfo->external_input.N = data->simulationInfo->external_input.n;
rewind(pFile);

do{
c = fgetc(pFile);
if (c==EOF) break;
}while(c!='\n');

m = data->modelData->nInputVars;
data->simulationInfo->external_input.u = (modelica_real**)calloc(modelica_integer_max(1,n),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,m),sizeof(modelica_real));
data->simulationInfo->external_input.t = (modelica_real*)calloc(modelica_integer_max(1,data->simulationInfo->external_input.n),sizeof(modelica_real));

for(i = 0; i < data->simulationInfo->external_input.n; ++i){
c = fscanf(pFile, "%lf", &data->simulationInfo->external_input.t[i]);
for(j = 0; j < m; ++j){
c = fscanf(pFile, "%lf", &data->simulationInfo->external_input.u[i][j]);
}
if(c<0)
data->simulationInfo->external_input.n = i;
}
fclose(pFile);
}

int externalInputFree(DATA* data)
{
if(data->simulationInfo->external_input.active){
Expand Down Expand Up @@ -190,3 +233,4 @@ int externalInputUpdate(DATA* data)
}
return 0;
}

7 changes: 6 additions & 1 deletion SimulationRuntime/c/util/simulation_options.c
Expand Up @@ -49,6 +49,7 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
/* FLAG_IIT */ "iit",
/* FLAG_ILS */ "ils",
/* FLAG_INITIAL_STEP_SIZE */ "initialStepSize",
/* FLAG_INPUT_CSV */ "csvInput",
/* FLAG_INPUT_FILE */ "exInputFile",
/* FLAG_INPUT_FILE_STATES */ "stateFile",
/* FLAG_IPOPT_HESSE*/ "ipopt_hesse",
Expand Down Expand Up @@ -106,6 +107,7 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
/* FLAG_IIT */ "[double] value specifies a time for the initialization of the model",
/* FLAG_ILS */ "[int] default: 1",
/* FLAG_INITIAL_STEP_SIZE */ "value specifies an initial stepsize for the dassl solver",
/* FLAG_INPUT_CSV */ "value specifies an csv-file with inputs for the simulation/optimization of the model",
/* FLAG_INPUT_FILE */ "value specifies an external file with inputs for the simulation/optimization of the model",
/* FLAG_INPUT_FILE_STATES */ "value specifies an file with states start values for the optimization of the model",
/* FLAG_IPOPT_HESSE */ "value specifies the hessian for Ipopt",
Expand Down Expand Up @@ -188,9 +190,11 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
" The value is an Integer with default value 1.",
/* FLAG_INITIAL_STEP_SIZE */
" Value specifies an initial stepsize for the dassl solver.",
/* FLAG_INPUT_CSV */
" Value specifies an csv-file with inputs for the simulation/optimization of the model",
/* FLAG_INPUT_FILE */
" Value specifies an external file with inputs for the simulation/optimization of the model.",
/* FLAG_INPUT_FILE_STATES */
/* FLAG_INPUT_FILE_STATES */
" Value specifies an file with states start values for the optimization of the model.",
/* FLAG_IPOPT_HESSE */
" Value specifies the hessematrix for Ipopt(OMC, BFGS, const).",
Expand Down Expand Up @@ -305,6 +309,7 @@ const int FLAG_TYPE[FLAG_MAX] = {
/* FLAG_IIT */ FLAG_TYPE_OPTION,
/* FLAG_ILS */ FLAG_TYPE_OPTION,
/* FLAG_INITIAL_STEP_SIZE */ FLAG_TYPE_OPTION,
/* FLAG_INPUT_CSV */ FLAG_TYPE_OPTION,
/* FLAG_INPUT_FILE */ FLAG_TYPE_OPTION,
/* FLAG_INPUT_FILE_STATES */ FLAG_TYPE_OPTION,
/* FLAG_IPOPT_HESSE */ FLAG_TYPE_OPTION,
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/c/util/simulation_options.h
Expand Up @@ -57,6 +57,7 @@ enum _FLAG
FLAG_IIT,
FLAG_ILS,
FLAG_INITIAL_STEP_SIZE,
FLAG_INPUT_CSV,
FLAG_INPUT_FILE,
FLAG_INPUT_FILE_STATES,
FLAG_IPOPT_HESSE,
Expand Down

0 comments on commit 152dfb1

Please sign in to comment.