Skip to content

Commit e34d2b9

Browse files
authored
fix replaceSubModel from memory (#1264)
1 parent 54ebbdc commit e34d2b9

9 files changed

Lines changed: 114 additions & 22 deletions

File tree

src/OMSimulatorLib/Component.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ namespace oms
7979
virtual const FMUInfo* getFMUInfo() const { return nullptr; }
8080
virtual oms_status_enu_t deleteStartValue(const ComRef& cref) { return oms_status_ok; }
8181
virtual std::vector<Values> getValuesResources() { return{}; }
82-
virtual oms_status_enu_t setValuesResources(std::vector<Values>& allResources) { return oms_status_ok; }
83-
virtual oms_status_enu_t updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList) { return oms_status_ok; }
82+
virtual Values& getValues() {return values;}
83+
virtual oms_status_enu_t setValuesResources(Values& values) { return oms_status_ok; }
84+
virtual oms_status_enu_t updateOrDeleteStartValueInReplacedComponent(std::vector<std::string> &warningList) { return oms_status_ok; }
8485
virtual oms_status_enu_t exportToSSMTemplate(pugi::xml_node& ssmNode) { return logError_NotImplemented; }
8586
virtual oms_status_enu_t exportToSSVTemplate(pugi::xml_node& ssvNode, Snapshot& snapshot) { return logError_NotImplemented; }
8687
virtual oms_status_enu_t freeState() { return logError_NotImplemented; }
@@ -165,6 +166,7 @@ namespace oms
165166
oms_component_enu_t type;
166167
std::string path; ///< resource file (fmu, mat)
167168
std::string tempDir; ///< unzipped fmu
169+
Values values;
168170
};
169171
}
170172

src/OMSimulatorLib/ComponentFMUCS.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,15 +1716,17 @@ oms_status_enu_t oms::ComponentFMUCS::deleteStartValue(const ComRef& cref)
17161716
return oms_status_error;
17171717
}
17181718

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

1725-
std::vector<oms::Values> oms::ComponentFMUCS::getValuesResources()
1726-
{
1727-
return this->values.parameterResources;
1729+
return oms_status_ok;
17281730
}
17291731

17301732
oms_status_enu_t oms::ComponentFMUCS::updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList)

src/OMSimulatorLib/ComponentFMUCS.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ namespace oms
7777

7878
Variable* getVariable(const ComRef& cref);
7979

80+
Values& getValues() { return values; }
81+
oms_status_enu_t setValuesResources(Values& values);
82+
8083
oms_status_enu_t getBoolean(const ComRef& cref, bool& value);
8184
oms_status_enu_t getBoolean(const fmi2ValueReference& vr, bool& value);
8285
oms_status_enu_t getInteger(const ComRef& cref, int& value);
@@ -96,8 +99,6 @@ namespace oms
9699

97100
oms_status_enu_t deleteStartValue(const ComRef& cref);
98101
oms_status_enu_t updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList);
99-
oms_status_enu_t setValuesResources(std::vector<Values>& allValuesResources);
100-
std::vector<Values> getValuesResources();
101102

102103
oms_status_enu_t setFmuTime(double time) {this->time = time; return oms_status_ok;}
103104
fmiHandle* getFMU() {return fmu;}

src/OMSimulatorLib/ComponentFMUME.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,15 +1529,17 @@ oms_status_enu_t oms::ComponentFMUME::deleteStartValue(const ComRef& cref)
15291529
return oms_status_error;
15301530
}
15311531

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

1538-
std::vector<oms::Values> oms::ComponentFMUME::getValuesResources()
1539-
{
1540-
return this->values.parameterResources;
1542+
return oms_status_ok;
15411543
}
15421544

15431545
oms_status_enu_t oms::ComponentFMUME::updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList)

src/OMSimulatorLib/ComponentFMUME.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ namespace oms
7373

7474
Variable* getVariable(const ComRef& cref);
7575

76+
Values& getValues() { return values; }
77+
oms_status_enu_t setValuesResources(Values& values);
78+
7679
oms_status_enu_t getBoolean(const ComRef& cref, bool& value);
7780
oms_status_enu_t getBoolean(const fmi2ValueReference& vr, bool& value);
7881
oms_status_enu_t getInteger(const ComRef& cref, int& value);
@@ -92,8 +95,6 @@ namespace oms
9295

