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