Chaste Commit::a9c8bf7350f67d7cf086e6fe3cf5461521554546
CylindricalHoneycombMeshGenerator.cpp
1/*
2
3Copyright (c) 2005-2026, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "CylindricalHoneycombMeshGenerator.hpp"
37
38#include <boost/make_shared.hpp>
39#include <boost/shared_ptr.hpp>
40#include "RandomNumberGenerator.hpp"
42#include "ChasteSyscalls.hpp"
43
44CylindricalHoneycombMeshGenerator::CylindricalHoneycombMeshGenerator(unsigned numNodesAlongWidth, unsigned numNodesAlongLength, unsigned ghosts, double scaleFactor)
45{
46 mpMesh = nullptr;
47 mDomainWidth = numNodesAlongWidth*scaleFactor;
48 mNumCellWidth = numNodesAlongWidth; //*1 because cells are considered to be size one
49 mNumCellLength = numNodesAlongLength;
50 mMeshFilename = "mesh";
51 mGhostNodeIndices.clear();
52 // The code below won't work in parallel
54
55 // An older version of the constructor might allow the wrong argument through to the scale factor
56 assert(scaleFactor > 0.0);
57
58 // Get a unique mesh filename
59 std::stringstream pid;
60 pid << getpid();
61
62 OutputFileHandler output_file_handler("2D_temporary_honeycomb_mesh_"+ pid.str());
63
64 unsigned num_nodes_along_width = mNumCellWidth;
65 unsigned num_nodes_along_length = mNumCellLength;
66 double horizontal_spacing = mDomainWidth / (double)num_nodes_along_width;
67 double vertical_spacing = (sqrt(3.0)/2)*horizontal_spacing;
68
69 // This line is needed to define ghost nodes later
70 mDomainDepth = (double)(num_nodes_along_length) * vertical_spacing;
71
72 // Take account of ghost nodes
73 num_nodes_along_length += 2*ghosts;
74
75 unsigned num_nodes = num_nodes_along_width*num_nodes_along_length;
76 unsigned num_elem_along_width = num_nodes_along_width-1;
77 unsigned num_elem_along_length = num_nodes_along_length-1;
78 unsigned num_elem = 2*num_elem_along_width*num_elem_along_length;
79 unsigned num_edges = 3*num_elem_along_width*num_elem_along_length + num_elem_along_width + num_elem_along_length;
80
81 double x0 = 0;
82 double y0 = -vertical_spacing*ghosts;
83
84 mBottom = -vertical_spacing*ghosts;
85 mTop = mBottom + vertical_spacing*(num_nodes_along_length-1);
86
87 // Write node file
88 out_stream p_node_file = output_file_handler.OpenOutputFile(mMeshFilename+".node");
89 (*p_node_file) << std::scientific;
90 //(*p_node_file) << std::setprecision(20);
91 (*p_node_file) << num_nodes << "\t2\t0\t1" << std::endl;
92
93 unsigned node = 0;
94 for (unsigned i=0; i<num_nodes_along_length; i++)
95 {
96 for (unsigned j=0; j<num_nodes_along_width; j++)
97 {
98 if (i<ghosts || i>=(ghosts+mNumCellLength))
99 {
100 mGhostNodeIndices.insert(node);
101 }
102 unsigned boundary = 0;
103 if ((i==0) || (i==num_nodes_along_length-1))
104 {
105 boundary = 1;
106 }
107
108 const double adjustment = i % 2 != 0 ? 0.5 : 0.0;
109 double x = x0 + horizontal_spacing*((double)j + adjustment);
110 double y = y0 + vertical_spacing*(double)i;
111
112 // Avoid floating point errors which upset OffLatticeSimulation
113 if ((y<0.0) && (y>-1e-12))
114 {
115 // Difficult to cover - just corrects floating point errors that have occurred from time to time!
116 // LCOV_EXCL_START
117 y = 0.0;
118 // LCOV_EXCL_STOP
119 }
120
121 (*p_node_file) << node++ << "\t" << x << "\t" << y << "\t" << boundary << std::endl;
122 }
123 }
124 p_node_file->close();
125
126 // Write element file and edge file
127 out_stream p_elem_file = output_file_handler.OpenOutputFile(mMeshFilename+".ele");
128 (*p_elem_file) << std::scientific;
129
130 out_stream p_edge_file = output_file_handler.OpenOutputFile(mMeshFilename+".edge");
131 (*p_node_file) << std::scientific;
132
133 (*p_elem_file) << num_elem << "\t3\t0" << std::endl;
134 (*p_edge_file) << num_edges << "\t1" << std::endl;
135
136 unsigned elem = 0;
137 unsigned edge = 0;
138 for (unsigned i=0; i<num_elem_along_length; i++)
139 {
140 for (unsigned j=0; j < num_elem_along_width; j++)
141 {
142 unsigned node0 = i*num_nodes_along_width + j;
143 unsigned node1 = i*num_nodes_along_width + j+1;
144 unsigned node2 = (i+1)*num_nodes_along_width + j;
145
146 if (i%2 != 0)
147 {
148 node2 = node2 + 1;
149 }
150
151 (*p_elem_file) << elem++ << "\t" << node0 << "\t" << node1 << "\t" << node2 << std::endl;
152
153 unsigned horizontal_edge_is_boundary_edge = 0;
154 unsigned vertical_edge_is_boundary_edge = 0;
155 if (i==0)
156 {
157 horizontal_edge_is_boundary_edge = 1;
158 }
159
160 (*p_edge_file) << edge++ << "\t" << node0 << "\t" << node1 << "\t" << horizontal_edge_is_boundary_edge << std::endl;
161 (*p_edge_file) << edge++ << "\t" << node1 << "\t" << node2 << "\t" << 0 << std::endl;
162 (*p_edge_file) << edge++ << "\t" << node2 << "\t" << node0 << "\t" << vertical_edge_is_boundary_edge << std::endl;
163
164 node0 = i*num_nodes_along_width + j + 1;
165
166 if (i%2 != 0)
167 {
168 node0 = node0 - 1;
169 }
170 node1 = (i+1)*num_nodes_along_width + j+1;
171 node2 = (i+1)*num_nodes_along_width + j;
172
173 (*p_elem_file) << elem++ << "\t" << node0 << "\t" << node1 << "\t" << node2 << std::endl;
174 }
175 }
176
177 for (unsigned i=0; i<num_elem_along_length; i++)
178 {
179 unsigned node0, node1;
180
181 if (i%2==0)
182 {
183 node0 = (i+1)*num_nodes_along_width - 1;
184 node1 = (i+2)*num_nodes_along_width - 1;
185 }
186 else
187 {
188 node0 = (i+1)*num_nodes_along_width;
189 node1 = (i)*num_nodes_along_width;
190 }
191 (*p_edge_file) << edge++ << "\t" << node0 << "\t" << node1 << "\t" << 1 << std::endl;
192 }
193
194 for (unsigned j=0; j<num_elem_along_width; j++)
195 {
196 unsigned node0 = num_nodes_along_width*(num_nodes_along_length-1) + j;
197 unsigned node1 = num_nodes_along_width*(num_nodes_along_length-1) + j+1;
198 (*p_edge_file) << edge++ << "\t" << node1 << "\t" << node0 << "\t" << 1 << std::endl;
199 }
200
201 p_elem_file->close();
202 p_edge_file->close();
203
204 // Having written the mesh to file, now construct it using TrianglesMeshReader.
205 // Nested scope so the reader closes files before we delete them below.
206 {
207 TrianglesMeshReader<2,2> mesh_reader(output_file_handler.GetOutputDirectoryFullPath() + mMeshFilename);
208 mpMesh = boost::make_shared<Cylindrical2dMesh>(mDomainWidth);
209 mpMesh->ConstructFromMeshReader(mesh_reader);
210 }
211
212 // Make the mesh cylindrical (we use Triangle library mode inside this ReMesh call)
213 mpMesh->ReMesh();
214
215 // Delete the temporary files
216 output_file_handler.FindFile("").Remove();
217
218 // The original files have been deleted, it is better if the mesh object forgets about them
219 mpMesh->SetMeshHasChangedSinceLoading();
220}
221
222boost::shared_ptr<MutableMesh<2,2> > CylindricalHoneycombMeshGenerator::GetMesh()
223{
224 EXCEPTION("A cylindrical mesh was created but a normal mesh is being requested.");
225 return mpMesh; // Not really
226}
227
228boost::shared_ptr<Cylindrical2dMesh> CylindricalHoneycombMeshGenerator::GetCylindricalMesh()
229{
230 return boost::static_pointer_cast<Cylindrical2dMesh>(mpMesh);
231}
#define EXCEPTION(message)
boost::shared_ptr< Cylindrical2dMesh > GetCylindricalMesh()
boost::shared_ptr< MutableMesh< 2, 2 > > GetMesh()
CylindricalHoneycombMeshGenerator(unsigned numNodesAlongWidth, unsigned numNodesAlongLength, unsigned ghosts=3, double scaleFactor=1.0)
void Remove() const
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()