Skip to content

Commit d54078e

Browse files
authored
Refactoring of ComponentFMUCS/ME (#1058)
1 parent 98c92df commit d54078e

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed

src/OMSimulatorLib/ComponentFMUCS.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,16 @@ oms::Component* oms::ComponentFMUCS::NewComponent(const oms::ComRef& cref, oms::
202202
for (auto const& v : component->allVariables)
203203
{
204204
if (v.isInput())
205-
component->inputs.push_back(v);
205+
component->inputs.push_back(v.getIndex());
206206
else if (v.isOutput())
207207
{
208-
component->outputs.push_back(v);
208+
component->outputs.push_back(v.getIndex());
209209
component->outputsGraph.addNode(Connector(oms_causality_output, v.getType(), v.getCref(), component->getFullCref()));
210210
}
211211
else if (v.isParameter())
212-
component->parameters.push_back(v);
212+
component->parameters.push_back(v.getIndex());
213213
else if (v.isCalculatedParameter())
214-
component->calculatedParameters.push_back(v);
214+
component->calculatedParameters.push_back(v.getIndex());
215215

216216
if (v.isInitialUnknown())
217217
component->initialUnknownsGraph.addNode(Connector(v.getCausality(), v.getType(), v.getCref(), component->getFullCref()));
@@ -223,18 +223,18 @@ oms::Component* oms::ComponentFMUCS::NewComponent(const oms::ComRef& cref, oms::
223223
while (component->connectors.size() > 0 && NULL == component->connectors.back())
224224
component->connectors.pop_back();
225225

226-
int i = 1;
226+
int j = 1;
227227
int size = 1 + component->inputs.size();
228-
for (const auto& v : component->inputs)
229-
component->connectors.push_back(new Connector(oms_causality_input, v.getType(), v.getCref(), component->getFullCref(), i++/(double)size));
230-
i = 1;
228+
for (const auto& i : component->inputs)
229+
component->connectors.push_back(new Connector(oms_causality_input, component->allVariables[i].getType(), component->allVariables[i].getCref(), component->getFullCref(), j++/(double)size));
230+
j = 1;
231231
size = 1 + component->outputs.size();
232-
for (const auto& v : component->outputs)
233-
component->connectors.push_back(new Connector(oms_causality_output, v.getType(), v.getCref(), component->getFullCref(), i++/(double)size));
234-
for (const auto& v : component->parameters)
235-
component->connectors.push_back(new Connector(oms_causality_parameter, v.getType(), v.getCref(), component->getFullCref()));
236-
for (const auto& v : component->calculatedParameters)
237-
component->connectors.push_back(new Connector(oms_causality_calculatedParameter, v.getType(), v.getCref(), component->getFullCref()));
232+
for (const auto& i : component->outputs)
233+
component->connectors.push_back(new Connector(oms_causality_output, component->allVariables[i].getType(), component->allVariables[i].getCref(), component->getFullCref(), j++/(double)size));
234+
for (const auto& i : component->parameters)
235+
component->connectors.push_back(new Connector(oms_causality_parameter, component->allVariables[i].getType(), component->allVariables[i].getCref(), component->getFullCref()));
236+
for (const auto& i : component->calculatedParameters)
237+
component->connectors.push_back(new Connector(oms_causality_calculatedParameter, component->allVariables[i].getType(), component->allVariables[i].getCref(), component->getFullCref()));
238238
component->connectors.push_back(NULL);
239239
component->element.setConnectors(&component->connectors[0]);
240240

@@ -440,8 +440,8 @@ oms_status_enu_t oms::ComponentFMUCS::initializeDependencyGraph_initialUnknowns(
440440
for (int i=0; i < N; i++)
441441
{
442442
logDebug(std::string(getCref()) + ": " + getPath() + " initial unknown " + std::string(initialUnknownsGraph.getNodes()[i]) + " depends on all inputs");
443-
for (int j=0; j < inputs.size(); j++)
444-
initialUnknownsGraph.addEdge(inputs[j].makeConnector(this->getFullCref()), initialUnknownsGraph.getNodes()[i]);
443+
for (const auto& j : inputs)
444+
initialUnknownsGraph.addEdge(allVariables[j].makeConnector(this->getFullCref()), initialUnknownsGraph.getNodes()[i]);
445445
}
446446
return oms_status_ok;
447447
}
@@ -484,8 +484,8 @@ oms_status_enu_t oms::ComponentFMUCS::initializeDependencyGraph_initialUnknowns(
484484
else if ((startIndex[i] + 1 == startIndex[i+1]) && (dependency[startIndex[i]] == 0))
485485
{
486486
logDebug(std::string(getCref()) + ": " + getPath() + " initial unknown " + std::string(initialUnknownsGraph.getNodes()[i]) + " depends on all inputs");
487-
for (int j = 0; j < inputs.size(); j++)
488-
initialUnknownsGraph.addEdge(inputs[j].makeConnector(this->getFullCref()), initialUnknownsGraph.getNodes()[i]);
487+
for (const auto& j : inputs)
488+
initialUnknownsGraph.addEdge(allVariables[j].makeConnector(this->getFullCref()), initialUnknownsGraph.getNodes()[i]);
489489
}
490490
else
491491
{
@@ -524,29 +524,30 @@ oms_status_enu_t oms::ComponentFMUCS::initializeDependencyGraph_outputs()
524524
return oms_status_ok;
525525
}
526526

527-
for (int i = 0; i < outputs.size(); i++)
527+
for (int i=0; i < outputs.size(); i++)
528528
{
529+
Variable& output = allVariables[outputs[i]];
529530
if (startIndex[i] == startIndex[i + 1])
530531
{
531-
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(outputs[i]) + " has no dependencies");
532+
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(output) + " has no dependencies");
532533
}
533534
else if ((startIndex[i] + 1 == startIndex[i + 1]) && (dependency[startIndex[i]] == 0))
534535
{
535-
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(outputs[i]) + " depends on all");
536-
for (int j = 0; j < inputs.size(); j++)
537-
outputsGraph.addEdge(inputs[j].makeConnector(this->getFullCref()), outputs[i].makeConnector(this->getFullCref()));
536+
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(output) + " depends on all");
537+
for (const auto& j : inputs)
538+
outputsGraph.addEdge(allVariables[j].makeConnector(this->getFullCref()), output.makeConnector(this->getFullCref()));
538539
}
539540
else
540541
{
541542
for (size_t j = startIndex[i]; j < startIndex[i + 1]; j++)
542543
{
543544
if (dependency[j] < 1 || dependency[j] > allVariables.size())
544545
{
545-
logWarning("Output " + std::string(outputs[i]) + " has bad dependency on variable with index " + std::to_string(dependency[j]) + " which couldn't be resolved");
546+
logWarning("Output " + std::string(output) + " has bad dependency on variable with index " + std::to_string(dependency[j]) + " which couldn't be resolved");
546547
return logError(std::string(getCref()) + ": erroneous dependencies detected in modelDescription.xml");
547548
}
548-
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(outputs[i]) + " depends on " + std::string(allVariables[dependency[j] - 1]));
549-
outputsGraph.addEdge(allVariables[dependency[j] - 1].makeConnector(this->getFullCref()), outputs[i].makeConnector(this->getFullCref()));
549+
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(output) + " depends on " + std::string(allVariables[dependency[j] - 1]));
550+
outputsGraph.addEdge(allVariables[dependency[j] - 1].makeConnector(this->getFullCref()), output.makeConnector(this->getFullCref()));
550551
}
551552
}
552553
}

