|
| 1 | +#include "MeshBuilder.hpp" |
| 2 | + |
| 3 | +#include "axom/slic.hpp" |
| 4 | +#include "axom/primal.hpp" |
| 5 | + |
| 6 | +namespace shared { |
| 7 | + |
| 8 | +MeshBuilder MeshBuilder::Unify( std::initializer_list<MeshBuilder> meshes ) |
| 9 | +{ |
| 10 | + std::vector<mfem::Mesh*> mesh_list; |
| 11 | + mesh_list.reserve( meshes.size() ); |
| 12 | + for ( auto& mesh : meshes ) { |
| 13 | + // NOTE: the const cast is because the constructor requires a non-const mesh, even though the data is copied and not |
| 14 | + // altered |
| 15 | + mesh_list.push_back( &const_cast<mfem::Mesh&>( *mesh ) ); |
| 16 | + } |
| 17 | + return mfem::Mesh( mesh_list.data(), mesh_list.size() ); |
| 18 | +} |
| 19 | + |
| 20 | +MeshBuilder MeshBuilder::SquareMesh( int n_x_els, int n_y_els ) |
| 21 | +{ |
| 22 | + return mfem::Mesh::MakeCartesian2D( n_x_els, n_y_els, mfem::Element::QUADRILATERAL ); |
| 23 | +} |
| 24 | + |
| 25 | +MeshBuilder MeshBuilder::CubeMesh( int n_x_els, int n_y_els, int n_z_els ) |
| 26 | +{ |
| 27 | + return mfem::Mesh::MakeCartesian3D( n_x_els, n_y_els, n_z_els, mfem::Element::HEXAHEDRON ); |
| 28 | +} |
| 29 | + |
| 30 | +MeshBuilder MeshBuilder::HypercubeMesh( int dim, int n_els ) |
| 31 | +{ |
| 32 | + switch ( dim ) { |
| 33 | + case 1: |
| 34 | + return mfem::Mesh::MakeCartesian1D( n_els ); |
| 35 | + case 2: |
| 36 | + return mfem::Mesh::MakeCartesian2D( n_els, n_els, mfem::Element::QUADRILATERAL ); |
| 37 | + case 3: |
| 38 | + return mfem::Mesh::MakeCartesian3D( n_els, n_els, n_els, mfem::Element::HEXAHEDRON ); |
| 39 | + default: |
| 40 | + SLIC_ERROR_ROOT( "Mesh dimension must be between 1 and 3." ); |
| 41 | + return mfem::Mesh(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +MeshBuilder::MeshBuilder( mfem::Mesh&& mesh ) : mesh_{ std::move( mesh ) } { mesh_.EnsureNodes(); } |
| 46 | + |
| 47 | +MeshBuilder&& MeshBuilder::translate( std::initializer_list<double> dx ) |
| 48 | +{ |
| 49 | + SLIC_ERROR_ROOT_IF( static_cast<int>( dx.size() ) != mesh_.SpaceDimension(), "Invalid size for dx" ); |
| 50 | + auto& coords = *mesh_.GetNodes(); |
| 51 | + for ( int d = 0; d < mesh_.SpaceDimension(); ++d ) { |
| 52 | + for ( int i = 0; i < mesh_.GetNV(); ++i ) { |
| 53 | + auto vdof = coords.FESpace()->DofToVDof( i, d ); |
| 54 | + coords[vdof] += *( dx.begin() + d ); |
| 55 | + } |
| 56 | + } |
| 57 | + return std::move( *this ); |
| 58 | +} |
| 59 | + |
| 60 | +MeshBuilder&& MeshBuilder::updateAttrib( int old_attrib, int new_attrib ) |
| 61 | +{ |
| 62 | + for ( int i = 0; i < mesh_.GetNE(); ++i ) { |
| 63 | + if ( mesh_.GetAttribute( i ) == old_attrib ) { |
| 64 | + mesh_.SetAttribute( i, new_attrib ); |
| 65 | + } |
| 66 | + } |
| 67 | + // add the new attribute to the mesh's list of attributes |
| 68 | + auto old_attrib_idx = mesh_.attributes.Find( old_attrib ); |
| 69 | + if ( old_attrib_idx >= 0 ) { |
| 70 | + mesh_.attributes[old_attrib_idx] = new_attrib; |
| 71 | + } |
| 72 | + mesh_.attributes.Sort(); |
| 73 | + mesh_.attributes.Unique(); |
| 74 | + return std::move( *this ); |
| 75 | +} |
| 76 | + |
| 77 | +MeshBuilder&& MeshBuilder::bdrAttribInfo() |
| 78 | +{ |
| 79 | + for ( int i{ 0 }; i < mesh_.bdr_attributes.Size(); ++i ) { |
| 80 | + auto current_attrib = mesh_.bdr_attributes[i]; |
| 81 | + auto num_elems = 0; |
| 82 | + auto attrib_bbox = axom::primal::BoundingBox<double, 3>(); |
| 83 | + for ( int j = 0; j < mesh_.GetNBE(); ++j ) { |
| 84 | + if ( mesh_.GetBdrAttribute( j ) == current_attrib ) { |
| 85 | + num_elems++; |
| 86 | + mfem::Array<int> bdr_dofs; |
| 87 | + mesh_.GetNodes()->FESpace()->GetBdrElementDofs( j, bdr_dofs ); |
| 88 | + for ( int k{ 0 }; k < bdr_dofs.Size(); ++k ) { |
| 89 | + axom::primal::Point<double, 3> vert; |
| 90 | + for ( int d{ 0 }; d < mesh_.SpaceDimension(); ++d ) { |
| 91 | + auto vdof = mesh_.GetNodes()->FESpace()->DofToVDof( bdr_dofs[k], d ); |
| 92 | + vert[d] = ( *mesh_.GetNodes() )[vdof]; |
| 93 | + } |
| 94 | + attrib_bbox.addPoint( vert ); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + if ( num_elems > 0 ) { |
| 99 | + std::cout << "Boundary attribute " << current_attrib << std::endl; |
| 100 | + std::cout << " Number of elements: " << num_elems << std::endl; |
| 101 | + std::cout << " Min coordinate: " << attrib_bbox.getMin() << std::endl; |
| 102 | + std::cout << " Max coordinate: " << attrib_bbox.getMax() << std::endl; |
| 103 | + std::cout << " Coordinate range: " << attrib_bbox.range() << std::endl; |
| 104 | + } |
| 105 | + } |
| 106 | + return std::move( *this ); |
| 107 | +} |
| 108 | + |
| 109 | +MeshBuilder&& MeshBuilder::updateBdrAttrib( int old_attrib, int new_attrib ) |
| 110 | +{ |
| 111 | + for ( int i = 0; i < mesh_.GetNBE(); ++i ) { |
| 112 | + if ( mesh_.GetBdrAttribute( i ) == old_attrib ) { |
| 113 | + mesh_.SetBdrAttribute( i, new_attrib ); |
| 114 | + } |
| 115 | + } |
| 116 | + // add the new boundary attribute to the mesh's list of boundary attributes |
| 117 | + auto old_attrib_idx = mesh_.bdr_attributes.Find( old_attrib ); |
| 118 | + if ( old_attrib_idx >= 0 ) { |
| 119 | + mesh_.bdr_attributes[old_attrib_idx] = new_attrib; |
| 120 | + } |
| 121 | + mesh_.bdr_attributes.Sort(); |
| 122 | + mesh_.bdr_attributes.Unique(); |
| 123 | + return std::move( *this ); |
| 124 | +} |
| 125 | + |
| 126 | +MeshBuilder::operator mfem::Mesh*() { return &mesh_; } |
| 127 | + |
| 128 | +MeshBuilder::operator const mfem::Mesh*() const { return &mesh_; } |
| 129 | + |
| 130 | +MeshBuilder::operator mfem::Mesh&() { return mesh_; } |
| 131 | + |
| 132 | +MeshBuilder::operator const mfem::Mesh&() const { return mesh_; } |
| 133 | + |
| 134 | +ParMeshBuilder::ParMeshBuilder( MPI_Comm comm, MeshBuilder&& mesh ) : pmesh_{ comm, mesh } {} |
| 135 | + |
| 136 | +ParMeshBuilder&& ParMeshBuilder::setNodesFEColl( mfem::H1_FECollection fe_coll ) |
| 137 | +{ |
| 138 | + mfem::FiniteElementCollection* fe_coll_ptr = fe_coll.Clone( fe_coll.GetOrder() ); |
| 139 | + mfem::ParFiniteElementSpace* fe_space = |
| 140 | + new mfem::ParFiniteElementSpace( &pmesh_, fe_coll_ptr, pmesh_.SpaceDimension() ); |
| 141 | + pmesh_.SetNodalFESpace( fe_space ); |
| 142 | + pmesh_.GetNodes()->MakeOwner( fe_coll_ptr ); |
| 143 | + return std::move( *this ); |
| 144 | +} |
| 145 | + |
| 146 | +mfem::ParGridFunction& ParMeshBuilder::getNodes() |
| 147 | +{ |
| 148 | + // static_cast should be OK; MeshBuilder meshes always have Nodes so this won't be null and this should never be a |
| 149 | + // GridFunction |
| 150 | + return *static_cast<mfem::ParGridFunction*>( pmesh_.GetNodes() ); |
| 151 | +} |
| 152 | + |
| 153 | +const mfem::ParGridFunction& ParMeshBuilder::getNodes() const |
| 154 | +{ |
| 155 | + // static_cast should be OK; MeshBuilder meshes always have Nodes so this won't be null and this should never be a |
| 156 | + // GridFunction |
| 157 | + return *static_cast<const mfem::ParGridFunction*>( pmesh_.GetNodes() ); |
| 158 | +} |
| 159 | + |
| 160 | +mfem::ParFiniteElementSpace& ParMeshBuilder::getNodesFESpace() { return *getNodes().ParFESpace(); } |
| 161 | + |
| 162 | +const mfem::ParFiniteElementSpace& ParMeshBuilder::getNodesFESpace() const { return *getNodes().ParFESpace(); } |
| 163 | + |
| 164 | +ParMeshBuilder::operator const mfem::ParMesh&() const { return pmesh_; } |
| 165 | + |
| 166 | +} // namespace shared |
0 commit comments