orbit.f90 Source File


This file depends on

sourcefile~~orbit.f90~~EfferentGraph sourcefile~orbit.f90 orbit.f90 sourcefile~constants.f90 constants.f90 sourcefile~orbit.f90->sourcefile~constants.f90 sourcefile~field_instance.f90 field_instance.f90 sourcefile~orbit.f90->sourcefile~field_instance.f90 sourcefile~params.f90 params.f90 sourcefile~orbit.f90->sourcefile~params.f90 sourcefile~field_instance.f90->sourcefile~constants.f90 sourcefile~field_base.f90 field_base.f90 sourcefile~field_instance.f90->sourcefile~field_base.f90 sourcefile~params.f90->sourcefile~constants.f90 sourcefile~field_base.f90->sourcefile~constants.f90

Files dependent on this one

sourcefile~~orbit.f90~~AfferentGraph sourcefile~orbit.f90 orbit.f90 sourcefile~bounce.f90 bounce.f90 sourcefile~bounce.f90->sourcefile~orbit.f90 sourcefile~grid.f90 grid.f90 sourcefile~grid.f90->sourcefile~bounce.f90 sourcefile~electric_rabe.f90 electric_rabe.f90 sourcefile~electric_rabe.f90->sourcefile~grid.f90 sourcefile~precession.f90 precession.f90 sourcefile~electric_rabe.f90->sourcefile~precession.f90 sourcefile~saved_bounce_integrals.f90 saved_bounce_integrals.f90 sourcefile~electric_rabe.f90->sourcefile~saved_bounce_integrals.f90 sourcefile~precession.f90->sourcefile~grid.f90 sourcefile~saved_bounce_integrals.f90->sourcefile~grid.f90

Source Code

module orbit
    use constants, only: dp

    implicit none

