This example demonstrates the use of the extended MHD module in OFT. In this example reconnection in a periodic-slab current sheet will be simulated following the setup used to the GEM Challange.
Code Walk Through
The code consists of the main run program and subroutines for field initialization.
Code Walk Through
As usual, we start with a few module imports for required functionality.
PROGRAM xmhd_tearing
IMPLICIT NONE
INTEGER(i4) :: io_unit,ierr
REAL(r8), POINTER :: vec_vals(:)
TYPE(oft_xmhd_2d_sim) :: mhd_sim
TYPE(multigrid_mesh) :: mg_mesh
TYPE(oft_blag_zerob), TARGET :: blag_zerob
TYPE(poss_scalar_bfield) :: field_init
CLASS(oft_solver), POINTER :: minv => null()
CLASS(oft_matrix), POINTER :: mop => null()
CLASS(oft_vector), POINTER :: u,v
Plasma parameters and evaluation routines.
Definition mhd_utils.F90:14
real(r8), parameter elec_charge
- Electron charge [C]
Definition mhd_utils.F90:22
real(r8), parameter mu0
- Permeability of free-space
Definition mhd_utils.F90:18
real(r8), parameter proton_mass
- Proton mass [Kg]
Definition mhd_utils.F90:20
Multigrid initialization using nested meshes.
Definition multigrid_build.F90:14
subroutine multigrid_construct_surf(mg_mesh, grnd_pt)
Construct multi-level surface mesh.
Definition multigrid_build.F90:948
Multi-Level grid implementation using nested meshes.
Definition multigrid.F90:14
Base environment functions and aliases for Open FUSION Toolkit.
Definition oft_base.F90:23
Surface lagrange FE operator definitions.
Definition blag_operators.F90:20
subroutine oft_blag_project(fe_rep, field, x)
Project a scalar field onto a boundary Lagrange basis.
Definition blag_operators.F90:695
subroutine oft_blag_getmop(fe_rep, mat, bc_flag)
Construct mass matrix for a boundary Lagrange scalar representation.
Definition blag_operators.F90:459
Abstract vector and matrix interfaces.
Definition lin_alg_base.F90:20
Field initializations and evaluation for common scalar analytic field types.
Definition oft_scalar_inits.F90:14
Abstract solver interfaces and select native implementations.
Definition solver_base.F90:32
Matrix and vector management routines.
Definition solver_utils.F90:14
subroutine create_diag_pre(pre)
Needs docs.
Definition solver_utils.F90:616
subroutine create_cg_solver(solver, force_native)
Needs docs.
Definition solver_utils.F90:435
Solve time-dependent MHD equations in 2D with lagrange basis set.
Definition xmhd_2d.F90:8
Multigrid meshes and ML context structure.
Definition multigrid.F90:44
Needs docs.
Definition blag_operators.F90:104
Abstract matrix class.
Definition lin_alg_base.F90:566
Abstract vector class.
Definition lin_alg_base.F90:69
Boundary interpolation class for a scalar field.
Definition oft_scalar_inits.F90:31
Base class for OFT solvers.
Definition solver_base.F90:99
Local Variables
Next we define the local variables needed to initialize our case and run the time-dependent solve and post-processing. Compared to previous examples, we now have specialized initial condition and post-processing objects.
INTEGER(i4) :: order = 2
INTEGER(i4) :: nsteps = 100
INTEGER(i4) :: rst_freq = 10
REAL(r8) :: B_0(3)=0.d0
REAL(r8) :: n0 = 1.d0
REAL(r8) :: velx0 = 1.d0
REAL(r8) :: vely0 = 1.d0
REAL(r8) :: velz0 = 1.d0
REAL(r8) :: t0 = 1.d0
REAL(r8) :: psi0 = 1.d0
REAL(r8) :: by0 = 1.d0
REAL(r8) :: chi=1.d0
REAL(r8) :: eta=1.d0
REAL(r8) :: nu=1.d0
REAL(r8) :: gamma=2.d0/3.d0
REAL(r8) :: D_diff=1.d0
REAL(r8) :: den_scale=1.d19
REAL(r8) :: den_inf = 2.d-1
REAL(r8) :: lam_n = 2.d0
REAL(r8) :: lam_b = 2.d0
REAL(r8) :: L_x = 2.d0
REAL(r8) :: L_z = 2.d0
REAL(r8) :: delta = 1.d-1
REAL(r8) :: dt = 1.d-3
LOGICAL :: pm=.false.
LOGICAL :: use_mfnk=.false.
LOGICAL :: linear=.false.
NAMELIST/harris_sheet_options/order, chi, eta, nu, gamma, d_diff, &
dt, nsteps, rst_freq, use_mfnk, pm, linear, n0, psi0, velx0, vely0, velz0, &
t0, by0, den_scale, delta, lam_n, lam_b, l_x, l_z
OFT Initialization
See API Example 1: Helmholtz solve for a detailed description of calls in this section.
OPEN(newunit=io_unit,file=
oft_env%ifile)
READ(io_unit,harris_sheet_options,iostat=ierr)
CLOSE(io_unit)
CALL mhd_sim%setup(mg_mesh,order)
NULLIFY(u,v,mop,vec_vals)
minv%A=>mop
minv%its=-2
field_init%func=>dens_harris
CALL u%set(0.d0)
CALL minv%apply(u,v)
CALL u%scale(n0)
CALL u%get_local(vec_vals)
vec_vals = vec_vals / den_scale
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,1)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,1)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,1)
END IF
CALL u%set(velx0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,2)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,2)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,2)
END IF
CALL u%set(vely0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,3)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,3)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,3)
END IF
CALL u%set(velz0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,4)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,4)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,4)
END IF
CALL u%set(t0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,5)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,5)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,5)
END IF
field_init%func=>psi_harris_eq
CALL u%set(0.d0)
CALL minv%apply(u,v)
CALL u%scale(psi0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,6)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,6)
END IF
field_init%func=>psi_harris_pert
CALL u%set(0.d0)
CALL minv%apply(u,v)
CALL u%scale(psi0)
IF(linear)THEN
CALL u%get_local(vec_vals)
CALL mhd_sim%u%restore_local(vec_vals,6)
ELSE
CALL mhd_sim%u%get_local(vec_vals,6)
CALL v%restore_local(vec_vals)
CALL u%add(1.d0, 1.d0, v)
CALL u%get_local(vec_vals)
CALL mhd_sim%u%restore_local(vec_vals,6)
END IF
CALL u%set(by0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,7)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,7)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,7)
END IF
CALL u%delete
CALL v%delete
CALL mop%delete
DEALLOCATE(u,v,mop)
CALL minv%pre%delete
DEALLOCATE(minv%pre)
CALL minv%delete
DEALLOCATE(minv)
mhd_sim%chi=chi
mhd_sim%eta=eta
mhd_sim%nu=nu
mhd_sim%B_0=b_0
mhd_sim%gamma=gamma
mhd_sim%D_diff=d_diff
mhd_sim%dt=dt
mhd_sim%den_scale=den_scale
mhd_sim%nsteps=nsteps
mhd_sim%rst_freq=rst_freq
mhd_sim%mfnk=use_mfnk
mhd_sim%linear=linear
IF(linear)THEN
CALL mhd_sim%run_lin_simulation()
ELSE
CALL mhd_sim%run_simulation()
END IF
contains
subroutine oft_finalize()
Finalize Open FUSION Toolkit environment.
Definition oft_base.F90:364
type(oft_env_type) oft_env
Global container for environment information.
Definition oft_base.F90:137
subroutine oft_init(nthreads, quiet)
Initializes Open FUSION Toolkit run environment.
Definition oft_base.F90:190
class(oft_bmesh), pointer, public mesh
Current mesh level.
Definition xmhd_2d.F90:131
type(oft_ml_fem_type), target, public ml_oft_blagrange
Multilevel finite element representation.
Definition xmhd_2d.F90:132
subroutine, public xmhd_2d_plot(self)
Plot the current solution state using XDMF/HDF5 output Runtime options are set in the main input file...
Definition xmhd_2d.F90:1931
To set the initial conditions we define a set of functions
The non-uniform initial conditions for this case is given by
\[ n_0 = (cosh(z / \lambda))^{-2} + n_{\infty} \]
\[ B_0 = tanh(z / \lambda) \hat{x} \]
The perturbation is then given by
\[ \delta B = -\frac{\pi}{L_z} cos(2 \pi x / L_x) sin(\pi z / L_z) \hat{x} + \frac{2 \pi}{L_x} sin(2 \pi x / L_x) cos(\pi z / L_z) \hat{x} \]
SUBROUTINE dens_harris(pt, val)
REAL(r8), INTENT(in) :: pt(3)
REAL(r8), INTENT(out) :: val
val = den_inf + 1/((cosh(pt(2)/lam_n))**2)
END SUBROUTINE dens_harris
SUBROUTINE psi_harris_eq(pt, val)
REAL(r8), INTENT(in) :: pt(3)
REAL(r8), INTENT(out) :: val
val = -lam_b*log(cosh(pt(2)/lam_b))
END SUBROUTINE psi_harris_eq
SUBROUTINE psi_harris_pert(pt, val)
REAL(r8), INTENT(in) :: pt(3)
REAL(r8), INTENT(out) :: val
val = - delta*cos(2*pi*pt(1)/l_x)*cos(pi*pt(2)/l_z)
END SUBROUTINE psi_harris_pert
END PROGRAM xmhd_tearing
Complete Source
PROGRAM xmhd_tearing
IMPLICIT NONE
INTEGER(i4) :: io_unit,ierr
REAL(r8), POINTER :: vec_vals(:)
TYPE(oft_xmhd_2d_sim) :: mhd_sim
TYPE(multigrid_mesh) :: mg_mesh
TYPE(oft_blag_zerob), TARGET :: blag_zerob
TYPE(poss_scalar_bfield) :: field_init
CLASS(oft_solver), POINTER :: minv => null()
CLASS(oft_matrix), POINTER :: mop => null()
CLASS(oft_vector), POINTER :: u,v
INTEGER(i4) :: order = 2
INTEGER(i4) :: nsteps = 100
INTEGER(i4) :: rst_freq = 10
REAL(r8) :: B_0(3)=0.d0
REAL(r8) :: n0 = 1.d0
REAL(r8) :: velx0 = 1.d0
REAL(r8) :: vely0 = 1.d0
REAL(r8) :: velz0 = 1.d0
REAL(r8) :: t0 = 1.d0
REAL(r8) :: psi0 = 1.d0
REAL(r8) :: by0 = 1.d0
REAL(r8) :: chi=1.d0
REAL(r8) :: eta=1.d0
REAL(r8) :: nu=1.d0
REAL(r8) :: gamma=2.d0/3.d0
REAL(r8) :: D_diff=1.d0
REAL(r8) :: den_scale=1.d19
REAL(r8) :: den_inf = 2.d-1
REAL(r8) :: lam_n = 2.d0
REAL(r8) :: lam_b = 2.d0
REAL(r8) :: L_x = 2.d0
REAL(r8) :: L_z = 2.d0
REAL(r8) :: delta = 1.d-1
REAL(r8) :: dt = 1.d-3
LOGICAL :: pm=.false.
LOGICAL :: use_mfnk=.false.
LOGICAL :: linear=.false.
NAMELIST/harris_sheet_options/order, chi, eta, nu, gamma, d_diff, &
dt, nsteps, rst_freq, use_mfnk, pm, linear, n0, psi0, velx0, vely0, velz0, &
t0, by0, den_scale, delta, lam_n, lam_b, l_x, l_z
OPEN(newunit=io_unit,file=
oft_env%ifile)
READ(io_unit,harris_sheet_options,iostat=ierr)
CLOSE(io_unit)
CALL mhd_sim%setup(mg_mesh,order)
NULLIFY(u,v,mop,vec_vals)
minv%A=>mop
minv%its=-2
field_init%func=>dens_harris
CALL u%set(0.d0)
CALL minv%apply(u,v)
CALL u%scale(n0)
CALL u%get_local(vec_vals)
vec_vals = vec_vals / den_scale
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,1)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,1)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,1)
END IF
CALL u%set(velx0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,2)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,2)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,2)
END IF
CALL u%set(vely0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,3)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,3)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,3)
END IF
CALL u%set(velz0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,4)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,4)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,4)
END IF
CALL u%set(t0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,5)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,5)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,5)
END IF
field_init%func=>psi_harris_eq
CALL u%set(0.d0)
CALL minv%apply(u,v)
CALL u%scale(psi0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,6)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,6)
END IF
field_init%func=>psi_harris_pert
CALL u%set(0.d0)
CALL minv%apply(u,v)
CALL u%scale(psi0)
IF(linear)THEN
CALL u%get_local(vec_vals)
CALL mhd_sim%u%restore_local(vec_vals,6)
ELSE
CALL mhd_sim%u%get_local(vec_vals,6)
CALL v%restore_local(vec_vals)
CALL u%add(1.d0, 1.d0, v)
CALL u%get_local(vec_vals)
CALL mhd_sim%u%restore_local(vec_vals,6)
END IF
CALL u%set(by0)
CALL u%get_local(vec_vals)
IF(linear)THEN
CALL mhd_sim%u0%restore_local(vec_vals,7)
vec_vals = 0.d0
CALL mhd_sim%u%restore_local(vec_vals,7)
ELSE
CALL mhd_sim%u%restore_local(vec_vals,7)
END IF
CALL u%delete
CALL v%delete
CALL mop%delete
DEALLOCATE(u,v,mop)
CALL minv%pre%delete
DEALLOCATE(minv%pre)
CALL minv%delete
DEALLOCATE(minv)
mhd_sim%chi=chi
mhd_sim%eta=eta
mhd_sim%nu=nu
mhd_sim%B_0=b_0
mhd_sim%gamma=gamma
mhd_sim%D_diff=d_diff
mhd_sim%dt=dt
mhd_sim%den_scale=den_scale
mhd_sim%nsteps=nsteps
mhd_sim%rst_freq=rst_freq
mhd_sim%mfnk=use_mfnk
mhd_sim%linear=linear
IF(linear)THEN
CALL mhd_sim%run_lin_simulation()
ELSE
CALL mhd_sim%run_simulation()
END IF
CONTAINS
SUBROUTINE dens_harris(pt, val)
REAL(r8), INTENT(in) :: pt(3)
REAL(r8), INTENT(out) :: val
val = den_inf + 1/((cosh(pt(2)/lam_n))**2)
END SUBROUTINE dens_harris
SUBROUTINE psi_harris_eq(pt, val)
REAL(r8), INTENT(in) :: pt(3)
REAL(r8), INTENT(out) :: val
val = -lam_b*log(cosh(pt(2)/lam_b))
END SUBROUTINE psi_harris_eq
SUBROUTINE psi_harris_pert(pt, val)
REAL(r8), INTENT(in) :: pt(3)
REAL(r8), INTENT(out) :: val
val = - delta*cos(2*pi*pt(1)/l_x)*cos(pi*pt(2)/l_z)
END SUBROUTINE psi_harris_pert
END PROGRAM xmhd_tearing