Chaste  Release::2017.1
ToroidalHoneycombVertexMeshGenerator.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 "ToroidalHoneycombVertexMeshGenerator.hpp"
37 
39  unsigned numElementsUp,
40  double cellRearrangementThreshold,
41  double t2Threshold)
42 {
43  // numElementsAcross and numElementsUp must be even for toroidal meshes
44  assert(numElementsAcross > 1);
45  assert(numElementsUp > 1);
46  assert(numElementsAcross%2 == 0);
47  assert(numElementsUp%2 == 0);
48 
49  assert(cellRearrangementThreshold > 0.0);
50  assert(t2Threshold > 0.0);
51 
52  std::vector<Node<2>*> nodes;
53  std::vector<VertexElement<2,2>*> elements;
54 
55  unsigned node_index = 0;
56  unsigned node_indices[6];
57  unsigned element_index;
58 
59  // Create the nodes
60  for (unsigned j=0; j<2*numElementsUp; j++)
61  {
62  for (unsigned i=0; i<numElementsAcross; i++)
63  {
64  double x_coord = ((j%4 == 0)||(j%4 == 3)) ? i+0.5 : i;
65  double y_coord = (1.5*j - 0.5*(j%2))*0.5/sqrt(3.0);
66 
67  Node<2>* p_node = new Node<2>(node_index, false , x_coord, y_coord);
68  nodes.push_back(p_node);
69  node_index++;
70  }
71  }
72 
73  /*
74  * Create the elements. The array node_indices contains the
75  * global node indices from bottom, going anticlockwise.
76  */
77  for (unsigned j=0; j<numElementsUp; j++)
78  {
79  for (unsigned i=0; i<numElementsAcross; i++)
80  {
81  element_index = j*numElementsAcross + i;
82 
83  node_indices[0] = 2*j*numElementsAcross + i + 1*(j%2==1);
84  node_indices[1] = node_indices[0] + numElementsAcross + 1*(j%2==0);
85  node_indices[2] = node_indices[0] + 2*numElementsAcross + 1*(j%2==0);
86  node_indices[3] = node_indices[0] + 3*numElementsAcross;
87  node_indices[4] = node_indices[0] + 2*numElementsAcross - 1*(j%2==1);
88  node_indices[5] = node_indices[0] + numElementsAcross - 1*(j%2==1);
89 
90  if (i == numElementsAcross-1) // on far right
91  {
92  node_indices[0] -= numElementsAcross*(j%2==1);
93  node_indices[1] -= numElementsAcross;
94  node_indices[2] -= numElementsAcross;
95  node_indices[3] -= numElementsAcross*(j%2==1);
96  }
97  if (j == numElementsUp-1) // on far top
98  {
99  node_indices[2] -= 2*numElementsAcross*numElementsUp;
100  node_indices[3] -= 2*numElementsAcross*numElementsUp;
101  node_indices[4] -= 2*numElementsAcross*numElementsUp;
102  }
103 
104  std::vector<Node<2>*> element_nodes;
105  for (unsigned k=0; k<6; k++)
106  {
107  element_nodes.push_back(nodes[node_indices[k]]);
108  }
109  VertexElement<2,2>* p_element = new VertexElement<2,2>(element_index, element_nodes);
110  elements.push_back(p_element);
111  }
112  }
113 
114  double mesh_width = numElementsAcross;
115  double mesh_height = 1.5*numElementsUp/sqrt(3.0);
116 
117  mpMesh = new Toroidal2dVertexMesh(mesh_width, mesh_height, nodes, elements, cellRearrangementThreshold, t2Threshold);
118 }
119 
121 {
122  EXCEPTION("A toroidal mesh was created but a normal mesh is being requested.");
123  return mpMesh; // Not really
124 }
125 
127 {
128  return (Toroidal2dVertexMesh*) mpMesh;
129 }
Definition: Node.hpp:58
#define EXCEPTION(message)
Definition: Exception.hpp:143
ToroidalHoneycombVertexMeshGenerator(unsigned numElementsAcross, unsigned numElementsUp, double cellRearrangementThreshold=0.01, double t2Threshold=0.001)