C++ mpi module for stochmagnet_main Package
SM_MacroCellsDemagnetizedField.h
1 #ifndef SM_MacroCellsDemagnetizedField_H
2 #define SM_MacroCellsDemagnetizedField_H
3 
4 //base classes
5 #include "SM_Object.h"
6 
7 //macro cells network header
8 #include "SM_MacroCellsNetwork.h"
9 
10 //magnetization field defined on macro cells network header
11 #include "SM_MacroCellsMagnetizationField.h"
12 
13 //storage matrix header
14 #include "SM_PackedBlockSymmetricMatrix.h"
15 
16 
50 class SM_MacroCellsDemagnetizedField : public virtual SM_Object {
51 
52 
53 public:
56  static constexpr tFlag PACKED_STORAGE=1;
57 
60  static constexpr tFlag NO_STORAGE=0;
61 private :
62 
63  //type class
64  typedef SM_Object SuperClass;
66 
67  //single attributes
68  //-----------------
69 
70  //constants for computing H
71  tReal mLambdaHs;
72  tReal mLambdaH;
73 
74 
75  //type of satorage of the demagnetized matrix in {NO_STORAGE, PACKED_STORAGE}
76  tFlag mStorage;
77 
78 
79 
80  //demagnetized field Hdem at each macro cell
81  SM_RealField mHdem;
82 
83  //demagnetized matrix
84  CORE_UniquePointer<SM_PackedBlockMatrix<tReal,SM_Constants::DIM>> mMatrix;
85 
86 
87  //multiple associations
88  //---------------------
89 
90 
91 
92 protected:
93  // CONSTRUCTORS
97  mLambdaHs=0;
98  mLambdaH=0;
99 
100  mStorage=NO_STORAGE;
101  }
102 
103  // DESTRUCTORS
107  }
108 
109 
110 public :
111 
112  //Instance building
113  //=================
114 
115 
129  virtual tMemSize getMemorySize() const {
130  return sizeof(*this)+this->getContentsMemorySize();
131  }
132 
141  virtual tMemSize getContentsMemorySize() const {
142  tMemSize mem=SuperClass::getContentsMemorySize();
143  mem+=mHdem.getContentsMemorySize();
144  mem+=(mMatrix.get()==null)?0:mMatrix->getContentsMemorySize();
145  return mem;
146  }
147 
151  virtual CORE_UniquePointer<SM_MacroCellsDemagnetizedField> newInstance() const=0;
152 
153 public:
157  inline void setStorage(const tFlag& f) {
158  mStorage=f;
159  }
162  inline const tFlag& getStorage() const {
163  return mStorage;
164  }
165 
166 
169  inline const SM_RealField& getField() const {
170  return mHdem;
171  }
172 
175  inline SM_RealField& getField() {
176  return mHdem;
177  }
178 
179 
180 
181 public:
182 
185  inline tBoolean hasMatrix() const {
186  return (mMatrix.get()!=null);
187  }
190  inline void setMatrix(CORE_UniquePointer<SM_PackedBlockMatrix<tReal,SM_Constants::DIM>> matrix) {
191  mMatrix=std::move(matrix);
192  }
196  return *mMatrix.get();
197  }
201  return *mMatrix.get();
202  }
203 
204 
205 
206 public:
210  inline const tReal& getLambdaH() const {
211  return mLambdaH;
212  }
216  inline const tReal& getLambdaHs() const {
217  return mLambdaHs;
218  }
219 
220  //discretize the field
221  //======================
222 public:
233  virtual void discretize(const SM_Material& material,const SM_MacroCellsNetwork& network);
234 
235 
236  //demagnetized matrix build by macro cells
237  //=========================================
238 public:
243  virtual void computeMatrix(const SM_Material& material,
244  const SM_MacroCellsNetwork& network)=0;
245 
246 protected:
260  inline static void BuildPackedBlockMatrix(tReal lambdaH,const tReal& lambdaHs,const tReal& Wi,
261  tInteger i,tInteger j,
262  const tReal *Xi,const tReal* Xj,
263  tReal *vBij,
264  tReal& rij,std::array<tReal,SM_Constants::DIM>& U) {
265 
266  //zero tolerance
267  tReal eps=1.e-9;
268 
269  //\f$ r_{ij}=|X_j-X_i| f$
270  //\f$ U_{ij}=\frac{1}{r_ij}. (X_j-X)\f$
271  //\f$ H_i=\lambda_H \sum_{j=0, j \neq i }^{j=P-1} (3*<U_ij,S_j>*U_ij}-S_j))/r_{ij}^3 - \frac{\lambda_{H_s}}{N_i} S_i \f$
272  //\f$ H_i= \sum_{ij} M_{ij}. S_j \f$
273  // M_{ii}^{pq}= - \frac{\lambda_{H_s}}{N_i} . \delta_{pq}
274  // M_{ij]^{pq}=\frac{\lambdaH}{r_{ij}^3} (3.Up.U.q -\delta_{pq})
275 
276 
277  //I_{ij]=j.(j+1)/2+i
278  //next row : i+1
279  //next column C_{j+1}-C_{j]=(j+1).(j+2)/2 - j.(j+1)/2=0.5.(j+1).(j+2-j)=j+1
280  //next diagonal D_{j+1}-D_j=0.5.(j+1).(j+2)+j+1-0.5.j.(j+1)-j=0.5.(j+1).(j+2-j)+1=j+2
281 
282  //compute U,rij
283 
284  //rij^2
285  rij=0;
286  for(auto& Uk:U) {
287  //Uij
288  Uk=(*Xj)-(*Xi);
289  //rij
290  rij+=Uk*Uk;
291  //ietartors at next coorinates
292  Xj++;
293  Xi++;
294  }
295 
296  if (rij<eps) {
297  BuildPackedDiagonalBlockMatrix(Wi,lambdaHs,vBij);
298  } else {
299 
300  //lambdaH=LambdaH/rij^2
301  lambdaH/=rij;
302  //rij
303  rij=sqrt(rij);
304  //LambdaH=LambdaH/rij^3
305  lambdaH/=rij;
306 
307  //U is normalized
308  for(auto& Ui:U) Ui/=rij;
309 
310  const tReal *Ui=null,*Uj=U.data();
311  for(j=0;j<SM_Constants::DIM;j++) {//loop on column j in [0,dim[
312  //H_ij=3.Ui.Uj.lambdaH - lambdaH.delta_ij
313  Ui=U.data();
314  for(i=0;i<j;i++) {//loop on row i in[0,j[ of column j
315  (*vBij)=3;
316  (*vBij)*=(*Ui);
317  (*vBij)*=(*Uj);
318  (*vBij)*=lambdaH;
319  //iterator at next row i
320  vBij++;
321  Ui++;
322  }
323  //i=j diagonal element
324  (*vBij)=3;
325  (*vBij)*=(*Ui);
326  (*vBij)*=(*Uj);
327  (*vBij)-=1.0L;
328  (*vBij)*=lambdaH;
329 
330 
331  //iterator at next colum j
332  vBij++;
333  Uj++;
334  }//end packed values
335  }//end null rij condition
336  }
337 
338 
350  inline static void BuildPackedSupBlockMatrix(tReal lambdaH,
351  tInteger i,tInteger j,
352  const tReal *Xi,const tReal* Xj,
353  tReal *vBij,
354  tReal& rij,std::array<tReal,SM_Constants::DIM>& U) {
355  //\f$ r_{ij}=|X_j-X_i| f$
356  //\f$ U_{ij}=\frac{1}{r_ij}. (X_j-X)\f$
357  //\f$ H_i=\lambda_H \sum_{j=0, j \neq i }^{j=P-1} (3*<U_ij,S_j>*U_ij}-S_j))/r_{ij}^3 - \frac{\lambda_{H_s}}{N_i} S_i \f$
358  //\f$ H_i= \sum_{ij} M_{ij}. S_j \f$
359  // M_{ii}^{pq}= - \frac{\lambda_{H_s}}{N_i} . \delta_{pq}
360  // M_{ij]^{pq}=\frac{\lambdaH}{r_{ij}^3} (3.Up.U.q -\delta_{pq})
361 
362 
363  //I_{ij]=j.(j+1)/2+i
364  //next row : i+1
365  //next column C_{j+1}-C_{j]=(j+1).(j+2)/2 - j.(j+1)/2=0.5.(j+1).(j+2-j)=j+1
366  //next diagonal D_{j+1}-D_j=0.5.(j+1).(j+2)+j+1-0.5.j.(j+1)-j=0.5.(j+1).(j+2-j)+1=j+2
367 
368  //compute U,rij
369 
370  //rij^2
371  rij=0;
372  for(auto& Uk:U) {
373  //Uij
374  Uk=(*Xj)-(*Xi);
375  //rij
376  rij+=Uk*Uk;
377  //iterators at next coorinates
378  Xj++;
379  Xi++;
380  }
381 
382  //lambdaH=LambdaH/rij^2
383  lambdaH/=rij;
384  //rij
385  rij=sqrt(rij);
386  //LambdaH=LambdaH/rij^3
387  lambdaH/=rij;
388 
389  //U is normalized
390  for(auto& Ui:U) Ui/=rij;
391 
392  const tReal *Ui=null,*Uj=U.data();
393  for(j=0;j<SM_Constants::DIM;j++) {//loop on column j in [0,dim[
394  //H_ij=3.Ui.Uj.lambdaH - lambdaH.delta_ij
395  Ui=U.data();
396  for(i=0;i<j;i++) {//loop on row i in[0,j[ of column j
397  (*vBij)=3;
398  (*vBij)*=(*Ui);
399  (*vBij)*=(*Uj);
400  (*vBij)*=lambdaH;
401 
402 #ifdef DEBUG
403  if (std::isnan(*vBij)) {
404  throw CORE_Exception("stochmagnet/operators/fields",
405  "SM_MacroCellsDemagnetizedField::BuildPackedSupBlockMatrix(Xi="+functions_array::toString(3,Xi-3)+
406  " Xj="+functions_array::toString(3,Xj-3)+",LH="+std::to_string(lambdaH)+" rij="+std::to_string(rij)+" U:"+functions_array::toString(U),
407  "nan values");
408  }
409 #endif
410 
411  //iterator at next row i
412  vBij++;
413  Ui++;
414  }
415  //i=j diagonal element
416  (*vBij)=3;
417  (*vBij)*=(*Ui);
418  (*vBij)*=(*Uj);
419  (*vBij)-=1.0L;
420  (*vBij)*=lambdaH;
421 
422 
423  //iterator at next colum j
424  vBij++;
425  Uj++;
426  }
427  }
433  inline static void BuildDiagonalBlockMatrix(const tReal& V,
434  const tReal& lambdaHs,
435  tReal& diag) {
436 
437  diag=lambdaHs;
438  diag*=-1;
439  diag/=V;
440 #ifdef DEBUG
441  if (std::isnan(diag)) {
442  throw CORE_Exception("stochmagnet/operators/fields",
443  "SM_MacroCellsDemagnetizedField::BuildDiagonalBlockMatrix("+std::to_string(V)+","+std::to_string(lambdaHs)+")",
444  "nan values");
445  }
446 #endif
447 
448  }
454  inline static void BuildPackedDiagonalBlockMatrix(const tReal& V,
455  const tReal& lambdaHs,
456  tReal *iDiag) {
457 
458  memset(iDiag,0,sizeof(tReal)*SM_PackedBlockMatrix<tReal,SM_Constants::DIM>::BLOCK_SIZE);
459  BuildDiagonalBlockMatrix(V,lambdaHs,(*iDiag));
460  //std::cout<<"diag:"<<(*iDiag)<<"\n";
461  const tReal &d=(*iDiag);
462  for(tDimension j=2;j<=SM_Constants::DIM;j++) {//loop on column j in [0,dim[
463  iDiag+=j;
464  (*iDiag)=d;
465  }
466 
467 
468  }
469 
470 
471 
472 protected:
473 
484  void computeBlocksSymmetricMatrix(const tReal& lambdaH,const tReal& lambdaHs,
485  const tReal* vMCsVolume,
486  const tReal *vMCsX,
487  const tInteger& start,const tInteger& end,
488  tReal *supBlocksValues,
489  tReal *diagBlocksValues) const;
490 
501  void computeBlocksGeneralMatrix(const tReal& lambdaH,const tReal& lambdaHs,
502  const tReal* vMCsVolumes,
503  const tInteger& nX,const tReal *vX,
504  const tInteger& nP,const tReal *vP,
505  tReal *supBlocksValues) const;
506 
507 
508 public:
513  virtual void computeField(const SM_MacroCellsNetwork& network,
515 
516 
517 protected:
533  void computeFieldSlice(const tReal& lambdaH,const tReal& lambdaHs,
534  const tReal *P,const tReal *eP,
535  const tReal* M_P,
536  const tInteger& nMacroCells,
537  const tReal *X,
538  const tReal *M_X,
539  const tReal* iMCsVolume,
540  tReal *H) const;
541 private:
557  void computeFieldSliceInSeparatedNetworkStorage(const tReal& lambdaH,const tReal& lambdaHs,
558  const tReal *P,const tReal *eP,
559  const tReal* M_P,
560  const tInteger& nMacroCells,
561  const tReal *X,
562  const tReal *M_X,
563  const tReal* iMCsVolume,
564  tReal *H) const;
565 
566 public:
579  void computeSeparatedFieldSlice(const tReal& lambdaH,const tReal& lambdaHs,
580  const tReal *P,const tReal *eP,
581  const tReal* M_P,
582  const tInteger& nMacroCells,
583  const tReal *X,
584  tReal *H) const;
585 
586 
587  // String representation
588  // =======================
589 public:
590 
591 
594  virtual tString toString() const override {
595  std::stringstream ret;
596  ret<<SuperClass::toString()<<"\n";
597  ret<<"\t storage of demagnetized field: "<<((int) mStorage)<<"\n";
598  ret<<"\t Hdem:\n";
599  tIndex s=mHdem.getElementsNumber()/10;
600  s=(s==0)?1:s;
601  for(tIndex i=0;i<mHdem.getElementsNumber();i+=s) {
602  ret<<"\t\t ["<<i<<"]="<<functions_array::toString(SM_Constants::DIM,mHdem(i))<<"\n";
603  }
604  return ret.str();
605  }
606 
607 public:
608 
609 
610 
611 };
612 
613 
614 #endif
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: CORE_Field.h:102
tIndex getElementsNumber() const
return the number values of the container
Definition: CORE_Field.h:135
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
virtual tString toString() const
return the string representation of the object node
Definition: CORE_Object.h:333
static constexpr tDimension DIM
space dimension
Definition: SM_Constants.h:80
This class is describes a demagnetized operator.
Definition: SM_MacroCellsDemagnetizedField.h:50
SM_PackedBlockMatrix< tReal, SM_Constants::DIM > & getMatrix()
get matrix
Definition: SM_MacroCellsDemagnetizedField.h:195
static void BuildPackedSupBlockMatrix(tReal lambdaH, tInteger i, tInteger j, const tReal *Xi, const tReal *Xj, tReal *vBij, tReal &rij, std::array< tReal, SM_Constants::DIM > &U)
build the symmetric block element of the superior matrix in a packed storage
Definition: SM_MacroCellsDemagnetizedField.h:350
void computeBlocksGeneralMatrix(const tReal &lambdaH, const tReal &lambdaHs, const tReal *vMCsVolumes, const tInteger &nX, const tReal *vX, const tInteger &nP, const tReal *vP, tReal *supBlocksValues) const
compute the column blocks matrix (i,j) j in [start,end] and j in [0,i]
Definition: SM_MacroCellsDemagnetizedField.cpp:397
const tReal & getLambdaHs() const
get self lambda Hs
Definition: SM_MacroCellsDemagnetizedField.h:216
const tReal & getLambdaH() const
get lambda H
Definition: SM_MacroCellsDemagnetizedField.h:210
void computeSeparatedFieldSlice(const tReal &lambdaH, const tReal &lambdaHs, const tReal *P, const tReal *eP, const tReal *M_P, const tInteger &nMacroCells, const tReal *X, tReal *H) const
compute Hdem[X]=F_{P,M}(X)
Definition: SM_MacroCellsDemagnetizedField.cpp:245
static void BuildPackedDiagonalBlockMatrix(const tReal &V, const tReal &lambdaHs, tReal *iDiag)
build the diagonal block element of the matrix
Definition: SM_MacroCellsDemagnetizedField.h:454
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_MacroCellsDemagnetizedField.h:141
void setStorage(const tFlag &f)
set storage type
Definition: SM_MacroCellsDemagnetizedField.h:157
static constexpr tFlag PACKED_STORAGE
the storage if in a packed matrix
Definition: SM_MacroCellsDemagnetizedField.h:56
void computeFieldSlice(const tReal &lambdaH, const tReal &lambdaHs, const tReal *P, const tReal *eP, const tReal *M_P, const tInteger &nMacroCells, const tReal *X, const tReal *M_X, const tReal *iMCsVolume, tReal *H) const
compute Hdem[X]=F_{P,M}(X)
Definition: SM_MacroCellsDemagnetizedField.cpp:44
static void BuildPackedBlockMatrix(tReal lambdaH, const tReal &lambdaHs, const tReal &Wi, tInteger i, tInteger j, const tReal *Xi, const tReal *Xj, tReal *vBij, tReal &rij, std::array< tReal, SM_Constants::DIM > &U)
build the symmetric block element of the superior matrix in a packed storage
Definition: SM_MacroCellsDemagnetizedField.h:260
const tFlag & getStorage() const
yet storage type in {NO_STORAGE,PACKED_STORAGE}
Definition: SM_MacroCellsDemagnetizedField.h:162
void setMatrix(CORE_UniquePointer< SM_PackedBlockMatrix< tReal, SM_Constants::DIM >> matrix)
set matrix
Definition: SM_MacroCellsDemagnetizedField.h:190
static void BuildDiagonalBlockMatrix(const tReal &V, const tReal &lambdaHs, tReal &diag)
build the diagonal block element of the matrix
Definition: SM_MacroCellsDemagnetizedField.h:433
virtual tString toString() const override
return string representaton of the operator
Definition: SM_MacroCellsDemagnetizedField.h:594
virtual void discretize(const SM_Material &material, const SM_MacroCellsNetwork &network)
discretize the field
Definition: SM_MacroCellsDemagnetizedField.cpp:12
virtual CORE_UniquePointer< SM_MacroCellsDemagnetizedField > newInstance() const =0
create a New instance of this
static constexpr tFlag NO_STORAGE
no storage
Definition: SM_MacroCellsDemagnetizedField.h:60
virtual void computeField(const SM_MacroCellsNetwork &network, const SM_MacroCellsMagnetizationField &M)=0
compute the demagnetized field on macro cells network
tBoolean hasMatrix() const
return true if the matrix exists
Definition: SM_MacroCellsDemagnetizedField.h:185
const SM_RealField & getField() const
get the demagnetized field on macro cells
Definition: SM_MacroCellsDemagnetizedField.h:169
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_MacroCellsDemagnetizedField.h:129
virtual ~SM_MacroCellsDemagnetizedField(void)
destroy
Definition: SM_MacroCellsDemagnetizedField.h:106
SM_MacroCellsDemagnetizedField(void)
create a network class
Definition: SM_MacroCellsDemagnetizedField.h:96
virtual void computeMatrix(const SM_Material &material, const SM_MacroCellsNetwork &network)=0
compute the matrix ot he demagnetized operator
SM_RealField & getField()
get the demagnetized field on macro cells
Definition: SM_MacroCellsDemagnetizedField.h:175
const SM_PackedBlockMatrix< tReal, SM_Constants::DIM > & getMatrix() const
get matrix
Definition: SM_MacroCellsDemagnetizedField.h:200
void computeBlocksSymmetricMatrix(const tReal &lambdaH, const tReal &lambdaHs, const tReal *vMCsVolume, const tReal *vMCsX, const tInteger &start, const tInteger &end, tReal *supBlocksValues, tReal *diagBlocksValues) const
compute the column blocks matrix (i,j) j in [start,end] and j in [0,i]
Definition: SM_MacroCellsDemagnetizedField.cpp:322
This class is describes a Magnetization field.
Definition: SM_MacroCellsMagnetizationField.h:23
This class is describes a macro cell network.
Definition: SM_MacroCellsNetwork.h:25
This class describes a materials defined by state attributes:
Definition: SM_Material.h:61
This class is a base class for Stoch Microm package.
Definition: SM_Object.h:36
This class described a matrix by packed block of size in a row storage.
Definition: SM_PackedBlockMatrix.h:18