C++ main module for stochmagnet Package  1.0
SM_DemagnetizedOperator.h
Go to the documentation of this file.
1 #ifndef SM_DemagnetizedOperator_H
2 #define SM_DemagnetizedOperator_H
3 
4 //base classes
5 #include "SM_TemplatedOperator.h"
6 
7 //network class
8 #include "SM_Network.h"
9 
10 
32 class SM_DemagnetizedOperator : public SM_TemplatedOperator<SM_DemagnetizedOperator> {
33 
34  //attributes
35 private :
36 
37 
38  //association
40 
41 protected:
42  // CONSTRUCTORS
46  setName("Demagnetized");
47  }
48 
49  // DESTRUCTORS
52  virtual ~SM_DemagnetizedOperator(void) {
53  }
54 
55 public :
56 
57  //Instance building
58  //=================
59 
60 
67  }
68 
72  virtual CORE_UniquePointer<SM_Operator> NewInstance() const override {
73  return New();
74  }
75 
76 
90  virtual tMemSize getMemorySize() const {
91  return sizeof(*this)+getContentsMemorySize();
92  }
93 
102  virtual tMemSize getContentsMemorySize() const {
104  mem+=mAreMagnetizedCells.getSize()*sizeof(tBoolean);
105  return mem;
106  }
107 public:
108 
112  virtual void discretize(const SM_System& system);
113 
117  inline void updateMagnetizedCells(const SM_RealField& Mu) {
118 
119  tDimension k,dim=Mu.getDimension();
121  auto Mu_jk=Mu.cbegin();
122  tReal n2Muj;
124  [&](auto& isM) {
125  n2Muj=0;
126  for (k=0;k<dim;k++) {
127  n2Muj+=(*Mu_jk)*(*Mu_jk);
128  Mu_jk++;
129  }
130  isM=(n2Muj>1.e-12);
131 
132  });
133  }
134 
135 
144  virtual void computeMagneticField(const tIndex& t,
145  const SM_Network& network,
146  const tReal *mu,
147  tReal *B) const override {
148  return computeTemplatedMagneticField(t,network,mu,B);
149  }
157  virtual void computeMagneticFieldAndEnergy(const tIndex& t,
158  const SM_Network& network,
159  const tReal *mu,
160  tReal *B,
161  tReal& E) const override{
162  return computeTemplatedMagneticFieldAndEnergy(t,network,mu,B,E);
163  }
172  inline void computeTemplatedMagneticField(const tIndex& t,
173  const SM_Network& network,
174  const tReal *mu,
175  tReal *B) const {
176  tReal E=0;
177  computeTemplatedMagneticFieldAndEnergy(t,network,mu,B,E);
178  }
189  const SM_Network& network,
190  const tReal *mu,
191  tReal *B,
192  tReal& E) const {
193 
194  //get the number of particles of the network
195  const tIndex& nParticles=network.getParticlesNumber();
196 
197  //get the dimension of the netwok
198  const tDimension& dim=network.getDimension();
199 
200  //strength of the netweok
201  const tReal& lambda=network.getLambda();
202  //coordinates of the network
203  const tReal *particles=network.getParticlesCoordinates().getValues();
204 
205  //iterator on Mu at point j
206  const tReal *Mu_jk,*Mu_j=null;
207  tReal lambda3=lambda*lambda*lambda;
208 
209  //iterators on dimension
210  tDimension k;
211 
212  //iterators on particles
213  tIndex i,j;
214 
215  //distance d
216  tReal d3PiPjL3,d2PiPj,dd;
217 
218  //iterator on particle i
219  const tReal *Pik,*Pi;
220  const tReal *Mu_ik,*Mu_i=mu;
221 
222  //iterators on particle j
223  const tReal *Pjk,*Pj=null;
224 
225  //<P^i-P^j,mu^j>
226  tReal sRijMuj;
227 
228  //initializer
229  tReal *Bik,*Bi=B;
230 
231  tReal Bdem_ik;
232 
233  //energy
234  E=0;
235 
236  //coordinates of the particle i
237  Pi=particles;
238 
239  //magnetized
240 
241 
242  for (i=0;i<nParticles;i++) {//loop on i particle Pi
243 
244 
245  //bdem_i=0
246  memset(Bi,0,dim*sizeof(tReal));
247 
248 
249 
250  //magnetic moment of the particle j
251  Mu_j=mu;
252  //coordinates of the particle j
253  Pj=particles;//first particle
254 
255  auto isMj=mAreMagnetizedCells.cbegin();
256  for (j=0;j<nParticles;j++) { //loop on j particle
257  if (j!=i) {
258  //test if the particule j is magnetized
259  if (*isMj) {
260 
261  //d2PiPj=|P_j-Pj|^2
262  d2PiPj=0;
263  //sXMu=<P^i-P^j,\mu^j>
264  sRijMuj=0;
265  //iterator
266  Pik=Pi;
267  Pjk=Pj;
268  Mu_jk=Mu_j;
269  for (k=0;k<dim;k++) {
270  //compute |Pi_Pj|^2
271  dd=(*Pik)-(*Pjk);
272  d2PiPj+=dd*dd;
273  //compute <Mu^j,Pi-Pj>
274  sRijMuj+=dd*(*Mu_jk);
275 
276  //next iterator
277  Pik++;
278  Pjk++;
279  Mu_jk++;
280  }
281 
282  //d3PiPj=|P_j-Pj|^3.\lambda^3
283  d3PiPjL3=(d2PiPj*sqrt(d2PiPj)*lambda3);
284 
285  Bik=Bi;
286  Mu_jk=Mu_j;
287  Pik=Pi;
288  Pjk=Pj;
289  Mu_ik=Mu_i;
290  for (k=0;k<dim;k++) {
291 
292  //Bd
293  Bdem_ik=-2*((*Mu_jk)-3.*(sRijMuj/d2PiPj)*((*Pik)-(*Pjk)) )/d3PiPjL3;
294  (*Bik)+=Bdem_ik;
295 
296  //energy
297  E-=0.5*Bdem_ik*(*Mu_ik);
298 
299  //next coordinate
300  Bik++;
301  Pik++;
302  Pjk++;
303  Mu_jk++;
304  Mu_ik++;
305  }
306  }//|mu_j|!=0
307  } //i!=j
308 
309 
310  //next iterator on j
311  Mu_j+=dim;//magnetic moment at next particle j
312  Pj+=dim;//coordinates of next particle j
313  isMj++;//next particle is magnetized
314  }//end loop on point j
315 
316 
317 
318  //coordinates of next particle i
319  Pi+=dim;
320  //mu at next particle i
321  Mu_i+=dim;
322  //Bd at next particle i
323  Bi+=dim;
324  }//end loop on particle i
325 
326 
327  }//end method
328 
329 
330 
337  virtual tReal computeEnergy(const tIndex& t,
338  const SM_Network& network,
339  const tReal *mu) const override{
340  return computeTemplatedEnergy(t,network,mu);
341  }
342 
351  const SM_Network& network,
352  const tReal *mu) const {
353 
354  //get the number of particles of the network
355  const tIndex& nParticles=network.getParticlesNumber();
356 
357  //get the dimension of the netwok
358  const tDimension& dim=network.getDimension();
359 
360  //strength of the network
361  const tReal& lambda=network.getLambda();
362  tReal iLambda3=1./(lambda*lambda*lambda);
363 
364  //coordinates of the network
365  const tReal *particles=network.getParticlesCoordinates().getValues();
366 
367 
368  //iterator on particles
369  tIndex i,j;
370 
371  //iterator on dimension
372  tDimension d;
373 
374  //Mu at particle i
375  const tReal *Mui=mu,*Mui_d;
376 
377  //Mu at particle j
378  const tReal *Muj,*Muj_d;
379 
380  //iterator on particle i
381  const tReal *Pi_d,*Pi=particles;
382 
383  //iterators on particle j
384  const tReal *Pj_d,*Pj=null;
385 
386  //rij=P_j-P_i
387  tReal r[3];
388  tReal *rij;
389  //dij2=|P_j-P_i|^2
390  tReal dij2;
391  //dij3=|P_j-P_i|^3
392  tReal dij3;
393 
394  //sMu=<Mu_i,Mu_j>
395  //rMui=<rij,Mu_i>
396  //rMuj=<rij,Mu_j>
397  tReal sMu,rMui,rMuj;
398 
399  //energy
400  tReal E=0;
401 
402  for (i=0;i<nParticles;i++) {//loop on particles i
403 
404  Pj=particles;
405  Muj=mu;
406  for (j=0;j<nParticles;j++) {//loop on particles j \=i
407  if (j!=i) {
408  //rij=P_j-Pj
409  //dij=|rij|^2
410  rij=r;
411  dij2=0;
412  sMu=0;
413  rMui=0;
414  rMuj=0;
415  Pj_d=Pj;
416  Pi_d=Pi;
417  Mui_d=Mui;
418  Muj_d=Muj;
419  for (d=0;d<dim;d++) {
420  (*rij)=(*Pj_d)-(*Pi_d);
421  dij2+=(*rij)*(*rij);
422  sMu+= (*Mui_d)*(*Muj_d);
423  rMui+=(*Mui_d)*(*rij);
424  rMuj+=(*Muj_d)*(*rij);
425  Pj_d++;
426  Pi_d++;
427  rij++;
428  Mui_d++;
429  Muj_d++;
430  }
431 
432  dij3=dij2*sqrt(dij2);
433 
434  E+=iLambda3*(sMu-(3*rMui*rMuj/dij2))/dij3;
435  }//end condition i!=j
436 
437  //next j-particles
438  Muj+=dim;
439  Pj+=dim;
440 
441  }//end loop on particle j
442 
443 
444  //next i-particle
445  Mui+=dim;
446  Pi+=dim;
447 
448 
449  }//end loop on particle i
450 
451 
452  return E;
453  }//end method
454 };
455 
456 
457 #endif
tUCInt tDimension
Definition: CORE_StdPtrField.h:567
K getDimension() const
get the dimension of the field
Definition: CORE_Field.h:143
const T * getValues() const
get the values of the array for reading
Definition: CORE_Field.h:298
tIndex getElementsNumber() const
return the number values of the container
Definition: CORE_Field.h:118
constexpr auto cbegin() const
return begin iterator for reading
Definition: CORE_Field.h:199
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:94
auto begin()
return begin iterator for writing
Definition: CORE_PtrArray.h:309
auto end()
return end iterator for writing
Definition: CORE_PtrArray.h:315
void setSize(const tIndex &n)
set the number of values
Definition: CORE_PtrArray.h:142
tIndex getSize() const
return the size of the array for writing
Definition: CORE_PtrArray.h:151
this class describes a standart arithmetic array type implemented with a memory allocation with type ...
Definition: CORE_StdPtrArray.h:10
This class is describes a demagnetized operator.
Definition: SM_DemagnetizedOperator.h:32
virtual void computeMagneticFieldAndEnergy(const tIndex &t, const SM_Network &network, const tReal *mu, tReal *B, tReal &E) const override
compute the magnetic field at time step t by virtual method
Definition: SM_DemagnetizedOperator.h:157
void updateMagnetizedCells(const SM_RealField &Mu)
update the boolean array to indicates if mu at particule is magnetized
Definition: SM_DemagnetizedOperator.h:117
static CORE_UniquePointer< SM_DemagnetizedOperator > New()
build a new instance of the operator
Definition: SM_DemagnetizedOperator.h:64
CORE_BooleanArray mAreMagnetizedCells
Definition: SM_DemagnetizedOperator.h:39
void computeTemplatedMagneticField(const tIndex &t, const SM_Network &network, const tReal *mu, tReal *B) const
compute the magnetic field
Definition: SM_DemagnetizedOperator.h:172
virtual tReal computeEnergy(const tIndex &t, const SM_Network &network, const tReal *mu) const override
compute the energy at time step index t
Definition: SM_DemagnetizedOperator.h:337
SM_DemagnetizedOperator(void)
create a network class
Definition: SM_DemagnetizedOperator.h:45
virtual void computeMagneticField(const tIndex &t, const SM_Network &network, const tReal *mu, tReal *B) const override
compute the magnetic field at time step t by virtual method
Definition: SM_DemagnetizedOperator.h:144
tReal computeTemplatedEnergy(const tIndex &t, const SM_Network &network, const tReal *mu) const
compute the energy at time step index t
Definition: SM_DemagnetizedOperator.h:350
virtual CORE_UniquePointer< SM_Operator > NewInstance() const override
create a New instance of this @retrun an unique pointer to the instance
Definition: SM_DemagnetizedOperator.h:72
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_DemagnetizedOperator.h:90
virtual void discretize(const SM_System &system)
discretize the operator : do nothing by default
Definition: SM_DemagnetizedOperator.cpp:5
virtual ~SM_DemagnetizedOperator(void)
destroy
Definition: SM_DemagnetizedOperator.h:52
void computeTemplatedMagneticFieldAndEnergy(const tIndex &t, const SM_Network &network, const tReal *mu, tReal *B, tReal &E) const
compute the magnetic field and energy
Definition: SM_DemagnetizedOperator.h:188
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_DemagnetizedOperator.h:102
This class is describes a a network.
Definition: SM_Network.h:18
tDimension getDimension() const
return the dimension
Definition: SM_Network.h:133
const tReal & getLambda() const
get the lambda value
Definition: SM_Network.h:201
tInteger getParticlesNumber() const
return the particles number
Definition: SM_Network.h:146
const SM_RealField & getParticlesCoordinates() const
void get particles coordinates
Definition: SM_Network.h:181
void setName(const tString &name)
set the name
Definition: SM_Operator.h:95
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: SM_Operator.h:80
This class is a simulation of one trajectory class for Stoch Microm package.
Definition: SM_System.h:51
This class is describes an implmentation of operator with the class I.
Definition: SM_TemplatedOperator.h:18
typename std::unique_ptr< T, CORE_Object::Delete > CORE_UniquePointer
Definition: sp.h:8
#define tIndex
Definition: types.h:157
#define tMemSize
Definition: types.h:166
#define tBoolean
Definition: types.h:151
#define tReal
Definition: types.h:137