aboutsummaryrefslogtreecommitdiff
path: root/src/optics.c
blob: 5f01089226627d6fb6c3f26d7b194ece00275978 (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <math.h>
#include "optics.h"
#include "misc.h"
#include "quantum_efficiency.h"
#include <stdio.h> /* for fprintf() */
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h> /* for gsl_strerror() */
#include <gsl/gsl_spline.h>

/* Macro to compute the size of a static C array.
 *
 * See https://stackoverflow.com/questions/1598773. */
#define LEN(x) ((sizeof(x)/sizeof(0[x]))/((size_t)(!(sizeof(x) % sizeof(0[x])))))

/* Absorption coefficients for H2O and D2O as a function of wavelength from
 * SNOMAN.
 *
 * Note: These numbers come from prod/media.dat.
 *
 * FIXME: Should I be using the values from media_2_09.dat or media_qoca_*.cmd? */
static double absorption_coefficient_h2o_wavelengths[7] = {250.0, 350.0, 400.0, 450.0, 500.0, 548.0, 578.0};
static double absorption_coefficient_h2o[7] = {1e-5, 1e-5, 1.3e-5, 6.0e-5, 2.0e-4, 5.8e-4, 9.2e-4};

static double absorption_coefficient_d2o_wavelengths[7] = {254.0, 313.0, 366.0, 406.0, 436.0, 548.0, 578.0};
static double absorption_coefficient_d2o[7] = {1e-5, 1e-5, 1e-5, 1e-5, 1e-5, 1e-5, 1e-5};

/* From prod/media.dat for Acrylic (good). */
static double absorption_coefficient_acrylic_wavelengths[10] = {300.0, 310.0, 320.0, 330.0, 340.0, 350.0, 360.0, 380.0, 400.0, 450.0};
static double absorption_coefficient_acrylic[10] = {0.3641, 0.0966, 0.0667, 0.049, 0.035, 0.0261, 0.0194, 0.0115, 0.0086, 0.0071};

static int initialized = 0;

/* D2O absorption length weighted by the Cerenkov spectrum and the quantum
 * efficiency. */
static double absorption_length_d2o_weighted = 0.0;
/* H2O absorption length weighted by the Cerenkov spectrum and the quantum
 * efficiency. */
static double absorption_length_h2o_weighted = 0.0;
/* Acrylic absorption length weighted by the Cerenkov spectrum and the quantum
 * efficiency. */
static double absorption_length_acrylic_weighted = 0.0;


/* From Table 4 in the paper. */
static double A0 = 0.243905091;
static double A1 = 9.53518094e-3;
static double A2 = -3.64358110e-3;
static double A3 = 2.65666426e-4;
static double A4 = 1.59189325e-3;
static double A5 = 2.45733798e-3;
static double A6 = 0.897478251;
static double A7 = -1.63066183e-2;
static double UV = 0.2292020;
static double IR = 5.432937;

static double RIND_H2O_C1 = 1.302;
static double RIND_H2O_C2 = 0.01562;
static double RIND_H2O_C3 = 0.32;

static double RIND_D2O_C1 = 1.302;
static double RIND_D2O_C2 = 0.01333;
static double RIND_D2O_C3 = 0.32;

/* Wavelength of 1 eV particle in nm.
 *
 * From http://pdg.lbl.gov/2018/reviews/rpp2018-rev-phys-constants.pdf. */
static double HC = 1.239841973976e3;

static double gsl_absorption_length_snoman_d2o(double wavelength, void *params)
{
    /* Returns the absorption length in D2O weighted by the quantum efficiency
     * and the Cerenkov spectrum. */
    double qe;

    qe = get_quantum_efficiency(wavelength);

    return qe*get_absorption_length_snoman_d2o(wavelength)/pow(wavelength,2);
}

static double gsl_absorption_length_snoman_h2o(double wavelength, void *params)
{
    /* Returns the absorption length in H2O weighted by the quantum efficiency
     * and the Cerenkov spectrum. */
    double qe;

    qe = get_quantum_efficiency(wavelength);

    return qe*get_absorption_length_snoman_h2o(wavelength)/pow(wavelength,2);
}

static double gsl_absorption_length_snoman_acrylic(double wavelength, void *params)
{
    /* Returns the absorption length in acrylic weighted by the quantum
     * efficiency and the Cerenkov spectrum. */
    double qe;

    qe = get_quantum_efficiency(wavelength);

    return qe*get_absorption_length_snoman_acrylic(wavelength)/pow(wavelength,2);
}

static double gsl_cerenkov(double wavelength, void *params)
{
    /* Returns the quantum efficiency multiplied by the Cerenkov spectrum. */
    double qe;

    qe = get_quantum_efficiency(wavelength);

    return qe/pow(wavelength,2);
}

int optics_init(void)
{
    double norm;
    double result, error;
    size_t nevals;
    int status;
    gsl_integration_cquad_workspace *w;
    gsl_function F;

    w = gsl_integration_cquad_workspace_alloc(100);

    F.function = &gsl_cerenkov;
    F.params = NULL;

    status = gsl_integration_cquad(&F, 200, 800, 0, 1e-2, w, &norm, &error, &nevals);

    if (status) {
        fprintf(stderr, "error integrating cerenkov distribution: %s\n", gsl_strerror(status));
        exit(1);
    }

    F.function = &gsl_absorption_length_snoman_d2o;

    status = gsl_integration_cquad(&F, 200, 800, 0, 1e-2, w, &result, &error, &nevals);

    absorption_length_d2o_weighted = result/norm;

    if (status) {
        fprintf(stderr, "error integrating cerenkov distribution: %s\n", gsl_strerror(status));
        exit(1);
    }

    F.function = &gsl_absorption_length_snoman_h2o;

    status = gsl_integration_cquad(&F, 200, 800, 0, 1e-2, w, &result, &error, &nevals);

    absorption_length_h2o_weighted = result/norm;

    if (status) {
        fprintf(stderr, "error integrating cerenkov distribution: %s\n", gsl_strerror(status));
        exit(1);
    }

    F.function = &gsl_absorption_length_snoman_acrylic;

    status = gsl_integration_cquad(&F, 200, 800, 0, 1e-2, w, &result, &error, &nevals);

    absorption_length_acrylic_weighted = result/norm;

    if (status) {
        fprintf(stderr, "error integrating cerenkov distribution: %s\n", gsl_strerror(status));
        exit(1);
    }

    gsl_integration_cquad_workspace_free(w);

    initialized = 1;

    return 0;
}

double get_weighted_absorption_length_snoman_h2o(void)
{
    /* Returns the weighted absorption length in H2O in cm. */

    if (!initialized) {
        fprintf(stderr, "weighted absorption length hasn't been initialized!\n");
        exit(1);
    }

    return absorption_length_h2o_weighted;
}

double get_absorption_length_snoman_h2o(double wavelength)
{
    /* Returns the absorption length in H2O in cm. */
    static gsl_spline *spline;
    static gsl_interp_accel *acc;

    if (!spline) {
        spline = gsl_spline_alloc(gsl_interp_linear, LEN(absorption_coefficient_h2o_wavelengths));
        gsl_spline_init(spline, absorption_coefficient_h2o_wavelengths, absorption_coefficient_h2o, LEN(absorption_coefficient_h2o_wavelengths));
        acc = gsl_interp_accel_alloc();
    }

    if (wavelength < absorption_coefficient_h2o_wavelengths[0])
        wavelength = absorption_coefficient_h2o_wavelengths[0];

    if (wavelength > absorption_coefficient_h2o_wavelengths[LEN(absorption_coefficient_h2o_wavelengths)-1])
        wavelength = absorption_coefficient_h2o_wavelengths[LEN(absorption_coefficient_h2o_wavelengths)-1];

    return 1.0/gsl_spline_eval(spline, wavelength, acc);
}

double get_weighted_absorption_length_snoman_d2o(void)
{
    /* Returns the weighted absorption length in D2O in cm. */

    if (!initialized) {
        fprintf(stderr, "weighted absorption length hasn't been initialized!\n");
        exit(1);
    }

    return absorption_length_d2o_weighted;
}

double get_absorption_length_snoman_d2o(double wavelength)
{
    /* Returns the absorption length in D2O in cm. */
    static gsl_spline *spline;
    static gsl_interp_accel *acc;

    if (!spline) {
        spline = gsl_spline_alloc(gsl_interp_linear, LEN(absorption_coefficient_d2o_wavelengths));
        gsl_spline_init(spline, absorption_coefficient_d2o_wavelengths, absorption_coefficient_d2o, LEN(absorption_coefficient_d2o_wavelengths));
        acc = gsl_interp_accel_alloc();
    }

    if (wavelength < absorption_coefficient_d2o_wavelengths[0])
        wavelength = absorption_coefficient_d2o_wavelengths[0];

    if (wavelength > absorption_coefficient_d2o_wavelengths[LEN(absorption_coefficient_d2o_wavelengths)-1])
        wavelength = absorption_coefficient_d2o_wavelengths[LEN(absorption_coefficient_d2o_wavelengths)-1];

    return 1.0/gsl_spline_eval(spline, wavelength, acc);
}

double get_weighted_absorption_length_snoman_acrylic(void)
{
    /* Returns the weighted absorption length in acrylic in cm. */

    if (!initialized) {
        fprintf(stderr, "weighted absorption length hasn't been initialized!\n");
        exit(1);
    }

    return absorption_length_acrylic_weighted;
}

double get_absorption_length_snoman_acrylic(double wavelength)
{
    /* Returns the absorption length in acrylic in cm. */
    static gsl_spline *spline;
    static gsl_interp_accel *acc;

    if (!spline) {
        spline = gsl_spline_alloc(gsl_interp_linear, LEN(absorption_coefficient_acrylic_wavelengths));
        gsl_spline_init(spline, absorption_coefficient_acrylic_wavelengths, absorption_coefficient_acrylic, LEN(absorption_coefficient_acrylic_wavelengths));
        acc = gsl_interp_accel_alloc();
    }

    if (wavelength < absorption_coefficient_acrylic_wavelengths[0])
        wavelength = absorption_coefficient_acrylic_wavelengths[0];

    if (wavelength > absorption_coefficient_acrylic_wavelengths[LEN(absorption_coefficient_acrylic_wavelengths)-1])
        wavelength = absorption_coefficient_acrylic_wavelengths[LEN(absorption_coefficient_acrylic_wavelengths)-1];

    return 1.0/gsl_spline_eval(spline, wavelength, acc);
}

double get_index(double p, double wavelength, double T)
{
    /* Returns the index of refraction of pure water for a given density,
     * wavelength, and temperature. The density should be in units of g/cm^3,
     * the wavelength in nm, and the temperature in Celsius.
     *
     * See "Refractive Index of Water and Steam as a function of Wavelength,
     * Temperature, and Density" by Schiebener et al. 1990. */
    /* normalize the temperature and pressure */
    wavelength = wavelength/589.0;
    T = (T+273.15)/273.15;

    /* first we compute the right hand side of Equation 7 */
    double c = A0 + A1*p + A2*T + A3*pow(wavelength,2)*T + A4/pow(wavelength,2) + A5/(pow(wavelength,2)-pow(UV,2)) + A6/(pow(wavelength,2)-pow(IR,2)) + A7*pow(p,2);
    c *= p;

    return sqrt((2*c+1)/(1-c));
}

double get_index_snoman_h2o(double wavelength)
{
    /* Returns the index of refraction of light water that SNOMAN uses for a
     * given wavelength.  The wavelength should be in units of nm.
     *
     * The coefficients come from media.dat in SNOMAN version 5.0294 and the
     * formula used to compute the index of refraction comes from the SNOMAN
     * companion page for titles MEDA. */
    double E;

    /* Calculate the energy of the photon in eV. */
    E = HC/wavelength;

    return RIND_H2O_C1 + RIND_H2O_C2*exp(RIND_H2O_C3*E);
}

double get_index_snoman_d2o(double wavelength)
{
    /* Returns the index of refraction of heavy water that SNOMAN uses for a
     * given wavelength.  The wavelength should be in units of nm.
     *
     * The coefficients come from media.dat in SNOMAN version 5.0294 and the
     * formula used to compute the index of refraction comes from the SNOMAN
     * companion page for titles MEDA. */
    double E;

    /* Calculate the energy of the photon in eV. */
    E = HC/wavelength;

    return RIND_D2O_C1 + RIND_D2O_C2*exp(RIND_D2O_C3*E);
}