ASCOT5
Loading...
Searching...
No Matches
B_field.c
Go to the documentation of this file.
1
16#include <stdio.h>
17#include "ascot5.h"
18#include "error.h"
19#include "print.h"
20#include "B_field.h"
21#include "Bfield/B_GS.h"
22#include "Bfield/B_2DS.h"
23#include "Bfield/B_3DS.h"
24#include "Bfield/B_STS.h"
25#include "Bfield/B_TC.h"
26
45 real** offload_array) {
46 int err = 0;
47 switch(offload_data->type) {
48
49 case B_field_type_GS:
50 err = B_GS_init_offload(&(offload_data->BGS), offload_array);
51 offload_data->offload_array_length =
52 offload_data->BGS.offload_array_length;
53 break;
54
56 err = B_2DS_init_offload(&(offload_data->B2DS), offload_array);
57 offload_data->offload_array_length =
58 offload_data->B2DS.offload_array_length;
59 break;
60
62 err = B_3DS_init_offload(&(offload_data->B3DS), offload_array);
63 offload_data->offload_array_length =
64 offload_data->B3DS.offload_array_length;
65 break;
66
68 err = B_STS_init_offload(&(offload_data->BSTS), offload_array);
69 offload_data->offload_array_length =
70 offload_data->BSTS.offload_array_length;
71 break;
72
73 case B_field_type_TC:
74 err = B_TC_init_offload(&(offload_data->BTC), offload_array);
75 offload_data->offload_array_length =
76 offload_data->BTC.offload_array_length;
77 break;
78
79 default:
80 /* Unregonized input. Produce error. */
81 print_err("Error: Unregonized magnetic field type.");
82 err = 1;
83 break;
84 }
85
86 if(!err) {
87 print_out(VERBOSE_IO, "Estimated memory usage %.1f MB\n",
88 offload_data->offload_array_length
89 * sizeof(real) / (1024.0*1024.0) );
90 }
91
92 return err;
93}
94
106 real** offload_array) {
107 switch(offload_data->type) {
108 case B_field_type_GS:
109 B_GS_free_offload(&(offload_data->BGS), offload_array);
110 break;
111
112 case B_field_type_2DS:
113 B_2DS_free_offload(&(offload_data->B2DS), offload_array);
114 break;
115
116 case B_field_type_3DS:
117 B_3DS_free_offload(&(offload_data->B3DS), offload_array);
118 break;
119
120 case B_field_type_STS:
121 B_STS_free_offload(&(offload_data->BSTS), offload_array);
122 break;
123
124 case B_field_type_TC:
125 B_TC_free_offload(&(offload_data->BTC), offload_array);
126 break;
127 }
128}
129
144 real* offload_array) {
145 int err = 0;
146
147 switch(offload_data->type) {
148 case B_field_type_GS:
149 B_GS_init(
150 &(Bdata->BGS), &(offload_data->BGS), offload_array);
151 break;
152
153 case B_field_type_2DS:
155 &(Bdata->B2DS), &(offload_data->B2DS), offload_array);
156 break;
157
158 case B_field_type_3DS:
160 &(Bdata->B3DS), &(offload_data->B3DS), offload_array);
161 break;
162
163 case B_field_type_STS:
165 &(Bdata->BSTS), &(offload_data->BSTS), offload_array);
166 break;
167
168 case B_field_type_TC:
169 B_TC_init(
170 &(Bdata->BTC), &(offload_data->BTC), offload_array);
171 break;
172
173 default:
174 /* Unregonized input. Produce error. */
175 print_err("Error: Unregonized magnetic field type.\n");
176 err = 1;
177 break;
178 }
179 Bdata->type = offload_data->type;
180
181 return err;
182}
183
202 B_field_data* Bdata) {
203 a5err err = 0;
204
205 switch(Bdata->type) {
206 case B_field_type_GS:
207 err = B_GS_eval_psi(psi, r, phi, z, &(Bdata->BGS));
208 break;
209
210 case B_field_type_2DS:
211 err = B_2DS_eval_psi(psi, r, phi, z, &(Bdata->B2DS));
212 break;
213
214 case B_field_type_3DS:
215 err = B_3DS_eval_psi(psi, r, phi, z, &(Bdata->B3DS));
216 break;
217
218 case B_field_type_STS:
219 err = B_STS_eval_psi(psi, r, phi, z, &(Bdata->BSTS));
220 break;
221
222 case B_field_type_TC:
223 err = B_TC_eval_psi(psi, r, phi, z, &(Bdata->BTC));
224 break;
225
226 default:
227 /* Unregonized input. Produce error. */
228 err = error_raise( ERR_UNKNOWN_INPUT, __LINE__, EF_B_FIELD );
229 break;
230 }
231
232 if(err) {
233 /* In case of error, return some reasonable value to avoid further
234 complications */
235 psi[0] = 1;
236 }
237
238 return err;
239}
240
266 B_field_data* Bdata) {
267 a5err err = 0;
268
269 switch(Bdata->type) {
270 case B_field_type_GS:
271 err = B_GS_eval_psi_dpsi(psi_dpsi, r, phi, z, &(Bdata->BGS));
272 break;
273
274 case B_field_type_2DS:
275 err = B_2DS_eval_psi_dpsi(psi_dpsi, r, phi, z, &(Bdata->B2DS));
276 break;
277
278 case B_field_type_3DS:
279 err = B_3DS_eval_psi_dpsi(psi_dpsi, r, phi, z, &(Bdata->B3DS));
280 break;
281
282 case B_field_type_STS:
283 err = B_STS_eval_psi_dpsi(psi_dpsi, r, phi, z, &(Bdata->BSTS));
284 break;
285
286 case B_field_type_TC:
287 err = B_TC_eval_psi_dpsi(psi_dpsi, r, phi, z, &(Bdata->BTC));
288 break;
289
290 default:
291 /* Unregonized input. Produce error. */
292 err = error_raise( ERR_UNKNOWN_INPUT, __LINE__, EF_B_FIELD );
293 break;
294 }
295
296 if(err) {
297 /* In case of error, return some reasonable values to avoid further
298 complications */
299 psi_dpsi[0] = 1;
300 for(int k=1; k<4; k++) {psi_dpsi[k] = 0;}
301 }
302
303 return err;
304}
305
328 a5err err = 0;
329
330 real psi0 = 0.0, psi1 = 1.0;
331 switch(Bdata->type) {
332 case B_field_type_GS:
333 psi0 = Bdata->BGS.psi0;
334 psi1 = Bdata->BGS.psi1;
335 break;
336
337 case B_field_type_2DS:
338 psi0 = Bdata->B2DS.psi0;
339 psi1 = Bdata->B2DS.psi1;
340 break;
341
342 case B_field_type_3DS:
343 psi0 = Bdata->B3DS.psi0;
344 psi1 = Bdata->B3DS.psi1;
345 break;
346
347 case B_field_type_STS:
348 psi0 = Bdata->BSTS.psi0;
349 psi1 = Bdata->BSTS.psi1;
350 break;
351
352 case B_field_type_TC:
353 psi0 = Bdata->BTC.psival;
354 psi1 = 2.0;
355 break;
356
357 default:
358 /* Unregonized input. Produce error. */
359 err = error_raise( ERR_UNKNOWN_INPUT, __LINE__, EF_B_FIELD );
360 break;
361 }
362
363 real delta = (psi1 - psi0);
364 if( (psi - psi0) / delta < 0 ) {
365 err = error_raise( ERR_INPUT_UNPHYSICAL, __LINE__, EF_B_FIELD );
366 } else {
367 rho[0] = sqrt( (psi - psi0) / delta );
368 rho[1] = 1.0 / (2*delta*rho[0]);
369 }
370
371 if(err) {
372 /* In case of error, return some reasonable value to avoid further
373 complications */
374 rho[0] = 1;
375 rho[1] = 0;
376 }
377
378 return err;
379}
380
411 B_field_data* Bdata) {
412 a5err err = 0;
413
414 switch(Bdata->type) {
415 case B_field_type_GS:
416 err = B_GS_eval_rho_drho(rho_drho, r, phi, z, &(Bdata->BGS));
417 break;
418
419 case B_field_type_2DS:
420 err = B_2DS_eval_rho_drho(rho_drho, r, phi, z, &(Bdata->B2DS));
421 break;
422
423 case B_field_type_3DS:
424 err = B_3DS_eval_rho_drho(rho_drho, r, phi, z, &(Bdata->B3DS));
425 break;
426
427 case B_field_type_STS:
428 err = B_STS_eval_rho_drho(rho_drho, r, phi, z, &(Bdata->BSTS));
429 break;
430
431 case B_field_type_TC:
432 err = B_TC_eval_rho_drho(rho_drho, r, phi, z, &(Bdata->BTC));
433 break;
434
435 default:
436 /* Unregonized input. Produce error. */
437 err = error_raise( ERR_UNKNOWN_INPUT, __LINE__, EF_B_FIELD );
438 break;
439 }
440
441 if(err) {
442 /* In case of error, return some reasonable values to avoid further
443 complications */
444 rho_drho[0] = 1;
445 for(int k=1; k<4; k++) {rho_drho[k] = 0;}
446 }
447
448 return err;
449}
450
473 B_field_data* Bdata) {
474 a5err err = 0;
475
476 switch(Bdata->type) {
477 case B_field_type_GS:
478 err = B_GS_eval_B(B, r, phi, z, &(Bdata->BGS));
479 break;
480
481 case B_field_type_2DS:
482 err = B_2DS_eval_B(B, r, phi, z, &(Bdata->B2DS));
483 break;
484
485 case B_field_type_3DS:
486 err = B_3DS_eval_B(B, r, phi, z, &(Bdata->B3DS));
487 break;
488
489 case B_field_type_STS:
490 err = B_STS_eval_B(B, r, phi, z, &(Bdata->BSTS));
491 break;
492
493 case B_field_type_TC:
494 err = B_TC_eval_B(B, r, phi, z, &(Bdata->BTC));
495 break;
496
497 default:
498 /* Unregonized input. Produce error. */
499 err = error_raise( ERR_UNKNOWN_INPUT, __LINE__, EF_B_FIELD );
500 break;
501 }
502
503 if(err) {
504 /* In case of error, return some reasonable values to avoid further
505 complications */
506 B[0] = 1;
507 for(int k=1; k<3; k++) {B[k] = 0;}
508 }
509
510 return err;
511}
512
548 B_field_data* Bdata) {
549 a5err err = 0;
550
551 switch(Bdata->type) {
552 case B_field_type_GS:
553 err = B_GS_eval_B_dB(B_dB, r, phi, z, &(Bdata->BGS));
554 break;
555
556 case B_field_type_2DS:
557 err = B_2DS_eval_B_dB(B_dB, r, phi, z, &(Bdata->B2DS));
558 break;
559
560 case B_field_type_3DS:
561 err = B_3DS_eval_B_dB(B_dB, r, phi, z, &(Bdata->B3DS));
562 break;
563
564 case B_field_type_STS:
565 err = B_STS_eval_B_dB(B_dB, r, phi, z, &(Bdata->BSTS));
566 break;
567
568 case B_field_type_TC:
569 err = B_TC_eval_B_dB(B_dB, r, phi, z, &(Bdata->BTC));
570 break;
571
572 default:
573 /* Unregonized input. Produce error. */
574 err = error_raise( ERR_UNKNOWN_INPUT, __LINE__, EF_B_FIELD );
575 break;
576 }
577
578 if(err) {
579 /* In case of error, return some reasonable values to avoid further
580 complications */
581 B_dB[0] = 1;
582 for(int k=1; k<12; k++) {B_dB[k] = 0;}
583 }
584
585 return err;
586}
587
600 a5err err = 0;
601
602 switch(Bdata->type) {
603 case B_field_type_GS:
604 err = B_GS_get_axis_rz(rz, &(Bdata->BGS));
605 break;
606
607 case B_field_type_2DS:
608 err = B_2DS_get_axis_rz(rz, &(Bdata->B2DS));
609 break;
610
611 case B_field_type_3DS:
612 err = B_3DS_get_axis_rz(rz, &(Bdata->B3DS));
613 break;
614
615 case B_field_type_STS:
616 err = B_STS_get_axis_rz(rz, &(Bdata->BSTS), phi);
617 break;
618
619 case B_field_type_TC:
620 err = B_TC_get_axis_rz(rz, &(Bdata->BTC));
621 break;
622
623 default:
624 /* Unregonized input. Produce error. */
625 err = error_raise( ERR_UNKNOWN_INPUT, __LINE__, EF_B_FIELD );
626 break;
627 }
628
629 if(err) {
630 /* In case of error, return some reasonable values to avoid further
631 complications */
632 rz[0] = 1.0;
633 rz[1] = 0.0;
634 }
635
636 return err;
637}
a5err B_2DS_eval_psi(real *psi, real r, real phi, real z, B_2DS_data *Bdata)
Evaluate poloidal flux psi.
Definition B_2DS.c:237
void B_2DS_free_offload(B_2DS_offload_data *offload_data, real **offload_array)
Free offload array.
Definition B_2DS.c:163
void B_2DS_init(B_2DS_data *Bdata, B_2DS_offload_data *offload_data, real *offload_array)
Initialize magnetic field data struct on target.
Definition B_2DS.c:176
a5err B_2DS_eval_psi_dpsi(real psi_dpsi[4], real r, real phi, real z, B_2DS_data *Bdata)
Evaluate poloidal flux psi and its derivatives.
Definition B_2DS.c:260
a5err B_2DS_eval_rho_drho(real rho_drho[4], real r, real phi, real z, B_2DS_data *Bdata)
Evaluate normalized poloidal flux rho and its derivatives.
Definition B_2DS.c:291
a5err B_2DS_eval_B(real B[3], real r, real phi, real z, B_2DS_data *Bdata)
Evaluate magnetic field.
Definition B_2DS.c:329
a5err B_2DS_get_axis_rz(real rz[2], B_2DS_data *Bdata)
Return magnetic axis R-coordinate.
Definition B_2DS.c:444
a5err B_2DS_eval_B_dB(real B_dB[12], real r, real phi, real z, B_2DS_data *Bdata)
Evaluate magnetic field and its derivatives.
Definition B_2DS.c:375
int B_2DS_init_offload(B_2DS_offload_data *offload_data, real **offload_array)
Initialize magnetic field offload data.
Definition B_2DS.c:69
Header file for B_2DS.c.
void B_3DS_free_offload(B_3DS_offload_data *offload_data, real **offload_array)
Free offload array.
Definition B_3DS.c:202
a5err B_3DS_eval_rho_drho(real rho_drho[4], real r, real phi, real z, B_3DS_data *Bdata)
Evaluate normalized poloidal flux rho and its derivatives.
Definition B_3DS.c:340
a5err B_3DS_eval_B(real B[3], real r, real phi, real z, B_3DS_data *Bdata)
Evaluate magnetic field.
Definition B_3DS.c:378
int B_3DS_init_offload(B_3DS_offload_data *offload_data, real **offload_array)
Initialize magnetic field offload data.
Definition B_3DS.c:93
void B_3DS_init(B_3DS_data *Bdata, B_3DS_offload_data *offload_data, real *offload_array)
Initialize magnetic field data struct on target.
Definition B_3DS.c:215
a5err B_3DS_eval_psi_dpsi(real psi_dpsi[4], real r, real phi, real z, B_3DS_data *Bdata)
Evaluate poloidal flux psi and its derivatives.
Definition B_3DS.c:309
a5err B_3DS_get_axis_rz(real rz[2], B_3DS_data *Bdata)
Return magnetic axis R-coordinate.
Definition B_3DS.c:489
a5err B_3DS_eval_psi(real *psi, real r, real phi, real z, B_3DS_data *Bdata)
Evaluate poloidal flux psi.
Definition B_3DS.c:285
a5err B_3DS_eval_B_dB(real B_dB[12], real r, real phi, real z, B_3DS_data *Bdata)
Evaluate magnetic field and its derivatives.
Definition B_3DS.c:425
Header file for B_3DS.c.
void B_GS_init(B_GS_data *Bdata, B_GS_offload_data *offload_data, real *offload_array)
Initialize magnetic field data struct on target.
Definition B_GS.c:144
a5err B_GS_eval_psi(real *psi, real r, real phi, real z, B_GS_data *Bdata)
Evaluate poloidal flux psi.
Definition B_GS.c:174
a5err B_GS_eval_B(real B[3], real r, real phi, real z, B_GS_data *Bdata)
Evaluate magnetic field.
Definition B_GS.c:327
a5err B_GS_eval_B_dB(real B_dB[12], real r, real phi, real z, B_GS_data *Bdata)
Evaluate magnetic field and its derivatives.
Definition B_GS.c:406
a5err B_GS_get_axis_rz(real rz[2], B_GS_data *Bdata)
Return magnetic axis R-coordinate.
Definition B_GS.c:557
void B_GS_free_offload(B_GS_offload_data *offload_data, real **offload_array)
Free offload array.
Definition B_GS.c:131
a5err B_GS_eval_rho_drho(real rho_drho[4], real r, real phi, real z, B_GS_data *Bdata)
Evaluate normalized poloidal flux rho and its derivatives.
Definition B_GS.c:296
a5err B_GS_eval_psi_dpsi(real psi_dpsi[4], real r, real phi, real z, B_GS_data *Bdata)
Evaluate poloidal flux psi and its derivatives.
Definition B_GS.c:227
int B_GS_init_offload(B_GS_offload_data *offload_data, real **offload_array)
Initialize magnetic field offload data.
Definition B_GS.c:91
Header file for B_GS.c.
a5err B_STS_eval_rho_drho(real rho_drho[4], real r, real phi, real z, B_STS_data *Bdata)
Evaluate normalized poloidal flux rho and its derivatives.
Definition B_STS.c:396
void B_STS_free_offload(B_STS_offload_data *offload_data, real **offload_array)
Free offload array.
Definition B_STS.c:225
void B_STS_init(B_STS_data *Bdata, B_STS_offload_data *offload_data, real *offload_array)
Initialize magnetic field data struct on target.
Definition B_STS.c:237
a5err B_STS_eval_B_dB(real B_dB[12], real r, real phi, real z, B_STS_data *Bdata)
Evaluate magnetic field and its derivatives.
Definition B_STS.c:480
int B_STS_init_offload(B_STS_offload_data *offload_data, real **offload_array)
Initialize magnetic field offload data.
Definition B_STS.c:98
a5err B_STS_eval_psi(real *psi, real r, real phi, real z, B_STS_data *Bdata)
Evaluate poloidal flux psi.
Definition B_STS.c:322
a5err B_STS_eval_psi_dpsi(real psi_dpsi[4], real r, real phi, real z, B_STS_data *Bdata)
Evaluate poloidal flux psi and its derivatives.
Definition B_STS.c:354
a5err B_STS_get_axis_rz(real rz[2], B_STS_data *Bdata, real phi)
Return magnetic axis Rz-coordinates.
Definition B_STS.c:531
a5err B_STS_eval_B(real B[3], real r, real phi, real z, B_STS_data *Bdata)
Evaluate magnetic field.
Definition B_STS.c:445
Header file for B_STS.c.
void B_TC_free_offload(B_TC_offload_data *offload_data, real **offload_array)
Free offload array.
Definition B_TC.c:78
a5err B_TC_eval_rho_drho(real rho_drho[4], real r, real phi, real z, B_TC_data *Bdata)
Evaluate normalized poloidal flux rho and its derivatives.
Definition B_TC.c:153
a5err B_TC_eval_psi(real *psi, real r, real phi, real z, B_TC_data *Bdata)
Evaluate poloidal flux psi.
Definition B_TC.c:114
a5err B_TC_eval_psi_dpsi(real psi_dpsi[4], real r, real phi, real z, B_TC_data *Bdata)
Evaluate poloidal flux psi and its derivatives.
Definition B_TC.c:132
a5err B_TC_eval_B_dB(real B_dB[12], real r, real phi, real z, B_TC_data *Bdata)
Evaluate magnetic field and its derivatives.
Definition B_TC.c:207
void B_TC_init(B_TC_data *Bdata, B_TC_offload_data *offload_data, real *offload_array)
Initialize magnetic field data struct on target.
Definition B_TC.c:91
int B_TC_init_offload(B_TC_offload_data *offload_data, real **offload_array)
Initialize magnetic field offload data.
Definition B_TC.c:47
a5err B_TC_get_axis_rz(real rz[2], B_TC_data *Bdata)
Return magnetic axis R-coordinate.
Definition B_TC.c:246
a5err B_TC_eval_B(real B[3], real r, real phi, real z, B_TC_data *Bdata)
Evaluate magnetic field.
Definition B_TC.c:175
Header file for B_TC.c.
a5err B_field_eval_psi_dpsi(real psi_dpsi[4], real r, real phi, real z, real t, B_field_data *Bdata)
Evaluate poloidal flux psi and its derivatives.
Definition B_field.c:265
int B_field_init_offload(B_field_offload_data *offload_data, real **offload_array)
Load magnetic field data and prepare parameters.
Definition B_field.c:44
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:327
a5err B_field_eval_psi(real *psi, real r, real phi, real z, real t, B_field_data *Bdata)
Evaluate poloidal flux psi.
Definition B_field.c:201
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:547
a5err B_field_eval_rho_drho(real rho_drho[4], real r, real phi, real z, B_field_data *Bdata)
Evaluate normalized poloidal flux rho and its derivatives.
Definition B_field.c:410
int B_field_init(B_field_data *Bdata, B_field_offload_data *offload_data, real *offload_array)
Initialize magnetic field data struct on target.
Definition B_field.c:143
a5err B_field_get_axis_rz(real rz[2], B_field_data *Bdata, real phi)
Return magnetic axis Rz-coordinates.
Definition B_field.c:599
void B_field_free_offload(B_field_offload_data *offload_data, real **offload_array)
Free offload array and reset parameters.
Definition B_field.c:105
a5err B_field_eval_B(real B[3], real r, real phi, real z, real t, B_field_data *Bdata)
Evaluate magnetic field.
Definition B_field.c:472
Header file for B_field.c.
@ B_field_type_2DS
Definition B_field.h:29
@ B_field_type_TC
Definition B_field.h:32
@ B_field_type_3DS
Definition B_field.h:30
@ B_field_type_STS
Definition B_field.h:31
@ B_field_type_GS
Definition B_field.h:28
Main header file for ASCOT5.
double real
Definition ascot5.h:85
Error module for ASCOT5.
unsigned long int a5err
Simulation error flag.
Definition error.h:17
@ EF_B_FIELD
Definition error.h:46
@ ERR_UNKNOWN_INPUT
Definition error.h:64
@ ERR_INPUT_UNPHYSICAL
Definition error.h:65
static DECLARE_TARGET_SIMD a5err error_raise(error_type type, int line, error_file file)
Raise a new error.
Definition error.h:86
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
int offload_array_length
Definition B_2DS.h:28
int offload_array_length
Definition B_3DS.h:37
int offload_array_length
Definition B_GS.h:29
int offload_array_length
Definition B_STS.h:38
int offload_array_length
Definition B_TC.h:30
Magnetic field simulation data.
Definition B_field.h:63
Magnetic field offload data.
Definition B_field.h:44
B_2DS_offload_data B2DS
Definition B_field.h:47
B_field_type type
Definition B_field.h:45
B_TC_offload_data BTC
Definition B_field.h:50
B_GS_offload_data BGS
Definition B_field.h:46
B_STS_offload_data BSTS
Definition B_field.h:49
B_3DS_offload_data B3DS
Definition B_field.h:48