src/OMSimulatorLib/ComponentFMUCS.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ namespace oms
9090
oms_status_enu_t setFmuTime(double time) {this->time = time; return oms_status_ok;}
9191
fmi2_import_t* getFMU() {return fmu;}
9292
std::vector<Variable> getAllVariables() {return allVariables;}
93-
std::vector<Variable> getInputs() {return inputs;}
94-
std::vector<Variable> getOutputs() {return outputs;}
9593

9694
oms_status_enu_t getRealOutputDerivative(const ComRef& cref, SignalDerivative& der);
9795
oms_status_enu_t setRealInputDerivative(const ComRef& cref, const SignalDerivative& der);
@@ -132,12 +130,12 @@ namespace oms
132130
FMUInfo fmuInfo;
133131

134132
std::vector<Variable> allVariables;
135-
std::vector<Variable> inputs;
136-
std::vector<Variable> outputs;
137-
std::vector<Variable> parameters;
138-
std::vector<Variable> calculatedParameters;
139-
std::vector<bool> exportVariables;
133+
std::vector<unsigned int> calculatedParameters;
140134
std::vector<unsigned int> derivatives;
135+
std::vector<unsigned int> inputs;
136+
std::vector<unsigned int> outputs;
137+
std::vector<unsigned int> parameters;
138+
std::vector<bool> exportVariables;
141139

