Advanced Multi-Physics (AMP)
On-Line Documentation
BoxMesh.h
Go to the documentation of this file.
1#ifndef included_AMP_BoxMesh
2#define included_AMP_BoxMesh
3
4#include "AMP/geometry/Geometry.h"
5#include "AMP/mesh/Mesh.h"
6#include "AMP/mesh/MeshID.h"
7#include "AMP/mesh/MeshIterator.h"
8#include "AMP/utils/ArraySize.h"
9#include "AMP/utils/Utilities.h"
10
11namespace AMP::LinearAlgebra {
12class Vector;
13}
14
15#include <array>
16#include <map>
17#include <memory>
18#include <vector>
19
20
21namespace AMP::Mesh {
22
23class structuredMeshElement;
24class structuredMeshIterator;
25
26
71{
72public:
78 class Box
79 {
80 public:
91 constexpr explicit Box(
92 int ifirst, int ilast, int jfirst = 0, int jlast = 0, int kfirst = 0, int klast = 0 );
93 constexpr Box();
94 constexpr ArraySize size() const;
95 int first[3];
96 int last[3];
97 std::string print() const;
98
99 private:
100 };
101
109 {
110 public:
112 constexpr MeshElementIndex() = default;
122 constexpr explicit MeshElementIndex(
123 GeomType type, uint8_t side, int x, int y = 0, int z = 0 );
124 constexpr void reset();
125 constexpr void reset( GeomType type, uint8_t side, int x, int y = 0, int z = 0 );
126 constexpr bool isNull() const { return d_side == 255; }
127 constexpr bool operator==( const MeshElementIndex &rhs ) const;
128 constexpr bool operator!=( const MeshElementIndex &rhs ) const;
129 constexpr bool operator>( const MeshElementIndex &rhs ) const;
130 constexpr bool operator>=( const MeshElementIndex &rhs ) const;
131 constexpr bool operator<( const MeshElementIndex &rhs ) const;
132 constexpr bool operator<=( const MeshElementIndex &rhs ) const;
133 constexpr const auto &index() const { return d_index; }
134 constexpr int index( int d ) const { return d_index[d]; }
135 constexpr int &index( int d ) { return d_index[d]; }
136 constexpr int operator[]( int d ) const { return d_index[d]; }
137 constexpr int &operator[]( int d ) { return d_index[d]; }
138 constexpr GeomType type() const { return static_cast<GeomType>( d_type ); }
139 constexpr uint8_t side() const { return d_side; }
140 static constexpr size_t numElements( const MeshElementIndex &first,
141 const MeshElementIndex &last );
142 std::string print() const;
143
144 private:
145 uint8_t d_type = 0;
146 uint8_t d_side = 255;
147 std::array<int, 3> d_index = { 0, 0, 0 };
148 friend class BoxMesh;
150 };
151
158 {
159 public: // iterator_traits
160 using iterator_category = std::random_access_iterator_tag;
162 using difference_type = ptrdiff_t;
163 using pointer = const MeshElementIndex *;
165
166 public:
169 const MeshElementIndex &last,
170 const AMP::Mesh::BoxMesh *mesh,
171 size_t pos = 0 );
180 bool operator==( const MeshElementIndexIterator &rhs ) const;
181 bool operator!=( const MeshElementIndexIterator &rhs ) const;
182 bool empty() const { return d_size == 0; }
183 void set( uint32_t i ) { d_pos = i; }
184 size_t size() const { return d_size; }
185 size_t position() const { return d_pos; }
186 inline const auto &first() const { return d_first; }
187 inline const auto &last() const { return d_last; }
188
189 private:
190 // Data members
191 bool d_checkBoundary = false;
192 std::array<bool, 3> d_isPeriodic = { false, false, false };
193 std::array<int, 3> d_globalSize = { 0, 0, 0 };
194 uint32_t d_pos = { 0 };
195 uint32_t d_size = { 0 };
198 };
199
200
201public:
209 static std::shared_ptr<BoxMesh> generate( std::shared_ptr<const MeshParameters> params );
210
211
213 std::unique_ptr<Mesh> clone() const override = 0;
214
215
224 static size_t estimateMeshSize( std::shared_ptr<const MeshParameters> params );
225
234 static ArraySize estimateLogicalMeshSize( std::shared_ptr<const MeshParameters> params );
235
236
243 static size_t maxProcs( std::shared_ptr<const MeshParameters> params );
244
245
247 virtual ~BoxMesh();
248
249
250 /* Return the number of local element of the given type
251 * \param type Geometric type
252 */
253 size_t numLocalElements( const GeomType type ) const override final;
254
255
256 /* Return the global number of elements of the given type
257 * Note: depending on the mesh this routine may require global communication across the mesh.
258 * \param type Geometric type
259 */
260 size_t numGlobalElements( const GeomType type ) const override final;
261
262
263 /* Return the number of ghost elements of the given type on the current processor
264 * \param type Geometric type
265 */
266 size_t numGhostElements( const GeomType type, const int gcw ) const override final;
267
268
275 MeshIterator getIterator( const GeomType type, const int gcw = 0 ) const override final;
276
277
285 const int gcw = 0 ) const override final;
286
287
293 std::vector<int> getBoundaryIDs() const override final;
294
295
306 const int id,
307 const int gcw = 0 ) const override final;
308
314 std::vector<int> getBlockIDs() const override final;
315
316
324 virtual MeshIterator
325 getBlockIDIterator( const GeomType type, const int id, const int gcw = 0 ) const override final;
326
327
337 MeshElementPtr getElement( const MeshElementID &id ) const override final;
338
339
348 const GeomType type ) const override final;
349
350
352 inline Box getGlobalBox( int gcw = 0 ) const;
353
354
356 inline Box getLocalBox( int gcw = 0 ) const;
357
358
360 inline std::vector<bool> periodic() const;
361
363 inline ArraySize size() const;
364
366 inline ArraySize localSize() const;
367
369 inline std::vector<size_t> numBlocks() const;
370
371
373 bool operator==( const Mesh &mesh ) const override;
374
375
386
395 virtual void coord( const MeshElementIndex &index, double *pos ) const = 0;
396
401 virtual std::array<AMP::Array<double>, 3> localCoord() const = 0;
402
407 virtual std::array<AMP::Array<double>, 3> globalCoord() const = 0;
408
410 virtual bool isOnSurface( const MeshElementIndex &index ) const;
411
413 virtual bool isOnBoundary( const MeshElementIndex &index, int id ) const;
414
415
416public: // BoxMesh specific functionality
424 virtual AMP::Geometry::Point physicalToLogical( const AMP::Geometry::Point &x ) const = 0;
425
435 MeshElementIndex getElementFromLogical( const AMP::Geometry::Point &x, GeomType type ) const;
436
447 MeshElementIndex getElementFromPhysical( const AMP::Geometry::Point &x, GeomType type ) const;
448
450 inline int getRank( const MeshElementIndex &id ) const;
451
453 inline MeshElementID convert( const MeshElementIndex &id ) const;
454
456 inline MeshElementIndex convert( const MeshElementID &id ) const;
457
458
460 std::shared_ptr<AMP::LinearAlgebra::Vector> createVector( const std::string &name,
461 int gcw = 0 );
462
464 inline void fixPeriodic( MeshElementIndex &id ) const;
465
466public: // Convenience typedef
467 typedef AMP::Utilities::stackVector<std::pair<MeshElementIndex, MeshElementIndex>, 32>
469
470
471public: // Advanced functions
472 // Get the surface id for a given surface
473 int getSurfaceID( int surface ) const;
474
475 // Get the surface set for a given surface/type
476 ElementBlocks getSurface( int surface, GeomType type ) const;
477
478 // Helper function to return the indices of the local block owned by the given processor
479 inline std::array<int, 6> getLocalBlock( int rank ) const;
480
481 // Helper functions to identify the iterator blocks
483 getIteratorRange( std::array<int, 6> range, const GeomType type, const int gcw ) const;
484 ElementBlocks intersect( const ElementBlocks &v1, const ElementBlocks &v2 ) const;
485
486 // Helper function to create an iterator from an ElementBlocks list
488
489
490protected:
491 // Constructor
492 explicit BoxMesh( std::shared_ptr<const MeshParameters> );
493 explicit BoxMesh( const BoxMesh & );
494 BoxMesh &operator=( const BoxMesh & ) = delete;
495
496 // Function to create the load balancing
497 static void loadBalance( std::array<int, 3> size,
498 int N_procs,
499 std::vector<int> *startIndex,
500 std::vector<int> minSize = {} );
501
502 // Function to initialize the mesh data once the logical mesh info has been created
503 void initialize( const std::array<int, 3> &size,
504 const std::array<int, 6> &ids,
505 const std::vector<int> &minSize = {} );
506
507 // Function to finalize the mesh data once the coordinates have been set
508 void finalize( const std::string &name, const std::vector<double> &displacement );
509
510 // Function to finalize the mesh data once the coordinates have been set
511 std::vector<double> getDisplacement( std::shared_ptr<const AMP::Database> db );
512
513 // Function to finalize the mesh data once the coordinates have been set
514 virtual void createBoundingBox();
515
516 // Helper function to fill the node data for a uniform cartesian mesh
517 static void fillCartesianNodes( int dim,
518 const int *globalSize,
519 const double *range,
520 const std::vector<MeshElementIndex> &index,
521 std::vector<double> *coord );
522
523 // Helper function to check if an element is on a given side
524 bool onSide( const MeshElementIndex &index, int d, int s ) const;
525
526 // Get the surface (including mapped surfaces)
527 ElementBlocks getSurface2( int surface, GeomType type ) const;
528
529 // Helper function to map points on the boundary
530 template<uint8_t NDIM>
531 std::vector<MeshElementIndex> createMap( const std::vector<MeshElementIndex> ) const;
533
534protected: // Write/read restart data
535 void writeRestart( int64_t ) const override;
537
538
539protected: // Friend functions to access protected functions
542 typedef std::vector<MeshElementIndex> IndexList;
543 typedef std::shared_ptr<IndexList> ListPtr;
544 typedef std::tuple<IndexList, IndexList> SurfaceMapStruct;
545
546
547protected: // Internal data
548 const int d_rank, d_size; // Cached values for the rank and size
549 int d_blockID; // Block id for the mesh
550 const std::array<int, 3> d_globalSize; // The size of the logical domain in each direction
551 const std::array<int, 3> d_numBlocks; // The number of local box in each direction
552 const std::vector<int> d_startIndex[3]; // The first index for each block
553 const std::vector<int> d_endIndex[3]; // The end index (last=1) for each block
554 const std::array<int, 6> d_localIndex; // Local index range (cached for performance)
555 const std::array<int, 3> d_indexSize; // Local index size (local box + 2) (cached)
556 const std::array<int, 6> d_surfaceId; // ID of each surface (if any, -1 if not)
557 mutable std::vector<ListPtr> d_surface[4]; // List of surface elements
558 mutable std::vector<ListPtr> d_bnd[4][6]; // List of boundary elements
559 SurfaceMapStruct d_surfaceMaps[3]; // Surface maps
560
561protected:
563};
564
565
566// Function to write an index to std::ostream
567std::ostream &operator<<( std::ostream &out, const BoxMesh::MeshElementIndex &x );
568
569
570} // namespace AMP::Mesh
571
572#include "AMP/mesh/structured/BoxMesh.inline.h"
573
574
575#endif
Simple class to store the array dimensions.
Definition ArraySize.h:138
Class to manage reading/writing restart data.
Structure to identify a logical box.
Definition BoxMesh.h:79
int last[3]
Ending element.
Definition BoxMesh.h:96
constexpr ArraySize size() const
Return the size of the box.
int first[3]
Starting element.
Definition BoxMesh.h:95
constexpr Box()
Empty constructor.
constexpr Box(int ifirst, int ilast, int jfirst=0, int jlast=0, int kfirst=0, int klast=0)
Default constructor.
std::string print() const
MeshElementIndexIterator end() const
bool operator==(const MeshElementIndexIterator &rhs) const
MeshElementIndexIterator & operator++()
MeshElementIndexIterator(const MeshElementIndex &first, const MeshElementIndex &last, const AMP::Mesh::BoxMesh *mesh, size_t pos=0)
MeshElementIndexIterator & operator+=(int N)
MeshElementIndexIterator & operator--()
std::random_access_iterator_tag iterator_category
Definition BoxMesh.h:160
MeshElementIndexIterator & operator[](int)
MeshElementIndexIterator & operator+=(const MeshElementIndexIterator &it)
MeshElementIndexIterator begin() const
bool operator!=(const MeshElementIndexIterator &rhs) const
Structure to uniquely identify an element.
Definition BoxMesh.h:109
uint8_t d_type
Mesh element type.
Definition BoxMesh.h:145
static constexpr size_t numElements(const MeshElementIndex &first, const MeshElementIndex &last)
constexpr bool operator>(const MeshElementIndex &rhs) const
Operator >
constexpr int & index(int d)
Definition BoxMesh.h:135
constexpr bool operator<(const MeshElementIndex &rhs) const
Operator <.
constexpr GeomType type() const
Definition BoxMesh.h:138
constexpr int index(int d) const
Definition BoxMesh.h:134
constexpr void reset(GeomType type, uint8_t side, int x, int y=0, int z=0)
uint8_t d_side
Are we dealing with x, y, or z faces/edges.
Definition BoxMesh.h:146
constexpr bool operator>=(const MeshElementIndex &rhs) const
Operator >=.
constexpr bool operator<=(const MeshElementIndex &rhs) const
Operator <=.
constexpr MeshElementIndex(GeomType type, uint8_t side, int x, int y=0, int z=0)
Default constructor.
constexpr uint8_t side() const
Definition BoxMesh.h:139
constexpr bool isNull() const
Definition BoxMesh.h:126
constexpr bool operator!=(const MeshElementIndex &rhs) const
Operator !=.
constexpr int operator[](int d) const
Definition BoxMesh.h:136
constexpr int & operator[](int d)
Definition BoxMesh.h:137
std::array< int, 3 > d_index
Global x, y, z index (may be negative)
Definition BoxMesh.h:147
constexpr MeshElementIndex()=default
Empty constructor.
constexpr bool operator==(const MeshElementIndex &rhs) const
Operator ==.
constexpr const auto & index() const
Definition BoxMesh.h:133
A class used to represent a logically rectangular box mesh.
Definition BoxMesh.h:71
const std::array< int, 6 > d_surfaceId
Definition BoxMesh.h:556
std::vector< size_t > numBlocks() const
Return the number of blocks.
size_t numGhostElements(const GeomType type, const int gcw) const override final
size_t numLocalElements(const GeomType type) const override final
bool onSide(const MeshElementIndex &index, int d, int s) const
const std::array< int, 3 > d_numBlocks
Definition BoxMesh.h:551
virtual MeshIterator getBlockIDIterator(const GeomType type, const int id, const int gcw=0) const override final
Return an MeshIterator over the given geometric objects on the given block ID set.
ElementBlocks getSurface(int surface, GeomType type) const
virtual bool isOnBoundary(const MeshElementIndex &index, int id) const
Check if the element is on the given boundary id.
const std::vector< int > d_endIndex[3]
Definition BoxMesh.h:553
static std::shared_ptr< BoxMesh > generate(std::shared_ptr< const MeshParameters > params)
Read in mesh files, partition domain, and prepare environment for simulation.
int getRank(const MeshElementIndex &id) const
Get the rank that owns the element.
size_t numGlobalElements(const GeomType type) const override final
const std::array< int, 3 > d_indexSize
Definition BoxMesh.h:555
std::tuple< IndexList, IndexList > SurfaceMapStruct
Definition BoxMesh.h:544
ArraySize localSize() const
Return the size of the mesh.
virtual void createBoundingBox()
virtual void coord(const MeshElementIndex &index, double *pos) const =0
Return a mesh element's coordinates given it's id.
std::vector< ListPtr > d_surface[4]
Definition BoxMesh.h:557
std::unique_ptr< Mesh > clone() const override=0
Virtual function to copy the mesh (allows use to proply copy the derived class)
ElementBlocks intersect(const ElementBlocks &v1, const ElementBlocks &v2) const
ArraySize size() const
Return the size of the mesh.
static void fillCartesianNodes(int dim, const int *globalSize, const double *range, const std::vector< MeshElementIndex > &index, std::vector< double > *coord)
MeshElementIndex getElementFromLogical(const AMP::Geometry::Point &x, GeomType type) const
Return the element containing the point.
std::array< int, 6 > getLocalBlock(int rank) const
std::shared_ptr< AMP::LinearAlgebra::Vector > createVector(const std::string &name, int gcw=0)
Create an ArrayVector over the mesh.
virtual MeshIterator getSurfaceIterator(const GeomType type, const int gcw=0) const override final
Return an MeshIterator over the given geometric objects on the surface.
std::vector< int > getBoundaryIDs() const override final
Return the list of all boundary ID sets in the mesh.
static void loadBalance(std::array< int, 3 > size, int N_procs, std::vector< int > *startIndex, std::vector< int > minSize={})
Box getGlobalBox(int gcw=0) const
Return the global logical box.
void finalize(const std::string &name, const std::vector< double > &displacement)
const int d_rank
Definition BoxMesh.h:548
MeshElementIndex getElementFromPhysical(const AMP::Geometry::Point &x, GeomType type) const
Return the element containing the point.
ElementBlocks getSurface2(int surface, GeomType type) const
static ArraySize estimateLogicalMeshSize(std::shared_ptr< const MeshParameters > params)
Estimate the number of elements in the mesh.
void initialize(const std::array< int, 3 > &size, const std::array< int, 6 > &ids, const std::vector< int > &minSize={})
const std::vector< int > d_startIndex[3]
Definition BoxMesh.h:552
virtual MeshIterator getBoundaryIDIterator(const GeomType type, const int id, const int gcw=0) const override final
Return an MeshIterator over the given geometric objects on the given boundary ID set.
void fixPeriodic(MeshElementIndex &id) const
Fix periodic boundaries in the element index.
void writeRestart(int64_t) const override
Write restart data to file.
std::vector< double > getDisplacement(std::shared_ptr< const AMP::Database > db)
static size_t estimateMeshSize(std::shared_ptr< const MeshParameters > params)
Estimate the number of elements in the mesh.
ElementBlocks getIteratorRange(std::array< int, 6 > range, const GeomType type, const int gcw) const
std::vector< ListPtr > d_bnd[4][6]
Definition BoxMesh.h:558
BoxMesh(int64_t, AMP::IO::RestartManager *)
std::vector< int > getBlockIDs() const override final
Return the list of all boundary ID sets in the mesh.
virtual std::array< AMP::Array< double >, 3 > globalCoord() const =0
Return the vertex coordinates for the global box.
virtual bool isOnSurface(const MeshElementIndex &index) const
Check if the element is on the surface.
virtual AMP::Geometry::Point physicalToLogical(const AMP::Geometry::Point &x) const =0
Return the logical coordinates.
std::vector< bool > periodic() const
Return the bool vector indicating which directions are periodic.
const std::array< int, 6 > d_localIndex
Definition BoxMesh.h:554
std::vector< MeshElementIndex > IndexList
Definition BoxMesh.h:542
MeshElementID convert(const MeshElementIndex &id) const
Convert the MeshElementIndex to the MeshElementID.
const std::array< int, 3 > d_globalSize
Definition BoxMesh.h:550
static size_t maxProcs(std::shared_ptr< const MeshParameters > params)
Return the maximum number of processors that can be used with the mesh.
virtual ~BoxMesh()
Destructor.
std::shared_ptr< IndexList > ListPtr
Definition BoxMesh.h:543
int getSurfaceID(int surface) const
Box getLocalBox(int gcw=0) const
Return the local logical box.
SurfaceMapStruct d_surfaceMaps[3]
Definition BoxMesh.h:559
MeshElementPtr getElement(const MeshElementID &id) const override final
Return a mesh element given it's id.
MeshIterator getIterator(const GeomType type, const int gcw=0) const override final
Return an MeshIterator over the given geometric objects.
const int d_size
Definition BoxMesh.h:548
MeshIterator createIterator(const ElementBlocks &list) const
virtual ElementListPtr getElementParents(const MeshElement &elem, const GeomType type) const override final
Return the parent elements of the given mesh element.
std::vector< MeshElementIndex > createMap(const std::vector< MeshElementIndex >) const
virtual std::array< AMP::Array< double >, 3 > localCoord() const =0
Return the vertex coordinates for the local box.
A pointer class to wrap a MeshElementVector.
A class used to iterate over elements in a Mesh.
A class used to abstract away mesh from an application.
Definition Mesh.h:57
std::unique_ptr< MeshElement > MeshElementPtr
Pointer to MeshElement and MeshElementVector.
Definition Mesh.h:69
A derived class used to define a mesh element.
Stack based vector.
Definition Utilities.h:423
GeomType
Enumeration for basic mesh-based quantities.
Definition MeshID.h:12
std::ostream & operator<<(std::ostream &out, GeomType x)
A structure used to identify the mesh element.
Definition MeshID.h:156



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:40.
Comments on this page