PeriDyno 0.8.2
GmshGenerator.cpp
Go to the documentation of this file.
1#include "GmshGenerator.h"
2
3#include <vector>
4#include <gmsh.h>
5
6namespace dyno
7{
8 template<typename TDataType>
10 : Node()
11 {
12 }
13
14 template<typename TDataType>
16 {
17 }
18
19 template<typename TDataType>
21 {
23
24 gmsh::model::add("x2");
25
26
27 double lc = 1;//2e-1;
28 int pIndex = 1;
29 gmsh::model::geo::addPoint(-1, -1, -1, lc, pIndex++);
30 gmsh::model::geo::addPoint(1, -1, -1, lc, pIndex++);
31 gmsh::model::geo::addPoint(1, 1, -1, lc, pIndex++);
32 gmsh::model::geo::addPoint(-1, 1, -1, lc, pIndex++);
33 gmsh::model::geo::addPoint(-1, -1, 1, lc, pIndex++);
34 gmsh::model::geo::addPoint(1, -1, 1, lc, pIndex++);
35 gmsh::model::geo::addPoint(1, 1, 1, lc, pIndex++);
36 gmsh::model::geo::addPoint(-1, 1, 1, lc, pIndex++);
37
38 // The API to create lines with the built-in kernel follows the same
39 // conventions: the first 2 arguments are point tags, the last (optional one)
40 // is the line tag.
41 int lIndex = 1;
42 int p, q;
43 p = 1;
44 for (int i = 0; i < 3; i++)
45 {
46 gmsh::model::geo::addLine(p, p + 1, lIndex++);
47 p++;
48 }
49 gmsh::model::geo::addLine(p, 1, lIndex++);
50
51 p = 5;
52 for (int i = 0; i < 3; i++)
53 {
54 gmsh::model::geo::addLine(p, p + 1, lIndex++);
55 p++;
56 }
57 gmsh::model::geo::addLine(p, 5, lIndex++);
58
59 p = 1;
60 q = 5;
61 for (int i = 0; i < 4; i++)
62 {
63 gmsh::model::geo::addLine(p, q, lIndex++);
64 p++;
65 q++;
66 }
67
68
69 // The philosophy to construct curve loops and surfaces is similar: the first
70 // argument is now a vector of integers.
71 gmsh::model::geo::addCurveLoop({ 1, 2, 3, 4 }, 1);
72 gmsh::model::geo::addCurveLoop({ 5, 6, 7, 8 }, 2);
73 gmsh::model::geo::addCurveLoop({ 1, 10, -5, -9 }, 3);
74 gmsh::model::geo::addCurveLoop({ 2, 11, -6, -10 }, 4);
75 gmsh::model::geo::addCurveLoop({ 3, 12, -7, -11 }, 5);
76 gmsh::model::geo::addCurveLoop({ -4,12, 8, -9 }, 6);
77
78
79
80 gmsh::model::geo::addPlaneSurface({ 1 }, 1);
81 gmsh::model::geo::addPlaneSurface({ 2 }, 2);
82 gmsh::model::geo::addPlaneSurface({ 3 }, 3);
83 gmsh::model::geo::addPlaneSurface({ 4 }, 4);
84 gmsh::model::geo::addPlaneSurface({ 5 }, 5);
85 gmsh::model::geo::addPlaneSurface({ 6 }, 6);
86
87 gmsh::model::geo::addSurfaceLoop({ 1, 2, 3, 4, 5, 6 }, 1);
88 gmsh::model::geo::addVolume({ 1 }, 1);
89 // Physical groups are defined by providing the dimension of the group (0 for
90 // physical points, 1 for physical curves, 2 for physical surfaces and 3 for
91 // phsyical volumes) followed by a vector of entity tags. The last (optional)
92 // argument is the tag of the new group to create.
93
94 gmsh::model::setPhysicalName(3, 1, "My volume");
95
96 // Before it can be meshed, the internal CAD representation must be
97 // synchronized with the Gmsh model, which will create the relevant Gmsh data
98 // structures. This is achieved by the gmsh::model::geo::synchronize() API
99 // call for the built-in CAD kernel. Synchronizations can be called at any
100 // time, but they involve a non trivial amount of processing; so while you
101 // could synchronize the internal CAD data after every CAD command, it is
102 // usually better to minimize the number of synchronization points.
103 gmsh::model::geo::synchronize();
104
105 // We can then generate a 2D mesh...
106 gmsh::model::mesh::generate();
107 //gmsh::model::mesh::refine();
108
109 std::vector<double> nodes, y;
110 std::vector<std::size_t> nodeTags;
111 gmsh::model::mesh::getNodes(nodeTags, nodes, y, -2, -1, false, true);
112 std::cout << "The number of nodes: " << nodes.size() / 3 << std::endl;
113
114 std::vector<int> elementTypes;
115 std::vector<std::vector<std::size_t> > elementTags, nodeTags2;
116 gmsh::model::mesh::getElements(elementTypes, elementTags, nodeTags2, -2, -1);
117 std::cout << "The number of elements: " << nodeTags2[1].size() / 3 << std::endl;
118
119 gmsh::finalize();
120
121
122 std::vector<Coord> vertices;
123 std::vector<TopologyModule::Triangle> indices;
124
125 for (size_t i = 0; i < nodes.size() / 3; i++)
126 {
127 vertices.push_back(Coord(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]));
128 }
129
130 for (size_t i = 0; i < nodeTags2[1].size() / 3; i++)
131 {
132 TopologyModule::Triangle t(nodeTags2[1][3 * i] - 1, nodeTags2[1][3 * i + 1] - 1, nodeTags2[1][3 * i + 2] - 1);
133 indices.push_back(t);
134 }
135
136 //setup the output data
137 if (this->outGmsh()->isEmpty())
138 {
139 this->outGmsh()->allocate();
140 }
141
142 auto mesh = this->outGmsh()->getDataPtr();
143
144 mesh->setPoints(vertices);
145 mesh->setTriangles(indices);
146
147 mesh->update();
148
149 vertices.clear();
150 indices.clear();
151
152 nodes.clear();
153 y.clear();
154 nodeTags.clear();
155 elementTypes.clear();
156 elementTags.clear();
157 nodeTags2.clear();
158
159
160 }
161
163}
TDataType::Coord Coord
Definition: GmshGenerator.h:29
void resetStates() override
RenderRef initialize(QWindow *window, bool defaultRender)
Definition: QtImGui.cpp:157
This is an implementation of AdditiveCCD based on peridyno.
Definition: Array.h:24
DEFINE_CLASS(CircularEmitter)
static void add(GeometryImpl::NewVertex &vtx, int index)
Definition: ofbx.cpp:2460