! list of c callable set/get routines allowing third party software to 
! set/access nbi module variables
!
! $id: nbi_set_com.f90,v 1.6 2009-09-16 19:46:14 alex_pletzer exp $
! dmc modified: set/get subroutines now, not just set...

subroutine nbi_open_log_file(logfile)
  ! find appropriate unit number, open log file,
  ! and set global nonlin..
  use nbi_com, only : nonlin
  implicit none
  
  character(len=*), intent(in) :: logfile

  ! locals
  integer :: iunit
  character(len=32) :: flogname


  ! convert to fortran string
  call cstring(flogname, logfile, '2f')
  ! open file
  open(unit = nonlin, file = flogname, form='formatted')

end subroutine nbi_open_log_file

subroutine nbi_close_log_file
  ! close log file
  use nbi_com, only : nonlin
  implicit none

  close(unit = nonlin)

end subroutine nbi_close_log_file

subroutine nbi_set_comm(comm)
  ! set communicator
  use nbi_mpi_com
  use mpi_proc_data  ! (from portlib library)
  implicit none

  integer, intent(in) :: comm ! communicator

  !------------------------
  integer :: ierloc
  !------------------------

  nbi_comm = comm

  ! also set the one in the portlib module...
  call mpi_env_comm_add(comm,ierloc)  ! ierloc not checked at present

end subroutine nbi_set_comm

subroutine nbi_get_comm(comm)
  ! get communicator
  use nbi_mpi_com
  implicit none

  integer, intent(out) :: comm ! communicator

  comm = nbi_comm

end subroutine nbi_get_comm



subroutine nbi_set_myid(procid)
  ! set proc id
  use nbi_mpi_com
  implicit none
  integer, intent(in) :: procid

#ifdef __MPI
  myid = procid
  myidd = procid
  if(numprocs.eq.0) numprocs = -1  ! disable nbi_mpi_com_init
#else
  call nbi_mpi_com_init
#endif

end subroutine nbi_set_myid

subroutine nbi_get_myid(procid)
  ! get proc id
  use nbi_mpi_com
  implicit none
  integer, intent(out) :: procid

  call nbi_mpi_com_init

  procid = myidd

end subroutine nbi_get_myid



subroutine nbi_set_numprocs(nprocs)
  ! set number of processes
  use nbi_mpi_com
  implicit none
  integer, intent(in) :: nprocs

#ifdef __MPI
  numprocs = nprocs
  numprocd = nprocs
#else
  call nbi_mpi_com_init
#endif

end subroutine nbi_set_numprocs

subroutine nbi_get_numprocs(nprocs)
  ! get number of processes
  use nbi_mpi_com
  implicit none
  integer, intent(out) :: nprocs

  call nbi_mpi_com_init

  nprocs = numprocs

end subroutine nbi_get_numprocs

