! $Id: runTeq.f90,v 1.2 2007/07/23 16:00:58 meyer Exp $

Module runTeq_mod

  USE setTeq_mod

  IMPLICIT NONE

CONTAINS

  SUBROUTINE genTeq

    t_ncallsf = 0 ! necessary for keqic=1 to work in equila
    IF(t_rfix==0) t_rfix=t_rb
    CALL pfcmem   ! set up filaments
    IF ( t_keqic==1 .OR. t_keqic==2 ) THEN
       IF(ALLOCATED(t_cscl00)) THEN
          IF (t_cscl00(1)/=0.0d0 .AND. ASSOCIATED(t_rbd00)) THEN
             IF (t_nc/=0) t_cc(1:t_nc)=t_cscl00(1:t_nc)
             IF (t_nbd/=0) t_rbd(1:t_nbd)=t_rbd00(1:t_nbd)
             IF (t_nbd/=0) t_zbd(1:t_nbd)=t_zbd00(1:t_nbd)
             IF (t_nfbd/=0) t_rfbd(1:t_nfbd)=t_rfbd00(1:t_nfbd)
             IF (t_nfbd/=0) t_zfbd(1:t_nfbd)=t_zfbd00(1:t_nfbd)
          ELSE
             IF (t_nc/=0) t_cscl00(1:t_nc)=t_cc(1:t_nc)
          END IF
       END IF
    ELSEIF( t_keqic==3 ) THEN
       IF (t_nc/=0) t_cc(1:t_nc)=t_cscl01(1:t_nc)
       IF (t_nbd/=0) t_rbd(1:t_nbd)=t_rbd01(1:t_nbd)
       IF (t_nbd/=0) t_zbd(1:t_nbd)=t_zbd01(1:t_nbd)
       IF (t_nfbd/=0) t_rfbd(1:t_nfbd)=t_rfbd01(1:t_nfbd)
       IF (t_nfbd/=0) t_zfbd(1:t_nfbd)=t_zfbd01(1:t_nfbd)
    END IF
    CALL getstrg(1) ! get remaining memory; also calls setgeo---the grid
  END SUBROUTINE genTeq

  SUBROUTINE exeTeq

    t_ncallsf = t_ncallsf + 1
    IF ( t_ksave.eq.1 ) THEN
       IF ( t_nc.ne.0) t_cscl00(1: t_nc)= t_cc(1: t_nc)
       t_rbd00=t_rbd
       t_zbd00=t_zbd
       t_rfbd00=t_rfbd
       t_zfbd00=t_zfbd
    END IF

    call gs_equil0
    call teq_interface

    IF ( (t_lasteqsav==0 .AND. t_lasteq==1) .OR. (t_lasteqsav==1 .AND.&
         ( (t_pkgname=="eq") .OR. ((t_pkgname/="eq") .AND. (t_lasteq==1)&
         ) ) ) .OR. (t_lasteqsav==2) ) THEN
       IF (t_nc/=0) t_cscl01(1:t_nc)=t_cc(1:t_nc)
       IF (t_jmkm/=0) t_psi01(1:t_jmkm)=t_psi(1:t_jmkm)
       IF (t_nbd/=0) t_rbd01(1:t_nbd)=t_rbd(1:t_nbd)
       IF (t_nbd/=0) t_zbd01(1:t_nbd)=t_zbd(1:t_nbd)
       IF (t_nfbd/=0) t_rfbd01(1:t_nfbd)=t_rfbd(1:t_nfbd)
       IF (t_nfbd/=0) t_zfbd01(1:t_nfbd)=t_zfbd(1:t_nfbd)
       t_prsx01=t_prsx
    END IF
    ! prepare for starting up in future from current state (t_keqic >0 )
    ! should do this here only IF package=eq, and do ceq's in ceqexe
    IF ( t_ksave ==2 .AND. (t_lasteq == 1 .OR. t_pkgname=="eq") ) THEN
       IF (t_nc/=0) t_cscl00(1:t_nc)=t_cc(1:t_nc)
       IF (t_jmkm/=0) t_psi00(1:t_jmkm)=t_psi(1:t_jmkm)
       IF (t_nbd/=0) t_rbd00(1:t_nbd)=t_rbd(1:t_nbd)
       IF (t_nbd/=0) t_zbd00(1:t_nbd)=t_zbd(1:t_nbd)
       IF (t_nbd/=0) t_rfbd00(1:t_nbd)=t_rfbd(1:t_nbd)
       IF (t_nbd/=0) t_zfbd00(1:t_nbd)=t_zfbd(1:t_nbd)
       t_prsx00=t_prsx
       t_ksave = 0 ! if one needs lasteq=1 for meq solve, this wrecks save
    END IF
  END SUBROUTINE exeTeq

  SUBROUTINE runTeq
    CALL genTeq
    CALL exeTeq 
  END SUBROUTINE runTeq

  SUBROUTINE startinv
    USE teqPol_mod, only : t_inv_k,t_inv_p
    CALL genTeq
    CALL start_inv(t_inv_k,t_inv_p)
  END SUBROUTINE startinv

  SUBROUTINE invTeq
    USE teqPol_mod, only : t_inv_k,t_inv_p
    CALL genTeq
    CALL teq_inv(t_inv_k,t_inv_p)
  END SUBROUTINE invTeq

END Module runTeq_mod
