Skip to content

Commit

Permalink
fix replaceSubModel from memory (OpenModelica#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 committed Oct 10, 2023
1 parent 54ebbdc commit e34d2b9
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/OMSimulatorLib/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ namespace oms
virtual const FMUInfo* getFMUInfo() const { return nullptr; }
virtual oms_status_enu_t deleteStartValue(const ComRef& cref) { return oms_status_ok; }
virtual std::vector<Values> getValuesResources() { return{}; }
virtual oms_status_enu_t setValuesResources(std::vector<Values>& allResources) { return oms_status_ok; }
virtual oms_status_enu_t updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList) { return oms_status_ok; }
virtual Values& getValues() {return values;}
virtual oms_status_enu_t setValuesResources(Values& values) { return oms_status_ok; }
virtual oms_status_enu_t updateOrDeleteStartValueInReplacedComponent(std::vector<std::string> &warningList) { return oms_status_ok; }
virtual oms_status_enu_t exportToSSMTemplate(pugi::xml_node& ssmNode) { return logError_NotImplemented; }
virtual oms_status_enu_t exportToSSVTemplate(pugi::xml_node& ssvNode, Snapshot& snapshot) { return logError_NotImplemented; }
virtual oms_status_enu_t freeState() { return logError_NotImplemented; }
Expand Down Expand Up @@ -165,6 +166,7 @@ namespace oms
oms_component_enu_t type;
std::string path; ///< resource file (fmu, mat)
std::string tempDir; ///< unzipped fmu
Values values;
};
}

Expand Down
16 changes: 9 additions & 7 deletions src/OMSimulatorLib/ComponentFMUCS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1716,15 +1716,17 @@ oms_status_enu_t oms::ComponentFMUCS::deleteStartValue(const ComRef& cref)
return oms_status_error;
}

oms_status_enu_t oms::ComponentFMUCS::setValuesResources(std::vector<Values>& allValuesResources)
oms_status_enu_t oms::ComponentFMUCS::setValuesResources(Values& values)
{
this->values.parameterResources = allValuesResources;
return oms_status_ok;
}
// set all ssv and ssm resources from the old component to replacing component
this->values.parameterResources = values.parameterResources;
// set all user define values from the old component to replacing component as user defined values have higher priority
// over modeldescription.xml
this->values.realStartValues = values.realStartValues;
this->values.integerStartValues = values.integerValues;
this->values.booleanStartValues = values.booleanStartValues;

std::vector<oms::Values> oms::ComponentFMUCS::getValuesResources()
{
return this->values.parameterResources;
return oms_status_ok;
}

oms_status_enu_t oms::ComponentFMUCS::updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList)
Expand Down
5 changes: 3 additions & 2 deletions src/OMSimulatorLib/ComponentFMUCS.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ namespace oms

Variable* getVariable(const ComRef& cref);

Values& getValues() { return values; }
oms_status_enu_t setValuesResources(Values& values);

oms_status_enu_t getBoolean(const ComRef& cref, bool& value);
oms_status_enu_t getBoolean(const fmi2ValueReference& vr, bool& value);
oms_status_enu_t getInteger(const ComRef& cref, int& value);
Expand All @@ -96,8 +99,6 @@ namespace oms

oms_status_enu_t deleteStartValue(const ComRef& cref);
oms_status_enu_t updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList);
oms_status_enu_t setValuesResources(std::vector<Values>& allValuesResources);
std::vector<Values> getValuesResources();

oms_status_enu_t setFmuTime(double time) {this->time = time; return oms_status_ok;}
fmiHandle* getFMU() {return fmu;}
Expand Down
16 changes: 9 additions & 7 deletions src/OMSimulatorLib/ComponentFMUME.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,15 +1529,17 @@ oms_status_enu_t oms::ComponentFMUME::deleteStartValue(const ComRef& cref)
return oms_status_error;
}

oms_status_enu_t oms::ComponentFMUME::setValuesResources(std::vector<Values>& allValuesResources)
oms_status_enu_t oms::ComponentFMUME::setValuesResources(Values& values)
{
this->values.parameterResources = allValuesResources;
return oms_status_ok;
}
// set all ssv and ssm resources from the old component to replacing component
this->values.parameterResources = values.parameterResources;
// set all user define values from the old component to replacing component as user defined values have higher priority
// over modeldescription.xml
this->values.realStartValues = values.realStartValues;
this->values.integerStartValues = values.integerValues;
this->values.booleanStartValues = values.booleanStartValues;