9396
oms_status_enu_t deleteStartValue(const ComRef& cref);
9497
oms_status_enu_t updateOrDeleteStartValueInReplacedComponent(std::vector<std::string>& warningList);
95-
oms_status_enu_t setValuesResources(std::vector<Values>& allValuesResources);
96-
std::vector<Values> getValuesResources();
9798

9899
oms_status_enu_t registerSignalsForResultFile(ResultWriter& resultFile);
99100
oms_status_enu_t updateSignals(ResultWriter& resultWriter);

src/OMSimulatorLib/System.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@ oms_status_enu_t oms::System::replaceSubModel(const oms::ComRef& cref, const std
399399
replaceComponent->getElement()->setGeometry(component->second->getElement()->getGeometry());
400400

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

405404
// update or delete the start value in ssv of with the replaced component
406405
replaceComponent->updateOrDeleteStartValueInReplacedComponent(warningList);

testsuite/simulation/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ replaceSubmodel9.lua \
7474
replaceSubmodel10.lua \
7575
replaceSubmodel11.py \
7676
replaceSubmodel12.py \
77+
replaceSubmodel13.py \
78+
replaceSubmodel14.py \
7779
setExternalInputs.lua \
7880
simulation.lua \
7981
simulation.py \
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## status: correct
2+
## teardown_command: rm -rf replacesubmodel_13_py/
3+
## linux: no
4+
## mingw32: no
5+
## mingw64: yes
6+
## win: no
7+
## mac: no
8+
9+
from OMSimulator import OMSimulator
10+
oms = OMSimulator()
11+
12+
oms.setCommandLineOption("--suppressPath=true")
13+
oms.setTempDirectory("./replacesubmodel_13_py/")
14+
oms.setWorkingDirectory("./replacesubmodel_13_py/")
15+
16+
oms.newModel("model")
17+
18+
oms.addSystem("model.root", oms.system_wc)
19+
20+
oms.addSubModel("model.root.A", "../../resources/replaceA.fmu")
21+
22+
## priority over modeldescription.xml
23+
oms.setReal("model.root.A.u", 10.0)
24+
25+
print("info: Before replacing the Model")
26+
print("info: model.root.A.u : " , oms.getReal("model.root.A.u")[0])
27+
28+
oms.replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", False)
29+
30+
# src, status = oms.exportSnapshot("model")
31+
# print(src)
32+
33+
print("info: After replacing the Model")
34+
print("info: model.root.A.u : " , oms.getReal("model.root.A.u")[0])
35+
36+
oms.terminate("model")
37+
oms.delete("model")
38+
39+
## Result:
40+
## info: Before replacing the Model
41+
## info: model.root.A.u : 10.0
42+
## info: After replacing the Model
43+
## info: model.root.A.u : 10.0
44+
## endResult
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## status: correct
2+
## teardown_command: rm -rf replacesubmodel_14_py/
3+
## linux: no
4+
## mingw32: no
5+
## mingw64: yes
6+
## win: no
7+
## mac: no
8+
9+
from OMSimulator import OMSimulator
10+
oms = OMSimulator()
11+
12+
oms.setCommandLineOption("--suppressPath=true")
13+
oms.setTempDirectory("./replacesubmodel_14_py/")
14+
oms.setWorkingDirectory("./replacesubmodel_14_py/")
15+
16+
oms.newModel("model")
17+
18+
oms.addSystem("model.root", oms.system_wc)
19+
20+
oms.addSubModel("model.root.A", "../../resources/replaceA.fmu")
21+
22+
print("info: Before replacing the Model")
23+
print("info: model.root.A.u : " , oms.getReal("model.root.A.u")[0])
24+
25+
oms.replaceSubModel("model.root.A", "../../resources/replaceA_extended.fmu", False)
26+
27+
## no priority exists read the new value from modeldescription.xml from the replaced component
28+
print("info: After replacing the Model")
29+
print("info: model.root.A.u : " , oms.getReal("model.root.A.u")[0])
30+
31+
oms.terminate("model")
32+
oms.delete("model")
33+
34+
## Result:
35+
## info: Before replacing the Model
36+
## info: model.root.A.u : 5.0
37+
## info: After replacing the Model
38+
## info: model.root.A.u : 7.0
39+
## endResult

0 commit comments

Comments
 (0)