gridgen4mod Module

FIXME : Add more documentation

MRH 15/11/2017 Previous versions of gridgen4_2 produced different theta grids on different machines In this version of gridgen4mod I have corrected this bug by making changes in the following subroutines (in gridgen4_2): heapsort- modified the inequalities in the sorting algorithm for reasons discussed below. get_thetares-corrected this routine so that thetares is 0 when thetares is computed using (theta (i) - theta (j)) and theta(i) equals theta (j) to machine error, and the output should be identically 0. get_lambdares-corrected this routine in the same manner as get_thetares, the output is 0 when it should be identically 0. gg4remove- modification of the inequalities used to choose which points to delete from the superset of theta points.

The function of this module seems to be to optimise the choice of theta grid points. It does this by taking the magnetic field and theta grid generated in the geometry module, and spline fitting the magnetic field on a fine-mesh theta grid. By using the metrics defined in get_lambdares and get_thetares the subroutine gg4remove removes points from the fine-mesh theta grid iteratively until the required number of points remain. The theta grid pair with the lowest value for the metric is deleted at each iteration. The reason for the bug was that these metrics were calculated in a sloppy manner, and the routine for finding the lowest value in the metric did not allow for the case that the metrics were identical up to machine precision: -In the 1st iteration of gg4remove all of the metrics should be identically 0 but were not -The 1st theta grid pair were deleted based on a decision made with machine precision sized values of the metric-this led to random behaviour -When all duplicate values of the theta grid pairs ----- (theta grid superset contains duplicate values of the theta grid pairs for reasons unbeknownst to me) had all been deleted, because the fine-mesh theta grid had almost equally spaced points the values for the metrics for the theta grid pairs near theta =0 were almost exactly equal. -Depending on the machine, the metric would be slightly lower for one pair of points than another and therefore the choice of which pair of points to delete 1st from the non-duplicate set was random. This behaviour led to different theta grids between different machines.

I fixed the above bug by biasing the gg4remove procedure to remove the 1st theta grid pair in array element order (of the sorted theta grid superset array) in the case that the metrics were equal up to machine error. The variable precision_bound controls this behaviour, precision_bound equal 0.0 returns this module to its original behaviour. Finite but small precision_bound is enough to remove machine dependence. I biased heapsort in a similar manner to enable debugging - the sorting algorithm was also machine dependent because of the duplicate theta grid points in the superset.

run_name.gridgen.200 contains the debug output from this module To see verbose output ensure that debug_output =.true.and verbose_debug_output =.true.


Contents


Subroutines

public subroutine gridgen4read(filename, ntheta, nperiod, ntgrid, nlambda, theta, alambda, gbdrift, gradpar, cvdrift, gds2, bmag, gds21, gds22, cvdrift0, gbdrift0)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename
integer, intent(inout) :: ntheta
integer, intent(inout) :: nperiod
integer, intent(inout) :: ntgrid
integer, intent(inout) :: nlambda
real, intent(out), dimension (-ntgrid:ntgrid) :: theta
real, intent(out), dimension (nlambda) :: alambda
real, intent(out), dimension (-ntgrid:ntgrid) :: gbdrift
real, intent(out), dimension (-ntgrid:ntgrid) :: gradpar
real, intent(out), dimension (-ntgrid:ntgrid) :: cvdrift
real, intent(out), dimension (-ntgrid:ntgrid) :: gds2
real, intent(out), dimension (-ntgrid:ntgrid) :: bmag
real, intent(out), dimension (-ntgrid:ntgrid) :: gds21
real, intent(out), dimension (-ntgrid:ntgrid) :: gds22
real, intent(out), dimension (-ntgrid:ntgrid) :: cvdrift0
real, intent(out), dimension (-ntgrid:ntgrid) :: gbdrift0

public subroutine gridgen4(n, nbmag, thetain, bmagin, npadd, alknob, epsknob, bpknob, extrknob, thetamax, deltaw, widthw, ntheta, nlambda, thetagrid, bmaggrid, alambdagrid)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n
integer, intent(in) :: nbmag
real, intent(in), dimension (nbmag) :: thetain
real, intent(in), dimension (nbmag) :: bmagin
integer, intent(in) :: npadd
real, intent(in) :: alknob
real, intent(in) :: epsknob
real, intent(in) :: bpknob
real, intent(in) :: extrknob
real, intent(in) :: thetamax
real, intent(in) :: deltaw
real, intent(in) :: widthw
integer, intent(inout) :: ntheta
integer, intent(inout) :: nlambda
real, intent(out), dimension (ntheta+1) :: thetagrid
real, intent(out), dimension (ntheta+1) :: bmaggrid
real, intent(out), dimension (nlambda) :: alambdagrid

