ode-toolbox

ODE integration tools
git clone https://git.0xfab.ch/ode-toolbox.git
Log | Files | Refs | README | LICENSE

commit 365fcfdeb2266c4489fad8742603429d43984a75
parent fcd1001762977ef2e9938b662a512008d6052766
Author: Fabian Wermelinger <fabianw@mavt.ethz.ch>
Date:   Tue, 21 Feb 2017 17:43:27 +0100

added serializer

Diffstat:
MGnuplotDump.h | 8+++++---
MTimeStepper/StepperBase.h | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MTimeStepper/explicit/variableStep/RKF45.h | 7+++++--
MTimeStepper/explicit/variableStep/RKV56.h | 7+++++--
4 files changed, 83 insertions(+), 7 deletions(-)

diff --git a/GnuplotDump.h b/GnuplotDump.h @@ -78,15 +78,17 @@ private: } public: - GnuplotDump(const std::string fname="gnuplot", const int type=BIN) : m_ftype(type), m_fname(fname) + GnuplotDump(const std::string fname="gnuplot", const int type=BIN, const size_t pos=0) : m_ftype(type), m_fname(fname) { if (m_ftype == ASCII) { - m_file.open(m_fname+".dat", std::ios_base::out); + m_file.open(m_fname+".dat", std::ios_base::out|std::ios_base::app); m_file.setf(std::ios::scientific, std::ios::floatfield); } else - m_file.open(m_fname+".bin", std::ios_base::out|std::ios_base::binary); + m_file.open(m_fname+".bin", std::ios_base::out|std::ios_base::binary|std::ios_base::app); + + if (pos > 0) m_file.seekp(pos); } ~GnuplotDump() { diff --git a/TimeStepper/StepperBase.h b/TimeStepper/StepperBase.h @@ -56,6 +56,11 @@ struct StepperSettings writeCount = 0; reportGranularity= p("-ts_reportGranularity").asInt(100); bFixedStep = true; + + // restarts + bRestart = p("-ts_restart").asBool(false); + restartstep = p("-ts_restart_step").asInt(0); + } Real aTol; @@ -77,6 +82,11 @@ struct StepperSettings size_t reportGranularity; bool bFixedStep; + // restarts + bool bRestart; + int restartstep; + + // helper void print() const { @@ -164,4 +174,62 @@ public: inline void dispose() { delete m_kernel; } }; +// serializer +struct _simulation_state_t +{ + Real t; + Real dt; + Real tFinal; + Real dtDump; + Real tDump; + size_t step; + size_t nsteps; + size_t writeCount; +} __attribute__((packed)); + + +template <typename Tinput> +void serialize(const Tinput& U, const StepperSettings& S, const std::string fname="restart.bin") +{ + _simulation_state_t state; + state.t = S.t; + state.dt = S.dt; + state.tFinal = S.tFinal; + state.dtDump = S.dtDump; + state.tDump = S.tDump; + state.step = S.step; + state.nsteps = S.nsteps; + state.writeCount = S.writeCount; + + ofstream _stream(fname, ios::out | ios::binary); + _stream.write((const char*)&state.t, sizeof(_simulation_state_t)); + + const char* const _src = (const char*)U.data(); + _stream.write(_src, sizeof(typename Tinput::DataType)*U.size()); + + _stream.close(); +} + +template <typename Tinput> +void deserialize(Tinput& U, StepperSettings& S, const std::string fname="restart.bin") +{ + ifstream _stream(fname, ios::in | ios::binary); + + _simulation_state_t state; + _stream.read((char*)&state.t, sizeof(_simulation_state_t)); + S.t = state.t; + S.dt = state.dt; + S.tFinal = state.tFinal; + S.dtDump = state.dtDump; + S.tDump = state.tDump; + S.step = state.step; + S.nsteps = state.nsteps; + S.writeCount = state.writeCount; + + char* const _dst = (char*)U.data(); + _stream.read(_dst, sizeof(typename Tinput::DataType)*U.size()); + + _stream.close(); +} + #endif /* STEPPERBASE_H_FCCSM4HL */ diff --git a/TimeStepper/explicit/variableStep/RKF45.h b/TimeStepper/explicit/variableStep/RKF45.h @@ -149,12 +149,12 @@ public: virtual void step(void const* const data=nullptr) { - size_t& step = m_settings.step; + size_t& step = m_settings.step; Real& t = m_settings.t; Real& dt = m_settings.dt; const Real& tFinal = m_settings.tFinal; Real& tDump = m_settings.tDump; - if (m_settings.dtDump > 0.0) tDump = t + m_settings.dtDump; + if (!m_settings.bRestart && m_settings.dtDump > 0.0) tDump = t + m_settings.dtDump; while(t < tFinal) { @@ -177,6 +177,9 @@ public: if (m_settings.step % m_settings.reportGranularity == 0) std::printf("Time = %e;\tStep = %d\n", m_settings.t, m_settings.step); + + if (m_settings.restartstep > 0 && step % m_settings.restartstep == 0) + serialize<Tinput>(m_U, m_settings); } } }; diff --git a/TimeStepper/explicit/variableStep/RKV56.h b/TimeStepper/explicit/variableStep/RKV56.h @@ -156,12 +156,12 @@ public: virtual void step(void const* const data=nullptr) { - size_t& step = m_settings.step; + size_t& step = m_settings.step; Real& t = m_settings.t; Real& dt = m_settings.dt; const Real& tFinal = m_settings.tFinal; Real& tDump = m_settings.tDump; - if (m_settings.dtDump > 0.0) tDump = t + m_settings.dtDump; + if (!m_settings.bRestart && m_settings.dtDump > 0.0) tDump = t + m_settings.dtDump; while(t < tFinal) { @@ -184,6 +184,9 @@ public: if (m_settings.step % m_settings.reportGranularity == 0) std::printf("Time = %e;\tStep = %d\n", m_settings.t, m_settings.step); + + if (m_settings.restartstep > 0 && step % m_settings.restartstep == 0) + serialize<Tinput>(m_U, m_settings); } } };