-
Notifications
You must be signed in to change notification settings - Fork 7
/
Mesh.cpp
277 lines (217 loc) · 7.97 KB
/
Mesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Copyright (c) 2015-2017 Lester Hedges <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cmath>
#include "Mesh.h"
#include "MersenneTwister.h"
/*! \file Mesh.cpp
\brief A class for the level-set domain fixed-grid mesh.
*/
namespace slsm
{
Element::Element() :
area(0),
nodes(4, 0),
boundarySegments(2, 0)
{
}
Node::Node() :
neighbours(4, 0),
elements(4, 0),
boundaryPoints(4, 0),
nElements(0),
isActive(false),
isDomain(false),
isMasked(false),
isMine(false)
{
}
Mesh::Mesh(unsigned int width_,
unsigned int height_) :
width(width_),
height(height_),
nElements(width*height),
nNodes((1+width)*(1+height))
{
// Resize element and node data structures.
elements.resize(nElements);
nodes.resize(nNodes);
// Resize 2D to 1D mapping vector.
xyToIndex.resize(width+1);
for (unsigned int i=0;i<width+1;i++)
xyToIndex[i].resize(height+1);
// Calculate node nearest neighbours.
initialiseNodes();
// Initialise elements (and node to element connectivity).
initialiseElements();
}
unsigned int Mesh::getClosestNode(const Coord& point) const
{
// Get element index.
unsigned int element = getElement(point);
// Work out distance relative to element centre.
double dx = point.x - elements[element].coord.x;
double dy = point.y - elements[element].coord.y;
// Point lies in left half.
if (dx < 0)
{
// Lower left quadrant.
if (dy < 0) return elements[element].nodes[0];
// Upper left quadrant.
else return elements[element].nodes[3];
}
// Point lies in right half.
else
{
// Lower right quadrant.
if (dy < 0) return elements[element].nodes[1];
// Upper right quadrant.
else return elements[element].nodes[2];
}
}
unsigned int Mesh::getClosestNode(double x, double y) const
{
// Get element index.
unsigned int element = getElement(x, y);
// Work out distance relative to element centre.
double dx = x - elements[element].coord.x;
double dy = y - elements[element].coord.y;
// Point lies in left half.
if (dx < 0)
{
// Lower left quadrant.
if (dy < 0) return elements[element].nodes[0];
// Upper left quadrant.
else return elements[element].nodes[3];
}
// Point lies in right half.
else
{
// Lower right quadrant.
if (dy < 0) return elements[element].nodes[1];
// Upper right quadrant.
else return elements[element].nodes[2];
}
}
unsigned int Mesh::getElement(const Coord& point) const
{
// Subtract a small value to ensure that we round down.
double x = point.x - 1e-6;
double y = point.y - 1e-6;
// Enforce lower bound.
if (x < 0) x = 0;
if (y < 0) y = 0;
// Work out x and y element indices (cells are unit width).
unsigned int elementX = std::floor(x);
unsigned int elementY = std::floor(y);
// Return global element index.
return (elementY*width + elementX);
}
unsigned int Mesh::getElement(double x, double y) const
{
// Subtract a small value to ensure that we round down.
x -= 1e-6;
y -= 1e-6;
// Enforce lower bound.
if (x < 0) x = 0;
if (y < 0) y = 0;
// Work out x and y element indices (cells are unit width).
unsigned int elementX = std::floor(x);
unsigned int elementY = std::floor(y);
// Return global element index.
return (elementY*width + elementX);
}
void Mesh::initialiseNodes()
{
// Coordinates of the node.
unsigned int x, y;
// Loop over all nodes.
for (unsigned int i=0;i<nNodes;i++)
{
// Mark node as in bulk.
nodes[i].isDomain = false;
// Mark node as unmasked.
nodes[i].isMasked = false;
// Zero number of connected elements.
nodes[i].nElements = 0;
// Zero number of boundary points.
nodes[i].nBoundaryPoints = 0;
// Work out node coordinates.
x = i % (width + 1);
y = int(i / (width + 1));
// Node lies on the domain boundary.
if ((x == 0) || (x == width) || (y == 0) || (y == height))
nodes[i].isDomain = true;
// Set node coordinates.
nodes[i].coord.x = x;
nodes[i].coord.y = y;
// Add to 2D mapping vector.
xyToIndex[x][y] = i;
// Determine nearest neighbours.
initialiseNeighbours(i, x, y);
}
}
void Mesh::initialiseElements()
{
// Coordinates of the element.
unsigned int x, y;
// Number of nodes along width of mesh (number of elements plus one).
unsigned int w = width + 1;
// Loop over all elements.
for (unsigned int i=0;i<nElements;i++)
{
// Work out element coordinates.
x = i % width;
y = int(i / width);
// Store coordinates of elemente centre.
elements[i].coord.x = x + 0.5;
elements[i].coord.y = y + 0.5;
// Store connectivity (element --> node)
// Node on bottom left corner of element.
elements[i].nodes[0] = x + (y * w);
// Node on bottom right corner of element.
elements[i].nodes[1] = x + 1 + (y * w);
// Node on top right corner of element.
elements[i].nodes[2] = x + 1 + ((y + 1) * w);
// Node on top right corner of element.
elements[i].nodes[3] = x + ((y + 1) * w);
// Fill reverse connectivity arrays (node --> element)
for (unsigned int j=0;j<4;j++)
{
unsigned int node = elements[i].nodes[j];
nodes[node].elements[nodes[node].nElements] = i;
nodes[node].nElements++;
}
}
}
void Mesh::initialiseNeighbours(unsigned int node, unsigned int x, unsigned int y)
{
// Number of nodes along width and height of mesh (number of elements plus one).
unsigned int w = width + 1;
unsigned int h = height + 1;
// First assume the mesh is periodic (in case we add this feature).
// Neighbours to left and right.
nodes[node].neighbours[0] = (x - 1 + w) % w + (y * w);
nodes[node].neighbours[1] = (x + 1 + w) % w + (y * w);
// Neighbours below and above.
nodes[node].neighbours[2] = x + (w * ((y - 1 + h) % h));
nodes[node].neighbours[3] = x + (w * ((y + 1 + h) % h));
// Now flag out of bounds neighbours (the mesh isn't periodic).
// Node is on first or last row.
if (x == 0) nodes[node].neighbours[0] = nNodes;
else if (x == width) nodes[node].neighbours[1] = nNodes;
// Node is on first or last column.
if (y == 0) nodes[node].neighbours[2] = nNodes;
else if (y == height) nodes[node].neighbours[3] = nNodes;
}
}