public subroutine gridgen4_1(n, nbmag, thetain, bmagin, npadd, alknob, epsknob, bpknob, extrknob, thetamax, deltaw, widthw, tension, ntheta, nlambda, thetagrid, bmaggrid, alambdagrid)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n
integer, intent(in) :: nbmag
real, intent(in), dimension (nbmag) :: thetain
real, intent(in), dimension (nbmag) :: bmagin
integer, intent(in) :: npadd
real, intent(in) :: alknob
real, intent(in) :: epsknob
real, intent(in) :: bpknob
real, intent(in) :: extrknob
real, intent(in) :: thetamax
real, intent(in) :: deltaw
real, intent(in) :: widthw
real, intent(in) :: tension
integer, intent(inout) :: ntheta
integer, intent(inout) :: nlambda
real, intent(out), dimension (ntheta+1) :: thetagrid
real, intent(out), dimension (ntheta+1) :: bmaggrid
real, intent(out), dimension (nlambda) :: alambdagrid

public subroutine gridgen4_2(n, nbmag, thetain, bmagin, npadd, alknob, epsknob, bpknob, extrknob, thetamax, deltaw, widthw, tension, ntheta, nbset, thetagrid, bmaggrid, bset)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n
integer, intent(in) :: nbmag
real, intent(in), dimension (nbmag) :: thetain
real, intent(in), dimension (nbmag) :: bmagin
integer, intent(in) :: npadd
real, intent(in) :: alknob
real, intent(in) :: epsknob
real, intent(in) :: bpknob
real, intent(in) :: extrknob
real, intent(in) :: thetamax
real, intent(in) :: deltaw
real, intent(in) :: widthw
real, intent(in) :: tension
integer, intent(inout) :: ntheta
integer, intent(inout) :: nbset
real, intent(out), dimension (ntheta+1) :: thetagrid
real, intent(out), dimension (ntheta+1) :: bmaggrid
real, intent(out), dimension (nbset) :: bset

public subroutine dsmooth(n, xin, yin, yout)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n
real, intent(in), dimension (:) :: xin
real, intent(in), dimension (:) :: yin
real, intent(out), dimension (:) :: yout

public subroutine d2smooth(n, xin, yin, yout)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n
real, intent(in), dimension (:) :: xin
real, intent(in), dimension (:) :: yin
real, intent(out), dimension (:) :: yout

public subroutine smooth(n, xin, yin, var, yout, ifail)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n
real, intent(in), dimension (:) :: xin
real, intent(in), dimension (:) :: yin
real, intent(inout) :: var
real, intent(out), dimension (:) :: yout
integer, intent(out) :: ifail

public subroutine cubgcv(x, f, df, n, y, c, ic, var, job, se, wk, ier)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
real, dimension (:) :: x
real, dimension (:) :: f
real, dimension (:) :: df
integer :: n
real, dimension (:) :: y
real, dimension (:,:) :: c
integer :: ic
real :: var
integer :: job
real, dimension (:) :: se
real, dimension (0:n+1,7) :: wk
integer :: ier

public subroutine spint1(x, avh, y, dy, avdy, n, a, c, ic, r, t, ier)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
real, dimension (:) :: x
real :: avh
real, dimension (:) :: y
real, dimension (:) :: dy
real :: avdy
integer :: n
real, dimension (:) :: a
real, dimension (:,:) :: c
integer :: ic
real, dimension (0:n+1,3) :: r
real, dimension (0:n+1,2) :: t
integer :: ier

public subroutine spfit1(x, avh, dy, n, rho, p, q, fun, var, stat, a, c, ic, r, t, u, v)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
real, dimension (:) :: x
real :: avh
real, dimension (:) :: dy
integer :: n
real :: rho
real :: p
real :: q
real :: fun
real :: var
real, dimension (6) :: stat
real, dimension (:) :: a
real, dimension (ic,3) :: c
integer :: ic
real, dimension (0:n+1,3) :: r
real, dimension (0:n+1,2) :: t
real, dimension (0:n+1) :: u
real, dimension (0:n+1) :: v

public subroutine sperr1(x, avh, dy, n, r, p, var, se)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
real, dimension (:) :: x
real :: avh
real, dimension (:) :: dy
integer :: n
real, dimension (0:n+1, 3) :: r
real :: p
real :: var
real, dimension (:) :: se

public subroutine spcof1(x, avh, y, dy, n, p, q, a, c, ic, u, v)

FIXME : Add documentation

Arguments

Type IntentOptional Attributes Name
real, dimension (:) :: x
real :: avh
real, dimension (:) :: y
real, dimension (:) :: dy
integer :: n
real :: p
real :: q
real, dimension (:) :: a
real, dimension (ic, 3) :: c
integer :: ic
real, dimension (0:n+1) :: u
real, dimension (0:n+1) :: v