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  {
22 
27 class mesh
28  {
29 public:
32  inline mesh(Settings &mySets )
33  : paramTetra(mySets.paramTetra)
34  {
35  readMesh(mySets);
36  indexReorder();
37 
38  if (mySets.verbose)
39  { std::cout << " reindexed\n"; }
40 
41  double xmin = minNodes(Nodes::IDX_X);
42  double xmax = maxNodes(Nodes::IDX_X);
43 
44  double ymin = minNodes(Nodes::IDX_Y);
45  double ymax = maxNodes(Nodes::IDX_Y);
46 
47  double zmin = minNodes(Nodes::IDX_Z);
48  double zmax = maxNodes(Nodes::IDX_Z);
49 
50  l = Eigen::Vector3d(xmax - xmin, ymax - ymin, zmax - zmin);
51  c = Eigen::Vector3d(0.5 * (xmax + xmin), 0.5 * (ymax + ymin), 0.5 * (zmax + zmin));
52 
53  // Find the longest axis of the sample.
54  Nodes::index long_axis;
55  if (l.x() > l.y())
56  {
57  if (l.x() > l.z())
58  long_axis = Nodes::IDX_X;
59  else
60  long_axis = Nodes::IDX_Z;
61  }
62  else
63  {
64  if (l.y() > l.z())
65  long_axis = Nodes::IDX_Y;
66  else
67  long_axis = Nodes::IDX_Z;
68  }
69  sortNodes(long_axis);
70 
71  // Compute the per-region volumes and the total volume.
72  std::for_each(tet.begin(), tet.end(),
73  [this](Tetra::Tet const &te)
74  {
75  paramTetra[te.idxPrm].volume += te.calc_vol();
76  });
77  vol = std::transform_reduce(paramTetra.begin(), paramTetra.end(), 0.0,
78  std::plus<>(), [](const Tetra::prm &region){ return region.volume; });
79 
80  // Build the list of all the mesh edges.
81  edges.reserve(tet.size() * 6); // 6 (non unique) edges per tetrahedron
82  std::for_each(tet.begin(), tet.end(),
83  [this](Tetra::Tet const &te)
84  {
85  for (int i = 0; i < 3; ++i)
86  {
87  for (int j = i + 1; j < 4; ++j)
88  { edges.push_back(std::minmax(te.ind[i], te.ind[j])); }
89  }
90  });
91  std::sort(EXEC_POL, edges.begin(), edges.end());
92  auto last = std::unique(EXEC_POL, edges.begin(), edges.end());
93  edges.erase(last, edges.end());
94  edges.shrink_to_fit(); // save memory, as this could be quite big
95  }
96 
98  inline int getNbNodes(void) const { return node.size(); }
99 
101  inline int getNbFacs(void) const { return fac.size(); }
102 
104  inline int getNbTets(void) const { return tet.size(); }
105 
107  inline const Eigen::Vector3d getNode_p(const int i) const { return node[i].p; }
108 
110  inline const Eigen::Vector3d getNode_u(const int i) const { return node[i].get_u(Nodes::NEXT); }
111 
113  inline const Eigen::Vector3d getNode_v(const int i) const { return node[i].get_v(Nodes::NEXT); }
114 
116  inline double getProj_ep(const int i) const {return node[i].proj_ep();}
117 
119  inline double getProj_eq(const int i) const {return node[i].proj_eq();}
120 
122  inline void set_node_u0(const int i, Eigen::Vector3d const &val)
123  { node[i].d[Nodes::CURRENT].u = val; }
124 
126  inline void set_node_zero_v(const int i) { node[i].d[Nodes::NEXT].v.setZero(); }
127 
129  void infos(void) const;
130 
132  inline void setBasis(const double r)
133  {
134  std::for_each(EXEC_POL, node.begin(), node.end(),
135  [&r](Nodes::Node &nod) { nod.setBasis(r); });
136  }
137 
139  inline void updateNode(int i, double vp, double vq, const double dt)
140  { node[i].make_evol(vp*gamma0, vq*gamma0, dt); }
141 
143  inline void evolution(void)
144  {
145  std::for_each(EXEC_POL, node.begin(), node.end(),
146  [](Nodes::Node &nod) { nod.evolution(); });
147  }
148 
150  Eigen::Vector3d c;
151 
153  Eigen::Vector3d l;
154 
156  double vol;
157 
159  std::vector<Facette::Fac> fac;
160 
162  std::vector<Tetra::Tet> tet;
163 
165  std::vector<Tetra::prm> &paramTetra;
166 
170  double readSol(bool VERBOSE ,
171  const std::string fileName );
172 
175  inline void init_distrib(Settings const &mySets )
176  {
177  std::for_each(node.begin(),node.end(),[&mySets](Nodes::Node &n)
178  {
179  n.d[Nodes::CURRENT].u = mySets.getMagnetization(n.p);
180  n.d[Nodes::NEXT].u = n.d[Nodes::CURRENT].u;
181  n.d[Nodes::NEXT].phi = 0.;
182  n.d[Nodes::NEXT].phiv = 0.;
183  } );
184  }
185 
189  double avg(std::function<double(Nodes::Node, Nodes::index)> getter ,
190  Nodes::index d ,
191  int region = -1 ) const;
192 
194  double max_angle() const
195  {
196  double min_dot_product = std::transform_reduce(EXEC_POL, edges.begin(), edges.end(), 1.0,
197  [](double a, double b){ return std::min(a, b); },
198  [this](const std::pair<int, int> edge)
199  {
200  Eigen::Vector3d m1 = getNode_u(edge.first);
201  Eigen::Vector3d m2 = getNode_u(edge.second);
202  return m1.dot(m2);
203  });
204  return std::acos(min_dot_product);
205  }
206 
208  void savesol(const int precision ,
209  const std::string fileName ,
210  std::string const &metadata ) const;
211 
214  bool savesol(const int precision ,
215  const std::string fileName ,
216  std::string const &metadata ,
217  std::vector<double> const &val ) const;
218 
221  inline void set(const int i ,
222  std::function<void(Nodes::Node &, const double)> what_to_set ,
223  const double val )
224  { what_to_set(node[i], val); }
225 
226 private:
229  std::vector<Nodes::Node> node;
230 
234  std::vector<std::pair<int, int>> edges;
235 
241  std::vector<int> node_index;
242 
244  void checkMeshFile(Settings const &mySets );
245 
247  void readNodes(Settings const &mySets );
248 
250  void readTetraedrons(Settings const &mySets );
251 
253  void readTriangles(Settings const &mySets );
254 
256  void readMesh(Settings const &mySets );
257 
259  double doOnNodes(const double init_val ,
260  const Nodes::index coord ,
261  std::function<bool(double, double)> whatToDo ) const;
262 
264  inline double minNodes(const Nodes::index coord ) const
265  {
266  return doOnNodes(__DBL_MAX__, coord, [](double a, double b) { return a < b; });
267  }
268 
270  inline double maxNodes(const Nodes::index coord ) const
271  {
272  return doOnNodes(__DBL_MIN__, coord, [](double a, double b) { return a > b; });
273  }
274 
298  void indexReorder();
299 
302  void sortNodes(Nodes::index long_axis );
303 
304  }; // end class mesh
305 
306  } // end namespace Mesh
307 #endif
Definition: mesh.h:28
const Eigen::Vector3d getNode_p(const int i) const
Definition: mesh.h:107
void indexReorder()
Definition: mesh.cpp:46
Eigen::Vector3d l
Definition: mesh.h:153
const Eigen::Vector3d getNode_u(const int i) const
Definition: mesh.h:110
void readMesh(Settings const &mySets)
Definition: read.cpp:12
int getNbFacs(void) const
Definition: mesh.h:101
Eigen::Vector3d c
Definition: mesh.h:150
mesh(Settings &mySets)
Definition: mesh.h:32
void set_node_zero_v(const int i)
Definition: mesh.h:126
void init_distrib(Settings const &mySets)
Definition: mesh.h:175
double max_angle() const
Definition: mesh.h:194
int getNbNodes(void) const
Definition: mesh.h:98
std::vector< Nodes::Node > node
Definition: mesh.h:229
double minNodes(const Nodes::index coord) const
Definition: mesh.h:264
double getProj_ep(const int i) const
Definition: mesh.h:116
std::vector< Tetra::Tet > tet
Definition: mesh.h:162
double maxNodes(const Nodes::index coord) const
Definition: mesh.h:270
int getNbTets(void) const
Definition: mesh.h:104
void set_node_u0(const int i, Eigen::Vector3d const &val)
Definition: mesh.h:122
void sortNodes(Nodes::index long_axis)
Definition: mesh.cpp:95
std::vector< Facette::Fac > fac
Definition: mesh.h:159
void setBasis(const double r)
Definition: mesh.h:132
void evolution(void)
Definition: mesh.h:143
const Eigen::Vector3d getNode_v(const int i) const
Definition: mesh.h:113
std::vector< std::pair< int, int > > edges
Definition: mesh.h:234
std::vector< int > node_index
Definition: mesh.h:241
std::vector< Tetra::prm > & paramTetra
Definition: mesh.h:165
void updateNode(int i, double vp, double vq, const double dt)
Definition: mesh.h:139
double getProj_eq(const int i) const
Definition: mesh.h:119
double vol
Definition: mesh.h:156
void set(const int i, std::function< void(Nodes::Node &, const double)> what_to_set, const double val)
Definition: mesh.h:221
Container for all the settings provided by the user, with conversions to/from YAML.
Definition: feellgoodSettings.h:63
int verbose
Definition: feellgoodSettings.h:125
Definition: tetra.h:126
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,...
constexpr double a[N][NPI]
Definition: facette.h:28
index
Definition: node.h:34
header to define struct Node
Definition: node.h:61
Definition: tetra.h:64
namespace Tetra header containing Tet class, some constants, and integrales