Skip to content

Commit

Permalink
Adapt Cpp code to increased use of calculatedParameter, ticket:4027
Browse files Browse the repository at this point in the history
This follows up 25249c8
"Fix initialization problems containing parameters having implicitly fixed=false"

- move initialization details to the three sub-methods
  initializeMemory/FreeVariables/BoundVariables
- in particular place initParameterEquations() in initializeBoundVariables
- avoid duplicate initialization steps in FMUWrapper and FMU2Wrapper
- force HARDCODED_START_VALUES for FMI export -- this is needed for FMI2,
  because calculatedParameters have no values in the XML file;
  it is also needed for FMI1 tests that re-import FMUs (e.g. Crane*)
  • Loading branch information
rfranke committed Oct 18, 2016
1 parent 55548c1 commit d783820
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
46 changes: 25 additions & 21 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -5871,32 +5871,13 @@ case SIMCODE(modelInfo = MODELINFO(__),makefileParams = MAKEFILE_PARAMS(__)) th
let initialequations = functionInitialEquations(initialEquations,"initEquation",simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation, false, true, false)
let boundparameterequations = functionInitialEquations(parameterEquations,"initParameterEquation",simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation, false, true, true)
<<
// convenience function for full initialization
void <%lastIdentOfPath(modelInfo.name)%>Initialize::initialize()
{
initializeMemory();

#if !defined(FMU_BUILD)
#if defined(__vxworks)
_reader = shared_ptr<IPropertyReader>(new XmlPropertyReader("/SYSTEM/bundles/com.boschrexroth.<%modelname%>/<%fileNamePrefix%>_init.xml"));
#else
_reader = shared_ptr<IPropertyReader>(new XmlPropertyReader("<%makefileParams.compileDir%>/<%fileNamePrefix%>_init.xml"));
#endif
_reader->readInitialValues(*this, getSimVars());
#endif
initializeFreeVariables();
/*Start complex expressions */
<%complexStartExpressions%>
/* End complex expression */
<%if(boolAnd(boolNot(Flags.isSet(Flags.HARDCODED_START_VALUES)), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS))) then 'checkParameters();' else '//checkParameters();'%>
initParameterEquations();
initializeBoundVariables();
<%if(boolAnd(boolNot(Flags.isSet(Flags.HARDCODED_START_VALUES)), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS))) then 'checkVariables();' else '//checkVariables();'%>
saveAll();

<%lastIdentOfPath(modelInfo.name)%>WriteOutput::initialize();
<%lastIdentOfPath(modelInfo.name)%>Jacobian::initialize();
<%lastIdentOfPath(modelInfo.name)%>Jacobian::initializeColoredJacobianA();
//delete reader;
}

void <%lastIdentOfPath(modelInfo.name)%>Initialize::initializeMemory()
Expand All @@ -5910,6 +5891,10 @@ case SIMCODE(modelInfo = MODELINFO(__),makefileParams = MAKEFILE_PARAMS(__)) th
initializeAlgloopSolverVariables();
//init alg loop vars
<%initAlgloopvars%>

<%lastIdentOfPath(modelInfo.name)%>WriteOutput::initialize();
<%lastIdentOfPath(modelInfo.name)%>Jacobian::initialize();
<%lastIdentOfPath(modelInfo.name)%>Jacobian::initializeColoredJacobianA();
}
<%if(boolAnd(boolNot(Flags.isSet(Flags.HARDCODED_START_VALUES)), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS))) then
<<
Expand Down Expand Up @@ -5940,6 +5925,15 @@ case SIMCODE(modelInfo = MODELINFO(__),makefileParams = MAKEFILE_PARAMS(__)) th
%>
void <%lastIdentOfPath(modelInfo.name)%>Initialize::initializeFreeVariables()
{
#if !defined(FMU_BUILD)
#if defined(__vxworks)
_reader = shared_ptr<IPropertyReader>(new XmlPropertyReader("/SYSTEM/bundles/com.boschrexroth.<%modelname%>/<%fileNamePrefix%>_init.xml"));
#else
_reader = shared_ptr<IPropertyReader>(new XmlPropertyReader("<%makefileParams.compileDir%>/<%fileNamePrefix%>_init.xml"));
#endif
_reader->readInitialValues(*this, getSimVars());
#endif

_simTime = 0.0;
_state_var_reinitialized = false;

Expand Down Expand Up @@ -5968,13 +5962,21 @@ case SIMCODE(modelInfo = MODELINFO(__),makefileParams = MAKEFILE_PARAMS(__)) th
//init inputs
stepStarted(0.0);
#endif

/*Start complex expressions */
<%complexStartExpressions%>
/* End complex expression */
<%if(boolAnd(boolNot(Flags.isSet(Flags.HARDCODED_START_VALUES)), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS))) then 'checkParameters();' else '//checkParameters();'%>
//delete reader;
}

