Skip to content

Commit

Permalink
use the DefaultExperiment settings from modeldescription.xml (OpenMod…
Browse files Browse the repository at this point in the history
…elica#1273)

* use the DefaultExperiment settings from modeldescription.xml
* set separate working directory for test
  • Loading branch information
arun3688 committed Nov 9, 2023
1 parent 614c663 commit f7c50c9
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 51 deletions.
2 changes: 1 addition & 1 deletion include/OMSimulator/OMSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ OMSAPI oms_status_enu_t OMSCALL oms_exportDependencyGraphs(const char* cref, con
OMSAPI oms_status_enu_t OMSCALL oms_exportSnapshot(const char* cref, char** contents);
OMSAPI oms_status_enu_t OMSCALL oms_exportSSMTemplate(const char * cref, const char * filename);
OMSAPI oms_status_enu_t OMSCALL oms_exportSSVTemplate(const char* cref, const char* filename);
OMSAPI oms_status_enu_t OMSCALL oms_extractFMIKind(const char* filename, oms_fmi_kind_enu_t* kind);
OMSAPI oms_status_enu_t OMSCALL oms_extractFMIKind(const char* filename, oms_fmi_kind_enu_t* kind, oms_fmu_default_experiment_settings* defaultExperiment);
OMSAPI oms_status_enu_t OMSCALL oms_fetchExternalModelInterfaces(const char* cref, char*** names, char*** domains, int** dimensions);
OMSAPI void OMSCALL oms_freeMemory(void* obj);
OMSAPI oms_status_enu_t OMSCALL oms_getBoolean(const char* cref, bool* value);
Expand Down
10 changes: 10 additions & 0 deletions include/OMSimulator/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,16 @@ typedef struct {
unsigned int maxOutputDerivativeOrder;
} oms_fmu_info_t;

/*
* FMU default experiment settings from modeldescription.xml
*/
typedef struct {
double startTime; // default startTime for simulation
double stopTime; // default stoptTime for simulation
double tolerance; // default relative integration tolerance
double stepSize; // default stepSize
} oms_fmu_default_experiment_settings;

/**
* \brief External model specific attributes
*/
Expand Down
49 changes: 44 additions & 5 deletions src/OMSimulatorLib/OMSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,12 @@ oms_status_enu_t oms_RunFile(const char* filename)
std::string fmuName = systemName + (oms::ComRef::isValidIdent(path.stem().string()) ? ("." + path.stem().string()) : ".fmu");
oms_fmi_kind_enu_t kind;

status = oms_extractFMIKind(filename, &kind);
/* intialize the defaultExperiment with defaults set in Flags.cpp setDefaults() or
* values provided from user from commandLine (e.g) OMSimulator A.fmu --stopTime=10 --stepSize=10
*/
oms_fmu_default_experiment_settings defaultExperiment = {oms::Flags::StartTime(), oms::Flags::StopTime(), oms::Flags::Tolerance(), oms::Flags::MaximumStepSize()};

status = oms_extractFMIKind(filename, &kind, &defaultExperiment);
if (oms_status_ok != status) return logError("oms_extractFMIKind failed");

status = oms_newModel(modelName.c_str());
Expand All @@ -1204,9 +1209,12 @@ oms_status_enu_t oms_RunFile(const char* filename)

if (oms::Flags::ResultFile() != "<default>")
oms_setResultFile(modelName.c_str(), oms::Flags::ResultFile().c_str(), 1);
oms_setStartTime(modelName.c_str(), oms::Flags::StartTime());
oms_setStopTime(modelName.c_str(), oms::Flags::StopTime());
oms_setTolerance(modelName.c_str(), oms::Flags::Tolerance(), oms::Flags::Tolerance());
oms_setStartTime(modelName.c_str(), defaultExperiment.startTime);
oms_setStopTime(modelName.c_str(), defaultExperiment.stopTime);
oms_setTolerance(modelName.c_str(), defaultExperiment.tolerance, defaultExperiment.tolerance);
// set the maximum stepSize
oms_setVariableStepSize(modelName.c_str(), oms::Flags::InitialStepSize(), oms::Flags::MinimumStepSize(), defaultExperiment.stepSize);

if (kind == oms_fmi_kind_me_and_cs)
oms_setSolver(systemName.c_str(), oms::Flags::DefaultModeIsCS() ? oms::Flags::MasterAlgorithm() : oms::Flags::Solver());
else
Expand Down Expand Up @@ -1839,7 +1847,7 @@ oms_status_enu_t oms_getVariableStepSize(const char* cref, double* initialStepSi
return oms_status_ok;
}

oms_status_enu_t oms_extractFMIKind(const char* filename, oms_fmi_kind_enu_t* kind)
oms_status_enu_t oms_extractFMIKind(const char* filename, oms_fmi_kind_enu_t* kind, oms_fmu_default_experiment_settings* defaultExperiment)
{
if (!kind)
return logError("Invalid argument \"kind=NULL\"");
Expand Down Expand Up @@ -1870,6 +1878,37 @@ oms_status_enu_t oms_extractFMIKind(const char* filename, oms_fmi_kind_enu_t* ki
return oms_status_error;
}

// get default experiment settings if exists
if (node.child("DefaultExperiment"))
{
/* give priority for values provided from command line,
* if the user overrides those variables then we should take those values
* and not the default values from <DefaultExperiment> in modeldescription.xml
*/
if (defaultExperiment->startTime == 0.0)
{
if (node.child("DefaultExperiment").attribute("startTime").as_string() != "")
defaultExperiment->startTime = node.child("DefaultExperiment").attribute("startTime").as_double();
}
if (defaultExperiment->stopTime == 1.0)
{
if (node.child("DefaultExperiment").attribute("stopTime").as_string() != "")
defaultExperiment->stopTime = node.child("DefaultExperiment").attribute("stopTime").as_double();
}
if (defaultExperiment->tolerance == 1e-4)
{
if (node.child("DefaultExperiment").attribute("tolerance").as_string() != "")
defaultExperiment->tolerance = node.child("DefaultExperiment").attribute("tolerance").as_double();
}
if (defaultExperiment->stepSize == 1e-3) // maximumStepSize
{
if (node.child("DefaultExperiment").attribute("stepSize").as_string() != "")
defaultExperiment->stepSize = node.child("DefaultExperiment").attribute("stepSize").as_double();
else
defaultExperiment->stepSize = (defaultExperiment->stopTime - defaultExperiment->startTime) / 500;
}
}

return oms_status_ok;
}

Expand Down
9 changes: 5 additions & 4 deletions testsuite/simulation/replaceSubmodel1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

oms_setCommandLineOption("--suppressPath=true")
oms_setTempDirectory("./replacesubmodel_01_lua/")
oms_setWorkingDirectory("./replacesubmodel_01_lua/")

oms_newModel("model")

oms_addSystem("model.root", oms_system_wc)

oms_addSubModel("model.root.A", "../resources/replaceA.fmu")
oms_addSubModel("model.root.B", "../resources/replaceB.fmu")
oms_addSubModel("model.root.A", "../../resources/replaceA.fmu")
oms_addSubModel("model.root.B", "../../resources/replaceB.fmu")

oms_setReal("model.root.A.u", 10.0)
oms_setReal("model.root.A.t", -10.0)
Expand Down Expand Up @@ -42,7 +43,7 @@ oms_delete("model")

oms_importFile("replaceSubmodel1.ssp")

oms_replaceSubModel("model.root.A", "../resources/replaceA_extended.fmu", false)
oms_replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", false)
src, status = oms_exportSnapshot("model")
print(src)

Expand Down Expand Up @@ -309,7 +310,7 @@ oms_delete("model")
-- info: model.root.B.u1 : -13.0
-- info: model.root.B.z : -15.0
-- error: [getVariable] Unknown signal "model.root.A.dummy"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../resources/replaceA_extended.fmu"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../../resources/replaceA_extended.fmu"
-- warning: deleting start value "A.t" in "inline" resources, because the identifier couldn't be resolved to any system signal in the replacing model
-- <?xml version="1.0"?>
-- <oms:snapshot
Expand Down
9 changes: 5 additions & 4 deletions testsuite/simulation/replaceSubmodel10.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

oms_setCommandLineOption("--suppressPath=true")
oms_setTempDirectory("./replacesubmodel_10_lua/")
oms_setWorkingDirectory("./replacesubmodel_10_lua/")

oms_newModel("model")

oms_addSystem("model.root", oms_system_wc)

oms_addSubModel("model.root.A", "../resources/replaceA.fmu")
oms_addSubModel("model.root.B", "../resources/replaceB.fmu")
oms_addSubModel("model.root.A", "../../resources/replaceA.fmu")
oms_addSubModel("model.root.B", "../../resources/replaceB.fmu")

oms_newResources("model.root:root.ssv")

Expand Down Expand Up @@ -45,7 +46,7 @@ oms_delete("model")
oms_importFile("replaceSubmodel10.ssp")

-- reimport the old snapshot and replacing is not done
oms_replaceSubModel("model.root.A", "../resources/replaceA_extended.fmu", true)
oms_replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", true)
src, status = oms_exportSnapshot("model")
print(src)

Expand Down Expand Up @@ -304,7 +305,7 @@ oms_delete("model")
-- info: model.root.B.u1 : -13.0
-- info: model.root.B.z : -15.0
-- error: [getVariable] Unknown signal "model.root.A.dummy"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../resources/replaceA_extended.fmu"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../../resources/replaceA_extended.fmu"
-- warning: deleting start value "A.t" in "resources/root.ssv" resources, because the identifier couldn't be resolved to any system signal in the replacing model
-- <?xml version="1.0"?>
-- <oms:snapshot
Expand Down
7 changes: 4 additions & 3 deletions testsuite/simulation/replaceSubmodel11.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

oms.setCommandLineOption("--suppressPath=true")
oms.setTempDirectory("./replacesubmodel_11_py/")
oms.setWorkingDirectory("./replacesubmodel_11_py/")

oms.newModel("model")

oms.addSystem("model.root", oms.system_wc)

oms.addSubModel("model.root.A", "../resources/Modelica.Blocks.Sources.Sine.fmu")
oms.addSubModel("model.root.B", "../resources/Modelica.Blocks.Math.Gain.fmu")
oms.addSubModel("model.root.A", "../../resources/Modelica.Blocks.Sources.Sine.fmu")
oms.addSubModel("model.root.B", "../../resources/Modelica.Blocks.Math.Gain.fmu")

## add connections
oms.addConnection("model.root.A.y", "model.root.B.u")
Expand All @@ -33,7 +34,7 @@

oms.importFile("replaceSubmodel11.ssp")

oms.replaceSubModel("model.root.B", "../resources/Modelica.Blocks.Math.Gain.fmu", False)
oms.replaceSubModel("model.root.B", "../../resources/Modelica.Blocks.Math.Gain.fmu", False)

src, status = oms.exportSnapshot("model")
print(src)
Expand Down
9 changes: 5 additions & 4 deletions testsuite/simulation/replaceSubmodel12.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

oms.setCommandLineOption("--suppressPath=true")
oms.setTempDirectory("./replacesubmodel_12_py/")
oms.setWorkingDirectory("./replacesubmodel_12_py/")

oms.newModel("model")

oms.addSystem("model.root", oms.system_wc)

oms.addSubModel("model.root.A", "../resources/Modelica.Blocks.Sources.Sine.fmu")
oms.addSubModel("model.root.B", "../resources/Modelica.Blocks.Math.Gain.fmu")
oms.addSubModel("model.root.A", "../../resources/Modelica.Blocks.Sources.Sine.fmu")
oms.addSubModel("model.root.B", "../../resources/Modelica.Blocks.Math.Gain.fmu")

## add connections
oms.addConnection("model.root.A.y", "model.root.B.u")
Expand All @@ -33,7 +34,7 @@

oms.importFile("replaceSubmodel12.ssp")

oms.replaceSubModel("model.root.B", "../resources/Modelica.Blocks.Sources.Step.fmu", False)
oms.replaceSubModel("model.root.B", "../../resources/Modelica.Blocks.Sources.Step.fmu", False)

src, status = oms.exportSnapshot("model")
print(src)
Expand All @@ -44,7 +45,7 @@

## Result:
## error: [getVariable] Unknown signal "model.root.B.u"
## warning: deleting connection "A.y ==> B.u", as signal "u" couldn't be resolved to any signal in the replaced submodel "../resources/Modelica.Blocks.Sources.Step.fmu"
## warning: deleting connection "A.y ==> B.u", as signal "u" couldn't be resolved to any signal in the replaced submodel "../../resources/Modelica.Blocks.Sources.Step.fmu"
## <?xml version="1.0"?>
## <oms:snapshot
## xmlns:oms="https://raw.githubusercontent.com/OpenModelica/OMSimulator/master/schema/oms.xsd"
Expand Down
9 changes: 5 additions & 4 deletions testsuite/simulation/replaceSubmodel2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

oms_setCommandLineOption("--suppressPath=true")
oms_setTempDirectory("./replacesubmodel_02_lua/")
oms_setWorkingDirectory("./replacesubmodel_02_lua/")

oms_newModel("model")

oms_addSystem("model.root", oms_system_wc)

oms_addSubModel("model.root.A", "../resources/replaceA.fmu")
oms_addSubModel("model.root.B", "../resources/replaceB.fmu")
oms_addSubModel("model.root.A", "../../resources/replaceA.fmu")
oms_addSubModel("model.root.B", "../../resources/replaceB.fmu")

oms_newResources("model.root:root.ssv")

Expand Down Expand Up @@ -44,7 +45,7 @@ oms_delete("model")

oms_importFile("replaceSubmodel2.ssp")

oms_replaceSubModel("model.root.A", "../resources/replaceA_extended.fmu", false)
oms_replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", false)
src, status = oms_exportSnapshot("model")
print(src)

Expand Down Expand Up @@ -303,7 +304,7 @@ oms_delete("model")
-- info: model.root.B.u1 : -13.0
-- info: model.root.B.z : -15.0
-- error: [getVariable] Unknown signal "model.root.A.dummy"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../resources/replaceA_extended.fmu"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../../resources/replaceA_extended.fmu"
-- warning: deleting start value "A.t" in "resources/root.ssv" resources, because the identifier couldn't be resolved to any system signal in the replacing model
-- <?xml version="1.0"?>
-- <oms:snapshot
Expand Down
9 changes: 5 additions & 4 deletions testsuite/simulation/replaceSubmodel3.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

oms_setCommandLineOption("--suppressPath=true")
oms_setTempDirectory("./replacesubmodel_03_lua/")
oms_setWorkingDirectory("./replacesubmodel_03_lua/")

oms_newModel("model")

oms_addSystem("model.root", oms_system_wc)

oms_addSubModel("model.root.A", "../resources/replaceA.fmu")
oms_addSubModel("model.root.A", "../../resources/replaceA.fmu")
oms_newResources("model.root.A:replaceA.ssv")

oms_addSubModel("model.root.B", "../resources/replaceB.fmu")
oms_addSubModel("model.root.B", "../../resources/replaceB.fmu")
oms_newResources("model.root.B:replaceB.ssv")

oms_setReal("model.root.A.u", 10.0)
Expand Down Expand Up @@ -45,7 +46,7 @@ oms_delete("model")

oms_importFile("replaceSubmodel3.ssp")

oms_replaceSubModel("model.root.A", "../resources/replaceA_extended.fmu", false)
oms_replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", false)
src, status = oms_exportSnapshot("model")
print(src)

Expand Down Expand Up @@ -319,7 +320,7 @@ oms_delete("model")
-- info: model.root.B.u1 : -13.0
-- info: model.root.B.z : -15.0
-- error: [getVariable] Unknown signal "model.root.A.dummy"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../resources/replaceA_extended.fmu"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../../resources/replaceA_extended.fmu"
-- warning: deleting start value "A.t" in "resources/replaceA.ssv" resources, because the identifier couldn't be resolved to any system signal in the replacing model
-- <?xml version="1.0"?>
-- <oms:snapshot
Expand Down
7 changes: 4 additions & 3 deletions testsuite/simulation/replaceSubmodel4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

oms_setCommandLineOption("--suppressPath=true")
oms_setTempDirectory("./replacesubmodel_04_lua/")
oms_setWorkingDirectory("./replacesubmodel_04_lua/")

oms_importFile("../resources/replaceSubmodel4.ssp")
oms_importFile("../../resources/replaceSubmodel4.ssp")

src, status = oms_exportSnapshot("model")
print(src)
Expand All @@ -21,7 +22,7 @@ print("info: model.root.B.u : " .. oms_getReal("model.root.B.u"))
print("info: model.root.B.u1 : " .. oms_getReal("model.root.B.u1"))
print("info: model.root.B.z : " .. oms_getReal("model.root.B.z"))

oms_replaceSubModel("model.root.A", "../resources/replaceA_extended.fmu", false)
oms_replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", false)

src, status = oms_exportSnapshot("model")
print(src)
Expand Down Expand Up @@ -290,7 +291,7 @@ oms_delete("model")
-- info: model.root.B.u1 : -13.0
-- info: model.root.B.z : -15.0
-- error: [getVariable] Unknown signal "model.root.A.dummy"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../resources/replaceA_extended.fmu"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../../resources/replaceA_extended.fmu"
-- warning: deleting start value "A.t" in "resources/root.ssm" resources, because the identifier couldn't be resolved to any system signal in the replacing model
-- warning: deleting start value "A.t" in "resources/root.ssv" resources, because the identifier couldn't be resolved to any system signal in the replacing model
-- <?xml version="1.0"?>
Expand Down
7 changes: 4 additions & 3 deletions testsuite/simulation/replaceSubmodel5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

oms_setCommandLineOption("--suppressPath=true")
oms_setTempDirectory("./replacesubmodel_05_lua/")
oms_setWorkingDirectory("./replacesubmodel_05_lua/")

oms_importFile("../resources/replaceSubmodel5.ssp")
oms_importFile("../../resources/replaceSubmodel5.ssp")

src, status = oms_exportSnapshot("model")
print(src)
Expand All @@ -21,7 +22,7 @@ print("info: model.root.B.u : " .. oms_getReal("model.root.B.u"))
print("info: model.root.B.u1 : " .. oms_getReal("model.root.B.u1"))
print("info: model.root.B.z : " .. oms_getReal("model.root.B.z"))

oms_replaceSubModel("model.root.A", "../resources/replaceA_extended.fmu", false)
oms_replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", false)

src, status = oms_exportSnapshot("model")
print(src)
Expand Down Expand Up @@ -282,7 +283,7 @@ oms_delete("model")
-- info: model.root.B.u1 : 1.0
-- info: model.root.B.z : 1.0
-- error: [getVariable] Unknown signal "model.root.A.dummy"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../resources/replaceA_extended.fmu"
-- warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../../resources/replaceA_extended.fmu"
-- warning: deleting start value "A.t" in "resources/root.ssm" resources, because the identifier couldn't be resolved to any system signal in the replacing model
-- warning: deleting start value "A.t" in "resources/root.ssv" resources, because the identifier couldn't be resolved to any system signal in the replacing model
-- <?xml version="1.0"?>
Expand Down
9 changes: 5 additions & 4 deletions testsuite/simulation/replaceSubmodel6.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

oms.setCommandLineOption("--suppressPath=true")
oms.setTempDirectory("./replacesubmodel_06_py/")
oms.setWorkingDirectory("./replacesubmodel_06_py/")

oms.newModel("model")

oms.addSystem("model.root", oms.system_wc)

oms.addSubModel("model.root.A", "../resources/replaceA.fmu")
oms.addSubModel("model.root.B", "../resources/replaceB.fmu")
oms.addSubModel("model.root.A", "../../resources/replaceA.fmu")
oms.addSubModel("model.root.B", "../../resources/replaceB.fmu")

oms.setReal("model.root.A.u", 10.0)
oms.setReal("model.root.A.t", -10.0)
Expand Down Expand Up @@ -45,7 +46,7 @@

oms.importFile("replaceSubmodel6.ssp")

oms.replaceSubModel("model.root.A", "../resources/replaceA_extended.fmu", False)
oms.replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", False)

src, status = oms.exportSnapshot("model")
print(src)
Expand Down Expand Up @@ -81,7 +82,7 @@

## Result:
## error: [getVariable] Unknown signal "model.root.A.dummy"
## warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../resources/replaceA_extended.fmu"
## warning: deleting connection "A.dummy ==> B.u1", as signal "dummy" couldn't be resolved to any signal in the replaced submodel "../../resources/replaceA_extended.fmu"
## warning: deleting start value "A.t" in "inline" resources, because the identifier couldn't be resolved to any system signal in the replacing model
## <?xml version="1.0"?>
## <oms:snapshot
Expand Down

0 comments on commit f7c50c9

Please sign in to comment.