init_kt_grids_range Subroutine

public subroutine init_kt_grids_range(rhostar, kt_grids_range_config_in)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
real, intent(in) :: rhostar
type(kt_grids_range_config_type), intent(in), optional :: kt_grids_range_config_in

Contents

Source Code


Source Code

  subroutine init_kt_grids_range(rhostar, kt_grids_range_config_in)
    use theta_grid, only: drhodpsi
    use file_utils, only: error_unit
    use mp, only: mp_abort, proc0
    implicit none
    real, intent(in) :: rhostar
    type(kt_grids_range_config_type), intent(in), optional :: kt_grids_range_config_in
    integer :: ierr
    !> Temporary value of n0 as floating point
    real :: n0_tmp
    !> Temporary variable for swapping n0_{min,max}
    integer :: n0_swap

    if (initialized) return
    initialized = .true.

    call read_parameters_range(kt_grids_range_config_in)

    ierr = error_unit()

    !Override kyspacing_option in certain cases
    select case (kyspacingopt_switch)
    case (kyspacingopt_exp)
       if (aky_min <= 0) then
          if (proc0) write(ierr,'("Cannot use kyspacing_option=",A," with aky_min<=0.0 --> setting to",A)') &
               "'exponential'","'linear'"
          kyspacingopt_switch=kyspacingopt_linear
       end if
    end select

    if (n0_min > 0) then
       !Important to only do the following check and fix if naky > 0
       !as the second argument of `mod` must be non-zero. Failing to
       !guard against this can lead to different outcomes with
       !different compilers
       if (naky > 1) then
          !If toroidal mode number range would lead to non-integer mode numbers then
          !we adjust the range to try to fix this.
          if(mod(n0_max - n0_min, naky - 1) /= 0) then
             !Give a warning message that we're changing things
             if (proc0) then
                write(ierr,'("Warning: toroidal mode number range setup would lead to non-integer")')
                write(ierr,'("         mode numbers --> Attempting to adjust range slightly.")')
             end if

             !n0_max should be n0_min + I*(naky-1) or n0_min + (I+1)*(naky-1)
             !where I is int(n0_max-n0_min/(naky-1)) and we add 1 if the
             !remainder (n0_max-n0_min)/(naky-1) - I is > 0.5
             !Note it's naky-1 rather than naky as this is number of intervals

             !First calculate the floating step size
             n0_tmp = real(n0_max - n0_min) / (naky - 1)

             !Now construct the new upper limit, int(n0_tmp) = I
             !nint(n0_tmp-int(n0_tmp)) should be either 0 or 1 depending
             !on if the remainder is < or > 0.5
             n0_max = n0_min + (int(n0_tmp) + nint(n0_tmp - int(n0_tmp))) * (naky-1)

             !Double check it's all OK now, should always be fine but just
             !putting this here in case of logic error or strange cases.
             if (mod(n0_max - n0_min, naky-1) /= 0) &
                call mp_abort("Attempt to fix toroidal mode number range failed - aborting.", .true.)

          end if
       end if

       !If n0_min>n0_max swap values
       if (n0_min > n0_max) then
          if (proc0) write(ierr,'("Warning: Swapping max and min n0 values")')
          n0_swap = n0_min
          n0_min = n0_max
          n0_max = n0_swap
       end if

       !If n0_min == n0_max ensure naky=1
       if (n0_min == n0_max) then
          if (naky /= 1 .and. proc0) write(ierr,'("Warning: Forcing naky=1 as n0_min==n0_max.")')
          naky = 1
       end if

       !If there's only one mode then force n0_max=n0_min
       if (naky == 1) n0_max = n0_min

       !Set the upper and lower aky limits
       aky_max = n0_max * drhodpsi * rhostar
       aky_min = n0_min * drhodpsi * rhostar
    end if
  end subroutine init_kt_grids_range