void <%lastIdentOfPath(modelInfo.name)%>Initialize::initializeBoundVariables()
{
//variable decls
<%varDecls%>

initParameterEquations();

//construct external objects once
if (!_constructedExternalObjects)
constructExternalObjects();
Expand All @@ -5993,14 +5995,16 @@ case SIMCODE(modelInfo = MODELINFO(__),makefileParams = MAKEFILE_PARAMS(__)) th
//init alg loop solvers
<%initAlgloopSolvers%>

for(int i=0;i<_dimZeroFunc;i++)
for(int i = 0; i < _dimZeroFunc; i++)
{
getCondition(i);
}

//initialAnalyticJacobian();

<%functionInitDelay(delayedExps,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>

<%if(boolAnd(boolNot(Flags.isSet(Flags.HARDCODED_START_VALUES)), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS))) then 'checkVariables();' else '//checkVariables();'%>
}

void <%lastIdentOfPath(modelInfo.name)%>Initialize::initEquations()
Expand Down
1 change: 1 addition & 0 deletions Compiler/Template/CodegenFMUCpp.tpl
Expand Up @@ -75,6 +75,7 @@ case SIMCODE(modelInfo=modelInfo as MODELINFO(__)) then
let numBoolVars = numBoolvars(modelInfo)
let numStringVars = numStringvars(modelInfo)

let flag = Flags.set(Flags.HARDCODED_START_VALUES, true)
let cpp = CodegenCpp.translateModel(simCode)
let()= textFile(fmuWriteOutputHeaderFile(simCode , &extraFuncs , &extraFuncsDecl, ""),'OMCpp<%fileNamePrefix%>WriteOutput.h')
let()= textFile(fmuModelHeaderFile(simCode, extraFuncs, extraFuncsDecl, "",guid, FMUVersion), 'OMCpp<%fileNamePrefix%>FMU.h')
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -3339,6 +3339,12 @@ package Flags
constant ConfigFlag DAE_MODE;
constant ConfigFlag EQUATIONS_PER_FILE;

function set
input DebugFlag inFlag;
input Boolean inValue;
output Boolean outOldValue;
end set;

function isSet
input DebugFlag inFlag;
output Boolean outValue;
Expand Down
8 changes: 5 additions & 3 deletions SimulationRuntime/cpp/Include/FMU/FMUWrapper.h
Expand Up @@ -151,10 +151,12 @@ class FMUWrapper : public IFMUInterface
virtual fmiStatus initialize(fmiBoolean toleranceControlled, fmiReal relativeTolerance, fmiEventInfo& eventInfo)
{
//LOGGER_WRITE("Initialize",LC_OTHER,LL_DEBUG);
// TODO: here is some code duplication to SimulationRuntime/cpp/Core/Solver/Initailization.cpp
Logger::write("Initializing memory and variables",LC_MOD,LL_DEBUG);
_model->initialize();
Logger::write("Initializing memory and variables", LC_MOD, LL_DEBUG);
_model->initializeMemory();
_model->initializeFreeVariables();
_model->initializeBoundVariables();
_model->saveAll();

_model->setInitial(true);

bool restart=true;
Expand Down
7 changes: 5 additions & 2 deletions SimulationRuntime/cpp/Include/FMU2/FMU2Wrapper.cpp
Expand Up @@ -64,7 +64,8 @@ FMU2Wrapper::FMU2Wrapper(fmi2String instanceName, fmi2String GUID,
_GUID = GUID;
_logCategories = loggingOn? 0xFFFF: 0x0000;
_model = createSystemFMU(&_global_settings);
_model->initialize();
_model->initializeMemory();
_model->initializeFreeVariables();
_string_buffer.resize(_model->getDimString());
_clock_buffer = new bool[_model->getDimClock()];
std::fill(_clock_buffer, _clock_buffer + _model->getDimClock(), false);
Expand Down Expand Up @@ -153,8 +154,10 @@ fmi2Status FMU2Wrapper::reset()

void FMU2Wrapper::updateModel()
{
if (_model->initial())
if (_model->initial()) {
_model->initializeBoundVariables();
_model->saveAll();
}
_model->evaluateAll(); // derivatives and algebraic variables
_need_update = false;
}
Expand Down

0 comments on commit d783820

Please sign in to comment.