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 
12 #include "Containers/FOctree.hpp"
13 #include "Containers/FVector.hpp"
14 
15 #include "Core/FFmmAlgorithmThreadTsm.hpp"
16 
17 #include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
18 #include "Kernels/Rotation/FRotationCell.hpp"
19 #include "Kernels/Rotation/FRotationKernel.hpp"
20 
21 #include "mesh.h"
22 
27 namespace scal_fmm
28  {
29 
30 const int P = 9;
32 typedef double FReal;
34 typedef FTypedRotationCell<FReal, P>
37 typedef FP2PParticleContainerIndexed<FReal>
40 typedef FTypedLeaf<FReal, ContainerClass>
43 typedef FOctree<FReal, CellClass, ContainerClass, LeafClass>
46 typedef FRotationKernel<FReal, CellClass, ContainerClass, P>
49 typedef FFmmAlgorithmThreadTsm<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass>
53 const int NbLevels = 8;
54 const int SizeSubLevels = 6;
55 const double boxWidth = 2.01;
56 const FPoint<FReal> boxCenter(0., 0., 0.);
62 class fmm
63  {
64 public:
67  inline fmm(Mesh::mesh &msh ,
68  std::vector<Tetra::prm> & prmTet ,
69  std::vector<Facette::prm> & prmFac ,
70  const int ScalfmmNbThreads )
71  : prmTetra(prmTet), prmFacette(prmFac), NOD(msh.getNbNodes()),
73  {
74  omp_set_num_threads(ScalfmmNbThreads);
75  norm = 1. / (2. * msh.l.maxCoeff());
76 
77  FSize idxPart = 0;
78  for (idxPart = 0; idxPart < NOD; ++idxPart)
79  {
80  Eigen::Vector3d pTarget = norm*(msh.getNode_p(idxPart) - msh.c);
81  tree.insert(FPoint<FReal>(pTarget.x(), pTarget.y(), pTarget.z()),
82  FParticleType::FParticleTypeTarget, idxPart);
83  }
84 
85  insertCharges<Tetra::Tet, Tetra::NPI>(msh.tet, idxPart, msh.c);
86  insertCharges<Facette::Fac, Facette::NPI>(msh.fac, idxPart, msh.c);
87 
88  srcDen.resize( msh.getNbFacs()*Facette::NPI + msh.getNbTets()*Tetra::NPI );
89  corr.resize(NOD);
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> srcDen;
103 
105  std::vector<double> corr;
106 
108  std::vector<Tetra::prm> prmTetra;
109 
111  std::vector<Facette::prm> prmFacette;
112 
113 private:
114  const int NOD;
119  double norm;
126  template<class T, const int NPI>
127  void insertCharges(std::vector<T> const &container, FSize &idx, Eigen::Ref<Eigen::Vector3d> const c)
128  {
129  std::for_each(container.begin(), container.end(),
130  [this, c, &idx](T const &elem)
131  {
132  Eigen::Matrix<double,Nodes::DIM,NPI> gauss;
133  elem.getPtGauss(gauss);
134 
135  for (int j = 0; j < NPI; j++, idx++)
136  {
137  double x = norm*(gauss(0,j) - c.x());
138  double y = norm*(gauss(1,j) - c.y());
139  double z = norm*(gauss(2,j) - c.z());
140  tree.insert(FPoint<FReal>(x,y,z), FParticleType::FParticleTypeSource, idx, 0.0);
141  }
142  }); // end for_each
143  }
144 
147  void calc_charges(std::function<const Eigen::Vector3d(Nodes::Node)> getter, Mesh::mesh &msh)
148  {
149  int nsrc(0);
150  std::fill(srcDen.begin(),srcDen.end(),0);
151 
152  std::for_each(msh.tet.begin(), msh.tet.end(),
153  [this, getter, &nsrc](Tetra::Tet const &tet)
154  { tet.charges(prmTetra[tet.idxPrm], getter, srcDen, nsrc); });
155  std::fill(corr.begin(),corr.end(),0);
156  std::for_each(msh.fac.begin(), msh.fac.end(),
157  [this, getter, &nsrc](Facette::Fac const &fac)
158  { fac.charges(prmFacette[fac.idxPrm], getter, srcDen, nsrc, corr); });
159  }
160 
164  void demag(std::function<const Eigen::Vector3d(Nodes::Node)> getter,
165  std::function<void(Nodes::Node &, const double)> setter, Mesh::mesh &msh)
166  {
167  FmmClass algo(&tree, &kernels);
168  calc_charges(getter, msh);
169 
170  // reset potentials and forces - physicalValues[idxPart] = Q
171  tree.forEachLeaf(
172  [this](LeafClass *leaf)
173  {
174  const int nbParticlesInLeaf = leaf->getSrc()->getNbParticles();
175  const FVector<long long> &indexes = leaf->getSrc()->getIndexes();
176  FReal *const physicalValues = leaf->getSrc()->getPhysicalValues();
177  for (int idxPart = 0; idxPart < nbParticlesInLeaf; ++idxPart)
178  {
179  physicalValues[idxPart] = srcDen[indexes[idxPart] - NOD];
180  }
181 
182  std::fill_n(leaf->getTargets()->getPotentials(),
183  leaf->getTargets()->getNbParticles(), 0);
184  });
185 
186  tree.forEachCell([](CellClass *cell) { cell->resetToInitialState(); });
187 
188  algo.execute();
189 
190  tree.forEachLeaf(
191  [this, &msh, setter](LeafClass *leaf)
192  {
193  const FReal *const potentials = leaf->getTargets()->getPotentials();
194  const int nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
195  const FVector<long long> &indexes = leaf->getTargets()->getIndexes();
196 
197  for (int idxPart = 0; idxPart < nbParticlesInLeaf; ++idxPart)
198  {
199  const int indexPartOrig = indexes[idxPart];
200  msh.set(indexPartOrig, setter,
201  (potentials[idxPart] * norm + corr[indexPartOrig]) / (4 * M_PI));
202  }
203  });
204  }
205  }; // end class fmm
206 
207  } // namespace scal_fmm
208 #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:63
const int NOD
Definition: fmm_demag.h:114
double norm
Definition: fmm_demag.h:119
fmm(Mesh::mesh &msh, std::vector< Tetra::prm > &prmTet, std::vector< Facette::prm > &prmFac, const int ScalfmmNbThreads)
Definition: fmm_demag.h:67
void insertCharges(std::vector< T > const &container, FSize &idx, Eigen::Ref< Eigen::Vector3d > const c)
Definition: fmm_demag.h:127
std::vector< double > corr
Definition: fmm_demag.h:105
void calc_demag(Mesh::mesh &msh)
Definition: fmm_demag.h:95
std::vector< double > srcDen
Definition: fmm_demag.h:102
void calc_charges(std::function< const Eigen::Vector3d(Nodes::Node)> getter, Mesh::mesh &msh)
Definition: fmm_demag.h:147
std::vector< Facette::prm > prmFacette
Definition: fmm_demag.h:111
OctreeClass tree
Definition: fmm_demag.h:116
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:164
std::vector< Tetra::prm > prmTetra
Definition: fmm_demag.h:108
KernelClass kernels
Definition: fmm_demag.h:117
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:47
FP2PParticleContainerIndexed< FReal > ContainerClass
Definition: fmm_demag.h:38
FTypedRotationCell< FReal, P > CellClass
Definition: fmm_demag.h:35
FTypedLeaf< FReal, ContainerClass > LeafClass
Definition: fmm_demag.h:41
FOctree< FReal, CellClass, ContainerClass, LeafClass > OctreeClass
Definition: fmm_demag.h:44
const int P
Definition: fmm_demag.h:30
const double boxWidth
Definition: fmm_demag.h:55
const int SizeSubLevels
Definition: fmm_demag.h:54
const int NbLevels
Definition: fmm_demag.h:53
double FReal
Definition: fmm_demag.h:32
FFmmAlgorithmThreadTsm< OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass
Definition: fmm_demag.h:50
Definition: node.h:61