MODULE layered_deposits !----------------------------------------------------------------------- ! NAME ! layered_deposits ! ! DESCRIPTION ! Manages layered deposits thanks to a linked list data structure. ! Provides data types and subroutines to manipulate layers of ice and dust. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use numerics, only: dp, di, k4, tol, eps use surf_ice, only: rho_co2ice, rho_h2oice ! DECLARATION ! ----------- implicit none (type, external) ! PARAMETERS ! ---------- ! Numerical parameter logical(k4), protected :: do_layering ! Physical parameters logical(k4), protected :: impose_dust_ratio ! Impose dust-to-ice ratio instead of dust tendency (see 'dust2ice_ratio') real(dp), protected :: dust2ice_ratio ! Dust-to-ice ratio when ice condenses (10% by default) real(dp), protected :: d_dust ! Tendency of dust [kg/m2/y] real(dp), parameter :: dry_lag_porosity = 0.2_dp ! Porosity of dust lag real(dp), parameter :: wet_lag_porosity = 0.15_dp ! Porosity of water ice lag real(dp), parameter :: co2ice_porosity = 0.05_dp ! Porosity of CO2 ice real(dp), parameter :: h2oice_porosity = 0.1_dp ! Porosity of H2O ice real(dp), parameter :: rho_dust = 2500._dp ! Density of dust [kg.m-3] real(dp), parameter :: h_patchy_dust = 5.e-4_dp ! Height under which a dust lag is considered patchy [m] real(dp), parameter :: h_patchy_ice = 5.e-4_dp ! Height under which a H2O ice lag is considered patchy [m] ! Stratum type codes integer(di), parameter :: STR_TYPE_EMPTY = 0_di ! Empty stratum integer(di), parameter :: STR_TYPE_CO2ICE = 1_di ! CO2 ice stratum integer(di), parameter :: STR_TYPE_H2OICE = 2_di ! H2O ice stratum integer(di), parameter :: STR_TYPE_DUST = 3_di ! Dust stratum integer(di), parameter :: STR_TYPE_MIXED = 4_di ! Mixed stratum (ice and dust) ! Parameters for pore ice profile reconstruction integer(di), parameter :: ncoef_poreice = 4_di ! Number of cubic coefficients for pore ice profile real(dp), parameter :: dz_poreice = 0.01_dp ! Target depth step for pore-ice profile reconstruction [m] integer(di), parameter :: nmin_poreice = 4_di ! Minimal number of points for pore-ice profile reconstruction integer(di), parameter :: nmax_poreice = 64_di ! Maximal number of points for pore-ice profile reconstruction ! Parameters for CO2 flux correction (inspired by Levrard et al. 2007) real(dp), parameter :: hmin_lag_co2 = 0.5_dp ! Minimal height of the lag deposit to reduce the sublimation rate [m] real(dp), parameter :: fred_sublco2 = 0.1_dp ! Reduction factor of sublimation rate ! DATA STRUCTURES ! --------------- ! Stratum = node of the linked list type :: stratum real(dp) :: top_elevation = 1._dp ! Layer top_elevation (top height from the surface) [m] real(dp) :: h_co2ice = 0._dp ! Height of CO2 ice [m] real(dp) :: h_h2oice = 0._dp ! Height of H2O ice [m] real(dp) :: h_dust = 1._dp ! Height of dust [m] real(dp) :: h_pore = 0._dp ! Height of pores [m] real(dp), dimension(ncoef_poreice) :: coef_poreice = 0._dp ! Coefficients for pore ice profile real(dp) :: form_date = 0._dp ! Stratum formation date [planetary years] type(stratum), pointer :: up => null() ! Upper stratum (next node) type(stratum), pointer :: down => null() ! Lower stratum (previous node) end type stratum ! Layering = linked list type :: layering integer(di) :: nb_str = 0 ! Number of strata type(stratum), pointer :: bottom => null() ! First stratum (head of the list) at the very bottom type(stratum), pointer :: top => null() ! Last stratum (tail of the list) at the very top !type(stratum), pointer :: subice => null() ! Shallowest stratum containing compact ice end type layering ! Array of pointers to "stratum" type :: ptrarray type(stratum), pointer :: p end type ptrarray ! VISIBILITY ! ---------- private public :: do_layering, ncoef_poreice, layering, ptrarray public :: set_layered_deposits_config, add_stratum, ini_layering, del_layering, print_layering, get_nb_str_max, map2array public :: array2map, layering2surfice, surfice2layering, compute_totmass_layering, compute_poreice_dustlag, evolve_layering contains !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ !======================================================================= SUBROUTINE set_layered_deposits_config(do_layering_in,impose_dust_ratio_in,d_dust_in,dust2ice_ratio_in) !----------------------------------------------------------------------- ! NAME ! set_layered_deposits_config ! ! DESCRIPTION ! Setter for "layered_deposits" configuration parameters. ! ! AUTHORS & DATE ! JB Clement, 02/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use utility, only: real2str, bool2str use display, only: print_msg, LVL_NFO use stoppage, only: stop_clean ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- logical(k4), intent(in) :: do_layering_in, impose_dust_ratio_in real(dp), intent(in) :: d_dust_in, dust2ice_ratio_in ! CODE ! ---- do_layering = do_layering_in impose_dust_ratio = impose_dust_ratio_in d_dust = d_dust_in dust2ice_ratio = dust2ice_ratio_in call print_msg('do_layering = '//bool2str(do_layering),LVL_NFO) call print_msg('impose_dust_ratio = '//bool2str(impose_dust_ratio),LVL_NFO) call print_msg('d_dust = '//real2str(d_dust),LVL_NFO) call print_msg('dust2ice_ratio = '//real2str(dust2ice_ratio),LVL_NFO) if (dust2ice_ratio < 0._dp .or. dust2ice_ratio > 1._dp) call stop_clean(__FILE__,__LINE__,'''dust2ice_ratio'' outside admissible range [0.,1.]!',1) END SUBROUTINE set_layered_deposits_config !======================================================================= !======================================================================= ELEMENTAL FUNCTION get_str_type(str) RESULT(type) !----------------------------------------------------------------------- ! NAME ! get_str_type ! ! DESCRIPTION ! Determine stratum type from composition and pore-ice state. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), intent(in) :: str ! RESULT ! ------ integer(di) :: type ! Type of the stratum ! CODE ! ---- if (str%h_co2ice <= tol .and. str%h_h2oice <= tol .and. str%h_dust <= tol .and. is_poreice_empty(str)) then type = STR_TYPE_EMPTY else if (str%h_co2ice > str%h_h2oice .and. str%h_co2ice > str%h_dust) then type = STR_TYPE_CO2ICE else if (str%h_h2oice > str%h_co2ice .and. str%h_h2oice > str%h_dust) then type = STR_TYPE_H2OICE else if (str%h_dust > str%h_co2ice .and. str%h_dust > str%h_h2oice) then type = STR_TYPE_DUST else type = STR_TYPE_MIXED end if END FUNCTION get_str_type !======================================================================= !======================================================================= ELEMENTAL FUNCTION strtype2txt(type) RESULT(txt_type) !----------------------------------------------------------------------- ! NAME ! strtype2txt ! ! DESCRIPTION ! Convert stratum type integer code into a readable string. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- integer(di), intent(in) :: type ! RESULT ! ------ character(16) :: txt_type ! Readable name of the stratum type ! CODE ! ---- select case (type) case (STR_TYPE_EMPTY) txt_type = 'EMPTY' case (STR_TYPE_CO2ICE) txt_type = 'CO2_ICE' case (STR_TYPE_H2OICE) txt_type = 'H2O_ICE' case (STR_TYPE_DUST) txt_type = 'DUST' case (STR_TYPE_MIXED) txt_type = 'MIXED' case default txt_type = 'UNKNOWN' end select END FUNCTION strtype2txt !======================================================================= !======================================================================= SUBROUTINE print_stratum(this) !----------------------------------------------------------------------- ! NAME ! print_stratum ! ! DESCRIPTION ! Print a stratum. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use utility, only: real2str use display, only: print_msg, LVL_DBG ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), intent(in) :: this ! LOCAL VARIABLES ! --------------- integer(di) :: i character(128) :: msg ! CODE ! ---- call print_msg('Nature [-]: '//trim(strtype2txt(get_str_type(this))),LVL_DBG) call print_msg('Formation date [plnt y]: '//real2str(this%form_date),LVL_DBG) call print_msg('Top elevation [m]: '//real2str(this%top_elevation),LVL_DBG) call print_msg('Height of CO2 ice [m]: '//real2str(this%h_co2ice),LVL_DBG) call print_msg('Height of H2O ice [m]: '//real2str(this%h_h2oice),LVL_DBG) call print_msg('Height of dust [m]: '//real2str(this%h_dust),LVL_DBG) call print_msg('Height of pores [m]: '//real2str(this%h_pore),LVL_DBG) msg = 'Pore-ice profile coefs [m3/m3]: {' do i = 1, size(this%coef_poreice) msg = trim(msg)//real2str(this%coef_poreice(i))//', ' end do msg = trim(msg)//'}' call print_msg(trim(msg),LVL_DBG) END SUBROUTINE print_stratum !======================================================================= !======================================================================= PURE SUBROUTINE add_stratum(this,top_elevation,co2ice,h2oice,dust,pore,coef_poreice,form_date) !----------------------------------------------------------------------- ! NAME ! add_stratum ! ! DESCRIPTION ! Add a stratum to the top of a layering. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this real(dp), intent(in), optional :: top_elevation, co2ice, h2oice, dust, pore real(dp), dimension(:), intent(in), optional :: coef_poreice real(dp), intent(in), optional :: form_date ! LOCAL VARIABLES ! --------------- type(stratum), pointer :: str ! CODE ! ---- ! Creation of the stratum allocate(str) if (present(top_elevation)) str%top_elevation = top_elevation if (present(co2ice)) str%h_co2ice = co2ice if (present(h2oice)) str%h_h2oice = h2oice if (present(dust)) str%h_dust = dust if (present(pore)) str%h_pore = pore if (present(coef_poreice)) str%coef_poreice(:) = coef_poreice(:) if (present(form_date)) str%form_date = form_date ! Increment the number of strata this%nb_str = this%nb_str + 1 ! Add the stratum to the layering if (.not. associated(this%bottom)) then ! If it is the very first one this%bottom => str this%top => str else str%down => this%top this%top%up => str this%top => str end if END SUBROUTINE add_stratum !======================================================================= !======================================================================= PURE SUBROUTINE insert_stratum(this,str_prev,top_elevation,co2ice,h2oice,dust,pore,coef_poreice,form_date) !----------------------------------------------------------------------- ! NAME ! insert_stratum ! ! DESCRIPTION ! Insert a stratum after a given previous stratum in the layering. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this type(stratum), pointer, intent(inout) :: str_prev real(dp), intent(in), optional :: top_elevation, co2ice, h2oice, dust, pore real(dp), dimension(:), intent(in), optional :: coef_poreice real(dp), intent(in), optional :: form_date ! LOCAL VARIABLES ! --------------- type(stratum), pointer :: str, current ! CODE ! ---- if (.not. associated(str_prev%up)) then ! If str_prev is the top stratum of the layering call add_stratum(this,top_elevation,co2ice,h2oice,dust,pore,coef_poreice,form_date) else ! Creation of the stratum allocate(str) if (present(top_elevation)) str%top_elevation = top_elevation if (present(co2ice)) str%h_co2ice = co2ice if (present(h2oice)) str%h_h2oice = h2oice if (present(dust)) str%h_dust = dust if (present(pore)) str%h_pore = pore if (present(coef_poreice)) str%coef_poreice(:) = coef_poreice(:) if (present(form_date)) str%form_date = form_date ! Increment the number of strata this%nb_str = this%nb_str + 1 ! Insert the stratum into the layering at the right place str%down => str_prev str%up => str_prev%up str_prev%up => str str%up%down => str ! Correct the value of top_elevation for the upper strata current => str%up do while (associated(current)) current%top_elevation = current%down%top_elevation + thickness(current) current => current%up end do end if END SUBROUTINE insert_stratum !======================================================================= !======================================================================= PURE SUBROUTINE del_stratum(this,str) !----------------------------------------------------------------------- ! NAME ! del_stratum ! ! DESCRIPTION ! Delete a stratum from the layering and fix links. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this type(stratum), pointer, intent(inout) :: str ! LOCAL VARIABLES ! --------------- type(stratum), pointer :: new_str ! CODE ! ---- ! Protect the sub-surface strata from deletion if (str%top_elevation <= 0._dp) return ! Decrement the number of strata this%nb_str = this%nb_str - 1 ! Making the new links if (associated(str%down)) then ! If there is a lower stratum str%down%up => str%up else ! If it was the first stratum (bottom of layering) this%bottom => str%up end if if (associated(str%up)) then ! If there is an upper stratum str%up%down => str%down else ! If it was the last stratum (top of layering) this%top => str%down end if ! Remove the stratum from the layering new_str => str%down nullify(str%up,str%down) deallocate(str) nullify(str) str => new_str END SUBROUTINE del_stratum !======================================================================= !======================================================================= ELEMENTAL FUNCTION are_strata_similar(str_lo,str_hi,frac_tol,coef_tol,date_tol) RESULT(similar) !----------------------------------------------------------------------- ! NAME ! are_strata_similar ! ! DESCRIPTION ! Check if two adjacent strata can be merged based on composition, ! pore-ice profile, and optional formation-date proximity. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! Negative value for date_tol means that formation date is not checked ! for similarity. !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), intent(in) :: str_lo, str_hi real(dp), intent(in) :: frac_tol, coef_tol, date_tol ! RESULT ! ------ logical(k4) :: similar ! True if the two strata can be merged ! LOCAL VARIABLES ! --------------- real(dp) :: h_lo, h_hi real(dp) :: f_lo_co2, f_lo_h2o, f_lo_dust, f_lo_pore real(dp) :: f_hi_co2, f_hi_h2o, f_hi_dust, f_hi_pore ! CODE ! ---- similar = .false. ! Keep the neutral base layer untouched. if (str_lo%top_elevation <= tol) return ! Strata must be of the same type to be merged if (get_str_type(str_lo) /= get_str_type(str_hi)) return ! Check that the composition fractions are similar within the tolerance h_lo = thickness(str_lo) f_lo_co2 = str_lo%h_co2ice/h_lo f_lo_h2o = str_lo%h_h2oice/h_lo f_lo_dust = str_lo%h_dust/h_lo f_lo_pore = str_lo%h_pore/h_lo h_hi = thickness(str_hi) f_hi_co2 = str_hi%h_co2ice/h_hi f_hi_h2o = str_hi%h_h2oice/h_hi f_hi_dust = str_hi%h_dust/h_hi f_hi_pore = str_hi%h_pore/h_hi if (abs(f_lo_co2 - f_hi_co2) > frac_tol) return if (abs(f_lo_h2o - f_hi_h2o) > frac_tol) return if (abs(f_lo_dust - f_hi_dust) > frac_tol) return if (abs(f_lo_pore - f_hi_pore) > frac_tol) return if (maxval(abs(str_lo%coef_poreice - str_hi%coef_poreice)) > coef_tol) return ! Optionally check that the formation dates are close enough if (date_tol >= 0._dp) then if (abs(str_lo%form_date - str_hi%form_date) > date_tol) return end if similar = .true. END FUNCTION are_strata_similar !======================================================================= !======================================================================= ELEMENTAL SUBROUTINE merge_adjacent_strata(this,frac_tol,coef_tol,date_tol,max_pass) !----------------------------------------------------------------------- ! NAME ! merge_adjacent_strata ! ! DESCRIPTION ! Merge adjacent strata with similar properties to limit unnecessary ! stratification growth. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! Negative value for date_tol means that formation date is not checked ! for similarity. !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this real(dp), intent(in), optional :: frac_tol, coef_tol, date_tol integer(di), intent(in), optional :: max_pass ! LOCAL VARIABLES ! --------------- type(stratum), pointer :: str_lo, str_hi real(dp) :: h_lo, h_hi, h_tot, w_lo, w_hi real(dp) :: frac_tol_loc, coef_tol_loc, date_tol_loc logical(k4) :: merged_in_pass integer(di) :: ipass, max_pass_loc ! CODE ! ---- if (this%nb_str <= 2) return frac_tol_loc = 1.e-2_dp coef_tol_loc = 1.e-2_dp date_tol_loc = -1._dp if (present(frac_tol)) frac_tol_loc = max(0._dp,frac_tol) if (present(coef_tol)) coef_tol_loc = max(0._dp,coef_tol) if (present(date_tol)) date_tol_loc = date_tol max_pass_loc = this%nb_str if (present(max_pass)) max_pass_loc = max(1_di,max_pass) do ipass = 1,max_pass_loc merged_in_pass = .false. str_lo => this%bottom do while (associated(str_lo) .and. associated(str_lo%up)) str_hi => str_lo%up if (are_strata_similar(str_lo,str_hi,frac_tol_loc,coef_tol_loc,date_tol_loc)) then h_lo = thickness(str_lo) h_hi = thickness(str_hi) h_tot = h_lo + h_hi w_lo = h_lo/h_tot w_hi = h_hi/h_tot str_lo%top_elevation = str_hi%top_elevation str_lo%h_co2ice = str_lo%h_co2ice + str_hi%h_co2ice str_lo%h_h2oice = str_lo%h_h2oice + str_hi%h_h2oice str_lo%h_dust = str_lo%h_dust + str_hi%h_dust str_lo%h_pore = str_lo%h_pore + str_hi%h_pore str_lo%coef_poreice(:) = w_lo*str_lo%coef_poreice(:) + w_hi*str_hi%coef_poreice(:) str_lo%form_date = w_lo*str_lo%form_date + w_hi*str_hi%form_date call del_stratum(this,str_hi) merged_in_pass = .true. cycle end if str_lo => str_lo%up end do if (.not. merged_in_pass) exit end do END SUBROUTINE merge_adjacent_strata !======================================================================= !======================================================================= FUNCTION get_stratum(lay,i) RESULT(str) !----------------------------------------------------------------------- ! NAME ! get_stratum ! ! DESCRIPTION ! Return the i-th stratum in the layering. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use stoppage, only: stop_clean ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(in) :: lay integer(di), intent(in) :: i ! RESULT ! ------ type(stratum), pointer :: str ! Pointer to the i-th stratum ! LOCAL VARIABLES ! --------------- integer(di) :: k ! CODE ! ---- if (i < 1 .or. i > lay%nb_str) call stop_clean(__FILE__,__LINE__,'requested number is beyond the number of strata of the layering!',1) k = 1 str => lay%bottom do while (k /= i) str => str%up k = k + 1 end do END FUNCTION get_stratum !======================================================================= !======================================================================= ELEMENTAL SUBROUTINE ini_layering(this) !----------------------------------------------------------------------- ! NAME ! ini_layering ! ! DESCRIPTION ! Initialize the layering base structure. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- !~ ! DEPENDENCIES !~ ! ------------ !~ use soil, only: do_soil, layer, index_breccia, index_bedrock, regolith_porosity, breccia_porosity !~ use geometry, only: nsoil ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this ! PARAMETERS ! ---------- real(dp), dimension(ncoef_poreice), parameter :: coef_poreice_0 = 0._dp ! LOCAL VARIABLES ! --------------- !~ integer(di) :: i !~ real(dp) :: h_soil, h_pore, h_tot ! CODE ! ---- !~ ! Creation of strata at the bottom of the layering to describe the sub-surface !~ if (do_soil) then !~ do i = nsoil,index_bedrock,-1 !~ h_soil = layer(i) - layer(i - 1) ! No porosity !~ call add_stratum(this,-layer(i - 1),0._dp,0._dp,h_soil,0._dp,coef_poreice_0) !~ end do !~ do i = index_bedrock - 1,index_breccia,-1 !~ h_tot = layer(i) - layer(i - 1) !~ h_pore = h_tot*breccia_porosity !~ h_soil = h_tot - h_pore !~ call add_stratum(this,-layer(i - 1),0._dp,0._dp,h_soil,h_pore,coef_poreice_0) !~ end do !~ do i = index_breccia - 1,2,-1 !~ h_tot = layer(i) - layer(i - 1) !~ h_pore = h_tot*regolith_porosity !~ h_soil = h_tot - h_pore !~ call add_stratum(this,-layer(i - 1),0._dp,0._dp,h_soil,h_pore,coef_poreice_0) !~ end do !~ h_pore = layer(1)*regolith_porosity !~ h_soil = layer(1) - h_pore !~ call add_stratum(this,0._dp,0._dp,0._dp,h_soil,h_pore,coef_poreice_0) !~ end if ! Creation of a neutral base stratum at the surface call add_stratum(this,0._dp,0._dp,0._dp,0._dp,0._dp,coef_poreice_0) END SUBROUTINE ini_layering !======================================================================= !======================================================================= ELEMENTAL SUBROUTINE del_layering(this) !----------------------------------------------------------------------- ! NAME ! del_layering ! ! DESCRIPTION ! Delete all strata in a layering and reset counters. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this ! LOCAL VARIABLES ! --------------- type(stratum), pointer :: current, next ! CODE ! ---- current => this%bottom do while (associated(current)) next => current%up deallocate(current) nullify(current) current => next end do this%nb_str = 0 nullify(this%top) nullify(this%bottom) END SUBROUTINE del_layering !======================================================================= !======================================================================= SUBROUTINE print_layering(this) !----------------------------------------------------------------------- ! NAME ! print_layering ! ! DESCRIPTION ! Display all strata from top to bottom for a layering. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use utility, only: int2str use display, only: print_msg, LVL_DBG ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(in) :: this ! LOCAL VARIABLES ! --------------- type(stratum), pointer :: current integer(di) :: i ! CODE ! ---- current => this%top i = this%nb_str do while (associated(current)) call print_msg('Stratum '//int2str(i),LVL_DBG) call print_stratum(current) call print_msg('',LVL_DBG) current => current%down i = i - 1 end do END SUBROUTINE print_layering !======================================================================= !======================================================================= PURE FUNCTION get_nb_str_max(layerings_map) RESULT(nb_str_max) !----------------------------------------------------------------------- ! NAME ! get_nb_str_max ! ! DESCRIPTION ! Return the maximum number of strata across all grid/slope layerings. ! ! AUTHORS & DATE ! JB Clement, 04/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use geometry, only: ngrid, nslope ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), dimension(:,:), intent(in) :: layerings_map ! RESULT ! ------ integer(di) :: nb_str_max ! Maximum number of strata ! LOCAL VARIABLES ! --------------- integer(di) :: ig, islope ! CODE ! ---- nb_str_max = 0 do islope = 1,nslope do ig = 1,ngrid nb_str_max = max(layerings_map(ig,islope)%nb_str,nb_str_max) end do end do END FUNCTION get_nb_str_max !======================================================================= !======================================================================= SUBROUTINE map2array(layerings_map,layerings_array) !----------------------------------------------------------------------- ! NAME ! map2array ! ! DESCRIPTION ! Convert the layerings map into an allocatable array for output. ! ! AUTHORS & DATE ! JB Clement, 04/2024 ! ! NOTES ! The array is padded with zeros above the top stratum of each layering. !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use geometry, only: ngrid, nslope ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), dimension(:,:), intent(in) :: layerings_map real(dp), dimension(:,:,:,:), intent(out) :: layerings_array ! LOCAL VARIABLES ! --------------- type(stratum), pointer :: current integer(di) :: ig, islope, k ! CODE ! ---- layerings_array = 0._dp do islope = 1,nslope do ig = 1,ngrid current => layerings_map(ig,islope)%bottom k = 1 do while (associated(current)) layerings_array(ig,islope,k,1) = current%top_elevation layerings_array(ig,islope,k,2) = current%h_co2ice layerings_array(ig,islope,k,3) = current%h_h2oice layerings_array(ig,islope,k,4) = current%h_dust layerings_array(ig,islope,k,5) = current%h_pore layerings_array(ig,islope,k,6:9) = current%coef_poreice(:) layerings_array(ig,islope,k,10) = current%form_date current => current%up k = k + 1 end do end do end do END SUBROUTINE map2array !======================================================================= !======================================================================= PURE SUBROUTINE array2map(layerings_array,nb_str,layerings_map) !----------------------------------------------------------------------- ! NAME ! array2map ! ! DESCRIPTION ! Convert the stratification array back into the layerings map. ! ! AUTHORS & DATE ! JB Clement, 04/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use geometry, only: ngrid, nslope ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- real(dp), dimension(:,:,:,:), intent(in) :: layerings_array integer(di), dimension(:,:), intent(in) :: nb_str type(layering), dimension(:,:), intent(inout) :: layerings_map ! LOCAL VARIABLES ! --------------- integer(di) :: ig, islope, k ! CODE ! ---- do islope = 1,nslope do ig = 1,ngrid do k = 1,nb_str(ig,islope) call add_stratum(layerings_map(ig,islope),layerings_array(ig,islope,k,1), & layerings_array(ig,islope,k,2),layerings_array(ig,islope,k,3), & layerings_array(ig,islope,k,4),layerings_array(ig,islope,k,5), & layerings_array(ig,islope,k,6:9),layerings_array(ig,islope,k,10)) end do end do end do END SUBROUTINE array2map !======================================================================= !======================================================================= SUBROUTINE layering2surfice(layerings_map,h2o_ice,co2_ice,h2oice_depth) !----------------------------------------------------------------------- ! NAME ! layering2surfice ! ! DESCRIPTION ! Convert the layerings map into surface ice arrays. ! ! AUTHORS & DATE ! JB Clement, 01/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use geometry, only: ngrid, nslope ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), dimension(:,:), intent(in) :: layerings_map real(dp), dimension(:,:), intent(out) :: h2o_ice, co2_ice, h2oice_depth ! LOCAL VARIABLES ! --------------- integer(di) :: ig, islope ! CODE ! ---- h2o_ice(:,:) = 0._dp co2_ice(:,:) = 0._dp h2oice_depth(:,:) = 0._dp do ig = 1,ngrid do islope = 1,nslope if (get_str_type(layerings_map(ig,islope)%top) == STR_TYPE_CO2ICE) then co2_ice(ig,islope) = layerings_map(ig,islope)%top%h_co2ice*rho_co2ice else if (get_str_type(layerings_map(ig,islope)%top) == STR_TYPE_H2OICE) then h2o_ice(ig,islope) = layerings_map(ig,islope)%top%h_h2oice*rho_h2oice else call get_subice_layering(layerings_map(ig,islope),h2oice_depth(ig,islope),h2o_ice(ig,islope),co2_ice(ig,islope)) end if end do end do END SUBROUTINE layering2surfice !======================================================================= !======================================================================= PURE SUBROUTINE surfice2layering(h2o_ice,co2_ice,layerings_map) !----------------------------------------------------------------------- ! NAME ! surfice2layering ! ! DESCRIPTION ! Convert surface ice arrays into the layerings map. ! ! AUTHORS & DATE ! JB Clement, 01/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use geometry, only: ngrid, nslope ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- real(dp), dimension(:,:), intent(in) :: h2o_ice, co2_ice type(layering), dimension(:,:), intent(inout) :: layerings_map ! LOCAL VARIABLES ! --------------- integer(di) :: i, islope ! CODE ! ---- do islope = 1,nslope do i = 1,ngrid layerings_map(i,islope)%top%h_h2oice = h2o_ice(i,islope)/rho_h2oice layerings_map(i,islope)%top%h_co2ice = co2_ice(i,islope)/rho_co2ice end do end do END SUBROUTINE surfice2layering !======================================================================= !======================================================================= SUBROUTINE compute_totmass_layering(layerings_map,totmass_h2o,totmass_co2) !----------------------------------------------------------------------- ! NAME ! compute_totmass_layering ! ! DESCRIPTION ! Compute the total mass of H2O and CO2 ice held by the layerings. ! ! AUTHORS & DATE ! JB Clement, 08/2026 ! ! NOTES ! Only the compact ice of the strata is counted. The pore ice is ! described by 'coef_poreice' which are not filled yet. !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use numerics, only: qp use geometry, only: ngrid, nslope, cell_area use slopes, only: slope_weight ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), dimension(:,:), intent(in) :: layerings_map real(qp), intent(out) :: totmass_h2o ! Total mass of H2O ice in the layerings [kg] real(qp), intent(out) :: totmass_co2 ! Total mass of CO2 ice in the layerings [kg] ! LOCAL VARIABLES ! --------------- integer(di) :: i, islope real(dp) :: area type(stratum), pointer :: current ! CODE ! ---- totmass_h2o = 0._qp totmass_co2 = 0._qp do i = 1,ngrid do islope = 1,nslope area = cell_area(i)*slope_weight(i,islope) current => layerings_map(i,islope)%bottom do while (associated(current)) totmass_h2o = totmass_h2o + real(current%h_h2oice*rho_h2oice*area,qp) totmass_co2 = totmass_co2 + real(current%h_co2ice*rho_co2ice*area,qp) current => current%up end do end do end do END SUBROUTINE compute_totmass_layering !======================================================================= !======================================================================= SUBROUTINE print_layerings_map(this) !----------------------------------------------------------------------- ! NAME ! print_layerings_map ! ! DESCRIPTION ! Display the entire layerings map for all grids and slopes. ! ! AUTHORS & DATE ! JB Clement, 04/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use geometry, only: ngrid, nslope use display, only: print_msg, LVL_DBG use utility, only: int2str ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), dimension(:,:), intent(in) :: this ! LOCAL VARIABLES ! --------------- integer(di) :: ig, islope ! CODE ! ---- do ig = 1,ngrid call print_msg('Grid cell '//int2str(ig),LVL_DBG) do islope = 1,nslope call print_msg('Slope number '//int2str(islope),LVL_DBG) call print_layering(this(ig,islope)) call print_msg('~~~~~~~~~~',LVL_DBG) end do call print_msg('--------------------',LVL_DBG) end do END SUBROUTINE print_layerings_map !======================================================================= !======================================================================= PURE SUBROUTINE define_str_grid(h,dz,nmin,nmax,z) !----------------------------------------------------------------------- ! NAME ! define_str_grid ! ! DESCRIPTION ! Build a normalized-depth grid with adaptive point count. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- real(dp), intent(in) :: h, dz integer(di), intent(in) :: nmin, nmax real(dp), dimension(:), allocatable, intent(out) :: z ! LOCAL VARIABLES ! --------------- integer(di) :: n, i ! CODE ! ---- n = nmin if (dz > eps) n = ceiling(max(0._dp,h)/dz) n = max(n,nmin) n = min(n,nmax) n = max(n,2_di) if (allocated(z)) deallocate(z) allocate(z(n)) do i = 1,n z(i) = real(i - 1,dp)/real(n - 1,dp) end do END SUBROUTINE define_str_grid !======================================================================= !======================================================================= ELEMENTAL FUNCTION is_poreice_empty(str) RESULT(profile_empty) !----------------------------------------------------------------------- ! NAME ! is_poreice_empty ! ! DESCRIPTION ! Determine if a pore ice profile is empty. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), intent(in) :: str ! RESULT ! ------ logical(k4) :: profile_empty ! True if the pore ice profile is empty ! LOCAL VARIABLES ! --------------- integer(di) :: i ! CODE ! ---- profile_empty = .true. do i = 1,ncoef_poreice if (abs(str%coef_poreice(i)) > tol) then profile_empty = .false. exit end if end do END FUNCTION is_poreice_empty !======================================================================= !======================================================================= ELEMENTAL FUNCTION has_compact_ice(str) RESULT(has_ice) !----------------------------------------------------------------------- ! NAME ! has_compact_ice ! ! DESCRIPTION ! Check if a stratum contains compact ice. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), intent(in) :: str ! RESULT ! ------ logical(k4) :: has_ice ! True if the stratum contains compact ice ! CODE ! ---- has_ice = .false. if (str%h_co2ice > tol .or. str%h_h2oice > tol) has_ice = .true. END FUNCTION has_compact_ice !======================================================================= !======================================================================= ELEMENTAL FUNCTION is_pure_dust(str) RESULT(pure_dust) !----------------------------------------------------------------------- ! NAME ! is_pure_dust ! ! DESCRIPTION ! Check if a stratum contains only dust (not cemented by ice). ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), intent(in) :: str ! RESULT ! ------ logical(k4) :: pure_dust ! True if the stratum is made of dust only ! CODE ! ---- pure_dust = .false. if (str%h_dust > tol .and. .not. has_compact_ice(str) .and. is_poreice_empty(str)) pure_dust = .true. END FUNCTION is_pure_dust !======================================================================= !======================================================================= ELEMENTAL FUNCTION is_dust_lag(str) RESULT(dust_lag) !----------------------------------------------------------------------- ! NAME ! is_dust_lag ! ! DESCRIPTION ! Check if a stratum is a dust lag. ! ! AUTHORS & DATE ! JB Clement, 05/2025 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), intent(in) :: str ! RESULT ! ------ logical(k4) :: dust_lag ! True if the stratum is a dust lag ! CODE ! ---- dust_lag = .false. if (str%h_dust > tol .and. .not. has_compact_ice(str)) dust_lag = .true. END FUNCTION is_dust_lag !======================================================================= !======================================================================= ELEMENTAL SUBROUTINE compute_poreice_dustlag(this) !----------------------------------------------------------------------- ! NAME ! compute_poreice_dustlag ! ! DESCRIPTION ! Update top dust-lag pore-ice coefficients from dynamic pore filling. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use subsurface_ice, only: dyn_ss_ice_m ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this ! LOCAL VARIABLES ! --------------- ! CODE ! ---- ! Identify the contiguous top dust-lag column ! Ensure the top dust-lag column overlays H2O ice ! Build the grid for each dust-lag stratum ! If the dust lag is too thin to be resolved by the grid, skip the update ! Build the full grid and pore-ice profile for the dust-lag column ! Compute the dynamic grid for the pore filling model, which is finer near the surface to better resolve the expected gradients ! Interpolate the pore-ice profile onto the dynamic grid, assuming that all pores are filled below the dust lag ! Get the updated pore-ice profile ! Interpolate the updated pore-ice profile back onto the stratum-specific grids ! Update the pore-ice coefficients for each dust-lag stratum END SUBROUTINE compute_poreice_dustlag !======================================================================= !======================================================================= ELEMENTAL FUNCTION thickness(this) RESULT(h_str) !----------------------------------------------------------------------- ! NAME ! thickness ! ! DESCRIPTION ! Compute the total thickness of a stratum. ! ! AUTHORS & DATE ! JB Clement, 05/2025 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), intent(in) :: this ! RESULT ! ------ real(dp) :: h_str ! Thickness of the stratum [m] ! CODE ! ---- h_str = this%h_co2ice + this%h_h2oice + this%h_dust + this%h_pore END FUNCTION thickness !======================================================================= !======================================================================= SUBROUTINE get_subice_layering(this,h_toplag,h2o_ice,co2_ice) !----------------------------------------------------------------------- ! NAME ! get_subice_layering ! ! DESCRIPTION ! Get data about possible subsurface ice in a layering. ! ! AUTHORS & DATE ! JB Clement, 05/2025 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(in) :: this real(dp), intent(out) :: h2o_ice, co2_ice, h_toplag ! LOCAL VARIABLES ! --------------- type(stratum), pointer :: current ! CODE ! ---- h_toplag = 0._dp h2o_ice = 0._dp co2_ice = 0._dp current => this%top ! Is the considered stratum a dust lag? if (.not. is_dust_lag(current)) return do h_toplag = h_toplag + thickness(current) if (.not. associated(current%down)) then ! Bottom of layering and only dust lag strata h_toplag = 0._dp return end if current => current%down if (.not. is_dust_lag(current)) exit end do ! Is the stratum below the dust lag made of ice? if (current%h_h2oice > tol .and. current%h_h2oice > current%h_co2ice) then ! Yes, there is more H2O ice than CO2 ice if (h_toplag < h_patchy_dust) then ! But the dust lag is too thin to considered ice as subsurface ice h_toplag = 0._dp h2o_ice = current%h_h2oice*rho_h2oice end if else if (current%h_co2ice > tol .and. current%h_co2ice > current%h_h2oice) then ! No, there is more CO2 ice than H2O ice if (h_toplag < h_patchy_ice) co2_ice = current%h_co2ice*rho_co2ice ! The dust lag is too thin to considered ice as subsurface ice h_toplag = 0._dp ! Because it matters only for H2O ice end if END SUBROUTINE get_subice_layering !======================================================================= !======================================================================= PURE SUBROUTINE lift_dust(this,str,h2lift) !----------------------------------------------------------------------- ! NAME ! lift_dust ! ! DESCRIPTION ! Lift dust in a stratum by wind erosion or other processes. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this type(stratum), pointer, intent(inout) :: str real(dp), intent(in) :: h2lift ! CODE ! ---- ! Update of properties in the eroding dust lag str%top_elevation = str%top_elevation - h2lift*(1._dp + str%h_pore/str%h_dust) str%h_pore = max(0._dp,str%h_pore - h2lift*str%h_pore/str%h_dust) str%h_dust = max(0._dp,str%h_dust - h2lift) ! Remove the eroding dust lag if there is no dust anymore if (str%h_dust < tol) call del_stratum(this,str) END SUBROUTINE lift_dust !======================================================================= !======================================================================= PURE SUBROUTINE sublimate_ice(this,str,h2subl_co2ice,h2subl_h2oice,new_lag,zlag,current_date) !----------------------------------------------------------------------- ! NAME ! sublimate_ice ! ! DESCRIPTION ! Handle ice sublimation in a stratum and create lag layer. ! ! AUTHORS & DATE ! JB Clement, 03/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(layering), intent(inout) :: this type(stratum), pointer, intent(inout) :: str logical(k4), intent(inout) :: new_lag real(dp), intent(inout) :: zlag real(dp), intent(in) :: h2subl_co2ice, h2subl_h2oice real(dp), intent(in) :: current_date ! PARAMETERS ! ---------- real(dp), dimension(ncoef_poreice), parameter :: coef_poreice_0 = 0._dp ! LOCAL VARIABLES ! --------------- real(dp) :: h2subl, h_ice, h_pore, h_pore_new, h_tot real(dp) :: hlag_dust, hlag_h2oice type(stratum), pointer :: current ! CODE ! ---- h_ice = str%h_co2ice + str%h_h2oice h_pore = str%h_pore h2subl = h2subl_co2ice + h2subl_h2oice ! How much dust and H2O ice does ice sublimation involve to create the lag? hlag_dust = str%h_dust*h2subl/h_ice hlag_h2oice = 0._dp if (h2subl_co2ice > 0._dp .and. h2subl_h2oice < tol) hlag_h2oice = str%h_h2oice*h2subl/h_ice ! Update of properties in the sublimating stratum str%top_elevation = str%top_elevation - h2subl*(1._dp + h_pore/h_ice) - hlag_dust - hlag_h2oice str%h_co2ice = str%h_co2ice - h2subl_co2ice str%h_h2oice = str%h_h2oice - h2subl_h2oice - hlag_h2oice str%h_dust = str%h_dust - hlag_dust str%h_pore = str%h_pore - h2subl*h_pore/h_ice str%h_co2ice = max(0._dp,str%h_co2ice) str%h_h2oice = max(0._dp,str%h_h2oice) str%h_dust = max(0._dp,str%h_dust) str%h_pore = max(0._dp,str%h_pore) ! Correct the value of top_elevation for the upper strata current => str%up do while (associated(current)) current%top_elevation = current%down%top_elevation + thickness(current) current => current%up end do ! Remove the sublimating stratum if there is no ice anymore if (str%h_co2ice < tol .and. str%h_h2oice < tol) call del_stratum(this,str) ! Which porosity is considered? if (hlag_dust >= hlag_h2oice) then h_pore_new = hlag_dust*dry_lag_porosity/(1._dp - dry_lag_porosity) else h_pore_new = hlag_h2oice*wet_lag_porosity/(1._dp - wet_lag_porosity) end if h_tot = hlag_dust + hlag_h2oice + h_pore_new ! New stratum above the current stratum as a lag layer if (new_lag) then call insert_stratum(this,str,str%top_elevation + h_tot,0._dp,hlag_h2oice,hlag_dust,h_pore_new,coef_poreice_0,current_date) new_lag = .false. else str%up%top_elevation = str%up%top_elevation + h_tot str%up%h_h2oice = str%up%h_h2oice + hlag_h2oice str%up%h_dust = str%up%h_dust + hlag_dust str%up%h_pore = str%up%h_pore + h_pore_new end if zlag = zlag + h_tot END SUBROUTINE sublimate_ice !======================================================================= !======================================================================= SUBROUTINE get_co2_subl_resistance(current,R_subl) !----------------------------------------------------------------------- ! NAME ! get_co2_subl_resistance ! ! DESCRIPTION ! Compute CO2 sublimation inhibition factor due to upper lag strata. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- type(stratum), pointer, intent(in) :: current real(dp), intent(out) :: R_subl ! LOCAL VARIABLES ! --------------- real(dp) :: h_toplag type(stratum), pointer :: currentloc ! CODE ! ---- R_subl = 1._dp if (.not. associated(current%up)) return ! If there is no upper stratum h_toplag = 0._dp currentloc => current%up do while (associated(currentloc%up) .and. is_dust_lag(currentloc)) h_toplag = h_toplag + thickness(currentloc) currentloc => currentloc%up end do if (currentloc%h_h2oice >= h_patchy_ice) then ! There is a thick H2O ice layer above the dust lag, so CO2 sublimation is fully inhibited R_subl = 0._dp else h_toplag = h_toplag + thickness(currentloc) R_subl = fred_sublco2**(h_toplag/hmin_lag_co2) end if END SUBROUTINE get_co2_subl_resistance !======================================================================= !======================================================================= SUBROUTINE settle_h2o_surf_subsurf_tend(h2oice_depth,flux_ssice,d_h2oice,icell,jcell) !----------------------------------------------------------------------- ! NAME ! settle_h2o_surf_subsurf_tend ! ! DESCRIPTION ! Reconcile H2O surface and subsurface tendencies for one grid cell. ! ! AUTHORS & DATE ! JB Clement, 04/2026 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use display, only: print_msg, LVL_ERR, LVL_WRN, LVL_DBG, is_lvl_enabled ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- real(dp), intent(in) :: h2oice_depth real(dp), intent(inout) :: flux_ssice, d_h2oice integer(di), intent(in), optional :: icell, jcell ! LOCAL VARIABLES ! --------------- logical(k4) :: has_ssice, ss_sublim, ss_cond, surf_sublim, surf_cond character(64) :: cell_tag ! CODE ! ---- cell_tag = '' if (present(icell) .and. present(jcell)) then if (is_lvl_enabled(LVL_DBG)) write(cell_tag,'(a,i0,a,i0,a)') ' [i=',icell,',islope=',jcell,']' end if has_ssice = h2oice_depth > 0._dp ss_sublim = flux_ssice < -eps ss_cond = flux_ssice > eps surf_sublim = d_h2oice < -eps surf_cond = d_h2oice > eps if (has_ssice) then if (ss_sublim) then if (surf_sublim) then call print_msg('Sublimating subsurface ice but sublimating surface ice is also present!'//trim(cell_tag),LVL_ERR) else if (surf_cond) then call print_msg('Sublimating subsurface ice but condensing surface ice is also present!'//trim(cell_tag),LVL_WRN) call print_msg('This may happen because yearly minimum of frost is growing!',LVL_WRN) call print_msg('Choice: condensing surface ice overrules sublimating subsurface ice!',LVL_WRN) flux_ssice = 0._dp else ! Needed so the layering algorithm receives a retreat tendency d_h2oice = flux_ssice end if else if (ss_cond) then if (surf_cond) then call print_msg('Condensing subsurface ice but condensing surface ice is also present!'//trim(cell_tag),LVL_WRN) call print_msg('This may happen because yearly minimum of frost is growing!',LVL_WRN) call print_msg('Choice: condensing surface ice overrules condensing subsurface ice!',LVL_WRN) flux_ssice = 0._dp else if (surf_sublim) then call print_msg('Condensing subsurface ice but sublimating surface ice is also present!'//trim(cell_tag),LVL_ERR) else call print_msg('Condensing subsurface ice cannot be handled by the current layering algorithm!'//trim(cell_tag),LVL_ERR) end if else flux_ssice = 0._dp ! Very small subsurface flux is set to zero to avoid numerical issues end if else if (abs(flux_ssice) > eps) call print_msg('Subsurface ice flux is non-zero while there is no subsurface ice!'//trim(cell_tag),LVL_ERR) end if END SUBROUTINE settle_h2o_surf_subsurf_tend !======================================================================= !======================================================================= SUBROUTINE evolve_layering(this,d_co2ice,d_h2oice,h2oice_depth,flux_ssice,new_str,zshift_surf,new_lag,zlag,current,current_date,icell,jcell) !----------------------------------------------------------------------- ! NAME ! evolve_layering ! ! DESCRIPTION ! Main algorithm for layering evolution. Manages ice condensation/sublimation ! and dust sedimentation/lifting. Handles multiple scenarios: building new ! strata from ice/dust, retreating strata from sublimation/lifting, and mixed ! scenarios with CO2 ice sublimation combined with H2O ice condensation. ! ! AUTHORS & DATE ! JB Clement, 09/2024 ! ! NOTES ! !----------------------------------------------------------------------- ! DEPENDENCIES ! ------------ use stoppage, only: stop_clean use utility, only: real2str use display, only: print_msg, LVL_ERR use soil, only: regolith_porosity ! DECLARATION ! ----------- implicit none ! ARGUMENTS ! --------- real(dp), intent(in) :: h2oice_depth real(dp), intent(in) :: current_date integer(di), intent(in), optional :: icell, jcell real(dp), intent(inout) :: d_co2ice, d_h2oice, flux_ssice type(layering), intent(inout) :: this type(stratum), pointer, intent(inout) :: current logical(k4), intent(inout) :: new_str, new_lag real(dp), intent(out) :: zshift_surf, zlag ! PARAMETERS ! ---------- real(dp), dimension(ncoef_poreice), parameter :: coef_poreice_0 = 0._dp ! LOCAL VARIABLES ! --------------- real(dp) :: dh_co2ice, dh_h2oice, dh_dust, dh_dust_co2, dh_dust_h2o real(dp) :: h_co2ice_old, h_h2oice_old real(dp) :: h2subl_co2ice, h2subl_h2oice, h2subl_co2ice_tot, h2subl_h2oice_tot real(dp) :: h2lift, h2lift_tot, h_pore, h_tot!, R_subl logical(k4) :: unexpected ! CODE ! ---- call settle_h2o_surf_subsurf_tend(h2oice_depth,flux_ssice,d_h2oice,icell,jcell) dh_co2ice = d_co2ice/rho_co2ice dh_h2oice = d_h2oice/rho_h2oice dh_dust = 0._dp if (dh_h2oice > 0._dp) then ! To put a dust sedimentation tendency only when ice is condensing if (impose_dust_ratio) then if (dh_co2ice > 0._dp) then dh_dust = dust2ice_ratio*(dh_h2oice + dh_co2ice) else dh_dust = dust2ice_ratio*dh_h2oice end if else dh_dust = d_dust/rho_dust end if end if zshift_surf = this%top%top_elevation zlag = 0._dp unexpected = .false. !~~~~~~~~~~ ! NOTHING HAPPENS if (abs(dh_co2ice) < tol .and. abs(dh_h2oice) < tol .and. abs(dh_dust) < tol) then ! So we do nothing !~~~~~~~~~~ ! BUILDING A STRATUM (ice condensation and/or dust desimentation) else if (dh_co2ice >= 0._dp .and. dh_h2oice >= 0._dp .and. dh_dust >= 0._dp) then ! Which porosity is considered? if (dh_co2ice >= dh_h2oice .and. dh_co2ice >= dh_dust) then ! CO2 ice is dominant h_pore = dh_co2ice*co2ice_porosity/(1._dp - co2ice_porosity) else if (dh_h2oice >= dh_co2ice .and. dh_h2oice >= dh_dust) then ! H2O ice is dominant h_pore = dh_h2oice*h2oice_porosity/(1._dp - h2oice_porosity) else if (dh_dust >= dh_co2ice .and. dh_dust >= dh_h2oice) then ! Dust is dominant h_pore = dh_dust*regolith_porosity/(1._dp - regolith_porosity) else unexpected = .true. end if h_tot = dh_co2ice + dh_h2oice + dh_dust + h_pore ! New stratum at the top of the layering if (new_str) then call add_stratum(this,this%top%top_elevation + h_tot,dh_co2ice,dh_h2oice,dh_dust,h_pore,coef_poreice_0,current_date) new_str = .false. else this%top%top_elevation = this%top%top_elevation + h_tot this%top%h_co2ice = this%top%h_co2ice + dh_co2ice this%top%h_h2oice = this%top%h_h2oice + dh_h2oice this%top%h_dust = this%top%h_dust + dh_dust this%top%h_pore = this%top%h_pore + h_pore end if !~~~~~~~~~~ ! RETREATING A STRATUM (ice sublimation and/or dust lifting) else if (dh_co2ice <= 0._dp .and. dh_h2oice <= 0._dp .and. dh_dust <= 0._dp) then h2subl_co2ice_tot = abs(dh_co2ice) h2subl_h2oice_tot = abs(dh_h2oice) h2lift_tot = abs(dh_dust) do while ((h2subl_co2ice_tot > 0._dp .or. h2subl_h2oice_tot > 0._dp .or. h2lift_tot > 0._dp) .and. associated(current)) h_co2ice_old = current%h_co2ice h_h2oice_old = current%h_h2oice ! How much is CO2 ice sublimation inhibited by the top dust lag? !call get_co2_subl_resistance(current,R_subl) !h2subl_co2ice_tot = h2subl_co2ice_tot*R_subl ! Is there CO2 ice to sublimate? h2subl_co2ice = 0._dp if (h_co2ice_old > 0._dp .and. h2subl_co2ice_tot > 0._dp) then h2subl_co2ice = min(h2subl_co2ice_tot,h_co2ice_old) h2subl_co2ice_tot = h2subl_co2ice_tot - h2subl_co2ice end if ! Is there H2O ice to sublimate? h2subl_h2oice = 0._dp if (h_h2oice_old > 0._dp .and. h2subl_h2oice_tot > 0._dp) then h2subl_h2oice = min(h2subl_h2oice_tot,h_h2oice_old) h2subl_h2oice_tot = h2subl_h2oice_tot - h2subl_h2oice end if ! Ice sublimation if (h2subl_co2ice > 0._dp .or. h2subl_h2oice > 0._dp) call sublimate_ice(this,current,h2subl_co2ice,h2subl_h2oice,new_lag,zlag,current_date) ! Is there dust to lift? h2lift = 0._dp if (is_pure_dust(current) .and. h2lift_tot > 0._dp) then h2lift = min(h2lift_tot,current%h_dust) h2lift_tot = h2lift_tot - h2lift ! Dust lifting if (h2lift > 0._dp) call lift_dust(this,current,h2lift) else if (associated(current%up)) then if (is_pure_dust(current%up) .and. h2lift_tot > 0._dp) then h2lift = min(h2lift_tot,current%up%h_dust) h2lift_tot = h2lift_tot - h2lift ! Dust lifting if (h2lift > 0._dp) call lift_dust(this,current%up,h2lift) end if end if ! Still ice to be sublimated or dust to be lifted and no more ice in the current stratum so we move to the underlying stratum if ((h2subl_co2ice_tot > 0._dp .or. h2subl_h2oice_tot > 0._dp .or. h2lift_tot > 0._dp) .and. & current%h_co2ice < tol .and. current%h_h2oice < tol .and. is_poreice_empty(current)) then current => current%down new_lag = .true. else exit end if end do ! In case the retreat loop exhausted all strata while residual demand remains then the tendency is set to 0 if (h2subl_co2ice_tot > 0._dp) dh_co2ice = 0._dp ! No CO2 ice available anymore so the tendency is set to 0 if (h2subl_h2oice_tot > 0._dp) dh_h2oice = 0._dp ! No H2O ice available anymore so the tendency is set to 0 if (h2lift_tot > 0._dp) dh_dust = 0._dp ! No dust available anymore so the tendency is set to 0 !~~~~~~~~~~ ! CO2 ICE SUBLIMATION + H2O ICE CONDENSATION else if (dh_co2ice <= 0._dp .and. dh_h2oice >= 0._dp) then if (dh_dust >= 0._dp) then dh_dust_co2 = 0._dp dh_dust_h2o = dh_dust else dh_dust_co2 = dh_dust dh_dust_h2o = 0._dp end if if (abs(dh_co2ice) > dh_h2oice) then ! CO2 ice sublimation is dominant ! CO2 ICE SUBLIMATION: retreating a stratum h2subl_co2ice_tot = abs(dh_co2ice) h2lift_tot = abs(dh_dust_co2) do while ((h2subl_co2ice_tot > 0._dp .or. h2lift_tot > 0._dp) .and. associated(current)) h_co2ice_old = current%h_co2ice ! How much is CO2 ice sublimation inhibited by the top dust lag? !call get_co2_subl_resistance(current,R_subl) !h2subl_co2ice_tot = h2subl_co2ice_tot*R_subl ! Is there CO2 ice to sublimate? h2subl_co2ice = 0._dp if (h_co2ice_old > 0._dp .and. h2subl_co2ice_tot > 0._dp) then h2subl_co2ice = min(h2subl_co2ice_tot,h_co2ice_old) h2subl_co2ice_tot = h2subl_co2ice_tot - h2subl_co2ice end if ! Ice sublimation if (h2subl_co2ice > 0._dp) call sublimate_ice(this,current,h2subl_co2ice,0._dp,new_lag,zlag,current_date) ! Is there dust to lift? h2lift = 0._dp if (is_pure_dust(current) .and. h2lift_tot > 0._dp) then h2lift = min(h2lift_tot,current%h_dust) h2lift_tot = h2lift_tot - h2lift ! Dust lifting if (h2lift > 0._dp) call lift_dust(this,current,h2lift) else if (associated(current%up)) then if (is_pure_dust(current%up) .and. h2lift_tot > 0._dp) then h2lift = min(h2lift_tot,current%up%h_dust) h2lift_tot = h2lift_tot - h2lift ! Dust lifting if (h2lift > 0._dp) call lift_dust(this,current%up,h2lift) end if end if ! Still ice to be sublimated or dust to be lifted and no more ice in the current stratum so we move to the underlying stratum if ((h2subl_co2ice_tot > 0._dp .or. h2lift_tot > 0._dp) .and. current%h_co2ice < tol) then current => current%down new_lag = .true. else exit end if end do ! In case the retreat loop exhausted all strata while residual demand remains then the tendency is set to 0 if (h2subl_co2ice_tot > 0._dp) dh_co2ice = 0._dp ! No CO2 ice available anymore so the tendency is set to 0 if (h2lift_tot > 0._dp) dh_dust = 0._dp ! No dust available anymore so the tendency is set to 0 ! H2O ICE CONDENSATION: building a stratum h_pore = dh_h2oice*h2oice_porosity/(1._dp - h2oice_porosity) h_tot = dh_h2oice + dh_dust_h2o + h_pore ! New stratum at the top of the layering if (new_str) then call add_stratum(this,this%top%top_elevation + h_tot,0._dp,dh_h2oice,dh_dust_h2o,h_pore,coef_poreice_0,current_date) new_str = .false. else this%top%top_elevation = this%top%top_elevation + h_tot this%top%h_h2oice = this%top%h_h2oice + dh_h2oice this%top%h_dust = this%top%h_dust + dh_dust_h2o this%top%h_pore = this%top%h_pore + h_pore end if else ! H2O ice condensation is dominant ! H2O ICE CONDENSATION: building a stratum h_pore = dh_h2oice*h2oice_porosity/(1._dp - h2oice_porosity) h_tot = dh_h2oice + dh_dust_h2o + h_pore ! New stratum at the top of the layering if (new_str) then call add_stratum(this,this%top%top_elevation + h_tot,0._dp,dh_h2oice,dh_dust_h2o,h_pore,coef_poreice_0,current_date) new_str = .false. else this%top%top_elevation = this%top%top_elevation + h_tot this%top%h_h2oice = this%top%h_h2oice + dh_h2oice this%top%h_dust = this%top%h_dust + dh_dust_h2o this%top%h_pore = this%top%h_pore + h_pore end if ! CO2 ICE SUBLIMATION: retreating a stratum h2subl_co2ice_tot = abs(dh_co2ice) h2lift_tot = abs(dh_dust_co2) do while ((h2subl_co2ice_tot > 0._dp .or. h2lift_tot > 0._dp) .and. associated(current)) h_co2ice_old = current%h_co2ice ! How much is CO2 ice sublimation inhibited by the top dust lag? !call get_co2_subl_resistance(current,R_subl) !h2subl_co2ice_tot = h2subl_co2ice_tot*R_subl ! Is there CO2 ice to sublimate? h2subl_co2ice = 0._dp if (h_co2ice_old > 0._dp .and. h2subl_co2ice_tot > 0._dp) then h2subl_co2ice = min(h2subl_co2ice_tot,h_co2ice_old) h2subl_co2ice_tot = h2subl_co2ice_tot - h2subl_co2ice end if ! Ice sublimation if (h2subl_co2ice > 0._dp) call sublimate_ice(this,current,h2subl_co2ice,0._dp,new_lag,zlag,current_date) ! Is there dust to lift? h2lift = 0._dp if (is_pure_dust(current) .and. h2lift_tot > 0._dp) then h2lift = min(h2lift_tot,current%h_dust) h2lift_tot = h2lift_tot - h2lift ! Dust lifting if (h2lift > 0._dp) call lift_dust(this,current,h2lift) else if (associated(current%up)) then if (is_pure_dust(current%up) .and. h2lift_tot > 0._dp) then h2lift = min(h2lift_tot,current%up%h_dust) h2lift_tot = h2lift_tot - h2lift ! Dust lifting if (h2lift > 0._dp) call lift_dust(this,current%up,h2lift) end if end if ! Still ice to be sublimated or dust to be lifted and no more ice in the current stratum so we move to the underlying stratum if ((h2subl_co2ice_tot > 0._dp .or. h2lift_tot > 0._dp) .and. current%h_co2ice < tol) then current => current%down new_lag = .true. else exit end if end do ! In case the retreat loop exhausted all strata while residual demand remains then the tendency is set to 0 if (h2subl_co2ice_tot > 0._dp) dh_co2ice = 0._dp ! No CO2 ice available anymore so the tendency is set to 0 if (h2lift_tot > 0._dp) dh_dust = 0._dp ! No dust available anymore so the tendency is set to 0 end if !~~~~~~~~~~ ! NOT EXPECTED SITUATION else unexpected = .true. end if if (unexpected) then call print_msg(' dh_co2ice [m/y] = '//real2str(dh_co2ice),LVL_ERR) call print_msg(' dh_h2oice [m/y] = '//real2str(dh_h2oice),LVL_ERR) call print_msg(' dh_dust [m/y] = '//real2str(dh_dust),LVL_ERR) call print_msg(' flux_ssice [m/y] = '//real2str(flux_ssice),LVL_ERR) call stop_clean(__FILE__,__LINE__,'situation for the layering construction not managed!',1) end if ! New tendencies for ice d_co2ice = dh_co2ice*rho_co2ice d_h2oice = dh_h2oice*rho_h2oice zshift_surf = this%top%top_elevation - zshift_surf END SUBROUTINE evolve_layering !======================================================================= END MODULE layered_deposits