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