PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
gmshMeshPipeline.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2026 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include "gmshMeshPipeline.h"
12#include "inp/meshDeck.h"
13#include "inp/modelDeck.h"
14#include "mesh/mesh.h"
15#include "util/feElementDefs.h"
16#include <gmsh.h>
17#include <stdexcept>
18#include <unordered_map>
19
20namespace mesh_gen {
21
22int gmshMeshGenerateDim(const inp::ModelDeck *modelDeck) {
23 if (modelDeck == nullptr)
24 return 3;
25 const int d = static_cast<int>(modelDeck->d_dim);
26 if (d < 1 || d > 3)
27 return 3;
28 return d;
29}
30
32 const inp::ModelDeck *modelDeck) {
33
34 std::vector<std::size_t> nodeTags;
35 std::vector<double> coord;
36 std::vector<double> paramCoord;
37 gmsh::model::mesh::getNodes(nodeTags, coord, paramCoord);
38
39 std::unordered_map<std::size_t, std::size_t> tagToIdx;
40 tagToIdx.reserve(nodeTags.size());
41 for (size_t i = 0; i < nodeTags.size(); ++i)
42 tagToIdx[nodeTags[i]] = i;
43
44 std::vector<util::Point> nodes(nodeTags.size());
45 for (size_t i = 0; i < nodeTags.size(); ++i)
46 nodes[i] = util::Point(coord[3 * i], coord[3 * i + 1], coord[3 * i + 2]);
47
48 std::vector<int> elementTypes;
49 std::vector<std::vector<std::size_t>> elementTags, elementNodeTags;
50 gmsh::model::mesh::getElements(elementTypes, elementTags, elementNodeTags, -1, -1);
51
52 bool hasTetra = false;
53 for (size_t t = 0; t < elementTypes.size(); ++t) {
54 if (elementTypes[t] == util::msh_type_tetrahedron) {
55 hasTetra = true;
56 break;
57 }
58 }
59
60 if (hasTetra) {
61 std::vector<size_t> enc;
62 for (size_t t = 0; t < elementTypes.size(); ++t) {
63 if (elementTypes[t] != util::msh_type_tetrahedron)
64 continue;
65 const auto &nt = elementNodeTags[t];
66 for (size_t j = 0; j < nt.size(); j += 4) {
67 enc.push_back(tagToIdx.at(nt[j]));
68 enc.push_back(tagToIdx.at(nt[j + 1]));
69 enc.push_back(tagToIdx.at(nt[j + 2]));
70 enc.push_back(tagToIdx.at(nt[j + 3]));
71 }
72 }
73 if (enc.empty())
74 throw std::runtime_error("fillMeshFromActiveGmshModel: no Gmsh tetrahedron elements found.");
75 mesh_p->loadFromTetraElements3D(std::move(nodes), std::move(enc), meshDeck, modelDeck);
76 return;
77 }
78
79 std::vector<size_t> enc;
80 for (size_t t = 0; t < elementTypes.size(); ++t) {
81 if (elementTypes[t] != util::msh_type_triangle)
82 continue;
83 const auto &nt = elementNodeTags[t];
84 for (size_t j = 0; j < nt.size(); j += 3) {
85 enc.push_back(tagToIdx.at(nt[j]));
86 enc.push_back(tagToIdx.at(nt[j + 1]));
87 enc.push_back(tagToIdx.at(nt[j + 2]));
88 }
89 }
90
91 if (enc.empty())
92 throw std::runtime_error("fillMeshFromActiveGmshModel: no Gmsh triangle elements found.");
93
94 mesh_p->loadFromTriangleElements2D(std::move(nodes), std::move(enc), meshDeck, modelDeck);
95}
96
97} // namespace mesh_gen
A class for mesh data.
Definition mesh.h:53
void loadFromTetraElements3D(std::vector< util::Point > nodes, std::vector< size_t > enc, const inp::MeshDeck *meshDeck, const inp::ModelDeck *modelDeck)
Populate mesh from 3D tetrahedron data (0-based node indices in enc) without reading a file.
Definition mesh.cpp:209
void loadFromTriangleElements2D(std::vector< util::Point > nodes, std::vector< size_t > enc, const inp::MeshDeck *meshDeck, const inp::ModelDeck *modelDeck)
Populate mesh from 2D triangle data (0-based node indices in enc) without reading a file.
Definition mesh.cpp:171
static const int msh_type_triangle
Integer flag for triangle element.
static const int msh_type_tetrahedron
Integer flag for tetrahedron element.
void fillMeshFromActiveGmshModel(mesh::Mesh *mesh_p, const inp::MeshDeck *meshDeck, const inp::ModelDeck *modelDeck)
int gmshMeshGenerateDim(const inp::ModelDeck *modelDeck)
Structure to read and store mesh related input data.
Definition meshDeck.h:26
Structure to read and store model related input data.
Definition modelDeck.h:25
size_t d_dim
Dimension.
Definition modelDeck.h:106
A structure to represent 3d vectors.
Definition point.h:30