XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
magnetic_field.hpp
Go to the documentation of this file.
1 #ifndef MAGNETIC_FIELD_HPP
2 #define MAGNETIC_FIELD_HPP
4 #include "equil.hpp"
5 #include "bicub.hpp"
6 #include "cub_interp.hpp"
7 #include "range_view.hpp"
8 #include "tricub.hpp"
9 
10 // Magnetic field class
11 template<class Device>
13  // This line enables private access between the host and device implementations of this class
14  // i.e. Makes the copy from host to device more straightforward
15  template<class Device2> friend class MagneticField;
16 
17  KOKKOS_INLINE_FUNCTION void derivs(const double (&x)[2],double phi,double (&dx)[2]) const;
18 
19 #ifndef STELLARATOR
20  KOKKOS_INLINE_FUNCTION double I_value(double psi_in,int rgn3) const;
21  KOKKOS_INLINE_FUNCTION double I_deriv(double psi_in,int rgn3) const;
22 #endif
23 
24  int ff_step;
25  int ff_order;
26 
27 #ifndef STELLARATOR
30 #else
31  Tricub<Device> psi_tricub;
32  Tricub<Device> Br_tricub;
33  Tricub<Device> Bz_tricub;
34  Tricub<Device> Bphi_tricub;
35 #endif
36 
37  public:
38 
39  double bt_sign;
40  double bp_sign;
42  double inpsi;
43  double outpsi;
45 
46  MagneticField(NLReader::NamelistReader& nlr, const MagneticEquilFiles::Ptr& equil_files, const View<Equil::XPoint*, HostType>& xpts,
47  const RZPair& axis_in, const RZPair& psi_ref_point);
48 
49  void write() const;
50 
51  // Create a mirror with a different device type
52  template<class Device2>
55  m.bt_sign = bt_sign;
56  m.bp_sign = bp_sign;
57  m.ff_step = ff_step;
58  m.ff_order = ff_order;
59  m.bounds = bounds;
60 
61  m.inpsi = inpsi;
62  m.outpsi = outpsi;
63 
64  m.equil = equil;
65 #ifndef STELLARATOR
66  m.psi_bicub = psi_bicub.template mirror<Device2>();
67  m.I_interp = I_interp.template mirror<Device2>();
68 #else
69  m.psi_tricub = psi_tricub.template mirror<Device2>();
70  m.Br_tricub = Br_tricub.template mirror<Device2>();
71  m.Bz_tricub = Bz_tricub.template mirror<Device2>();
72  m.Bphi_tricub = Bphi_tricub.template mirror<Device2>();
73 #endif
74 
75  return m;
76  }
77 
78  // Analytic constructors
79 #ifdef STELLARATOR
80  MagneticField(PsiOption psi_opt, int eq_mr_in=-1, int eq_mz_in=-1, int eq_mphi_in=-1);
81 #else
82  MagneticField(PsiOption psi_opt, double safety_factor_coeff=1.0, int eq_mr_in=-1, int eq_mz_in=-1, int eq_mpsi_in=-1);
83 #endif
84 
85  // Default constructor
87 
88  KOKKOS_INLINE_FUNCTION double psi_norm() const;
89 
90  KOKKOS_INLINE_FUNCTION void get_psi(const SimdVector& v,Simd<double>& psi_out) const;
91 
92  KOKKOS_INLINE_FUNCTION void get_psi(const SimdVector2D& x,const Simd<double>& phi, Simd<double>& psi_out) const;
93 
94  KOKKOS_INLINE_FUNCTION double get_psi(double r, double z, double phi) const;
95 
96  KOKKOS_INLINE_FUNCTION void get_psi_and_derivs(double r, double z, double phi, double& psi, double& dpsidr, double& dpsidz, double& dpsidphi) const;
97 
98  KOKKOS_INLINE_FUNCTION void get_psi_unit_vec(const SimdVector2D& x, double phi, Simd<double>& cosa, Simd<double>& sina) const;
99 
100  KOKKOS_INLINE_FUNCTION double geometry_r(double r) const;
101 
102  KOKKOS_INLINE_FUNCTION bool is_in_region_1_or_2(double r,double z,double psi) const;
103  KOKKOS_INLINE_FUNCTION bool is_in_region_1(double r,double z,double psi) const;
104  KOKKOS_INLINE_FUNCTION void check_boundaries(const SimdVector2D& x, Simd<bool>& rz_outside) const;
105  KOKKOS_INLINE_FUNCTION void get_theta(const SimdVector2D& x, Simd<double>& theta) const;
106 
107  // Get complete B-field info
108  KOKKOS_INLINE_FUNCTION void field(const SimdVector& v, SimdVector &bvec, SimdVector (&jacb)[3], Simd<double>& psivec, SimdVector2D &gradpsi, SimdVector &tdb, Simd<bool>& rz_outside) const;
109 
110  KOKKOS_INLINE_FUNCTION void bvec_interpol(double r,double z,double phi,double &br,double &bz,double &bphi) const;
111 
112  KOKKOS_INLINE_FUNCTION void bmag_interpol(const SimdVector& v, Simd<double>& bmag) const;
113 
114  KOKKOS_INLINE_FUNCTION void bmag_interpol(const SimdVector2D& x, const Simd<double>& phi,Simd<double>& bmag) const;
115 
116  KOKKOS_INLINE_FUNCTION void follow_field(const SimdVector2D &x_org,const Simd<double>& phi_org, const Simd<double>& phi_dest,SimdVector2D &x_dest) const;
117 };
118 
119 #include "magnetic_field.tpp"
120 #endif
Definition: tricub.hpp:13
double inpsi
Boundary condition used in a few spots.
Definition: magnetic_field.hpp:42
KOKKOS_INLINE_FUNCTION bool is_in_region_1(double r, double z, double psi) const
Definition: magnetic_field.tpp:14
CubInterp< Device > I_interp
The object for interpolating I (for deriving toroidal magnetic field)
Definition: magnetic_field.hpp:29
KOKKOS_INLINE_FUNCTION void get_psi_and_derivs(double r, double z, double phi, double &psi, double &dpsidr, double &dpsidz, double &dpsidphi) const
Definition: magnetic_field.tpp:62
KOKKOS_INLINE_FUNCTION void bvec_interpol(double r, double z, double phi, double &br, double &bz, double &bphi) const
Definition: magnetic_field.tpp:262
Definition: simd.hpp:149
PsiOption
Definition: equil.hpp:11
KOKKOS_INLINE_FUNCTION void get_psi(const SimdVector &v, Simd< double > &psi_out) const
Definition: magnetic_field.tpp:46
KOKKOS_INLINE_FUNCTION void follow_field(const SimdVector2D &x_org, const Simd< double > &phi_org, const Simd< double > &phi_dest, SimdVector2D &x_dest) const
Definition: magnetic_field.tpp:344
KOKKOS_INLINE_FUNCTION void check_boundaries(const SimdVector2D &x, Simd< bool > &rz_outside) const
Definition: magnetic_field.tpp:19
KOKKOS_INLINE_FUNCTION void get_theta(const SimdVector2D &x, Simd< double > &theta) const
Definition: magnetic_field.tpp:24
KOKKOS_INLINE_FUNCTION double geometry_r(double r) const
Definition: magnetic_field.tpp:124
Definition: rz_bounds.hpp:4
int ff_order
Order of RK scheme used for field following. Can be 1, 2, or 4.
Definition: magnetic_field.hpp:25
Definition: NamelistReader.hpp:193
Definition: magnetic_field.hpp:12
KOKKOS_INLINE_FUNCTION double I_deriv(double psi_in, int rgn3) const
Definition: magnetic_field.tpp:108
RZBounds bounds
Simulation boundary.
Definition: magnetic_field.hpp:41
Equilibrium equil
The object containing information about the magnetic equilibrium.
Definition: magnetic_field.hpp:44
KOKKOS_INLINE_FUNCTION void bmag_interpol(const SimdVector &v, Simd< double > &bmag) const
Definition: magnetic_field.tpp:313
Definition: cub_interp.hpp:12
MagneticField()
Definition: magnetic_field.hpp:86
KOKKOS_INLINE_FUNCTION double I_value(double psi_in, int rgn3) const
Definition: magnetic_field.tpp:97
KOKKOS_INLINE_FUNCTION double psi_norm() const
Definition: magnetic_field.tpp:3
KOKKOS_INLINE_FUNCTION void field(const SimdVector &v, SimdVector &bvec, SimdVector(&jacb)[3], Simd< double > &psivec, SimdVector2D &gradpsi, SimdVector &tdb, Simd< bool > &rz_outside) const
Definition: magnetic_field.tpp:138
Definition: grid_structs.hpp:28
double outpsi
Boundary condition used in a few spots.
Definition: magnetic_field.hpp:43
double bt_sign
Whether toroidal field is reversed?
Definition: magnetic_field.hpp:39
Definition: bicub.hpp:14
double bp_sign
Whether poloidal field is reversed?
Definition: magnetic_field.hpp:40
Definition: simd.hpp:139
KOKKOS_INLINE_FUNCTION bool is_in_region_1_or_2(double r, double z, double psi) const
Definition: magnetic_field.tpp:9
Bicub< Device > psi_bicub
The object for interpolating psi (magnetic flux surfaces)
Definition: magnetic_field.hpp:28
int ff_step
Number of steps taken when projecting the particle location onto the midplane.
Definition: magnetic_field.hpp:24
KOKKOS_INLINE_FUNCTION void get_psi_unit_vec(const SimdVector2D &x, double phi, Simd< double > &cosa, Simd< double > &sina) const
Definition: magnetic_field.tpp:81
KOKKOS_INLINE_FUNCTION void derivs(const double(&x)[2], double phi, double(&dx)[2]) const
Definition: magnetic_field.tpp:458
void write() const
Definition: equil.hpp:29
MagneticField< Device2 > mirror() const
Definition: magnetic_field.hpp:53
std::shared_ptr< MagneticEquilFiles > Ptr
Definition: magnetic_equil_files.hpp:11