commit ca061f872ad658465f0863248f0175bc60234b2d parent 3a820196ce5f6b1062ec367b9b575eb05b65ffdf Author: Fabian Wermelinger <fabianw@mavt.ethz.ch> Date: Mon, 1 Feb 2021 00:31:41 +0100 Install headers below ODETB Diffstat:
15 files changed, 256 insertions(+), 250 deletions(-)
diff --git a/include/ArgumentParser.h b/include/ODETB/ArgumentParser.h diff --git a/include/GnuplotDump.h b/include/ODETB/GnuplotDump.h diff --git a/include/TimeStepper/KernelBase.h b/include/ODETB/TimeStepper/KernelBase.h diff --git a/include/ODETB/TimeStepper/StepperBase.h b/include/ODETB/TimeStepper/StepperBase.h @@ -0,0 +1,239 @@ +// File : StepperBase.h +// Date : Thu Sep 22 13:27:19 2016 +// Author : Fabian Wermelinger +// Description: Time stepper base class +// Copyright 2016 ETH Zurich. All Rights Reserved. +#ifndef STEPPERBASE_H_FCCSM4HL +#define STEPPERBASE_H_FCCSM4HL + +#include <ODETB/TimeStepper/KernelBase.h> + +#include <cassert> +#include <iostream> + +// forward declarations +template <typename Tinput, typename Trhs=Tinput> +class StepperBase; +template <typename Tinput, typename Trhs=Tinput> +class Euler; +template <typename Tinput, typename Trhs=Tinput> +class RK4; +template <typename Tinput, typename Trhs=Tinput> +class LSRK3; +template <typename Tinput, typename Trhs=Tinput> +class RKF45; +template <typename Tinput, typename Trhs=Tinput> +class RKV56; +#ifdef _USE_SUNDIALS_ +template <typename Tinput, typename Trhs=Tinput> +class BDF; +#endif /* _USE_SUNDIALS_ */ + +#ifdef _FLOAT_PRECISION_ +using Real = float; +#else +using Real = double; +#endif + +struct StepperSettings +{ + StepperSettings(ArgumentParser& p) + { + aTol = p("-ts_aTol").asDouble(1.0e-8); + rTol = p("-ts_rTol").asDouble(1.0e-8); + minScale = p("-ts_minScale").asDouble(0.2); + maxScale = p("-ts_maxScale").asDouble(10.0); + alpha = p("-ts_alpha").asDouble(1.0); + beta = p("-ts_beta").asDouble(0.0); + safety = p("-ts_safety").asDouble(0.9); + t = p("-ts_t0").asDouble(0.0); + dt = p("-ts_dt").asDouble(1.0e-4); + dtMin = p("-ts_dtMin").asDouble(0.0); + step = 0; + nsteps = p("-ts_nsteps").asInt(0); + p.set_strict_mode(); + tFinal = p("-ts_tend").asDouble(); + p.unset_strict_mode(); + dtDump = p("-ts_dtDump").asDouble(-1.0); + tDump = t; + writeGranularity = p("-ts_writeGranularity").asInt(1); + 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; + Real rTol; + Real minScale; + Real maxScale; + Real alpha; + Real beta; + Real safety; + Real t; + Real dt; + Real dtMin; + size_t step; + size_t nsteps; + Real tFinal; + Real dtDump; + Real tDump; + size_t writeGranularity; + size_t writeCount; + size_t reportGranularity; + bool bFixedStep; + + // restarts + bool bRestart; + int restartstep; + + + // helper + void print() const + { + printf("Time Stepper :\n"); + printf("\tAbsolute tolerance = %e\n", aTol); + printf("\tRelative tolerance = %e\n", rTol); + printf("\tMinimum time step scale = %e\n", minScale); + printf("\tMaximum time step scale = %e\n", maxScale); + printf("\tError PI control alpha = %e\n", alpha); + printf("\tError PI control beta = %e\n", beta); + } + + template <typename Tinput, typename Trhs=Tinput> + StepperBase<Tinput,Trhs>* stepperFactory(ArgumentParser& p, Tinput& U, KernelBase<Tinput,Trhs> * const kern=nullptr) + { + assert(kern != nullptr); + + if (p("-ts").asString("rkv56") == "euler") + { + std::cout << "Allocating Euler time stepper...\n"; + return new Euler<Tinput, Trhs>(U, *this, kern); + } + else if (p("-ts").asString("rkv56") == "rk4") + { + std::cout << "Allocating RK4 time stepper...\n"; + return new RK4<Tinput, Trhs>(U, *this, kern); + } + else if (p("-ts").asString("rkv56") == "lsrk3") + { + std::cout << "Allocating LSRK3 time stepper...\n"; + return new LSRK3<Tinput, Trhs>(U, *this, kern); + } + else if (p("-ts").asString("rkv56") == "rkf45") + { + std::cout << "Allocating RKF45 adaptive time stepper...\n"; + this->bFixedStep = false; + this->print(); + return new RKF45<Tinput, Trhs>(U, *this, kern); + } + else if (p("-ts").asString("rkv56") == "rkv56") + { + std::cout << "Allocating RKV56 adaptive time stepper...\n"; + this->bFixedStep = false; + this->print(); + return new RKV56<Tinput, Trhs>(U, *this, kern); + } +#ifdef _USE_SUNDIALS_ + else if (p("-ts").asString("rkv56") == "bdf") + { + std::cout + << "Allocating Sundials BDF/Newton implicit time stepper...\n"; + return new BDF<Tinput, Trhs>(U, *this, kern); + } +#endif /* _USE_SUNDIALS_ */ + else + return nullptr; + } +}; + + +template <typename Tinput, typename Trhs> +class StepperBase +{ + StepperBase(Tinput const& rhs) = delete; + StepperBase& operator=(Tinput const& rhs) = delete; + +protected: + StepperSettings& m_settings; + Tinput& m_U; + KernelBase<Tinput,Trhs> * const m_kernel; + +public: + StepperBase(Tinput& U, StepperSettings& settings, KernelBase<Tinput,Trhs> * const kern=nullptr) : m_settings(settings), m_U(U), m_kernel(kern) { } + virtual ~StepperBase() { } + + inline Tinput& U() { return m_U; } + inline Tinput const& U() const { return m_U; } + + virtual void step(void const* const data=nullptr) = 0; + + inline void write(const size_t step, const Real t, const Real dt, const Tinput& U, const void * const data=nullptr) + { + m_kernel->write(step, t, dt, U, data); + } +}; + +// 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/include/TimeStepper/TimeStepper.h b/include/ODETB/TimeStepper/TimeStepper.h diff --git a/include/TimeStepper/explicit/Euler.h b/include/ODETB/TimeStepper/explicit/Euler.h diff --git a/include/TimeStepper/explicit/LSRK3.h b/include/ODETB/TimeStepper/explicit/LSRK3.h diff --git a/include/TimeStepper/explicit/RK4.h b/include/ODETB/TimeStepper/explicit/RK4.h diff --git a/include/TimeStepper/explicit/variableStep/RKF45.h b/include/ODETB/TimeStepper/explicit/variableStep/RKF45.h diff --git a/include/TimeStepper/explicit/variableStep/RKV56.h b/include/ODETB/TimeStepper/explicit/variableStep/RKV56.h diff --git a/include/TimeStepper/implicit/BDF.h b/include/ODETB/TimeStepper/implicit/BDF.h diff --git a/include/common.h b/include/ODETB/common.h diff --git a/include/TimeStepper/StepperBase.h b/include/TimeStepper/StepperBase.h @@ -1,239 +0,0 @@ -// File : StepperBase.h -// Date : Thu Sep 22 13:27:19 2016 -// Author : Fabian Wermelinger -// Description: Time stepper base class -// Copyright 2016 ETH Zurich. All Rights Reserved. -#ifndef STEPPERBASE_H_FCCSM4HL -#define STEPPERBASE_H_FCCSM4HL - -#include <ODE/TimeStepper/KernelBase.h> - -#include <cassert> -#include <iostream> - -// forward declarations -template <typename Tinput, typename Trhs=Tinput> -class StepperBase; -template <typename Tinput, typename Trhs=Tinput> -class Euler; -template <typename Tinput, typename Trhs=Tinput> -class RK4; -template <typename Tinput, typename Trhs=Tinput> -class LSRK3; -template <typename Tinput, typename Trhs=Tinput> -class RKF45; -template <typename Tinput, typename Trhs=Tinput> -class RKV56; -#ifdef _USE_SUNDIALS_ -template <typename Tinput, typename Trhs=Tinput> -class BDF; -#endif /* _USE_SUNDIALS_ */ - -#ifdef _FLOAT_PRECISION_ -using Real = float; -#else -using Real = double; -#endif - -struct StepperSettings -{ - StepperSettings(ArgumentParser& p) - { - aTol = p("-ts_aTol").asDouble(1.0e-8); - rTol = p("-ts_rTol").asDouble(1.0e-8); - minScale = p("-ts_minScale").asDouble(0.2); - maxScale = p("-ts_maxScale").asDouble(10.0); - alpha = p("-ts_alpha").asDouble(1.0); - beta = p("-ts_beta").asDouble(0.0); - safety = p("-ts_safety").asDouble(0.9); - t = p("-ts_t0").asDouble(0.0); - dt = p("-ts_dt").asDouble(1.0e-4); - dtMin = p("-ts_dtMin").asDouble(0.0); - step = 0; - nsteps = p("-ts_nsteps").asInt(0); - p.set_strict_mode(); - tFinal = p("-ts_tend").asDouble(); - p.unset_strict_mode(); - dtDump = p("-ts_dtDump").asDouble(-1.0); - tDump = t; - writeGranularity = p("-ts_writeGranularity").asInt(1); - 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; - Real rTol; - Real minScale; - Real maxScale; - Real alpha; - Real beta; - Real safety; - Real t; - Real dt; - Real dtMin; - size_t step; - size_t nsteps; - Real tFinal; - Real dtDump; - Real tDump; - size_t writeGranularity; - size_t writeCount; - size_t reportGranularity; - bool bFixedStep; - - // restarts - bool bRestart; - int restartstep; - - - // helper - void print() const - { - printf("Time Stepper :\n"); - printf("\tAbsolute tolerance = %e\n", aTol); - printf("\tRelative tolerance = %e\n", rTol); - printf("\tMinimum time step scale = %e\n", minScale); - printf("\tMaximum time step scale = %e\n", maxScale); - printf("\tError PI control alpha = %e\n", alpha); - printf("\tError PI control beta = %e\n", beta); - } - - template <typename Tinput, typename Trhs=Tinput> - StepperBase<Tinput,Trhs>* stepperFactory(ArgumentParser& p, Tinput& U, KernelBase<Tinput,Trhs> * const kern=nullptr) - { - assert(kern != nullptr); - - if (p("-ts").asString("rkv56") == "euler") - { - std::cout << "Allocating Euler time stepper...\n"; - return new Euler<Tinput, Trhs>(U, *this, kern); - } - else if (p("-ts").asString("rkv56") == "rk4") - { - std::cout << "Allocating RK4 time stepper...\n"; - return new RK4<Tinput, Trhs>(U, *this, kern); - } - else if (p("-ts").asString("rkv56") == "lsrk3") - { - std::cout << "Allocating LSRK3 time stepper...\n"; - return new LSRK3<Tinput, Trhs>(U, *this, kern); - } - else if (p("-ts").asString("rkv56") == "rkf45") - { - std::cout << "Allocating RKF45 adaptive time stepper...\n"; - this->bFixedStep = false; - this->print(); - return new RKF45<Tinput, Trhs>(U, *this, kern); - } - else if (p("-ts").asString("rkv56") == "rkv56") - { - std::cout << "Allocating RKV56 adaptive time stepper...\n"; - this->bFixedStep = false; - this->print(); - return new RKV56<Tinput, Trhs>(U, *this, kern); - } -#ifdef _USE_SUNDIALS_ - else if (p("-ts").asString("rkv56") == "bdf") - { - std::cout - << "Allocating Sundials BDF/Newton implicit time stepper...\n"; - return new BDF<Tinput, Trhs>(U, *this, kern); - } -#endif /* _USE_SUNDIALS_ */ - else - return nullptr; - } -}; - - -template <typename Tinput, typename Trhs> -class StepperBase -{ - StepperBase(Tinput const& rhs) = delete; - StepperBase& operator=(Tinput const& rhs) = delete; - -protected: - StepperSettings& m_settings; - Tinput& m_U; - KernelBase<Tinput,Trhs> * const m_kernel; - -public: - StepperBase(Tinput& U, StepperSettings& settings, KernelBase<Tinput,Trhs> * const kern=nullptr) : m_settings(settings), m_U(U), m_kernel(kern) { } - virtual ~StepperBase() { } - - inline Tinput& U() { return m_U; } - inline Tinput const& U() const { return m_U; } - - virtual void step(void const* const data=nullptr) = 0; - - inline void write(const size_t step, const Real t, const Real dt, const Tinput& U, const void * const data=nullptr) - { - m_kernel->write(step, t, dt, U, data); - } -}; - -// 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/include/meson.build b/include/meson.build @@ -1,7 +1,13 @@ -common_headers= [ - 'common.h', - 'ArgumentParser.h', - 'GnuplotDump.h' - ] -install_headers(common_headers, subdir: 'ODETB') -install_subdir('TimeStepper', install_dir : get_option('includedir') / 'ODETB') +install_headers(files([ + 'ODETB/ArgumentParser.h', + 'ODETB/GnuplotDump.h', + 'ODETB/TimeStepper/KernelBase.h', + 'ODETB/TimeStepper/StepperBase.h', + 'ODETB/TimeStepper/TimeStepper.h', + 'ODETB/TimeStepper/explicit/Euler.h', + 'ODETB/TimeStepper/explicit/LSRK3.h', + 'ODETB/TimeStepper/explicit/RK4.h', + 'ODETB/TimeStepper/explicit/variableStep/RKF45.h', + 'ODETB/TimeStepper/explicit/variableStep/RKV56.h', + 'ODETB/common.h', +])) diff --git a/test/orderVerification/orderVerification.cpp b/test/orderVerification/orderVerification.cpp @@ -3,10 +3,10 @@ /* Author: Fabian Wermelinger */ /* Tag: Verify order of time stepper */ /* Copyright 2016 ETH Zurich. All Rights Reserved. */ -#include <GnuplotDump.h> -#include <TimeStepper/KernelBase.h> -#include <TimeStepper/TimeStepper.h> -#include <common.h> +#include <ODETB/GnuplotDump.h> +#include <ODETB/TimeStepper/KernelBase.h> +#include <ODETB/TimeStepper/TimeStepper.h> +#include <ODETB/common.h> #include <cassert> #include <iostream>