Skip to content

Commit

Permalink
- added functionality: read external input from csv for dassl and opt…
Browse files Browse the repository at this point in the history
…imization

 - for cheking input or initial optimization



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19227 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Feb 21, 2014
1 parent f851f83 commit dd30eaf
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 4 deletions.
1 change: 1 addition & 0 deletions SimulationRuntime/c/Makefile.common
Expand Up @@ -43,6 +43,7 @@ RUNTIMESIMSOLVER_HEADERS = ./simulation/solver/delay.h \
./simulation/solver/perform_simulation.c \
./simulation/solver/dassl.h \
./simulation/solver/events.h \
./simulation/solver/external_input.h\
./simulation/solver/solver_main.h

RUNTIMEMETA_HEADERS = ./meta/meta_modelica_builtin_boxptr.h \
Expand Down
16 changes: 14 additions & 2 deletions SimulationRuntime/c/optimization/initialOptimizer/initial_guess.c
Expand Up @@ -108,6 +108,9 @@ static int initial_guess_ipopt_sim(IPOPT_DATA_ *iData,SOLVER_INFO* solverInfo)
SIMULATION_DATA *sData = (SIMULATION_DATA*)data->localData[0];
SIMULATION_INFO *sInfo = &(data->simulationInfo);

if(!data->simulationInfo.external_input.active)
externalInputallocate(data);

/* Initial DASSL solver */
DASSL_DATA* dasslData = (DASSL_DATA*) malloc(sizeof(DASSL_DATA));

Expand All @@ -129,6 +132,12 @@ static int initial_guess_ipopt_sim(IPOPT_DATA_ *iData,SOLVER_INFO* solverInfo)
v[ii] = u0[j]*iData->scalVar[j + iData->nx];
}

if(!data->simulationInfo.external_input.active)
for(i = 0; i<iData->nu;++i)
data->simulationInfo.inputVars[i] = u0[i];
else
externalInputUpdate(data);

