Skip to content

Commit

Permalink
added SimVars class to cpp runtime which stores all model variables
Browse files Browse the repository at this point in the history
removed [Model]PreVariables class in cpp template and moved functionality to SimVars class
modified  get/set methods,array class,eventhandling classes to new SimvVars class


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25486 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed Apr 10, 2015
1 parent 166d96b commit cdfcc84
Show file tree
Hide file tree
Showing 26 changed files with 1,261 additions and 521 deletions.
591 changes: 308 additions & 283 deletions Compiler/Template/CodegenCpp.tpl

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions Compiler/Template/CodegenCppHpcom.tpl
Expand Up @@ -49,9 +49,7 @@ template translateModel(SimCode simCode)
let() = textFile(simulationExtensionHeaderFile(simCode, &extraFuncs, &extraFuncsDecl, ""), 'OMCpp<%fileNamePrefix%>Extension.h')
let() = textFile(simulationExtensionCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", preVarsCount), 'OMCpp<%fileNamePrefix%>Extension.cpp')
let() = textFile(simulationWriteOutputHeaderFile(simCode, &extraFuncs, &extraFuncsDecl, ""), 'OMCpp<%fileNamePrefix%>WriteOutput.h')
let() = textFile(simulationPreVarsHeaderFile(simCode, &extraFuncs, &extraFuncsDecl, "", MemberVariablePreVariables(modelInfo, hpcOmMemory, stringBool(useMemoryOptimization), false), generateAdditionalPublicMemberDeclaration(simCode, &extraFuncs, &extraFuncsDecl, ""), preVarsCount, false), 'OMCpp<%fileNamePrefix%>PreVariables.h')
let() = textFile(simulationWriteOutputCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", stateDerVectorName, stringBool(useMemoryOptimization)), 'OMCpp<%fileNamePrefix%>WriteOutput.cpp')
let() = textFile(simulationPreVarsCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", stateDerVectorName, stringBool(useMemoryOptimization)), 'OMCpp<%fileNamePrefix%>PreVariables.cpp')
let() = textFile(simulationWriteOutputAlgVarsCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", stateDerVectorName, stringBool(useMemoryOptimization)), 'OMCpp<%fileNamePrefix%>WriteOutputAlgVars.cpp')
let() = textFile(simulationWriteOutputParameterCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", stringBool(useMemoryOptimization)), 'OMCpp<%fileNamePrefix%>WriteOutputParameter.cpp')
let() = textFile(simulationWriteOutputAliasVarsCppFile(simCode, &extraFuncs, &extraFuncsDecl, "", stateDerVectorName, stringBool(useMemoryOptimization)), 'OMCpp<%fileNamePrefix%>WriteOutputAliasVars.cpp')
Expand Down Expand Up @@ -407,7 +405,6 @@ template simulationCppFile(SimCode simCode, Context context, Text& extraFuncs, T
<<
#include <Core/Modelica.h>
#include <Core/ModelicaDefine.h>
#include "OMCpp<%fileNamePrefix%>PreVariables.h"
#include "OMCpp<%fileNamePrefix%>.h"
#include "OMCpp<%fileNamePrefix%>Functions.h"
#include <Core/System/EventHandling.h>
Expand All @@ -419,7 +416,6 @@ template simulationCppFile(SimCode simCode, Context context, Text& extraFuncs, T
/* Constructor */
<%className%>::<%className%>(IGlobalSettings* globalSettings, boost::shared_ptr<IAlgLoopSolverFactory> nonlinsolverfactory, boost::shared_ptr<ISimData> sim_data)
: SystemDefaultImplementation(globalSettings)
, <%className%>PreVariables()
, _algLoopSolverFactory(nonlinsolverfactory)
, _sim_data(sim_data)
<%hpcomMemberVariableDefinition%>
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -2597,6 +2597,12 @@ package Util
input list<Integer> lst;
output Integer i;
end intProduct;

function sumStringDelimit2Int
input String lst;
input String delim;
output Integer i;
end sumStringDelimit2Int;

function testsuiteFriendly
input String in;
Expand Down
15 changes: 15 additions & 0 deletions Compiler/Util/Util.mo
Expand Up @@ -483,6 +483,21 @@ algorithm
str := stringDelimitList(lst1, delim);
end stringDelimitListNonEmptyElts;

public function sumStringDelimit2Int
" splits the input string at the delimiter string in list of strings and converts to integer list which is then summarized
"
input String inString;
input String delim;
output Integer i;
protected
list<String> lst;
list<Integer> lst2;
algorithm
lst:=stringSplitAtChar(inString,delim);
lst2:=List.map(lst, stringInt);
i:=List.fold(lst2,intAdd,0);
end sumStringDelimit2Int;

public function stringReplaceChar "Takes a string and two chars and replaces the first char with the second char:
Example: string_replace_char(\"hej.b.c\",\".\",\"_\") => \"hej_b_c\"
2007-11-26 BZ: Now it is possible to replace chars with emptychar, and
Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/cpp/Core/Modelica/ModelicaSystem.cpp
Expand Up @@ -7,8 +7,8 @@
#include <Core/DataExchange/Policies/TextfileWriter.h>
#include <Core/Modelica/ModelicaSystem.h>

Modelica::Modelica(IGlobalSettings* globalSettings, boost::shared_ptr<IAlgLoopSolverFactory> nonlinsolverfactory, boost::shared_ptr<ISimData>)
: SystemDefaultImplementation(globalSettings)
Modelica::Modelica(IGlobalSettings* globalSettings, boost::shared_ptr<IAlgLoopSolverFactory> nonlinsolverfactory, boost::shared_ptr<ISimData> sim_data, boost::shared_ptr<ISimVars> sim_vars)
: SystemDefaultImplementation(globalSettings,sim_data,sim_vars)
{
_dimBoolean = 0;
_dimInteger = 0;
Expand Down
65 changes: 52 additions & 13 deletions SimulationRuntime/cpp/Core/SimController/SimController.cpp
Expand Up @@ -17,7 +17,10 @@ SimController::SimController(PATH library_path, PATH modelicasystem_path)

SimController::~SimController()
{
_systems.clear();

// _systems.clear();
// _sim_vars.clear();

}
/*
#if defined(__TRICORE__) || defined(__vxworks)
Expand Down Expand Up @@ -55,11 +58,12 @@ boost::weak_ptr<IMixedSystem> SimController::LoadSystem(string modelLib,string m
}
//destroy system
_systems.erase(iter);
LoadSimData(modelKey);

}
boost::shared_ptr<ISimData> simData = getSimData(modelKey).lock();
boost::shared_ptr<ISimVars> simVars = getSimVars(modelKey).lock();
//create system
boost::shared_ptr<IMixedSystem> system = createSystem(modelLib, modelKey, _config->getGlobalSettings(), _algloopsolverfactory,simData);
boost::shared_ptr<IMixedSystem> system = createSystem(modelLib, modelKey, _config->getGlobalSettings(), _algloopsolverfactory,simData,simVars);
_systems[modelKey] = system;
return system;
}
Expand All @@ -80,10 +84,11 @@ boost::weak_ptr<IMixedSystem> SimController::LoadModelicaSystem(PATH modelica_pa
}
//destroy system
_systems.erase(iter);
LoadSimData(modelKey);

}
boost::shared_ptr<ISimData> simData = getSimData(modelKey).lock();
boost::shared_ptr<IMixedSystem> system = createModelicaSystem(modelica_path, modelKey, _config->getGlobalSettings(), _algloopsolverfactory,simData);
boost::shared_ptr<ISimVars> simVars = getSimVars(modelKey).lock();
boost::shared_ptr<IMixedSystem> system = createModelicaSystem(modelica_path, modelKey, _config->getGlobalSettings(), _algloopsolverfactory,simData,simVars);
_systems[modelKey] = system;
return system;
}
Expand All @@ -105,13 +110,28 @@ boost::weak_ptr<ISimData> SimController::LoadSimData(string modelKey)
boost::shared_ptr<ISimData> sim_data = createSimData();
_sim_data[modelKey] = sim_data;
return sim_data;
}


boost::weak_ptr<ISimVars> SimController::LoadSimVars(string modelKey,size_t dim_real,size_t dim_int,size_t dim_bool,size_t dim_pre_vars,size_t dim_z,size_t z_i)
{
//if the simdata is already loaded
std::map<string,boost::shared_ptr<ISimVars> > ::iterator iter = _sim_vars.find(modelKey);
if(iter!=_sim_vars.end())
{

//destroy system
_sim_vars.erase(iter);

}
//create system
boost::shared_ptr<ISimVars> sim_vars = createSimVars(dim_real,dim_int,dim_bool,dim_pre_vars,dim_z,z_i);
_sim_vars[modelKey] = sim_vars;
return sim_vars;
}



boost::weak_ptr<ISimData> SimController::getSimData(string modelname)
{

Expand All @@ -128,6 +148,24 @@ boost::weak_ptr<ISimData> SimController::getSimData(string modelname)
}
}

boost::weak_ptr<ISimVars> SimController::getSimVars(string modelname)
{


std::map<string,boost::shared_ptr<ISimVars> >::iterator iter = _sim_vars.find(modelname);
if(iter!=_sim_vars.end())
{
return iter->second;
}
else
{
string error = string("Simulation data was not found for model: ") + modelname;
throw ModelicaSimulationError(SIMMANAGER,error);
}
}



boost::weak_ptr<IMixedSystem> SimController::getSystem(string modelname)
{

Expand Down Expand Up @@ -166,7 +204,7 @@ void SimController::StartVxWorks(SimSettings simsettings,string modelKey)
global_settings->setLogType(simsettings.logType);
global_settings->setOutputPointType(simsettings.outputPointType);

_simMgr = boost::shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get()));
boost::shared_ptr<SimManager> simMgr = boost::shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get()));

ISolverSettings* solver_settings = _config->getSolverSettings();
solver_settings->setLowerLimit(simsettings.lower_limit);
Expand All @@ -175,7 +213,7 @@ void SimController::StartVxWorks(SimSettings simsettings,string modelKey)
solver_settings->setRTol(simsettings.tolerance);
solver_settings->setATol(simsettings.tolerance);

_simMgr->initialize();
simMgr->initialize();
}
catch( ModelicaSimulationError& ex)
{
Expand All @@ -188,7 +226,7 @@ void SimController::StartVxWorks(SimSettings simsettings,string modelKey)
// Added for real-time simulation using VxWorks and Bodas
void SimController::calcOneStep()
{
_simMgr->runSingleStep();
//_simMgr->runSingleStep();
}


Expand All @@ -214,17 +252,17 @@ void SimController::Start(SimSettings simsettings, string modelKey)
global_settings->setAlarmTime(simsettings.timeOut);
global_settings->setOutputPointType(simsettings.outputPointType);

_simMgr = boost::shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get()));
boost::shared_ptr<SimManager> simMgr = boost::shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get()));

ISolverSettings* solver_settings = _config->getSolverSettings();
solver_settings->setLowerLimit(simsettings.lower_limit);
solver_settings->sethInit(simsettings.lower_limit);
solver_settings->setUpperLimit(simsettings.upper_limit);
solver_settings->setRTol(simsettings.tolerance);
solver_settings->setATol(simsettings.tolerance);
_simMgr->initialize();
simMgr->initialize();

_simMgr->runSimulation();
simMgr->runSimulation();

boost::shared_ptr<IWriteOutput> writeoutput_system = boost::dynamic_pointer_cast<IWriteOutput>(mixedsystem);

Expand Down Expand Up @@ -264,6 +302,7 @@ void SimController::Start(SimSettings simsettings, string modelKey)

void SimController::Stop()
{
if(_simMgr)
_simMgr->stopSimulation();
/* if(_simMgr)
_simMgr->stopSimulation();
*/
}
7 changes: 4 additions & 3 deletions SimulationRuntime/cpp/Core/System/CMakeLists.txt
Expand Up @@ -12,7 +12,7 @@ set(BASE_LIB "" CACHE INTERNAL "" )

# add the system default implementation library
# add a static library with the system default implementation for generating c++ fmu targets
add_library(${SystemName}_FMU STATIC AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp)
add_library(${SystemName}_FMU STATIC AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp SimVars.cpp)
set_target_properties(${SystemName}_FMU PROPERTIES COMPILE_DEFINITIONS "DRUNTIME_STATIC_LINKING")
include_directories(${SUNDIALS_INCLUDE_DIR}/cvodes ${SUNDIALS_INCLUDE_DIR}/nvector ${SUNDIALS_INCLUDE_DIR}/sundials ${SUNDIALS_INCLUDE_DIR}/kinsol ${SUNDIALS_INCLUDE_DIR})
if (UNIX)
Expand All @@ -21,15 +21,15 @@ endif(UNIX)
install (TARGETS ${SystemName}_FMU DESTINATION lib/omc/${LIBINSTALLEXT})

IF(RUNTIME_STATIC_LINKING)
add_library(${SystemName}_static STATIC AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp FactoryExport.cpp)
add_library(${SystemName}_static STATIC AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp SimVars.cpp FactoryExport.cpp)
set_target_properties(${SystemName}_static PROPERTIES COMPILE_DEFINITIONS "DRUNTIME_STATIC_LINKING")
include_directories(${SUNDIALS_INCLUDE_DIR}/cvodes ${SUNDIALS_INCLUDE_DIR}/nvector ${SUNDIALS_INCLUDE_DIR}/sundials ${SUNDIALS_INCLUDE_DIR}/kinsol ${SUNDIALS_INCLUDE_DIR})
if (UNIX)
set_target_properties(${SystemName}_static PROPERTIES COMPILE_FLAGS -fPIC)
endif(UNIX)
install (TARGETS ${SystemName}_static DESTINATION lib/omc/${LIBINSTALLEXT})
ELSE(RUNTIME_STATIC_LINKING)
add_library(${SystemName} SHARED AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp FactoryExport.cpp)
add_library(${SystemName} SHARED AlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp SimVars.cpp FactoryExport.cpp)
target_link_libraries (${SystemName} ${Boost_LIBRARIES} ${LAPACK_LIBRARIES} ${OMCFactoryName} )

install (TARGETS ${SystemName} DESTINATION lib/omc/${LIBINSTALLEXT})
Expand Down Expand Up @@ -59,6 +59,7 @@ install (FILES
${CMAKE_SOURCE_DIR}/Include/Core/System/IStepEvent.h
${CMAKE_SOURCE_DIR}/Include/Core/System/IWriteOutput.h
${CMAKE_SOURCE_DIR}/Include/Core/System/IStateSelection.h
${CMAKE_SOURCE_DIR}/Include/Core/System/ISimVars.h
DESTINATION include/omc/cpp/Core/System)


Expand Down
39 changes: 18 additions & 21 deletions SimulationRuntime/cpp/Core/System/DiscreteEvents.cpp
Expand Up @@ -5,8 +5,8 @@
#include <Core/Math/Functions.h>


DiscreteEvents::DiscreteEvents(PreVariables* preVars)
: _preVars(preVars)
DiscreteEvents::DiscreteEvents(boost::shared_ptr<ISimVars> sim_vars)
: _sim_vars(sim_vars)
{
}

Expand All @@ -22,7 +22,7 @@ Inits the event variables
void DiscreteEvents::initialize()
{

_preVars->initPreVariables();
_sim_vars->initPreVariables();
//_preVars->_pre_vars.resize((boost::extents[_preVars->_pre_real_vars_idx.size()+_preVars->_pre_int_vars_idx.size()+_preVars->_pre_bool_vars_idx.size()]));
}

Expand All @@ -39,8 +39,7 @@ Saves a variable in _preVars->_pre_vars vector

void DiscreteEvents::save(double& var)
{
unsigned int i = _preVars->_pre_real_vars_idx[&var];
_preVars->_pre_vars[i]=var;
_sim_vars->setPreVar(var);
}

/**
Expand All @@ -49,8 +48,7 @@ Saves a variable in _preVars->_pre_vars vector

void DiscreteEvents::save(int& var)
{
unsigned int i = _preVars->_pre_int_vars_idx[&var];
_preVars->_pre_vars[i]=var;
_sim_vars->setPreVar(var);
}

/**
Expand All @@ -59,17 +57,16 @@ Saves a variable in _preVars->_pre_vars vector

void DiscreteEvents::save(bool& var)
{
unsigned int i = _preVars->_pre_bool_vars_idx[&var];
_preVars->_pre_vars[i]=var;
_sim_vars->setPreVar(var);
}

/**
Implementation of the Modelica pre operator
*/
double DiscreteEvents::pre(double& var)
{
unsigned int i = _preVars->_pre_real_vars_idx[&var];
return _preVars->_pre_vars[i];
double& pre_var = _sim_vars->getPreVar(var);
return pre_var;

}

Expand All @@ -78,8 +75,8 @@ Implementation of the Modelica pre operator
*/
double DiscreteEvents::pre(int& var)
{
unsigned int i = _preVars->_pre_int_vars_idx[&var];
return _preVars->_pre_vars[i];
double& pre_var = _sim_vars->getPreVar(var);
return pre_var;

}

Expand All @@ -88,8 +85,8 @@ Implementation of the Modelica pre operator
*/
double DiscreteEvents::pre(bool& var)
{
unsigned int i = _preVars->_pre_bool_vars_idx[&var];
return _preVars->_pre_vars[i];
double& pre_var = _sim_vars->getPreVar(var);
return pre_var;

}
/**
Expand Down Expand Up @@ -149,21 +146,21 @@ bool DiscreteEvents::change(bool& var)

bool DiscreteEvents::changeDiscreteVar(double& var)
{
unsigned int i = _preVars->_pre_real_vars_idx[&var];
return var != _preVars->_pre_vars[i];
double& pre_var = _sim_vars->getPreVar(var);
return var != pre_var;

}
bool DiscreteEvents::changeDiscreteVar(int& var)
{
unsigned int i = _preVars->_pre_int_vars_idx[&var];
return var != _preVars->_pre_vars[i];
double& pre_var = _sim_vars->getPreVar(var);
return var != pre_var;

}

bool DiscreteEvents::changeDiscreteVar(bool& var)
{
unsigned int i = _preVars->_pre_bool_vars_idx[&var];
return var != _preVars->_pre_vars[i];
double& pre_var = _sim_vars->getPreVar(var);
return var != pre_var;

}

Expand Down
10 changes: 3 additions & 7 deletions SimulationRuntime/cpp/Core/System/EventHandling.cpp
Expand Up @@ -23,14 +23,10 @@ EventHandling::~EventHandling(void)
/**
Inits the event variables
*/
boost::shared_ptr<DiscreteEvents> EventHandling::initialize(IEvent* event_system)
boost::shared_ptr<DiscreteEvents> EventHandling::initialize(IEvent* event_system,boost::shared_ptr<ISimVars> sim_vars)
{
//initialize prevars
//event_system->initPreVariables(preVars->_pre_real_vars_idx,preVars->_pre_int_vars_idx,preVars->_pre_bool_vars_idx);
//preVars->_pre_vars.resize((boost::extents[preVars->_pre_real_vars_idx.size()+preVars->_pre_int_vars_idx.size()+preVars->_pre_bool_vars_idx.size()]));
//initialize discrete event handling
PreVariables* preVars = dynamic_cast<PreVariables*>(event_system);
boost::shared_ptr<DiscreteEvents> discreteEvents = boost::shared_ptr<DiscreteEvents>(new DiscreteEvents(preVars));

boost::shared_ptr<DiscreteEvents> discreteEvents = boost::shared_ptr<DiscreteEvents>(new DiscreteEvents(sim_vars));
discreteEvents->initialize();
//initialize continuous event handling
_continuousEvents->initialize(event_system);
Expand Down

0 comments on commit cdfcc84

Please sign in to comment.