Feellgood
fmm_demag.h
Go to the documentation of this file.
1 #ifndef FMM_DEMAG_H
2 #define FMM_DEMAG_H
3 
9 #include "Utils/FParameters.hpp"
10 
11 #include "Components/FParticleType.hpp"
12 #include "Components/FTypedLeaf.hpp"
13 
14 #include "Containers/FOctree.hpp"
15 #include "Containers/FVector.hpp"
16 
17 #include "Core/FFmmAlgorithmThreadTsm.hpp"
18 
19 #include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
20 #include "Kernels/Rotation/FRotationCell.hpp"
21 #include "Kernels/Rotation/FRotationKernel.hpp"
22 
23 #include "mesh.h"
24 
29 namespace scal_fmm
30  {
31 
32 const int P = 9;
34 typedef double FReal;
36 typedef FTypedRotationCell<FReal, P>
39 typedef FP2PParticleContainerIndexed<FReal>
42 typedef FTypedLeaf<FReal, ContainerClass>
45 typedef FOctree<FReal, CellClass, ContainerClass, LeafClass>
48 typedef FRotationKernel<FReal, CellClass, ContainerClass, P>
51 typedef FFmmAlgorithmThreadTsm<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass>
55 const int NbLevels = 8;
56 const int SizeSubLevels = 6;
57 const double boxWidth = 2.01;
58 const FPoint<FReal> boxCenter(0., 0., 0.);
64 class fmm
65  {
66 public:
69  inline fmm(Mesh::mesh &msh ,
70  std::vector<Tetra::prm> & prmTet ,
71  std::vector<Facette::prm> & prmFac ,
72  const int ScalfmmNbThreads )
73  : prmTetra(prmTet), prmFacette(prmFac), NOD(msh.getNbNodes()),
75  {
76  omp_set_num_threads(ScalfmmNbThreads);
77  norm = 1. / (2. * msh.l.maxCoeff());
78 
79  FSize idxPart = 0;
80  for (idxPart = 0; idxPart < NOD; ++idxPart)
81  {
82  Eigen::Vector3d pTarget = norm*(msh.getNode_p(idxPart) - msh.c);
83  tree.insert(FPoint<FReal>(pTarget.x(), pTarget.y(), pTarget.z()),
84  FParticleType::FParticleTypeTarget, idxPart);
85  }
86 
87  insertCharges<Tetra::Tet, Tetra::NPI>(msh.tet, idxPart, msh.c);
88  insertCharges<Facette::Fac, Facette::NPI>(msh.fac, idxPart, msh.c);
89 
90  srcDen.resize( msh.getNbFacs()*Facette::NPI + msh.getNbTets()*Tetra::NPI );
91  corr.resize(NOD);
92  }
93 
97  void calc_demag(Mesh::mesh &msh )
98  {
99  demag(Nodes::get_u<Nodes::NEXT>, Nodes::set_phi, msh);
100  demag(Nodes::get_v<Nodes::NEXT>, Nodes::set_phiv, msh);
101  }
102 
104  std::vector<double> srcDen;
105 
107  std::vector<double> corr;
108 
110  std::vector<Tetra::prm> prmTetra;
111 
113  std::vector<Facette::prm> prmFacette;
114 
115 private:
116  const int NOD;
121  double norm;
128  template<class T, const int NPI>
129  void insertCharges(std::vector<T> const &container, FSize &idx, Eigen::Ref<Eigen::Vector3d> const c)
130  {
131  std::for_each(container.begin(), container.end(),
132  [this, c, &idx](T const &elem)
133  {
134  Eigen::Matrix<double,Nodes::DIM,NPI> gauss;
135  elem.getPtGauss(gauss);
136 
137  for (int j = 0; j < NPI; j++, idx++)
138  {
139  double x = norm*(gauss(0,j) - c.x());
140  double y = norm*(gauss(1,j) - c.y());
141  double z = norm*(gauss(2,j) - c.z());
142  tree.insert(FPoint<FReal>(x,y,z), FParticleType::FParticleTypeSource, idx, 0.0);
143  }
144  }); // end for_each
145  }
146 
149  void calc_charges(std::function<const Eigen::Vector3d(Nodes::Node)> getter, Mesh::mesh &msh)
150  {
151  int nsrc(0);
152  std::fill(srcDen.begin(),srcDen.end(),0);
153 
154  std::for_each(msh.tet.begin(), msh.tet.end(),
155  [this, getter, &nsrc](Tetra::Tet const &tet)
156  { tet.charges(prmTetra[tet.idxPrm], getter, srcDen, nsrc); });
157  std::fill(corr.begin(),corr.end(),0);
158  std::for_each(msh.fac.begin(), msh.fac.end(),
159  [this, getter, &nsrc](Facette::Fac const &fac)
160  { fac.charges(prmFacette[fac.idxPrm], getter, srcDen, nsrc, corr); });
161  }
162 
166  void demag(std::function<const Eigen::Vector3d(Nodes::Node)> getter,
167  std::function<void(Nodes::Node &, const double)> setter, Mesh::mesh &msh)
168  {
169  FmmClass algo(&tree, &kernels);
170  calc_charges(getter, msh);
171 
172  // reset potentials and forces - physicalValues[idxPart] = Q
173  tree.forEachLeaf(
174  [this](LeafClass *leaf)
175  {
176  const int nbParticlesInLeaf = leaf->getSrc()->getNbParticles();
177  const FVector<long long> &indexes = leaf->getSrc()->getIndexes();
178  FReal *const physicalValues = leaf->getSrc()->getPhysicalValues();
179  for (int idxPart = 0; idxPart < nbParticlesInLeaf; ++idxPart)
180  {
181  physicalValues[idxPart] = srcDen[indexes[idxPart] - NOD];
182  }
183 
184  std::fill_n(leaf->getTargets()->getPotentials(),
185  leaf->getTargets()->getNbParticles(), 0);
186  });
187 
188  tree.forEachCell([](CellClass *cell) { cell->resetToInitialState(); });
189 
190  algo.execute();
191 
192  tree.forEachLeaf(
193  [this, &msh, setter](LeafClass *leaf)
194  {
195  const FReal *const potentials = leaf->getTargets()->getPotentials();
196  const int nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
197  const FVector<long long> &indexes = leaf->getTargets()->getIndexes();
198 
199  for (int idxPart = 0; idxPart < nbParticlesInLeaf; ++idxPart)
200  {
201  const int indexPartOrig = indexes[idxPart];
202  msh.set(indexPartOrig, setter,
203  (potentials[idxPart] * norm + corr[indexPartOrig]) / (4 * M_PI));
204  }
205  });
206  }
207  }; // end class fmm
208 
209  } // namespace scal_fmm
210 #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:65
const int NOD
Definition: fmm_demag.h:116
double norm
Definition: fmm_demag.h:121
fmm(Mesh::mesh &msh, std::vector< Tetra::prm > &prmTet, std::vector< Facette::prm > &prmFac, const int ScalfmmNbThreads)
Definition: fmm_demag.h:69
void insertCharges(std::vector< T > const &container, FSize &idx, Eigen::Ref< Eigen::Vector3d > const c)
Definition: fmm_demag.h:129
std::vector< double > corr
Definition: fmm_demag.h:107
void calc_demag(Mesh::mesh &msh)
Definition: fmm_demag.h:97
std::vector< double > srcDen
Definition: fmm_demag.h:104
void calc_charges(std::function< const Eigen::Vector3d(Nodes::Node)> getter, Mesh::mesh &msh)
Definition: fmm_demag.h:149
std::vector< Facette::prm > prmFacette
Definition: fmm_demag.h:113
OctreeClass tree
Definition: fmm_demag.h:118
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:166
std::vector< Tetra::prm > prmTetra
Definition: fmm_demag.h:110
KernelClass kernels
Definition: fmm_demag.h:119
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:49
FP2PParticleContainerIndexed< FReal > ContainerClass
Definition: fmm_demag.h:40
FTypedRotationCell< FReal, P > CellClass
Definition: fmm_demag.h:37
FTypedLeaf< FReal, ContainerClass > LeafClass
Definition: fmm_demag.h:43
FOctree< FReal, CellClass, ContainerClass, LeafClass > OctreeClass
Definition: fmm_demag.h:46
const int P
Definition: fmm_demag.h:32
const double boxWidth
Definition: fmm_demag.h:57
const int SizeSubLevels
Definition: fmm_demag.h:56
const int NbLevels
Definition: fmm_demag.h:55
double FReal
Definition: fmm_demag.h:34
FFmmAlgorithmThreadTsm< OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass
Definition: fmm_demag.h:52
Definition: node.h:61