Feellgood
mesh.h
Go to the documentation of this file.
1 #ifndef mesh_h
2 #define mesh_h
3 
8 #include <cmath>
9 #include <algorithm>
10 #pragma GCC diagnostic push
11 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
12 #include <execution>
13 #pragma GCC diagnostic pop
14 
15 #include "facette.h"
16 #include "node.h"
17 #include "tetra.h"
18 #include "feellgoodSettings.h"
19 
20 namespace Mesh
21  {
23  using Edge = std::pair<int, int>;
24 
29 class mesh
30  {
31 public:
34  mesh(Settings &mySets )
35  : paramTetra(mySets.paramTetra), volumeRegions(mySets.paramTetra.size())
36  {
37  readMesh(mySets);
38  indexReorder();
39 
40  if (mySets.verbose)
41  { std::cout << " reindexed\n"; }
42 
43  double xmin = minNodes(Nodes::IDX_X);
44  double xmax = maxNodes(Nodes::IDX_X);
45 
46  double ymin = minNodes(Nodes::IDX_Y);
47  double ymax = maxNodes(Nodes::IDX_Y);
48 
49  double zmin = minNodes(Nodes::IDX_Z);
50  double zmax = maxNodes(Nodes::IDX_Z);
51 
52  l = Eigen::Vector3d(xmax - xmin, ymax - ymin, zmax - zmin);
53  c = Eigen::Vector3d(0.5 * (xmax + xmin), 0.5 * (ymax + ymin), 0.5 * (zmax + zmin));
54 
55  // Find the longest axis of the sample.
56  Nodes::index long_axis;
57  if (l.x() > l.y())
58  {
59  if (l.x() > l.z())
60  long_axis = Nodes::IDX_X;
61  else
62  long_axis = Nodes::IDX_Z;
63  }
64  else
65  {
66  if (l.y() > l.z())
67  long_axis = Nodes::IDX_Y;
68  else
69  long_axis = Nodes::IDX_Z;
70  }
71  sortNodes(long_axis);
72 
73  totalMagVol = 0;
74  // Compute the per-region volumes and the total volume.
75  std::for_each(tet.begin(), tet.end(),
76  [this](Tetra::Tet const &te)
77  {
78  double vol_tet = te.calc_vol();
79  paramTetra[te.idxPrm].volume += vol_tet;
80  if(isMagnetic(te))
81  totalMagVol += vol_tet;
82  });
83  vol = std::transform_reduce(paramTetra.begin(), paramTetra.end(), 0.0,
84  std::plus<>(), [](const Tetra::prm &region){ return region.volume; });
85 
86  // Build the list of tetrahedrons for each region.
87  std::for_each(tet.begin(), tet.end(), [this](Tetra::Tet const &te)
88  {
89  volumeRegions[te.idxPrm].push_back(te.idx);
90  });
91 
92  // Build the list of all the mesh edges.
93  edges.reserve(tet.size() * 6); // 6 (non unique) edges per tetrahedron
94  std::for_each(tet.begin(), tet.end(),
95  [this](Tetra::Tet const &te)
96  {
97  for (int i = 0; i < 3; ++i)
98  {
99  for (int j = i + 1; j < 4; ++j)
100  { edges.push_back(std::minmax(te.ind[i], te.ind[j])); }
101  }
102  });
103  std::sort(EXEC_POL, edges.begin(), edges.end());
104  auto last = std::unique(EXEC_POL, edges.begin(), edges.end());
105  edges.erase(last, edges.end());
106  edges.shrink_to_fit(); // save memory, as this could be quite big
107  magNode.resize(node.size());
108  std::fill(magNode.begin(),magNode.end(),false);
109  std::for_each(tet.begin(),tet.end(),[this](Tetra::Tet &te)
110  {
111  if(isMagnetic(te))
112  {
113  for (int i=0;i<4;i++)
114  { magNode[te.ind[i]] = true; }
115  }
116  });
117 
118  for(unsigned int i=0;i<tet.size();i++)
119  {
120  if (isMagnetic(tet[i]))
121  { magTet.push_back(i); }
122  }
123 
124  for(unsigned int i=0;i<fac.size();i++)
125  {
126  if (isMagnetic(fac[i]) && !isInMagList(magFac,fac[i]) )
127  { magFac.push_back(i); }
128  }
129 
130  if(mySets.getFieldType() == R4toR3)
131  { setExtSpaceField(mySets); }
132  }
133 
135  mesh(const mesh &) = delete;
136 
138  mesh &operator=(const mesh &) = delete;
139 
141  inline int getNbNodes(void) const { return node.size(); }
142 
144  inline int getNbFacs(void) const { return fac.size(); }
145 
147  inline int getNbTets(void) const { return tet.size(); }
148 
150  inline const Eigen::Vector3d getNode_p(const int i) const { return node[i].p; }
151 
153  inline const Eigen::Vector3d getNode_u(const int i) const { return node[i].get_u(Nodes::NEXT); }
154 
156  inline const Eigen::Vector3d getNode_v(const int i) const { return node[i].get_v(Nodes::NEXT); }
157 
159  inline double getProj_ep(const int i) const {return node[i].proj_ep();}
160 
162  inline double getProj_eq(const int i) const {return node[i].proj_eq();}
163 
165  inline void set_node_u0(const int i, Eigen::Vector3d const &val)
166  { node[i].d[Nodes::CURRENT].u = val; }
167 
169  inline void set_node_zero_v(const int i) { node[i].d[Nodes::NEXT].v.setZero(); }
170 
172  void infos(void) const;
173 
175  inline void setBasis(const double r)
176  {
177  std::for_each(EXEC_POL, node.begin(), node.end(),
178  [&r](Nodes::Node &nod) { nod.setBasis(r); });
179  }
180 
182  inline void updateNode(int i, double vp, double vq, const double dt)
183  { node[i].make_evol(vp*gamma0, vq*gamma0, dt); }
184 
186  inline void evolution(void)
187  {
188  std::for_each(EXEC_POL, node.begin(), node.end(),
189  [](Nodes::Node &nod) { nod.evolution(); });
190  }
191 
193  Eigen::Vector3d c;
194 
196  Eigen::Vector3d l;
197 
199  double vol;
200 
202  double totalMagVol;
203 
205  std::vector<Facette::Fac> fac;
206 
208  std::vector<Tetra::Tet> tet;
209 
211  std::vector<Tetra::prm> &paramTetra;
212 
216  double readSol(bool VERBOSE ,
217  const std::string fileName );
218 
221  inline void init_distrib(Settings const &mySets )
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 (mySets.getMagType() == POSITION_ONLY)
240  {
241  n.d[Nodes::CURRENT].u = mySets.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](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](int regIdx){ return paramTetra[regIdx].regName; });
273 
274  n.d[Nodes::CURRENT].u = mySets.getMagnetization(n.p, region_names);
275  n.d[Nodes::NEXT].u = n.d[Nodes::CURRENT].u;
276  }
277  }
278 
282  double avg(std::function<double(Nodes::Node, Nodes::index)> getter ,
283  Nodes::index d ,
284  int region = -1 ) const;
285 
287  double max_angle() const
288  {
289  double min_dot_product = std::transform_reduce(EXEC_POL, edges.begin(), edges.end(), 1.0,
290  [](double a, double b){ return std::min(a, b); },
291  [this](const Edge edge)
292  {
293  Eigen::Vector3d m1 = getNode_u(edge.first);
294  Eigen::Vector3d m2 = getNode_u(edge.second);
295  return m1.dot(m2);
296  });
297  return std::acos(min_dot_product);
298  }
299 
306  void savesol(const int precision ,
307  const std::string fileName ,
308  std::string const &metadata ,
309  bool withSpinAcc ,
310  std::vector<Eigen::Vector3d> &s ) const;
311 
314  inline void set(const int i ,
315  std::function<void(Nodes::Node &, const double)> what_to_set ,
316  const double val )
317  { what_to_set(node[i], val); }
318 
320  inline int getNodeIndex(const int i) const { return node_index[i]; }
321 
323  inline bool isMagnetic(const Tetra::Tet &t) { return (paramTetra[t.idxPrm].Ms > 0); }
324 
326  inline bool isMagnetic(const Facette::Fac &f)
327  { return (magNode[f.ind[0]] && magNode[f.ind[1]] && magNode[f.ind[2]]); }
328 
331  std::vector<Edge> edges;
332 
336  std::vector<bool> magNode;
337 
339  std::vector<int> magTet;
340 
342  std::vector<int> magFac;
343 
345  std::vector< Eigen::Matrix<double,Nodes::DIM,Tetra::NPI> > extSpaceField;
346 
347 private:
350  std::vector<Nodes::Node> node;
351 
357  std::vector<int> node_index;
358 
360  std::vector<std::vector<int>> volumeRegions;
361 
363  void checkMeshFile(Settings const &mySets );
364 
366  void readNodes(Settings const &mySets );
367 
369  void readTetraedrons(Settings const &mySets );
370 
372  void readTriangles(Settings const &mySets );
373 
375  void readMesh(Settings const &mySets );
376 
378  double doOnNodes(const double init_val ,
379  const Nodes::index coord ,
380  std::function<bool(double, double)> whatToDo ) const;
381 
383  inline double minNodes(const Nodes::index coord ) const
384  {
385  return doOnNodes(__DBL_MAX__, coord, [](double a, double b) { return a < b; });
386  }
387 
389  inline double maxNodes(const Nodes::index coord ) const
390  {
391  return doOnNodes(__DBL_MIN__, coord, [](double a, double b) { return a > b; });
392  }
393 
417  void indexReorder();
418 
421  void sortNodes(Nodes::index long_axis );
422 
426  double surface(std::vector<int> &facIndices);
427 
429  bool isInMagList(std::vector<int> &idxMagList, Facette::Fac &f)
430  {
431  auto it = std::find_if(idxMagList.begin(),idxMagList.end(),
432  [this,&f](int idx) { return (fac[idx] == f); });
433  return(it != idxMagList.end());
434  }
435 
438  void setExtSpaceField(Settings &s );
439  }; // end class mesh
440  } // end namespace Mesh
441 #endif
Definition: facette.h:105
Definition: mesh.h:30
const Eigen::Vector3d getNode_p(const int i) const
Definition: mesh.h:150
void indexReorder()
Definition: mesh.cpp:48
Eigen::Vector3d l
Definition: mesh.h:196
bool isInMagList(std::vector< int > &idxMagList, Facette::Fac &f)
Definition: mesh.h:429
std::vector< int > magTet
Definition: mesh.h:339
std::vector< Eigen::Matrix< double, Nodes::DIM, Tetra::NPI > > extSpaceField
Definition: mesh.h:345
const Eigen::Vector3d getNode_u(const int i) const
Definition: mesh.h:153
void readMesh(Settings const &mySets)
Definition: read.cpp:12
int getNbFacs(void) const
Definition: mesh.h:144
Eigen::Vector3d c
Definition: mesh.h:193
mesh(Settings &mySets)
Definition: mesh.h:34
void set_node_zero_v(const int i)
Definition: mesh.h:169
void init_distrib(Settings const &mySets)
Definition: mesh.h:221
double max_angle() const
Definition: mesh.h:287
std::vector< Edge > edges
Definition: mesh.h:331
int getNbNodes(void) const
Definition: mesh.h:141
std::vector< Nodes::Node > node
Definition: mesh.h:350
bool isMagnetic(const Tetra::Tet &t)
Definition: mesh.h:323
double minNodes(const Nodes::index coord) const
Definition: mesh.h:383
std::vector< bool > magNode
Definition: mesh.h:336
std::vector< std::vector< int > > volumeRegions
Definition: mesh.h:360
bool isMagnetic(const Facette::Fac &f)
Definition: mesh.h:326
int getNodeIndex(const int i) const
Definition: mesh.h:320
double getProj_ep(const int i) const
Definition: mesh.h:159
std::vector< Tetra::Tet > tet
Definition: mesh.h:208
double maxNodes(const Nodes::index coord) const
Definition: mesh.h:389
int getNbTets(void) const
Definition: mesh.h:147
void set_node_u0(const int i, Eigen::Vector3d const &val)
Definition: mesh.h:165
void sortNodes(Nodes::index long_axis)
Definition: mesh.cpp:97
std::vector< Facette::Fac > fac
Definition: mesh.h:205
mesh(const mesh &)=delete
void setBasis(const double r)
Definition: mesh.h:175
void evolution(void)
Definition: mesh.h:186
std::vector< int > magFac
Definition: mesh.h:342
double totalMagVol
Definition: mesh.h:202
const Eigen::Vector3d getNode_v(const int i) const
Definition: mesh.h:156
std::vector< int > node_index
Definition: mesh.h:357
std::vector< Tetra::prm > & paramTetra
Definition: mesh.h:211
void updateNode(int i, double vp, double vq, const double dt)
Definition: mesh.h:182
double getProj_eq(const int i) const
Definition: mesh.h:162
double vol
Definition: mesh.h:199
mesh & operator=(const mesh &)=delete
void set(const int i, std::function< void(Nodes::Node &, const double)> what_to_set, const double val)
Definition: mesh.h:314
Container for all the settings provided by the user, with conversions to/from YAML.
Definition: feellgoodSettings.h:73
int verbose
Definition: feellgoodSettings.h:135
mag_exprType getMagType() const
Definition: feellgoodSettings.h:268
Eigen::Vector3d getMagnetization(const Eigen::Ref< Eigen::Vector3d > p) const
Definition: feellgoodSettings.h:278
Definition: tetra.h:135
int idxPrm
Definition: element.h:47
std::vector< int > ind
Definition: element.h:44
contains namespace Facette header containing Fac class, and some constants and a less_than operator t...
many settings to give some parameters to the solver, boundary conditions for the problem,...
@ R4toR3
Definition: feellgoodSettings.h:46
@ POSITION_ONLY
M(x, y, z)
Definition: feellgoodSettings.h:29
std::pair< int, int > Edge
Definition: mesh.h:23
constexpr double a[N][NPI]
Definition: facette.h:55
index
Definition: node.h:34
header to define struct Node
Definition: node.h:61
Eigen::Vector3d p
Definition: node.h:62
dataNode d[NB_DATANODE]
Definition: node.h:70
double phi
Definition: node.h:52
double phiv
Definition: node.h:53
Eigen::Vector3d u
Definition: node.h:50
Definition: tetra.h:85
namespace Tetra header containing Tet class, some constants, and integrales