contains

    !ToDo make module from all global things like this one
    !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
    !
    subroutine elefie_can(x, derphi)
        !
        ! Computes the derivatives of the electrostatic potential over
        ! coordinates (covariant electric field). Potential is normalized
        ! to reference temperature : phinor = e phi / T.
        !
        !   Input parameters:
        !             formal:    x       -   array of coordinates
        !
        !   Output parameters:
        !             formal:    derphi  -   array of derivatives
        !
        real(dp), dimension(3) :: x, derphi
        !
        derphi = 0.d0
        !
    end subroutine elefie_can
    !
    !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
    !
    subroutine velo_can(tau, z, vz)
        !
        !
        !  Computes the components of the 5-D velocity          -  vz
        !  for given set of phase space coordinates             -  z.
        !
        !  Warning !!!  The dimensionless time is used (see below)
        !
        !  Order of the coordinates is the following:
        !  z(i) = x(i)  for i=1,2,3     - spatial coordinates with real
        !                                 dimension of the general covariant
        !                                 space coordinate system
        !  z(4) = p                     - momentum  module normalized to
        !                                 thermal momentum and sqrt(2);
        !  z(5) = alambd                - cosine of the pitch-angle
        !
        !  Input parameters:
        !            formal:  tau    -   dimensionless time: tau=sqrt(2*T/m)*t
        !                     z      -   see above
        !            common:  rmu    -   inverse relativistic temperature
        !                     ro0    -   Larmor radius for the reference
        !                                magnetic field and temperature:
        !                                ro0=sqrt(2*T/m)/(e*B_ref/m*c)
        !  Output parameters:
        !            formal:  vz     -   see above
        !
        !  Called routines: magfie, elefie_can
        !
        use params, only: rmu, ro0
        use field_instance, only: magfie
        !
        implicit none
        !
        integer :: i
        !
        real(dp), intent(in) :: tau
        real(dp), intent(in) :: z(:)
        real(dp), intent(out) :: vz(:)
        real(dp) x, bmod, sqrtg, bder, hcovar, hctrvr, hcurl
        real(dp) derphi
        real(dp) p, alambd, p2, ovmu, gamma2, gamma, ppar, vpa, coala
        real(dp) rmumag, rovsqg, rosqgb, rovbm
        real(dp) a_phi, a_b, a_c, hstar
        real(dp) s_hc, hpstar, phidot, bra
        !
        dimension x(3), bder(3), hcovar(3), hctrvr(3), hcurl(3)
        dimension derphi(3)
        dimension a_phi(3), a_b(3), a_c(3), hstar(3)
        !
        do i = 1, 3
            x(i) = z(i)
        end do
        !
        ! in magfie: x(i)   - set of 3 curvilinear space coordinates (input)
        !            bmod   - dimensionless magnetic field module: bmod=B/B_ref
        !            sqrtg  - Jacobian of space coordinates (square root of
        !                     metric tensor
        !            bder   - derivatives of logarithm of bmod over space coords
        !                     (covariant vector)
        !            hcovar - covariant components of the unit vector along
        !                     the magnetic field
        !            hctrvr - contravariant components of the unit vector along
        !                     the magnetic field
        !            hcurl  - contravariant components of the curl of this vector
        !
        call magfie(x, bmod, sqrtg, bder, hcovar, hctrvr, hcurl)
        !
        ! in elefie: x(i)   - space coords (input, see above)
        !            derphi - derivatives of the dimensionless electric potential
        !                     phihat=e*phi/T over space coords (covar. vector)
        !
        call elefie_can(x, derphi)
        !
        p = z(4)
        alambd = z(5)
        !
        p2 = p*p
        ovmu = 2.d0/rmu
        gamma2 = p2*ovmu + 1.d0
        gamma = dsqrt(gamma2)
        ppar = p*alambd
        ! vpa - dimensionless parallel velocity: vpa=v_parallel/sqrt(2*T/m)
        vpa = ppar/gamma
        coala = (1.d0 - alambd**2)
        ! rmumag - magnetic moment
        rmumag = .5d0*p2*coala/bmod
        !
        rovsqg = ro0/sqrtg
        rosqgb = .5d0*rovsqg/bmod
        rovbm = ro0/bmod
        !
        a_phi(1) = (hcovar(2)*derphi(3) - hcovar(3)*derphi(2))*rosqgb
        a_b(1) = (hcovar(2)*bder(3) - hcovar(3)*bder(2))*rovsqg
        a_phi(2) = (hcovar(3)*derphi(1) - hcovar(1)*derphi(3))*rosqgb
        a_b(2) = (hcovar(3)*bder(1) - hcovar(1)*bder(3))*rovsqg
        a_phi(3) = (hcovar(1)*derphi(2) - hcovar(2)*derphi(1))*rosqgb
        a_b(3) = (hcovar(1)*bder(2) - hcovar(2)*bder(1))*rovsqg
        !
        s_hc = 0.d0
        do i = 1, 3
            a_c(i) = hcurl(i)*rovbm
            s_hc = s_hc + a_c(i)*hcovar(i)
            hstar(i) = hctrvr(i) + ppar*a_c(i)
        end do
        hpstar = 1.d0 + ppar*s_hc
        !
        ! velocities in the coordinate space
        !
        ! phidot - derivative of the dmls el. potential over dmls time
        phidot = 0.d0
        do i = 1, 3
            bra = vpa*hstar(i) + a_phi(i) + a_b(i)*rmumag/gamma
            vz(i) = bra/hpstar
            phidot = phidot + vz(i)*derphi(i)
        end do
        !
        ! velocities in the phase space
        !
        vz(4) = -0.5d0*gamma*phidot/p
        vz(5) = -(0.5d0*coala/hpstar)*(sum(hstar*derphi)/p &
                                     + p*sum(hstar*bder)/gamma + alambd*sum(a_phi*bder))
        !
        vz(6) = alambd**2/bmod !dI/dtau
        vz(7) = 1.0_dp ! dtau/dtau
        ! radial excursion integrated from zero, avoids cancellation in
        ! z_end(1) - z_start(1) when the drift is tiny compared to s
        vz(8) = vz(1)
    end subroutine velo_can
    !
    !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
    !
    subroutine velo_axis(tau, z_axis, vz_axis)
        !
        ! Here variables z(1)=s and z(2)=theta are replaced with
        ! x_axis(1)=x=sqrt(s)*cos(theta) and x_axis(2)=y=sqrt(s)*sin(theta)
        !
        implicit none
        !
        real(dp), intent(in) :: tau
        real(dp), intent(in) :: z_axis(:)
        real(dp), intent(out) :: vz_axis(:)
        real(dp) :: derlogsqs
        integer, parameter :: ndim = 8
        real(dp), dimension(ndim) :: z, vz
        !
        z(1) = sqrt(z_axis(1)**2 + z_axis(2)**2)
        z(1) = max(z(1), 1.d-8)
        z(2) = atan2(z_axis(2), z_axis(1))
        z(3:ndim) = z_axis(3:ndim)
        !
        call velo_can(tau, z, vz)
        !
        derlogsqs = vz(1)/z(1)
        vz_axis(1) = derlogsqs*z_axis(1) - vz(2)*z_axis(2)
        vz_axis(2) = derlogsqs*z_axis(2) + vz(2)*z_axis(1)
        vz_axis(3:ndim) = vz(3:ndim)
        !
    end subroutine velo_axis
    !
    !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
    !
    subroutine orbit_timestep_axis(z, dtau, dtaumin, relerr, ierr)
        use odeint_allroutines_sub, only: odeint_allroutines
        !
        implicit none
        !
        integer, parameter :: ndim = 8, nstepmax = 1000000
        real(dp), parameter :: snear_axis = 0.01d0
        !
        logical :: near_axis
        integer :: ierr
        real(dp) :: dtau, dtaumin, phi, tau1, tau2, z1, z2
        real(dp) :: relerr
        !
        real(dp), dimension(2) :: y
        real(dp), dimension(ndim) :: z
        !
        if (abs(dtaumin*nstepmax) .le. abs(dtau)) then
            ierr = 2
            print *, 'orbit_timestep: number of steps exceeds nstepmax'
            return
        end if
        !
        ierr = 0
        y(1) = z(1)
        y(2) = z(2)
        phi = z(3)
        !
        call chamb_can(y, phi, ierr)
        !
        if (ierr .eq. 1) return
        tau1 = 0.d0
        tau2 = dtaumin
        !
        near_axis = .false.
        !
     do while ((dtau > 0 .and. (tau2 .lt. dtau)) .or. (dtau < 0 .and. (tau2 .gt. dtau)))
            if (near_axis) then
                if (z(1)**2 + z(2)**2 .gt. snear_axis**2) then
                    near_axis = .false.
                    z1 = sqrt(z(1)**2 + z(2)**2)
                    z2 = atan2(z(2), z(1))
                    z(1) = z1
                    z(2) = z2
                    !
                    call odeint_allroutines(z, ndim, tau1, tau2, relerr, velo_can)
                    !
                    y(1) = z(1)
                    y(2) = z(2)
                    phi = z(3)
                    !
                    call chamb_can(y, phi, ierr)
                    !
                    if (ierr .eq. 1) return
                else
                    !
                    call odeint_allroutines(z, ndim, tau1, tau2, relerr, velo_axis)
                    !
                end if
            else
                if (z(1) .lt. snear_axis) then
                    near_axis = .true.
                    z1 = z(1)*cos(z(2))
                    z2 = z(1)*sin(z(2))
                    z(1) = z1
                    z(2) = z2
                    !
                    call odeint_allroutines(z, ndim, tau1, tau2, relerr, velo_axis)
                    !
                else
                    !
                    call odeint_allroutines(z, ndim, tau1, tau2, relerr, velo_can)
                    !
                    y(1) = z(1)
                    y(2) = z(2)
                    phi = z(3)
                    !
                    call chamb_can(y, phi, ierr)
                    !
                    if (ierr .eq. 1) return
                    !
                end if
            end if
            tau1 = tau2
            tau2 = tau2 + dtaumin
        end do
        !
        tau2 = dtau
        !
        if (near_axis) then
            if (z(1)**2 + z(2)**2 .gt. snear_axis**2) then
                near_axis = .false.
                z1 = sqrt(z(1)**2 + z(2)**2)
                z2 = atan2(z(2), z(1))
                z(1) = z1
                z(2) = z2
                !
                call odeint_allroutines(z, ndim, tau1, tau2, relerr, velo_can)
                !
                y(1) = z(1)
                y(2) = z(2)
                phi = z(3)
                !
                call chamb_can(y, phi, ierr)
                !
                if (ierr .eq. 1) return
            else
                !
                call odeint_allroutines(z, ndim, tau1, tau2, relerr, velo_axis)
                !
            end if
        else
            if (z(1) .lt. snear_axis) then
                near_axis = .true.
                z1 = z(1)*cos(z(2))
                z2 = z(1)*sin(z(2))
                z(1) = z1
                z(2) = z2
                !
                call odeint_allroutines(z, ndim, tau1, tau2, relerr, velo_axis)
                !
            else
                !
                call odeint_allroutines(z, ndim, tau1, tau2, relerr, velo_can)
                !
                y(1) = z(1)
                y(2) = z(2)
                phi = z(3)
                !
                call chamb_can(y, phi, ierr)
                !
                if (ierr .eq. 1) return
                !
            end if
        end if
        !
        if (near_axis) then
            z1 = sqrt(z(1)**2 + z(2)**2)
            z2 = atan2(z(2), z(1))
            z(1) = z1
            z(2) = z2
        end if
        !
    end subroutine orbit_timestep_axis
    !odeint
    subroutine chamb_can(y, phi, ierr)
!
        use chamb_mod, only: rnegflag
!
! checks whether the point is inside the vacuum chamber
!  Input parameters:
!            formal: y(i) - coordinates on the poloidal cross section
!                    phi  - toroidal angle
! Outout parameters:
!            formal: ierr -error code (0 if the point is inside 1 - othervice)
!
        implicit none
!
        integer :: ierr
        double precision :: phi
        double precision, dimension(2) :: y
!
        if (y(1) .ge. 1.d0) then
            ierr = 1
        else
            ierr = 0
        end if
!
        if (rnegflag) then
            rnegflag = .false.
            ierr = 2
        end if
!
    end subroutine chamb_can
    !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
end module orbit