Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

[cpp] Implement initial state selection for cpp #3056

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions Compiler/Template/CodegenCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ case SIMCODE(modelInfo=MODELINFO(__)) then
void setAMatrix(unsigned int index, DynArrayDim2<int>& A);
bool getAMatrix(unsigned int index, DynArrayDim1<int>& A);
void setAMatrix(unsigned int index, DynArrayDim1<int>& A);

bool stateSelection();
bool stateSelectionSet(int i);
protected:
void initialize();
};
Expand Down Expand Up @@ -370,7 +371,8 @@ template getPreVarsCount(ModelInfo modelInfo)
::=
match modelInfo
case MODELINFO(varInfo=VARINFO(__)) then
let allVarCount = intAdd(stringInt(numRealvars(modelInfo)), intAdd(stringInt(numIntvars(modelInfo)), stringInt(numBoolvars(modelInfo))))
let allVarCount = intAdd(stringInt(
numRealvars(modelInfo)), intAdd(stringInt(numIntvars(modelInfo)), stringInt(numBoolvars(modelInfo))))
<<
<%allVarCount%>
>>
Expand Down Expand Up @@ -696,6 +698,17 @@ case SIMCODE(modelInfo = MODELINFO(__)) then

<%functionDimStateSets(stateSets, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace)%>
<%functionStateSets(stateSets, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>

bool <%lastIdentOfPath(modelInfo.name)%>StateSelection::stateSelection()
{
return SystemDefaultImplementation::stateSelection();
}

bool <%lastIdentOfPath(modelInfo.name)%>StateSelection::stateSelectionSet(int i)
{
return SystemDefaultImplementation::stateSelectionSet(i);
}

>>
end simulationStateSelectionCppFile;

Expand Down Expand Up @@ -3733,12 +3746,8 @@ match simCode

<%partitionInit%>

//Initialize the state vector
SystemDefaultImplementation::initialize();
//Instantiate auxiliary object for event handling functionality
//_event_handling.getCondition = boost::bind(&<%className%>::getCondition, this, _1);

//Todo: reindex all arrays removed // arrayReindex(modelInfo,useFlatArrayNotation)


_functions = new Functions(_simTime,__z,__zDot,_initial,_terminate);
>>
Expand Down Expand Up @@ -5753,6 +5762,8 @@ case SIMCODE(modelInfo = MODELINFO(__),makefileParams = MAKEFILE_PARAMS(__)) th

void <%lastIdentOfPath(modelInfo.name)%>Initialize::initializeMemory()
{

SystemDefaultImplementation::initialize();
_discrete_events = _event_handling->initialize(this,getSimVars());

//create and initialize Algloopsolvers
Expand Down Expand Up @@ -8408,13 +8419,6 @@ template variableType(DAE.Type type)
case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) then "void*"
end variableType;

template lastIdentOfPath(Path modelName) ::=
match modelName
case QUALIFIED(__) then lastIdentOfPath(path)
case IDENT(__) then name
case FULLYQUALIFIED(__) then lastIdentOfPath(path)
end lastIdentOfPath;

template identOfPath(Path modelName) ::=
match modelName
case QUALIFIED(__) then '<%name%>_<%lastIdentOfPath(path)%>'
Expand Down
11 changes: 11 additions & 0 deletions Compiler/Template/CodegenCppCommon.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,9 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
case CALL(path=IDENT(name="$getPart"), expLst={e1}) then
daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)

case CALL(path=IDENT(name="$stateSelectionSet"), expLst=ICONST(integer=setIndex)::_) then
'SystemDefaultImplementation::stateSelectionSet(<%setIndex%>)'

