Chaste  Release::2018.1
PottsElement.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 #include "PottsElement.hpp"
36 #include "RandomNumberGenerator.hpp"
37 
38 
39 template<unsigned DIM>
40 PottsElement<DIM>::PottsElement(unsigned index, const std::vector<Node<DIM>*>& rNodes)
41  : MutableElement<DIM,DIM>(index, rNodes)
42 {
43  this->RegisterWithNodes();
44 }
45 
46 template<unsigned DIM>
48 {
49 }
50 
51 template<unsigned DIM>
52 void PottsElement<DIM>::AddNode(Node<DIM>* pNode, const unsigned& rIndex)
53 {
54  // Add element to this node
55  pNode->AddElement(this->mIndex);
56 
57  // Add pNode to mNodes
58  this->mNodes.push_back(pNode);
59 }
60 
61 template<unsigned DIM>
63 {
64  assert(DIM == 2); // LCOV_EXCL_LINE
65  assert(this->GetNumNodes() != 0);
66 
67  if (this->GetNumNodes() <= 2)
68  {
69  return 1.0;
70  }
71 
72  double eig_max;
73  double eig_min;
74 
75  // See http://stackoverflow.com/questions/7059841/estimating-aspect-ratio-of-a-convex-hull for how to do it.
76  switch(DIM)
77  {
78  case 2:
79  {
80  // Calculate entries of covariance matrix (var_x,cov_xy;cov_xy,var_y)
81  double mean_x=0;
82  double mean_y=0;
83 
84  for (unsigned i=0; i<this->GetNumNodes(); i++)
85  {
86  mean_x += this->mNodes[i]->rGetLocation()[0];
87  mean_y += this->mNodes[i]->rGetLocation()[1];
88  }
89  mean_x /= this->GetNumNodes();
90  mean_y /= this->GetNumNodes();
91 
92  double variance_x = 0;
93  double variance_y = 0;
94  double covariance_xy = 0;
95 
96  for (unsigned i=0; i<this->GetNumNodes(); i++)
97  {
98  variance_x += pow((this->mNodes[i]->rGetLocation()[0]-mean_x),2);
99  variance_y += pow((this->mNodes[i]->rGetLocation()[1]-mean_y),2);
100  covariance_xy += (this->mNodes[i]->rGetLocation()[0]-mean_x)*(this->mNodes[i]->rGetLocation()[1]-mean_y);
101  }
102  variance_x /= this->GetNumNodes();
103  variance_y /= this->GetNumNodes();
104  covariance_xy /= this->GetNumNodes();
105 
106  // Calculate max/min eigenvalues
107  double trace = variance_x+variance_y;
108  double det = variance_x*variance_y - covariance_xy*covariance_xy;
109 
110  eig_max = 0.5*(trace+sqrt(trace*trace - 4*det));
111  eig_min = 0.5*(trace-sqrt(trace*trace - 4*det));
112 
113  break;
114  }
115 // case 3:
116 // {
117 // double mean_x = 0;
118 // double mean_y = 0;
119 // double mean_z = 0;
120 //
121 // for (unsigned i=0; i<this->GetNumNodes(); i++)
122 // {
123 // mean_x += this->mNodes[i]->rGetLocation()[0];
124 // mean_y += this->mNodes[i]->rGetLocation()[1];
125 // mean_z += this->mNodes[i]->rGetLocation()[2];
126 // }
127 // mean_x /= this->GetNumNodes();
128 // mean_y /= this->GetNumNodes();
129 // mean_z /= this->GetNumNodes();
130 //
131 // double variance_x = 0;
132 // double variance_y = 0;
133 // double variance_z = 0;
134 //
135 // double covariance_xy = 0;
136 // double covariance_xz = 0;
137 // double covariance_yz = 0;
138 //
139 // for (unsigned i=0; i<this->GetNumNodes(); i++)
140 // {
141 // double diff_x = this->mNodes[i]->rGetLocation()[0]-mean_x;
142 // double diff_y = this->mNodes[i]->rGetLocation()[1]-mean_y;
143 // double diff_z = this->mNodes[i]->rGetLocation()[2]-mean_z;
144 //
145 // variance_x += diff_x*diff_x;
146 // variance_y += diff_y*diff_y;
147 // variance_z += diff_z*diff_z;
148 // covariance_xy += diff_x*diff_y;
149 // covariance_xz += diff_x*diff_z;
150 // covariance_yz += diff_y*diff_z;
151 // }
152 // variance_x /= this->GetNumNodes();
153 // variance_y /= this->GetNumNodes();
154 // variance_z /= this->GetNumNodes();
155 // covariance_xy /= this->GetNumNodes();
156 // covariance_xz /= this->GetNumNodes();
157 // covariance_yz /= this->GetNumNodes();
158 //
159 // c_matrix<PetscScalar, 3, 3> covariance_matrix;
160 //
161 // covariance_matrix(0,0) = variance_x;
162 // covariance_matrix(0,1) = covariance_xy;
163 // covariance_matrix(0,2) = covariance_xz;
164 //
165 // covariance_matrix(1,0) = covariance_xy;
166 // covariance_matrix(1,1) = variance_y;
167 // covariance_matrix(1,2) = covariance_yz;
168 //
169 // covariance_matrix(2,0) = covariance_xz;
170 // covariance_matrix(2,1) = covariance_yz;
171 // covariance_matrix(2,2) = variance_z;
172 //
173 // const char N = 'N';
174 // const char L = 'L';
175 // PetscBLASInt size = DIM;
176 // PetscReal eigs[DIM];
177 // // Optimal work_size (102 entries) computed with a special call to LAPACKsyev_ (see documentation)
178 // // and rounded to the next power of 2
179 // const PetscBLASInt work_size = 128;
180 // PetscScalar workspace[work_size];
181 // PetscBLASInt info;
182 // LAPACKsyev_(&N, &L, &size, covariance_matrix.data(), &size, eigs, workspace, (PetscBLASInt*) &work_size, &info);
183 // assert(info == 0);
184 //
185 // // Lapack returns eigenvalues in ascending order
186 // eig_max = eigs[DIM-1];
187 // eig_min = eigs[0] +eigs[1];
188 // break;
189 // }
190  default:
192  }
193 
194  // As matrix is symmetric positive semidefinite
195  assert(eig_min >= 0);
196  assert(eig_max >= 0);
197 
198  if (eig_min == 0)
199  {
200  EXCEPTION("All nodes in an element lie in the same line/plane (2D/3D) so aspect ratio is infinite. This interferes with calculation of the Hamiltonian.");
201  }
202 
203  return eig_max/eig_min;
204 }
205 
206 // Explicit instantiation
207 template class PottsElement<1>;
208 template class PottsElement<2>;
209 template class PottsElement<3>;
double GetAspectRatio()
void AddNode(Node< DIM > *pNode, const unsigned &rIndex=UINT_MAX)
Definition: Node.hpp:58
#define EXCEPTION(message)
Definition: Exception.hpp:143
#define NEVER_REACHED
Definition: Exception.hpp:206
PottsElement(unsigned index, const std::vector< Node< DIM > * > &rNodes)
void AddElement(unsigned index)
Definition: Node.cpp:268