Chaste Commit::a9c8bf7350f67d7cf086e6fe3cf5461521554546
HoneycombMeshGenerator.cpp
1
2/*
3
4Copyright (c) 2005-2026, University of Oxford.
5All rights reserved.
6
7University of Oxford means the Chancellor, Masters and Scholars of the
8University of Oxford, having an administrative office at Wellington
9Square, Oxford OX1 2JD, UK.
10
11This file is part of Chaste.
12
13Redistribution and use in source and binary forms, with or without
14modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20 * Neither the name of the University of Oxford nor the names of its
21 contributors may be used to endorse or promote products derived from this
22 software without specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
30GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35*/
36
37#include "HoneycombMeshGenerator.hpp"
38
39#include <boost/make_shared.hpp>
40#include <boost/shared_ptr.hpp>
41#include "TrianglesMeshReader.hpp"
42#include "OutputFileHandler.hpp"
43#include "RandomNumberGenerator.hpp"
45#include "ChasteSyscalls.hpp"
46
47HoneycombMeshGenerator::HoneycombMeshGenerator(unsigned numNodesAlongWidth, unsigned numNodesAlongLength, unsigned ghosts, double scaleFactor)
48 : mpMesh(nullptr),
49 mMeshFilename("mesh"),
50 mDomainWidth(numNodesAlongWidth*scaleFactor),
51 mNumCellWidth(numNodesAlongWidth), //*1 because cells are considered to be size one
52 mNumCellLength(numNodesAlongLength)
53{
54 // The code below won't work in parallel
56
57 // An older version of the constructor might allow the wrong argument through to the scale factor
58 assert(scaleFactor > 0.0);
59
60 // Get a unique temporary foldername
61 std::stringstream pid;
62 pid << getpid();
63 OutputFileHandler output_file_handler("2D_temporary_honeycomb_mesh_" + pid.str());
64 std::string output_dir = output_file_handler.GetOutputDirectoryFullPath();
65
66 unsigned num_nodes_along_width = mNumCellWidth;
67 unsigned num_nodes_along_length = mNumCellLength;
68 double horizontal_spacing = mDomainWidth / (double)num_nodes_along_width;
69 double vertical_spacing = (sqrt(3.0)/2)*horizontal_spacing;
70
71 // This line is needed to define ghost nodes later
72 mDomainDepth = (double)(num_nodes_along_length) * vertical_spacing;
73
74 // Take account of ghost nodes
75 num_nodes_along_width += 2*ghosts;
76 num_nodes_along_length += 2*ghosts;
77
78 unsigned num_nodes = num_nodes_along_width*num_nodes_along_length;
79 unsigned num_elem_along_width = num_nodes_along_width-1;
80 unsigned num_elem_along_length = num_nodes_along_length-1;
81 unsigned num_elem = 2*num_elem_along_width*num_elem_along_length;
82 unsigned num_edges = 3*num_elem_along_width*num_elem_along_length + num_elem_along_width + num_elem_along_length;
83
84 double x0 = -horizontal_spacing*ghosts;
85 double y0 = -vertical_spacing*ghosts;
86
87 mBottom = -vertical_spacing*ghosts;
88 mTop = mBottom + vertical_spacing*(num_nodes_along_length-1);
89
90 // Write node file
91 out_stream p_node_file = output_file_handler.OpenOutputFile(mMeshFilename+".node");
92 (*p_node_file) << std::scientific;
93 (*p_node_file) << std::setprecision(20);
94 (*p_node_file) << num_nodes << "\t2\t0\t1" << std::endl;
95
96 unsigned node = 0;
97 for (unsigned i=0; i<num_nodes_along_length; i++)
98 {
99 for (unsigned j=0; j<num_nodes_along_width; j++)
100 {
101 if (i<ghosts || i>=(ghosts+mNumCellLength))
102 {
103 mGhostNodeIndices.insert(node);
104 }
105 else if (j < ghosts || j >= (ghosts+mNumCellWidth))
106 {
107 mGhostNodeIndices.insert(node);
108 }
109 unsigned boundary = 0;
110 if (i==0 || i==num_nodes_along_length-1 || j==0 || j==num_nodes_along_width-1)
111 {
112 boundary = 1;
113 }
114
115 const double adjustment = i % 2 != 0 ? 0.5 : 0.0;
116 double x = x0 + horizontal_spacing*((double)j + adjustment);
117 double y = y0 + vertical_spacing*(double)i;
118
119 // Avoid floating point errors which upset OffLatticeSimulation
120 if ((y<0.0) && (y>-1e-12))
121 {
122 // Difficult to cover - just corrects floating point errors that have occurred from time to time!
123 // LCOV_EXCL_START
124 y = 0.0;
125 // LCOV_EXCL_STOP
126 }
127
128 (*p_node_file) << node++ << "\t" << x << "\t" << y << "\t" << boundary << std::endl;
129 }
130 }
131 p_node_file->close();
132
133 // Write element file and edge file
134 out_stream p_elem_file = output_file_handler.OpenOutputFile(mMeshFilename+".ele");
135 (*p_elem_file) << std::scientific;
136
137 out_stream p_edge_file = output_file_handler.OpenOutputFile(mMeshFilename+".edge");
138 (*p_node_file) << std::scientific;
139
140 (*p_elem_file) << num_elem << "\t3\t0" << std::endl;
141 (*p_edge_file) << num_edges << "\t1" << std::endl;
142
143 unsigned elem = 0;
144 unsigned edge = 0;
145 for (unsigned i=0; i<num_elem_along_length; i++)
146 {
147 for (unsigned j=0; j < num_elem_along_width; j++)
148 {
149 unsigned node0 = i*num_nodes_along_width + j;
150 unsigned node1 = i*num_nodes_along_width + j+1;
151 unsigned node2 = (i+1)*num_nodes_along_width + j;
152
153 if (i%2 != 0)
154 {
155 node2 = node2 + 1;
156 }
157
158 (*p_elem_file) << elem++ << "\t" << node0 << "\t" << node1 << "\t" << node2 << std::endl;
159
160 unsigned horizontal_edge_is_boundary_edge = 0;
161 unsigned vertical_edge_is_boundary_edge = 0;
162 if (i==0)
163 {
164 horizontal_edge_is_boundary_edge = 1;
165 }
166 if (j==0 && i%2==0)
167 {
168 vertical_edge_is_boundary_edge = 1;
169 }
170
171 (*p_edge_file) << edge++ << "\t" << node0 << "\t" << node1 << "\t" << horizontal_edge_is_boundary_edge << std::endl;
172 (*p_edge_file) << edge++ << "\t" << node1 << "\t" << node2 << "\t" << 0 << std::endl;
173 (*p_edge_file) << edge++ << "\t" << node2 << "\t" << node0 << "\t" << vertical_edge_is_boundary_edge << std::endl;
174
175 node0 = i*num_nodes_along_width + j + 1;
176
177 if (i%2 != 0)
178 {
179 node0 = node0 - 1;
180 }
181 node1 = (i+1)*num_nodes_along_width + j+1;
182 node2 = (i+1)*num_nodes_along_width + j;
183
184 (*p_elem_file) << elem++ << "\t" << node0 << "\t" << node1 << "\t" << node2 << std::endl;
185 }
186 }
187
188 for (unsigned i=0; i<num_elem_along_length; i++)
189 {
190 unsigned node0, node1;
191
192 if (i%2==0)
193 {
194 node0 = (i+1)*num_nodes_along_width - 1;
195 node1 = (i+2)*num_nodes_along_width - 1;
196 }
197 else
198 {
199 node0 = (i+1)*num_nodes_along_width;
200 node1 = (i)*num_nodes_along_width;
201 }
202 (*p_edge_file) << edge++ << "\t" << node0 << "\t" << node1 << "\t" << 1 << std::endl;
203 }
204
205 for (unsigned j=0; j<num_elem_along_width; j++)
206 {
207 unsigned node0 = num_nodes_along_width*(num_nodes_along_length-1) + j;
208 unsigned node1 = num_nodes_along_width*(num_nodes_along_length-1) + j+1;
209 (*p_edge_file) << edge++ << "\t" << node1 << "\t" << node0 << "\t" << 1 << std::endl;
210 }
211
212 p_elem_file->close();
213 p_edge_file->close();
214
215 // Having written the mesh to file, now construct it using TrianglesMeshReader.
216 // Nested scope so the reader closes files before we try deleting them below the scope.
217 {
218 TrianglesMeshReader<2,2> mesh_reader(output_dir + mMeshFilename);
219 mpMesh = boost::make_shared<MutableMesh<2,2> >();
220 mpMesh->ConstructFromMeshReader(mesh_reader);
221 }
222
223 // Delete the temporary folder
224 output_file_handler.FindFile("").Remove();
225
226 // The original files have been deleted, it is better if the mesh object forgets about them
227 mpMesh->SetMeshHasChangedSinceLoading();
228}
229
230boost::shared_ptr<MutableMesh<2,2> > HoneycombMeshGenerator::GetMesh()
231{
232 return mpMesh;
233}
234
236{
237 std::vector<unsigned> location_indices;
238
239 for (unsigned i=0; i<mpMesh->GetNumNodes(); i++)
240 {
241 if (mGhostNodeIndices.find(i)==mGhostNodeIndices.end())
242 {
243 location_indices.push_back(i);
244 }
245 }
246 return location_indices;
247}
248
249boost::shared_ptr<MutableMesh<2,2> > HoneycombMeshGenerator::GetCircularMesh(double radius)
250{
251 if (!mGhostNodeIndices.empty())
252 {
253 EXCEPTION("Cannot call GetCircularMesh on a HoneycombMesh with ghost nodes");
254 }
255
256 // Centre the mesh at (0,0)
257 c_vector<double,2> centre = zero_vector<double>(2);
258 for (unsigned i=0; i<mpMesh->GetNumNodes(); i++)
259 {
260 centre += mpMesh->GetNode(i)->rGetLocation();
261 }
262 centre /= (double)mpMesh->GetNumNodes();
263
264 mpMesh->Translate(-centre[0], -centre[1]);
265
266 // Iterate over nodes, deleting any that lie more than the specified radius from (0,0)
267 for (unsigned i=0; i<mpMesh->GetNumAllNodes(); i++)
268 {
269 if (norm_2(mpMesh->GetNode(i)->rGetLocation()) >= radius)
270 {
271 mpMesh->DeleteNodePriorToReMesh(i);
272 }
273 else
274 {
275 // Jiggle the data
276 c_vector<double,2>& r_location = mpMesh->GetNode(i)->rGetModifiableLocation();
277 c_vector<double,2> shift;
279 double max_jiggle = radius*5e-6;
280 shift[0] = max_jiggle*(p_gen->ranf()-0.5);
281 shift[1] = max_jiggle*(p_gen->ranf()-0.5);
282 r_location += shift;
283 }
284 }
285
286 // Remesh
287 NodeMap map(mpMesh->GetNumNodes());
288 mpMesh->ReMesh(map);
289
290 return mpMesh;
291}
292
297
#define EXCEPTION(message)
void Remove() const
boost::shared_ptr< MutableMesh< 2, 2 > > GetCircularMesh(double radius)
std::vector< unsigned > GetCellLocationIndices()
virtual boost::shared_ptr< MutableMesh< 2, 2 > > GetMesh()
std::set< unsigned > mGhostNodeIndices
boost::shared_ptr< MutableMesh< 2, 2 > > mpMesh
std::string GetOutputDirectoryFullPath() const
FileFinder FindFile(std::string leafName) const
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
static bool IsSequential()
static RandomNumberGenerator * Instance()