142140
Values values; ///< start values defined before instantiating the FMU and external inputs defined after initialization
143141

src/OMSimulatorLib/ComponentFMUME.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,16 @@ oms::Component* oms::ComponentFMUME::NewComponent(const oms::ComRef& cref, oms::
205205
for (auto const& v : component->allVariables)
206206
{
207207
if (v.isInput())
208-
component->inputs.push_back(v);
208+
component->inputs.push_back(v.getIndex());
209209
else if (v.isOutput())
210210
{
211-
component->outputs.push_back(v);
211+
component->outputs.push_back(v.getIndex());
212212
component->outputsGraph.addNode(Connector(oms_causality_output, v.getType(), v.getCref(), component->getFullCref()));
213213
}
214214
else if (v.isParameter())
215-
component->parameters.push_back(v);
215+
component->parameters.push_back(v.getIndex());
216216
else if (v.isCalculatedParameter())
217-
component->calculatedParameters.push_back(v);
217+
component->calculatedParameters.push_back(v.getIndex());
218218

219219
if (v.isInitialUnknown())
220220
component->initialUnknownsGraph.addNode(Connector(v.getCausality(), v.getType(), v.getCref(), component->getFullCref()));
@@ -226,18 +226,18 @@ oms::Component* oms::ComponentFMUME::NewComponent(const oms::ComRef& cref, oms::
226226
while (component->connectors.size() > 0 && NULL == component->connectors.back())
227227
component->connectors.pop_back();
228228

229-
int i = 1;
229+
int j = 1;
230230
int size = 1 + component->inputs.size();
231-
for (const auto& v : component->inputs)
232-
component->connectors.push_back(new Connector(oms_causality_input, v.getType(), v.getCref(), component->getFullCref(), i++/(double)size));
233-
i = 1;
231+
for (const auto& i : component->inputs)
232+
component->connectors.push_back(new Connector(oms_causality_input, component->allVariables[i].getType(), component->allVariables[i].getCref(), component->getFullCref(), j++/(double)size));
233+
j = 1;
234234
size = 1 + component->outputs.size();
235-
for (const auto& v : component->outputs)
236-
component->connectors.push_back(new Connector(oms_causality_output, v.getType(), v.getCref(), component->getFullCref(), i++/(double)size));
237-
for (const auto& v : component->parameters)
238-
component->connectors.push_back(new Connector(oms_causality_parameter, v.getType(), v.getCref(), component->getFullCref()));
239-
for (const auto& v : component->calculatedParameters)
240-
component->connectors.push_back(new Connector(oms_causality_calculatedParameter, v.getType(), v.getCref(), component->getFullCref()));
235+
for (const auto& i : component->outputs)
236+
component->connectors.push_back(new Connector(oms_causality_output, component->allVariables[i].getType(), component->allVariables[i].getCref(), component->getFullCref(), j++/(double)size));
237+
for (const auto& i : component->parameters)
238+
component->connectors.push_back(new Connector(oms_causality_parameter, component->allVariables[i].getType(), component->allVariables[i].getCref(), component->getFullCref()));
239+
for (const auto& i : component->calculatedParameters)
240+
component->connectors.push_back(new Connector(oms_causality_calculatedParameter, component->allVariables[i].getType(), component->allVariables[i].getCref(), component->getFullCref()));
241241
component->connectors.push_back(NULL);
242242
component->element.setConnectors(&component->connectors[0]);
243243

@@ -443,8 +443,8 @@ oms_status_enu_t oms::ComponentFMUME::initializeDependencyGraph_initialUnknowns(
443443
for (int i=0; i < N; i++)
444444
{
445445
logDebug(std::string(getCref()) + ": " + getPath() + " initial unknown " + std::string(initialUnknownsGraph.getNodes()[i]) + " depends on all inputs");
446-
for (int j=0; j < inputs.size(); j++)
447-
initialUnknownsGraph.addEdge(inputs[j].makeConnector(this->getFullCref()), initialUnknownsGraph.getNodes()[i]);
446+
for (const auto& j : inputs)
447+
initialUnknownsGraph.addEdge(allVariables[j].makeConnector(this->getFullCref()), initialUnknownsGraph.getNodes()[i]);
448448
}
449449
return oms_status_ok;
450450
}
@@ -487,8 +487,8 @@ oms_status_enu_t oms::ComponentFMUME::initializeDependencyGraph_initialUnknowns(
487487
else if ((startIndex[i] + 1 == startIndex[i+1]) && (dependency[startIndex[i]] == 0))
488488
{
489489
logDebug(std::string(getCref()) + ": " + getPath() + " initial unknown " + std::string(initialUnknownsGraph.getNodes()[i]) + " depends on all inputs");
490-
for (int j = 0; j < inputs.size(); j++)
491-
initialUnknownsGraph.addEdge(inputs[j].makeConnector(this->getFullCref()), initialUnknownsGraph.getNodes()[i]);
490+
for (const auto& j : inputs)
491+
initialUnknownsGraph.addEdge(allVariables[j].makeConnector(this->getFullCref()), initialUnknownsGraph.getNodes()[i]);
492492
}
493493
else
494494
{
@@ -526,27 +526,28 @@ oms_status_enu_t oms::ComponentFMUME::initializeDependencyGraph_outputs()
526526

527527
for (int i=0; i < outputs.size(); i++)
528528
{
529+
Variable& output = allVariables[outputs[i]];
529530
if (startIndex[i] == startIndex[i + 1])
530531
{
531-
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(outputs[i]) + " has no dependencies");
532+
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(output) + " has no dependencies");
532533
}
533534
else if ((startIndex[i] + 1 == startIndex[i + 1]) && (dependency[startIndex[i]] == 0))
534535
{
535-
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(outputs[i]) + " depends on all");
536-
for (int j = 0; j < inputs.size(); j++)
537-
outputsGraph.addEdge(inputs[j].makeConnector(this->getFullCref()), outputs[i].makeConnector(this->getFullCref()));
536+
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(output) + " depends on all");
537+
for (const auto& j : inputs)
538+
outputsGraph.addEdge(allVariables[j].makeConnector(this->getFullCref()), output.makeConnector(this->getFullCref()));
538539
}
539540
else
540541
{
541542
for (size_t j = startIndex[i]; j < startIndex[i + 1]; j++)
542543
{
543544
if (dependency[j] < 1 || dependency[j] > allVariables.size())
544545
{
545-
logWarning("Output " + std::string(outputs[i]) + " has bad dependency on variable with index " + std::to_string(dependency[j]) + " which couldn't be resolved");
546+
logWarning("Output " + std::string(output) + " has bad dependency on variable with index " + std::to_string(dependency[j]) + " which couldn't be resolved");
546547
return logError(std::string(getCref()) + ": erroneous dependencies detected in modelDescription.xml");
547548
}
548-
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(outputs[i]) + " depends on " + std::string(allVariables[dependency[j] - 1]));
549-
outputsGraph.addEdge(allVariables[dependency[j] - 1].makeConnector(this->getFullCref()), outputs[i].makeConnector(this->getFullCref()));
549+
logDebug(std::string(getCref()) + ": " + getPath() + " output " + std::string(output) + " depends on " + std::string(allVariables[dependency[j] - 1]));
550+
outputsGraph.addEdge(allVariables[dependency[j] - 1].makeConnector(this->getFullCref()), output.makeConnector(this->getFullCref()));
550551
}
551552
}
552553
}

src/OMSimulatorLib/ComponentFMUME.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ namespace oms
133133
FMUInfo fmuInfo;
134134

135135
std::vector<Variable> allVariables;
136-
std::vector<Variable> inputs;
137-
std::vector<Variable> outputs;
138-
std::vector<Variable> parameters;
139-
std::vector<Variable> calculatedParameters;
140-
std::vector<bool> exportVariables;
136+
std::vector<unsigned int> calculatedParameters;
141137
std::vector<unsigned int> derivatives;
138+
std::vector<unsigned int> inputs;
139+
std::vector<unsigned int> outputs;
140+
std::vector<unsigned int> parameters;
141+
std::vector<bool> exportVariables;
142142

143143
Values values; ///< start values defined before instantiating the FMU and external inputs defined after initialization
144144

0 commit comments

Comments
 (0)