C++QEDCore  v2 Milestone 10
a framework for simulating open quantum dynamics – core
Trajectory.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)
3 // -*- C++ -*-
4 #ifndef CPPQEDCORE_UTILS_TRAJECTORY_H_INCLUDED
5 #define CPPQEDCORE_UTILS_TRAJECTORY_H_INCLUDED
6 
7 #include "TrajectoryFwd.h"
8 
9 #include "Archive.h"
10 #include "Exception.h"
11 #include "Evolved.h"
12 
13 #include <iosfwd>
14 #include <string>
15 
16 
18 namespace trajectory {
19 
20 
22 
43 void run(Trajectory & trajectory,
44  double time,
45  double deltaT,
46  unsigned sdf,
47  const std::string& ofn,
48  const std::string& initialFileName,
49  int precision,
50  bool displayInfo,
51  bool firstStateDisplay,
52  double autoStopEpsilon,
53  unsigned autoStopRepetition,
54  const std::string& parsedCommandLine
55  );
56 
57 
59 
73 void run(Trajectory &, long nDt,
74  double deltaT, unsigned sdf, const std::string& ofn, const std::string& initialFileName, int precision, bool displayInfo, bool firstStateDisplay,
75  double autoStopEpsilon, unsigned autoStopRepetition, const std::string& parsedCommandLine);
76 
77 
79 
85 template<typename A>
86 void run(Adaptive<A>&, double time, int dc,
87  unsigned sdf, const std::string& ofn, const std::string& initialFileName, int precision, bool displayInfo, bool firstStateDisplay,
88  double autoStopEpsilon, unsigned autoStopRepetition, const std::string& parsedCommandLine);
89 
90 
92 
100 void run(Trajectory &, const ParsRun& p);
101 
102 
104 
111 template<typename A>
112 void run(Adaptive<A>&, const ParsRun&);
113 
114 
116 
122 {
123  SerializationMetadata(std::string type=UNSPECIFIED, std::string id=UNSPECIFIED, int r=0)
124  : protocolVersion(0),
125  rank(r),
126  typeID(type),
127  trajectoryID(id)
128  {};
129 
130  int protocolVersion;
131  int rank;
132  std::string typeID;
133  std::string trajectoryID;
134 
135  static const std::string UNSPECIFIED;
136  static const std::string ARRAY_ONLY;
137 
138 private:
139 #ifndef DO_NOT_USE_BOOST_SERIALIZATION
140  friend class boost::serialization::access;
141  template<class Archive>
142  void serialize(Archive& ar, const unsigned int) {ar & protocolVersion & rank & typeID & trajectoryID;}
143 #endif // DO_NOT_USE_BOOST_SERIALIZATION
144 
145 };
146 
147 
148 template<typename T>
149 void writeViaSStream(const T&, std::ofstream*);
150 template<typename T>
151 void readViaSStream( T&, std::ifstream&);
152 SerializationMetadata readMeta(std::ifstream&);
153 
154 
156 
159 
162 
165 
167 {
168 public:
169  TrajectoryFileOpeningException(const std::string tag) : cpputils::TaggedException(tag) {}
170 
171 };
172 
173 
175 {
176 public:
177  StateFileOpeningException(const std::string tag) : cpputils::TaggedException(tag) {}
178 
179 };
180 
181 
183 inline double initialTimeStep(double highestFrequency) {return 1./(10.*highestFrequency);}
184 
185 
187 
194 class Trajectory : private boost::noncopyable
195 {
196 public:
198  void evolve(double deltaT) {evolve_v(deltaT);}
199 
201  std::ostream& display(std::ostream&, int precision
202  ) const;
203 
205 
206  double getTime() const {return getTime_v();}
207  double getDtDid() const {return getDtDid_v();}
208 
209 
210  std::ostream& displayParameters(std::ostream& os) const;
211 
212  std::ostream& logOnEnd(std::ostream& os) const {return logOnEnd_v(os);}
213 
215 
216  cpputils::iarchive& readState(cpputils::iarchive& iar) {return readState_v(iar);}
217  cpputils::oarchive& writeState(cpputils::oarchive& oar) const {return writeState_v(oar);}
218 
219 
220  virtual ~Trajectory() {}
221 
222 private:
223  virtual void evolve_v(double) = 0; // A step of exactly deltaT
224  virtual double getTime_v() const = 0;
225  virtual double getDtDid_v() const = 0;
226 
227  virtual std::ostream& displayParameters_v(std::ostream& ) const = 0;
228  virtual std::ostream& display_v (std::ostream&, int ) const = 0;
229  virtual std::ostream& displayKey_v (std::ostream&, size_t&) const = 0;
230 
231  virtual cpputils::iarchive& readState_v(cpputils::iarchive&) = 0;
232  virtual cpputils::oarchive& writeState_v(cpputils::oarchive&) const = 0;
233 
234  virtual std::ostream& logOnEnd_v(std::ostream& os) const {return os;}
235 
236 };
237 
238 
240 //
241 // Adaptive
242 //
244 
246 
247 template<typename A>
249 {
250 private:
252 
253  typedef typename EvolvedIO::Ptr Ptr;
254 
255 public:
256  AdaptiveIO(Ptr);
257 
259 
270 
272  double getTime() const {return evolvedIO_->getTime();}
273 
274 protected:
275  const Ptr getEvolvedIO() const {return evolvedIO_;}
276 
277  mutable SerializationMetadata meta_;
278 
279 private:
280  const Ptr evolvedIO_;
281 
282 };
283 
284 
286 
287 template<typename A>
288 class Adaptive : public trajectory::AdaptiveIO<A>, public virtual Trajectory
289 {
290 public:
291  // needed to break ambiguity with identically named functions in AdaptiveIO
293 
295 
297 
298  void step(double deltaT
299  ) {step_v(deltaT);}
300 
301  virtual ~Adaptive() {}
302 
303 protected:
304  using AdaptiveIO<A>::meta_;
305 
307  Adaptive(A&, typename Evolved::Derivs, double, double, double , const A&, const evolved::Maker<A>&);
308 
310  Adaptive(A&, typename Evolved::Derivs, double, const ParsEvolved&, const A&, const evolved::Maker<A>&);
311 
312  typedef typename Evolved::ConstPtr ConstPtr;
313  typedef typename Evolved:: Ptr Ptr;
314 
316 
317  const ConstPtr getEvolved() const {return ConstPtr(evolved_);}
318  const Ptr getEvolved() {return evolved_ ;}
319 
320  double getDtTry() const {return evolved_->getDtTry();}
322 
324  const std::string trajectoryID() const {return trajectoryID_v();}
325 
326  std::ostream& displayParameters_v(std::ostream&) const override;
327 
329  virtual cpputils::oarchive& writeStateMore_v(cpputils::oarchive &oar) const {return oar;}
330 
331 private:
333  cpputils::iarchive& readState_v(cpputils::iarchive& iar) final;
335  cpputils::oarchive& writeState_v(cpputils::oarchive& oar) const final;
336 
337  double getDtDid_v() const final {return evolved_->getDtDid();}
338 
339  void evolve_v(double deltaT) final {evolved::evolve<Adaptive>(*this,deltaT);}
340 
341  double getTime_v() const final {return evolved_->getTime();}
342 
343  virtual void step_v(double deltaT) = 0;
344 
345  virtual const std::string trajectoryID_v() const = 0;
346 
347  const Ptr evolved_;
348 
349  const double dtInit_;
350 
351 };
352 
353 
354 } // trajectory
355 
356 
357 #endif // CPPQEDCORE_UTILS_TRAJECTORY_H_INCLUDED
The class that is (meant to be, at least) the base of all exceptions in the framework.
Definition: Exception.h:18
Defines i/oarchive types depending on the DO_NOT_USE_BOOST_SERIALIZATION macro.
SerializationMetadata readMeta(std::ifstream &)
Needed separately for the Python i/o.
Corresponds to evolved::EvolvedIO, adding the capability of state serialization involving a Serializa...
Definition: Trajectory.h:248
Factory class for Evolved types.
Definition: Evolved.h:231
const std::string trajectoryID() const
redirected to a pure virtual, this is needed for serialization of trajectory metadata ...
Definition: Trajectory.h:324
double getDtDid() const
last perfomed timestep
Definition: Trajectory.h:207
The trajectory-bundle.
void evolve(double deltaT)
Propagation for a time interval of exactly deltaT.
Definition: Trajectory.h:198
A common interface for (adaptive stepsize) ODE drivers.
Definition: Evolved.h:170
cpputils::oarchive & writeState(cpputils::oarchive &oar) const
write to an archive
Definition: Trajectory.h:217
Raised when the trajectory type we try to read in from file does not match.
Definition: Trajectory.h:164
cpputils::oarchive & writeState(cpputils::oarchive &oar) const
Write the EvolvedIO to a cpputils::oarchive.
std::ostream & logOnEnd(std::ostream &os) const
print a log at the end summarizing overall (e.g. time-averaged) physical and numerical data during th...
Definition: Trajectory.h:212
double getTime() const
actual time instant
Definition: Trajectory.h:206
Class reporting also the “what-ness” of the exception.
Definition: Exception.h:25
cpputils::iarchive & readState(cpputils::iarchive &iar)
Read in the EvolvedIO from a cpputils::iarchive.
boost::archive::binary_oarchive oarchive
delegated to Boost.Serialization
Definition: Archive.h:74
std::ostream & displayParameters(std::ostream &os) const
print header
Defines class of the same name.
Aggregate of information about a trajectory-state archive.
Definition: Trajectory.h:121
virtual cpputils::oarchive & writeStateMore_v(cpputils::oarchive &oar) const
hook into Trajectory::writeState
Definition: Trajectory.h:329
std::ostream & display(std::ostream &, int precision) const
Displays a limited set of relevant physical and numerical information about the actual state of Traje...
Class for serialization of Evolved states.
Definition: Evolved.h:113
Raised when the rank of a trajectory we try to read in from file does not match.
Definition: Trajectory.h:158
double initialTimeStep(double highestFrequency)
A heuristic determination of the inital timestep from the highest frequency of a physical system...
Definition: Trajectory.h:183
Adaptive is basically an evolved::Evolved wrapped into the Trajectory interface.
Definition: Trajectory.h:288
boost::archive::binary_iarchive iarchive
delegated to Boost.Serialization
Definition: Archive.h:73
double getTime() const
Returns the time of the underlying EvolvedIO.
Definition: Trajectory.h:272
The base class of the trajectory-bundle condensing the quintessential characteristics of any trajecto...
Definition: Trajectory.h:194
void step(double deltaT)
corresponding to Evolved::step, it takes a single adaptive step
Definition: Trajectory.h:298
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
Adaptive(A &, typename Evolved::Derivs, double, double, double, const A &, const evolved::Maker< A > &)
Constructor taking the same parameters as needed to operate evolved::Maker.
virtual cpputils::iarchive & readStateMore_v(cpputils::iarchive &iar)
hook into Trajectory::readState
Definition: Trajectory.h:328
Defines tentative base classes for the exception classes of the framework.
const Ptr getEvolvedIO() const
note: not the same const-correctness as in Adaptive
Definition: Trajectory.h:275
cpputils::iarchive & readState(cpputils::iarchive &iar)
read from an archive
Definition: Trajectory.h:216
Raised when the dimensions of a trajectory state we try to read in from file does not match...
Definition: Trajectory.h:161