Skip to content

Commit

Permalink
- added protype interface for external state estimation data //import…
Browse files Browse the repository at this point in the history
…ant for nmpc loops

- fixed final constraints
 


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22127 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Sep 4, 2014
1 parent 2a078d6 commit d78dc7d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
115 changes: 89 additions & 26 deletions SimulationRuntime/c/optimization/DataManagement/MoveData.c
Expand Up @@ -46,6 +46,8 @@ static inline void calculatedScalingHelper(OptDataBounds * bounds, OptDataTime *
static inline void setRKCoeff(OptDataRK *rk, const int np);
static inline void printSomeModelInfos(OptDataBounds * bounds, OptDataDim * dim, DATA* data);

static inline void pickUpStates(OptData* optdata);

/* pick up model data
* author: Vitalij Ruge
*/
Expand All @@ -71,14 +73,18 @@ int pickUpModelData(DATA* data, SOLVER_INFO* solverInfo)
for(j = 0; j<dim->np;++j)
optData->v[i][j] = (modelica_real*)malloc(nReal*sizeof(modelica_real));
}
optData->data = data;

optData->v0 = (modelica_real*)malloc(nReal*sizeof(modelica_real));
memcpy(optData->v0, data->localData[1]->realVars, nReal*sizeof(modelica_real));

pickUpStates(optData);

optData->sv0 = (modelica_real*)malloc(dim->nx*sizeof(modelica_real));
for(i = 0; i<dim->nx; ++i)
optData->sv0[i] = optData->v0[i] * optData->bounds.scalF[i];

optData->data = data;

printSomeModelInfos(&optData->bounds, &optData->dim, data);

return 0;
Expand Down Expand Up @@ -651,34 +657,91 @@ void diffSynColoredOptimizerSystem(OptData *optData, modelica_real **J, const in
}

void diffSynColoredOptimizerSystemF(OptData *optData, modelica_real **J){
DATA * data = optData->data;
int i,j,l,ii, ll;
const int index = 4;
const h_index = optData->s.indexABCD[index];
const unsigned int * const cC = data->simulationInfo.analyticJacobians[h_index].sparsePattern.colorCols;
const unsigned int * const lindex = optData->s.lindex[index];
const int nx = data->simulationInfo.analyticJacobians[h_index].sizeCols;
const int Cmax = data->simulationInfo.analyticJacobians[h_index].sparsePattern.maxColors + 1;
const int dnx = optData->dim.nx;
const int dnxnc = optData->dim.nJ;
const modelica_real * const resultVars = data->simulationInfo.analyticJacobians[h_index].resultVars;
const unsigned int * const sPindex = data->simulationInfo.analyticJacobians[h_index].sparsePattern.index;

const int * index_J = (index == 3)? optData->s.indexJ3 : optData->s.indexJ2;
const int nJ1 = optData->dim.nJ + 1;

modelica_real **sV = optData->s.seedVec[index];
if(optData->dim.ncf > 0){
DATA * data = optData->data;
int i,j,l,ii, ll;
const int index = 4;
const h_index = optData->s.indexABCD[index];
const unsigned int * const cC = data->simulationInfo.analyticJacobians[h_index].sparsePattern.colorCols;
const unsigned int * const lindex = optData->s.lindex[index];
const int nx = data->simulationInfo.analyticJacobians[h_index].sizeCols;
const int Cmax = data->simulationInfo.analyticJacobians[h_index].sparsePattern.maxColors + 1;
const int dnx = optData->dim.nx;
const int dnxnc = optData->dim.nJ;
const modelica_real * const resultVars = data->simulationInfo.analyticJacobians[h_index].resultVars;
const unsigned int * const sPindex = data->simulationInfo.analyticJacobians[h_index].sparsePattern.index;

const int * index_J = (index == 3)? optData->s.indexJ3 : optData->s.indexJ2;
const int nJ1 = optData->dim.nJ + 1;

modelica_real **sV = optData->s.seedVec[index];

for(i = 1; i < Cmax; ++i){
data->simulationInfo.analyticJacobians[h_index].seedVars = sV[i];

data->callback->functionJacD_column(data);

for(ii = 0; ii < nx; ++ii){
if(cC[ii] == i){
for(j = lindex[ii]; j < lindex[ii + 1]; ++j){
ll = sPindex[j];
optData->Jf[ll][ii] = resultVars[ll];
}
}
}
}
}
}

