!**************************************************************************************************
!
!   #####   #    #  #####            ####   #
!   #    #  #    #  #    #          #    #  #
!   #    #  ######  #    #          #       #
!   #####   #    #  #    #          #  ###  #
!   #   #   #    #  #    #          #    #  #
!   #    #  #    #  #####  #######   ####   ###### _module
!
!**************************************************************************************************
!   Fortran90
!   Bernd Freytag:         Kiel, Copenhagen, Uppsala, Montpellier, Lyon
!   Werner Schaffenberger: Potsdam, Exeter
!   Sven Wedemeyer-Boehm:  Kiel, Freiburg
!   2011-01-27
!**************************************************************************************************
! Modules:
!   rhd_gl_module:  Global type definitions and parameters for rhd
!**************************************************************************************************


!------*************-------------------------------------------------------------------------------
module rhd_gl_module
!--------------------------------------------------------------------------------------------------
! NAME:
!   rhd_gl_module ('rhd_global_parameters_module')
!
! PURPOSE:
!   Save global parameters for rhd.
!
! CATEGORY:
!   Global type definitions and parameters
!
! CALLING SEQUENCE:
!   use rhd_gl_module
!
! TYPES:
!   gl_type:      ('global_type') structure type for global data:
!     nc_p:       ('number_channel_print') integer, channel for text output, typically 6
!     lw_p:       ('line_width_print') integer, desired maximal line width
!     parfile:    ('parameter_file') character, name of parameter file, typically 'rhd.par'
!                 Its contents is described in some detail in 'rhd_io_module.f90'.
!     stopfile:   ('stop_file') character, name of stop file, typically 'rhd.stop'
!                 The existence of this file is checked at the beginning of each new time step.
!                 If the file exists the simulation is interupted gracefully (and an end file is
!                 produced).
!     contfile:   ('continuation_file') character, name of continuation file, typically 'rhd.cont'
!                 This file is somewhat complementary to the stopfile. If the contfile exists
!                 at the beginning of a simulation, the startmodel is not read from the file
!                 specified by the entry 'infile_start' in the parameter file but from the file
!                 specified by 'outfile_end'. The data written into the full and mean file is
!                 appended to existing files.
!     statusfile: ('status_file') character, name of exit status file, typically 'rhd.done'
!                 If the simulation ends regularly this file is produced.
!     dumpfile:   ('dump_file') character, namp of dump request file, typically 'rhd.dump'
!                 If this file exists, the current model is written into a file specified
!                 snapshotfile
!     snapshotfile: ('snapshot_file') character, name of snapshot output file, typically 'rhd.snap'
!     version:    ('version') character, rhd code version
!   scale_type:   ('scale_type') structure type for typical scales
!   par_type:     ('parameter_type') structure type for data from input parameter file,
!                 the description of the components can be found in rhd_io_module.f90
!
! VARIABLES:
!   gl:        ('global') structure for global data
!   par:       ('parameter') structure for data from input parameter file.
!              See rhd_io_module.f90 for a description of the components.
!
! MODIFICATION HISTORY:
!   1997-05-29 (B.F. Kiel) Written
!   1997-06-05 (B.F.) Next version
!   1997-06-10 (B.F.) type box_type
!   1997-06-19 (B.F.) type action_type, grav_type, bottom_type, top_type
!   1997-07-23 (B.F.) type action_type, par_type extended
!   1997-07-24 (B.F.) type action_type, par_type extended
!   1997-10-14 (B.F.) type par_type extended
!   1997-10-21 (B.F.) type gl changed
!   1999-03-25 (B.F. Copenhagen) Name of energy in type mean_type change, outcommented
!                     New basic variables in type box_type
!   1999-04-06 (B.F.) Put into own file
!   1999-04-07 (B.F.) Parameter grav_type -> grav_mode; parameters mass_star, r0_grav added
!   1999-04-16 (B.F.) Parameter C_visDrag added
!   1999-05-14 (B.F.) Parameter dtime_incmax added
!   1999-05-18 (B.F.) Parameters C_radCourant, C_radCourantmax added
!   1999-05-19 (B.F.) Parameters C_radmaxeiChange, s_inflow, C_sChange, C_PChange added
!   1999-05-25 (B.F.) gravcorr_terms
!   1999-05-27 (B.F.) Parameters for artificial viscosity
!   1999-05-28 (B.F.) box_type into rhd_box_module, action_type into rhd_action_module
!   1999-05-31 (B.F.) Parameters C_visCourant, C_visCourantmax added
!   1999-06-08 (B.F.) Parameter C_maxeiChange added
!   1999-06-30 (B.F.) Parameter N_raditer added
!   1999-08-31 (B.F.) Parameters for radiative transfer added
!   1999-12-08 (B.F.) Parameters radraybase, radraystar added
!   2000-01-22 (B.F.) grav_type outcommented
!   2000-02-03 (B.F.) Type scale_type introduced
!   2000-03-23 (B.F.) gl%stopfile added
!   2000-04-02 (B.F.) Parameter N_radthin/thickpoint added
!   2000-04-08 (B.F.) gl%contfile added
!   2000-04-13 (B.F.) Parameters for Matthias Steffens radiation transport routines added
!   2000-04-28 (B.F.) Parameter r1_grav added
!   2000-05-29 (B.F.) Parameter kappamax added
!   2000-05-31 (B.F.) Parameter C_radTintminfac added
!   2000-12-10 (B.F. Uppsala) Parameter N_cellsperchunk added
!   2000-12-28 (B.F.) Parameter dtime_start, starttime added
!   2001-01-11 (B.F.) Parameter starttimestep added
!   2001-01-23 (B.F.) Parameters N_XXXcellsperchunk and test parameters added
!   2001-04-10 (B.F.) Parameters C_Hptopfactor, C_radTvisdtau, C_radTvis added
!   2001-06-29 (B.F.) Parameter C_radthintimefac added
!   2002-05-07 (B.F.) Parameters rho_min, luminositypervolume, istep_in_start added
!   2002-11-07 (B.F.) gl%lw_p
!   2002-11-08 (B.F.) Parameters r1_rad, dustscheme, N_outslicedim_mean added
!   2002-11-28 (B.F.) gl%statusfile; C_dust0X
!   2003-01-16 (B.F. Montpellier) gravcorr_terms removed
!   2003-01-22 (B.F.) Parameter C_slopered added
!   2004-02-27 (B.F.) Parameter C_CoreDrag added
!   2004-05-13 (B.F.) Parameters C_visP2Pcoeff, C_visP2Pincl(later: 1,2) added
!   2004-08-18 (B.F.) dumpfile, snapshotfile
!   2005-03-08 (B.F.) dtime_out_end
!   2005-03-11 (B.F.) chem_abumetal, chem_reacfile, chem_reacpath
!   2005-05-31 (WS&SWB) Parameters C_resB, C_resEpsilon added
!   2006-08-07 (SWB)  hion_datapath, hion_atomfile, hion_abufile, hion_edensfile, 
!                     hion_pffile, dtime_out_hion, hion_chunks
!   2006-11-07 (B.F. Lyon) New parameters added:
!                     N_radrsyslevel, N_radoutput, C_radtcool, C_radscool, C_raddcool,
!                     N_test4, N_test5, C_test4, C_test5, strtest4, strtest5,
!                     B1_inflow
!   2006-12-14 (B.F.) New parameter: heat_mode
!   2007-01-31 (B.F.) New parameter: nu_rotation
!   2007-02-08 (B.F.) New parameter: hdsplit
!   2007-04-16 (B.F.) New parameters: dustopafile, ar_dustGrainRadius
!   2007-12-12 (B.F.) More C_dust0X
!   2008-03-11 (B.F.) Parameters C_hydexpCourant, C_hydexpCourantmax added
!   2008-03-17 (B.F.) Parameters C_ReconstructiondTimeFactor, C_hydPredfactor added
!   2008-08-12 (B.F.) Parameters C_radTinci, C_radDinci added
!   2008-06-10 (W.S. Freiburg) side_bound_mag, top_bound_mag, bottom_bound_mag added. Magnetic 
!                     boundary conditions are now separated from hydrodynamic boundary conditions
!   2008-10-22 (W.S. Lansing)  parameters beta_inv, B_theta, B_phi, N_side, N_top, N_bottom
!   2008-10-31 (B.F.) New parameter: dustopamode
!   2009-06-16 (B.F.) New parameter: ar_RotationAxis
!   2010-02-15 (W.S. Freiburg) side_bound_mag split into side_bound_max_x and side_bound_max_y
!   2010-02-16 (W.S. Freiburg) New parameter: va_max
!   2010-04-05 (B.F. Lyon) Parameter C_rhoChangeTop added
!   2010-04-11 (W.S. Freiburg) Parameter C_resBconstant added
!   2010-04-14 (B.F.) B_theta, B_phi, N_side, N_top, N_bottom =>
!                     C_magthetaB, C_magphiB, N_magDiffSide, N_magDiffTop, N_magDiffBottom
!   2010-05-10 (B.F.) New parameters: outfile_chu1, outdim_chu1
!   2010-06-03 (B.F. Freiburg) New parameter: hdTimeIntegrationScheme
!   2010-06-09 (B.F. Lyon) New parameter: hdCheckFlux
!   2011-01-06 (W.S. Exeter) New parameters N_rescellsperchunk, C_resCourant, C_resCourantmax
!   2011-01-27 (W.S. Lyon) New parameter N_orderConstrainedTransport
!--------------------------------------------------------------------------------------------------
implicit none
!
! --- Global parameters ---
!
type gl_type
  integer                               :: nc_p, lw_p
  character(len=80)                     :: parfile, stopfile, contfile, statusfile, &
                                           dumpfile, snapshotfile
  character(len=80)                     :: version
