Feellgood
mesh.h
Go to the documentation of this file.
1 #ifndef mesh_h
2 #define mesh_h
3 
10 #include <cmath>
11 #include <algorithm>
12 #pragma GCC diagnostic push
13 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
14 #include <execution>
15 #pragma GCC diagnostic pop
16 
17 #include "triangle.h"
18 #include "node.h"
19 #include "tetra.h"
20 #include "settings.h"
21 
22 class BasicTri;
23 
24 namespace Mesh
25  {
27  using Edge = std::pair<int, int>;
28 
33 class mesh
34  {
35 public:
38  mesh(Settings &mySets ,
39  bool simplified = false )
40  : settings(mySets), volumeRegions(settings.paramTetra.size())
41  {
42  readMesh();
43  if (simplified)
44  { return; }
45  if (!controlTriangles())
46  { exit(1); }
47 
48  if (settings.verbose)
49  { std::cout << " reindexed\n"; }
50 
51  double xmin = minNodes(Nodes::IDX_X);
52  double xmax = maxNodes(Nodes::IDX_X);
53 
54  double ymin = minNodes(Nodes::IDX_Y);
55  double ymax = maxNodes(Nodes::IDX_Y);
56 
57  double zmin = minNodes(Nodes::IDX_Z);
58  double zmax = maxNodes(Nodes::IDX_Z);
59 
60  l = Eigen::Vector3d(xmax - xmin, ymax - ymin, zmax - zmin);
61  c = Eigen::Vector3d(0.5 * (xmax + xmin), 0.5 * (ymax + ymin), 0.5 * (zmax + zmin));
62 
63  // Find the longest axis of the sample.
64  Nodes::index long_axis;
65  if (l.x() > l.y())
66  {
67  if (l.x() > l.z())
68  long_axis = Nodes::IDX_X;
69  else
70  long_axis = Nodes::IDX_Z;
71  }
72  else
73  {
74  if (l.y() > l.z())
75  long_axis = Nodes::IDX_Y;
76  else
77  long_axis = Nodes::IDX_Z;
78  }
79  sortNodes(long_axis);
80 
81  totalMagVol = 0;
82  // Compute the per-region volumes and the total volume.
83  std::for_each(tet.begin(), tet.end(),
84  [this](const Tetra::Tet &te)
85  {
86  double vol_tet = te.calc_vol();
87  settings.paramTetra[te.idxPrm].volume += vol_tet;
88  if(isMagnetic(te))
89  totalMagVol += vol_tet;
90  });
91  vol = std::transform_reduce(settings.paramTetra.begin(), settings.paramTetra.end(), 0.0,
92  std::plus<>(), [](const Tetra::prm &region){ return region.volume; });
93 
94  // Build the list of tetrahedrons for each region.
95  std::for_each(tet.begin(), tet.end(), [this](const Tetra::Tet &te)
96  {
97  volumeRegions[te.idxPrm].push_back(te.idx);
98  });
99 
100  // Build the list of all the mesh edges.
101  edges.reserve(tet.size() * 6); // 6 (non-unique) edges per tetrahedron
102  std::for_each(tet.begin(), tet.end(),
103  [this](const Tetra::Tet &te)
104  {
105  for (int i = 0; i < 3; ++i)
106  {
107  for (int j = i + 1; j < 4; ++j)
108  { edges.push_back(std::minmax(te.ind[i], te.ind[j])); }
109  }
110  });
111  std::sort(EXEC_POL, edges.begin(), edges.end());
112  auto last = std::unique(EXEC_POL, edges.begin(), edges.end());
113  edges.erase(last, edges.end());
114  edges.shrink_to_fit(); // save memory, as this could be quite big
115  magNode.resize(node.size());
116  std::fill(magNode.begin(),magNode.end(),false);
117  std::for_each(tet.begin(),tet.end(),[this](Tetra::Tet &te)
118  {
119  if(isMagnetic(te))
120  {
121  magTet.push_back(te.idx);
122  for (int i=0;i<4;i++)
123  { magNode[te.ind[i]] = true; }
124  }
125  });
126 
127  for(unsigned int i=0;i<tri.size();i++)
128  {
129  if (isMagnetic(tri[i]) && !settings.paramTriangle[tri[i].idxPrm].suppress_charges)
130  { magTri.push_back(i); }
131  }
132 
133  if(settings.getFieldType() == R4toR3)
134  { setExtSpaceField(); }
135 
136  if (settings.absorb.enable)
137  { setAbsorbingEnds(); }
138  }
139 
141  mesh(const mesh &) = delete;
142 
144  mesh &operator=(const mesh &) = delete;
145 
147  inline int getNbNodes(void) const { return node.size(); }
148 
150  inline int getNbTris(void) const { return tri.size(); }
151 
153  inline int getNbTets(void) const { return tet.size(); }
154 
156  inline const Eigen::Vector3d getNode_p(const int i) const { return node[i].p; }
157 
159  inline const Eigen::Vector3d getNode_u(const int i, const Nodes::step k = Nodes::NEXT) const
160  { return node[i].get_u(k); }
161 
163  inline const Eigen::Vector3d getNode_v(const int i) const { return node[i].get_v(Nodes::NEXT); }
164 
166  inline double getProj_ep(const int i) const {return node[i].proj_ep();}
167 
169  inline double getProj_eq(const int i) const {return node[i].proj_eq();}
170 
172  inline void set_node_u0(const int i, const Eigen::Vector3d &val)
173  { node[i].d[Nodes::CURRENT].u = val; }
174 
176  inline void set_node_zero_v(const int i) { node[i].d[Nodes::NEXT].v.setZero(); }
177 
179  void infos(void) const;
180 
182  inline void setBasis(const double r)
183  {
184  std::for_each(EXEC_POL, node.begin(), node.end(),
185  [&r](Nodes::Node &nod) { nod.setBasis(r); });
186  }
187 
189  inline void updateNode(const int i, const double vp, const double vq, const double dt)
190  { node[i].make_evol(vp, vq, dt); }
191 
193  inline void evolution(void)
194  {
195  std::for_each(EXEC_POL, node.begin(), node.end(),
196  [](Nodes::Node &nod) { nod.evolution(); });
197  }
198 
200  Eigen::Vector3d c;
201 
203  Eigen::Vector3d l;
204 
206  double vol;
207 
209  double totalMagVol;
210 
212  std::vector<Triangle::Tri> tri;
213 
215  std::vector<Tetra::Tet> tet;
216 
220  double readSol(bool VERBOSE ,
221  const std::string& fileName );
222 
225  inline void init_distrib()
226  {
227  for (int nodeIdx = 0; nodeIdx < int(node.size()); ++nodeIdx)
228  {
229  Nodes::Node &n = node[nodeIdx];
230  n.d[Nodes::NEXT].phi = 0.;
231  n.d[Nodes::NEXT].phiv = 0.;
232 
233  // A non-magnetic node's magnetization is a vector of NAN.
234  if (!magNode[nodeIdx])
235  {
236  n.d[Nodes::CURRENT].u = Eigen::Vector3d(NAN, NAN, NAN);
237  n.d[Nodes::NEXT].u = n.d[Nodes::CURRENT].u;
238  continue;
239  }
240 
241  // If the initial magnetization depends only on the node position, we do not need to
242  // build the list of region names.
243  if (settings.getMagType() == POSITION_ONLY)
244  {
245  n.d[Nodes::CURRENT].u = settings.getMagnetization(n.p);
246  n.d[Nodes::NEXT].u = n.d[Nodes::CURRENT].u;
247  continue;
248  }
249 
250  // Get the list of region indices this node belongs to.
251  std::set<int> nodeRegions;
252  // skip region 0 (__default__) which should be empty
253  for (size_t regIdx = 1; regIdx < volumeRegions.size(); ++regIdx)
254  {
255  const std::vector<int> &regionTetras = volumeRegions[regIdx];
256  bool node_in_region = std::any_of(EXEC_POL,
257  regionTetras.begin(), regionTetras.end(),
258  [this, nodeIdx](const int tetIdx)
259  {
260  const Tetra::Tet &tetrahedron = tet[tetIdx];
261  for (int i = 0; i < Tetra::N; ++i) // node of tetrahedron tetIdx
262  {
263  if (tetrahedron.ind[i] == nodeIdx)
264  { return true; }
265  }
266  return false;
267  });
268  if (node_in_region)
269  { nodeRegions.insert(regIdx); }
270  }
271 
272  // Get the list of region names this node belongs to.
273  std::vector<std::string> region_names;
274  region_names.resize(nodeRegions.size());
275  std::transform(nodeRegions.begin(), nodeRegions.end(), region_names.begin(),
276  [this](const int regIdx){ return settings.paramTetra[regIdx].regName; });
277 
278  n.d[Nodes::CURRENT].u = settings.getMagnetization(n.p, region_names);
279  n.d[Nodes::NEXT].u = n.d[Nodes::CURRENT].u;
280  }
281  }
282 
289  double thiele(const int region ) const;
290 
294  double avg(const std::function<double(Nodes::Node, Nodes::index)>& getter ,
295  Nodes::index d ,
296  int region = -1 ) const;
297 
299  double max_angle() const
300  {
301  double min_dot_product = std::transform_reduce(EXEC_POL, edges.begin(), edges.end(), 1.0,
302  [](const double a, const double b){ return std::min(a, b); },
303  [this](const Edge &edge)
304  {
305  Eigen::Vector3d m1 = getNode_u(edge.first);
306  Eigen::Vector3d m2 = getNode_u(edge.second);
307  return m1.dot(m2);
308  });
309  return std::acos(min_dot_product);
310  }
311 
319  void savesol(const int precision ,
320  const std::string& fileName ,
321  const std::string &metadata ,
322  bool withSpinAcc ,
323  const std::vector<Eigen::Vector3d> &s ,
324  const std::vector<double> &V
325  ) const;
326 
329  inline void set(const int i ,
330  const std::function<void(Nodes::Node &, const double)>& what_to_set ,
331  const double val )
332  { what_to_set(node[i], val); }
333 
335  inline int getNodeIndex(const int i) const { return node_index[i]; }
336 
338  inline bool isMagnetic(const Tetra::Tet &t) const
339  { return (settings.paramTetra[t.idxPrm].Ms > 0); }
340 
342  inline bool isMagnetic(const Triangle::Tri &f)
343  { return (magNode[f.ind[0]] && magNode[f.ind[1]] && magNode[f.ind[2]]); }
344 
356  bool controlTriangles();
357 
360  std::vector<Edge> edges;
361 
365  std::vector<bool> magNode;
366 
368  std::vector<int> magTet;
369 
374  std::vector<int> magTri;
375 
377  std::vector< Eigen::Matrix<double,Nodes::DIM,Tetra::NPI> > extSpaceField;
378 
379 private:
382 
385  std::vector<Nodes::Node> node;
386 
392  std::vector<int> node_index;
393 
395  std::vector<std::vector<int>> volumeRegions;
396 
399  void checkMeshFile();
400 
402  void readNodes();
403 
405  void readTetraedrons();
406 
408  void readTriangles();
409 
411  void readMesh();
412 
414  double doOnNodes(const double init_val ,
415  const Nodes::index coord ,
416  const std::function<bool(double, double)>& whatToDo ) const;
417 
419  inline double minNodes(const Nodes::index coord ) const
420  {
421  return doOnNodes(__DBL_MAX__, coord, [](const double a, const double b)
422  { return a < b; });
423  }
424 
426  inline double maxNodes(const Nodes::index coord ) const
427  {
428  return doOnNodes(__DBL_MIN__, coord, [](const double a, const double b)
429  { return a > b; });
430  }
431 
434  void sortNodes(Nodes::index long_axis );
435 
438  bool diffTriHandler(const size_t inf, const size_t sup, const std::vector<BasicTri> &allTriCtnr,
439  std::vector<size_t> &indNodTriToAdd, std::vector<std::pair<int,int>> &regsTriToAdd);
440 
442  bool assertNoErrInTriangle(const int nbSurfTri, const int nbTetraFaces,
443  const std::pair<int,int> pairIdVolRegs, const int idSurfReg);
444 
448  double surface(std::vector<int> &triIndices) const;
449 
452  void setExtSpaceField();
453 
457  void setAbsorbingEnds();
458  }; // end class mesh
459  } // end namespace Mesh
460 #endif
Definition: mesh.cpp:14
Definition: mesh.h:34
const Eigen::Vector3d getNode_p(const int i) const
Definition: mesh.h:156
Eigen::Vector3d l
Definition: mesh.h:203
std::vector< int > magTet
Definition: mesh.h:368
std::vector< Eigen::Matrix< double, Nodes::DIM, Tetra::NPI > > extSpaceField
Definition: mesh.h:377
bool isMagnetic(const Tetra::Tet &t) const
Definition: mesh.h:338
Eigen::Vector3d c
Definition: mesh.h:200
void init_distrib()
Definition: mesh.h:225
void set_node_zero_v(const int i)
Definition: mesh.h:176
void set_node_u0(const int i, const Eigen::Vector3d &val)
Definition: mesh.h:172
void readMesh()
Definition: read.cpp:13
double max_angle() const
Definition: mesh.h:299
std::vector< Edge > edges
Definition: mesh.h:360
int getNbTris(void) const
Definition: mesh.h:150
int getNbNodes(void) const
Definition: mesh.h:147
std::vector< Nodes::Node > node
Definition: mesh.h:385
const Eigen::Vector3d getNode_u(const int i, const Nodes::step k=Nodes::NEXT) const
Definition: mesh.h:159
double minNodes(const Nodes::index coord) const
Definition: mesh.h:419
std::vector< bool > magNode
Definition: mesh.h:365
mesh(Settings &mySets, bool simplified=false)
Definition: mesh.h:38
std::vector< std::vector< int > > volumeRegions
Definition: mesh.h:395
Settings & settings
Definition: mesh.h:381
int getNodeIndex(const int i) const
Definition: mesh.h:335
bool isMagnetic(const Triangle::Tri &f)
Definition: mesh.h:342
double getProj_ep(const int i) const
Definition: mesh.h:166
std::vector< Tetra::Tet > tet
Definition: mesh.h:215
double maxNodes(const Nodes::index coord) const
Definition: mesh.h:426
int getNbTets(void) const
Definition: mesh.h:153
void sortNodes(Nodes::index long_axis)
Definition: mesh.cpp:334
std::vector< int > magTri
Definition: mesh.h:374
void set(const int i, const std::function< void(Nodes::Node &, const double)> &what_to_set, const double val)
Definition: mesh.h:329
mesh(const mesh &)=delete
void setBasis(const double r)
Definition: mesh.h:182
void evolution(void)
Definition: mesh.h:193
double totalMagVol
Definition: mesh.h:209
const Eigen::Vector3d getNode_v(const int i) const
Definition: mesh.h:163
std::vector< int > node_index
Definition: mesh.h:392
double getProj_eq(const int i) const
Definition: mesh.h:169
void updateNode(const int i, const double vp, const double vq, const double dt)
Definition: mesh.h:189
bool controlTriangles()
Definition: mesh.cpp:121
double vol
Definition: mesh.h:206
std::vector< Triangle::Tri > tri
Definition: mesh.h:212
mesh & operator=(const mesh &)=delete
Container for all the settings provided by the user, with conversions to/from YAML.
Definition: settings.h:72
int verbose
Definition: settings.h:135
std::vector< Tetra::prm > paramTetra
Definition: settings.h:216
Definition: tetra.h:136
Definition: triangle.h:109
int idxPrm
Definition: element.h:56
std::array< int, N > ind
Definition: element.h:53
std::pair< int, int > Edge
Definition: mesh.h:27
index
Definition: node.h:33
step
Definition: node.h:26
constexpr double a[N][NPI]
Definition: tetra.h:69
header to define struct Node
many settings to give some parameters to the solver, boundary conditions for the problem,...
@ R4toR3
Definition: settings.h:44
@ POSITION_ONLY
M(x, y, z)
Definition: settings.h:27
Definition: node.h:60
Eigen::Vector3d p
Definition: node.h:61
dataNode d[NB_DATANODE]
Definition: node.h:70
double phi
Definition: node.h:51
double phiv
Definition: node.h:52
Eigen::Vector3d u
Definition: node.h:49
Definition: tetra.h:88
namespace Tetra header containing Tet class, some constants, and integrales
contains namespace Triangle header containing Tri class, and some constants and a less_than operator ...