C++ mpi module for stochmagnet_main Package
SM_StochasticBoltzmann.h
1 #ifndef SM_StochasticBoltzmann_H
2 #define SM_StochasticBoltzmann_H
3 
4 //inherited class header
5 #include "SM_StochasticOutput.h"
6 
7 //beam class header
8 #include "SM_Beam.h"
9 
10 //suystem class header
11 #include "SM_System.h"
12 
13 //runner header
14 #include "CORE_Run.h"
15 
16 //timer include
17 #include "CORE_Chrono.h"
18 
19 //system headers
20 #include "SM_MonteCarloSystem.h"
21 #include "SM_LandauLifschitzSystem.h"
22 
31 class SM_StochasticBoltzmann : public SM_StochasticOutput<SM_StochasticBoltzmann> {
32 
33  // ATTRIBUTES
34 
35 public:
36 
37 
38 
39 private:
40  //class types definition
43 
44 
45  //easy axis
46  std::array<tReal,SM_Constants::DIM> mU;
47 
48  //last step index registered
49  tIndex mStepIndex;
50 
51  //number of subdivisions of the support of the probability
52  tInteger mN;
53 
54  //amplitude of one subdivision
55  tReal mDTheta;
56 
57  //Effective temperature
58  tReal mTeff;
59 
60  //boltzmann files
61  tString mBoltzmannFileName;
62  tString mSBoltzmannFileName;
63 
64 public:
65  // METHODS
66 
67  // CONSTRUCTORS
68 
72  mN=100;
73  mDTheta=M_PI/mN;
74  mU={0,0,1};
76  "<theta_i>",
77  "<number of angles of S with easy axes in [theta_i,theta_i+1[>",
78  "<total number of computed angles>",
79  "<normalized propability>",
80  "<theorical value>"
81  });
82  //no output values for cycle
83  std::valarray<tReal>& outputs=getOutputValues();
84  outputs.resize(0);
85  }
86 
87 
88 
89  // DESTRUCTORS
90 public:
91 
94  virtual ~SM_StochasticBoltzmann(void) {
95  }
96 
97 
98 
99 public:
100  //MEMORY
101 
115  virtual tMemSize getMemorySize() const {
116  return sizeof(*this)+getContentsMemorySize();
117  }
118 
127  virtual tMemSize getContentsMemorySize() const {
128  tMemSize mem=SuperClass::getContentsMemorySize();
129  mem+=mU.size()*sizeof(tIndex);
130  mem+=sizeof(tChar)*mBoltzmannFileName.length();
131  mem+=sizeof(tChar)*mSBoltzmannFileName.length();
132  return mem;
133  }
134 
138  inline static CORE_UniquePointer<Self> New() {
139  return CORE_UniquePointer<Self>(new Self(),
141  }
145  inline static CORE_UniquePointer<Self> New(const Self& c) {
146  CORE_UniquePointer<Self> p=New();
147  p->copy(c);
148  return p;
149  }
150 
151  //SET & GET Methods
152  //=================
153 
157  virtual void adimensionize(const SM_Material& material) override {
158 
159  }
163  virtual void copy(const SM_StochasticOutputComponent& c) override {
164  SuperClass::copy(c);
165  const Self *cc=dynamic_cast<const Self*>(&c);
166  if (cc!=null) {
168  setAxis(cc->getAxis());
169  }
170  }
171 
172 
176  inline void setSegmentsNumber(const tInteger& n) {
177  mN=n;
178  }
182  inline const tInteger& getSegmentsNumber() const {
183  return mN;
184  }
185 
189  inline void setAxis(const std::array<tReal,SM_Constants::DIM>& axis) {
190  mU=axis;
191  }
194  inline void setAxis(const std::vector<tReal>& axis) {
195  if (axis.size()<SM_Constants::DIM) {
196  //mU=0;
197  memset(mU.data(),0,sizeof(tReal)*SM_Constants::DIM);
198  //mU=axis
199  tReal *iU=mU.data();
200  for(const auto& Uk:axis) {
201  (*iU)=Uk;
202  iU++;
203  }
204  } else {
205  std::vector<tReal>::const_iterator iAxis=axis.begin();
206  for(auto& Uk:mU) {
207  Uk=(*iAxis);
208  iAxis++;
209  }
210  }
211  }
215  inline const std::array<tReal,SM_Constants::DIM>& getAxis() const {
216  return mU;
217  }
218 
219 
220 
221 
222  //computed methods
223  //================
224 
229  inline void printHeader(std::ofstream& file,const SM_Beam& beam) {
230 
231  //header is the data
232 
233  file<<"# Boltzmann normalized probability for 1 spin with "<<getPackedSimulationsIndex()<<"/"<<getPackedSimulationsNumber()<<" packed simulations of size "<<beam.getBeamSize()<<" simulations generated by soft:"<<CORE_Run::GetSoftName()<<" Version:"<<CORE_Run::GetVersion()<<"\n";
234 
235  //print beam output
236  tString str=beam.toString();
237  functions_string::replaceAll("\n","\n#",str);
238  file<<"#"<<str<<"\n";
239 
240  //print strochastic output
241  str=this->toString();
242  functions_string::replaceAll("\n","\n#",str);
243  file<<"#"<<str<<"\n";
244 
245  //Teff=Ku/(T.k_B)
247  file<<"#Teff:"<<mTeff<<"\n";
248  file<<"#";
249  for(const auto& d:getOutputDescription()) file<<d<<"\t";
250  file<<"\n";
251 
252  }
253 
254 
255 
256 
257  //implemented templated methods
258  //=============================
259 
265  inline tBoolean open(const SM_Beam& beam) {
266 
267 
268  //number of raws values number is null
269  tIndex &nRaws=getPackedRawValuesNumber();
270  nRaws=0;
271 
272  //set the size of the distribution
273  std::valarray<tReal>& rawValues=getPackedRawValues();
274  rawValues.resize(mN);
275 
276  //initialize to 0
277  memset(&rawValues[0],0,rawValues.size()*sizeof(tReal));
278 
279  //set the amplitude of one segment
280  mDTheta=M_PI/mN;
281 
282  //open the Boltzmann Propability file for writting
283  mBoltzmannFileName=CORE_IO::GetAbsolutePath(this->getOutputPath()+"/"+this->getPrefix());
284  if (getPackedSimulationsNumber()>1) {
285  mBoltzmannFileName+="-"+std::to_string(getPackedSimulationsIndex())+"p";
286  }
287  //sysmmetric boltzamnn file
288  mSBoltzmannFileName=mBoltzmannFileName;
289 
290  mBoltzmannFileName+=".bd";
291  mSBoltzmannFileName+=".sbd";
292  if (getIndex()==0) {//only one core print the output file
294  std::ofstream file(mBoltzmannFileName.c_str(),
295  std::ios::out);
296  if (file) {
297  printHeader(file,beam);
298  file.close();
299  } else {
300  std::cout<<"Fatal Error : "<<mBoltzmannFileName<<" can not be opened \n";
301  return false;
302  }
303 
304  std::ofstream sfile(mSBoltzmannFileName.c_str(),
305  std::ios::out);
306  if (sfile) {
307  printHeader(sfile,beam);
308  sfile.close();
309  } else {
310  std::cout<<"Fatal Error : "<<mSBoltzmannFileName<<" can not be opened \n";
311  return false;
312  }
313  }
314  }
315 
316  //set step index to an unreachable step value (infinity)
317  mStepIndex=functions_numeric::getMax<tIndex>();
318 
319 
320 
321  return true;
322  }
323 
324 
325 
326 
331  inline tBoolean open(const tIndex& s,const SM_System& system) {
332  tBoolean ok=true;
333  mStepIndex=functions_numeric::getMax<tIndex>();
334  return ok;
335  }
336 
342  inline tBoolean store(const SM_System& system) {
343 
344  if ((getRootIndex()==-1) || (getIndex()==0)) {
345  if (system.getStepIndex()!=mStepIndex) {
346  mStepIndex=system.getStepIndex();
347 
348  //get magnetic moment St
349  const SM_RealField& S=system.getMagneticMomentDirections();
350 
351  //begin iterator on S
352  const tReal* iS=&S[0];
353  //end iterator on S
354  const tReal* eS=iS;eS+=S.getSize();
355 
356  //sSU=\sum_p <Sp,U> for particle
357  tReal sSU=0;
358  const tReal *iU=mU.data();
359  const tReal *eU=iU;eU+=mU.size();
360  while (iS!=eS) {
361  iU=mU.data();
362  while (iU!=eU) {
363  sSU+=(*iS)*(*iU);
364  iS++;
365  iU++;
366  }
367  }
368  //arcos(<S,U>) in [0,PI[
369  sSU=acos(sSU);
370 
371  //register the angle in the distribution array
372  tIndex i=sSU/mDTheta;
373  getPackedRawValues()[i]++;
374 
375  //number of raw data
377  }
378  }
379 
380  return true;
381  }
382 
388  inline tBoolean close(const tIndex& s,const SM_System& system,const tBoolean& hasSucceeded) {
389  tBoolean ok=true;
390  if (hasSucceeded) store(system);
391  return ok;
392  }
393 
399  inline tBoolean close(const SM_Beam& beam) {
400  if (getRootIndex()==-1) {
401  tIndex& rawValuesNumber=getPackedRawValuesNumber();
402  std::valarray<tReal>& rawValues=getPackedRawValues();
403  beam.getSystem().sumOverNetworks(0,rawValuesNumber,rawValues.size(),&rawValues[0]);
404  }
405 
406  if (getIndex()==0) {
408  std::ofstream file(mBoltzmannFileName.c_str(),std::ios::app);
409  if (file) {
410  //std::cout<<"print Disribution "<<getIndex()<<"\n";
411  printDistribution(file,
414  file.close();
415  }
416 
417  std::ofstream sFile(mSBoltzmannFileName.c_str(),std::ios::app);
418  if (sFile) {
419  printSymmetricDistribution(sFile,
422  sFile.close();
423  }
424  }
425  }
426  return true;
427 
428  }
429 
436  virtual void closePackedSimulations(const SM_Beam& beam,
437  const tIndex& rawValuesNumber,
438  const std::valarray<tReal>& rawValues) override {
439 
440  if (getIndex()==0) {
441  std::ofstream file(CORE_IO::GetAbsolutePath(this->getOutputPath()+"/"+this->getPrefix()+".bd").c_str(),
442  std::ios::out);
443  if (file) {
444  file<<"#Closed Packed Simulations\n";
445  printHeader(file,beam);
446  printDistribution(file,
447  rawValuesNumber,
448  rawValues);
449  file.close();
450  }
451 
452 
453  std::ofstream sFile(CORE_IO::GetAbsolutePath(this->getOutputPath()+"/"+this->getPrefix()+".sbd").c_str(),
454  std::ios::out);
455  if (sFile) {
456  sFile<<"#Closed Symmetric Packed Simulations\n";
457  printHeader(sFile,beam);
458  printSymmetricDistribution(sFile,
459  rawValuesNumber,
460  rawValues);
461  sFile.close();
462  }
463  }
464  }
465 
466 
467 
477  inline static tBoolean LoadDistributionFile(const tString& fn,
478  std::vector<tReal>& intervals,
479  std::vector<tIndex>& intervalSamplesNumber,
480  tIndex& samplesNumber,
481  std::vector<tReal>& probability,
482  std::vector<tReal>& boltzmannProbability) {
483  std::ifstream f(fn.c_str(),std::ios::in);
484  if (f) {
485  std::string line;
486 
487  intervals.clear();
488  intervalSamplesNumber.clear();
489  probability.clear();
490  boltzmannProbability.clear();
491 
492  samplesNumber=0;
493  tReal x;
494  tIndex n,samplesNumber;
495  tReal Pk;
496  tReal thPk;
497  while ((!f.eof()) && (std::getline(f,line))) {
498 
499  //ignore empty lines & comment lines
500  if ((line.length()==0) || (line[0]=='#')) continue;
501 
502  //parser of line
503  std::stringstream sstr;
504 
505  //read the line
506  sstr<<line;
507 
508 
509  sstr>>x;//theta_i
510  sstr>>n;//number of samples n [theta_i,theta_{i+1})
511  sstr>>samplesNumber;//total number of samples
512  sstr>>Pk;//normalized probability
513  sstr>>thPk;//boltzmann probability
514 
515  //get the line values
516  //std::cout<<"line:"<<line<<" of "<< fn<<" -->"<<intervals.size()<<" theta="<<x<<" nSamples="<<n<<" nTotalSamplesNumber="<<samplesNumber<<" Pk:"<<Pk<<" \n";
517  //register the values
518  intervals.push_back(x);
519  intervalSamplesNumber.push_back(n);
520  probability.push_back(Pk);
521  boltzmannProbability.push_back(thPk);
522 
523  }
524  f.close();
525 
526  return true;
527  }
528  return false;
529  }
533  virtual tString toString() const override {
534  std::stringstream ret;
535  ret<<SuperClass::toString()<<"\n";
536  ret<<"\t Number of subdivisions :"<<mN<<"\n";
537  ret<<"\t Easy axis :"<<functions_array::toString(mU)<<"\n";
538  return ret.str();
539  }
540 
541 private:
542 
547  tReal computeBoltzmannDistribution(const tReal& theta) const {
548  tReal s=sin(theta);
549  tReal ret=s;
550  ret*=s;
551  ret*=mTeff;
552  ret*=-1.0;
553  ret=exp(ret);
554  ret*=s;
555  return ret;
556  }
563  tReal computeBoltzmannDistributionMaximum() const {
564  tReal Pmax=exp(-mTeff);
565  if (mTeff>=0.5) {
566  tReal Pmax2=sqrt(1./(2.*mTeff*exp(1.)));
567  Pmax=(Pmax2>Pmax)?Pmax2:Pmax;
568  }
569  return Pmax;
570  }
571 
579  inline void printDistribution(std::ofstream& file,
580  const tInteger& rawDataSize,
581  const std::valarray<tReal>& rawData) const {
582 
583 
584  tReal PBmax=computeBoltzmannDistributionMaximum();
585  //get the max value
586  tReal maxPk=rawData[0];
587  for(const auto& Pk:rawData) {
588  maxPk=(maxPk<Pk)?Pk:maxPk;
589  }
590 
591  if (maxPk>0) {
592  tReal theta=0;
593  for(const auto& Pk:rawData) {
594  //file<<"#theta_i N_i N=sim N_i p=N_i/max(N_i)";
595  file<<theta<<"\t"<<Pk<<"\t"<<rawDataSize<<"\t"<< (Pk/maxPk)<<"\t"<<(computeBoltzmannDistribution(theta)/PBmax)<<"\n";
596  theta+=mDTheta;
597  }
598  } else {
599  tReal theta=0;
600  for(const auto& Pk:rawData) {
601  //file<<"#theta_i N_i N=sim N_i p=N_i/max(N_i)";
602  file<<theta<<"\t"<<Pk<<"\t"<<rawDataSize<<"\t"<< Pk<<"\t"<<(computeBoltzmannDistribution(theta)/PBmax)<<"\n";
603  theta+=mDTheta;
604  }
605  }
606 
607  }
615  inline void printSymmetricDistribution(std::ofstream& file,
616  const tInteger& rawDataSize,
617  const std::valarray<tReal>& rawData) const {
618 
619  tInteger i,n=rawData.size();
620 
621  //compute the max
622 
623  const tReal *iRawData=&rawData[0];
624  const tReal *eRawData=iRawData;eRawData+=rawData.size();
625 
626  const tReal *iEnd=eRawData;iEnd--;
627 
628  n/=2;
629  i=0;
630  tReal nMaxSamples=0,nSamples;
631  while (i<n) {
632 
633  nSamples=(*iRawData)+(*iEnd);
634  nMaxSamples=(nMaxSamples<nSamples)?nSamples:nMaxSamples;
635 
636  //next i
637  i++;
638  iRawData++;
639  iEnd--;
640  }
641  tReal PBmax=computeBoltzmannDistributionMaximum();
642 
643  if (nMaxSamples>0) {
644  tReal theta=0;
645 
646  iRawData=&rawData[0];
647  iEnd=eRawData;iEnd--;
648  while (iRawData!=eRawData) {
649  //file<<"#theta_i N_i N=sim N_i p=N_i/max(N_i)";
650  if (iRawData!=iEnd) nSamples=(*iRawData)+(*iEnd);
651  else nSamples=(*iRawData);
652 
653  file<<theta<<"\t"<<nSamples<<"\t"<<rawDataSize<<"\t"<< (nSamples/nMaxSamples)<<"\t"<<(computeBoltzmannDistribution(theta)/PBmax)<<"\n";
654 
655  theta+=mDTheta;
656  iRawData++;
657  iEnd--;
658  }
659  } else {
660  tReal theta=0;
661  for(const auto& Pk:rawData) {
662  //file<<"#theta_i N_i N=sim N_i p=N_i/max(N_i)";
663  file<<theta<<"\t"<<Pk<<"\t"<<rawDataSize<<"\t"<< Pk<<"\t"<<(computeBoltzmannDistribution(theta)/PBmax)<<"\n";
664  theta+=mDTheta;
665  }
666  }
667 
668  }
669 
670 
671 
672 };
673 
674 #endif
tIndex getSize() const
return the number values of the container
Definition: CORE_Field.h:161
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
static constexpr tDimension DIM
space dimension
Definition: SM_Constants.h:80
static constexpr tReal K_B
Boltzmann constants : .
Definition: SM_Constants.h:44
This class describes a materials defined by state attributes:
Definition: SM_Material.h:61
const tReal & getAnisotropyEnergyFactor() const
get the anisotropy energy
Definition: SM_Material.h:274
This class stores stochastic Boltzmann distribution for 1 spin.
Definition: SM_StochasticBoltzmann.h:31
virtual ~SM_StochasticBoltzmann(void)
destroy
Definition: SM_StochasticBoltzmann.h:94
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_StochasticBoltzmann.h:115
virtual void closePackedSimulations(const SM_Beam &beam, const tIndex &rawValuesNumber, const std::valarray< tReal > &rawValues) override
reduction the raws values for packed simlations
Definition: SM_StochasticBoltzmann.h:436
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_StochasticBoltzmann.h:127
virtual void copy(const SM_StochasticOutputComponent &c) override
copy the stochastic data
Definition: SM_StochasticBoltzmann.h:163
virtual tString toString() const override
return the string representation of the class
Definition: SM_StochasticBoltzmann.h:533
void setAxis(const std::array< tReal, SM_Constants::DIM > &axis)
set the easy axis
Definition: SM_StochasticBoltzmann.h:189
static tBoolean LoadDistributionFile(const tString &fn, std::vector< tReal > &intervals, std::vector< tIndex > &intervalSamplesNumber, tIndex &samplesNumber, std::vector< tReal > &probability, std::vector< tReal > &boltzmannProbability)
load distribution from file
Definition: SM_StochasticBoltzmann.h:477
tBoolean open(const SM_Beam &beam)
open the stochastic data
Definition: SM_StochasticBoltzmann.h:265
tBoolean store(const SM_System &system)
store the stochastic data during the evolution method of the system
Definition: SM_StochasticBoltzmann.h:342
tBoolean close(const SM_Beam &beam)
close stochastic data at the end of all simulations at each epsilon
Definition: SM_StochasticBoltzmann.h:399
virtual void adimensionize(const SM_Material &material) override
adimensionize the output compoent with material characteristic
Definition: SM_StochasticBoltzmann.h:157
static CORE_UniquePointer< Self > New(const Self &c)
build a new instance of a stochastic data
Definition: SM_StochasticBoltzmann.h:145
SM_StochasticBoltzmann(void)
create
Definition: SM_StochasticBoltzmann.h:71
void setSegmentsNumber(const tInteger &n)
set the number of subdivisions of the support of the distribution
Definition: SM_StochasticBoltzmann.h:176
tBoolean close(const tIndex &s, const SM_System &system, const tBoolean &hasSucceeded)
close the stochastic data for the end of the simulation s
Definition: SM_StochasticBoltzmann.h:388
static CORE_UniquePointer< Self > New()
build a new instance of a stochastic data
Definition: SM_StochasticBoltzmann.h:138
const tInteger & getSegmentsNumber() const
get the number of subdivisions of the support of the distribution
Definition: SM_StochasticBoltzmann.h:182
void setAxis(const std::vector< tReal > &axis)
set the easy axis
Definition: SM_StochasticBoltzmann.h:194
tBoolean open(const tIndex &s, const SM_System &system)
open the stochastic data for simulation s
Definition: SM_StochasticBoltzmann.h:331
void printHeader(std::ofstream &file, const SM_Beam &beam)
print header on file
Definition: SM_StochasticBoltzmann.h:229
const std::array< tReal, SM_Constants::DIM > & getAxis() const
get the easy axis
Definition: SM_StochasticBoltzmann.h:215
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
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 & 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
virtual void copy(const SM_StochasticOutputComponent &c)
copy
Definition: SM_StochasticOutput.h:94
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
const SM_Material & getMaterial() const
get the material of the network
Definition: SM_System.h:193
const tReal & getNoiseTemperature() const
get the noise temperature in Kelvin
Definition: SM_System.h:399
virtual void sumOverNetworks(const int &rootNetwork, tIndex &n, const tIndex &nValues, tReal *values) const
sum the values of all network
Definition: SM_System.h:457
const tIndex & getStepIndex() const
get the step index
Definition: SM_System.h:415