diff --git a/HopsanCore/include/ComponentSystem.h b/HopsanCore/include/ComponentSystem.h index 3ded173f6..fe789c46d 100644 --- a/HopsanCore/include/ComponentSystem.h +++ b/HopsanCore/include/ComponentSystem.h @@ -129,6 +129,7 @@ namespace hopsan { bool checkModelBeforeSimulation(); virtual bool preInitialize(); bool initialize(const double startT, const double stopT); + void simulateOnceWithoutIncreasingTime(); void simulate(const double stopT); bool startRealtimeSimulation(double realTimeFactor=1); virtual void simulateMultiThreaded(const double startT, const double stopT, const size_t nDesiredThreads = 0, const bool noChanges=false, ParallelAlgorithmT algorithm=APrioriScheduling); diff --git a/HopsanCore/src/ComponentSystem.cpp b/HopsanCore/src/ComponentSystem.cpp index fad46be49..0b0e421d1 100644 --- a/HopsanCore/src/ComponentSystem.cpp +++ b/HopsanCore/src/ComponentSystem.cpp @@ -3339,6 +3339,30 @@ void ComponentSystem::distributeNodePointers(vector< vector > &/*rSplitNo // } //} + +//! @brief Used for initialization in exported FMUs +//! @details Calls simulateOneTimestep() for each component in the system. Time variable is not increased. +void ComponentSystem::simulateOnceWithoutIncreasingTime() +{ + //Signal components + for (size_t s=0; s < mComponentSignalptrs.size(); ++s) + { + mComponentSignalptrs[s]->simulateOneTimestep(); + } + + //C components + for (size_t c=0; c < mComponentCptrs.size(); ++c) + { + mComponentCptrs[c]->simulateOneTimestep(); + } + + //Q components + for (size_t q=0; q < mComponentQptrs.size(); ++q) + { + mComponentQptrs[q]->simulateOneTimestep(); + } +} + //! @brief Simulate function for single-threaded simulations. //! @param[in] stopT Simulate from current time until stop time void ComponentSystem::simulate(const double stopT) diff --git a/HopsanGenerator/templates/fmu2_model.c b/HopsanGenerator/templates/fmu2_model.c index 05b0bb3a4..5b29ffc53 100644 --- a/HopsanGenerator/templates/fmu2_model.c +++ b/HopsanGenerator/templates/fmu2_model.c @@ -265,6 +265,12 @@ fmi2Status fmi2GetReal(fmi2Component c, fmi2Real values[]) { fmuContext *fmu =(fmuContext*)c; + + if(state == Initializing) + { + fmu->pSystem->simulateOnceWithoutIncreasingTime(); //Solve equations without increasing time during initialization + } + fmi2Status status = fmi2OK; for(size_t i=0; i= NUMDATAPTRS+1) {