Feellgood
fmm_demag.h
Go to the documentation of this file.
1 #ifndef FMM_DEMAG_H
2 #define FMM_DEMAG_H
3 
10 #include "Components/FParticleType.hpp"
11 #include "Components/FTypedLeaf.hpp"
12 #include "Containers/FOctree.hpp"
13 #include "Core/FFmmAlgorithmThreadTsm.hpp"
14 #include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
15 #include "Kernels/Rotation/FRotationCell.hpp"
16 #include "Kernels/Rotation/FRotationKernel.hpp"
17 
18 #include "mesh.h"
19 
24 namespace scal_fmm
25  {
26 const int P = 9;
27 const int NbLevels = 6;
28 const int SizeSubLevels = 3;
30 typedef double FReal;
33 typedef FTypedRotationCell<FReal, P>
36 typedef FP2PParticleContainerIndexed<FReal>
39 typedef FTypedLeaf<FReal, ContainerClass>
42 typedef FOctree<FReal, CellClass, ContainerClass, LeafClass>
45 typedef FRotationKernel<FReal, CellClass, ContainerClass, P>
48 typedef FFmmAlgorithmThreadTsm<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass>
52 const double boxWidth = 2.01;
53 const FPoint<FReal> boxCenter(0., 0., 0.);
59 class fmm
60  {
61 public:
64  inline fmm(Mesh::mesh &msh ,
65  std::vector<Tetra::prm> & prmTet ,
66  std::vector<Facette::prm> & prmFac ,
67  const int ScalfmmNbThreads )
68  : prmTetra(prmTet), prmFacette(prmFac),
70  {
71  omp_set_num_threads(ScalfmmNbThreads);
72  norm = 2. / msh.l.maxCoeff();
73 
74  FSize idxPart = 0;
75  for (idxPart = 0; idxPart < (FSize)msh.magNode.size(); ++idxPart)
76  {
77  if (msh.magNode[idxPart])
78  {
79  Eigen::Vector3d pTarget = norm*(msh.getNode_p(idxPart) - msh.c);
80  tree.insert(FPoint<FReal>(pTarget.x(), pTarget.y(), pTarget.z()),
81  FParticleType::FParticleTypeTarget, idxPart);
82  }
83  }
84 
85  insertCharges<Tetra::Tet, Tetra::NPI>(msh.tet, msh.magTet, idxPart, msh.c);
86  insertCharges<Facette::Fac, Facette::NPI>(msh.fac, msh.magFac, idxPart, msh.c);
87 
88  srcDen.resize( msh.magFac.size()*Facette::NPI + msh.magTet.size()*Tetra::NPI );
89  corr.resize(msh.magNode.size());
90  }
91 
95  void calc_demag(Mesh::mesh &msh )
96  {
97  demag(Nodes::get_u<Nodes::NEXT>, Nodes::set_phi, msh);
98  demag(Nodes::get_v<Nodes::NEXT>, Nodes::set_phiv, msh);
99  }
100 
102  std::vector<double> corr;
103 
105  const std::vector<Tetra::prm> &prmTetra;
106 
108  const std::vector<Facette::prm> &prmFacette;
109 
110 private:
112  std::vector<double> srcDen;
113 
116 
119 
121  double norm;
122 
129  template<class T, const int NPI>
130  void insertCharges(std::vector<T> const &container, std::vector<int> const &idxContainer,
131  FSize &idx, Eigen::Ref<Eigen::Vector3d> const c)
132  {
133  std::for_each(idxContainer.begin(), idxContainer.end(),
134  [this,&container, c, &idx](int const &idxElem)
135  {
136  T const &elem = container[idxElem];
137  Eigen::Matrix<double,Nodes::DIM,NPI> gauss;
138  elem.getPtGauss(gauss);
139 
140  for (int j = 0; j < NPI; j++, idx++)
141  {
142  double x = norm*(gauss(0,j) - c.x());
143  double y = norm*(gauss(1,j) - c.y());
144  double z = norm*(gauss(2,j) - c.z());
145  tree.insert(FPoint<FReal>(x,y,z), FParticleType::FParticleTypeSource,
146  idx, 0.0);
147  }
148  });
149  }
150 
154  void calc_charges(std::function<const Eigen::Vector3d(const Nodes::Node&)> getter,
155  Mesh::mesh &msh)
156  {
157  int nsrc(0);
158  std::fill(srcDen.begin(),srcDen.end(),0);
159  std::for_each(msh.magTet.begin(),msh.magTet.end(),[this, &msh, getter, &nsrc](const int idx)
160  {
161  Tetra::Tet &t = msh.tet[idx];
162  Eigen::Matrix<double,Tetra::NPI,1> result =
163  t.charges(prmTetra[t.idxPrm].Ms, getter);
164  for(int i=0;i<Tetra::NPI;i++)
165  { srcDen[nsrc+i] = result(i); }
166  nsrc += Tetra::NPI;
167  });
168  std::fill(corr.begin(),corr.end(),0);
169  std::for_each(msh.magFac.begin(),msh.magFac.end(),[this, &msh, getter, &nsrc](const int idx)
170  {
171  Facette::Fac &f = msh.fac[idx];
172  Eigen::Matrix<double,Facette::NPI,1> result = f.charges(f.dMs, getter);
173  for(int i=0;i<Facette::NPI;i++)
174  { srcDen[nsrc+i] = result(i); }
175  nsrc += Facette::NPI;
176  f.correctionCharges(getter,result,corr);
177  });
178  }
179 
183  void demag(std::function<const Eigen::Vector3d(const Nodes::Node&)> getter,
184  std::function<void(Nodes::Node &, const double)> setter, Mesh::mesh &msh)
185  {
186  FmmClass algo(&tree, &kernels);
187  calc_charges(getter, msh);
188 
189  auto nbMagNodes = msh.magNode.size();
190  // reset potentials and forces - physicalValues[idxPart] = Q
191  tree.forEachLeaf(
192  [this,&nbMagNodes](LeafClass *leaf)
193  {
194  const int nbParticlesInLeaf = leaf->getSrc()->getNbParticles();
195  const auto &indexes = leaf->getSrc()->getIndexes();
196  FReal *const physicalValues = leaf->getSrc()->getPhysicalValues();
197  for (int idxPart = 0; idxPart < nbParticlesInLeaf; ++idxPart)
198  {
199  physicalValues[idxPart] = srcDen[indexes[idxPart] - nbMagNodes];
200  }
201 
202  std::fill_n(leaf->getTargets()->getPotentials(),
203  leaf->getTargets()->getNbParticles(), 0);
204  });
205 
206  tree.forEachCell([](CellClass *cell) { cell->resetToInitialState(); });
207 
208  algo.execute();
209 
210  tree.forEachLeaf(
211  [this, &msh, setter](LeafClass *leaf)
212  {
213  const FReal *const potentials = leaf->getTargets()->getPotentials();
214  const int nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
215  const auto &indexes = leaf->getTargets()->getIndexes();
216  for (int idxPart = 0; idxPart < nbParticlesInLeaf; ++idxPart)
217  {
218  const int indexPartOrig = indexes[idxPart];
219  msh.set(indexPartOrig, setter,
220  (potentials[idxPart] * norm + corr[indexPartOrig]) / (4 * M_PI));
221  }
222  });
223  }
224  }; // end class fmm
225 
226  } // namespace scal_fmm
227 #endif
Definition: mesh.h:32
const Eigen::Vector3d getNode_p(const int i) const
Definition: mesh.h:148
Eigen::Vector3d l
Definition: mesh.h:194
std::vector< int > magTet
Definition: mesh.h:337
Eigen::Vector3d c
Definition: mesh.h:191
std::vector< bool > magNode
Definition: mesh.h:334
std::vector< Tetra::Tet > tet
Definition: mesh.h:206
std::vector< Facette::Fac > fac
Definition: mesh.h:203
std::vector< int > magFac
Definition: mesh.h:343
void set(const int i, std::function< void(Nodes::Node &, const double)> what_to_set, const double val)
Definition: mesh.h:312
Definition: fmm_demag.h:60
double norm
Definition: fmm_demag.h:121
void demag(std::function< const Eigen::Vector3d(const Nodes::Node &)> getter, std::function< void(Nodes::Node &, const double)> setter, Mesh::mesh &msh)
Definition: fmm_demag.h:183
fmm(Mesh::mesh &msh, std::vector< Tetra::prm > &prmTet, std::vector< Facette::prm > &prmFac, const int ScalfmmNbThreads)
Definition: fmm_demag.h:64
std::vector< double > corr
Definition: fmm_demag.h:102
void insertCharges(std::vector< T > const &container, std::vector< int > const &idxContainer, FSize &idx, Eigen::Ref< Eigen::Vector3d > const c)
Definition: fmm_demag.h:130
void calc_demag(Mesh::mesh &msh)
Definition: fmm_demag.h:95
const std::vector< Facette::prm > & prmFacette
Definition: fmm_demag.h:108
std::vector< double > srcDen
Definition: fmm_demag.h:112
OctreeClass tree
Definition: fmm_demag.h:115
void calc_charges(std::function< const Eigen::Vector3d(const Nodes::Node &)> getter, Mesh::mesh &msh)
Definition: fmm_demag.h:154
const std::vector< Tetra::prm > & prmTetra
Definition: fmm_demag.h:105
KernelClass kernels
Definition: fmm_demag.h:118
class mesh, readMesh is expecting a mesh file in gmsh format either text or binary,...
const int NPI
Definition: facette.h:43
void set_phiv(Node &n, double val)
Definition: node.h:169
void set_phi(Node &n, double val)
Definition: node.h:166
const int NPI
Definition: tetra.h:50
const FPoint< FReal > boxCenter(0., 0., 0.)
FRotationKernel< FReal, CellClass, ContainerClass, P > KernelClass
Definition: fmm_demag.h:46
FP2PParticleContainerIndexed< FReal > ContainerClass
Definition: fmm_demag.h:37
FTypedRotationCell< FReal, P > CellClass
Definition: fmm_demag.h:34
FTypedLeaf< FReal, ContainerClass > LeafClass
Definition: fmm_demag.h:40
FOctree< FReal, CellClass, ContainerClass, LeafClass > OctreeClass
Definition: fmm_demag.h:43
const int P
Definition: fmm_demag.h:26
const double boxWidth
Definition: fmm_demag.h:52
const int SizeSubLevels
Definition: fmm_demag.h:28
const int NbLevels
Definition: fmm_demag.h:27
double FReal
Definition: fmm_demag.h:30
FFmmAlgorithmThreadTsm< OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass
Definition: fmm_demag.h:49
Definition: node.h:61