C++ mpi module for stochmagnet_main Package
SM_StochasticDecreasingNoise.h
1 #ifndef SM_StochasticDecreasingNoise_H
2 #define SM_StochasticDecreasingNoise_H
3 
4 //inhereited class header
5 #include "SM_StochasticOutput.h"
6 
7 //beam class header
8 #include "SM_Beam.h"
9 
10 //LL system header
11 #include "SM_LandauLifschitzSystem.h"
12 
13 //IO header
14 #include "CORE_IO.h"
15 
29 class SM_StochasticDecreasingNoise : public SM_StochasticOutput<SM_StochasticDecreasingNoise> {
30 
31  // ATTRIBUTES
32 
33 public:
34 
35 
36 
37 private:
38 
39  //type class
42 
43  //mean of S overt the particles
44  std::array<tReal,SM_Constants::DIM> mBarS;
45 
46  //normalized solution
47  std::array<tReal,SM_Constants::DIM> mSinf;
48 
49  //normalized times at t
50  std::valarray<tReal> mTimes;
51  tReal *miTime;//iterator on time
52 
53 
54 
55  tReal* miE;//iterator on mE
56 
57  tString mPackedFileName;
58  tString mFileName;
59 
60 
61  //number of registered times number
62  tIndex mRegisteredTimesNumber;
63 
64 
65 public:
66  // METHODS
67 
68  // CONSTRUCTORS
69 
73 
75  "time",
76  "E(|Sinf-S|^2/eps^2)"});
77  }
78 
79 
80  // DESTRUCTORS
84  }
85 
86 
87 
88 public:
89  //MEMORY
90 
104  virtual tMemSize getMemorySize() const {
105  return sizeof(*this)+getContentsMemorySize();
106  }
107 
116  virtual tMemSize getContentsMemorySize() const {
117  tMemSize mem=SuperClass::getContentsMemorySize();
118  mem+=sizeof(tReal)*mTimes.size();
119  mem+=mSinf.size()*sizeof(tReal);
120  mem+=mBarS.size()*sizeof(tReal);
121  return mem;
122  }
123 
127  inline static CORE_UniquePointer<SM_StochasticDecreasingNoise> New() {
128  return CORE_UniquePointer<SM_StochasticDecreasingNoise>(new SM_StochasticDecreasingNoise(),
130  }
131 
132  //SET & GET Methods
133  //=================
134 
138  virtual void adimensionize(const SM_Material& material) override {
139  }
142  inline void setSpinDirectionsAtRelaxationTime(const std::array<tReal,SM_Constants::DIM>& Hext) {
143 
144  const tReal *iHext=Hext.data();
145  tReal d=0;
146  for(auto& Sk:mSinf) {
147  //Sinf=Hext
148  Sk=(*iHext);
149  d+=Sk*Sk;
150 
151  iHext++;
152  }
153  //Sinf is normalized
154  d=sqrt(d);
155  if (d>0) {
156  for(auto& Sk:mSinf) {
157  Sk/=d;
158  }
159  }
160  }
161 
164  inline void setSpinDirectionsAtRelaxationTime(const std::vector<tReal>& Hext) {
165 
166  //iterator on Hex
167  auto iHext=Hext.begin();
168 
169  //iterator on Sinf
170  tReal *iSk=mSinf.data();
171 
172  //end iteraton on Sinf
173  const tReal* eS=mSinf.data();eS+=mSinf.size();
174 
175  //Sinf=0
176  memset(iSk,0,sizeof(tReal)*mSinf.size());
177 
178  tReal d=0;
179  while ((iHext!=Hext.end()) && (iSk!=eS)) {
180 
181  //Sinf=Hext
182  (*iSk)=(*iHext);
183  d+=(*iSk)*(*iSk);
184 
185  iHext++;
186  iSk++;
187  }
188 
189  //Sinf is normalized
190  d=sqrt(d);
191  if (d>0) {
192  for(auto& Sk:mSinf) {
193  Sk/=d;
194  }
195  }
196 
197 
198  }
199 
200 
201  //implemented templated methods
202  //===================
203 
207  inline tBoolean open(const SM_Beam& beam) {
208 
209  tBoolean ok=true;
210  if (getIndex()==0) {//only stochastic output index index 0 creates the data
211 
212  //system must be a LL system to work on this stochastic output
213  const SM_LandauLifschitzSystem* llSystem=dynamic_cast< const SM_LandauLifschitzSystem*>(&beam.getSystem());
214  if (llSystem==null) {
215  return false;
216  }
217 
218  //number of raws values number is null
219  tIndex &nRaws=getPackedRawValuesNumber();
220  nRaws=0;
221 
222 
223  //get the number of time steps for the simultaion
224  tInteger nDrawns=beam.getStepsNumber();
225  //maximum number of draws
226  nDrawns/=getDrawnStepsNumber();
227  nDrawns+=1;
228 
229  //E(t)=displaystyle \frac{1}{S} . \sum_{s=0}^{s=S-1} \f$
230  //drawn value per time
231  std::valarray<tReal>& E=getPackedRawValues();
232 
233  //time of drawn
234  mTimes.resize(nDrawns);
235  memset(&mTimes[0],0,mTimes.size()*sizeof(tReal));
236 
237  //initialize drawn values to 0
238  E.resize(mTimes.size());
239  memset(&E[0],0,E.size()*sizeof(tReal));
240 
241  //set the outputs
242  std::valarray<tReal>& outputs=getOutputValues();
243  outputs.resize(mTimes.size());
244  memset(&outputs[0],0,outputs.size()*sizeof(tReal));
245 
246 
247  //open the file for writting
248  mPackedFileName=CORE_IO::GetAbsolutePath(this->getOutputPath()+"/"+this->getPrefix());
249  mFileName=mPackedFileName;
250  if (getPackedSimulationsNumber()>1) {
251  mPackedFileName+="-"+std::to_string(getPackedSimulationsIndex())+"p";
252  }
253  mPackedFileName+=".dn";
254  mFileName+=".dn";
256  std::ofstream file(mPackedFileName.c_str(),std::ios::out);
257  if (file) {
258  printHeader(file,beam);
259 
260  } else {
261  std::cout<<"Fatal Error : "<<mPackedFileName<<" can not be opened \n";
262  return false;
263  }
264  }
265  }
266 
267  return ok;
268  }
269 
274  inline tBoolean open(const tIndex& s,const SM_System& system) {
275  tBoolean ok=true;
276  if (getIndex()==0) {
277  //initialize the pointer values for the simulation
278  miE=&getPackedRawValues()[0];
279  miTime=&mTimes[0];
280  mRegisteredTimesNumber=0;
281  }
282  return ok;
283  }
284 
285 
286 
287 
291  inline tBoolean store(const SM_System& system) {
292 
293  tBoolean ok=true;
294  if ((getRootIndex()==-1) || (getIndex()==0)) {
295 
296  //compute mean of S over the networks
297 
298  //std::cout<<"store for SM_StochaticOutputDecreasingNoise."<<getIndex()<<" RootIndex:"<<getRootIndex()<<"\n";
300  //std::cout<<"end store for SM_StochaticOutputDecreasingNoise."<<getIndex()<<"\n";
301  }
302 
303 
304  if (getIndex()==0) {
305 
306  //ll system casting
307  const SM_LandauLifschitzSystem* llSystem=dynamic_cast< const SM_LandauLifschitzSystem*>(&system);
308 
309 
310  if (mRegisteredTimesNumber>=mTimes.size()) {
311  throw CORE_Exception("stochmagnet/main/stochasticOutput",
312  "SM_StochasticDecreasingNoise::store()",
313  "error in times dimension");
314  }
315  //std::cout<<"compute registered data ("<<getIndex()<<")\n";
316  //compute |S-S_inf|^2
317  const tReal *iSinf_k=mSinf.data();
318  tReal dk,d=0;
319  for(auto& Sk:mBarS) {
320  dk=(Sk-(*iSinf_k));
321  dk*=dk;
322  d+=dk;
323 
324  iSinf_k++;
325 
326  }
327 
328 
329  //get the noise rate at t
330  tReal iepsilon2=llSystem->getNoiseRate();
331  //eps:=eps^2
332  iepsilon2*=iepsilon2;
333  //eps:=1/eps^2
334  if (iepsilon2>functions_numeric::getEpsilon<tReal>()) iepsilon2=1./iepsilon2;
335  else iepsilon2=1.;
336 
337 
338 
339  //register the drawn value
340  //compute \frac{1}{epsilon_t^2} . \sum_p |S_\infinity-\bar S(t)|^2
341  (*miE)+=d*iepsilon2;
342 
343 
344  //time registration of the draw
345  *miTime=llSystem->getTime();
346  // if ((*miTime)==llSystem->getTimeStepper().getTimeStep()) {
347  // std::cout<<" store t="<<(*miTime)<<" E="<<(*miE)<<"\n";
348  // }
349 
350  //iterators at next drawn
351  miE++;
352  miTime++;
353  mRegisteredTimesNumber++;
354  }
355 
356  //std::cout<<"end SM_StochDecrNoise::store("<<getIndex()<<")\n";
357  return ok;
358 
359  }
360 
366  inline tBoolean close(const tIndex& s,const SM_System& system,const tBoolean& hasSucceeded) {
367  tBoolean ok=true;
368 
369  //register the number of drawns
370  //the s-th simulation has succeeded : mEs[k]+=\bar \mu_s[k]
371  if (getIndex()==0) getPackedRawValuesNumber()++;
372 
373  return ok;
374  }
375 
376 
380  inline const std::valarray<tReal>& getTimes() const {
381  return mTimes;
382  }
386  inline const tIndex& getRegisteredTimesNumber() const {
387  return mRegisteredTimesNumber;
388  }
392  inline tBoolean close(const SM_Beam& beam) {
393  if (getIndex()==0) {
395  //\f$ E(\varepsilon)= \displaystyle \sqrt { \sum_{k=0}^{k=d-1} E_k(\bar mu_k(\varepsilon,T))^2
396  std::ofstream file(mPackedFileName.c_str(),std::ios::app);
397  flushStochasticOutput(file,getPackedRawValuesNumber(),getPackedRawValues());
398  file.close();
399  }
400  }
401  return true;
402 
403  }
404 
411  virtual void closePackedSimulations(const SM_Beam& beam,
412  const tIndex& rawValuesNumber,
413  const std::valarray<tReal>& rawValues) override {
414  if (getIndex()==0) {
415  std::ofstream file(mFileName,std::ios::out);
416  if (file) {
417  //file<<"#Closed Packed Simulations\n";
418  //file<<"# raw values number:" <<rawValuesNumber<<"\n";
419  //file<<"# raw values:";
420  //for(const auto& v:rawValues) file<<"\t"<<v;
421  //file<<"\n";
422  printHeader(file,beam);
423  flushStochasticOutput(file,rawValuesNumber,rawValues);
424 
425  file.close();
426  }
427  }
428 
429  }
430 
433  virtual tString toString() const override {
434  std::stringstream ret;
435  ret<<SuperClass::toString();
436  ret<<"\t Sinf="<<functions_array::toString(mSinf)<<"\n";
437  return ret.str();
438  }
439 
440 
441 private:
446  inline void printHeader(std::ofstream& file,const SM_Beam& beam) {
447  //header is the data
448  //get the system
449  file<<"#Decreasing Noise with "<<getPackedSimulationsIndex()<<"/"<<getPackedSimulationsNumber()<<" packed simulations of size "<<beam.getBeamSize()<<" simulations generated by soft:"<<CORE_Run::GetSoftName()<<" Version:"<<CORE_Run::GetVersion()<<"\n";
450 
451  //print beam output
452  tString str="beam:"+beam.toString();
453  functions_string::replaceAll("\n","\n#",str);
454  file<<"#"<<str<<"\n";
455 
456  //print strochastic output
457  str="StochasticOutput:"+toString();
458  functions_string::replaceAll("\n","\n#",str);
459  file<<"#"<<str<<"\n";
460  file<<"# bar S(s,t) = frac{1}{P} sum_p S(s,t,p) \n";
461  file<<"# E(t)= frac{1}{S} . sum_{s=0}^{s=S-1} frac{1}{varepsilon_t^2}. frac{1}{S_{inf}-bar S(s,t)|^2} \n";
462  file<<"#";
463  for(const auto& d:getOutputDescription()) file<<d<<"\t";
464  file<<"\n";
465  }
466 
467 
468  inline void flushStochasticOutput(std::ofstream& file,
469  const tIndex& rawValuesNumber,
470  const std::valarray<tReal>& rawValues) {
471  std::valarray<tReal>& outputs=getOutputValues();
472  const tReal *iRawValues=&rawValues[0];
473  const tReal *iTimes=&mTimes[0];
474  tReal *iOutputs=&outputs[0];
475  for(tIndex i=0;i<mRegisteredTimesNumber;i++) {
476  (*iOutputs)=(*iRawValues);
477  if (rawValuesNumber>0) (*iOutputs)/=rawValuesNumber;
478  file<<(*iTimes)<<"\t"<<(*iOutputs)<<"\t"<<rawValuesNumber<<"\n";
479  iRawValues++;
480  iTimes++;
481  }
482 
483 
484  file<<"\n";
485  }
486 
487 
488 
489 
490 };
491 
492 #endif
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
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
static const tString & GetSoftName()
get soft name
Definition: CORE_Run.h:115
static const tString & GetVersion()
get the version
Definition: CORE_Run.h:129
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 & getBeamSize() const
get the beam size
Definition: SM_Beam.h:166
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 tReal & getTime() const
get the time
Definition: SM_LandauLifschitzSystem.h:214
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 L2 Convergence :
Definition: SM_StochasticDecreasingNoise.h:29
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_StochasticDecreasingNoise.h:116
void setSpinDirectionsAtRelaxationTime(const std::vector< tReal > &Hext)
set the deterministic dierctions of spins at relaxation time
Definition: SM_StochasticDecreasingNoise.h:164
virtual ~SM_StochasticDecreasingNoise(void)
destroy
Definition: SM_StochasticDecreasingNoise.h:83
tBoolean open(const SM_Beam &beam)
open the stochastic data
Definition: SM_StochasticDecreasingNoise.h:207
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_StochasticDecreasingNoise.h:411
virtual tString toString() const override
return the string representation of the class
Definition: SM_StochasticDecreasingNoise.h:433
static CORE_UniquePointer< SM_StochasticDecreasingNoise > New()
build a new instance of a stochastic data
Definition: SM_StochasticDecreasingNoise.h:127
const tIndex & getRegisteredTimesNumber() const
get the number of registered times
Definition: SM_StochasticDecreasingNoise.h:386
void setSpinDirectionsAtRelaxationTime(const std::array< tReal, SM_Constants::DIM > &Hext)
set the deterministic directions of spins at relaxation time
Definition: SM_StochasticDecreasingNoise.h:142
tBoolean close(const tIndex &s, const SM_System &system, const tBoolean &hasSucceeded)
close the stochastic data for the simulation s
Definition: SM_StochasticDecreasingNoise.h:366
tBoolean close(const SM_Beam &beam)
close stochastic data at the end of all simulations
Definition: SM_StochasticDecreasingNoise.h:392
tBoolean store(const SM_System &system)
store the stochastic data during the relation method of the system
Definition: SM_StochasticDecreasingNoise.h:291
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_StochasticDecreasingNoise.h:104
tBoolean open(const tIndex &s, const SM_System &system)
open the stochastic data for simulation s
Definition: SM_StochasticDecreasingNoise.h:274
SM_StochasticDecreasingNoise(void)
create
Definition: SM_StochasticDecreasingNoise.h:72
const std::valarray< tReal > & getTimes() const
get the time discretization
Definition: SM_StochasticDecreasingNoise.h:380
virtual void adimensionize(const SM_Material &material) override
adimensionize the output compoent with material characteristic
Definition: SM_StochasticDecreasingNoise.h:138
const int & getRootIndex() const
get the root index of the output component
Definition: SM_StochasticOutputComponent.h:222
std::valarray< tReal > & getPackedRawValues()
get the packed raw values
Definition: SM_StochasticOutputComponent.h:301
void setOutputDescription(const std::vector< tString > &v)
et the output description
Definition: SM_StochasticOutputComponent.h:323
const tString & getPrefix() const
return the prefix
Definition: SM_StochasticOutputComponent.h:190
const tInteger & getPackedSimulationsIndex() const
get packed simulations index
Definition: SM_StochasticOutputComponent.h:257
const std::vector< tString > & getOutputDescription() const
get the string representation of the output values
Definition: SM_StochasticOutputComponent.h:316
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 tBoolean & hasLogPerPackedSimulations() const
return true if the stochastic output has log per pack of simulations
Definition: SM_StochasticOutputComponent.h:274
const tInteger & getDrawnStepsNumber() const
get the drawn number to compute the stochastic output
Definition: SM_StochasticOutputComponent.h:243
const tInteger & getPackedSimulationsNumber() const
get packed simulations number
Definition: SM_StochasticOutputComponent.h:269
const std::valarray< tReal > & getOutputValues() const
get the output values of the stochastic output for reading
Definition: SM_StochasticOutputComponent.h:330
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
void computeMagneticMomentDirectionsMeanOverNetworks(const int &rootNetwork, std::array< tReal, SM_Constants::DIM > &meanS) const
compute the mean over the domain of magneticmoment directions
Definition: SM_System.h:477