C++ mpi module for stochmagnet_main Package
SM_StochasticTrajectory.h
1 #ifndef SM_StochasticTrajectory_H
2 #define SM_StochasticTrajectory_H
3 
4 //super class header
5 #include "SM_StochasticOutput.h"
6 
7 
8 //Beam
9 #include "SM_Beam.h"
10 
11 //LL System to save
12 #include "SM_LandauLifschitzSystem.h"
13 
14 //IO header
15 #include "CORE_IO.h"
16 
28 class SM_StochasticTrajectory : public SM_StochasticOutput< SM_StochasticTrajectory> {
29 
30  // ATTRIBUTES
31 
32 public:
33 
34 
35 
36 private:
37 
38  //type class
41 
42  //particles selection to save the trajectory
43  std::vector<tIndex> mParticles;
44 
45  //simulations selection to save the trajectory of each point stored in mParticles
46  std::vector<tIndex> mSimulations;
47 
48  //iterator on simulation indices
49  std::vector<tIndex>::const_iterator mISimulation;
50 
51 
52  //trajectory file
53  std::ofstream *mFile;
54 
55  //energies of the system
56  std::valarray<tReal> mEnergies;
57 
58  //energy variation
59  tReal mT_old;
60  tReal mE_old;
61 
62 public:
63  // METHODS
64 
65  // CONSTRUCTORS
66 
70  mFile=NULL;
71  mParticles.resize(1);
72  mParticles[0]=0;
73  mT_old=0;
74  mE_old=0;
75 
76  }
77 
78 
79 
80  // DESTRUCTORS
81 public:
82 
85  virtual ~SM_StochasticTrajectory(void) {
86  }
87 
88 
89 
90 public:
91  //MEMORY
92 
106  virtual tMemSize getMemorySize() const {
107  return sizeof(*this)+getContentsMemorySize();
108  }
109 
118  virtual tMemSize getContentsMemorySize() const {
119  tMemSize mem=SuperClass::getContentsMemorySize();
120  mem+=mSimulations.size()*sizeof(tIndex);
121  mem+=mParticles.size()*sizeof(tIndex);
122  mem+=mEnergies.size()*sizeof(tReal);
123  return mem;
124  }
125 
129  inline static CORE_UniquePointer<SM_StochasticTrajectory> New() {
130  return CORE_UniquePointer<SM_StochasticTrajectory>(new SM_StochasticTrajectory(),SM_StochasticTrajectory::Delete());
131  }
135  inline static CORE_UniquePointer<SM_StochasticTrajectory> New(const SM_StochasticTrajectory& c) {
136  CORE_UniquePointer<SM_StochasticTrajectory> p=New();
137  p->copy(c);
138  return p;
139  }
140 
141  //SET & GET Methods
142  //=================
146  virtual void copy(const SM_StochasticOutputComponent& c) override {
147  SuperClass::copy(c);
148  const SM_StochasticTrajectory *cc=dynamic_cast<const SM_StochasticTrajectory* >(&c);
149  if (cc!=null) {
151  setParticles(cc->getParticles());
152  }
153  }
154 
158  virtual void adimensionize(const SM_Material& material) override {
159  }
160 
164  inline void setSimulations(std::initializer_list<tIndex>&& simulationIndices) {
165  mSimulations=simulationIndices;
166  mISimulation=mSimulations.begin();
167  }
171  inline void setSimulations(const std::vector<tIndex>& simulationIndices) {
172  mSimulations=simulationIndices;
173  mISimulation=mSimulations.begin();
174  }
175 
176 
179  inline const std::vector<tIndex>& getSimulations() const {
180  return mSimulations;
181  }
185  inline void setParticles(std::initializer_list<tIndex>&& particlesIndices) {
186  mParticles=particlesIndices;
187  }
191  inline void setParticles(const std::vector<tIndex>& particlesIndices) {
192  mParticles=particlesIndices;
193  }
196  inline const std::vector<tIndex>& getParticles() const {
197  return mParticles;
198  }
199 
200 
201  //implemented templated methods
202  //===================
203 
208  inline tBoolean open(const SM_Beam& beam) {
209  //sort the index of simulation
210  std::sort(mSimulations.begin(),mSimulations.end());
211  //sort the index of particles
212  std::sort(mParticles.begin(),mParticles.end());
213  //iterator on simulationd
214  mISimulation=mSimulations.begin();
215  //last registered total energy
216  mE_old=0;
217  //last registered time
218  mT_old=0;
219 
220  return (dynamic_cast< const SM_LandauLifschitzSystem *>(&beam.getSystem())!=null);
221  }
222 
227  inline tBoolean open(const tIndex& s,const SM_System& system) {
228  //reset the file
229 
230  if (mFile!=null) {
231  mFile->close();
232  delete mFile;
233  mFile=NULL;
234  }
235 
236 
237  tBoolean ok=true;
238  while (mISimulation!=mSimulations.end()) {//loop on simulations index
239  if (s<(*mISimulation)) {
240  break;//simulations is not saved
241  }
242  if (s==(*mISimulation)) {
243  if ((getIndex()==0) || (getRootIndex()==-1)) {
244  mFile=new std::ofstream(CORE_IO::GetAbsolutePath(getOutputPath()+"/"
245  +getPrefix()+"-s"+std::to_string(s)
246  +"-i"+std::to_string(getIndex())
247  +".txt").c_str(),
248  std::ios::out);
249  (*mFile)<<"#trajectory file of simulation at index "<<s<<"\n";
250  tString str=system.toString();
251  functions_string::replaceAll("\n","\n#",str);
252  (*mFile) <<"# X: when the particle with index does not exists \n";
253  (*mFile)<<"#System:"<<str<<"\n";
254  (*mFile)<<"#timeIndex ";
255  (*mFile)<<"\t time ";
256  (*mFile)<<"\t dt ";
257  (*mFile)<<"\t epsilon ";
258  //direction of particles
259  for(const auto& p : mParticles) {
260  (*mFile)<<"\t S_"<<p<<".x";
261  (*mFile)<<"\t S_"<<p<<".y";
262  (*mFile)<<"\t S_"<<p<<".z";
263  }
264  //total energy
265  (*mFile)<<"\t Etotal";
266  //energies
267  std::vector<tString> names;
268  system.getOperatorsName(names);
269  for(const auto& name:names) (*mFile)<<"\t E"<<name;
270 
271  mEnergies.resize(names.size()+1);
272 
273  //variation of total energy
274  (*mFile)<<"\t DEtotal/Dt";
275 
276  //net torque
277  (*mFile)<<"\t netTorque";
278 
279  //end
280  (*mFile)<<"\n";
281  }
282 
283  mT_old=0;
284  mE_old=0;
285  break;
286  }
287 
288  //next index imulation to save
289  mISimulation++;
290  }
291  return ok;
292  }
293 
294 
295 
299  inline tBoolean store(const SM_System& system) {
300  //std::cout<<"begin SM_StochTrajectory::store("<<getIndex()<<")\n";
301  //get LL system
302  const SM_LandauLifschitzSystem * llSystem=dynamic_cast< const SM_LandauLifschitzSystem *>(&system);
303 
304  //compute the current energies
305  llSystem->computeEnergies(llSystem->getTimeIndex(),mEnergies);
306  tReal netTorque=llSystem->computeNetTorque();
307 
308  //write mu at time step i
309  if (mFile!=NULL) {
310 
311 
312  //number of particles of the system
313  tIndex nParticles=system.getNetwork().getParticlesNumber();
314 
315  //get S at time
316  const SM_RealField& S=system.getMagneticMomentDirections();
317  //get dimension of S
318  tDimension k;
319  //iterator on S at particle p
320  const tReal *iSp=null;
321  //time of the mu
322  (*mFile)<<llSystem->getTimeIndex();
323  (*mFile)<<"\t"<<std::setprecision(12)<<llSystem->getTime();
324  (*mFile)<<"\t"<<std::setprecision(12)<<llSystem->getTimeStepper().getTimeStep();
325  (*mFile)<<"\t"<<std::setprecision(12)<<llSystem->getNoiseRate();
326  //print the particles
327  for(const auto& p : mParticles) {
328  if (p<nParticles) {
329  iSp=S(p);
330  for (k=0;k<SM_Constants::DIM;k++) {
331  (*mFile)<<"\t"<<std::setprecision(12)<<(*iSp);
332  iSp++;
333  }
334  } else {
335  for (k=0;k<SM_Constants::DIM;k++) {
336  (*mFile)<<"\t\t 0.";
337  }
338  }
339 
340  }
341 
342 
343  //total energy
344  tReal dE=mEnergies[0];
345  //current time
346  tReal dT=llSystem->getTime();
347  //print the energy in file
348  for(const auto& E : mEnergies) (*mFile)<<"\t"<<E;
349 
350  //compute time variation
351  dT-=mT_old;
352  //compute energy variation
353  dE-=mE_old;
354  //save the current energy and time for the next computation
355  mE_old=mEnergies[0];
356  mT_old=llSystem->getTime();
357  //print the variation of energy with respect of time
358  if (dT!=0) (*mFile)<<"\t"<<std::setprecision(12)<<(dE/dT);
359  else (*mFile)<<"\t"<<std::setprecision(12)<<dE;
360 
361  //compute the current net torque
362  (*mFile)<<"\t"<<std::setprecision(12)<<netTorque;
363 
364 
365  (*mFile)<<"\n";
366 
367  }
368  //std::cout<<"end SM_StochTrajectory::store("<<getIndex()<<")\n";
369  return true;
370  }
376  inline tBoolean close(const tIndex& s,const SM_System& system,const tBoolean& hasSucceeded) {
377  if (mFile!=NULL) {
378  mFile->close();
379  delete mFile;
380  mFile=NULL;
381  }
382  return true;
383  }
387  inline tBoolean close(const SM_Beam& beam) {
388  return true;
389  }
390 
397  virtual void closePackedSimulations(const SM_Beam& beam,
398  const tIndex& rawValuesNumber,
399  const std::valarray<tReal>& rawValues) override {
400 
401 
402  }
403 
404 
405 public:
406 
407 
408 public:
409  // OTHERS methods
410 
411 
412 
413 
414 };
415 
416 #endif
static tString GetAbsolutePath(const tString &path)
get the absolute path of the path
Definition: CORE_IO.h:365
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
This class defines a general stochastic beam of trajectories of system.
Definition: SM_Beam.h:61
const SM_System & getSystem() const
get the system
Definition: SM_Beam.h:264
static constexpr tDimension DIM
space dimension
Definition: SM_Constants.h:80
This class is a simulation of one trajectory class for Stoch Magnet package.
Definition: SM_LandauLifschitzSystem.h:59
const tIndex & getTimeIndex() const
get the time index
Definition: SM_LandauLifschitzSystem.h:221
const tReal & getTime() const
get the time
Definition: SM_LandauLifschitzSystem.h:214
const SM_TimeStepper & getTimeStepper() const
get the time stepper
Definition: SM_LandauLifschitzSystem.h:200
const tReal & getNoiseRate() const
get the noise rate
Definition: SM_LandauLifschitzSystem.h:251
tReal computeNetTorque() const
compute Net Torque
Definition: SM_LandauLifschitzSystem.h:347
This class describes a materials defined by state attributes:
Definition: SM_Material.h:61
const tInteger & getParticlesNumber() const
return the particles number
Definition: SM_Network.h:349
This class stores stochastic outpus computed for each trajectory of a simulation of trajectories of a...
Definition: SM_StochasticOutputComponent.h:36
const int & getRootIndex() const
get the root index of the output component
Definition: SM_StochasticOutputComponent.h:222
const tString & getPrefix() const
return the prefix
Definition: SM_StochasticOutputComponent.h:190
const int & getIndex() const
get the index of the output component
Definition: SM_StochasticOutputComponent.h:217
const tString & getOutputPath() const
return the output path
Definition: SM_StochasticOutputComponent.h:178
this class implements the virtual methods of its base class SM_StochasticOutputComponent with templat...
Definition: SM_StochasticOutput.h:24
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_StochasticOutput.h:84
virtual void copy(const SM_StochasticOutputComponent &c)
copy
Definition: SM_StochasticOutput.h:94
This class stores stochastic trajectories for a list of points and a list of simulations for LL beam.
Definition: SM_StochasticTrajectory.h:28
void setSimulations(std::initializer_list< tIndex > &&simulationIndices)
set the simulation indices to save
Definition: SM_StochasticTrajectory.h:164
const std::vector< tIndex > & getSimulations() const
get the simulations
Definition: SM_StochasticTrajectory.h:179
static CORE_UniquePointer< SM_StochasticTrajectory > New()
build a new instance of a stochastic data
Definition: SM_StochasticTrajectory.h:129
tBoolean open(const tIndex &s, const SM_System &system)
open the stochastic data for simulation s
Definition: SM_StochasticTrajectory.h:227
tBoolean close(const SM_Beam &beam)
close stochastic data at the end of all simulations
Definition: SM_StochasticTrajectory.h:387
const std::vector< tIndex > & getParticles() const
get the simulations
Definition: SM_StochasticTrajectory.h:196
tBoolean open(const SM_Beam &beam)
open the stochastic data
Definition: SM_StochasticTrajectory.h:208
tBoolean store(const SM_System &system)
store the stochastic data during the relation method of the system
Definition: SM_StochasticTrajectory.h:299
virtual ~SM_StochasticTrajectory(void)
destroy
Definition: SM_StochasticTrajectory.h:85
static CORE_UniquePointer< SM_StochasticTrajectory > New(const SM_StochasticTrajectory &c)
build a new instance of a stochastic data
Definition: SM_StochasticTrajectory.h:135
virtual void adimensionize(const SM_Material &material) override
adimensionize the output compoent with material characteristic
Definition: SM_StochasticTrajectory.h:158
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_StochasticTrajectory.h:118
SM_StochasticTrajectory(void)
create
Definition: SM_StochasticTrajectory.h:69
tBoolean close(const tIndex &s, const SM_System &system, const tBoolean &hasSucceeded)
close the stochastic data for the simulation s
Definition: SM_StochasticTrajectory.h:376
virtual void closePackedSimulations(const SM_Beam &beam, const tIndex &rawValuesNumber, const std::valarray< tReal > &rawValues) override
close stochastic data at the end of all packed simulations
Definition: SM_StochasticTrajectory.h:397
void setSimulations(const std::vector< tIndex > &simulationIndices)
set the simulation indices to save
Definition: SM_StochasticTrajectory.h:171
virtual void copy(const SM_StochasticOutputComponent &c) override
copy the stochastic data
Definition: SM_StochasticTrajectory.h:146
void setParticles(std::initializer_list< tIndex > &&particlesIndices)
set the particle indices to save
Definition: SM_StochasticTrajectory.h:185
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_StochasticTrajectory.h:106
void setParticles(const std::vector< tIndex > &particlesIndices)
set the particle indices to save
Definition: SM_StochasticTrajectory.h:191
This class is a one simulation of a beam for Stoch Magnet package.
Definition: SM_System.h:53
const SM_RealField & getMagneticMomentDirections() const
get the unit direction of spins at time
Definition: SM_System.h:267
virtual tString toString() const override
turn the class into a string representation
Definition: SM_System.h:749
const tReal & computeEnergies(std::valarray< tReal > &energies) const
compute the energies per operators
Definition: SM_System.h:705
const SM_Network & getNetwork() const
get the network
Definition: SM_System.h:170
void getOperatorsName(std::vector< tString > &names) const
get the operator names
Definition: SM_System.h:358
const tReal & getTimeStep() const
return the time step
Definition: SM_TimeStepper.h:108