Skip to content
Open
25 changes: 19 additions & 6 deletions src/OMSimulatorLib/AlgLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ inline bool checkFlag(int flag, std::string functionName)
logError("SUNDIALS_ERROR: " + functionName + " failed with flag = " + std::to_string(flag));
return false;
}
logDebug("SUNDIALS_INFO: " + functionName + " failed with flag = " + std::to_string(flag));
logDebug("SUNDIALS_INFO: " + functionName + " succeeded with flag = " + std::to_string(flag));
return true;
}

Expand Down Expand Up @@ -391,6 +391,12 @@ oms::KinsolSolver* oms::KinsolSolver::NewKinsolSolver(const int algLoopNum, cons
return kinsolSolver;
}

static std::string to_string(double value) {
std::ostringstream s;
s << value;
return s.str();
}

/**
* @brief Solve algebraic system with KINSOL
*
Expand All @@ -413,7 +419,10 @@ oms_status_enu_t oms::KinsolSolver::kinsolSolve(System& syst, DirectedGraph& gra
int flag;
double fNormValue;

logDebug("Solving system " + std::to_string(kinsolUserData->algLoopNumber));
if (Flags::DumpAlgLoops())
logInfo("Solving system " + std::to_string(kinsolUserData->algLoopNumber + 1) + " to within tolerance " + to_string(fnormtol));
else
logDebug("Solving system " + std::to_string(kinsolUserData->algLoopNumber + 1) + " to within tolerance " + to_string(fnormtol));

if (SCC.connections.size() != size)
{
Expand Down Expand Up @@ -445,13 +454,17 @@ oms_status_enu_t oms::KinsolSolver::kinsolSolve(System& syst, DirectedGraph& gra

/* Check solution */
flag = nlsKinsolResiduals(initialGuess, fTmp, user_data);
fNormValue = N_VWL2Norm(fTmp, fTmp);
fNormValue = N_VWL2Norm(fTmp, fScale);
if ( fNormValue > fnormtol )
{
logWarning("Solution of algebraic loop " + std::to_string(((KINSOL_USER_DATA *)user_data)->algLoopNumber) + "not within precission given by fnormtol: " + std::to_string(fnormtol));
logDebug("2-norm of residual of solution: " + std::to_string(fNormValue));
logWarning("Solution of algebraic loop " + std::to_string(((KINSOL_USER_DATA *)user_data)->algLoopNumber + 1) + " not within precission given by fnormtol: " + to_string(fnormtol));
logDebug("2-norm of residual of solution: " + to_string(fNormValue));
return oms_status_warning;
}
else if (Flags::DumpAlgLoops())
{
logInfo("2-norm of residual of solution: " + to_string(fNormValue) + " <= " + to_string(fnormtol));
}

logDebug("Solved system " + std::to_string(kinsolUserData->algLoopNumber) + " successfully");
return oms_status_ok;
Expand Down Expand Up @@ -649,7 +662,7 @@ oms_status_enu_t oms::AlgLoop::fixPointIteration(System& syst, DirectedGraph& gr
{
return logError("max. number of iterations (" + std::to_string(maxIterations) + ") exceeded at time = " + std::to_string(syst.getTime()));
}
logDebug("CompositeModel::solveAlgLoop: maxRes: " + std::to_string(maxRes) + ", iterations: " + std::to_string(it) + " at time = " + std::to_string(syst.getTime()));
logDebug("CompositeModel::solveAlgLoop: maxRes: " + to_string(maxRes) + ", iterations: " + std::to_string(it) + " at time = " + std::to_string(syst.getTime()));
return oms_status_ok;
}

Expand Down
19 changes: 15 additions & 4 deletions src/OMSimulatorLib/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "TLMBusConnector.h"

#include <stdarg.h>
#include <sstream>

void oms::fmi2logger(fmi2ComponentEnvironment env, fmi2String instanceName, fmi2Status status, fmi2String category, fmi2String message, ...)
{
Expand All @@ -53,22 +54,32 @@ void oms::fmi2logger(fmi2ComponentEnvironment env, fmi2String instanceName, fmi2
va_start(argp, message);
len = vsnprintf(msg, 1000, message, argp);

std::ostringstream s;
s << instanceName;
if (category != nullptr)
s << " (" << category << "): ";
else
s << ": ";
s << msg;

std::string output(s.str());

switch (status)
{
case fmi2OK:
case fmi2Pending:
logDebug(std::string(instanceName) + " (" + category + "): " + msg);
logDebug(output);
break;
case fmi2Warning:
logWarning(std::string(instanceName) + " (" + category + "): " + msg);
logWarning(output);
break;
case fmi2Discard:
case fmi2Error:
case fmi2Fatal:
logError(std::string(instanceName) + " (" + category + "): " + msg);
logError(output);
break;
default:
logWarning("fmiStatus = unknown; " + std::string(instanceName) + " (" + category + "): " + msg);
logWarning("fmiStatus = unknown; " + output);
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/OMSimulatorLib/ComponentFMUCS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,12 @@ oms_status_enu_t oms::ComponentFMUCS::setResourcesHelper1(Values values)
if (oms_status_ok != setReal(cref, v.second))
return logError("Failed to set start value for " + std::string(v.first));
}
for (const auto &v : values.stringStartValues)
{
oms::ComRef cref = getValidCref(v.first);
if (oms_status_ok != setString(cref, v.second))
return logError("Failed to set start value for " + std::string(v.first));
}

return oms_status_ok;
}
Expand Down Expand Up @@ -726,6 +732,19 @@ oms_status_enu_t oms::ComponentFMUCS::setResourcesHelper2(Values values)
}
}
}
for (const auto &v : res.second.stringStartValues)
{
if (res.second.linkResources) // set values only if resources are linked in ssd
{
oms::ComRef tail(v.first);
oms::ComRef head = tail.pop_front();
if (head == getCref())
{
if (oms_status_ok != setString(tail, v.second))
return logError("Failed to set start value for " + std::string(v.first));
}
}
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/OMSimulatorLib/ComponentFMUME.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,12 @@ oms_status_enu_t oms::ComponentFMUME::setResourcesHelper1(Values values)
if (oms_status_ok != setReal(cref, v.second))
return logError("Failed to set start value for " + std::string(v.first));
}
for (const auto &v : values.stringStartValues)
{
oms::ComRef cref = getValidCref(v.first);
if (oms_status_ok != setString(cref, v.second))
return logError("Failed to set start value for " + std::string(v.first));
}