end type gl_type
!
type (gl_type), save                    :: gl
!
! --- Typical scales ---
!
type scale_type
  real                                  :: length
end type scale_type
!
type (scale_type), save                 :: scale
!
! --- All parameters from parameter file ---
!
integer, parameter                      :: ndesmax=20, nhismax=20, &
                                           n_dustGrainRadiusMax=100
!
type par_type
  integer                               :: ndes
  character(len=80), dimension(ndesmax) :: description
  integer                               :: nhis
  character(len=80), dimension(nhismax) :: history
  !
  real                                  :: Teff, &
                                           grav, mass_star, r0_grav, r1_grav, r1_rad, &
                                           nu_rotation
  real, dimension(3)                    :: ar_RotationAxis
  character(len=80)                     :: heat_mode, grav_mode
  !
  character(len=80)                     :: side_bound, top_bound, bottom_bound
  character(len=80)                     :: side_bound_mag_x1, side_bound_mag_x2, top_bound_mag, bottom_bound_mag
  real                                  :: s_inflow, &
                                           C_sChange, C_PChange, C_TChange, C_rhoChangeTop, C_Tsurf, &
                                           C_Hptopfactor, rho_min, luminositypervolume, C_CoreDrag, &
                                           C_magthetaB, C_magphiB, B1_inflow
  integer                               :: N_magDiffSide, N_magDiffTop, N_magDiffBottom
  !
  real                                  :: abuX, abuY, abuZ, Qmol, Gamma
  character(len=80)                     :: eosfile, eospath
  real                                  :: kappamax
  character(len=80)                     :: opafile, opapath
  real                                  :: chem_abumetal
  character(len=80)                     :: chem_reacfile, chem_reacpath
  !
  character(len=80)                     :: hion_datapath, hion_atomfile, hion_abufile, &
                                           hion_edensfile, hion_pffile
  real                                  :: dtime_out_hion
  integer                               :: hion_chunks
  !
  character(len=80)                     :: hdscheme, hdTimeIntegrationScheme, hdsplit, &
                                           hdCheckFlux, reconstruction
  integer                               :: N_hydIter, N_hydMaxIter, N_orderConstrainedTransport
  integer                               :: N_hydcellsperchunk, N_viscellsperchunk, &
                                           N_rescellsperchunk, N_radcellsperchunk
  real                                  :: C_slopered, C_ReconstructiondTimeFactor, C_hydPredfactor
  real                                  :: C_visNeu1, C_visNeu2, C_visDrag, C_visBound
  real                                  :: beta_inv, va_max
  !
  character(len=80)                     :: dustscheme, dustopafile, dustopamode
  integer                               :: n_dustGrainRadius
  real                                  :: C_dust01, C_dust02, C_dust03, C_dust04, C_dust05, &
                                           C_dust06, C_dust07, C_dust08, C_dust09, C_dust10
  real, dimension(1:n_dustGrainRadiusMax)::ar_dustGrainRadius
  !
  character(len=80)                     :: radscheme
  integer                               :: N_radminiter, N_raditer, N_radmaxiter, &
                                           N_radtaurefine, &
                                           N_radthinpoint, N_radthickpoint, &
                                           N_radtheta, N_radphi, N_radsubray, N_radband, &
                                           N_radrsyslevel, N_radoutput
  real                                  :: C_radimplicitmu, C_raditereps, C_raditerstep, &
                                           C_radkappasmooth, C_radTstep, C_radTsmooth, &
                                           C_radTintminfac, &
                                           C_radHtautop, C_radTvisdtau, C_radTvis, &
                                           C_radthintimefac, C_radtcool, C_radscool, C_raddcool, &
                                           C_radTinci, C_radDinci
  character(len=80)                     :: radraybase, radraystar, radpressure
  !
  real                                  :: C_visLinear, &
                                           C_visSmagorinsky, C_visArtificial, C_visExpansion, &
                                           C_visPrturb, &
                                           C_visTensordiag, C_visTensoroff, C_visTensordiv, &
                                           C_visP2Pcoeff, C_visP2Pincl1, C_visP2Pincl2, &
                                           C_resB, C_resBconstant, C_resEpsilon
  !
  real                                  :: starttime, cputime, cputime_remainlimit, &
                                           endtime, plustime
  integer                               :: starttimestep, endtimestep, plustimestep
  !
  real                                  :: dtime_min, dtime_max, dtime_min_stop, dtime_incmax, &
                                           dtime_start, &
                                           C_Courant      , C_Courantmax      , C_maxeiChange, &
                                           C_hydexpCourant, C_hydexpCourantmax, &
                                           C_radCourant   , C_radCourantmax   , C_radmaxeiChange, &
                                           C_visCourant   , C_visCourantmax   , &
                                           C_resCourant   , C_resCourantmax
  !
  real                                  :: dtime_out_full, dtime_out_mean, dtime_out_end
  integer                               :: dtimestep_out_fine, istep_in_start, N_outslicedim_mean
  character(len=80)                     :: infile_start, &
                                           outfile_end , outform_end , outconv_end , &
                                           outfile_full, outform_full, outconv_full, &
                                           outfile_mean, outform_mean, outconv_mean, &
                                           outfile_fine, outform_fine, outconv_fine, &
                                           outfile_chu1
  integer, dimension(2,3)               :: outdim_chu1
  character(len=80)                     :: action
  integer                               :: N_test1, N_test2, N_test3, N_test4, N_test5
  real                                  :: C_test1, C_test2, C_test3, C_test4, C_test5
  character(len=80)                     :: strtest1, strtest2, strtest3, strtest4, strtest5
end type par_type
!
type(par_type), save                    :: par
!
end module rhd_gl_module
