Skip to content

Commit

Permalink
Remove simulation option -exInputFile. (#9518)
Browse files Browse the repository at this point in the history
  - The functionality of this option was very similar to `-csvInput`. The
    only differences were:

      - `exInputFile` accepted space separated CSV files only.
      - `exInputFile` always looked for a default file called `externalInput.csv`
        if no file was explicitly specified.

  - The flag is redundant and confusing. Its functionality can be achieved
    by `-csvInput`.

  - Fixes #9488.
  • Loading branch information
mahge committed Oct 14, 2022
1 parent a528d6d commit 7cc2dd0
Show file tree
Hide file tree
Showing 10 changed files with 558 additions and 645 deletions.
92 changes: 17 additions & 75 deletions OMCompiler/SimulationRuntime/c/simulation/solver/external_input.c
Expand Up @@ -55,45 +55,31 @@ int externalInputallocate(DATA* data)
FILE * pFile = NULL;
int i,j;
short useLibCsvH = 1;
char * cflags = NULL;
char * csv_input_file = NULL;


cflags = (char*)omc_flagValue[FLAG_INPUT_CSV];
if(!cflags){
cflags = (char*)omc_flagValue[FLAG_INPUT_FILE];
useLibCsvH = 0;
if(cflags){
pFile = omc_fopen(cflags,"r");
if(pFile == NULL)
warningStreamPrint(LOG_STDOUT, 0, "OMC can't find the file %s.",cflags);
}else{
pFile = omc_fopen("externalInput.csv","r");
}
csv_input_file = (char*)omc_flagValue[FLAG_INPUT_CSV];
if(!csv_input_file) {
data->simulationInfo->external_input.active = 0;
return 0;
}

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 < data->modelData->nInputVars; ++j){
printf("u%d(t)= %f \t",j+1,data->simulationInfo->external_input.u[i][j]);
}
externalInputallocate2(data, csv_input_file);

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 < data->modelData->nInputVars; ++j){
printf("u%d(t)= %f \t",j+1,data->simulationInfo->external_input.u[i][j]);
}
printf("\n========================================================\n");
}

data->simulationInfo->external_input.i = 0;
printf("\n========================================================\n");
}

data->simulationInfo->external_input.i = 0;
return 0;
}

