Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
GaussianQuadratureRule.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 <cmath>
37
38#include "GaussianQuadratureRule.hpp"
39#include "Exception.hpp"
41
42template<unsigned ELEMENT_DIM>
44{
45 assert(index < mNumQuadPoints);
46 return mPoints[index];
47}
48
49template<unsigned ELEMENT_DIM>
51{
52 assert(index < mNumQuadPoints);
53 return mWeights[index];
54}
55
56template<unsigned ELEMENT_DIM>
58{
59 return mNumQuadPoints;
60}
61
62template <unsigned ELEMENT_DIM>
63GaussianQuadratureRule<ELEMENT_DIM>::GaussianQuadratureRule([[maybe_unused]] unsigned quadratureOrder)
64{
65 if constexpr (ELEMENT_DIM == 0)
66 {
67 mNumQuadPoints = 1;
68 mWeights.push_back(1);
69 mPoints.push_back(ChastePoint<0>());
70 }
71 else if constexpr (ELEMENT_DIM == 1)
72 {
73 switch (quadratureOrder)
74 {
75 case 0:
76 case 1: // 1d, 1st order
77 // 1 point rule
78 mWeights.push_back(1);
79 mPoints.push_back(ChastePoint<1>(0.5));
80 break;
81 case 2:
82 case 3: // 1d, 3rd order
83 // 2 point rule
84 mWeights.push_back(0.5);
85 mWeights.push_back(0.5);
86 {
87 double sqrt_one_third = sqrt(1.0 / 3.0);
88 mPoints.push_back(ChastePoint<1>((-sqrt_one_third + 1.0) / 2.0));
89 mPoints.push_back(ChastePoint<1>((sqrt_one_third + 1.0) / 2.0));
90 }
91 break;
92 case 4:
93 case 5: // 1d, 5th order
94 // 3 point rule
95 mWeights.push_back(5.0 / 18.0);
96 mWeights.push_back(4.0 / 9.0);
97 mWeights.push_back(5.0 / 18.0);
98
99 {
100 double sqrt_three_fifths = sqrt(3.0 / 5.0);
101 mPoints.push_back(ChastePoint<1>((-sqrt_three_fifths + 1.0) / 2.0));
102 mPoints.push_back(ChastePoint<1>(0.5));
103 mPoints.push_back(ChastePoint<1>((sqrt_three_fifths + 1.0) / 2.0));
104 }
105 break;
106 default:
107 EXCEPTION("Gauss quadrature order not supported.");
108 }
109 assert(mPoints.size() == mWeights.size());
110 mNumQuadPoints = mPoints.size();
111 }
112 else if constexpr (ELEMENT_DIM == 2)
113 {
114 double one_third = 1.0 / 3.0;
115 double one_sixth = 1.0 / 6.0;
116 double two_thirds = 2.0 / 3.0;
117 switch (quadratureOrder)
118 {
119 case 0: // 2d, 0th order
120 case 1: // 2d, 1st order
121 // 1 point rule
122 mWeights.push_back(0.5);
123 mPoints.push_back(ChastePoint<2>(one_third, one_third));
124 break;
125
126 case 2: // 2d, 2nd order
127 // 3 point rule
128 mWeights.push_back(one_sixth);
129 mWeights.push_back(one_sixth);
130 mWeights.push_back(one_sixth);
131
132 mPoints.push_back(ChastePoint<2>(two_thirds, one_sixth));
133 mPoints.push_back(ChastePoint<2>(one_sixth, one_sixth));
134 mPoints.push_back(ChastePoint<2>(one_sixth, two_thirds));
135 break;
136
137 case 3: // 2d, 3rd order - derived by hand and using a Macsyma script to solve the cubic
138 // 60*x^3 - 60*x^2 + 15*x - 1;
139 // 6 point rule
140 {
141 double w = 1.0 / 12.0;
142 mWeights.push_back(w);
143 mWeights.push_back(w);
144 mWeights.push_back(w);
145 mWeights.push_back(w);
146 mWeights.push_back(w);
147 mWeights.push_back(w);
148
149 double inverse_tan = atan(0.75);
150 double cos_third = cos(inverse_tan / 3.0);
151 double sin_third = sin(inverse_tan / 3.0);
152 // a = 0.23193336461755
153 double a = sin_third / (2 * sqrt(3.0)) - cos_third / 6.0 + 1.0 / 3.0;
154 // b = 0.10903901046622
155 double b = -sin_third / (2 * sqrt(3.0)) - cos_third / 6.0 + 1.0 / 3.0;
156 // c = 0.659028
157 double c = cos_third / 3.0 + 1.0 / 3.0;
158
159 mPoints.push_back(ChastePoint<2>(a, b));
160 mPoints.push_back(ChastePoint<2>(a, c));
161 mPoints.push_back(ChastePoint<2>(b, a));
162 mPoints.push_back(ChastePoint<2>(b, c));
163 mPoints.push_back(ChastePoint<2>(c, a));
164 mPoints.push_back(ChastePoint<2>(c, b));
165 }
166 break;
167 default:
168 EXCEPTION("Gauss quadrature order not supported.");
169 }
170 assert(mPoints.size() == mWeights.size());
171 mNumQuadPoints = mPoints.size();
172 }
173 else if constexpr (ELEMENT_DIM == 3)
174 {
175 switch (quadratureOrder)
176 {
177 case 0: // 3d, 0th order
178 case 1: // 3d, 1st order
179 // 1 point rule
180 mWeights.push_back(1.0 / 6.0);
181 mPoints.push_back(ChastePoint<3>(0.25, 0.25, 0.25));
182 break;
183
184 case 2: // 2nd order
185 // 4 point rule
186 {
187 double sqrt_fifth = 1.0 / sqrt(5.0);
188 double a = (1.0 + 3.0 * sqrt_fifth) / 4.0; // 0.585410196624969;
189 double b = (1.0 - sqrt_fifth) / 4.0; // 0.138196601125011;
190 double w = 1.0 / 24.0;
191
192 mWeights.push_back(w);
193 mWeights.push_back(w);
194 mWeights.push_back(w);
195 mWeights.push_back(w);
196
197 mPoints.push_back(ChastePoint<3>(a, b, b));
198 mPoints.push_back(ChastePoint<3>(b, a, b));
199 mPoints.push_back(ChastePoint<3>(b, b, a));
200 mPoints.push_back(ChastePoint<3>(b, b, b));
201 }
202 break;
203
204 case 3: // 3d, 3rd order
205 // 8 point rule
206 /* The main options were
207 * 5-point rule. Commonly published rule has four symmetric points and
208 * a negative weight in the centre. We would like to avoid
209 * negative weight (certainly for interpolation).
210 * 8-point rule. Uses two sets of symmetric points (as 4 point rule with a,b and then with c,d).
211 * This one is hard to derive a closed form solution to.
212 */
213 {
214 double root_seventeen = sqrt(17.0);
215 double root_term = sqrt(1022.0 - 134.0 * root_seventeen);
216 double b = (55.0 - 3.0 * root_seventeen + root_term) / 196; // b = 0.328055
217 double d = (55.0 - 3.0 * root_seventeen - root_term) / 196; // d = 0.106952
218
219 double a = 1.0 - 3.0 * b; // a = 0.0158359
220 double c = 1.0 - 3.0 * d; // c = 0.679143
221
222 // w1 = 0.023088 (= 0.138528/6)
223 double w1 = (20.0 * d * d - 10.0 * d + 1.0) / (240.0 * (2.0 * d * d - d - 2.0 * b * b + b)); // w1 = 0.0362942
224 double w2 = 1.0 / 24.0 - w1; // w2 = 0.0185787 (=0.111472/6)
225
226 mWeights.push_back(w1);
227 mWeights.push_back(w1);
228 mWeights.push_back(w1);
229 mWeights.push_back(w1);
230
231 mWeights.push_back(w2);
232 mWeights.push_back(w2);
233 mWeights.push_back(w2);
234 mWeights.push_back(w2);
235
236 mPoints.push_back(ChastePoint<3>(a, b, b));
237 mPoints.push_back(ChastePoint<3>(b, a, b));
238 mPoints.push_back(ChastePoint<3>(b, b, a));
239 mPoints.push_back(ChastePoint<3>(b, b, b));
240
241 mPoints.push_back(ChastePoint<3>(c, d, d));
242 mPoints.push_back(ChastePoint<3>(d, c, d));
243 mPoints.push_back(ChastePoint<3>(d, d, c));
244 mPoints.push_back(ChastePoint<3>(d, d, d));
245 }
246 break;
247
248 default:
249 EXCEPTION("Gauss quadrature order not supported.");
250 }
251 assert(mPoints.size() == mWeights.size());
252 mNumQuadPoints = mPoints.size();
253 }
254 else
255 {
256 EXCEPTION("Gauss quadrature rule not available for this dimension.");
257 }
258}
259
260// Explicit instantiation
261template class GaussianQuadratureRule<0>;
262template class GaussianQuadratureRule<1>;
263template class GaussianQuadratureRule<2>;
264template class GaussianQuadratureRule<3>;
265template class GaussianQuadratureRule<4>;
#define EXCEPTION(message)
const ChastePoint< ELEMENT_DIM > & rGetQuadPoint(unsigned index) const
GaussianQuadratureRule(unsigned quadratureOrder)
double GetWeight(unsigned index) const