printGuess = (short)(ACTIVE_STREAM(LOG_INIT) && !ACTIVE_STREAM(LOG_SOLVER));
if(printGuess){
printf("\n****initial guess****");
Expand All @@ -139,7 +148,8 @@ static int initial_guess_ipopt_sim(IPOPT_DATA_ *iData,SOLVER_INFO* solverInfo)
for(jj=0; jj<iData->deg; ++jj, ++k){
solverInfo->currentStepSize = iData->time[k] - iData->time[k-1];
iData->data->localData[1]->timeValue = iData->time[k];

if(data->simulationInfo.external_input.active)
externalInputUpdate(data);
dasrt_step(data, solverInfo);

if(printGuess)
Expand All @@ -149,7 +159,7 @@ static int initial_guess_ipopt_sim(IPOPT_DATA_ *iData,SOLVER_INFO* solverInfo)
v[j] = sData->realVars[j] * iData->scalVar[j];

for(; j< iData->nv; ++j)
v[j] = iData->start_u[j-iData->nx] * iData->scalVar[j];
v[j] = data->simulationInfo.inputVars[j-iData->nx] * iData->scalVar[j];

v += iData->nv;
/* updateContinuousSystem(iData->data); */
Expand All @@ -172,6 +182,8 @@ static int initial_guess_ipopt_sim(IPOPT_DATA_ *iData,SOLVER_INFO* solverInfo)
printf("\n*****initial guess done*****");

dasrt_deinitial(solverInfo->solverData);
externalInputFree(data);
data->simulationInfo.external_input.active = 0;

solverInfo->solverData = (void*)iData;
sInfo->solverMethod = "optimization";
Expand Down
4 changes: 4 additions & 0 deletions SimulationRuntime/c/simulation/solver/dassl.c
Expand Up @@ -46,6 +46,8 @@
#include "f2c.h"
#include "meta_modelica.h"

#include "external_input.h"

static const char *dasslMethodStr[DASSL_MAX] = {"unknown",
"dassl",
"dasslwort",
Expand Down Expand Up @@ -306,6 +308,8 @@ int dasrt_step(DATA* simData, SOLVER_INFO* solverInfo)

/* read input vars */
if(solverInfo->solverMethod != S_OPTIMIZATION) {
if(simData->simulationInfo.external_input.active)
externalInputUpdate(simData);
simData->callback->input_function(simData);
}

Expand Down
120 changes: 120 additions & 0 deletions SimulationRuntime/c/simulation/solver/external_input.c
@@ -0,0 +1,120 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#include <string.h>
#include <setjmp.h>

#include "openmodelica.h"
#include "openmodelica_func.h"
#include "simulation_data.h"

#include "util/omc_error.h"
#include "util/memory_pool.h"

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


int externalInputallocate(DATA* data)
{
FILE * pFile;
int n,m,c;
int i,j;

pFile = fopen("externalInput.csv","r");
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;
}
--n;
data->simulationInfo.external_input.n = n;
rewind(pFile);

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

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

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

return 0;
}

int externalInputFree(DATA* data)
{
if(data->simulationInfo.external_input.active){
int j;

free(data->simulationInfo.external_input.t);
for(j = 0; j < data->simulationInfo.external_input.n; ++j)
free(data->simulationInfo.external_input.u[j]);
free(data->simulationInfo.external_input.u);
}
return 0;
}


int externalInputUpdate(DATA* data)
{
double t;
int i;
t = data->localData[0]->timeValue;
while(t > data->simulationInfo.external_input.t[data->simulationInfo.external_input.i+1]
&& data->simulationInfo.external_input.i < (data->simulationInfo.external_input.n-2)){
++data->simulationInfo.external_input.i;
}

data->simulationInfo.external_input.dt = (data->simulationInfo.external_input.t[data->simulationInfo.external_input.i+1] - data->simulationInfo.external_input.t[data->simulationInfo.external_input.i]);
for(i = 0; i < data->modelData.nInputVars; ++i){
data->simulationInfo.inputVars[i] =
data->simulationInfo.external_input.u[data->simulationInfo.external_input.i][i] +
data->simulationInfo.external_input.dt*(t -data->simulationInfo.external_input.t[data->simulationInfo.external_input.i])*
data->simulationInfo.external_input.u[data->simulationInfo.external_input.i+1][i];
}
return 0;
}
34 changes: 34 additions & 0 deletions SimulationRuntime/c/simulation/solver/external_input.h
@@ -0,0 +1,34 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/


int externalInputallocate(DATA* data);
int externalInputFree(DATA* data);
int externalInputUpdate(DATA* data);
5 changes: 3 additions & 2 deletions SimulationRuntime/c/simulation/solver/solver_main.c
Expand Up @@ -41,6 +41,7 @@
#include "dassl.h"
#include "delay.h"
#include "events.h"
#include "external_input.h"
#include "varinfo.h"
#include "stateset.h"
#include "radau.h"
Expand Down Expand Up @@ -219,7 +220,7 @@ int initializeSolverData(DATA* data, SOLVER_INFO* solverInfo)
}
#endif


externalInputallocate(data);
if(measure_time_flag)
{
rt_accumulate(SIM_TIMER_PREINIT);
Expand Down Expand Up @@ -304,7 +305,7 @@ int freeSolverData(DATA* data, SOLVER_INFO* solverInfo)
{
/* free other solver memory */
}

externalInputFree(data);
/* free stateset data */
freeStateSetData(data);

Expand Down
17 changes: 17 additions & 0 deletions SimulationRuntime/c/simulation_data.h
Expand Up @@ -150,6 +150,22 @@ typedef struct ANALYTIC_JACOBIAN

}ANALYTIC_JACOBIAN;

/* EXTERNAL_INPUT
*
* extern input for dassl and optimization
*
*/
typedef struct EXTERNAL_INPUT
{
modelica_boolean active;
float** u;
float* t;
modelica_real dt;
modelica_integer n;
modelica_integer i;

}EXTERNAL_INPUT;

/* Alias data with various types*/
typedef struct DATA_REAL_ALIAS
{
Expand Down Expand Up @@ -489,6 +505,7 @@ typedef struct SIMULATION_INFO

modelica_real* inputVars;
modelica_real* outputVars;
EXTERNAL_INPUT external_input;

ANALYTIC_JACOBIAN* analyticJacobians;

Expand Down

0 comments on commit dd30eaf

Please sign in to comment.