ASCOT5
Loading...
Searching...
No Matches
B_3DS.c File Reference

3D magnetic field with tricubic spline interpolation More...

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "../ascot5.h"
#include "../error.h"
#include "../print.h"
#include "B_3DS.h"
#include "../spline/interp.h"

Go to the source code of this file.

Functions

int B_3DS_init (B_3DS_data *data, int p_n_r, real p_r_min, real p_r_max, int p_n_z, real p_z_min, real p_z_max, int b_n_r, real b_r_min, real b_r_max, int b_n_phi, real b_phi_min, real b_phi_max, int b_n_z, real b_z_min, real b_z_max, real axis_r, real axis_z, real psi0, real psi1, real *psi, real *B_r, real *B_phi, real *B_z)
 Initialize magnetic field data.
 
void B_3DS_free (B_3DS_data *data)
 Free allocated resources.
 
void B_3DS_offload (B_3DS_data *data)
 Offload data to the accelerator.
 
a5err B_3DS_eval_psi (real *psi, real r, real phi, real z, B_3DS_data *Bdata)
 Evaluate poloidal flux psi.
 
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.
 
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.
 
a5err B_3DS_eval_B (real B[3], real r, real phi, real z, B_3DS_data *Bdata)
 Evaluate magnetic field.
 
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.
 
a5err B_3DS_get_axis_rz (real rz[2], B_3DS_data *Bdata)
 Return magnetic axis R-coordinate.
 

Variables

int psigrid_n_r
 
int psigrid_n_z
 
real psigrid_r_min
 
real psigrid_r_max
 
real psigrid_z_min
 
real psigrid_z_max
 
int Bgrid_n_r
 
int Bgrid_n_z
 
real Bgrid_r_min
 
real Bgrid_r_max
 
real Bgrid_z_min
 
real Bgrid_z_max
 
int Bgrid_n_phi
 
real Bgrid_phi_min
 
real Bgrid_phi_max
 
real psi0
 
real psi1
 
real axis_r
 
real axis_z
 

Detailed Description

3D magnetic field with tricubic spline interpolation

This module represents a magnetic field where data is given in $R\phi z$- grid from which it is interpolated with tricubic splines.

The magnetic field is evaluated from magnetic field strength $\mathbf{B}$ which may not be divergence free. However, $B_R$ and $B_z$ components are also evaluated from poloidal magnetic flux $\psi(R,z)$ as

\begin{align*}
B_R &= -\frac{1}{R}\frac{\partial\psi}{\partial z}\\
B_z &= \frac{1}{R}\frac{\partial\psi}{\partial R}
\end{align*}

The total field is then a sum of components interpolated directly from $\mathbf{B}$ and components calculated via interpolated $\psi$. Note that $\psi$ is assumed to be axisymmetric and is interpolated with bicubic splines. $\psi$ and $\mathbf{B}$ are given in separate grids.

This module does no extrapolation so if queried value is outside the $Rz$-grid an error is thrown.

The toroidal angle phi is treated as a periodic coordinate meaning that B(phi) = B(phi + N*(b_phimax - b_phimin)) being N the periodic number. Do note that to avoid duplicate data, the last points in phi axis in B data are not at b_phimax, i.e. br[:,-1,:] != BR(phi=b_phimax). It is user's responsibility to provide input whose $\phi$-grid makes sense (in that it actually represents a periodic field).

Definition in file B_3DS.c.

Function Documentation

◆ B_3DS_init()

int B_3DS_init ( B_3DS_data * data,
int p_n_r,
real p_r_min,
real p_r_max,
int p_n_z,
real p_z_min,
real p_z_max,
int b_n_r,
real b_r_min,
real b_r_max,
int b_n_phi,
real b_phi_min,
real b_phi_max,
int b_n_z,
real b_z_min,
real b_z_max,
real axis_r,
real axis_z,
real psi0,
real psi1,
real * psi,
real * B_r,
real * B_phi,
real * B_z )

Initialize magnetic field data.

