Skip to content

Commit

Permalink
- add -overrideFile functionality to the simulation
Browse files Browse the repository at this point in the history
  runtime to be used when you have a lot of variables
  to override and the command line size does not suffice.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13104 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Oct 1, 2012
1 parent f869dbc commit 0da4b68
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
57 changes: 48 additions & 9 deletions SimulationRuntime/c/simulation/simulation_input_xml.cpp
Expand Up @@ -94,7 +94,7 @@ typedef struct omc_ModelInput
// a map for overrides
typedef std::map<std::string, std::string> omc_CommandLineOverrides;
// function to handle command line settings override
omc_ModelInput doOverride(omc_ModelInput mi, MODEL_DATA* modelData, std::string* override);
omc_ModelInput doOverride(omc_ModelInput mi, MODEL_DATA* modelData, std::string* override, std::string* overrideFile);

/* reads double value from a string */
void read_value(std::string s, modelica_real* res);
Expand Down Expand Up @@ -288,7 +288,8 @@ void read_input_xml(int argc, char **argv,

// deal with override
std::string* override = (string*)getFlagValue("override", argc, argv);
mi = doOverride(mi, modelData, override);
std::string* overrideFile = (string*)getFlagValue("overrideFile", argc, argv);
mi = doOverride(mi, modelData, override, overrideFile);

/* read all the DefaultExperiment values */
DEBUG_INFO(LOG_SOLVER, "read all the DefaultExperiment values:");
Expand Down Expand Up @@ -1051,16 +1052,52 @@ inline void read_value(std::string s, int* res)
}


omc_ModelInput doOverride(omc_ModelInput mi, MODEL_DATA* modelData, std::string* override)
omc_ModelInput doOverride(omc_ModelInput mi, MODEL_DATA* modelData, std::string* override, std::string* overrideFile)
{
omc_CommandLineOverrides mOverrides;
if (override)
char* overrideStr = NULL;
if ((override != NULL) && (overrideFile != NULL))
{
THROW("simulation_input_xml.cpp: usage error you cannot have both -override and -overrideFile active at the same time. see Model -? for more info!");
}

if (override != NULL)
{
overrideStr = strdup(override->c_str());
}

if (overrideFile != NULL)
{
/* read override values from file */
DEBUG_INFO_AL1(LOG_SOLVER, "read override values from file: %s", overrideFile->c_str());
std::ifstream infile;

infile.open(overrideFile->c_str(), ifstream::in);
if (infile.is_open() == false)
{
THROW1("simulation_input_xml.cpp: could not open the file given to -overrideFile=%s", overrideFile->c_str());
}

std::string line;
// get the first line
std::getline(infile, line);
std::string overrideLine(line);
// get the rest of the lines
while (std::getline(infile, line))
{
overrideLine += "," + line;
}

overrideStr = strdup(overrideLine.c_str());
infile.close();
}

if (overrideStr != NULL)
{
/* read override values */
DEBUG_INFO_AL1(LOG_SOLVER, "read override values: %s", override->c_str());
std::string key, value;
char *str = strdup(override->c_str());
char *p = strtok(str, ",");
/* read override values */
DEBUG_INFO_AL1(LOG_SOLVER, "read override values: %s", overrideStr);
char *p = strtok(overrideStr, ",");
while (p)
{
std::string *key_val = new string(p);
Expand Down Expand Up @@ -1089,6 +1126,8 @@ omc_ModelInput doOverride(omc_ModelInput mi, MODEL_DATA* modelData, std::string*
p = strtok(NULL, ",");
}

free(overrideStr);

// now we have all overrides in mOverrides, override mi now
mi.de["solver"] = mOverrides.count("solver") ? mOverrides["solver"] : mi.de["solver"];
mi.de["startTime"] = mOverrides.count("startTime") ? mOverrides["startTime"] : mi.de["startTime"];
Expand Down Expand Up @@ -1152,7 +1191,7 @@ omc_ModelInput doOverride(omc_ModelInput mi, MODEL_DATA* modelData, std::string*
{
mi.sAli[i]["start"] = mOverrides.count(mi.sAli[i]["name"]) ? mOverrides[mi.sAli[i]["name"]] : mi.sAli[i]["start"];
}
DEBUG_INFO_AL1(LOG_SOLVER, "override done!: %s", override->c_str());
DEBUG_INFO(LOG_SOLVER, "override done!");
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions SimulationRuntime/c/simulation/simulation_runtime.cpp
Expand Up @@ -547,6 +547,11 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data)
"startTime=val1,stopTime=val2,stepSize=val3,tolerance=val4,\n\t "
"solver=\"see -m\",outputFormat=\"mat|plt|csv|empty\",variableFilter=\"filter\"> "
"\n\t\t[override the variables or the simulation settings in the XML setup file]" << "\n\t"
<< "<-overrideFile overrideFileName"
"\n\t\t[note that: -overrideFile CANNOT be used with -override]" <<
"\n\t\t[use when variables for -override are too many and do not fit in command line size]" <<
"\n\t\t[overrideFileName contains lines of the form: var1=start1]" <<
"\n\t\t[will override the variables or the simulation settings in the XML setup file with the values from the file]" << "\n\t"
<< "<-output a,b,c>"
"\n\t\t[output the variables a, b and c at the end of the simulation to the standard output as time = value, a = value, b = value, c = value]" << "\n\t"
<< "<-noemit>"
Expand Down

0 comments on commit 0da4b68

Please sign in to comment.