! Copyright 2022, Francois BOUCHUT, Anne MANGENEY, Marc PERUZZETTO, Antoine LUCAS,  Jacques SAINTE-MARIE
! This program is distributed under the terms of the Lesser GPL v3
! This file is part of SHALTOP.


module parameters
  
  implicit double precision (a-h,o-z)

  !Parameters!----------------------------------------
  !Mathetmatical constants
  double precision,parameter :: pi=2.d0*acos(0.d0)
  !Maximum size of arrays
  integer,parameter :: lx=2500
  integer,parameter :: ly=1500
  !Precision
  integer,parameter :: psav = 4

  double precision :: delx,dely
  double precision, dimension(:), allocatable :: grillex, grilley
  
  !Physical constants
  double precision :: grav,density
  !No data value
  double precision :: ndv

  !Parameters from old don.bump file--------------------------------
  !Simulation duration and record options
  double precision :: tmax
  integer :: nbim
  double precision :: dt_im
  !Grid physical size
  double precision :: per,pery
  !Friction law
  integer :: icomp,istart,ieq
  double precision :: adelta,gammadelta
  double precision :: delta1, delta2,delta3, delta4
  double precision :: alpha, beta, diam, wlong, power, tau, eta, I0, ksi, lambda, vol_frac
  double precision :: uw
  !Minimum flow thickness
  double precision :: epshf,epshff,epsh_front,epsh_unormmax
  !Other parameters
  integer :: irestf
  !Curvature computation
  integer :: icurv,isym
  !Viscosity
  integer :: ivisc
  double precision :: rapvisc,cflvisc,viscnu, betavisc

  !Initial conditions----------------------------------------------
  !Topography mass creation
  integer :: hinit_vert
  integer :: initz, ipr
  double precision :: theta0, theta1,xv,yv,zv,av,bv,rai
  character (len=300) :: file_m_init,file_z_init,file_spatial_theta
  !Initial velocity
  integer :: initu,initut
  character (len=300) :: file_u_init,file_ut_init

  !Source terms---------------------------------------------------
  !Source term for thickness
  integer :: i_rho_source,i_vel_source
  double precision :: t_source_start, t_source_end,source_flux
  double precision :: vel_source_dir,vel_source_norm
  character (len=300) :: file_rho_source,file_u_source,file_ut_source
  integer :: i_source_flux_var
  double precision :: temps_init,deltmax_init
  
  !Parameters from old donfr.bump file------------------------------
  !Boundary conditions
  integer :: limhypl,limhypr,limhyps,limhypn
  double precision :: rhogauche,rhodroite,rhosud,rhonord &
       &,qgauche,qdroite,qsud,qnord,milx,wopen,tpsopen
  !Boundary conditions for viscosity
  integer :: limviscl,limviscr,limviscn,limviscs
  !Parameters on frame
  integer :: nx,ny,dens1,dens2,n1,n2

  !Simutlation parameters----------------------------------------
  !Parameters for saving directory
  character (len=250):: folder_output,f77data,file_params
  character (len=200):: working_directory
  integer :: len_output,len_z,len_m,len_spat_theta,len_work,len_file_params,len_m_source
  integer :: len_u,len_ut
  !Spatial variations of theta
  double precision,dimension(:,:),allocatable :: filt
  !Set Saint Venant like equations
  integer :: stvenant
  !Algorithm parameters
  integer :: isimpl, mordre, mtordre, massordre, nt,  smooth_velocity
  double precision :: gamma, ckappa, cflhyp, deltmax, amortrec, &
       & corvide,troncdz,eps0, eps_smooth_velocity, max_velocity
  !Outputs recording
  integer :: nbdat, lwrstepx, lwrstepy, imformat
  double precision :: coeff_p_stat,coeff_p_dyn

