/
Cubature.Rmd
228 lines (185 loc) · 8.53 KB
/
Cubature.Rmd
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
---
title: "Cubature methods"
date: "`r format(Sys.time(), '%d %B, %Y')`"
author: "baptiste Auguié"
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 2
fig_width: 7
fig_height: 4
fig_caption: true
vignette: >
%\VignetteIndexEntry{Cubature methods}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```
## Overview of available cubatures
The main function of the package is `cubs(N, method)`, where `N` is the requested number of integration points, and `method` one of the built-in cubature methods. The function returns a data.frame with three columns: `phi`, the azimuth ($\varphi\in [0,2\pi]$), `theta`, the polar angle ($\theta\in [0,\pi]$), and `weight` the associated cubature weight.
```{r demo, message=FALSE, echo=FALSE, results='asis'}
library(cubs)
library(glue)
for (method in c('lebedev', 'sphericaldesigns', "gl", 'fibonacci', 'grid', "qmc", "random")){
# cat(glue("", method))
print(knitr::kable(cubs(N = 5, method), caption = method))
}
```
Note that the requested number of points $N$ is interpreted as a minimum number; the exact number of points will vary for each method, some having relatively sparse coverage.
```{r granularity, echo=FALSE, fig.height=2}
library(purrr)
library(ggplot2)
library(dplyr)
cs <- list(qmc = cubs::qmc_table,
fibonacci = cubs::fibonacci_table,
gl = cubs::gl_table,
random = cubs::random_table,
grid = cubs::grid_table,
lebedev = cubs::lebedev_table$N,
sphericaldesigns = cubs::sphericaldesigns_table$N)
all <- purrr::map2_df(names(cs), cs, function(.x,.y)
data.frame(cubature = .x, N=.y, stringsAsFactors = FALSE))
all$cubature <- factor(all$cubature, levels = unique(all$cubature))
all$cubature <- factor(all$cubature,
levels = c("random","qmc","grid", "fibonacci", "gl", "sphericaldesigns", "lebedev"),
labels = c("random","qmc","grid", "fibonacci", "gl", "sd", "lebedev"))
ggplot(all %>% filter(N<200), aes(cubature, N, colour=cubature)) +
geom_point(pch='|',size=5) +
coord_flip() +
scale_y_continuous(expand=c(0,0),breaks=seq(0,200,by=50),lim=c(0,200))+
theme_bw() + guides(colour='none') +
theme(axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
axis.text.y = element_text(hjust = 1),
axis.text.x = element_text(),
plot.margin = margin(5,20,5,5),
panel.grid.major.x = element_line(colour = 'grey90'),
axis.ticks.x = element_line(), axis.ticks.length=unit(2,'mm'))+
labs(y = "N", colour='cubature')
```
## Test functions
We now compare the performance of these methods on 3 different integrands,
\begin{align}
f_{1}(x, y, z)=& 1+x+y^{2}+x^{2} y + x^{4}+y^{5}+x^{2} y^{2} z^{2} \\
f_{2}(x, y, z)=& \tfrac{3}{4} e^{\left[-(9 x-2)^{2} / 4-(9 y-2)^{2} / 4-(9 z-2)^{2} / 4\right]} +\tfrac{3}{4} e^{\left[-(9 x+1)^{2} / 49-(9 y+1) / 10-(9 z+1) / 10\right]} +\tfrac{1}{2} e^{\left[-(9 x-7)^{2} / 4-(9 y-3)^{2} / 4-(9 z-5)^{2} / 4\right]}-\tfrac{1}{5} e^{\left[-(9 x-4)^{2}-(9 y-7)^{2}-(9 z-5)^{2}\right]} \\
f_{3}(\varphi, \theta)=& \tfrac{1}{4\pi} + \cos(12\varphi)\sin^{12}(\theta)
\end{align}
with the usual spherical coordinates,
\begin{align}
x = &\cos(\varphi)\sin(\theta)\\
y = & \sin(\varphi)\sin(\theta)\\
z = & \cos(\theta).
\end{align}
```{r integrands, echo=FALSE, fig.height=2}
library(dplyr)
library(ggplot2)
g <- expand.grid(phi=seq(0,2*pi,length=360), theta=seq(0,pi,length=180))
g$f1 <- cubs:::f1(g$phi,g$theta)
g$f2 <- cubs:::f2(g$phi,g$theta)
g$f3 <- cubs:::f3(g$phi,g$theta)
library(tidyr)
gm <- g %>% pivot_longer(c('f1','f2','f3')) %>%
group_by(name) %>%
mutate(normalised = scales::rescale(value))
gm$name <- factor(gm$name, labels = c('f[1]','f[2]','f[3]'))
p <- ggplot(gm, aes(phi, theta, fill=normalised)) +
facet_grid(~name, labeller = label_parsed)+
geom_raster() +
coord_equal()+
scale_fill_viridis_c() +
theme_minimal(base_size = 12) +
theme(legend.position = 'none', panel.spacing.x = unit(3,'mm'),
axis.text.x = element_text(vjust=0.5)) +
scale_x_continuous(expand=c(0,0), breaks=seq(0,2,by=1/2)*pi, labels=expression(0,pi/2,pi,3*pi/2,2*pi)) +
scale_y_continuous(expand=c(0,0), breaks=seq(0,1,by=1/4)*pi, labels=expression(0,pi/4,pi/2,3*pi/4,pi)) +
labs(x=expression(varphi), y=expression(theta))
p
```
## Convergence comparison
We plot below the relative error against the known integrals ($I_1 = 216\pi/35$, $I_2 =6.6961822200736179523\dots$, $I_3=1$).
```{r params, echo=FALSE, results='asis'}
library(purrr)
cs <- list(
qmc = cubs::qmc_table[!as.logical(cubs::qmc_table %% 10)],
fibonacci = cubs::fibonacci_table[!as.logical(cubs::fibonacci_table %% 10)],
gl = cubs::gl_table,
random = cubs::random_table[!as.logical(cubs::random_table %% 10)],
grid = c(cubs::grid_table),
lebedev = cubs::lebedev_table$N,
sphericaldesigns = cubs::sphericaldesigns_table$N)
allcubs <- purrr::map2_df(names(cs), cs,
function(.x,.y) data.frame(cubature = .x, N=.y, stringsAsFactors = FALSE))
params <- allcubs %>% filter(N <= 5000) %>%
arrange(N, cubature) %>% mutate(ID = row_number())
```
```{r convergence, echo=FALSE, fig.height=3}
set.seed(123)
I1 <- 216*pi/35
I2 <- 6.6961822200736179523
I3 <- 1
test_quadrature <- function(ii){
q <- data.frame(cubs(params$N[ii], params$cubature[ii]))
vals1 <- cubs:::f1(q$phi, q$theta)
vals2 <- cubs:::f2(q$phi, q$theta)
vals3 <- cubs:::f3(q$phi, q$theta)
d1 <- data.frame(sum1 = 4*pi*sum(vals1 * q$weight),
sum2 = 4*pi*sum(vals2 * q$weight),
sum3 = 4*pi*sum(vals3 * q$weight))
mutate(d1, ID=ii,
err1 = abs(sum1 - I1)/I1,
err2 = abs(sum2 - I2)/I2,
err3 = abs(sum3 - I3)/I3)
}
res <- map_df(params$ID, test_quadrature)
m <- left_join(params, res, by='ID') %>% pivot_longer(c("err1",'err2','err3'))
m[m$value <1e-16, 'value'] <- 1e-16
m$cubname <- factor(m$cubature, levels = c("sphericaldesigns", "lebedev", "gl", "grid", "fibonacci",
"random", "qmc"),
labels = c("sph. des.", "Lebedev", "Gauß-L.", "grid", "Fibonacci",
"random", "QMC"))
name.labs <- setNames(c("rel. error (f1)","rel. error (f2)","rel. error (f3)"),
c("err1",'err2','err3'))
ggplot(m, aes(N, value, colour=cubname)) +
geom_line()+
scale_colour_brewer(palette = 'Set1') +
facet_grid(.~name, labeller = labeller(name = name.labs)) +
annotation_logticks(sides = 'b', colour = 'grey70', size = 0.15,
long = unit(2,'mm'), mid = unit(1,'mm')) +
scale_y_log10(lim=c(10^(-16),10^1.2), minor_breaks=10^seq(-16,4),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x))
) +
scale_x_log10(lim=c(2,10^4),
minor_breaks=scales::log_breaks(n=35),
expand=c(0,0),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x))
) +
theme_minimal(base_size = 12) +
theme(legend.position='top', #
# legend.position = c(0.32,0.35),
panel.spacing.x = unit(3,'mm'),
strip.placement.y = 'outside',
panel.background = element_rect(colour = 'grey20', size = 0.2),
panel.grid.major = element_line(colour = 'grey70', size = 0.15),
panel.grid.minor = element_line(colour = 'grey70', size = 0.05),
legend.background = element_rect(fill='white'),
strip.text.y = element_blank(),
strip.text.x = element_blank(),
# legend.text = element_text(size=8),
legend.title = element_blank(),
legend.key.height = unit(3,'mm'),
legend.key.width = unit(3,'mm'),
# axis.title.y = element_blank(),
plot.margin = margin(0,0,0,r = 2),
legend.margin = margin(1, 1, 1, 1,'mm'),
legend.spacing.x = unit(2, "mm"),
legend.spacing.y = unit(0, "mm")) +
# guides(colour=guide_legend(nrow=1)) +
labs(x = expression(N[inc]), y='relative error',
colour='')
```
Three methods stand out and vastly outperform the others: Lebedev, Spherical t-Designs, and (somewhat behind) Gauß-Legendre. These cubature methods are known to integrate exactly spherical harmonics up to a given order, and are in that sense optimal choices (assuming the integrand is well-behaved in this basis).