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