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