C++ mpi module for stochmagnet_main Package
SM_MonteCarloSystem.hpp
1 #ifndef SM_MonteCarloSystem_HPP
2 #define SM_MonteCarloSystem_HPP
3 
4 template<class StochOutputImplement>
5 inline tBoolean SM_MonteCarloSystem::stochasticRun(const tIndex& steppersNumber,
8 
9 
10  //number of drawn for stochastic output
11  const tInteger &nDrawnSteps=statistics.getDrawnStepsNumber();
12 
13  //no statictics
14  if (nDrawnSteps==0) return stochasticRun(steppersNumber,sfs);
15 
16  tString routineName="SMOMPI_MonteCarloSystem::stochasticRun<StochOutputI>(const tIndex&,SM_StochasticFunction&,StochOutputI&)";
18 
19 
20  //return value
21  tBoolean succeeds=true;
22 
23  //network of the system
24  const SM_Network& network=this->getNetwork();
25 
26  //material of the system
27  const SM_Material& material=this->getMaterial();
28 
29  //number of particles
30  const tIndex& nParticles=network.getParticlesNumber();
31  //number of halo particles
32  const tIndex& nHaloParticles=network.getHaloParticlesNumber();
33 
34  //iterator atomic spin direrctions
36  if (St.getElementsNumber()!=nParticles+nHaloParticles) {
37  throw CORE_Exception("stochmagnet/montecarlo",
38  "SMOMPI_MonteCarloSystem::stochasticRun<StochF>()",
39  "bad size of magnetic moments directions");
40  }
41  //energy scaling alpha= \frac{\bar E }{k_B.T}
42  const tReal& alpha=getEnergyScaling();
43 
44  //amplitude of the trial move
45  //sigma=\frac{2}{25}*\left( \frac{T*k_B}{\mu_B} \right))^{\frac{1}{5}}
46  const tReal& sigma=getNoiseRate();
47 
48  //get the step index
49  tIndex& t=getStepIndex();
50  tIndex iStep=0;
51  if (nDrawnSteps==1) {
52  while (iStep<steppersNumber) {
53 
54  MCStep(sfs,material,network,St,t,
55  alpha,sigma,mMovesNumber,mRejectsNumber);
56 
57  //store the statictics
58  statistics.store(*this);
59  //next step
61  t++;
62  iStep++;
63  }//end loop on stepper of equilibrium
64  }
65  else {
66  tInteger i;
67 
68  do {
69  i=0;
70  while (i<nDrawnSteps) {
71  MCStep(sfs,material,network,St,t,
72  alpha,sigma,mMovesNumber,mRejectsNumber);
73  //next step
75  i++;
76  t++;
77  iStep++;
78  }
79  //store the statistics
80  statistics.store(*this);
81  } while (iStep<steppersNumber);
82  }
83 
84  CORE_Profiler::EndCallRoutine(routineName);
85  return succeeds;
86 }
87 
88 template<class StochFunctionsImplement>
89 inline tBoolean SM_MonteCarloSystem::stochasticRunWithSCStochasticFunctions(const tIndex& steppersNumber,
91  tString routineName="SMOMPI_MonteCarloSystem::templatedStochasticRun<StochFI>(const tIndex&,Multi<StochF>&)";
93 
94  //return value
95  tBoolean succeeds=true;
96 
97  //network of the system
98  const SM_Network& network=this->getNetwork();
99 
100  //material of the system
101  const SM_Material& material=this->getMaterial();
102 
103  //number of particles
104  const tIndex& nParticles=network.getParticlesNumber();
105 
106  //number of halo particles
107  const tIndex& nHaloParticles=network.getHaloParticlesNumber();
108 
109  //iterator atomic spin direrctions
111  if (St.getElementsNumber()!=nParticles+nHaloParticles) {
112  throw CORE_Exception("stochmagnet/montecarlo",
113  "SMOMPI_MonteCarloSystem::templatedStochasticRun<StochF>()",
114  "bad size of magnetic moments directions");
115  }
116 
117 
118  //energy scaling alpha= \frac{\bar E }{k_B.T}
119  const tReal& alpha=getEnergyScaling();
120 
121  //amplitude of the trial move
122  //sigma=\frac{2}{25}*\left( \frac{T*k_B}{\mu_B} \right))^{\frac{1}{5}}
123  const tReal& sigma=getNoiseRate();
124 
125  //get the step index
126  tIndex& t=getStepIndex();
127  tIndex iStep=0;
128  while (iStep<steppersNumber) {
129 
130  MCStepWithSCStochasticFunctions<StochFunctionsImplement>(sfs,material,network,St,t,
131  alpha,sigma,mMovesNumber,mRejectsNumber);
132  //next step
134  t++;
135  iStep++;
136  }//end loop on stepper of equilibrium
137 
138  CORE_Profiler::EndCallRoutine(routineName);
139  return succeeds;
140 
141 }
142 
143 template<class StochFunctionsImplement,class StochOutputImplement>
144 inline tBoolean SM_MonteCarloSystem::stochasticRunWithSCStochasticFunctions(const tIndex& steppersNumber,
147 
148 
149 
150  //number of drawn for stochastic output
151  const tInteger &nDrawnSteps=statistics.getDrawnStepsNumber();
152 
153  //no statictics
154  if (nDrawnSteps==0) return stochasticRunWithSCStochasticFunctions<StochFunctionsImplement>(steppersNumber,sfs);
155 
156  tString routineName="SMOMPI_MonteCarloSystem::templatedStochasticRun<StochFI,StochOI>(const tIndex&,StochF&,StochOI&)";
157  CORE_Profiler::StartCallRoutine(routineName);
158 
159  //return value
160  tBoolean succeeds=true;
161 
162  //network of the system
163  const SM_Network& network=this->getNetwork();
164 
165  //material of the system
166  const SM_Material& material=this->getMaterial();
167 
168  //number of particles
169  const tIndex& nParticles=network.getParticlesNumber();
170 
171  //number of halo particles
172  const tIndex& nHaloParticles=network.getHaloParticlesNumber();
173 
174  //iterator atomic spin direrctions
176  if (St.getElementsNumber()!=nParticles+nHaloParticles) {
177  throw CORE_Exception("stochmagnet/montecarlo",
178  "SMOMPI_MonteCarloSystem::templatedStochasticRun<StochF,StochO>()",
179  "bad size of magnetic moments directions");
180  }
181  //energy scaling alpha= \frac{\bar E }{k_B.T}
182  const tReal& alpha=getEnergyScaling();
183 
184  //amplitude of the trial move
185  //sigma=\frac{2}{25}*\left( \frac{T*k_B}{\mu_B} \right))^{\frac{1}{5}}
186  const tReal& sigma=getNoiseRate();
187 
188  //get the step index
189  tIndex& t=getStepIndex();
190  tIndex iStep=0;
191  if (nDrawnSteps==1) {
192  while (iStep<steppersNumber) {
193 
194  MCStepWithSCStochasticFunctions<StochFunctionsImplement>(sfs,material,network,St,t,
195  alpha,sigma,mMovesNumber,mRejectsNumber);
196 
197  //store the statictics
198  statistics.store(*this);
199  //next step
201  t++;
202  iStep++;
203  }//end loop on stepper of equilibrium
204  }
205  else {
206  tInteger i;
207  do {
208  i=0;
209  while (i<nDrawnSteps) {
210  MCStepWithSCStochasticFunctions<StochFunctionsImplement>(sfs,material,network,St,t,
211  alpha,sigma,mMovesNumber,mRejectsNumber);
212 
213  //next step
215  i++;
216  t++;
217  iStep++;
218  }
219  //store the statistics
220  statistics.store(*this);
221  } while (iStep<steppersNumber);
222  }
223  CORE_Profiler::EndCallRoutine(routineName);
224  return succeeds;
225 }
226 
227 
228 template<class StochFunctionsImplement>
230  const SM_Material& material,const SM_Network& network,
231  SM_RealField& S,
232  const tIndex& stepIndex,const tReal& alpha,const tReal& sigma,
233  tIndex& nMoves,tIndex& nRejects) {
234 
235 
236  //number of particles, ignore halo particles
237  const tIndex& nParticles=network.getParticlesNumber();
238 
239  tReal u=0;//uniform random in [0,1[
240  tIndex i=0;//index of the spin
241  tReal *Si=null;//atomic spin direction of the spin i
242  const tReal *eSi=null;//end ietartor at coordinate of spin
243  std::array<tReal,SM_Constants::DIM> oldSi;//od values of Si before moving
244  tReal Eold;//old energy of the spin i before moving
245 
246  tReal P;//probability to accepet the move
247  //stochastic function for threadId 0
248  SM_StochasticFunctions<StochFunctionsImplement>& stochF=multiStochasticFunctions[0];
249 
250  for(tIndex r=0;r<nParticles;r++) {
251 
252  //number of moves
253  nMoves++;
254 
255  //randomize the spin
256  u=stochF.scUniformRandom();
257 
258  //index of the spin
259  i=nParticles*u;
260 
261  //direction of the spin i
262  Si=&S[0];
263  Si+=SM_Constants::DIM*i;
264  eSi=Si;eSi+=SM_Constants::DIM;
265 
266  //store the S value at i into oldS
267  memcpy(oldSi.data(),Si,SM_Constants::DIM*sizeof(tReal)); //dest,source,size
268 
269  //update state of operators
270  this->updateOperatorsState(stepIndex,network,material,S);
271 
272  //compute old spin energy
273  Eold=this->computeSpinEnergy(i,stepIndex,network,material,S);
274 
275  //trial move of the spin
276  this->randomTrialMoveWithSCStochasticFunctions(stochF,sigma,Si,eSi);
277 
278  //compute energy of new state of the spin i
279  P=this->computeSpinEnergy(i,stepIndex,network,material,S);
280 
281  //compute probability to keep the new change
282  //P=exp(-\alpha.\Delta E)
283  P-=Eold;
284  P*=alpha;
285  P*=-1.L;
286  P=exp(P);
287 
288  u=stochF.scUniformRandom();
289  //accept only if P>=u
290  if (u>P) {
291  //reject the new position
292  memcpy(Si,oldSi.data(),SM_Constants::DIM*sizeof(tReal)); //dest,source,size
293  nRejects++;
294  }
295 
296  }//end loop on particles drawn
297 }
298 
299 template<class StochFunctionsImplement>
301  const tReal& sigma,
302  tReal *bS,const tReal* eS) const{
303  tReal *S=bS;
304  tReal invNormS=0;
305 
306  tFlag moveType=(tFlag)(3.*stochasticFunctions.scUniformRandom());//type of move
307 
308  switch(moveType) {
309 
310  case 0://flip move
311  while (S!=eS) {
312  (*S)*=-1;
313  S++;
314  }
315  break;
316  case 1://gaussian move
317 
318  while (S!=eS) {
319  (*S)+=sigma*stochasticFunctions.scNormalRandom();
320  invNormS+=(*S)*(*S);
321  S++;
322  }
323  invNormS=1./sqrt(invNormS);
324  S=bS;
325  while (S!=eS) {
326  (*S)*=invNormS;
327  S++;
328  }
329  break;
330  case 2://random move
331 
332  while (S!=eS) {
333  (*S)=stochasticFunctions.scNormalRandom();
334  invNormS+=(*S)*(*S);
335  S++;
336  }
337  invNormS=1./sqrt(invNormS);
338  S=bS;
339  while (S!=eS) {
340  (*S)*=invNormS;
341  S++;
342  }
343  break;
344 
345  }
346 }
347 
348 #endif
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
tIndex getElementsNumber() const
return the number values of the container
Definition: CORE_Field.h:135
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 constexpr tDimension DIM
space dimension
Definition: SM_Constants.h:80
This class describes a materials defined by state attributes:
Definition: SM_Material.h:61
const tReal & getEnergyScaling() const
get the energy scaling
Definition: SM_MonteCarloSystem.h:140
virtual void updateStateForNextStep(SM_RealField &St)=0
update the state for next step
void MCStep(SM_MultiStochasticFunctionsInterface &multiStochasticFunctions, const SM_Material &material, const SM_Network &network, SM_RealField &S, const tIndex &stepIndex, const tReal &alpha, const tReal &sigma, tIndex &nMoves, tIndex &nRejects)
stepper step
Definition: SM_MonteCarloSystem.cpp:61
void MCStepWithSCStochasticFunctions(SM_MultiStochasticFunctions< StochFunctionsImplement > &multiStochasticFunctions, const SM_Material &material, const SM_Network &network, SM_RealField &S, const tIndex &stepIndex, const tReal &alpha, const tReal &sigma, tIndex &nMoves, tIndex &nRejects)
stepper step with templated stochastic function
Definition: SM_MonteCarloSystem.hpp:229
tBoolean stochasticRunWithSCStochasticFunctions(const tIndex &steppersNumber, SM_MultiStochasticFunctions< StochFunctionsImplement > &sfs)
compute the relaxation process by calling only virtual methods
Definition: SM_MonteCarloSystem.hpp:89
const tReal & getNoiseRate() const
get the noise rate constant
Definition: SM_MonteCarloSystem.h:134
tBoolean stochasticRun(const tIndex &steppersNumber, SM_MultiStochasticFunctionsInterface &sfs)
compute the steppers number of system for reaching the relaxed state
Definition: SM_MonteCarloSystem.cpp:3
void randomTrialMoveWithSCStochasticFunctions(SM_StochasticFunctions< StochFunctionsImplement > &stochasticFunctions, const tReal &sigma, tReal *bS, const tReal *eS) const
make a trial move
Definition: SM_MonteCarloSystem.hpp:300
This class describes a multi stochastic functions based on same random number generator which impleme...
Definition: SM_MultiStochasticFunctionsInterface.h:18
This class describes a multi stochastic functions based on same random number generator which impleme...
Definition: SM_MultiStochasticFunctions.h:26
This class is describes a network composed by.
Definition: SM_Network.h:66
const tInteger & getHaloParticlesNumber() const
return the halo particles number
Definition: SM_Network.h:355
const tInteger & getParticlesNumber() const
return the particles number
Definition: SM_Network.h:349
This class describes a stochastic functions with templated methods.
Definition: SM_StochasticFunctions.h:25
tReal scNormalRandom()
compute a normal random number by static casting method
Definition: SM_StochasticFunctions.h:114
tReal scUniformRandom()
compute a uniform random number in [0,1] bu static casting method
Definition: SM_StochasticFunctions.h:121
const tInteger & getDrawnStepsNumber() const
get the drawn number to compute the stochastic output
Definition: SM_StochasticOutputComponent.h:243
this class implements the virtual methods of its base class SM_StochasticOutputComponent with templat...
Definition: SM_StochasticOutput.h:24
tBoolean store(const SM_System &system)
store the stochastic data during the relation method of the system
Definition: SM_StochasticOutput.h:164
const SM_RealField & getMagneticMomentDirections() const
get the unit direction of spins at time
Definition: SM_System.h:267
void updateOperatorsState(const tIndex &timeIndex, const SM_Network &network, const SM_Material &material, const SM_RealField &S)
update the operators state before computeing magnetic field
Definition: SM_System.h:573
const SM_Network & getNetwork() const
get the network
Definition: SM_System.h:170
const SM_Material & getMaterial() const
get the material of the network
Definition: SM_System.h:193
tReal computeSpinEnergy(const tIndex &i, const SM_Network &network, const SM_Material &material, const SM_RealField &S) const
compute the energy of the spin of the system
Definition: SM_System.h:629
const tIndex & getStepIndex() const
get the step index
Definition: SM_System.h:415