jacobi.template
geom.h
Go to the documentation of this file.
1 #ifndef GEOM_FILE
2 #define GEOM_FILE
3 #include <array>
4 #include <functional> // function; C++11
5 #include <iostream>
6 #include <memory> // shared_ptr
7 #include <string>
8 #include <vector>
9 
13 class Mesh
14 {
15 public:
24  explicit Mesh(int ndim, int nvert_e = 0, int ndof_e = 0, int nedge_e = 0);
25 
26  __attribute__((noinline))
27  Mesh(Mesh const &) = default;
28 
29  Mesh &operator=(Mesh const &) = delete;
30 
31 
38  virtual ~Mesh();
39 
47  explicit Mesh(std::string const &fname);
48 
56  void ReadVertexBasedMesh(std::string const &fname);
57 
62  int Nelems() const
63  {
64  return _nelem;
65  }
66 
71  int NverticesElements() const
72  {
73  return _nvert_e;
74  }
75 
80  int NdofsElement() const
81  {
82  return _ndof_e;
83  }
84 
89  int Nnodes() const
90  {
91  return _nnode;
92  }
93 
98  int Ndims() const
99  {
100  return _ndim;
101  }
102 
110  {
111  SetNelem(nelem); // number of elements
112  SetNverticesElement(nvert_e); // vertices per element
113  _ia.resize(nelem * nvert_e);
114  }
115 
120  const std::vector<int> &GetConnectivity() const
121  {
122  return _ia;
123  }
124 
129  std::vector<int> &GetConnectivity()
130  {
131  return _ia;
132  }
133 
140  void Resize_Coords(int nnodes, int ndim)
141  {
142  SetNnode(nnodes); // number of nodes
143  SetNdim(ndim); // space dimension
144  _xc.resize(nnodes * ndim);
145  }
146 
151  const std::vector<double> &GetCoords() const
152  {
153  return _xc;
154  }
155 
160  std::vector<double> &GetCoords()
161  {
162  return _xc;
163  }
164 
170  void SetValues(std::vector<double> &v, const std::function<double(double, double)> &func) const;
171  void SetBoundaryValues(std::vector<double> &v, const std::function<double(double, double)> &func) const;
172  void SetDirchletValues(std::vector<double> &v, const std::function<double(double, double)> &func) const;
173 
177  void Debug() const;
178 
182  void DebugEdgeBased() const;
183 
188  virtual std::vector<int> Index_DirichletNodes() const;
189  virtual std::vector<int> Index_BoundaryNodes() const;
190 
199  void Write_ascii_matlab(std::string const &fname, std::vector<double> const &v) const;
200 
208  void Export_scicomp(std::string const &basename) const;
209 
221  virtual void Visualize(std::vector<double> const &v) const;
222 
227  int Nedges() const
228  {
229  return _nedge;
230  }
231 
236  int NedgesElements() const
237  {
238  return _nedge_e;
239  }
240 
245  const std::vector<int> &GetEdgeConnectivity() const
246  {
247  return _ea;
248  }
249 
254  std::vector<int> &GetEdgeConnectivity()
255  {
256  return _ea;
257  }
258 
263  const std::vector<int> &GetEdges() const
264  {
265  return _edges;
266  }
267 
272  std::vector<int> &GetEdges()
273  {
274  return _edges;
275  }
276 
282  std::vector<std::vector<int>> Node2NodeGraph() const
283  {
285  //auto v1=Node2NodeGraph_1();
286  //auto v2=Node2NodeGraph_2();
287  //if ( equal(v1.cbegin(),v1.cend(),v2.begin()) )
288  //{
289  //std::cout << "\nidentical Versions\n";
290  //}
291  //else
292  //{
293  //std::cout << "\nE R R O R in Versions\n";
294  //}
295 
296  //return Node2NodeGraph_1();
297  return Node2NodeGraph_2(); // 2 times faster than version 1
298  }
299 
306  virtual std::vector<int> const &GetFathersOfVertices() const
307  {
308  return _dummy;
309  }
310 
314  void Del_EdgeConnectivity();
315 
316 protected:
317  //public:
318  void SetNelem(int nelem)
319  {
320  _nelem = nelem;
321  }
322 
324  {
325  _nvert_e = nvert;
326  }
327 
328  void SetNdofsElement(int ndof)
329  {
330  _ndof_e = ndof;
331  }
332 
333  void SetNnode(int nnode)
334  {
335  _nnode = nnode;
336  }
337 
338  void SetNdim(int ndim)
339  {
340  _ndim = ndim;
341  }
342 
343  void SetNedge(int nedge)
344  {
345  _nedge = nedge;
346  }
347 
355  void ReadVectexBasedMesh(std::string const &fname);
356 
363  {
364  //DeriveEdgeFromVertexBased_slow();
365  //DeriveEdgeFromVertexBased_fast();
367  }
371 
372 
373 
380 
385  int Nnbedges() const
386  {
387  return static_cast<int>(_bedges.size());
388  }
389 
394  virtual bool Check_array_dimensions() const;
395 
401  void PermuteVertices_EdgeBased(std::vector<int> const &old2new);
402 
403 private:
409  std::vector<std::vector<int>> Node2NodeGraph_1() const; // is correct
410 
418  std::vector<std::vector<int>> Node2NodeGraph_2() const; // is correct
419 
420  //private:
421 protected:
422  int _nelem;
423  int _nvert_e;
424  int _ndof_e;
425  int _nnode;
426  int _ndim;
427  std::vector<int> _ia;
428  std::vector<double> _xc;
429 
430 protected:
431  // B.C.
432  std::vector<int> _bedges;
433 // 2020-01-08
434  std::vector<int> _sdedges;
435 
436  //private:
437 protected:
438  // edge based connectivity
439  int _nedge;
440  int _nedge_e;
441  std::vector<int> _edges;
442  std::vector<int> _ea;
443  // B.C.
444  std::vector<int> _ebedges;
445 
446 private:
447  const std::vector<int> _dummy;
448 
449 };
450 
451 
452 // *********************************************************************
453 
454 class RefinedMesh: public Mesh
455 {
456 public:
466  //explicit RefinedMesh(Mesh const &cmesh, std::vector<bool> const &ibref = std::vector<bool>(0));
467  RefinedMesh(Mesh const &cmesh, std::vector<bool> const &ibref);
468  //RefinedMesh(Mesh const &cmesh, std::vector<bool> const &ibref);
469 
476  explicit RefinedMesh(Mesh const &cmesh)
477  : RefinedMesh(cmesh, std::vector<bool>(0))
478  {}
479 
480 
481  RefinedMesh(RefinedMesh const &) = delete;
482  //RefinedMesh(RefinedMesh const&&) = delete;
483 
484  RefinedMesh &operator=(RefinedMesh const &) = delete;
485  //RefinedMesh& operator=(RefinedMesh const&&) = delete;
486 
490  virtual ~RefinedMesh() override;
491 
500  Mesh RefineElements(std::vector<bool> const &ibref);
501 
508  void RefineAllElements(int nref = 1);
509 
516  std::vector<int> const &GetFathersOfVertices() const override
517  {
518  return _vfathers;
519  }
520 
521 protected:
526  bool Check_array_dimensions() const override;
527 
533  void PermuteVertices_EdgeBased(std::vector<int> const &old2new);
534 
535 
536 private:
537  //Mesh const & _cmesh; //!< coarse mesh
538  std::vector<bool> const _ibref;
539  int _nref;
540  std::vector<int> _vfathers;
541 
542 };
543 
544 // *********************************************************************
545 
547 {
548 public:
557  gMesh_Hierarchy(Mesh const &cmesh, int nlevel);
558 
559  size_t size() const
560  {
561  return _gmesh.size();
562  }
563 
571  Mesh const &operator[](int lev) const
572  {
573  return *_gmesh.at(lev);
574  }
575 
582  Mesh const &finest() const
583  {
584  return *_gmesh.back();
585  }
586 
593  Mesh const &coarsest() const
594  {
595  return *_gmesh.front();
596  }
597 
598 private:
599  std::vector<std::shared_ptr<Mesh>> _gmesh;
600 
601 };
602 
603 
604 
605 // *********************************************************************
609 class Mesh_2d_3_square: public Mesh
610 {
611 public:
621  Mesh_2d_3_square(int nx, int ny, int myid = 0, int procx = 1, int procy = 1);
622 
626  ~Mesh_2d_3_square() override;
627 
632  void SetU(std::vector<double> &u) const;
633 
638  void SetF(std::vector<double> &f) const;
639 
644  std::vector<int> Index_DirichletNodes() const override;
645  std::vector<int> Index_BoundaryNodes() const override;
646 
662  void SaveVectorP(std::string const &name, std::vector<double> const &u) const;
663 
664  // here will still need to implement in the class
665  // GetBound(), AddBound()
666  // or better a generalized way with indices and their appropriate ranks for MPI communication
667 
668 private:
681  void GetCoordsInRectangle(int nx, int ny, double xl, double xr, double yb, double yt,
682  double xc[]);
690  void GetConnectivityInRectangle(int nx, int ny, int ia[]);
691 
692 private:
693  int _myid;
694  int _procx;
695  int _procy;
696  std::array<int, 4> _neigh;
697  int _color;
698 
699  double _xl;
700  double _xr;
701  double _yb;
702  double _yt;
703  int _nx;
704  int _ny;
705 };
706 
707 // *********************************************************************
708 
709 
710 
711 
712 #endif
Mesh::Write_ascii_matlab
void Write_ascii_matlab(std::string const &fname, std::vector< double > const &v) const
Definition: geom.cpp:123
Mesh::Export_scicomp
void Export_scicomp(std::string const &basename) const
Definition: geom.cpp:173
Mesh::DeriveVertexFromEdgeBased
void DeriveVertexFromEdgeBased()
Definition: geom.cpp:587
Mesh::NedgesElements
int NedgesElements() const
Definition: geom.h:236
Mesh::SetNelem
void SetNelem(int nelem)
Definition: geom.h:318
Mesh::_nedge
int _nedge
number of edges in mesh
Definition: geom.h:439
Mesh::SetBoundaryValues
void SetBoundaryValues(std::vector< double > &v, const std::function< double(double, double)> &func) const
Definition: geom.cpp:42
xc
xc
Definition: ascii_read_meshvector.m:30
Mesh::_ea
std::vector< int > _ea
edge based element connectivity
Definition: geom.h:442
Mesh::_edges
std::vector< int > _edges
edges of mesh (vertices ordered ascending)
Definition: geom.h:441
gMesh_Hierarchy
Definition: geom.h:546
Mesh::Node2NodeGraph_2
std::vector< std::vector< int > > Node2NodeGraph_2() const
Definition: geom.cpp:631
function
function[xc, ia, v]
Definition: ascii_read_meshvector.m:1
Mesh::Ndims
int Ndims() const
Definition: geom.h:98
Mesh::operator=
Mesh & operator=(Mesh const &)=delete
Mesh::DeriveEdgeFromVertexBased
void DeriveEdgeFromVertexBased()
Definition: geom.h:362
Mesh::_bedges
std::vector< int > _bedges
boundary edges [nbedges][2] storing start/end vertex
Definition: geom.h:432
Mesh::Resize_Coords
void Resize_Coords(int nnodes, int ndim)
Definition: geom.h:140
Mesh::Index_BoundaryNodes
virtual std::vector< int > Index_BoundaryNodes() const
Definition: geom.cpp:286
Mesh::Resize_Connectivity
void Resize_Connectivity(int nelem, int nvert_e)
Definition: geom.h:109
Mesh::Node2NodeGraph
std::vector< std::vector< int > > Node2NodeGraph() const
Definition: geom.h:282
RefinedMesh::operator=
RefinedMesh & operator=(RefinedMesh const &)=delete
Mesh::ReadVertexBasedMesh
void ReadVertexBasedMesh(std::string const &fname)
Definition: geom.cpp:712
Mesh::_ebedges
std::vector< int > _ebedges
boundary edges [nbedges]
Definition: geom.h:444
Mesh_2d_3_square
Definition: geom.h:609
Mesh::PermuteVertices_EdgeBased
void PermuteVertices_EdgeBased(std::vector< int > const &old2new)
Definition: geom.cpp:1033
RefinedMesh::_nref
int _nref
number of regular refinements performed
Definition: geom.h:539
Mesh::GetConnectivity
const std::vector< int > & GetConnectivity() const
Definition: geom.h:120
fname
end fname
Definition: visualize_par_results.m:44
Mesh::Del_EdgeConnectivity
void Del_EdgeConnectivity()
Definition: geom.cpp:800
Mesh::_sdedges
std::vector< int > _sdedges
boundary edges [nbedges][2] with left/right subdomain number
Definition: geom.h:434
RefinedMesh::_ibref
const std::vector< bool > _ibref
refinement info
Definition: geom.h:538
Mesh::SetNverticesElement
void SetNverticesElement(int nvert)
Definition: geom.h:323
RefinedMesh::RefineAllElements
void RefineAllElements(int nref=1)
Definition: geom.cpp:861
Mesh::GetCoords
std::vector< double > & GetCoords()
Definition: geom.h:160
gMesh_Hierarchy::finest
const Mesh & finest() const
Definition: geom.h:582
Mesh_2d_3_square::_procy
int _procy
number of MPI ranks in y-direction
Definition: geom.h:695
Mesh::Nedges
int Nedges() const
Definition: geom.h:227
Mesh
Definition: geom.h:13
Mesh::_dummy
const std::vector< int > _dummy
empty dummy vector
Definition: geom.h:447
Mesh::_nedge_e
int _nedge_e
number of edges per element
Definition: geom.h:440
Mesh_2d_3_square::_procx
int _procx
number of MPI ranks in x-direction
Definition: geom.h:694
Mesh::SetValues
void SetValues(std::vector< double > &v, const std::function< double(double, double)> &func) const
Definition: geom.cpp:32
Mesh::_ia
std::vector< int > _ia
element connectivity
Definition: geom.h:427
Mesh::Node2NodeGraph_1
std::vector< std::vector< int > > Node2NodeGraph_1() const
Definition: geom.cpp:672
Mesh::DeriveEdgeFromVertexBased_slow
void DeriveEdgeFromVertexBased_slow()
Definition: geom.cpp:514
Mesh_2d_3_square::Mesh_2d_3_square
Mesh_2d_3_square(int nx, int ny, int myid=0, int procx=1, int procy=1)
Definition: geom.cpp:1099
Mesh_2d_3_square::_neigh
std::array< int, 4 > _neigh
MPI ranks of neighbors (negative: no neighbor but b.c.)
Definition: geom.h:696
Mesh_2d_3_square::Index_BoundaryNodes
std::vector< int > Index_BoundaryNodes() const override
Definition: geom.cpp:1199
Mesh::SetDirchletValues
void SetDirchletValues(std::vector< double > &v, const std::function< double(double, double)> &func) const
Definition: geom.cpp:52
Mesh::GetEdges
std::vector< int > & GetEdges()
Definition: geom.h:272
Mesh::Mesh
Mesh(int ndim, int nvert_e=0, int ndof_e=0, int nedge_e=0)
Definition: geom.cpp:21
Mesh::Nnodes
int Nnodes() const
Definition: geom.h:89
u
u
Definition: laplacian.m:3
v
v
Definition: ascii_read_meshvector.m:40
Mesh::_ndim
int _ndim
space dimension of the problem (1, 2, or 3)
Definition: geom.h:426
Mesh_2d_3_square::~Mesh_2d_3_square
~Mesh_2d_3_square() override
Definition: geom.cpp:1136
Mesh::Nnbedges
int Nnbedges() const
Definition: geom.h:385
Mesh_2d_3_square::_nx
int _nx
number of intervals in x-direction
Definition: geom.h:703
Mesh_2d_3_square::SetU
void SetU(std::vector< double > &u) const
Definition: geom.cpp:1140
RefinedMesh::RefineElements
Mesh RefineElements(std::vector< bool > const &ibref)
Definition: geom.cpp:835
Mesh::GetFathersOfVertices
virtual const std::vector< int > & GetFathersOfVertices() const
Definition: geom.h:306
Mesh::GetEdges
const std::vector< int > & GetEdges() const
Definition: geom.h:263
Mesh_2d_3_square::_xl
double _xl
x coordinate of lower left corner of square
Definition: geom.h:699
Mesh_2d_3_square::SetF
void SetF(std::vector< double > &f) const
Definition: geom.cpp:1154
gMesh_Hierarchy::coarsest
const Mesh & coarsest() const
Definition: geom.h:593
Mesh::_nvert_e
int _nvert_e
number of vertices per element
Definition: geom.h:423
Mesh_2d_3_square::GetConnectivityInRectangle
void GetConnectivityInRectangle(int nx, int ny, int ia[])
Definition: geom.cpp:1254
RefinedMesh
Definition: geom.h:454
Mesh::Visualize
virtual void Visualize(std::vector< double > const &v) const
Definition: geom.cpp:239
Mesh::SetNedge
void SetNedge(int nedge)
Definition: geom.h:343
Mesh_2d_3_square::_myid
int _myid
my MPI rank
Definition: geom.h:693
Mesh::~Mesh
virtual ~Mesh()
Definition: geom.cpp:29
gMesh_Hierarchy::gMesh_Hierarchy
gMesh_Hierarchy(Mesh const &cmesh, int nlevel)
Definition: geom.cpp:1081
Mesh::__attribute__
__attribute__((noinline)) Mesh(Mesh const &)=default
Mesh_2d_3_square::SaveVectorP
void SaveVectorP(std::string const &name, std::vector< double > const &u) const
Definition: geom.cpp:1208
ndim
ndim
Definition: ascii_read_meshvector.m:23
Mesh::_ndof_e
int _ndof_e
degrees of freedom (d.o.f.) per element
Definition: geom.h:424
ia
ia
Definition: ascii_read_meshvector.m:35
Mesh::DeriveEdgeFromVertexBased_fast
void DeriveEdgeFromVertexBased_fast()
Definition: geom.cpp:412
RefinedMesh::~RefinedMesh
virtual ~RefinedMesh() override
Definition: geom.cpp:832
gMesh_Hierarchy::operator[]
const Mesh & operator[](int lev) const
Definition: geom.h:571
RefinedMesh::GetFathersOfVertices
const std::vector< int > & GetFathersOfVertices() const override
Definition: geom.h:516
gMesh_Hierarchy::size
size_t size() const
Definition: geom.h:559
Mesh::ReadVectexBasedMesh
void ReadVectexBasedMesh(std::string const &fname)
RefinedMesh::Check_array_dimensions
bool Check_array_dimensions() const override
Definition: geom.cpp:1069
nnode
function vertex minimal boundary edge info in an ASCII file Matlab indexing is stored(starts with 1). % % The output file format is compatible with Mesh_2d_3_matlab nnode
Definition: ascii_write_mesh.m:23
Mesh_2d_3_square::_xr
double _xr
x coordinate of lower right corner of square
Definition: geom.h:700
Mesh::_nelem
int _nelem
number elements
Definition: geom.h:422
Mesh::SetNdofsElement
void SetNdofsElement(int ndof)
Definition: geom.h:328
RefinedMesh::RefinedMesh
RefinedMesh(Mesh const &cmesh)
Definition: geom.h:476
Mesh_2d_3_square::Index_DirichletNodes
std::vector< int > Index_DirichletNodes() const override
Definition: geom.cpp:1169
Mesh::GetConnectivity
std::vector< int > & GetConnectivity()
Definition: geom.h:129
RefinedMesh::_vfathers
std::vector< int > _vfathers
stores the 2 fathers of each vertex (equal fathers denote original coarse vertex)
Definition: geom.h:540
Mesh_2d_3_square::GetCoordsInRectangle
void GetCoordsInRectangle(int nx, int ny, double xl, double xr, double yb, double yt, double xc[])
Definition: geom.cpp:1233
Mesh_2d_3_square::_yt
double _yt
y coordinate of upper right corner of square
Definition: geom.h:702
Mesh::SetNnode
void SetNnode(int nnode)
Definition: geom.h:333
Mesh::GetEdgeConnectivity
std::vector< int > & GetEdgeConnectivity()
Definition: geom.h:254
Mesh::Check_array_dimensions
virtual bool Check_array_dimensions() const
Definition: geom.cpp:782
Mesh::GetEdgeConnectivity
const std::vector< int > & GetEdgeConnectivity() const
Definition: geom.h:245
Mesh::NverticesElements
int NverticesElements() const
Definition: geom.h:71
Mesh::SetNdim
void SetNdim(int ndim)
Definition: geom.h:338
gMesh_Hierarchy::_gmesh
std::vector< std::shared_ptr< Mesh > > _gmesh
mesh hierarchy from coarse ([0]) to fine.
Definition: geom.h:599
Mesh_2d_3_square::_color
int _color
red/black coloring (checker board) of subdomains
Definition: geom.h:697
Mesh_2d_3_square::_ny
int _ny
number of intervals in y-direction
Definition: geom.h:704
Mesh_2d_3_square::_yb
double _yb
y coordinate or lower left corner of square
Definition: geom.h:701
nelem
nelem
Definition: ascii_read_meshvector.m:24
Mesh::DeriveEdgeFromVertexBased_fast_2
void DeriveEdgeFromVertexBased_fast_2()
Definition: geom.cpp:298
Mesh::Debug
void Debug() const
Definition: geom.cpp:64
Mesh::Index_DirichletNodes
virtual std::vector< int > Index_DirichletNodes() const
Definition: geom.cpp:260
Mesh::DebugEdgeBased
void DebugEdgeBased() const
Definition: geom.cpp:90
nvert_e
nvert_e
Definition: ascii_write_mesh.m:26
Mesh::_nnode
int _nnode
number nodes/vertices
Definition: geom.h:425
Mesh::_xc
std::vector< double > _xc
coordinates
Definition: geom.h:428
nnodes
nnodes
Definition: visualize_par_results.m:21
RefinedMesh::RefinedMesh
RefinedMesh(Mesh const &cmesh, std::vector< bool > const &ibref)
Definition: geom.cpp:816
Mesh::GetCoords
const std::vector< double > & GetCoords() const
Definition: geom.h:151
RefinedMesh::PermuteVertices_EdgeBased
void PermuteVertices_EdgeBased(std::vector< int > const &old2new)
Definition: geom.cpp:1055
nvert
nvert
Definition: ascii_read_meshvector.m:25
Mesh::Nelems
int Nelems() const
Definition: geom.h:62
Mesh::NdofsElement
int NdofsElement() const
Definition: geom.h:80