ASCOT5
Loading...
Searching...
No Matches
mhd_nonstat.c
Go to the documentation of this file.
1
5#include <stdlib.h>
6#include "../ascot5.h"
7#include "../print.h"
8#include "../error.h"
9#include "../boozer.h"
10#include "../spline/interp.h"
11#include "../B_field.h"
12#include "../math.h"
13#include "../mhd.h"
14#include "mhd_nonstat.h"
15
23int mhd_nonstat_init(mhd_nonstat_data* data, int nmode, int nrho, int ntime,
24 real rhomin, real rhomax, real tmin, real tmax,
25 int* moden, int* modem, real* amplitude_nm,
26 real* omega_nm, real* phase_nm, real* alpha, real* phi) {
27
28 int err = 0;
29 data->n_modes = nmode;
30 data->rho_min = rhomin;
31 data->rho_max = rhomax;
32 data->nmode = (int*) malloc(nmode * sizeof(int));
33 data->mmode = (int*) malloc(nmode * sizeof(int));
34 data->omega_nm = (real*) malloc(nmode * sizeof(real));
35 data->phase_nm = (real*) malloc(nmode * sizeof(real));
36 data->amplitude_nm = (real*) malloc(nmode * sizeof(real));
37 data->phi_nm = (interp2D_data*) malloc(nmode * sizeof(interp2D_data));
38 data->alpha_nm = (interp2D_data*) malloc(nmode * sizeof(interp2D_data));
39 for(int i = 0; i < nmode; i++) {
40 data->nmode[i] = moden[i];
41 data->mmode[i] = modem[i];
42 data->omega_nm[i] = omega_nm[i];
43 data->phase_nm[i] = phase_nm[i];
44 data->amplitude_nm[i] = amplitude_nm[i];
45
46 err = interp2Dcomp_setup(&data->alpha_nm[i], &alpha[i*nrho*ntime],
47 nrho, ntime, NATURALBC, NATURALBC,
48 rhomin, rhomax, tmin, tmax);
49 if(err) {
50 print_err("Error: Failed to initialize splines.\n");
51 return err;
52 }
53 err = interp2Dcomp_setup(&data->phi_nm[i], &phi[i*nrho*ntime],
54 nrho, ntime, NATURALBC, NATURALBC,
55 rhomin, rhomax, tmin, tmax);
56 if(err) {
57 print_err("Error: Failed to initialize splines.\n");
58 return err;
59 }
60 }
61
62 /* Print some sanity check on data */
63 print_out(VERBOSE_IO, "\nMHD input (non-stationary)\n");
64 print_out(VERBOSE_IO, "Grid: nrho = %4.d rhomin = %3.3f rhomax = %3.3f\n",
65 nrho, data->rho_min, data->rho_max);
66 print_out(VERBOSE_IO, " ntime = %4.d tmin = %3.3f tmax = %3.3f\n",
67 ntime, tmin, tmax);
68
69 print_out(VERBOSE_IO, "\nModes:\n");
70 for(int i = 0; i < nmode; i++) {
72 "(n,m) = (%2.d,%2.d) Amplitude = %3.3g Frequency = %3.3g"
73 " Phase = %3.3g\n",
74 data->nmode[i], data->mmode[i], data->amplitude_nm[i],
75 data->omega_nm[i], data->phase_nm[i]);
76 }
77
78 return err;
79}
80
87 for(int i = 0; i < data->n_modes; i++) {
88 free(data->phi_nm[i].c);
89 free(data->alpha_nm[i].c);
90 }
91 free(data->nmode);
92 free(data->mmode);
93 free(data->phase_nm);
94 free(data->omega_nm);
95 free(data->amplitude_nm);
96}
97
104 //TODO: Implement
105}
106
137a5err mhd_nonstat_eval(real mhd_dmhd[10], real r, real phi, real z, real t,
138 int includemode, boozer_data* boozerdata,
139 mhd_nonstat_data* mhddata, B_field_data* Bdata) {
140
141 a5err err = 0;
142
143 real ptz[12];
144 int isinside;
145 if(!err) {
146 err = boozer_eval_psithetazeta(ptz, &isinside, r, phi, z, Bdata,
147 boozerdata);
148 }
149 real rho[2];
150 if(!err && isinside) {
151 err = B_field_eval_rho(rho, ptz[0], Bdata);
152 }
153
154 int iterations = mhddata->n_modes;
155
156 /* Initialize values */
157 for(int i=0; i<10; i++) {
158 mhd_dmhd[i] = 0;
159 }
160
161 int interperr = 0;
162 for(int i = 0; i < iterations; i++){
163 if( includemode != MHD_INCLUDE_ALL && includemode != i ) { continue; }
164 /* Get interpolated values */
165 real a_da[6], phi_dphi[6];
166 interperr += interp2Dcomp_eval_df(a_da, &(mhddata->alpha_nm[i]),
167 rho[0], t);
168 interperr += interp2Dcomp_eval_df(phi_dphi, &(mhddata->phi_nm[i]),
169 rho[0], t);
170
171 /* The interpolation returns dx/drho but we require dx/dpsi. Second
172 order derivatives are not needed. */
173 a_da[1] *= rho[1];
174 phi_dphi[1] *= rho[1];
175
176 /* These are used frequently, so store them in separate variables */
177 real mhdarg = mhddata->nmode[i] * ptz[8]
178 - mhddata->mmode[i] * ptz[4]
179 - mhddata->omega_nm[i] * t
180 + mhddata->phase_nm[i];
181 real sinmhd = sin(mhdarg);
182 real cosmhd = cos(mhdarg);
183
184 /* Sum over modes to get alpha, phi */
185 mhd_dmhd[0] += a_da[0] * mhddata->amplitude_nm[i] * cosmhd;
186 mhd_dmhd[5] += phi_dphi[0] * mhddata->amplitude_nm[i] * cosmhd;
187
188 /* Time derivatives */
189 mhd_dmhd[1] += a_da[0] * mhddata->amplitude_nm[i]
190 * mhddata->omega_nm[i] * sinmhd
191 + a_da[2] * mhddata->amplitude_nm[i] * cosmhd;
192 mhd_dmhd[6] += phi_dphi[0] * mhddata->amplitude_nm[i]
193 * mhddata->omega_nm[i] * sinmhd
194 + phi_dphi[2] * mhddata->amplitude_nm[i] * cosmhd;
195
196 /* R component of gradients */
197 mhd_dmhd[2] += mhddata->amplitude_nm[i]
198 * ( a_da[1] * ptz[1] * cosmhd
199 + a_da[0] * mhddata->mmode[i] * ptz[5] * sinmhd
200 - a_da[0] * mhddata->nmode[i] * ptz[9] * sinmhd);
201 mhd_dmhd[7] += mhddata->amplitude_nm[i]
202 * ( phi_dphi[1] * ptz[1] * cosmhd
203 + phi_dphi[0] * mhddata->mmode[i] * ptz[5] * sinmhd
204 - phi_dphi[0] * mhddata->nmode[i] * ptz[9] * sinmhd);
205
206 /* phi component of gradients */
207 mhd_dmhd[3] += (1/r) * mhddata->amplitude_nm[i]
208 * ( a_da[1] * ptz[2] * cosmhd
209 + a_da[0] * mhddata->mmode[i] * ptz[6] * sinmhd
210 - a_da[0] * mhddata->nmode[i] * ptz[10] * sinmhd);
211 mhd_dmhd[8] += (1/r) * mhddata->amplitude_nm[i]
212 * ( phi_dphi[1] * ptz[2] * cosmhd
213 + phi_dphi[0] * mhddata->mmode[i] * ptz[6] * sinmhd
214 - phi_dphi[0] * mhddata->nmode[i] * ptz[10] * sinmhd);
215
216 /* z component of gradients */
217 mhd_dmhd[4] += mhddata->amplitude_nm[i]
218 * ( a_da[1] * ptz[3] * cosmhd
219 + a_da[0] * mhddata->mmode[i] * ptz[7] * sinmhd
220 - a_da[0] * mhddata->nmode[i] * ptz[11] * sinmhd);
221 mhd_dmhd[9] += mhddata->amplitude_nm[i]
222 * ( phi_dphi[1] * ptz[3] * cosmhd
223 + phi_dphi[0] * mhddata->mmode[i] * ptz[7] * sinmhd
224 - phi_dphi[0] * mhddata->nmode[i] * ptz[11] * sinmhd);
225 }
226
227 /* Omit evaluation if point outside the boozer or mhd grid. */
228 if(!isinside || interperr) {
229 for(int i=0; i<10; i++) {
230 mhd_dmhd[i] = 0;
231 }
232 }
233
234 return err;
235}
236
269 real pert_field[7], real r, real phi, real z, real t, int pertonly,
270 int includemode, boozer_data* boozerdata, mhd_nonstat_data* mhddata,
271 B_field_data* Bdata) {
272 a5err err = 0;
273 real mhd_dmhd[10];
274 if(!err) {
275 err = mhd_nonstat_eval(mhd_dmhd, r, phi, z, t, includemode, boozerdata,
276 mhddata, Bdata);
277 }
278 /* see example of curl evaluation in step_gc_rk4.c, ydot_gc*/
279 real B_dB[15];
280 if(!err) {
281 err = B_field_eval_B_dB(B_dB, r, phi, z, t, Bdata);
282 }
283
284 if(!err) {
285 real B[3];
286 B[0] = B_dB[0];
287 B[1] = B_dB[4];
288 B[2] = B_dB[8];
289
290 real curlB[3];
291 curlB[0] = B_dB[10]/r - B_dB[7];
292 curlB[1] = B_dB[3] - B_dB[9];
293 curlB[2] = (B[1] - B_dB[2])/r + B_dB[5];
294
295 real gradalpha[3];
296 gradalpha[0] = mhd_dmhd[2];
297 gradalpha[1] = mhd_dmhd[3];
298 gradalpha[2] = mhd_dmhd[4];
299
300 real gradalphacrossB[3];
301
302 math_cross(gradalpha, B, gradalphacrossB);
303
304 pert_field[0] = mhd_dmhd[0]*curlB[0] + gradalphacrossB[0];
305 pert_field[1] = mhd_dmhd[0]*curlB[1] + gradalphacrossB[1];
306 pert_field[2] = mhd_dmhd[0]*curlB[2] + gradalphacrossB[2];
307
308 pert_field[3] = -mhd_dmhd[7] - B[0]*mhd_dmhd[1];
309 pert_field[4] = -mhd_dmhd[8] - B[1]*mhd_dmhd[1];
310 pert_field[5] = -mhd_dmhd[9] - B[2]*mhd_dmhd[1];
311 pert_field[6] = mhd_dmhd[5];
312
313 if(!pertonly) {
314 pert_field[0] += B[0];
315 pert_field[1] += B[1];
316 pert_field[2] += B[2];
317 }
318 }
319
320 return err;
321}
a5err B_field_eval_rho(real rho[2], real psi, B_field_data *Bdata)
Evaluate normalized poloidal flux rho and its psi derivative.
Definition B_field.c:228
a5err B_field_eval_B_dB(real B_dB[15], real r, real phi, real z, real t, B_field_data *Bdata)
Evaluate magnetic field and its derivatives.
Definition B_field.c:449
Header file for B_field.c.
Main header file for ASCOT5.
double real
Definition ascot5.h:85
a5err boozer_eval_psithetazeta(real psithetazeta[12], int *isinside, real r, real phi, real z, B_field_data *Bdata, boozer_data *boozerdata)
Evaluate Boozer coordinates and partial derivatives.
Definition boozer.c:124
Header file for boozer.c.
Error module for ASCOT5.
unsigned long int a5err
Simulation error flag.
Definition error.h:17
Spline interpolation library.
int interp2Dcomp_setup(interp2D_data *str, real *f, int n_x, int n_y, int bc_x, int bc_y, real x_min, real x_max, real y_min, real y_max)
Set up splines to interpolate 2D scalar data.
@ NATURALBC
Definition interp.h:37
DECLARE_TARGET_END a5err interp2Dcomp_eval_df(real *f_df, interp2D_data *str, real x, real y)
Evaluate interpolated value and 1st and 2nd derivatives of 2D field.
Header file for math.c.
#define math_cross(a, b, c)
Calculate cross product for 3D vectors c = a x b.
Definition math.h:31
Header file for mhd.c.
#define MHD_INCLUDE_ALL
includemode parameter to include all modes (default)
Definition mhd.h:19
a5err mhd_nonstat_perturbations(real pert_field[7], real r, real phi, real z, real t, int pertonly, int includemode, boozer_data *boozerdata, mhd_nonstat_data *mhddata, B_field_data *Bdata)
Evaluate mhd perturbed fields Btilde, Etilde and potential Phi for full orbit.
void mhd_nonstat_free(mhd_nonstat_data *data)
Free allocated resources.
Definition mhd_nonstat.c:86
void mhd_nonstat_offload(mhd_nonstat_data *data)
Offload data to the accelerator.
int mhd_nonstat_init(mhd_nonstat_data *data, int nmode, int nrho, int ntime, real rhomin, real rhomax, real tmin, real tmax, int *moden, int *modem, real *amplitude_nm, real *omega_nm, real *phase_nm, real *alpha, real *phi)
Load MHD data.
Definition mhd_nonstat.c:23
a5err mhd_nonstat_eval(real mhd_dmhd[10], real r, real phi, real z, real t, int includemode, boozer_data *boozerdata, mhd_nonstat_data *mhddata, B_field_data *Bdata)
Evaluate the needed quantities from MHD mode for orbit following.
Header file for mhd_nonstat.c.
Macros for printing console output.
#define print_out(v,...)
Print to standard output.
Definition print.h:31
@ VERBOSE_IO
Definition print.h:20
#define print_err(...)
Print to standard error.
Definition print.h:42
Magnetic field simulation data.
Definition B_field.h:41
Data for mapping between the cylindrical and Boozer coordinates.
Definition boozer.h:16
Bicubic interpolation struct.
Definition interp.h:68
real * c
Definition interp.h:79
MHD parameters.
Definition mhd_nonstat.h:18
interp2D_data * phi_nm
2D splines (rho,time) for each mode's electric eigenfunction
Definition mhd_nonstat.h:35
interp2D_data * alpha_nm
2D splines (rho,time) for each mode's magnetic eigenfunction
Definition mhd_nonstat.h:31