C++ mpi module for stochmagnet_main Package
SM_StochasticCurieTemperature.h
1 #ifndef SM_StochasticCurieTemperature_H
2 #define SM_StochasticCurieTemperature_H
3 
4 //inherited class header
5 #include "SM_StochasticOutput.h"
6 
7 //beam class header
8 #include "SM_LandauLifschitzBeam.h"
9 
10 //suystem class header
11 #include "SM_System.h"
12 
13 //runner header
14 #include "CORE_Run.h"
15 
16 //string utility header
17 #include "core_string.h"
18 #include "core_numeric.h"
19 
20 
21 
34 class SM_StochasticCurieTemperature : public SM_StochasticOutput<SM_StochasticCurieTemperature> {
35 
36  // ATTRIBUTES
37 
38 public:
39 
40 
41 
42 private:
43 
44  //class types definition
47 
48  tBoolean mIsLLBeam;
49 
50  tString mCTPackedFileName;
51  tString mCTFileName;
52 
53 
54  tIndex mStepIndex;
55  std::array<tReal,SM_Constants::DIM> mBarS;//mean of S over the particles
56  tReal mNormS;
57 
58  tUCInt mTemperatureIndex;
59  tUCInt mMeanSIndex;
60  tUCInt mNSamplesIndex;
61  tUCInt mNSimsIndex;
62 public:
63  // METHODS
64 
65  // CONSTRUCTORS
66 
70 
72  "<Temperature (K)>",
73  "<Mean of |S|>",
74  "<samples number>",
75  "<number of simulations>",
76  });
77 
78  mTemperatureIndex=0;
79  mMeanSIndex=1;
80  mNSamplesIndex=2;
81  mNSimsIndex=3;
82 
83  std::valarray<tReal>& outputs=getOutputValues();
84  outputs.resize(4);
85 
86  memset(&outputs[0],0,outputs.size()*sizeof(tReal));
87 
88  }
89 
90 
91 
92  // DESTRUCTORS
93 public:
94 
98  }
99 
100 
101 
102 public:
103  //MEMORY
104 
118  virtual tMemSize getMemorySize() const {
119  return sizeof(*this)+getContentsMemorySize();
120  }
121 
130  virtual tMemSize getContentsMemorySize() const {
131  tMemSize mem=SuperClass::getContentsMemorySize();
132  mem+=mCTFileName.length()*sizeof(tChar);
133  mem+=mCTPackedFileName.length()*sizeof(tChar);
134  mem+=mBarS.size()*sizeof(tReal);
135  return mem;
136  }
137 
141  inline static CORE_UniquePointer<Self> New() {
142  return CORE_UniquePointer<Self>(new Self(),
144  }
148  inline static CORE_UniquePointer<Self> New(const Self& c) {
149  CORE_UniquePointer<Self> p=New();
150  p->copy(c);
151  return p;
152  }
153 
154  //SET & GET Methods
155  //=================
159  virtual void copy(const SM_StochasticOutputComponent& c) override {
160  SuperClass::copy(c);
161  }
162 
163 
167  virtual void adimensionize(const SM_Material& material) override {
168  }
169 
174  inline void printHeader(std::ofstream& file,const SM_Beam& beam) {
175  //header is the data
176  //get the system
177  file<<"# Curie Temperature with "<<getPackedSimulationsIndex()<<"/"<<getPackedSimulationsNumber()<<" packed simulations of size "<<beam.getBeamSize()<<" simulations generated by soft:"<<CORE_Run::GetSoftName()<<" Version:"<<CORE_Run::GetVersion()<<"\n";
178 
179  //print beam output
180  tString str="beam:"+beam.toString();
181  core_string::replaceAll("\n","\n#",str);
182  file<<"#"<<str<<"\n";
183 
184  //print strochastic output
185  str="StochasticOutput:"+toString();
186  core_string::replaceAll("\n","\n#",str);
187  file<<"#"<<str<<"\n";
188 
189  //Tc=J*\epsilon * z / 3.kB
190  tReal Tc=beam.getSystem().getMaterial().getExchangeEnergyFactor();
193  Tc/=(3.0*SM_Constants::K_B);
194 
195  file<<"# T: temperature N : simulations number\n";
196  file<<"# for all simulation s in [0,N[:\n";
197  file<<"# \t for all step t in [1,T[, bar S_k(s,t)=frac{1}{P} sum_{p=0}^{p=P-1} S^s_k(t,p) \n"; file<<"# \t for all step t in [1,T[, bar S_k(s,t)=frac{1}{P} sum_{p=0}^{p=P-1} S^s_k(t,p) \n";
198  file<<"# \t E_s=frac{1}{T} sum_{t=0}^{t=T-1} |bar S_k(s,t)|\n";
199  file<<"# E=frac{1}{N} sum_{s=0}^{s=N-1} E_s\n";
200  file<<"\n";
201  file<<"# Curie Temperature: "<<Tc<<"\n";
202  file<<"#";
203  for(const auto& d:getOutputDescription()) file<<d<<"\t";
204  file<<"\n";
205  }
206 
207 
208 
209  //implemented templated methods
210  //=============================
211 
221  inline tBoolean open(const SM_Beam& beam) {
222  tString routineName="SM_StochasticCurieTemperature::open()";
223  CORE_Profiler::StartCallRoutine(routineName);
224 
225 
226  //init the class state
227  //----------------------
228 
229  //set step index to an unreachable step value (infinity)
230  mStepIndex=core_numeric::getInfinity<tIndex>();
231 
232  if (getIndex()==0) {
233  std::valarray<tReal>& outputs=getOutputValues();
234 
235  //temperature
236  outputs[mTemperatureIndex]=beam.getSystem().getNoiseTemperature();
237 
238  //open the core & global CT Files for writting
239  //--------------------------------------------
240  mCTFileName=CORE_IO::GetAbsolutePath(this->getOutputPath()+"/"+this->getPrefix());
241  mCTPackedFileName=mCTFileName;
242  if (getPackedSimulationsNumber()>1) {
243  mCTPackedFileName+="-"+std::to_string(getPackedSimulationsIndex())+"p";
244  }
245  mCTPackedFileName+=".ct";
246  mCTFileName+=".ct";
247 
249 
250  std::ofstream file(mCTPackedFileName.c_str(),std::ios::out);
251  if (file) {
252  printHeader(file,beam);
253 
254  } else {
255  std::cout<<"Fatal Error : "<<mCTPackedFileName<<" can not be opened \n";
256  return false;
257  }
258  }
259 
260 
261  //init the stochastic computings
262  //---------------------------------
263  //number of raws values number is null
264  tIndex &nRaws=getPackedRawValuesNumber();
265  nRaws=0;
266 
267  //set the size of the distribution
268  std::valarray<tReal>& rawValues=getPackedRawValues();
269 
270  rawValues.resize(2);//E(|S|),nSimulations per pack
271 
272  //initialize to 0
273  memset(&rawValues[0],0,rawValues.size()*sizeof(tReal));
274 
275 
276  }
277  CORE_Profiler::EndCallRoutine(routineName);
278  return true;
279  }
280 
281 
282 
283 
289  inline tBoolean open(const tIndex& s,const SM_System& system) {
290 
291  tString routineName="SM_StochasticCurieTemperature::open(s,system)";
292  CORE_Profiler::StartCallRoutine(routineName);
293 
294  tBoolean ok=true;
295  //initialize the step index
296  mStepIndex=core_numeric::getInfinity<tIndex>();
297 
298  CORE_Profiler::EndCallRoutine(routineName);
299 
300  return ok;
301  }
302 
308  inline tBoolean store(const SM_System& system) {
309  tString routineName="SM_StochasticCurieTemperature::store()";
310  CORE_Profiler::StartCallRoutine(routineName);
311  //compute the esperance at time t
312  //std::cout<<"SM_StochasticCurieTemperature::store() Index:"<<getIndex()<<" rootIndex:"<<getRootIndex()<<"\n";
313  if ((getIndex()==0)||(getRootIndex()==-1)) computeEsperanceContribution(system.getStepIndex(),system);
314  CORE_Profiler::EndCallRoutine(routineName);
315  return true;
316  }
317 
318 
319 
329  inline tBoolean close(const tIndex& s,const SM_System& system,const tBoolean& hasSucceeded) {
330  tBoolean ok=true;
331  tString routineName="SM_StochasticCurieTemperature::close(s,system,hasSucceeded)";
332  CORE_Profiler::StartCallRoutine(routineName);
333 
334  if ((getIndex()==0)||(getRootIndex()==-1)){
335  if (hasSucceeded) {
336  //compute the esperance at last time
337  computeEsperanceContribution(system.getStepIndex(),system);
338  }
339  }
340  CORE_Profiler::EndCallRoutine(routineName);
341  return ok;
342  }
343 
351  inline tBoolean close(const SM_Beam& beam) {
352  tString routineName="SM_StochasticCurieTemperature::close(beam)";
353  CORE_Profiler::StartCallRoutine(routineName);
354 
355 
356  if (getIndex()==0) {
357  //number of simulation of the beam
358  getPackedRawValues()[1]=beam.getBeamSize();
359 
361  //\f$ E(\varepsilon)= \displaystyle \sqrt { \sum_{k=0}^{k=d-1} E_k(\bar mu_k(\varepsilon,T))^2
362  std::ofstream file(mCTPackedFileName.c_str(),std::ios::app);
363  //file<<"# raw values number:"<<getPackedRawValuesNumber()<<"\n";
364  //file<<"# raw values:";
365  //for(const auto& v:getPackedRawValues()) file<<"\t"<<v;
366  //file<<"\n";
367  flushStochasticOutput(file,getPackedRawValuesNumber(),getPackedRawValues());
368  file.close();
369  }
370  }
371 
372  CORE_Profiler::EndCallRoutine(routineName);
373  return true;
374 
375  }
376 
377 
387  virtual void closePackedSimulations(const SM_Beam& beam,
388  const tIndex& rawValuesNumber,
389  const std::valarray<tReal>& rawValues) override {
390  tString routineName="SM_StochasticCurieTemperature::closePackedSimulations(beam,nRaws,raws)";
391  CORE_Profiler::StartCallRoutine(routineName);
392 
393  if (getIndex()==0) {
394  std::ofstream file(mCTFileName,std::ios::out);
395  if (file) {
396  //file<<"#Closed Packed Simulations\n";
397  //file<<"# raw values number:" <<rawValuesNumber<<"\n";
398  //file<<"# raw values:";
399  //for(const auto& v:rawValues) file<<"\t"<<v;
400  //file<<"\n";
401  printHeader(file,beam);
402  flushStochasticOutput(file,rawValuesNumber,rawValues);
403  file.close();
404  }
405  }
406  CORE_Profiler::EndCallRoutine(routineName);
407  }
408 
411  virtual tString toString() const override {
412  std::stringstream ret;
413  ret<<SuperClass::toString();
414  return ret.str();
415  }
416 
417 
418 private:
419 
428  inline void computeEsperanceContribution(const tIndex& stepIndex,const SM_System& system) {
429 
430 
431  if (mStepIndex!=stepIndex) {
432 
433  mStepIndex=stepIndex;
434 
435  //compute mean of S over the networks store in index 0
437 
438  if (getIndex()==0) {
439  //compute norm of the mean of S
440  mNormS=0;
441  for(const auto& Sk:mBarS) {
442  mNormS+=Sk*Sk;
443  }
444  mNormS=sqrt(mNormS);
445 
446  //esperance contribution
447  std::valarray<tReal> &Es=getPackedRawValues();
448  Es[0]+=mNormS;
449 
450 
451  //the s-th simulation has succeeded : mEs[k]+=\bar \mu_s[k]
453  //std::cout<<"stepIndex:"<<mStepIndex<<" nRawValues:"<<getPackedRawValuesNumber()<<"\n";
454  }
455  }
456 
457  }
458 
459  inline void flushStochasticOutput(std::ofstream& file,
460  const tIndex& rawValuesNumber,
461  const std::valarray<tReal>& rawValues) {
462  std::valarray<tReal>& outputs=getOutputValues();
463  outputs[mMeanSIndex]=rawValues[0]/rawValuesNumber;//mean of |S|
464  outputs[mNSamplesIndex]=rawValuesNumber;
465  outputs[mNSimsIndex]=rawValues[1];//sum of simulations of packed simulations
466  for(const auto v:outputs) {
467  file<<v<<"\t";
468  }
469  file<<"\n";
470  }
471 
472 
473 
474 
475 };
476 
477 #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
static void EndCallRoutine(const tString &routineName)
end calling routine
Definition: CORE_Profiler.h:91
static void StartCallRoutine(const tString &routineName)
start calling routine
Definition: CORE_Profiler.h:61
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 tReal K_B
Boltzmann constants : .
Definition: SM_Constants.h:44
const tUCInt & getCoordinationNumber() const
get the coodination number
Definition: SM_CrystalStructure.h:169
This class describes a materials defined by state attributes:
Definition: SM_Material.h:61
const tReal & getExchangeEnergyFactor() const
get the exchange energy
Definition: SM_Material.h:316
const tReal & getSpinWaveMFCorrection() const
get the spin wave MF correction
Definition: SM_Material.h:340
const SM_CrystalStructure & getCrystalStructure() const
get the crystal structure
Definition: SM_Material.h:198
This class stores stochastic Curie temperature esperance for a dimension D.
Definition: SM_StochasticCurieTemperature.h:34
virtual tString toString() const override
return the string representation of the class
Definition: SM_StochasticCurieTemperature.h:411
SM_StochasticCurieTemperature(void)
create
Definition: SM_StochasticCurieTemperature.h:69
tBoolean close(const tIndex &s, const SM_System &system, const tBoolean &hasSucceeded)
close the stochastic data for the simulation at index s
Definition: SM_StochasticCurieTemperature.h:329
static CORE_UniquePointer< Self > New()
build a new instance of a stochastic data
Definition: SM_StochasticCurieTemperature.h:141
tBoolean open(const SM_Beam &beam)
open the stochastic data at the begininf of simulations
Definition: SM_StochasticCurieTemperature.h:221
tBoolean store(const SM_System &system)
store the stochastic data during the evolution method of the system
Definition: SM_StochasticCurieTemperature.h:308
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_StochasticCurieTemperature.h:387
tBoolean open(const tIndex &s, const SM_System &system)
open the stochastic data for simulation at index s
Definition: SM_StochasticCurieTemperature.h:289
virtual void copy(const SM_StochasticOutputComponent &c) override
copy the stochastic data
Definition: SM_StochasticCurieTemperature.h:159
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_StochasticCurieTemperature.h:118
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_StochasticCurieTemperature.h:130
virtual ~SM_StochasticCurieTemperature(void)
destroy
Definition: SM_StochasticCurieTemperature.h:97
virtual void adimensionize(const SM_Material &material) override
adimensionize the output compoent with material characteristic
Definition: SM_StochasticCurieTemperature.h:167
tBoolean close(const SM_Beam &beam)
close stochastic data at the end of one packed simulations
Definition: SM_StochasticCurieTemperature.h:351
void printHeader(std::ofstream &file, const SM_Beam &beam)
print he file header
Definition: SM_StochasticCurieTemperature.h:174
static CORE_UniquePointer< Self > New(const Self &c)
build a new instance of a stochastic data
Definition: SM_StochasticCurieTemperature.h:148
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
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
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
const tIndex & getStepIndex() const
get the step index
Definition: SM_System.h:415