Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
Cylindrical2dNodesOnlyMesh.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 <map>
37#include "Cylindrical2dNodesOnlyMesh.hpp"
38
40 : NodesOnlyMesh<2>(),
41 mWidth(width)
42{
43 assert(width > 0.0);
44}
45
46void Cylindrical2dNodesOnlyMesh::SetUpBoxCollection(double cutOffLength, c_vector<double, 2*2> domainSize, int numLocalRows, c_vector<bool,2> isDimPeriodic)
47{
48 // Ensure that the width is a multiple of cut-off length
49 if (fmod( mWidth,cutOffLength ) > 1e-14)
50 {
51 EXCEPTION("The periodic width must be a multiple of cut off length.");
52 }
53 else if (mWidth/cutOffLength == 2.0)
54 {
55 // A width of two boxes gives different simulation results as some connections are considered twice.
56 EXCEPTION( "The periodic domain width cannot be 2*CutOffLength." );
57 }
58
59 // We force the domain to the periodic width
60 domainSize[0] = 0;
61 domainSize[1] = mWidth;
62
63 // Forcde the periodicity to periodic only in x
64 isDimPeriodic = zero_vector<bool>(2);
65 isDimPeriodic[0] = true;
66
67
68 NodesOnlyMesh<2>::SetUpBoxCollection(cutOffLength, domainSize, PETSC_DECIDE, isDimPeriodic); // Only difference is that this "true" makes the boxes periodic.
69
70 this->AddNodesToBoxes();
71}
72
73double Cylindrical2dNodesOnlyMesh::GetWidth(const unsigned& rDimension) const
74{
75 double width = 0.0;
76 assert(rDimension==0 || rDimension==1);
77 if (rDimension==0)
78 {
79 width = mWidth;
80 }
81 else
82 {
83 width = MutableMesh<2,2>::GetWidth(rDimension);
84 }
85 return width;
86}
87
88
89void Cylindrical2dNodesOnlyMesh::SetNode(unsigned nodeIndex, ChastePoint<2> point, bool concreteMove)
90{
91 // concreteMove should always be false for NodesOnlyMesh as no elements to check
92 assert(!concreteMove);
93
94 double x_coord = point.rGetLocation()[0];
95
96 // Perform a periodic movement if necessary
97 if (x_coord >= mWidth)
98 {
99 // Move point to the left
100 point.SetCoordinate(0, x_coord - mWidth);
101 }
102 else if (x_coord < 0.0)
103 {
104 double new_x_coord = x_coord + mWidth;
105 double fudge_factor = 1e-14;
106 // This is to ensure that the position is never equal to mWidth, which would be outside the box domain.
107 // This is due to the fact that mWidth-1e-16=mWidth
108 if (new_x_coord > mWidth-fudge_factor)
109 {
110 new_x_coord = mWidth-fudge_factor;
111 }
112 point.SetCoordinate(0, new_x_coord);
113 }
114
115 // Update the node's location
116 this->GetNode(nodeIndex)->SetPoint(point);
117}
118
120{
121 // Call method on parent class
122 unsigned node_index = NodesOnlyMesh<2>::AddNode(pNewNode);
123
124 // If necessary move it to be back on the cylinder
125 ChastePoint<2> new_node_point = pNewNode->GetPoint();
126 SetNode(node_index, new_node_point);
127
128 return node_index;
129
130}
131
132c_vector<double, 2> Cylindrical2dNodesOnlyMesh::GetVectorFromAtoB(const c_vector<double, 2>& rLocation1, const c_vector<double, 2>& rLocation2)
133{
134 c_vector<double, 2> vector = rLocation2 - rLocation1;
135 vector[0] = fmod(vector[0], mWidth);
136
137 /*
138 * Handle the cylindrical condition here: if the points are more
139 * than halfway around the cylinder apart, measure the other way.
140 */
141 if (vector[0] > 0.5*mWidth)
142 {
143 vector[0] -= mWidth;
144 }
145 else if (vector[0] < -0.5*mWidth)
146 {
147 vector[0] += mWidth;
148 }
149 return vector;
150}
151
152// Refresh mesh -> Check and then move if outside box
154{
155
156 // Check if the x values are in the domain, if not, get the fmod and relocate.
157 unsigned num_nodes = mNodes.size();
158 for (unsigned i=0; i<num_nodes; i++)
159 {
160 double& x_location = (mNodes[i]->rGetModifiableLocation())[0];
161 if (x_location < 0.0)
162 {
163 x_location = fmod(x_location, mWidth) + mWidth;
164 }
165 else if (x_location >= mWidth)
166 {
167 x_location = fmod(x_location, mWidth);
168 }
169 }
170
171 // Now run the base class method
173}
174
175// Serialization for Boost >= 1.36
#define EXCEPTION(message)
#define CHASTE_CLASS_EXPORT(T)
virtual void RefreshMesh()
virtual double GetWidth(const unsigned &rDimension) const
Node< SPACE_DIM > * GetNode(unsigned index) const
std::vector< Node< SPACE_DIM > * > mNodes
c_vector< double, DIM > & rGetLocation()
void SetCoordinate(unsigned i, double value)
double GetWidth(const unsigned &rDimension) const
unsigned AddNode(Node< 2 > *pNewNode)
virtual void SetUpBoxCollection(double cutOffLength, c_vector< double, 2 *2 > domainSize, int numLocalRows=PETSC_DECIDE, c_vector< bool, 2 > isDimPeriodic=unit_vector< bool >(2, 0))
void SetNode(unsigned nodeIndex, ChastePoint< 2 > point, bool concreteMove=false)
c_vector< double, 2 > GetVectorFromAtoB(const c_vector< double, 2 > &rLocation1, const c_vector< double, 2 > &rLocation2)
Definition Node.hpp:59
ChastePoint< SPACE_DIM > GetPoint() const
Definition Node.cpp:133
unsigned AddNode(Node< SPACE_DIM > *pNewNode)
void SetUpBoxCollection(const std::vector< Node< SPACE_DIM > * > &rNodes)