The offload array must contain the following data:

  • offload_array[ j*Bn_r*Bn_phi + z*Bn_r + i] = B_R(R_i, phi_z, z_j) [T]
  • offload_array[ Bn_r*Bn_z*Bn_phi + j*Bn_r*Bn_phi + z*Bn_r + i] = B_phi(R_i, phi_z, z_j) [T]
  • offload_array[2*Bn_r*Bn_z*Bn_phi + j*Bn_r*Bn_phi + z*Bn_r + i] = B_z(R_i, phi_z, z_j) [T]
  • offload_array[3*Bn_r*Bn_z*Bn_phi + j*n_r + i] = psi(R_i, z_j) [V*s*m^-1]
Parameters
datapointer to the data struct
p_n_rnumber of r grid points in psi data
p_r_minminimum R coordinate in psi data grid [m]
p_r_maxmaximum R coordinate in psi data grid [m]
p_n_znumber of z grid points in psi data
p_z_minminimum z coordinate in psi data grid [m]
p_z_maxmaximum z coordinate in psi data grid [m]
p_n_rnumber of r grid points in B data
b_r_minminimum R coordinate in B data grid [m]
b_r_maxmaximum R coordinate in B data grid [m]
b_n_phinumber of phi grid points in B data
b_phi_minminimum phi coordinate in B data grid [rad]
b_phi_maxmaximum phi coordinate in B data grid [rad]
b_n_znumber of z grid points in B data
b_z_minminimum z coordinate in B data grid [m]
b_z_maxmaximum z coordinate in B data grid [m]
axis_rR coordinate of magnetic axis [m]
axis_zz coordinate of magnetic axis [m]
psi0poloidal flux at magnetic axis [Vs/m]
psi1poloidal flux at separatrix [Vs/m]
psipoloidal flux psi(R_i,z_j) = arr[j*n_r + i] [Vs/m]
B_rMagnetic field R component B_r(R_i,phi_j,z_k) = arr[k*b_n_r*b_n_phi + j*b_n_r + i] [T]
B_phiMagnetic field phi component B_phi(R_i,phi_j,z_k) = arr[k*b_n_r*b_n_phi + j*b_n_r + i] [T]
B_zMagnetic field z component B_z(R_i,phi_j,z_k) = arr[k*b_n_r*b_n_phi + j*b_n_r + i] [T]
Returns
zero if initialization succeeded

Definition at line 106 of file B_3DS.c.

◆ B_3DS_free()

void B_3DS_free ( B_3DS_data * data)

Free allocated resources.

Parameters
datapointer to the data struct

Definition at line 191 of file B_3DS.c.

◆ B_3DS_offload()

void B_3DS_offload ( B_3DS_data * data)

Offload data to the accelerator.

Parameters
datapointer to the data struct

Definition at line 203 of file B_3DS.c.

◆ B_3DS_eval_psi()

a5err B_3DS_eval_psi ( real * psi,
real r,
real phi,
real z,
B_3DS_data * Bdata )

Evaluate poloidal flux psi.

Parameters
psipointer where psi [V*s*m^-1] value will be stored
rR coordinate [m]
phiphi coordinate [rad]
zz coordinate [m]
Bdatapointer to magnetic field data struct
Returns
Non-zero a5err value if evaluation failed, zero otherwise

Definition at line 224 of file B_3DS.c.

◆ B_3DS_eval_psi_dpsi()

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.

Parameters
psi_dpsipointer for storing psi [V*s*m^-1] and its derivatives
rR coordinate [m]
phiphi coordinate [rad]
zz coordinate [m]
Bdatapointer to magnetic field data struct
Returns
Non-zero a5err value if evaluation failed, zero otherwise

Definition at line 248 of file B_3DS.c.

◆ B_3DS_eval_rho_drho()

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.

Parameters
rho_drhopointer where rho and its derivatives will be stored
rR coordinate [m]
phiphi coordinate [rad]
zz coordinate [m]
Bdatapointer to magnetic field data struct
Returns
Non-zero a5err value if evaluation failed, zero otherwise

