From 47bbd58c1db47dac7283f8117d517b07dbfe45d8 Mon Sep 17 00:00:00 2001 From: Wenqing Wang <wenqing.wang@ufz.de> Date: Wed, 22 Mar 2017 12:15:47 +0100 Subject: [PATCH] [dt] Apply clang-format to the files in TimeStepping --- .../Algorithms/FixedTimeStepping.cpp | 61 +++++++++++------ .../Algorithms/FixedTimeStepping.h | 28 ++++---- .../Algorithms/ITimeStepAlgorithm.h | 3 +- ...erationNumberBasedAdaptiveTimeStepping.cpp | 24 ++++--- ...IterationNumberBasedAdaptiveTimeStepping.h | 63 ++++++++++------- NumLib/TimeStepping/TimeStep.h | 68 +++++++++++-------- 6 files changed, 149 insertions(+), 98 deletions(-) diff --git a/NumLib/TimeStepping/Algorithms/FixedTimeStepping.cpp b/NumLib/TimeStepping/Algorithms/FixedTimeStepping.cpp index 7ee39bc34fc..4c0e4269df6 100644 --- a/NumLib/TimeStepping/Algorithms/FixedTimeStepping.cpp +++ b/NumLib/TimeStepping/Algorithms/FixedTimeStepping.cpp @@ -24,17 +24,28 @@ namespace NumLib { - -FixedTimeStepping::FixedTimeStepping(double t0, double tn, const std::vector<double> &vec_all_dt) -: _t_initial(t0), _t_end(computeEnd(t0, tn, vec_all_dt)), _dt_vector(vec_all_dt), _ts_prev(t0), _ts_current(t0) -{} +FixedTimeStepping::FixedTimeStepping(double t0, + double tn, + const std::vector<double>& vec_all_dt) + : _t_initial(t0), + _t_end(computeEnd(t0, tn, vec_all_dt)), + _dt_vector(vec_all_dt), + _ts_prev(t0), + _ts_current(t0) +{ +} FixedTimeStepping::FixedTimeStepping(double t0, double tn, double dt) -: _t_initial(t0), _t_end(tn), _dt_vector(static_cast<std::size_t>(std::ceil((tn-t0)/dt)), dt), _ts_prev(t0), _ts_current(t0) -{} + : _t_initial(t0), + _t_end(tn), + _dt_vector(static_cast<std::size_t>(std::ceil((tn - t0) / dt)), dt), + _ts_prev(t0), + _ts_current(t0) +{ +} -std::unique_ptr<ITimeStepAlgorithm> -FixedTimeStepping::newInstance(BaseLib::ConfigTree const& config) +std::unique_ptr<ITimeStepAlgorithm> FixedTimeStepping::newInstance( + BaseLib::ConfigTree const& config) { //! \ogs_file_param{prj__time_loop__time_stepping__type} config.checkConfigParameter("type", "FixedTimeStepping"); @@ -42,9 +53,9 @@ FixedTimeStepping::newInstance(BaseLib::ConfigTree const& config) //! \ogs_file_param{prj__time_loop__time_stepping__FixedTimeStepping__t_initial} auto const t_initial = config.getConfigParameter<double>("t_initial"); //! \ogs_file_param{prj__time_loop__time_stepping__FixedTimeStepping__t_end} - auto const t_end = config.getConfigParameter<double>("t_end"); + auto const t_end = config.getConfigParameter<double>("t_end"); //! \ogs_file_param{prj__time_loop__time_stepping__FixedTimeStepping__timesteps} - auto const delta_ts = config.getConfigSubtree("timesteps"); + auto const delta_ts = config.getConfigSubtree("timesteps"); std::vector<double> timesteps; double t_curr = t_initial; @@ -53,7 +64,8 @@ FixedTimeStepping::newInstance(BaseLib::ConfigTree const& config) // TODO: consider adding call "listNonEmpty" to config tree //! \ogs_file_param{prj__time_loop__time_stepping__FixedTimeStepping__timesteps__pair} auto const range = delta_ts.getConfigSubtreeList("pair"); - if (range.begin() == range.end()) { + if (range.begin() == range.end()) + { OGS_FATAL("no timesteps have been given"); } for (auto const pair : range) @@ -61,16 +73,19 @@ FixedTimeStepping::newInstance(BaseLib::ConfigTree const& config) //! \ogs_file_param{prj__time_loop__time_stepping__FixedTimeStepping__timesteps__pair__repeat} auto const repeat = pair.getConfigParameter<std::size_t>("repeat"); //! \ogs_file_param{prj__time_loop__time_stepping__FixedTimeStepping__timesteps__pair__delta_t} - delta_t = pair.getConfigParameter<double>("delta_t"); + delta_t = pair.getConfigParameter<double>("delta_t"); - if (repeat == 0) { + if (repeat == 0) + { OGS_FATAL("<repeat> is zero."); } - if (delta_t <= 0.0) { + if (delta_t <= 0.0) + { OGS_FATAL("timestep <delta_t> is <= 0.0."); } - if (t_curr <= t_end) { + if (t_curr <= t_end) + { timesteps.resize(timesteps.size() + repeat, delta_t); t_curr += repeat * delta_t; @@ -96,8 +111,9 @@ const TimeStep FixedTimeStepping::getTimeStep() const bool FixedTimeStepping::next() { // check if last time step - if (_ts_current.steps() == _dt_vector.size() - || std::abs(_ts_current.current()-_t_end) < std::numeric_limits<double>::epsilon()) + if (_ts_current.steps() == _dt_vector.size() || + std::abs(_ts_current.current() - _t_end) < + std::numeric_limits<double>::epsilon()) return false; // confirm current time and move to the next if accepted @@ -107,17 +123,20 @@ bool FixedTimeStepping::next() // prepare the next time step info _ts_current = _ts_prev; double dt = _dt_vector[_ts_prev.steps()]; - if (_ts_prev.current() + dt > _t_end) // upper bound by t_end + if (_ts_prev.current() + dt > _t_end) // upper bound by t_end dt = _t_end - _ts_prev.current(); _ts_current += dt; return true; } -double FixedTimeStepping::computeEnd(double t_initial, double t_end, const std::vector<double> &dt_vector) +double FixedTimeStepping::computeEnd(double t_initial, + double t_end, + const std::vector<double>& dt_vector) { - double t_sum = t_initial + std::accumulate(dt_vector.begin(), dt_vector.end(), 0.); + double t_sum = + t_initial + std::accumulate(dt_vector.begin(), dt_vector.end(), 0.); return std::min(t_end, t_sum); } -} //NumLib +} // NumLib diff --git a/NumLib/TimeStepping/Algorithms/FixedTimeStepping.h b/NumLib/TimeStepping/Algorithms/FixedTimeStepping.h index af57f4f9ddb..54eff53aeff 100644 --- a/NumLib/TimeStepping/Algorithms/FixedTimeStepping.h +++ b/NumLib/TimeStepping/Algorithms/FixedTimeStepping.h @@ -16,18 +16,19 @@ #include "ITimeStepAlgorithm.h" -namespace BaseLib { class ConfigTree; } +namespace BaseLib +{ +class ConfigTree; +} namespace NumLib { - /** * \brief Fixed time stepping algorithm * * This algorithm returns time step size defined by a user priori. */ -class FixedTimeStepping final - : public ITimeStepAlgorithm +class FixedTimeStepping final : public ITimeStepAlgorithm { public: /** @@ -58,17 +59,17 @@ public: * @param t_end finish time * @param vec_all_dt a vector of all time steps */ - FixedTimeStepping(double t_initial, double t_end, const std::vector<double> &vec_all_dt); + FixedTimeStepping(double t_initial, double t_end, + const std::vector<double>& vec_all_dt); /// Create timestepper from the given configuration - static std::unique_ptr<ITimeStepAlgorithm> newInstance(BaseLib::ConfigTree const& config); + static std::unique_ptr<ITimeStepAlgorithm> newInstance( + BaseLib::ConfigTree const& config); /// return the beginning of time steps double begin() const override { return _t_initial; } - /// return the end of time steps double end() const override { return _t_end; } - /// return current time step const TimeStep getTimeStep() const override; @@ -77,13 +78,16 @@ public: /// return if current time step is accepted bool accepted() const override { return true; } - /// return a history of time step sizes - const std::vector<double>& getTimeStepSizeHistory() const override { return _dt_vector; } + const std::vector<double>& getTimeStepSizeHistory() const override + { + return _dt_vector; + } private: /// determine true end time - static double computeEnd(double t_initial, double t_end, const std::vector<double> &dt_vector); + static double computeEnd(double t_initial, double t_end, + const std::vector<double>& dt_vector); /// initial time const double _t_initial; @@ -97,4 +101,4 @@ private: TimeStep _ts_current; }; -} //NumLib +} // NumLib diff --git a/NumLib/TimeStepping/Algorithms/ITimeStepAlgorithm.h b/NumLib/TimeStepping/Algorithms/ITimeStepAlgorithm.h index 2dda10b913d..c6ec7c3a238 100644 --- a/NumLib/TimeStepping/Algorithms/ITimeStepAlgorithm.h +++ b/NumLib/TimeStepping/Algorithms/ITimeStepAlgorithm.h @@ -17,7 +17,6 @@ namespace NumLib { - /** * \brief Interface of time stepping algorithms * @@ -47,4 +46,4 @@ public: virtual ~ITimeStepAlgorithm() = default; }; -} //NumLib +} // NumLib diff --git a/NumLib/TimeStepping/Algorithms/IterationNumberBasedAdaptiveTimeStepping.cpp b/NumLib/TimeStepping/Algorithms/IterationNumberBasedAdaptiveTimeStepping.cpp index d6e27d960e3..8ff11ddad86 100644 --- a/NumLib/TimeStepping/Algorithms/IterationNumberBasedAdaptiveTimeStepping.cpp +++ b/NumLib/TimeStepping/Algorithms/IterationNumberBasedAdaptiveTimeStepping.cpp @@ -43,14 +43,18 @@ IterationNumberBasedAdaptiveTimeStepping:: bool IterationNumberBasedAdaptiveTimeStepping::next() { // check current time step - if (std::abs(_ts_current.current()-_t_end) < std::numeric_limits<double>::epsilon()) + if (std::abs(_ts_current.current() - _t_end) < + std::numeric_limits<double>::epsilon()) return false; // confirm current time and move to the next if accepted - if (accepted()) { + if (accepted()) + { _ts_pre = _ts_current; _dt_vector.push_back(_ts_current.dt()); - } else { + } + else + { ++_n_rejected_steps; } @@ -72,7 +76,7 @@ double IterationNumberBasedAdaptiveTimeStepping::getNextTimeStepSize() const // if this is the first time step // then we use initial guess provided by a user - if ( _ts_pre.steps() == 0 ) + if (_ts_pre.steps() == 0) { dt = _initial_ts; } @@ -83,17 +87,17 @@ double IterationNumberBasedAdaptiveTimeStepping::getNextTimeStepSize() const if (!_multiplier_vector.empty()) tmp_multiplier = _multiplier_vector[0]; // finding the right multiplier - for (std::size_t i=0; i<_iter_times_vector.size(); i++ ) - if ( this->_iter_times > _iter_times_vector[i] ) + for (std::size_t i = 0; i < _iter_times_vector.size(); i++) + if (this->_iter_times > _iter_times_vector[i]) tmp_multiplier = _multiplier_vector[i]; // multiply the the multiplier dt = _ts_pre.dt() * tmp_multiplier; } // check whether out of the boundary - if ( dt < _min_ts ) + if (dt < _min_ts) dt = _min_ts; - else if ( dt > _max_ts ) + else if (dt > _max_ts) dt = _max_ts; double t_next = dt + _ts_pre.current(); @@ -105,7 +109,7 @@ double IterationNumberBasedAdaptiveTimeStepping::getNextTimeStepSize() const bool IterationNumberBasedAdaptiveTimeStepping::accepted() const { - return ( this->_iter_times <= this->_max_iter ); + return (this->_iter_times <= this->_max_iter); } -} // NumLib +} // NumLib diff --git a/NumLib/TimeStepping/Algorithms/IterationNumberBasedAdaptiveTimeStepping.h b/NumLib/TimeStepping/Algorithms/IterationNumberBasedAdaptiveTimeStepping.h index 38058e29d31..953e7d41b20 100644 --- a/NumLib/TimeStepping/Algorithms/IterationNumberBasedAdaptiveTimeStepping.h +++ b/NumLib/TimeStepping/Algorithms/IterationNumberBasedAdaptiveTimeStepping.h @@ -17,20 +17,22 @@ namespace NumLib { - /** * \brief Iteration number based adaptive time stepping * * This algorithm estimates a time step size depending on the number of * iterations (e.g. of iterative linear solvers, nonlinear methods, partitioned - * coupling) needed in the last time step (see Hoffmann (2010) for Newton-Raphson case). + * coupling) needed in the last time step (see Hoffmann (2010) for + * Newton-Raphson case). * The new time step \f$\Delta t_{n+1}\f$ size is calculated as * \f[ * \Delta t_{n+1} = \alpha \Delta t_n * \f] - * with the previous time step size \f$\Delta t_{n}\f$ and a multiplier coefficient + * with the previous time step size \f$\Delta t_{n}\f$ and a multiplier + * coefficient * \f$\alpha\f$ depending on the iteration number. - * Note that a time step size is always bounded by the minimum and maximum allowed + * Note that a time step size is always bounded by the minimum and maximum + * allowed * value. * \f[ * \Delta t_{\min} \le \Delta t \le \Delta t_{\max} @@ -39,24 +41,30 @@ namespace NumLib * For example, users can setup the following time stepping strategy based on * the iteration number of the Newton-Raphson method in the last time step. * <table border="1"> - * <tr><th>Num. of Newton steps</th><th>0-2</th><th>3-6</th><th>7-8</th><th>9<</th></tr> - * <tr><th>Time step size multiplier</th><th>1.6</th><th>1.</th><th>0.5</th><th>0.25 (repeat time step)</th></tr> - * <tr><th>Upper and lower bound</th><th colspan="4"> \f$ 1. \le \Delta t \le 10.\f$ </th></tr> + * <tr><th>Num. of Newton + * steps</th><th>0-2</th><th>3-6</th><th>7-8</th><th>9<</th></tr> + * <tr><th>Time step size + * multiplier</th><th>1.6</th><th>1.</th><th>0.5</th><th>0.25 (repeat time + * step)</th></tr> + * <tr><th>Upper and lower bound</th><th colspan="4"> \f$ 1. \le \Delta t \le + * 10.\f$ </th></tr> * </table> - * A time step size is increased for the small iteration number, and decreased for the - * large iteration number. If the iteration number exceeds a user-defined threshold (e.g. 9), + * A time step size is increased for the small iteration number, and decreased + * for the + * large iteration number. If the iteration number exceeds a user-defined + * threshold (e.g. 9), * a time step is repeated with a smaller time step size. * * Reference * - Hoffmann J (2010) Reactive Transport and Mineral Dissolution/Precipitation * in Porous Media:Efficient Solution Algorithms, Benchmark Computations and - * Existence of Global Solutions. PhD thesis. pp82. Friedrich-Alexander-Universität Erlangen-Nürnberg. + * Existence of Global Solutions. PhD thesis. pp82. + * Friedrich-Alexander-Universität Erlangen-Nürnberg. * */ class IterationNumberBasedAdaptiveTimeStepping : public ITimeStepAlgorithm { public: - /** * Constructor * @@ -71,18 +79,18 @@ public: * If an iteration number is larger than \f$i_n\f$, current time step is * repeated with the new time step size. * @param multiplier_vector a vector of multiplier coefficients - * (\f$a_1\f$, \f$a_2\f$, ..., \f$a_n\f$) corresponding to the intervals given by iter_times_vector. + * (\f$a_1\f$, \f$a_2\f$, ..., \f$a_n\f$) corresponding to the intervals + * given by iter_times_vector. * A time step size is calculated by \f$\Delta t_{n+1} = a * \Delta t_{n}\f$ */ - IterationNumberBasedAdaptiveTimeStepping(double t_initial, - double t_end, - double min_ts, - double max_ts, - double initial_ts, - std::vector<std::size_t> - iter_times_vector, - std::vector<double> - multiplier_vector); + IterationNumberBasedAdaptiveTimeStepping( + double t_initial, + double t_end, + double min_ts, + double max_ts, + double initial_ts, + std::vector<std::size_t>& iter_times_vector, + std::vector<double>& multiplier_vector); ~IterationNumberBasedAdaptiveTimeStepping() override = default; @@ -106,10 +114,12 @@ public: } /// set the number of iterations - void setNIterations(std::size_t n_itr) {this->_iter_times = n_itr;} - + void setNIterations(std::size_t n_itr) { this->_iter_times = n_itr; } /// return the number of repeated steps - std::size_t getNumberOfRepeatedSteps() const {return this->_n_rejected_steps;} + std::size_t getNumberOfRepeatedSteps() const + { + return this->_n_rejected_steps; + } private: /// calculate the next time step size @@ -119,7 +129,8 @@ private: const double _t_initial; /// end time const double _t_end; - /// this vector stores the number of iterations to which the respective multiplier coefficient will be applied + /// this vector stores the number of iterations to which the respective + /// multiplier coefficient will be applied const std::vector<std::size_t> _iter_times_vector; /// this vector stores the multiplier coefficients const std::vector<double> _multiplier_vector; @@ -143,4 +154,4 @@ private: std::size_t _n_rejected_steps; }; -} // NumLib +} // NumLib diff --git a/NumLib/TimeStepping/TimeStep.h b/NumLib/TimeStepping/TimeStep.h index 1a593a7035b..a85ebb4827f 100644 --- a/NumLib/TimeStepping/TimeStep.h +++ b/NumLib/TimeStepping/TimeStep.h @@ -15,12 +15,13 @@ namespace NumLib { - /** * \brief Time step object * - * TimeStep class contains previous time(\f$t_{n}\f$), current time (\f$t_{n+1}\f$), - * time step length (\f$\Delta t_{n+1}\f$), and the number of time steps (\f$n+1\f$). + * TimeStep class contains previous time(\f$t_{n}\f$), current time + * (\f$t_{n+1}\f$), + * time step length (\f$\Delta t_{n+1}\f$), and the number of time steps + * (\f$n+1\f$). */ class TimeStep { @@ -30,7 +31,12 @@ public: * @param current_time current time */ explicit TimeStep(double current_time) - : _previous(current_time), _current(current_time), _dt(_current-_previous), _steps(0) {} + : _previous(current_time), + _current(current_time), + _dt(_current - _previous), + _steps(0) + { + } /** * Initialize a time step @@ -39,11 +45,21 @@ public: * @param n the number of time steps */ TimeStep(double previous_time, double current_time, std::size_t n) - : _previous(previous_time), _current(current_time), _dt(_current-_previous), _steps(n) {} + : _previous(previous_time), + _current(current_time), + _dt(_current - _previous), + _steps(n) + { + } /// copy a time step - TimeStep(const TimeStep &src) - : _previous(src._previous), _current(src._current), _dt(_current-_previous), _steps(src._steps) {} + TimeStep(const TimeStep& src) + : _previous(src._previous), + _current(src._current), + _dt(_current - _previous), + _steps(src._steps) + { + } /// copy a time step TimeStep& operator=(const TimeStep& src) = default; @@ -57,7 +73,7 @@ public: } /// increment time step - TimeStep &operator+=(const double dt) + TimeStep& operator+=(const double dt) { _previous = _current; _current += dt; @@ -67,35 +83,33 @@ public: } /// compare current time - bool operator<(const TimeStep &t) const {return (_current < t._current);} - + bool operator<(const TimeStep& t) const { return (_current < t._current); } /// compare current time - bool operator<(const double &t) const {return (_current < t);} - + bool operator<(const double& t) const { return (_current < t); } /// compare current time - bool operator<=(const TimeStep &t) const {return (_current <= t._current);} + bool operator<=(const TimeStep& t) const + { + return (_current <= t._current); + } /// compare current time - bool operator<=(const double &t) const {return (_current <= t);} - + bool operator<=(const double& t) const { return (_current <= t); } /// compare current time - bool operator==(const TimeStep &t) const {return (_current == t._current);} + bool operator==(const TimeStep& t) const + { + return (_current == t._current); + } /// compare current time - bool operator==(const double &t) const {return (_current == t);} - + bool operator==(const double& t) const { return (_current == t); } /// return previous time step - double previous() const {return _previous;} - + double previous() const { return _previous; } /// return current time step - double current() const {return _current;} - + double current() const { return _current; } /// time step size from _previous - double dt() const {return _dt;} - + double dt() const { return _dt; } /// the number of time _steps - std::size_t steps() const {return _steps;} - + std::size_t steps() const { return _steps; } private: /// previous time step double _previous; @@ -107,4 +121,4 @@ private: std::size_t _steps; }; -} //NumLib +} // NumLib -- GitLab