/
sphere-quad.cpp
264 lines (233 loc) · 8.08 KB
/
sphere-quad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Copyright (C) 2005-2012 Massachusetts Institute of Technology.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This file is compiled into a program sphere_quad that is used to
generate the file sphere-quad.h, which is a table of quadrature
points and weights for integrating on spheres in 1d/2d/3d. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define SHIFT3(x,y,z) {double SHIFT3_dummy = z; z = y; y = x; x = SHIFT3_dummy;}
#define CHECK(condition, message) do { \
if (!(condition)) { \
fprintf(stderr, "CHECK failure on line %d of " __FILE__ ": " \
message "\n", __LINE__); exit(EXIT_FAILURE); \
} \
} while (0)
/* Compute quadrature points and weights for integrating on the
unit sphere. x, y, z, and weight should be arrays of num_sq_pts
values to hold the coordinates and weights of the quadrature points
on output. Currently, num_sq_pts = 12, 50, and 72 are supported. */
void spherical_quadrature_points(double *x, double *y, double *z,
double *weight, int num_sq_pts)
{
int i,j,k,l, n = 0;
double x0, y0, z0, w;
if (num_sq_pts == 50) {
/* Computes quadrature points and weights for 50-point 11th degree
integration formula on a unit sphere. This particular quadrature
formula has the advantage, for our purposes, of preserving the
symmetry group of an octahedron (i.e. simple cubic symmetry, with
respect to the Cartesian xyz axes). References:
A. D. McLaren, "Optimal Numerical Integration on a Sphere,"
Math. Comp. 17, pp. 361-383 (1963).
Also in: Arthur H. Stroud, "Approximate Calculation of Multiple
Integrals" (Prentice Hall, 1971) (formula number U3:11-1).
This code was written with the help of example code by
John Burkardt:
http://www.psc.edu/~burkardt/src_pt/stroud/stroud.html */
x0 = 1; y0 = z0 = 0;
w = 9216 / 725760.0;
for (i = 0; i < 2; ++i) {
x0 = -x0;
for (j = 0; j < 3; ++j) {
SHIFT3(x0,y0,z0);
x[n] = x0; y[n] = y0; z[n] = z0; weight[n++] = w;
}
}
x0 = y0 = sqrt(0.5); z0 = 0;
w = 16384 / 725760.0;
for (i = 0; i < 2; ++i) {
x0 = -x0;
for (j = 0; j < 2; ++j) {
y0 = -y0;
for (k = 0; k < 3; ++k) {
SHIFT3(x0,y0,z0);
x[n] = x0; y[n] = y0; z[n] = z0; weight[n++] = w;
}
}
}
x0 = y0 = z0 = sqrt(1.0 / 3.0);
w = 15309 / 725760.0;
for (i = 0; i < 2; ++i) {
x0 = -x0;
for (j = 0; j < 2; ++j) {
y0 = -y0;
for (k = 0; k < 2; ++k) {
z0 = -z0;
x[n] = x0; y[n] = y0; z[n] = z0; weight[n++] = w;
}
}
}
x0 = y0 = sqrt(1.0 / 11.0); z0 = 3 * x0;
w = 14641 / 725760.0;
for (i = 0; i < 2; ++i) {
x0 = -x0;
for (j = 0; j < 2; ++j) {
y0 = -y0;
for (k = 0; k < 2; ++k) {
z0 = -z0;
for (l = 0; l < 3; ++l) {
SHIFT3(x0,y0,z0);
x[n] = x0; y[n] = y0; z[n] = z0; weight[n++] = w;
}
}
}
}
}
else if (num_sq_pts == 72 || num_sq_pts == 12) {
/* As above (same references), but with a 72-point 14th degree
formula, this time with the symmetry group of an icosohedron.
(Stroud formula number U3:14-1.) Alternatively, just use
the 12-point 5th degree formula consisting of the vertices
of a regular icosohedron. */
/* first, the vertices of an icosohedron: */
x0 = sqrt(0.5 - sqrt(0.05));
y0 = sqrt(0.5 + sqrt(0.05));
z0 = 0;
if (num_sq_pts == 72)
w = 125 / 10080.0;
else
w = 1 / 12.0;
for (i = 0; i < 2; ++i) {
x0 = -x0;
for (j = 0; j < 2; ++j) {
y0 = -y0;
for (k = 0; k < 3; ++k) {
SHIFT3(x0,y0,z0);
x[n] = x0; y[n] = y0; z[n] = z0; weight[n++] = w;
}
}
}
if (num_sq_pts == 72) {
/* it would be nice, for completeness, to have more
digits here: */
double coords[3][5] = {
{ -0.151108275, 0.315838353, 0.346307112, -0.101808787, -0.409228403 },
{ 0.155240600, 0.257049387, 0.666277790, 0.817386065, 0.501547712 },
{ 0.976251323, 0.913330032, 0.660412970, 0.567022920, 0.762221757 }
};
w = 143 / 10080.0;
for (l = 0; l < 5; ++l) {
x0 = coords[0][l]; y0 = coords[1][l]; z0 = coords[2][l];
for (i = 0; i < 3; ++i) {
double dummy = x0;
x0 = z0;
z0 = -y0;
y0 = -dummy;
for (j = 0; j < 3; ++j) {
SHIFT3(x0,y0,z0);
x[n] = x0; y[n] = y0; z[n] = z0; weight[n++] = w;
}
y0 = -y0;
z0 = -z0;
x[n] = x0; y[n] = y0; z[n] = z0; weight[n++] = w;
}
}
}
}
else
CHECK(0, "spherical_quadrature_points: passed unknown # points!");
CHECK(n == num_sq_pts,
"bug in spherical_quadrature_points: wrong number of points!");
}
#define NQUAD3 50 /* use 50-point quadrature formula by default */
/**********************************************************************/
#define K_PI 3.141592653589793238462643383279502884197
#define NQUAD2 12
/**********************************************************************/
double sqr(double x) { return x * x; }
double dist2(double x1, double y1, double z1,
double x2, double y2, double z2)
{
return sqr(x1-x2) + sqr(y1-y2) + sqr(z1-z2);
}
double min2(double a, double b) { return a < b ? a : b; }
/* sort the array to maximize the spacing of each point with the
previous points */
void sort_by_distance(int n, double x[], double y[], double z[], double w[])
{
for (int i = 1; i < n; ++i) {
double d2max = 0;
double d2maxsum = 0;
int jmax = i;
for (int j = i; j < n; ++j) {
double d2min = 1e20, d2sum = 0;
for (int k = 0; k < i; ++k) {
double d2 = float(dist2(x[k],y[k],z[k], x[j],y[j],z[j]));
d2min = min2(d2min, d2);
d2sum += d2;
}
if (d2min > d2max ||
(d2min == d2max && d2sum > d2maxsum)) {
d2max = d2min;
d2maxsum = d2sum;
jmax = j;
}
}
double xi = x[i], yi = y[i], zi = z[i], wi = w[i];
x[i] = x[jmax]; y[i] = y[jmax]; z[i] = z[jmax]; w[i] = w[jmax];
x[jmax] = xi; y[jmax] = yi; z[jmax] = zi; w[jmax] = wi;
}
}
int main(void)
{
int i;
double x2[NQUAD2], y2[NQUAD2], z2[NQUAD2], w2[NQUAD2];
double x3[NQUAD3], y3[NQUAD3], z3[NQUAD3], w3[NQUAD3];
printf(
"/* For 1d, 2d, and 3d, quadrature points and weights on a unit sphere.\n"
" There are num_sphere_quad[dim-1] points i, with the i-th point at\n"
" (x,y,z) = (sphere_quad[dim-1][i][ 0, 1, 2 ]), and with a quadrature\n"
" weight sphere_quad[dim-1][i][3]. */\n\n");
printf("static const int num_sphere_quad[3] = { %d, %d, %d };\n\n",
2, NQUAD2, NQUAD3);
printf("static const double sphere_quad[3][%d][4] = {\n", NQUAD3);
printf(" { {0,0,1,0.5}, {0,0,-1,0.5} },\n");
for (i = 0; i < NQUAD2; ++i) {
x2[i] = cos(2*i * K_PI / NQUAD2);
y2[i] = sin(2*i * K_PI / NQUAD2);
z2[i] = 0.0;
w2[i] = 1.0 / NQUAD2;
}
sort_by_distance(NQUAD2,x2,y2,z2,w2);
printf(" {\n");
for (i = 0; i < NQUAD2; ++i) {
printf(" { %0.20g, %0.20g, %0.20g, %0.20g },\n",
x2[i], y2[i], z2[i], w2[i]);
}
printf(" },\n");
printf(" {\n");
spherical_quadrature_points(x3,y3,z3, w3, NQUAD3);
sort_by_distance(NQUAD3,x3,y3,z3,w3);
for (i = 0; i < NQUAD3; ++i) {
printf(" { %0.20g, %0.20g, %0.20g, %0.20g },\n",
x3[i], y3[i], z3[i], w3[i]);
}
printf(" }\n");
printf("};\n");
return 0;
}