accu.template
Loading...
Searching...
No Matches
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
13class Mesh
14{
15public:
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
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 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
315
316protected:
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
403private:
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:
421protected:
422 int _nelem;
425 int _nnode;
426 int _ndim;
427 std::vector<int> _ia;
428 std::vector<double> _xc;
429
430protected:
431 // B.C.
432 std::vector<int> _bedges;
433// 2020-01-08
434 std::vector<int> _sdedges;
435
436 //private:
437protected:
438 // edge based connectivity
439 int _nedge;
441 std::vector<int> _edges;
442 std::vector<int> _ea;
443 // B.C.
444 std::vector<int> _ebedges;
445
446private:
447 const std::vector<int> _dummy;
448
449};
450
451
452// *********************************************************************
453
454class RefinedMesh: public Mesh
455{
456public:
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
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
521protected:
526 bool Check_array_dimensions() const override;
527
533 void PermuteVertices_EdgeBased(std::vector<int> const &old2new);
534
535
536private:
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{
548public:
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
598private:
599 std::vector<std::shared_ptr<Mesh>> _gmesh;
600
601};
602
603
604
605// *********************************************************************
610{
611public:
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
668private:
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
692private:
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
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
void SetU(std::vector< double > &u) const
Definition geom.cpp:1141
int _procx
number of MPI ranks in x-direction
Definition geom.h:694
~Mesh_2d_3_square() override
Definition geom.cpp:1137
double _xl
x coordinate of lower left corner of square
Definition geom.h:699
std::array< int, 4 > _neigh
MPI ranks of neighbors (negative: no neighbor but b.c.)
Definition geom.h:696
void GetCoordsInRectangle(int nx, int ny, double xl, double xr, double yb, double yt, double xc[])
Definition geom.cpp:1234
std::vector< int > Index_DirichletNodes() const override
Definition geom.cpp:1170
double _xr
x coordinate of lower right corner of square
Definition geom.h:700
double _yt
y coordinate of upper right corner of square
Definition geom.h:702
void SaveVectorP(std::string const &name, std::vector< double > const &u) const
Definition geom.cpp:1209
int _myid
my MPI rank
Definition geom.h:693
int _color
red/black coloring (checker board) of subdomains
Definition geom.h:697
void SetF(std::vector< double > &f) const
Definition geom.cpp:1155
std::vector< int > Index_BoundaryNodes() const override
Definition geom.cpp:1200
void GetConnectivityInRectangle(int nx, int ny, int ia[])
Definition geom.cpp:1255
int _procy
number of MPI ranks in y-direction
Definition geom.h:695
int _nx
number of intervals in x-direction
Definition geom.h:703
int _ny
number of intervals in y-direction
Definition geom.h:704
double _yb
y coordinate or lower left corner of square
Definition geom.h:701
Definition geom.h:14
const std::vector< double > & GetCoords() const
Definition geom.h:151
void DeriveEdgeFromVertexBased_fast()
Definition geom.cpp:413
void Resize_Connectivity(int nelem, int nvert_e)
Definition geom.h:109
void Debug() const
Definition geom.cpp:65
std::vector< int > _ea
edge based element connectivity
Definition geom.h:442
int _nedge_e
number of edges per element
Definition geom.h:440
void Resize_Coords(int nnodes, int ndim)
Definition geom.h:140
const std::vector< int > & GetEdges() const
Definition geom.h:263
std::vector< int > _sdedges
boundary edges [nbedges][2] with left/right subdomain number
Definition geom.h:434
std::vector< int > _ia
element connectivity
Definition geom.h:427
void DeriveEdgeFromVertexBased()
Definition geom.h:362
std::vector< double > & GetCoords()
Definition geom.h:160
int _ndof_e
degrees of freedom (d.o.f.) per element
Definition geom.h:424
void Export_scicomp(std::string const &basename) const
Definition geom.cpp:174
std::vector< double > _xc
coordinates
Definition geom.h:428
std::vector< int > & GetEdgeConnectivity()
Definition geom.h:254
void ReadVectexBasedMesh(std::string const &fname)
void SetNedge(int nedge)
Definition geom.h:343
int Ndims() const
Definition geom.h:98
std::vector< std::vector< int > > Node2NodeGraph_2() const
Definition geom.cpp:632
int _nelem
number elements
Definition geom.h:422
void SetNdofsElement(int ndof)
Definition geom.h:328
virtual std::vector< int > Index_DirichletNodes() const
Definition geom.cpp:261
virtual ~Mesh()
Definition geom.cpp:30
virtual bool Check_array_dimensions() const
Definition geom.cpp:783
const std::vector< int > & GetEdgeConnectivity() const
Definition geom.h:245
std::vector< int > & GetEdges()
Definition geom.h:272
int _ndim
space dimension of the problem (1, 2, or 3)
Definition geom.h:426
std::vector< int > _ebedges
boundary edges [nbedges]
Definition geom.h:444
int _nnode
number nodes/vertices
Definition geom.h:425
int Nelems() const
Definition geom.h:62
void DeriveVertexFromEdgeBased()
Definition geom.cpp:588
void Write_ascii_matlab(std::string const &fname, std::vector< double > const &v) const
Definition geom.cpp:124
int Nnodes() const
Definition geom.h:89
int Nnbedges() const
Definition geom.h:385
int _nedge
number of edges in mesh
Definition geom.h:439
std::vector< int > _bedges
boundary edges [nbedges][2] storing start/end vertex
Definition geom.h:432
void DeriveEdgeFromVertexBased_fast_2()
Definition geom.cpp:299
void SetNdim(int ndim)
Definition geom.h:338
int NverticesElements() const
Definition geom.h:71
std::vector< std::vector< int > > Node2NodeGraph() const
Definition geom.h:282
__attribute__((noinline)) Mesh(Mesh const &)=default
int NedgesElements() const
Definition geom.h:236
Mesh & operator=(Mesh const &)=delete
std::vector< int > & GetConnectivity()
Definition geom.h:129
void PermuteVertices_EdgeBased(std::vector< int > const &old2new)
Definition geom.cpp:1034
void SetNnode(int nnode)
Definition geom.h:333
void Del_EdgeConnectivity()
Definition geom.cpp:801
void ReadVertexBasedMesh(std::string const &fname)
Definition geom.cpp:713
void DeriveEdgeFromVertexBased_slow()
Definition geom.cpp:515
void DebugEdgeBased() const
Definition geom.cpp:91
virtual std::vector< int > const & GetFathersOfVertices() const
Definition geom.h:306
int Nedges() const
Definition geom.h:227
void SetDirchletValues(std::vector< double > &v, const std::function< double(double, double)> &func) const
Definition geom.cpp:53
void SetNelem(int nelem)
Definition geom.h:318
int NdofsElement() const
Definition geom.h:80
void SetBoundaryValues(std::vector< double > &v, const std::function< double(double, double)> &func) const
Definition geom.cpp:43
const std::vector< int > _dummy
empty dummy vector
Definition geom.h:447
std::vector< int > _edges
edges of mesh (vertices ordered ascending)
Definition geom.h:441
virtual std::vector< int > Index_BoundaryNodes() const
Definition geom.cpp:287
void SetNverticesElement(int nvert)
Definition geom.h:323
const std::vector< int > & GetConnectivity() const
Definition geom.h:120
std::vector< std::vector< int > > Node2NodeGraph_1() const
Definition geom.cpp:673
void SetValues(std::vector< double > &v, const std::function< double(double, double)> &func) const
Definition geom.cpp:33
void Visualize(std::vector< double > const &v) const
Definition geom.cpp:240
int _nvert_e
number of vertices per element
Definition geom.h:423
bool Check_array_dimensions() const override
Definition geom.cpp:1070
virtual ~RefinedMesh() override
Definition geom.cpp:833
RefinedMesh(Mesh const &cmesh)
Definition geom.h:476
std::vector< bool > const _ibref
refinement info
Definition geom.h:538
void PermuteVertices_EdgeBased(std::vector< int > const &old2new)
Definition geom.cpp:1056
Mesh RefineElements(std::vector< bool > const &ibref)
Definition geom.cpp:836
std::vector< int > _vfathers
stores the 2 fathers of each vertex (equal fathers denote original coarse vertex)
Definition geom.h:540
void RefineAllElements(int nref=1)
Definition geom.cpp:862
std::vector< int > const & GetFathersOfVertices() const override
Definition geom.h:516
RefinedMesh & operator=(RefinedMesh const &)=delete
RefinedMesh(RefinedMesh const &)=delete
int _nref
number of regular refinements performed
Definition geom.h:539
Mesh const & operator[](int lev) const
Definition geom.h:571
Mesh const & coarsest() const
Definition geom.h:593
std::vector< std::shared_ptr< Mesh > > _gmesh
mesh hierarchy from coarse ([0]) to fine.
Definition geom.h:599
Mesh const & finest() const
Definition geom.h:582
size_t size() const
Definition geom.h:559