contains

  subroutine parameters_input (read_string1,read_string2)
    character (len=300), intent(in) :: read_string1,read_string2
    working_directory=read_string1
    len_work=lnblnk(working_directory)
    file_params=read_string2
    len_file_params=lnblnk(file_params)
  end subroutine parameters_input

  !Defining default parameters
  subroutine parameters_init

    !No Data value
    ndv=99999.d0
    
    !Box parameters
    per=-1
    pery=-1
    nx=-1
    ny=-1
    !Output folder
    folder_output='data2'
    lwrstepx=1   ! x-pas utilise dans l'ecriture des tableaux 2d
    lwrstepy=1   ! y-pas utilise dans l'ecriture des tableaux 2d
    imformat=1   ! format des images  0: gnuplot, 1: matlab
    !Maximum time and recorded outputs
    tmax=10.d0
    deltmax=1.d0! maximum time step
    nt=100000000 ! Maximum number of iterations
    nbim=10 ! number of states images, in addition to initial state
    dt_im=-1. ! time step between states images, overides nbim if > 0
    epshf=1.d-6
    nbdat=1000
    !minimum thickness for flow front tracking
    epsh_front=-1.
    !minimum thickness for maximum velocity recording (eps0 by default)
    epsh_unormmax=-1.
    !Initial topography
    initz=0
    file_z_init='z.d'
    theta0=0
    theta1=0
    !Initial Mass
    ipr=0
    !If ipr=0 and initial mass is read from file, hinit_vert=0 means mass is directly normal to topography
    !hinit_vert=1 means thickness is given in the vertical direction. In this case values will be multiplied
    !by the cosine of the slope to get thickness normal to topography (valid in thin-layer approx)
    hinit_vert=0 
    file_m_init='m.d'
    xv=0
    yv=0
    zv=0
    av=0
    bv=0
    rai=1
    !Initial velocity
    initu=-1
    initut=-1
    file_u_init='u.d'
    file_ut_init='ut.d'
    
    !Source term for mass
    !0: no source term, 1: thickness source term, 2: flux source term 
    i_rho_source=0
    file_rho_source='m.d'
    !Total flux on input area (ndv : use values from file_rho_source).
    source_flux=ndv
    t_source_start=-1.
    t_source_end=-1.
    i_source_flux_var=0
    !Limit delt at deltmax_init until temps_init if discharge is imposed
    !Usefull when non initial mass is present to limit added mass on single step
    temps_init = 1.
    deltmax_init = 1d-2
    
    !Source term for velocity
    !0: no source term,
    !1:use files file_u_source and file_ut_source,
    !2: uniform velocity on mask given by file_u_source, with direction
    ! vel_source_dir (in degrees, counter-clockwise, from the east
    ! and norm vel_course
    i_vel_source=0
    ! if 'n.a.' is specified, no velocity is imposed.
    ! If i_vel_source==2, specify at least file_u_source, used as mask
    file_u_source='n.a.'
    file_ut_source='n.a.'
    vel_source_norm=0
    vel_source_dir=0
    
    !Friction law (Pouliquen type)
    icomp=1
    delta1=20
    delta2=25
    delta3=21
    delta4=26
    file_spatial_theta='filt.d'
    !For icomp=11, friction law is simple Coulomb and delta1 is deduced with
    !delta1=adelta*volume**gammadelta (see states_init subroutine). Default values
    !are taken from Lucas et al. 2014, Fig 2
    adelta=1.
    gammadelta=-0.0774
    !Relation between froude number and hstop, froude=beta*(h/hstop)-alpha
    !Forterre and Pouliquen 2003, beta=0.136 for glass beads, 0.65 for sand (eq. 4.17)
    !Values must be adapted with icomp=32, Edwards et al 2018, as froude number definition is different
    beta=0.136
    !Forterre and Pouliquen 2003, alpha=0 for glass beads, -0.77 for sand (eq. 4.17)
    !Values must be adapted with icomp=32, Edwards et al 2018, as froude number definition is different
    alpha=0
    !Forterre and Pouliquen 2003, wlong=8.25d-4 for glass beads and wlong = 1.624d-3 for sand (Figure 8)
    !Mangeney et al 2007 : wlong=8.d-4
    !Pouliquen and Forterre 2002, wlong=0.65d-3
    wlong=0.65d-3
    !Pouliquen and Forterre 2002, power=1.d-3
    !Edwards et al 2017, 2018 : power=1.d-1, 1
    power=1.d-3
    !Edwards et al 2017, 2018 : beta threshold, different from beta (equal in Pouliquen and Forterre 2002)
    !Should be taken between 1 and 2.
    lambda=1.5
    !Pouliquen 1999, Exponential law
    diam=7.d-4
    !Bingham law
    tau=1500.d3
    eta=50.d0
    !mu(I)
    I0=0.279
    vol_frac=1.d0
    !Voellmy rheology
    ksi=200.d0
    !mu(v) frictional velocity weakening rheology, see Lucas et al. (2014)
    uw=2.d0

    !Parameters for impact pressure computation
    !Hydrostatic impact pressure : p = coeff_p_stat*0.5*grav*h
    !Dynamic impact pressure : p = coeff_p_dyn*0.5*u**2
    !The two coefficients are empirical
    coeff_p_stat=1.d0
    coeff_p_dyn=1.d0

    !Viscosity
    !Use or not viscosity, derivation only for z=z(x0 !!!)
    ivisc=0
    !Ratio of volume viscosity/shear viscosity.
    !We assume \lambda=rapvisc*\mu, rapvisc\ge -1.
    !rapvisc=0 in Rocha, Johnson, Gray 2018 approx,
    !rapvisc=2   in F. March Europ. J. Mech. B/Fluids 2007
    rapvisc=2.d0
    !CFL criterium
    cflvisc=1.d0/8.d0 !theoretcal value
    !Equaion (2.15 from Rocha, Johnson, gray 2018
    viscnu=2.82
    !Boundary conditions for viscosity
    !-1 : perdiodic, 0 : Dirichlet, 1 : Neumann, 2 : slide
    limviscl=2
    limviscr=2
    limviscs=2
    limviscn=2
    
    !Constant
    grav=9.81d0
    density=1000.d0
    gamma=2.d0!1.4! 
    !Resolution options
    icurv=1 !Curvature for friction term (0 no curvature, 1 full curvature tensor, 2 and 3 approximated curvature, see topography.f90)
    isym=1 !Symetrisation (1 yes, 0 no (curvature terms ignored))
    stvenant=0 !St Venant system (0 no, full equations, 1 yes, no slope variation)
    isimpl=2 ! 1: schema hydrostatique, 2: schema hydrostatique simplifie
    mordre=2 ! ordre spatial pour Euler (1 ou 2)
    mtordre=2 ! ordre temporel pour Euler (1 ou 2), recommande : 2
    massordre=2 ! ordre spatial pour le changement de repere d'un MNT (1 ou 2)
    cflhyp=.25d0! .25 a l'ordre 1, .125 a l'ordre 2 en x, en pratique prendre le double
    amortrec=0.25d0 ! amortissement de ENO (entre 0.=minmod et 1.=ENO)
    corvide=1.d0! correction de detente vide (0.=non, 1.=oui)
    troncdz=1000.d0! troncature de deltaz dans fluxc (au moins 1.)
    eps0=1d-14! Minimum value for h, u, and ut
    !For velocity computation when rho is too small
    !(1 : max between rho and eps_smooth_velocity, 2 : add rho and eps_smooth_velocity)
    smooth_velocity=0
    eps_smooth_velocity=1d-20! Minimum value for rho, or value added to rho, for velocity computation
    !Set maximum velocity (-1 : no maximum)
    max_velocity=-1
    
    !Boundary counditions
    
    limhypl=1
    rhogauche=0
    qgauche=0
    milx=0
    wopen=0
    tpsopen=0
    
    limhypr=1
    rhodroite=0
    qdroite=0
    
    limhyps=1
    rhosud=0
    qsud=0
    
    limhypn=1
    rhonord=0
    qnord=0
    
  end subroutine parameters_init

!-----------------------------------------------------------------------------
  
  subroutine parameters_read

    implicit double precision (a-h,o-z)
    character(len=200) :: buffer, label
    integer :: pos
    integer, parameter :: fh = 1000
    integer :: ios = 0
    integer :: line = 0

    if (len_work.gt.1) then
       file_params=working_directory(:len_work)//'/'//file_params(:len_file_params)
    else
       file_params=file_params(:len_file_params)
    end if

    write(*,*) file_params
    
    open(fh,file=file_params)
    write(*,*) 'Reading parameters : '

    do while (ios==0)

       read(fh, '(A)', iostat=ios) buffer
       if (ios==0) then
          
          line=line+1
          ! Find the first instance of whitespace.  Split label and data.
          pos = scan(buffer, '    ')
          label = buffer(1:pos)
          buffer = buffer(pos+1:)
          
          select case (label)

          case('folder_output')
             read(buffer, *, iostat=ios) folder_output
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',folder_output
             
          case('tmax')
             read(buffer, *, iostat=ios) tmax
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',tmax

          case('nbim')
             read(buffer, *, iostat=ios) nbim
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',nbim

          case('dt_im')
             read(buffer, *, iostat=ios) dt_im
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',dt_im
             
         
             
          case('nbdat')
             read(buffer, *, iostat=ios) nbdat
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',nbdat

          case('epsh_front')
             read(buffer, *, iostat=ios) epsh_front
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',epsh_front

          case('epsh_unormmax')
             read(buffer, *, iostat=ios) epsh_unormmax
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',epsh_unormmax

          case('coeff_p_dyn')
             read(buffer, *, iostat=ios) coeff_p_dyn
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',coeff_p_dyn

          case('coeff_p_stat')
             read(buffer, *, iostat=ios) coeff_p_stat
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',coeff_p_stat
             
          case('initz')
             read(buffer, *, iostat=ios) initz
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',initz

          case('file_z_init')
             read(buffer, *, iostat=ios) file_z_init
             file_z_init=buffer
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :', file_z_init

          case('theta0')
             read(buffer, *, iostat=ios) theta0
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',theta0

          case('theta1')
             read(buffer, *, iostat=ios) theta1
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',theta1

          case('ipr')
             read(buffer, *, iostat=ios) ipr
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',ipr

		case('hinit_vert')
             read(buffer, *, iostat=ios) hinit_vert
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',hinit_vert
             
          case('file_m_init')
             read(buffer, *, iostat=ios) file_m_init
             file_m_init=buffer
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',file_m_init

          case('initu')
             read(buffer, *, iostat=ios) initu
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',initu

          case('file_u_init')
             read(buffer, *, iostat=ios) file_u_init
             file_u_init=buffer
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',file_u_init

          case('file_ut_init')
             read(buffer, *, iostat=ios) file_ut_init
             file_ut_init=buffer
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',file_ut_init
             
          case('initut')
             read(buffer, *, iostat=ios) initut
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',initut
             
          case('xv')
             read(buffer, *, iostat=ios) xv
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',xv

          case('yv')
             read(buffer, *, iostat=ios) yv
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',yv
             
          case('zv')
             read(buffer, *, iostat=ios) zv
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',zv
             
          case('av')
             read(buffer, *, iostat=ios) av
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',av

          case('bv')
             read(buffer, *, iostat=ios) bv
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',bv

          case('rai')
             read(buffer, *, iostat=ios) rai
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',rai

          case('per')
             read(buffer, *, iostat=ios) per
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',per

          case('pery')
             read(buffer, *, iostat=ios) pery
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',pery

          case('i_rho_source')
             read(buffer, *, iostat=ios) i_rho_source
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',i_rho_source

          case('temps_init')
             read(buffer, *, iostat=ios) temps_init
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',temps_init

          case('deltmax_init')
             read(buffer, *, iostat=ios) deltmax_init
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',deltmax_init
             
             
          case('file_rho_source')
             read(buffer, *, iostat=ios) file_rho_source
             file_rho_source=buffer
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',file_rho_source

          case('source_flux')
             read(buffer, *, iostat=ios) source_flux
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',source_flux

          case('i_source_flux_var')
             read(buffer, *, iostat=ios) i_source_flux_var
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',i_source_flux_var
             
          case('t_source_start')
             read(buffer, *, iostat=ios) t_source_start
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',t_source_start

          case('t_source_end')
             read(buffer, *, iostat=ios) t_source_end
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',t_source_end

          case('i_vel_source')
             read(buffer, *, iostat=ios) i_vel_source
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',i_vel_source

          case('file_u_source')
             read(buffer, *, iostat=ios) file_u_source
             file_u_source=buffer
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',file_u_source

          case('file_ut_source')
             read(buffer, *, iostat=ios) file_ut_source
             file_ut_source=buffer
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',file_ut_source

          case('vel_source_dir')
             read(buffer, *, iostat=ios) vel_source_dir
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',vel_source_dir

          case('vel_source_norm')
             read(buffer, *, iostat=ios) vel_source_norm
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',vel_source_norm
             
          case('nx')
             read(buffer, *, iostat=ios) nx
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',nx

          case('ny')
             read(buffer, *, iostat=ios) ny
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',ny

          case('icomp')
             read(buffer, *, iostat=ios) icomp
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',icomp

          case('file_spatial_theta')
             read(buffer, *, iostat=ios) file_spatial_theta
             file_spatial_theta=buffer
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',file_spatial_theta

          case('adelta ')
             read(buffer, *, iostat=ios) adelta
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',adelta

          case('gammadelta ')
             read(buffer, *, iostat=ios) gammadelta
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',gammadelta
             
          case('delta1 ')
             read(buffer, *, iostat=ios) delta1
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',delta1

          case('delta2')
             read(buffer, *, iostat=ios) delta2
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',delta2

          case('delta3')
             read(buffer, *, iostat=ios) delta3
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',delta3

          case('delta4')
             read(buffer, *, iostat=ios) delta4
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',delta4

          case('alpha')
             read(buffer, *, iostat=ios) alpha
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',alpha
             
          case('beta')
             read(buffer, *, iostat=ios) beta
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',beta

          case('diam')
             read(buffer, *, iostat=ios) diam
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',diam

          case('wlong')
             read(buffer, *, iostat=ios) wlong
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',wlong

          case('lambda')
             read(buffer, *, iostat=ios) wlong
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',wlong
             
          case('power')
             read(buffer, *, iostat=ios) power
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',power

          case('tau')
             read(buffer, *, iostat=ios) tau
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',tau

          case('eta')
             read(buffer, *, iostat=ios) eta
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',eta

          case('I0')
             read(buffer, *, iostat=ios) I0
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',I0

          case('uw')
             read(buffer, *, iostat=ios) uw
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',uw

          case('vol_frac')
             read(buffer, *, iostat=ios) vol_frac
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',vol_frac

          case('ksi')
             read(buffer, *, iostat=ios) ksi
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',ksi

          case('ivisc')
             read(buffer, *, iostat=ios) ivisc
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',ivisc

          case('rapvisc')
             read(buffer, *, iostat=ios) rapvisc
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',rapvisc

          case('cflvisc')
             read(buffer, *, iostat=ios) cflvisc
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',cflvisc

          case('viscnu')
             read(buffer, *, iostat=ios) viscnu
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',viscnu

          case('limviscl')
             read(buffer, *, iostat=ios) limviscl
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',limviscl
             
          case('limviscn')
             read(buffer, *, iostat=ios) limviscn
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',limviscn

          case('limviscr')
             read(buffer, *, iostat=ios) limviscr
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',limviscr

          case('limviscs')
             read(buffer, *, iostat=ios) limviscs
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',limviscs

          case('grav')
             read(buffer, *, iostat=ios) grav
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',grav

          case('ndv')
             read(buffer, *, iostat=ios) ndv
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',ndv

          case('density')
             read(buffer, *, iostat=ios) density
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',density

          case('epshf')
             read(buffer, *, iostat=ios) epshf
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',epshf

          case('icurv')
             read(buffer, *, iostat=ios) icurv
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',icurv

          case('isym')
             read(buffer, *, iostat=ios) isym
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',isym

          case('stvenant')
             read(buffer, *, iostat=ios) stvenant
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',stvenant

          case('limhypl')
             read(buffer, *, iostat=ios) limhypl
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',limhypl

          case('limhypr')
             read(buffer, *, iostat=ios) limhypr
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',limhypr

          case('limhypn')
             read(buffer, *, iostat=ios) limhypn
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',limhypn

          case('limhyps')
             read(buffer, *, iostat=ios) limhyps
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',limhys

          case('rhogauche')
             read(buffer, *, iostat=ios) rhogauche
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',rhogauch

          case('rhodroite')
             read(buffer, *, iostat=ios) rhodroite
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',rhodroite

          case('eps0')
             read(buffer, *, iostat=ios) eps0
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',eps0

          case('eps_smooth_velocity')
             read(buffer, *, iostat=ios) eps_smooth_velocity
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',eps_smooth_velocity
             
          case('smooth_velocity')
             read(buffer, *, iostat=ios) smooth_velocity
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',smooth_velocity

          case('max_velocity')
             read(buffer, *, iostat=ios) max_velocity
             len=index(label,' ')-1
             write(*,*) 'Reading '//label(:len)//' :',max_velocity

          case('rhonord')
             read(buffer, *, iostat=ios) rhono

          case('rhosud')
             read(buffer, *, iostat=ios) rhosud

          case('qgauche')
             read(buffer, *, iostat=ios) qgauche

          case('qdroite')
             read(buffer, *, iostat=ios) qdroite

          case('qnord')
             read(buffer, *, iostat=ios) qnord

          case('qsud')
             read(buffer, *, iostat=ios) qsud

          case('milx')
             read(buffer, *, iostat=ios) milx

          case('wopen')
             read(buffer, *, iostat=ios) wopen

          case('tpsopen')
             read(buffer, *, iostat=ios) tpsopen

          case('gamma')
             read(buffer, *, iostat=ios) gamma

          case('isimpl')
             read(buffer, *, iostat=ios) isimpl

          case('mordre')
             read(buffer, *, iostat=ios) mordre

          case('mtordre')
             read(buffer, *, iostat=ios) mtordre
             
          case('cflhyp')
             read(buffer, *, iostat=ios) cflhyp

          case('nt')
             read(buffer, *, iostat=ios) nt

          case('deltmax')
             read(buffer, *, iostat=ios) deltmax

          case('corvide')
             read(buffer, *, iostat=ios) crovide

          case('troncdz')
             read(buffer, *, iostat=ios) troncdz

          case('imformat')
             read(buffer, *, iostat=ios) imformat

          case('lwrstepx')
             read(buffer, *, iostat=ios) lwrstpex

          case('lwrstepy')
             read(buffer, *, iostat=ios) lwrstpey
           
          end select
       end if
    end do

    close(fh)

  end subroutine parameters_read

  !----------------------------------------------------------------------------

  subroutine parameters_process

    !Check if box parameters are given
    if ((per.lt.0).or.(pery.lt.0).or.(nx.lt.0).or.(ny.lt.0)) then
       if (per.lt.0) then
          write(*,*) 'X box size (per) not given'
       end if
       if (pery.lt.0) then
          write(*,*) 'Y box size (pery) not given'
       end if
       if (nx.lt.0) then
          write(*,*) 'X number of points (nx) not given'
       end if
       if (ny.lt.0) then
          write(*,*) 'Y number of points (ny) not given'
       end if
       call abort
    end if
    
    !Spatial steps
    delx=per/float(nx)
    dely=pery/float(ny)
    
    !Angle conversion in radians
    delta1=delta1*pi/180.d0
    delta2=delta2*pi/180.d0
    delta3=delta3*pi/180.d0
    delta4=delta4*pi/180.d0

    !X and Y axis
    allocate(grillex(0:nx+1))
    allocate(grilley(0:ny+1))
    do i=0,nx+1
       grillex(i)=(float(i)-0.5d0)*delx
    enddo
    do j=0,ny+1
       grilley(j)=(float(j)-0.5d0)*dely
    enddo

    if (len_work.gt.1) then
       folder_output=working_directory(:len_work)//'/'//folder_output
       if (file_m_init(1:1).ne.'/') then
          file_m_init=working_directory(:len_work)//'/'//file_m_init
       end if
       if (file_z_init(1:1).ne.'/') then
          file_z_init=working_directory(:len_work)//'/'//file_z_init
       end if
       if (file_spatial_theta(1:1).ne.'/') then
          file_spatial_theta=working_directory(:len_work)//'/'//file_spatial_theta
       end if
	   if (file_rho_source(1:1).ne.'/') then
          file_rho_source=working_directory(:len_work)//'/'//file_rho_source
       end if
	   if (file_u_init(1:1).ne.'/') then
          file_u_init=working_directory(:len_work)//'/'//file_u_init
       end if
	   if (file_ut_init(1:1).ne.'/') then
          file_ut_init=working_directory(:len_work)//'/'//file_ut_init
       end if
    end if
    len_output=lnblnk(folder_output)
    len_m=lnblnk(file_m_init)
    len_m_source=lnblnk(file_rho_source)
    len_z=lnblnk(file_z_init)
    len_u=lnblnk(file_u_init)
    len_ut=lnblnk(file_ut_init)
    len_spat_theta=lnblnk(file_spatial_theta)
    
    !Spatial variation of friction angle
    allocate (filt(0:nx+1,0:ny+1))
    filt=0
    if ((icomp.eq.4).or.(icomp.eq.41)) then
       call spatial_theta
    endif

    !ending time for source terms
    !if the given ending time is less than 0, source is set until the end of the simulation.
    if (t_source_end.lt.0) then
       t_source_end=tmax+1
    end if

    !Simulation parameters
    ckappa=0.5*grav

    !Maximum thickness for unorm_max computation
    if (epsh_unormmax.lt.0) then
       epsh_unormmax=eps0
    end if

    !Time interval between recordings, if not specified in param file
    if (dt_im.gt.0) then
       nbim=int(tmax/dt_im)
    end if

    
    !Viscosity
    !Optimum formula ! We assume  \lambda+\betavisc\mu\ge 0 (ie rapvisc+betavisc\ge 0.), with 0\le\betavisc\le 1
    betavisc=max(0.d0,-rapvisc)
    
    
  end subroutine parameters_process 

  !------------------------------------------------------------------------

  !Read map theta
  subroutine spatial_theta

    open(999,file=file_spatial_theta(:len_spat_theta), form='formatted',status='unknown')
    do j=1,ny
       do i=1,nx
          read(999,*) filt(i,j)
       enddo
    enddo
    close(999)

  end subroutine spatial_theta

  !------------------------------------------------------------------------
  
  !Print parameters
  subroutine print_param

    write(*,"( '______________________________________________')")

    !Topography and mass
    write(*,*) 'Main parameters'
    write(*,*) ' '
    write(*,*) 'Grid Size: ', int(nx),'x',int(ny)
    write(*,*) 'Model Size: ', real(per,psav),'x',real(pery,psav)
    write(*,*) 'Tmax:', tmax
    write(*,*) 'Gravity:', real(grav,psav)
    write(*,*) ' '
    write(*,*) 'Z type: '
    if (initz.EQ.0) then
       write(*,*) 'From file'
    else
       write(*,*) 'Generated'
    endif
    write(*,*) ' '
    write(*,*) 'Mass type: '
    if (ipr.EQ.0) then
       write(*,*) 'From file'
    else
       write(*,*) 'Generated'
    endif
    write(*,*) ' '

    if (ivisc.eq.1) then
       write(*,*) 'Viscosity computation, valid if z=z(x)'
       write(*,*) 'rapvisc',rapvisc
       write(*,*) 'betavisc',betavisc
       write(*,*) 'rapvisc',viscnu
    end if
    
    !Friction law
    conv_rd = 180/pi
    basal_rheology:select case(icomp)
    case(1)
       write(*,*) 'Friction type: Coulomb'
       write(*,*) 'delta1:', real(delta1*conv_rd,psav)

    case(11)
       write(*,*) 'Friction type: Coulomb'
       write(*,*) 'delta1=adelta*volume**(gammadelta)'
       write(*,*) 'adelta:',real(adelta*conv_rd,psav)
       write(*,*) 'gammadelta:',real(gammadelta,psav)

    case(2)
       write(*,*) 'Friction type: Pouliquen (exp(-hstop/dist))'
       write(*,*) 'delta1:', real(delta1*conv_rd,psav)
       write(*,*) 'delta2:',real(delta2*conv_rd,psav)
       write(*,*) 'delta3:',real(delta3*conv_rd,psav)
       write(*,*) 'delta4:',real(delta4*conv_rd,psav)

    case(3)
       write(*,*) 'Friction type: Pouliquen (**power* (hstart - fricmain))'
       write(*,*) 'delta1:', real(delta1*conv_rd,psav)
       write(*,*) 'delta2:',real(delta2*conv_rd,psav)
       write(*,*) 'delta3:',real(delta3*conv_rd,psav)
       
    case(31)
       write(*,*) 'Friction type: Pouliquen 4 angles (**power* (hstart - fricmain))'
       write(*,*) 'delta1:', real(delta1*conv_rd,psav)
       write(*,*) 'delta2:',real(delta2*conv_rd,psav)
       write(*,*) 'delta3:',real(delta3*conv_rd,psav)
       write(*,*) 'delta4:',real(delta4*conv_rd,psav)

    case(4)
       write(*,*) 'Coulomb friction w/ filtering'
       write(*,*) 'delta1:',real(delta1*conv_rd,psav)
       write(*,*) 'delta2:',real(delta2*conv_rd,psav)
       write(*,*) 'delta3:',real(delta3*conv_rd,psav)
       write(*,*) 'delta4:',real(delta4*conv_rd,psav)

    case(5)
       write(*,*) 'Coulomb friction + Bingham'
       write(*,*) 'delta1:', real(delta1*conv_rd,psav)
       write(*,*) 'tau:',real(tau,psav)
       write(*,*) 'eta:',real(eta,psav)

    case(6)
       write(*,*) 'Bingham'
       write(*,*) 'tau:',real(tau,psav)
       write(*,*) 'eta:',real(eta,psav)
    end select basal_rheology

    write(*,*) ' '
    write(*,*) 'Initialisation dans ',folder_output(:len_output)

  end subroutine print_param

end module parameters
