diff --git a/src/base/oft_stitching.F90 b/src/base/oft_stitching.F90 index 4b4f7053f..15d3a2738 100644 --- a/src/base/oft_stitching.F90 +++ b/src/base/oft_stitching.F90 @@ -12,6 +12,7 @@ !! @ingroup doxy_oft_grid !--------------------------------------------------------------------------------- MODULE oft_stitching +USE, INTRINSIC :: iso_c_binding, ONLY: c_int8_t USE oft_base IMPLICIT NONE #include "local.h" @@ -25,12 +26,14 @@ MODULE oft_stitching LOGICAL :: full = .FALSE. !< Flag indicating local mesh is complete LOGICAL :: skip = .FALSE. !< Flag for skipping stitching operations INTEGER(i4) :: nbemax = 0 !< Maximum number of boundary elements + INTEGER(i4) :: nown = -1 !< Length of `own` (-1 if not yet built) INTEGER(i4) :: nbe = 0 !< Number of local boundary elements INTEGER(i4) :: nie = 0 !< Number of interior elements INTEGER(i4) :: nle = 0 !< Number of local boundary elements INTEGER(i4) :: nproc_con = 0 !< Number of connections to other processors INTEGER(i4) :: proc_split = 0 !< Location of self in processor list LOGICAL, POINTER, CONTIGUOUS, DIMENSION(:) :: leo => NULL() !< List of boundary element ownership + INTEGER(c_int8_t), POINTER, CONTIGUOUS, DIMENSION(:) :: own => NULL() !< Ownership weight (1/0) for each local element (built on first reduction) LOGICAL, POINTER, CONTIGUOUS, DIMENSION(:) :: be => NULL() !< Boundary element flag INTEGER(i4), POINTER, CONTIGUOUS, DIMENSION(:) :: lbe => NULL() !< List of boundary elements in full vector INTEGER(i4), POINTER, CONTIGUOUS, DIMENSION(:) :: lie => NULL() !< List of interior elements in full vector @@ -83,6 +86,96 @@ MODULE oft_stitching END INTERFACE oft_global_stitch CONTAINS !--------------------------------------------------------------------------------- +!> Build local ownership weights used by global reductions +!! +!! Reductions sum only owned entries directly, instead of summing all entries and +!! subtracting non-owned (shared/periodic) duplicates. The latter is subject to +!! catastrophic cancellation when duplicates dominate the sum, which can produce +!! negative values for self dot-products. A 0/1 integer weight is used instead of a +!! logical mask as the resulting multiply vectorizes with GCC, while a masked +!! selection (MERGE/IF) does not. +!--------------------------------------------------------------------------------- +subroutine seam_build_own(self,n) +type(oft_seam), intent(inout) :: self !< Seam structure +integer(i4), intent(in) :: n !< Length of local arrays +integer(i4) :: i +DEBUG_STACK_PUSH +!$omp critical (oft_seam_own_init) +IF(self%nown/=n)THEN + IF(ASSOCIATED(self%own))DEALLOCATE(self%own) + ALLOCATE(self%own(n)) + self%own=1_c_int8_t + DO i=1,self%nbe + IF(.NOT.self%leo(i))self%own(self%lbe(i))=0_c_int8_t + END DO + self%nown=n +END IF +!$omp end critical (oft_seam_own_init) +DEBUG_STACK_POP +end subroutine seam_build_own +!--------------------------------------------------------------------------------- +!> Local owned-only real(r8) dot product (explicit-shape arrays allow vectorization) +!--------------------------------------------------------------------------------- +function owned_dp_r8(a,b,w,n) result(c) +integer(i4), intent(in) :: n !< Length of local arrays +real(r8), intent(in) :: a(n) !< Local vector 1 for dot_product +real(r8), intent(in) :: b(n) !< Local vector 2 for dot_product +integer(c_int8_t), intent(in) :: w(n) !< Ownership weights (1/0) +integer(i4) :: i +real(r8) :: c +c=0.d0 +!$omp parallel do simd reduction(+:c) if(n>OFT_OMP_VTHRESH) +do i=1,n + c=c+a(i)*b(i)*REAL(w(i),r8) +end do +end function owned_dp_r8 +!--------------------------------------------------------------------------------- +!> Local owned-only complex(c8) dot product (explicit-shape arrays allow vectorization) +!--------------------------------------------------------------------------------- +function owned_dp_c8(a,b,w,n) result(c) +integer(i4), intent(in) :: n !< Length of local arrays +complex(c8), intent(in) :: a(n) !< Local vector 1 for dot_product +complex(c8), intent(in) :: b(n) !< Local vector 2 for dot_product +integer(c_int8_t), intent(in) :: w(n) !< Ownership weights (1/0) +integer(i4) :: i +complex(c8) :: c +c=(0.d0,0.d0) +!$omp parallel do simd reduction(+:c) if(n>OFT_OMP_VTHRESH) +do i=1,n + c=c+CONJG(a(i))*b(i)*REAL(w(i),r8) +end do +end function owned_dp_c8 +!--------------------------------------------------------------------------------- +!> Local owned-only real(r8) sum (explicit-shape arrays allow vectorization) +!--------------------------------------------------------------------------------- +function owned_sum_r8(a,w,n) result(c) +integer(i4), intent(in) :: n !< Length of local array +real(r8), intent(in) :: a(n) !< Local data for reduction +integer(c_int8_t), intent(in) :: w(n) !< Ownership weights (1/0) +integer(i4) :: i +real(r8) :: c +c=0.d0 +!$omp parallel do simd reduction(+:c) if(n>OFT_OMP_VTHRESH) +do i=1,n + c=c+a(i)*REAL(w(i),r8) +end do +end function owned_sum_r8 +!--------------------------------------------------------------------------------- +!> Local owned-only complex(c8) sum (explicit-shape arrays allow vectorization) +!--------------------------------------------------------------------------------- +function owned_sum_c8(a,w,n) result(c) +integer(i4), intent(in) :: n !< Length of local array +complex(c8), intent(in) :: a(n) !< Local data for reduction +integer(c_int8_t), intent(in) :: w(n) !< Ownership weights (1/0) +integer(i4) :: i +complex(c8) :: c +c=(0.d0,0.d0) +!$omp parallel do simd reduction(+:c) if(n>OFT_OMP_VTHRESH) +do i=1,n + c=c+a(i)*REAL(w(i),r8) +end do +end function owned_sum_c8 +!--------------------------------------------------------------------------------- !> real(r8) dot_product implementation of \ref oft_stitching::oft_global_reduction !--------------------------------------------------------------------------------- function global_dp_r8(self,a,b,n,no_reduce) result(c) @@ -100,16 +193,15 @@ function global_dp_r8(self,a,b,n,no_reduce) result(c) !---Global reduction c=0.d0 IF(.NOT.self%skip)THEN - !$omp parallel reduction(+:c) if(n>OFT_OMP_VTHRESH) - !$omp do simd - do i=1,n - c=c+a(i)*b(i) - end do - !$omp do simd - do i=1,self%nbe - if(.NOT.self%leo(i))c=c-a(self%lbe(i))*b(self%lbe(i)) - end do - !$omp end parallel + IF(self%nbe>0)THEN + IF(self%nown/=n)CALL seam_build_own(self,n) + c=owned_dp_r8(a,b,self%own,n) + ELSE + !$omp parallel do simd reduction(+:c) if(n>OFT_OMP_VTHRESH) + do i=1,n + c=c+a(i)*b(i) + end do + END IF END IF IF(do_reduce)c=oft_mpi_sum(c) DEBUG_STACK_POP @@ -132,16 +224,15 @@ function global_dp_c8(self,a,b,n,no_reduce) result(c) !---Global reduction c=0.d0 IF(.NOT.self%skip)THEN - !$omp parallel reduction(+:c) if(n>OFT_OMP_VTHRESH) - !$omp do simd - do i=1,n - c=c+CONJG(a(i))*b(i) - end do - !$omp do simd - do i=1,self%nbe - if(.NOT.self%leo(i))c=c-CONJG(a(self%lbe(i)))*b(self%lbe(i)) - end do - !$omp end parallel + IF(self%nbe>0)THEN + IF(self%nown/=n)CALL seam_build_own(self,n) + c=owned_dp_c8(a,b,self%own,n) + ELSE + !$omp parallel do simd reduction(+:c) if(n>OFT_OMP_VTHRESH) + do i=1,n + c=c+CONJG(a(i))*b(i) + end do + END IF END IF IF(do_reduce)c=oft_mpi_sum(c) DEBUG_STACK_POP @@ -159,16 +250,15 @@ function global_reduction_r8(self,a,n) result(c) !---Global reduction c=0.d0 IF(.NOT.self%skip)THEN - !$omp parallel reduction(+:c) if(n>OFT_OMP_VTHRESH) - !$omp do simd - do i=1,n - c=c+a(i) - end do - !$omp do simd - do i=1,self%nbe - if(.NOT.self%leo(i))c=c-a(self%lbe(i)) - end do - !$omp end parallel + IF(self%nbe>0)THEN + IF(self%nown/=n)CALL seam_build_own(self,n) + c=owned_sum_r8(a,self%own,n) + ELSE + !$omp parallel do simd reduction(+:c) if(n>OFT_OMP_VTHRESH) + do i=1,n + c=c+a(i) + end do + END IF END IF IF(.NOT.self%full)c=oft_mpi_sum(c) DEBUG_STACK_POP @@ -186,16 +276,15 @@ function global_reduction_c8(self,a,n) result(c) !---Global reduction c=0.d0 IF(.NOT.self%skip)THEN - !$omp parallel reduction(+:c) if(n>OFT_OMP_VTHRESH) - !$omp do simd - do i=1,n - c=c+a(i) - end do - !$omp do simd - do i=1,self%nbe - if(.NOT.self%leo(i))c=c-a(self%lbe(i)) - end do - !$omp end parallel + IF(self%nbe>0)THEN + IF(self%nown/=n)CALL seam_build_own(self,n) + c=owned_sum_c8(a,self%own,n) + ELSE + !$omp parallel do simd reduction(+:c) if(n>OFT_OMP_VTHRESH) + do i=1,n + c=c+a(i) + end do + END IF END IF IF(.NOT.self%full)c=oft_mpi_sum(c) DEBUG_STACK_POP @@ -658,6 +747,8 @@ subroutine destory_seam(self) integer(i4) :: i DEBUG_STACK_PUSH IF(ASSOCIATED(self%leo))DEALLOCATE(self%leo) +IF(ASSOCIATED(self%own))DEALLOCATE(self%own) +self%nown=-1 IF(ASSOCIATED(self%be))DEALLOCATE(self%be) IF(ASSOCIATED(self%lbe))DEALLOCATE(self%lbe) IF(ASSOCIATED(self%lie))DEALLOCATE(self%lie) diff --git a/src/docs/doc_mug_main.md b/src/docs/doc_mug_main.md index 6468e012a..924735e5e 100644 --- a/src/docs/doc_mug_main.md +++ b/src/docs/doc_mug_main.md @@ -12,7 +12,8 @@ MUG has two different implementations: - The primary implementation (\ref xmhd) using H(Curl) and Lagrange basis functions - An alternative implementation (\ref xmhd_lag) using only Lagrange basis functions and divergence cleaning -\warning Not all capability is implemented in the \ref xmhd_lag "Lagrange only" variant. +Both implementations support the same equations, options, and time integration (see \ref doc_mug_xmhd_eqns_lag +for the differences, which relate to the magnetic field representation). \section doc_mhd_main_ex MUG Examples The following examples illustrate usage of MUG to run time-dependent resistive and Hall MHD simulations. @@ -21,6 +22,7 @@ The following examples illustrate usage of MUG to run time-dependent resistive a - \subpage doc_mug_sph_ex2 - \subpage doc_mug_recon_ex - \subpage doc_mug_gyro_grav_ex + - \subpage doc_mug_gyro_grav_lag_ex \section doc_mhd_main_eqns MHD System MUG solves the single- or two-temperature extended MHD equations: continuity, momentum (with optional @@ -30,4 +32,4 @@ temperature evolution with anisotropic thermal conduction. The complete system a - \subpage doc_mug_xmhd_eqns -Most features are also supported by the \ref xmhd_lag "Lagrange only" variant. +The same system is also advanced by the \ref xmhd_lag "Lagrange only" variant, see \ref doc_mug_xmhd_eqns_lag. diff --git a/src/docs/doc_mug_xmhd_eqns.md b/src/docs/doc_mug_xmhd_eqns.md index 0024fe5d5..b5b4c92af 100644 --- a/src/docs/doc_mug_xmhd_eqns.md +++ b/src/docs/doc_mug_xmhd_eqns.md @@ -4,7 +4,9 @@ MUG: Extended MHD equations (H(Curl) + Lagrange implementation) {#doc_mug_xm [TOC] This page documents the system of equations advanced by the nonlinear time integration in the -primary MUG implementation (\ref xmhd, driven by \ref xmhd::xmhd_run "xmhd_run"). The complete +primary MUG implementation (\ref xmhd, driven by \ref xmhd::xmhd_run "xmhd_run"). The Lagrange-only +variant (\ref xmhd_lag) advances the same system, with the differences listed in +\ref doc_mug_xmhd_eqns_lag. The complete system is defined by the nonlinear residual function \ref xmhd::xmhd_errmatrix_apply "xmhd_errmatrix_apply". The Jacobian built in \ref xmhd::xmhd_build_ops "xmhd_build_ops" is used only for preconditioning, so it contains only an approximate subset of the terms below and is not @@ -126,8 +128,6 @@ This is Braginskii's parallel stress, with \f$ \mu_\parallel = m_i n_0 \nu_\para stress rather than replacing its parallel part, so with `visc_type='iso'` the total parallel coefficient is \f$ \mu_\parallel + \mu_f \f$. -\note In the Lagrange-only variant (\ref xmhd_lag), `nu_par` is still the isotropic fluid viscosity. - **Gyroviscosity** (\f$ \Pi_{gv} \f$) If `nu_gyro>0` @@ -358,3 +358,22 @@ Additional details: - Rows of the auxiliary equations (\f$ N_2 \f$, \f$ J_2 \f$) are scaled by `n2_scale` and `j2_scale`. They are assembled only for the implicit solve, and the right-hand side uses the auxiliary fields from the previous step. + +\section doc_mug_xmhd_eqns_lag Lagrange-only variant + +The Lagrange-only variant (\ref xmhd_lag) solves the system above, with the same options, time +discretization, and boundary conditions, but represents the magnetic field using vector Lagrange +elements (the same basis as \f$ V \f$). The differences are: + - As the nodal magnetic field is not divergence free by construction, a divergence cleaning term is + added to the induction equation (including solid regions) + \f[ \frac{\partial B}{\partial t} = - \nabla \times E + \kappa_{db} \nabla \nabla \cdot B \f] + where \f$ \kappa_{db} \f$ is set by `k_db` (m^2/s, default 1). The term is integrated by parts with the + boundary term dropped and is always time-centered. The periodic divergence cleaning of \ref xmhd is not + used (`nclean` is ignored), and the divergence error \f$ \int (\nabla \cdot B)^2 dV \f$ is always computed + and reported (`xmhd_monitor_div` is ignored). + - The magnetic field boundary condition (`bbc`) is applied to the nodal field: `bbc='bc'` holds the normal + component of \f$ B \f$ fixed, and `bbc='ic'` holds the tangential components fixed, on the boundary. On the + axis of rotationally periodic (revolved) meshes the components normal to the axis are held fixed, as for + \f$ V \f$. + - The hyper-resistivity terms (`eta_hyper>0`), which use an auxiliary H(Curl) field \f$ J_2 \f$ with the + Lagrange magnetic field, have not been verified against \ref xmhd. diff --git a/src/examples/MUG/gyro_gravitational/CMakeLists.txt b/src/examples/MUG/gyro_gravitational/CMakeLists.txt index baafdd2e5..f04345751 100644 --- a/src/examples/MUG/gyro_gravitational/CMakeLists.txt +++ b/src/examples/MUG/gyro_gravitational/CMakeLists.txt @@ -5,3 +5,4 @@ configure_file( oft_in.xml ${CMAKE_CURRENT_BINARY_DIR} COPYONLY) install(FILES run_scan.py DESTINATION examples/MUG/gyro_gravitational COMPONENT examples) configure_file( run_scan.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY) oft_add_exe( MUG_gyro_grav.F90 ) +oft_add_exe( MUG_gyro_grav_lag.F90 ) diff --git a/src/examples/MUG/gyro_gravitational/MUG_gyro_grav_lag.F90 b/src/examples/MUG/gyro_gravitational/MUG_gyro_grav_lag.F90 new file mode 100644 index 000000000..5ec89ba95 --- /dev/null +++ b/src/examples/MUG/gyro_gravitational/MUG_gyro_grav_lag.F90 @@ -0,0 +1,238 @@ +!------------------------------------------------------------------------------ +! Flexible Unstructured Simulation Infrastructure with Open Numerics (Open FUSION Toolkit) +! +! SPDX-License-Identifier: LGPL-3.0-only +!------------------------------------------------------------------------------ +!!MUG Example: Gravitational instability with the Lagrange-only model {#doc_mug_gyro_grav_lag_ex} +!!============================ +!! +!![TOC] +!! +!! This example runs the gyroviscous gravitational instability case of \ref doc_mug_gyro_grav_ex with the +!! \ref xmhd_lag "Lagrange-only" extended MHD module, where the magnetic field is represented using vector +!! Lagrange elements with divergence cleaning (see \ref doc_mug_xmhd_eqns_lag). The problem setup, input file, +!! and post-processing are the same as for that example, and the growth rate scan can be run with +!! +!! \verbatim +!! python run_scan.py --exe ./MUG_gyro_grav_lag --plot +!! \endverbatim +!! +!! The only differences from \ref doc_mug_gyro_grav_ex are the FE spaces that are constructed and the +!! projection of the initial magnetic field onto the vector Lagrange space. +!! +!!\section doc_mug_gyro_grav_lag_ex_code Code walk through +!! +!!\subsection doc_mug_gyro_grav_lag_ex_code_helper Helper module +!! +!! The helper module defines an implementation of \ref fem_utils::fem_interp "fem_interp" that +!! evaluates the initial density and magnetic field. +! START SOURCE +MODULE gyro_grav_lag_helpers +USE oft_base +USE fem_utils, ONLY: fem_interp +IMPLICIT NONE +!------------------------------------------------------------------------------ +!> Initial condition for the gravitational instability (SI units) +!------------------------------------------------------------------------------ +TYPE, EXTENDS(fem_interp) :: grav_interp + CHARACTER(LEN=1) :: field = 'n' !< Field to evaluate ('n' or 'b') + REAL(r8) :: n0 = 1.d19 !< Density at the midplane [m^-3] + REAL(r8) :: B0 = 1.d0 !< Field at the midplane [T] + REAL(r8) :: Ln = 1.d0 !< Density gradient scale length [m] + REAL(r8) :: bcoef = 0.d0 !< \f$ 2 \mu_0 (m_i g L_n + k T) \f$ + REAL(r8) :: eps = 1.d-6 !< Relative amplitude of the density perturbation + REAL(r8) :: kx = 1.d0 !< Wavenumber along x [m^-1] + REAL(r8) :: Lz = 1.d0 !< Distance between the walls [m] +CONTAINS + !> Evaluate field + PROCEDURE :: interp => grav_interp_apply +END TYPE grav_interp +CONTAINS +!------------------------------------------------------------------------------ +!> Evaluate the density (equilibrium plus perturbation) or the equilibrium field +!------------------------------------------------------------------------------ +SUBROUTINE grav_interp_apply(self,cell,f,gop,val) +CLASS(grav_interp), INTENT(inout) :: self !< Interpolation object +INTEGER(i4), INTENT(in) :: cell !< Cell for interpolation +REAL(r8), INTENT(in) :: f(:) !< Position in cell in logical coord [4] +REAL(r8), INTENT(in) :: gop(3,4) !< Logical gradient vectors at f [3,4] +REAL(r8), INTENT(out) :: val(:) !< Field value at the point +REAL(r8) :: pt(3),neq +pt=self%mesh%log2phys(cell,f) +neq=self%n0*EXP(pt(3)/self%Ln) +SELECT CASE(self%field) + CASE('n') + val(1)=neq + self%eps*self%n0*COS(self%kx*pt(1))*COS(pi*pt(3)/self%Lz) + CASE('b') + val=[0.d0, SQRT(self%B0**2 - self%bcoef*(neq-self%n0)), 0.d0] + CASE DEFAULT + CALL oft_abort('Unknown field','grav_interp_apply',__FILE__) +END SELECT +END SUBROUTINE grav_interp_apply +END MODULE gyro_grav_lag_helpers +!!\subsection doc_mug_gyro_grav_lag_ex_code_driver Driver program +!! +!! The driver reads the case parameters in normalized units, builds the Lagrange FE spaces, projects the +!! initial fields, sets gravity, and runs the nonlinear model with a small perturbation. +PROGRAM MUG_gyro_grav_lag +USE oft_base +!---Grid +USE multigrid, ONLY: multigrid_mesh +USE multigrid_build, ONLY: multigrid_construct +!---Linear algebra +USE oft_la_base, ONLY: oft_vector, oft_matrix +USE oft_solver_base, ONLY: oft_solver +USE oft_solver_utils, ONLY: create_cg_solver, create_diag_pre +!---Lagrange FE space +USE oft_lag_basis, ONLY: oft_lag_setup +USE oft_lag_operators, ONLY: lag_setup_interp, oft_lag_getmop, oft_lag_project, & + oft_lag_vgetmop, oft_lag_vproject +!---Physics +USE mhd_utils, ONLY: mu0, proton_mass, elec_charge +USE xmhd_lag, ONLY: xmhd_run, xmhd_minlev, xmhd_sub_fields, g_accel, xmhd_ML_lagrange, & + xmhd_ML_vlagrange +USE gyro_grav_lag_helpers, ONLY: grav_interp +IMPLICIT NONE +!!\subsubsection doc_mug_gyro_grav_lag_ex_code_vars Local variables and input options +!! +!! The case is defined in the paper's normalized units (`beta`, `g`, `Ln`, `I0`), with the reference +!! density `n_bar` [m^-3] and field `B_bar` [T] setting the physical scales. The ion mass is the proton +!! mass, so `mu_ion=1` must be set in `xmhd_options`. The domain size is taken from the mesh. +CLASS(oft_solver), POINTER :: minv => NULL() +CLASS(oft_matrix), POINTER :: mop => NULL() +CLASS(oft_vector), POINTER :: u,v,b,vel,den,temp +TYPE(xmhd_sub_fields) :: equil_fields +TYPE(grav_interp), TARGET :: init_field +TYPE(multigrid_mesh) :: mg_mesh +INTEGER(i4) :: io_unit,ierr +REAL(r8) :: d_i,omega_i,g_phys,T0,Lx,zmin,zmax +INTEGER(i4) :: order = 2 !< FE order +INTEGER(i4) :: minlev = 1 !< Lowest FE level for multigrid +REAL(r8) :: n_bar = 1.d19 !< Reference density [m^-3] +REAL(r8) :: B_bar = 1.d-2 !< Reference field [T] +REAL(r8) :: beta = 1.d-3 !< Plasma beta at the midplane +REAL(r8) :: g = 5.d-3 !< Gravity [d_i Omega_i^2] +REAL(r8) :: Ln = 1.d2 !< Density gradient scale length [d_i] +REAL(r8) :: I0 = 1.d2 !< Field at the midplane [B_bar] +REAL(r8) :: eps = 1.d-6 !< Relative amplitude of the density perturbation +NAMELIST/gyro_grav_options/order,minlev,n_bar,B_bar,beta,g,Ln,I0,eps +!!\subsubsection doc_mug_gyro_grav_lag_ex_code_setup Mesh and FE setup +CALL oft_init +OPEN(NEWUNIT=io_unit,FILE=oft_env%ifile) +READ(io_unit,gyro_grav_options,IOSTAT=ierr) +CLOSE(io_unit) +IF(ierr/=0)CALL oft_abort('Error reading "gyro_grav_options"','MUG_gyro_grav_lag',__FILE__) +!---Place the mesh ground point at the top wall, away from the periodic boundaries in x +! (corner vertices on both periodic boundaries have too many periodic copies) +CALL multigrid_construct(mg_mesh,[0.d0,0.d0,1.d0]) +!---Lagrange +ALLOCATE(xmhd_ML_lagrange,xmhd_ML_vlagrange) +CALL oft_lag_setup(mg_mesh,order,xmhd_ML_lagrange,ML_vlag_obj=xmhd_ML_vlagrange,minlev=minlev) +CALL lag_setup_interp(xmhd_ML_lagrange) +!!\subsubsection doc_mug_gyro_grav_lag_ex_code_params Physical parameters +!! +!! Normalized parameters are converted to SI units, and the equilibrium temperature is set from +!! \f$ \beta = 2 \mu_0 n_0 k T / B_0^2 \f$ at the midplane. +omega_i=elec_charge*B_bar/proton_mass +d_i=SQRT(proton_mass/(mu0*n_bar*elec_charge**2)) +g_phys=g*d_i*omega_i**2 +Lx=MAXVAL(mg_mesh%mesh%r(1,:))-MINVAL(mg_mesh%mesh%r(1,:)) +zmin=-oft_mpi_max(-MINVAL(mg_mesh%mesh%r(3,:))) +zmax=oft_mpi_max(MAXVAL(mg_mesh%mesh%r(3,:))) +Lx=oft_mpi_max(Lx) +init_field%mesh=>mg_mesh%mesh +init_field%n0=n_bar +init_field%B0=I0*B_bar +init_field%Ln=Ln*d_i +init_field%eps=eps +init_field%kx=2.d0*pi/Lx +init_field%Lz=zmax-zmin +T0=beta*init_field%B0**2/(2.d0*mu0*n_bar*elec_charge) +init_field%bcoef=2.d0*mu0*(proton_mass*g_phys*init_field%Ln + elec_charge*T0) +IF(oft_env%head_proc)THEN + WRITE(*,*) + WRITE(*,'(A)')'Gravitational instability case' + WRITE(*,'(A,ES12.4)')' d_i [m] = ',d_i + WRITE(*,'(A,ES12.4)')' Omega_i [s^-1] = ',omega_i + WRITE(*,'(A,ES12.4)')' T [eV] = ',T0 + WRITE(*,'(A,ES12.4)')' g [m/s^2] = ',g_phys + WRITE(*,'(A,2ES12.4)')' Lx, Lz [d_i] = ',Lx/d_i,init_field%Lz/d_i + WRITE(*,*) +END IF +IF(ABS(zmax+zmin)>1.d-6*init_field%Lz)CALL oft_abort('Mesh must be centered in z', & + 'MUG_gyro_grav_lag',__FILE__) +IF(init_field%B0**2<=init_field%bcoef*n_bar*(EXP(zmax/init_field%Ln)-1.d0)) & + CALL oft_abort('Field vanishes inside the domain (increase I0)','MUG_gyro_grav_lag',__FILE__) +!!\subsubsection doc_mug_gyro_grav_lag_ex_code_ic Initial conditions +!! +!! The magnetic field is projected onto the vector Lagrange space and the density onto the Lagrange space. +!! The temperature is uniform and the plasma starts at rest. +NULLIFY(mop) +CALL oft_lag_vgetmop(xmhd_ML_vlagrange%current_level,mop,"none") +CALL create_cg_solver(minv) +minv%A=>mop +minv%its=-3 +minv%atol=1.d-12 +CALL create_diag_pre(minv%pre) +CALL xmhd_ML_vlagrange%vec_create(u) +CALL xmhd_ML_vlagrange%vec_create(v) +CALL xmhd_ML_vlagrange%vec_create(b) +init_field%field='b' +CALL oft_lag_vproject(xmhd_ML_lagrange%current_level,init_field,v) +CALL b%set(0.d0) +CALL minv%apply(b,v) +CALL u%delete +CALL v%delete +CALL mop%delete +DEALLOCATE(u,v,mop) +CALL minv%pre%delete +CALL minv%delete +!---Density +NULLIFY(mop) +CALL oft_lag_getmop(xmhd_ML_lagrange%current_level,mop,"none") +CALL create_cg_solver(minv) +minv%A=>mop +minv%its=-3 +minv%atol=1.d-12 +CALL create_diag_pre(minv%pre) +CALL xmhd_ML_lagrange%vec_create(v) +CALL xmhd_ML_lagrange%vec_create(den) +init_field%field='n' +CALL oft_lag_project(xmhd_ML_lagrange%current_level,init_field,v) +CALL den%set(0.d0) +CALL minv%apply(den,v) +CALL v%delete +CALL mop%delete +DEALLOCATE(v,mop) +CALL minv%pre%delete +CALL minv%delete +!---Temperature and velocity +CALL xmhd_ML_lagrange%vec_create(temp) +CALL temp%set(T0) +CALL xmhd_ML_vlagrange%vec_create(vel) +CALL vel%set(0.d0) +!!\subsubsection doc_mug_gyro_grav_lag_ex_code_run Run simulation +!! +!! Gravity points along \f$ -\hat{z} \f$, which corresponds to \f$ g_{accel} > 0 \f$ (see \ref xmhd_lag::g_accel "g_accel"). +!! The growth rate is then obtained from the kinetic energy in the history file `xmhd.hist`, +!! \f$ \gamma = \frac{1}{2} \partial_t \ln E_k \f$ (see `run_scan.py`). +g_accel=g_phys +xmhd_minlev=minlev +equil_fields%B=>b +equil_fields%V=>vel +equil_fields%Ne=>den +equil_fields%Ti=>temp +CALL xmhd_run(equil_fields) +CALL oft_finalize +END PROGRAM MUG_gyro_grav_lag +!!\section doc_mug_gyro_grav_lag_ex_input Input files +!! +!! The case is run, and the growth rates compared with theory, by `run_scan.py`, which writes an +!! input file of the following form for each value of \f$ \beta \f$. +!! +! OFT_DOC_INCLUDE: oft.in +!! +!! +!! The preconditioner is set in `oft_in.xml` (block-Jacobi with LU on each block). +!! +! OFT_DOC_INCLUDE: oft_in.xml diff --git a/src/lin_alg/deriv_matrices.F90 b/src/lin_alg/deriv_matrices.F90 index 16515629f..7e9b57d63 100644 --- a/src/lin_alg/deriv_matrices.F90 +++ b/src/lin_alg/deriv_matrices.F90 @@ -886,7 +886,7 @@ subroutine mf_mat_apply_real(self,a,b) a1 = self%tmp%norm(1) eps=self%b0*MAX(ABS(bb),a1)*SIGN(1.d0,bb)/aa ELSE - bb=SQRT(self%u0%dot(self%u0)) + bb=SQRT(MAX(self%u0%dot(self%u0),0.d0)) eps=self%b0*SQRT(1.d0+bb)/aa END IF END IF diff --git a/src/lin_alg/native_la.F90 b/src/lin_alg/native_la.F90 index 025dfdcd0..103c6d529 100644 --- a/src/lin_alg/native_la.F90 +++ b/src/lin_alg/native_la.F90 @@ -790,7 +790,7 @@ function vec_norm(self,itype) result(norm) norm=oft_global_reduction(self%stitch_info,vtmp,self%n) DEALLOCATE(vtmp) CASE(2) - norm=SQRT(self%dot(self)) + norm=SQRT(MAX(self%dot(self),0.d0)) ! Clamp round-off from shared/periodic duplicates CASE(3) nloc=0.d0 DO i=1,self%n @@ -1642,7 +1642,7 @@ function cvec_norm(self,itype) result(norm) norm=oft_global_reduction(self%stitch_info,vtmp,self%n) DEALLOCATE(vtmp) CASE(2) - norm=SQRT(REAL(self%dot(self),r8)) + norm=SQRT(MAX(REAL(self%dot(self),r8),0.d0)) ! Clamp round-off from shared/periodic duplicates CASE(3) nloc=0.d0 DO i=1,self%n diff --git a/src/lin_alg/native_solvers.F90 b/src/lin_alg/native_solvers.F90 index ec3829dda..2c6227ce6 100644 --- a/src/lin_alg/native_solvers.F90 +++ b/src/lin_alg/native_solvers.F90 @@ -366,7 +366,7 @@ recursive subroutine cg_solver_apply(self,u,g) if(associated(self%pre))call u%new(s) if(associated(self%bc))call self%bc%apply(g) if(associated(self%orthog))call self%orthog%apply(u) -uu = u%dot(u) +uu = MAX(u%dot(u),0.d0) if(uu==0.d0)then alfa=1.d0; f=0.d0 call q%set(0.d0) @@ -387,7 +387,7 @@ recursive subroutine cg_solver_apply(self,u,g) call p%add(0.d0,1.d0,s) end if gg = g%dot(p) -ggin = g%dot(g) +ggin = MAX(g%dot(g),0.d0) ! Clamp round-off (see GMRES Arnoldi step) 100 FORMAT (I6,3ES14.6) 110 FORMAT (I6,4ES14.6) if((oft_env%pm.AND.oft_env%head_proc))write(*,100)0,f,SQRT(uu),SQRT(ggin) @@ -437,13 +437,13 @@ recursive subroutine cg_solver_apply(self,u,g) gg=g%dot(q) ! Without preconditioning or orthogonalization q==g, so skip the redundant reduction if(associated(self%pre).OR.associated(self%orthog))then - ggnew = g%dot(g) + ggnew = MAX(g%dot(g),0.d0) else - ggnew = gg + ggnew = MAX(gg,0.d0) end if beta=gg/ggold if((k<=self%itplot.OR.MOD(k,self%itplot)==0).AND.oft_env%pm)THEN - uu=u%dot(u) ! Only needed for output + uu=MAX(u%dot(u),0.d0) ! Only needed for output if(oft_env%head_proc)write(*,110)k,f,SQRT(uu),SQRT(ggnew),SQRT(ggnew/uu) END IF if(k==its)exit @@ -617,13 +617,13 @@ recursive subroutine gmres_solver_apply(self,u,g) allocate(hdots(nrits+1),vw(nrits+1),znorm(nrits)) c=0.d0; s=0.d0; res=0.d0; h=0.d0 ! -uu = u%dot(u) +uu = MAX(u%dot(u),0.d0) IF(uu>0.d0)call A%apply(u,w) if(associated(self%bc))call self%bc%apply(w) !if(associated(self%cleaner))call self%cleaner%apply(g) call r%add(0.d0,1.d0,g,-1.d0,w) !if(associated(self%cleaner))call self%cleaner%apply(r) -gg = r%dot(r) +gg = MAX(r%dot(r),0.d0) ! Clamp round-off (see Arnoldi step) ggin=gg 100 FORMAT (I6,2ES14.6) 110 FORMAT (I6,3ES14.6) @@ -656,7 +656,7 @@ recursive subroutine gmres_solver_apply(self,u,g) end if if(associated(self%bc))call self%bc%apply(z(i)%f) if(associated(self%orthog))call self%orthog%apply(z(i)%f) - IF(res_u_tol>0.d0)znorm(i)=SQRT(z(i)%f%dot(z(i)%f)) + IF(res_u_tol>0.d0)znorm(i)=SQRT(MAX(z(i)%f%dot(z(i)%f),0.d0)) call A%apply(z(i)%f,w) if(associated(self%bc))call self%bc%apply(w) !---Arnoldi iteration (CGS with selective re-orthogonalization) @@ -666,19 +666,22 @@ recursive subroutine gmres_solver_apply(self,u,g) vw(i+1)%f=>w hdots(1:i+1)=w%mdot(vw(1:i+1),i+1) ! Fuse projections and initial norm into one reduction h(1:i,i)=hdots(1:i) - wnorm0=SQRT(hdots(i+1)) + wnorm0=SQRT(MAX(hdots(i+1),0.d0)) do k=1,i call w%add(1.d0,-h(k,i),v(k)%f) end do !if(associated(self%cleaner))call self%cleaner%apply(w) - h(i+1,i)=SQRT(w%dot(w)) + ! Native dot products subtract shared/periodic duplicate entries from the full local sum, so + ! after near-complete cancellation (eg. an exact preconditioner) the result can be slightly + ! negative; clamp to zero so this is treated as a (lucky) breakdown instead of producing NaN + h(i+1,i)=SQRT(MAX(w%dot(w),0.d0)) IF(h(i+1,i)0.d0)call v(i+1)%f%add(0.d0,1.d0/h(i+1,i),w) !if(associated(self%cleaner))call self%cleaner%apply(v(i+1)%f) @@ -711,7 +714,7 @@ recursive subroutine gmres_solver_apply(self,u,g) do k=1,i call w%add(1.d0,hdots(k),z(k)%f) end do - inner_conv=(ABS(res(i+1))0.d0)call A%apply(u,w) if(associated(self%bc))call self%bc%apply(w) !if(associated(self%cleaner))call self%cleaner%apply(g) call r%add((0.d0,0.d0),(1.d0,0.d0),g,(-1.d0,0.d0),w) !if(associated(self%cleaner))call self%cleaner%apply(r) -gg = REAL(r%dot(r)) +gg = MAX(REAL(r%dot(r),r8),0.d0) ! Clamp round-off (see Arnoldi step) ggin=gg 100 FORMAT (I6,2ES14.6) 110 FORMAT (I6,3ES14.6) @@ -953,7 +956,7 @@ recursive subroutine cgmres_solver_apply(self,u,g) end if if(associated(self%bc))call self%bc%apply(z(i)%f) if(associated(self%orthog))call self%orthog%apply(z(i)%f) - IF(res_u_tol>0.d0)znorm(i)=SQRT(REAL(z(i)%f%dot(z(i)%f),r8)) + IF(res_u_tol>0.d0)znorm(i)=SQRT(MAX(REAL(z(i)%f%dot(z(i)%f),r8),0.d0)) call A%apply(z(i)%f,w) if(associated(self%bc))call self%bc%apply(w) !---Arnoldi iteration (CGS with selective re-orthogonalization) @@ -963,19 +966,22 @@ recursive subroutine cgmres_solver_apply(self,u,g) vw(i+1)%f=>w hdots(1:i+1)=w%mdot(vw(1:i+1),i+1) ! Fuse projections and initial norm into one reduction h(1:i,i)=CONJG(hdots(1:i)) ! mdot gives w^H v_k - wnorm0=SQRT(REAL(hdots(i+1),r8)) + wnorm0=SQRT(MAX(REAL(hdots(i+1),r8),0.d0)) do k=1,i call w%add((1.d0,0.d0),-h(k,i),v(k)%f) end do !if(associated(self%cleaner))call self%cleaner%apply(w) - h(i+1,i)=SQRT(REAL(w%dot(w),r8)) + ! Native dot products subtract shared/periodic duplicate entries from the full local sum, so + ! after near-complete cancellation (eg. an exact preconditioner) the result can be slightly + ! negative; clamp to zero so this is treated as a (lucky) breakdown instead of producing NaN + h(i+1,i)=SQRT(MAX(REAL(w%dot(w),r8),0.d0)) IF(ABS(h(i+1,i))0.d0)call v(i+1)%f%add((0.d0,0.d0),(1.d0,0.d0)/h(i+1,i),w) !if(associated(self%cleaner))call self%cleaner%apply(v(i+1)%f) @@ -1008,7 +1014,7 @@ recursive subroutine cgmres_solver_apply(self,u,g) do k=1,i call w%add((1.d0,0.d0),hdots(k),z(k)%f) end do - inner_conv=(ABS(res(i+1))kin,2->iso) -LOGICAL, ALLOCATABLE, DIMENSION(:) :: solid_cell !< Solid region flag +LOGICAL, ALLOCATABLE, DIMENSION(:) :: solid_reg !< Solid region flag for each mesh region (valid on all MG levels) !---Physical parameters REAL(r8) :: jac_dt = 1.E-3_r8 !< Time step REAL(r8) :: dt_max = -1.d0 @@ -1804,7 +1804,7 @@ subroutine xmhd_build_ops(fullin) !------------------------------------------------------------------------------ ! Handle solid region !------------------------------------------------------------------------------ - IF(solid_cell(i))THEN + IF(solid_reg(mesh%reg(i)))THEN eta_curr=eta_reg(mesh%reg(i)) IF(ASSOCIATED(res_shaping))eta_curr = eta_curr*res_shaping(pt) do jc=1,oft_hcurl%nce @@ -2563,7 +2563,7 @@ subroutine xmhd_build_ops(fullin) ! Add local contribution to full matrix !------------------------------------------------------------------------------ IF(xmhd_hall.AND.xmhd_two_temp)NULLIFY(mtmp(1,7)%m) - IF(solid_cell(i))THEN + IF(solid_reg(mesh%reg(i)))THEN CALL xmhd_rep%mat_add_local(Jac,mtmp_solid,iloc,tlocks) ELSE CALL xmhd_rep%mat_add_local(Jac,mtmp,iloc,tlocks) @@ -2771,7 +2771,7 @@ subroutine xmhd_alloc_ops ! ALLOCATE(j_lag(oft_lagrange%nce)) DO i=1,mesh%nc - IF(solid_cell(i))THEN + IF(solid_reg(mesh%reg(i)))THEN CALL oft_lagrange%ncdofs(i,j_lag) node_flag(j_lag)=1.d0 END IF @@ -2887,10 +2887,10 @@ subroutine xmhd_setup_regions() ALLOCATE(reg_types(mesh%nreg)) reg_types=1 IF(.NOT.ALLOCATED(eta_reg))THEN - ALLOCATE(eta_reg(mesh%nreg),solid_cell(mesh%nc)) + ALLOCATE(eta_reg(mesh%nreg),solid_reg(mesh%nreg)) END IF eta_reg=-1.d0 -solid_cell=.FALSE. +solid_reg=.FALSE. IF(xmhd_root_node%associated())THEN !---Look for pre node CALL xml_get_element(xmhd_root_node,"region",reg_nodes,ierr) @@ -2948,10 +2948,11 @@ subroutine xmhd_setup_regions() END IF ! xmhd_rw=.FALSE. +solid_reg=(reg_types==2) DO i=1,mesh%nc - IF(reg_types(mesh%reg(i))==2)THEN + IF(solid_reg(mesh%reg(i)))THEN xmhd_rw=.TRUE. - solid_cell(i)=.TRUE. + EXIT END IF END DO #ifdef HAVE_MPI @@ -3376,7 +3377,7 @@ subroutine xmhd_errmatrix_apply(self,a,b) !------------------------------------------------------------------------------ ! Compute F (u) for solid region !------------------------------------------------------------------------------ - IF(solid_cell(i))THEN + IF(solid_reg(mesh%reg(i)))THEN IF(xmhd_adv_b)THEN !---Eta J eta_curr=eta_reg(mesh%reg(i)) @@ -3698,7 +3699,7 @@ subroutine xmhd_errmatrix_apply(self,a,b) end do END IF !---Lagrange rows (V,n,T) - IF(.NOT.solid_cell(i))THEN + IF(.NOT.solid_reg(mesh%reg(i)))THEN DO jr=1,oft_lagrange%nce vout(j_lag(jr),:) = vout(j_lag(jr),:) + lag_loc(jr,1:3)/vel_scale IF(xmhd_adv_den)nout(j_lag(jr)) = nout(j_lag(jr)) + lag_loc(jr,4)/den_scale @@ -3933,7 +3934,7 @@ subroutine xmhd_massmatrix_apply(self,a,b) bc_loc(jr) = bc_loc(jr) + DOT_PRODUCT(hcurl_rop(:,jr),u0%B)*det end do !---Electron inertia - IF(xmhd_hall.AND.(.NOT.solid_cell(i)))THEN + IF(xmhd_hall.AND.(.NOT.solid_reg(mesh%reg(i))))THEN u0%J=u0%J*me_factor*elec_mass*det/(mu0*neq*elec_charge**2) do jr=1,oft_hcurl%nce bc_loc(jr) = bc_loc(jr) + DOT_PRODUCT(hcurl_cop(:,jr),u0%J) @@ -3943,7 +3944,7 @@ subroutine xmhd_massmatrix_apply(self,a,b) do jr=1,oft_hgrad%nce bg_loc(jr) = bg_loc(jr) + DOT_PRODUCT(hgrad_rop(:,jr),u0%B)*det end do - IF(solid_cell(i))CYCLE + IF(solid_reg(mesh%reg(i)))CYCLE !---Compute F_{V,n,T) (u) do jr=1,oft_lagrange%nce lag_loc(jr,1:3) = lag_loc(jr,1:3) + lag_rop(jr)*u0%V*det @@ -4238,11 +4239,6 @@ subroutine xmhd_set_level(level) if(level>xmhd_ML_hcurl%nlevels.OR.level<=0)then call oft_abort('Invalid FE level','xmhd_set_level',__FILE__) end if -! if(levelML_xmhd_rep%current_level !--- @@ -4251,6 +4247,8 @@ subroutine xmhd_set_level(level) CALL xmhd_ML_hcurl_grad%set_level(level,propogate=.TRUE.) IF(.NOT.oft_3D_hcurl_cast(oft_hcurl,xmhd_ML_hcurl%current_level))CALL oft_abort("Invalid Curl FE object","xmhd_run",__FILE__) IF(.NOT.oft_3D_h1_cast(oft_hgrad,xmhd_ML_H1grad%current_level))CALL oft_abort("Invalid Grad FE object","xmhd_run",__FILE__) +!---Operate on the mesh of this level (coarser than the finest mesh on geometric MG levels) +mesh=>oft_hcurl%mesh xmhd_level=level ! xmhd_lev=oft_hcurl_lev oft_xmhd_ops=>oft_xmhd_ops_ML(xmhd_level) diff --git a/src/physics/xmhd_lag.F90 b/src/physics/xmhd_lag.F90 index 314f58c77..44e4cf35c 100644 --- a/src/physics/xmhd_lag.F90 +++ b/src/physics/xmhd_lag.F90 @@ -5,65 +5,34 @@ !--------------------------------------------------------------------------------- !> @file xmhd_lag.F90 ! -!> Module for modeling extended MHD with a standard lagrange basis set with -!! divergence cleaning. +!> Module for modeling extended MHD with a standard Lagrange basis set and divergence +!! cleaning. !! -!! The equations currently solved are the equations of extended xMHD with -!! scalar temperature and pressure. +!! Solves the same single- or two-temperature extended MHD equations as the primary +!! implementation (\ref xmhd), documented in \ref doc_mug_xmhd_eqns, but with all fields, +!! including the magnetic field, represented using Lagrange elements. As the nodal magnetic +!! field is not divergence free by construction, a divergence cleaning term is added to the +!! induction equation !! -!! \f[ \frac{\partial n}{\partial t} + \nabla \cdot \left( n u \right) = -!! D \nabla^2 n \f] +!! \f[ \frac{\partial B}{\partial t} = - \nabla \times E + \kappa_{db} \nabla \nabla \cdot B \f] !! -!! \f[ \rho \left[ \frac{\partial u}{\partial t} + u \cdot \nabla u \right] -!! = J \times B - \nabla nk(T_e + T_i) - \nabla \cdot \Pi \f] +!! where \f$ \kappa_{db} \f$ is set by `k_db`. The cleaning term is included in all regions +!! (including solid regions) and is time-centered. As a result the periodic divergence +!! cleaning used by \ref xmhd is not needed, and `nclean` is ignored. !! -!! \f[ \frac{\partial B}{\partial t} = - \nabla \times \left( -u \times B + -!! \eta J + \frac{1}{ne} \left( J \times B - \nabla nkT_e \right) + \frac{m_e}{ne^2} -!! \frac{\partial J}{\partial t} \right) + \kappa_{db} \nabla \nabla \cdot B \f] -!! -!! \f[ \frac{n}{\gamma-1} \left[ \frac{\partial T_i}{\partial t} + u \cdot \nabla T_i \right] = -!! -nkT_i \nabla \cdot u - \nabla \cdot q_i + Q_i \f] -!! -!! For two temperature -!! \f[ \frac{n}{\gamma-1} \left[ \frac{\partial T_e}{\partial t} + u_e \cdot \nabla T_e \right] = -!! -nkT_e \nabla \cdot u_e - \nabla \cdot q_e + Q_e \f] -!! -!! \f[ u_e = u - \frac{J}{ne} \f] -!! -!! **Thermal transport** -!! -!! \f[ q_s = - n \left[ \chi_{\parallel,s} \hat{b} \hat{b} + \chi_{\perp,s} \left( I -!! - \hat{b} \hat{b} \right) \right] \cdot \nabla T_s \f] -!! -!! With `xmhd_brag=T` and single temperature (\f$ T_e = T_i \f$) -!! \f[ \chi_{\parallel,i} = \chi_{\parallel,e}, \chi_{\perp,i} = min(\chi_{\perp,i},\chi_{\parallel,i}) \f] -!! -!! **Heat sources** -!! -!! For single temperature (\f$ T_e = T_i \f$) -!! \f[ Q_i = \left[ \eta J^2 - \left( \nabla u \right)^T : \Pi \right] / 2 \f] -!! -!! For two temperature -!! \f[ Q_i = - \left( \nabla u \right)^T : \Pi, Q_e = \eta J^2 \f] -!! -!! **Viscosity** -!! -!! \f[ W = \left( \nabla u + (\nabla u)^T - \frac{2}{3} I \nabla \cdot u \right) \f] -!! -!! With `visc_type='kin'` -!! \f[ \Pi = - \nu \nabla u \f] -!! -!! With `visc_type='iso'` -!! \f[ \Pi = - \nu W \f] -!! -!! With `visc_type='ani'` -!! \f[ \Pi = - \left[ \nu_{\parallel} \hat{b} \hat{b} -!! + \nu_{\perp} \left( I - \hat{b} \hat{b} \right) \right] \cdot W \f] +!! The full system of equations is defined by the nonlinear residual +!! (@ref xmhd_lag::xmhd_errmatrix_apply "xmhd_errmatrix_apply"). !! !! Runtime options are available when using the driver (@ref xmhd_lag::xmhd_run "xmhd_run") !! and post-processing (@ref xmhd_lag::xmhd_plot "xmhd_plot") routines, see their documentation !! for available options. !! +!! @note Electron mass is artificially enhanced by a factor using @ref xmhd_lag::me_factor +!! "me_factor" +!! +!! @warning The hyper-resistivity terms (`eta_hyper>0`, auxiliary field \f$ J_2 \f$ in H(Curl)) +!! have not been updated to match \ref xmhd and are untested in this module. +!! !! @authors Chris Hansen !! @date November 2018 !! @ingroup doxy_oft_physics @@ -93,18 +62,21 @@ MODULE xmhd_lag USE fem_base, ONLY: fem_max_levels, fem_common_linkage, oft_fem_type, oft_ml_fem_type USE fem_composite, ONLY: oft_fem_comp_type, oft_ml_fem_comp_type, oft_ml_fe_comp_vecspace USE fem_utils, ONLY: fem_avg_bcc, fem_interp, cc_interp, cross_interp, & - tensor_dot_interp, fem_partition, fem_dirichlet_diag, fem_dirichlet_vec -USE oft_lag_basis, ONLY: oft_lag_eval_all, oft_lag_geval_all, & - oft_scalar_fem, oft_3D_lagrange_cast + tensor_dot_interp, fem_partition, fem_dirichlet_diag, fem_dirichlet_vec, & + fem_map_flag, comp_fem_partition +USE oft_lag_basis, ONLY: oft_lag_eval_all, oft_3D_lagrange_cast, & + oft_lag_geval_all, oft_scalar_fem, lagrange_vector_unpack, lagrange_vector_repack, & + lagrange_vector_packrows, lagrange_vector_packcols, lagrange_vector_transmat, & + lagrange_vector_packblock USE oft_lag_operators, ONLY: oft_lag_vgetmop, oft_lag_vrinterp, oft_lag_vdinterp, & oft_lag_vproject, oft_lag_project_div, oft_lag_rinterp, oft_lag_ginterp, & - lag_vbc_tensor, lag_vbc_diag, oft_lag_vcinterp -USE oft_hcurl_basis, ONLY: oft_hcurl_eval_all, & - oft_hcurl_ceval_all, oft_hcurl_get_cgops, oft_hcurl_fem, & - oft_3D_hcurl_cast + lag_vbc_tensor, lag_vbc_diag, lag_build_per_vinterp, oft_lag_vcinterp +USE oft_hcurl_basis, ONLY: oft_hcurl_eval_all, oft_3D_hcurl_cast, & + oft_hcurl_ceval_all, oft_hcurl_get_cgops, oft_hcurl_fem USE oft_hcurl_operators, ONLY: oft_hcurl_rinterp !--- -USE diagnostic, ONLY: tfluxfun, vec_energy, weighted_vec_energy +USE diagnostic, ONLY: tfluxfun, vec_energy, weighted_vec_energy, scal_energy, & + scal_int, field_probe USE mhd_utils IMPLICIT NONE #include "local.h" @@ -121,6 +93,7 @@ MODULE xmhd_lag !! unnecessarily load the linear solver. !--------------------------------------------------------------------------------- type, abstract, public :: oft_xmhd_driver + LOGICAL :: rst_warn = .TRUE. contains !> Modify vectors to apply forcing procedure(xmhd_driver_apply), deferred :: apply @@ -168,24 +141,24 @@ MODULE xmhd_lag !> xMHD operator container !------------------------------------------------------------------------------ type :: xmhd_ops - LOGICAL, CONTIGUOUS, POINTER, DIMENSION(:) :: b_bc => NULL() !< B-field BC flag (only if `xmhd_adv_b=F`) - LOGICAL, CONTIGUOUS, POINTER, DIMENSION(:) :: v_bc => NULL() !< Velocity BC flag LOGICAL, CONTIGUOUS, POINTER, DIMENSION(:) :: n_bc => NULL() !< Density BC flag - LOGICAL, CONTIGUOUS, POINTER, DIMENSION(:) :: t_bc => NULL() !< Temperature BC flag (Ti/Te) + LOGICAL, CONTIGUOUS, POINTER, DIMENSION(:) :: t_bc => NULL() !< Temperature BC flag LOGICAL, CONTIGUOUS, POINTER, DIMENSION(:) :: solid_node => NULL() !< Solid region flag - class(oft_matrix), pointer :: J => NULL() !< Jacobian matrix - class(oft_matrix), pointer :: interp => NULL() !< Interpolation matrix - type(oft_xmhd_errmatrix) :: A !< NL metric matrix + INTEGER(4), CONTIGUOUS, POINTER, DIMENSION(:) :: b_bc_type => NULL() !< Magnetic field BC flag (see \ref xmhd_lag::xmhd_set_bc "xmhd_set_bc") + INTEGER(4), CONTIGUOUS, POINTER, DIMENSION(:) :: v_bc_type => NULL() !< Velocity BC flag (see \ref xmhd_lag::xmhd_set_bc "xmhd_set_bc") + CLASS(oft_matrix), POINTER :: J => NULL() !< Jacobian matrix + CLASS(oft_matrix), POINTER :: interp => NULL() !< Interpolation matrix + TYPE(oft_xmhd_errmatrix) :: A !< NL metric matrix end type xmhd_ops !------------------------------------------------------------------------------ !> Interpolate a xMHD operator fields !------------------------------------------------------------------------------ type :: xmhd_interp_cache - integer(i4) :: cell = -1 - real(r8), contiguous, pointer, dimension(:) :: Te => NULL() !< Electron temperature + integer(i4) :: cell = -1 !< Cell index for values + real(r8), contiguous, pointer, dimension(:) :: Te => NULL() !< Lagrange values real(r8), contiguous, pointer, dimension(:) :: N2 => NULL() !< Hyper-diff aux values real(r8), contiguous, pointer, dimension(:) :: J2 => NULL() !< Hyper-res aux values (HCurl) - real(r8), contiguous, pointer, dimension(:,:) :: lf => NULL() !< Main fields (B, V, N, Ti) + real(r8), contiguous, pointer, dimension(:,:) :: lf => NULL() !< Lagrange values (B,V,N,Ti) end type xmhd_interp_cache !------------------------------------------------------------------------------ !> Interpolate a xMHD operator fields @@ -194,11 +167,11 @@ MODULE xmhd_lag real(r8), contiguous, pointer, dimension(:) :: Te_loc => NULL() !< Local Te values real(r8), contiguous, pointer, dimension(:) :: N2_loc => NULL() !< Local hyper-diff aux values real(r8), contiguous, pointer, dimension(:) :: J2_loc => NULL() !< Local hyper-res aux values (HCurl) - real(r8), contiguous, pointer, dimension(:,:) :: lf_loc => NULL() !< Local field values (B, V, N, Ti) + real(r8), contiguous, pointer, dimension(:,:) :: lf_loc => NULL() !< Local Lagrange values (B,V,N,Ti) class(oft_vector), pointer :: u => NULL() !< Field to interpolate + class(oft_hcurl_fem), pointer :: curl_rep => NULL() !< H(Curl) FE representation (hyper-resistivity only) class(oft_scalar_fem), pointer :: lag_rep => NULL() !< Lagrange FE representation - class(oft_hcurl_fem), pointer :: hcurl_rep => NULL() !< H(Curl) FE representation - type(xmhd_interp_cache), pointer, dimension(:) :: cache => NULL() !< Thread cache + type(xmhd_interp_cache), pointer :: cache(:) => NULL() !< Thread local field cache contains !> Retrieve local values for interpolation procedure :: setup => xmhd_interp_setup @@ -208,14 +181,14 @@ MODULE xmhd_lag procedure :: delete => xmhd_interp_delete end type xmhd_interp !------------------------------------------------------------------------------ -!> Unpack object for local fields evaluated by \ref xmhd_lag::xmhd_interp "xmhd_interp" +!> Unpack object for local fields evaluated by @ref xmhd_lag::xmhd_interp "xmhd_interp" !------------------------------------------------------------------------------ type :: xmhd_loc_values REAL(r8) :: N = 0.d0 !< Density REAL(r8) :: Ti = 0.d0 !< Ion temperature (T in single fluid) REAL(r8) :: Te = 0.d0 !< Electron temperature - REAL(r8) :: divB = 0.d0 !< Divergence error REAL(r8) :: N2 = 0.d0 !< Hyper-diff aux field + REAL(r8) :: divB = 0.d0 !< Divergence error REAL(r8) :: B(3) = 0.d0 !< Magnetic field REAL(r8) :: V(3) = 0.d0 !< Velocity field REAL(r8) :: J2(3) = 0.d0 !< Hyper-res aux field @@ -228,8 +201,8 @@ MODULE xmhd_lag REAL(r8) :: dV(3,3) = 0.d0 !< Velocity shear tensor end type xmhd_loc_values !------------------------------------------------------------------------------ -!> Object for sub-fields used by \ref xmhd_lag::oft_xmhd_push "oft_xmhd_push" -!! and \ref xmhd_lag::oft_xmhd_pop "oft_xmhd_pop" +!> Object for sub-fields used by @ref xmhd_lag::oft_xmhd_push "oft_xmhd_push" +!! and @ref xmhd_lag::oft_xmhd_pop "oft_xmhd_pop" !------------------------------------------------------------------------------ type, public :: xmhd_sub_fields CLASS(oft_vector), POINTER :: B => NULL() !< Magnetic field "Vector Lagrange" @@ -238,7 +211,7 @@ MODULE xmhd_lag CLASS(oft_vector), POINTER :: Ti => NULL() !< Ion temperature "Lagrange" CLASS(oft_vector), POINTER :: Te => NULL() !< Electron temperature "Lagrange" CLASS(oft_vector), POINTER :: N2 => NULL() !< Hyper-diff aux field "Lagrange" - CLASS(oft_vector), POINTER :: J2 => NULL() !< Hyper-res aux field "HCurl" + CLASS(oft_vector), POINTER :: J2 => NULL() !< Hyper-res aux field "H(Curl)" end type xmhd_sub_fields !---Interface definitions abstract interface @@ -256,10 +229,6 @@ subroutine xmhd_driver_apply(self,up,u,v,t,dt) end subroutine xmhd_driver_apply !------------------------------------------------------------------------------ !> Sample probe signals from solution -!! -!! @param[in,out] self Probe object -!! @param[in,out] sub_fields Unpacked fields at current time -!! @param[in] t Current time !------------------------------------------------------------------------------ subroutine xmhd_probe_apply(self,sub_fields,t) import oft_xmhd_probe, xmhd_sub_fields, r8 @@ -293,13 +262,15 @@ end subroutine xmhd_probe_apply LOGICAL :: xmhd_adv_temp = .TRUE. !< Advance temperature LOGICAL :: xmhd_ohmic = .FALSE. !< Resistive heating LOGICAL :: xmhd_visc_heat = .FALSE. !< Viscous heating -LOGICAL :: xmhd_brag = .FALSE. !< Braginskii thermal conduction +LOGICAL :: xmhd_brag = .FALSE. !< Braginskii temperature-dependent thermal conduction LOGICAL :: xmhd_linear = .FALSE. !< Compute linearized matrix LOGICAL :: xmhd_upwind = .TRUE. !< Use upwinding LOGICAL :: xmhd_rw = .FALSE. !< Resistive wall present +LOGICAL :: xmhd_monitor_div = .FALSE. !< Monitor div error? (unused, always computed for Lagrange B) LOGICAL :: xmhd_two_temp = .FALSE. !< Run 2 Temperature model LOGICAL :: xmhd_therm_equil = .FALSE. !< Include cross-species thermalization LOGICAL :: xmhd_diss_centered = .FALSE. !< Time-center dissipation terms +LOGICAL :: xmhd_lin_bdf2 = .TRUE. !< Use BDF2 time integration for linear runs (otherwise backward Euler) LOGICAL :: xmhd_lin_euler_start = .FALSE. !< Start linear BDF2 runs with one backward Euler step LOGICAL :: xmhd_skip_update = .FALSE. !< Internal flag for skipping Jac update INTEGER(i4) :: te_ind = -1 !< Index of electron temperature field @@ -307,24 +278,27 @@ end subroutine xmhd_probe_apply INTEGER(i4) :: j2_ind = -1 !< Index of hyper-res aux field INTEGER(i4), ALLOCATABLE, DIMENSION(:,:) :: xmhd_mat_mask !< Matrix block mask !---Boundary conditions -LOGICAL :: xmhd_bnorm_force = .TRUE. !< Force B-norm to zero in cleaning -CHARACTER(LEN=4) :: vbc = 'all' !< Velocity BC ('all','norm','tang') -LOGICAL :: xmhd_vbcdir = .FALSE. !< Velocity BC type flag -INTEGER(i4) :: vbc_type = 1 !< Velocity BC type -CHARACTER(LEN=1) :: nbc = 'd' !< Density BC ('d','n') -CHARACTER(LEN=1) :: tbc = 'd' !< Temperature BC ('d','n') -CHARACTER(LEN=3) :: visc_type = 'kin' !< Viscosity type ('kin','iso','ani') -INTEGER(i4) :: visc_itype = 1 !< Viscosity type flag (1->kin,2->iso,3->ani) -LOGICAL, ALLOCATABLE, DIMENSION(:) :: solid_cell !< Solid region flag +LOGICAL :: xmhd_bnorm_force = .TRUE. !< Force B-norm to zero in cleaning (unused, kept for API compatibility with \ref xmhd) +CHARACTER(LEN=2) :: bbc = 'bc' !< Magnetic field BC ('bc','ic') +CHARACTER(LEN=4) :: vbc = 'all' !< Velocity BC ('all','norm','tang','xml') +LOGICAL :: xmhd_bbcdir = .FALSE. !< Magnetic field projected BC flag +LOGICAL :: xmhd_vbcdir = .FALSE. !< Velocity projected BC flag +CHARACTER(LEN=1) :: nbc = 'd' !< Density BC ('d','n','x') +CHARACTER(LEN=1) :: tbc = 'd' !< Temperature BC ('d','n','x') +CHARACTER(LEN=3) :: visc_type = 'kin' !< Viscosity type ('kin','iso') +INTEGER(i4) :: visc_itype = 1 !< Viscosity type flag (1->kin,2->iso) +LOGICAL, ALLOCATABLE, DIMENSION(:) :: reg_solid !< Solid region flag for each mesh region (valid on all MG levels) !---Physical parameters REAL(r8) :: jac_dt = 1.E-3_r8 !< Time step -REAL(r8) :: k_db = 1._r8 !< Divergence cleaning term +REAL(r8) :: dt_max = -1.d0 !< Maximum time step +REAL(r8) :: k_db = 1._r8 !< Divergence cleaning diffusivity [m^2/s] REAL(r8) :: eta = 1.E-2_r8 !< Resistivity \f$ \eta / \mu_0 \f$ REAL(r8) :: eta_temp = -1._r8 !< Reference temperature for resistivity REAL(r8), ALLOCATABLE, DIMENSION(:) :: eta_reg !< Resistivity scale factors REAL(r8) :: eta_hyper = -1.d0 !< Hyper resistivity -REAL(r8) :: nu_par = 0.d0 !< Fluid (parallel) viscosity (note: dynamic viscosity is nu=nu_par*den_scale/n0) -REAL(r8) :: nu_perp = 0.d0 !< Fluid perpendicular viscosity if anisotropic, as nu_par +REAL(r8) :: nu_par = -1.d0 !< Braginskii parallel viscosity, added to `nu_visc` (neg to disable) +REAL(r8) :: nu_visc = -1.d0 !< Fluid viscosity, must be set (note: dynamic viscosity is nu=nu_visc*den_scale/n0) +REAL(r8) :: nu_gyro = -1.d0 !< Gyroviscosity coefficient REAL(r8) :: m_ion = -1.d0 !< Ion mass in SI units REAL(r8) :: mu_ion = 2.d0 !< Ion mass in atomic units REAL(r8) :: d_dens = 1.E0_r8 !< Particle diffusivity @@ -333,26 +307,35 @@ end subroutine xmhd_probe_apply REAL(r8) :: me_factor = 100.E0_r8 !< Artificial electron mass factor REAL(r8) :: k_boltz = elec_charge !< Boltzmann constant REAL(r8) :: temp_gamma = 5.d0/3.d0 !< Ratio of specific heats -REAL(r8) :: kappa_par = 1.d0 !< Parallel thermal conductivity factor -REAL(r8) :: kappa_perp = 1.d0 !< Perpendicular thermal conductivity factor +REAL(r8) :: kappa_par(2) = 1.d0 !< Parallel thermal conductivity factor +REAL(r8) :: kappa_perp(2) = 1.d0 !< Perpendicular thermal conductivity factor +!> Lower limit on the electron perpendicular diffusivity [m^2/s] with Braginskii conduction (two temperatures) +REAL(r8), PARAMETER :: chi_perp_e_min = 0.d0 REAL(r8), PUBLIC :: vel_scale = 1.d3 !< Velocity scale factor REAL(r8), PUBLIC :: den_scale = -1.d0 !< Density scale factor REAL(r8), PUBLIC :: n2_scale = -1.d0 !< Hyper-diffusivity aux variable scale factor -REAL(r8), PUBLIC :: j2_scale = -1.d0 !< Hyper-resistivity aux variable scale factor +REAL(r8), PUBLIC :: j2_scale = -1.d0 !< Hyper-resistivity aux variable scale factor REAL(r8), PUBLIC :: den_floor = -1.d0 !< Density floor REAL(r8), PUBLIC :: temp_floor = -1.d0 !< Temperature floor REAL(r8), PUBLIC :: g_accel = 0.d0 !< Gravitational acceleration [m/s^2], acting in the -Z direction for `g_accel>0` -REAL(r8) :: xmhd_eps = 1.d-10 !< Epsilon for magnetic field normalization +REAL(r8), PUBLIC :: xmhd_B_eps = 1.d-10 !< Epsilon for magnetic field normalization INTEGER(i4) :: xmhd_taxis = 3 !< Axis for toroidal flux and current !> Resistivity profile PROCEDURE(oft_1d_func), PUBLIC, POINTER :: res_profile => NULL() +!> Resistivity shaping +PROCEDURE(oft_poss_func), PUBLIC, POINTER :: res_shaping => NULL() +!> Diffusivity shaping +PROCEDURE(oft_poss_func), PUBLIC, POINTER :: diff_shaping => NULL() +!> Viscosity shaping +PROCEDURE(oft_poss_func), PUBLIC, POINTER :: visc_shaping => NULL() +!> Thermal conductivity shaping +PROCEDURE(oft_poss_func), PUBLIC, POINTER :: cond_shaping => NULL() !---Multi-level environment INTEGER(i4) :: xmhd_blevel = 0 !< Highest level on base meshes -! INTEGER(i4) :: xmhd_lev = 1 !< Active FE level INTEGER(i4) :: xmhd_level = 1 !< Active FE level INTEGER(i4) :: xmhd_lin_level = 1 !< Highest linear element level INTEGER(i4) :: xmhd_nlevels = 1 !< Number of total levels -INTEGER(i4) :: xmhd_minlev = 3 !< Lowest MG level +INTEGER(i4) :: xmhd_minlev = -1 !< Lowest MG level INTEGER(i4), DIMENSION(fem_max_levels) :: nu_xmhd = 1 !< Number of smoother iterations !---Operators and preconditioning TYPE(oft_fem_comp_type), POINTER :: xmhd_rep => NULL() !< Active field representation @@ -364,9 +347,11 @@ end subroutine xmhd_probe_apply TYPE(oft_matrix_ptr), POINTER, DIMENSION(:) :: ml_int => NULL() !< MG interpolation operators TYPE(oft_matrix_ptr), POINTER, DIMENSION(:) :: ml_J => NULL() !< MG Jacobian operators CLASS(oft_solver), POINTER :: xmhd_pre => NULL() !< Preconditioner object +INTEGER(i4) :: gmres_maxits = 200 +INTEGER(i4) :: gmres_ninner = 20 INTEGER(i4) :: xmhd_prefreq = 30 !< Desired update frequency for preconditioner INTEGER(i4) :: xmhd_opcount = 1 !< Number of time steps since preconditioner update -INTEGER(i4) :: xmhd_nparts = 1 !< Number of local partitions for preconditioning +INTEGER(i4), DIMENSION(fem_max_levels) :: xmhd_nparts = 1 !< Total number of partitions for preconditioning (divided across MPI processes) REAL(r8) :: xmhd_opdt = 1.d0 !< Time step for current preconditioner object LOGICAL :: xmhd_mfnk = .FALSE. !< Use Jacobian free non-linear advance !---Interpolator cache variables @@ -376,12 +361,13 @@ end subroutine xmhd_probe_apply REAL(r8), CONTIGUOUS, POINTER, DIMENSION(:,:) :: xmhd_hcurl_cop => NULL() !$omp threadprivate(xmhd_lag_rop,xmhd_lag_gop,xmhd_hcurl_rop,xmhd_hcurl_cop) REAL(r8), ALLOCATABLE, DIMENSION(:,:) :: neg_source,neg_flag -! CLASS(multigrid_mesh), POINTER :: mg_mesh => NULL() CLASS(oft_mesh), POINTER :: mesh => NULL() +! TYPE(oft_ml_fem_type), POINTER, PUBLIC :: xmhd_ML_lagrange => NULL() -TYPE(oft_ml_fem_type), POINTER, PUBLIC :: xmhd_ML_hcurl => NULL() +TYPE(oft_ml_fem_type), POINTER, PUBLIC :: xmhd_ML_hcurl => NULL() !< H(Curl) space (hyper-resistivity only) TYPE(oft_ml_fem_comp_type), POINTER, PUBLIC :: xmhd_ML_vlagrange => NULL() +! CLASS(oft_scalar_fem), POINTER :: oft_lagrange => NULL() CLASS(oft_hcurl_fem), POINTER :: oft_hcurl => NULL() !------------------------------------------------------------------------------ @@ -399,8 +385,8 @@ end subroutine xmhd_probe_apply !! for new simulations. They are merely placeholders to indicate the type of the input. !! !! **Option group:** `xmhd_options` -!! | Option | Description | Type [dim] | -!! |------------|------------|-----| +!! | Option | Description | Type [dim] | +!! |------------|---------------|------------| !! | `xmhd_jcb=T` | Apply magnetic force to fluid | bool | !! | `xmhd_advec=T` | Advect fluid | bool | !! | `xmhd_adv_den=T` | Advance density | bool | @@ -409,44 +395,52 @@ end subroutine xmhd_probe_apply !! | `xmhd_ohmic=F` | Include Ohmic heating | bool | !! | `xmhd_visc_heat=F` | Include Viscous heating | bool | !! | `xmhd_brag=F` | Use Braginskii thermal conduction coefficients | bool | -!! | `xmhd_upwind=T` | Use upwinding for advection terms | bool | +!! | `xmhd_upwind=T` | Use streamline upwinding for advection terms (see \ref doc_mug_xmhd_eqns_upwind). Ignored (always off) for linear runs | bool | !! | `xmhd_therm_equil=F` | Include thermal equilibration between electrons/ions | bool | !! | `xmhd_diss_centered=F` | Use time-centered dissipation terms for (V, Ti, Te) | bool | +!! | `xmhd_lin_bdf2=T` | Linear runs only: use second-order BDF2 time integration (otherwise first-order backward Euler) | bool | !! | `xmhd_lin_euler_start=F` | Linear runs only: take one backward Euler step before BDF2, instead of using the initial condition for both BDF2 history levels (costs one extra preconditioner update) | bool | -!! | `vbc="all"` | Velocity boundary condition ("all") | str(3) | -!! | `nbc="d"` | Density boundary condition ("d", "n") | str(1) | -!! | `tbc="d"` | Temperature boundary condition ("d", "n") | str(1) | -!! | `visc_type="kin"` | Fluid viscosity type ("kin", "iso", "ani") | str(3) | -!! | `dt=1` | Maximum allowed time step | real | -!! | `k_db=1` | Magnetic divergence cleaning factor | real | -!! | `eta=1` | Plasma resistivity, in diffusivity units | real | +!! | `bbc="bc"` | Magnetic field boundary condition ("bc": fixed normal component, "ic": fixed tangential components) | str(2) | +!! | `vbc="all"` | Velocity boundary condition ("all", "norm", "tang", "xml": per surface from ``) | str(4) | +!! | `nbc="d"` | Density boundary condition ("d", "n", "x": per surface from ``) | str(1) | +!! | `tbc="d"` | Temperature boundary condition ("d", "n", "x": per surface from ``) | str(1) | +!! | `visc_type="kin"` | Fluid viscosity type ("kin", "iso") | str(3) | +!! | `dt=1` | Initial time step | real | +!! | `dt_max=dt` | Maximum allowed time step (neg to use `dt`) | real | +!! | `k_db=1` | Magnetic divergence cleaning diffusivity, in diffusivity units | real | +!! | `eta=1E-2` | Plasma resistivity, in diffusivity units | real | !! | `eta_temp=-1` | Base resistivity temperature (neg to disable) | real | !! | `eta_hyper=-1` | Plasma hyper-resistivity, in diffusivity units (neg to disable) | real | -!! | `nu_par=0` | Plasma (parallel) viscosity, in diffusivity units | real | -!! | `nu_perp=0` | Plasma perpendicular viscosity, in diffusivity units | real | +!! | `nu_visc=-1` | Fluid viscosity (`visc_type`), in diffusivity units. Must be set (0 for none). Legacy name `nu_perp` is accepted but deprecated | real | +!! | `nu_par=-1` | Braginskii parallel viscosity, added to `nu_visc`, in diffusivity units (neg to disable) | real | +!! | `nu_gyro=-1` | Scale factor for Braginskii ion gyroviscosity (neg to disable) | real | !! | `d_dens=1` | Plasma diffusivity, in diffusivity units | real | -!! | `d2_dens=-1` | Plasma hyper-diffusivity, in diffusivity units (neg to disable) | real | -!! | `kappa_par=1` | Plasma parallel thermal conduction, in diffusivity units | real | -!! | `kappa_perp=1` | Plasma perpendicular thermal conduction, in diffusivity units | real | +!! | `d2_dens=-1` | Plasma hyper-diffusivity, in diffusivity units (neg to disable) | real | +!! | `kappa_par=1` | Parallel thermal conduction (ion, electron), in diffusivity units or as a factor on Braginskii (`xmhd_brag=T`) | real [2] | +!! | `kappa_perp=1` | Perpendicular thermal conduction (ion, electron), in diffusivity units or as a factor on Braginskii (`xmhd_brag=T`) | real [2] | !! | `nsteps=2000` | Number of time steps to take | int | !! | `rst_freq=10` | Frequency of dump files | int | !! | `lin_tol=1E-9` | Linear solver tolerance | real | !! | `nl_tol=1E-5` | Non-Linear solver tolerance | real | !! | `nu_xmhd=1` | Number of smoother iterations for MG preconditioner | int [nlevels] | -!! | `xmhd_nparts=1` | Number of local partitions for factor based preconditioning | int | -!! | `nclean=500` | Frequency of divergence cleaning | int | +!! | `xmhd_nparts=1` | Total number of partitions (colors) for factor based preconditioning. On distributed meshes this is divided across MPI processes (`xmhd_nparts/nprocs` per process, rounded down), so it must be at least the number of processes, and a result of one partition per process disables coloring | int [nlevels] | +!! | `gmres_maxits=200` | Maximum number of GMRES iterations per linear solve | int | +!! | `gmres_ninner=20` | GMRES restart length (iterations between restarts) | int | +!! | `nclean=500` | Frequency of divergence cleaning (ignored, divergence is controlled by `k_db`) | int | !! | `rst_ind=0` | Index of restart file to initialize from | int | -!! | `maxextrap=2` | Extrapolation order for NL initial guess | int | +!! | `maxextrap=2` | Number of previous steps used to extrapolate the NL initial guess (<2 to disable) | int | !! | `ittarget=60` | Target number of linear iterations per time step | int | !! | `mu_ion=2` | Ion mass in atomic units | real | !! | `me_factor=100` | Electron mass factor | real | !! | `te_factor=1` | Electron temperature factor (single temperature) | real | !! | `xmhd_prefreq=30` | Frequency of full preconditioner update | int | +!! | `xmhd_monitor_div=F` | Monitor divergence error (ignored, the divergence error is always computed) | bool | !! | `xmhd_mfnk=F` | Use Matrix-Free NL solve | bool | +!! | `nl_update=2` | Update the preconditioner when a matrix-free NL solve takes at least this many iterations | int | ! !------------------------------------------------------------------------------ -SUBROUTINE xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget) -real(r8), intent(out) :: dt !< Maximum timestep +SUBROUTINE xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget,nl_update) +real(r8), intent(out) :: dt !< Initial timestep real(r8), intent(out) :: lin_tol !< Linear solver tolerance real(r8), intent(out) :: nl_tol !< Nonlinear solver tolerance integer(i4), intent(out) :: rst_ind !< Index of file for restart (rst_ind>0) @@ -455,13 +449,16 @@ SUBROUTINE xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,m integer(i4), intent(out) :: nclean !< Frequency to clean divergence integer(i4), intent(out) :: maxextrap !< Extrapolation order for initial guess integer(i4), intent(out) :: ittarget !< Maximum number of linear iterations +integer(i4), intent(out) :: nl_update !< Maximum number of linear iterations integer(i4) :: io_unit,ierr +real(r8) :: nu_perp !< Legacy name for `nu_visc` (deprecated) !--- namelist/xmhd_options/xmhd_jcb,xmhd_advec,xmhd_adv_den,xmhd_adv_temp,xmhd_hall,xmhd_ohmic, & - xmhd_visc_heat,xmhd_brag,xmhd_upwind,vbc,nbc,tbc,visc_type,dt,k_db,eta,eta_temp,nu_par, & - nu_perp,d_dens,kappa_par,kappa_perp,nsteps,rst_freq,lin_tol,nl_tol,nu_xmhd,xmhd_nparts, & - nclean,rst_ind,maxextrap,ittarget,mu_ion,me_factor,xmhd_prefreq,xmhd_mfnk,xmhd_diss_centered, & - xmhd_therm_equil,te_factor,d2_dens,eta_hyper,xmhd_lin_euler_start + xmhd_visc_heat,xmhd_brag,xmhd_upwind,xmhd_therm_equil,bbc,vbc,nbc,tbc,visc_type,dt,eta, & + eta_temp,nu_par,nu_visc,nu_perp,nu_gyro,d_dens,kappa_par,kappa_perp,nsteps,rst_freq,lin_tol,nl_tol,nu_xmhd, & + xmhd_nparts,nclean,rst_ind,maxextrap,ittarget,nl_update,mu_ion,me_factor,te_factor,xmhd_prefreq, & + xmhd_monitor_div,xmhd_mfnk,xmhd_diss_centered,eta_hyper,d2_dens,gmres_ninner,gmres_maxits, & + dt_max,xmhd_lin_bdf2,xmhd_lin_euler_start,k_db DEBUG_STACK_PUSH !------------------------------------------------------------------------------ ! Set defaults @@ -474,6 +471,9 @@ SUBROUTINE xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,m nclean=500 maxextrap=2 ittarget=60 +nl_update=2 +dt_max=-1.d0 +nu_perp=-HUGE(nu_perp) !------------------------------------------------------------------------------ ! Read-in Parameters !------------------------------------------------------------------------------ @@ -482,6 +482,12 @@ SUBROUTINE xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,m close(io_unit) if(ierr<0)call oft_abort('No MHD options found in input file.','xmhd_read_settings',__FILE__) if(ierr>0)call oft_abort('Error parsing MHD options in input file.','xmhd_read_settings',__FILE__) +!---Legacy name for fluid viscosity +IF(nu_perp>-HUGE(nu_perp))THEN + CALL oft_warn('"nu_perp" is deprecated, use "nu_visc" instead (value copied to "nu_visc")') + nu_visc=nu_perp +END IF +IF(dt_max<0.d0)dt_max=dt !---Look for xMHD node IF(ASSOCIATED(oft_env%xml))THEN CALL xml_get_element(oft_env%xml,"xmhd",xmhd_root_node,ierr) @@ -495,24 +501,12 @@ SUBROUTINE xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,m !------------------------------------------------------------------------------ m_ion=mu_ion*proton_mass ! Scale by proton mass IF(m_ion<0.d0)CALL oft_abort('Invalid Ion mass','xmhd_read_settings',__FILE__) -!---Set velocity BC flag -IF(vbc(1:3)/='all')xmhd_vbcdir=.TRUE. -SELECT CASE(TRIM(vbc)) - CASE("norm") - vbc_type=2 - CASE("tang") - vbc_type=3 -END SELECT !---Check viscosity type SELECT CASE(visc_type) CASE('kin') visc_itype=1 - nu_perp=nu_par CASE('iso') visc_itype=2 - nu_perp=nu_par - CASE('ani') - visc_itype=3 CASE DEFAULT CALL oft_abort('Unknown viscosity type','xmhd_read_settings',__FILE__) END SELECT @@ -527,19 +521,26 @@ SUBROUTINE xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,m DEBUG_STACK_POP END SUBROUTINE xmhd_read_settings !------------------------------------------------------------------------------ -!> Check upwinding settings before a nonlinear run +!> Check physics settings before a time-dependent run !! -!! Aborts if upwinding is requested for an advected field whose diffusivity is not positive, -!! which the upwinding weight requires. +!! Aborts if the fluid viscosity (`nu_visc`) has not been set, or if upwinding is requested +!! for an advected field whose diffusivity is not positive, which the upwinding weight requires +!! (see \ref doc_mug_xmhd_eqns_upwind). !------------------------------------------------------------------------------ SUBROUTINE xmhd_check_settings() +IF(nu_visc<0.d0)CALL oft_abort('Fluid viscosity "nu_visc" must be set (0 for none)', & + 'xmhd_check_settings',__FILE__) IF(xmhd_upwind)THEN - IF(xmhd_advec.AND.(nu_perp<=0.d0))CALL oft_abort('Upwinding requires a positive fluid viscosity when advection is enabled', & + IF(xmhd_advec.AND.(nu_visc<=0.d0))CALL oft_abort('Upwinding requires "nu_visc>0" when advection is enabled', & 'xmhd_check_settings',__FILE__) IF(xmhd_adv_den.AND.(d_dens<=0.d0))CALL oft_abort('Upwinding requires "d_dens>0" when density is advanced', & 'xmhd_check_settings',__FILE__) - IF(xmhd_adv_temp.AND.(kappa_perp<=0.d0))CALL oft_abort('Upwinding requires "kappa_perp>0" when temperature is advanced', & - 'xmhd_check_settings',__FILE__) + IF(xmhd_adv_temp)THEN + IF(kappa_perp(1)<=0.d0)CALL oft_abort('Upwinding requires "kappa_perp(1)>0" when temperature is advanced', & + 'xmhd_check_settings',__FILE__) + IF(xmhd_two_temp.AND.(kappa_perp(2)<=0.d0))CALL oft_abort( & + 'Upwinding requires "kappa_perp(2)>0" when electron temperature is advanced','xmhd_check_settings',__FILE__) + END IF END IF END SUBROUTINE xmhd_check_settings !------------------------------------------------------------------------------ @@ -548,10 +549,11 @@ END SUBROUTINE xmhd_check_settings !! Runtime options are set in the main input file using the group !! `xmhd_options` group, see \ref xmhd_read_settings. !------------------------------------------------------------------------------ -subroutine xmhd_run(initial_fields,driver,probes) +subroutine xmhd_run(initial_fields,driver,probes,profile_only) TYPE(xmhd_sub_fields), INTENT(inout) :: initial_fields !< Initial conditions CLASS(oft_xmhd_driver), OPTIONAL, INTENT(inout) :: driver !< Forcing object CLASS(oft_xmhd_probe), OPTIONAL, INTENT(inout) :: probes !< Probe object +LOGICAL, OPTIONAL, INTENT(in) :: profile_only !< Profile operator timing and stop? !---Jacobian solver type(oft_nksolver) :: nksolver type(oft_native_gmres_solver), target :: solver @@ -559,10 +561,9 @@ subroutine xmhd_run(initial_fields,driver,probes) integer(i4), allocatable, dimension(:) :: levels !---Local variables class(oft_vector), pointer :: u,v,up -type(xmhd_sub_fields) :: sub_fields type(oft_timer) :: mytimer real(r8), pointer :: vals(:) -integer(i4) :: i,j,k,ierr,io_unit,io_stat,rst_version,rst_tmp,baseit,nredo +integer(i4) :: i,j,k,ierr,io_unit,io_stat,rst_version,rst_tmp,baseit,nredo,nl_update integer(i4) :: itcount(XMHD_ITCACHE) real(r8) :: dt,dthist(XMHD_ITCACHE) real(r8) :: mag,div_err,mer,merp,ver,verp,gerr,cerr,verr,elapsed_time @@ -579,24 +580,22 @@ subroutine xmhd_run(initial_fields,driver,probes) integer(i4) :: hist_i4(3) real(4) :: hist_r4(11) !---Input variables -real(r8) :: lin_tol,nl_tol +real(r8) :: lin_tol,nl_tol,scale_tmp(4) integer(i4) :: rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget DEBUG_STACK_PUSH mg_mesh=>xmhd_ML_lagrange%ml_mesh -IF(.NOT.oft_3D_lagrange_cast(oft_lagrange,xmhd_ML_lagrange%current_level))CALL oft_abort("Invalid lagrange FE object","xmhd_run",__FILE__) +IF(.NOT.oft_3D_lagrange_cast(oft_lagrange,xmhd_ML_lagrange%current_level))CALL oft_abort("Invalid Lagrange FE object","xmhd_run",__FILE__) mesh=>oft_lagrange%mesh +force_refactor=.FALSE. !------------------------------------------------------------------------------ ! Read-in Parameters !------------------------------------------------------------------------------ IF(ASSOCIATED(initial_fields%Te))xmhd_two_temp=.TRUE. -CALL xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget) +CALL xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget,nl_update) CALL xmhd_check_settings IF(den_scale<0.d0)den_scale=SQRT(initial_fields%Ne%dot(initial_fields%Ne)/REAL(initial_fields%Ne%ng,8)) IF((d2_dens>0.d0).AND.(n2_scale<0.d0))n2_scale=den_scale*(REAL(oft_lagrange%order,8)/mesh%hrms)**2 -IF((eta_hyper>0.d0).AND.(j2_scale<0.d0))THEN - j2_scale=(REAL(oft_lagrange%order,8)/mesh%hrms)**2 - IF(.NOT.oft_3D_hcurl_cast(oft_hcurl,xmhd_ML_hcurl%current_level))CALL oft_abort("Invalid HCurl FE object","xmhd_run",__FILE__) -END IF +IF((eta_hyper>0.d0).AND.(j2_scale<0.d0))j2_scale=(REAL(oft_lagrange%order,8)/mesh%hrms)**2 !------------------------------------------------------------------------------ ! Setup ML environment !------------------------------------------------------------------------------ @@ -610,7 +609,7 @@ subroutine xmhd_run(initial_fields,driver,probes) !------------------------------------------------------------------------------ ! Create extrapolation fields !------------------------------------------------------------------------------ -IF(maxextrap>0)THEN +IF(maxextrap>1)THEN ALLOCATE(extrap_fields(maxextrap),extrapt(maxextrap)) DO i=1,maxextrap CALL oft_xmhd_create(extrap_fields(i)%f) @@ -684,22 +683,22 @@ subroutine xmhd_run(initial_fields,driver,probes) !------------------------------------------------------------------------------ call xmhd_alloc_ops jac_dt=dt/2.d0 -ALLOCATE(neg_flag(3,mesh%nc),neg_source(3,mesh%nc)) -neg_flag=0.d0 -neg_source=0.d0 -call xmhd_set_ops(u) !---Force velocity to zero in solid regions IF(xmhd_rw)THEN NULLIFY(vals) DO j=1,3 - CALL initial_fields%V%get_local(vals,j) + CALL u%get_local(vals,j+3) DO i=1,oft_lagrange%ne IF(oft_xmhd_ops%solid_node(i))vals(i)=0.d0 END DO - CALL initial_fields%V%restore_local(vals,j) + CALL u%restore_local(vals,j+3) END DO DEALLOCATE(vals) END IF +ALLOCATE(neg_flag(3,mesh%nc),neg_source(3,mesh%nc)) +neg_flag=0.d0 +neg_source=0.d0 +call xmhd_set_ops(u) !------------------------------------------------------------------------------ ! Compute energies !------------------------------------------------------------------------------ @@ -715,13 +714,14 @@ subroutine xmhd_run(initial_fields,driver,probes) tcurr=oft_xmhd_ops%A%diag_vals(4) npart=oft_xmhd_ops%A%diag_vals(5)/mesh_vol temp_avg=oft_xmhd_ops%A%diag_vals(6)/mesh_vol -derror=oft_xmhd_ops%A%diag_vals(7) -jump_error=-1.d0 IF(xmhd_two_temp)THEN - tempe_avg=oft_xmhd_ops%A%diag_vals(8)/mesh_vol + tempe_avg=oft_xmhd_ops%A%diag_vals(7)/mesh_vol ELSE tempe_avg=-1.d0 END IF +!---Divergence error (no jump error for continuous B) +derror=oft_xmhd_ops%A%diag_vals(8) +jump_error=-1.d0 !------------------------------------------------------------------------------ ! Print run information !------------------------------------------------------------------------------ @@ -734,39 +734,49 @@ subroutine xmhd_run(initial_fields,driver,probes) WRITE(*,'(A)')'Starting non-linear xMHD run' WRITE(*,100)'Lorentz = ',xmhd_jcb WRITE(*,100)'V-Advec = ',xmhd_advec + WRITE(*,100)'B-Adv = ',xmhd_adv_b WRITE(*,100)'N-Adv = ',xmhd_adv_den WRITE(*,100)'T-Adv = ',xmhd_adv_temp IF(xmhd_two_temp)THEN WRITE(*,100)'Two Temp = ',xmhd_two_temp ELSE - WRITE(*,102)'Te/Ti = ',te_factor + WRITE(*,102)'Te/Ti = ',te_factor END IF WRITE(*,100)'Hall = ',xmhd_hall WRITE(*,100)'Ohmic = ',xmhd_ohmic WRITE(*,100)'Visc Heat = ',xmhd_visc_heat WRITE(*,100)'Upwinding = ',xmhd_upwind + WRITE(*,101)'B-BC = ',bbc WRITE(*,101)'V-BC = ',vbc IF(xmhd_adv_den)WRITE(*,101) 'N-BC = ',nbc IF(xmhd_adv_temp)WRITE(*,101)'T-BC = ',tbc WRITE(*,102)'t0 = ',t WRITE(*,102)'dt = ',dt + WRITE(*,102)'k_db = ',k_db WRITE(*,102)'eta0 = ',eta WRITE(*,102)'eta0_Temp = ',eta_temp WRITE(*,101)'visc_type = ',visc_type WRITE(*,102)' nu_par = ',nu_par - WRITE(*,102)' nu_perp = ',nu_perp + WRITE(*,102)' nu_visc = ',nu_visc IF(xmhd_adv_den)WRITE(*,102)'D_dens = ',d_dens IF(xmhd_adv_temp)THEN - WRITE(*,102)'kappa_par = ',kappa_par - WRITE(*,102)'kappa_perp = ',kappa_perp + IF(xmhd_two_temp)THEN + WRITE(*,102)'kappa_par_i = ',kappa_par(1) + WRITE(*,102)'kappa_perp_i = ',kappa_perp(1) + WRITE(*,102)'kappa_par_e = ',kappa_par(2) + WRITE(*,102)'kappa_perp_e = ',kappa_perp(2) + ELSE + WRITE(*,102)'kappa_par = ',kappa_par(1) + WRITE(*,102)'kappa_perp = ',kappa_perp(1) + END IF END IF - IF(temp_floor<0.d0)WRITE(*,102)'temp_floor = ',temp_floor - IF(den_floor<0.d0)WRITE(*,102)'den_floor = ',den_floor + IF(temp_floor>0.d0)WRITE(*,102)'temp_floor = ',temp_floor + IF(den_floor>0.d0)WRITE(*,102)'den_floor = ',den_floor WRITE(*,102)'L-Tol = ',lin_tol WRITE(*,102)'NL-Tol = ',nl_tol WRITE(*,102)'Mag Energy = ',mer WRITE(*,102)'Kin Energy = ',ver - WRITE(*,'(2X,A,ES11.3)')'Div Error = ',derror + WRITE(*,102)'Div Error = ',derror WRITE(*,102)'# B-DOFs = ',REAL(3*oft_lagrange%global%ne,8) WRITE(*,102)'# V-DOFs = ',REAL(3*oft_lagrange%global%ne,8) WRITE(*,102)'# N-DOFs = ',REAL(oft_lagrange%global%ne,8) @@ -774,6 +784,9 @@ subroutine xmhd_run(initial_fields,driver,probes) WRITE(*,'(A)')'============================' WRITE(*,*) END IF +IF(PRESENT(profile_only))THEN + IF(profile_only)CALL xmhd_profile(u) +END IF !------------------------------------------------------------------------------ ! Setup linear solver !------------------------------------------------------------------------------ @@ -785,10 +798,10 @@ subroutine xmhd_run(initial_fields,driver,probes) ELSE solver%A=>oft_xmhd_ops%J END IF -solver%its=200 +solver%its=gmres_maxits solver%atol=lin_tol solver%itplot=1 -solver%nrits=20 +solver%nrits=gmres_ninner solver%pm=oft_env%pm !------------------------------------------------------------------------------ ! Setup Preconditioner @@ -837,7 +850,7 @@ subroutine xmhd_run(initial_fields,driver,probes) verp=ver IF(oft_env%head_proc)CALL mytimer%tick !---Update extrapolation fields - IF(maxextrap>0)THEN + IF(maxextrap>1)THEN DO j=maxextrap,2,-1 CALL extrap_fields(j)%f%add(0.d0,1.d0,extrap_fields(j-1)%f) extrapt(j)=extrapt(j-1) @@ -871,18 +884,18 @@ subroutine xmhd_run(initial_fields,driver,probes) IF(i==2)xmhd_opcount=0 IF(MOD(xmhd_opcount,xmhd_prefreq)==0.OR.ABS(xmhd_opdt-dt)/dt>.15d0)xmhd_opcount=0 !---Update preconditioning matrices if necessary - IF(xmhd_mfnk.AND.xmhd_opcount/=0.AND.(nksolver%lits>baseit+1.OR.nksolver%nlits>1))THEN - IF(oft_env%head_proc)WRITE(*,*)'Force: update Jacobian',i,nksolver%lits,baseit,nredo - IF(nredo<1)THEN - jac_dt=dt/2.d0 - xmhd_skip_update=.FALSE. - CALL xmhd_set_ops(u) - xmhd_skip_update=.TRUE. - baseit=-1 - nredo=nredo+1 - ELSE - xmhd_opcount=0 - END IF + IF(xmhd_mfnk.AND.xmhd_opcount/=0.AND.(nksolver%lits>baseit+1.OR.nksolver%nlits>=nl_update))THEN + IF(oft_env%head_proc)WRITE(*,*)'Update Jacobian',nksolver%lits,baseit,nredo + IF(nredo<1)THEN + jac_dt=dt/2.d0 + xmhd_skip_update=.FALSE. + CALL xmhd_set_ops(u) + xmhd_skip_update=.TRUE. + baseit=-1 + nredo=nredo+1 + ELSE + xmhd_opcount=0 + END IF END IF IF(xmhd_opcount==0)THEN jac_dt=dt/2.d0 @@ -912,10 +925,10 @@ subroutine xmhd_run(initial_fields,driver,probes) tcurr=oft_xmhd_ops%A%diag_vals(4) npart=oft_xmhd_ops%A%diag_vals(5)/mesh_vol temp_avg=oft_xmhd_ops%A%diag_vals(6)/mesh_vol - derror=oft_xmhd_ops%A%diag_vals(7) - IF(xmhd_two_temp)tempe_avg=oft_xmhd_ops%A%diag_vals(8)/mesh_vol + IF(xmhd_two_temp)tempe_avg=oft_xmhd_ops%A%diag_vals(7)/mesh_vol + derror=oft_xmhd_ops%A%diag_vals(8) !---Extrapolate solution - IF(maxextrap>0)CALL vector_extrapolate(extrapt,extrap_fields,nextrap,t+dt,u) + IF(maxextrap>1)CALL vector_extrapolate(extrapt,extrap_fields,nextrap,t+dt,u) !---Apply driver BCs IF(PRESENT(driver))THEN CALL driver%apply(up,u,v,t,dt) @@ -959,7 +972,7 @@ subroutine xmhd_run(initial_fields,driver,probes) hist_i4=(/rst_ind+i-1,nksolver%lits,nksolver%nlits/) hist_r4=REAL([t,tflux,tcurr,mer,jump_error,derror,ver,npart,temp_avg,tempe_avg,elapsed_time],4) 103 FORMAT(' Timestep',I8,ES14.6,2X,I4,2X,I4,F12.3,ES12.2) - WRITE(*,103)rst_ind+i,t,nksolver%lits,nksolver%nlits,elapsed_time,dt + WRITE(*,103)rst_ind+i,t+dt,nksolver%lits,nksolver%nlits,elapsed_time,dt IF(oft_debug_print(1))WRITE(*,*) CALL hist_file%write(data_i4=hist_i4, data_r4=hist_r4) END IF @@ -978,8 +991,8 @@ subroutine xmhd_run(initial_fields,driver,probes) dthist(MOD(i,XMHD_ITCACHE)+1)=dt t=t+dt dt=ittarget*SUM(dthist)/SUM(itcount) - IF(dt>dtin)dt=dtin - IF(dt<1.d-10)CALL oft_abort('Time step dropped too low!','xmhd_run',__FILE__) + IF(dt>dt_max)dt=dt_max + IF(dt<1.d-12)CALL oft_abort('Time step dropped too low!','xmhd_run',__FILE__) !------------------------------------------------------------------------------ ! Write dump file !------------------------------------------------------------------------------ @@ -1012,7 +1025,7 @@ subroutine xmhd_run(initial_fields,driver,probes) CALL v%delete DEALLOCATE(u,up,v) !--- extrapolation fields -IF(maxextrap>0)THEN +IF(maxextrap>1)THEN DO i=1,maxextrap CALL extrap_fields(i)%f%delete DEALLOCATE(extrap_fields(i)%f) @@ -1025,7 +1038,7 @@ end subroutine xmhd_run !> Main driver subroutine for extended MHD time advance !! !! Runtime options are set in the main input file using the group `xmhd_options` group, -!! see \ref xmhd_read_settings. +!! see \ref xmhd_run. !! !! @note This method assumes that [B0, V0, Ne0, Ti0, Te0] constitute an !! equilibrium. @@ -1039,16 +1052,15 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) integer(i4) :: nlevels integer(i4), allocatable, dimension(:) :: levels !---Local variables -class(oft_vector), pointer :: du,u0,v,un1,un2 -type(xmhd_sub_fields) :: sub_fields +class(oft_vector), pointer :: du,u0,v,un1,un2,dutmp type(oft_xmhd_massmatrix) :: mop type(oft_timer) :: mytimer -integer(i4) :: i,j,ierr,io_unit,io_stat,rst_tmp +integer(i4) :: i,j,ierr,io_unit,io_stat,rst_tmp,nl_update integer(i4) :: itcount(XMHD_ITCACHE) real(r8) :: dt,dthist(XMHD_ITCACHE) real(r8) :: mag,div_err,mer,merp,ver,verp,gerr,cerr,verr,elapsed_time real(r8) :: fac,lramp,tflux,tcurr,t,dtin,div_error,jump_error,derror,de_scale -real(r8) :: ndens,npart,temp_avg,tempe_avg,mesh_vol,dtp +real(r8) :: ndens,npart,temp_avg,tempe_avg,mesh_vol real(r8), pointer, dimension(:) :: vals => NULL() character(LEN=XMHD_RST_LEN) :: rst_char character(LEN=OFT_SLEN) :: comm_line @@ -1064,9 +1076,10 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) !---Input variables real(r8) :: lin_tol,nl_tol integer(i4) :: rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget +logical :: use_bdf2 DEBUG_STACK_PUSH +IF(.NOT.oft_3D_lagrange_cast(oft_lagrange,xmhd_ML_lagrange%current_level))CALL oft_abort("Invalid Lagrange FE object","xmhd_lin_run",__FILE__) mg_mesh=>xmhd_ML_lagrange%ml_mesh -IF(.NOT.oft_3D_lagrange_cast(oft_lagrange,xmhd_ML_lagrange%current_level))CALL oft_abort("Invalid lagrange FE object","xmhd_run",__FILE__) mesh=>oft_lagrange%mesh !------------------------------------------------------------------------------ ! Read-in Parameters @@ -1074,7 +1087,7 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) IF(ASSOCIATED(equil_fields%Te).AND.ASSOCIATED(pert_fields%Te))xmhd_two_temp=.TRUE. IF(ASSOCIATED(equil_fields%Te).NEQV.ASSOCIATED(pert_fields%Te))CALL oft_abort( & "Te0 and dTe ICs are required for two temp.", "xmhd_lin_run", __FILE__) -CALL xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget) +CALL xmhd_read_settings(dt,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget,nl_update) IF(den_scale<0.d0)den_scale=SQRT(equil_fields%Ne%dot(equil_fields%Ne)/REAL(equil_fields%Ne%ng,8)) IF((d2_dens>0.d0).AND.(n2_scale<0.d0))n2_scale=den_scale*(REAL(oft_lagrange%order,8)/mesh%hrms)**2 IF((eta_hyper>0.d0).AND.(j2_scale<0.d0))j2_scale=(REAL(oft_lagrange%order,8)/mesh%hrms)**2 @@ -1083,11 +1096,17 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) xmhd_visc_heat=.FALSE. xmhd_brag=.FALSE. xmhd_linear=.TRUE. +xmhd_upwind=.FALSE. xmhd_diss_centered=.TRUE. +use_bdf2=xmhd_lin_bdf2 +CALL xmhd_check_settings !------------------------------------------------------------------------------ ! Setup ML environment !------------------------------------------------------------------------------ CALL xmhd_setup_rep +nlevels=xmhd_nlevels-xmhd_minlev+1 +IF(nlevels>1)xmhd_ml_vecspace%ML_FE_rep=>ML_xmhd_rep +CALL xmhd_alloc_ops !------------------------------------------------------------------------------ ! Create solver fields !------------------------------------------------------------------------------ @@ -1099,7 +1118,7 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) !------------------------------------------------------------------------------ ! Create extrapolation fields !------------------------------------------------------------------------------ -IF(maxextrap>0)THEN +IF(maxextrap>1)THEN ALLOCATE(extrap_fields(maxextrap),extrapt(maxextrap)) DO i=1,maxextrap CALL oft_xmhd_create(extrap_fields(i)%f) @@ -1129,7 +1148,7 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) CALL hist_file%add_field('ven', 'r4', desc="Kinetic energy [J]") CALL hist_file%add_field('ne', 'r4', desc="Average density [m^-3]") CALL hist_file%add_field('ti', 'r4', desc="Average ion temperature [eV]") - CALL hist_file%add_field('te', 'r4', desc="Average ion temperature [eV]") + CALL hist_file%add_field('te', 'r4', desc="Average electron temperature [eV]") CALL hist_file%add_field('stime','r4', desc="Walltime [s]") END IF !------------------------------------------------------------------------------ @@ -1150,15 +1169,28 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) dt=dtin ELSE IF(rst_ind>0)CALL oft_abort('Restart does not exist.','xmhd_run',__FILE__) + !---Look for restart at one level down + WRITE(rst_char,105)xmhd_nlevels-1 + rst=oft_file_exist(TRIM('xMHD_l'//rst_char//'.rst')) + IF(rst.AND.(xmhd_nlevels-1>=xmhd_minlev))THEN + CALL xmhd_set_level(xmhd_level-1) + NULLIFY(dutmp) + CALL oft_xmhd_create(dutmp) + CALL oft_xmhd_rst_load(dutmp, 'xMHD_l'//rst_char//'.rst', 'U', t, dt) + dt=dtin + CALL oft_xmhd_interp(xmhd_ml_vecspace,dutmp,du) + CALL dutmp%delete() + DEALLOCATE(dutmp) + ELSE + CALL pert_fields%V%scale(1.d0/vel_scale) + CALL pert_fields%Ne%scale(1.d0/den_scale) + CALL oft_xmhd_push(pert_fields,du) + END IF CALL equil_fields%V%scale(1.d0/vel_scale) CALL equil_fields%Ne%scale(1.d0/den_scale) CALL oft_xmhd_push(equil_fields,u0) CALL equil_fields%V%scale(vel_scale) CALL equil_fields%Ne%scale(den_scale) - !--- - CALL pert_fields%V%scale(1.d0/vel_scale) - CALL pert_fields%Ne%scale(1.d0/den_scale) - CALL oft_xmhd_push(pert_fields,du) t=0.d0 !---Create initial conditions restart file WRITE(rst_char,105)0 @@ -1171,27 +1203,44 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) !------------------------------------------------------------------------------ ! Setup Operators !------------------------------------------------------------------------------ -call xmhd_alloc_ops -!---Optionally start BDF2 with a backward Euler step (operators switched to BDF2 at step 2) -IF(xmhd_lin_euler_start)THEN - jac_dt=dt -ELSE - jac_dt=dt*2.d0/3.d0 -END IF -call xmhd_set_ops(u0) -mop%u0=>u0 -!---Force perturbation to zero in solid regions +!---Force velocity perturbation to zero in solid regions IF(xmhd_rw)THEN NULLIFY(vals) DO j=1,3 - CALL pert_fields%V%get_local(vals,j) + CALL du%get_local(vals,j+3) DO i=1,oft_lagrange%ne IF(oft_xmhd_ops%solid_node(i))vals(i)=0.d0 END DO - CALL pert_fields%V%restore_local(vals,j) + CALL du%restore_local(vals,j+3) + END DO + DEALLOCATE(vals) +END IF +!---Zero the perturbed velocity and magnetic field components normal to the axis (y,z) on the +! axis of revolved meshes (see xmhd_set_bc) +IF(mesh%periodic%revolved)THEN + NULLIFY(vals) + DO j=2,3 + CALL du%get_local(vals,j+3) + DO i=1,oft_lagrange%ne + IF(oft_lagrange%per_vmap(i)==3)vals(i)=0.d0 + END DO + CALL du%restore_local(vals,j+3) + CALL du%get_local(vals,j) + DO i=1,oft_lagrange%ne + IF(oft_lagrange%per_vmap(i)==3)vals(i)=0.d0 + END DO + CALL du%restore_local(vals,j) END DO DEALLOCATE(vals) +ENDIF +!---Optionally start BDF2 with a backward Euler step (operators switched to BDF2 at step 2) +IF(use_bdf2.AND.(.NOT.xmhd_lin_euler_start))THEN + jac_dt=dt*2.d0/3.d0 +ELSE + jac_dt=dt END IF +call xmhd_set_ops(u0) +mop%u0=>u0 !------------------------------------------------------------------------------ ! Compute energies !------------------------------------------------------------------------------ @@ -1205,8 +1254,8 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) tcurr=mop%diag_vals(4) npart=mop%diag_vals(5)/mesh_vol temp_avg=mop%diag_vals(6)/mesh_vol -derror=mop%diag_vals(7) -tempe_avg=mop%diag_vals(8)/mesh_vol +tempe_avg=mop%diag_vals(7)/mesh_vol +derror=mop%diag_vals(8) jump_error=-1.d0 !------------------------------------------------------------------------------ ! Print run information @@ -1225,26 +1274,35 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) WRITE(*,100)'Hall = ',xmhd_hall WRITE(*,100)'Ohmic = ',xmhd_ohmic WRITE(*,100)'Visc Heat = ',xmhd_visc_heat + WRITE(*,101)'B-BC = ',bbc WRITE(*,101)'V-BC = ',vbc IF(xmhd_adv_den)WRITE(*,101)'N-BC = ',nbc IF(xmhd_adv_temp)WRITE(*,101)'T-BC = ',tbc WRITE(*,102)'t0 = ',t WRITE(*,102)'dt = ',dt + WRITE(*,102)'k_db = ',k_db WRITE(*,102)'eta0 = ',eta WRITE(*,102)'eta0_Temp = ',eta_temp WRITE(*,101)'visc_type = ',visc_type WRITE(*,102)' nu_par = ',nu_par - WRITE(*,102)' nu_perp = ',nu_perp + WRITE(*,102)' nu_visc = ',nu_visc IF(xmhd_adv_den)WRITE(*,102)'D_dens = ',d_dens IF(xmhd_adv_temp)THEN - WRITE(*,102)'kappa_par = ',kappa_par - WRITE(*,102)'kappa_perp = ',kappa_perp + IF(xmhd_two_temp)THEN + WRITE(*,102)'kappa_par_i = ',kappa_par(1) + WRITE(*,102)'kappa_perp_i = ',kappa_perp(1) + WRITE(*,102)'kappa_par_e = ',kappa_par(2) + WRITE(*,102)'kappa_perp_e = ',kappa_perp(2) + ELSE + WRITE(*,102)'kappa_par = ',kappa_par(1) + WRITE(*,102)'kappa_perp = ',kappa_perp(1) + END IF END IF WRITE(*,102)'L-Tol = ',lin_tol WRITE(*,102)'NL-Tol = ',nl_tol WRITE(*,102)'Mag Energy = ',mer WRITE(*,102)'Kin Energy = ',ver - WRITE(*,'(2X,A,ES11.3)')'Div Error = ',derror + WRITE(*,102)'Div Error = ',derror WRITE(*,102)'# B-DOFs = ',REAL(3*oft_lagrange%global%ne,4) WRITE(*,102)'# V-DOFs = ',REAL(3*oft_lagrange%global%ne,4) WRITE(*,102)'# N-DOFs = ',REAL(oft_lagrange%global%ne,4) @@ -1256,10 +1314,10 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) ! Setup linear solver !------------------------------------------------------------------------------ solver%A=>oft_xmhd_ops%J -solver%its=400 +solver%its=gmres_maxits solver%atol=lin_tol solver%itplot=1 -solver%nrits=40 +solver%nrits=gmres_ninner solver%pm=oft_env%pm !------------------------------------------------------------------------------ ! Setup Preconditioner @@ -1285,14 +1343,20 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) !------------------------------------------------------------------------------ ! Begin time stepping !------------------------------------------------------------------------------ -!---Without the Euler start-up step, use the initial condition for both BDF2 history levels -IF(.NOT.xmhd_lin_euler_start)CALL un1%add(0.d0,1.d0,du) +IF(PRESENT(escale))THEN + de_scale=SQRT(escale/(ver+mer/mu0)) + CALL du%scale(de_scale) +END IF +IF(use_bdf2)THEN + CALL un2%add(0.d0,1.d0,du) + CALL un1%add(0.d0,1.d0,du) +END IF DO i=1,nsteps merp=mer verp=ver IF(oft_env%head_proc)CALL mytimer%tick !---Update extrapolation fields - IF(maxextrap>0)THEN + IF(maxextrap>1)THEN DO j=maxextrap,2,-1 CALL extrap_fields(j)%f%add(0.d0,1.d0,extrap_fields(j-1)%f) extrapt(j)=extrapt(j-1) @@ -1305,7 +1369,7 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) ! Perform Linear Advance !------------------------------------------------------------------------------ !---Switch from the backward Euler start-up step to BDF2 (requires a preconditioner update) - IF(xmhd_lin_euler_start.AND.(i==2))THEN + IF(use_bdf2.AND.xmhd_lin_euler_start.AND.(i==2))THEN jac_dt=dt*2.d0/3.d0 CALL xmhd_set_ops(u0) CALL xmhd_pre%update(.TRUE.) @@ -1313,7 +1377,7 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) !---Compute metric to get RHS CALL un2%add(0.d0,1.d0,un1) CALL un1%add(0.d0,1.d0,du) - IF((i>1).OR.(.NOT.xmhd_lin_euler_start))THEN + IF(use_bdf2.AND.((i>1).OR.(.NOT.xmhd_lin_euler_start)))THEN CALL un2%add(-1.d0,4.d0,un1) CALL un2%scale(1.d0/3.d0) CALL mop%apply(un2,v) @@ -1321,7 +1385,6 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) ELSE CALL mop%apply(du,v) END IF - ! CALL mop%apply(du,v) !---Get diagnostics mer=mop%diag_vals(1)/2.d0 ver=mop%diag_vals(2)*m_ion/2.d0 @@ -1329,9 +1392,9 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) tcurr=mop%diag_vals(4) npart=mop%diag_vals(5)/mesh_vol temp_avg=mop%diag_vals(6)/mesh_vol - derror=mop%diag_vals(7) - tempe_avg=mop%diag_vals(8)/mesh_vol - IF(maxextrap>0)CALL vector_extrapolate(extrapt,extrap_fields,nextrap,t+dt,du) + tempe_avg=mop%diag_vals(7)/mesh_vol + derror=mop%diag_vals(8) + IF(maxextrap>1)CALL vector_extrapolate(extrapt,extrap_fields,nextrap,t+dt,du) !------------------------------------------------------------------------------ ! Scale fields !------------------------------------------------------------------------------ @@ -1353,6 +1416,7 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) IF(oft_debug_print(1))WRITE(*,*) CALL hist_file%write(data_i4=hist_i4, data_r4=hist_r4) END IF + IF(solver%cits<0)CALL oft_abort("Solver did not converge", "xmhd_lin_run", __FILE__) !---Update current time t=t+dt !------------------------------------------------------------------------------ @@ -1387,7 +1451,7 @@ subroutine xmhd_lin_run(equil_fields,pert_fields,escale) CALL un2%delete DEALLOCATE(un1,un2) !--- extrapolation fields -IF(maxextrap>0)THEN +IF(maxextrap>1)THEN DO i=1,maxextrap CALL extrap_fields(i)%f%delete DEALLOCATE(extrap_fields(i)%f) @@ -1400,12 +1464,14 @@ end subroutine xmhd_lin_run !> Evaluate upwinding weight !! !! Weight for the inconsistent streamline-upwind Petrov-Galerkin (SUPG) method, where the -!! streamline modification of the test functions is applied to the advection terms only. The -!! weight is the optimal value for 1D advection-diffusion, +!! streamline modification of the test functions is applied to the advection terms only (see +!! \ref doc_mug_xmhd_eqns_upwind). The weight is the optimal value for 1D advection-diffusion, !! \f$ w = \beta h_e / (2 |v|) \f$ with \f$ \beta = \coth(Pe) - 1/Pe \f$ and \f$ Pe = |v| h_e / (2 \nu) \f$, !! from A. N. Brooks and T. J. R. Hughes, Comput. Methods Appl. Mech. Eng. 32, 199 (1982). !! -!! Where the local diffusivity is not positive full upwinding (\f$ \beta = 1 \f$) is used. +!! Where the local diffusivity is not positive (eg. zeroed by a shaping function) full upwinding +!! (\f$ \beta = 1 \f$) is used. Uniform zero diffusivities are rejected when upwinding is enabled +!! (see \ref xmhd_lag::xmhd_check_settings "xmhd_check_settings"). !! !! @result Upwinding weight [s] !------------------------------------------------------------------------------ @@ -1445,18 +1511,20 @@ END FUNCTION xmhd_elem_size !------------------------------------------------------------------------------ subroutine xmhd_build_ops(fullin) class(fem_interp), intent(inout) :: fullin !< Interpolator to evaluate fields -type(oft_local_mat), allocatable, dimension(:,:) :: mtmp +type(oft_local_mat), allocatable, dimension(:,:) :: mtmp,mtmp_solid type(oft_1d_int), allocatable, dimension(:) :: iloc -integer(i4) :: ii,i,ip,j,m,n,jr,jc,jp,nrows,jtmp(1),li,lj +integer(i4) :: ii,i,ip,j,m,n,jr,jc,jp,nrows,jtmp(1),vi,vj,vl,vm,vk,vlc,li,lj integer(i4), allocatable, target :: j_lag(:),j_hcurl(:) +integer(i4), parameter :: lev_cevita(3,6)=RESHAPE([1,2,3, 1,3,2, 2,3,1, 2,1,3, 3,1,2, 3,2,1],[3,6]) integer(KIND=omp_lock_kind), allocatable, dimension(:) :: tlocks -real(r8) :: f,diff,chi_temp(2),bmag,det,uvec(3),umat(3,3),nu_tmp(2),ten1(3,3) -real(r8), allocatable, target :: lag_rop(:),lag_gop(:,:),hcurl_rop(:,:),hcurl_cop(:,:) +real(r8) :: f,diff,chi_temp(4),bmag,det,uvec(3),umat(3,3),nu_tmp(2),ten1(3,3) +real(r8), allocatable, target :: lag_rop(:),lag_gop(:,:) +real(r8), allocatable, target :: hcurl_rop(:,:),hcurl_cop(:,:) real(r8), allocatable :: lag_gopt(:,:),b_rop(:,:,:),b_cop(:,:,:) class(oft_vector), pointer :: tmp real(r8) :: goptmp(3,4),vol,b(1,1),eta_curr,u,c1,c2,c3,c4,divv,tgam_fac,fulltmp(41) -real(r8) :: bhat(3),dVT(3,3),cgop(3,6) -real(r8) :: he,v0_mag,uten(3,3,3) +real(r8) :: bhat(3),dvT(3,3),pt(3),diff_tmp,c_gyro,krok_delta +real(r8) :: he,v0_mag,cgop(3,6),uten(3,3,3) logical :: curved class(oft_matrix), pointer :: Jac type(oft_quad_type), pointer :: quad @@ -1465,7 +1533,7 @@ subroutine xmhd_build_ops(fullin) IF(oft_debug_print(2))write(*,'(4X,A,I4)')'Building xMHD Jacobian: ',xmhd_level !--- Jac=>oft_xmhd_ops%J -quad=>oft_lagrange%quad +quad=>xmhd_ML_lagrange%levels(xmhd_ML_lagrange%nlevels)%fe%quad tgam_fac=temp_gamma - 1.d0 !------------------------------------------------------------------------------ ! Reset matrices @@ -1479,17 +1547,22 @@ subroutine xmhd_build_ops(fullin) !------------------------------------------------------------------------------ ! Construct matrix !------------------------------------------------------------------------------ -!$omp parallel private(det,j_lag,lag_rop,lag_gop,b_rop,b_cop,li,lj, & -!$omp curved,goptmp,vol,j,m,jr,jc,bhat,he,v0_mag,u0,dVT,eta_curr,u, & -!$omp uvec,umat,mtmp,chi_temp,bmag,c1,c2,c3,c4,divv,nu_tmp,ten1,cgop, & -!$omp fulltmp,lag_gopt,uten,i,ii,iloc,j_hcurl,hcurl_rop,hcurl_cop) +!$omp parallel private(det,j_lag,j_hcurl,lag_rop,lag_gop,hcurl_rop,b_rop,b_cop, & +!$omp hcurl_cop,curved,goptmp,vol,j,m,jr,jc,bhat,he,v0_mag,dvT,eta_curr,u,uvec,umat, & +!$omp mtmp,mtmp_solid,chi_temp,bmag,c1,c2,c3,c4,divv,nu_tmp,ten1,cgop,fulltmp,lag_gopt,pt,diff_tmp, & +!$omp uten,i,ii,iloc,u0,c_gyro,krok_delta,vi,vj,vl,vm,vk,vlc,li,lj) !---Thread local arrays allocate(j_lag(oft_lagrange%nce)) allocate(lag_rop(oft_lagrange%nce),lag_gop(3,oft_lagrange%nce)) allocate(lag_gopt(oft_lagrange%nce,3)) allocate(b_rop(oft_lagrange%nce,3,3),b_cop(oft_lagrange%nce,3,3)) -allocate(mtmp(xmhd_rep%nfields,xmhd_rep%nfields)) +allocate(mtmp(xmhd_rep%nfields,xmhd_rep%nfields),mtmp_solid(xmhd_rep%nfields,xmhd_rep%nfields)) CALL xmhd_rep%mat_setup_local(mtmp, xmhd_mat_mask) +DO li=1,3 + DO lj=1,3 + mtmp_solid(li,lj)%m=>mtmp(li,lj)%m + END DO +END DO allocate(iloc(xmhd_rep%nfields)) DO i=1,8 iloc(i)%v=>j_lag @@ -1497,23 +1570,19 @@ subroutine xmhd_build_ops(fullin) IF(xmhd_two_temp)iloc(te_ind)%v=>j_lag IF(n2_ind>0)iloc(n2_ind)%v=>j_lag IF(j2_ind>0)THEN - ALLOCATE(hcurl_rop(3,oft_hcurl%nce),hcurl_cop(3,oft_hcurl%nce)) - ALLOCATE(j_hcurl(oft_hcurl%nce)) + allocate(j_hcurl(oft_hcurl%nce)) + allocate(hcurl_rop(3,oft_hcurl%nce),hcurl_cop(3,oft_hcurl%nce)) iloc(j2_ind)%v=>j_hcurl - !---Setup caching - IF(xmhd_level==xmhd_nlevels)THEN - xmhd_hcurl_rop=>hcurl_rop - xmhd_hcurl_cop=>hcurl_cop - END IF END IF !---Setup caching IF(xmhd_level==xmhd_nlevels)THEN xmhd_lag_rop=>lag_rop xmhd_lag_gop=>lag_gop + IF(j2_ind>0)THEN + xmhd_hcurl_rop=>hcurl_rop + xmhd_hcurl_cop=>hcurl_cop + END IF END IF -!--- Define viscosity temporary varible (currently static) -nu_tmp(1) = nu_par -nu_tmp(2) = nu_perp !------------------------------------------------------------------------------ ! Integration loop !------------------------------------------------------------------------------ @@ -1531,25 +1600,25 @@ subroutine xmhd_build_ops(fullin) !------------------------------------------------------------------------------ ! Quadrature Loop !------------------------------------------------------------------------------ - IF(xmhd_hall.AND.xmhd_two_temp)THEN - mtmp(1,8)%m=>mtmp(1,te_ind)%m ! Point hall-term coupling to electrons - mtmp(2,8)%m=>mtmp(2,te_ind)%m - mtmp(3,8)%m=>mtmp(3,te_ind)%m + IF(xmhd_hall.AND.xmhd_two_temp)THEN ! Point hall-term coupling to electrons + DO li=1,3 + mtmp(li,8)%m=>mtmp(li,te_ind)%m + END DO END IF DO m=1,quad%np !------------------------------------------------------------------------------ ! Get local reconstructed operators !------------------------------------------------------------------------------ - IF(curved.OR.(m==1))THEN + if(curved.OR.m==1)then call mesh%jacobian(i,quad%pts(:,m),goptmp,vol) IF(j2_ind>0)call oft_hcurl_get_cgops(goptmp,cgop) - END IF + end if det=vol*quad%wts(m) !---Lagrange basis functions CALL oft_lag_eval_all(oft_lagrange,i,quad%pts(:,m),lag_rop) CALL oft_lag_geval_all(oft_lagrange,i,quad%pts(:,m),lag_gop,goptmp) lag_gopt=TRANSPOSE(lag_gop) - !---Setup rop/cop + !---Vector Lagrange value and curl operators, b_rop(:,:,k) and b_cop(:,:,k) for component k b_rop=0.d0; b_cop=0.d0 !$omp simd DO jr=1,oft_lagrange%nce @@ -1565,12 +1634,13 @@ subroutine xmhd_build_ops(fullin) !------------------------------------------------------------------------------ ! Reconstruct fields !------------------------------------------------------------------------------ + fulltmp=0.d0 call fullin%interp(i,quad%pts(:,m),goptmp,fulltmp) CALL xmhd_interp_unpack(fulltmp,u0) v0_mag = magnitude(u0%V) dvT = TRANSPOSE(u0%dV) divv = u0%dV(1,1) + u0%dV(2,2) + u0%dV(3,3) - bmag = SQRT(SUM(u0%B**2)) + xmhd_eps + bmag = SQRT(SUM(u0%B**2)) + xmhd_B_eps bhat = u0%B/bmag !---Handle density and temperature floors IF(temp_floor>0.d0)u0%Ti=MAX(temp_floor,u0%Ti) @@ -1585,23 +1655,40 @@ subroutine xmhd_build_ops(fullin) eta_curr=eta*(eta_temp/u0%Te)**(3.d0/2.d0) END IF END IF + pt=mesh%log2phys(i,quad%pts(:,m)) + IF(ASSOCIATED(res_shaping))eta_curr = eta_curr*res_shaping(pt) + !---Viscous diffusion + nu_tmp(1) = nu_par + nu_tmp(2) = nu_visc + IF(ASSOCIATED(visc_shaping))nu_tmp = nu_tmp*visc_shaping(pt) + !---Density diffusion + diff_tmp=d_dens + IF(ASSOCIATED(diff_shaping))diff_tmp = diff_tmp*diff_shaping(pt) + !---Thermal conduction IF(xmhd_brag)THEN IF(xmhd_two_temp)THEN - chi_temp=brag_ion_transport(u0%N,u0%Ti,mu_ion,bmag)/u0%N + chi_temp(1:2) = brag_ion_transport(u0%N,u0%Ti,mu_ion,bmag)/u0%N + chi_temp(3:4) = brag_elec_transport(u0%N,u0%Te,bmag)/u0%N + chi_temp(3) = kappa_par(2)*chi_temp(3) + chi_temp(4) = MAX(chi_perp_e_min, kappa_perp(2)*chi_temp(4)) ELSE - chi_temp=brag_comb_transport(u0%N,u0%Ti,mu_ion,bmag)/u0%N + chi_temp(1:2)=brag_comb_transport(u0%N,u0%Ti,mu_ion,bmag)/u0%N END IF - chi_temp(1)=kappa_par*chi_temp(1) - chi_temp(2)=kappa_perp*chi_temp(2) + chi_temp(1)=kappa_par(1)*chi_temp(1) + chi_temp(2)=kappa_perp(1)*chi_temp(2) ELSE - chi_temp(1)=kappa_par*den_scale/u0%N - chi_temp(2)=kappa_perp*den_scale/u0%N + chi_temp(1)=kappa_par(1)*den_scale/u0%N + chi_temp(2)=kappa_perp(1)*den_scale/u0%N + chi_temp(3)=kappa_par(2)*den_scale/u0%N + chi_temp(4)=kappa_perp(2)*den_scale/u0%N END IF + IF(ASSOCIATED(cond_shaping))chi_temp = chi_temp*cond_shaping(pt) !------------------------------------------------------------------------------ - ! Handle solid region + ! Handle solid region (resistive diffusion of B with divergence cleaning) !------------------------------------------------------------------------------ - IF(solid_cell(i))THEN + IF(reg_solid(mesh%reg(i)))THEN eta_curr=eta_reg(mesh%reg(i)) + IF(ASSOCIATED(res_shaping))eta_curr = eta_curr*res_shaping(pt) c1 = eta_curr*jac_dt*det do li=1,3 do jc=1,oft_lagrange%nce @@ -1624,6 +1711,9 @@ subroutine xmhd_build_ops(fullin) CYCLE END IF !------------------------------------------------------------------------------ +! Compute dB/dt matrix +!------------------------------------------------------------------------------ +!------------------------------------------------------------------------------ ! Compute dB/dt(B) matrix (1:3,1:3) !------------------------------------------------------------------------------ IF(xmhd_adv_b)THEN @@ -1654,10 +1744,41 @@ subroutine xmhd_build_ops(fullin) end do end do end do + !---Hyper resistivity (rows/columns scaled by j2_scale below) + IF(j2_ind>0)THEN + c1 = -eta_hyper*jac_dt*det + IF(.NOT.xmhd_diss_centered)c1 = 2.d0*c1 + do jc=1,oft_hcurl%nce + uvec = hcurl_cop(:,jc)*c1 + do lj=1,3 + !$omp simd + do jr=1,oft_lagrange%nce + mtmp(lj,j2_ind)%m(jr,jc) = mtmp(lj,j2_ind)%m(jr,jc) & + + b_cop(jr,1,lj)*uvec(1) + b_cop(jr,2,lj)*uvec(2) + b_cop(jr,3,lj)*uvec(3) + end do + end do + uvec = hcurl_rop(:,jc)*det + !$omp simd + do jr=1,oft_hcurl%nce + mtmp(j2_ind,j2_ind)%m(jr,jc) = mtmp(j2_ind,j2_ind)%m(jr,jc) & + + DOT_PRODUCT(hcurl_rop(:,jr),uvec) + end do + end do + do li=1,3 + do jc=1,oft_lagrange%nce + uvec = b_cop(jc,:,li)*det + !$omp simd + do jr=1,oft_hcurl%nce + mtmp(j2_ind,li)%m(jr,jc) = mtmp(j2_ind,li)%m(jr,jc) & + + DOT_PRODUCT(hcurl_cop(:,jr),uvec) + end do + end do + end do + END IF !------------------------------------------------------------------------------ ! Compute dB/dt(V) matrix (1:3,4:6) !------------------------------------------------------------------------------ - c1 = jac_dt*det*vel_scale + c1 = jac_dt*det do jc=1,oft_lagrange%nce uvec = u0%B*lag_rop(jc)*c1 do lj=1,3 @@ -1676,10 +1797,10 @@ subroutine xmhd_build_ops(fullin) ! Compute dB/dt(n) matrix (1:3,7) !------------------------------------------------------------------------------ IF(xmhd_hall.AND.xmhd_adv_den)THEN - c1 = -jac_dt*det*den_scale/(mu0*elec_charge*u0%N*u0%N) - c2 = -k_boltz*u0%Te*jac_dt*det*den_scale/(elec_charge*u0%N) + c1 = -jac_dt*det/(mu0*elec_charge*u0%N*u0%N) + c2 = -k_boltz*u0%Te*jac_dt*det/(elec_charge*u0%N) c3 = c2/u0%N - IF(xmhd_linear)umat(:,1)=k_boltz*u0%dTe*jac_dt*det*den_scale/(elec_charge*u0%N) + IF(xmhd_linear)umat(:,1)=k_boltz*u0%dTe*jac_dt*det/(elec_charge*u0%N) do jc=1,oft_lagrange%nce uvec = (cross_product(u0%J,u0%B)*c1 + u0%dN*c3)*lag_rop(jc) + lag_gop(:,jc)*c2 IF(xmhd_linear)uvec = uvec + umat(:,1)*lag_rop(jc) @@ -1693,7 +1814,7 @@ subroutine xmhd_build_ops(fullin) end do END IF !------------------------------------------------------------------------------ -! Compute dB/dt(T) matrix (1:3,8) +! Compute dB/dt(T) matrix (1:3,8) or dB/dt(Te) matrix (1:3,te_ind) ! If 'xmhd_two_temp=True', mtmp(1:3,8)%m => mtmp(1:3,te_ind)%m !------------------------------------------------------------------------------ IF(xmhd_hall.AND.xmhd_adv_temp)THEN @@ -1711,38 +1832,6 @@ subroutine xmhd_build_ops(fullin) end do end do END IF - !---Hyper resistivity - IF(j2_ind>0)THEN - c1 = -eta_hyper*jac_dt*det*j2_scale - IF(.NOT.xmhd_diss_centered)c1 = 2.d0*c1 - do jc=1,oft_hcurl%nce - uvec = hcurl_cop(:,jc)*c1 - do lj=1,3 - !$omp simd - do jr=1,oft_lagrange%nce - mtmp(lj,j2_ind)%m(jr,jc) = mtmp(lj,j2_ind)%m(jr,jc) & - + b_cop(jr,1,lj)*uvec(1) + b_cop(jr,2,lj)*uvec(2) + b_cop(jr,3,lj)*uvec(3) - end do - end do - ! - uvec = hcurl_rop(:,jc)*det - !$omp simd - do jr=1,oft_hcurl%nce - mtmp(j2_ind,j2_ind)%m(jr,jc) = mtmp(j2_ind,j2_ind)%m(jr,jc) & - + DOT_PRODUCT(hcurl_rop(:,jr),uvec) - end do - end do - do li=1,3 - do jc=1,oft_lagrange%nce - uvec = b_cop(jc,:,li)*det/j2_scale - !$omp simd - do jr=1,oft_hcurl%nce - mtmp(j2_ind,li)%m(jr,jc) = mtmp(j2_ind,li)%m(jr,jc) & - + DOT_PRODUCT(hcurl_cop(:,jr),uvec) - end do - end do - end do - END IF END IF ! Advance B test !------------------------------------------------------------------------------ ! Compute dV/dt matrix @@ -1751,7 +1840,7 @@ subroutine xmhd_build_ops(fullin) ! Compute dV/dt(B) matrix (4:6,1:3) !------------------------------------------------------------------------------ IF(xmhd_jcb)THEN - c1 = -jac_dt*det/(vel_scale*u0%N*m_ion*mu0) + c1 = -jac_dt*det/(u0%N*m_ion*mu0) do li=1,3 do jc=1,oft_lagrange%nce uvec = (cross_product(u0%J,b_rop(jc,:,li)) + cross_product(b_cop(jc,:,li),u0%B))*c1 @@ -1776,21 +1865,14 @@ subroutine xmhd_build_ops(fullin) END IF IF(xmhd_linear)c2 = jac_dt*det c3=2.d0/3.d0 - IF(visc_itype==3)THEN - !--- Anisotropic tensor (nu_par*bb + nu_perp*(I-bb)) - do j=1,3 - ten1(:,j) = (nu_tmp(1)-nu_tmp(2))*bhat(j)*bhat - ten1(j,j) = ten1(j,j)+nu_tmp(2) - end do - ELSE - c2 = nu_tmp(1)*c2 - END IF + c4 = 0.5d0*nu_tmp(1)*c2 + c2 = nu_tmp(2)*c2 !---Advection and mass do jc=1,oft_lagrange%nce u = lag_rop(jc)*det IF(xmhd_advec)THEN u = u + DOT_PRODUCT(u0%V,lag_gop(:,jc))*jac_dt*det - umat = dVT*lag_rop(jc)*jac_dt*det + umat = dvT*lag_rop(jc)*jac_dt*det ELSE umat = 0.d0 END IF @@ -1802,97 +1884,147 @@ subroutine xmhd_build_ops(fullin) ELSE uvec=0.d0 END IF + !$omp simd private(u) + do jr=1,oft_lagrange%nce + u = DOT_PRODUCT(lag_gopt(jr,:),uvec) + !---Vx submatrix (1,1:3) + mtmp(4,4)%m(jr,jc) = mtmp(4,4)%m(jr,jc) + lag_rop(jr)*umat(1,1) + u + mtmp(4,5)%m(jr,jc) = mtmp(4,5)%m(jr,jc) + lag_rop(jr)*umat(1,2) + mtmp(4,6)%m(jr,jc) = mtmp(4,6)%m(jr,jc) + lag_rop(jr)*umat(1,3) + !---Vy submatrix (2,1:3) + mtmp(5,4)%m(jr,jc) = mtmp(5,4)%m(jr,jc) + lag_rop(jr)*umat(2,1) + mtmp(5,5)%m(jr,jc) = mtmp(5,5)%m(jr,jc) + lag_rop(jr)*umat(2,2) + u + mtmp(5,6)%m(jr,jc) = mtmp(5,6)%m(jr,jc) + lag_rop(jr)*umat(2,3) + !---Vz submatrix (3,1:3) + mtmp(6,4)%m(jr,jc) = mtmp(6,4)%m(jr,jc) + lag_rop(jr)*umat(3,1) + mtmp(6,5)%m(jr,jc) = mtmp(6,5)%m(jr,jc) + lag_rop(jr)*umat(3,2) + mtmp(6,6)%m(jr,jc) = mtmp(6,6)%m(jr,jc) + lag_rop(jr)*umat(3,3) + u + end do !---Viscous term SELECT CASE(visc_itype) - CASE(1) - uvec = uvec + c2*lag_gop(:,jc) - !$omp simd private(u) - do jr=1,oft_lagrange%nce - u = DOT_PRODUCT(lag_gopt(jr,:),uvec) - !---Vx submatrix (1,1:3) - mtmp(4,4)%m(jr,jc) = mtmp(4,4)%m(jr,jc) + lag_rop(jr)*umat(1,1) + u - mtmp(4,5)%m(jr,jc) = mtmp(4,5)%m(jr,jc) + lag_rop(jr)*umat(1,2) - mtmp(4,6)%m(jr,jc) = mtmp(4,6)%m(jr,jc) + lag_rop(jr)*umat(1,3) - !---Vy submatrix (2,1:3) - mtmp(5,4)%m(jr,jc) = mtmp(5,4)%m(jr,jc) + lag_rop(jr)*umat(2,1) - mtmp(5,5)%m(jr,jc) = mtmp(5,5)%m(jr,jc) + lag_rop(jr)*umat(2,2) + u - mtmp(5,6)%m(jr,jc) = mtmp(5,6)%m(jr,jc) + lag_rop(jr)*umat(2,3) - !---Vz submatrix (3,1:3) - mtmp(6,4)%m(jr,jc) = mtmp(6,4)%m(jr,jc) + lag_rop(jr)*umat(3,1) - mtmp(6,5)%m(jr,jc) = mtmp(6,5)%m(jr,jc) + lag_rop(jr)*umat(3,2) - mtmp(6,6)%m(jr,jc) = mtmp(6,6)%m(jr,jc) + lag_rop(jr)*umat(3,3) + u - end do - CASE(2) - uvec = uvec + c2*lag_gop(:,jc) - !$omp simd private(u) - do jr=1,oft_lagrange%nce - u = DOT_PRODUCT(lag_gopt(jr,:),uvec) - !---Vx submatrix (1,1:3) - mtmp(4,4)%m(jr,jc) = mtmp(4,4)%m(jr,jc) + lag_rop(jr)*umat(1,1) & - + lag_gopt(jr,1)*uvec(1) - c3*lag_gopt(jr,1)*uvec(1) + u - mtmp(4,5)%m(jr,jc) = mtmp(4,5)%m(jr,jc) + lag_rop(jr)*umat(1,2) & - + lag_gopt(jr,2)*uvec(1) - c3*lag_gopt(jr,1)*uvec(2) - mtmp(4,6)%m(jr,jc) = mtmp(4,6)%m(jr,jc) + lag_rop(jr)*umat(1,3) & - + lag_gopt(jr,3)*uvec(1) - c3*lag_gopt(jr,1)*uvec(3) - !---Vy submatrix (2,1:3) - mtmp(5,4)%m(jr,jc) = mtmp(5,4)%m(jr,jc) + lag_rop(jr)*umat(2,1) & - + lag_gopt(jr,1)*uvec(2) - c3*lag_gopt(jr,2)*uvec(1) - mtmp(5,5)%m(jr,jc) = mtmp(5,5)%m(jr,jc) + lag_rop(jr)*umat(2,2) & - + lag_gopt(jr,2)*uvec(2) - c3*lag_gopt(jr,2)*uvec(2) + u - mtmp(5,6)%m(jr,jc) = mtmp(5,6)%m(jr,jc) + lag_rop(jr)*umat(2,3) & - + lag_gopt(jr,3)*uvec(2) - c3*lag_gopt(jr,2)*uvec(3) - !---Vz submatrix (3,1:3) - mtmp(6,4)%m(jr,jc) = mtmp(6,4)%m(jr,jc) + lag_rop(jr)*umat(3,1) & - + lag_gopt(jr,1)*uvec(3) - c3*lag_gopt(jr,3)*uvec(1) - mtmp(6,5)%m(jr,jc) = mtmp(6,5)%m(jr,jc) + lag_rop(jr)*umat(3,2) & - + lag_gopt(jr,2)*uvec(3) - c3*lag_gopt(jr,3)*uvec(2) - mtmp(6,6)%m(jr,jc) = mtmp(6,6)%m(jr,jc) + lag_rop(jr)*umat(3,3) & - + lag_gopt(jr,3)*uvec(3) - c3*lag_gopt(jr,3)*uvec(3) + u - end do - CASE(3) ! Case 'ani' - DO jr=1,3 - DO j=1,3 - uten(:,j,jr)=c2*(ten1(:,jr)*lag_gop(j,jc) - c3*ten1(:,j)*lag_gop(jr,jc)) + CASE(1) ! Kinematic viscosity + DO vj=1,3 + DO vi=1,3 + !$omp simd + DO jr=1,oft_lagrange%nce + mtmp(3+vj,3+vj)%m(jr,jc) = mtmp(3+vj,3+vj)%m(jr,jc) & + + c2*lag_gopt(jr,vi)*lag_gop(vi,jc) + END DO + END DO + END DO + CASE(2) ! Isotropic viscosity + DO vj=1,3 + DO vi=1,3 + !$omp simd + DO jr=1,oft_lagrange%nce + mtmp(3+vj,3+vj)%m(jr,jc) = mtmp(3+vj,3+vj)%m(jr,jc) & + + c2*lag_gopt(jr,vi)*lag_gop(vi,jc) + mtmp(3+vj,3+vi)%m(jr,jc) = mtmp(3+vj,3+vi)%m(jr,jc) & + + c2*lag_gopt(jr,vi)*lag_gop(vj,jc) + END DO + END DO + !$omp simd + DO jr=1,oft_lagrange%nce + mtmp(3+vj,4)%m(jr,jc) = mtmp(3+vj,4)%m(jr,jc) & + - 2.d0*c2*lag_gopt(jr,vj)*lag_gop(1,jc)/3.d0 + mtmp(3+vj,5)%m(jr,jc) = mtmp(3+vj,5)%m(jr,jc) & + - 2.d0*c2*lag_gopt(jr,vj)*lag_gop(2,jc)/3.d0 + mtmp(3+vj,6)%m(jr,jc) = mtmp(3+vj,6)%m(jr,jc) & + - 2.d0*c2*lag_gopt(jr,vj)*lag_gop(3,jc)/3.d0 END DO END DO - uvec = uvec + c2*MATMUL(ten1,lag_gop(:,jc)) - !$omp simd private(u) - do jr=1,oft_lagrange%nce - u = DOT_PRODUCT(lag_gopt(jr,:),uvec) - !---Vx submatrix (1,1:3) - mtmp(4,4)%m(jr,jc) = mtmp(4,4)%m(jr,jc) + lag_rop(jr)*umat(1,1) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,1,1)) + u - mtmp(4,5)%m(jr,jc) = mtmp(4,5)%m(jr,jc) + lag_rop(jr)*umat(1,2) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,1,2)) - mtmp(4,6)%m(jr,jc) = mtmp(4,6)%m(jr,jc) + lag_rop(jr)*umat(1,3) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,1,3)) - !---Vy submatrix (2,1:3) - mtmp(5,4)%m(jr,jc) = mtmp(5,4)%m(jr,jc) + lag_rop(jr)*umat(2,1) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,2,1)) - mtmp(5,5)%m(jr,jc) = mtmp(5,5)%m(jr,jc) + lag_rop(jr)*umat(2,2) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,2,2)) + u - mtmp(5,6)%m(jr,jc) = mtmp(5,6)%m(jr,jc) + lag_rop(jr)*umat(2,3) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,2,3)) - !---Vz submatrix (3,1:3) - mtmp(6,4)%m(jr,jc) = mtmp(6,4)%m(jr,jc) + lag_rop(jr)*umat(3,1) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,3,1)) - mtmp(6,5)%m(jr,jc) = mtmp(6,5)%m(jr,jc) + lag_rop(jr)*umat(3,2) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,3,2)) - mtmp(6,6)%m(jr,jc) = mtmp(6,6)%m(jr,jc) + lag_rop(jr)*umat(3,3) & - + DOT_PRODUCT(lag_gopt(jr,:),uten(:,3,3)) + u - end do END SELECT + !--- + IF(nu_tmp(1)>0.d0)THEN + !--- 2 b (b . grad(psi)) - 2/3 grad(psi) + uvec = 2.d0*bhat*DOT_PRODUCT(bhat,lag_gop(:,jc)) - 2.d0*lag_gop(:,jc)/3.d0 + DO vj=1,3 + DO jr=1,oft_lagrange%nce + !--- 3 b_j (b . grad(phi)) - d_j phi + u = c4*(3.d0*bhat(vj)*DOT_PRODUCT(bhat,lag_gopt(jr,:)) - lag_gopt(jr,vj)) + DO vm=1,3 + mtmp(3+vj,3+vm)%m(jr,jc) = mtmp(3+vj,3+vm)%m(jr,jc) + u*uvec(vm) + END DO + END DO + END DO + END IF + !--- + IF(nu_gyro>0.d0)THEN + c_gyro=nu_gyro*u0%Ti*jac_dt*det/(4.d0*bmag) + IF(.NOT.xmhd_diss_centered)c_gyro = 2.d0*c_gyro + DO vj=1,3 + DO vm=1,3 + IF(vj==vm)THEN + krok_delta=1.d0 + ELSE + krok_delta=0.d0 + END IF + c_gyro=ABS(c_gyro) + DO vlc=1,6 + vi=lev_cevita(1,vlc) + vk=lev_cevita(2,vlc) + vl=lev_cevita(3,vlc) + DO jr=1,oft_lagrange%nce + mtmp(3+vj,3+vm)%m(jr,jc) = mtmp(3+vj,3+vm)%m(jr,jc) & + - c_gyro*lag_gopt(jr,vi)*bhat(vk)*(krok_delta + 3.d0*bhat(vm)*bhat(vj))*lag_gop(vl,jc) + mtmp(3+vj,3+vl)%m(jr,jc) = mtmp(3+vj,3+vl)%m(jr,jc) & + - c_gyro*lag_gopt(jr,vi)*bhat(vk)*(krok_delta + 3.d0*bhat(vm)*bhat(vj))*lag_gop(vm,jc) + IF(vl==vm)THEN + mtmp(3+vj,4)%m(jr,jc) = mtmp(3+vj,4)%m(jr,jc) & + + 2.d0*c_gyro*lag_gopt(jr,vi)*bhat(vk)*(krok_delta + 3.d0*bhat(vm)*bhat(vj))*lag_gop(1,jc)/3.d0 + mtmp(3+vj,5)%m(jr,jc) = mtmp(3+vj,5)%m(jr,jc) & + + 2.d0*c_gyro*lag_gopt(jr,vi)*bhat(vk)*(krok_delta + 3.d0*bhat(vm)*bhat(vj))*lag_gop(2,jc)/3.d0 + mtmp(3+vj,6)%m(jr,jc) = mtmp(3+vj,6)%m(jr,jc) & + + 2.d0*c_gyro*lag_gopt(jr,vi)*bhat(vk)*(krok_delta + 3.d0*bhat(vm)*bhat(vj))*lag_gop(3,jc)/3.d0 + END IF + END DO + c_gyro=-c_gyro + END DO + END DO + END DO + ! + DO vi=1,3 + DO vk=1,3 + IF(vi==vk)THEN + krok_delta=1.d0 + ELSE + krok_delta=0.d0 + END IF + c_gyro=ABS(c_gyro) + DO vlc=1,6 + vj=lev_cevita(1,vlc) + vl=lev_cevita(2,vlc) + vm=lev_cevita(3,vlc) + DO jr=1,oft_lagrange%nce + mtmp(3+vj,3+vl)%m(jr,jc) = mtmp(3+vj,3+vl)%m(jr,jc) & + + c_gyro*lag_gopt(jr,vi)*bhat(vm)*(krok_delta + 3.d0*bhat(vi)*bhat(vk))*lag_gop(vk,jc) + mtmp(3+vj,3+vk)%m(jr,jc) = mtmp(3+vj,3+vk)%m(jr,jc) & + + c_gyro*lag_gopt(jr,vi)*bhat(vm)*(krok_delta + 3.d0*bhat(vi)*bhat(vk))*lag_gop(vl,jc) + IF(vl==vk)THEN + mtmp(3+vj,4)%m(jr,jc) = mtmp(3+vj,4)%m(jr,jc) & + - 2.d0*c_gyro*lag_gopt(jr,vi)*bhat(vm)*(krok_delta + 3.d0*bhat(vi)*bhat(vk))*lag_gop(1,jc)/3.d0 + mtmp(3+vj,5)%m(jr,jc) = mtmp(3+vj,5)%m(jr,jc) & + - 2.d0*c_gyro*lag_gopt(jr,vi)*bhat(vm)*(krok_delta + 3.d0*bhat(vi)*bhat(vk))*lag_gop(2,jc)/3.d0 + mtmp(3+vj,6)%m(jr,jc) = mtmp(3+vj,6)%m(jr,jc) & + - 2.d0*c_gyro*lag_gopt(jr,vi)*bhat(vm)*(krok_delta + 3.d0*bhat(vi)*bhat(vk))*lag_gop(3,jc)/3.d0 + END IF + END DO + c_gyro=-c_gyro + END DO + END DO + END DO + END IF end do !------------------------------------------------------------------------------ ! Compute dV/dt(n) matrix (4:6,7) !------------------------------------------------------------------------------ IF(xmhd_adv_den)THEN - c1 = jac_dt*det*den_scale/(u0%N*u0%N*m_ion*mu0*vel_scale) - c3 = (u0%Ti+u0%Te)*k_boltz*jac_dt*det*den_scale/(u0%N*m_ion*vel_scale) + c1 = jac_dt*det/(u0%N*u0%N*m_ion*mu0) + c3 = (u0%Ti+u0%Te)*k_boltz*jac_dt*det/(u0%N*m_ion) c2 = c3/u0%N IF(xmhd_linear)THEN c1 = 0.d0 - c2 = c3/(u0%Ti+u0%Te) + c2 = k_boltz*jac_dt*det/(u0%N*m_ion) END IF do jc=1,oft_lagrange%nce uvec = -lag_rop(jc)*u0%dN*c2 + lag_gop(:,jc)*c3 @@ -1907,10 +2039,10 @@ subroutine xmhd_build_ops(fullin) end do END IF !------------------------------------------------------------------------------ -! Compute dV/dt(T) matrix (4:6,8) and dV/dt(Te) matrix (4:6,te_ind) +! Compute dV/dt(T) matrix (4:6,8) and dV/dt(Te) matrix (4:6,te_ind) !------------------------------------------------------------------------------ IF(xmhd_adv_temp)THEN - c2 = k_boltz*jac_dt*det/(m_ion*vel_scale) + c2 = k_boltz*jac_dt*det/(m_ion) IF(.NOT.xmhd_two_temp)c2=c2*(1.d0+te_factor) c1 = c2/u0%N do jc=1,oft_lagrange%nce @@ -1939,9 +2071,9 @@ subroutine xmhd_build_ops(fullin) !------------------------------------------------------------------------------ IF(xmhd_adv_den)THEN c2 = jac_dt*det - c1 = c2*vel_scale/den_scale + c1 = c2 !---Upwinding setup - IF(xmhd_upwind)c3 = jac_dt*det*xmhd_upwind_weight(v0_mag,he,d_dens) + IF(xmhd_upwind)c3 = jac_dt*det*xmhd_upwind_weight(v0_mag,he,diff_tmp) !------------------------------------------------------------------------------ ! Compute dn/dt(V) matrix (7,4:6) !------------------------------------------------------------------------------ @@ -1959,7 +2091,7 @@ subroutine xmhd_build_ops(fullin) !------------------------------------------------------------------------------ do jc=1,oft_lagrange%nce u = DOT_PRODUCT(u0%V,lag_gop(:,jc))*c2 + divv*lag_rop(jc)*c2 + lag_rop(jc)*det - uvec = d_dens*c2*lag_gop(:,jc) + uvec = diff_tmp*c2*lag_gop(:,jc) IF(xmhd_upwind)uvec = uvec + u0%V*(DOT_PRODUCT(u0%V,lag_gop(:,jc)))*c3 !$omp simd do jr=1,oft_lagrange%nce @@ -1969,7 +2101,7 @@ subroutine xmhd_build_ops(fullin) end do !---Hyper diffusivity IF(n2_ind>0)THEN - c1 = -d2_dens*jac_dt*det*n2_scale/den_scale + c1 = -d2_dens*jac_dt*det IF(.NOT.xmhd_diss_centered)c1 = 2.d0*c1 do jc=1,oft_lagrange%nce uvec = lag_gop(:,jc)*c1 @@ -1978,7 +2110,7 @@ subroutine xmhd_build_ops(fullin) mtmp(7,n2_ind)%m(jr,jc) = mtmp(7,n2_ind)%m(jr,jc) & + DOT_PRODUCT(lag_gopt(jr,:),uvec) end do - uvec = lag_gop(:,jc)*det*den_scale/n2_scale + uvec = lag_gop(:,jc)*det !$omp simd do jr=1,oft_lagrange%nce mtmp(n2_ind,7)%m(jr,jc) = mtmp(n2_ind,7)%m(jr,jc) & @@ -2015,7 +2147,7 @@ subroutine xmhd_build_ops(fullin) !------------------------------------------------------------------------------ ! Compute dT/dt(V) matrix (8,4:6) !------------------------------------------------------------------------------ - c3 = jac_dt*det*vel_scale ! advection + c3 = jac_dt*det ! advection c1 = u0%Ti*tgam_fac*c3 ! compression c2 = m_ion*tgam_fac*den_scale*c3/(k_boltz*u0%N) !---Viscous term @@ -2023,28 +2155,31 @@ subroutine xmhd_build_ops(fullin) IF(.NOT.xmhd_two_temp)c2 = c2/(1.d0+te_factor) SELECT CASE(visc_itype) CASE(1) - umat = -c2*nu_tmp(1)*u0%dV + umat = -c2*nu_tmp(2)*u0%dV CASE(2) !--- Nabla(V) + (Nabla(V))^T - umat = -c2*nu_tmp(1)*(u0%dV+dVT) + umat = -c2*nu_tmp(2)*(u0%dV+dvT) !--- (- 2/3 I Div(V)) - umat(1,1) = umat(1,1) + c2*nu_tmp(1)*2.d0*divv/3.d0 - umat(2,2) = umat(2,2) + c2*nu_tmp(1)*2.d0*divv/3.d0 - umat(3,3) = umat(3,3) + c2*nu_tmp(1)*2.d0*divv/3.d0 - CASE(3) - !--- Anisotropic tensor (nu_par*bb + nu_perp*(I-bb)) - do j=1,3 - ten1(:,j) = (nu_tmp(1)-nu_tmp(2))*bhat(j)*bhat - ten1(j,j) = ten1(j,j)+nu_tmp(2) - end do - ten1=c2*ten1/2.d0 - umat = -MATMUL(ten1,dVT+2.d0*u0%dV) - MATMUL(dVT,ten1) & - + 2.d0/3.d0*ten1*divv - u = SUM(SUM(ten1*u0%dV,DIM=2)) - umat(1,1) = umat(1,1) + 2.d0/3.d0*u - umat(2,2) = umat(2,2) + 2.d0/3.d0*u - umat(3,3) = umat(3,3) + 2.d0/3.d0*u + umat(1,1) = umat(1,1) + c2*nu_tmp(2)*2.d0*divv/3.d0 + umat(2,2) = umat(2,2) + c2*nu_tmp(2)*2.d0*divv/3.d0 + umat(3,3) = umat(3,3) + c2*nu_tmp(2)*2.d0*divv/3.d0 END SELECT + IF(nu_tmp(1)>0.d0)THEN + !--- W = Nabla(V) + (Nabla(V))^T - 2/3 I Div(V) + ten1 = u0%dV+dvT + ten1(1,1) = ten1(1,1) - 2.d0*divv/3.d0 + ten1(2,2) = ten1(2,2) - 2.d0*divv/3.d0 + ten1(3,3) = ten1(3,3) - 2.d0*divv/3.d0 + !--- b W b + u = DOT_PRODUCT(bhat,MATMUL(ten1,bhat)) + !--- (b W b)(3*bb - I)/2 + do j=1,3 + ten1(:,j) = 3.d0*bhat(j)*bhat + ten1(j,j) = ten1(j,j) - 1.d0 + end do + umat = umat - 0.5d0*c2*nu_tmp(1)*u*ten1 + END IF + !--- Gyroviscosity does no work (Pi_gv:Nabla(V) = 0), so it has no heating term ELSE umat=0.d0 END IF @@ -2062,7 +2197,7 @@ subroutine xmhd_build_ops(fullin) ! Compute dT/dt(n) matrix (8,7) !------------------------------------------------------------------------------ IF(xmhd_ohmic.AND.xmhd_adv_den.AND.(.NOT.xmhd_two_temp))THEN - c1 = eta_curr*tgam_fac*DOT_PRODUCT(u0%J,u0%J)*den_scale*jac_dt*det/ & + c1 = eta_curr*tgam_fac*DOT_PRODUCT(u0%J,u0%J)*jac_dt*det/ & ((1.d0+te_factor)*mu0*u0%N*u0%N*k_boltz) do jc=1,oft_lagrange%nce u = lag_rop(jc)*c1 @@ -2120,7 +2255,7 @@ subroutine xmhd_build_ops(fullin) !------------------------------------------------------------------------------ ! Compute dTe/dt(V) matrix (te_ind,4:6) !------------------------------------------------------------------------------ - c1 = jac_dt*det*vel_scale ! advection + c1 = jac_dt*det ! advection c2 = u0%Te*tgam_fac*c1 ! compression do jc=1,oft_lagrange%nce uvec = lag_gop(:,jc)*c2 + u0%dTe*lag_rop(jc)*c1 @@ -2138,7 +2273,7 @@ subroutine xmhd_build_ops(fullin) c1 = 0.d0 IF(xmhd_ohmic)c1 = eta_curr*tgam_fac*DOT_PRODUCT(u0%J,u0%J)/(mu0*u0%N*u0%N*k_boltz) c1 = c1 + DOT_PRODUCT(u0%J,u0%dTe)/(mu0*u0%N*u0%N*elec_charge) ! advection - c1 = c1*den_scale*jac_dt*det + c1 = c1*jac_dt*det do jc=1,oft_lagrange%nce u = lag_rop(jc)*c1 !$omp simd @@ -2162,18 +2297,13 @@ subroutine xmhd_build_ops(fullin) end do END IF !------------------------------------------------------------------------------ -! Compute dTe/dt(Te) matrix (8,8) +! Compute dTe/dt(Te) matrix (te_ind,te_ind) !------------------------------------------------------------------------------ - IF(xmhd_brag)THEN - chi_temp = brag_elec_transport(u0%N,u0%Te,bmag)/u0%N - chi_temp(1) = kappa_par*chi_temp(1) - chi_temp(2) = MAX(1.d1, kappa_perp*chi_temp(2)) - END IF umat(:,1) = u0%V-u0%J/(mu0*u0%N*elec_charge) ! advection !---Upwinding setup IF(xmhd_upwind)THEN v0_mag = magnitude(umat(:,1)) - c1 = jac_dt*det*xmhd_upwind_weight(v0_mag,he,chi_temp(2)*tgam_fac) + c1 = jac_dt*det*xmhd_upwind_weight(v0_mag,he,chi_temp(4)*tgam_fac) END IF c2 = divv*tgam_fac ! compression IF(xmhd_therm_equil)c2 = c2 - (1.d0 - 3.d0*u0%Ti/u0%Te)/(2.d0*elec_ion_therm_rate(u0%Te,u0%N,mu_ion)) @@ -2185,16 +2315,16 @@ subroutine xmhd_build_ops(fullin) u = (lag_rop(jc)*c2 + DOT_PRODUCT(umat(:,1),lag_gop(:,jc)))*jac_dt*det u = u + lag_rop(jc)*det !---Thermal conduction - uvec = ((chi_temp(1)-chi_temp(2))*DOT_PRODUCT(lag_gop(:,jc),bhat)*bhat & - + chi_temp(2)*lag_gop(:,jc))*c3 - u = u - ((chi_temp(1)-chi_temp(2))*DOT_PRODUCT(u0%dN,bhat)*DOT_PRODUCT(bhat,lag_gop(:,jc)) & - + chi_temp(2)*DOT_PRODUCT(u0%dN,lag_gop(:,jc)))*c3/u0%N + uvec = ((chi_temp(3)-chi_temp(4))*DOT_PRODUCT(lag_gop(:,jc),bhat)*bhat & + + chi_temp(4)*lag_gop(:,jc))*c3 + u = u - ((chi_temp(3)-chi_temp(4))*DOT_PRODUCT(u0%dN,bhat)*DOT_PRODUCT(bhat,lag_gop(:,jc)) & + + chi_temp(4)*DOT_PRODUCT(u0%dN,lag_gop(:,jc)))*c3/u0%N !---Upwinding IF(xmhd_upwind)uvec = uvec + umat(:,1)*(DOT_PRODUCT(umat(:,1),lag_gop(:,jc)))*c1 !$omp simd do jr=1,oft_lagrange%nce - mtmp(te_ind,te_ind)%m(jr,jc) = mtmp(te_ind,te_ind)%m(jr,jc) + lag_rop(jr)*u & - + DOT_PRODUCT(lag_gopt(jr,:),uvec) + mtmp(te_ind,te_ind)%m(jr,jc) = mtmp(te_ind,te_ind)%m(jr,jc) + lag_rop(jr)*u & + + DOT_PRODUCT(lag_gopt(jr,:),uvec) end do end do END IF @@ -2203,84 +2333,74 @@ subroutine xmhd_build_ops(fullin) ! End quadrature loop !------------------------------------------------------------------------------ end do + IF(xmhd_hall.AND.xmhd_two_temp)THEN ! Remove Hall-term alias before BCs and periodic transforms + DO li=1,3 + NULLIFY(mtmp(li,8)%m) + END DO + END IF + !------------------------------------------------------------------------------ + ! Apply row/column scaling for scaled fields (V, n, N2, J2) + !------------------------------------------------------------------------------ + DO m=1,xmhd_rep%nfields + DO li=4,6 + IF(ASSOCIATED(mtmp(li,m)%m).AND.(m/=li))mtmp(li,m)%m = mtmp(li,m)%m/vel_scale + IF(ASSOCIATED(mtmp(m,li)%m).AND.(m/=li))mtmp(m,li)%m = mtmp(m,li)%m*vel_scale + END DO + IF(ASSOCIATED(mtmp(7,m)%m).AND.(m/=7))mtmp(7,m)%m = mtmp(7,m)%m/den_scale + IF(ASSOCIATED(mtmp(m,7)%m).AND.(m/=7))mtmp(m,7)%m = mtmp(m,7)%m*den_scale + IF((n2_ind>0).AND.(m/=n2_ind))THEN + IF(ASSOCIATED(mtmp(n2_ind,m)%m))mtmp(n2_ind,m)%m = mtmp(n2_ind,m)%m/n2_scale + IF(ASSOCIATED(mtmp(m,n2_ind)%m))mtmp(m,n2_ind)%m = mtmp(m,n2_ind)%m*n2_scale + END IF + IF((j2_ind>0).AND.(m/=j2_ind))THEN + IF(ASSOCIATED(mtmp(j2_ind,m)%m))mtmp(j2_ind,m)%m = mtmp(j2_ind,m)%m/j2_scale + IF(ASSOCIATED(mtmp(m,j2_ind)%m))mtmp(m,j2_ind)%m = mtmp(m,j2_ind)%m*j2_scale + END IF + END DO !------------------------------------------------------------------------------ ! Apply BCs to local matrices !------------------------------------------------------------------------------ CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%n_bc(j_lag),7) CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%t_bc(j_lag),8) IF(xmhd_two_temp)CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%t_bc(j_lag),te_ind) - !---Magnetic BC - IF(xmhd_adv_b)THEN - DO jr=1,oft_lagrange%nce - IF(oft_lagrange%global%gbe(j_lag(jr)))THEN - CALL lag_vbc_tensor(oft_lagrange,j_lag(jr),2,umat) - DO m=1,xmhd_rep%nfields - IF(ASSOCIATED(mtmp(1,m)%m))THEN - DO jc=1,oft_lagrange%nce - !---Unpack - uvec(1)=mtmp(1,m)%m(jr,jc) - uvec(2)=mtmp(2,m)%m(jr,jc) - uvec(3)=mtmp(3,m)%m(jr,jc) - !---Apply BC - uvec = MATMUL(umat,uvec) - !---Repack - mtmp(1,m)%m(jr,jc)=uvec(1) - mtmp(2,m)%m(jr,jc)=uvec(2) - mtmp(3,m)%m(jr,jc)=uvec(3) - END DO - END IF - END DO - END IF - END DO - ELSE - CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%b_bc(j_lag),1) - CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%b_bc(j_lag),2) - CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%b_bc(j_lag),3) - END IF - !---Velocity BC - IF(xmhd_vbcdir)THEN - DO jr=1,oft_lagrange%nce - IF(oft_lagrange%global%gbe(j_lag(jr)))THEN - CALL lag_vbc_tensor(oft_lagrange,j_lag(jr),vbc_type,umat) - DO m=1,xmhd_rep%nfields - IF(ASSOCIATED(mtmp(4,m)%m))THEN - DO jc=1,oft_lagrange%nce - !---Unpack - uvec(1)=mtmp(4,m)%m(jr,jc) - uvec(2)=mtmp(5,m)%m(jr,jc) - uvec(3)=mtmp(6,m)%m(jr,jc) - !---Apply BC - uvec = MATMUL(umat,uvec) - !---Repack - mtmp(4,m)%m(jr,jc)=uvec(1) - mtmp(5,m)%m(jr,jc)=uvec(2) - mtmp(6,m)%m(jr,jc)=uvec(3) - END DO - END IF - END DO + !---Magnetic field and velocity BCs + CALL vec_bc_local(mtmp,j_lag,oft_xmhd_ops%b_bc_type,0) + CALL vec_bc_local(mtmp,j_lag,oft_xmhd_ops%v_bc_type,3) + !---Handle rotational periodicity + IF(mesh%periodic%revolved)THEN + CALL lagrange_vector_packblock(oft_lagrange,mtmp(4:6,4:6),j_lag) + IF(xmhd_adv_b)THEN + CALL lagrange_vector_packblock(oft_lagrange,mtmp(1:3,1:3),j_lag) + CALL lagrange_vector_packblock(oft_lagrange,mtmp(1:3,4:6),j_lag) + IF(xmhd_jcb)CALL lagrange_vector_packblock(oft_lagrange,mtmp(4:6,1:3),j_lag) + END IF + DO m=7,xmhd_rep%nfields + IF(ASSOCIATED(mtmp(m,4)%m))CALL lagrange_vector_packcols(oft_lagrange,mtmp(m,4:6),j_lag) + IF(ASSOCIATED(mtmp(4,m)%m))CALL lagrange_vector_packrows(oft_lagrange,mtmp(4:6,m),j_lag) + IF(xmhd_adv_b)THEN + IF(ASSOCIATED(mtmp(m,1)%m))CALL lagrange_vector_packcols(oft_lagrange,mtmp(m,1:3),j_lag) + IF(ASSOCIATED(mtmp(1,m)%m))CALL lagrange_vector_packrows(oft_lagrange,mtmp(1:3,m),j_lag) END IF END DO - ELSE - CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%v_bc(j_lag),4) - CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%v_bc(j_lag),5) - CALL xmhd_rep%mat_zero_local_rows(mtmp,oft_xmhd_ops%v_bc(j_lag),6) END IF !------------------------------------------------------------------------------ ! Add local contribution to full matrix !------------------------------------------------------------------------------ - IF(xmhd_hall.AND.xmhd_two_temp)NULLIFY(mtmp(1,8)%m,mtmp(2,8)%m,mtmp(3,8)%m) - CALL xmhd_rep%mat_add_local(Jac,mtmp,iloc,tlocks) + IF(reg_solid(mesh%reg(i)))THEN + CALL xmhd_rep%mat_add_local(Jac,mtmp_solid,iloc,tlocks) + ELSE + CALL xmhd_rep%mat_add_local(Jac,mtmp,iloc,tlocks) + END IF end do END DO !---Delete temporary matrices deallocate(j_lag) -deallocate(lag_rop,lag_gop,b_rop,b_cop) -deallocate(lag_gopt) -IF(j2_ind>0)DEALLOCATE(hcurl_rop,hcurl_cop,j_hcurl) +deallocate(lag_rop,lag_gop,lag_gopt,b_rop,b_cop) +IF(j2_ind>0)deallocate(j_hcurl,hcurl_rop,hcurl_cop) NULLIFY(xmhd_lag_rop,xmhd_lag_gop,xmhd_hcurl_rop,xmhd_hcurl_cop) !---Destroy local matrix CALL xmhd_rep%mat_destroy_local(mtmp) -deallocate(mtmp,iloc) +deallocate(mtmp,mtmp_solid,iloc) !$omp end parallel !--Destroy thread locks DO i=1,xmhd_rep%nfields @@ -2294,59 +2414,90 @@ subroutine xmhd_build_ops(fullin) CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%n_bc,7) CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%t_bc,8) IF(xmhd_two_temp)CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%t_bc,te_ind) -!---Magnetic BC -IF(xmhd_adv_b)THEN - DO i=1,oft_lagrange%nbe - IF(.NOT.oft_lagrange%linkage%leo(i))CYCLE - j=oft_lagrange%lbe(i) - IF(.NOT.oft_lagrange%global%gbe(j))CYCLE - jtmp=j - CALL lag_vbc_diag(oft_lagrange,j,2,umat) - DO jr=1,3 - DO jc=1,3 - CALL Jac%add_values(jtmp,jtmp,umat(jr,jc),1,1,jr,jc) +CALL vec_bc_diag(oft_xmhd_ops%b_bc_type,0,xmhd_bbcdir) +CALL vec_bc_diag(oft_xmhd_ops%v_bc_type,3,xmhd_vbcdir) +!------------------------------------------------------------------------------ +! Final assembly +!------------------------------------------------------------------------------ +call oft_xmhd_create(tmp) +call Jac%assemble(tmp) +call tmp%delete +DEALLOCATE(tmp) +DEBUG_STACK_POP +contains +!------------------------------------------------------------------------------ +!> Apply boundary conditions to the rows of a vector Lagrange field in the local matrix +!! +!! Rows with projected BCs (`bc_type>1`) are multiplied by the BC tensor (see +!! \ref oft_lag_operators::lag_vbc_tensor "lag_vbc_tensor") and rows with Dirichlet BCs +!! are zeroed for all components (`bc_type==1`) or the components normal to the axis of +!! revolved meshes (`bc_type==-1`). +!------------------------------------------------------------------------------ +subroutine vec_bc_local(mtmp,j_lag,bc_type,offset) +type(oft_local_mat), intent(inout) :: mtmp(:,:) !< Local matrix blocks +integer(i4), intent(in) :: j_lag(:) !< Local to global Lagrange DOF mapping for cell +integer(i4), intent(in) :: bc_type(:) !< BC type for each Lagrange node +integer(i4), intent(in) :: offset !< Index of first vector component block minus one +integer(i4) :: jr,jc,m +real(r8) :: nn(3,3),vtmp(3) +IF(ANY(bc_type(j_lag)>1))THEN + DO jr=1,oft_lagrange%nce + IF(bc_type(j_lag(jr))<=1)CYCLE + IF(.NOT.oft_lagrange%global%gbe(j_lag(jr)))CYCLE + CALL lag_vbc_tensor(oft_lagrange,j_lag(jr),bc_type(j_lag(jr)),nn) + DO m=1,xmhd_rep%nfields + IF(.NOT.ASSOCIATED(mtmp(offset+1,m)%m))CYCLE + DO jc=1,SIZE(mtmp(offset+1,m)%m,2) + vtmp(1)=mtmp(offset+1,m)%m(jr,jc) + vtmp(2)=mtmp(offset+2,m)%m(jr,jc) + vtmp(3)=mtmp(offset+3,m)%m(jr,jc) + vtmp = MATMUL(nn,vtmp) + mtmp(offset+1,m)%m(jr,jc)=vtmp(1) + mtmp(offset+2,m)%m(jr,jc)=vtmp(2) + mtmp(offset+3,m)%m(jr,jc)=vtmp(3) END DO END DO END DO -ELSE - CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%b_bc,1) - CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%b_bc,2) - CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%b_bc,3) END IF -!---Velocity BC -IF(xmhd_vbcdir)THEN +CALL xmhd_rep%mat_zero_local_rows(mtmp,(bc_type(j_lag)==1),offset+1) +CALL xmhd_rep%mat_zero_local_rows(mtmp,(ABS(bc_type(j_lag))==1),offset+2) +CALL xmhd_rep%mat_zero_local_rows(mtmp,(ABS(bc_type(j_lag))==1),offset+3) +end subroutine vec_bc_local +!------------------------------------------------------------------------------ +!> Set diagonal entries for boundary conditions on a vector Lagrange field +!------------------------------------------------------------------------------ +subroutine vec_bc_diag(bc_type,offset,proj_bcs) +integer(i4), intent(in) :: bc_type(:) !< BC type for each Lagrange node +integer(i4), intent(in) :: offset !< Index of first vector component block minus one +logical, intent(in) :: proj_bcs !< Projected BCs present? +integer(i4) :: i,j,jr,jc,jtmp(1) +real(r8) :: nn(3,3) +IF(proj_bcs)THEN DO i=1,oft_lagrange%nbe IF(.NOT.oft_lagrange%linkage%leo(i))CYCLE j=oft_lagrange%lbe(i) + IF(bc_type(j)<=1)CYCLE IF(.NOT.oft_lagrange%global%gbe(j))CYCLE jtmp=j - CALL lag_vbc_diag(oft_lagrange,j,vbc_type,umat) + CALL lag_vbc_diag(oft_lagrange,j,bc_type(j),nn) + CALL lagrange_vector_transmat(oft_lagrange,nn,j) DO jr=1,3 DO jc=1,3 - CALL Jac%add_values(jtmp,jtmp,umat(jr,jc),1,1,jr+3,jc+3) + CALL Jac%add_values(jtmp,jtmp,nn(jr,jc),1,1,jr+offset,jc+offset) END DO END DO END DO -ELSE - CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%v_bc,4) - CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%v_bc,5) - CALL fem_dirichlet_diag(oft_lagrange,Jac,oft_xmhd_ops%v_bc,6) END IF -!------------------------------------------------------------------------------ -! Final assembly -!------------------------------------------------------------------------------ -call oft_xmhd_create(tmp) -call Jac%assemble(tmp) -call tmp%delete -DEALLOCATE(tmp) -DEBUG_STACK_POP +CALL fem_dirichlet_diag(oft_lagrange,Jac,(bc_type==1),offset+1) +CALL fem_dirichlet_diag(oft_lagrange,Jac,(ABS(bc_type)==1),offset+2) +CALL fem_dirichlet_diag(oft_lagrange,Jac,(ABS(bc_type)==1),offset+3) +end subroutine vec_bc_diag end subroutine xmhd_build_ops !------------------------------------------------------------------------------ !> Setup and allocate operators used in xMHD advance !------------------------------------------------------------------------------ subroutine xmhd_alloc_ops -integer(i4) :: i,j,level,levelin,offset -INTEGER(i4), ALLOCATABLE :: color_tmp(:) +integer(i4) :: i,j,level,levelin integer(i4), ALLOCATABLE :: j_lag(:) REAL(r8), POINTER :: node_flag(:) CLASS(oft_vector), POINTER :: vectmp @@ -2391,13 +2542,13 @@ subroutine xmhd_alloc_ops xmhd_mat_mask(3,3)=2 END IF !---V rows -DO j=1,3 - IF(xmhd_jcb)xmhd_mat_mask(3+j,1:3)=1 - xmhd_mat_mask(3+j,4:6)=1 - IF(xmhd_adv_den)xmhd_mat_mask(3+j,7)=1 +DO j=4,6 + IF(xmhd_jcb)xmhd_mat_mask(j,1:3)=1 + xmhd_mat_mask(j,4:6)=1 + IF(xmhd_adv_den)xmhd_mat_mask(j,7)=1 IF(xmhd_adv_temp)THEN - xmhd_mat_mask(3+j,8)=1 - IF(xmhd_two_temp)xmhd_mat_mask(3+j,te_ind)=1 + xmhd_mat_mask(j,8)=1 + IF(xmhd_two_temp)xmhd_mat_mask(j,te_ind)=1 END IF END DO !---n rows @@ -2417,14 +2568,14 @@ subroutine xmhd_alloc_ops xmhd_mat_mask(8,8)=1 !---Te rows IF(xmhd_two_temp)THEN + IF(xmhd_adv_b)xmhd_mat_mask(te_ind,1:3)=1 xmhd_mat_mask(te_ind,4:6)=1 + IF(xmhd_adv_den)xmhd_mat_mask(te_ind,7)=1 xmhd_mat_mask(te_ind,te_ind)=1 IF(xmhd_therm_equil)THEN xmhd_mat_mask(8,te_ind)=1 xmhd_mat_mask(te_ind,8)=1 END IF - IF(xmhd_adv_b)xmhd_mat_mask(te_ind,1:3)=1 - IF(xmhd_adv_den)xmhd_mat_mask(te_ind,7)=1 ELSE IF(xmhd_ohmic.AND.xmhd_adv_b)xmhd_mat_mask(8,1:3)=1 IF(xmhd_ohmic.AND.xmhd_adv_den)xmhd_mat_mask(8,7)=1 @@ -2443,31 +2594,30 @@ subroutine xmhd_alloc_ops CALL xmhd_rep%mat_create(ops%J,xmhd_mat_mask) ml_J(level-xmhd_minlev+1)%m=>ops%J !---Set element coloring - IF(xmhd_nparts>1)THEN + IF(xmhd_nparts(level)>1)THEN SELECT TYPE(this=>ops%J) - CLASS IS(oft_native_matrix) + CLASS IS(oft_matrix) ALLOCATE(this%color(this%nr)) - this%color=0 - offset=0 - DO i=1,xmhd_rep%nfields - ALLOCATE(color_tmp(xmhd_rep%fields(i)%fe%ne)) - SELECT TYPE(this=>xmhd_rep%fields(i)%fe) - CLASS IS(oft_fem_type) - CALL fem_partition(this,color_tmp,xmhd_nparts) - CLASS DEFAULT - CALL oft_abort("Invalid FE representation for partitioning", & - "xmhd_alloc_ops",__FILE__) - END SELECT - DO j=1,xmhd_rep%fields(i)%fe%ne - this%color(offset+j)=color_tmp(j) - END DO - offset=offset+xmhd_rep%fields(i)%fe%ne - DEALLOCATE(color_tmp) - END DO + IF(mesh%fullmesh)THEN + j = xmhd_nparts(level) + ELSE + j = INT(REAL(xmhd_nparts(level),8)/REAL(oft_env%nprocs,8),4) + END IF + IF(j<=0)THEN + CALL oft_abort('Coloring results in fewer than one block per processor (xmhd_nparts < nprocs)','xmhd_alloc_ops',__FILE__) + ELSE IF(j==1)THEN + DEALLOCATE(this%color) + ELSE + CALL comp_fem_partition(xmhd_rep,this%color,j) + END IF END SELECT END IF end do -CALL ML_xmhd_rep%build_interp(xmhd_minlev) +IF(mesh%periodic%revolved)THEN + CALL build_ML_interp() +ELSE + CALL ML_xmhd_rep%build_interp(xmhd_minlev) +END IF !---Alias do level=levelin,xmhd_minlev,-1 call xmhd_set_level(level) @@ -2490,7 +2640,7 @@ subroutine xmhd_alloc_ops ! ALLOCATE(j_lag(oft_lagrange%nce)) DO i=1,mesh%nc - IF(solid_cell(i))THEN + IF(reg_solid(mesh%reg(i)))THEN CALL oft_lagrange%ncdofs(i,j_lag) node_flag(j_lag)=1.d0 END IF @@ -2506,8 +2656,99 @@ subroutine xmhd_alloc_ops CALL xmhd_set_bc() END DO call xmhd_set_level(levelin) -! IF(oft_debug_print(2))WRITE(*,*)' Done' DEBUG_STACK_POP +contains +!------------------------------------------------------------------------------ +!> Build MG interpolation operators for rotationally periodic (revolved) meshes +!! +!! Coarse and fine vector Lagrange fields (B and V) may use different local frames on the +!! periodic boundaries, so their interpolation blocks couple all three components. +!------------------------------------------------------------------------------ +subroutine build_ML_interp() +type(oft_matrix_ptr) :: vec_interp(3,3,2) +class(oft_vector), pointer :: fvec,cvec,fvec_lag,cvec_lag +type(oft_graph_ptr), POINTER :: graphs(:,:) +type(oft_matrix_ptr), POINTER :: mats(:,:) +INTEGER(i4) :: i,j,k,levmin,ierr,offset +INTEGER(i4), PARAMETER :: vec_offsets(2) = [0,3] +DEBUG_STACK_PUSH +levmin=xmhd_minlev+1 +OUTER: DO j=levmin,ML_xmhd_rep%nlevels +!------------------------------------------------------------------------------ +! Create composite matrix +!------------------------------------------------------------------------------ + CALL xmhd_set_level(j) + !---Specify child graphs + ALLOCATE(graphs(ML_xmhd_rep%nfields,ML_xmhd_rep%nfields)) + DO i=1,ML_xmhd_rep%nfields + IF(.NOT.ASSOCIATED(ML_xmhd_rep%ml_fields(i)%ml%interp_graphs(j)%g))THEN + DEALLOCATE(graphs) + CYCLE OUTER + END IF + graphs(i,i)%g=>ML_xmhd_rep%ml_fields(i)%ml%interp_graphs(j)%g + END DO + NULLIFY(cvec_lag,fvec_lag) + CALL xmhd_ML_lagrange%vec_create(cvec_lag,level=xmhd_ML_lagrange%level-1) + CALL xmhd_ML_lagrange%vec_create(fvec_lag) + DO k=1,2 + offset=vec_offsets(k) + DO i=offset+1,offset+3 + graphs(offset+1,i)%g=>ML_xmhd_rep%ml_fields(i)%ml%interp_graphs(j)%g + graphs(offset+2,i)%g=>ML_xmhd_rep%ml_fields(i)%ml%interp_graphs(j)%g + graphs(offset+3,i)%g=>ML_xmhd_rep%ml_fields(i)%ml%interp_graphs(j)%g + CALL create_matrix(vec_interp(1,i-offset,k)%m,graphs(offset+1:offset+1,i:i),fvec_lag,cvec_lag) + CALL create_matrix(vec_interp(2,i-offset,k)%m,graphs(offset+2:offset+2,i:i),fvec_lag,cvec_lag) + CALL create_matrix(vec_interp(3,i-offset,k)%m,graphs(offset+3:offset+3,i:i),fvec_lag,cvec_lag) + END DO + END DO + CALL cvec_lag%delete + CALL fvec_lag%delete + DEALLOCATE(cvec_lag,fvec_lag) + !---Get coarse and fine vectors + CALL ML_xmhd_rep%vec_create(cvec,level=ML_xmhd_rep%level-1) + CALL ML_xmhd_rep%vec_create(fvec) + !---Construct matrix + CALL create_matrix(ML_xmhd_rep%interp_matrices(j)%m,graphs,fvec,cvec) + DEALLOCATE(graphs) +!------------------------------------------------------------------------------ +! Combine child matrices into composite matrix +!------------------------------------------------------------------------------ + !---Specify child matrices + ALLOCATE(mats(ML_xmhd_rep%nfields,ML_xmhd_rep%nfields)) + DO i=1,ML_xmhd_rep%nfields + IF(.NOT.ASSOCIATED(ML_xmhd_rep%ml_fields(i)%ml%interp_matrices(j)%m))THEN + CALL oft_abort('Sub-field matrix not allocated.','build_ML_interp',__FILE__) + END IF + mats(i,i)%m=>ML_xmhd_rep%ml_fields(i)%ml%interp_matrices(j)%m + END DO + DO k=1,2 + offset=vec_offsets(k) + DO i=offset+1,offset+3 + mats(offset+1,i)%m=>vec_interp(1,i-offset,k)%m + mats(offset+2,i)%m=>vec_interp(2,i-offset,k)%m + mats(offset+3,i)%m=>vec_interp(3,i-offset,k)%m + END DO + CALL lag_build_per_vinterp(xmhd_ML_lagrange,vec_interp(:,:,k)) + END DO + !---Combine matrices + CALL combine_matrices(mats,ML_xmhd_rep%nfields,ML_xmhd_rep%nfields,ML_xmhd_rep%interp_matrices(j)%m) + DO k=1,2 + offset=vec_offsets(k) + DO i=offset+1,offset+3 + CALL mats(offset+1,i)%m%delete() + CALL mats(offset+2,i)%m%delete() + CALL mats(offset+3,i)%m%delete() + END DO + END DO + DEALLOCATE(mats) + CALL ML_xmhd_rep%interp_matrices(j)%m%assemble + !---Delete temporaries + CALL cvec%delete + CALL fvec%delete + DEALLOCATE(cvec,fvec) +END DO OUTER +DEBUG_STACK_POP +end subroutine build_ML_interp end subroutine xmhd_alloc_ops !------------------------------------------------------------------------------ !> Setup material regions from XML input file @@ -2524,10 +2765,9 @@ subroutine xmhd_setup_regions() ALLOCATE(reg_types(mesh%nreg)) reg_types=1 IF(.NOT.ALLOCATED(eta_reg))THEN - ALLOCATE(eta_reg(mesh%nreg),solid_cell(mesh%nc)) + ALLOCATE(eta_reg(mesh%nreg),reg_solid(mesh%nreg)) END IF eta_reg=-1.d0 -solid_cell=.FALSE. IF(xmhd_root_node%associated())THEN !---Look for pre node CALL xml_get_element(xmhd_root_node,"region",reg_nodes,ierr) @@ -2536,23 +2776,26 @@ subroutine xmhd_setup_regions() reg_node=>reg_nodes%nodes(i+1)!%this !--- CALL xml_get_element(reg_node,"id",inner_node,ierr) - IF(ierr/=0)CALL oft_xml_abort("Error reading regions IDs for group","xmhd_setup_regions",__FILE__) + IF(ierr/=0)CALL oft_abort("Error reading regions IDs for group", & + "xmhd_setup_regions",__FILE__) CALL xml_read_content(inner_node,regs,iostat=ierr) - IF(ierr/=0)CALL oft_xml_abort("Error reading values in id group","xmhd_setup_regions",__FILE__) - ! IF(.NOT.ALLOCATED(regs))CALL oft_abort("Zero values given in id group", & - ! "xmhd_setup_regions",__FILE__) - ! IF(SIZE(regs)>mesh%nreg)CALL oft_abort("Too many id values specified","xmhd_setup_regions", & + IF(ierr/=0)CALL oft_abort("Error reading values in id group", & + "xmhd_setup_regions",__FILE__) + ! IF(nread_id==0)CALL oft_abort("Zero values given in id group", & + ! "xmhd_setup_regions",__FILE__) + ! IF(ierr>0)CALL oft_abort("Too many id values specified","xmhd_setup_regions", & ! __FILE__) IF(ANY(regs>mesh%nreg).OR.ANY(regs<=0))CALL oft_abort("Invalid region ID","xmhd_setup_regions",__FILE__) !--- CALL xml_get_element(reg_node,"eta",inner_node,ierr) CALL xml_read_content(inner_node,eta,iostat=ierr) - IF(ierr/=0)CALL oft_xml_abort("Error reading values in eta group", & - "xmhd_setup_regions",__FILE__) + IF(ierr/=0)CALL oft_xml_abort("Error reading values in eta group","xmhd_setup_regions",__FILE__) + ! IF(nread_eta==0)CALL oft_abort("Zero values given in eta group", & + ! "xmhd_setup_regions",__FILE__) ! IF(ierr>0)CALL oft_abort("Too many eta values specified","xmhd_setup_regions", & - ! __FILE__) + ! __FILE__) IF(eta<0.d0)CALL oft_abort("Invalid eta value specified","xmhd_setup_regions", & - __FILE__) + __FILE__) !---Get region type CALL xml_get_element(reg_node,"type",inner_node,ierr) IF(ierr/=0)THEN @@ -2561,9 +2804,9 @@ subroutine xmhd_setup_regions() CALL xml_read_content(inner_node,reg_type,iostat=ierr) IF(ierr/=0)CALL oft_xml_abort("Error reading values in type group","xmhd_setup_regions",__FILE__) ! IF(nread_eta==0)CALL oft_abort("Zero values given in type group", & - ! "xmhd_setup_regions",__FILE__) + ! "xmhd_setup_regions",__FILE__) ! IF(ierr>0)CALL oft_abort("Too many type values specified","xmhd_setup_regions", & - ! __FILE__) + ! __FILE__) IF(reg_type<1.OR.reg_type>2)CALL oft_abort("Invalid type specified","xmhd_setup_regions", & __FILE__) END IF @@ -2581,13 +2824,12 @@ subroutine xmhd_setup_regions() END IF END IF ! +reg_solid=(reg_types==2) xmhd_rw=.FALSE. DO i=1,mesh%nc - IF(reg_types(mesh%reg(i))==2)THEN - xmhd_rw=.TRUE. - solid_cell(i)=.TRUE. - END IF + IF(reg_solid(mesh%reg(i)))xmhd_rw=.TRUE. END DO +DEALLOCATE(reg_types) #ifdef HAVE_MPI call MPI_ALLREDUCE(MPI_IN_PLACE,xmhd_rw,1,OFT_MPI_LOGICAL,MPI_LOR,oft_env%COMM,ierr) #endif @@ -2596,34 +2838,112 @@ subroutine xmhd_setup_regions() end subroutine xmhd_setup_regions !------------------------------------------------------------------------------ !> Set BC flags +!! +!! Boundary conditions for the vector fields (B and V) are set for each Lagrange node using +!! a type flag: +!! - `0`: No BC +!! - `1`: Dirichlet on all components (increment held fixed) +!! - `2`: Fixed normal component (`bbc='bc'`, `vbc='norm'`) +!! - `3`: Fixed tangential components (`bbc='ic'`, `vbc='tang'`) +!! - `-1`: Fixed components normal to the axis (y,z) on the axis of revolved meshes !------------------------------------------------------------------------------ subroutine xmhd_set_bc -integer(i4) :: i,j +integer(i4) :: i,j,k +integer(4) :: nnodes,nread,ierr,nbfs +integer(4), pointer, dimension(:) :: bc_flag +logical, allocatable, dimension(:) :: vert_flag,edge_flag,face_flag,bc_tmp +TYPE(xml_node) :: bc_node DEBUG_STACK_PUSH +nbfs=MAXVAL(mesh%bfs) +#ifdef HAVE_MPI +call MPI_ALLREDUCE(MPI_IN_PLACE,nbfs,1,OFT_MPI_I4,MPI_MAX,oft_env%COMM,ierr) +#endif !------------------------------------------------------------------------------ -! Set boundary condition flag for B +! Set boundary condition for B !------------------------------------------------------------------------------ -ALLOCATE(oft_xmhd_ops%b_bc(oft_lagrange%ne)) -oft_xmhd_ops%b_bc=.FALSE. +ALLOCATE(oft_xmhd_ops%b_bc_type(oft_lagrange%ne)) +oft_xmhd_ops%b_bc_type=0 IF(xmhd_adv_b)THEN - !---Do nothing + IF(bbc(1:2)=='bc')THEN + DO i=1,oft_lagrange%ne + IF(oft_lagrange%global%gbe(i))oft_xmhd_ops%b_bc_type(i)=2 + END DO + ELSE IF(bbc(1:2)=='ic')THEN + DO i=1,oft_lagrange%ne + IF(oft_lagrange%global%gbe(i))oft_xmhd_ops%b_bc_type(i)=3 + END DO + ELSE + CALL oft_abort('Invalid B Boundary Condition.','xmhd_bc',__FILE__) + END IF + !---Hold the field components normal to the axis (y,z) fixed on the axis of revolved meshes + DO i=1,oft_lagrange%ne + IF((oft_lagrange%per_vmap(i)==3).AND.(oft_xmhd_ops%b_bc_type(i)==0))oft_xmhd_ops%b_bc_type(i)=-1 + END DO ELSE - oft_xmhd_ops%b_bc=.TRUE. + oft_xmhd_ops%b_bc_type=1 END IF +xmhd_bbcdir=ANY(oft_xmhd_ops%b_bc_type>1) !------------------------------------------------------------------------------ -! Set boundary condition flag for V +! Apply boundary condition to V !------------------------------------------------------------------------------ -ALLOCATE(oft_xmhd_ops%v_bc(oft_lagrange%ne)) -oft_xmhd_ops%v_bc=.FALSE. -IF(vbc(1:4)=='none'.OR.vbc(1:4)=='norm')THEN +ALLOCATE(oft_xmhd_ops%v_bc_type(oft_lagrange%ne)) +oft_xmhd_ops%v_bc_type=0 +IF(vbc(1:4)=='none')THEN !---Do nothing ELSE IF(vbc(1:3)=='all')THEN - oft_xmhd_ops%v_bc=(oft_lagrange%global%gbe.OR.oft_xmhd_ops%solid_node) + DO i=1,oft_lagrange%ne + IF(oft_lagrange%global%gbe(i).OR.oft_xmhd_ops%solid_node(i))oft_xmhd_ops%v_bc_type(i)=1 + END DO +ELSE IF(vbc(1:4)=='norm')THEN + DO i=1,oft_lagrange%ne + IF(oft_lagrange%global%gbe(i).OR.oft_xmhd_ops%solid_node(i))oft_xmhd_ops%v_bc_type(i)=2 + END DO +ELSE IF(vbc(1:4)=='tang')THEN + DO i=1,oft_lagrange%ne + IF(oft_lagrange%global%gbe(i).OR.oft_xmhd_ops%solid_node(i))oft_xmhd_ops%v_bc_type(i)=3 + END DO +ELSE IF(vbc(1:3)=='xml')THEN + IF(.NOT.xmhd_root_node%associated())CALL oft_abort('No xMHD node in XML file', 'xmhd_bc', __FILE__) + CALL xml_get_element(xmhd_root_node,"vel_bc",bc_node,ierr) + IF(ierr/=0)CALL oft_abort("Error locating velocity BC node in XML", "xmhd_bc", __FILE__) + CALL xml_read_content(bc_node,bc_flag,iostat=ierr) + IF((SIZE(bc_flag)0))CALL oft_abort("Error reading velocity BCs", "xmhd_bc", & + __FILE__) + ALLOCATE(vert_flag(mesh%np),edge_flag(mesh%ne),face_flag(mesh%nf),bc_tmp(oft_lagrange%ne)) + ! Loop over BC types (1->'all', 2->'norm', 3->'tang') + DO k=3,1,-1 + vert_flag=.FALSE. + edge_flag=.FALSE. + face_flag=.FALSE. + bc_tmp=.FALSE. + DO i=1,mesh%nbf + j = mesh%lbf(i) + IF(mesh%bfs(i)<0)CYCLE + IF(bc_flag(mesh%bfs(i))==k)THEN + vert_flag(mesh%lf(:,j))=.TRUE. + edge_flag(ABS(mesh%lfe(:,j)))=.TRUE. + face_flag(j)=.TRUE. + END IF + END DO + CALL fem_map_flag(oft_lagrange, vert_flag, edge_flag, face_flag, bc_tmp) + bc_tmp = (oft_lagrange%global%gbe.AND.bc_tmp) + DO i=1,oft_lagrange%ne + IF(bc_tmp(i))oft_xmhd_ops%v_bc_type(i)=k + END DO + END DO + DEALLOCATE(vert_flag,edge_flag,face_flag,bc_flag,bc_tmp) ELSE CALL oft_abort('Invalid V Boundary Condition.','xmhd_bc',__FILE__) END IF +!---Zero the velocity components normal to the axis (y,z) on the axis of revolved +! meshes (v_bc_type=-1); assumes rotation about x, as in the axis detection of +! oft_lag_setup +DO i=1,oft_lagrange%ne + IF((oft_lagrange%per_vmap(i)==3).AND.(oft_xmhd_ops%v_bc_type(i)==0))oft_xmhd_ops%v_bc_type(i)=-1 +END DO +xmhd_vbcdir=ANY(oft_xmhd_ops%v_bc_type>1) !------------------------------------------------------------------------------ -! Set boundary condition flag for Ne +! Apply boundary condition to n !------------------------------------------------------------------------------ ALLOCATE(oft_xmhd_ops%n_bc(oft_lagrange%ne)) IF(xmhd_adv_den)THEN @@ -2632,6 +2952,29 @@ subroutine xmhd_set_bc !---Do nothing ELSE IF(nbc=='d')THEN oft_xmhd_ops%n_bc=(oft_lagrange%global%gbe.OR.oft_xmhd_ops%solid_node) + ELSE IF(nbc=='x')THEN + IF(.NOT.xmhd_root_node%associated())CALL oft_abort('No xMHD node in XML file', 'xmhd_bc', __FILE__) + CALL xml_get_element(xmhd_root_node,"den_bc",bc_node,ierr) + IF(ierr/=0)CALL oft_abort("Error locating density BC node in XML", "xmhd_bc", __FILE__) + CALL xml_read_content(bc_node,bc_flag,iostat=ierr) + IF((SIZE(bc_flag)0))CALL oft_abort("Error reading density BCs", "xmhd_bc", & + __FILE__) + ALLOCATE(vert_flag(mesh%np),edge_flag(mesh%ne),face_flag(mesh%nf)) + vert_flag=.FALSE. + edge_flag=.FALSE. + face_flag=.FALSE. + DO i=1,mesh%nbf + j = mesh%lbf(i) + IF(mesh%bfs(i)<0)CYCLE + IF(bc_flag(mesh%bfs(i))==1)THEN ! Dirichlet on all components + vert_flag(mesh%lf(:,j))=.TRUE. + edge_flag(ABS(mesh%lfe(:,j)))=.TRUE. + face_flag(j)=.TRUE. + END IF + END DO + CALL fem_map_flag(oft_lagrange,vert_flag,edge_flag,face_flag,oft_xmhd_ops%n_bc) + DEALLOCATE(vert_flag,edge_flag,face_flag,bc_flag) + oft_xmhd_ops%n_bc = (oft_lagrange%global%gbe.AND.oft_xmhd_ops%n_bc) ELSE CALL oft_abort('Invalid N Boundary Condition.','xmhd_bc',__FILE__) END IF @@ -2639,7 +2982,7 @@ subroutine xmhd_set_bc oft_xmhd_ops%n_bc=.TRUE. END IF !------------------------------------------------------------------------------ -! Set boundary condition flag for Ti/Te +! Apply boundary condition to T !------------------------------------------------------------------------------ ALLOCATE(oft_xmhd_ops%t_bc(oft_lagrange%ne)) IF(xmhd_adv_temp)THEN @@ -2648,6 +2991,29 @@ subroutine xmhd_set_bc !---Do nothing ELSE IF(tbc=='d')THEN oft_xmhd_ops%t_bc=(oft_lagrange%global%gbe.OR.oft_xmhd_ops%solid_node) + ELSE IF(tbc=='x')THEN + IF(.NOT.xmhd_root_node%associated())CALL oft_abort('No xMHD node in XML file', 'xmhd_bc', __FILE__) + CALL xml_get_element(xmhd_root_node,"temp_bc",bc_node,ierr) + IF(ierr/=0)CALL oft_abort("Error locating temperature BC node in XML", "xmhd_bc", __FILE__) + CALL xml_read_content(bc_node,bc_flag,iostat=ierr) + IF((SIZE(bc_flag)0))CALL oft_abort("Error reading temperature BCs", "xmhd_bc", & + __FILE__) + ALLOCATE(vert_flag(mesh%np),edge_flag(mesh%ne),face_flag(mesh%nf)) + vert_flag=.FALSE. + edge_flag=.FALSE. + face_flag=.FALSE. + DO i=1,mesh%nbf + j = mesh%lbf(i) + IF(mesh%bfs(i)<0)CYCLE + IF(bc_flag(mesh%bfs(i))==1)THEN ! Dirichlet on all components + vert_flag(mesh%lf(:,j))=.TRUE. + edge_flag(ABS(mesh%lfe(:,j)))=.TRUE. + face_flag(j)=.TRUE. + END IF + END DO + CALL fem_map_flag(oft_lagrange,vert_flag,edge_flag,face_flag,oft_xmhd_ops%t_bc) + DEALLOCATE(vert_flag,edge_flag,face_flag,bc_flag) + oft_xmhd_ops%t_bc = (oft_lagrange%global%gbe.AND.oft_xmhd_ops%t_bc) ELSE CALL oft_abort('Invalid T Boundary Condition.','xmhd_bc',__FILE__) END IF @@ -2665,20 +3031,20 @@ subroutine xmhd_errmatrix_apply(self,a,b) class(oft_xmhd_errmatrix), intent(inout) :: self !< Error matrix object class(oft_vector), target, intent(inout) :: a !< Source field class(oft_vector), intent(inout) :: b !< Result of metric function -real(r8), allocatable, dimension(:) :: n_loc,t_loc,te_loc,n2_loc,j2_loc -real(r8), allocatable, dimension(:,:) :: b_loc,v_loc,vpin_loc,jpin_loc integer(i4) :: i,ii,ip,j,m,jr,jp,ncemax,nrows,ptind(2),li -integer(i4), allocatable :: j_lag(:),j_hcurl(:) -real(r8) :: f,diff,chi_temp(2),bmag,det -real(r8), allocatable, target :: lag_rop(:),lag_gop(:,:),hcurl_rop(:,:),hcurl_cop(:,:) -real(r8), allocatable ::lag_gopt(:,:),b_rop(:,:,:),b_cop(:,:,:) -real(r8), pointer, contiguous, dimension(:) :: npin,nout,tout,teout,n2out,j2out -real(r8), pointer, contiguous, dimension(:,:) :: vpin,vout,jpin,bout +integer(i4), allocatable, dimension(:) :: j_lag,j_hcurl +real(r8) :: f,diff,chi_temp(4),bmag,det +real(r8) :: mloc(3,3),goptmp(3,4),vol,eta_curr,neg_vols(3),divv,diff_tmp +real(r8) :: bhat(3),jp0q(3),np0q,vad(3),vad_mag,he,c1,cgop(3,6),nu_tmp(3),ten1(3,3) +real(r8) :: diag_vals(8),pt(3),pt_r,floor_tmp(3),fulltmp(41),u,uvec(3),umat(3,3),ten2(3,3),ten3(3,3) +real(r8), allocatable, dimension(:) :: te_loc,j2_loc,n2_loc +real(r8), allocatable, dimension(:,:) :: vpin_loc,lag_loc,b_loc +real(r8), target, allocatable, dimension(:) :: lag_rop +real(r8), target, allocatable, dimension(:,:) :: lag_gop,hcurl_rop,hcurl_cop +real(r8), pointer, contiguous, dimension(:) :: npin +real(r8), pointer, contiguous, dimension(:) :: nout,tout,teout,j2out,n2out +real(r8), pointer, contiguous, dimension(:,:) :: vpin,bpin,vout,bout real(r8), pointer, dimension(:) :: vtmp -real(r8) :: mloc(3,3),goptmp(3,4),vol,eta_curr,neg_vols(3),u,uvec(3),umat(3,3) -real(r8) :: bhat(3),jp0q(3),np0q,divv,floor_tmp(3),fulltmp(41) -real(r8) :: vad(3),vad_mag,he,c1,nu_tmp(3),ten1(3,3) -real(r8) :: diag_vals(8),pt(3),pt_r,cgop(3,6) logical :: curved,back_step type(oft_quad_type), pointer :: quad TYPE(xmhd_interp) :: full_interp @@ -2695,52 +3061,45 @@ subroutine xmhd_errmatrix_apply(self,a,b) !------------------------------------------------------------------------------ ! Get local field values from previous step if necessary !------------------------------------------------------------------------------ -NULLIFY(jpin,npin,vpin) +NULLIFY(npin,vpin,bpin) back_step=.FALSE. IF(ASSOCIATED(self%up))THEN - ALLOCATE(jpin(3,oft_lagrange%ne)) - vtmp=>jpin(1,:) - CALL self%up%get_local(vtmp,1) - vtmp=>jpin(2,:) - CALL self%up%get_local(vtmp,2) - vtmp=>jpin(3,:) - CALL self%up%get_local(vtmp,3) + ALLOCATE(bpin(oft_lagrange%ne,3)) + DO li=1,3 + vtmp=>bpin(:,li) + CALL self%up%get_local(vtmp,li) + END DO + CALL lagrange_vector_unpack(oft_lagrange,bpin,.TRUE.) CALL self%up%get_local(npin,7) IF(xmhd_upwind)THEN - ALLOCATE(vpin(3,oft_lagrange%ne)) - vtmp=>vpin(1,:) - CALL self%up%get_local(vtmp,4) - vtmp=>vpin(2,:) - CALL self%up%get_local(vtmp,5) - vtmp=>vpin(3,:) - CALL self%up%get_local(vtmp,6) + ALLOCATE(vpin(oft_lagrange%ne,3)) + DO li=1,3 + vtmp=>vpin(:,li) + CALL self%up%get_local(vtmp,li+3) + END DO + CALL lagrange_vector_unpack(oft_lagrange,vpin,.TRUE.) END IF back_step=.TRUE. END IF !------------------------------------------------------------------------------ ! Get local field values for result !------------------------------------------------------------------------------ -NULLIFY(bout,nout,tout,teout,n2out,j2out) +NULLIFY(nout,tout,teout,j2out,n2out) CALL b%set(0.d0) -ALLOCATE(bout(3,oft_lagrange%ne)) -vtmp=>bout(1,:) -CALL b%get_local(vtmp,1) -vtmp=>bout(2,:) -CALL b%get_local(vtmp,2) -vtmp=>bout(3,:) -CALL b%get_local(vtmp,3) -ALLOCATE(vout(3,oft_lagrange%ne)) -vtmp=>vout(1,:) -CALL b%get_local(vtmp,4) -vtmp=>vout(2,:) -CALL b%get_local(vtmp,5) -vtmp=>vout(3,:) -CALL b%get_local(vtmp,6) +ALLOCATE(bout(oft_lagrange%ne,3),vout(oft_lagrange%ne,3)) +DO li=1,3 + vtmp=>bout(:,li) + CALL b%get_local(vtmp,li) + vtmp=>vout(:,li) + CALL b%get_local(vtmp,li+3) +END DO +CALL lagrange_vector_unpack(oft_lagrange,bout,.TRUE.) +CALL lagrange_vector_unpack(oft_lagrange,vout,.TRUE.) CALL b%get_local(nout,7) CALL b%get_local(tout,8) IF(xmhd_two_temp)CALL b%get_local(teout,te_ind) -IF(n2_ind>0)CALL b%get_local(n2out,n2_ind) IF(j2_ind>0)CALL b%get_local(j2out,j2_ind) +IF(n2_ind>0)CALL b%get_local(n2out,n2_ind) !---Zero diagnostic values diag_vals=0.d0 ptind=(/1,2/) @@ -2749,29 +3108,23 @@ subroutine xmhd_errmatrix_apply(self,a,b) !------------------------------------------------------------------------------ ! Apply metric function !------------------------------------------------------------------------------ -!$omp parallel private(det,j_lag,lag_rop,lag_gop,b_rop,b_cop,li,lag_gopt, & -!$omp curved,goptmp,vol,j,m,jr,bhat,jp0q,vad,vad_mag,he,c1,u0,divv,fulltmp, & -!$omp np0q,eta_curr,u,b_loc,v_loc,pt_r,vpin_loc,jpin_loc,floor_tmp,n2_loc,j2_loc, & -!$omp n_loc,t_loc,te_loc,chi_temp,bmag,uvec,umat,pt,nu_tmp,ten1,i,ii,hcurl_rop, & -!$omp hcurl_cop,j_hcurl,cgop) & +!$omp parallel private(det,j_lag,j_hcurl,lag_rop,lag_gop,hcurl_rop,li, & +!$omp hcurl_cop,curved,goptmp,vol,j,m,jr,bhat,jp0q,vad,vad_mag,he,c1,cgop,divv,diff_tmp, & +!$omp np0q,eta_curr,u,b_loc,lag_loc,pt_r,vpin_loc,chi_temp,te_loc, & +!$omp j2_loc,n2_loc,bmag,uvec,umat,pt,nu_tmp,ten1,ten2,ten3,i,ii,floor_tmp,u0,fulltmp) & !$omp reduction(+:neg_vols) reduction(+:diag_vals) allocate(j_lag(oft_lagrange%nce)) -allocate(lag_rop(oft_lagrange%nce),lag_gop(3,oft_lagrange%nce),lag_gopt(oft_lagrange%nce,3)) -allocate(b_loc(oft_lagrange%nce,3)) -allocate(v_loc(oft_lagrange%nce,3),n_loc(oft_lagrange%nce),t_loc(oft_lagrange%nce)) +allocate(lag_rop(oft_lagrange%nce),lag_gop(3,oft_lagrange%nce)) +allocate(b_loc(oft_lagrange%nce,3),lag_loc(oft_lagrange%nce,5)) IF(xmhd_two_temp)allocate(te_loc(oft_lagrange%nce)) -IF(n2_ind>0)allocate(n2_loc(oft_lagrange%nce)) IF(j2_ind>0)THEN - ALLOCATE(hcurl_rop(3,oft_hcurl%nce),hcurl_cop(3,oft_hcurl%nce)) - ALLOCATE(j2_loc(oft_hcurl%nce),j_hcurl(oft_hcurl%nce)) + allocate(j_hcurl(oft_hcurl%nce),j2_loc(oft_hcurl%nce)) + allocate(hcurl_rop(3,oft_hcurl%nce),hcurl_cop(3,oft_hcurl%nce)) xmhd_hcurl_rop=>hcurl_rop xmhd_hcurl_cop=>hcurl_cop END IF -IF(back_step)allocate(vpin_loc(7,oft_lagrange%nce)) -allocate(b_rop(oft_lagrange%nce,3,3),b_cop(oft_lagrange%nce,3,3)) -!--- Define viscosity temporary varible (currently static) -nu_tmp(1) = nu_par -nu_tmp(2) = nu_perp +IF(n2_ind>0)allocate(n2_loc(oft_lagrange%nce)) +IF(back_step)allocate(vpin_loc(oft_lagrange%nce,7)) !---Setup caching xmhd_lag_rop=>lag_rop xmhd_lag_gop=>lag_gop @@ -2787,16 +3140,16 @@ subroutine xmhd_errmatrix_apply(self,a,b) !------------------------------------------------------------------------------ ! Init local entries !------------------------------------------------------------------------------ - b_loc=0.d0; v_loc=0.d0; n_loc=0.d0; t_loc=0.d0 + b_loc=0.d0; lag_loc=0.d0 IF(xmhd_two_temp)te_loc=0.d0 - IF(n2_ind>0)n2_loc=0.d0 IF(j2_ind>0)j2_loc=0.d0 + IF(n2_ind>0)n2_loc=0.d0 !---Lagrange elements IF(back_step)THEN do jr=1,oft_lagrange%nce - vpin_loc(1,jr) = npin(j_lag(jr)) - vpin_loc(2:4,jr) = jpin(:,j_lag(jr)) - IF(xmhd_upwind)vpin_loc(5:7,jr) = vpin(:,j_lag(jr)) + vpin_loc(jr,1) = npin(j_lag(jr)) + vpin_loc(jr,2:4) = bpin(j_lag(jr),:) + IF(xmhd_upwind)vpin_loc(jr,5:7) = vpin(j_lag(jr),:) end do END IF !------------------------------------------------------------------------------ @@ -2806,49 +3159,40 @@ subroutine xmhd_errmatrix_apply(self,a,b) !------------------------------------------------------------------------------ ! Get local reconstructed operators !------------------------------------------------------------------------------ - IF(curved.OR.m==1)THEN + if(curved.OR.m==1)then call mesh%jacobian(i,quad%pts(:,m),goptmp,vol) IF(j2_ind>0)call oft_hcurl_get_cgops(goptmp,cgop) - END IF + end if det=vol*quad%wts(m) - !---Lagrange elements + !---Get local reconstruction operators CALL oft_lag_eval_all(oft_lagrange,i,quad%pts(:,m),lag_rop) CALL oft_lag_geval_all(oft_lagrange,i,quad%pts(:,m),lag_gop,goptmp) - lag_gopt=TRANSPOSE(lag_gop) IF(j2_ind>0)THEN CALL oft_hcurl_eval_all(oft_hcurl,i,quad%pts(:,m),hcurl_rop,goptmp) CALL oft_hcurl_ceval_all(oft_hcurl,i,quad%pts(:,m),hcurl_cop,cgop) END IF - !---Setup rop/cop - b_rop=0.d0; b_cop=0.d0 - do jr=1,oft_lagrange%nce - b_rop(jr,1,1)=lag_rop(jr); b_rop(jr,2,2)=lag_rop(jr); b_rop(jr,3,3)=lag_rop(jr) - b_cop(jr,1,3)=lag_gopt(jr,2); b_cop(jr,1,2)=-lag_gopt(jr,3) - b_cop(jr,2,1)=lag_gopt(jr,3); b_cop(jr,2,3)=-lag_gopt(jr,1) - b_cop(jr,3,2)=lag_gopt(jr,1); b_cop(jr,3,1)=-lag_gopt(jr,2) - end do !---Evaluate centering values call full_interp%interp(i,quad%pts(:,m),goptmp,fulltmp) CALL xmhd_interp_unpack(fulltmp,u0) divv = u0%dV(1,1)+u0%dV(2,2)+u0%dV(3,3) - bmag = SQRT(SUM(u0%B**2)) + xmhd_eps + bmag = SQRT(SUM(u0%B**2)) + xmhd_B_eps bhat = u0%B/bmag - !---Reconstruct previous step fields + !---Reconstruct previous step fields if necessary vad=u0%V; vad_mag = magnitude(vad) np0q=u0%N; jp0q=u0%J IF(back_step)THEN np0q=0.d0; jp0q=0.d0 do jr=1,oft_lagrange%nce - np0q = np0q + lag_rop(jr)*vpin_loc(1,jr) - jp0q(1) = jp0q(1) + lag_gop(2,jr)*vpin_loc(4,jr) - lag_gop(3,jr)*vpin_loc(3,jr) - jp0q(2) = jp0q(2) + lag_gop(3,jr)*vpin_loc(2,jr) - lag_gop(1,jr)*vpin_loc(4,jr) - jp0q(3) = jp0q(3) + lag_gop(1,jr)*vpin_loc(3,jr) - lag_gop(2,jr)*vpin_loc(2,jr) + np0q = np0q + lag_rop(jr)*vpin_loc(jr,1) + jp0q(1) = jp0q(1) + lag_gop(2,jr)*vpin_loc(jr,4) - lag_gop(3,jr)*vpin_loc(jr,3) + jp0q(2) = jp0q(2) + lag_gop(3,jr)*vpin_loc(jr,2) - lag_gop(1,jr)*vpin_loc(jr,4) + jp0q(3) = jp0q(3) + lag_gop(1,jr)*vpin_loc(jr,3) - lag_gop(2,jr)*vpin_loc(jr,2) end do np0q = np0q*den_scale IF(xmhd_upwind)THEN vad=0.d0 do jr=1,oft_lagrange%nce - vad = vad + lag_rop(jr)*vpin_loc(5:7,jr) + vad = vad + lag_rop(jr)*vpin_loc(jr,5:7) end do vad=vad*vel_scale; vad_mag = magnitude(vad) END IF @@ -2866,14 +3210,34 @@ subroutine xmhd_errmatrix_apply(self,a,b) IF(den_floor>0.d0)u0%N=MAX(den_floor,u0%N) IF(den_floor>0.d0)np0q=MAX(den_floor,np0q) !---Transport coefficients (if fixed) + pt=mesh%log2phys(i,quad%pts(:,m)) eta_curr=eta*eta_reg(mesh%reg(i)) - IF(.NOT.xmhd_brag)THEN - chi_temp(1)=kappa_par*den_scale/u0%N - chi_temp(2)=kappa_perp*den_scale/u0%N + IF(ASSOCIATED(res_shaping))eta_curr = eta_curr*res_shaping(pt) + nu_tmp(1) = nu_par + nu_tmp(2) = nu_visc + IF(ASSOCIATED(visc_shaping))nu_tmp = nu_tmp*visc_shaping(pt) + diff_tmp=d_dens + IF(ASSOCIATED(diff_shaping))diff_tmp = diff_tmp*diff_shaping(pt) + IF(xmhd_brag)THEN + IF(xmhd_two_temp)THEN + chi_temp(1:2) = brag_ion_transport(u0%N,u0%Ti,mu_ion,bmag)/u0%N + chi_temp(3:4) = brag_elec_transport(u0%N,u0%Te,bmag)/u0%N + chi_temp(3) = kappa_par(2)*chi_temp(3) + chi_temp(4) = MAX(chi_perp_e_min, kappa_perp(2)*chi_temp(4)) + ELSE + chi_temp(1:2)=brag_comb_transport(u0%N,u0%Ti,mu_ion,bmag)/u0%N + END IF + chi_temp(1)=kappa_par(1)*chi_temp(1) + chi_temp(2)=kappa_perp(1)*chi_temp(2) + ELSE + chi_temp(1)=kappa_par(1)*den_scale/u0%N + chi_temp(2)=kappa_perp(1)*den_scale/u0%N + chi_temp(3)=kappa_par(2)*den_scale/u0%N + chi_temp(4)=kappa_perp(2)*den_scale/u0%N END IF + IF(ASSOCIATED(cond_shaping))chi_temp = chi_temp*cond_shaping(pt) !---Compute diagnostics IF(self%dt<0.d0)THEN - pt=mesh%log2phys(i,quad%pts(:,m)) pt_r = MAX(1.d-10,SUM(pt(ptind)**2)) diag_vals(1) = diag_vals(1) + SUM(u0%B**2)*det diag_vals(2) = diag_vals(2) + u0%N*SUM(u0%V**2)*det @@ -2883,25 +3247,22 @@ subroutine xmhd_errmatrix_apply(self,a,b) diag_vals(4) = diag_vals(4) + uvec(xmhd_taxis)*det/pt_r diag_vals(5) = diag_vals(5) + u0%N*det diag_vals(6) = diag_vals(6) + u0%Ti*det - diag_vals(7) = diag_vals(7) + (u0%divB**2)*det - diag_vals(8) = diag_vals(8) + u0%Te*det + diag_vals(7) = diag_vals(7) + u0%Te*det + diag_vals(8) = diag_vals(8) + (u0%divB**2)*det END IF !------------------------------------------------------------------------------ ! Compute F (u) for solid region !------------------------------------------------------------------------------ - IF(solid_cell(i))THEN + IF(reg_solid(mesh%reg(i)))THEN IF(xmhd_adv_b)THEN !---Eta J eta_curr=eta_reg(mesh%reg(i)) + IF(ASSOCIATED(res_shaping))eta_curr = eta_curr*res_shaping(pt) uvec = eta_curr*u0%J*self%dt*det - !---Add contributions - do li=1,3 - !$omp simd + c1 = u0%divB*k_db*self%dt*det do jr=1,oft_lagrange%nce - b_loc(jr,li) = b_loc(jr,li) & - + b_cop(jr,1,li)*uvec(1) + b_cop(jr,2,li)*uvec(2) + b_cop(jr,3,li)*uvec(3) & - + lag_rop(jr)*u0%B(li)*det + u0%divB*lag_gopt(jr,li)*k_db*self%dt*det - end do + b_loc(jr,:) = b_loc(jr,:) + cross_product(uvec,lag_gop(:,jr)) & + + lag_rop(jr)*u0%B*det + lag_gop(:,jr)*c1 end do END IF CYCLE @@ -2916,9 +3277,10 @@ subroutine xmhd_errmatrix_apply(self,a,b) ELSE eta_curr=eta*(eta_temp/u0%Te)**(3.d0/2.d0) END IF + IF(ASSOCIATED(res_shaping))eta_curr = eta_curr*res_shaping(pt) END IF !---Eta J - VxB - uvec = eta_curr*u0%J + cross_product(u0%B,u0%V) + uvec = eta_curr*u0%J + cross_product(u0%B,u0%V) IF((self%dt>0.d0).OR.xmhd_diss_centered)THEN c1 = 1.d0 IF(.NOT.xmhd_diss_centered)c1 = 2.d0 @@ -2939,20 +3301,18 @@ subroutine xmhd_errmatrix_apply(self,a,b) END IF END IF uvec = uvec*det - !---Add contributions - do li=1,3 - !$omp simd + !---Divergence cleaning + c1 = u0%divB*k_db*self%dt*det + !---Add contributions (Curl(phi e_k) . E = (E x Grad(phi))_k) do jr=1,oft_lagrange%nce - b_loc(jr,li) = b_loc(jr,li) & - + b_cop(jr,1,li)*uvec(1) + b_cop(jr,2,li)*uvec(2) + b_cop(jr,3,li)*uvec(3) & - + lag_rop(jr)*u0%B(li)*det + u0%divB*lag_gopt(jr,li)*k_db*self%dt*det - end do + b_loc(jr,:) = b_loc(jr,:) + cross_product(uvec,lag_gop(:,jr)) & + + lag_rop(jr)*u0%B*det + lag_gop(:,jr)*c1 end do !---Hyper-resistivity IF((self%dt>0.d0).AND.(j2_ind>0))THEN do jr=1,oft_hcurl%nce j2_loc(jr) = j2_loc(jr) + (DOT_PRODUCT(hcurl_rop(:,jr),u0%J2) & - + DOT_PRODUCT(hcurl_cop(:,jr),u0%J))*det/j2_scale + + DOT_PRODUCT(hcurl_cop(:,jr),u0%J))*det end do END IF END IF ! Advance B @@ -2961,7 +3321,7 @@ subroutine xmhd_errmatrix_apply(self,a,b) !------------------------------------------------------------------------------ uvec = 0.d0; umat = 0.d0 !---JxB - IF(xmhd_jcb)uvec = -cross_product(u0%J,u0%B)/(u0%N*m_ion*mu0) + IF(xmhd_jcb)uvec = uvec - cross_product(u0%J,u0%B)/(u0%N*m_ion*mu0) !---Gravity uvec(3) = uvec(3) + g_accel !---Advection @@ -2973,30 +3333,55 @@ subroutine xmhd_errmatrix_apply(self,a,b) IF((self%dt>0.d0).OR.xmhd_diss_centered)THEN SELECT CASE(visc_itype) CASE(1) - umat = nu_tmp(1)*u0%dV/(u0%N/den_scale) + umat = nu_tmp(2)*u0%dV/(u0%N/den_scale) CASE(2) !--- Nabla(V) + (Nabla(V))^T - umat = nu_tmp(1)*(u0%dV+TRANSPOSE(u0%dV))/(u0%N/den_scale) - !--- - 2/3 I Div(V) - u = 2.d0*nu_tmp(1)*divv/(3.d0*u0%N/den_scale) - umat(1,1) = umat(1,1) - u - umat(2,2) = umat(2,2) - u - umat(3,3) = umat(3,3) - u - CASE(3) - !--- Nabla(V) + (Nabla(V))^T - umat = (u0%dV+TRANSPOSE(u0%dV))/(u0%N/den_scale) + umat = nu_tmp(2)*(u0%dV+TRANSPOSE(u0%dV))/(u0%N/den_scale) !--- - 2/3 I Div(V) - u = 2.d0*divv/(3.d0*u0%N/den_scale) + u = 2.d0*nu_tmp(2)*divv/(3.d0*u0%N/den_scale) umat(1,1) = umat(1,1) - u umat(2,2) = umat(2,2) - u umat(3,3) = umat(3,3) - u - !--- Anisotropic tensor (nu_par*bb + nu_perp*(I-bb)) - do j=1,3 - ten1(:,j) = (nu_tmp(1)-nu_tmp(2))*bhat(j)*bhat - ten1(j,j) = ten1(j,j)+nu_tmp(2) - end do - umat = MATMUL(ten1,umat) END SELECT + IF(nu_tmp(1)>0.d0)THEN + !--- W = Nabla(V) + (Nabla(V))^T + ten1 = (u0%dV+TRANSPOSE(u0%dV)) + !--- - 2/3 I Div(V) + u = 2.d0*divv/3.d0 + ten1(1,1) = ten1(1,1) - u + ten1(2,2) = ten1(2,2) - u + ten1(3,3) = ten1(3,3) - u + !--- b W b + u = DOT_PRODUCT(bhat,MATMUL(ten1,bhat)) + !--- 3*bb - I + do j=1,3 + ten1(:,j) = 3.d0*bhat(j)*bhat + ten1(j,j) = ten1(j,j) - 1.d0 + end do + umat = umat + 0.5d0*nu_tmp(1)*u*ten1*den_scale/u0%N + END IF + IF(nu_gyro>0.d0)THEN + !--- W = Nabla(V) + (Nabla(V))^T + ten3 = (u0%dV+TRANSPOSE(u0%dV)) + !--- - 2/3 I Div(V) + u = 2.d0*divv/3.d0 + ten3(1,1) = ten3(1,1) - u + ten3(2,2) = ten3(2,2) - u + ten3(3,3) = ten3(3,3) - u + !--- I + 3*bb + do j=1,3 + ten1(:,j) = 3.d0*bhat(j)*bhat + ten1(j,j) = ten1(j,j) + 1.d0 + end do + !--- b x W (I + 3*bb) and transpose + ten2 = MATMUL(ten1,ten3) + ten1 = MATMUL(ten3,ten1) + do j=1,3 + ten1(:,j) = cross_product(bhat,ten1(:,j)) + ten2(j,:) = cross_product(ten2(j,:),bhat) + end do + umat = umat - nu_gyro*u0%Ti*(ten1 - ten2)/(4.d0*bmag) + END IF IF(.NOT.xmhd_diss_centered)umat = 2.d0*umat IF(xmhd_adv_den)uvec = uvec - MATMUL(u0%dN,umat)/u0%N !---Upwinding @@ -3009,50 +3394,47 @@ subroutine xmhd_errmatrix_apply(self,a,b) END IF END IF !---Mass and scale - umat = TRANSPOSE(umat)*self%dt*det/vel_scale - uvec = (uvec*self%dt + u0%V)*det/vel_scale + umat = TRANSPOSE(umat)*self%dt*det + uvec = (uvec*self%dt + u0%V)*det !---Add contributions - do li=1,3 - !$omp simd do jr=1,oft_lagrange%nce - v_loc(jr,li) = v_loc(jr,li) + lag_rop(jr)*uvec(li) & - + lag_gopt(jr,1)*umat(li,1) + lag_gopt(jr,2)*umat(li,2) & - + lag_gopt(jr,3)*umat(li,3) - end do + lag_loc(jr,1:3) = lag_loc(jr,1:3) + lag_rop(jr)*uvec & + + lag_gop(1,jr)*umat(:,1) + lag_gop(2,jr)*umat(:,2) & + + lag_gop(3,jr)*umat(:,3) end do !------------------------------------------------------------------------------ ! Compute F_n (u) !------------------------------------------------------------------------------ IF(xmhd_adv_den)THEN + u = 0.d0; uvec = 0.d0 !---Compression and advection - u = DOT_PRODUCT(u0%V,u0%dN) + u = u + DOT_PRODUCT(u0%V,u0%dN) u = u + u0%N*divv !---Density diffusion - uvec = d_dens*u0%dN + uvec = diff_tmp*u0%dN IF((self%dt>0.d0).OR.xmhd_diss_centered)THEN c1 = 1.d0 IF(.NOT.xmhd_diss_centered)c1 = 2.d0 - uvec = uvec - d2_dens*u0%dN2 + uvec = uvec - c1*d2_dens*u0%dN2 END IF !---Upwinding IF(xmhd_upwind)THEN - c1 = xmhd_upwind_weight(vad_mag,he,d_dens) + c1 = xmhd_upwind_weight(vad_mag,he,diff_tmp) uvec = uvec + vad*c1*DOT_PRODUCT(u0%V,u0%dN) END IF IF(neg_source(1,i)>0.d0)u = u - neg_source(1,i)*den_floor/ABS(4.d0*self%dt) !---Mass and scale - uvec = uvec*self%dt*det/den_scale - u = (u*self%dt + floor_tmp(1))*det/den_scale + uvec = uvec*self%dt*det + u = (u*self%dt + floor_tmp(1))*det !---Add contributions - !$omp simd do jr=1,oft_lagrange%nce - n_loc(jr) = n_loc(jr) + lag_rop(jr)*u & - + lag_gopt(jr,1)*uvec(1) + lag_gopt(jr,2)*uvec(2) + lag_gopt(jr,3)*uvec(3) + lag_loc(jr,4) = lag_loc(jr,4) + lag_rop(jr)*u & + + DOT_PRODUCT(lag_gop(:,jr),uvec) end do !---Hyper-diffusivity IF((self%dt>0.d0).AND.(n2_ind>0))THEN - u = u0%N2*det/n2_scale - uvec = u0%dN*det/n2_scale + u = u0%N2*det + uvec = u0%dN*det do jr=1,oft_lagrange%nce n2_loc(jr) = n2_loc(jr) + lag_rop(jr)*u & + DOT_PRODUCT(lag_gop(:,jr),uvec) @@ -3070,26 +3452,24 @@ subroutine xmhd_errmatrix_apply(self,a,b) IF(.NOT.xmhd_two_temp)c1=c1/(1.d0+te_factor) SELECT CASE(visc_itype) CASE(1) - u = u - nu_tmp(1)*c1*SUM(SUM(u0%dV*u0%dV,DIM=1)) + u = u - nu_tmp(2)*c1*SUM(SUM(u0%dV*u0%dV,DIM=1)) CASE(2) !--- Nabla(V) + (Nabla(V))^T - u = u - nu_tmp(1)*c1*SUM(SUM(u0%dV*(u0%dV + TRANSPOSE(u0%dV)),DIM=1)) + u = u - nu_tmp(2)*c1*SUM(SUM(u0%dV*(u0%dV + TRANSPOSE(u0%dV)),DIM=1)) !--- - 2/3 I Div(V) - u = u + 2.d0*nu_tmp(1)*c1*(divv**2)/3.d0 - CASE(3) - !--- Nabla(V) + (Nabla(V))^T - 2/3 I Div(V) - umat = TRANSPOSE(u0%dV)+u0%dV - umat(1,1) = umat(1,1)-2.d0*divv/3.d0 - umat(2,2) = umat(2,2)-2.d0*divv/3.d0 - umat(3,3) = umat(3,3)-2.d0*divv/3.d0 - !--- Anisotropic tensor (nu_par*bb + nu_perp*(I-bb)) - do j=1,3 - ten1(:,j) = (nu_tmp(1)-nu_tmp(2))*bhat(j)*bhat - ten1(j,j) = ten1(j,j)+nu_tmp(2) - end do - ten1 = MATMUL(ten1,umat) - u = u - c1*SUM(SUM(u0%dV*ten1,DIM=1)) + u = u + 2.d0*nu_tmp(2)*c1*(divv**2)/3.d0 END SELECT + IF(nu_tmp(1)>0.d0)THEN + !--- W = Nabla(V) + (Nabla(V))^T + umat = u0%dV+TRANSPOSE(u0%dV) + !--- - 2/3 I Div(V) + umat(1,1) = umat(1,1)-2.d0*divv/3.d0 + umat(2,2) = umat(2,2)-2.d0*divv/3.d0 + umat(3,3) = umat(3,3)-2.d0*divv/3.d0 + !--- -Pi_par:Nabla(V) = (3/4) mu_par (b W b)^2 + u = u - 0.75d0*nu_tmp(1)*c1*DOT_PRODUCT(bhat,MATMUL(umat,bhat))**2 + END IF + !--- Gyroviscosity does no work (Pi_gv:Nabla(V) = 0), so it has no heating term END IF !---Electron heat sources (Ohmic, Equilibration) IF(xmhd_two_temp)THEN @@ -3103,18 +3483,9 @@ subroutine xmhd_errmatrix_apply(self,a,b) u = u + divv*u0%Ti*(temp_gamma-1.d0) !---Thermal conduction IF((self%dt>0.d0).OR.xmhd_diss_centered)THEN - IF(xmhd_brag)THEN - IF(xmhd_two_temp)THEN - chi_temp=brag_ion_transport(u0%N,u0%Ti,mu_ion,bmag)/u0%N - ELSE - chi_temp=brag_comb_transport(u0%N,u0%Ti,mu_ion,bmag)/u0%N - END IF - chi_temp(1)=kappa_par*chi_temp(1) - chi_temp(2)=kappa_perp*chi_temp(2) - END IF c1 = 1.d0 IF(.NOT.xmhd_diss_centered)c1 = 2.d0 - uvec = ((chi_temp(1)-chi_temp(2))*bhat*DOT_PRODUCT(bhat,u0%dTi) & + uvec = uvec + ((chi_temp(1)-chi_temp(2))*bhat*DOT_PRODUCT(bhat,u0%dTi) & + chi_temp(2)*u0%dTi)*(temp_gamma-1.d0)*c1 u = u - ((chi_temp(1)-chi_temp(2))*DOT_PRODUCT(u0%dN,bhat)*DOT_PRODUCT(bhat,u0%dTi) & + chi_temp(2)*DOT_PRODUCT(u0%dN,u0%dTi))*(temp_gamma-1.d0)*c1/u0%N @@ -3130,10 +3501,9 @@ subroutine xmhd_errmatrix_apply(self,a,b) uvec = uvec*self%dt*det u = (u*self%dt + floor_tmp(2))*det !---Add contributions - !$omp simd do jr=1,oft_lagrange%nce - t_loc(jr) = t_loc(jr) + lag_rop(jr)*u & - + lag_gopt(jr,1)*uvec(1) + lag_gopt(jr,2)*uvec(2) + lag_gopt(jr,3)*uvec(3) + lag_loc(jr,5) = lag_loc(jr,5) + lag_rop(jr)*u & + + DOT_PRODUCT(lag_gop(:,jr),uvec) end do END IF !------------------------------------------------------------------------------ @@ -3150,17 +3520,12 @@ subroutine xmhd_errmatrix_apply(self,a,b) u = u + (divv+DOT_PRODUCT(u0%J,u0%dN)/(mu0*elec_charge*u0%N**2))*u0%Te*(temp_gamma-1.d0) !---Thermal conduction IF((self%dt>0.d0).OR.xmhd_diss_centered)THEN - IF(xmhd_brag)THEN - chi_temp = brag_elec_transport(u0%N,u0%Te,bmag)/u0%N - chi_temp(1) = kappa_par*chi_temp(1) - chi_temp(2) = MAX(1.d1, kappa_perp*chi_temp(2)) - END IF c1 = 1.d0 IF(.NOT.xmhd_diss_centered)c1 = 2.d0 - uvec = uvec + ((chi_temp(1)-chi_temp(2))*bhat*DOT_PRODUCT(bhat,u0%dTe) & - + chi_temp(2)*u0%dTe)*(temp_gamma-1.d0)*c1 - u = u - ((chi_temp(1)-chi_temp(2))*DOT_PRODUCT(u0%dN,bhat)*DOT_PRODUCT(bhat,u0%dTe) & - + chi_temp(2)*DOT_PRODUCT(u0%dN,u0%dTe))*(temp_gamma-1.d0)*c1/u0%N + uvec = uvec + ((chi_temp(3)-chi_temp(4))*bhat*DOT_PRODUCT(bhat,u0%dTe) & + + chi_temp(4)*u0%dTe)*(temp_gamma-1.d0)*c1 + u = u - ((chi_temp(3)-chi_temp(4))*DOT_PRODUCT(u0%dN,bhat)*DOT_PRODUCT(bhat,u0%dTe) & + + chi_temp(4)*DOT_PRODUCT(u0%dN,u0%dTe))*(temp_gamma-1.d0)*c1/u0%N !---Upwinding IF(xmhd_upwind)THEN IF(back_step)THEN @@ -3169,7 +3534,7 @@ subroutine xmhd_errmatrix_apply(self,a,b) vad = vad-u0%J/(mu0*u0%N*elec_charge) END IF vad_mag = magnitude(vad) - c1 = xmhd_upwind_weight(vad_mag,he,chi_temp(2)*(temp_gamma-1.d0)) + c1 = xmhd_upwind_weight(vad_mag,he,chi_temp(4)*(temp_gamma-1.d0)) IF(.NOT.xmhd_diss_centered)c1 = 2.d0*c1 uvec = uvec + vad*c1*DOT_PRODUCT(u0%V-u0%J/(mu0*u0%N*elec_charge),u0%dTe) END IF @@ -3181,7 +3546,7 @@ subroutine xmhd_errmatrix_apply(self,a,b) !---Add contributions do jr=1,oft_lagrange%nce te_loc(jr) = te_loc(jr) + lag_rop(jr)*u & - + lag_gopt(jr,1)*uvec(1) + lag_gopt(jr,2)*uvec(2) + lag_gopt(jr,3)*uvec(3) + + DOT_PRODUCT(lag_gop(:,jr),uvec) end do END IF !------------------------------------------------------------------------------ @@ -3192,38 +3557,35 @@ subroutine xmhd_errmatrix_apply(self,a,b) ! Add local contributions !------------------------------------------------------------------------------ !$omp critical(xmhderr_red) - !---Lagrange rows (B,V,n,T) - IF(solid_cell(i))THEN + !---B rows + IF(xmhd_adv_b)THEN DO jr=1,oft_lagrange%nce - bout(:,j_lag(jr)) = bout(:,j_lag(jr)) + b_loc(jr,:) + bout(j_lag(jr),:) = bout(j_lag(jr),:) + b_loc(jr,:) END DO - ELSE + IF(j2_ind>0)THEN + do jr=1,oft_hcurl%nce + j2out(j_hcurl(jr)) = j2out(j_hcurl(jr)) + j2_loc(jr)/j2_scale + end do + END IF + END IF + !---Lagrange rows (V,n,T) + IF(.NOT.reg_solid(mesh%reg(i)))THEN DO jr=1,oft_lagrange%nce - bout(:,j_lag(jr)) = bout(:,j_lag(jr)) + b_loc(jr,:) - vout(:,j_lag(jr)) = vout(:,j_lag(jr)) + v_loc(jr,:) - IF(xmhd_adv_den)THEN - nout(j_lag(jr)) = nout(j_lag(jr)) + n_loc(jr) - IF(n2_ind>0)n2out(j_lag(jr)) = n2out(j_lag(jr)) + n2_loc(jr) - END IF + vout(j_lag(jr),:) = vout(j_lag(jr),:) + lag_loc(jr,1:3)/vel_scale + IF(xmhd_adv_den)nout(j_lag(jr)) = nout(j_lag(jr)) + lag_loc(jr,4)/den_scale IF(xmhd_adv_temp)THEN - tout(j_lag(jr)) = tout(j_lag(jr)) + t_loc(jr) + tout(j_lag(jr)) = tout(j_lag(jr)) + lag_loc(jr,5) IF(xmhd_two_temp)teout(j_lag(jr)) = teout(j_lag(jr)) + te_loc(jr) END IF + IF(n2_ind>0)n2out(j_lag(jr)) = n2out(j_lag(jr)) + n2_loc(jr)/n2_scale END DO - IF(j2_ind>0)THEN - DO jr=1,oft_hcurl%nce - j2out(j_hcurl(jr)) = j2out(j_hcurl(jr)) + j2_loc(jr) - END DO - END IF END IF !$omp end critical(xmhderr_red) end do end do !---Delete temporary matrices -deallocate(j_lag) -deallocate(lag_rop,lag_gop,lag_gopt) -deallocate(b_rop,b_cop) -deallocate(b_loc,v_loc,n_loc,t_loc) +deallocate(j_lag,lag_rop,lag_gop) +deallocate(b_loc,lag_loc) IF(xmhd_two_temp)deallocate(te_loc) IF(n2_ind>0)DEALLOCATE(n2_loc) IF(j2_ind>0)DEALLOCATE(j2_loc,j_hcurl,hcurl_rop,hcurl_cop) @@ -3237,55 +3599,21 @@ subroutine xmhd_errmatrix_apply(self,a,b) CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,7),nout,oft_xmhd_ops%n_bc) CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,8),tout,oft_xmhd_ops%t_bc) IF(xmhd_two_temp)CALL fem_dirichlet_vec(oft_lagrange,full_interp%Te_loc,teout,oft_xmhd_ops%t_bc) -!---Magnetic BC -IF(xmhd_adv_b)THEN - !$omp parallel do private(j,mloc) - DO i=1,oft_lagrange%nbe - j=oft_lagrange%lbe(i) - IF(.NOT.oft_lagrange%global%gbe(j))CYCLE - CALL lag_vbc_tensor(oft_lagrange,j,2,mloc) - bout(:,j)=MATMUL(mloc,bout(:,j)) - IF(.NOT.oft_lagrange%linkage%leo(i))CYCLE - CALL lag_vbc_diag(oft_lagrange,j,2,mloc) - bout(:,j)=bout(:,j)+MATMUL(mloc,full_interp%lf_loc(j,1:3)) - END DO -ELSE - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,1),bout(1,:),oft_xmhd_ops%b_bc) - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,2),bout(2,:),oft_xmhd_ops%b_bc) - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,3),bout(3,:),oft_xmhd_ops%b_bc) -END IF -!---Velocity BC -IF(xmhd_vbcdir)THEN - !$omp parallel do private(j,mloc) - DO i=1,oft_lagrange%nbe - j=oft_lagrange%lbe(i) - IF(.NOT.oft_lagrange%global%gbe(j))CYCLE - CALL lag_vbc_tensor(oft_lagrange,j,vbc_type,mloc) - vout(:,j)=MATMUL(mloc,vout(:,j)) - IF(.NOT.oft_lagrange%linkage%leo(i))CYCLE - CALL lag_vbc_diag(oft_lagrange,j,vbc_type,mloc) - vout(:,j)=vout(:,j)+MATMUL(mloc,full_interp%lf_loc(j,4:6)) - END DO -ELSE - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,4),vout(1,:),oft_xmhd_ops%v_bc) - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,5),vout(2,:),oft_xmhd_ops%v_bc) - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,6),vout(3,:),oft_xmhd_ops%v_bc) -END IF +CALL xmhd_vec_bc_vec(full_interp%lf_loc(:,1:3),bout,oft_xmhd_ops%b_bc_type,xmhd_bbcdir) +CALL xmhd_vec_bc_vec(full_interp%lf_loc(:,4:6),vout,oft_xmhd_ops%v_bc_type,xmhd_vbcdir) !------------------------------------------------------------------------------ ! Set result from local field values, summing contributions across seams !------------------------------------------------------------------------------ -vtmp=>bout(1,:) -CALL b%restore_local(vtmp,1,add=.TRUE.,wait=.TRUE.) -vtmp=>bout(2,:) -CALL b%restore_local(vtmp,2,add=.TRUE.,wait=.TRUE.) -vtmp=>bout(3,:) -CALL b%restore_local(vtmp,3,add=.TRUE.,wait=.TRUE.) -vtmp=>vout(1,:) -CALL b%restore_local(vtmp,4,add=.TRUE.,wait=.TRUE.) -vtmp=>vout(2,:) -CALL b%restore_local(vtmp,5,add=.TRUE.,wait=.TRUE.) -vtmp=>vout(3,:) -CALL b%restore_local(vtmp,6,add=.TRUE.,wait=.TRUE.) +CALL lagrange_vector_repack(oft_lagrange,bout,.TRUE.) +CALL lagrange_vector_repack(oft_lagrange,vout,.TRUE.) +DO li=1,3 + vtmp=>bout(:,li) + CALL b%restore_local(vtmp,li,add=.TRUE.,wait=.TRUE.) +END DO +DO li=1,3 + vtmp=>vout(:,li) + CALL b%restore_local(vtmp,li+3,add=.TRUE.,wait=.TRUE.) +END DO CALL b%restore_local(nout,7,add=.TRUE.,wait=.TRUE.) CALL b%restore_local(tout,8,add=.TRUE.,wait=(8/=xmhd_rep%nfields)) IF(xmhd_two_temp)CALL b%restore_local(teout,te_ind,add=.TRUE.,wait=(te_ind/=xmhd_rep%nfields)) @@ -3304,7 +3632,7 @@ subroutine xmhd_errmatrix_apply(self,a,b) IF(n2_ind>0)DEALLOCATE(n2out) IF(j2_ind>0)DEALLOCATE(j2out) IF(back_step)THEN - DEALLOCATE(jpin,npin) + DEALLOCATE(bpin,npin) IF(xmhd_upwind)DEALLOCATE(vpin) END IF CALL full_interp%delete @@ -3317,6 +3645,38 @@ subroutine xmhd_errmatrix_apply(self,a,b) DEBUG_STACK_POP end subroutine xmhd_errmatrix_apply !------------------------------------------------------------------------------ +!> Apply boundary conditions to the local residual of a vector Lagrange field (B or V) +!! +!! Projected BCs (`bc_type>1`) replace the constrained part of the residual with the +!! deviation from the current field (on owned nodes), while Dirichlet BCs (`bc_type==1` +!! for all components or `bc_type==-1` for the components normal to the axis) are applied +!! component wise (see \ref xmhd_lag::xmhd_set_bc "xmhd_set_bc"). +!------------------------------------------------------------------------------ +subroutine xmhd_vec_bc_vec(uloc,vout,bc_type,proj_bcs) +real(r8), intent(inout) :: uloc(:,:) !< Current local field values [ne,3] +real(r8), intent(inout) :: vout(:,:) !< Local residual [ne,3] +integer(i4), intent(in) :: bc_type(:) !< BC type for each Lagrange node +logical, intent(in) :: proj_bcs !< Projected BCs present? +integer(i4) :: i,j +real(r8) :: mloc(3,3) +IF(proj_bcs)THEN + !$omp parallel do private(j,mloc) + DO i=1,oft_lagrange%nbe + j=oft_lagrange%lbe(i) + IF(bc_type(j)<=1)CYCLE + IF(.NOT.oft_lagrange%global%gbe(j))CYCLE + CALL lag_vbc_tensor(oft_lagrange,j,bc_type(j),mloc) + vout(j,:)=MATMUL(mloc,vout(j,:)) + IF(.NOT.oft_lagrange%linkage%leo(i))CYCLE + CALL lag_vbc_diag(oft_lagrange,j,bc_type(j),mloc) + vout(j,:)=vout(j,:)+MATMUL(mloc,uloc(j,1:3)) + END DO +END IF +CALL fem_dirichlet_vec(oft_lagrange,uloc(:,1),vout(:,1),(bc_type==1)) +CALL fem_dirichlet_vec(oft_lagrange,uloc(:,2),vout(:,2),(ABS(bc_type)==1)) +CALL fem_dirichlet_vec(oft_lagrange,uloc(:,3),vout(:,3),(ABS(bc_type)==1)) +end subroutine xmhd_vec_bc_vec +!------------------------------------------------------------------------------ !> Compute the mass matrix for solution field !! !! b = M(a) @@ -3325,15 +3685,15 @@ subroutine xmhd_massmatrix_apply(self,a,b) class(oft_xmhd_massmatrix), intent(inout) :: self !< Mass matrix object class(oft_vector), target, intent(inout) :: a !< Source field class(oft_vector), intent(inout) :: b !< Result of metric function -real(r8), allocatable :: b_loc(:,:),v_loc(:,:),n_loc(:),t_loc(:),te_loc(:) +real(r8), allocatable :: b_loc(:,:),te_loc(:),lag_loc(:,:) integer(i4) :: i,ii,ip,j,m,jr,jc,jp,ptind(2),li integer(i4), allocatable :: j_lag(:) -real(r8), allocatable, target :: lag_rop(:),lag_gop(:,:),hcurl_rop(:,:),hcurl_cop(:,:),b_cop(:,:,:) +real(r8), target, allocatable :: lag_rop(:),lag_gop(:,:) real(r8), pointer, contiguous, dimension(:) :: neqin,nout,tout,teout real(r8), pointer, contiguous, dimension(:,:) :: vout,bout real(r8), pointer, dimension(:) :: vtmp real(r8) :: mloc(3,3),goptmp(3,4),vol,f,det,fulltmp(41) -real(r8) :: bhat(3),neq,diag_vals(8),pt(3),pt_r +real(r8) :: neq,diag_vals(8),pt(3),pt_r logical :: curved,neq_present type(oft_quad_type), pointer :: quad TYPE(xmhd_interp) :: full_interp @@ -3358,20 +3718,15 @@ subroutine xmhd_massmatrix_apply(self,a,b) !------------------------------------------------------------------------------ NULLIFY(nout,tout,teout) CALL b%set(0.d0) -ALLOCATE(bout(3,oft_lagrange%ne)) -vtmp=>bout(1,:) -CALL b%get_local(vtmp,1) -vtmp=>bout(2,:) -CALL b%get_local(vtmp,2) -vtmp=>bout(3,:) -CALL b%get_local(vtmp,3) -ALLOCATE(vout(3,oft_lagrange%ne)) -vtmp=>vout(1,:) -CALL b%get_local(vtmp,4) -vtmp=>vout(2,:) -CALL b%get_local(vtmp,5) -vtmp=>vout(3,:) -CALL b%get_local(vtmp,6) +ALLOCATE(bout(oft_lagrange%ne,3),vout(oft_lagrange%ne,3)) +DO li=1,3 + vtmp=>bout(:,li) + CALL b%get_local(vtmp,li) + vtmp=>vout(:,li) + CALL b%get_local(vtmp,li+3) +END DO +CALL lagrange_vector_unpack(oft_lagrange,bout,.TRUE.) +CALL lagrange_vector_unpack(oft_lagrange,vout,.TRUE.) CALL b%get_local(nout,7) CALL b%get_local(tout,8) IF(xmhd_two_temp)CALL b%get_local(teout,te_ind) @@ -3380,30 +3735,21 @@ subroutine xmhd_massmatrix_apply(self,a,b) ptind=(/1,2/) IF(xmhd_taxis==1)ptind(1)=3 IF(xmhd_taxis==2)ptind(2)=3 -! -IF(j2_ind>0)THEN - ALLOCATE(hcurl_rop(3,oft_hcurl%nce),hcurl_cop(3,oft_hcurl%nce)) - hcurl_rop=0.d0 - hcurl_cop=0.d0 -END IF !------------------------------------------------------------------------------ ! Apply metric function !------------------------------------------------------------------------------ -!$omp parallel private(det,j_lag,lag_rop,lag_gop,li,b_cop,curved,goptmp, & -!$omp vol,j,m,jr,bhat,neq,pt,pt_r,u0,fulltmp,b_loc,v_loc,n_loc,t_loc, & -!$omp te_loc,i,ii) reduction(+:diag_vals) +!$omp parallel private(det,j_lag,lag_rop,lag_gop,u0,curved,goptmp,vol,j,m,jr,neq,pt,pt_r, & +!$omp b_loc,te_loc,lag_loc,i,ii,fulltmp,mloc) reduction(+:diag_vals) allocate(j_lag(oft_lagrange%nce)) allocate(lag_rop(oft_lagrange%nce),lag_gop(3,oft_lagrange%nce)) -allocate(b_loc(3,oft_lagrange%nce)) -allocate(v_loc(3,oft_lagrange%nce),n_loc(oft_lagrange%nce),t_loc(oft_lagrange%nce)) +allocate(b_loc(oft_lagrange%nce,3),lag_loc(oft_lagrange%nce,5)) IF(xmhd_two_temp)allocate(te_loc(oft_lagrange%nce)) -allocate(b_cop(3,oft_lagrange%nce,3)) !---Setup caching xmhd_lag_rop=>lag_rop xmhd_lag_gop=>lag_gop IF(j2_ind>0)THEN - xmhd_hcurl_rop=>hcurl_rop - xmhd_hcurl_cop=>hcurl_cop + ALLOCATE(xmhd_hcurl_rop(3,oft_hcurl%nce),xmhd_hcurl_cop(3,oft_hcurl%nce)) + xmhd_hcurl_rop=0.d0; xmhd_hcurl_cop=0.d0 END IF !---Loop over cells !$omp do schedule(static) @@ -3413,11 +3759,12 @@ subroutine xmhd_massmatrix_apply(self,a,b) curved=cell_is_curved(mesh,i) ! Straight cell test !---Get local to global DOF mapping call oft_lagrange%ncdofs(i,j_lag) + !---Init local entries + b_loc=0.d0; lag_loc=0.d0 + IF(xmhd_two_temp)te_loc=0.d0 !------------------------------------------------------------------------------ ! Quadrature Loop !------------------------------------------------------------------------------ - b_loc=0.d0; v_loc=0.d0; n_loc=0.d0; t_loc=0.d0 - IF(xmhd_two_temp)te_loc=0.d0 DO m=1,quad%np !------------------------------------------------------------------------------ ! Get local reconstructed operators @@ -3431,13 +3778,6 @@ subroutine xmhd_massmatrix_apply(self,a,b) ! call full_interp%interp(i,quad%pts(:,m),goptmp,fulltmp) CALL xmhd_interp_unpack(fulltmp,u0) - b_cop=0.d0 - b_cop(1,:,3)=lag_gop(2,:) - b_cop(1,:,2)=-lag_gop(3,:) - b_cop(2,:,1)=lag_gop(3,:) - b_cop(2,:,3)=-lag_gop(1,:) - b_cop(3,:,2)=lag_gop(1,:) - b_cop(3,:,1)=-lag_gop(2,:) !---Compute diagnostics pt_r = MAX(1.d-10,SUM(pt(ptind)**2)) diag_vals(1) = diag_vals(1) + SUM(u0%B**2)*det @@ -3453,36 +3793,33 @@ subroutine xmhd_massmatrix_apply(self,a,b) neq = neq*den_scale diag_vals(2) = diag_vals(2) + neq*SUM(u0%V**2)*det ELSE + neq=u0%N diag_vals(2) = diag_vals(2) + u0%N*SUM(u0%V**2)*det END IF diag_vals(5) = diag_vals(5) + u0%N*det diag_vals(6) = diag_vals(6) + u0%Ti*det - diag_vals(7) = diag_vals(7) + (u0%divb**2)*det - diag_vals(8) = diag_vals(8) + u0%Te*det + diag_vals(7) = diag_vals(7) + u0%Te*det + diag_vals(8) = diag_vals(8) + (u0%divB**2)*det !------------------------------------------------------------------------------ ! Compute mass matrix !------------------------------------------------------------------------------ - IF(solid_cell(i))THEN - do jr=1,oft_lagrange%nce - b_loc(:,jr) = b_loc(:,jr) + lag_rop(jr)*u0%B*det - end do - CYCLE - END IF + !---Compute M_b (u) + do jr=1,oft_lagrange%nce + b_loc(jr,:) = b_loc(jr,:) + lag_rop(jr)*u0%B*det + end do !---Electron inertia - IF(xmhd_hall)THEN - u0%J=u0%J*me_factor*elec_mass*det/(mu0*neq*den_scale*elec_charge**2) - do li=1,3 + IF(xmhd_hall.AND.(.NOT.reg_solid(mesh%reg(i))))THEN + u0%J=u0%J*me_factor*elec_mass*det/(mu0*neq*elec_charge**2) do jr=1,oft_lagrange%nce - b_loc(li,jr) = b_loc(li,jr) + DOT_PRODUCT(b_cop(:,jr,li),u0%J) - end do + b_loc(jr,:) = b_loc(jr,:) + cross_product(u0%J,lag_gop(:,jr)) end do END IF + IF(reg_solid(mesh%reg(i)))CYCLE !---Compute F_{V,n,T) (u) do jr=1,oft_lagrange%nce - b_loc(:,jr) = b_loc(:,jr) + lag_rop(jr)*u0%B*det - v_loc(:,jr) = v_loc(:,jr) + lag_rop(jr)*u0%V*det/vel_scale - IF(xmhd_adv_den)n_loc(jr) = n_loc(jr) + lag_rop(jr)*u0%N*det/den_scale - IF(xmhd_adv_temp)t_loc(jr) = t_loc(jr) + lag_rop(jr)*u0%Ti*det + lag_loc(jr,1:3) = lag_loc(jr,1:3) + lag_rop(jr)*u0%V*det + IF(xmhd_adv_den)lag_loc(jr,4) = lag_loc(jr,4) + lag_rop(jr)*u0%N*det + IF(xmhd_adv_temp)lag_loc(jr,5) = lag_loc(jr,5) + lag_rop(jr)*u0%Ti*det IF(xmhd_two_temp)te_loc(jr) = te_loc(jr) + lag_rop(jr)*u0%Te*det end do !------------------------------------------------------------------------------ @@ -3493,25 +3830,23 @@ subroutine xmhd_massmatrix_apply(self,a,b) !------------------------------------------------------------------------------ ! Add local contributions !------------------------------------------------------------------------------ - !---Lagrange rows (B,V,n,T) do jr=1,oft_lagrange%nce - bout(:,j_lag(jr)) = bout(:,j_lag(jr)) + b_loc(:,jr) - vout(:,j_lag(jr)) = vout(:,j_lag(jr)) + v_loc(:,jr) - IF(xmhd_adv_den)nout(j_lag(jr)) = nout(j_lag(jr)) + n_loc(jr) - IF(xmhd_adv_temp)tout(j_lag(jr)) = tout(j_lag(jr)) + t_loc(jr) + bout(j_lag(jr),:) = bout(j_lag(jr),:) + b_loc(jr,:) + vout(j_lag(jr),:) = vout(j_lag(jr),:) + lag_loc(jr,1:3)/vel_scale + IF(xmhd_adv_den)nout(j_lag(jr)) = nout(j_lag(jr)) + lag_loc(jr,4)/den_scale + IF(xmhd_adv_temp)tout(j_lag(jr)) = tout(j_lag(jr)) + lag_loc(jr,5) IF(xmhd_two_temp)teout(j_lag(jr)) = teout(j_lag(jr)) + te_loc(jr) end do !$omp end critical(xmhderr_red) end do end do !---Delete temporary matrices -deallocate(j_lag) -deallocate(lag_rop,lag_gop,b_cop) -deallocate(b_loc,v_loc,n_loc,t_loc) +deallocate(j_lag,lag_rop,lag_gop) +deallocate(b_loc,lag_loc) IF(xmhd_two_temp)deallocate(te_loc) +IF(j2_ind>0)DEALLOCATE(xmhd_hcurl_rop,xmhd_hcurl_cop) NULLIFY(xmhd_lag_rop,xmhd_lag_gop,xmhd_hcurl_rop,xmhd_hcurl_cop) !$omp end parallel -IF(j2_ind>0)deallocate(hcurl_rop,hcurl_cop) !------------------------------------------------------------------------------ ! Boundary conditions !------------------------------------------------------------------------------ @@ -3519,73 +3854,41 @@ subroutine xmhd_massmatrix_apply(self,a,b) CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,7),nout,oft_xmhd_ops%n_bc) CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,8),tout,oft_xmhd_ops%t_bc) IF(xmhd_two_temp)CALL fem_dirichlet_vec(oft_lagrange,full_interp%Te_loc,teout,oft_xmhd_ops%t_bc) -!---Magnetic BC -IF(xmhd_adv_b)THEN - !$omp parallel do private(j,mloc) - DO i=1,oft_lagrange%nbe - j=oft_lagrange%lbe(i) - IF(.NOT.oft_lagrange%global%gbe(j))CYCLE - CALL lag_vbc_tensor(oft_lagrange,j,2,mloc) - bout(:,j)=MATMUL(mloc,bout(:,j)) - IF(.NOT.oft_lagrange%linkage%leo(i))CYCLE - CALL lag_vbc_diag(oft_lagrange,j,2,mloc) - bout(:,j)=bout(:,j)+MATMUL(mloc,full_interp%lf_loc(j,1:3)) - END DO -ELSE - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,1),bout(1,:),oft_xmhd_ops%b_bc) - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,2),bout(2,:),oft_xmhd_ops%b_bc) - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,3),bout(3,:),oft_xmhd_ops%b_bc) -END IF -!---Velocity BC -IF(xmhd_vbcdir)THEN - !---Copy values for boundary terms - !$omp parallel do private(j,mloc) - DO i=1,oft_lagrange%nbe - j=oft_lagrange%lbe(i) - IF(.NOT.oft_lagrange%global%gbe(j))CYCLE - CALL lag_vbc_tensor(oft_lagrange,j,vbc_type,mloc) - vout(:,j)=MATMUL(mloc,vout(:,j)) - IF(.NOT.oft_lagrange%linkage%leo(i))CYCLE - CALL lag_vbc_diag(oft_lagrange,j,vbc_type,mloc) - vout(:,j)=vout(:,j)+MATMUL(mloc,full_interp%lf_loc(j,4:6)) - END DO -ELSE - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,4),vout(1,:),oft_xmhd_ops%v_bc) - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,5),vout(2,:),oft_xmhd_ops%v_bc) - CALL fem_dirichlet_vec(oft_lagrange,full_interp%lf_loc(:,6),vout(3,:),oft_xmhd_ops%v_bc) -END IF +CALL xmhd_vec_bc_vec(full_interp%lf_loc(:,1:3),bout,oft_xmhd_ops%b_bc_type,xmhd_bbcdir) +CALL xmhd_vec_bc_vec(full_interp%lf_loc(:,4:6),vout,oft_xmhd_ops%v_bc_type,xmhd_vbcdir) !------------------------------------------------------------------------------ ! Set result from local field values, summing contributions across seams !------------------------------------------------------------------------------ -vtmp=>bout(1,:) -CALL b%restore_local(vtmp,1,add=.TRUE.,wait=.TRUE.) -vtmp=>bout(2,:) -CALL b%restore_local(vtmp,2,add=.TRUE.,wait=.TRUE.) -vtmp=>bout(3,:) -CALL b%restore_local(vtmp,3,add=.TRUE.,wait=.TRUE.) -vtmp=>vout(1,:) -CALL b%restore_local(vtmp,4,add=.TRUE.,wait=.TRUE.) -vtmp=>vout(2,:) -CALL b%restore_local(vtmp,5,add=.TRUE.,wait=.TRUE.) -vtmp=>vout(3,:) -CALL b%restore_local(vtmp,6,add=.TRUE.,wait=.TRUE.) +CALL lagrange_vector_repack(oft_lagrange,bout,.TRUE.) +CALL lagrange_vector_repack(oft_lagrange,vout,.TRUE.) +DO li=1,3 + vtmp=>bout(:,li) + CALL b%restore_local(vtmp,li,add=.TRUE.,wait=.TRUE.) +END DO +DO li=1,3 + vtmp=>vout(:,li) + CALL b%restore_local(vtmp,li+3,add=.TRUE.,wait=.TRUE.) +END DO CALL b%restore_local(nout,7,add=.TRUE.,wait=.TRUE.) CALL b%restore_local(tout,8,add=.TRUE.,wait=(8/=xmhd_rep%nfields)) IF(xmhd_two_temp)CALL b%restore_local(teout,te_ind,add=.TRUE.,wait=(te_ind/=xmhd_rep%nfields)) -tout=0.d0 -IF(n2_ind>0)CALL b%restore_local(tout,n2_ind,add=.TRUE.,wait=(n2_ind/=xmhd_rep%nfields)) -IF(j2_ind>0)CALL b%restore_local(tout,j2_ind,add=.TRUE.,wait=(j2_ind/=xmhd_rep%nfields)) +nout=0.d0 +IF(n2_ind>0)CALL b%restore_local(nout,n2_ind,add=.TRUE.,wait=(n2_ind/=xmhd_rep%nfields)) +IF(j2_ind>0)THEN + ALLOCATE(vtmp(oft_hcurl%ne)) + vtmp=0.d0 + CALL b%restore_local(vtmp,j2_ind,add=.TRUE.,wait=(j2_ind/=xmhd_rep%nfields)) + DEALLOCATE(vtmp) +END IF !---Pack diagnostics -diag_vals(2)=diag_vals(2) diag_vals(3:4)=diag_vals(3:4)/(2*pi) -diag_vals(5)=diag_vals(5) self%diag_vals=oft_mpi_sum(diag_vals,8) !------------------------------------------------------------------------------ ! Destroy worker arrays !------------------------------------------------------------------------------ DEALLOCATE(bout,vout,nout,tout) -IF(xmhd_two_temp)DEALLOCATE(teout) IF(ASSOCIATED(neqin))DEALLOCATE(neqin) +IF(xmhd_two_temp)DEALLOCATE(teout) CALL full_interp%delete DEBUG_STACK_POP end subroutine xmhd_massmatrix_apply @@ -3594,30 +3897,27 @@ end subroutine xmhd_massmatrix_apply !------------------------------------------------------------------------------ subroutine xmhd_diag(u,diag_vals,ueq) class(oft_vector), target, intent(inout) :: u !< Field for diagnostic evaluation -real(r8), intent(out) :: diag_vals(8) !< Resulting diagnostic values [8] +real(r8), intent(out) :: diag_vals(8) !< Resulting diagnostic values [8] class(oft_vector), optional, intent(inout) :: ueq !< Equilibrium field (linear calculation) -real(r8), allocatable :: vin_loc(:,:) -integer(i4) :: i,ii,ip,j,m,jr,jc,jp,ptind(2),li +integer(i4) :: i,ii,ip,j,m,jr,jc,jp,ptind(2) integer(i4), allocatable :: j_lag(:) -real(r8), allocatable, target :: lag_rop(:),lag_gop(:,:),hcurl_rop(:,:),hcurl_cop(:,:) -real(r8), pointer, contiguous, dimension(:) :: neqin,nout,tout -real(r8), pointer, contiguous, dimension(:,:) :: vout,bout -real(r8), pointer, dimension(:) :: vtmp +real(r8), target, allocatable :: lag_rop(:),lag_gop(:,:) +real(r8), pointer, contiguous, dimension(:) :: neqin real(r8) :: mloc(3,3),goptmp(3,4),vol,f,det,fulltmp(41) -real(r8) :: bhat(3),neq,pt(3),pt_r +real(r8) :: neq,pt(3),pt_r logical :: curved,neq_present type(oft_quad_type), pointer :: quad TYPE(xmhd_interp) :: full_interp type(xmhd_loc_values) :: u0 DEBUG_STACK_PUSH -IF(oft_debug_print(1))write(*,'(2X,A)')'Computing xMHD diagnostics' +IF(oft_debug_print(1))write(*,'(2X,A)')'Compute xMHD diagnostics' quad=>oft_lagrange%quad !------------------------------------------------------------------------------ ! Get local field values from source !------------------------------------------------------------------------------ -NULLIFY(neqin) full_interp%u=>u CALL full_interp%setup(mesh) +NULLIFY(neqin) IF(PRESENT(ueq))THEN neq_present=.TRUE. CALL ueq%get_local(neqin,7) @@ -3629,25 +3929,19 @@ subroutine xmhd_diag(u,diag_vals,ueq) ptind=(/1,2/) IF(xmhd_taxis==1)ptind(1)=3 IF(xmhd_taxis==2)ptind(2)=3 -! -IF(j2_ind>0)THEN - ALLOCATE(hcurl_rop(3,oft_hcurl%nce),hcurl_cop(3,oft_hcurl%nce)) - hcurl_rop=0.d0 - hcurl_cop=0.d0 -END IF !------------------------------------------------------------------------------ ! Apply metric function !------------------------------------------------------------------------------ -!$omp parallel private(det,j_lag,lag_rop,lag_gop,li,curved,goptmp,vol, & -!$omp j,m,jr,bhat,neq,pt,pt_r,u0,fulltmp,i,ii) reduction(+:diag_vals) +!$omp parallel private(det,j_lag,lag_rop,lag_gop,curved,goptmp,vol,j,m,jr,neq,pt,pt_r, & +!$omp u0,i,ii,fulltmp,mloc) reduction(+:diag_vals) allocate(j_lag(oft_lagrange%nce)) allocate(lag_rop(oft_lagrange%nce),lag_gop(3,oft_lagrange%nce)) !---Setup caching xmhd_lag_rop=>lag_rop xmhd_lag_gop=>lag_gop IF(j2_ind>0)THEN - xmhd_hcurl_rop=>hcurl_rop - xmhd_hcurl_cop=>hcurl_cop + ALLOCATE(xmhd_hcurl_rop(3,oft_hcurl%nce),xmhd_hcurl_cop(3,oft_hcurl%nce)) + xmhd_hcurl_rop=0.d0; xmhd_hcurl_cop=0.d0 END IF !---Loop over cells !$omp do schedule(static) @@ -3661,9 +3955,6 @@ subroutine xmhd_diag(u,diag_vals,ueq) ! Quadrature Loop !------------------------------------------------------------------------------ DO m=1,quad%np -!------------------------------------------------------------------------------ -! Get local reconstructed operators -!------------------------------------------------------------------------------ if(curved.OR.m==1)call mesh%jacobian(i,quad%pts(:,m),goptmp,vol) pt=mesh%log2phys(i,quad%pts(:,m)) det=vol*quad%wts(m) @@ -3692,24 +3983,18 @@ subroutine xmhd_diag(u,diag_vals,ueq) END IF diag_vals(5) = diag_vals(5) + u0%N*det diag_vals(6) = diag_vals(6) + u0%Ti*det - diag_vals(7) = diag_vals(7) + (u0%divB**2)*det - diag_vals(8) = diag_vals(8) + u0%Te*det -!------------------------------------------------------------------------------ -! End quadrature loop -!------------------------------------------------------------------------------ - end do + diag_vals(7) = diag_vals(7) + u0%Te*det + diag_vals(8) = diag_vals(8) + (u0%divB**2)*det + END DO end do end do !---Delete temporary matrices -deallocate(j_lag) -deallocate(lag_rop,lag_gop) -NULLIFY(xmhd_lag_rop,xmhd_lag_gop) +deallocate(j_lag,lag_rop,lag_gop) +IF(j2_ind>0)DEALLOCATE(xmhd_hcurl_rop,xmhd_hcurl_cop) +NULLIFY(xmhd_lag_rop,xmhd_lag_gop,xmhd_hcurl_rop,xmhd_hcurl_cop) !$omp end parallel -IF(j2_ind>0)DEALLOCATE(hcurl_rop,hcurl_cop) !---Pack diagnostics -diag_vals(2)=diag_vals(2) diag_vals(3:4)=diag_vals(3:4)/(2*pi) -diag_vals(5)=diag_vals(5) diag_vals=oft_mpi_sum(diag_vals,8) !------------------------------------------------------------------------------ ! Destroy worker arrays @@ -3735,7 +4020,7 @@ subroutine xmhd_setup_rep n2_ind=ML_xmhd_rep%nfields END IF IF(eta_hyper>0.d0)THEN - IF(xmhd_ML_hcurl%nlevels==0)CALL oft_abort("Hyper-resistivity requires HCurl FE space", & + IF(.NOT.ASSOCIATED(xmhd_ML_hcurl))CALL oft_abort("Hyper-resistivity requires HCurl FE space", & "xmhd_setup_rep", __FILE__) ML_xmhd_rep%nfields=ML_xmhd_rep%nfields+1 j2_ind=ML_xmhd_rep%nfields @@ -3770,37 +4055,36 @@ subroutine xmhd_setup_rep ML_xmhd_rep%ml_fields(j2_ind)%ml=>xmhd_ML_hcurl ML_xmhd_rep%field_tags(j2_ind)='j2' END IF -call ML_xmhd_rep%setup +call ML_xmhd_rep%setup() xmhd_rep=>ML_xmhd_rep%current_level !---Declare legacy variables xmhd_blevel=xmhd_ML_lagrange%blevel xmhd_nlevels=xmhd_ML_lagrange%nlevels xmhd_level=xmhd_ML_lagrange%level +IF(xmhd_minlev<0)xmhd_minlev=xmhd_nlevels +IF(j2_ind>0)THEN + IF(.NOT.oft_3D_hcurl_cast(oft_hcurl,xmhd_ML_hcurl%current_level))CALL oft_abort("Invalid Curl FE object","xmhd_setup_rep",__FILE__) +END IF end subroutine xmhd_setup_rep !------------------------------------------------------------------------------ -!> Set the current level for xMHD +!> Set the current level for xMHD model !------------------------------------------------------------------------------ subroutine xmhd_set_level(level) integer(i4), intent(in) :: level !< Desired level if(level>xmhd_ML_lagrange%nlevels.OR.level<=0)then call oft_abort('Invalid FE level','xmhd_set_level',__FILE__) end if -! if(levelML_xmhd_rep%current_level !--- CALL xmhd_ML_lagrange%set_level(level) -IF(.NOT.oft_3D_lagrange_cast(oft_lagrange,xmhd_ML_lagrange%current_level))CALL oft_abort("Invalid lagrange FE object","xmhd_set_level",__FILE__) +IF(.NOT.oft_3D_lagrange_cast(oft_lagrange,xmhd_ML_lagrange%current_level))CALL oft_abort("Invalid FE object","xmhd_set_level",__FILE__) IF(j2_ind>0)THEN CALL xmhd_ML_hcurl%set_level(level) - IF(.NOT.oft_3D_hcurl_cast(oft_hcurl,xmhd_ML_hcurl%current_level))CALL oft_abort("Invalid HCurl FE object","xmhd_set_level",__FILE__) + IF(.NOT.oft_3D_hcurl_cast(oft_hcurl,xmhd_ML_hcurl%current_level))CALL oft_abort("Invalid Curl FE object","xmhd_set_level",__FILE__) END IF xmhd_level=level -! xmhd_lev=oft_lagrange_lev +mesh=>oft_lagrange%mesh ! Mesh for the active level (coarser on geometric MG levels) oft_xmhd_ops=>oft_xmhd_ops_ML(xmhd_level) end subroutine xmhd_set_level !------------------------------------------------------------------------------ @@ -3821,37 +4105,39 @@ subroutine xmhd_interp_setup(self,mesh) class(oft_mesh), target, intent(inout) :: mesh INTEGER(i4) :: i REAL(r8), POINTER, DIMENSION(:) :: vtmp -!---Set FEM link +!---Set FEM links +IF(j2_ind>0)self%curl_rep=>oft_hcurl self%lag_rep=>oft_lagrange -self%hcurl_rep=>oft_hcurl self%mesh=>mesh !---Get local slice IF(.NOT.ASSOCIATED(self%lf_loc))THEN ALLOCATE(self%lf_loc(self%lag_rep%ne,8)) IF(xmhd_two_temp)ALLOCATE(self%Te_loc(self%lag_rep%ne)) + IF(j2_ind>0)ALLOCATE(self%J2_loc(self%curl_rep%ne)) IF(n2_ind>0)ALLOCATE(self%N2_loc(self%lag_rep%ne)) - IF(j2_ind>0)ALLOCATE(self%J2_loc(self%hcurl_rep%ne)) END IF DO i=1,8 vtmp=>self%lf_loc(:,i) CALL self%u%get_local(vtmp,i) END DO +CALL lagrange_vector_unpack(oft_lagrange,self%lf_loc(:,1:3),.TRUE.) +CALL lagrange_vector_unpack(oft_lagrange,self%lf_loc(:,4:6),.TRUE.) IF(xmhd_two_temp)CALL self%u%get_local(self%Te_loc,te_ind) -IF(n2_ind>0)CALL self%u%get_local(self%N2_loc,n2_ind) IF(j2_ind>0)CALL self%u%get_local(self%J2_loc,j2_ind) +IF(n2_ind>0)CALL self%u%get_local(self%N2_loc,n2_ind) !---Allocate thread cache IF(.NOT.ASSOCIATED(self%cache))THEN ALLOCATE(self%cache(oft_env%nthreads)) DO i=1,oft_env%nthreads - ALLOCATE(self%cache(i)%lf(8,self%lag_rep%nce)) + ALLOCATE(self%cache(i)%lf(self%lag_rep%nce,8)) IF(xmhd_two_temp)ALLOCATE(self%cache(i)%Te(self%lag_rep%nce)) + IF(j2_ind>0)ALLOCATE(self%cache(i)%J2(self%curl_rep%nce)) IF(n2_ind>0)ALLOCATE(self%cache(i)%N2(self%lag_rep%nce)) - IF(j2_ind>0)ALLOCATE(self%cache(i)%J2(self%hcurl_rep%nce)) self%cache(i)%cell=-1 self%cache(i)%lf=0.d0 IF(xmhd_two_temp)self%cache(i)%Te=0.d0 - IF(n2_ind>0)self%cache(i)%N2=0.d0 IF(j2_ind>0)self%cache(i)%J2=0.d0 + IF(n2_ind>0)self%cache(i)%N2=0.d0 END DO END IF end subroutine xmhd_interp_setup @@ -3865,153 +4151,155 @@ subroutine xmhd_interp_delete(self) IF(ASSOCIATED(self%lf_loc))THEN DEALLOCATE(self%lf_loc) IF(xmhd_two_temp)DEALLOCATE(self%Te_loc) - IF(n2_ind>0)DEALLOCATE(self%N2_loc) IF(j2_ind>0)DEALLOCATE(self%J2_loc) + IF(n2_ind>0)DEALLOCATE(self%N2_loc) END IF IF(ASSOCIATED(self%cache))THEN DO i=1,oft_env%nthreads DEALLOCATE(self%cache(i)%lf) IF(xmhd_two_temp)DEALLOCATE(self%cache(i)%Te) - IF(n2_ind>0)DEALLOCATE(self%cache(i)%N2) IF(j2_ind>0)DEALLOCATE(self%cache(i)%J2) + IF(n2_ind>0)DEALLOCATE(self%cache(i)%N2) END DO DEALLOCATE(self%cache) END IF -NULLIFY(self%lag_rep,self%u) +NULLIFY(self%curl_rep,self%lag_rep,self%u) end subroutine xmhd_interp_delete !------------------------------------------------------------------------------ -!> Reconstruct xmhd operator linearization fields +!> Reconstruct xMHD solution fields +!! +!! Packed layout matches the interpolator in the primary implementation (\ref xmhd), with the +!! divergence of B appended at index 41 !------------------------------------------------------------------------------ subroutine xmhd_interp_apply(self,cell,f,gop,val) class(xmhd_interp), intent(inout) :: self !< Interpolation object integer(i4), intent(in) :: cell !< Cell for interpolation real(r8), intent(in) :: f(:) !< Position in cell in logical coord [4] real(r8), intent(in) :: gop(3,4) !< Logical gradient vectors at f [3,4] -real(r8), intent(out) :: val(:) !< Reconstructed field at f [26] +real(r8), intent(out) :: val(:) !< Reconstructed field at f [41] integer(i4), allocatable :: j(:) integer(i4) :: jc,k -real(r8) :: lf0(8),dlf0(3,5),b0(3),j0(3),divB,cgop(3,6) -real(r8) :: Te,dTe(3),N2,dN2(3),J2(3),J2c(3) -real(r8), allocatable :: lag_rop(:),lag_gop(:,:),hcurl_rop(:,:),hcurl_cop(:,:) +real(r8) :: cgop(3,6),Te,dTe(3),tmp,vtmp(3) +real(r8) :: N2,dN2(3),J2(3),J2c(3),lf0(8),dlf0(3,8),j0(3),divB +real(r8), allocatable, target :: lag_rop(:),lag_gop(:,:) +real(r8), allocatable :: hcurl_rop(:,:),hcurl_cop(:,:) +real(r8), pointer, contiguous :: rop(:),gopl(:,:) +type(xmhd_interp_cache), pointer :: tloc DEBUG_STACK_PUSH -IF(.NOT.ASSOCIATED(self%lf_loc))CALL oft_abort('Setup has not been called!','xmhd_interp_apply',__FILE__) +IF(.NOT.ASSOCIATED(self%lf_loc))CALL oft_abort('Setup has not been called!', & + 'xmhd_interp_apply',__FILE__) !---Pull cache -IF(self%cache(oft_tid+1)%cell/=cell)THEN +tloc=>self%cache(oft_tid+1) +IF(tloc%cell/=cell)THEN allocate(j(self%lag_rep%nce)) call self%lag_rep%ncdofs(cell,j) do jc=1,self%lag_rep%nce - self%cache(oft_tid+1)%lf(:,jc)=self%lf_loc(j(jc),:) - IF(xmhd_two_temp)self%cache(oft_tid+1)%Te(jc)=self%Te_loc(j(jc)) - IF(n2_ind>0)self%cache(oft_tid+1)%N2(jc)=self%N2_loc(j(jc)) + tloc%lf(jc,:)=self%lf_loc(j(jc),:) + IF(xmhd_two_temp)tloc%Te(jc)=self%Te_loc(j(jc)) + IF(n2_ind>0)tloc%N2(jc)=self%N2_loc(j(jc)) end do deallocate(j) IF(j2_ind>0)THEN - allocate(j(self%hcurl_rep%nce)) - call self%hcurl_rep%ncdofs(cell,j) - do jc=1,self%hcurl_rep%nce - IF(j2_ind>0)self%cache(oft_tid+1)%J2(jc)=self%J2_loc(j(jc)) + allocate(j(self%curl_rep%nce)) + call self%curl_rep%ncdofs(cell,j) + do jc=1,self%curl_rep%nce + tloc%J2(jc)=self%J2_loc(j(jc)) end do deallocate(j) END IF - self%cache(oft_tid+1)%cell=cell + tloc%cell=cell END IF -!--- -lf0=0.d0; dlf0=0.d0; b0=0.d0; j0=0.d0; divB=0.d0 -Te=0.d0; dTe=0.d0 -N2=0.d0; dN2=0.d0 -J2=0.d0; J2c=0.d0 +!---Get basis function values (cached or evaluated) +NULLIFY(rop,gopl) IF(ASSOCIATED(xmhd_lag_rop))THEN - !---Reconstruct all fields and gradient tensor - do jc=1,self%lag_rep%nce - lf0 = lf0 + self%cache(oft_tid+1)%lf(:,jc)*xmhd_lag_rop(jc) - DO k=1,5 - dlf0(:,k) = dlf0(:,k) + self%cache(oft_tid+1)%lf(3+k,jc)*xmhd_lag_gop(:,jc) - END DO - !---Reconstruct current density - j0(1) = j0(1) + xmhd_lag_gop(2,jc)*self%cache(oft_tid+1)%lf(3,jc) & - - xmhd_lag_gop(3,jc)*self%cache(oft_tid+1)%lf(2,jc) - j0(2) = j0(2) + xmhd_lag_gop(3,jc)*self%cache(oft_tid+1)%lf(1,jc) & - - xmhd_lag_gop(1,jc)*self%cache(oft_tid+1)%lf(3,jc) - j0(3) = j0(3) + xmhd_lag_gop(1,jc)*self%cache(oft_tid+1)%lf(2,jc) & - - xmhd_lag_gop(2,jc)*self%cache(oft_tid+1)%lf(1,jc) - !---Div error - divB = divB + SUM(self%cache(oft_tid+1)%lf(1:3,jc)*xmhd_lag_gop(:,jc)) - !---Electron temperature - IF(xmhd_two_temp)THEN - Te = Te + self%cache(oft_tid+1)%Te(jc)*xmhd_lag_rop(jc) - dTe = dTe + self%cache(oft_tid+1)%Te(jc)*xmhd_lag_gop(:,jc) - END IF - !---Hyper-diff aux variable - IF(n2_ind>0)THEN - N2 = N2 + self%cache(oft_tid+1)%N2(jc)*xmhd_lag_rop(jc) - dN2 = dN2 + self%cache(oft_tid+1)%N2(jc)*xmhd_lag_gop(:,jc) - END IF - end do - IF(j2_ind>0)THEN - do jc=1,self%hcurl_rep%nce - J2 = J2 + self%cache(oft_tid+1)%J2(jc)*xmhd_hcurl_rop(:,jc) - J2c = J2c + self%cache(oft_tid+1)%J2(jc)*xmhd_hcurl_cop(:,jc) - end do - END IF + rop=>xmhd_lag_rop + gopl=>xmhd_lag_gop ELSE - !---Lagrange elements ALLOCATE(lag_rop(self%lag_rep%nce),lag_gop(3,self%lag_rep%nce)) CALL oft_lag_eval_all(self%lag_rep,cell,f,lag_rop) CALL oft_lag_geval_all(self%lag_rep,cell,f,lag_gop,gop) - do jc=1,self%lag_rep%nce - !---Reconstruct all fields and gradient tensor - lf0 = lf0 + self%cache(oft_tid+1)%lf(:,jc)*lag_rop(jc) - DO k=1,5 - dlf0(:,k) = dlf0(:,k) + self%cache(oft_tid+1)%lf(3+k,jc)*lag_gop(:,jc) - END DO - !---Reconstruct current density - j0(1) = j0(1) + lag_gop(2,jc)*self%cache(oft_tid+1)%lf(3,jc) & - - lag_gop(3,jc)*self%cache(oft_tid+1)%lf(2,jc) - j0(2) = j0(2) + lag_gop(3,jc)*self%cache(oft_tid+1)%lf(1,jc) & - - lag_gop(1,jc)*self%cache(oft_tid+1)%lf(3,jc) - j0(3) = j0(3) + lag_gop(1,jc)*self%cache(oft_tid+1)%lf(2,jc) & - - lag_gop(2,jc)*self%cache(oft_tid+1)%lf(1,jc) - !---Div error - divB = divB + SUM(self%cache(oft_tid+1)%lf(1:3,jc)*lag_gop(:,jc)) - !---Electron temperature - IF(xmhd_two_temp)THEN - Te = Te + self%cache(oft_tid+1)%Te(jc)*lag_rop(jc) - dTe = dTe + self%cache(oft_tid+1)%Te(jc)*lag_gop(:,jc) - END IF - !---Hyper-diff aux variable - IF(n2_ind>0)THEN - N2 = N2 + self%cache(oft_tid+1)%N2(jc)*lag_rop(jc) - dN2 = dN2 + self%cache(oft_tid+1)%N2(jc)*lag_gop(:,jc) - END IF - end do - DEALLOCATE(lag_rop,lag_gop) - IF(j2_ind>0)THEN - CALL oft_hcurl_get_cgops(gop,cgop) - ALLOCATE(hcurl_rop(3,self%hcurl_rep%nce),hcurl_cop(3,self%hcurl_rep%nce)) - CALL oft_hcurl_eval_all(self%hcurl_rep,cell,f,hcurl_rop,gop) - CALL oft_hcurl_ceval_all(self%hcurl_rep,cell,f,hcurl_cop,cgop) - do jc=1,self%hcurl_rep%nce - J2 = J2 + self%cache(oft_tid+1)%J2(jc)*hcurl_rop(:,jc) - J2c = J2c + self%cache(oft_tid+1)%J2(jc)*hcurl_cop(:,jc) - end do - DEALLOCATE(hcurl_rop,hcurl_cop) - END IF + rop=>lag_rop + gopl=>lag_gop END IF -!---Pack -val(1:5)=lf0(4:8); val(6:14)=RESHAPE(dlf0(:,1:3),(/9/)) ! V -val(15:17)=dlf0(:,4); val(18:20)=dlf0(:,5) ! N, T -val(21:23)=lf0(1:3); val(24:26)=j0; val(27)=divB ! B, J, divB +!---Reconstruct Lagrange fields and gradients +lf0=0.d0; dlf0=0.d0 +Te=0.d0; dTe=0.d0 +N2=0.d0; dN2=0.d0 +J2=0.d0; J2c=0.d0 +DO k=1,8 + tmp=0.d0 + vtmp=0.d0 + DO jc=1,self%lag_rep%nce + tmp = tmp + tloc%lf(jc,k)*rop(jc) + vtmp = vtmp + tloc%lf(jc,k)*gopl(:,jc) + END DO + lf0(k)=tmp + dlf0(:,k)=vtmp +END DO IF(xmhd_two_temp)THEN - val(28) = Te - val(29:31) = dTe + DO jc=1,self%lag_rep%nce + Te = Te + tloc%Te(jc)*rop(jc) + dTe = dTe + tloc%Te(jc)*gopl(:,jc) + END DO END IF IF(n2_ind>0)THEN - val(32) = N2 - val(33:35) = dN2 + DO jc=1,self%lag_rep%nce + N2 = N2 + tloc%N2(jc)*rop(jc) + dN2 = dN2 + tloc%N2(jc)*gopl(:,jc) + END DO END IF +IF(ALLOCATED(lag_rop))DEALLOCATE(lag_rop,lag_gop) +!---Current density (J = Curl(B)) and divergence error from B gradient (dlf0(i,k) = d B_k / dx_i) +j0(1) = dlf0(2,3) - dlf0(3,2) +j0(2) = dlf0(3,1) - dlf0(1,3) +j0(3) = dlf0(1,2) - dlf0(2,1) +divB = dlf0(1,1) + dlf0(2,2) + dlf0(3,3) +!---Hyper-resistivity auxiliary field IF(j2_ind>0)THEN - val(36:38) = J2 - val(39:41) = J2c + IF(ASSOCIATED(xmhd_hcurl_rop))THEN + do jc=1,self%curl_rep%nce + J2 = J2 + tloc%J2(jc)*xmhd_hcurl_rop(:,jc) + J2c = J2c + tloc%J2(jc)*xmhd_hcurl_cop(:,jc) + end do + ELSE + CALL oft_hcurl_get_cgops(gop,cgop) + ALLOCATE(hcurl_rop(3,self%curl_rep%nce),hcurl_cop(3,self%curl_rep%nce)) + CALL oft_hcurl_eval_all(self%curl_rep,cell,f,hcurl_rop,gop) + CALL oft_hcurl_ceval_all(self%curl_rep,cell,f,hcurl_cop,cgop) + do jc=1,self%curl_rep%nce + J2 = J2 + tloc%J2(jc)*hcurl_rop(:,jc) + J2c = J2c + tloc%J2(jc)*hcurl_cop(:,jc) + end do + DEALLOCATE(hcurl_rop,hcurl_cop) + END IF +END IF +!---Pack +val(1:5)=lf0(4:8) ! (Vx, Vy, Vz, N, Ti) +val(6:14)=RESHAPE(dlf0(:,4:6),(/9/)) ! dV_i / dx_j +val(15:17)=dlf0(:,7) ! dN / dx_j +val(18:20)=dlf0(:,8) ! dTi /dx_j +val(21:23)=lf0(1:3) +val(24:26)=j0 +IF(SIZE(val)>=41)THEN + val(27:41) = 0.d0 + IF(xmhd_two_temp)THEN + val(27) = Te + val(28:30) = dTe + END IF + IF(n2_ind>0)THEN + val(31) = N2 + val(32:34) = dN2 + END IF + IF(j2_ind>0)THEN + val(35:37) = J2 + val(38:40) = J2c + END IF + val(41) = divB +ELSE IF(SIZE(val)>=30)THEN + IF(xmhd_two_temp)THEN + val(27) = Te + val(28:30) = dTe + END IF END IF DEBUG_STACK_POP end subroutine xmhd_interp_apply @@ -4026,19 +4314,20 @@ subroutine xmhd_interp_unpack(vals,vloc) vloc%dV = RESHAPE(vals(6:14),(/3,3/))*vel_scale vloc%N = vals(4)*den_scale; vloc%dN = vals(15:17)*den_scale vloc%Ti = vals(5); vloc%dTi = vals(18:20) -vloc%B = vals(21:23); vloc%J = vals(24:26); vloc%divB=vals(27) +vloc%B = vals(21:23); vloc%J = vals(24:26) +vloc%divB = vals(41) IF(xmhd_two_temp)THEN - vloc%Te = vals(28); vloc%dTe = vals(29:31) + vloc%Te = vals(27); vloc%dTe = vals(28:30) ELSE vloc%Te = te_factor*vloc%Ti; vloc%dTe = te_factor*vloc%dTi END IF IF(n2_ind>0)THEN - vloc%N2 = vals(32)*n2_scale - vloc%dN2 = vals(33:35)*n2_scale + vloc%N2 = vals(31)*n2_scale + vloc%dN2 = vals(32:34)*n2_scale END IF IF(j2_ind>0)THEN - vloc%J2 = vals(36:38)*j2_scale - vloc%J2c = vals(39:41)*j2_scale + vloc%J2 = vals(35:37)*j2_scale + vloc%J2c = vals(38:40)*j2_scale END IF end subroutine xmhd_interp_unpack !------------------------------------------------------------------------------ @@ -4068,10 +4357,7 @@ subroutine xmhd_set_ops(uin) levelin=xmhd_level minlev=xmhd_minlev qorder=oft_lagrange%order -nfields=26 -IF(xmhd_two_temp)nfields=30 -IF(n2_ind>0)nfields=nfields+1 -IF(j2_ind>0)nfields=nfields+1 +nfields=41 ! Average all interpolated fields (see xmhd_interp_apply) for consistent packed layout do i=levelin,minlev,-1 call xmhd_set_level(i) !------------------------------------------------------------------------------ @@ -4091,10 +4377,10 @@ subroutine xmhd_set_ops(uin) else !---Create cell centered storage if(ASSOCIATED(fullcctmp))DEALLOCATE(fullcctmp) - allocate(fullcctmp(26,mesh%nc)) + allocate(fullcctmp(nfields,mesh%nc)) !---Transfer from distributed to local grid if(xmhd_ML_lagrange%level==xmhd_ML_lagrange%blevel)then - call multigrid_base_pushcc(mg_mesh,fullcc,fullcctmp,26) + call multigrid_base_pushcc(mg_mesh,fullcc,fullcctmp,nfields) !---Average values to over child cells else !$omp parallel do private(k) @@ -4247,22 +4533,30 @@ subroutine oft_xmhd_push(sub_fields,u) CALL sub_fields%V%get_slice(vals,3) CALL u%restore_slice(vals,6) END IF -!---Update density +!---Update density field IF(ASSOCIATED(sub_fields%Ne))THEN CALL sub_fields%Ne%get_slice(vals) CALL u%restore_slice(vals,7) END IF -!---Update ion temperature +!---Update temperature fields IF(ASSOCIATED(sub_fields%Ti))THEN CALL sub_fields%Ti%get_slice(vals) CALL u%restore_slice(vals,8) END IF -!---Update electron temperature IF(ASSOCIATED(sub_fields%Te))THEN CALL sub_fields%Te%get_slice(vals) CALL u%restore_slice(vals,te_ind) END IF +IF(ASSOCIATED(sub_fields%N2))THEN + CALL sub_fields%N2%get_slice(vals) + CALL u%restore_slice(vals,n2_ind) +END IF IF(ASSOCIATED(vals))DEALLOCATE(vals) +IF(ASSOCIATED(sub_fields%J2))THEN + CALL sub_fields%J2%get_slice(vals) + CALL u%restore_slice(vals,j2_ind) + DEALLOCATE(vals) +END IF DEBUG_STACK_POP end subroutine oft_xmhd_push !------------------------------------------------------------------------------ @@ -4307,7 +4601,16 @@ subroutine oft_xmhd_pop(u,sub_fields) CALL u%get_slice(vals,te_ind) CALL sub_fields%Te%restore_slice(vals) END IF -DEALLOCATE(vals) +IF(ASSOCIATED(sub_fields%N2))THEN + CALL u%get_slice(vals,n2_ind) + CALL sub_fields%N2%restore_slice(vals) +END IF +IF(ASSOCIATED(vals))DEALLOCATE(vals) +IF(ASSOCIATED(sub_fields%J2))THEN + CALL u%get_slice(vals,j2_ind) + CALL sub_fields%J2%restore_slice(vals) + DEALLOCATE(vals) +END IF DEBUG_STACK_POP end subroutine oft_xmhd_pop !------------------------------------------------------------------------------ @@ -4319,79 +4622,14 @@ subroutine oft_xmhd_interp(self,acors,afine) class(ml_xmhd_vecspace), intent(inout) :: self class(oft_vector), intent(inout) :: acors !< Coarse level vector to interpolate class(oft_vector), intent(inout) :: afine !< Interpolated solution on fine level -INTEGER(i4) :: i,j -real(r8), pointer, dimension(:) :: vtmp -real(r8), pointer, dimension(:,:) :: v3tmp -real(r8) :: nn(3,3) DEBUG_STACK_PUSH CALL xmhd_set_level(xmhd_level+1) ! Step one level up !---Interpolate CALL afine%set(0.d0) CALL oft_xmhd_ops%interp%apply(acors,afine) -!---Apply boundary conditions -ALLOCATE(v3tmp(3,oft_lagrange%ne)) -DO i=1,3 - vtmp=>v3tmp(i,:) - CALL afine%get_local(vtmp,iblock=i) -END DO -IF(xmhd_adv_b)THEN - !---Zero boundary values - !$omp parallel do private(j,nn) - do i=1,oft_lagrange%nbe - j=oft_lagrange%lbe(i) - if(oft_lagrange%global%gbe(j))then - CALL lag_vbc_tensor(oft_lagrange,j,2,nn) - v3tmp(:,j)=MATMUL(nn, v3tmp(:,j)) - end if - end do -ELSE - v3tmp=0.d0 -END IF -DO i=1,3 - vtmp=>v3tmp(i,:) - CALL afine%restore_local(vtmp,iblock=i,wait=.TRUE.) -END DO -!--- -DO i=1,3 - vtmp=>v3tmp(i,:) - CALL afine%get_local(vtmp,iblock=i+3) -END DO -IF(xmhd_vbcdir)THEN - !$omp parallel do private(j,nn) - DO i=1,oft_lagrange%nbe - j=oft_lagrange%lbe(i) - IF(oft_lagrange%global%gbe(j))THEN - CALL lag_vbc_tensor(oft_lagrange,j,vbc_type,nn) - v3tmp(:,j)=MATMUL(nn, v3tmp(:,j)) - END IF - END DO -ELSE - !$omp parallel do - DO i=1,oft_lagrange%ne - IF(oft_xmhd_ops%v_bc(i))v3tmp(:,i)=0.d0 - END DO -END IF -DO i=1,3 - vtmp=>v3tmp(i,:) - CALL afine%restore_local(vtmp,iblock=i+3,wait=.TRUE.) -END DO -DEALLOCATE(v3tmp) -NULLIFY(vtmp) -! -CALL afine%get_local(vtmp,iblock=7) -!$omp parallel do -DO i=1,oft_lagrange%ne - IF(oft_xmhd_ops%n_bc(i))vtmp(i)=0.d0 -END DO -CALL afine%restore_local(vtmp,iblock=7,wait=.TRUE.) -! -CALL afine%get_local(vtmp,iblock=8) -!$omp parallel do -DO i=1,oft_lagrange%ne - IF(oft_xmhd_ops%t_bc(i))vtmp(i)=0.d0 -END DO -CALL afine%restore_local(vtmp,iblock=8) -DEALLOCATE(vtmp) +!---Enforce boundary conditions on interpolated vector fields +CALL xmhd_vec_bc_zero(afine,0,oft_xmhd_ops%b_bc_type) +CALL xmhd_vec_bc_zero(afine,3,oft_xmhd_ops%v_bc_type) DEBUG_STACK_POP end subroutine oft_xmhd_interp !------------------------------------------------------------------------------ @@ -4404,95 +4642,93 @@ subroutine oft_xmhd_inject(self,afine,acors) class(oft_vector), intent(inout) :: afine !< Fine level vector to inject class(oft_vector), intent(inout) :: acors !< Injected solution on coarse level real(r8), pointer, dimension(:) :: vtmp -real(r8), pointer, dimension(:,:) :: v3tmp -real(r8) :: nn(3,3) -integer(i4) :: i,j +integer(i4) :: i DEBUG_STACK_PUSH call xmhd_set_level(xmhd_level-1) ! Step one level down !---Restrict CALL oft_xmhd_ops_ML(xmhd_level+1)%interp%applyT(afine,acors) !---Apply boundary conditions -ALLOCATE(v3tmp(3,oft_lagrange%ne)) -DO i=1,3 - vtmp=>v3tmp(i,:) - CALL acors%get_local(vtmp,iblock=i) -END DO -IF(xmhd_adv_b)THEN - !---Zero boundary values - !$omp parallel do private(j,nn) - do i=1,oft_lagrange%nbe - j=oft_lagrange%lbe(i) - if(oft_lagrange%global%gbe(j))then - CALL lag_vbc_tensor(oft_lagrange,j,2,nn) - v3tmp(:,j)=MATMUL(nn, v3tmp(:,j)) - end if - end do -ELSE - v3tmp=0.d0 +CALL xmhd_vec_bc_zero(acors,0,oft_xmhd_ops%b_bc_type) +CALL xmhd_vec_bc_zero(acors,3,oft_xmhd_ops%v_bc_type) +! +NULLIFY(vtmp) +CALL acors%get_local(vtmp,iblock=7) +WHERE(oft_xmhd_ops%n_bc) + vtmp = 0.d0 +END WHERE +CALL acors%restore_local(vtmp,iblock=7,wait=.TRUE.) +! +CALL acors%get_local(vtmp,iblock=8) +WHERE(oft_xmhd_ops%t_bc) + vtmp = 0.d0 +END WHERE +CALL acors%restore_local(vtmp,iblock=8,wait=.TRUE.) +! +IF(te_ind>0)THEN + CALL acors%get_local(vtmp,iblock=te_ind) + WHERE(oft_xmhd_ops%t_bc) + vtmp = 0.d0 + END WHERE + CALL acors%restore_local(vtmp,iblock=te_ind,wait=.TRUE.) END IF +DEALLOCATE(vtmp) +DEBUG_STACK_POP +end subroutine oft_xmhd_inject +!------------------------------------------------------------------------------ +!> Zero the constrained components of a vector field (B or V) in an xMHD vector +!! +!! Applies the BC tensor for projected BCs (`bc_type>1`) and zeros all components +!! (`bc_type==1`) or the components normal to the axis (`bc_type==-1`) for Dirichlet BCs, +!! see \ref xmhd_lag::xmhd_set_bc "xmhd_set_bc". +!------------------------------------------------------------------------------ +subroutine xmhd_vec_bc_zero(u,offset,bc_type) +class(oft_vector), intent(inout) :: u !< xMHD vector +integer(i4), intent(in) :: offset !< Index of first vector component block minus one +integer(i4), intent(in) :: bc_type(:) !< BC type for each Lagrange node +integer(i4) :: i,j +real(r8) :: nn(3,3) +real(r8), pointer, dimension(:) :: vtmp +real(r8), pointer, dimension(:,:) :: v3tmp +DEBUG_STACK_PUSH +ALLOCATE(v3tmp(oft_lagrange%ne,3)) DO i=1,3 - vtmp=>v3tmp(i,:) - CALL acors%restore_local(vtmp,iblock=i,wait=.TRUE.) -END DO -!--- -DO i=1,3 - vtmp=>v3tmp(i,:) - CALL acors%get_local(vtmp,iblock=i+3) + vtmp=>v3tmp(:,i) + CALL u%get_local(vtmp,iblock=offset+i) END DO -IF(xmhd_vbcdir)THEN +CALL lagrange_vector_unpack(oft_lagrange,v3tmp,.TRUE.) +IF(ANY(bc_type>1))THEN !$omp parallel do private(j,nn) DO i=1,oft_lagrange%nbe j=oft_lagrange%lbe(i) - IF(oft_lagrange%global%gbe(j))THEN - CALL lag_vbc_tensor(oft_lagrange,j,vbc_type,nn) - v3tmp(:,j)=MATMUL(nn, v3tmp(:,j)) - END IF - END DO -ELSE - !$omp parallel do - DO i=1,oft_lagrange%ne - IF(oft_xmhd_ops%v_bc(i))v3tmp(:,i)=0.d0 + IF(bc_type(j)<=1)CYCLE + IF(.NOT.oft_lagrange%global%gbe(j))CYCLE + CALL lag_vbc_tensor(oft_lagrange,j,bc_type(j),nn) + v3tmp(j,:)=MATMUL(nn,v3tmp(j,:)) END DO END IF -DO i=1,3 - vtmp=>v3tmp(i,:) - CALL acors%restore_local(vtmp,iblock=i+3,wait=.TRUE.) -END DO -DEALLOCATE(v3tmp) -NULLIFY(vtmp) -! -CALL acors%get_local(vtmp,iblock=7) !$omp parallel do DO i=1,oft_lagrange%ne - IF(oft_xmhd_ops%n_bc(i))vtmp(i)=0.d0 + IF(bc_type(i)==1)v3tmp(i,1)=0.d0 + IF(ABS(bc_type(i))==1)v3tmp(i,2:3)=0.d0 END DO -CALL acors%restore_local(vtmp,iblock=7,wait=.TRUE.) -! -CALL acors%get_local(vtmp,iblock=8) -!$omp parallel do -DO i=1,oft_lagrange%ne - IF(oft_xmhd_ops%t_bc(i))vtmp(i)=0.d0 +CALL lagrange_vector_repack(oft_lagrange,v3tmp,.TRUE.) +DO i=1,3 + vtmp=>v3tmp(:,i) + CALL u%restore_local(vtmp,iblock=offset+i,wait=.TRUE.) END DO -CALL acors%restore_local(vtmp,iblock=8) -! -IF(xmhd_two_temp)THEN - CALL acors%get_local(vtmp,iblock=te_ind) - !$omp parallel do - DO i=1,oft_lagrange%ne - IF(oft_xmhd_ops%t_bc(i))vtmp(i)=0.d0 - END DO - CALL acors%restore_local(vtmp,iblock=te_ind) -END IF -DEALLOCATE(vtmp) +DEALLOCATE(v3tmp) DEBUG_STACK_POP -end subroutine oft_xmhd_inject +end subroutine xmhd_vec_bc_zero !------------------------------------------------------------------------------ !> Save or load driver info from restart file !------------------------------------------------------------------------------ subroutine xmhd_driver_rst_dummy(self,rst_file) class(oft_xmhd_driver), intent(inout) :: self !< Forcing object character(*), intent(in) :: rst_file !< Name of restart file -IF(oft_env%head_proc)CALL oft_warn('No driver rst specified') +IF(oft_env%head_proc.AND.self%rst_warn)THEN + CALL oft_warn('No driver rst specified') + self%rst_warn=.FALSE. +END IF end subroutine xmhd_driver_rst_dummy !------------------------------------------------------------------------------ !> Flush internal write buffers for probe object @@ -4533,7 +4769,7 @@ subroutine xmhd_plot(probes) type(oft_timer) :: mytimer real(r8), pointer :: bvout(:,:) real(r8), pointer :: vals(:),hcvals(:) -integer(i4) :: i,j,ip,u_ip,ierr,io_flag,io_unit,io_stat,rst_cur,rst_tmp +integer(i4) :: i,j,ip,u_ip,ierr,io_flag,io_unit,io_stat,rst_cur,rst_tmp,nl_update real(r8) :: mag,div_err,mer,merp,ver,verp,gerr,cerr,verr real(r8) :: fac,lramp,tflux,tcurr,t,tp,td real(r8) :: ndens @@ -4619,7 +4855,7 @@ subroutine xmhd_plot(probes) !------------------------------------------------------------------------------ ! Read-in run parameters (only `rst_freq` is used) !------------------------------------------------------------------------------ -CALL xmhd_read_settings(dt_run,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget) +CALL xmhd_read_settings(dt_run,lin_tol,nl_tol,rst_ind,nsteps,rst_freq,nclean,maxextrap,ittarget,nl_update) rst_ind=rst_start nsteps=rst_end !------------------------------------------------------------------------------ @@ -4655,7 +4891,7 @@ subroutine xmhd_plot(probes) IF(.NOT.oft_3D_hcurl_cast(oft_hcurl,xmhd_ML_hcurl%current_level))CALL oft_abort("Invalid Curl FE object","xmhd_plot",__FILE__) call xmhd_ML_hcurl%vec_create(sub_fields%J2) END IF -!--------------------------------------------------------------------------- +!------------------------------------------------------------------------------ ! Setup Lagrange mass solver !------------------------------------------------------------------------------ NULLIFY(lmop) @@ -4664,7 +4900,7 @@ subroutine xmhd_plot(probes) lminv%A=>lmop lminv%its=-2 CALL create_diag_pre(lminv%pre) -!---Look for and load linearization fields +!---Load linearization fields if available IF(linear)THEN rst_cur=0 WRITE(rst_char,100)rst_cur @@ -4686,6 +4922,7 @@ subroutine xmhd_plot(probes) !------------------------------------------------------------------------------ ! Extract temperatures and plot !------------------------------------------------------------------------------ + vals=>bvout(1,:) CALL sub_fields%Ti%get_local(vals) CALL mesh%save_vertex_scalar(vals,xdmf,'T0') !---Electron temperature @@ -4702,9 +4939,10 @@ subroutine xmhd_plot(probes) CALL sub_fields%V%get_local(vals,2) vals=>bvout(3,:) CALL sub_fields%V%get_local(vals,3) + CALL lagrange_vector_unpack(oft_lagrange,bvout) CALL mesh%save_vertex_vector(bvout,xdmf,'V0') !------------------------------------------------------------------------------ -! Extract magnetic field and plot +! Project magnetic field and plot !------------------------------------------------------------------------------ vals=>bvout(1,:) CALL sub_fields%B%get_local(vals,1) @@ -4712,6 +4950,7 @@ subroutine xmhd_plot(probes) CALL sub_fields%B%get_local(vals,2) vals=>bvout(3,:) CALL sub_fields%B%get_local(vals,3) + CALL lagrange_vector_unpack(oft_lagrange,bvout) CALL mesh%save_vertex_vector(bvout,xdmf,'B0') !---Project current density and plot Jfield%u=>sub_fields%B @@ -4725,6 +4964,7 @@ subroutine xmhd_plot(probes) CALL ap%get_local(vals,2) vals=>bvout(3,:) CALL ap%get_local(vals,3) + CALL lagrange_vector_unpack(oft_lagrange,bvout) CALL mesh%save_vertex_vector(bvout,xdmf,'J0') END IF !------------------------------------------------------------------------------ @@ -4766,6 +5006,7 @@ subroutine xmhd_plot(probes) file_tmp='xMHD_'//rst_char//'.rst' END IF rst=oft_file_exist(TRIM(file_tmp)) +CALL oft_mpi_barrier(ierr) IF(.NOT.rst)THEN CALL oft_abort('Restart file does not exist.','xmhd_plot',__FILE__) ELSE @@ -4808,6 +5049,7 @@ subroutine xmhd_plot(probes) file_tmp='xMHD_'//rst_char//'.rst' END IF rst=oft_file_exist(TRIM(file_tmp)) + CALL oft_mpi_barrier(ierr) IF(.NOT.rst)THEN CALL oft_abort('Restart file does not exist.','xmhd_plot',__FILE__) ELSE @@ -4831,7 +5073,7 @@ subroutine xmhd_plot(probes) END IF CALL xdmf%add_timestep(td) !------------------------------------------------------------------------------ -! Retrieve sub-fields and scale +! Retrieve sub-fields and setup interpolations !------------------------------------------------------------------------------ CALL oft_xmhd_pop(uc,sub_fields) CALL sub_fields%V%scale(vel_scale) @@ -4878,6 +5120,7 @@ subroutine xmhd_plot(probes) CALL sub_fields%V%get_local(vals,2) vals=>bvout(3,:) CALL sub_fields%V%get_local(vals,3) + CALL lagrange_vector_unpack(oft_lagrange,bvout) CALL mesh%save_vertex_vector(bvout,xdmf,'V') !------------------------------------------------------------------------------ ! Project magnetic field and plot @@ -4888,6 +5131,7 @@ subroutine xmhd_plot(probes) CALL sub_fields%B%get_local(vals,2) vals=>bvout(3,:) CALL sub_fields%B%get_local(vals,3) + CALL lagrange_vector_unpack(oft_lagrange,bvout) CALL mesh%save_vertex_vector(bvout,xdmf,'B') !---Hyper-res aux field IF(j2_ind>0)THEN @@ -4902,6 +5146,7 @@ subroutine xmhd_plot(probes) CALL ap%get_local(vals,2) vals=>bvout(3,:) CALL ap%get_local(vals,3) + CALL lagrange_vector_unpack(oft_lagrange,bvout) CALL mesh%save_vertex_vector(bvout,xdmf,'J2') END IF !---Current density @@ -4916,6 +5161,7 @@ subroutine xmhd_plot(probes) CALL ap%get_local(vals,2) vals=>bvout(3,:) CALL ap%get_local(vals,3) + CALL lagrange_vector_unpack(oft_lagrange,bvout) CALL mesh%save_vertex_vector(bvout,xdmf,'J') !---Divergence error IF(plot_div)THEN @@ -4984,4 +5230,49 @@ subroutine xmhd_plot(probes) DEALLOCATE(bvout) DEBUG_STACK_POP end subroutine xmhd_plot +!------------------------------------------------------------------------------ +!> Simple subroutine to compute timing of different solve phases +!------------------------------------------------------------------------------ +subroutine xmhd_profile(u) +class(oft_vector), intent(inout) :: u +INTEGER(4) :: i,j,k +REAL(8) :: elapsed_time,matvec_time,nlop_time,build_optime,dot_time +class(oft_vector), pointer :: v +type(oft_timer) :: mytimer +CALL xmhd_rep%vec_create(v) +matvec_time=0.d0 +nlop_time=0.d0 +build_optime=0.d0 +dot_time=0.d0 +IF(oft_env%head_proc)WRITE(*,'(A)')'Starting profiling' +comm_times=0 +DO i=1,10 + IF(oft_env%head_proc)CALL mytimer%tick + CALL xmhd_set_ops(u) + IF(oft_env%head_proc)build_optime=build_optime+mytimer%tock() + DO j=1,10 + IF(oft_env%head_proc)CALL mytimer%tick + CALL oft_xmhd_ops%A%apply(u, v) + IF(oft_env%head_proc)nlop_time=nlop_time+mytimer%tock() + ! + IF(oft_env%head_proc)CALL mytimer%tick + DO k=1,10 + CALL oft_xmhd_ops%J%apply(u, v) + END DO + IF(oft_env%head_proc)matvec_time=matvec_time+mytimer%tock() + IF(oft_env%head_proc)CALL mytimer%tick + DO k=1,100 + elapsed_time=u%dot(v) + END DO + IF(oft_env%head_proc)dot_time=dot_time+mytimer%tock() + END DO +END DO +IF(oft_env%head_proc)THEN + WRITE(*,'(A,ES11.3)')' Dot = ',dot_time/REAL(10*10*100,8) + WRITE(*,'(A,ES11.3)')' MatVec = ',matvec_time/REAL(10*10*10,8) + WRITE(*,'(A,ES11.3)')' NL Op = ',nlop_time/REAL(10*10,8) + WRITE(*,'(A,ES11.3)')' Build = ',build_optime/REAL(10,8) +END IF +CALL oft_finalize() +end subroutine xmhd_profile end module xmhd_lag diff --git a/src/tests/physics/MUG/CMakeLists.txt b/src/tests/physics/MUG/CMakeLists.txt index 3f3861518..f6d5d531d 100644 --- a/src/tests/physics/MUG/CMakeLists.txt +++ b/src/tests/physics/MUG/CMakeLists.txt @@ -27,3 +27,10 @@ configure_file( test_MUG_visc.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY ) oft_add_test( test_MUG_gyro.F90 test_phys_helpers ) configure_file( test_MUG_gyro.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY ) configure_file( test_MUG_hall.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY ) +oft_add_test( test_MUG_visc_lag.F90 test_phys_helpers ) +configure_file( test_MUG_visc_lag.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY ) +oft_add_test( test_MUG_gyro_lag.F90 test_phys_helpers ) +configure_file( test_MUG_gyro_lag.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY ) +configure_file( test_MUG_hall_lag.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY ) +oft_add_test( test_MUG_revolved_lag.F90 test_phys_helpers ) +configure_file( test_MUG_revolved_lag.py ${CMAKE_CURRENT_BINARY_DIR} COPYONLY ) diff --git a/src/tests/physics/MUG/mug_testing.py b/src/tests/physics/MUG/mug_testing.py index 9501d7ecf..927d0a503 100644 --- a/src/tests/physics/MUG/mug_testing.py +++ b/src/tests/physics/MUG/mug_testing.py @@ -199,8 +199,8 @@ def validate_mug_output(steps_exp=11, linear=False, two_temp=False): / """ -# Magnetic field representation of the 3-D xMHD modules: the mixed H(curl)/Lagrange module (xmhd) sets a Dirichlet -# condition on B at the wall; the nodal Lagrange module (xmhd_lag) relies on divergence cleaning instead +# Magnetic field representation of the 3-D xMHD modules: the mixed H(curl)/Lagrange module (xmhd) and the nodal +# Lagrange module (xmhd_lag), which adds a divergence cleaning term (k_db) to the induction equation REPRESENTATIONS = ('hcurl', 'lagrange') @@ -237,7 +237,7 @@ def sound_setup(representation,nbase,nlevels,order,minlev,nu_mhd='-1',linear=Fal only) are finite-difference approximations, so the linear tolerance is relaxed. ''' exe, rep_options, timeout = {'hcurl': ('./test_sound', " bbc='bc'\n nu_visc=0.\n", 2000), - 'lagrange': ('./test_sound_lag', " k_db=1.\n", 1000)}[representation] + 'lagrange': ('./test_sound_lag', " bbc='bc'\n nu_visc=0.\n k_db=1.\n", 1000)}[representation] # Start linear BDF2 runs with a backward Euler step if linear: rep_options += " xmhd_lin_euler_start=T\n" @@ -273,9 +273,8 @@ def alfven_setup(representation,nbase,nlevels,order,minlev,nu_mhd='-1',linear=Fa (nonlinear runs only); `bdf2=False` uses first-order backward Euler steps (linear runs only). Order 4 needs a tighter linear tolerance to reach the expected error, otherwise MPI runs (with a different multigrid layout) do not reproduce the serial errors. ''' - # nu_par is parallel-only viscosity in xmhd (no effect on this transverse wave) but the fluid viscosity in xmhd_lag exe, rep_options, timeout = {'hcurl': ('./test_alfven', " bbc='bc'\n nu_visc=1.E-3\n", 4000), - 'lagrange': ('./test_alfven_lag', " nu_par=1.e-3\n", 2000)}[representation] + 'lagrange': ('./test_alfven_lag', " bbc='bc'\n nu_visc=1.E-3\n k_db=1.\n", 2000)}[representation] # Start linear BDF2 runs with a backward Euler step, or use backward Euler throughout if linear: if bdf2: diff --git a/src/tests/physics/MUG/test_MUG_gyro.py b/src/tests/physics/MUG/test_MUG_gyro.py index 29c67aaff..c442e3f7a 100644 --- a/src/tests/physics/MUG/test_MUG_gyro.py +++ b/src/tests/physics/MUG/test_MUG_gyro.py @@ -126,19 +126,20 @@ def alfven_linear(nu_gyro, hall=False, me_factor=100.): x = expm(np.linalg.solve(mass, mat)*t_final) @ x0 return np.array([[-x[0].imag, x[0].real], [-x[1].imag, x[1].real]]) -def gyro_case(nu_gyro, hall=False, me_factor=100., linear=False): +def gyro_case(nu_gyro, hall=False, me_factor=100., linear=False, exe='./test_MUG_gyro'): '''Write the input for an Alfven wave with ion temperature ti0 and run test_MUG_gyro `hall` enables the Hall, electron pressure, and electron inertia terms, with electron mass factor `me_factor`. `linear` advances the wave with the linearized model (xmhd_lin_run, started with a backward Euler step) about - the uniform equilibrium instead of the nonlinear model. + the uniform equilibrium instead of the nonlinear model. `exe` selects the driver program (`./test_MUG_gyro_lag` + for the Lagrange-only module). ''' os.chdir(test_dir) with open('oft.in','w+') as fid: fid.write(oft_in_template.format(ti0, eta, nu_visc, nu_gyro, 'T' if hall else 'F', me_factor, 'T' if linear else 'F')) # Single FE level: block-Jacobi/LU preconditioner inside one GMRES iteration (as in mug_testing.mug_command) - return run_OFT("./test_MUG_gyro oft.in oft_in_xmhd.xml", 1, 600) + return run_OFT("{0} oft.in oft_in_xmhd.xml".format(exe), 1, 600) def validate_result(nu_gyro, err_exp, hall=False, me_factor=100.): '''Compare the final transverse velocity with linear theory diff --git a/src/tests/physics/MUG/test_MUG_gyro_lag.F90 b/src/tests/physics/MUG/test_MUG_gyro_lag.F90 new file mode 100644 index 000000000..50213a994 --- /dev/null +++ b/src/tests/physics/MUG/test_MUG_gyro_lag.F90 @@ -0,0 +1,191 @@ +!--------------------------------------------------------------------------------- +! Flexible Unstructured Simulation Infrastructure with Open Numerics (Open FUSION Toolkit) +! +! SPDX-License-Identifier: LGPL-3.0-only +!--------------------------------------------------------------------------------- +!> @file test_MUG_gyro_lag.F90 +! +!> Regression test for xMHD_lag (all fields on Lagrange elements) gyroviscosity. +!! Same as test_MUG_gyro. A linearly polarized traveling alfven wave is +!! initialized in a triply periodic box with a uniform ion temperature and +!! advanced for one half period. Gyroviscosity splits the +!! two circular polarizations, which rotates the polarization of the wave. +!! The final velocity is projected onto both polarizations for comparison +!! to linear theory by the driver script. The same driver tests the Hall +!! term, which also splits the circular polarizations, and can advance the +!! wave with either the nonlinear or the linearized model. +!! +!! @ingroup testing +!------------------------------------------------------------------------------ +PROGRAM test_MUG_gyro_lag +USE oft_base +!--Grid +USE multigrid, ONLY: multigrid_mesh +USE multigrid_build, ONLY: multigrid_construct +!---Linear algebra +USE oft_la_base, ONLY: oft_vector, oft_matrix +USE oft_solver_base, ONLY: oft_solver +USE oft_solver_utils, ONLY: create_cg_solver, create_diag_pre +!---Lagrange FE space +USE oft_lag_basis, ONLY: oft_lag_setup +USE oft_lag_operators, ONLY: lag_setup_interp, oft_lag_vproject, oft_lag_vgetmop +!---Physics +USE oft_vector_inits, ONLY: uniform_field +USE mhd_utils, ONLY: mu0, proton_mass +USE xmhd_lag, ONLY: xmhd_run, xmhd_lin_run, xmhd_minlev, xmhd_taxis, xmhd_sub_fields, & + xmhd_ML_lagrange, xmhd_ML_vlagrange +USE test_phys_helpers, ONLY: alfven_eig, lag_vec_mode_proj +IMPLICIT NONE +!---Mass matrix solvers +CLASS(oft_solver), POINTER :: minv => NULL() +CLASS(oft_matrix), POINTER :: mop => NULL() +CLASS(oft_solver), POINTER :: lag_minv => NULL() +CLASS(oft_matrix), POINTER :: lag_mop => NULL() +!---Local variables +CLASS(oft_vector), POINTER :: b,vel,u,v,den,temp,db,dvel,dden,dtemp +TYPE(xmhd_sub_fields) :: equil_fields,pert_fields +TYPE(uniform_field) :: z_field +TYPE(alfven_eig), TARGET :: alf_field +TYPE(multigrid_mesh) :: mg_mesh +INTEGER(i4) :: io_unit,ierr +REAL(r8) :: B0,proj(2,3) +INTEGER(i4) :: order = 2 +INTEGER(i4) :: minlev = 1 +REAL(r8) :: delta = 1.d-3 +REAL(r8) :: v_alf = 1.d4 +REAL(r8) :: N0 = 1.d19 +REAL(r8) :: ti0 = 2.d0 +LOGICAL :: linear = .FALSE. +NAMELIST/test_gyro_options/order,minlev,delta,ti0,linear +!------------------------------------------------------------------------------ +! Initialize enviroment +!------------------------------------------------------------------------------ +CALL oft_init +!---Read in options +OPEN(NEWUNIT=io_unit,FILE=oft_env%ifile) +READ(io_unit,test_gyro_options,IOSTAT=ierr) +CLOSE(io_unit) +!------------------------------------------------------------------------------ +! Setup grid +!------------------------------------------------------------------------------ +CALL multigrid_construct(mg_mesh) +!------------------------------------------------------------------------------ +! Build FE structures +!------------------------------------------------------------------------------ +!--- Lagrange +ALLOCATE(xmhd_ML_lagrange,xmhd_ML_vlagrange) +CALL oft_lag_setup(mg_mesh,order,xmhd_ML_lagrange,ML_vlag_obj=xmhd_ML_vlagrange,minlev=minlev) +CALL lag_setup_interp(xmhd_ML_lagrange) +!------------------------------------------------------------------------------ +! Create Lagrange vector mass matrix solver (magnetic field) +!------------------------------------------------------------------------------ +NULLIFY(mop) +CALL oft_lag_vgetmop(xmhd_ML_vlagrange%current_level,mop,"none") +CALL create_cg_solver(minv) +minv%A=>mop +minv%its=-3 +minv%atol=1.d-10 +CALL create_diag_pre(minv%pre) +!--- +CALL xmhd_ML_vlagrange%vec_create(u) +CALL xmhd_ML_vlagrange%vec_create(v) +CALL xmhd_ML_vlagrange%vec_create(b) +CALL xmhd_ML_vlagrange%vec_create(db) +!------------------------------------------------------------------------------ +! Set B = B0*(zhat + dB) from uniform field and alfven wave init (B0*zhat and +! B0*dB are kept separate for linear runs) +!------------------------------------------------------------------------------ +B0=v_alf*SQRT(mu0*2.d0*N0*proton_mass) +z_field%val=(/0.d0,0.d0,1.d0/) +CALL oft_lag_vproject(xmhd_ML_lagrange%current_level,z_field,v) +CALL u%set(0.d0) +CALL minv%apply(u,v) +CALL b%add(0.d0,B0,u) +!--- +alf_field%mesh=>mg_mesh%mesh +CALL oft_lag_vproject(xmhd_ML_lagrange%current_level,alf_field,v) +CALL u%set(0.d0) +CALL minv%apply(u,v) +CALL db%add(0.d0,delta*B0,u) +IF(.NOT.linear)CALL b%add(1.d0,1.d0,db) +!--- +CALL u%delete +CALL v%delete +CALL mop%delete +DEALLOCATE(u,v,mop) +CALL minv%pre%delete +CALL minv%delete +!------------------------------------------------------------------------------ +! Create Lagrange metric solver +!------------------------------------------------------------------------------ +NULLIFY(lag_mop) +CALL oft_lag_vgetmop(xmhd_ML_vlagrange%current_level,lag_mop,"none") +CALL create_cg_solver(lag_minv) +lag_minv%A=>lag_mop +lag_minv%its=-3 +lag_minv%atol=1.d-10 +CALL create_diag_pre(lag_minv%pre) +!--- +CALL xmhd_ML_vlagrange%vec_create(u) +CALL xmhd_ML_vlagrange%vec_create(v) +CALL xmhd_ML_vlagrange%vec_create(vel) +CALL xmhd_ML_vlagrange%vec_create(dvel) +!------------------------------------------------------------------------------ +! Set V from alfven wave init (zero equilibrium flow for linear runs) +!------------------------------------------------------------------------------ +CALL oft_lag_vproject(xmhd_ML_lagrange%current_level,alf_field,v) +CALL u%set(0.d0) +CALL lag_minv%apply(u,v) +CALL dvel%add(0.d0,delta*v_alf,u) +CALL vel%set(0.d0) +IF(.NOT.linear)CALL vel%add(0.d0,1.d0,dvel) +!--- +CALL u%delete +CALL v%delete +CALL lag_mop%delete +DEALLOCATE(u,v,lag_mop) +!------------------------------------------------------------------------------ +! Run simulation +!------------------------------------------------------------------------------ +xmhd_minlev=minlev +xmhd_taxis=2 +oft_env%pm=.FALSE. +CALL xmhd_ML_lagrange%vec_create(den) +CALL xmhd_ML_lagrange%vec_create(temp) +CALL den%set(N0) +CALL temp%set(ti0) +equil_fields%B=>b +equil_fields%V=>vel +equil_fields%Ne=>den +equil_fields%Ti=>temp +IF(linear)THEN + CALL xmhd_ML_lagrange%vec_create(dden) + CALL xmhd_ML_lagrange%vec_create(dtemp) + CALL dden%set(0.d0) + CALL dtemp%set(0.d0) + pert_fields%B=>db + pert_fields%V=>dvel + pert_fields%Ne=>dden + pert_fields%Ti=>dtemp + CALL xmhd_lin_run(equil_fields,pert_fields) + !---Final velocity is the perturbation (no equilibrium flow) + CALL vel%add(1.d0,1.d0,dvel) +ELSE + CALL xmhd_run(equil_fields) +END IF +!------------------------------------------------------------------------------ +! Project final velocity onto both polarizations (normalized by initial amplitude) +!------------------------------------------------------------------------------ +CALL vel%scale(1.d0/(delta*v_alf)) +CALL lag_vec_mode_proj(xmhd_ML_lagrange%current_level,mg_mesh%mesh,vel, & + alf_field%k_dir,alf_field%lam,order*2,proj) +IF(oft_env%head_proc)THEN + OPEN(NEWUNIT=io_unit,FILE='gyro.results') + WRITE(io_unit,*)proj(:,1) + WRITE(io_unit,*)proj(:,2) + WRITE(io_unit,*)proj(:,3) + CLOSE(io_unit) +END IF +!---Finalize enviroment +CALL oft_finalize +END PROGRAM test_MUG_gyro_lag diff --git a/src/tests/physics/MUG/test_MUG_gyro_lag.py b/src/tests/physics/MUG/test_MUG_gyro_lag.py new file mode 100644 index 000000000..60877d507 --- /dev/null +++ b/src/tests/physics/MUG/test_MUG_gyro_lag.py @@ -0,0 +1,44 @@ +#------------------------------------------------------------------------------ +# Flexible Unstructured Simulation Infrastructure with Open Numerics (Open FUSION Toolkit) +# +# SPDX-License-Identifier: LGPL-3.0-only +#------------------------------------------------------------------------------ +# MUG tests: rotation of the polarization of a shear Alfven wave in a triply periodic box (xMHD with all fields on +# Lagrange elements) by Braginskii gyroviscosity and by the Hall term in Ohm's law. Same cases, input template, and +# linear theory as test_MUG_gyro.py, run with the test_MUG_gyro_lag driver. Shared with test_MUG_hall_lag.py, which +# holds the Hall-only cases +import pytest +from test_MUG_gyro import gyro_case as _gyro_case, validate_result + +def gyro_case(nu_gyro, hall=False, me_factor=100., linear=False): + return _gyro_case(nu_gyro, hall=hall, me_factor=me_factor, linear=linear, exe='./test_MUG_gyro_lag') + +#============================================================================ +# Each case runs with the nonlinear and the linearized model, which share the linear theory and the deviation +# tolerance but have separate regression values +#============================================================================ +# Reference case without gyroviscosity +@pytest.mark.parametrize("linear,err_exp", ((False, 4.266953E-04), pytest.param(True, 9.117855E-04, marks=pytest.mark.linear))) +def test_alfven_ref(linear, err_exp): + '''Linearly polarized shear Alfven wave without gyroviscosity, which keeps its polarization.''' + assert gyro_case(-1., linear=linear) + assert validate_result(0.0, err_exp) + +# Gyroviscous rotation of the polarization +@pytest.mark.parametrize("linear,err_exp", ((False, 1.767624E-03), pytest.param(True, 2.074131E-03, marks=pytest.mark.linear))) +def test_gyro_rotation(linear, err_exp): + '''Shear Alfven wave with Braginskii's gyroviscous stress (S. I. Braginskii, Rev. Plasma Phys. 1, 205 (1965)), + whose polarization rotates from x toward y (B along +z) at half the split between the circular polarizations, + eta_4 k^2/rho = k^2 T_i/B0. The opposite sign of the stress would rotate it the other way.''' + assert gyro_case(1., linear=linear) + assert validate_result(1.0, err_exp) + +#============================================================================ +# Gyroviscosity with the Hall term (Hall-only cases are in test_MUG_hall_lag.py) +@pytest.mark.parametrize("linear,err_exp", ((False, 3.098049E-03), pytest.param(True, 3.009142E-03, marks=pytest.mark.linear))) +def test_hall_gyro(linear, err_exp): + '''Shear Alfven wave with both gyroviscosity and the Hall term (default electron mass factor of 100). Their + rotation rates are in the ratio beta_i/2 ~ 0.96 and, with the conventions above, add, which checks the relative + sign of the two terms.''' + assert gyro_case(1., hall=True, linear=linear) + assert validate_result(1.0, err_exp, hall=True) diff --git a/src/tests/physics/MUG/test_MUG_hall_lag.py b/src/tests/physics/MUG/test_MUG_hall_lag.py new file mode 100644 index 000000000..d7b79a4b2 --- /dev/null +++ b/src/tests/physics/MUG/test_MUG_hall_lag.py @@ -0,0 +1,32 @@ +#------------------------------------------------------------------------------ +# Flexible Unstructured Simulation Infrastructure with Open Numerics (Open FUSION Toolkit) +# +# SPDX-License-Identifier: LGPL-3.0-only +#------------------------------------------------------------------------------ +# MUG tests: rotation of the polarization of a shear Alfven wave in a triply periodic box (xMHD with all fields on +# Lagrange elements) by the Hall term in Ohm's law, with and without significant electron inertia. Same cases as +# test_MUG_hall.py, using the test_MUG_gyro_lag driver (the combined Hall and gyroviscosity case is in +# test_MUG_gyro_lag.py) +import pytest +from test_MUG_gyro_lag import gyro_case +from test_MUG_gyro import validate_result + +#============================================================================ +# Each case runs with the nonlinear and the linearized model, which have separate regression values +#============================================================================ +# Hall term +@pytest.mark.parametrize("linear,err_exp", ((False, 1.381234E-03), pytest.param(True, 1.712936E-03, marks=pytest.mark.linear))) +def test_hall_rotation(linear, err_exp): + '''Shear Alfven wave with the Hall term in Ohm's law, which splits the whistler and ion cyclotron branches and + rotates the polarization at half their split, k^2 v_A d_i/2 (k d_i = 0.32). The electron mass factor is 1, so + electron inertia is negligible (k^2 d_e^2 = 3E-5).''' + assert gyro_case(-1., hall=True, me_factor=1., linear=linear) + assert validate_result(0.0, err_exp, hall=True, me_factor=1.) + +# Electron inertia +@pytest.mark.parametrize("linear,err_exp", ((False, 1.376975E-03), pytest.param(True, 1.726795E-03, marks=pytest.mark.linear))) +def test_hall_inertia(linear, err_exp): + '''Shear Alfven wave with the Hall term and an electron mass factor of 1000 (k^2 d_e^2 = 0.028), where electron + inertia shifts the phase of V by ~0.035 relative to the inertia-free result.''' + assert gyro_case(-1., hall=True, me_factor=1000., linear=linear) + assert validate_result(0.0, err_exp, hall=True, me_factor=1000.) diff --git a/src/tests/physics/MUG/test_MUG_revolved_lag.F90 b/src/tests/physics/MUG/test_MUG_revolved_lag.F90 new file mode 100644 index 000000000..787193153 --- /dev/null +++ b/src/tests/physics/MUG/test_MUG_revolved_lag.F90 @@ -0,0 +1,154 @@ +!--------------------------------------------------------------------------------- +! Flexible Unstructured Simulation Infrastructure with Open Numerics (Open FUSION Toolkit) +! +! SPDX-License-Identifier: LGPL-3.0-only +!--------------------------------------------------------------------------------- +! Regression test for xMHD_lag on a rotationally periodic (revolved) domain. A wedge about +! the x-axis is initialized with a Z-pinch equilibrium: an azimuthal field B = c (0,-z,y), +! with uniform axial current, balanced by the pressure gradient of a quadratic temperature +! profile. Both are exactly represented by quadratic Lagrange elements, so the discrete +! state is an equilibrium only if the azimuthal vectors are correctly rotated across the +! periodic boundaries. The equilibrium is advanced and the RMS velocity (normalized by +! the Alfven speed at the wall) and the relative change in B are written for the driver. +MODULE test_revolved_fields +USE oft_base +USE fem_utils, ONLY: fem_interp +IMPLICIT NONE +TYPE, EXTENDS(fem_interp) :: pinch_field + CHARACTER(LEN=1) :: field = 'b' + REAL(r8) :: c = 0.1d0 ! Field gradient [T/m] + REAL(r8) :: T0 = 1.d0 ! Temperature on axis [eV] +CONTAINS + PROCEDURE :: interp => pinch_field_interp +END TYPE pinch_field +CONTAINS +SUBROUTINE pinch_field_interp(self,cell,f,gop,val) +CLASS(pinch_field), INTENT(inout) :: self +INTEGER(i4), INTENT(in) :: cell +REAL(r8), INTENT(in) :: f(:) +REAL(r8), INTENT(in) :: gop(3,4) +REAL(r8), INTENT(out) :: val(:) +REAL(r8) :: pt(3) +pt=self%mesh%log2phys(cell,f) +SELECT CASE(self%field) + CASE('b') + val(1:3)=self%c*[0.d0,-pt(3),pt(2)] + CASE('t') + val(1)=self%T0*(1.d0-(pt(2)**2+pt(3)**2)/2.d0) +END SELECT +END SUBROUTINE pinch_field_interp +END MODULE test_revolved_fields +!------------------------------------------------------------------------------ +PROGRAM test_MUG_revolved_lag +USE oft_base +USE multigrid, ONLY: multigrid_mesh +USE multigrid_build, ONLY: multigrid_construct +USE oft_la_base, ONLY: oft_vector, oft_matrix +USE oft_solver_base, ONLY: oft_solver +USE oft_solver_utils, ONLY: create_cg_solver, create_diag_pre +USE oft_lag_basis, ONLY: oft_lag_setup +USE oft_lag_operators, ONLY: lag_setup_interp, oft_lag_vproject, oft_lag_vgetmop, & + oft_lag_getmop, oft_lag_project, oft_lag_vrinterp +USE diagnostic, ONLY: vec_energy +USE mhd_utils, ONLY: mu0, proton_mass, elec_charge +USE xmhd_lag, ONLY: xmhd_run, xmhd_minlev, xmhd_sub_fields, xmhd_ML_lagrange, xmhd_ML_vlagrange +USE test_revolved_fields, ONLY: pinch_field +IMPLICIT NONE +CLASS(oft_solver), POINTER :: minv => NULL() +CLASS(oft_matrix), POINTER :: mop => NULL() +CLASS(oft_vector), POINTER :: u,v,b,b_init,vel,den,temp +TYPE(xmhd_sub_fields) :: equil_fields +TYPE(pinch_field), TARGET :: init_field +TYPE(oft_lag_vrinterp), TARGET :: bfield,vfield +TYPE(multigrid_mesh) :: mg_mesh +INTEGER(i4) :: io_unit,ierr +REAL(r8) :: v_alf,vol,b_energy,db_energy,v_energy +INTEGER(i4) :: order = 2 +INTEGER(i4) :: minlev = 1 +REAL(r8) :: c = 0.1d0 +REAL(r8) :: n0 = 1.d19 +NAMELIST/test_revolved_options/order,minlev,c,n0 +CALL oft_init +OPEN(NEWUNIT=io_unit,FILE=oft_env%ifile) +READ(io_unit,test_revolved_options,IOSTAT=ierr) +CLOSE(io_unit) +CALL multigrid_construct(mg_mesh) +IF(.NOT.mg_mesh%mesh%periodic%revolved)CALL oft_abort('Mesh is not revolved','test_MUG_revolved_lag',__FILE__) +ALLOCATE(xmhd_ML_lagrange,xmhd_ML_vlagrange) +CALL oft_lag_setup(mg_mesh,order,xmhd_ML_lagrange,ML_vlag_obj=xmhd_ML_vlagrange,minlev=minlev) +CALL lag_setup_interp(xmhd_ML_lagrange) +!---Pressure balance: n0 e T0 (1+te_factor)/2 = c^2/mu0 with te_factor=1 +init_field%mesh=>mg_mesh%mesh +init_field%c=c +init_field%T0=c**2/(mu0*n0*elec_charge) +!---Magnetic field +NULLIFY(mop) +CALL oft_lag_vgetmop(xmhd_ML_vlagrange%current_level,mop,"none") +CALL create_cg_solver(minv) +minv%A=>mop +minv%its=-3 +minv%atol=1.d-12 +CALL create_diag_pre(minv%pre) +CALL xmhd_ML_vlagrange%vec_create(v) +CALL xmhd_ML_vlagrange%vec_create(b) +CALL xmhd_ML_vlagrange%vec_create(b_init) +CALL xmhd_ML_vlagrange%vec_create(vel) +init_field%field='b' +CALL oft_lag_vproject(xmhd_ML_lagrange%current_level,init_field,v) +CALL b%set(0.d0) +CALL minv%apply(b,v) +CALL b_init%add(0.d0,1.d0,b) +CALL vel%set(0.d0) +CALL v%delete +CALL mop%delete +DEALLOCATE(v,mop) +CALL minv%pre%delete +CALL minv%delete +!---Temperature and density +NULLIFY(mop) +CALL oft_lag_getmop(xmhd_ML_lagrange%current_level,mop,"none") +CALL create_cg_solver(minv) +minv%A=>mop +minv%its=-3 +minv%atol=1.d-12 +CALL create_diag_pre(minv%pre) +CALL xmhd_ML_lagrange%vec_create(v) +CALL xmhd_ML_lagrange%vec_create(temp) +CALL xmhd_ML_lagrange%vec_create(den) +init_field%field='t' +CALL oft_lag_project(xmhd_ML_lagrange%current_level,init_field,v) +CALL temp%set(0.d0) +CALL minv%apply(temp,v) +CALL den%set(n0) +CALL v%delete +CALL mop%delete +DEALLOCATE(v,mop) +!---Run +xmhd_minlev=minlev +oft_env%pm=.FALSE. +equil_fields%B=>b +equil_fields%V=>vel +equil_fields%Ne=>den +equil_fields%Ti=>temp +CALL xmhd_run(equil_fields) +!---RMS velocity normalized by the wall Alfven speed and relative change in B +v_alf=c/SQRT(mu0*n0*2.d0*proton_mass) +vfield%u=>vel +CALL vfield%setup(xmhd_ML_lagrange%current_level) +v_energy=vec_energy(mg_mesh%mesh,vfield,order*2) +bfield%u=>b_init +CALL bfield%setup(xmhd_ML_lagrange%current_level) +b_energy=vec_energy(mg_mesh%mesh,bfield,order*2) +CALL b%add(1.d0,-1.d0,b_init) +bfield%u=>b +CALL bfield%setup(xmhd_ML_lagrange%current_level) +db_energy=vec_energy(mg_mesh%mesh,bfield,order*2) +vol=oft_mpi_sum(SUM(mg_mesh%mesh%cv)) +IF(oft_env%head_proc)THEN + OPEN(NEWUNIT=io_unit,FILE='revolved.results') + WRITE(io_unit,*)SQRT(v_energy/vol)/v_alf + WRITE(io_unit,*)SQRT(db_energy/b_energy) + CLOSE(io_unit) +END IF +CALL oft_finalize +END PROGRAM test_MUG_revolved_lag diff --git a/src/tests/physics/MUG/test_MUG_revolved_lag.py b/src/tests/physics/MUG/test_MUG_revolved_lag.py new file mode 100644 index 000000000..039e02406 --- /dev/null +++ b/src/tests/physics/MUG/test_MUG_revolved_lag.py @@ -0,0 +1,150 @@ +#------------------------------------------------------------------------------ +# Flexible Unstructured Simulation Infrastructure with Open Numerics (Open FUSION Toolkit) +# +# SPDX-License-Identifier: LGPL-3.0-only +#------------------------------------------------------------------------------ +# MUG tests: Z-pinch equilibrium on a rotationally periodic (revolved) wedge about the x-axis (xMHD with all fields +# on Lagrange elements). The azimuthal field and quadratic pressure are exactly represented, so the state must +# remain in equilibrium, which requires the vector fields (B and V) to be rotated correctly across the periodic +# boundaries. Run with the magnetic and velocity boundary conditions applied on the curved outer wall. +import os +import sys +import pytest +test_dir = os.path.abspath(os.path.dirname(__file__)) +sys.path.append(os.path.abspath(os.path.join(test_dir, '..', '..'))) +sys.path.append(os.path.abspath(os.path.join(test_dir, '..', '..', 'fem'))) +from oft_testing import run_OFT +from test_vlag_periodic import write_mesh, x_wedge + +# Input file template, filled in by the setup function +oft_in_template = """ +&runtime_options + ppn=1 + debug=0 + test_run=T +/ + +&mesh_options + meshname='revolved' + cad_type=0 + nlevels={nlevels} + nbase={nbase} + grid_order=1 +/ + +&native_mesh_options + filename='revolved_tet.h5' + reflect=T + ref_periodic=T +/ + +&test_revolved_options + order=2 + minlev={minlev} + c=0.1 + n0=1.E19 +/ + +&xmhd_options + xmhd_jcb=T + xmhd_advec=T + xmhd_adv_den=F + xmhd_adv_temp=T + xmhd_hall=F + xmhd_upwind=F + bbc='{bbc}' + vbc='{vbc}' + tbc='d' + dt=2.E-7 + eta=1.E-3 + nu_visc=1.E-1 + kappa_par=1.E-6,1.E-6 + kappa_perp=1.E-6,1.E-6 + nsteps=25 + rst_freq=1000 + lin_tol=1.E-12 + nl_tol=1.E-8 + nu_xmhd={nu_mhd} + maxextrap=2 + ittarget=100 +/ +""" + +# Multigrid preconditioner with GMRES/Jacobi smoothing on each level +mg_xml = """ + +
+      
+        
+          4
+          4
+          

+        
+
+
+
+
+""" + +# Allowed RMS velocity (relative to the wall Alfven speed) and relative change in B, well above the round-off level +# (~1E-8) reached when the vector fields are rotated correctly across the periodic boundaries +vel_tol = 1.E-6 +b_tol = 1.E-6 + + +def revolved_case(bbc, vbc, nbase=1, nlevels=1, minlev=2, nu_mhd='-1', nproc=1): + '''Write the input and mesh for the Z-pinch equilibrium and run test_MUG_revolved_lag + + The FE levels run from `minlev` to the mesh levels plus one (quadratic elements); a multigrid preconditioner + (`mg_xml`) is used if there is more than one. + ''' + os.chdir(test_dir) + write_mesh('revolved_tet.h5', x_wedge, 'tet') + with open('oft.in', 'w+') as fid: + fid.write(oft_in_template.format(bbc=bbc, vbc=vbc, nbase=nbase, nlevels=nlevels, minlev=minlev, nu_mhd=nu_mhd)) + if os.path.exists('revolved.results'): + os.remove('revolved.results') + xml_file = 'oft_in_xmhd.xml' + if nlevels+1 > minlev: + xml_file = 'oft_in_revolved_mg.xml' + with open(xml_file, 'w+') as fid: + fid.write(mg_xml) + return run_OFT("./test_MUG_revolved_lag oft.in {0}".format(xml_file), nproc, 600) + + +def validate_result(): + '''Check that the flow and the change in B remain at round-off level''' + with open('revolved.results', 'r') as fid: + vel_err = float(fid.readline()) + b_err = float(fid.readline()) + print("RMS V/v_A = {0:.4E}, |dB|/|B| = {1:.4E}".format(vel_err, b_err)) + retval = True + if not vel_err < vel_tol: + print("FAILED: Spurious flow generated! ({0})".format(vel_err)) + retval = False + if not b_err < b_tol: + print("FAILED: Magnetic field changed! ({0})".format(b_err)) + retval = False + return retval + + +@pytest.mark.parametrize("bbc,vbc", (('bc', 'all'), ('bc', 'norm'), ('ic', 'tang'))) +def test_pinch(bbc, vbc): + '''Z-pinch equilibrium on a revolved tetrahedral wedge (quadratic elements, single level) with the given + magnetic and velocity boundary conditions.''' + assert revolved_case(bbc, vbc) + assert validate_result() + + +def test_pinch_mg(): + '''Z-pinch equilibrium on a refined revolved tetrahedral wedge with a multigrid preconditioner, which uses + the interpolation operators for vector fields on revolved meshes (geometric and polynomial levels).''' + assert revolved_case('bc', 'all', nbase=2, nlevels=2, minlev=1, nu_mhd='2,2,2') + assert validate_result() + + +@pytest.mark.mpi +def test_pinch_mpi(): + '''Z-pinch equilibrium on a revolved tetrahedral wedge distributed over two MPI processes.''' + assert revolved_case('bc', 'all', nbase=1, nlevels=2, minlev=3, nproc=2) + assert validate_result() diff --git a/src/tests/physics/MUG/test_MUG_visc.py b/src/tests/physics/MUG/test_MUG_visc.py index cd832d2d4..7ed9d7168 100644 --- a/src/tests/physics/MUG/test_MUG_visc.py +++ b/src/tests/physics/MUG/test_MUG_visc.py @@ -103,16 +103,17 @@ def sound_linear(nu_long): u_hat = (expm(mat*t_final) @ x0)[1]/0.6 return np.array([-u_hat.imag, u_hat.real]) -def visc_case(visc_type,nu_visc,nu_par,b_dir): +def visc_case(visc_type,nu_visc,nu_par,b_dir,exe='./test_MUG_visc'): '''Write the input for a damped sound wave and run test_MUG_visc - `b_dir` is the (unnormalized) field direction as a namelist string. + `b_dir` is the (unnormalized) field direction as a namelist string. `exe` selects the driver program + (`./test_MUG_visc_lag` for the Lagrange-only module). ''' os.chdir(test_dir) with open('oft.in','w+') as fid: fid.write(oft_in_template.format(b_dir,visc_type,nu_visc,nu_par,d_dens,kappa)) # Single FE level: block-Jacobi/LU preconditioner inside one GMRES iteration (as in mug_testing.mug_command) - return run_OFT("./test_MUG_visc oft.in oft_in_xmhd.xml", 1, 600) + return run_OFT("{0} oft.in oft_in_xmhd.xml".format(exe), 1, 600) def validate_result(nu_long,err_exp): '''Compare the final velocity with linear theory diff --git a/src/tests/physics/MUG/test_MUG_visc_lag.F90 b/src/tests/physics/MUG/test_MUG_visc_lag.F90 new file mode 100644 index 000000000..6d8683597 --- /dev/null +++ b/src/tests/physics/MUG/test_MUG_visc_lag.F90 @@ -0,0 +1,195 @@ +!--------------------------------------------------------------------------------- +! Flexible Unstructured Simulation Infrastructure with Open Numerics (Open FUSION Toolkit) +! +! SPDX-License-Identifier: LGPL-3.0-only +!--------------------------------------------------------------------------------- +!> @file test_MUG_visc_lag.F90 +! +!> Regression test for xMHD_lag (all fields on Lagrange elements) viscosity. +!! Same as test_MUG_visc. A traveling sound wave is initialized in a triply +!! periodic box with a uniform magnetic field, which sets the field direction +!! for the parallel viscosity but is not advanced. The wave is +!! advanced for one period and the velocity is projected onto the initial +!! mode to measure viscous damping, which is compared to linear theory by the +!! driver script. +!! +!! @ingroup testing +!------------------------------------------------------------------------------ +PROGRAM test_MUG_visc_lag +USE oft_base +!--Grid +USE multigrid, ONLY: multigrid_mesh +USE multigrid_build, ONLY: multigrid_construct +!---Linear algebra +USE oft_la_base, ONLY: oft_vector, oft_matrix +USE oft_solver_base, ONLY: oft_solver +USE oft_solver_utils, ONLY: create_cg_solver, create_diag_pre +!---Lagrange FE space +USE oft_lag_basis, ONLY: oft_lag_setup +USE oft_lag_operators, ONLY: lag_setup_interp, oft_lag_vproject, oft_lag_vgetmop, & + oft_lag_getmop, oft_lag_project +!---Physics +USE oft_vector_inits, ONLY: uniform_field +USE mhd_utils, ONLY: elec_charge, proton_mass +USE xmhd_lag, ONLY: xmhd_run, xmhd_minlev, xmhd_taxis, temp_floor, xmhd_adv_b, & + xmhd_sub_fields, xmhd_ML_lagrange, xmhd_ML_vlagrange +USE test_phys_helpers, ONLY: sound_eig, lag_vec_mode_proj +IMPLICIT NONE +!---Mass matrix solvers +CLASS(oft_solver), POINTER :: minv => NULL() +CLASS(oft_matrix), POINTER :: mop => NULL() +CLASS(oft_solver), POINTER :: bminv => NULL() +CLASS(oft_matrix), POINTER :: bmop => NULL() +!---Local variables +CLASS(oft_vector), POINTER :: u,v,b,vel,n,temp +TYPE(xmhd_sub_fields) :: equil_fields +TYPE(sound_eig), TARGET :: sound_field +TYPE(uniform_field) :: b_field +TYPE(multigrid_mesh) :: mg_mesh +INTEGER(i4) :: io_unit,ierr +REAL(r8) :: T0,v_delta,proj(2,3) +INTEGER(i4) :: order = 2 +INTEGER(i4) :: minlev = 1 +REAL(r8) :: v_sound = 2.d4 +REAL(r8) :: N0 = 1.d19 +REAL(r8) :: delta = 1.d-3 +REAL(r8) :: b_dir(3) = (/0.d0,0.d0,1.d0/) +NAMELIST/test_visc_options/order,minlev,delta,b_dir +!------------------------------------------------------------------------------ +! Initialize enviroment +!------------------------------------------------------------------------------ +CALL oft_init +!---Read in options +OPEN(NEWUNIT=io_unit,FILE=oft_env%ifile) +READ(io_unit,test_visc_options,IOSTAT=ierr) +CLOSE(io_unit) +!------------------------------------------------------------------------------ +! Setup grid +!------------------------------------------------------------------------------ +CALL multigrid_construct(mg_mesh) +!------------------------------------------------------------------------------ +! Build FE structures +!------------------------------------------------------------------------------ +!--- Lagrange +ALLOCATE(xmhd_ML_lagrange,xmhd_ML_vlagrange) +CALL oft_lag_setup(mg_mesh,order,xmhd_ML_lagrange,ML_vlag_obj=xmhd_ML_vlagrange,minlev=minlev) +CALL lag_setup_interp(xmhd_ML_lagrange) +!------------------------------------------------------------------------------ +! Set uniform B along b_dir (sets bhat for parallel viscosity, not advanced) +!------------------------------------------------------------------------------ +NULLIFY(bmop) +CALL oft_lag_vgetmop(xmhd_ML_vlagrange%current_level,bmop,"none") +CALL create_cg_solver(bminv) +bminv%A=>bmop +bminv%its=-3 +bminv%atol=1.d-10 +CALL create_diag_pre(bminv%pre) +CALL xmhd_ML_vlagrange%vec_create(u) +CALL xmhd_ML_vlagrange%vec_create(v) +CALL xmhd_ML_vlagrange%vec_create(b) +b_field%val=b_dir/SQRT(SUM(b_dir**2)) +CALL oft_lag_vproject(xmhd_ML_lagrange%current_level,b_field,v) +CALL u%set(0.d0) +CALL bminv%apply(u,v) +CALL b%add(0.d0,1.d0,u) +CALL u%delete +CALL v%delete +CALL bmop%delete +DEALLOCATE(u,v,bmop) +CALL bminv%pre%delete +CALL bminv%delete +!------------------------------------------------------------------------------ +! Create Lagrange metric solver +!------------------------------------------------------------------------------ +NULLIFY(mop) +CALL oft_lag_getmop(xmhd_ML_lagrange%current_level,mop,"none") +CALL create_cg_solver(minv) +minv%A=>mop +minv%its=-3 +minv%atol=1.d-10 +CALL create_diag_pre(minv%pre) +!--- +CALL xmhd_ML_lagrange%vec_create(u) +CALL xmhd_ML_lagrange%vec_create(v) +CALL xmhd_ML_lagrange%vec_create(n) +CALL xmhd_ML_lagrange%vec_create(temp) +!------------------------------------------------------------------------------ +! Set n and T from sound wave init +!------------------------------------------------------------------------------ +sound_field%mesh=>mg_mesh%mesh +sound_field%delta=delta +sound_field%field='n' +CALL oft_lag_project(xmhd_ML_lagrange%current_level,sound_field,v) +CALL u%set(0.d0) +CALL minv%apply(u,v) +CALL n%add(0.d0,N0,u) +!--- +sound_field%field='t' +CALL oft_lag_project(xmhd_ML_lagrange%current_level,sound_field,v) +CALL u%set(0.d0) +CALL minv%apply(u,v) +T0=(v_sound**2)*3.d0*proton_mass/(5.d0*elec_charge) +CALL temp%add(0.d0,T0,u) +!--- +CALL u%delete +CALL v%delete +CALL mop%delete +DEALLOCATE(u,v,mop) +CALL minv%pre%delete +CALL minv%delete +!------------------------------------------------------------------------------ +! Create Lagrange vector metric solver +!------------------------------------------------------------------------------ +NULLIFY(mop) +CALL oft_lag_vgetmop(xmhd_ML_vlagrange%current_level,mop,"none") +minv%A=>mop +minv%its=-3 +minv%atol=1.d-10 +CALL create_diag_pre(minv%pre) +!--- +CALL xmhd_ML_vlagrange%vec_create(u) +CALL xmhd_ML_vlagrange%vec_create(v) +CALL xmhd_ML_vlagrange%vec_create(vel) +!------------------------------------------------------------------------------ +! Set V from sound wave init +!------------------------------------------------------------------------------ +sound_field%field='v' +CALL oft_lag_vproject(xmhd_ML_lagrange%current_level,sound_field,v) +CALL u%set(0.d0) +CALL minv%apply(u,v) +v_delta=T0*elec_charge/(proton_mass*v_sound) +CALL vel%add(0.d0,v_delta,u) +!--- +CALL u%delete +CALL v%delete +CALL mop%delete +DEALLOCATE(u,v,mop) +!------------------------------------------------------------------------------ +! Run simulation +!------------------------------------------------------------------------------ +xmhd_minlev=minlev +xmhd_taxis=2 +xmhd_adv_b=.FALSE. +oft_env%pm=.FALSE. +temp_floor=T0*1.e-2 +equil_fields%B=>b +equil_fields%V=>vel +equil_fields%Ne=>n +equil_fields%Ti=>temp +CALL xmhd_run(equil_fields) +!------------------------------------------------------------------------------ +! Project final velocity onto the initial mode (normalized by initial amplitude) +!------------------------------------------------------------------------------ +CALL vel%scale(1.d0/(v_delta*delta)) +CALL lag_vec_mode_proj(xmhd_ML_lagrange%current_level,mg_mesh%mesh,vel, & + sound_field%k_dir,sound_field%lam,order*2,proj) +IF(oft_env%head_proc)THEN + OPEN(NEWUNIT=io_unit,FILE='visc.results') + WRITE(io_unit,*)proj(:,1) + WRITE(io_unit,*)proj(:,2) + WRITE(io_unit,*)proj(:,3) + CLOSE(io_unit) +END IF +!---Finalize enviroment +CALL oft_finalize +END PROGRAM test_MUG_visc_lag diff --git a/src/tests/physics/MUG/test_MUG_visc_lag.py b/src/tests/physics/MUG/test_MUG_visc_lag.py new file mode 100644 index 000000000..6e6c32219 --- /dev/null +++ b/src/tests/physics/MUG/test_MUG_visc_lag.py @@ -0,0 +1,47 @@ +#------------------------------------------------------------------------------ +# Flexible Unstructured Simulation Infrastructure with Open Numerics (Open FUSION Toolkit) +# +# SPDX-License-Identifier: LGPL-3.0-only +#------------------------------------------------------------------------------ +# MUG tests: viscous damping of a sound wave in a triply periodic box (xMHD with all fields on Lagrange elements), +# with a uniform field that sets the direction of the parallel stress but is not advanced. Same cases, input template, +# and linear theory as test_MUG_visc.py, run with the test_MUG_visc_lag driver +from test_MUG_visc import visc_case as _visc_case, validate_result, nu_visc, b_magic + +def visc_case(visc_type,nu_visc,nu_par,b_dir): + return _visc_case(visc_type,nu_visc,nu_par,b_dir,exe='./test_MUG_visc_lag') + +#============================================================================ +# Isotropic viscosity +def test_kin(): + '''Sound wave damped by the `kin` viscous stress (nu grad V), which acts on the longitudinal velocity with + coefficient nu.''' + assert visc_case('kin',nu_visc,-1.,'0.,0.,1.') + assert validate_result(nu_visc,6.246030554160897E-003) + +def test_iso(): + '''Sound wave damped by the `iso` viscous stress (nu W, with W the rate of strain tensor), which acts on the + longitudinal velocity with coefficient 4/3 nu.''' + assert visc_case('iso',nu_visc,-1.,'0.,0.,1.') + assert validate_result(4.0*nu_visc/3.0,7.594990997046444E-003) + +#============================================================================ +# Parallel viscosity +def test_par_aligned(): + '''Sound wave along B damped by Braginskii's parallel stress (S. I. Braginskii, Rev. Plasma Phys. 1, 205 + (1965)) with eta_0 = m_i n0 nu_par, which acts on the longitudinal velocity with coefficient 4/3 nu_par, the same + as `iso`.''' + assert visc_case('kin',0.,nu_visc,'0.,0.,1.') + assert validate_result(4.0*nu_visc/3.0,7.728128491080409E-003) + +def test_par_perp(): + '''Sound wave across B damped by the parallel stress, which acts on the longitudinal velocity with coefficient + 1/3 nu_par, a quarter of the field-aligned case.''' + assert visc_case('kin',0.,nu_visc,'1.,0.,0.') + assert validate_result(nu_visc/3.0,3.337417272638117E-003) + +def test_par_magic(): + '''Sound wave at the angle to B where b.W.b = 0 (cos^2 = 1/3), so the parallel stress vanishes and the wave + is undamped.''' + assert visc_case('kin',0.,nu_visc,b_magic) + assert validate_result(0.0,1.452759337776555E-003) diff --git a/src/tests/physics/MUG/test_alfven.py b/src/tests/physics/MUG/test_alfven.py index 8b26ec275..18de31fd4 100644 --- a/src/tests/physics/MUG/test_alfven.py +++ b/src/tests/physics/MUG/test_alfven.py @@ -167,6 +167,45 @@ def test_lin_r1_p4_mpi(petsc_flag): assert alfven_setup(1,2,4,3,'0,0,2,2,2',linear=True,petsc=petsc_flag) assert validate_alfven(berr_exp,verr_exp,linear=True) +#============================================================================ +# Runs with geometric multigrid, refined tetrahedral mesh, quadratic elements +def test_nl_r2_p2_gmg(): + '''Nonlinear Alfven wave, quadratic elements on a once-refined tetrahedral mesh, serial, with a multigrid + preconditioner that includes a linear-element level on the coarse mesh (geometric and p-multigrid). Reference + errors match a single-level (direct solve) run on the same mesh.''' + berr_exp = 1.011290E-02 + verr_exp = 4.422328E-03 + assert alfven_setup(2,2,2,1,'2,2,2') + assert validate_alfven(berr_exp,verr_exp) +@pytest.mark.slow +@pytest.mark.mpi +def test_nl_r2_p2_gmg_mpi(): + '''Nonlinear Alfven wave, quadratic elements on a once-refined tetrahedral mesh, on two MPI processes, with + geometric multigrid down to the distributed base mesh.''' + berr_exp = 1.011290E-02 + verr_exp = 4.422328E-03 + assert alfven_setup(1,3,2,2,'0,0,2,2') + assert validate_alfven(berr_exp,verr_exp) +@pytest.mark.linear +@pytest.mark.slow +def test_lin_r2_p2_gmg(): + '''Linearized Alfven wave, quadratic elements on a once-refined tetrahedral mesh, serial, with geometric and + p-multigrid.''' + berr_exp = 1.013647E-02 + verr_exp = 4.472189E-03 + assert alfven_setup(2,2,2,1,'2,2,2',linear=True) + assert validate_alfven(berr_exp,verr_exp,linear=True) +@pytest.mark.linear +@pytest.mark.slow +@pytest.mark.mpi +def test_lin_r2_p2_gmg_mpi(): + '''Linearized Alfven wave, quadratic elements on a once-refined tetrahedral mesh, on two MPI processes, with + geometric multigrid down to the distributed base mesh.''' + berr_exp = 1.013647E-02 + verr_exp = 4.472189E-03 + assert alfven_setup(1,3,2,2,'0,0,2,2',linear=True) + assert validate_alfven(berr_exp,verr_exp,linear=True) + #============================================================================ # Nonlinear runs, hexahedral mesh, quadratic elements def test_hex_nl_r1_p2(petsc_flag=False): diff --git a/src/tests/physics/MUG/test_alfven_lag.py b/src/tests/physics/MUG/test_alfven_lag.py index 2d8ec8435..540a6796d 100644 --- a/src/tests/physics/MUG/test_alfven_lag.py +++ b/src/tests/physics/MUG/test_alfven_lag.py @@ -101,6 +101,27 @@ def test_lin_r1_p2_mpi(petsc_flag): assert alfven_setup(1,2,2,3,linear=True,petsc=petsc_flag) assert validate_alfven(berr_exp,verr_exp,linear=True) +# Linearized runs with backward Euler time stepping, tetrahedral mesh, quadratic elements +@pytest.mark.linear +def test_lin_r1_p2_euler(petsc_flag=False): + '''Linearized Alfven wave with first-order backward Euler steps (`xmhd_lin_bdf2=F`), quadratic elements on a + tetrahedral mesh, serial.''' + berr_exp = 4.9956453588323249E-002 + verr_exp = 4.7189969360939520E-002 + assert alfven_setup(1,1,2,2,linear=True,petsc=petsc_flag,bdf2=False) + assert validate_alfven(berr_exp,verr_exp,linear=True) +@pytest.mark.linear +@pytest.mark.mpi +@pytest.mark.coverage +@pytest.mark.parametrize("petsc_flag", (True, False)) +def test_lin_r1_p2_euler_mpi(petsc_flag): + '''Linearized Alfven wave with first-order backward Euler steps (`xmhd_lin_bdf2=F`), quadratic elements on a + tetrahedral mesh, on two MPI processes. Run with native and PETSc linear algebra.''' + berr_exp = 4.9956453588323249E-002 + verr_exp = 4.7189969360939520E-002 + assert alfven_setup(1,2,2,3,linear=True,petsc=petsc_flag,bdf2=False) + assert validate_alfven(berr_exp,verr_exp,linear=True) + #============================================================================ # Linearized runs, tetrahedral mesh, cubic elements @pytest.mark.linear @@ -145,6 +166,45 @@ def test_lin_r1_p4_mpi(petsc_flag): assert alfven_setup(1,2,4,3,'0,0,2,2,2',linear=True,petsc=petsc_flag) assert validate_alfven(berr_exp,verr_exp,linear=True) +#============================================================================ +# Runs with geometric multigrid, refined tetrahedral mesh, quadratic elements +def test_nl_r2_p2_gmg(): + '''Nonlinear Alfven wave, quadratic elements on a once-refined tetrahedral mesh, serial, with a multigrid + preconditioner that includes a linear-element level on the coarse mesh (geometric and p-multigrid). Reference + errors match a single-level (direct solve) run on the same mesh.''' + berr_exp = 6.723490E-03 + verr_exp = 4.733513E-03 + assert alfven_setup(2,2,2,1,'2,2,2') + assert validate_alfven(berr_exp,verr_exp) +@pytest.mark.slow +@pytest.mark.mpi +def test_nl_r2_p2_gmg_mpi(): + '''Nonlinear Alfven wave, quadratic elements on a once-refined tetrahedral mesh, on two MPI processes, with + geometric multigrid down to the distributed base mesh.''' + berr_exp = 6.723490E-03 + verr_exp = 4.733513E-03 + assert alfven_setup(1,3,2,2,'0,0,2,2') + assert validate_alfven(berr_exp,verr_exp) +@pytest.mark.linear +@pytest.mark.slow +def test_lin_r2_p2_gmg(): + '''Linearized Alfven wave, quadratic elements on a once-refined tetrahedral mesh, serial, with geometric and + p-multigrid.''' + berr_exp = 6.761864E-03 + verr_exp = 4.791372E-03 + assert alfven_setup(2,2,2,1,'2,2,2',linear=True) + assert validate_alfven(berr_exp,verr_exp,linear=True) +@pytest.mark.linear +@pytest.mark.slow +@pytest.mark.mpi +def test_lin_r2_p2_gmg_mpi(): + '''Linearized Alfven wave, quadratic elements on a once-refined tetrahedral mesh, on two MPI processes, with + geometric multigrid down to the distributed base mesh.''' + berr_exp = 6.761864E-03 + verr_exp = 4.791372E-03 + assert alfven_setup(1,3,2,2,'0,0,2,2',linear=True) + assert validate_alfven(berr_exp,verr_exp,linear=True) + #============================================================================ # Nonlinear runs, hexahedral mesh, quadratic elements def test_hex_nl_r1_p2(petsc_flag=False):