Feellgood
fmm_demag.h
Go to the documentation of this file.
1 #ifndef FMM_DEMAG_H
2 #define FMM_DEMAG_H
3 
9 #include "Components/FParticleType.hpp"
10 #include "Components/FTypedLeaf.hpp"
11 #include "Containers/FOctree.hpp"
12 #include "Core/FFmmAlgorithmThreadTsm.hpp"
13 #include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
14 #include "Kernels/Rotation/FRotationCell.hpp"
15 #include "Kernels/Rotation/FRotationKernel.hpp"
16 
17 #include "mesh.h"
18 
23 namespace scal_fmm
24  {
25 const int P = 9;
26 const int NbLevels = 6;
27 const int SizeSubLevels = 3;
29 typedef double FReal;
31 typedef FTypedRotationCell<FReal, P>
34 typedef FP2PParticleContainerIndexed<FReal>
37 typedef FTypedLeaf<FReal, ContainerClass>
40 typedef FOctree<FReal, CellClass, ContainerClass, LeafClass>
43 typedef FRotationKernel<FReal, CellClass, ContainerClass, P>
46 typedef FFmmAlgorithmThreadTsm<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass>
50 const double boxWidth = 2.01;
51 const FPoint<FReal> boxCenter(0., 0., 0.);
57 class fmm
58  {
59 public:
62  inline fmm(Mesh::mesh &msh ,
63  std::vector<Tetra::prm> & prmTet ,
64  std::vector<Facette::prm> & prmFac ,
65  const int ScalfmmNbThreads )
66  : prmTetra(prmTet), prmFacette(prmFac), NOD(msh.getNbNodes()),
68  {
69  omp_set_num_threads(ScalfmmNbThreads);
70  norm = 2. / msh.l.maxCoeff();
71 
72  FSize idxPart = 0;
73  for (idxPart = 0; idxPart < NOD; ++idxPart)
74  {
75  Eigen::Vector3d pTarget = norm*(msh.getNode_p(idxPart) - msh.c);
76  tree.insert(FPoint<FReal>(pTarget.x(), pTarget.y(), pTarget.z()),
77  FParticleType::FParticleTypeTarget, idxPart);
78  }
79 
80  insertCharges<Tetra::Tet, Tetra::NPI>(msh.tet, idxPart, msh.c);
81  insertCharges<Facette::Fac, Facette::NPI>(msh.fac, idxPart, msh.c);
82 
83  srcDen.resize( msh.getNbFacs()*Facette::NPI + msh.getNbTets()*Tetra::NPI );
84  corr.resize(NOD);
85  }
86 
90  void calc_demag(Mesh::mesh &msh )
91  {
92  demag(Nodes::get_u<Nodes::NEXT>, Nodes::set_phi, msh);
93  demag(Nodes::get_v<Nodes::NEXT>, Nodes::set_phiv, msh);
94  }
95 
97  std::vector<double> srcDen;
98 
100  std::vector<double> corr;
101 
103  std::vector<Tetra::prm> prmTetra;
104 
106  std::vector<Facette::prm> prmFacette;
107 
108 private:
109  const int NOD;
115  double norm;
122  template<class T, const int NPI>
123  void insertCharges(std::vector<T> const &container, FSize &idx, Eigen::Ref<Eigen::Vector3d> const c)
124  {
125  std::for_each(container.begin(), container.end(),
126  [this, c, &idx](T const &elem)
127  {
128  Eigen::Matrix<double,Nodes::DIM,NPI> gauss;
129  elem.getPtGauss(gauss);
130 
131  for (int j = 0; j < NPI; j++, idx++)
132  {
133  double x = norm*(gauss(0,j) - c.x());
134  double y = norm*(gauss(1,j) - c.y());
135  double z = norm*(gauss(2,j) - c.z());
136  tree.insert(FPoint<FReal>(x,y,z), FParticleType::FParticleTypeSource, idx, 0.0);
137  }
138  }); // end for_each
139  }
140 
143  void calc_charges(std::function<const Eigen::Vector3d(Nodes::Node)> getter, Mesh::mesh &msh)
144  {
145  int nsrc(0);
146  std::fill(srcDen.begin(),srcDen.end(),0);
147 
148  std::for_each(msh.tet.begin(), msh.tet.end(),
149  [this, getter, &nsrc](Tetra::Tet const &tet)
150  { tet.charges(prmTetra[tet.idxPrm], getter, srcDen, nsrc); });
151  std::fill(corr.begin(),corr.end(),0);
152  std::for_each(msh.fac.begin(), msh.fac.end(),
153  [this, getter, &nsrc](Facette::Fac const &fac)
154  { fac.charges(prmFacette[fac.idxPrm], getter, srcDen, nsrc, corr); });
155  }
156 
160  void demag(std::function<const Eigen::Vector3d(Nodes::Node)> getter,
161  std::function<void(Nodes::Node &, const double)> setter, Mesh::mesh &msh)
162  {
163  FmmClass algo(&tree, &kernels);
164  calc_charges(getter, msh);
165 
166  // reset potentials and forces - physicalValues[idxPart] = Q
167  tree.forEachLeaf(
168  [this](LeafClass *leaf)
169  {
170  const int nbParticlesInLeaf = leaf->getSrc()->getNbParticles();
171  const auto &indexes = leaf->getSrc()->getIndexes();
172  FReal *const physicalValues = leaf->getSrc()->getPhysicalValues();
173  for (int idxPart = 0; idxPart < nbParticlesInLeaf; ++idxPart)
174  {
175  physicalValues[idxPart] = srcDen[indexes[idxPart] - NOD];
176  }
177 
178  std::fill_n(leaf->getTargets()->getPotentials(),
179  leaf->getTargets()->getNbParticles(), 0);
180  });
181 
182  tree.forEachCell([](CellClass *cell) { cell->resetToInitialState(); });
183 
184  algo.execute();
185 
186  tree.forEachLeaf(
187  [this, &msh, setter](LeafClass *leaf)
188  {
189  const FReal *const potentials = leaf->getTargets()->getPotentials();
190  const int nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
191  const auto &indexes = leaf->getTargets()->getIndexes();
192  for (int idxPart = 0; idxPart < nbParticlesInLeaf; ++idxPart)
193  {
194  const int indexPartOrig = indexes[idxPart];
195  msh.set(indexPartOrig, setter,
196  (potentials[idxPart] * norm + corr[indexPartOrig]) / (4 * M_PI));
197  }
198  });
199  }
200  }; // end class fmm
201 
202  } // namespace scal_fmm
203 #endif
Definition: facette.h:69
Definition: mesh.h:27
const Eigen::Vector3d getNode_p(const int i) const
Definition: mesh.h:83
Eigen::Vector3d l
Definition: mesh.h:128
int getNbFacs(void) const
Definition: mesh.h:77
Eigen::Vector3d c
Definition: mesh.h:125
std::vector< Tetra::Tet > tet
Definition: mesh.h:137
int getNbTets(void) const
Definition: mesh.h:80
std::vector< Facette::Fac > fac
Definition: mesh.h:134
void set(const int i, std::function< void(Nodes::Node &, const double)> what_to_set, const double val)
Definition: mesh.h:178
Definition: tetra.h:124
Definition: fmm_demag.h:58
const int NOD
Definition: fmm_demag.h:109
double norm
Definition: fmm_demag.h:115
fmm(Mesh::mesh &msh, std::vector< Tetra::prm > &prmTet, std::vector< Facette::prm > &prmFac, const int ScalfmmNbThreads)
Definition: fmm_demag.h:62
void insertCharges(std::vector< T > const &container, FSize &idx, Eigen::Ref< Eigen::Vector3d > const c)
Definition: fmm_demag.h:123
std::vector< double > corr
Definition: fmm_demag.h:100
void calc_demag(Mesh::mesh &msh)
Definition: fmm_demag.h:90
std::vector< double > srcDen
Definition: fmm_demag.h:97
void calc_charges(std::function< const Eigen::Vector3d(Nodes::Node)> getter, Mesh::mesh &msh)
Definition: fmm_demag.h:143
std::vector< Facette::prm > prmFacette
Definition: fmm_demag.h:106
OctreeClass tree
Definition: fmm_demag.h:111
void demag(std::function< const Eigen::Vector3d(Nodes::Node)> getter, std::function< void(Nodes::Node &, const double)> setter, Mesh::mesh &msh)
Definition: fmm_demag.h:160
std::vector< Tetra::prm > prmTetra
Definition: fmm_demag.h:103
KernelClass kernels
Definition: fmm_demag.h:113
class mesh, readMesh is expecting a mesh file in gmsh format either text or binary,...
const int NPI
Definition: facette.h:18
void set_phiv(Node &n, double val)
Definition: node.h:174
void set_phi(Node &n, double val)
Definition: node.h:171
const int NPI
Definition: tetra.h:23
const FPoint< FReal > boxCenter(0., 0., 0.)
FRotationKernel< FReal, CellClass, ContainerClass, P > KernelClass
Definition: fmm_demag.h:44
FP2PParticleContainerIndexed< FReal > ContainerClass
Definition: fmm_demag.h:35
FTypedRotationCell< FReal, P > CellClass
Definition: fmm_demag.h:32
FTypedLeaf< FReal, ContainerClass > LeafClass
Definition: fmm_demag.h:38
FOctree< FReal, CellClass, ContainerClass, LeafClass > OctreeClass
Definition: fmm_demag.h:41
const int P
Definition: fmm_demag.h:25
const double boxWidth
Definition: fmm_demag.h:50
const int SizeSubLevels
Definition: fmm_demag.h:27
const int NbLevels
Definition: fmm_demag.h:26
double FReal
Definition: fmm_demag.h:29
FFmmAlgorithmThreadTsm< OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass
Definition: fmm_demag.h:47
Definition: node.h:61