| 1 | MODULE stopping_crit_mod |
|---|
| 2 | |
|---|
| 3 | implicit none |
|---|
| 4 | |
|---|
| 5 | type :: stopFlags |
|---|
| 6 | logical :: surface_h2oice_change = .false. |
|---|
| 7 | logical :: zero_dh2oice = .false. |
|---|
| 8 | logical :: surface_co2ice_change = .false. |
|---|
| 9 | logical :: pressure_change = .false. |
|---|
| 10 | logical :: max_iter_reached = .false. |
|---|
| 11 | logical :: max_years_reached = .false. |
|---|
| 12 | logical :: time_limit_reached = .false. |
|---|
| 13 | contains |
|---|
| 14 | procedure :: is_any_set |
|---|
| 15 | procedure :: stop_code |
|---|
| 16 | procedure :: stop_message |
|---|
| 17 | end type |
|---|
| 18 | |
|---|
| 19 | !======================================================================= |
|---|
| 20 | contains |
|---|
| 21 | !======================================================================= |
|---|
| 22 | ! To detect if any flag is true |
|---|
| 23 | FUNCTION is_any_set(this) RESULT(stopPEM) |
|---|
| 24 | |
|---|
| 25 | class(stopFlags), intent(in) :: this |
|---|
| 26 | logical :: stopPEM |
|---|
| 27 | |
|---|
| 28 | stopPEM = this%surface_h2oice_change .or. & |
|---|
| 29 | this%zero_dh2oice .or. & |
|---|
| 30 | this%surface_co2ice_change .or. & |
|---|
| 31 | this%pressure_change .or. & |
|---|
| 32 | this%max_iter_reached .or. & |
|---|
| 33 | this%max_years_reached .or. & |
|---|
| 34 | this%time_limit_reached |
|---|
| 35 | |
|---|
| 36 | END FUNCTION is_any_set |
|---|
| 37 | |
|---|
| 38 | !======================================================================= |
|---|
| 39 | ! To give the digit code corresponding to the stopping flag |
|---|
| 40 | FUNCTION stop_code(this) result(code) |
|---|
| 41 | |
|---|
| 42 | class(StopFlags), intent(in) :: this |
|---|
| 43 | integer :: code |
|---|
| 44 | |
|---|
| 45 | code = 0 |
|---|
| 46 | if (this%surface_h2oice_change) then; code = 1 |
|---|
| 47 | elseif (this%zero_dh2oice) then; code = 2 |
|---|
| 48 | elseif (this%surface_co2ice_change) then; code = 3 |
|---|
| 49 | elseif (this%pressure_change) then; code = 4 |
|---|
| 50 | elseif (this%max_iter_reached) then; code = 5 |
|---|
| 51 | elseif (this%max_years_reached) then; code = 6 |
|---|
| 52 | elseif (this%time_limit_reached) then; code = 7 |
|---|
| 53 | endif |
|---|
| 54 | |
|---|
| 55 | END FUNCTION stop_code |
|---|
| 56 | |
|---|
| 57 | !======================================================================= |
|---|
| 58 | ! To give the message corresponding to the stopping flag |
|---|
| 59 | FUNCTION stop_message(this) result(msg) |
|---|
| 60 | |
|---|
| 61 | class(StopFlags), intent(in) :: this |
|---|
| 62 | character(:), allocatable :: msg |
|---|
| 63 | integer :: stopCode |
|---|
| 64 | |
|---|
| 65 | if (this%surface_h2oice_change) then; msg = " **** STOPPING because surface of h2o ice has changed too much (see message above)." |
|---|
| 66 | elseif (this%zero_dh2oice) then; msg = " **** STOPPING because tendencies on h2o ice = 0 (see message above)." |
|---|
| 67 | elseif (this%surface_co2ice_change) then; msg = " **** STOPPING because surface of co2 ice has changed too much (see message above)." |
|---|
| 68 | elseif (this%pressure_change) then; msg = " **** STOPPING because surface global pressure has changed too much (see message above)." |
|---|
| 69 | elseif (this%max_iter_reached) then; msg = " **** STOPPING because maximum number of iterations is reached (possibly due to orbital parameters)." |
|---|
| 70 | elseif (this%max_years_reached) then; msg = " **** STOPPING because maximum number of Martian years to be simulated is reached." |
|---|
| 71 | elseif (this%time_limit_reached) then; msg = " **** STOPPING because the job time limit is going to be reached." |
|---|
| 72 | endif |
|---|
| 73 | |
|---|
| 74 | END FUNCTION stop_message |
|---|
| 75 | |
|---|
| 76 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
|---|
| 77 | !!! |
|---|
| 78 | !!! Purpose: Criterions to check if the PEM needs to call the PCM |
|---|
| 79 | !!! Author: RV & LL, 02/2023 |
|---|
| 80 | !!! |
|---|
| 81 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
|---|
| 82 | |
|---|
| 83 | SUBROUTINE stopping_crit_h2o_ice(cell_area,h2oice_ini_surf,is_h2oice_sublim_ini,h2o_ice,stopCrit,ngrid) |
|---|
| 84 | |
|---|
| 85 | use time_evol_mod, only: h2o_ice_crit |
|---|
| 86 | use comslope_mod, only: subslope_dist, nslope |
|---|
| 87 | |
|---|
| 88 | implicit none |
|---|
| 89 | |
|---|
| 90 | !======================================================================= |
|---|
| 91 | ! |
|---|
| 92 | ! Routine to check if the h2o ice criterion to stop the PEM is reached |
|---|
| 93 | ! |
|---|
| 94 | !======================================================================= |
|---|
| 95 | ! Inputs |
|---|
| 96 | !------- |
|---|
| 97 | integer, intent(in) :: ngrid ! # of physical grid points |
|---|
| 98 | real, dimension(ngrid), intent(in) :: cell_area ! Area of the cells |
|---|
| 99 | real, dimension(ngrid,nslope), intent(in) :: h2o_ice ! Actual density of h2o ice |
|---|
| 100 | real, intent(in) :: h2oice_ini_surf ! Initial surface of sublimating h2o ice |
|---|
| 101 | logical, dimension(ngrid,nslope), intent(in) :: is_h2oice_sublim_ini ! Grid points where h2o ice was initially sublimating |
|---|
| 102 | ! Outputs |
|---|
| 103 | !-------- |
|---|
| 104 | type(stopFlags), intent(inout) :: stopCrit ! Stopping criterion |
|---|
| 105 | ! Locals |
|---|
| 106 | ! ------ |
|---|
| 107 | integer :: i, islope ! Loop |
|---|
| 108 | real :: h2oice_now_surf ! Current surface of h2o ice |
|---|
| 109 | |
|---|
| 110 | !======================================================================= |
|---|
| 111 | if (stopCrit%stop_code() > 0) return |
|---|
| 112 | |
|---|
| 113 | ! Computation of the present surface of h2o ice still sublimating |
|---|
| 114 | h2oice_now_surf = 0. |
|---|
| 115 | do i = 1,ngrid |
|---|
| 116 | do islope = 1,nslope |
|---|
| 117 | if (is_h2oice_sublim_ini(i,islope) .and. h2o_ice(i,islope) > 0.) h2oice_now_surf = h2oice_now_surf + cell_area(i)*subslope_dist(i,islope) |
|---|
| 118 | enddo |
|---|
| 119 | enddo |
|---|
| 120 | |
|---|
| 121 | ! Check of the criterion |
|---|
| 122 | if (h2oice_now_surf < h2oice_ini_surf*(1. - h2o_ice_crit)) then |
|---|
| 123 | stopCrit%surface_h2oice_change = .true. |
|---|
| 124 | write(*,*) "Reason of stopping: the surface of h2o ice sublimating reaches the threshold" |
|---|
| 125 | write(*,*) "h2oice_now_surf < h2oice_ini_surf*(1. - h2o_ice_crit)", h2oice_now_surf < h2oice_ini_surf*(1. - h2o_ice_crit) |
|---|
| 126 | write(*,*) "Initial surface of h2o ice sublimating =", h2oice_ini_surf |
|---|
| 127 | write(*,*) "Current surface of h2o ice sublimating =", h2oice_now_surf |
|---|
| 128 | write(*,*) "Percentage of change accepted =", h2o_ice_crit*100 |
|---|
| 129 | else if (h2oice_now_surf > h2oice_ini_surf*(1. + h2o_ice_crit)) then |
|---|
| 130 | stopCrit%surface_h2oice_change = .true. |
|---|
| 131 | write(*,*) "Reason of stopping: the surface of h2o ice sublimating reaches the threshold" |
|---|
| 132 | write(*,*) "h2oice_now_surf > h2oice_ini_surf*(1. + h2o_ice_crit)", h2oice_now_surf > h2oice_ini_surf*(1. + h2o_ice_crit) |
|---|
| 133 | write(*,*) "Initial surface of h2o ice sublimating =", h2oice_ini_surf |
|---|
| 134 | write(*,*) "Current surface of h2o ice sublimating =", h2oice_now_surf |
|---|
| 135 | write(*,*) "Percentage of change accepted =", h2o_ice_crit*100 |
|---|
| 136 | endif |
|---|
| 137 | |
|---|
| 138 | END SUBROUTINE stopping_crit_h2o_ice |
|---|
| 139 | |
|---|
| 140 | !======================================================================= |
|---|
| 141 | |
|---|
| 142 | SUBROUTINE stopping_crit_co2(cell_area,co2ice_sublim_surf_ini,is_co2ice_sublim_ini,co2_ice,stopCrit,ngrid,ps_avg_global_ini,ps_avg_global,nslope) |
|---|
| 143 | |
|---|
| 144 | use time_evol_mod, only: co2_ice_crit, ps_criterion |
|---|
| 145 | use comslope_mod, only: subslope_dist |
|---|
| 146 | |
|---|
| 147 | implicit none |
|---|
| 148 | |
|---|
| 149 | !======================================================================= |
|---|
| 150 | ! |
|---|
| 151 | ! Routine to check if the co2 and pressure criteria to stop the PEM are reached |
|---|
| 152 | ! |
|---|
| 153 | !======================================================================= |
|---|
| 154 | ! Inputs |
|---|
| 155 | !------- |
|---|
| 156 | integer, intent(in) :: ngrid, nslope ! # of grid physical grid points |
|---|
| 157 | real, dimension(ngrid), intent(in) :: cell_area ! Area of the cells |
|---|
| 158 | real, dimension(ngrid,nslope), intent(in) :: co2_ice ! Actual density of co2 ice |
|---|
| 159 | real, intent(in) :: co2ice_sublim_surf_ini ! Initial surface of sublimatingco2 ice |
|---|
| 160 | logical, dimension(ngrid,nslope), intent(in) :: is_co2ice_sublim_ini ! Grid points where co2 ice was initially sublimating |
|---|
| 161 | real, intent(in) :: ps_avg_global_ini ! Planet average pressure from the PCM start files |
|---|
| 162 | real, intent(in) :: ps_avg_global ! Planet average pressure from the PEM computations |
|---|
| 163 | ! Outputs |
|---|
| 164 | !-------- |
|---|
| 165 | type(stopFlags), intent(inout) :: stopCrit ! Stopping criterion |
|---|
| 166 | |
|---|
| 167 | ! Locals |
|---|
| 168 | ! ------ |
|---|
| 169 | integer :: i, islope ! Loop |
|---|
| 170 | real :: co2ice_now_surf ! Current surface of co2 ice |
|---|
| 171 | |
|---|
| 172 | !======================================================================= |
|---|
| 173 | if (stopCrit%stop_code() > 0) return |
|---|
| 174 | |
|---|
| 175 | ! Computation of the present surface of co2 ice still sublimating |
|---|
| 176 | co2ice_now_surf = 0. |
|---|
| 177 | do i = 1,ngrid |
|---|
| 178 | do islope = 1,nslope |
|---|
| 179 | if (is_co2ice_sublim_ini(i,islope) .and. co2_ice(i,islope) > 0.) co2ice_now_surf = co2ice_now_surf + cell_area(i)*subslope_dist(i,islope) |
|---|
| 180 | enddo |
|---|
| 181 | enddo |
|---|
| 182 | |
|---|
| 183 | ! Check of the criterion |
|---|
| 184 | if (co2ice_now_surf < co2ice_sublim_surf_ini*(1. - co2_ice_crit)) then |
|---|
| 185 | stopCrit%surface_co2ice_change = .true. |
|---|
| 186 | write(*,*) "Reason of stopping: the surface of co2 ice sublimating reaches the threshold" |
|---|
| 187 | write(*,*) "co2ice_now_surf < co2ice_sublim_surf_ini*(1. - co2_ice_crit)", co2ice_now_surf < co2ice_sublim_surf_ini*(1. - co2_ice_crit) |
|---|
| 188 | write(*,*) "Initial surface of co2 ice sublimating =", co2ice_sublim_surf_ini |
|---|
| 189 | write(*,*) "Current surface of co2 ice sublimating =", co2ice_now_surf |
|---|
| 190 | write(*,*) "Percentage of change accepted =", co2_ice_crit*100. |
|---|
| 191 | else if (co2ice_now_surf > co2ice_sublim_surf_ini*(1. + co2_ice_crit)) then |
|---|
| 192 | stopCrit%surface_co2ice_change = .true. |
|---|
| 193 | write(*,*) "Reason of stopping: the surface of co2 ice sublimating reaches the threshold" |
|---|
| 194 | write(*,*) "co2ice_now_surf > co2ice_sublim_surf_ini*(1. + co2_ice_crit)", co2ice_now_surf > co2ice_sublim_surf_ini*(1. + co2_ice_crit) |
|---|
| 195 | write(*,*) "Current surface of co2 ice sublimating =", co2ice_now_surf |
|---|
| 196 | write(*,*) "Initial surface of co2 ice sublimating =", co2ice_sublim_surf_ini |
|---|
| 197 | write(*,*) "Percentage of change accepted =", co2_ice_crit*100. |
|---|
| 198 | endif |
|---|
| 199 | |
|---|
| 200 | if (ps_avg_global < ps_avg_global_ini*(1. - ps_criterion)) then |
|---|
| 201 | stopCrit%pressure_change = .true. |
|---|
| 202 | write(*,*) "Reason of stopping: the global pressure reaches the threshold" |
|---|
| 203 | write(*,*) "ps_avg_global < ps_avg_global_ini*(1. - ps_criterion)", ps_avg_global < ps_avg_global_ini*(1. - ps_criterion) |
|---|
| 204 | write(*,*) "Initial global pressure =", ps_avg_global_ini |
|---|
| 205 | write(*,*) "Current global pressure =", ps_avg_global |
|---|
| 206 | write(*,*) "Percentage of change accepted =", ps_criterion*100. |
|---|
| 207 | else if (ps_avg_global > ps_avg_global_ini*(1. + ps_criterion)) then |
|---|
| 208 | stopCrit%pressure_change = .true. |
|---|
| 209 | write(*,*) "Reason of stopping: the global pressure reaches the threshold" |
|---|
| 210 | write(*,*) "ps_avg_global > ps_avg_global_ini*(1. + ps_criterion)", ps_avg_global > ps_avg_global_ini*(1. + ps_criterion) |
|---|
| 211 | write(*,*) "Initial global pressure =", ps_avg_global_ini |
|---|
| 212 | write(*,*) "Current global pressure =", ps_avg_global |
|---|
| 213 | write(*,*) "Percentage of change accepted =", ps_criterion*100. |
|---|
| 214 | endif |
|---|
| 215 | |
|---|
| 216 | END SUBROUTINE stopping_crit_co2 |
|---|
| 217 | |
|---|
| 218 | !======================================================================= |
|---|
| 219 | |
|---|
| 220 | SUBROUTINE stopping_crit_h2o(ngrid,nslope,cell_area,delta_h2o_adsorbed,delta_h2o_icetablesublim,h2o_ice,d_h2oice,S_atm_2_h2o,S_h2o_2_atm,S_atm_2_h2oice,S_h2oice_2_atm,stopCrit) |
|---|
| 221 | |
|---|
| 222 | use time_evol_mod, only: dt |
|---|
| 223 | use comslope_mod, only: subslope_dist, def_slope_mean |
|---|
| 224 | use comcstfi_h, only: pi |
|---|
| 225 | |
|---|
| 226 | implicit none |
|---|
| 227 | |
|---|
| 228 | !======================================================================= |
|---|
| 229 | ! |
|---|
| 230 | ! Routine to check if the h2o is only exchanged between grid points |
|---|
| 231 | ! |
|---|
| 232 | !======================================================================= |
|---|
| 233 | ! Inputs |
|---|
| 234 | !------- |
|---|
| 235 | integer, intent(in) :: ngrid, nslope ! # of grid points, # of subslopes |
|---|
| 236 | real, dimension(ngrid), intent(in) :: cell_area ! Area of each mesh grid (m^2) |
|---|
| 237 | real, dimension(ngrid), intent(in) :: delta_h2o_adsorbed ! Mass of H2O adsorbed/desorbded in the soil (kg/m^2) |
|---|
| 238 | real, dimension(ngrid), intent(in) :: delta_h2o_icetablesublim ! Mass of H2O that condensed/sublimated at the ice table |
|---|
| 239 | real, dimension(ngrid,nslope), intent(in) :: h2o_ice ! H2O ice (kg/m^2) |
|---|
| 240 | real, dimension(ngrid,nslope), intent(in) :: d_h2oice ! Tendency of H2O ice (kg/m^2/year) |
|---|
| 241 | ! Outputs |
|---|
| 242 | !-------- |
|---|
| 243 | type(stopFlags), intent(inout) :: stopCrit ! Stopping criterion |
|---|
| 244 | real, intent(out) :: S_atm_2_h2o, S_h2o_2_atm, S_atm_2_h2oice, S_h2oice_2_atm ! Variables to conserve H2O |
|---|
| 245 | ! Locals |
|---|
| 246 | ! ------ |
|---|
| 247 | integer :: i, islope ! Loop |
|---|
| 248 | !======================================================================= |
|---|
| 249 | ! Checks to escape the subroutine (adjusment not relevant in 1D or if the PEM should stop already) |
|---|
| 250 | if (ngrid == 1 .or. stopCrit%stop_code() > 0) then |
|---|
| 251 | S_atm_2_h2o = 1. |
|---|
| 252 | S_h2o_2_atm = S_atm_2_h2o |
|---|
| 253 | S_atm_2_h2oice = 1. |
|---|
| 254 | S_h2oice_2_atm = S_atm_2_h2oice |
|---|
| 255 | return |
|---|
| 256 | endif |
|---|
| 257 | |
|---|
| 258 | ! We compute the amount of water going out from the atmosphere (S_atm_2_h2o) and going into the atmophere (S_h2o_2_atm) |
|---|
| 259 | S_atm_2_h2o = 0. |
|---|
| 260 | S_h2o_2_atm = 0. |
|---|
| 261 | S_atm_2_h2oice = 0. |
|---|
| 262 | S_h2oice_2_atm = 0. |
|---|
| 263 | do i = 1,ngrid |
|---|
| 264 | if (delta_h2o_adsorbed(i) > 0.) then |
|---|
| 265 | S_atm_2_h2o = S_atm_2_h2o + delta_h2o_adsorbed(i)*cell_area(i) |
|---|
| 266 | else |
|---|
| 267 | S_h2o_2_atm = S_h2o_2_atm + delta_h2o_adsorbed(i)*cell_area(i) |
|---|
| 268 | endif |
|---|
| 269 | if (delta_h2o_icetablesublim(i) > 0.) then |
|---|
| 270 | S_atm_2_h2o = S_atm_2_h2o + delta_h2o_icetablesublim(i)*cell_area(i) |
|---|
| 271 | else |
|---|
| 272 | S_h2o_2_atm = S_h2o_2_atm + delta_h2o_icetablesublim(i)*cell_area(i) |
|---|
| 273 | endif |
|---|
| 274 | do islope = 1,nslope |
|---|
| 275 | if (d_h2oice(i,islope) > 0.) then |
|---|
| 276 | S_atm_2_h2o = S_atm_2_h2o + d_h2oice(i,islope)*dt*cell_area(i)*subslope_dist(i,islope)/cos(def_slope_mean(islope)*pi/180.) |
|---|
| 277 | S_atm_2_h2oice = S_atm_2_h2oice + d_h2oice(i,islope)*dt*cell_area(i)*subslope_dist(i,islope)/cos(def_slope_mean(islope)*pi/180.) |
|---|
| 278 | else if (d_h2oice(i,islope) < 0. .and. h2o_ice(i,islope) > 0.) then |
|---|
| 279 | S_h2o_2_atm = S_h2o_2_atm - d_h2oice(i,islope)*dt*cell_area(i)*subslope_dist(i,islope)/cos(def_slope_mean(islope)*pi/180.) |
|---|
| 280 | S_h2oice_2_atm = S_h2oice_2_atm - d_h2oice(i,islope)*dt*cell_area(i)*subslope_dist(i,islope)/cos(def_slope_mean(islope)*pi/180.) |
|---|
| 281 | endif |
|---|
| 282 | enddo |
|---|
| 283 | enddo |
|---|
| 284 | |
|---|
| 285 | ! Since relative atmospheric water is kept constant, we need to equate condensing reservoirs to the sublimating ones. |
|---|
| 286 | ! It is not possible if one of them is missing. |
|---|
| 287 | if ((abs(S_atm_2_h2oice) < 1.e-10 .or. abs(S_h2oice_2_atm) < 1.e-10)) then |
|---|
| 288 | write(*,*) "Reason of stopping: there is no sublimating or condensing h2o ice!" |
|---|
| 289 | write(*,*) "This can be due to the absence of h2o ice in the PCM run." |
|---|
| 290 | write(*,*) "Amount of condensing ice =", S_atm_2_h2oice |
|---|
| 291 | write(*,*) "Amount of sublimating ice =", S_h2oice_2_atm |
|---|
| 292 | stopCrit%zero_dh2oice = .true. |
|---|
| 293 | endif |
|---|
| 294 | |
|---|
| 295 | END SUBROUTINE stopping_crit_h2o |
|---|
| 296 | |
|---|
| 297 | END MODULE |
|---|