Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 131 additions & 40 deletions src/base/oft_stitching.F90
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/docs/doc_mug_main.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
25 changes: 22 additions & 3 deletions src/docs/doc_mug_xmhd_eqns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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.
1 change: 1 addition & 0 deletions src/examples/MUG/gyro_gravitational/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Loading