Skip to content

Commit 495b3a7

Browse files
authored
Add setString to c and python api (#1125)
1 parent e610ad2 commit 495b3a7

File tree

14 files changed

+245
-6
lines changed

14 files changed

+245
-6
lines changed

src/OMSimulatorLib/Component.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ namespace oms
9191
virtual oms_status_enu_t setFaultInjection(const ComRef& signal, oms_fault_type_enu_t faultType, double faultValue) { return oms_status_error; }
9292
virtual oms_status_enu_t setInteger(const ComRef& cref, int value) { return logError_NotImplemented; }
9393
virtual oms_status_enu_t setReal(const ComRef& cref, double value) { return logError_NotImplemented; }
94+
virtual oms_status_enu_t setString(const ComRef& cref, const std::string& value) { return logError_NotImplemented; }
9495
virtual oms_status_enu_t setRealInputDerivative(const ComRef& cref, const SignalDerivative& der) { return logError_NotImplemented; }
9596
virtual oms_status_enu_t stepUntil(double stopTime) { return oms_status_ok; }
9697
virtual oms_status_enu_t newResources(const std::string& ssvFileName, const std::string& ssmFileName, bool externalResources) { return logError_NotImplemented; }

src/OMSimulatorLib/ComponentFMUCS.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,60 @@ oms_status_enu_t oms::ComponentFMUCS::setReal(const ComRef& cref, double value)
15571557
return oms_status_ok;
15581558
}
15591559

1560+
oms_status_enu_t oms::ComponentFMUCS::setString(const ComRef& cref, const std::string& value)
1561+
{
1562+
CallClock callClock(clock);
1563+
int j=-1;
1564+
for (size_t i = 0; i < allVariables.size(); i++)
1565+
{
1566+
if (allVariables[i].getCref() == cref && allVariables[i].isTypeString())
1567+
{
1568+
j = i;
1569+
break;
1570+
}
1571+
}
1572+
1573+
if (!fmu || j < 0)
1574+
return logError_UnknownSignal(getFullCref() + cref);
1575+
1576+
if (getModel().validState(oms_modelState_virgin|oms_modelState_enterInstantiation|oms_modelState_instantiated))
1577+
if (allVariables[j].isCalculated() || allVariables[j].isIndependent())
1578+
return logWarning("It is not allowed to provide a start value if initial=\"calculated\" or causality=\"independent\".");
1579+
1580+
if (oms_modelState_virgin == getModel().getModelState())
1581+
{
1582+
// check for local resources available
1583+
if (values.hasResources())
1584+
{
1585+
return values.setStringResources(cref, value, getFullCref(), false, oms_modelState_virgin);
1586+
}
1587+
// check for resources in root
1588+
else if (getParentSystem() && getParentSystem()->getValues().hasResources())
1589+
{
1590+
return getParentSystem()->getValues().setStringResources(getCref()+cref, value, getParentSystem()->getFullCref(), false, oms_modelState_virgin);
1591+
}
1592+
// check for resources in top level root
1593+
else if (getParentSystem()->getParentSystem() && getParentSystem()->getParentSystem()->getValues().hasResources())
1594+
{
1595+
return getParentSystem()->getParentSystem()->getValues().setStringResources(getCref()+cref, value, getParentSystem()->getParentSystem()->getFullCref(), false, oms_modelState_virgin);
1596+
}
1597+
else
1598+
{
1599+
//inline parameter settings
1600+
values.setString(cref, value);
1601+
}
1602+
}
1603+
else
1604+
{
1605+
fmi2_value_reference_t vr = allVariables[j].getValueReference();
1606+
fmi2_string_t value_ = value.c_str();
1607+
if (fmi2_status_ok != fmi2_import_set_string(fmu, &vr, 1, &value_))
1608+
return oms_status_error;
1609+
}
1610+
1611+
return oms_status_ok;
1612+
}
1613+
15601614
oms_status_enu_t oms::ComponentFMUCS::deleteStartValue(const ComRef& cref)
15611615
{
15621616
// check for local resources

src/OMSimulatorLib/ComponentFMUCS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ namespace oms
8787
oms_status_enu_t setBoolean(const ComRef& cref, bool value);
8888
oms_status_enu_t setInteger(const ComRef& cref, int value);
8989
oms_status_enu_t setReal(const ComRef& cref, double value);
90+
oms_status_enu_t setString(const ComRef& cref, const std::string& value);
9091

9192
oms_status_enu_t getDirectionalDerivative(const ComRef& unknownCref, const ComRef& knownCref, double& value);
9293
oms_status_enu_t getDirectionalDerivativeHeper(const int unknownIndex, const int knownindex, const std::vector<int>& dependencyList, double& value);

src/OMSimulatorLib/ComponentFMUME.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,60 @@ oms_status_enu_t oms::ComponentFMUME::setReal(const ComRef& cref, double value)
15201520
return oms_status_ok;
15211521
}
15221522

