Chaste  Release::2018.1
HoneycombVertexMeshGenerator.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 "HoneycombVertexMeshGenerator.hpp"
37 
39  unsigned numElementsUp,
40  bool isFlatBottom,
41  double cellRearrangementThreshold,
42  double t2Threshold,
43  double elementArea)
44 {
45  assert(numElementsAcross > 0);
46  assert(numElementsUp > 0);
47  assert(cellRearrangementThreshold > 0.0);
48  assert(t2Threshold > 0.0);
49  assert(elementArea > 0.0);
50 
51  std::vector<Node<2>*> nodes;
52  std::vector<VertexElement<2,2>*> elements;
53 
54  unsigned node_index = 0;
55  unsigned node_indices[6];
56  unsigned element_index;
57 
58  // Create the nodes, row by row, from the bottom up
59 
60  // On the first row we have numElementsAcross nodes, all of which are boundary nodes
61  for (unsigned i=0; i<numElementsAcross; i++)
62  {
63  Node<2>* p_node = new Node<2>(node_index, true, i+0.5, 0);
64  nodes.push_back(p_node);
65  node_index++;
66  }
67 
68  /*
69  * On each interior row we have numElementsAcross+1 nodes. On the second and penultimate
70  * row all nodes are boundary nodes. On other rows the first and last nodes only
71  * are boundary nodes.
72  */
73  for (unsigned j=1; j<2*numElementsUp+1; j++)
74  {
75  for (unsigned i=0; i<=numElementsAcross; i++)
76  {
77  double x_coord = ((j%4 == 0)||(j%4 == 3)) ? i+0.5 : i;
78  double y_coord = (1.5*j - 0.5*(j%2))*0.5/sqrt(3.0);
79  bool is_boundary_node = (j==1 || j==2*numElementsUp || i==0 || i==numElementsAcross) ? true : false;
80 
81  Node<2>* p_node = new Node<2>(node_index, is_boundary_node, x_coord, y_coord);
82  nodes.push_back(p_node);
83  node_index++;
84  }
85  }
86 
87  /*
88  * On the last row we have numElementsAcross nodes, all of which are boundary nodes.
89  */
90  double y_coord = (1.5*(2*numElementsUp+1) - 0.5*((2*numElementsUp+1)%2))*0.5/sqrt(3.0);
91  if (((2*numElementsUp+1)%4 == 0)||((2*numElementsUp+1)%4 == 3))
92  {
93  Node<2>* p_node = new Node<2>(node_index, true, 0.5, y_coord);
94  nodes.push_back(p_node);
95  node_index++;
96  }
97  for (unsigned i=1; i<numElementsAcross; i++)
98  {
99  double x_coord = (((2*numElementsUp+1)%4 == 0)||((2*numElementsUp+1)%4 == 3)) ? i+0.5 : i;
100 
101  Node<2>* p_node = new Node<2>(node_index, true, x_coord, y_coord);
102  nodes.push_back(p_node);
103  node_index++;
104  }
105  if (((2*numElementsUp+1)%4 == 1)||((2*numElementsUp+1)%4 == 2))
106  {
107  Node<2>* p_node = new Node<2>(node_index, true, numElementsAcross, y_coord);
108  nodes.push_back(p_node);
109  node_index++;
110  }
111 
112  /*
113  * Create the elements. The array node_indices contains the
114  * global node indices from bottom, going anticlockwise.
115  */
116  for (unsigned j=0; j<numElementsUp; j++)
117  {
118  for (unsigned i=0; i<numElementsAcross; i++)
119  {
120  if (j==0)
121  {
122  node_indices[0] = i;
123  }
124  else
125  {
126  node_indices[0] = 2*j*(numElementsAcross+1) - 1*(j%2==0) + i; // different for even/odd rows
127  }
128  node_indices[1] = node_indices[0] + numElementsAcross + 1 + 1*(j%2==0 && j>0);
129  node_indices[2] = node_indices[1] + numElementsAcross + 1;
130  node_indices[3] = node_indices[2] + numElementsAcross + 1*(j%2==1 && j<numElementsUp-1);
131  node_indices[4] = node_indices[2] - 1;
132  node_indices[5] = node_indices[1] - 1;
133 
134  std::vector<Node<2>*> element_nodes;
135  for (unsigned k=0; k<6; k++)
136  {
137  element_nodes.push_back(nodes[node_indices[k]]);
138  }
139 
140  element_index = j*numElementsAcross + i;
141  VertexElement<2,2>* p_element = new VertexElement<2,2>(element_index, element_nodes);
142  elements.push_back(p_element);
143  }
144  }
145 
146  mpMesh = new MutableVertexMesh<2,2>(nodes, elements, cellRearrangementThreshold, t2Threshold);
147 
148  // Scale the mesh so that each element's area takes the value elementArea
149  mpMesh->Scale(sqrt(elementArea*2.0/sqrt(3.0)), sqrt(elementArea*2.0/sqrt(3.0)));
150 }
151 
153 {
154  delete mpMesh;
155 }
156 
158 {
159  return mpMesh;
160 }
Definition: Node.hpp:58
virtual MutableVertexMesh< 2, 2 > * GetMesh()
virtual void Scale(const double xFactor=1.0, const double yFactor=1.0, const double zFactor=1.0)