C++ main module for stochmagnet Package  1.0
SM_Beam.h
Go to the documentation of this file.
1 #ifndef SM_Beam_H
2 #define SM_Beam_H
3 
4 //define the std::valarray object
5 #include <valarray>
6 //define the std::array object
7 #include <array>
8 
9 //base class
10 #include "SM_Object.h"
11 
12 //core run for class factoru
13 #include "CORE_Run.h"
14 
15 //system class
16 #include "SM_System.h"
18 #include "SM_StratonovichSystem.h"
19 #include "SM_ItoSystem.h"
20 
21 //constant noise rate
23 
24 //stochastic data
25 #include "SM_StochasticFunction.h"
26 
27 //network data
28 #include "SM_Network.h"
29 
30 //Time stepper data
31 #include "SM_TimeStepper.h"
32 
33 //operator interface
34 #include "SM_Operator.h"
35 
46 class SM_Beam : public SM_Object {
47 
48  //attributes
49 private :
50 
51 
52  //number of simulations
54 
55  //mu array of size mBeamSize x nTimeStepsNumber x mParticlesNumber x mDimension
56  std::valarray<tReal> mMu;
57 
58  //mu array of size mBeamSize x nTimeStepsNumber x (nOperatorsNumber+1)
59  std::valarray<tReal> mEs;
60 
61  //templated method called
63 
64  //associations
65  //============
66 
67  //stochastic function
69 
70  //system
72 
73  //deterministic system
75  std::valarray<tReal> mDMu;
76  std::valarray<tReal> mDEs;
77 
78 protected:
79  // CONSTRUCTORS
82  SM_Beam(void) {
83  mBeamSize=1000;
85  }
86 
87  // DESTRUCTORS
90  virtual ~SM_Beam(void) {
91  }
92 
93 public :
94  // CREATE class as a pointer
95 
99  inline static CORE_UniquePointer<SM_Beam> New() {
101  SM_Beam::Delete());
102  }
103 
104 
105 
106  //MEMORY
107 
121  virtual tMemSize getMemorySize() const {
122  return sizeof(*this)+getContentsMemorySize();
123  }
124 
133  virtual tMemSize getContentsMemorySize() const {
135  mem+=mMu.size()*sizeof(tReal);
136  mem+=mEs.size()*sizeof(tReal);
137  mem+=mDMu.size()*sizeof(tReal);
138  mem+=mDEs.size()*sizeof(tReal);
139  mem+=(mStochasticFunction.get()==null)?0:mStochasticFunction->getMemorySize();
140  mem+=(mSystem.get()==null)?0:mSystem->getMemorySize();
141  mem+=(mDeterministicSystem.get()==null)?0:mDeterministicSystem->getMemorySize();
142 
143  return mem;
144  }
145 
146 
147  //SET & GET methods
148 
149 
153  inline void setIsTemplatedSimulateVersion(const tBoolean& v) {
155  }
156 
160  inline void setBeamSize(const tIndex& n) {
161  mBeamSize=n;
162  }
166  inline const tIndex& getBeamSize() const {
167  return mBeamSize;
168  }
169 
174  mStochasticFunction=std::move(f);
175  }
180  return *mStochasticFunction.get();
181  }
186  mSystem=std::move(sys);
187  }
191  virtual const SM_System& getSystem() const {
192  return *mSystem.get();
193  }
194 
198  virtual SM_System& getSystem() {
199  return *mSystem.get();
200  }
201 
202 
206  inline const std::valarray<tReal>& getMagneticMoment() const {
207  return mMu;
208  }
212  inline std::valarray<tReal>& getMagneticMoment() {
213  return mMu;
214  }
218  inline const std::valarray<tReal>& getEnergies() const {
219  return mEs;
220  }
224  inline std::valarray<tReal>& getEnergies() {
225  return mEs;
226  }
227  // MAIN Method
228 
229 public:
232  inline void discretize() {
233  //get system
234  const SM_System& system=getSystem();
235 
236  //get network
237  const SM_Network& network=system.getNetwork();
238 
239  //get time stepper
240  const SM_TimeStepper& stepper=system.getTimeStepper();
241 
242  //number of time steps
243  const tIndex& nTimeSteps=stepper.getMaximumTimeStepsNumber();
244 
245  //the mu by simulation
246  tIndex muSize=network.getDimension()*network.getParticlesNumber()*(nTimeSteps+1);
247 
248  //the E by simulation
249  tIndex ESize=(system.getOperatorsNumber()+1)*nTimeSteps;
250 
251  //size of mu
252  try {
253  mMu.resize(mBeamSize * muSize);
254  } catch (std::exception& e) {
255  CORE_Run::Out().printError("not enough memory to alocate "+std::to_string(mBeamSize*muSize*sizeof(tReal)/1000000)+" Mo of memory for mMu");
256  CORE_Run::Out()<<e.what()<<"\n";
257  }
258  //size of E
259  try {
260  mEs.resize(mBeamSize * ESize);
261  } catch (std::exception& e) {
262  CORE_Run::Out().printError("not enough memory to alocate "+std::to_string(mBeamSize*ESize*sizeof(tReal)/1000000)+" Mo of memory for mEs");
263  CORE_Run::Out()<<e.what()<<"\n";
264  }
265 
266  //discretize the system
267  mSystem->discretize();
268 
269  //define the determinitic system
270  mDeterministicSystem=SM_ItoSystem::New();//create a deterministic system
271  mDeterministicSystem->getNetwork().copy(network);//same network
272  mDeterministicSystem->getTimeStepper().copy(stepper);//same stepper
273  mDeterministicSystem->getLandauLifschitzFunction().copy(system.getLandauLifschitzFunction());//same LL
275  mDeterministicSystem->setNoiseRateFunction(noise);//no noise
276  mDeterministicSystem->getNoiseRateFunction().setNoiseRate(0);
277  mDeterministicSystem->copyOperators(system);//same operators
278  mDeterministicSystem->setInitialMagneticMoment(system.getInitialMagneticMoment());//same mu0
279  mDMu.resize(muSize);
280  mDEs.resize(ESize);
281  mDeterministicSystem->discretize();//discretize
282 
283  }
284 
288  inline const std::valarray<tReal>& getDeterministicEnergies() const {
289  return mDEs;
290  }
291 
296  if (mDeterministicSystem.get()!=null) {
297  mDeterministicSystem->makeRelaxation(null,
298  &mDMu[0],
299  &mDEs[0]);
300  }
302  return simulate();
303  }
304 private:
308  tIndex simulate();
309 
314  //get the system
315  SM_System &system=getSystem();
316 
317  //get the noise of the system
318  const SM_NoiseRateFunction *nF=&system.getNoiseRateFunction();
319 
320  //cast to templated classes
321 
322  SM_ItoSystem* sys1=dynamic_cast<SM_ItoSystem*>(&getSystem());
323  if (sys1!=null) {
325  return templatedSimulate(*sys1,*dynamic_cast<const SM_ConstantNoiseRateFunction*>(nF));
326  } else if (nF->isInstanceOf<SM_InverseNoiseRateFunction>()) {
327  return templatedSimulate(*sys1,*dynamic_cast<const SM_InverseNoiseRateFunction*>(nF));
328  }
329  }
330  SM_StratonovichSystem* sys2=dynamic_cast<SM_StratonovichSystem*>(&getSystem());
331  if (sys2!=null) {
333  return templatedSimulate(*sys2,*dynamic_cast<const SM_ConstantNoiseRateFunction*>(nF));
334  } else if (nF->isInstanceOf<SM_InverseNoiseRateFunction>()) {
335  return templatedSimulate(*sys2,*dynamic_cast<const SM_InverseNoiseRateFunction*>(nF));
336  }
337  }
339  if (sys3!=null) {
341  return templatedSimulate(*sys3,*dynamic_cast<const SM_ConstantNoiseRateFunction*>(nF));
342  } else if (nF->isInstanceOf<SM_InverseNoiseRateFunction>()) {
343  return templatedSimulate(*sys3,*dynamic_cast<const SM_InverseNoiseRateFunction*>(nF));
344  }
345  }
346 
347  //default simulation
348  return simulate();
349  }
350 
351 public:
355  virtual tString toString() const override {
356  std::stringstream ret;
357  //print id
358  ret<<SM_Object::toString()<<"\n";
359  //print beam size
360  ret<<"beam size: "<<mBeamSize<<"\n";
361 
362  ret<<"stochastic Function:";
363  if (mStochasticFunction.get()!=null)
364  ret<<mStochasticFunction->toString()<<"\n";
365  else
366  ret<<"null\n";
367  ret<<"system:";
368  if (mSystem.get()!=null)
369  ret<<mSystem->toString()<<"\n";
370  else
371  ret<<"null\n";
372 
373  return ret.str();
374  }
375 private:
378  template<class SystemImpl,class NoiseImpl>
379  tIndex templatedSimulate(SystemImpl& system,const NoiseImpl& noise);
380 
381 public:
385  void computeL2Convergence(std::valarray<tReal>& L2conv) const;
386 
387 
388 };
389 
390 #include "SM_Beam.hpp"
391 
392 #endif
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:94
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
tBoolean isInstanceOf() const
test if the clas T is an instance of this class
Definition: CORE_Object.h:271
virtual tString toString() const
return the string representation of the object node
Definition: CORE_Object.h:314
void printError(const tString &msg)
print the message on error output
Definition: CORE_Out.h:288
static CORE_Out & Out()
return the output stream
Definition: CORE_Run.h:174
This class is a simulation of a beam of trajectories class for Stoch Microm package.
Definition: SM_Beam.h:46
tIndex templatedSimulate()
simulate a beam by computing mu field by calling only templated methods
Definition: SM_Beam.h:313
SM_Beam(void)
create
Definition: SM_Beam.h:82
std::valarray< tReal > mDMu
Definition: SM_Beam.h:75
CORE_UniquePointer< SM_System > mSystem
Definition: SM_Beam.h:71
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_Beam.h:133
std::valarray< tReal > & getEnergies()
return the energies array
Definition: SM_Beam.h:224
std::valarray< tReal > & getMagneticMoment()
return the mu array
Definition: SM_Beam.h:212
virtual tString toString() const override
turn the class into a string representation
Definition: SM_Beam.h:355
std::valarray< tReal > mMu
Definition: SM_Beam.h:56
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_Beam.h:121
std::valarray< tReal > mEs
Definition: SM_Beam.h:59
void setSystem(CORE_UniquePointer< SM_System > &sys)
set the system
Definition: SM_Beam.h:185
tIndex mBeamSize
Definition: SM_Beam.h:53
virtual SM_System & getSystem()
return the system for writing
Definition: SM_Beam.h:198
void discretize()
discretize the system
Definition: SM_Beam.h:232
tIndex simulate()
simulate a beam by computing mu field by calling only virtual methods
Definition: SM_Beam.cpp:13
tIndex runSimulations()
run the simulations
Definition: SM_Beam.h:295
const std::valarray< tReal > & getDeterministicEnergies() const
return the deterministic energies
Definition: SM_Beam.h:288
tBoolean mIsTemplatedSimulateVersion
Definition: SM_Beam.h:62
const tIndex & getBeamSize() const
get the beam size
Definition: SM_Beam.h:166
const std::valarray< tReal > & getEnergies() const
return the Energy array
Definition: SM_Beam.h:218
static CORE_UniquePointer< SM_Beam > New()
build a new instance of a SM_Beam
Definition: SM_Beam.h:99
virtual ~SM_Beam(void)
destroy
Definition: SM_Beam.h:90
CORE_UniquePointer< SM_System > mDeterministicSystem
Definition: SM_Beam.h:74
void computeL2Convergence(std::valarray< tReal > &L2conv) const
compute the L2 convergence for mu
Definition: SM_Beam.cpp:67
void setBeamSize(const tIndex &n)
set the beam size
Definition: SM_Beam.h:160
CORE_UniquePointer< SM_StochasticFunction > mStochasticFunction
Definition: SM_Beam.h:68
std::valarray< tReal > mDEs
Definition: SM_Beam.h:76
void setStochasticFunction(CORE_UniquePointer< SM_StochasticFunction > &f)
set the stochastic function
Definition: SM_Beam.h:173
const SM_StochasticFunction & getStochasticFunction() const
get the stochastic function
Definition: SM_Beam.h:179
void setIsTemplatedSimulateVersion(const tBoolean &v)
set true if the templated simulate method is called
Definition: SM_Beam.h:153
const std::valarray< tReal > & getMagneticMoment() const
return the mu array
Definition: SM_Beam.h:206
virtual const SM_System & getSystem() const
return the system for reading
Definition: SM_Beam.h:191
This class describes an noise rate function of the form .
Definition: SM_ConstantNoiseRateFunction.h:13
static CORE_UniquePointer< SM_ConstantNoiseRateFunction > New()
build a new instance of a SM_NoiseRateFunction
Definition: SM_ConstantNoiseRateFunction.h:61
This class describes an noise rate function of the form .
Definition: SM_InverseNoiseRateFunction.h:13
This class is a simulation of one trajectory class for Stoch Microm package based on Ito system.
Definition: SM_ItoSystem.h:12
static CORE_UniquePointer< SM_ItoSystem > New()
build a new instance of a system
Definition: SM_ItoSystem.h:45
This class is describes a a network.
Definition: SM_Network.h:18
tDimension getDimension() const
return the dimension
Definition: SM_Network.h:133
tInteger getParticlesNumber() const
return the particles number
Definition: SM_Network.h:146
This class describes a noise rate function.
Definition: SM_NoiseRateFunction.h:13
This class is a base class for Stoch Microm package.
Definition: SM_Object.h:19
This class describes a stochastic function interface to generate random numbers.
Definition: SM_StochasticFunction.h:15
This class is a simulation of one trajectory class for Stoch Microm package based on Stratonovich sys...
Definition: SM_StratonovichNormalizedSystem.h:12
This class is a simulation of one trajectory class for Stoch Microm package based on Stratonovich sys...
Definition: SM_StratonovichSystem.h:12
This class is a simulation of one trajectory class for Stoch Microm package.
Definition: SM_System.h:51
tIndex getOperatorsNumber() const
get the operator with name
Definition: SM_System.h:317
const SM_LandauLifschitzFunction & getLandauLifschitzFunction() const
get the Landau Lifschitz function return the Landau lifschitz function for reading
Definition: SM_System.h:262
const SM_RealField & getInitialMagneticMoment() const
set the initial Magnetic Moment
Definition: SM_System.h:224
SM_NoiseRateFunction & getNoiseRateFunction()
get the noise rate function
Definition: SM_System.h:245
const SM_Network & getNetwork() const
get the network
Definition: SM_System.h:165
const SM_TimeStepper & getTimeStepper() const
get the time stepper
Definition: SM_System.h:180
This class is describes a time stepper.
Definition: SM_TimeStepper.h:13
const tIndex & getMaximumTimeStepsNumber() const
return the time steps number
Definition: SM_TimeStepper.h:130
typename std::unique_ptr< T, CORE_Object::Delete > CORE_UniquePointer
Definition: sp.h:8
#define tIndex
Definition: types.h:157
#define tString
Definition: types.h:147
#define tMemSize
Definition: types.h:166
#define tBoolean
Definition: types.h:151
#define tReal
Definition: types.h:137