aboutsummaryrefslogtreecommitdiff
path: root/teukolsky_data.h
blob: d95dbfe3f1cf3e4232d48ed243dc978cd6664db2 (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
/*
 * Copyright 2017 Anton Khirnov <anton@khirnov.net>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#ifndef TEUKOLSKY_DATA_H
#define TEUKOLSKY_DATA_H

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

/**
 * API usage:
 *
 * First, allocate the solver context with td_context_alloc(). All interaction
 * with the solver is done through this context.
 *
 * Fill any fields in the context that are described as settable by the caller.
 * Call td_solve() to solve the equation determined by the option values.
 *
 * The metric/extrinsic curvature can be evaluated with td_eval_metric()/td_eval_curv().
 *
 * Finally, free the solver context with td_context_free().
 */

/**
 * Identifies the specific initial data family to use.
 */
enum TDFamily {
    /**
     * The time-antisymmetric initial data used in Abrahams&Evans PhysRevD v49,n8 (1994).
     * Conformally flat spatial metric.
     *  r                / x        x  3 \        /    x  2  \
     * K  = -60√(2/π) a | ---  -  (---)   | * exp| - (---)    | sin(2θ)
     *  θ                \ L        L    /        \    L     /
     */
    TD_FAMILY_AE_TIME_ANTISYM = 0,
    /**
     * Simpler time-antisymmetric initial data.
     * Conformally flat spatial metric.
     *  r       x       /    x  2  \
     * K  = a  ---  exp| - (---)    | sin(2θ)
     *  θ       L       \    L     /
     */
    TD_FAMILY_SIMPLE_TIME_ANTISYM,
};

typedef struct TDContext {
    /**
     *  Solver internals, not to be accessed by the caller
     */
    void *priv;

    /********************************
     *  options, set by the caller  *
     ********************************/

    /**
     * A callback that will be used to print diagnostic messages.
     *
     * Defaults to fprintf(stderr, ...)
     */
    void (*log_callback)(const struct TDContext *td, int level,
                         const char *fmt, va_list);

    /**
     * Arbitrary user data, e.g. to be used by the log callback.
     */
    void *opaque;

    /********************************
     *  initial data parameters     *
     ********************************/

    /**
     * The amplitude in the I function.
     * Defaults to 1.
     */
    double amplitude;

    /********************************
     *        solver options        *
     ********************************/

    /**
     * The number of basis functions in each direction.
     * [0] - radial, [1] - angular
     */
    unsigned int nb_coeffs[2];

    /**
     * The scaling factor used in the basis functions.
     * [0] - radial, [1] - angular
     */
    double basis_scale_factor[2];

    /**
     * Maximum number of Newton iterations.
     */
    unsigned int max_iter;

    /**
     * Absolute tolerance. The solver is deemed to have converged
     * after maximum difference between iterations is below this bound.
     */
    double atol;

    /**
     * Number of threads to use for parallelization. Set to 0 to automatically
     * detect according to the number of CPU cores.
     */
    unsigned int nb_threads;

    double *coeffs[3];

    unsigned int solution_branch;

    enum TDFamily family;
} TDContext;

/**
 * Allocate and initialize the solver.
 */
TDContext *td_context_alloc(void);

/**
 * Free the solver and everything associated with it.
 */
void td_context_free(TDContext **td);

/**
 * Solve the equation for the conformal factor ψ and export the expansion coefficients in the
 * context.
 *
 * @return >= 0 on success, a negative error code on failure
 */
int td_solve(TDContext *td, double *coeffs_init[3]);

/**
 * Evaluate the 3-metric γ_ij at the specified rectangular grid (in cylindrical
 * coordinates { ρ, z, φ }).
 *
 * @param td the solver context
 * @param rho the array of ρ coordinates.
 * @param nb_coords_rho the number of elements in rho.
 * @param z   the array of z coordinates.
 * @param nb_coords_z the number of elements in z.
 * @param nb_comp number of needed components of the metric
 * @param comp a nb_comp-sized array specifying the components of the metric to evaluate
 * @param diff_order a nb_comp-sized array specifying the order of the derivatives of the
 *                   metric to evaluate. The first element specifies the derivative wrt ρ,
 *                   the second wrt z. I.e. diff_order[i] = { 0, 0 } evaluates the metric
 *                   itself, diff_order[i] = { 0, 1 } evaluates ∂γ/∂z etc.
 * @param out a nb_comp-sized array of pointers to the arrays into which the values of the
 *            metric will be written. Each requested component is evaluated on the grid
 *            formed by the outer product of the rho and z vectors. I.e.
 *            out[l][j * out_strides[l] + i] = γ_comp[l](rho[i], z[j]). The length of each
 *            array in out must be nb_coords_rho * nb_coords_z.
 * @param out_strides a nb_comp-sized array of distances (in double-sized elements), for each
 *                    array in out, between two elements corresponding to the same ρ but one
 *                    step in z. Each element in out_strides must be at least nb_coords_rho.
 *
 * @return >= 0 on success, a negative error code on failure.
 */
int td_eval_psi(const TDContext *td,
                size_t nb_coords, const double *r, const double *theta,
                const unsigned int diff_order[2],
                double *out);
int td_eval_krr(const TDContext *td,
                size_t nb_coords, const double *r, const double *theta,
                const unsigned int diff_order[2],
                double *out);
int td_eval_kpp(const TDContext *td,
                size_t nb_coords, const double *r, const double *theta,
                const unsigned int diff_order[2],
                double *out);
int td_eval_krt(const TDContext *td,
                size_t nb_coords, const double *r, const double *theta,
                const unsigned int diff_order[2],
                double *out);

#endif /* TEUKOLSKY_DATA_H */