aboutsummaryrefslogtreecommitdiff
path: root/src/optics.c
blob: 4a0722a36a362cd1b03af3c280038b6548943fdf (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#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>
#include "misc.h"
#include "db.h"
#include "dict.h"

char optics_err[256];

/* Absorption coefficients for H2O and D2O as a function of wavelength from
 * SNOMAN.
 *
 * Note: These numbers come from mcprod/media_qoca_d2o_20060110.cmd. */
static double absorption_coefficient_h2o_wavelengths[6] = {337.0, 365.0, 386.0, 420.0, 500.0, 620.0};
static double absorption_coefficient_h2o[6] = {8.410e-05, 2.880e-06, 1.431e-04, 1.034e-04, 5.239e-04, 2.482e-03};

static double rayleigh_coefficient_wavelengths[51];
static double rayleigh_coefficient_h2o[51];
static double rayleigh_coefficient_d2o[51];

static double absorption_coefficient_d2o_wavelengths[6] = {337.0, 365.0, 386.0, 420.0, 500.0, 620.0};
static double absorption_coefficient_d2o[6] = {6.057e-05, 2.680e-05, 3.333e-05, 3.006e-05, 2.773e-05, 2.736e-05};

static double absorption_coefficient_acrylic_wavelengths[6] = {337.0, 365.0, 386.0, 420.0, 500.0, 620.0};
static double absorption_coefficient_acrylic[6] = {5.610e-02, 2.279e-02, 1.204e-02, 7.587e-03, 7.036e-03, 7.068e-03};

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;
/* D2O Rayleigh scattering length weighted by the Cerenkov spectrum and the
 * quantum efficiency. */
static double rayleigh_scattering_length_d2o_weighted = 0.0;
/* H2O Rayleigh scattering length weighted by the Cerenkov spectrum and the
 * quantum efficiency. */
static double rayleigh_scattering_length_h2o_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_rayleigh_scattering_length_snoman_d2o(double wavelength, void *params)
{
    /* Returns the Rayleigh scattering length in D2O weighted by the quantum efficiency
     * and the Cerenkov spectrum. */
    double qe;

    qe = get_quantum_efficiency(wavelength);

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

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

    qe = get_quantum_efficiency(wavelength);

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

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(dict *db)
{
    int i;
    double norm;
    double result, error;
    size_t nevals;
    int status;
    gsl_integration_cquad_workspace *w;
    gsl_function F;
    dbval *rspr;

    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);
    }

    rspr = get_bank(db, "RSPR", 1);

    if (!rspr) {
        sprintf(optics_err, "failed to load RSPR bank\n");
        return -1;
    }

    for (i = 0; i < 51; i++) {
        rayleigh_coefficient_wavelengths[i] = rspr[30+i*3].u32;
        rayleigh_coefficient_d2o[i] = rspr[30+i*3+1].f;
        rayleigh_coefficient_h2o[i] = rspr[30+i*3+2].f;
    }

    F.function = &gsl_rayleigh_scattering_length_snoman_d2o;

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

    rayleigh_scattering_length_d2o_weighted = result/norm;

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

    F.function = &gsl_rayleigh_scattering_length_snoman_h2o;

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

    rayleigh_scattering_length_h2o_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_rayleigh_scattering_length_snoman_d2o(double wavelength)
{
    /* Returns the Rayleigh scattering length in D2O in cm. */
    return 1.0/interp1d(wavelength,rayleigh_coefficient_wavelengths, rayleigh_coefficient_d2o,LEN(rayleigh_coefficient_wavelengths));
}

double get_rayleigh_scattering_length_snoman_h2o(double wavelength)
{
    /* Returns the Rayleigh scattering length in H2O in cm. */
    return 1.0/interp1d(wavelength,rayleigh_coefficient_wavelengths, rayleigh_coefficient_h2o,LEN(rayleigh_coefficient_wavelengths));
}

double get_weighted_rayleigh_scattering_length_snoman_d2o(void)
{
    /* Returns the weighted Rayleigh scattering length in D2O in cm. */

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

    return rayleigh_scattering_length_d2o_weighted;
}

double get_weighted_rayleigh_scattering_length_snoman_h2o(void)
{
    /* Returns the weighted Rayleigh scattering length in H2O in cm. */

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

    return rayleigh_scattering_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);
}