case CALL(path=IDENT(name="sample"), expLst={ICONST(integer=index), start, interval}) then
let &preExp = buffer "" /*BUFD*/
let eStart = daeExp(start, contextOther, &preExp, &varDecls, simCode, &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
Expand Down Expand Up @@ -3118,5 +3121,13 @@ template timeEventLength(SimCode simCode)
>>
end timeEventLength;

template lastIdentOfPath(Path modelName)
::=
match modelName
case QUALIFIED(__) then lastIdentOfPath(path)
case IDENT(__) then name
case FULLYQUALIFIED(__) then lastIdentOfPath(path)
end lastIdentOfPath;

annotation(__OpenModelica_Interface="backend");
end CodegenCppCommon;
3 changes: 2 additions & 1 deletion Compiler/Template/CodegenFMUCppHpcom.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import CodegenFMUCpp.*;
import CodegenCppHpcom.*;
import CodegenUtil;
import CodegenCpp;
import CodegenCppCommon;
import CodegenCppInit;

template translateModel(SimCode simCode, String FMUVersion, String FMUType)
Expand All @@ -68,7 +69,7 @@ template translateModel(SimCode simCode, String FMUVersion, String FMUType)
let &extraFuncsDecl = buffer "" /*BUFD*/
let &complexStartExpressions = buffer ""

let className = CodegenCpp.lastIdentOfPath(modelInfo.name)
let className = CodegenCppCommon.lastIdentOfPath(modelInfo.name)
let numRealVars = numRealvarsHpcom(modelInfo, hpcomData.hpcOmMemory)
let numIntVars = numIntvarsHpcom(modelInfo, hpcomData.hpcOmMemory)
let numBoolVars = numBoolvarsHpcom(modelInfo, hpcomData.hpcOmMemory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ void Initialization::initializeSystem()
delete[] conditions0;
delete[] conditions1;
}
/** @} */ // end of coreSimcontroller
/** @} */ // end of coreSimcontroller
3 changes: 1 addition & 2 deletions SimulationRuntime/cpp/Core/Solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.9)

project(${SolverName})

add_library(${SolverName} SolverDefaultImplementation.cpp AlgLoopSolverDefaultImplementation.cpp SolverSettings.cpp SystemStateSelection.cpp FactoryExport.cpp SimulationMonitor.cpp)
add_library(${SolverName} SolverDefaultImplementation.cpp AlgLoopSolverDefaultImplementation.cpp SolverSettings.cpp FactoryExport.cpp SimulationMonitor.cpp)

