Skip to content

Commit

Permalink
export start values in subsystem to SSV template (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 committed Jul 12, 2022
1 parent 4c9975e commit 0040f72
Show file tree
Hide file tree
Showing 10 changed files with 536 additions and 28 deletions.
15 changes: 15 additions & 0 deletions src/OMSimulatorLib/ComponentFMUCS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,8 @@ oms_status_enu_t oms::ComponentFMUCS::newResources(const std::string& ssvFilenam
resources.ssmFile = "resources/" + ssmFilename;
// copy modeldescriptionVariableUnits to ssv resources which will be used to export units
resources.modelDescriptionVariableUnits = values.modelDescriptionVariableUnits;
// copy modeldescriptionVariableUnitDefinitions to ssv resources which will be used to export unit definitions
resources.modeldescriptionUnitDefinitions = values.modeldescriptionUnitDefinitions;
values.parameterResources[0].allresources["resources/" + ssvFilename] = resources;
}

Expand Down Expand Up @@ -1516,6 +1518,7 @@ oms_status_enu_t oms::ComponentFMUCS::setBoolean(const ComRef& cref, bool value)

if (oms_modelState_virgin == getModel().getModelState())
{
values.updateModelDescriptionBooleanStartValue(cref, value);
// check for local resources available
if (values.hasResources())
{
Expand Down Expand Up @@ -1566,6 +1569,8 @@ oms_status_enu_t oms::ComponentFMUCS::setInteger(const ComRef& cref, int value)

if (oms_modelState_virgin == getModel().getModelState())
{
// update start values in top level Modeldescription.xml to be exported in ssv templates
values.updateModelDescriptionIntegerStartValue(cref, value);
// check for local resources available
if (values.hasResources())
{
Expand Down Expand Up @@ -1619,19 +1624,24 @@ oms_status_enu_t oms::ComponentFMUCS::setReal(const ComRef& cref, double value)

if (oms_modelState_virgin == getModel().getModelState())
{
// update start values in top level Modeldescription.xml to be exported in ssv templates
values.updateModelDescriptionRealStartValue(cref, value);
// check for local resources available
if (values.hasResources())
{
values.copyModelDescriptionUnitToResources(values);
return values.setRealResources(cref, value, getFullCref(), false, oms_modelState_virgin);
}
// check for resources in root
else if (getParentSystem() && getParentSystem()->getValues().hasResources())
{
getParentSystem()->getValues().copyModelDescriptionUnitToResources(values);
return getParentSystem()->getValues().setRealResources(getCref()+cref, value, getParentSystem()->getFullCref(), false, oms_modelState_virgin);
}
// check for resources in top level root
else if (getParentSystem()->getParentSystem() && getParentSystem()->getParentSystem()->getValues().hasResources())
{
getParentSystem()->getParentSystem()->getValues().copyModelDescriptionUnitToResources(values);
return getParentSystem()->getParentSystem()->getValues().setRealResources(getCref()+cref, value, getParentSystem()->getParentSystem()->getFullCref(), false, oms_modelState_virgin);
}
else
Expand Down Expand Up @@ -1672,6 +1682,7 @@ oms_status_enu_t oms::ComponentFMUCS::setString(const ComRef& cref, const std::s

if (oms_modelState_virgin == getModel().getModelState())
{
values.updateModelDescriptionStringStartValue(cref, value);
// check for local resources available
if (values.hasResources())
{
Expand Down Expand Up @@ -1718,6 +1729,10 @@ oms_status_enu_t oms::ComponentFMUCS::setUnit(const ComRef &cref, const std::str
}
}
}

// set unit in top level modeldescription.xml
values.updateModelDescriptionVariableUnit(cref, value);

// check for local resources available
if (values.hasResources())
{
Expand Down
1 change: 1 addition & 0 deletions src/OMSimulatorLib/ComponentFMUCS.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ namespace oms

oms_status_enu_t deleteReferencesInSSD(const std::string& filename);
oms_status_enu_t deleteResourcesInSSP(const std::string& filename);
void copyModelDescriptionUnit(Values& value);

protected:
ComponentFMUCS(const ComRef& cref, System* parentSystem, const std::string& fmuPath);
Expand Down
15 changes: 15 additions & 0 deletions src/OMSimulatorLib/ComponentFMUME.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,8 @@ oms_status_enu_t oms::ComponentFMUME::setBoolean(const ComRef& cref, bool value)

if (oms_modelState_virgin == getModel().getModelState())
{
// update start values in top level Modeldescription.xml to be exported in ssv templates
values.updateModelDescriptionBooleanStartValue(cref, value);
// check for local resources available
if (values.hasResources())
{
Expand Down Expand Up @@ -1503,6 +1505,8 @@ oms_status_enu_t oms::ComponentFMUME::setInteger(const ComRef& cref, int value)

if (oms_modelState_virgin == getModel().getModelState())
{
// update start values in top level Modeldescription.xml to be exported in ssv templates
values.updateModelDescriptionIntegerStartValue(cref, value);
// check for local resources available
if (values.hasResources())
{
Expand Down Expand Up @@ -1581,19 +1585,24 @@ oms_status_enu_t oms::ComponentFMUME::setReal(const ComRef& cref, double value)

if (oms_modelState_virgin == getModel().getModelState())
{
// update start values in top level Modeldescription.xml to be exported in ssv templates
values.updateModelDescriptionRealStartValue(cref, value);
// check for local resources available
if (values.hasResources())
{
values.copyModelDescriptionUnitToResources(values);
return values.setRealResources(cref, value, getFullCref(), false, oms_modelState_virgin);
}
// check for resources in root
else if (getParentSystem() && getParentSystem()->getValues().hasResources())
{
getParentSystem()->getValues().copyModelDescriptionUnitToResources(values);
return getParentSystem()->getValues().setRealResources(getCref()+cref, value, getParentSystem()->getFullCref(), false, oms_modelState_virgin);
}
// check for resources in top level root
else if (getParentSystem()->getParentSystem() && getParentSystem()->getParentSystem()->getValues().hasResources())
{
getParentSystem()->getParentSystem()->getValues().copyModelDescriptionUnitToResources(values);
return getParentSystem()->getParentSystem()->getValues().setRealResources(getCref()+cref, value, getParentSystem()->getParentSystem()->getFullCref(), false, oms_modelState_virgin);
}
else
Expand Down Expand Up @@ -1634,6 +1643,8 @@ oms_status_enu_t oms::ComponentFMUME::setString(const ComRef& cref, const std::s

if (oms_modelState_virgin == getModel().getModelState())
{
// update start values in top level Modeldescription.xml to be exported in ssv templates
values.updateModelDescriptionStringStartValue(cref, value);
// check for local resources available
if (values.hasResources())
{
Expand Down Expand Up @@ -1680,6 +1691,10 @@ oms_status_enu_t oms::ComponentFMUME::setUnit(const ComRef &cref, const std::str
}
}
}

// set unit in top level modeldescription.xml
values.updateModelDescriptionVariableUnit(cref, value);

// check for local resources available
if (values.hasResources())
{
Expand Down
21 changes: 20 additions & 1 deletion src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ oms_status_enu_t oms::Model::exportSSVTemplate(const oms::ComRef& cref, const st

for (const auto& component : system->getComponents())
{
if(component.first == tail || cref.isEmpty()) // allow querying single component or whole model
if(component.first == tail || cref.isEmpty() || cref.isValidIdent()) // allow querying single component or whole model
{
if (oms_status_ok != component.second->exportToSSVTemplate(ssvNode, snapshot))
{
Expand All @@ -417,6 +417,25 @@ oms_status_enu_t oms::Model::exportSSVTemplate(const oms::ComRef& cref, const st
}
}

// check in subsystems
ComRef tailA(tail);
ComRef frontA = tailA.pop_front();

//std::cout <<"\n subsystem: " << tailA.c_str() << "==>" << frontA.c_str() << "==>" << cref.c_str();
for (const auto &subsytem : system->getSubSystems())
{
for (const auto &component : subsytem.second->getComponents())
{
if (component.first == tailA || cref.isEmpty() || (system->getSystem(frontA) && tailA.isEmpty()))
{
if (oms_status_ok != component.second->exportToSSVTemplate(ssvNode, snapshot))
{
return logError("export of ssv template failed for component " + std::string(system->getFullCref() + std::string(component.first)));
}
}
}
}

//ssvdoc.save(std::cout);
ssvdoc.append_copy(snapshot.getResourceNode("template.ssv"));

Expand Down
68 changes: 49 additions & 19 deletions src/OMSimulatorLib/Values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ oms::Values::~Values()
oms_status_enu_t oms::Values::setReal(const ComRef& cref, double value)
{
realStartValues[cref] = value;

// update start values in ssv template
auto realValue = modelDescriptionRealStartValues.find(cref);
if (realValue != modelDescriptionRealStartValues.end())
modelDescriptionRealStartValues[cref] = value;

setUnitDefinitions(cref);

return oms_status_ok;
Expand Down Expand Up @@ -105,43 +99,79 @@ oms_status_enu_t oms::Values::setInteger(const ComRef& cref, int value)
{
integerStartValues[cref] = value;

// update start values in ssv template
auto integerValue = modelDescriptionIntegerStartValues.find(cref);
if (integerValue != modelDescriptionIntegerStartValues.end())
modelDescriptionIntegerStartValues[cref] = value;

return oms_status_ok;
}

oms_status_enu_t oms::Values::setBoolean(const ComRef& cref, bool value)
{
booleanStartValues[cref] = value;

// update start values in ssv template
auto boolValue = modelDescriptionBooleanStartValues.find(cref);
if (boolValue != modelDescriptionBooleanStartValues.end())
modelDescriptionBooleanStartValues[cref] = value;

return oms_status_ok;
}

oms_status_enu_t oms::Values::setString(const ComRef& cref, const std::string& value)
{
stringStartValues[cref] = value;

return oms_status_ok;
}

void oms::Values::updateModelDescriptionRealStartValue(const ComRef& cref, double value)
{
// update start values in ssv template
auto realValue = modelDescriptionRealStartValues.find(cref);
if (realValue != modelDescriptionRealStartValues.end())
modelDescriptionRealStartValues[cref] = value;
}

void oms::Values::updateModelDescriptionIntegerStartValue(const ComRef& cref, int value)
{
// update start values in ssv template
auto integerValue = modelDescriptionIntegerStartValues.find(cref);
if (integerValue != modelDescriptionIntegerStartValues.end())
modelDescriptionIntegerStartValues[cref] = value;
}

void oms::Values::updateModelDescriptionBooleanStartValue(const ComRef& cref, bool value)
{
// update start values in ssv template
auto boolValue = modelDescriptionBooleanStartValues.find(cref);
if (boolValue != modelDescriptionBooleanStartValues.end())
modelDescriptionBooleanStartValues[cref] = value;
}

void oms::Values::updateModelDescriptionStringStartValue(const ComRef& cref, std::string value)
{
// update start values in ssv template
auto stringValue = modelDescriptionStringStartValues.find(cref);
if (stringValue != modelDescriptionStringStartValues.end())
modelDescriptionStringStartValues[cref] = value;
}

return oms_status_ok;
void oms::Values::updateModelDescriptionVariableUnit(const ComRef& cref, const std::string& value)
{
modelDescriptionVariableUnits[cref] = value; // override if exists otherwise make a new entry
// update unit Definitions
for (const auto & it:modeldescriptionUnitDefinitions)
if (it.first == cref.c_str())
modeldescriptionUnitDefinitions[it.first] = {}; // make the baseUnit empty, as we do not calculate base units
}

void oms::Values::copyModelDescriptionUnitToResources(Values& value)
{
for (auto &it: parameterResources)
{
for (auto &res: it.allresources)
{
res.second.modelDescriptionVariableUnits = value.modelDescriptionVariableUnits;
res.second.modeldescriptionUnitDefinitions = value.modeldescriptionUnitDefinitions;
}
}
}

oms_status_enu_t oms::Values::setUnit(const ComRef& cref, const std::string& value)
{
variableUnits[cref] = value;
// update unit values in ssv template
modelDescriptionVariableUnits[cref] = value; // override if exists otherwise make a new entry
// update unit Definitions
for (auto & it : unitDefinitionsToExportInSSP)
{
Expand Down
7 changes: 7 additions & 0 deletions src/OMSimulatorLib/Values.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ namespace oms
oms_status_enu_t setUnit(const ComRef& cref, const std::string& value);
void setUnitDefinitions(const ComRef& cref);
void getFilteredUnitDefinitionsToSSD(std::map<std::string, std::map<std::string, std::string>>& unitDefinitions);
void updateModelDescriptionRealStartValue(const ComRef& cref, double value);
void updateModelDescriptionIntegerStartValue(const ComRef& cref, int value);
void updateModelDescriptionBooleanStartValue(const ComRef& cref, bool value);
void updateModelDescriptionStringStartValue(const ComRef& cref, std::string value);

void updateModelDescriptionVariableUnit(const ComRef& cref, const std::string& value);
void copyModelDescriptionUnitToResources(Values& value);

oms_status_enu_t getBoolean(const ComRef& cref, bool& value);
oms_status_enu_t getInteger(const ComRef& cref, int& value);
Expand Down
5 changes: 3 additions & 2 deletions testsuite/export/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
TEST = ../rtest -v

TESTFILES = \
exportSSVTemplate.lua \
exportSSVTemplate.py \
exportSSVTemplate1.lua \
exportSSVTemplate2.py \
exportSSVTemplate3.lua \

# Run make failingtest
FAILINGTESTFILES = \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
-- status: correct
-- teardown_command: rm -rf exportSSVTemplate_lua/
-- teardown_command: rm -rf exportSSVTemplate_01_lua/
-- linux: yes
-- mingw32: yes
-- mingw64: yes
-- win: yes
-- mac: yes

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

function readFile(filename)
local f = assert(io.open(filename, "r"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## status: correct
## teardown_command: rm -rf exportSSVTemplate_py/
## teardown_command: rm -rf exportSSVTemplate_02_py/
## linux: yes
## mac: no
## mingw32: yes
Expand All @@ -9,8 +9,8 @@
import OMSimulator as oms

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

model = oms.newModel("exportSSVTemplate")
root = model.addSystem("root", oms.Types.System.WC)
Expand Down

0 comments on commit 0040f72

Please sign in to comment.