C++ mpi module for stochmagnet_main Package
SM_StochasticEnergy.h
1 #ifndef SM_StochasticEnergy_H
2 #define SM_StochasticEnergy_H
3 
4 #include "SM_StochasticOutput.h"
5 
6 #include "SM_Beam.h"
7 #include "SM_LandauLifschitzSystem.h"
8 #include "CORE_IO.h"
9 
21 class SM_StochasticEnergy : public SM_StochasticOutput<SM_StochasticEnergy> {
22 
23  // ATTRIBUTES
24 
25 public:
26 
27 
28 
29 private:
30 
31 
32  //type class
35 
36  //number of time steps
37  tIndex mTimeStepsNumber;
38 
39  //number of energies depening on number of operators
40  tUCInt mEnergiesNumber;
41 
42 
43  tIndex mEpsilonIndex;//iterator on mEpsilons
44 
45  std::valarray<tReal> mTimes;//values of times of size mTimeStepsNumber
46  std::valarray<tReal> mEpsilons;//Epsilon at t of size mTimeStepsNumber;
47 
48  std::valarray<tReal> mEnergies;//Energies at t by operator of size mTimeStepsNumber x mEnergiesNumber;
49  std::valarray<tReal> mEnergiesAtS;//Energies at simulation s of size mTimeStepsNumber x mEnergiesNumber;
50  std::valarray<tReal> mEs;//energy of the system at t
51 
52  std::ofstream *mFile;
53 public:
54  // METHODS
55 
56  // CONSTRUCTORS
57 
61  mTimeStepsNumber=0;
62  mEpsilonIndex=0;
63  mFile=NULL;
64  }
65 
66 
67 
68  // DESTRUCTORS
69 public:
70 
73  virtual ~SM_StochasticEnergy(void) {
74  }
75 
76 
77 
78 public:
79  //MEMORY
80 
94  virtual tMemSize getMemorySize() const {
95  return sizeof(*this)+getContentsMemorySize();
96  }
97 
106  virtual tMemSize getContentsMemorySize() const {
107  tMemSize mem=SuperClass::getContentsMemorySize();
108  mem+= mEnergies.size()*sizeof(tReal);
109  mem+= mEpsilons.size()*sizeof(tReal);
110  mem+= mTimes.size()*sizeof(tReal);
111  mem+= mEnergiesAtS.size()*sizeof(tReal);
112  mem+= mEs.size()*sizeof(tReal);
113  return mem;
114  }
115 
119  inline static CORE_UniquePointer<SM_StochasticEnergy> New() {
120  return CORE_UniquePointer<SM_StochasticEnergy>(new SM_StochasticEnergy(),
122  }
123 
124  //SET & GET Methods
125  //=================
126 
127 
128 
132  virtual void adimensionize(const SM_Material& material) override {
133  }
134 
138  inline void initialize(const SM_Beam& beam) {
139 
140 
141  mEpsilonIndex=0;
142 
143  //energies number
144  mEnergiesNumber=beam.getSystem().getOperatorsNumber()+1;
145 
146 
147  mTimeStepsNumber=beam.getStepsNumber()+1;
148  mTimes.resize(mTimeStepsNumber);
149  mTimes=0;
150 
151 
152  //epsilon(t)
153  mEpsilons.resize(mTimeStepsNumber);
154 
155  //E(t)
156  mEnergies.resize(mTimeStepsNumber*mEnergiesNumber);
157  //E(t) at simulation s
158  mEnergiesAtS.resize(mTimeStepsNumber*mEnergiesNumber);
159 
160  //E enregy of the system
161  mEs.resize(mEnergiesNumber);
162 
163  }
164 
165  //implemented templated methods
166  //=============================
167 
171  tBoolean open(const SM_Beam& beam) {
172 
173  //verify the system is LL
174  const SM_LandauLifschitzSystem *llSystem=dynamic_cast< const SM_LandauLifschitzSystem *>(&beam.getSystem());
175  if (llSystem==null) return false;
176 
177  if (getIndex()==0) {
178 
179  //initialize the data from beam
180  initialize(beam);
181 
182  //number of raws values number is null
183  tIndex &nRaws=getPackedRawValuesNumber();
184  nRaws=0;
185 
186  tString fn=CORE_IO::GetAbsolutePath(getOutputPath()+"/"+getPrefix()+".me").c_str();
187  if (mFile!=NULL) {
188  mFile->close();
189  delete mFile;
190  mFile=NULL;
191  }
192  if ((mEpsilonIndex==0)&&(llSystem!=null)) {
193 
194  mFile=new std::ofstream(fn,std::ios::out);
195  if ((mFile==null) || !(*mFile)) {
196  std::cout<<"Fatal error "<<fn<<" can not be written \n";
197  return false;
198  }
199 
200  tString str=beam.toString();
201  functions_string::replaceAll("\n","\n#",str);
202  (*mFile)<<"#"<<str<<"\n";
203  (*mFile)<<"#time epsilon ";
204  (*mFile)<<"\t Etotal";
205  std::vector<tString> names;
206  llSystem->getOperatorsName(names);
207  for(const auto& name:names) (*mFile)<<"\t E"<<name;
208  (*mFile)<<"\n";
209 
210  }
211  }
212 
213  //reset the energies to 0
214  mEnergies=0;
215  return true;
216  }
217 
218 
223  inline tBoolean open(const tIndex& s,const SM_System& system) {
224  mEnergiesAtS=0;//energies at simulation s of size mEnergiesNumber x mTimesSteps
225  return true;
226  }
227 
228 
232  tBoolean store(const SM_System& system) {
233 
234  const SM_LandauLifschitzSystem *llSystem=dynamic_cast< const SM_LandauLifschitzSystem *>(&system);
235  //compute the energies of the systems
236  llSystem->computeEnergies(llSystem->getTimeIndex(),mEs);
237 
238  if (getIndex()==0) {
239  //time
240  const tReal& time=llSystem->getTime();
241 
242  //epsilon
243  const tReal& epsilon_t=llSystem->getNoiseRate();
244 
245  if (mTimeStepsNumber==1) {
246  //time step
247  tReal dt=llSystem->getTimeStepper().getTimeStep()/2.;
248  if (fabs(mTimes[0]-time)<dt) {
249 
250  const tReal *Es=&mEs[0];
251  //register energies
252  tReal *EAtS=&mEnergiesAtS[0];
253  for (tUCInt k=0;k<mEnergiesNumber;k++) {
254  (*EAtS)+=(*Es);
255  Es++;
256  EAtS++;
257  }
258  //register epsilon
259  mEpsilons[0]=epsilon_t;
260  }
261 
262  } else {
263  //save energies at time
264 
265  //compute the energies of the systems
266 
267  const tReal *Es=&mEs[0];
268 
269  //register the time
270  const tIndex& timeIndex=llSystem->getTimeIndex();
271  mTimes[timeIndex]=time;
272  //register epsilon
273  mEpsilons[timeIndex]=epsilon_t;
274 
275  //register the energies
276  tReal *EAtS=&mEnergiesAtS[timeIndex*mEnergiesNumber];
277  for (tUCInt k=0;k<mEnergiesNumber;k++) {
278  (*EAtS)+=(*Es);
279  Es++;
280  EAtS++;
281  }
282  }
283  }
284 
285  return true;
286  }
287 
288 
289 
295  inline tBoolean close(const tIndex& s,const SM_System& system,const tBoolean& hasSucceeded) {
296 
297  if (hasSucceeded) {
298  if (getIndex()==0) {
300 
301  //register the data for simlation s
302  tReal *Es=&mEnergies[0];
303  const tReal *EAtS=&mEnergiesAtS[0];
304  const tReal *EAtSe=EAtS+mEnergiesAtS.size();
305  while (EAtS!=EAtSe) {
306  (*Es)+=(*EAtS);
307  Es++;
308  EAtS++;
309  }
310  }
311  }
312  return true;
313  }
314 
318  inline tBoolean close(const SM_Beam& beam) {
319  if (getIndex()==0) {
320  const tIndex &nRaws=getPackedRawValuesNumber();
321  //E(t)=\frac{1}{S} \sum_{s=0}^{s=S-1} E(t,epsilon_t)
322  tReal *Es=&mEnergies[0];
323  const tReal *Ese=Es+mEnergies.size();
324  while (Es!=Ese) {
325  (*Es)/=nRaws;
326  Es++;
327  }
328 
329  //save the file
330  if (mFile!=NULL) {
331  const tReal *iEpsilon=&mEpsilons[0];
332  const tReal *iEnergies=&mEnergies[0];
333  const tReal *iT=&mTimes[0];
334  tIndex t;
335  tUCInt k;
336  (*mFile) <<std::setprecision(12);
337  for (t=0;t<mTimeStepsNumber;t++) {
338  (*mFile)<<(*iT);
339  iT++;
340 
341  (*mFile)<<"\t"<<(*iEpsilon);
342  iEpsilon++;
343 
344  for (k=0;k<mEnergiesNumber;k++) {
345  (*mFile)<<"\t"<<(*iEnergies);
346  iEnergies++;
347  }
348  (*mFile)<<"\n";
349 
350 
351  }
352 
353  mFile->close();
354  delete mFile;
355  mFile=NULL;
356  }
357 
358  //next epsilon index
359  mEpsilonIndex++;
360  }
361 
362  return true;
363  }
364 
365 
366 
367 
374  virtual void closePackedSimulations(const SM_Beam& beam,
375  const tIndex& rawValuesNumber,
376  const std::valarray<tReal>& rawValues) override {
377 
378 
379  }
380 
383  virtual tString toString() const override {
384  std::stringstream ret;
385  ret<<SuperClass::toString();
386  return ret.str();
387  }
388 
389 public:
390 
391 
392 public:
393  // OTHERS methods
394 
395 
396 
397 
398 };
399 
400 #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
virtual tString toString() const override
turn the class into a string representation
Definition: SM_Beam.h:315
const tIndex & getStepsNumber() const
get the number of steps for stochastic computation
Definition: SM_Beam.h:207
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
This class describes a materials defined by state attributes:
Definition: SM_Material.h:61
This class stores stochastic energy:
Definition: SM_StochasticEnergy.h:21
virtual void adimensionize(const SM_Material &material) override
adimensionize the output compoent with material characteristic
Definition: SM_StochasticEnergy.h:132
tBoolean open(const tIndex &s, const SM_System &system)
open the stochastic data for simulation s
Definition: SM_StochasticEnergy.h:223
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_StochasticEnergy.h:94
tBoolean open(const SM_Beam &beam)
open the stochastic data
Definition: SM_StochasticEnergy.h:171
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_StochasticEnergy.h:106
tBoolean store(const SM_System &system)
store the stochastic data during the relations method of the system
Definition: SM_StochasticEnergy.h:232
virtual ~SM_StochasticEnergy(void)
destroy
Definition: SM_StochasticEnergy.h:73
virtual tString toString() const override
return the string representation of the class
Definition: SM_StochasticEnergy.h:383
tBoolean close(const tIndex &s, const SM_System &system, const tBoolean &hasSucceeded)
close the stochastic data for the simulation s
Definition: SM_StochasticEnergy.h:295
void initialize(const SM_Beam &beam)
set the number of epsilon & energies
Definition: SM_StochasticEnergy.h:138
tBoolean close(const SM_Beam &beam)
close stochastic data at the end of all simulations
Definition: SM_StochasticEnergy.h:318
static CORE_UniquePointer< SM_StochasticEnergy > New()
build a new instance of a stochastic data
Definition: SM_StochasticEnergy.h:119
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_StochasticEnergy.h:374
SM_StochasticEnergy(void)
create
Definition: SM_StochasticEnergy.h:60
const tString & getPrefix() const
return the prefix
Definition: SM_StochasticOutputComponent.h:190
virtual tString toString() const override
return the string representation of the class
Definition: SM_StochasticOutputComponent.h:398
const tIndex & getPackedRawValuesNumber() const
get the packed raw values number
Definition: SM_StochasticOutputComponent.h:286
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
This class is a one simulation of a beam for Stoch Magnet package.
Definition: SM_System.h:53
tIndex getOperatorsNumber() const
get the operators number
Definition: SM_System.h:352
const tReal & computeEnergies(std::valarray< tReal > &energies) const
compute the energies per operators
Definition: SM_System.h:705
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