Definition at line 279 of file B_3DS.c.

◆ B_3DS_eval_B()

a5err B_3DS_eval_B ( real B[3],
real r,
real phi,
real z,
B_3DS_data * Bdata )

Evaluate magnetic field.

Parameters
Bpointer to array where magnetic field values are stored
rR coordinate [m]
phiphi coordinate [deg]
zz coordinate [m]
Bdatapointer to magnetic field data struct
Returns
Non-zero a5err value if evaluation failed, zero otherwise

Definition at line 317 of file B_3DS.c.

◆ B_3DS_eval_B_dB()

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.

Parameters
B_dBpointer to array where the field and its derivatives are stored
rR coordinate [m]
phiphi coordinate [deg]
zz coordinate [m]
Bdatapointer to magnetic field data struct
Returns
Non-zero a5err value if evaluation failed, zero otherwise

Definition at line 364 of file B_3DS.c.

◆ B_3DS_get_axis_rz()

a5err B_3DS_get_axis_rz ( real rz[2],
B_3DS_data * Bdata )

Return magnetic axis R-coordinate.

Parameters
rzpointer where axis R and z [m] values will be stored
Bdatapointer to magnetic field data struct
Returns
Zero a5err value as this function can't fail.

Definition at line 428 of file B_3DS.c.

Variable Documentation

◆ psigrid_n_r

int psigrid_n_r

Number of R grid points in psi data

Definition at line 42 of file B_3DS.c.

◆ psigrid_n_z

int psigrid_n_z

Number of z grid points in psi data

Definition at line 43 of file B_3DS.c.

◆ psigrid_r_min

real psigrid_r_min

Minimum R grid point in psi data [m]

Definition at line 44 of file B_3DS.c.

◆ psigrid_r_max

real psigrid_r_max

Maximum R grid point in psi data [m]

Definition at line 45 of file B_3DS.c.

◆ psigrid_z_min

real psigrid_z_min

Minimum z grid point in psi data [m]

Definition at line 46 of file B_3DS.c.

◆ psigrid_z_max

real psigrid_z_max

Maximum z grid point in psi data [m]

Definition at line 47 of file B_3DS.c.

◆ Bgrid_n_r

int Bgrid_n_r

Number of R grid points in B data

Definition at line 49 of file B_3DS.c.

◆ Bgrid_n_z

int Bgrid_n_z

Number of z grid points in B data

Definition at line 50 of file B_3DS.c.

◆ Bgrid_r_min

real Bgrid_r_min

Minimum R coordinate in the grid in B data [m]

Definition at line 51 of file B_3DS.c.

◆ Bgrid_r_max

real Bgrid_r_max

Maximum R coordinate in the grid in B data [m]

Definition at line 52 of file B_3DS.c.

◆ Bgrid_z_min

real Bgrid_z_min

Minimum z coordinate in the grid in B data [m]

Definition at line 53 of file B_3DS.c.

◆ Bgrid_z_max

real Bgrid_z_max

Maximum z coordinate in the grid in B data [m]

Definition at line 54 of file B_3DS.c.

◆ Bgrid_n_phi

int Bgrid_n_phi

Number of phi grid points in B data

Definition at line 55 of file B_3DS.c.

◆ Bgrid_phi_min

real Bgrid_phi_min

Minimum phi grid point in B data [rad]

Definition at line 56 of file B_3DS.c.

◆ Bgrid_phi_max

real Bgrid_phi_max

Maximum phi grid point in B data [rad]

Definition at line 57 of file B_3DS.c.

◆ psi0

real psi0

Poloidal flux value at magnetic axis [V*s*m^-1]

Definition at line 59 of file B_3DS.c.

◆ psi1

real psi1

Poloidal flux value at separatrix [V*s*m^-1]

Definition at line 60 of file B_3DS.c.

◆ axis_r

real axis_r

R coordinate of magnetic axis [m]

Definition at line 61 of file B_3DS.c.

◆ axis_z

real axis_z

z coordinate of magnetic axis [m]

Definition at line 62 of file B_3DS.c.