for(i = 1; i < Cmax; ++i){
data->simulationInfo.analyticJacobians[h_index].seedVars = sV[i];
/*!
* pick up start values from csv for states
* author: Vitalij Ruge
**/
static inline void pickUpStates(OptData* optData){
char* cflags;
cflags = (char*)omc_flagValue[FLAG_INPUT_FILE_STATES];

data->callback->functionJacD_column(data);
if(cflags){
FILE * pFile = NULL;
pFile = fopen(cflags,"r");
if(pFile == NULL){
warningStreamPrint(LOG_STDOUT, 0, "OMC can't find the file %s.",cflags);
}else{
int c, 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);
}else{
int i, j;
double start_value;
char buffer[200];

--n;
if(n != optData->dim.nx)
warningStreamPrint(LOG_STDOUT, 0, "size %i != %i of %s", n, optData->dim.nx, cflags);

rewind(pFile);
for(i =0; i< n; ++i){
fscanf(pFile, "%s", buffer);
fscanf(pFile, "%lf", &start_value);

printf("\nset %s.start %g", buffer,start_value);

for(j = 0; j < optData->dim.nx; ++j){
if(!strcmp(optData->data->modelData.realVarsData[j].info.name, buffer)){
optData->data->localData[0]->realVars[i] = start_value;
optData->data->localData[1]->realVars[i] = start_value;
optData->data->localData[2]->realVars[i] = start_value;
optData->v0[i] = start_value;
}
}

for(ii = 0; ii < nx; ++ii){
if(cC[ii] == i){
for(j = lindex[ii]; j < lindex[ii + 1]; ++j){
ll = sPindex[j];
optData->Jf[ll][ii] = resultVars[ll];
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions SimulationRuntime/c/util/simulation_options.c
Expand Up @@ -45,6 +45,7 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
/* FLAG_IIT */ "iit",
/* FLAG_ILS */ "ils",
/* FLAG_INPUT_FILE */ "exInputFile",
/* FLAG_INPUT_FILE_STATES */ "stateFile",
/* FLAG_INTERACTIVE */ "interactive",
/* FLAG_IOM */ "iom",
/* FLAG_IPOPT_HESSE*/ "ipopt_hesse",
Expand Down Expand Up @@ -91,6 +92,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_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_INTERACTIVE */ "specify interactive simulation",
/* FLAG_IOM */ "value specifies the initialization optimization method",
/* FLAG_IPOPT_HESSE */ "value specifies the hessian for Ipopt",
Expand Down Expand Up @@ -138,6 +140,7 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
/* FLAG_ILS */ "value specifies the number of steps for homotopy method (required: -iim=symbolic) or\n'start value homotopy' method (required: -iim=numeric -iom=nelder_mead_ex)",
/* FLAG_INTERACTIVE */ "specify interactive simulation",
/* 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_IOM */ "value specifies the initialization optimization method",
/* FLAG_IPOPT_HESSE */ "value specifies the hessematrix for Ipopt(OMC, BFGS, const)",
/* FLAG_IPOPT_JAC */ "value specifies the jacobian for Ipopt(SYM, NUM, NUMDENSE)",
Expand Down Expand Up @@ -183,6 +186,7 @@ const int FLAG_TYPE[FLAG_MAX] = {
/* FLAG_IIT */ FLAG_TYPE_OPTION,
/* FLAG_ILS */ FLAG_TYPE_OPTION,
/* FLAG_INPUT_FILE */ FLAG_TYPE_OPTION,
/* FLAG_INPUT_FILE_STATES */ FLAG_TYPE_OPTION,
/* FLAG_INTERACTIVE */ FLAG_TYPE_FLAG,
/* FLAG_IOM */ 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 @@ -52,6 +52,7 @@ enum _FLAG
FLAG_IIT,
FLAG_ILS,
FLAG_INPUT_FILE,
FLAG_INPUT_FILE_STATES,
FLAG_INTERACTIVE,
FLAG_IOM,
FLAG_IPOPT_HESSE,
Expand Down

0 comments on commit d78dc7d

Please sign in to comment.