return oms_status_ok;
}
Expand Down Expand Up @@ -760,6 +766,19 @@ oms_status_enu_t oms::ComponentFMUME::setResourcesHelper2(Values values)
}
}
}
for (const auto &v : res.second.stringStartValues)
{
if (res.second.linkResources) // set values only if resources are linked in ssd
{
oms::ComRef tail(v.first);
oms::ComRef head = tail.pop_front();
if (head == getCref())
{
if (oms_status_ok != setString(tail, v.second))
return logError("Failed to set start value for " + std::string(v.first));
}
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/OMSimulatorLib/Connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ oms::Connector* oms::Connector::NewConnector(const pugi::xml_node& node, const s
type = oms_signal_type_boolean;
else if (typeString == "Enumeration")
type = oms_signal_type_enum;
else if (typeString == "String")
type = oms_signal_type_string;
// TODO handle "Binary" type for FMI-2.1 see specification
else
{
Expand Down
5 changes: 5 additions & 0 deletions src/OMSimulatorLib/OMSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,11 @@ oms_status_enu_t oms_RunFile(const char* filename)
{
char* cref;
oms_importFile(filename, &cref);
if (!cref)
{
logError("Importing file \"" + std::string(filename) + "\" failed");
return oms_status_error;
}

if (oms::Flags::ResultFile().given)
oms_setResultFile(cref, oms::Flags::ResultFile().value.c_str(), 1);
Expand Down
10 changes: 6 additions & 4 deletions src/OMSimulatorLib/SystemSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ oms_status_enu_t oms::SystemSC::initialize()

// Call CVodeSetLinearSolver to set the dense linear solver */
flag = CVodeSetLinearSolver(solverData.cvode.mem, solverData.cvode.linSol, solverData.cvode.J);
if (flag < 0) logError("SUNDIALS_ERROR: CVDense() failed with flag = " + std::to_string(flag));
if (flag < 0) logError("SUNDIALS_ERROR: CVodeSetLinearSolver() failed with flag = " + std::to_string(flag));

logInfo("maximum step size for '" + std::string(getFullCref()) + "': " + std::to_string(maximumStepSize));
flag = CVodeSetMaxStep(solverData.cvode.mem, maximumStepSize);
Expand Down Expand Up @@ -949,7 +949,7 @@ oms_status_enu_t oms::SystemSC::stepUntil(double stopTime)
oms_status_enu_t oms::SystemSC::updateInputs(DirectedGraph& graph)
{
CallClock callClock(clock);
oms_status_enu_t status;
oms_status_enu_t status, return_status = oms_status_ok;
int loopNum = 0;

// input := output
Expand Down Expand Up @@ -998,10 +998,12 @@ oms_status_enu_t oms::SystemSC::updateInputs(DirectedGraph& graph)
if (oms_status_ok != status)
{
forceLoopsToBeUpdated();
return status;
if (status > return_status)
return_status = status;
}
loopNum++;
}
}
return oms_status_ok;

return return_status;
}
14 changes: 14 additions & 0 deletions src/OMSimulatorLib/Values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,20 @@ oms_status_enu_t oms::Values::importStartValuesHelper(const pugi::xml_node& para
setBoolean(cref, value);
}
}
else if(itparameters->child(oms::ssp::Version1_0::ssv::string_type))
{
const char *value = itparameters->child(oms::ssp::Version1_0::ssv::string_type).attribute("value").as_string();
if (!mappedcrefs.empty())
{
for (const auto &mappedcref : mappedcrefs)
setString(mappedcref, value);
}
else
{
// no mapping entry found, apply the default cref found in ssv file
setString(cref, value);
}
}
else
{
logError("Failed to import " + std::string(oms::ssp::Version1_0::ssv::parameter) + ":Unknown ParameterBinding-type");
Expand Down
2 changes: 2 additions & 0 deletions testsuite/simulation/equationPair.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
## info: Alg. loop (size 2/4)
## equationPair.root.equation2
## equationPair.root.equation1
## info: Solving system 1 to within tolerance 0.0001
## info: iteration 1
## inputs:
## equation2.x2: 8.4
Expand Down Expand Up @@ -107,6 +108,7 @@
## residuals:
## res[0]: -4.44595e-05
## res[1]: 0
## info: 2-norm of residual of solution: 4.44595e-05 <= 0.0001
## info: No result file will be created
## x1: 4.5572 (res: 0.00e+00)
## x1: 0.0928 (res: -4.45e-05)
Expand Down