!!!!!
!
!fichier g2s_g2mc.f90
!
!Entree principale du module G2MC.
!Generation d'un jeu de donnees pour un calcul en Monte-Carlo
!
!!!!!!

subroutine G2MC(NENTRY,HENTRY,IENTRY,JENTRY,KENTRY)
  use salget_funs_mod
  use celluleBase
  use cellulePlaced
  use boundCond
  use ptNodes
  use pretraitement
  use derivedPSPLOT
  use monteCarlo
  use track
  use segArc
  use GANLIB
  implicit none

  integer NENTRY
  integer IENTRY,JENTRY
  type(c_ptr) KENTRY
  character*12 HENTRY
  dimension IENTRY(*),JENTRY(*),KENTRY(*),HENTRY(*)

! INPUT/OUTPUT PARAMETERS:
!  NENTRY : NUMBER OF LINKED LISTS AND FILES USED BY THE MODULE.
!  HENTRY : CHARACTER*12 NAME OF EACH LINKED LIST OR FILE.
!  IENTRY : =1 LINKED LIST; =2 XSM FILE; =3 SEQUENTIAL BINARY FILE;
!           =4 SEQUENTIAL ASCII FILE; =5 DIRECT ACCESS FILE.
!  JENTRY : =0 THE LINKED LIST OR FILE IS CREATED.
!           =1 THE LINKED LIST OR FILE IS OPEN FOR MODIFICATIONS;
!           =2 THE LINKED LIST OR FILE IS OPEN IN READ-ONLY MODE.
!  KENTRY : FILE UNIT NUMBER OR LINKED LIST ADDRESS.
!           DIMENSION HENTRY(NENTRY),IENTRY(NENTRY),JENTRY(NENTRY),
!           KENTRY(NENTRY)

  type(c_ptr)  :: ipGeo,ipGeo_1,ipTrk
  integer      :: ipMC,ipSal,ipPs,sizeB,sizeP,sizeSA,nbNode,nbCLP,nbFlux
  logical,parameter :: drawMix = .false. ! set to .true. to draw the mix numbers
  integer      :: lgMaxGig=0
  integer,dimension(10) :: datain
  integer,allocatable,dimension(:) :: merg
  character(len=12) :: name_geom

  if (nentry == 2) then
     ipMC   = FILUNIT(KENTRY(1)) ! Monte-Carlo file generated
     ipSal  = FILUNIT(KENTRY(2)) ! input Salome file (surfacic elements)
     ipPs   = -1         ! no postscript file
     ipTrk  = c_null_ptr ! no tracking object
     ipGeo_1= c_null_ptr ! no geometry read
     ! check that second argumnet is file to write
     ! then the tracking object
     if ((IENTRY(1) /= 4) .or. (JENTRY(1) /= 0)) &
        call XABORT('G2MC: a new ascii file expected at LHS for nontaining MC info')
     if ((IENTRY(2) /= 4) .or. (JENTRY(2) /= 2)) &
        call XABORT('G2MC: read-only ascii file expected at RHS with surfacic elements')
  else if (nentry == 3) then
     ipPs   = -1        ! no postscript file
     ipTrk  = KENTRY(1) ! tracking object generated
     ipMC   = FILUNIT(KENTRY(2)) ! Monte-Carlo file generated
     ipGeo_1= KENTRY(3) ! geometry read
     ! check that second argumnet is file to write
     ! then the tracking object
     if ((IENTRY(1) /= 1) .or. (JENTRY(1) /= 0)) &
        call XABORT('G2MC: a new PyLCM object expected for tracking')
     if ((IENTRY(2) /= 4) .or. (JENTRY(2) /= 0)) &
        call XABORT('G2MC: a new file was expected for writing geometry')
  else if (nentry == 4) then
     !le fichier ps doit etre genere
     ipTrk  = KENTRY(1) ! tracking object generated
     ipMC   = FILUNIT(KENTRY(2)) ! Monte-Carlo file generated
     ipPs   = FILUNIT(KENTRY(3)) ! psfile generated
     g_psp_isEpsFile = (index(HENTRY(3),'.eps')/=0) !is it an eps file ?
     ipGeo_1= KENTRY(4) ! geometry read
     if ((IENTRY(1) /= 1) .or. (JENTRY(1) /= 0)) &
        call XABORT('G2MC: a new PyLCM object expected for tracking')
     if ((IENTRY(2) /= 4) .or. (JENTRY(2) /= 0)) &
        call XABORT('G2MC: a new file was expected for writing geometry')
     if ((IENTRY(3) /= 4) .or. (JENTRY(3) /= 0)) &
        call XABORT('G2MC: a new file was expected for the postscript file')
  else
     call XABORT('G2MC: you must provide 2, 3 or 4 arguments')
  end if
  
  sizeB = 0   !cellules de base
  sizeP = 0   !cellules placees
  sizeSA = 0  !elements geometriques
  if(c_associated(ipGeo_1)) then
     ! copy the input geometric object
     call lcmop(ipGeo,'geom_copy',0,1,0)
     call lcmequ(ipGeo_1,ipGeo)

     !initialisation des differents tableaux
     call initializeData()

     !pretraitement des donnees lues (remplace la partie python)
     !+complession des cellules de base et remplissage du tableau
     !des cellules placees
     call prepareData(ipGeo,sizeB,sizeP,lgMaxGig)

     !en sortie, toutes les cellules de base ont tous leurs
     !champs remplis, et le tableau des cellules placees est pret

     !eclatement des cellules
     call splitCells(sizeP,sizeSA)

     !creation de nouveaux segements aux interfaces des cellules
     !et elimination des doublons
     call addSegsAndClean(sizeSA)

     !prise en compte des conditions aux limites
     call appliBoundariConditions(ipGeo,sizeSA,nbCLP)

     !calcul des nodes delimites par les elements
     call createNodes(sizeSA,nbNode)
     call lcmcl(ipGeo,2)
  else
     if (JENTRY(nentry) == 0) call XABORT('G2M: an existing Salomon file is expected')
     !initialisation de TabSegArc
     call salget(datain,4,ipSal,0,'dimensions for geometry')
     nbNode=datain(3)
     sizeSA=datain(4)
     rewind(ipSal)
     !write(*,*) 'debut : initializeData'
     call initializeTabSegArc(sizeSA)
     call initializebCData()  
     !write(*,*) 'fin   : initializeData'
     allocate(merg(nbNode))
     call generateTabSegArc(ipSal,sizeSA,nbNode,nbCLP,nbFlux,merg,name_geom)
     deallocate(merg)
  endif

  !impression des segArc charges
  if (ipPs /= -1) call drawSegArc(ipPs,sizeSA,.true.,drawMix)

  !creation du fichier de commande Monte-Carlo
  if (index(HENTRY(1),'.tp')/=0) then
     ! generate a Tripoli4 datafile
     call generateTripoliFile(ipMC,sizeSA,nbNode)
  else if (index(HENTRY(1),'.sp')/=0) then
     ! generate a Serpent datafile
     call generateSerpentFile(ipMC,sizeSA,nbNode)
  else
     ! generate a MCNP datafile
     call generateMCNPFile(ipMC,sizeSA,nbNode)
  end if

  print *,"  At end of G2MC:","    ",nbNode,"volumes"

  !liberation de la memoire allouee
  call destroyData(sizeB,sizeP,sizeSA)
end subroutine G2MC