Expand Down Expand Up @@ -157,50 +143,6 @@ void externalInputallocate2(DATA* data, char *filename){
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
6 changes: 0 additions & 6 deletions OMCompiler/SimulationRuntime/c/util/simulation_options.c
Expand Up @@ -83,7 +83,6 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
/* FLAG_IMPRK_LS */ "impRKLS",
/* FLAG_INITIAL_STEP_SIZE */ "initialStepSize",
/* FLAG_INPUT_CSV */ "csvInput",
/* FLAG_INPUT_FILE */ "exInputFile",
/* FLAG_INPUT_FILE_STATES */ "stateFile",
/* FLAG_INPUT_PATH */ "inputPath",
/* FLAG_IPOPT_HESSE*/ "ipopt_hesse",
Expand Down Expand Up @@ -214,7 +213,6 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
/* FLAG_IMPRK_LS */ "selects the linear solver of the integration methods: impeuler, trapezoid and imprungekuta",
/* FLAG_INITIAL_STEP_SIZE */ "value specifies an initial step size for supported 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_INPUT_PATH */ "value specifies a path for reading the input files i.e., model_init.xml and model_info.json",
/* FLAG_IPOPT_HESSE */ "value specifies the hessian for Ipopt",
Expand Down Expand Up @@ -420,8 +418,6 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
" Value specifies an initial step size, used by the methods: dassl, ida",
/* 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_INPUT_PATH */
Expand Down Expand Up @@ -668,7 +664,6 @@ const flag_repeat_policy FLAG_REPEAT_POLICIES[FLAG_MAX] = {
/* FLAG_IMPRK_LS */ FLAG_REPEAT_POLICY_FORBID,
/* FLAG_INITIAL_STEP_SIZE */ FLAG_REPEAT_POLICY_FORBID,
/* FLAG_INPUT_CSV */ FLAG_REPEAT_POLICY_FORBID,
/* FLAG_INPUT_FILE */ FLAG_REPEAT_POLICY_FORBID,
/* FLAG_INPUT_FILE_STATES */ FLAG_REPEAT_POLICY_FORBID,
/* FLAG_INPUT_PATH */ FLAG_REPEAT_POLICY_FORBID,
/* FLAG_IPOPT_HESSE*/ FLAG_REPEAT_POLICY_FORBID,
Expand Down Expand Up @@ -798,7 +793,6 @@ const int FLAG_TYPE[FLAG_MAX] = {
/* FLAG_IMPRK_ORDER */ 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_INPUT_PATH */ FLAG_TYPE_OPTION,
/* FLAG_IPOPT_HESSE */ FLAG_TYPE_OPTION,
Expand Down
1 change: 0 additions & 1 deletion OMCompiler/SimulationRuntime/c/util/simulation_options.h
Expand Up @@ -99,7 +99,6 @@ enum _FLAG
FLAG_IMPRK_LS,
FLAG_INITIAL_STEP_SIZE,
FLAG_INPUT_CSV,
FLAG_INPUT_FILE,
FLAG_INPUT_FILE_STATES,
FLAG_INPUT_PATH,
FLAG_IPOPT_HESSE,
Expand Down
Expand Up @@ -73,13 +73,13 @@

\maketitle
\abstract{
The initial guess is (any other nonlinear problem) important to find the soulation. The optimizer need for each time point an initial guess. Currently it's not possible formulate a initial guess/start value for each time point with Modelica. The following notes help to make a initial guess for dyn. optimization with \OpenModelica{}.\\
The initial guess is (any other nonlinear problem) important to find the solution. The optimizer need for each time point an initial guess. Currently it's not possible formulate a initial guess/start value for each time point with Modelica. The following notes help to make a initial guess for dyn. optimization with \OpenModelica{}.\\

tip: see \url{https://build.openmodelica.org/Documentation/OpenModelica.Scripting.html} for \OpenModelica{} scripting.
}

\section{prior simulator}
\OpenModelica{} automaticly done a prior simulatn and use the simulation as intial guess.
\OpenModelica{} automatically done a prior simulation and use the simulation as initial guess.
\begin{lstlisting}[language=Pascal, caption={initial guess with simulation},frame=single,
label=lst:init]
optimize(modelxxx, simflags="-ipopt_init SIM");
Expand All @@ -91,14 +91,14 @@ \subsection{prior simulator with const inputs}
\subsection{prior simulator with external inputs}
\OpenModelica{} fetch the inputs around the prior simulation from a file.
\begin{lstlisting}[language=Pascal, caption={external inputs},frame=single]
optimize(modelxxx, simflags="-exInputFile xxx.csv -ipopt_init SIM");
optimize(modelxxx, simflags="-csvInput xxx.csv -ipopt_init SIM");
\end{lstlisting}
\begin{lstlisting}[language=Pascal, caption={example xxx.csv},frame=single,label=lst:exInputFile2]
time input1 input2 input3
0 -1 2 3
0.2 -0.9 2.3 3.5
0.6 -0.5 2.0 2.9
1.1 0.3 1.6 3.1);
\begin{lstlisting}[language=Pascal, caption={example xxx.csv},frame=single,label=lst:csvInput2]
time, input1, input2, input3
0, -1, 2, 3
0.2, -0.9, 2.3, 3.5
0.6, -0.5, 2.0, 2.9
1.1, 0.3, 1.6, 3.1);
\end{lstlisting}

\section{file}
Expand Down
Expand Up @@ -8,38 +8,18 @@ getErrorString();
loadFile("BatchReactor.mo");
getErrorString();

optimize(nmpcBatchReactor, numberOfIntervals=20, tolerance = 1e-8, simflags="-ipopt_max_iter=12 -exInputFile ReferenceFiles/initInputForBR.csv -lv LOG_IPOPT_ERROR");
optimize(nmpcBatchReactor, numberOfIntervals=20, tolerance = 1e-8, simflags="-ipopt_max_iter=12 -csvInput ReferenceFiles/initInputForBR.csv -lv LOG_IPOPT_ERROR");
getErrorString();


// true
// ""
// true
// ""
// record SimulationResult
// resultFile = "nmpcBatchReactor_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 20, tolerance = 0.00000001, method = 'optimization', fileNamePrefix = 'nmpcBatchReactor', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-ipopt_jac=NUM'",
// messages = "
// ******************************************************************************
// This program contains Ipopt, a library for large-scale nonlinear optimization.
// Ipopt is released as open source code under the Eclipse Public License (EPL).
// For more information visit https://github.com/coin-or/Ipopt
// ******************************************************************************
//
// "
// end SimulationResult;
// ""
// {"Files Equal!"}
// ""
// endResult
// Result:
// true
// ""
// true
// ""
// record SimulationResult
// resultFile = "nmpcBatchReactor_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 20, tolerance = 1e-08, method = 'optimization', fileNamePrefix = 'nmpcBatchReactor', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-ipopt_max_iter=12 -exInputFile ReferenceFiles/initInputForBR.csv -lv LOG_IPOPT_ERROR'",
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 20, tolerance = 1e-08, method = 'optimization', fileNamePrefix = 'nmpcBatchReactor', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-ipopt_max_iter=12 -csvInput ReferenceFiles/initInputForBR.csv -lv LOG_IPOPT_ERROR'",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
//
// Optimizer Variables
Expand Down
4 changes: 2 additions & 2 deletions testsuite/openmodelica/cruntime/optimization/basic/DMwarm.mos
Expand Up @@ -5,7 +5,7 @@
setCommandLineOptions("+g=Optimica"); getErrorString();
loadFile("DM.mo"); getErrorString();

optimize(diesel_model, stopTime=0.5, numberOfIntervals=450,tolerance=1e-10, simflags="-lv LOG_IPOPT_ERROR -optimizerNP 1 -exInputFile ./ReferenceFiles/initDM.csv -ipopt_warm_start 12 -emit_protected"); getErrorString();
optimize(diesel_model, stopTime=0.5, numberOfIntervals=450,tolerance=1e-10, simflags="-lv LOG_IPOPT_ERROR -optimizerNP 1 -csvInput ./ReferenceFiles/initDM.csv -ipopt_warm_start 12 -emit_protected"); getErrorString();

res := OpenModelica.Scripting.compareSimulationResults("diesel_model_res.mat","ReferenceFiles/diesel_model_ref.mat","diesel_model_res.csv",0.05,0.0005); getErrorString();

Expand All @@ -16,7 +16,7 @@ res := OpenModelica.Scripting.compareSimulationResults("diesel_model_res.mat","R
// ""
// record SimulationResult
// resultFile = "diesel_model_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 0.5, numberOfIntervals = 450, tolerance = 1e-10, method = 'optimization', fileNamePrefix = 'diesel_model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-lv LOG_IPOPT_ERROR -optimizerNP 1 -exInputFile ./ReferenceFiles/initDM.csv -ipopt_warm_start 12 -emit_protected'",
// simulationOptions = "startTime = 0.0, stopTime = 0.5, numberOfIntervals = 450, tolerance = 1e-10, method = 'optimization', fileNamePrefix = 'diesel_model', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-lv LOG_IPOPT_ERROR -optimizerNP 1 -csvInput ./ReferenceFiles/initDM.csv -ipopt_warm_start 12 -emit_protected'",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
//
// Optimizer Variables
Expand Down
Expand Up @@ -5,7 +5,7 @@
setCommandLineOptions("+g=Optimica"); getErrorString();
loadFile("DM.mo"); getErrorString();

optimize(diesel_model, stopTime=0.5, numberOfIntervals=450,tolerance=1e-10, simflags="-lv LOG_IPOPT_ERROR -optimizerNP 1 -exInputFile ./ReferenceFiles/initDM.csv -ipopt_warm_start 12 -emit_protected", outputFormat="csv"); getErrorString();
optimize(diesel_model, stopTime=0.5, numberOfIntervals=450,tolerance=1e-10, simflags="-lv LOG_IPOPT_ERROR -optimizerNP 1 -csvInput ./ReferenceFiles/initDM.csv -ipopt_warm_start 12 -emit_protected", outputFormat="csv"); getErrorString();
optimize(diesel_model, stopTime=0.5, numberOfIntervals=450,tolerance=1e-10, simflags="-lv LOG_IPOPT_ERROR -optimizerNP 1 -csvInput ./diesel_model_res.csv -ipopt_max_iter=-1 -emit_protected", outputFormat="mat"); getErrorString();

res := OpenModelica.Scripting.compareSimulationResults("diesel_model_res.mat","ReferenceFiles/diesel_model_ref.mat","diesel_model_diff.csv",0.05,0.0005,{"u_f","u_wg"}); getErrorString();
Expand All @@ -17,7 +17,7 @@ res := OpenModelica.Scripting.compareSimulationResults("diesel_model_res.mat","R
// ""
// record SimulationResult
// resultFile = "diesel_model_res.csv",
// simulationOptions = "startTime = 0.0, stopTime = 0.5, numberOfIntervals = 450, tolerance = 1e-10, method = 'optimization', fileNamePrefix = 'diesel_model', options = '', outputFormat = 'csv', variableFilter = '.*', cflags = '', simflags = '-lv LOG_IPOPT_ERROR -optimizerNP 1 -exInputFile ./ReferenceFiles/initDM.csv -ipopt_warm_start 12 -emit_protected'",
// simulationOptions = "startTime = 0.0, stopTime = 0.5, numberOfIntervals = 450, tolerance = 1e-10, method = 'optimization', fileNamePrefix = 'diesel_model', options = '', outputFormat = 'csv', variableFilter = '.*', cflags = '', simflags = '-lv LOG_IPOPT_ERROR -optimizerNP 1 -csvInput ./ReferenceFiles/initDM.csv -ipopt_warm_start 12 -emit_protected'",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
//
// Optimizer Variables
Expand Down

0 comments on commit 7cc2dd0

Please sign in to comment.