1523+
oms_status_enu_t oms::ComponentFMUME::setString(const ComRef& cref, const std::string& value)
1524+
{
1525+
CallClock callClock(clock);
1526+
int j=-1;
1527+
for (size_t i = 0; i < allVariables.size(); i++)
1528+
{
1529+
if (allVariables[i].getCref() == cref && allVariables[i].isTypeString())
1530+
{
1531+
j = i;
1532+
break;
1533+
}
1534+
}
1535+
1536+
if (!fmu || j < 0)
1537+
return logError_UnknownSignal(getFullCref() + cref);
1538+
1539+
if (getModel().validState(oms_modelState_virgin|oms_modelState_enterInstantiation|oms_modelState_instantiated))
1540+
if (allVariables[j].isCalculated() || allVariables[j].isIndependent())
1541+
return logWarning("It is not allowed to provide a start value if initial=\"calculated\" or causality=\"independent\".");
1542+
1543+
if (oms_modelState_virgin == getModel().getModelState())
1544+
{
1545+
// check for local resources available
1546+
if (values.hasResources())
1547+
{
1548+
return values.setStringResources(cref, value, getFullCref(), false, oms_modelState_virgin);
1549+
}
1550+
// check for resources in root
1551+
else if (getParentSystem() && getParentSystem()->getValues().hasResources())
1552+
{
1553+
return getParentSystem()->getValues().setStringResources(getCref()+cref, value, getParentSystem()->getFullCref(), false, oms_modelState_virgin);
1554+
}
1555+
// check for resources in top level root
1556+
else if (getParentSystem()->getParentSystem() && getParentSystem()->getParentSystem()->getValues().hasResources())
1557+
{
1558+
return getParentSystem()->getParentSystem()->getValues().setStringResources(getCref()+cref, value, getParentSystem()->getParentSystem()->getFullCref(), false, oms_modelState_virgin);
1559+
}
1560+
else
1561+
{
1562+
//inline parameter settings
1563+
values.setString(cref, value);
1564+
}
1565+
}
1566+
else
1567+
{
1568+
fmi2_value_reference_t vr = allVariables[j].getValueReference();
1569+
fmi2_string_t value_ = value.c_str();
1570+
if (fmi2_status_ok != fmi2_import_set_string(fmu, &vr, 1, &value_))
1571+
return oms_status_error;
1572+
}
1573+
1574+
return oms_status_ok;
1575+
}
1576+
15231577
oms_status_enu_t oms::ComponentFMUME::registerSignalsForResultFile(ResultWriter& resultFile)
15241578
{
15251579
resultFileMapping.clear();

src/OMSimulatorLib/ComponentFMUME.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ namespace oms
8282
oms_status_enu_t setBoolean(const ComRef& cref, bool value);
8383
oms_status_enu_t setInteger(const ComRef& cref, int value);
8484
oms_status_enu_t setReal(const ComRef& cref, double value);
85+
oms_status_enu_t setString(const ComRef& cref, const std::string& value);
8586

8687
oms_status_enu_t getDirectionalDerivative(const ComRef& unknownCref, const ComRef& knownCref, double& value);
8788
oms_status_enu_t getDirectionalDerivativeHeper(const int unknownIndex, const int knownIndex, const std::vector<int>& dependencyList, double& value);

src/OMSimulatorLib/OMSimulator.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,23 @@ oms_status_enu_t oms_setStopTime(const char* cref, double stopTime)
15751575
return model->setStopTime(stopTime);
15761576
}
15771577

