From eb3156525bb9880a6f8985d51cb1cb2ebf383a5d Mon Sep 17 00:00:00 2001 From: kabdelhak Date: Thu, 18 Apr 2019 11:24:16 +0200 Subject: [PATCH] [cpp] Implement initial state selection for cpp - Split up selection for individual sets - Moved state selection from Solver to System --- Compiler/Template/CodegenCpp.tpl | 32 ++++++---- Compiler/Template/CodegenCppCommon.tpl | 11 ++++ Compiler/Template/CodegenFMUCppHpcom.tpl | 3 +- .../cpp/Core/SimController/Initialization.cpp | 2 +- .../cpp/Core/Solver/CMakeLists.txt | 3 +- .../Solver/SolverDefaultImplementation.cpp | 6 -- .../cpp/Core/System/CMakeLists.txt | 5 +- .../System/SystemDefaultImplementation.cpp | 16 ++++- .../SystemStateSelection.cpp | 64 +++++++++++-------- .../Core/Solver/SolverDefaultImplementation.h | 3 - .../cpp/Include/Core/System/IStateSelection.h | 2 + .../Core/System/SystemDefaultImplementation.h | 7 ++ .../{Solver => System}/SystemStateSelection.h | 16 ++--- .../cpp/Include/Solver/ARKode/ARKode.h | 1 + .../cpp/Include/Solver/CVode/CVode.h | 6 +- .../cpp/Include/Solver/Euler/Euler.h | 3 +- .../cpp/Include/Solver/IDA/IDA.h | 1 + .../cpp/Include/Solver/Peer/Peer.h | 1 + .../cpp/Include/Solver/RK12/RK12.h | 1 + .../cpp/Include/Solver/RTEuler/RTEuler.h | 1 + .../cpp/Solver/ARKode/ARKode.cpp | 5 +- SimulationRuntime/cpp/Solver/CVode/CVode.cpp | 10 ++- .../cpp/Solver/CppDASSL/CppDASSL.cpp | 2 +- SimulationRuntime/cpp/Solver/Euler/Euler.cpp | 6 +- SimulationRuntime/cpp/Solver/IDA/IDA.cpp | 4 +- SimulationRuntime/cpp/Solver/Peer/Peer.cpp | 4 +- SimulationRuntime/cpp/Solver/RK12/RK12.cpp | 4 +- .../cpp/Solver/RTEuler/RTEuler.cpp | 3 +- 28 files changed, 142 insertions(+), 80 deletions(-) rename SimulationRuntime/cpp/Core/{Solver => System}/SystemStateSelection.cpp (78%) rename SimulationRuntime/cpp/Include/Core/{Solver => System}/SystemStateSelection.h (71%) diff --git a/Compiler/Template/CodegenCpp.tpl b/Compiler/Template/CodegenCpp.tpl index 856f0cc99b..5dec187afd 100644 --- a/Compiler/Template/CodegenCpp.tpl +++ b/Compiler/Template/CodegenCpp.tpl @@ -321,7 +321,8 @@ case SIMCODE(modelInfo=MODELINFO(__)) then void setAMatrix(unsigned int index, DynArrayDim2& A); bool getAMatrix(unsigned int index, DynArrayDim1& A); void setAMatrix(unsigned int index, DynArrayDim1& A); - + bool stateSelection(); + bool stateSelectionSet(int i); protected: void initialize(); }; @@ -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%> >> @@ -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; @@ -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); >> @@ -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 @@ -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)%>' diff --git a/Compiler/Template/CodegenCppCommon.tpl b/Compiler/Template/CodegenCppCommon.tpl index f027b8d9ee..9879786f7e 100644 --- a/Compiler/Template/CodegenCppCommon.tpl +++ b/Compiler/Template/CodegenCppCommon.tpl @@ -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) @@ -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; diff --git a/Compiler/Template/CodegenFMUCppHpcom.tpl b/Compiler/Template/CodegenFMUCppHpcom.tpl index 0528b042d1..ef9a514be7 100644 --- a/Compiler/Template/CodegenFMUCppHpcom.tpl +++ b/Compiler/Template/CodegenFMUCppHpcom.tpl @@ -53,6 +53,7 @@ import CodegenFMUCpp.*; import CodegenCppHpcom.*; import CodegenUtil; import CodegenCpp; +import CodegenCppCommon; import CodegenCppInit; template translateModel(SimCode simCode, String FMUVersion, String FMUType) @@ -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) diff --git a/SimulationRuntime/cpp/Core/SimController/Initialization.cpp b/SimulationRuntime/cpp/Core/SimController/Initialization.cpp index 987051099b..60de125c00 100644 --- a/SimulationRuntime/cpp/Core/SimController/Initialization.cpp +++ b/SimulationRuntime/cpp/Core/SimController/Initialization.cpp @@ -63,4 +63,4 @@ void Initialization::initializeSystem() delete[] conditions0; delete[] conditions1; } -/** @} */ // end of coreSimcontroller \ No newline at end of file +/** @} */ // end of coreSimcontroller diff --git a/SimulationRuntime/cpp/Core/Solver/CMakeLists.txt b/SimulationRuntime/cpp/Core/Solver/CMakeLists.txt index 6d94277ffb..a2d72c97a1 100644 --- a/SimulationRuntime/cpp/Core/Solver/CMakeLists.txt +++ b/SimulationRuntime/cpp/Core/Solver/CMakeLists.txt @@ -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") @@ -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) diff --git a/SimulationRuntime/cpp/Core/Solver/SolverDefaultImplementation.cpp b/SimulationRuntime/cpp/Core/Solver/SolverDefaultImplementation.cpp index 38802dd046..064a674e28 100644 --- a/SimulationRuntime/cpp/Core/Solver/SolverDefaultImplementation.cpp +++ b/SimulationRuntime/cpp/Core/Solver/SolverDefaultImplementation.cpp @@ -43,7 +43,6 @@ SolverDefaultImplementation::SolverDefaultImplementation(IMixedSystem* system, I , _solverStatus (ISolver::UNDEF_STATUS) , _outputCommand (IWriteOutput::WRITEOUT) { - _state_selection = shared_ptr(new SystemStateSelection(system)); #ifdef RUNTIME_PROFILING if(MeasureTime::getInstance() != NULL) @@ -103,11 +102,6 @@ const ISolver::SOLVERSTATUS SolverDefaultImplementation::getSolverStatus() return _solverStatus; }; -bool SolverDefaultImplementation::stateSelection() -{ - return _state_selection->stateSelection(1); -} - void SolverDefaultImplementation::initialize() { SimulationMonitor::initialize(); diff --git a/SimulationRuntime/cpp/Core/System/CMakeLists.txt b/SimulationRuntime/cpp/Core/System/CMakeLists.txt index 9968e85639..5257e261ad 100644 --- a/SimulationRuntime/cpp/Core/System/CMakeLists.txt +++ b/SimulationRuntime/cpp/Core/System/CMakeLists.txt @@ -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 $ DESTINATION ${LIBINSTALLEXT} OPTIONAL) @@ -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 diff --git a/SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp b/SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp index 62379d4ef1..376b594424 100644 --- a/SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp +++ b/SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp @@ -5,13 +5,13 @@ #include #include +#include #include #include #include #include #include - template void InitVars::setStartValue(T& variable,T val,bool overwriteOldValue) { @@ -73,6 +73,7 @@ SystemDefaultImplementation::SystemDefaultImplementation(IGlobalSettings *global , _modelName(modelName) , _freeVariablesLock(false) { + _state_selection = shared_ptr(new SystemStateSelection(this)); } SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementation& instance) @@ -114,6 +115,8 @@ SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementa , _modelName(instance.getModelName()) , _freeVariablesLock(false) { + + _state_selection = shared_ptr(new SystemStateSelection(this)); } /* @@ -214,6 +217,7 @@ int SystemDefaultImplementation::getDimRHS() const /// (Re-) initialize the system of equations void SystemDefaultImplementation::initialize() { + _state_selection->initialize(); _callType = IContinuous::CONTINUOUS; /* @@ -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 /* diff --git a/SimulationRuntime/cpp/Core/Solver/SystemStateSelection.cpp b/SimulationRuntime/cpp/Core/System/SystemStateSelection.cpp similarity index 78% rename from SimulationRuntime/cpp/Core/Solver/SystemStateSelection.cpp rename to SimulationRuntime/cpp/Core/System/SystemStateSelection.cpp index 3aa8649b45..12fd469a9b 100644 --- a/SimulationRuntime/cpp/Core/Solver/SystemStateSelection.cpp +++ b/SimulationRuntime/cpp/Core/System/SystemStateSelection.cpp @@ -4,13 +4,14 @@ */ #include #include -#include -#include +#include +#include +#include #include #include -SystemStateSelection::SystemStateSelection(IMixedSystem* system) +SystemStateSelection::SystemStateSelection(SystemDefaultImplementation* system) :_system(system) ,_colPivot() ,_rowPivot() @@ -18,9 +19,7 @@ SystemStateSelection::SystemStateSelection(IMixedSystem* system) { - _state_selection = dynamic_cast(system); - if ( !_state_selection) - throw ModelicaSimulationError(MATH_FUNCTION,"No state selection system"); + } @@ -28,7 +27,15 @@ void SystemStateSelection::initialize() { #if defined(__vxworks) #else - _dimStateSets = _state_selection->getDimStateSets(); + + _state_selection = dynamic_cast(_system); + if (!_state_selection) + throw ModelicaSimulationError(MATH_FUNCTION, "No state selection system"); + + _mixed_selection = dynamic_cast(_system); + if (!_mixed_selection) + throw ModelicaSimulationError(MATH_FUNCTION, "No mixed system"); + _dimStateSets = _state_selection->getDimStateSets(); _dimStates.clear(); _dimStateCanditates.clear(); @@ -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 oldColPivot(new int[_dimStateCanditates[i]]); - boost::shared_array 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 oldColPivot(new int[_dimStateCanditates[i]]); + boost::shared_array 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)); @@ -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) diff --git a/SimulationRuntime/cpp/Include/Core/Solver/SolverDefaultImplementation.h b/SimulationRuntime/cpp/Include/Core/Solver/SolverDefaultImplementation.h index 8be4c2c8e6..d1358907a2 100644 --- a/SimulationRuntime/cpp/Include/Core/Solver/SolverDefaultImplementation.h +++ b/SimulationRuntime/cpp/Include/Core/Solver/SolverDefaultImplementation.h @@ -3,7 +3,6 @@ * * @{ */ -#include #include #ifdef RUNTIME_PROFILING #include @@ -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 @@ -65,7 +63,6 @@ class BOOST_EXTENSION_SOLVER_DECL SolverDefaultImplementation : public Simulatio ISolverSettings *_settings; ///< Settings for the solver - shared_ptr _state_selection; double _tInit, ///< (initiale) Startzeit (wird nicht vom Solver verändert) _tCurrent, ///< current time (is changed by the solver) diff --git a/SimulationRuntime/cpp/Include/Core/System/IStateSelection.h b/SimulationRuntime/cpp/Include/Core/System/IStateSelection.h index 8cd34be9c4..4f08113ea6 100644 --- a/SimulationRuntime/cpp/Include/Core/System/IStateSelection.h +++ b/SimulationRuntime/cpp/Include/Core/System/IStateSelection.h @@ -23,5 +23,7 @@ class IStateSelection virtual void setAMatrix(unsigned int index, DynArrayDim2& A) = 0; virtual bool getAMatrix(unsigned int index, DynArrayDim1 & A) = 0 ; virtual void setAMatrix(unsigned int index, DynArrayDim1& A) = 0; + virtual bool stateSelection() = 0; + virtual bool stateSelectionSet(int i) = 0; }; /** @} */ // end of coreSystem diff --git a/SimulationRuntime/cpp/Include/Core/System/SystemDefaultImplementation.h b/SimulationRuntime/cpp/Include/Core/System/SystemDefaultImplementation.h index 347a884186..924f01e99f 100644 --- a/SimulationRuntime/cpp/Include/Core/System/SystemDefaultImplementation.h +++ b/SimulationRuntime/cpp/Include/Core/System/SystemDefaultImplementation.h @@ -3,6 +3,9 @@ * Core module for all algebraic and ode systems * @{ */ + +//#include +class SystemStateSelection; /*****************************************************************************/ /** @@ -158,6 +161,8 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation virtual void setStringStartValue(string& var, string val, bool overwriteOldValue = false); virtual void setStringStartValue(BaseArray& avar, string val, bool overwriteOldValue = true); virtual void setStringStartValue(BaseArray& avar, const BaseArray& aval, bool overwriteOldValue = true); + virtual bool stateSelection(); + virtual bool stateSelectionSet(int i); protected: void Assert(bool cond, const string& msg); void Terminate(string msg); @@ -168,6 +173,7 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation bool isConsistent(); shared_ptr _simObjects; + shared_ptr _state_selection; double _simTime; ///< current simulation time (given by the solver) @@ -231,6 +237,7 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation bool _useAnalyticalJacobian; bool _freeVariablesLock; ///< modification status of independent variables + }; /// Mark free variables unchanged. diff --git a/SimulationRuntime/cpp/Include/Core/Solver/SystemStateSelection.h b/SimulationRuntime/cpp/Include/Core/System/SystemStateSelection.h similarity index 71% rename from SimulationRuntime/cpp/Include/Core/Solver/SystemStateSelection.h rename to SimulationRuntime/cpp/Include/Core/System/SystemStateSelection.h index bc3814de4b..7b1c9f7494 100644 --- a/SimulationRuntime/cpp/Include/Core/Solver/SystemStateSelection.h +++ b/SimulationRuntime/cpp/Include/Core/System/SystemStateSelection.h @@ -1,21 +1,20 @@ #pragma once -/** @addtogroup coreSolver +/** @addtogroup coreSystem * * @{ */ -#if defined(__TRICORE__) || defined(__vxworks) -#define BOOST_EXTENSION_STATESELECT_DECL -#endif + #include #include - -class BOOST_EXTENSION_STATESELECT_DECL SystemStateSelection +class SystemDefaultImplementation; +class SystemStateSelection { public: - SystemStateSelection(IMixedSystem* system); + SystemStateSelection(SystemDefaultImplementation* system); ~SystemStateSelection(); + bool stateSelectionSet(int switchStates, int i); bool stateSelection(int switchStates); void initialize(); @@ -23,8 +22,9 @@ class BOOST_EXTENSION_STATESELECT_DECL SystemStateSelection void setAMatrix(int* newEnable, unsigned int index); int comparePivot(int* oldPivot, int* newPivot, int switchStates, unsigned int index); - IMixedSystem* _system; + SystemDefaultImplementation* _system; IStateSelection* _state_selection; + IMixedSystem* _mixed_selection; vector > _rowPivot; vector > _colPivot; unsigned int _dimStateSets; diff --git a/SimulationRuntime/cpp/Include/Solver/ARKode/ARKode.h b/SimulationRuntime/cpp/Include/Solver/ARKode/ARKode.h index e8209e4461..c01f6567be 100644 --- a/SimulationRuntime/cpp/Include/Solver/ARKode/ARKode.h +++ b/SimulationRuntime/cpp/Include/Solver/ARKode/ARKode.h @@ -174,6 +174,7 @@ double IEvent* _event_system; IMixedSystem* _mixed_system; ITime* _time_system; + IStateSelection* _state_selection; #ifdef RUNTIME_PROFILING std::vector measureTimeFunctionsArray; diff --git a/SimulationRuntime/cpp/Include/Solver/CVode/CVode.h b/SimulationRuntime/cpp/Include/Solver/CVode/CVode.h index 408ca0cf26..c005118f84 100644 --- a/SimulationRuntime/cpp/Include/Solver/CVode/CVode.h +++ b/SimulationRuntime/cpp/Include/Solver/CVode/CVode.h @@ -83,6 +83,7 @@ class Cvode virtual int reportErrorMessage(std::ostream& messageStream); virtual bool stateSelection(); + virtual bool stateSelectionSet(int i); private: // Solveraufruf @@ -173,17 +174,14 @@ double int const* _jacobianAIndex; int const* _jacobianALeadindex; - - - bool _cvode_initialized; - ISystemProperties* _properties; IContinuous* _continuous_system; IEvent* _event_system; IMixedSystem* _mixed_system; ITime* _time_system; + IStateSelection* _state_selection; int _numberOfOdeEvaluations; diff --git a/SimulationRuntime/cpp/Include/Solver/Euler/Euler.h b/SimulationRuntime/cpp/Include/Solver/Euler/Euler.h index 3fb63d31ac..b904586eba 100644 --- a/SimulationRuntime/cpp/Include/Solver/Euler/Euler.h +++ b/SimulationRuntime/cpp/Include/Solver/Euler/Euler.h @@ -191,5 +191,6 @@ class Euler : public ISolver, public SolverDefaultImplementation IEvent* _event_system; IMixedSystem* _mixed_system; ITime* _time_system; + IStateSelection* _state_selection; }; -/** @} */ // end of solverEuler \ No newline at end of file +/** @} */ // end of solverEuler diff --git a/SimulationRuntime/cpp/Include/Solver/IDA/IDA.h b/SimulationRuntime/cpp/Include/Solver/IDA/IDA.h index d2d6c021f7..72bdd32180 100644 --- a/SimulationRuntime/cpp/Include/Solver/IDA/IDA.h +++ b/SimulationRuntime/cpp/Include/Solver/IDA/IDA.h @@ -185,6 +185,7 @@ double IEvent* _event_system; IMixedSystem* _mixed_system; ITime* _time_system; + IStateSelection* _state_selection; #ifdef RUNTIME_PROFILING std::vector *measureTimeFunctionsArray; diff --git a/SimulationRuntime/cpp/Include/Solver/Peer/Peer.h b/SimulationRuntime/cpp/Include/Solver/Peer/Peer.h index 41ebc63535..7e8b99c988 100644 --- a/SimulationRuntime/cpp/Include/Solver/Peer/Peer.h +++ b/SimulationRuntime/cpp/Include/Solver/Peer/Peer.h @@ -138,6 +138,7 @@ class Peer // IEvent* _event_system; // IMixedSystem* _mixed_system; ITime* _time_system[5]; + IStateSelection* _state_selection; // std::vector measureTimeFunctionsArray; // MeasureTimeValues *measuredFunctionStartValues, *measuredFunctionEndValues; diff --git a/SimulationRuntime/cpp/Include/Solver/RK12/RK12.h b/SimulationRuntime/cpp/Include/Solver/RK12/RK12.h index ae1969c36c..421128c784 100644 --- a/SimulationRuntime/cpp/Include/Solver/RK12/RK12.h +++ b/SimulationRuntime/cpp/Include/Solver/RK12/RK12.h @@ -168,5 +168,6 @@ class RK12 : public ISolver, public SolverDefaultImplementation IEvent* _event_system; IMixedSystem* _mixed_system; ITime* _time_system; + IStateSelection* _state_selection; }; /** @} */ // end of solverRK12 diff --git a/SimulationRuntime/cpp/Include/Solver/RTEuler/RTEuler.h b/SimulationRuntime/cpp/Include/Solver/RTEuler/RTEuler.h index 1983f5a2ea..d385b6fa7a 100644 --- a/SimulationRuntime/cpp/Include/Solver/RTEuler/RTEuler.h +++ b/SimulationRuntime/cpp/Include/Solver/RTEuler/RTEuler.h @@ -89,5 +89,6 @@ class RTEuler : public ISolver, public SolverDefaultImplementation IEvent* _event_system; IMixedSystem* _mixed_system; ITime* _time_system; + IStateSelection* _state_selection; }; /** @} */ // end of solverRteuler diff --git a/SimulationRuntime/cpp/Solver/ARKode/ARKode.cpp b/SimulationRuntime/cpp/Solver/ARKode/ARKode.cpp index aaa2350fc0..ce93832865 100644 --- a/SimulationRuntime/cpp/Solver/ARKode/ARKode.cpp +++ b/SimulationRuntime/cpp/Solver/ARKode/ARKode.cpp @@ -89,6 +89,8 @@ void Arkode::initialize() _event_system = dynamic_cast(_system); _mixed_system = dynamic_cast(_system); _time_system = dynamic_cast(_system); + _state_selection = dynamic_cast(_system); + IGlobalSettings* global_settings = dynamic_cast(_arkodesettings)->getGlobalSettings(); // Kennzeichnung, dass initialize()() (vor der Integration) aufgerufen wurde _idid = 5000; @@ -582,8 +584,9 @@ void Arkode::writeArkodeOutput(const double &time, const double &h, const int &s bool Arkode::stateSelection() { - return SolverDefaultImplementation::stateSelection(); + return _state_selection->stateSelection(); } + int Arkode::calcFunction(const double& time, const double* y, double* f) { int returnValue = 0; diff --git a/SimulationRuntime/cpp/Solver/CVode/CVode.cpp b/SimulationRuntime/cpp/Solver/CVode/CVode.cpp index 98f8714228..c6c36707cd 100644 --- a/SimulationRuntime/cpp/Solver/CVode/CVode.cpp +++ b/SimulationRuntime/cpp/Solver/CVode/CVode.cpp @@ -136,6 +136,8 @@ void Cvode::initialize() _event_system = dynamic_cast(_system); _mixed_system = dynamic_cast(_system); _time_system = dynamic_cast(_system); + _state_selection = dynamic_cast(_system); + IGlobalSettings* global_settings = dynamic_cast(_cvodesettings)->getGlobalSettings(); // Kennzeichnung, dass initialize()() (vor der Integration) aufgerufen wurde _idid = 5000; @@ -734,8 +736,14 @@ void Cvode::writeCVodeOutput(const double &time, const double &h, const int &stp bool Cvode::stateSelection() { - return SolverDefaultImplementation::stateSelection(); + return _state_selection->stateSelection(); } + +bool Cvode::stateSelectionSet(int i) +{ + return _state_selection->stateSelectionSet(i); +} + int Cvode::calcFunction(const double& time, const double* y, double* f) { #ifdef RUNTIME_PROFILING diff --git a/SimulationRuntime/cpp/Solver/CppDASSL/CppDASSL.cpp b/SimulationRuntime/cpp/Solver/CppDASSL/CppDASSL.cpp index 859c19cac5..3951683fd4 100644 --- a/SimulationRuntime/cpp/Solver/CppDASSL/CppDASSL.cpp +++ b/SimulationRuntime/cpp/Solver/CppDASSL/CppDASSL.cpp @@ -445,7 +445,7 @@ void CppDASSL::giveZeroVal(const double &t, const double *y, double *zeroValue) bool CppDASSL::stateSelection() { - return SolverDefaultImplementation::stateSelection(); + return _state_selections[0]->stateSelection(); } void CppDASSL::setTimeOut(unsigned int time_out) diff --git a/SimulationRuntime/cpp/Solver/Euler/Euler.cpp b/SimulationRuntime/cpp/Solver/Euler/Euler.cpp index 7881f24c43..66d2a7e63b 100644 --- a/SimulationRuntime/cpp/Solver/Euler/Euler.cpp +++ b/SimulationRuntime/cpp/Solver/Euler/Euler.cpp @@ -61,8 +61,9 @@ Euler::~Euler() bool Euler::stateSelection() { - return SolverDefaultImplementation::stateSelection(); + return _state_selection->stateSelection(); } + void Euler::initialize() { // Kennzeichnung, dass assemble() (vor der Integration) aufgerufen wurde @@ -73,6 +74,7 @@ void Euler::initialize() _event_system = dynamic_cast(_system); _mixed_system = dynamic_cast(_system); _time_system = dynamic_cast(_system); + _state_selection = dynamic_cast(_system); //(Re-) Initialization of solver -> call default implementation service SolverDefaultImplementation::initialize(); @@ -1320,4 +1322,4 @@ void Euler::calcJac(double* yHelp, double* _fHelp, const double* _f, double* jac } } } -/** @} */ // end of solverEuler \ No newline at end of file +/** @} */ // end of solverEuler diff --git a/SimulationRuntime/cpp/Solver/IDA/IDA.cpp b/SimulationRuntime/cpp/Solver/IDA/IDA.cpp index 3ff0f73dc8..64bb5fb8f0 100644 --- a/SimulationRuntime/cpp/Solver/IDA/IDA.cpp +++ b/SimulationRuntime/cpp/Solver/IDA/IDA.cpp @@ -159,6 +159,8 @@ void Ida::initialize() _event_system = dynamic_cast(_system); _mixed_system = dynamic_cast(_system); _time_system = dynamic_cast(_system); + _state_selection = dynamic_cast(_system); + IGlobalSettings* global_settings = dynamic_cast(_idasettings)->getGlobalSettings(); // Kennzeichnung, dass initialize()() (vor der Integration) aufgerufen wurde _idid = 5000; @@ -812,7 +814,7 @@ void Ida::writeIDAOutput(const double &time, const double &h, const int &stp) bool Ida::stateSelection() { - return SolverDefaultImplementation::stateSelection(); + return _state_selection->stateSelection(); } int Ida::calcFunction(const double& time, const double* y, double *yp,double* res) { diff --git a/SimulationRuntime/cpp/Solver/Peer/Peer.cpp b/SimulationRuntime/cpp/Solver/Peer/Peer.cpp index 4d40321898..a3d504f12c 100644 --- a/SimulationRuntime/cpp/Solver/Peer/Peer.cpp +++ b/SimulationRuntime/cpp/Solver/Peer/Peer.cpp @@ -89,6 +89,8 @@ void Peer::initialize() IContinuous *continuous_system = dynamic_cast(_system); ITime *time_system = dynamic_cast(_system); IGlobalSettings* global_settings = dynamic_cast(_peersettings)->getGlobalSettings(); + _state_selection = dynamic_cast(_system); + _numThreads=_peersettings->getGlobalSettings()->getSolverThreads(); _hOut = global_settings->gethOutput(); #ifdef MPIPEER @@ -583,7 +585,7 @@ void Peer::writePeerOutput(const double &time, const double &h, const int &stp) bool Peer::stateSelection() { - return SolverDefaultImplementation::stateSelection(); + return _state_selection->stateSelection(); } void Peer::setTimeOut(unsigned int time_out) diff --git a/SimulationRuntime/cpp/Solver/RK12/RK12.cpp b/SimulationRuntime/cpp/Solver/RK12/RK12.cpp index 2beb88c7f9..a4599d0ccc 100644 --- a/SimulationRuntime/cpp/Solver/RK12/RK12.cpp +++ b/SimulationRuntime/cpp/Solver/RK12/RK12.cpp @@ -52,6 +52,7 @@ RK12::RK12(IMixedSystem* system, ISolverSettings* settings) { } + RK12::~RK12() { if(_z) @@ -75,7 +76,7 @@ RK12::~RK12() bool RK12::stateSelection() { - return SolverDefaultImplementation::stateSelection(); + return _state_selection->stateSelection(); } void RK12::initialize() { @@ -87,6 +88,7 @@ void RK12::initialize() _event_system = dynamic_cast(_system); _mixed_system = dynamic_cast(_system); _time_system = dynamic_cast(_system); + _state_selection = dynamic_cast(_system); //(Re-) Initialization of solver -> call default implementation service SolverDefaultImplementation::initialize(); diff --git a/SimulationRuntime/cpp/Solver/RTEuler/RTEuler.cpp b/SimulationRuntime/cpp/Solver/RTEuler/RTEuler.cpp index 183fe66d43..73e3e4d755 100644 --- a/SimulationRuntime/cpp/Solver/RTEuler/RTEuler.cpp +++ b/SimulationRuntime/cpp/Solver/RTEuler/RTEuler.cpp @@ -37,6 +37,7 @@ void RTEuler::initialize() _event_system = dynamic_cast(_system); _mixed_system = dynamic_cast(_system); _time_system = dynamic_cast(_system); + _state_selection = dynamic_cast(_system); _dimSys = _continuous_system->getDimContinuousStates(); @@ -123,7 +124,7 @@ ISolver::SOLVERSTATUS RTEuler::getSolverStatus() bool RTEuler::stateSelection() { - return SolverDefaultImplementation::stateSelection(); + return _state_selection->stateSelection(); } void RTEuler::solve(const SOLVERCALL command)