C++QEDCore  v2 Milestone 10
a framework for simulating open quantum dynamics – core
Evolved.h
Go to the documentation of this file.
1 // Copyright András Vukics 2006–2014. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt)
2 // -*- C++ -*-
4 #ifndef CPPQEDCORE_UTILS_EVOLVED_H_INCLUDED
5 #define CPPQEDCORE_UTILS_EVOLVED_H_INCLUDED
6 
7 #include "EvolvedFwd.h"
8 
9 #include "core_config.h"
10 
11 #include <boost/shared_ptr.hpp> // instead of std::tr1::shared_ptr
12 #include <boost/function.hpp> // instead of std::tr1::function
13 #include <boost/utility.hpp>
14 
15 #ifndef DO_NOT_USE_BOOST_SERIALIZATION
16 #include <boost/serialization/base_object.hpp>
17 #include <boost/serialization/split_member.hpp>
18 #endif // DO_NOT_USE_BOOST_SERIALIZATION
19 
20 
22 namespace evolved {
23 
24 
26 
29 enum SteppingFunction {SF_RKCK, SF_RK8PD};
30 
32 
33 std::ostream& operator<<(std::ostream&, SteppingFunction);
34 std::istream& operator>>(std::istream&, SteppingFunction&);
36 
37 
39 
53 {
54 public:
56 
57  double getTime( ) const {return t_;}
58  void setTime(double t) {t_=t;}
60 
61  double getDtDid() const {return dtDid_;}
62 
63  double getDtTry( ) const {return dtTry_;}
64 
66 
70  void setDtTry(double dtTry) {dtTry_=dtTry;}
71 
73 
74  double getEpsRel() const {return epsRel_;}
75  double getEpsAbs() const {return epsAbs_;}
76 
77 
78  void update(double t, double dtTry);
79 
81 
82 protected:
84  TimeStepBookkeeper(double dtInit,
85  double epsRel,
86  double epsAbs
87  );
88 
89 private:
90 #ifndef DO_NOT_USE_BOOST_SERIALIZATION
91  friend class boost::serialization::access;
92  template<class Archive>
93  void serialize(Archive& ar, const unsigned int) {ar & t_ & dtDid_ & dtTry_;}
94 #endif // DO_NOT_USE_BOOST_SERIALIZATION
95 
96  double t_, dtTry_, dtDid_;
97 
98  const double epsRel_, epsAbs_;
99 
100 };
101 
102 
104 
112 template<typename A>
113 class EvolvedIO : public TimeStepBookkeeper, private boost::noncopyable
114 {
115 public:
116  typedef boost::shared_ptr<EvolvedIO> Ptr;
117  typedef boost::shared_ptr<const EvolvedIO> ConstPtr;
118 
120  EvolvedIO(A&, double dtInit, double epsRel, double epsAbs);
121 
122  using TimeStepBookkeeper::operator=;
123 
124  A & getA() {return this->a_;}
125  A const& getA() const {return this->a_;}
126 
127  virtual ~EvolvedIO() {}
128 
129 private:
130 
131 #ifndef DO_NOT_USE_BOOST_SERIALIZATION
132 
134 
136  template<class Archive>
137  void save(Archive& ar, const unsigned int) const
138  {A temp(a_); ar & temp & boost::serialization::base_object<TimeStepBookkeeper>(*this);}
139 
140  template<class Archive>
141  void load(Archive& ar, const unsigned int)
142  {A temp; ar & temp & boost::serialization::base_object<TimeStepBookkeeper>(*this); a_.reference(temp);}
143 
144  BOOST_SERIALIZATION_SPLIT_MEMBER()
145 
146 #endif // DO_NOT_USE_BOOST_SERIALIZATION
147 
148  A& a_;
149 
150 };
151 
152 
153 
155 
169 template<typename A>
170 class Evolved : public EvolvedIO<A>
171 {
172 public:
173  typedef boost::function<void(double, const A&, A&)> Derivs;
174 
175  typedef boost::shared_ptr< Evolved> Ptr;
176  typedef boost::shared_ptr<const Evolved> ConstPtr;
177 
179  Evolved(A&, Derivs, double dtInit, double epsRel, double epsAbs);
180 
181  using TimeStepBookkeeper::operator=;
182  using EvolvedIO<A>::getA;
183 
184  virtual ~Evolved() {}
185 
187  void step(double deltaT
188  );
189 
190  std::ostream& displayParameters(std::ostream& os) const {return displayParameters_v(os);}
191 
193 
194  const Derivs getDerivs() const {return derivs_;}
196 
197  size_t nFailedSteps() const {return nFailedSteps_v();}
198 
199 private:
200  virtual void step_v(double deltaT) = 0;
201  virtual std::ostream& displayParameters_v(std::ostream&) const = 0;
202  virtual size_t nFailedSteps_v() const = 0;
203 
204 
205  Derivs derivs_;
206 
207 };
208 
209 
211 
212 
214 template<typename E>
215 void evolve(E&, double deltaT);
216 
217 
219 template<typename E>
220 void evolveTo(E& e, double t)
221 {
222  evolve(e,t-e.getTime());
223 }
225 
226 
227 
229 
230 template<typename A>
231 class Maker
232 {
233 public:
234  typedef typename Evolved<A>::Ptr Ptr;
235  typedef typename Evolved<A>::Derivs Derivs;
236 
238  const Ptr operator()(A& array, Derivs derivs, double dtInit, double epsRel, double epsAbs,
239  const A& scaleAbs
240  ) const {return make(array,derivs,dtInit,epsRel,epsAbs,scaleAbs);}
241 
242  virtual ~Maker() {}
243 
244 private:
245  virtual const Ptr make(A&, Derivs, double dtInit, double epsRel, double epsAbs, const A& scaleAbs) const = 0;
246 
247 };
248 
249 
250 
251 } // evolved
252 
253 
254 #endif // CPPQEDCORE_UTILS_EVOLVED_H_INCLUDED
EvolvedIO(A &, double dtInit, double epsRel, double epsAbs)
straightforward constructor
void evolveTo(E &e, double t)
evolves up to exactly time t
Definition: Evolved.h:220
Factory class for Evolved types.
Definition: Evolved.h:231
void evolve(E &, double deltaT)
evolves for exactly time deltaT
A common interface for (adaptive stepsize) ODE drivers.
Definition: Evolved.h:170
size_t nFailedSteps() const
number of failed steps in the last timestep (delegates to pure virtual)
Definition: Evolved.h:197
SteppingFunction
Enumeration for different stepping-function types, for i/o operations.
Definition: Evolved.h:29
const Ptr operator()(A &array, Derivs derivs, double dtInit, double epsRel, double epsAbs, const A &scaleAbs) const
The factory member function expecting the most generic set of parameters.
Definition: Evolved.h:238
TimeStepBookkeeper(double dtInit, double epsRel, double epsAbs)
straightforward constructor
void setDtTry(double dtTry)
Sets the timestep to try in the next step.
Definition: Evolved.h:70
Evolved(A &, Derivs, double dtInit, double epsRel, double epsAbs)
straightforward constructor
void step(double deltaT)
takes a single adaptive step
Class for serialization of Evolved states.
Definition: Evolved.h:113
std::ostream & displayParameters(std::ostream &os) const
delegates to private virtual
Definition: Evolved.h:190
Bookkeeps the timestep-data of Evolved.
Definition: Evolved.h:52
friend class boost::serialization::access
The serialization of A by reference leads to memory leak (for not completely understood reasons)...
Definition: Evolved.h:135
double getEpsAbs() const
absolute precision
Definition: Evolved.h:75
boost::function< void(double, const A &, A &)> Derivs
the strategy functor to calculate time derivative at a given time (3rd argument for output) ...
Definition: Evolved.h:173
double getDtDid() const
returns the last performed timestep
Definition: Evolved.h:61
double getEpsRel() const
relative precision
Definition: Evolved.h:74
TimeStepBookkeeper & operator=(const TimeStepBookkeeper &)
straightforward assignment operator that avoids self-assignment
double getDtTry() const
returns the timestep to try in the next step
Definition: Evolved.h:63
virtual ~EvolvedIO()
necessary in order that EvolvedIO be polymorphic
Definition: Evolved.h:127
Comprises utilities related to ODE adaptive evolution.
Definition: Evolved.h:22