std::vector<oms::Values> oms::ComponentFMUME::getValuesResources()
{
return this->values.parameterResources;
return oms_status_ok;
}

oms_status_enu_t oms::ComponentFMUME::updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList)
Expand Down
5 changes: 3 additions & 2 deletions src/OMSimulatorLib/ComponentFMUME.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ namespace oms

Variable* getVariable(const ComRef& cref);

Values& getValues() { return values; }
oms_status_enu_t setValuesResources(Values& values);

oms_status_enu_t getBoolean(const ComRef& cref, bool& value);
oms_status_enu_t getBoolean(const fmi2ValueReference& vr, bool& value);
oms_status_enu_t getInteger(const ComRef& cref, int& value);
Expand All @@ -92,8 +95,6 @@ namespace oms

oms_status_enu_t deleteStartValue(const ComRef& cref);
oms_status_enu_t updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList);
oms_status_enu_t setValuesResources(std::vector<Values>& allValuesResources);
std::vector<Values> getValuesResources();

oms_status_enu_t registerSignalsForResultFile(ResultWriter& resultFile);
oms_status_enu_t updateSignals(ResultWriter& resultWriter);
Expand Down
3 changes: 1 addition & 2 deletions src/OMSimulatorLib/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,7 @@ oms_status_enu_t oms::System::replaceSubModel(const oms::ComRef& cref, const std
replaceComponent->getElement()->setGeometry(component->second->getElement()->getGeometry());

// copy all the resources from old component to replacing component
std::vector<Values> allResources = component->second->getValuesResources();
replaceComponent->setValuesResources(allResources);
replaceComponent->setValuesResources(component->second->getValues());

// update or delete the start value in ssv of with the replaced component
replaceComponent->updateOrDeleteStartValueInReplacedComponent(warningList);
Expand Down
2 changes: 2 additions & 0 deletions testsuite/simulation/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ replaceSubmodel9.lua \
replaceSubmodel10.lua \
replaceSubmodel11.py \
replaceSubmodel12.py \
replaceSubmodel13.py \
replaceSubmodel14.py \
setExternalInputs.lua \
simulation.lua \
simulation.py \
Expand Down
44 changes: 44 additions & 0 deletions testsuite/simulation/replaceSubmodel13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## status: correct
## teardown_command: rm -rf replacesubmodel_13_py/
## linux: no
## mingw32: no
## mingw64: yes
## win: no
## mac: no

from OMSimulator import OMSimulator
oms = OMSimulator()

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

oms.newModel("model")

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

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

## priority over modeldescription.xml
oms.setReal("model.root.A.u", 10.0)

print("info: Before replacing the Model")
print("info: model.root.A.u : " , oms.getReal("model.root.A.u")[0])

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

# src, status = oms.exportSnapshot("model")
# print(src)

print("info: After replacing the Model")
print("info: model.root.A.u : " , oms.getReal("model.root.A.u")[0])

oms.terminate("model")
oms.delete("model")

## Result:
## info: Before replacing the Model
## info: model.root.A.u : 10.0
## info: After replacing the Model
## info: model.root.A.u : 10.0
## endResult
39 changes: 39 additions & 0 deletions testsuite/simulation/replaceSubmodel14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## status: correct
## teardown_command: rm -rf replacesubmodel_14_py/
## linux: no
## mingw32: no
## mingw64: yes
## win: no
## mac: no

from OMSimulator import OMSimulator
oms = OMSimulator()

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

oms.newModel("model")

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

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

print("info: Before replacing the Model")
print("info: model.root.A.u : " , oms.getReal("model.root.A.u")[0])

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

## no priority exists read the new value from modeldescription.xml from the replaced component
print("info: After replacing the Model")
print("info: model.root.A.u : " , oms.getReal("model.root.A.u")[0])

oms.terminate("model")
oms.delete("model")

## Result:
## info: Before replacing the Model
## info: model.root.A.u : 5.0
## info: After replacing the Model
## info: model.root.A.u : 7.0
## endResult

0 comments on commit e34d2b9

Please sign in to comment.