if(NOT BUILD_SHARED_LIBS)
set_target_properties(${SolverName} PROPERTIES COMPILE_DEFINITIONS "RUNTIME_STATIC_LINKING;ENABLE_SUNDIALS_STATIC")
Expand All @@ -23,7 +23,6 @@ install(FILES
${CMAKE_SOURCE_DIR}/Include/Core/Solver/ISolverSettings.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/SolverSettings.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/SolverDefaultImplementation.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/SystemStateSelection.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/SimulationMonitor.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/FactoryExport.h
DESTINATION include/omc/cpp/Core/Solver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ SolverDefaultImplementation::SolverDefaultImplementation(IMixedSystem* system, I
, _solverStatus (ISolver::UNDEF_STATUS)
, _outputCommand (IWriteOutput::WRITEOUT)
{
_state_selection = shared_ptr<SystemStateSelection>(new SystemStateSelection(system));

#ifdef RUNTIME_PROFILING
if(MeasureTime::getInstance() != NULL)
Expand Down Expand Up @@ -103,11 +102,6 @@ const ISolver::SOLVERSTATUS SolverDefaultImplementation::getSolverStatus()
return _solverStatus;
};

bool SolverDefaultImplementation::stateSelection()
{
return _state_selection->stateSelection(1);
}

void SolverDefaultImplementation::initialize()
{
SimulationMonitor::initialize();
Expand Down
5 changes: 3 additions & 2 deletions SimulationRuntime/cpp/Core/System/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ set(BASE_LIB "" CACHE INTERNAL "" )
#ENDIF (NOT ((${CMAKE_SYSTEM_NAME} MATCHES "Darwin") OR MSVC))

# add the system default implementation library
add_library(${SystemName} LinearAlgLoopDefaultImplementation.cpp NonLinearAlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp SimVars.cpp FactoryExport.cpp)
add_library(${SystemName} SystemStateSelection.cpp LinearAlgLoopDefaultImplementation.cpp NonLinearAlgLoopDefaultImplementation.cpp AlgLoopSolverFactory.cpp EventHandling.cpp DiscreteEvents.cpp ContinuousEvents.cpp SystemDefaultImplementation.cpp SimVars.cpp FactoryExport.cpp)

if(NOT BUILD_SHARED_LIBS)
set_target_properties(${SystemName} PROPERTIES COMPILE_DEFINITIONS "RUNTIME_STATIC_LINKING;ENABLE_SUNDIALS_STATIC")
endif(NOT BUILD_SHARED_LIBS)

target_link_libraries(${SystemName} ${Boost_LIBRARIES} ${LAPACK_LIBRARIES} ${OMCFactoryName} ${SimulationSettings} ${ExtensionUtilitiesName})
target_link_libraries(${SystemName} ${Boost_LIBRARIES} ${LAPACK_LIBRARIES} ${MathName} ${OMCFactoryName} ${SimulationSettings} ${ExtensionUtilitiesName})
add_precompiled_header(${SystemName} Include/Core/Modelica.h)

install(FILES $<TARGET_PDB_FILE:${SystemName}> DESTINATION ${LIBINSTALLEXT} OPTIONAL)
Expand All @@ -27,6 +27,7 @@ install(TARGETS ${SystemName} DESTINATION ${LIBINSTALLEXT})
#install (TARGETS ${BasiLibName} DESTINATION ${LIBINSTALLEXT})
#ENDIF (NOT ((${CMAKE_SYSTEM_NAME} MATCHES "Darwin") OR MSVC))
install(FILES
${CMAKE_SOURCE_DIR}/Include/Core/System/SystemStateSelection.h
${CMAKE_SOURCE_DIR}/Include/Core/System/SystemDefaultImplementation.h
${CMAKE_SOURCE_DIR}/Include/Core/System/LinearAlgLoopDefaultImplementation.h
${CMAKE_SOURCE_DIR}/Include/Core/System/NonLinearAlgLoopDefaultImplementation.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>

#include <Core/System/SystemStateSelection.h>
#include <Core/System/FactoryExport.h>
#include <Core/Utils/extension/logger.hpp>
#include <Core/System/EventHandling.h>
#include <Core/System/SystemDefaultImplementation.h>
#include <Core/System/AlgLoopSolverFactory.h>


template <class T>
void InitVars<T>::setStartValue(T& variable,T val,bool overwriteOldValue)
{
Expand Down Expand Up @@ -73,6 +73,7 @@ SystemDefaultImplementation::SystemDefaultImplementation(IGlobalSettings *global
, _modelName(modelName)
, _freeVariablesLock(false)
{
_state_selection = shared_ptr<SystemStateSelection>(new SystemStateSelection(this));
}

SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementation& instance)
Expand Down Expand Up @@ -114,6 +115,8 @@ SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementa
, _modelName(instance.getModelName())
, _freeVariablesLock(false)
{

_state_selection = shared_ptr<SystemStateSelection>(new SystemStateSelection(this));
}

/*
Expand Down Expand Up @@ -214,6 +217,7 @@ int SystemDefaultImplementation::getDimRHS() const
/// (Re-) initialize the system of equations
void SystemDefaultImplementation::initialize()
{
_state_selection->initialize();
_callType = IContinuous::CONTINUOUS;

/*
Expand Down Expand Up @@ -804,6 +808,16 @@ double SystemDefaultImplementation::computeNextTimeEvents(double currTime, std::
return closestTimeEvent;
}

bool SystemDefaultImplementation::stateSelection()
{
return _state_selection->stateSelection(1);
}

bool SystemDefaultImplementation::stateSelectionSet(int i)
{
return _state_selection->stateSelectionSet(1,i);
}

/** @} */ // end of coreSystem

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@
*/
#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>
#include <Core/Solver/FactoryExport.h>
#include <Core/Solver/SystemStateSelection.h>
#include <Core/System/FactoryExport.h>
#include <Core/System/SystemDefaultImplementation.h>
#include <Core/System/SystemStateSelection.h>
#include <Core/Math/ArrayOperations.h>
#include <Core/Math/Functions.h>


SystemStateSelection::SystemStateSelection(IMixedSystem* system)
SystemStateSelection::SystemStateSelection(SystemDefaultImplementation* system)
:_system(system)
,_colPivot()
,_rowPivot()
,_initialized(false)

{

_state_selection = dynamic_cast<IStateSelection*>(system);
if ( !_state_selection)
throw ModelicaSimulationError(MATH_FUNCTION,"No state selection system");


}

void SystemStateSelection::initialize()
{
#if defined(__vxworks)
#else
_dimStateSets = _state_selection->getDimStateSets();

_state_selection = dynamic_cast<IStateSelection*>(_system);
if (!_state_selection)
throw ModelicaSimulationError(MATH_FUNCTION, "No state selection system");

_mixed_selection = dynamic_cast<IMixedSystem*>(_system);
if (!_mixed_selection)
throw ModelicaSimulationError(MATH_FUNCTION, "No mixed system");
_dimStateSets = _state_selection->getDimStateSets();

_dimStates.clear();
_dimStateCanditates.clear();
Expand Down Expand Up @@ -66,23 +73,28 @@ SystemStateSelection::~SystemStateSelection()

bool SystemStateSelection::stateSelection(int switchStates)
{
#if defined(__vxworks)
return true;

#else
if(!_initialized)
initialize();
int res=0;
int changed = false;
for(int i=0; i<_dimStateSets; i++)
{
boost::shared_array<int> oldColPivot(new int[_dimStateCanditates[i]]);
boost::shared_array<int> oldRowPivot(new int[_dimDummyStates[i]]);
const matrix_t& stateset_matrix = _system->getStateSetJacobian(i);

/* call pivoting function to select the states */

#if defined(__vxworks)
return true;
#else
if(!_initialized)
initialize();
int changed = false;
for(int i=0; i<_dimStateSets; i++)
{
changed = changed || stateSelectionSet(switchStates, i);
}
return changed;
#endif
}

bool SystemStateSelection::stateSelectionSet(int switchStates, int i)
{
int res=0;
int changed = false;
boost::shared_array<int> oldColPivot(new int[_dimStateCanditates[i]]);
boost::shared_array<int> oldRowPivot(new int[_dimDummyStates[i]]);
const matrix_t& stateset_matrix = _mixed_selection->getStateSetJacobian(i);
/* call pivoting function to select the states */

memcpy(oldColPivot.get(), _colPivot[i].get(), _dimStateCanditates[i]*sizeof(int));
memcpy(oldRowPivot.get(), _rowPivot[i].get(), _dimDummyStates[i]*sizeof(int));
Expand All @@ -106,17 +118,13 @@ return true;
{
memcpy(_colPivot[i].get(), oldColPivot.get(), _dimStateCanditates[i]*sizeof(int));
memcpy(_rowPivot[i].get(), oldRowPivot.get(), _dimDummyStates[i]*sizeof(int));


}
delete [] jac_;
if(res)
changed = true;
else
changed = false;
}
return changed;
#endif
return changed;
}

void SystemStateSelection::setAMatrix(int* newEnable, unsigned int index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*
* @{
*/
#include <Core/Solver/SystemStateSelection.h>
#include <Core/Solver/SimulationMonitor.h>
#ifdef RUNTIME_PROFILING
#include <Core/Utils/extension/measure_time.hpp>
Expand Down Expand Up @@ -54,7 +53,6 @@ class BOOST_EXTENSION_SOLVER_DECL SolverDefaultImplementation : public Simulatio

/// Called by solver after every successful integration step (calls writeOutput)
void writeToFile(const int& stp, const double& t, const double& h);
virtual bool stateSelection();

protected:
// Member variables
Expand All @@ -65,7 +63,6 @@ class BOOST_EXTENSION_SOLVER_DECL SolverDefaultImplementation : public Simulatio
ISolverSettings
*_settings; ///< Settings for the solver

shared_ptr<SystemStateSelection> _state_selection;
double
_tInit, ///< (initiale) Startzeit (wird nicht vom Solver verändert)
_tCurrent, ///< current time (is changed by the solver)
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/Include/Core/System/IStateSelection.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ class IStateSelection
virtual void setAMatrix(unsigned int index, DynArrayDim2<int>& A) = 0;
virtual bool getAMatrix(unsigned int index, DynArrayDim1<int> & A) = 0 ;
virtual void setAMatrix(unsigned int index, DynArrayDim1<int>& A) = 0;
virtual bool stateSelection() = 0;
virtual bool stateSelectionSet(int i) = 0;
};
/** @} */ // end of coreSystem
Loading