Program TESTPALEO

!                 >>>>>   N O T E   <<<<<
! {{{ and }}} are VIM folding markers. DO NOT REMOVE THEM.
! Vim is an improved version of the Vi text editor.

Use modpaleo

Implicit None

! SPECIFICATION {{{
!------- Constants -------------------------------------------------

Integer, Parameter :: R8 = SELECTED_REAL_KIND(12,100)

Integer, Parameter :: &
   NPMAX = 200, &! Maximum data points allowed in arrays
   hfIn  = 34,  &! Input file handle
   hfOut = 35    ! Output file handle

Integer, Parameter :: BADVAL = -1000000

!------- Input Variables -------------------------------------------

Integer :: npoints ! Number of radial points

Real(R8), Dimension(NPMAX) :: &
   rminor, &! Half-width of magnetic surfaces, r [m]
   te,     &! Electron temperature [keV]
   ne,     &! Electron density [m^-3]
   zeff,   &! Z_effective
   q,      &! Magnetic q
   qprime, &! Derivative of q w.r.t. rho (rho=r/a)
   qdprime,&! Second derivative of q w.r.t. rho
   etanc    ! Neoclassical resistivity [Ohm.m]

Real(R8) :: &
   rminbnd, &! Effective radius = sqrt(<R^-2>/<|gradrho|^2/R^2>) 
             ! approximately equal to minor radius [m] 
             ! [see e.g. Eq.(72) of J.D. Callen POP 12, 092512, 20005] 
   rmajbnd   ! Major radius [m]

namelist /paleo_data/ &
   npoints, rminor,  te,      ne,      zeff,    &
   q,       qprime,  qdprime, etanc,   rminbnd, rmajbnd

!------- Output Variables ------------------------------------------

Real(R8), Dimension(NPMAX) :: &
   xkePLC, &! Electron thermal diffusivity [m^2/s]
   helmul   ! Helical multiplier (M) caused by helically
            ! resonant radial diffusion

!------- Local Variables ------------------------------------------

Integer :: nerr=0 ! IO error code

Integer :: j ! Loop counter
!}}}
!------- EXECUTION BODY --------------------------------------------

Open( hfIn, file='input', status='old', iostat=nerr )
If ( nerr /= 0 ) Then
   Write(*,'("Cannot open input file!")')
   Stop
End If

Open( hfOut, file='output', status='replace', iostat=nerr )
If ( nerr /= 0 ) Then
   Write(*,'("ERROR: Cannot create output file!")')
   Stop
End If

Write(*,'("Processing...")')

!.. Input variables are initialized with bad values
npoints = BADVAL
rminor  = dble(BADVAL)
te      = dble(BADVAL)
ne      = dble(BADVAL)
zeff    = dble(BADVAL)
q       = dble(BADVAL)
qprime  = dble(BADVAL)
etanc   = dble(BADVAL)
rminbnd = dble(BADVAL)
rmajbnd = dble(BADVAL)

!.. Read from the input file
Read( hfIn, NML=paleo_data, iostat=nerr )

If ( nerr /= 0 ) Then
   Write(*,'("ERROR: Unrecognized input data!")')
   Stop
End If
Close( unit= hfIn )

Call check_input( dble(npoints) , "npoints"  )
Call check_input( rminor(1) , "rminor"  )
Call check_input( te(1)     , "te"      )
Call check_input( ne(1)     , "ne"      )
Call check_input( zeff(1)   , "zeff"    )
Call check_input( q(1)      , "q"       )
Call check_input( qprime(1) , "qprime"  )
Call check_input( qdprime(1), "qdprime" )
Call check_input( etanc(1)  , "etanc"   )
Call check_input( rminbnd   , "rminbnd" )
Call check_input( rmajbnd   , "rmajbnd" )

!.. Write input profiles
Write(hfOut,'("#.. Each column from 1 to 7 below represents&
   & an input profile, which was used in the file named ""input""")')
Write(hfOut,'("# "I6,8I11)') (j,j=1,8)
Write(hfOut,'("#unit"A5,31A11)') &
   "m","m^-3"," ","keV"," "," "," ","Ohm.m"
Write(hfOut,'("# "A9,31A11)') &
   "rminor","ne","zeff","te","q","qprime","qdprime","etanc"

Do j=1, npoints
   write(hfOut,'(F11.6,7ES11.3)') &
      rminor(j), ne(j), zeff(j), &
      te(j), q(j), qprime(j), qdprime(j), etanc(j)
End Do

xkePLC = 0._R8
helmul = 0._R8

Do j = 1, npoints

   Call paleo( &
      te      = te(j),     ne      = ne(j),      &
      zeff    = zeff(j),   q       = q(j),       &
      qprime  = qprime(j), qdprime = qdprime(j), &
      etanc   = etanc(j),                        &
      rmajbnd = rmajbnd,   rminbnd = rminbnd,    &
      xkePLC  = xkePLC(j), helmul  = helmul(j) )

End Do

Write(hfOut,'("#.. Output profiles: electron thermal diffusivity&
   & and helical multiplier")')
Write(hfOut,'("#unit"A6,3A11)') "m","m^2/s",""
Write(hfOut,'("# "A9,3A11)') "rminor","xkePLC","helmul"

Do j=1, npoints
   write(hfOut,'(0P2F11.6,3ES11.3)') &
      rminor(j), xkePLC(j), helmul(j)
End Do

Close( unit= hfOut )

write(*,'("MODPALEO finished sucessfully!")')

Contains

Subroutine check_input( vval, vname )!{{{
! This internal subroutine checks for variable with BADVAL
! If so, print an error message and stop the program
Implicit None

Real(R8), Intent(In) :: vval ! the value to be checked
Character(len=*) :: vname ! the corresponding variable name

If ( abs( vval - dble(BADVAL) ) < 1e-6 ) Then
   Write(*,'("ERROR: Variable """A7""" is not provided in the&
      & input file!"/"Stop")') trim(vname)
   Stop
End If

End Subroutine check_input!}}}

End Program TESTPALEO