1578+
oms_status_enu_t oms_setString(const char* cref, const char* value)
1579+
{
1580+
oms::ComRef tail(cref);
1581+
oms::ComRef front = tail.pop_front();
1582+
1583+
oms::Model* model = oms::Scope::GetInstance().getModel(front);
1584+
if (!model)
1585+
return logError_ModelNotInScope(front);
1586+
1587+
front = tail.pop_front();
1588+
oms::System* system = model->getSystem(front);
1589+
if (!system)
1590+
return logError_SystemNotInModel(model->getCref(), front);
1591+
1592+
return system->setString(tail, value);
1593+
}
1594+
15781595
oms_status_enu_t oms_getTime(const char* cref, double* time)
15791596
{
15801597
oms::ComRef tail(cref);

src/OMSimulatorLib/OMSimulator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ OMSAPI oms_status_enu_t OMSCALL oms_setResultFile(const char* cref, const char*
151151
OMSAPI oms_status_enu_t OMSCALL oms_setSolver(const char* cref, oms_solver_enu_t solver);
152152
OMSAPI oms_status_enu_t OMSCALL oms_setStartTime(const char* cref, double startTime);
153153
OMSAPI oms_status_enu_t OMSCALL oms_setStopTime(const char* cref, double stopTime);
154+
OMSAPI oms_status_enu_t OMSCALL oms_setString(const char* cref, const char* value);
154155
OMSAPI oms_status_enu_t OMSCALL oms_setTempDirectory(const char* newTempDir);
155156
OMSAPI oms_status_enu_t OMSCALL oms_setTLMBusGeometry(const char* bus, const ssd_connector_geometry_t* geometry);
156157
OMSAPI oms_status_enu_t OMSCALL oms_setTLMConnectionParameters(const char* crefA, const char* crefB, const oms_tlm_connection_parameters_t* parameters);

src/OMSimulatorLib/System.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,52 @@ oms_status_enu_t oms::System::setReal(const ComRef& cref, double value)
23492349
return logError_UnknownSignal(getFullCref() + cref);
23502350
}
23512351

2352+
oms_status_enu_t oms::System::setString(const ComRef& cref, const std::string& value)
2353+
{
2354+
if (!getModel().validState(oms_modelState_virgin|oms_modelState_enterInstantiation|oms_modelState_instantiated|oms_modelState_initialization|oms_modelState_simulation))
2355+
return logError_ModelInWrongState(getModel().getCref());
2356+
2357+
oms::ComRef tail(cref);
2358+
oms::ComRef head = tail.pop_front();
2359+
2360+
auto subsystem = subsystems.find(head);
2361+
if (subsystem != subsystems.end())
2362+
return subsystem->second->setString(tail, value);
2363+
2364+
auto component = components.find(head);
2365+
if (component != components.end())
2366+
return component->second->setString(tail, value);
2367+
2368+
for (auto &connector: connectors)
2369+
{
2370+
if (connector && connector->getName() == cref && connector->isTypeString())
2371+
{
2372+
// check for local resources available
2373+
if (values.hasResources())
2374+
{
2375+
return values.setStringResources(cref, value, getFullCref(), true, getModel().getModelState());
2376+
}
2377+
// check for resources in top level system
2378+
else if (getParentSystem() && getParentSystem()->values.hasResources())
2379+
{
2380+
//return getParentSystem()->setRealSystemResources(getCref()+cref, value, connector->isOutput());
2381+
return getParentSystem()->values.setStringResources(getCref() + cref, value, getParentSystem()->getFullCref(), true, getModel().getModelState());
2382+
}
2383+
else
2384+
{
2385+
// set external inputs, after initialization
2386+
if (oms_modelState_simulation == getModel().getModelState())
2387+
values.stringValues[cref] = value;
2388+
else
2389+
values.setString(cref, value);
2390+
return oms_status_ok;
2391+
}
2392+
}
2393+
}
2394+
2395+
return logError_UnknownSignal(getFullCref() + cref);
2396+
}
2397+
23522398
oms_status_enu_t oms::System::getReals(const std::vector<oms::ComRef> &sr, std::vector<double> &values)
23532399
{
23542400
oms_status_enu_t status = oms_status_ok;

src/OMSimulatorLib/System.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ namespace oms
155155
oms_status_enu_t setBoolean(const ComRef& cref, bool value);
156156
oms_status_enu_t setInteger(const ComRef& cref, int value);
157157
oms_status_enu_t setReal(const ComRef& cref, double value);
158+
oms_status_enu_t setString(const ComRef& cref, const std::string& value);
158159

159160
oms_status_enu_t getReals(const std::vector<ComRef> &crefs, std::vector<double> &values);
160161
oms_status_enu_t setReals(const std::vector<ComRef> &crefs, std::vector<double> values);

src/OMSimulatorLib/Values.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ oms::Values::~Values()
5353
oms_status_enu_t oms::Values::setReal(const ComRef& cref, double value)
5454
{
5555
realStartValues[cref] = value;
56+
5657
// update start values in ssv template
5758
auto realValue = modelDescriptionRealStartValues.find(cref);
5859
if (realValue != modelDescriptionRealStartValues.end())
@@ -64,6 +65,7 @@ oms_status_enu_t oms::Values::setReal(const ComRef& cref, double value)
6465
oms_status_enu_t oms::Values::setInteger(const ComRef& cref, int value)
6566
{
6667
integerStartValues[cref] = value;
68+
6769
// update start values in ssv template
6870
auto integerValue = modelDescriptionIntegerStartValues.find(cref);
6971
if (integerValue != modelDescriptionIntegerStartValues.end())
@@ -75,6 +77,7 @@ oms_status_enu_t oms::Values::setInteger(const ComRef& cref, int value)
7577
oms_status_enu_t oms::Values::setBoolean(const ComRef& cref, bool value)
7678
{
7779
booleanStartValues[cref] = value;
80+
7881
// update start values in ssv template
7982
auto boolValue = modelDescriptionBooleanStartValues.find(cref);
8083
if (boolValue != modelDescriptionBooleanStartValues.end())
@@ -83,6 +86,18 @@ oms_status_enu_t oms::Values::setBoolean(const ComRef& cref, bool value)
8386
return oms_status_ok;
8487
}
8588

89+
oms_status_enu_t oms::Values::setString(const ComRef& cref, const std::string& value)
90+
{
91+
stringStartValues[cref] = value;
92+
93+
// update start values in ssv template
94+
auto stringValue = modelDescriptionStringStartValues.find(cref);
95+
if (stringValue != modelDescriptionStringStartValues.end())
96+
modelDescriptionStringStartValues[cref] = value;
97+
98+
return oms_status_ok;
99+
}
100+
86101
oms_status_enu_t oms::Values::getReal(const ComRef& cref, double& value)
87102
{
88103
auto realValue = realStartValues.find(cref);
@@ -169,6 +184,40 @@ oms_status_enu_t oms::Values::setRealResources(const ComRef& cref, double value,
169184
return oms_status_ok;
170185
}
171186

187+
oms_status_enu_t oms::Values::setStringResources(const ComRef& cref, const std::string& value, const ComRef& fullCref, bool externalInput, oms_modelState_enu_t modelState)
188+
{
189+
bool resourceAvailable = false;
190+
for (auto &it : parameterResources)
191+
{
192+
for (auto &res : it.allresources)
193+
{
194+
//update the value in all resources, so that same cref in multiple ssv can be updated, this can result in duplication
195+
auto stringValue = res.second.stringStartValues.find(cref);
196+
if (stringValue != res.second.stringStartValues.end())
197+
{
198+
if (oms_modelState_simulation == modelState && externalInput)
199+
res.second.stringValues[cref] = value;
200+
else
201+
res.second.setString(cref, value);
202+
resourceAvailable = true;
203+
// return oms_status_ok; return here to avoid updating the same value in different ssv file
204+
}
205+
}
206+
}
207+
208+
if (!resourceAvailable)
209+
{
210+
auto &it = parameterResources.front();
211+
for (auto &res : it.allresources)
212+
{
213+
// insert the new signal at the first resource available
214+
res.second.setString(cref, value);
215+
break;
216+
}
217+
}
218+
return oms_status_ok;
219+
}
220+
172221
oms_status_enu_t oms::Values::setIntegerResources(const ComRef& cref, int value, const ComRef& fullCref, bool externalInput, oms_modelState_enu_t modelState)
173222
{
174223
bool resourceAvailable = false;

0 commit comments

Comments
 (0)