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 
138  mesh(const mesh &) = delete;
139 
141  mesh &operator=(const mesh &) = delete;
142 
144  inline int getNbNodes(void) const { return node.size(); }
145 
147  inline int getNbTris(void) const { return tri.size(); }
148 
150  inline int getNbTets(void) const { return tet.size(); }
151 
153  inline const Eigen::Vector3d getNode_p(const int i) const { return node[i].p; }
154 
156  inline const Eigen::Vector3d getNode_u(const int i) const { return node[i].get_u(Nodes::NEXT); }
157 
159  inline const Eigen::Vector3d getNode_v(const int i) const { return node[i].get_v(Nodes::NEXT); }
160 
162  inline double getProj_ep(const int i) const {return node[i].proj_ep();}
163 
165  inline double getProj_eq(const int i) const {return node[i].proj_eq();}
166 
168  inline void set_node_u0(const int i, const Eigen::Vector3d &val)
169  { node[i].d[Nodes::CURRENT].u = val; }
170 
172  inline void set_node_zero_v(const int i) { node[i].d[Nodes::NEXT].v.setZero(); }
173 
175  void infos(void) const;
176 
178  inline void setBasis(const double r)
179  {
180  std::for_each(EXEC_POL, node.begin(), node.end(),
181  [&r](Nodes::Node &nod) { nod.setBasis(r); });
182  }
183 
185  inline void updateNode(const int i, const double vp, const double vq, const double dt)
186  { node[i].make_evol(vp*gamma0, vq*gamma0, dt); }
187 
189  inline void evolution(void)
190  {
191  std::for_each(EXEC_POL, node.begin(), node.end(),
192  [](Nodes::Node &nod) { nod.evolution(); });
193  }
194 
196  Eigen::Vector3d c;
197 
199  Eigen::Vector3d l;
200 
202  double vol;
203 
205  double totalMagVol;
206 
208  std::vector<Triangle::Tri> tri;
209 
211  std::vector<Tetra::Tet> tet;
212 
216  double readSol(bool VERBOSE ,
217  const std::string& fileName );
218 
221  inline void init_distrib()
222  {
223  for (int nodeIdx = 0; nodeIdx < int(node.size()); ++nodeIdx)
224  {
225  Nodes::Node &n = node[nodeIdx];
226  n.d[Nodes::NEXT].phi = 0.;
227  n.d[Nodes::NEXT].phiv = 0.;
228 
229  // A non-magnetic node's magnetization is a vector of NAN.
230  if (!magNode[nodeIdx])
231  {
232  n.d[Nodes::CURRENT].u = Eigen::Vector3d(NAN, NAN, NAN);
233  n.d[Nodes::NEXT].u = n.d[Nodes::CURRENT].u;
234  continue;
235  }
236 
237  // If the initial magnetization depends only on the node position, we do not need to
238  // build the list of region names.
239  if (settings.getMagType() == POSITION_ONLY)
240  {
241  n.d[Nodes::CURRENT].u = settings.getMagnetization(n.p);
242  n.d[Nodes::NEXT].u = n.d[Nodes::CURRENT].u;
243  continue;
244  }
245 
246  // Get the list of region indices this node belongs to.
247  std::set<int> nodeRegions;
248  // skip region 0 (__default__) which should be empty
249  for (size_t regIdx = 1; regIdx < volumeRegions.size(); ++regIdx)
250  {
251  const std::vector<int> &regionTetras = volumeRegions[regIdx];
252  bool node_in_region = std::any_of(EXEC_POL,
253  regionTetras.begin(), regionTetras.end(),
254  [this, nodeIdx](const int tetIdx)
255  {
256  const Tetra::Tet &tetrahedron = tet[tetIdx];
257  for (int i = 0; i < Tetra::N; ++i) // node of tetrahedron tetIdx
258  {
259  if (tetrahedron.ind[i] == nodeIdx)
260  { return true; }
261  }
262  return false;
263  });
264  if (node_in_region)
265  { nodeRegions.insert(regIdx); }
266  }
267 
268  // Get the list of region names this node belongs to.
269  std::vector<std::string> region_names;
270  region_names.resize(nodeRegions.size());
271  std::transform(nodeRegions.begin(), nodeRegions.end(), region_names.begin(),
272  [this](const int regIdx){ return settings.paramTetra[regIdx].regName; });
273 
274  n.d[Nodes::CURRENT].u = settings.getMagnetization(n.p, region_names);
275  n.d[Nodes::NEXT].u = n.d[Nodes::CURRENT].u;
276  }
277  }
278 
285  double thiele(const int region ) const;
286 
290  double avg(const std::function<double(Nodes::Node, Nodes::index)>& getter ,
291  Nodes::index d ,
292  int region = -1 ) const;
293 
295  double max_angle() const
296  {
297  double min_dot_product = std::transform_reduce(EXEC_POL, edges.begin(), edges.end(), 1.0,
298  [](const double a, const double b){ return std::min(a, b); },
299  [this](const Edge &edge)
300  {
301  Eigen::Vector3d m1 = getNode_u(edge.first);
302  Eigen::Vector3d m2 = getNode_u(edge.second);
303  return m1.dot(m2);
304  });
305  return std::acos(min_dot_product);
306  }
307 
314  void savesol(const int precision ,
315  const std::string& fileName ,
316  const std::string &metadata ,
317  bool withSpinAcc ,
318  std::vector<Eigen::Vector3d> &s ) const;
319 
322  inline void set(const int i ,
323  const std::function<void(Nodes::Node &, const double)>& what_to_set ,
324  const double val )
325  { what_to_set(node[i], val); }
326 
328  inline int getNodeIndex(const int i) const { return node_index[i]; }
329 
331  inline bool isMagnetic(const Tetra::Tet &t) const
332  { return (settings.paramTetra[t.idxPrm].Ms > 0); }
333 
335  inline bool isMagnetic(const Triangle::Tri &f)
336  { return (magNode[f.ind[0]] && magNode[f.ind[1]] && magNode[f.ind[2]]); }
337 
349  bool controlTriangles();
350 
353  std::vector<Edge> edges;
354 
358  std::vector<bool> magNode;
359 
361  std::vector<int> magTet;
362 
367  std::vector<int> magTri;
368 
370  std::vector< Eigen::Matrix<double,Nodes::DIM,Tetra::NPI> > extSpaceField;
371 
372 private:
375 
378  std::vector<Nodes::Node> node;
379 
385  std::vector<int> node_index;
386 
388  std::vector<std::vector<int>> volumeRegions;
389 
392  void checkMeshFile();
393 
395  void readNodes();
396 
398  void readTetraedrons();
399 
401  void readTriangles();
402 
404  void readMesh();
405 
407  double doOnNodes(const double init_val ,
408  const Nodes::index coord ,
409  const std::function<bool(double, double)>& whatToDo ) const;
410 
412  inline double minNodes(const Nodes::index coord ) const
413  {
414  return doOnNodes(__DBL_MAX__, coord, [](const double a, const double b)
415  { return a < b; });
416  }
417 
419  inline double maxNodes(const Nodes::index coord ) const
420  {
421  return doOnNodes(__DBL_MIN__, coord, [](const double a, const double b)
422  { return a > b; });
423  }
424 
427  void sortNodes(Nodes::index long_axis );
428 
431  bool diffTriHandler(const size_t inf, const size_t sup, const std::vector<BasicTri> &allTriCtnr,
432  std::vector<size_t> &indNodTriToAdd, std::vector<std::pair<int,int>> &regsTriToAdd);
433 
435  bool assertNoErrInTriangle(const int nbSurfTri, const int nbTetraFaces,
436  const std::pair<int,int> pairIdVolRegs, const int idSurfReg);
437 
441  double surface(std::vector<int> &triIndices) const;
442 
445  void setExtSpaceField();
446  }; // end class mesh
447  } // end namespace Mesh
448 #endif
Definition: mesh.cpp:14
Definition: mesh.h:34
const Eigen::Vector3d getNode_p(const int i) const
Definition: mesh.h:153
Eigen::Vector3d l
Definition: mesh.h:199
std::vector< int > magTet
Definition: mesh.h:361
std::vector< Eigen::Matrix< double, Nodes::DIM, Tetra::NPI > > extSpaceField
Definition: mesh.h:370
bool isMagnetic(const Tetra::Tet &t) const
Definition: mesh.h:331
const Eigen::Vector3d getNode_u(const int i) const
Definition: mesh.h:156
Eigen::Vector3d c
Definition: mesh.h:196
void init_distrib()
Definition: mesh.h:221
void set_node_zero_v(const int i)
Definition: mesh.h:172
void set_node_u0(const int i, const Eigen::Vector3d &val)
Definition: mesh.h:168
void readMesh()
Definition: read.cpp:12
double max_angle() const
Definition: mesh.h:295
std::vector< Edge > edges
Definition: mesh.h:353
int getNbTris(void) const
Definition: mesh.h:147
int getNbNodes(void) const
Definition: mesh.h:144
std::vector< Nodes::Node > node
Definition: mesh.h:378
double minNodes(const Nodes::index coord) const
Definition: mesh.h:412
std::vector< bool > magNode
Definition: mesh.h:358
mesh(Settings &mySets, bool simplified=false)
Definition: mesh.h:38
std::vector< std::vector< int > > volumeRegions
Definition: mesh.h:388
Settings & settings
Definition: mesh.h:374
int getNodeIndex(const int i) const
Definition: mesh.h:328
bool isMagnetic(const Triangle::Tri &f)
Definition: mesh.h:335
double getProj_ep(const int i) const
Definition: mesh.h:162
std::vector< Tetra::Tet > tet
Definition: mesh.h:211
double maxNodes(const Nodes::index coord) const
Definition: mesh.h:419
int getNbTets(void) const
Definition: mesh.h:150
void sortNodes(Nodes::index long_axis)
Definition: mesh.cpp:334
std::vector< int > magTri
Definition: mesh.h:367
void set(const int i, const std::function< void(Nodes::Node &, const double)> &what_to_set, const double val)
Definition: mesh.h:322
mesh(const mesh &)=delete
void setBasis(const double r)
Definition: mesh.h:178
void evolution(void)
Definition: mesh.h:189
double totalMagVol
Definition: mesh.h:205
const Eigen::Vector3d getNode_v(const int i) const
Definition: mesh.h:159
std::vector< int > node_index
Definition: mesh.h:385
double getProj_eq(const int i) const
Definition: mesh.h:165
void updateNode(const int i, const double vp, const double vq, const double dt)
Definition: mesh.h:185
bool controlTriangles()
Definition: mesh.cpp:121
double vol
Definition: mesh.h:202
std::vector< Triangle::Tri > tri
Definition: mesh.h:208
mesh & operator=(const mesh &)=delete
Container for all the settings provided by the user, with conversions to/from YAML.
Definition: settings.h:70
int verbose
Definition: settings.h:136
std::vector< Tetra::prm > paramTetra
Definition: settings.h:208
Definition: tetra.h:132
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
constexpr double a[N][NPI]
Definition: tetra.h:68
header to define struct Node
many settings to give some parameters to the solver, boundary conditions for the problem,...
@ R4toR3
Definition: settings.h:43
@ POSITION_ONLY
M(x, y, z)
Definition: settings.h:26
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:87
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 ...