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

module teqerr

  logical :: exceptions_enabled = .false.
  logical :: stop_on_error = .true.
  integer :: error_code = 0
  character(len = 128) :: error_msg = 'none'

  interface

     subroutine catch_inveq(ierr,inv,prof,glb)
       use teqlib
       integer, intent(inout) :: ierr
       type(profile_type), intent(inout) :: prof
       type(inv_type), intent(inout) :: inv
       type(global_type), intent(in) :: glb
     end subroutine catch_inveq

     subroutine catch_direq(ierr,dir,pf,inv,prof,glb)
       use teqlib
       integer, intent(inout) :: ierr
       type(dir_type), intent(inout) :: dir
       type(pf_type), intent(inout) :: pf
       type(profile_type), intent(inout) :: prof
       type(inv_type), intent(inout) :: inv
       type(global_type), intent(in) :: glb
     end subroutine catch_direq

     subroutine throw_xcpt(arg)
       integer, intent(inout) :: arg
     end subroutine throw_xcpt

     subroutine catch_dead_start(ierr,glb)
       use teqlib
       integer, intent(inout) :: ierr
       type(global_type), intent(in) ::glb
     end subroutine catch_dead_start

     subroutine catch_load_savefile(ierr,glb,dir,pf,inv,prof)
       use teqlib
       integer, intent(inout) :: ierr
       type(dir_type), intent(inout) :: dir
       type(pf_type), intent(inout) :: pf
       type(profile_type), intent(inout) :: prof
       type(inv_type), intent(inout) :: inv
       type(global_type), intent(in) :: glb
     end subroutine catch_load_savefile

  end interface

end module teqerr

subroutine errdetec(blah, flag)
  use teqerr
  use teqlib_input, only : logunit
  implicit none
  character*(*), intent(in) :: blah
  integer, intent(inout) :: flag
  integer :: ierr=0
  real :: sq

  error_code = flag
  error_msg  = blah

  if (flag < 0) then
     print *, blah," flag = -1: benign"
     write(logunit,*) blah," flag = -1: benign"
     return
  end if
  if (stop_on_error) stop
  if (exceptions_enabled) then
     print *, blah
     write(logunit,*) blah
     call throw_xcpt(flag)
  end if

end subroutine errdetec

subroutine savefile(glb,dir,pf,inv,prof)
  use teqlib
  use teqlib_input, only : inv_eq,fix_bound

  type(dir_type), intent(inout) :: dir
  type(pf_type), intent(inout) :: pf
  type(inv_type), intent(inout) :: inv
  type(profile_type), intent(inout) :: prof
  type(global_type), intent(in) :: glb
  call load_savefile(glb%filename)
  if(inv_eq==0 .or. (inv_eq==1 .and. .not.fix_bound)) then ! direct solve
     call find_direq(dir,pf,inv,prof,glb)
  else
     call find_inveq(inv,prof,glb)
  end if
end subroutine savefile

subroutine deadstar(glb)
  use DeadStart
  use teqlib
  type(global_type), intent(in) ::glb
  call do_dead_start(glb%tokamak,glb%pfcoil)
end subroutine deadstar

subroutine inveq(inv,prof,glb)
  use teqlib
  implicit none
  
  type(inv_type), intent(inout) :: inv
  type(profile_type), intent(inout) :: prof
  type(global_type), intent(in) :: glb
  call find_inveq(inv,prof,glb)
end subroutine inveq

subroutine direq(dir,pf,inv,prof,glb)
  use teqlib
  implicit none
  
  type(dir_type), intent(inout) :: dir
  type(pf_type), intent(inout) :: pf
  type(inv_type), intent(inout) :: inv
  type(profile_type), intent(inout) :: prof
  type(global_type), intent(in) :: glb
  call find_direq(dir,pf,inv,prof,glb)
end subroutine direq

