C++QEDCore  2.100.2 (v2 Milestone 10 Development branch)
a framework for simulating open quantum dynamics – core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
StochasticTrajectory.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_STOCHASTICTRAJECTORY_H_INCLUDED
5 #define CPPQEDCORE_UTILS_STOCHASTICTRAJECTORY_H_INCLUDED
6 
7 
8 #include "StochasticTrajectoryFwd.h"
9 
10 #include "Trajectory.h"
11 #include "Randomized.h"
12 
13 
14 #include <boost/ptr_container/ptr_vector.hpp>
15 
16 
17 namespace trajectory {
18 
19 
21 
32 template<typename T>
33 class Averageable : public virtual Trajectory
34 {
35 public:
36  virtual ~Averageable() {}
37 
38  const T toBeAveraged() const {return toBeAveraged_v();}
39 
40 private:
41  virtual const T toBeAveraged_v() const = 0;
42 
43 };
44 
45 
47 
48 template<typename A, typename T>
49 class Stochastic : public Adaptive<A>, public Averageable<T>
50 {
51 public:
52  virtual ~Stochastic() {}
53 
54 private:
55  typedef Adaptive<A> Base;
56 
57  typedef typename Base::Evolved Evolved;
58 
59  typedef randomized::Randomized::Ptr RandomizedPtr;
60 
61 protected:
63 
64  Stochastic(A&, typename Evolved::Derivs, double dtInit,
65  double epsRel, double epsAbs, const A& scaleAbs,
66  const evolved::Maker<A>&,
67  unsigned long seed,
68  bool noise,
69  const randomized::Maker&);
70 
71  Stochastic(A&, typename Evolved::Derivs, double dtInit,
72  const A& scaleAbs, const ParsStochastic&,
73  const evolved::Maker<A>&,
74  const randomized::Maker&);
75 
76 
78 
79  const RandomizedPtr getRandomized() const {return randomized_;}
80  bool isNoisy () const {return isNoisy_ ;}
82 
83  std::ostream& displayParameters_v(std::ostream&) const override;
84 
86 
87  cpputils::iarchive& readStateMore_v(cpputils::iarchive& iar) override {return iar & *randomized_;}
88  cpputils::oarchive& writeStateMore_v(cpputils::oarchive& oar) const override {return oar & *randomized_;}
90 
91 private:
92  const unsigned long seed_ ;
93  const bool isNoisy_;
94 
95  const RandomizedPtr randomized_;
96 
97 };
98 
99 
100 
102 namespace ensemble {
103 
105 template<typename T>
106 class Base {};
107 
108 } // ensemble
109 
110 
111 
113 
130 template<typename T, typename T_ELEM>
131 class Ensemble : public Averageable<T>, public ensemble::Base<T>
132 {
133 private:
134  typedef Averageable<T > Base;
135 
136 public:
137  typedef Averageable<T_ELEM> Elem;
138 
140 
141  typedef boost::ptr_vector<Elem> Impl;
142 
143  typedef T ToBeAveragedType;
144 
146 
147  const ToBeAveragedType averageInRange(size_t begin, size_t n) const;
148 
149  virtual ~Ensemble() {}
150 
151 protected:
152  typedef std::auto_ptr<Impl> Ptr;
153 
155  Ensemble(Ptr trajs,
156  bool displayProgress
157  ) : trajs_(trajs), displayProgress_(displayProgress) {}
158 
160  const Impl& getTrajectories() const {return trajs_;}
161 
162 #define FOR_EACH_function(f) for_each(trajs_,bind(&Elem::f,_1,boost::ref(ios))); return ios;
163 
165 
166  virtual cpputils::iarchive& readState_v(cpputils::iarchive& ios) final {FOR_EACH_function( readState)}
167  virtual cpputils::oarchive& writeState_v(cpputils::oarchive& ios) const final {FOR_EACH_function(writeState)}
169 
170 private:
171  std::ostream& logOnEnd_v(std::ostream& ios) const override {FOR_EACH_function(logOnEnd)}
172 
173 #undef FOR_EACH_function
174 
175  virtual void evolve_v(double deltaT) final;
176 
177  virtual double getTime_v() const final {return trajs_.front().getTime();}
178 
179  virtual std::ostream& displayParameters_v(std::ostream&) const final;
180 
181  virtual double getDtDid_v() const final;
182  // An average of getDtDid()-s from individual trajectories.
183 
184  virtual const ToBeAveragedType toBeAveraged_v() const final {return averageInRange(0,trajs_.size());}
185 
186  Impl trajs_; // cannot be const because ptr_vector “propagates constness” (very correctly)
187 
188  const bool displayProgress_;
189 
190 };
191 
192 
193 namespace ensemble {
194 
196 
204 template<typename T, typename T_ELEM>
205 class Traits
206 {
207 public:
209 
210  typedef typename EnsembleType::Elem Elem ;
211  typedef typename EnsembleType::Impl Impl ;
212  typedef typename EnsembleType::ToBeAveragedType ToBeAveragedType;
213 
214  static const ToBeAveragedType averageInRange(typename Impl::const_iterator, typename Impl::const_iterator, const EnsembleType&);
215 
216 };
217 
218 } // ensemble
219 
220 
221 } // trajectory
222 
223 
224 #endif // CPPQEDCORE_UTILS_STOCHASTICTRAJECTORY_H_INCLUDED
Defines the basic classes of the trajectory-bundle.
An ensemble of Averageable trajectories providing services for ensemble averaging and evolving the el...
cpputils::iarchive & readStateMore_v(cpputils::iarchive &iar) override
hook into Trajectory::readState
Factory class for Evolved types.
Definition: Evolved.h:231
The trajectory-bundle.
A common interface for (adaptive stepsize) ODE drivers.
Definition: Evolved.h:170
Density operator of arbitrary arity.
Represents a trajectory that has both adaptive ODE evolution and noise.
cpputils::oarchive & writeState(cpputils::oarchive &oar) const
write to an archive
Definition: Trajectory.h:217
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
Randomized & related.
cpputils::oarchive & writeStateMore_v(cpputils::oarchive &oar) const override
hook into Trajectory::writeState
A base-class to Ensemble with customized behaviour according to the type of T
boost::archive::binary_oarchive oarchive
delegated to Boost.Serialization
Definition: Archive.h:74
const Impl & getTrajectories() const
Getter.
Aggregate of parameters pertaining to stochastic simulations.
const ToBeAveragedType averageInRange(size_t begin, size_t n) const
Averages only in a range begin..begin+n-1
Factory class for Randomized types.
Definition: Randomized.h:112
const T toBeAveraged() const
returns the set of quantities condensed in a variable of type T that are “to be averaged” ...
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
The base class of the trajectory-bundle condensing the quintessential characteristics of any trajecto...
Definition: Trajectory.h:194
The very general concept of an averageable trajectory.
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
Stochastic(A &, typename Evolved::Derivs, double dtInit, double epsRel, double epsAbs, const A &scaleAbs, const evolved::Maker< A > &, unsigned long seed, bool noise, const randomized::Maker &)
Straightforward constructor combining the construction of Adaptive and randomized::Randomized.
Traits class governing how to average up several T_ELEM types into a T type in the most efficient way...
cpputils::iarchive & readState(cpputils::iarchive &iar)
read from an archive
Definition: Trajectory.h:216
boost::ptr_vector< Elem > Impl
The storage of the element trajectories is through a pointer-vector
Ensemble(Ptr trajs, bool displayProgress)
Generic constructor.