LCOV - code coverage report
Current view: top level - star/private - timestep.f90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 62.7 % 1215 762
Test Date: 2026-12-03 14:59:23 Functions: 98.3 % 59 58

            Line data    Source code
       1              : ! ***********************************************************************
       2              : !
       3              : !   Copyright (C) 2010-2019  The MESA Team
       4              : !
       5              : !   This program is free software: you can redistribute it and/or modify
       6              : !   it under the terms of the GNU Lesser General Public License
       7              : !   as published by the Free Software Foundation,
       8              : !   either version 3 of the License, or (at your option) any later version.
       9              : !
      10              : !   This program is distributed in the hope that it will be useful,
      11              : !   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12              : !   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      13              : !   See the GNU Lesser General Public License for more details.
      14              : !
      15              : !   You should have received a copy of the GNU Lesser General Public License
      16              : !   along with this program. If not, see <https://www.gnu.org/licenses/>.
      17              : !
      18              : ! ***********************************************************************
      19              : 
      20              :       module timestep
      21              : 
      22              :       use star_private_def
      23              :       use utils_lib, only: is_bad
      24              :       use const_def, only: dp, ln10, secyer, msun, lsun, convective_mixing
      25              :       use chem_def
      26              : 
      27              :       implicit none
      28              : 
      29              :       private
      30              :       public :: timestep_controller
      31              :       public :: check_change
      32              :       public :: check_integer_limit
      33              : 
      34              :       logical, parameter :: dbg_timestep = .false.
      35              :       real(dp) :: max_dt
      36              : 
      37              :       contains
      38              : 
      39           11 :       integer function timestep_controller(s, max_timestep)
      40              :          ! if don't return keep_going, then set result_reason to say why.
      41              :          type (star_info), pointer :: s
      42              :          real(dp), intent(in) :: max_timestep
      43              : 
      44              :          include 'formats'
      45              : 
      46           11 :          timestep_controller = do_timestep_limits(s, s% dt)
      47           11 :          if (timestep_controller /= keep_going) s% result_reason = timestep_limits
      48              : 
      49           11 :          if (s% force_timestep > 0) then
      50            0 :             s% dt_next = s% force_timestep
      51            0 :             s% why_Tlim = Tlim_force_timestep
      52            0 :             return
      53              :          end if
      54              : 
      55              :          ! strictly enforce maximum timestep
      56           11 :          max_dt = max_timestep
      57           11 :          if (max_dt <= 0) max_dt = 1d99
      58           11 :          if (s% dt_next > max_dt) then
      59            0 :             s% dt_next = max_dt
      60            0 :             s% why_Tlim = Tlim_max_timestep
      61              :          end if
      62              : 
      63           11 :          if (s% timestep_hold > s% model_number .and. s% dt_next > s% dt) then
      64            0 :             s% dt_next = s% dt
      65            0 :             s% why_Tlim = Tlim_timestep_hold
      66            0 :             if (s% report_dt_hard_limit_retries .and. timestep_controller == keep_going) &
      67            0 :                write(*,3) 'timestep_hold > model_number, so no timestep increase', &
      68            0 :                   s% timestep_hold, s% model_number
      69              :          end if
      70              : 
      71           11 :          if (is_bad_num(s% dt_next)) then
      72            0 :             write(*, *) 'timestep_controller: dt_next', s% dt_next
      73            0 :             if (s% stop_for_bad_nums) call mesa_error(__FILE__,__LINE__,'timestep_controller')
      74            0 :             timestep_controller = terminate
      75            0 :             s% termination_code = t_timestep_controller
      76            0 :             return
      77              :          end if
      78              : 
      79              :          if (dbg_timestep) then
      80              :             write(*,*) 'final result from timestep_controller for model_number', &
      81              :                timestep_controller, s% model_number
      82              :             write(*,1) 'lg dt/secyer', log10(s% dt/secyer)
      83              :             write(*,1) 'lg dt_next/secyer', log10(s% dt_next/secyer)
      84              :             write(*,1) 'dt_next/dt', s% dt_next/s% dt
      85              :             write(*,'(A)')
      86              :             write(*,'(A)')
      87              :             call mesa_error(__FILE__,__LINE__,'timestep_controller')
      88              :          end if
      89              : 
      90              :       end function timestep_controller
      91              : 
      92              : 
      93           11 :       integer function do_timestep_limits(s, dt)
      94              :          use rates_def, only: i_rate
      95              :          type (star_info), pointer :: s
      96              :          real(dp), intent(in) :: dt  ! timestep just completed
      97              : 
      98              :          real(dp) :: dt_limit_ratio(numTlim), order, max_timestep_factor
      99              :          integer :: i_limit, nz, ierr
     100              :          logical :: skip_hard_limit
     101              :          integer :: num_mix_boundaries  ! boundaries of regions with mixing_type /= no_mixing
     102           11 :          real(dp), pointer :: mix_bdy_q(:)  ! (num_mix_boundaries)
     103              :          integer, pointer :: mix_bdy_loc(:)  ! (num_mix_boundaries)
     104              : 
     105              :          include 'formats'
     106              : 
     107           11 :          if (s% never_skip_hard_limits) then
     108           11 :             skip_hard_limit = .false.
     109              :          else
     110              :             skip_hard_limit = (s% timestep_hold >= s% model_number) .or. &
     111              :                (s% relax_hard_limits_after_retry .and. &
     112            0 :                 s% model_number_for_last_retry == s% model_number)
     113              :          end if
     114              : 
     115           11 :          nz = s% nz
     116              : 
     117              :          ! NOTE: when we get here, complete_model has called the report routine,
     118              :          ! so we can use information that it has calculated
     119              : 
     120           11 :          ierr = 0
     121              : 
     122           11 :          num_mix_boundaries = s% num_mix_boundaries
     123           11 :          mix_bdy_q => s% mix_bdy_q
     124           11 :          mix_bdy_loc => s% mix_bdy_loc
     125              : 
     126           11 :          dt_limit_ratio(:) = 0d0
     127              : 
     128              :          do_timestep_limits = check_varcontrol_limit( &
     129           11 :             s, dt_limit_ratio(Tlim_struc))
     130           11 :          if (return_now(Tlim_struc)) return
     131              : 
     132           11 :          if (.not. s% doing_first_model_of_run) then
     133              : 
     134           10 :             if (s% use_other_timestep_limit) then
     135              :                do_timestep_limits = s% other_timestep_limit( &
     136            0 :                   s% id, skip_hard_limit, dt, dt_limit_ratio(Tlim_other_timestep_limit))
     137            0 :                if (return_now(Tlim_other_timestep_limit)) return
     138              :             end if
     139              : 
     140              :             do_timestep_limits = check_solver_iters_timestep_limit( &
     141           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_solver))
     142           10 :             if (return_now(Tlim_solver)) return
     143              : 
     144              :             do_timestep_limits = check_burn_steps_limit( &
     145           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_num_burn_steps))
     146           10 :             if (return_now(Tlim_num_burn_steps)) return
     147              : 
     148              :             do_timestep_limits = check_diffusion_steps_limit( &
     149           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_num_diff_solver_steps))
     150           10 :             if (return_now(Tlim_num_diff_solver_steps)) return
     151              : 
     152              :             do_timestep_limits = check_diffusion_iters_limit( &
     153           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_num_diff_solver_iters))
     154           10 :             if (return_now(Tlim_num_diff_solver_iters)) return
     155              : 
     156              :             do_timestep_limits = check_dX(s, skip_hard_limit, dt, &
     157              :                num_mix_boundaries, mix_bdy_loc, mix_bdy_q, &
     158           10 :                dt_limit_ratio(Tlim_dX), dt_limit_ratio(Tlim_dX_div_X))
     159           10 :             if (return_now(Tlim_dX_div_X)) return
     160              : 
     161              :             do_timestep_limits = check_dL_div_L( &
     162           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dL_div_L))
     163           10 :             if (return_now(Tlim_dL_div_L)) return
     164              : 
     165              :             do_timestep_limits = check_dlgP_change( &
     166           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_dlgP))
     167           10 :             if (return_now(Tlim_dlgP)) return
     168              : 
     169              :             do_timestep_limits = check_dlgRho_change( &
     170           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_dlgRho))
     171           10 :             if (return_now(Tlim_dlgRho)) return
     172              : 
     173              :             do_timestep_limits = check_dlgT_change( &
     174           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_dlgT))
     175           10 :             if (return_now(Tlim_dlgT)) return
     176              : 
     177              :             do_timestep_limits = check_dlgE_change( &
     178           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_dlgE))
     179           10 :             if (return_now(Tlim_dlgE)) return
     180              : 
     181              :             do_timestep_limits = check_dlgR_change( &
     182           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_dlgR))
     183           10 :             if (return_now(Tlim_dlgR)) return
     184              : 
     185              :             do_timestep_limits = check_lgL_nuc_cat_change( &
     186              :                s, num_mix_boundaries, mix_bdy_q, &
     187           10 :                skip_hard_limit, dt_limit_ratio(Tlim_dlgL_nuc_cat))
     188           10 :             if (return_now(Tlim_dlgL_nuc_cat)) return
     189              : 
     190              :             do_timestep_limits = check_lgL_H_change( &
     191           20 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgL_H))
     192           10 :             if (return_now(Tlim_dlgL_H)) return
     193              : 
     194              :             do_timestep_limits = check_lgL_He_change( &
     195           20 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgL_He))
     196           10 :             if (return_now(Tlim_dlgL_He)) return
     197              : 
     198              :             do_timestep_limits = check_lgL_z_change( &
     199           20 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgL_z))
     200           10 :             if (return_now(Tlim_dlgL_z)) return
     201              : 
     202              :             do_timestep_limits = check_lgL_power_photo_change( &
     203           20 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgL_power_photo))
     204           10 :             if (return_now(Tlim_dlgL_power_photo)) return
     205              : 
     206              :             do_timestep_limits = check_lgL_nuc_change( &
     207           20 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgL_nuc))
     208           10 :             if (return_now(Tlim_dlgL_nuc)) return
     209              : 
     210              :             do_timestep_limits = check_dlgTeff_change( &
     211           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgTeff))
     212           10 :             if (return_now(Tlim_dlgTeff)) return
     213              : 
     214              :             do_timestep_limits = check_dlgRho_cntr_change( &
     215           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgRho_cntr))
     216           10 :             if (return_now(Tlim_dlgRho_cntr)) return
     217              : 
     218              :             do_timestep_limits = check_dlgT_cntr_change( &
     219           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgT_cntr))
     220           10 :             if (return_now(Tlim_dlgT_cntr)) return
     221              : 
     222              :             do_timestep_limits = check_dlgT_max_change( &
     223           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgT_max))
     224           10 :             if (return_now(Tlim_dlgT_max)) return
     225              : 
     226              :             do_timestep_limits = check_dlgT_max_at_high_T_change( &
     227           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgT_max_at_high_T))
     228           10 :             if (return_now(Tlim_dlgT_max_at_high_T)) return
     229              : 
     230              :             do_timestep_limits = check_dlgP_cntr_change( &
     231           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlgP_cntr))
     232           10 :             if (return_now(Tlim_dlgP_cntr)) return
     233              : 
     234              :             do_timestep_limits = check_dYe_highT_change( &
     235           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_delta_Ye_highT))
     236           10 :             if (return_now(Tlim_delta_Ye_highT)) return
     237              : 
     238              :             do_timestep_limits = check_dlog_eps_nuc_change( &
     239           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dlog_eps_nuc))
     240           10 :             if (return_now(Tlim_dlog_eps_nuc)) return
     241              : 
     242              :             do_timestep_limits = check_dX_div_X_cntr( &
     243           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_dX_div_X_cntr))
     244           10 :             if (return_now(Tlim_dX_div_X_cntr)) return
     245              : 
     246              :             do_timestep_limits = check_lg_XH_cntr( &
     247           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_lg_XH_cntr))
     248           10 :             if (return_now(Tlim_lg_XH_cntr)) return
     249              : 
     250              :             do_timestep_limits = check_lg_XHe_cntr( &
     251           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_lg_XHe_cntr))
     252           10 :             if (return_now(Tlim_lg_XHe_cntr)) return
     253              : 
     254              :             do_timestep_limits = check_lg_XC_cntr( &
     255           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_lg_XC_cntr))
     256           10 :             if (return_now(Tlim_lg_XC_cntr)) return
     257              : 
     258              :             do_timestep_limits = check_lg_XNe_cntr( &
     259           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_lg_XNe_cntr))
     260           10 :             if (return_now(Tlim_lg_XNe_cntr)) return
     261              : 
     262              :             do_timestep_limits = check_lg_XO_cntr( &
     263           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_lg_XO_cntr))
     264           10 :             if (return_now(Tlim_lg_XO_cntr)) return
     265              : 
     266              :             do_timestep_limits = check_lg_XSi_cntr( &
     267           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_lg_XSi_cntr))
     268           10 :             if (return_now(Tlim_lg_XSi_cntr)) return
     269              : 
     270              :             do_timestep_limits = check_XH_cntr( &
     271           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_XH_cntr))
     272           10 :             if (return_now(Tlim_XH_cntr)) return
     273              : 
     274              :             do_timestep_limits = check_XHe_cntr( &
     275           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_XHe_cntr))
     276           10 :             if (return_now(Tlim_XHe_cntr)) return
     277              : 
     278              :             do_timestep_limits = check_XC_cntr( &
     279           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_XC_cntr))
     280           10 :             if (return_now(Tlim_XC_cntr)) return
     281              : 
     282              :             do_timestep_limits = check_XNe_cntr( &
     283           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_XNe_cntr))
     284           10 :             if (return_now(Tlim_XNe_cntr)) return
     285              : 
     286              :             do_timestep_limits = check_XO_cntr( &
     287           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_XO_cntr))
     288           10 :             if (return_now(Tlim_XO_cntr)) return
     289              : 
     290              :             do_timestep_limits = check_XSi_cntr( &
     291           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_XSi_cntr))
     292           10 :             if (return_now(Tlim_XSi_cntr)) return
     293              : 
     294              :             do_timestep_limits = check_delta_mstar( &
     295           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dmstar))
     296           10 :             if (return_now(Tlim_dmstar)) return
     297              : 
     298              :             do_timestep_limits = check_delta_mdot( &
     299           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_del_mdot))
     300           10 :             if (return_now(Tlim_del_mdot)) return
     301              : 
     302              :             do_timestep_limits = check_adjust_J_q( &
     303           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_adjust_J_q))
     304           10 :             if (return_now(Tlim_adjust_J_q)) return
     305              : 
     306              :             do_timestep_limits = check_delta_lgL( &
     307           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_lgL))
     308           10 :             if (return_now(Tlim_lgL)) return
     309              : 
     310              :             do_timestep_limits = check_dt_div_dt_cell_collapse( &
     311           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dt_div_dt_cell_collapse))
     312           10 :             if (return_now(Tlim_dt_div_dt_cell_collapse)) return
     313              : 
     314              :             do_timestep_limits = check_dt_div_min_dr_div_cs( &
     315           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dt_div_min_dr_div_cs))
     316           10 :             if (return_now(Tlim_dt_div_min_dr_div_cs)) return
     317              : 
     318              :             do_timestep_limits = check_delta_HR( &
     319           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_delta_HR))
     320           10 :             if (return_now(Tlim_delta_HR)) return
     321              : 
     322              :             do_timestep_limits = check_rel_error_in_energy( &
     323           10 :                s, skip_hard_limit, dt_limit_ratio(Tlim_error_in_energy_conservation))
     324           10 :             if (return_now(Tlim_error_in_energy_conservation)) return
     325              : 
     326              :             do_timestep_limits = check_dX_nuc_drop( &
     327           10 :                s, skip_hard_limit, dt, dt_limit_ratio(Tlim_dX_nuc_drop))
     328           10 :             if (return_now(Tlim_dX_nuc_drop)) return
     329              : 
     330              :          end if
     331              : 
     332          649 :          i_limit = maxloc(dt_limit_ratio(1:numTlim), dim=1)
     333              : 
     334           11 :          order = 1
     335           11 :          call filter_dt_next(s, order, dt_limit_ratio(i_limit))  ! sets s% dt_next
     336              : 
     337           11 :          if (s% log_max_temperature > s% min_logT_for_max_timestep_factor_at_high_T) then
     338            0 :             max_timestep_factor = s% max_timestep_factor_at_high_T
     339              :          else
     340           11 :             max_timestep_factor = s% max_timestep_factor
     341              :          end if
     342              : 
     343           11 :          if (max_timestep_factor > 0 .and. s% dt_next > max_timestep_factor*s% dt) then
     344           11 :             s% dt_next = max_timestep_factor*s% dt
     345           11 :             if (s% report_solver_dt_info) then
     346            0 :                write(*,2) 's% dt', s% model_number, s% dt
     347            0 :                write(*,2) 'max_timestep_factor', s% model_number, max_timestep_factor
     348            0 :                write(*,2) 's% dt_next', s% model_number, s% dt_next
     349              :             end if
     350           11 :             if (s% dt_next == 0d0) call mesa_error(__FILE__,__LINE__,'filter_dt_next')
     351           11 :             if (i_limit == Tlim_struc) i_limit = Tlim_max_timestep_factor
     352              :          end if
     353              : 
     354           11 :          if (s% min_timestep_factor > 0 .and. s% dt_next < s% min_timestep_factor*s% dt) then
     355            0 :             s% dt_next = s% min_timestep_factor*s% dt
     356            0 :             if (s% report_solver_dt_info) then
     357            0 :                write(*,2) 's% dt', s% model_number, s% dt
     358            0 :                write(*,2) 'min_timestep_factor', s% model_number, s% min_timestep_factor
     359            0 :                write(*,2) 's% dt_next', s% model_number, s% dt_next
     360              :             end if
     361            0 :             if (s% dt_next == 0d0) call mesa_error(__FILE__,__LINE__,'filter_dt_next')
     362            0 :             if (i_limit == Tlim_struc) i_limit = Tlim_min_timestep_factor
     363              :          end if
     364              : 
     365           11 :          s% why_Tlim = i_limit
     366           11 :          if (i_limit > 0) s% dt_why_count(i_limit) = s% dt_why_count(i_limit) + 1
     367              : 
     368              :          contains
     369              : 
     370          481 :          logical function return_now(i_limit)
     371              :             integer, intent(in) :: i_limit
     372          481 :             if (do_timestep_limits == keep_going) then
     373          481 :                return_now = .false.
     374              :                return
     375              :             end if
     376            0 :             return_now = .true.
     377            0 :             s% why_Tlim = i_limit
     378            0 :             if (i_limit > 0) s% dt_why_retry_count(i_limit) = s% dt_why_retry_count(i_limit) + 1
     379              :          end function return_now
     380              : 
     381              :       end function do_timestep_limits
     382              : 
     383              : 
     384           10 :       integer function check_integer_limit( &
     385              :             s, limit, hard_limit, value, msg, skip_hard_limit, dt, dt_limit_ratio)
     386              :          type (star_info), pointer :: s
     387              :          integer, intent(in) :: limit, hard_limit, value
     388              :          character (len=*), intent(in) :: msg
     389              :          logical, intent(in) :: skip_hard_limit
     390              :          real(dp), intent(in) :: dt
     391              :          real(dp), intent(inout) :: dt_limit_ratio
     392              :          include 'formats'
     393           10 :          if (value > hard_limit .and. hard_limit > 0 .and. (.not. skip_hard_limit)) then
     394            0 :             check_integer_limit = retry
     395            0 :             s% retry_message = trim(msg) // ' hard limit'
     396            0 :             if (s% report_dt_hard_limit_retries) then
     397            0 :                write(*,*) trim(msg) // ' hard limit', hard_limit, value
     398            0 :                write(*,3) trim(msg), s% model_number, value
     399            0 :                write(*,3) trim(msg) // ' hard limit', s% model_number, hard_limit
     400              :             end if
     401            0 :             return
     402              :          end if
     403           10 :          check_integer_limit = keep_going
     404           10 :          if (value <= 0 .or. limit <= 0) return
     405           10 :          dt_limit_ratio = dble(value)/dble(limit) - 0.05d0
     406              :             ! subtract a bit so that allow dt to grow even if value == limit
     407           10 :          if (dt_limit_ratio <= 1d0) dt_limit_ratio = 0
     408              :       end function check_integer_limit
     409              : 
     410              : 
     411           10 :       integer function check_burn_steps_limit(s, skip_hard_limit, dt, dt_limit_ratio)
     412              :          type (star_info), pointer :: s
     413              :          logical, intent(in) :: skip_hard_limit
     414              :          real(dp), intent(in) :: dt
     415              :          real(dp), intent(inout) :: dt_limit_ratio
     416              :          integer :: max_steps
     417           10 :          check_burn_steps_limit = keep_going
     418        10633 :          if (.not. s% op_split_burn .or. maxval(s% T_start(1:s%nz)) < s% op_split_burn_min_T) return
     419              : 
     420            0 :          max_steps = maxval(s% burn_num_iters(1:s% nz),mask=s% T(1:s%nz)>s% op_split_burn_min_T)
     421              :          check_burn_steps_limit = check_integer_limit( &
     422              :            s, s% burn_steps_limit, s% burn_steps_hard_limit, max_steps,  &
     423            0 :            'num_burn_solver_steps', skip_hard_limit, dt, dt_limit_ratio)
     424            0 :       end function check_burn_steps_limit
     425              : 
     426              : 
     427           10 :       integer function check_solver_iters_timestep_limit(s, skip_hard_limit, dt, dt_limit_ratio)
     428              :          type (star_info), pointer :: s
     429              :          logical, intent(in) :: skip_hard_limit
     430              :          real(dp), intent(in) :: dt
     431              :          real(dp), intent(inout) :: dt_limit_ratio
     432              :          integer :: iters, limit, hard_limit
     433              :          include 'formats'
     434           10 :          check_solver_iters_timestep_limit = keep_going
     435           10 :          iters = s% num_solver_iterations
     436           10 :          limit = s% solver_iters_timestep_limit
     437           10 :          hard_limit = -1
     438           10 :          if (s% using_gold_tolerances) then
     439           10 :             limit = s% gold_solver_iters_timestep_limit
     440           10 :             if ((s% use_gold2_tolerances .and. s% steps_before_use_gold2_tolerances < 0) .or. &
     441              :                 (s% steps_before_use_gold2_tolerances >= 0 .and. &
     442              :                    s% model_number > s% steps_before_use_gold2_tolerances + max(0,s% init_model_number))) then
     443            0 :                limit = s% gold2_solver_iters_timestep_limit
     444              :             end if
     445              :          end if
     446           10 :          if (s% used_extra_iter_in_solver_for_accretion) iters = iters - 1
     447              :          check_solver_iters_timestep_limit = check_integer_limit( &
     448              :            s, limit, hard_limit, &
     449           10 :            iters, 'num_solver_iterations', skip_hard_limit, dt, dt_limit_ratio)
     450           10 :       end function check_solver_iters_timestep_limit
     451              : 
     452              : 
     453           10 :       integer function check_diffusion_steps_limit(s, skip_hard_limit, dt, dt_limit_ratio)
     454              :          type (star_info), pointer :: s
     455              :          logical, intent(in) :: skip_hard_limit
     456              :          real(dp), intent(in) :: dt
     457              :          real(dp), intent(inout) :: dt_limit_ratio
     458           10 :          check_diffusion_steps_limit = keep_going
     459           10 :          if (.not. s% do_element_diffusion) return
     460              :          check_diffusion_steps_limit = check_integer_limit( &
     461              :            s, s% diffusion_steps_limit, s% diffusion_steps_hard_limit, &
     462              :            s% num_diffusion_solver_steps,  &
     463            0 :            'num_diffusion_solver_steps', skip_hard_limit, dt, dt_limit_ratio)
     464            0 :       end function check_diffusion_steps_limit
     465              : 
     466              : 
     467           10 :       integer function check_diffusion_iters_limit(s, skip_hard_limit, dt, dt_limit_ratio)
     468              :          type (star_info), pointer :: s
     469              :          logical, intent(in) :: skip_hard_limit
     470              :          real(dp), intent(in) :: dt
     471              :          real(dp), intent(inout) :: dt_limit_ratio
     472           10 :          check_diffusion_iters_limit = keep_going
     473           10 :          if (.not. s% do_element_diffusion) return
     474              :          check_diffusion_iters_limit = check_integer_limit( &
     475              :            s, s% diffusion_iters_limit, s% diffusion_iters_hard_limit, &
     476              :            s% num_diffusion_solver_iters,  &
     477            0 :            'num_diffusion_solver_iters', skip_hard_limit, dt, dt_limit_ratio)
     478            0 :       end function check_diffusion_iters_limit
     479              : 
     480              : 
     481           10 :       integer function check_dX(s, skip_hard_limit, dt, &
     482              :             n_mix_bdy, mix_bdy_loc, mix_bdy_q, &
     483              :             dX_dt_limit_ratio, dX_div_X_dt_limit_ratio)
     484              :          use num_lib, only: binary_search
     485              :          logical, intent(in) :: skip_hard_limit
     486              :          integer, intent(in) :: n_mix_bdy, mix_bdy_loc(:)
     487              :          real(dp), intent(in) :: dt
     488              :          real(dp), intent(in), pointer :: mix_bdy_q(:)
     489              :          real(dp), intent(inout) :: dX_dt_limit_ratio, dX_div_X_dt_limit_ratio
     490              : 
     491              :          type (star_info), pointer :: s
     492              :          real(dp) :: X, X_old, delta_X, delta_X_div_X, max_dX, max_dX_div_X, &
     493              :             bdy_dist_dm, max_dX_bdy_dist_dm, max_dX_div_X_bdy_dist_dm, cz_dist_limit, &
     494              :             D_mix_cutoff, ratio_tmp_dX, ratio_tmp_dX_div_X
     495              :          integer :: i, j, k, cid, bdy, max_dX_j, max_dX_k, max_dX_div_X_j, max_dX_div_X_k
     496              :          real(dp), dimension(max_dX_limit_ctrls) :: dX_limit, dX_hard_limit, &
     497              :             dX_div_X_limit, dX_div_X_hard_limit
     498              :          character (len=strlen) :: sp
     499              : 
     500              :          include 'formats'
     501              : 
     502           10 :          check_dX = keep_going
     503           10 :          dX_dt_limit_ratio = 0d0
     504           10 :          dX_div_X_dt_limit_ratio = 0d0
     505              : 
     506           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
     507              : 
     508         1010 :          do i=1, max_dx_limit_ctrls  ! go over all potential species + XYZ
     509         1000 :             if (s% dX_limit_min_X(i) > 1d0 .and. &
     510              :                 s% dX_div_X_limit_min_X(i) > 1d0) then
     511              :                cycle  ! go to next
     512              :             end if
     513              : 
     514         2020 :             dX_limit = s% dX_limit(i) * s% time_delta_coeff
     515         2020 :             dX_hard_limit = s% dX_hard_limit(i) * s% time_delta_coeff
     516              : 
     517           20 :             if (s% log_max_temperature > s% dX_div_X_at_high_T_limit_lgT_min(i)) then
     518            0 :                dX_div_X_limit = s% dX_div_X_at_high_T_limit(i)
     519            0 :                dX_div_X_hard_limit = s% dX_div_X_at_high_T_hard_limit(i)
     520              :             else
     521         2020 :                dX_div_X_limit = s% dX_div_X_limit(i)
     522         2020 :                dX_div_X_hard_limit = s% dX_div_X_hard_limit(i)
     523              :             end if
     524              : 
     525         2020 :             dX_div_X_limit = dX_div_X_limit * s% time_delta_coeff
     526         2020 :             dX_div_X_hard_limit = dX_div_X_hard_limit * s% time_delta_coeff
     527              : 
     528           20 :             max_dX = -1; max_dX_j = -1; max_dX_k = -1
     529           20 :             max_dX_div_X = -1; max_dX_div_X_j = -1; max_dX_div_X_k = -1
     530           20 :             bdy = 0
     531           20 :             max_dX_bdy_dist_dm = 0
     532           20 :             max_dX_div_X_bdy_dist_dm = 0
     533           20 :             cz_dist_limit = s% dX_mix_dist_limit*Msun
     534              : 
     535           20 :             if (s% set_min_D_mix .and. s% ye(s% nz) >= s% min_center_Ye_for_min_D_mix) then
     536            0 :                D_mix_cutoff = s% min_D_mix
     537              :             else
     538              :                D_mix_cutoff = 0
     539              :             end if
     540              : 
     541           20 :             sp = s% dX_limit_species(i)
     542              : 
     543        21266 :             do k = 1, s% nz
     544              : 
     545        21246 :                if (s% D_mix(k) > D_mix_cutoff) then
     546              :                   cycle
     547              :                end if
     548        18378 :                if (k < s% nz) then
     549        18378 :                   if (s% D_mix(k+1) > D_mix_cutoff) then
     550              :                      cycle
     551              :                   end if
     552              :                end if
     553              : 
     554              :                ! find the nearest mixing boundary
     555        18338 :                bdy = binary_search(n_mix_bdy, mix_bdy_q, bdy, s% q(k))
     556              :                ! don't check cells near a mixing boundary
     557        18338 :                if (bdy > 0 .and. bdy < n_mix_bdy) then
     558        16398 :                   bdy_dist_dm = s% xmstar*abs(s% q(k) - mix_bdy_q(bdy))
     559        16398 :                   if (bdy_dist_dm < cz_dist_limit) cycle
     560              :                else
     561              :                   bdy_dist_dm = 0
     562              :                end if
     563              : 
     564       165062 :                do j = 1, s% species
     565              : 
     566       146704 :                   cid = s% chem_id(j)
     567       146704 :                   if (sp == 'X') then  ! any hydrogen
     568            0 :                      if (cid /= ih1 .or. cid /= ih2 .or. cid /= ih3) cycle
     569       146704 :                   else if (sp == 'Y') then  ! any helium
     570            0 :                      if (cid /= ihe4 .or. cid /= ihe3) cycle
     571       146704 :                   else if (sp == 'Z') then  ! any metal
     572            0 :                      if (chem_isos% Z(cid) <= 2) cycle
     573              :                   else  ! single isotope
     574       146704 :                      if (trim(chem_isos% name(s% chem_id(j))) /= trim(sp)) cycle
     575              :                   end if
     576              : 
     577        18338 :                   X = s% xa(j,k)
     578        18338 :                   X_old = s% xa_old(j,k)
     579        18338 :                   delta_X = X_old - X  ! decrease in abundance
     580              : 
     581        18338 :                   if ((.not. s% dX_decreases_only(j)) .and. delta_X < 0) delta_X = -delta_X
     582              : 
     583        18338 :                   if (X >= s% dX_limit_min_X(i)) then  ! any check for dX_limit_* < 1 is useless since X <= 1 anyway
     584            0 :                      if ((.not. skip_hard_limit) .and. delta_X > dX_hard_limit(i)) then
     585            0 :                         check_dX = retry
     586            0 :                         s% why_Tlim = Tlim_dX
     587            0 :                         s% Tlim_dX_species = j
     588            0 :                         s% Tlim_dX_cell = k
     589            0 :                         s% retry_message = 'dX ' // trim(chem_isos% name(s% chem_id(j))) // ' hard limit'
     590            0 :                         s% retry_message_k = k
     591            0 :                         if (s% report_dt_hard_limit_retries) then
     592            0 :                            write(*,2) 'old xa', s% model_number, X_old
     593            0 :                            write(*,2) 'new xa', s% model_number, X
     594            0 :                            write(*,2) 'delta xa', s% model_number, delta_X
     595            0 :                            write(*,2) 'hard limit delta xa', s% model_number, dX_hard_limit
     596              :                         end if
     597            0 :                         return
     598              :                      end if
     599            0 :                      if (delta_X > max_dX) then
     600        18338 :                         max_dX = delta_X
     601        18338 :                         max_dX_j = j
     602        18338 :                         max_dX_k = k
     603        18338 :                         max_dX_bdy_dist_dm = bdy_dist_dm
     604              :                      end if
     605              :                   end if
     606        39584 :                   if (X >= s% dX_div_X_limit_min_X(i)) then
     607        18338 :                      delta_X_div_X = delta_X/X
     608        18338 :                      if ((.not. skip_hard_limit) .and. delta_X_div_X > dX_div_X_hard_limit(i)) then
     609            0 :                         check_dX = retry
     610            0 :                         s% why_Tlim = Tlim_dX_div_X
     611            0 :                         s% Tlim_dX_div_X_species = j
     612            0 :                         s% Tlim_dX_div_X_cell = k
     613            0 :                         s% retry_message = 'dX_div_X ' // trim(chem_isos% name(s% chem_id(j))) // ' hard limit'
     614            0 :                         s% retry_message_k = k
     615            0 :                         if (s% report_dt_hard_limit_retries) then
     616              :                            write(*, '(a30, i5, 99(/,a30,e20.10))') &
     617            0 :                               'delta_X_div_X ' // trim(chem_isos% name(s% chem_id(j))), &
     618            0 :                               k, 'delta_X_div_X', delta_X_div_X, &
     619            0 :                               'dX_div_X_hard_limit', dX_div_X_hard_limit
     620            0 :                            write(*,2) 'old xa', s% model_number, X_old
     621            0 :                            write(*,2) 'new xa', s% model_number, X
     622            0 :                            write(*,2) 'delta_X_div_X', s% model_number, delta_X_div_X
     623            0 :                            write(*,2) 'dX_div_X_hard_limit', s% model_number, dX_div_X_hard_limit
     624              :                         end if
     625            0 :                         return
     626              :                      end if
     627        18338 :                      if (delta_X_div_X > max_dX_div_X) then
     628         3787 :                         max_dX_div_X = delta_X_div_X
     629         3787 :                         max_dX_div_X_j = j
     630         3787 :                         max_dX_div_X_k = k
     631         3787 :                         max_dX_div_X_bdy_dist_dm = bdy_dist_dm
     632              :                      end if
     633              :                   end if
     634              :                end do
     635              :             end do
     636              : 
     637           20 :             if (dX_limit(i) > 0) then
     638           20 :                ratio_tmp_dX = max_dX/dX_limit(i)
     639           20 :                if (ratio_tmp_dX > dX_dt_limit_ratio) then
     640            0 :                   dX_dt_limit_ratio = ratio_tmp_dX
     641            0 :                   if (dX_dt_limit_ratio <= 1d0) then
     642            0 :                      dX_dt_limit_ratio = 0
     643              :                   else
     644            0 :                      s% Tlim_dX_species = max_dX_j
     645            0 :                      s% Tlim_dX_cell = max_dX_k
     646              :    !                  write(*, '(a30, i5, 99e20.10)') &
     647              :    !                     ' limit dt because of large dX '// &
     648              :    !                        trim(chem_isos% name(s% chem_id(max_dX_j))) // &
     649              :    !                     ' k, max, lim, m ', &
     650              :    !                     max_dX_k, max_dX, dX_limit(i), &
     651              :    !                     max_dX_bdy_dist_dm/Msun
     652              :                   end if
     653              :                end if
     654              :             end if
     655              : 
     656           30 :             if (dX_div_X_limit(i) > 0) then
     657           20 :                ratio_tmp_dX_div_X = max_dX_div_X/dX_div_X_limit(i)
     658           20 :                if (ratio_tmp_dX_div_X > dX_div_X_dt_limit_ratio) then  ! pick out largest culprit only!
     659           10 :                   dX_div_X_dt_limit_ratio = ratio_tmp_dX_div_X
     660           10 :                   if (dX_div_X_dt_limit_ratio <= 1d0) then
     661           10 :                      dX_div_X_dt_limit_ratio = 0
     662              :                   else
     663            0 :                      s% Tlim_dX_div_X_species = max_dX_div_X_j
     664            0 :                      s% Tlim_dX_div_X_cell = max_dX_div_X_k
     665              :    !                  write(*, '(a35, i5, 99e20.10)') &  ! shouldn't be written as is isn't guarantueed
     666              :                                                          ! this control will trigger timestep reduction
     667              :    !                     ' limit dt because of large dX_div_X ' // &
     668              :    !                        trim(chem_isos% name(s% chem_id(max_dX_div_X_j))) // &
     669              :    !                     ' k, max, lim, m ', &
     670              :    !                     max_dX_div_X_k, max_dX_div_X, dX_div_X_limit(i), &
     671              :    !                     max_dX_div_X_bdy_dist_dm/Msun
     672              :                   end if
     673              :                end if
     674              :             end if
     675              :          end do
     676              :       end function check_dX
     677              : 
     678              : 
     679           10 :       integer function check_dL_div_L(s, skip_hard_limit, dt, dL_div_L_dt_ratio)
     680              :          type (star_info), pointer :: s
     681              :          logical, intent(in) :: skip_hard_limit
     682              :          real(dp), intent(in) :: dt
     683              :          real(dp), intent(inout) :: dL_div_L_dt_ratio
     684              : 
     685              :          real(dp) :: L, abs_dL, abs_dL_div_L, max_dL_div_L
     686              :          integer :: k, max_dL_div_L_k
     687              :          real(dp) :: dL_div_L_limit_min_L, dL_div_L_limit, dL_div_L_hard_limit
     688              : 
     689              :          include 'formats'
     690              : 
     691           10 :          check_dL_div_L = keep_going
     692              : 
     693           10 :          dL_div_L_limit_min_L = Lsun*s% dL_div_L_limit_min_L
     694           10 :          dL_div_L_limit = s% dL_div_L_limit*s% time_delta_coeff
     695           10 :          dL_div_L_hard_limit = s% dL_div_L_hard_limit*s% time_delta_coeff
     696              : 
     697           10 :          if (dL_div_L_limit_min_L <= 0) return
     698           10 :          if (dL_div_L_limit <= 0 .and. dL_div_L_hard_limit <= 0) return
     699              : 
     700            0 :          max_dL_div_L = -1
     701            0 :          max_dL_div_L_k = -1
     702            0 :          abs_dL_div_L = 0; L=0  ! to quiet gfortran
     703              : 
     704            0 :          do k = 1, s% nz
     705            0 :             L = s% L(k)
     706            0 :             abs_dL = abs(L - s% L_start(k))
     707            0 :             if (L >= dL_div_L_limit_min_L) then
     708            0 :                abs_dL_div_L = abs_dL/L
     709              :                if (dL_div_L_hard_limit > 0 .and. (.not. skip_hard_limit) &
     710            0 :                      .and. abs_dL_div_L > dL_div_L_hard_limit) then
     711            0 :                   check_dL_div_L= retry
     712            0 :                   s% retry_message = 'dL_div_L hard limit'
     713            0 :                   s% retry_message_k = k
     714            0 :                   if (s% report_dt_hard_limit_retries) then
     715            0 :                      write(*,2) 'L', L
     716            0 :                      write(*,2) 'L_start', s% L_start(k)
     717            0 :                      write(*,2) 'abs_dL_div_L', abs_dL_div_L
     718            0 :                      write(*,2) 'dL_div_L_hard_limit', dL_div_L_hard_limit
     719              :                   end if
     720            0 :                   return
     721              :                end if
     722            0 :                if (abs_dL_div_L > max_dL_div_L) then
     723            0 :                   max_dL_div_L = abs_dL_div_L
     724            0 :                   max_dL_div_L_k = k
     725              :                end if
     726              :             end if
     727              :          end do
     728              : 
     729            0 :          if (dL_div_L_limit <= 0) return
     730            0 :          dL_div_L_dt_ratio = max_dL_div_L/dL_div_L_limit
     731              : 
     732            0 :       end function check_dL_div_L
     733              : 
     734              : 
     735          160 :       integer function check_change( &
     736              :             s, delta_value, lim_in, hard_lim_in, max_k, msg, &
     737              :             skip_hard_limit, dt_limit_ratio, relative_excess)
     738              :          type (star_info), pointer :: s
     739              :          real(dp), intent(in) :: delta_value, lim_in, hard_lim_in
     740              :          integer, intent(in) :: max_k
     741              :          character (len=*), intent(in) :: msg
     742              :          logical, intent(in) :: skip_hard_limit
     743              :          real(dp), intent(inout) :: dt_limit_ratio
     744              :          real(dp), intent(out) :: relative_excess
     745              :          real(dp) :: abs_change, lim, hard_lim
     746              :          include 'formats'
     747          160 :          if (is_bad(delta_value)) then
     748            0 :             write(*,1) trim(msg) // ' delta_value', delta_value
     749            0 :             call mesa_error(__FILE__,__LINE__,'check_change')
     750              :          end if
     751          160 :          check_change = keep_going
     752          160 :          abs_change = abs(delta_value)
     753          160 :          lim = lim_in*s% time_delta_coeff
     754          160 :          hard_lim = hard_lim_in*s% time_delta_coeff
     755          160 :          if (hard_lim > 0 .and. abs_change > hard_lim .and. (.not. skip_hard_limit)) then
     756            0 :             s% retry_message = trim(msg) // ' hard limit'
     757            0 :             s% retry_message_k = max_k
     758            0 :             check_change = retry
     759            0 :             return
     760              :          end if
     761          160 :          if (lim <= 0) return
     762          120 :          relative_excess = (abs_change - lim) / lim
     763          120 :          dt_limit_ratio = abs_change/lim  ! 1d0/(s% timestep_dt_factor**relative_excess)
     764          120 :          if (is_bad(dt_limit_ratio)) then
     765            0 :             write(*,1) trim(msg) // ' dt_limit_ratio', dt_limit_ratio, abs_change, lim
     766            0 :             call mesa_error(__FILE__,__LINE__,'check_change')
     767              :          end if
     768          120 :          if (dt_limit_ratio <= 1d0) dt_limit_ratio = 0
     769              :       end function check_change
     770              : 
     771              : 
     772           10 :       subroutine get_dlgP_info(s, i, max_dlnP)
     773              :          type (star_info), pointer :: s
     774              :          integer, intent(out) :: i
     775              :          real(dp), intent(out) :: max_dlnP
     776              :          real(dp) :: lim, dlnP
     777              :          integer :: k
     778              :          include 'formats'
     779           10 :          lim = ln10*s% delta_lgP_limit_min_lgP
     780           10 :          i = 0
     781           10 :          max_dlnP = 0
     782        10633 :          do k=1,s% nz
     783        10623 :             if (s% lnPeos(k) < lim) cycle
     784            0 :             dlnP = abs(s% lnPeos(k) - s% lnPeos_start(k))
     785           10 :             if (dlnP > max_dlnP) then
     786            0 :                max_dlnP = dlnP
     787            0 :                i = k
     788              :             end if
     789              :          end do
     790           10 :       end subroutine get_dlgP_info
     791              : 
     792              : 
     793           10 :       integer function check_dlgP_change(s, skip_hard_limit, dt_limit_ratio)
     794              :          type (star_info), pointer :: s
     795              :          logical, intent(in) :: skip_hard_limit
     796              :          real(dp), intent(inout) :: dt_limit_ratio
     797              :          real(dp) :: relative_excess, max_dlnP
     798              :          integer :: i
     799              :          include 'formats'
     800           10 :          check_dlgP_change = keep_going
     801           10 :          call get_dlgP_info(s, i, max_dlnP)
     802           10 :          if (i == 0) return
     803              :          check_dlgP_change = check_change(s, max_dlnP/ln10, &
     804              :             s% delta_lgP_limit, s% delta_lgP_hard_limit, &
     805            0 :             i, 'delta_lgP', skip_hard_limit, dt_limit_ratio, relative_excess)
     806            0 :          if (check_dlgP_change /= keep_going .and. s% report_dt_hard_limit_retries) then
     807            0 :             write(*,3) 'lgP', i, s% lnPeos(i)/ln10
     808            0 :             write(*,3) 'lgP_old', i, s% lnPeos_start(i)/ln10
     809            0 :             write(*,3) 'dlgP', i, (s% lnPeos(i) - s% lnPeos_start(i))/ln10
     810            0 :             write(*,3) 'hard_limit', i, s% delta_lgP_hard_limit
     811              :          end if
     812              :       end function check_dlgP_change
     813              : 
     814              : 
     815           10 :       subroutine get_dlgRho_info(s, i, max_dlnRho)
     816              :          type (star_info), pointer :: s
     817              :          integer, intent(out) :: i
     818              :          real(dp), intent(out) :: max_dlnRho
     819              :          real(dp) :: lim, dlnRho, max_abs_dlnRho
     820              :          integer :: k
     821              :          include 'formats'
     822           10 :          lim = ln10*s% delta_lgRho_limit_min_lgRho
     823           10 :          i = 0
     824           10 :          max_abs_dlnRho = 0
     825        10633 :          do k=1,s% nz
     826        10623 :             if (s% lnd(k) < lim) cycle
     827            0 :             dlnRho = s% lnd(k) - s% lnd_start(k)
     828           10 :             if (abs(dlnRho) > max_abs_dlnRho) then
     829            0 :                max_dlnRho = dlnRho
     830            0 :                max_abs_dlnRho = abs(dlnRho)
     831            0 :                i = k
     832              :             end if
     833              :          end do
     834           10 :       end subroutine get_dlgRho_info
     835              : 
     836              : 
     837           10 :       integer function check_dlgRho_change(s, skip_hard_limit, dt_limit_ratio)
     838              :          ! check max change in log10(density)
     839              :          type (star_info), pointer :: s
     840              :          logical, intent(in) :: skip_hard_limit
     841              :          real(dp), intent(inout) :: dt_limit_ratio
     842              :          real(dp) :: relative_excess, max_dlnd
     843              :          integer :: i
     844              :          include 'formats'
     845           10 :          check_dlgRho_change = keep_going
     846           10 :          call get_dlgRho_info(s, i, max_dlnd)
     847           10 :          if (i == 0) return
     848              :          check_dlgRho_change = check_change(s, abs(max_dlnd)/ln10, &
     849              :             s% delta_lgRho_limit, s% delta_lgRho_hard_limit, &
     850            0 :             i, 'delta_lgRho', skip_hard_limit, dt_limit_ratio, relative_excess)
     851            0 :          if (check_dlgRho_change /= keep_going .and. s% report_dt_hard_limit_retries) then
     852            0 :             write(*,3) 'lgRho', i, s% lnd(i)/ln10
     853            0 :             write(*,3) 'lgRho_old', i, s% lnd_start(i)/ln10
     854            0 :             write(*,3) 'dlgRho', i, (s% lnd(i) - s% lnd_start(i))/ln10
     855            0 :             write(*,3) 'hard_limit', i, s% delta_lgRho_hard_limit
     856              :          end if
     857              :       end function check_dlgRho_change
     858              : 
     859              : 
     860           10 :       subroutine get_dlgT_info(s, i, max_dlnT)
     861              :          type (star_info), pointer :: s
     862              :          integer, intent(out) :: i
     863              :          real(dp), intent(out) :: max_dlnT
     864              :          real(dp) :: lim, dlnT, abs_max_dlnT
     865              :          integer :: k
     866              :          include 'formats'
     867           10 :          lim = ln10*s% delta_lgT_limit_min_lgT
     868           10 :          i = 0
     869           10 :          abs_max_dlnT = 0
     870        10633 :          do k=1,s% nz
     871        10623 :             if (s% lnT(k) < lim) cycle
     872            0 :             dlnT = s% lnT(k) - s% lnT_start(k)
     873           10 :             if (abs(dlnT) > abs_max_dlnT) then
     874            0 :                max_dlnT = dlnT
     875            0 :                abs_max_dlnT = abs(dlnT)
     876            0 :                i = k
     877              :             end if
     878              :          end do
     879           10 :       end subroutine get_dlgT_info
     880              : 
     881              : 
     882           10 :       integer function check_dlgT_change(s, skip_hard_limit, dt_limit_ratio)
     883              :          ! check max change in log10(temperature)
     884              :          type (star_info), pointer :: s
     885              :          logical, intent(in) :: skip_hard_limit
     886              :          real(dp), intent(inout) :: dt_limit_ratio
     887              :          real(dp) :: relative_excess, max_dlnT
     888              :          integer :: i
     889              :          include 'formats'
     890           10 :          check_dlgT_change = keep_going
     891           10 :          call get_dlgT_info(s, i, max_dlnT)
     892           10 :          if (i == 0) return
     893              :          check_dlgT_change = check_change(s, abs(max_dlnT)/ln10, &
     894              :             s% delta_lgT_limit, s% delta_lgT_hard_limit, &
     895            0 :             i, 'delta_lgT', skip_hard_limit, dt_limit_ratio, relative_excess)
     896            0 :          if (check_dlgT_change /= keep_going .and. s% report_dt_hard_limit_retries) then
     897            0 :             write(*,3) 'lgT', i, s% lnT(i)/ln10
     898            0 :             write(*,3) 'lgT_old', i, s% lnT_start(i)/ln10
     899            0 :             write(*,3) 'dlgT', i, (s% lnT(i) - s% lnT_start(i))/ln10
     900            0 :             write(*,3) 'hard_limit', i, s% delta_lgT_hard_limit
     901              :          end if
     902              :       end function check_dlgT_change
     903              : 
     904              : 
     905           10 :       subroutine get_dlgE_info(s, i, max_dlnE)
     906              :          type (star_info), pointer :: s
     907              :          integer, intent(out) :: i
     908              :          real(dp), intent(out) :: max_dlnE
     909              :          real(dp) :: lim, dlnE
     910              :          integer :: k
     911              :          include 'formats'
     912           10 :          lim = ln10*s% delta_lgE_limit_min_lgE
     913           10 :          i = 0
     914           10 :          max_dlnE = 0
     915        10633 :          do k=1,s% nz
     916        10623 :             if (s% lnE(k) < lim) cycle
     917            0 :             dlnE = abs(s% energy(k) - s% energy_start(k))/s% energy(k)
     918           10 :             if (dlnE > max_dlnE) then
     919            0 :                max_dlnE = dlnE
     920            0 :                i = k
     921              :             end if
     922              :          end do
     923           10 :       end subroutine get_dlgE_info
     924              : 
     925              : 
     926           10 :       integer function check_dlgE_change(s, skip_hard_limit, dt_limit_ratio)
     927              :          ! check max change in log10(internal energy)
     928              :          type (star_info), pointer :: s
     929              :          logical, intent(in) :: skip_hard_limit
     930              :          real(dp), intent(inout) :: dt_limit_ratio
     931              :          real(dp) :: relative_excess, max_dlnE
     932              :          integer :: i
     933              :          include 'formats'
     934           10 :          check_dlgE_change = keep_going
     935           10 :          call get_dlgE_info(s, i, max_dlnE)
     936           10 :          if (i == 0) return
     937              :          check_dlgE_change = check_change(s, max_dlnE/ln10, &
     938              :             s% delta_lgE_limit, s% delta_lgE_hard_limit, &
     939            0 :             i, 'delta_lgE', skip_hard_limit, dt_limit_ratio, relative_excess)
     940            0 :          if (check_dlgE_change /= keep_going .and. s% report_dt_hard_limit_retries) then
     941            0 :             write(*,3) 'lgE', i, safe_log10(s% energy(i))
     942            0 :             write(*,3) 'lgE_old', i, safe_log10(s% energy_start(i))
     943            0 :             write(*,3) 'dlgE', i, (s% energy(i) - s% energy_start(i))/s% energy(i)/ln10
     944            0 :             write(*,3) 'hard_limit', i, s% delta_lgE_hard_limit
     945              :          end if
     946              :       end function check_dlgE_change
     947              : 
     948              : 
     949           10 :       subroutine get_dlgR_info(s, i, max_dlnR)
     950              :          type (star_info), pointer :: s
     951              :          integer, intent(out) :: i
     952              :          real(dp), intent(out) :: max_dlnR
     953              :          real(dp) :: lim, dlnR
     954              :          integer :: k
     955              :          include 'formats'
     956           10 :          lim = ln10*s% delta_lgR_limit_min_lgR
     957           10 :          i = 0
     958           10 :          max_dlnR = 0
     959        10633 :          do k=1,s% nz
     960        10623 :             if (s% lnR(k) < lim) cycle
     961            0 :             dlnR = abs(s% lnR(k) - s% lnR_start(k))
     962           10 :             if (dlnR > max_dlnR) then
     963            0 :                max_dlnR = dlnR
     964            0 :                i = k
     965              :             end if
     966              :          end do
     967           10 :       end subroutine get_dlgR_info
     968              : 
     969              : 
     970           10 :       integer function check_dlgR_change(s, skip_hard_limit, dt_limit_ratio)
     971              :          type (star_info), pointer :: s
     972              :          logical, intent(in) :: skip_hard_limit
     973              :          real(dp), intent(inout) :: dt_limit_ratio
     974              :          real(dp) :: relative_excess, max_dlnR
     975              :          integer :: i
     976              :          include 'formats'
     977           10 :          check_dlgR_change = keep_going
     978           10 :          call get_dlgR_info(s, i, max_dlnR)
     979           10 :          if (i == 0) return
     980              :          check_dlgR_change = check_change(s, max_dlnR/ln10, &
     981              :             s% delta_lgR_limit, s% delta_lgR_hard_limit, &
     982            0 :             i, 'delta_lgR', skip_hard_limit, dt_limit_ratio, relative_excess)
     983            0 :          if (check_dlgR_change /= keep_going .and. s% report_dt_hard_limit_retries) then
     984            0 :             write(*,3) 'lgR', i, s% lnR(i)/ln10
     985            0 :             write(*,3) 'lgR_old', i, s% lnR_start(i)/ln10
     986            0 :             write(*,3) 'dlgR', i, (s% lnR(i) - s% lnR_start(i))/ln10
     987            0 :             write(*,3) 'hard_limit', i, s% delta_lgR_hard_limit
     988              :          end if
     989              :       end function check_dlgR_change
     990              : 
     991              : 
     992           50 :       integer function check_lgL( &
     993              :             s, iso_in, msg, skip_hard_limit, dt, dt_limit_ratio)
     994              :          type (star_info), pointer :: s
     995              :          integer, intent(in) :: iso_in
     996              :          character (len=*), intent(in) :: msg
     997              :          logical, intent(in) :: skip_hard_limit
     998              :          real(dp), intent(in) :: dt
     999              :          real(dp), intent(inout) :: dt_limit_ratio
    1000              : 
    1001              :          real(dp) :: &
    1002              :             new_L, max_other_L, old_L, lim, hard_lim, lgL_min, &
    1003              :             drop_factor, relative_limit, lgL, max_other_lgL, lgL_old, &
    1004              :             abs_change, relative_excess
    1005              :          logical, parameter :: dbg = .false.
    1006              :          integer :: iso
    1007              :          include 'formats'
    1008           50 :          check_lgL = keep_going
    1009              : 
    1010           50 :          iso = iso_in
    1011           50 :          if (iso == iprot) then  ! check_lgL_power_photo_change
    1012           10 :             if (s% log_max_temperature < s% min_lgT_for_lgL_power_photo_limit) return
    1013            0 :             new_L = abs(s% power_photo)
    1014            0 :             max_other_L = 0d0
    1015            0 :             old_L = abs(s% power_photo_old)
    1016            0 :             lim = s% delta_lgL_power_photo_limit
    1017            0 :             hard_lim = s% delta_lgL_power_photo_hard_limit
    1018            0 :             lgL_min = s% lgL_power_photo_burn_min
    1019            0 :             drop_factor = s% lgL_power_photo_drop_factor
    1020            0 :             relative_limit = 0d0
    1021           40 :          else if (iso == ineut) then  ! check_lgL_nuc_change
    1022           10 :             if (s% log_max_temperature > s% max_lgT_for_lgL_nuc_limit) return
    1023           10 :             new_L = s% power_nuc_burn
    1024           10 :             max_other_L = 0d0
    1025           10 :             old_L = s% power_nuc_burn_old
    1026           10 :             if (s% log_max_temperature > &
    1027              :                   s% delta_lgL_nuc_at_high_T_limit_lgT_min) then
    1028            0 :                lim = s% delta_lgL_nuc_at_high_T_limit
    1029            0 :                hard_lim = s% delta_lgL_nuc_at_high_T_hard_limit
    1030              :             else
    1031           10 :                lim = s% delta_lgL_nuc_limit
    1032           10 :                hard_lim = s% delta_lgL_nuc_hard_limit
    1033              :             end if
    1034           10 :             lgL_min = s% lgL_nuc_burn_min
    1035           10 :             drop_factor = s% lgL_nuc_drop_factor
    1036           10 :             relative_limit = 0d0
    1037           30 :          else if (iso == ih1) then
    1038           10 :             new_L = s% power_h_burn
    1039           10 :             max_other_L = max(s% power_he_burn, s% power_z_burn)
    1040           10 :             old_L = s% power_h_burn_old
    1041           10 :             lim = s% delta_lgL_H_limit
    1042           10 :             hard_lim = s% delta_lgL_H_hard_limit
    1043           10 :             lgL_min = s% lgL_H_burn_min
    1044           10 :             drop_factor = s% lgL_H_drop_factor
    1045           10 :             relative_limit = s% lgL_H_burn_relative_limit
    1046           20 :          else if (iso == ihe4) then
    1047           10 :             new_L = s% power_he_burn
    1048           10 :             max_other_L = max(s% power_h_burn, s% power_z_burn)
    1049           10 :             old_L = s% power_he_burn_old
    1050           10 :             lim = s% delta_lgL_He_limit
    1051           10 :             hard_lim = s% delta_lgL_He_hard_limit
    1052           10 :             lgL_min = s% lgL_He_burn_min
    1053           10 :             drop_factor = s% lgL_He_drop_factor
    1054           10 :             relative_limit = s% lgL_He_burn_relative_limit
    1055           10 :          else if (iso == isi28) then
    1056           10 :             new_L = s% power_z_burn
    1057           10 :             max_other_L = max(s% power_h_burn, s% power_he_burn)
    1058           10 :             old_L = s% power_z_burn_old
    1059           10 :             lim = s% delta_lgL_z_limit
    1060           10 :             hard_lim = s% delta_lgL_z_hard_limit
    1061           10 :             lgL_min = s% lgL_z_burn_min
    1062           10 :             drop_factor = s% lgL_z_drop_factor
    1063           10 :             relative_limit = s% lgL_z_burn_relative_limit
    1064              :          else
    1065            0 :             call mesa_error(__FILE__,__LINE__,'bad iso arg for check_lgL')
    1066              :          end if
    1067              : 
    1068           40 :          if (old_L < 0d0) return
    1069              : 
    1070           37 :          lim = lim*s% time_delta_coeff
    1071           37 :          hard_lim = hard_lim*s% time_delta_coeff
    1072              : 
    1073           37 :          if (new_L < old_L) then
    1074           11 :             lim = lim*drop_factor
    1075           11 :             hard_lim = hard_lim*drop_factor
    1076              :          end if
    1077              : 
    1078              :          if (dbg) write(*,*)
    1079              :          if (dbg) write(*,1) trim(msg) // ' new_L', new_L
    1080              :          if (dbg) write(*,1) 'old_L', old_L
    1081           37 :          if (new_L <= 0 .or. old_L <= 0) return
    1082              : 
    1083           32 :          lgL = safe_log10(new_L)
    1084              :          if (dbg) write(*,1) 'lgL', lgL
    1085           32 :          if (lgL < lgL_min) return
    1086              : 
    1087           20 :          if (max_other_L > 0) then
    1088           10 :             max_other_lgL = safe_log10(max_other_L)
    1089              :             if (dbg) write(*,1) 'max_other_lgL', max_other_lgL
    1090           10 :             if (max_other_lgL - relative_limit > lgL) return
    1091              :          end if
    1092              : 
    1093           20 :          lgL_old = safe_log10(old_L)
    1094              :          if (dbg) write(*,1) 'lgL_old', lgL_old
    1095           20 :          abs_change = abs(lgL - lgL_old)
    1096              :          if (dbg) write(*,1) 'abs_change', abs_change
    1097              :          if (dbg) write(*,1) 'hard_lim', hard_lim
    1098           20 :          if (hard_lim > 0 .and. abs_change > hard_lim .and. (.not. skip_hard_limit)) then
    1099            0 :             if (s% report_dt_hard_limit_retries) then
    1100            0 :                write(*,1) trim(msg) // ' end', lgL
    1101            0 :                write(*,1) trim(msg) // ' start', lgL_old
    1102            0 :                write(*,1) trim(msg) // ' delta', lgL - lgL_old
    1103            0 :                write(*,1) trim(msg) // ' hard_lim', hard_lim
    1104              :             end if
    1105            0 :             check_lgL = retry
    1106            0 :             s% retry_message = trim(msg) // ' hard limit'
    1107            0 :             return
    1108              :          end if
    1109              : 
    1110              :          if (dbg) write(*,1) 'lim', lim
    1111           20 :          if (lim <= 0) return
    1112              : 
    1113            0 :          relative_excess = (abs_change - lim) / lim
    1114              :          if (dbg) write(*,1) 'relative_excess', relative_excess
    1115            0 :          dt_limit_ratio = 1d0/pow(s% timestep_dt_factor,relative_excess)
    1116            0 :          if (dt_limit_ratio <= 1d0) dt_limit_ratio = 0
    1117              : 
    1118              :       end function check_lgL
    1119              : 
    1120              : 
    1121           10 :       integer function check_lgL_H_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1122              :          type (star_info), pointer :: s
    1123              :          logical, intent(in) :: skip_hard_limit
    1124              :          real(dp), intent(in) :: dt
    1125              :          real(dp), intent(inout) :: dt_limit_ratio
    1126              :          check_lgL_H_change = check_lgL( &
    1127           10 :             s, ih1, 'check_lgL_H_change', skip_hard_limit, dt, dt_limit_ratio)
    1128              :       end function check_lgL_H_change
    1129              : 
    1130              : 
    1131           10 :       integer function check_lgL_He_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1132              :          type (star_info), pointer :: s
    1133              :          logical, intent(in) :: skip_hard_limit
    1134              :          real(dp), intent(in) :: dt
    1135              :          real(dp), intent(inout) :: dt_limit_ratio
    1136              :          check_lgL_He_change = check_lgL( &
    1137           10 :             s, ihe4, 'check_lgL_He_change', skip_hard_limit, dt, dt_limit_ratio)
    1138              :       end function check_lgL_He_change
    1139              : 
    1140              : 
    1141           10 :       integer function check_lgL_z_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1142              :          type (star_info), pointer :: s
    1143              :          logical, intent(in) :: skip_hard_limit
    1144              :          real(dp), intent(in) :: dt
    1145              :          real(dp), intent(inout) :: dt_limit_ratio
    1146              :          check_lgL_z_change = check_lgL( &
    1147           10 :             s, isi28, 'check_lgL_z_change', skip_hard_limit, dt, dt_limit_ratio)
    1148              :       end function check_lgL_z_change
    1149              : 
    1150              : 
    1151           10 :       integer function check_lgL_power_photo_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1152              :          type (star_info), pointer :: s
    1153              :          logical, intent(in) :: skip_hard_limit
    1154              :          real(dp), intent(in) :: dt
    1155              :          real(dp), intent(inout) :: dt_limit_ratio
    1156              :          check_lgL_power_photo_change = check_lgL( &
    1157           10 :             s, iprot, 'check_lgL_power_photo_change', skip_hard_limit, dt, dt_limit_ratio)
    1158              :       end function check_lgL_power_photo_change
    1159              : 
    1160              : 
    1161           10 :       integer function check_lgL_nuc_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1162              :          type (star_info), pointer :: s
    1163              :          logical, intent(in) :: skip_hard_limit
    1164              :          real(dp), intent(in) :: dt
    1165              :          real(dp), intent(inout) :: dt_limit_ratio
    1166              :          include 'formats'
    1167              :          check_lgL_nuc_change = check_lgL( &
    1168           10 :             s, ineut, 'check_lgL_nuc_change', skip_hard_limit, dt, dt_limit_ratio)
    1169              :       end function check_lgL_nuc_change
    1170              : 
    1171              : 
    1172           10 :       integer function check_lgL_nuc_cat_change( &
    1173              :             s, n_mix_bdy, mix_bdy_q, skip_hard_limit, dt_limit_ratio)
    1174              :          use rates_def
    1175              :          use num_lib, only: binary_search
    1176              :          type (star_info), pointer :: s
    1177              :          logical, intent(in) :: skip_hard_limit
    1178              :          integer, intent(in) :: n_mix_bdy
    1179              :          real(dp), intent(in), pointer :: mix_bdy_q(:)
    1180              :          real(dp), intent(inout) :: dt_limit_ratio
    1181              : 
    1182              :          integer :: k, max_j, max_k, bdy
    1183              :          real(dp) :: max_lgL_diff, relative_excess, max_diff, cat_burn_min, &
    1184              :             max_luminosity, max_luminosity_start
    1185              : 
    1186              :          include 'formats'
    1187              : 
    1188           10 :          check_lgL_nuc_cat_change = keep_going
    1189              : 
    1190           10 :          if (s% delta_lgL_nuc_cat_limit <= 0 .and. s% delta_lgL_nuc_cat_hard_limit <= 0) return
    1191              : 
    1192            0 :          cat_burn_min = exp10(s% lgL_nuc_cat_burn_min)
    1193            0 :          max_diff = 0
    1194            0 :          max_j = -1
    1195            0 :          max_k = -1
    1196            0 :          bdy = -1
    1197              : 
    1198            0 :          do k = 1, s% nz
    1199              : 
    1200              :             ! find the nearest mixing boundary
    1201            0 :             bdy = binary_search(n_mix_bdy, mix_bdy_q, bdy, s% q(k))
    1202              :             ! don't check cells near a mixing boundary
    1203            0 :             if (bdy > 0) then
    1204            0 :                if (abs(s% q(k) - mix_bdy_q(bdy)) < s% lgL_nuc_mix_dist_limit) cycle
    1205              :             end if
    1206              : 
    1207            0 :             call do1_category(ipp,k)
    1208            0 :             call do1_category(icno,k)
    1209            0 :             call do1_category(i3alf,k)
    1210            0 :             call do1_category(i_burn_c,k)
    1211            0 :             call do1_category(i_burn_n,k)
    1212            0 :             call do1_category(i_burn_o,k)
    1213            0 :             call do1_category(i_burn_ne,k)
    1214            0 :             call do1_category(i_burn_na,k)
    1215            0 :             call do1_category(i_burn_mg,k)
    1216            0 :             call do1_category(i_burn_si,k)
    1217            0 :             call do1_category(i_burn_s,k)
    1218            0 :             call do1_category(i_burn_ar,k)
    1219            0 :             call do1_category(i_burn_ca,k)
    1220            0 :             call do1_category(i_burn_ti,k)
    1221            0 :             call do1_category(i_burn_cr,k)
    1222            0 :             call do1_category(i_burn_fe,k)
    1223            0 :             call do1_category(icc,k)
    1224            0 :             call do1_category(ico,k)
    1225            0 :             call do1_category(ioo,k)
    1226              : 
    1227              :          end do
    1228              : 
    1229              : 
    1230            0 :          if (max_diff <= 0) return
    1231              : 
    1232            0 :          max_lgL_diff = log10(max_diff/Lsun)
    1233            0 :          s% Tlim_dlgL_nuc_category = max_j
    1234            0 :          s% Tlim_dlgL_nuc_cell = max_k
    1235              : 
    1236              :          check_lgL_nuc_cat_change = check_change(s, max_lgL_diff, &
    1237              :             s% delta_lgL_nuc_cat_limit, s% delta_lgL_nuc_cat_hard_limit, &
    1238            0 :             max_k, 'delta_lgL_nuc_cat', skip_hard_limit, dt_limit_ratio, relative_excess)
    1239            0 :          if (check_lgL_nuc_cat_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1240            0 :             write(*,1) 'max_luminosity ' // trim(category_name(max_j)), max_luminosity
    1241            0 :             write(*,1) 'max_luminosity_start ' // trim(category_name(max_j)), max_luminosity_start
    1242              :          end if
    1243              : 
    1244              :          contains
    1245              : 
    1246            0 :          subroutine do1_category(j, k)
    1247              :             integer, intent(in) :: j, k
    1248              :             real(dp) :: diff, abs_diff
    1249            0 :             if (s% luminosity_by_category(j,k) < cat_burn_min) return
    1250            0 :             if (s% luminosity_by_category_start(j,k) < cat_burn_min) return
    1251            0 :             diff = s% luminosity_by_category(j,k) - s% luminosity_by_category_start(j,k)
    1252            0 :             abs_diff = abs(diff)
    1253            0 :             if (abs_diff <= max_diff) return
    1254            0 :             max_luminosity = s% luminosity_by_category(j,k)
    1255            0 :             max_luminosity_start = s% luminosity_by_category_start(j,k)
    1256            0 :             max_diff = abs_diff
    1257            0 :             max_j = j
    1258            0 :             max_k = k
    1259              :          end subroutine do1_category
    1260              : 
    1261              :       end function check_lgL_nuc_cat_change
    1262              : 
    1263              : 
    1264           10 :       integer function check_dlgTeff_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1265              :          type (star_info), pointer :: s
    1266              :          logical, intent(in) :: skip_hard_limit
    1267              :          real(dp), intent(in) :: dt
    1268              :          real(dp), intent(inout) :: dt_limit_ratio
    1269              :          real(dp) :: relative_excess
    1270              :          include 'formats'
    1271           10 :          check_dlgTeff_change = keep_going
    1272           10 :          dt_limit_ratio = 0d0
    1273           10 :          if (s% doing_relax .or. s% Teff_old <= 0 .or. s% Teff <= 0) return
    1274              :          check_dlgTeff_change = check_change(s, safe_log10(s% Teff/s% Teff_old), &
    1275              :             s% delta_lgTeff_limit, s% delta_lgTeff_hard_limit, &
    1276           10 :             0, 'delta_lgTeff', skip_hard_limit, dt_limit_ratio, relative_excess)
    1277           10 :          if (check_dlgTeff_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1278            0 :             write(*,1) 'lgTeff', safe_log10(s% Teff)
    1279            0 :             write(*,1) 'lgTeff_old', safe_log10(s% Teff_old)
    1280              :          end if
    1281              :       end function check_dlgTeff_change
    1282              : 
    1283              : 
    1284           10 :       integer function check_dYe_highT_change(s, skip_hard_limit, dt_limit_ratio)  ! check max change in Ye
    1285              :          type (star_info), pointer :: s
    1286              :          logical, intent(in) :: skip_hard_limit
    1287              :          real(dp), intent(inout) :: dt_limit_ratio
    1288              :          real(dp) :: relative_excess, max_diff, ye_diff, T_limit
    1289              :          integer :: i, k
    1290              :          include 'formats'
    1291           10 :          check_dYe_highT_change = keep_going
    1292           10 :          dt_limit_ratio = 0d0
    1293           10 :          if (s% doing_relax) return
    1294           10 :          i = 0
    1295           10 :          max_diff = 0
    1296           10 :          T_limit = s% minT_for_highT_Ye_limit
    1297        10633 :          do k=1, s% nz
    1298        10623 :             if (s% T(k) < T_limit) cycle
    1299            0 :             ye_diff = abs(s% ye(k) - s% ye_start(k))
    1300            0 :             if (ye_diff <= max_diff) cycle
    1301            0 :             max_diff = ye_diff
    1302        10633 :             i = k
    1303              :          end do
    1304              :          check_dYe_highT_change = check_change(s, max_diff, &
    1305              :             s% delta_Ye_highT_limit, s% delta_Ye_highT_hard_limit, &
    1306           10 :             i, 'delta_Ye_highT', .false., dt_limit_ratio, relative_excess)
    1307           10 :          if (check_dYe_highT_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1308            0 :             write(*,2) 'ye', i, s% ye(i)
    1309            0 :             write(*,2) 'ye_start', i, s% ye_start(i)
    1310              :          end if
    1311              :       end function check_dYe_highT_change
    1312              : 
    1313              : 
    1314           10 :       integer function check_dlgT_max_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1315              :          type (star_info), pointer :: s
    1316              :          logical, intent(in) :: skip_hard_limit
    1317              :          real(dp), intent(in) :: dt
    1318              :          real(dp), intent(inout) :: dt_limit_ratio
    1319              :          real(dp) :: relative_excess, change, lnTmax, lnTmax_start
    1320              :          integer :: lnTmax_k
    1321              :          include 'formats'
    1322           10 :          check_dlgT_max_change = keep_going
    1323           10 :          dt_limit_ratio = 0d0
    1324           10 :          if (s% doing_relax) return
    1325           10 :          if (s% delta_lgT_max_limit_lgT_min < 0d0) return
    1326           10 :          if (s% delta_lgT_max_limit_only_after_near_zams) then
    1327            0 :             if (s% X(s% nz) > 0.1d0 .and. &
    1328              :                 s% L_nuc_burn_total/s% L_phot < s% Lnuc_div_L_zams_limit ) return
    1329              :          end if
    1330        10633 :          lnTmax_k = maxloc(s% lnT(1:s% nz),dim=1)
    1331           10 :          lnTmax = s% lnT(lnTmax_k)
    1332           10 :          if (lnTmax < s% delta_lgT_max_limit_lgT_min*ln10) return
    1333            0 :          lnTmax_start = maxval(s% lnT_start(1:s% nz))
    1334            0 :          change = (lnTmax - lnTmax_start)/ln10
    1335              :          check_dlgT_max_change = check_change(s, change, &
    1336              :             s% delta_lgT_max_limit, s% delta_lgT_max_hard_limit, &
    1337            0 :             lnTmax_k, 'delta_lgT_max', skip_hard_limit, dt_limit_ratio, relative_excess)
    1338            0 :          if (check_dlgT_max_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1339            0 :             write(*,2) 'lgT_max', lnTmax_k, lnTmax/ln10
    1340            0 :             write(*,2) 'lgT_max_old', lnTmax_k, lnTmax_start/ln10
    1341              :          end if
    1342              :       end function check_dlgT_max_change
    1343              : 
    1344              : 
    1345           10 :       integer function check_dlgT_max_at_high_T_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1346              :          type (star_info), pointer :: s
    1347              :          logical, intent(in) :: skip_hard_limit
    1348              :          real(dp), intent(in) :: dt
    1349              :          real(dp), intent(inout) :: dt_limit_ratio
    1350              :          real(dp) :: relative_excess, change, lnTmax_at_high_T, lnTmax_at_high_T_start
    1351              :          integer :: lnTmax_k
    1352              :          include 'formats'
    1353           10 :          check_dlgT_max_at_high_T_change = keep_going
    1354           10 :          dt_limit_ratio = 0d0
    1355           10 :          if (s% doing_relax) return
    1356           10 :          if (s% delta_lgT_max_at_high_T_limit_lgT_min < 0d0) return
    1357            0 :          lnTmax_k = maxloc(s% lnT(1:s% nz),dim=1)
    1358            0 :          lnTmax_at_high_T = s% lnT(lnTmax_k)
    1359            0 :          if (lnTmax_at_high_T < s% delta_lgT_max_at_high_T_limit_lgT_min*ln10) return
    1360            0 :          lnTmax_at_high_T_start = maxval(s% lnT_start(1:s% nz))
    1361            0 :          change = (lnTmax_at_high_T - lnTmax_at_high_T_start)/ln10
    1362              :          check_dlgT_max_at_high_T_change = check_change(s, change, &
    1363              :             s% delta_lgT_max_at_high_T_limit, s% delta_lgT_max_at_high_T_hard_limit, &
    1364            0 :             lnTmax_k, 'delta_lgT_max_at_high_T', skip_hard_limit, dt_limit_ratio, relative_excess)
    1365            0 :          if (check_dlgT_max_at_high_T_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1366            0 :             write(*,2) 'lgT_max', lnTmax_k, lnTmax_at_high_T/ln10
    1367            0 :             write(*,2) 'lgT_max_old', lnTmax_k, lnTmax_at_high_T_start/ln10
    1368              :          end if
    1369              :       end function check_dlgT_max_at_high_T_change
    1370              : 
    1371              : 
    1372           10 :       integer function check_dlgT_cntr_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1373              :          type (star_info), pointer :: s
    1374              :          logical, intent(in) :: skip_hard_limit
    1375              :          real(dp), intent(in) :: dt
    1376              :          real(dp), intent(inout) :: dt_limit_ratio
    1377              :          real(dp) :: relative_excess, change
    1378              :          include 'formats'
    1379           10 :          check_dlgT_cntr_change = keep_going
    1380           10 :          dt_limit_ratio = 0d0
    1381           10 :          if (s% doing_relax) return
    1382           10 :          if (s% delta_lgT_cntr_limit_only_after_near_zams) then
    1383           10 :             if (s% X(s% nz) > 0.1d0 .and. &
    1384              :                 s% L_nuc_burn_total/s% L_phot < s% Lnuc_div_L_zams_limit) return
    1385              :          end if
    1386           10 :          change = (s% lnT(s% nz) - s% lnT_start(s% nz))/ln10
    1387              :          check_dlgT_cntr_change = check_change(s, change, &
    1388              :             s% delta_lgT_cntr_limit, s% delta_lgT_cntr_hard_limit, &
    1389           10 :             s% nz, 'delta_lgT_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1390           10 :          if (check_dlgT_cntr_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1391            0 :             write(*,1) 'lgT_cntr', s% lnT(s% nz)/ln10
    1392            0 :             write(*,1) 'lgT_cntr_old', s% lnT_start(s% nz)/ln10
    1393              :          end if
    1394              :       end function check_dlgT_cntr_change
    1395              : 
    1396              : 
    1397           10 :       integer function check_dlgP_cntr_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1398              :          type (star_info), pointer :: s
    1399              :          logical, intent(in) :: skip_hard_limit
    1400              :          real(dp), intent(in) :: dt
    1401              :          real(dp), intent(inout) :: dt_limit_ratio
    1402              :          real(dp) :: relative_excess, change
    1403              :          include 'formats'
    1404           10 :          check_dlgP_cntr_change = keep_going
    1405           10 :          dt_limit_ratio = 0d0
    1406           10 :          if (s% doing_relax) return
    1407           10 :          change = (s% lnPeos(s% nz) - s% lnPeos_start(s% nz))/ln10
    1408              :          check_dlgP_cntr_change = check_change(s, change, &
    1409              :             s% delta_lgP_cntr_limit, s% delta_lgP_cntr_hard_limit, &
    1410           10 :             s% nz, 'delta_lgP_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1411           10 :          if (check_dlgP_cntr_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1412            0 :             write(*,1) 'lgP_cntr', s% lnPeos(s% nz)/ln10
    1413            0 :             write(*,1) 'lgP_cntr_old', s% lnPeos_start(s% nz)/ln10
    1414              :          end if
    1415              :       end function check_dlgP_cntr_change
    1416              : 
    1417              : 
    1418           10 :       integer function check_dlgRho_cntr_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1419              :          type (star_info), pointer :: s
    1420              :          logical, intent(in) :: skip_hard_limit
    1421              :          real(dp), intent(in) :: dt
    1422              :          real(dp), intent(inout) :: dt_limit_ratio
    1423              :          real(dp) :: relative_excess, dlgRho_cntr
    1424              :          integer :: nz
    1425              :          include 'formats'
    1426           10 :          check_dlgRho_cntr_change = keep_going
    1427           10 :          dt_limit_ratio = 0d0
    1428           10 :          if (s% doing_relax) return
    1429           10 :          nz = s% nz
    1430           10 :          dlgRho_cntr = (s% lnd(nz) - s% lnd_start(nz))/ln10
    1431              :          check_dlgRho_cntr_change = check_change(s, dlgRho_cntr, &
    1432              :             s% delta_lgRho_cntr_limit, s% delta_lgRho_cntr_hard_limit, &
    1433           10 :             nz, 'delta_lgRho_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1434           10 :          if (check_dlgRho_cntr_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1435            0 :             write(*,1) 'lgRho_cntr', s% lnd(s% nz)/ln10
    1436            0 :             write(*,1) 'lgRho_cntr_old', s% lnd_start(s% nz)/ln10
    1437              :          end if
    1438              :       end function check_dlgRho_cntr_change
    1439              : 
    1440              : 
    1441           10 :       integer function check_dlog_eps_nuc_change(s, skip_hard_limit, dt, dt_limit_ratio)
    1442              :          type (star_info), pointer :: s
    1443              :          logical, intent(in) :: skip_hard_limit
    1444              :          real(dp), intent(in) :: dt
    1445              :          real(dp), intent(inout) :: dt_limit_ratio
    1446              :          real(dp) :: relative_excess, max_ratio, ratio, delta, &
    1447              :             limit_ratio, delta_r
    1448              :          integer :: k, j, nz, k_max
    1449              :          include 'formats'
    1450           10 :          check_dlog_eps_nuc_change = keep_going
    1451           10 :          nz = s% nz
    1452           10 :          limit_ratio = exp10(s% delta_log_eps_nuc_limit)
    1453           10 :          max_ratio = limit_ratio
    1454           10 :          k_max = 0
    1455        10633 :          zoneloop: do k=1,nz
    1456        10623 :             if (s% eps_nuc_start(k) < 1) cycle zoneloop
    1457         2295 :             ratio = s% eps_nuc(k)/s% eps_nuc_start(k)
    1458              :             if (s% mixing_type(k) /= convective_mixing .and. &
    1459         2295 :                 s% mixing_type(min(nz,k+1)) /= convective_mixing .and. &
    1460           10 :                 ratio > max_ratio) then
    1461          674 :                do j = 1, s% num_conv_boundaries
    1462          541 :                   delta_r = abs(s% r(s% conv_bdy_loc(j)) - s% r(k))
    1463          674 :                   if (delta_r <= s% scale_height(k)) then
    1464              :                      cycle zoneloop  ! skip ones that are too close to convection zone
    1465              :                   end if
    1466              :                end do
    1467          133 :                max_ratio = ratio
    1468          133 :                k_max = k
    1469              :             end if
    1470              :          end do zoneloop
    1471           10 :          if (k_max > 0) then
    1472           10 :             delta = log10(max_ratio)
    1473              :          else
    1474            0 :             delta = 0
    1475              :          end if
    1476              :          check_dlog_eps_nuc_change = check_change(s, delta, &
    1477              :             s% delta_log_eps_nuc_limit, s% delta_log_eps_nuc_hard_limit, &
    1478              :             k_max, 'delta_log_eps_nuc', &
    1479           10 :             skip_hard_limit, dt_limit_ratio, relative_excess)
    1480           10 :          if (check_dlog_eps_nuc_change /= keep_going .and. s% report_dt_hard_limit_retries) then
    1481            0 :             write(*,2) 'log_eps_nuc', k_max, safe_log10(abs(s% eps_nuc(k_max)))
    1482            0 :             write(*,2) 'log_eps_nuc_old', k_max, safe_log10(abs(s% eps_nuc_start(k_max)))
    1483              :          end if
    1484           10 :       end function check_dlog_eps_nuc_change
    1485              : 
    1486              : 
    1487           10 :       integer function check_dX_div_X_cntr(s, skip_hard_limit, dt_limit_ratio)
    1488              :          type (star_info), pointer :: s
    1489              :          logical, intent(in) :: skip_hard_limit
    1490              :          real(dp), intent(inout) :: dt_limit_ratio
    1491              :          real(dp) :: relative_excess, max_abs_dX_div_X, X, dX, abs_dX_div_X
    1492              :          integer :: j, nz, j_max
    1493              :          include 'formats'
    1494           10 :          check_dX_div_X_cntr = keep_going
    1495           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1496           10 :          nz = s% nz
    1497           10 :          max_abs_dX_div_X = -1
    1498           90 :          do j=1,s% species
    1499           80 :             X = s% xa(j,nz)
    1500           80 :             if (X > s% delta_dX_div_X_cntr_max) cycle
    1501            0 :             if (X < s% delta_dX_div_X_cntr_min) cycle
    1502            0 :             if (X <= 0d0) cycle
    1503            0 :             dX = X - s% xa_old(j,nz)
    1504            0 :             if (s% delta_dX_div_X_drop_only .and. dX > 0) cycle
    1505            0 :             abs_dX_div_X = abs(dX/X)
    1506           10 :             if (abs_dX_div_X > max_abs_dX_div_X) then
    1507            0 :                max_abs_dX_div_X = abs_dX_div_X
    1508            0 :                j_max = j
    1509              :             end if
    1510              :          end do
    1511           10 :          if (max_abs_dX_div_X <= 0d0) return
    1512              :          check_dX_div_X_cntr = check_change(s, max_abs_dX_div_X, &
    1513              :             s% delta_dX_div_X_cntr_limit, s% delta_dX_div_X_cntr_hard_limit, &
    1514            0 :             0, 'delta_dX_div_X_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1515            0 :          if (check_dX_div_X_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1516            0 :             write(*,1) chem_isos% name(s% chem_id(j_max)) // ' X', s% xa(j_max,nz)
    1517            0 :             write(*,1) chem_isos% name(s% chem_id(j_max)) // ' X old', s% xa_old(j_max,nz)
    1518              :          end if
    1519              :       end function check_dX_div_X_cntr
    1520              : 
    1521              : 
    1522           10 :       integer function check_lg_XH_cntr(s, skip_hard_limit, dt_limit_ratio)
    1523              :          type (star_info), pointer :: s
    1524              :          logical, intent(in) :: skip_hard_limit
    1525              :          real(dp), intent(inout) :: dt_limit_ratio
    1526              :          real(dp) :: relative_excess, lg_XH_cntr, lg_XH_cntr_old
    1527              :          integer :: h1, nz
    1528              :          include 'formats'
    1529           10 :          check_lg_XH_cntr = keep_going
    1530           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1531           10 :          h1 = s% net_iso(ih1)
    1532           10 :          if (h1 == 0) return
    1533           10 :          nz = s% nz
    1534           10 :          if (s% xa(h1,nz) < 1d-10) return
    1535           10 :          lg_XH_cntr = log10(s% xa(h1,nz))
    1536           10 :          if (lg_XH_cntr > s% delta_lg_XH_cntr_max) return
    1537           10 :          if (lg_XH_cntr < s% delta_lg_XH_cntr_min) return
    1538           10 :          lg_XH_cntr_old = safe_log10(s% xa_old(h1,nz))
    1539           10 :          if (s% delta_lg_XH_drop_only .and. lg_XH_cntr >= lg_XH_cntr_old) return
    1540              :          check_lg_XH_cntr = check_change(s, lg_XH_cntr - lg_XH_cntr_old, &
    1541              :             s% delta_lg_XH_cntr_limit, s% delta_lg_XH_cntr_hard_limit, &
    1542           10 :             nz, 'delta_lg_XH_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1543           10 :          if (check_lg_XH_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1544            0 :             write(*,1) 'lg_XH_cntr', lg_XH_cntr
    1545            0 :             write(*,1) 'lg_XH_cntr_old', lg_XH_cntr_old
    1546            0 :             write(*,1) 'delta', lg_XH_cntr - lg_XH_cntr_old
    1547              :          end if
    1548              :       end function check_lg_XH_cntr
    1549              : 
    1550              : 
    1551           10 :       integer function check_lg_XHe_cntr(s, skip_hard_limit, dt_limit_ratio)
    1552              :          type (star_info), pointer :: s
    1553              :          logical, intent(in) :: skip_hard_limit
    1554              :          real(dp), intent(inout) :: dt_limit_ratio
    1555              :          real(dp) :: relative_excess, lg_XHe_cntr, lg_XHe_cntr_old
    1556              :          integer :: h1, he4, nz
    1557              :          real(dp) :: xh1, xhe4
    1558              :          include 'formats'
    1559           10 :          check_lg_XHe_cntr = keep_going
    1560           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1561           10 :          h1 = s% net_iso(ih1)
    1562           10 :          he4 = s% net_iso(ihe4)
    1563           10 :          if (h1 == 0 .or. he4 == 0) return
    1564           10 :          nz = s% nz
    1565           10 :          xh1 = s% xa(h1,nz)
    1566           10 :          xhe4 = s% xa(he4,nz)
    1567           10 :          if (xhe4 < max(xh1, 1d-10)) return
    1568            0 :          lg_XHe_cntr = log10(xhe4)
    1569            0 :          if (lg_XHe_cntr > s% delta_lg_XHe_cntr_max) return
    1570            0 :          if (lg_XHe_cntr < s% delta_lg_XHe_cntr_min) return
    1571            0 :          lg_XHe_cntr_old = safe_log10(s% xa_old(he4,nz))
    1572            0 :          if (s% delta_lg_XHe_drop_only .and. lg_XHe_cntr >= lg_XHe_cntr_old) return
    1573              :          check_lg_XHe_cntr = check_change(s, lg_XHe_cntr - lg_XHe_cntr_old, &
    1574              :             s% delta_lg_XHe_cntr_limit, s% delta_lg_XHe_cntr_hard_limit, &
    1575            0 :             nz, 'delta_lg_XHe_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1576            0 :          if (check_lg_XHe_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1577            0 :             write(*,1) 'lg_XHe_cntr', lg_XHe_cntr
    1578            0 :             write(*,1) 'lg_XHe_cntr_old', lg_XHe_cntr_old
    1579            0 :             write(*,1) 'delta', lg_XHe_cntr - lg_XHe_cntr_old
    1580              :          end if
    1581              :       end function check_lg_XHe_cntr
    1582              : 
    1583              : 
    1584           10 :       integer function check_lg_XC_cntr(s, skip_hard_limit, dt_limit_ratio)
    1585              :          type (star_info), pointer :: s
    1586              :          logical, intent(in) :: skip_hard_limit
    1587              :          real(dp), intent(inout) :: dt_limit_ratio
    1588              :          real(dp) :: relative_excess, lg_XC_cntr, lg_XC_cntr_old
    1589              :          integer :: h1, he4, c12, nz
    1590              :          real(dp) :: xh1, xhe4, xc12
    1591              :          include 'formats'
    1592           10 :          check_lg_XC_cntr = keep_going
    1593           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1594           10 :          h1 = s% net_iso(ih1)
    1595           10 :          he4 = s% net_iso(ihe4)
    1596           10 :          c12 = s% net_iso(ic12)
    1597           10 :          if (h1 == 0 .or. he4 == 0 .or. c12 == 0) return
    1598           10 :          nz = s% nz
    1599           10 :          xh1 = s% xa(h1,nz)
    1600           10 :          xhe4 = s% xa(he4,nz)
    1601           10 :          xc12 = s% xa(c12,nz)
    1602           10 :          if (xc12 < max(xh1, xhe4, 1d-10)) return
    1603            0 :          if (s% xa(c12,nz) < 1d-10) return
    1604            0 :          lg_XC_cntr = log10(xc12)
    1605            0 :          if (lg_XC_cntr > s% delta_lg_XC_cntr_max) return
    1606            0 :          if (lg_XC_cntr < s% delta_lg_XC_cntr_min) return
    1607            0 :          lg_XC_cntr_old = safe_log10(s% xa_old(c12,nz))
    1608            0 :          if (s% delta_lg_XC_drop_only .and. lg_XC_cntr >= lg_XC_cntr_old) return
    1609              :          check_lg_XC_cntr = check_change(s, lg_XC_cntr - lg_XC_cntr_old, &
    1610              :             s% delta_lg_XC_cntr_limit, s% delta_lg_XC_cntr_hard_limit, &
    1611            0 :             nz, 'delta_lg_XC_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1612            0 :          if (check_lg_XC_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1613            0 :             write(*,1) 'lg_XC_cntr', lg_XC_cntr
    1614            0 :             write(*,1) 'lg_XC_cntr_old', lg_XC_cntr_old
    1615            0 :             write(*,1) 'delta', lg_XC_cntr - lg_XC_cntr_old
    1616              :          end if
    1617              :       end function check_lg_XC_cntr
    1618              : 
    1619              : 
    1620           10 :       integer function check_lg_XNe_cntr(s, skip_hard_limit, dt_limit_ratio)
    1621              :          type (star_info), pointer :: s
    1622              :          logical, intent(in) :: skip_hard_limit
    1623              :          real(dp), intent(inout) :: dt_limit_ratio
    1624              :          real(dp) :: relative_excess, lg_XNe_cntr, lg_XNe_cntr_old
    1625              :          integer :: h1, he4, c12, ne20, nz
    1626              :          real(dp) :: xh1, xhe4, xc12, xne20
    1627              :          include 'formats'
    1628           10 :          check_lg_XNe_cntr = keep_going
    1629           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1630           10 :          h1 = s% net_iso(ih1)
    1631           10 :          he4 = s% net_iso(ihe4)
    1632           10 :          c12 = s% net_iso(ic12)
    1633           10 :          ne20 = s% net_iso(ine20)
    1634           10 :          if (h1 == 0 .or. he4 == 0 .or. c12 == 0 .or. ne20 == 0) return
    1635           10 :          nz = s% nz
    1636           10 :          xh1 = s% xa(h1,nz)
    1637           10 :          xhe4 = s% xa(he4,nz)
    1638           10 :          xc12 = s% xa(c12,nz)
    1639           10 :          xne20 = s% xa(ne20,nz)
    1640           10 :          if (xne20 < max(xh1, xhe4, xc12, 1d-10)) return
    1641            0 :          lg_XNe_cntr = log10(xne20)
    1642            0 :          if (lg_XNe_cntr > s% delta_lg_XNe_cntr_max) return
    1643            0 :          if (lg_XNe_cntr < s% delta_lg_XNe_cntr_min) return
    1644            0 :          lg_XNe_cntr_old = safe_log10(s% xa_old(ne20,nz))
    1645            0 :          if (s% delta_lg_XNe_drop_only .and. lg_XNe_cntr >= lg_XNe_cntr_old) return
    1646              :          check_lg_XNe_cntr = check_change(s, lg_XNe_cntr - lg_XNe_cntr_old, &
    1647              :             s% delta_lg_XNe_cntr_limit, s% delta_lg_XNe_cntr_hard_limit, &
    1648            0 :             nz, 'delta_lg_XNe_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1649            0 :          if (check_lg_XNe_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1650            0 :             write(*,1) 'lg_XNe_cntr', lg_XNe_cntr
    1651            0 :             write(*,1) 'lg_XNe_cntr_old', lg_XNe_cntr_old
    1652            0 :             write(*,1) 'delta', lg_XNe_cntr - lg_XNe_cntr_old
    1653              :          end if
    1654              :       end function check_lg_XNe_cntr
    1655              : 
    1656              : 
    1657           10 :       integer function check_lg_XO_cntr(s, skip_hard_limit, dt_limit_ratio)
    1658              :          type (star_info), pointer :: s
    1659              :          logical, intent(in) :: skip_hard_limit
    1660              :          real(dp), intent(inout) :: dt_limit_ratio
    1661              :          real(dp) :: relative_excess, lg_XO_cntr, lg_XO_cntr_old
    1662              :          integer :: h1, he4, c12, o16, nz
    1663              :          real(dp) :: xh1, xhe4, xc12, xo16
    1664              :          include 'formats'
    1665           10 :          check_lg_XO_cntr = keep_going
    1666           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1667           10 :          h1 = s% net_iso(ih1)
    1668           10 :          he4 = s% net_iso(ihe4)
    1669           10 :          c12 = s% net_iso(ic12)
    1670           10 :          o16 = s% net_iso(io16)
    1671           10 :          if (h1 == 0 .or. he4 == 0 .or. c12 == 0 .or. o16 == 0) return
    1672           10 :          nz = s% nz
    1673           10 :          xh1 = s% xa(h1,nz)
    1674           10 :          xhe4 = s% xa(he4,nz)
    1675           10 :          xc12 = s% xa(c12,nz)
    1676           10 :          xo16 = s% xa(o16,nz)
    1677           10 :          if (xo16 < max(xh1, xhe4, xc12, 1d-10)) return
    1678            0 :          lg_XO_cntr = log10(xo16)
    1679            0 :          if (lg_XO_cntr > s% delta_lg_XO_cntr_max) return
    1680            0 :          if (lg_XO_cntr < s% delta_lg_XO_cntr_min) return
    1681            0 :          lg_XO_cntr_old = safe_log10(s% xa_old(o16,nz))
    1682            0 :          if (s% delta_lg_XO_drop_only .and. lg_XO_cntr >= lg_XO_cntr_old) return
    1683              :          check_lg_XO_cntr = check_change(s, lg_XO_cntr - lg_XO_cntr_old, &
    1684              :             s% delta_lg_XO_cntr_limit, s% delta_lg_XO_cntr_hard_limit, &
    1685            0 :             nz, 'delta_lg_XO_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1686            0 :          if (check_lg_XO_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1687            0 :             write(*,1) 'lg_XO_cntr', lg_XO_cntr
    1688            0 :             write(*,1) 'lg_XO_cntr_old', lg_XO_cntr_old
    1689            0 :             write(*,1) 'delta', lg_XO_cntr - lg_XO_cntr_old
    1690              :          end if
    1691              :       end function check_lg_XO_cntr
    1692              : 
    1693              : 
    1694           10 :       integer function check_lg_XSi_cntr(s, skip_hard_limit, dt_limit_ratio)
    1695              :          type (star_info), pointer :: s
    1696              :          logical, intent(in) :: skip_hard_limit
    1697              :          real(dp), intent(inout) :: dt_limit_ratio
    1698              :          real(dp) :: relative_excess, lg_XSi_cntr, lg_XSi_cntr_old
    1699              :          integer :: h1, he4, c12, si28, nz
    1700              :          real(dp) :: xh1, xhe4, xc12, xsi28
    1701              :          include 'formats'
    1702           10 :          check_lg_XSi_cntr = keep_going
    1703           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1704           10 :          h1 = s% net_iso(ih1)
    1705           10 :          he4 = s% net_iso(ihe4)
    1706           10 :          c12 = s% net_iso(ic12)
    1707           10 :          si28 = s% net_iso(isi28)
    1708           10 :          if (h1 == 0 .or. he4 == 0 .or. c12 == 0 .or. si28 == 0) return
    1709            0 :          nz = s% nz
    1710            0 :          xh1 = s% xa(h1,nz)
    1711            0 :          xhe4 = s% xa(he4,nz)
    1712            0 :          xc12 = s% xa(c12,nz)
    1713            0 :          xsi28 = s% xa(si28,nz)
    1714            0 :          if (xsi28 < max(xh1, xhe4, xc12, 1d-10)) return
    1715            0 :          lg_XSi_cntr = log10(xsi28)
    1716            0 :          if (lg_XSi_cntr > s% delta_lg_XSi_cntr_max) return
    1717            0 :          if (lg_XSi_cntr < s% delta_lg_XSi_cntr_min) return
    1718            0 :          lg_XSi_cntr_old = safe_log10(s% xa_old(si28,nz))
    1719            0 :          if (s% delta_lg_XSi_drop_only .and. lg_XSi_cntr >= lg_XSi_cntr_old) return
    1720              :          check_lg_XSi_cntr = check_change(s, lg_XSi_cntr - lg_XSi_cntr_old, &
    1721              :             s% delta_lg_XSi_cntr_limit, s% delta_lg_XSi_cntr_hard_limit, &
    1722            0 :             nz, 'delta_lg_XSi_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1723            0 :          if (check_lg_XSi_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1724            0 :             write(*,1) 'lg_XSi_cntr', lg_XSi_cntr
    1725            0 :             write(*,1) 'lg_XSi_cntr_old', lg_XSi_cntr_old
    1726            0 :             write(*,1) 'delta', lg_XSi_cntr - lg_XSi_cntr_old
    1727              :          end if
    1728              :       end function check_lg_XSi_cntr
    1729              : 
    1730              : 
    1731           10 :       integer function check_XH_cntr(s, skip_hard_limit, dt_limit_ratio)
    1732              :          type (star_info), pointer :: s
    1733              :          logical, intent(in) :: skip_hard_limit
    1734              :          real(dp), intent(inout) :: dt_limit_ratio
    1735              :          real(dp) :: relative_excess, XH_cntr, XH_cntr_old
    1736              :          integer :: h1, nz
    1737              :          include 'formats'
    1738           10 :          check_XH_cntr = keep_going
    1739           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1740           10 :          h1 = s% net_iso(ih1)
    1741           10 :          if (h1 == 0) return
    1742           10 :          nz = s% nz
    1743           10 :          XH_cntr = s% xa(h1,nz)
    1744           10 :          XH_cntr_old = s% xa_old(h1,nz)
    1745           10 :          if (s% delta_XH_drop_only .and. XH_cntr >= XH_cntr_old) return
    1746              :          check_XH_cntr = check_change(s, XH_cntr - XH_cntr_old, &
    1747              :             s% delta_XH_cntr_limit, s% delta_XH_cntr_hard_limit, &
    1748           10 :             nz, 'delta_XH_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1749           10 :          if (check_XH_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1750            0 :             write(*,1) 'XH_cntr', XH_cntr
    1751            0 :             write(*,1) 'XH_cntr_old', XH_cntr_old
    1752            0 :             write(*,1) 'delta', XH_cntr - XH_cntr_old
    1753              :          end if
    1754              :       end function check_XH_cntr
    1755              : 
    1756              : 
    1757           10 :       integer function check_XHe_cntr(s, skip_hard_limit, dt_limit_ratio)
    1758              :          type (star_info), pointer :: s
    1759              :          logical, intent(in) :: skip_hard_limit
    1760              :          real(dp), intent(inout) :: dt_limit_ratio
    1761              :          real(dp) :: relative_excess, XHe_cntr, XHe_cntr_old
    1762              :          integer :: he4, nz
    1763              :          include 'formats'
    1764           10 :          check_XHe_cntr = keep_going
    1765           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1766           10 :          he4 = s% net_iso(ihe4)
    1767           10 :          if (he4 == 0) return
    1768           10 :          nz = s% nz
    1769           10 :          XHe_cntr = s% xa(he4,nz)
    1770           10 :          XHe_cntr_old = s% xa_old(he4,nz)
    1771           10 :          if (s% delta_XHe_drop_only .and. XHe_cntr >= XHe_cntr_old) return
    1772              :          check_XHe_cntr = check_change(s, XHe_cntr - XHe_cntr_old, &
    1773              :             s% delta_XHe_cntr_limit, s% delta_XHe_cntr_hard_limit, &
    1774           10 :             nz, 'delta_XHe_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1775           10 :          if (check_XHe_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1776            0 :             write(*,1) 'XHe_cntr', XHe_cntr
    1777            0 :             write(*,1) 'XHe_cntr_old', XHe_cntr_old
    1778            0 :             write(*,1) 'delta', XHe_cntr - XHe_cntr_old
    1779              :          end if
    1780              :       end function check_XHe_cntr
    1781              : 
    1782              : 
    1783           10 :       integer function check_XC_cntr(s, skip_hard_limit, dt_limit_ratio)
    1784              :          type (star_info), pointer :: s
    1785              :          logical, intent(in) :: skip_hard_limit
    1786              :          real(dp), intent(inout) :: dt_limit_ratio
    1787              :          real(dp) :: relative_excess, XC_cntr, XC_cntr_old
    1788              :          integer :: c12, nz
    1789              :          include 'formats'
    1790           10 :          check_XC_cntr = keep_going
    1791           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1792           10 :          c12 = s% net_iso(ic12)
    1793           10 :          if (c12 == 0) return
    1794           10 :          nz = s% nz
    1795           10 :          XC_cntr = s% xa(c12,nz)
    1796           10 :          XC_cntr_old = s% xa_old(c12,nz)
    1797           10 :          if (s% delta_XC_drop_only .and. XC_cntr >= XC_cntr_old) return
    1798              :          check_XC_cntr = check_change(s, XC_cntr - XC_cntr_old, &
    1799              :             s% delta_XC_cntr_limit, s% delta_XC_cntr_hard_limit, &
    1800           10 :             nz, 'delta_XC_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1801           10 :          if (check_XC_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1802            0 :             write(*,1) 'XC_cntr', XC_cntr
    1803            0 :             write(*,1) 'XC_cntr_old', XC_cntr_old
    1804            0 :             write(*,1) 'delta', XC_cntr - XC_cntr_old
    1805              :          end if
    1806              :       end function check_XC_cntr
    1807              : 
    1808              : 
    1809           10 :       integer function check_XNe_cntr(s, skip_hard_limit, dt_limit_ratio)
    1810              :          type (star_info), pointer :: s
    1811              :          logical, intent(in) :: skip_hard_limit
    1812              :          real(dp), intent(inout) :: dt_limit_ratio
    1813              :          real(dp) :: relative_excess, XNe_cntr, XNe_cntr_old
    1814              :          integer :: ne20, nz
    1815              :          include 'formats'
    1816           10 :          check_XNe_cntr = keep_going
    1817           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1818           10 :          ne20 = s% net_iso(ine20)
    1819           10 :          if (ne20 == 0) return
    1820           10 :          nz = s% nz
    1821           10 :          XNe_cntr = s% xa(ne20,nz)
    1822           10 :          XNe_cntr_old = s% xa_old(ne20,nz)
    1823           10 :          if (s% delta_XNe_drop_only .and. XNe_cntr >= XNe_cntr_old) return
    1824              :          check_XNe_cntr = check_change(s, XNe_cntr - XNe_cntr_old, &
    1825              :             s% delta_XNe_cntr_limit, s% delta_XNe_cntr_hard_limit, &
    1826           10 :             nz, 'delta_XNe_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1827           10 :          if (check_XNe_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1828            0 :             write(*,1) 'XNe_cntr', XNe_cntr
    1829            0 :             write(*,1) 'XNe_cntr_old', XNe_cntr_old
    1830            0 :             write(*,1) 'delta', XNe_cntr - XNe_cntr_old
    1831              :          end if
    1832              :       end function check_XNe_cntr
    1833              : 
    1834              : 
    1835           10 :       integer function check_XO_cntr(s, skip_hard_limit, dt_limit_ratio)
    1836              :          type (star_info), pointer :: s
    1837              :          logical, intent(in) :: skip_hard_limit
    1838              :          real(dp), intent(inout) :: dt_limit_ratio
    1839              :          real(dp) :: relative_excess, XO_cntr, XO_cntr_old
    1840              :          integer :: o16, nz
    1841              :          include 'formats'
    1842           10 :          check_XO_cntr = keep_going
    1843           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1844           10 :          o16 = s% net_iso(io16)
    1845           10 :          if (o16 == 0) return
    1846           10 :          nz = s% nz
    1847           10 :          XO_cntr = s% xa(o16,nz)
    1848           10 :          XO_cntr_old = s% xa_old(o16,nz)
    1849           10 :          if (s% delta_XO_drop_only .and. XO_cntr >= XO_cntr_old) return
    1850              :          check_XO_cntr = check_change(s, XO_cntr - XO_cntr_old, &
    1851              :             s% delta_XO_cntr_limit, s% delta_XO_cntr_hard_limit, &
    1852           10 :             nz, 'delta_XO_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1853           10 :          if (check_XO_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1854            0 :             write(*,1) 'XO_cntr', XO_cntr
    1855            0 :             write(*,1) 'XO_cntr_old', XO_cntr_old
    1856            0 :             write(*,1) 'delta', XO_cntr - XO_cntr_old
    1857              :          end if
    1858              :       end function check_XO_cntr
    1859              : 
    1860              : 
    1861           10 :       integer function check_XSi_cntr(s, skip_hard_limit, dt_limit_ratio)
    1862              :          type (star_info), pointer :: s
    1863              :          logical, intent(in) :: skip_hard_limit
    1864              :          real(dp), intent(inout) :: dt_limit_ratio
    1865              :          real(dp) :: relative_excess, XSi_cntr, XSi_cntr_old
    1866              :          integer :: si28, nz
    1867              :          include 'formats'
    1868           10 :          check_XSi_cntr = keep_going
    1869           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    1870           10 :          si28 = s% net_iso(isi28)
    1871           10 :          if (si28 == 0) return
    1872            0 :          nz = s% nz
    1873            0 :          XSi_cntr = s% xa(si28,nz)
    1874            0 :          XSi_cntr_old = s% xa_old(si28,nz)
    1875            0 :          if (s% delta_XSi_drop_only .and. XSi_cntr >= XSi_cntr_old) return
    1876              :          check_XSi_cntr = check_change(s, XSi_cntr - XSi_cntr_old, &
    1877              :             s% delta_XSi_cntr_limit, s% delta_XSi_cntr_hard_limit, &
    1878            0 :             nz, 'delta_XSi_cntr', skip_hard_limit, dt_limit_ratio, relative_excess)
    1879            0 :          if (check_XSi_cntr /= keep_going .and. s% report_dt_hard_limit_retries) then
    1880            0 :             write(*,1) 'XSi_cntr', XSi_cntr
    1881            0 :             write(*,1) 'XSi_cntr_old', XSi_cntr_old
    1882            0 :             write(*,1) 'delta', XSi_cntr - XSi_cntr_old
    1883              :          end if
    1884              :       end function check_XSi_cntr
    1885              : 
    1886              : 
    1887           10 :       integer function check_delta_mdot(s, skip_hard_limit, dt, dt_limit_ratio)
    1888              :          type (star_info), pointer :: s
    1889              :          logical, intent(in) :: skip_hard_limit
    1890              :          real(dp), intent(in) :: dt
    1891              :          real(dp), intent(inout) :: dt_limit_ratio
    1892              :          real(dp) :: mdot, mdot_old, delta_mdot, lim, hard_lim
    1893           10 :          check_delta_mdot = keep_going
    1894           10 :          mdot = s% mstar_dot
    1895           10 :          mdot_old = s% mstar_dot_old
    1896              :          delta_mdot = abs(mdot - mdot_old)/ &
    1897              :             (s% delta_mdot_atol*Msun/secyer + &
    1898           10 :                s% delta_mdot_rtol*max(abs(mdot),abs(mdot_old)))
    1899           10 :          if (delta_mdot == 0) return
    1900            0 :          lim = s% delta_mdot_limit*s% time_delta_coeff
    1901            0 :          hard_lim = s% delta_mdot_hard_limit*s% time_delta_coeff
    1902            0 :          if (hard_lim > 0 .and. (.not. skip_hard_limit)) then
    1903            0 :             if (delta_mdot > hard_lim) then
    1904            0 :                if (s% report_dt_hard_limit_retries) &
    1905            0 :                   write(*, '(a30, f20.10, 99e20.10)') 'delta_mdot_hard_limit', &
    1906            0 :                      delta_mdot, hard_lim
    1907            0 :                s% retry_message = 'delta_mdot_hard_limit'
    1908            0 :                check_delta_mdot = retry
    1909            0 :                return
    1910              :             end if
    1911              :          end if
    1912            0 :          if (lim <= 0) return
    1913            0 :          dt_limit_ratio = delta_mdot/lim
    1914            0 :          if (dt_limit_ratio <= 1d0) dt_limit_ratio = 0
    1915              :       end function check_delta_mdot
    1916              : 
    1917              : 
    1918           10 :       integer function check_delta_mstar(s, skip_hard_limit, dt, dt_limit_ratio)
    1919              :          type (star_info), pointer :: s
    1920              :          logical, intent(in) :: skip_hard_limit
    1921              :          real(dp), intent(in) :: dt
    1922              :          real(dp), intent(inout) :: dt_limit_ratio
    1923              :          real(dp) :: delta_lg_star_mass, lim, hard_lim
    1924           10 :          check_delta_mstar = keep_going
    1925           10 :          delta_lg_star_mass = abs(log10(s% mstar/s% mstar_old))
    1926           10 :          lim = s% delta_lg_star_mass_limit*s% time_delta_coeff
    1927           10 :          hard_lim = s% delta_lg_star_mass_hard_limit*s% time_delta_coeff
    1928           10 :          if (hard_lim > 0 .and. (.not. skip_hard_limit)) then
    1929            0 :             if (delta_lg_star_mass > hard_lim) then
    1930            0 :                if (s% report_dt_hard_limit_retries) &
    1931            0 :                   write(*, '(a30, f20.10, 99e20.10)') 'delta_lg_star_mass_hard_limit', &
    1932            0 :                      delta_lg_star_mass, hard_lim
    1933            0 :                check_delta_mstar = retry
    1934            0 :                s% retry_message = 'delta_lg_star_mass_hard_limit'
    1935            0 :                return
    1936              :             end if
    1937              :          end if
    1938           10 :          if (lim <= 0) return
    1939           10 :          dt_limit_ratio = delta_lg_star_mass/lim
    1940           10 :          if (dt_limit_ratio <= 1d0) dt_limit_ratio = 0
    1941              :       end function check_delta_mstar
    1942              : 
    1943              : 
    1944           10 :       integer function check_adjust_J_q(s, skip_hard_limit, dt_limit_ratio)
    1945              :          type (star_info), pointer :: s
    1946              :          logical, intent(in) :: skip_hard_limit
    1947              :          real(dp), intent(inout) :: dt_limit_ratio
    1948              :          real(dp) :: relative_excess
    1949              :          include 'formats'
    1950           10 :          check_adjust_J_q = keep_going
    1951           10 :          dt_limit_ratio = 0d0
    1952           10 :          if (s% doing_relax) return
    1953           10 :          if (.not. (s% rotation_flag .and. s% do_adjust_J_lost .and. s% mstar_dot < 0d0)) return
    1954              :          ! we care about s% adjust_J_q remaining above a given limit
    1955              :          ! so we use 1-S% adjust_J_q
    1956              :          check_adjust_J_q = check_change(s, 1-s% adjust_J_q, &
    1957              :             1-s% adjust_J_q_limit, &
    1958              :             1-s% adjust_J_q_hard_limit, &
    1959              :             0, 'check_adjust_J_q', &
    1960            0 :             .false., dt_limit_ratio, relative_excess)
    1961            0 :       end function check_adjust_J_q
    1962              : 
    1963              : 
    1964           10 :       integer function check_delta_lgL(s, skip_hard_limit, dt_limit_ratio)
    1965              :          type (star_info), pointer :: s
    1966              :          logical, intent(in) :: skip_hard_limit
    1967              :          real(dp), intent(inout) :: dt_limit_ratio
    1968              :          real(dp) :: dlgL, relative_excess
    1969              :          include 'formats'
    1970           10 :          check_delta_lgL = keep_going
    1971           10 :          dt_limit_ratio = 0d0
    1972           10 :          if (s% doing_relax) return
    1973           10 :          if (s% L_surf < s% delta_lgL_limit_L_min .or. s% L_surf_old <= 0d0) then
    1974            0 :             dlgL = 0
    1975              :          else
    1976           10 :             dlgL = log10(s% L_surf/s% L_surf_old)
    1977              :          end if
    1978           10 :          if (is_bad(dlgL)) then
    1979            0 :             write(*,2) 's% L_surf', s% model_number, s% L_surf
    1980            0 :             write(*,2) 's% L_surf_old', s% model_number, s% L_surf_old
    1981            0 :             call mesa_error(__FILE__,__LINE__,'check_delta_lgL')
    1982              :          end if
    1983              :          check_delta_lgL = check_change(s, dlgL, &
    1984              :             s% delta_lgL_limit, s% delta_lgL_hard_limit, &
    1985           10 :             0, 'delta_lgL', skip_hard_limit, dt_limit_ratio, relative_excess)
    1986           10 :          if (check_delta_lgL /= keep_going .and. s% report_dt_hard_limit_retries) then
    1987            0 :             write(*,1) 'L_surf', s% L_surf
    1988            0 :             write(*,1) 'L_surf_old', s% L_surf_old
    1989              :          end if
    1990              :       end function check_delta_lgL
    1991              : 
    1992              : 
    1993           10 :       integer function check_delta_HR(s, skip_hard_limit, dt_limit_ratio)
    1994              :          type (star_info), pointer :: s
    1995              :          logical, intent(in) :: skip_hard_limit
    1996              :          real(dp), intent(inout) :: dt_limit_ratio
    1997              :          real(dp) :: dlgL, dlgTeff, dHR, relative_excess
    1998              :          include 'formats'
    1999           10 :          check_delta_HR = keep_going
    2000              :          if (s% L_phot_old <= 0 .or. s% Teff_old <= 0 .or. &
    2001           10 :              s% L_phot <= 0 .or. s% Teff <= 0) return
    2002           10 :          dlgL = log10(s% L_phot/s% L_phot_old)
    2003           10 :          dlgTeff = log10(s% Teff/s% Teff_old)
    2004           10 :          dHR = sqrt(pow2(s% delta_HR_ds_L*dlgL) + pow2(s% delta_HR_ds_Teff*dlgTeff))
    2005           10 :          if (is_bad(dHR)) then
    2006            0 :             write(*,1) 's% L_phot_old', s% L_phot_old
    2007            0 :             write(*,1) 's% L_phot', s% L_phot
    2008            0 :             write(*,1) 's% Teff_old', s% Teff_old
    2009            0 :             write(*,1) 's% Teff', s% Teff
    2010            0 :             write(*,1) 'dHR', dHR
    2011            0 :             if (s% stop_for_bad_nums) call mesa_error(__FILE__,__LINE__,'check_delta_HR')
    2012              :          end if
    2013              :          check_delta_HR = check_change(s, dHR, &
    2014              :             s% delta_HR_limit, s% delta_HR_hard_limit, &
    2015           10 :             0, 'delta_HR', skip_hard_limit, dt_limit_ratio, relative_excess)
    2016           10 :          if (check_delta_HR /= keep_going .and. s% report_dt_hard_limit_retries) then
    2017            0 :             write(*,1) 's% L_phot_old', s% L_phot_old
    2018            0 :             write(*,1) 's% L_phot', s% L_phot
    2019            0 :             write(*,1) 's% Teff_old', s% Teff_old
    2020            0 :             write(*,1) 's% Teff', s% Teff
    2021            0 :             write(*,1) 'dHR', dHR
    2022              :          end if
    2023              :       end function check_delta_HR
    2024              : 
    2025              : 
    2026           10 :       integer function check_rel_error_in_energy(s, skip_hard_limit, dt_limit_ratio)
    2027              :          type (star_info), pointer :: s
    2028              :          logical, intent(in) :: skip_hard_limit
    2029              :          real(dp), intent(inout) :: dt_limit_ratio
    2030              :          real(dp) :: rel_error, relative_excess
    2031              :          include 'formats'
    2032           10 :          check_rel_error_in_energy = keep_going
    2033           10 :          dt_limit_ratio = 0d0
    2034           10 :          if (s% doing_relax) return
    2035           10 :          rel_error = abs(s% error_in_energy_conservation/s% total_energy_end)
    2036              :          check_rel_error_in_energy = check_change(s, rel_error, &
    2037              :             s% limit_for_rel_error_in_energy_conservation, &
    2038              :             s% hard_limit_for_rel_error_in_energy_conservation, &
    2039              :             0, 'hard_limit_for_rel_error_in_energy_conservation', &
    2040           10 :             .false., dt_limit_ratio, relative_excess)
    2041           10 :          if (check_rel_error_in_energy /= keep_going .and. s% report_dt_hard_limit_retries) then
    2042            0 :             write(*,1) 'error_in_energy_conservation', s% error_in_energy_conservation
    2043            0 :             write(*,1) 'total_energy_end', s% total_energy_end
    2044            0 :             write(*,1) 'rel_error', s% error_in_energy_conservation/s% total_energy_end
    2045              :          end if
    2046              :       end function check_rel_error_in_energy
    2047              : 
    2048              : 
    2049           10 :       integer function check_dt_div_dt_cell_collapse(s, skip_hard_limit, dt, dt_limit_ratio)
    2050              :          use star_utils, only: eval_min_cell_collapse_time
    2051              :          type (star_info), pointer :: s
    2052              :          logical, intent(in) :: skip_hard_limit
    2053              :          real(dp), intent(in) :: dt
    2054              :          real(dp), intent(inout) :: dt_limit_ratio
    2055              :          real(dp) :: ratio, dt_timescale, relative_excess
    2056              :          integer :: min_collapse_k, ierr
    2057              :          include 'formats'
    2058           10 :          check_dt_div_dt_cell_collapse = keep_going
    2059           10 :          if (s% doing_relax) return
    2060              :          dt_timescale = eval_min_cell_collapse_time( &
    2061           10 :             s, 2, s% nz, min_collapse_k, ierr)
    2062           10 :          if (ierr /= 0) return
    2063           10 :          if (dt_timescale < 1d-30) return
    2064           10 :          ratio = dt/dt_timescale
    2065              :          !write(*,2) 'dt dt_cell_collapse ratio', min_collapse_k, dt, dt_timescale, ratio
    2066              :          check_dt_div_dt_cell_collapse = check_change(s, ratio, &
    2067              :             s% dt_div_dt_cell_collapse_limit, s% dt_div_dt_cell_collapse_hard_limit, &
    2068           10 :             min_collapse_k, 'dt_div_dt_cell_collapse', skip_hard_limit, dt_limit_ratio, relative_excess)
    2069           10 :          if (check_dt_div_dt_cell_collapse /= keep_going .and. s% report_dt_hard_limit_retries) then
    2070            0 :             write(*,2) 'min dt_cell_collapse', min_collapse_k, dt_timescale
    2071              :          end if
    2072              :       end function check_dt_div_dt_cell_collapse
    2073              : 
    2074              : 
    2075           10 :       integer function check_dt_div_min_dr_div_cs(s, skip_hard_limit, dt, dt_limit_ratio)
    2076              :          use star_utils, only: min_dr_div_cs
    2077              :          type (star_info), pointer :: s
    2078              :          logical, intent(in) :: skip_hard_limit
    2079              :          real(dp), intent(in) :: dt
    2080              :          real(dp), intent(inout) :: dt_limit_ratio
    2081              :          real(dp) :: ratio, dt_x, relative_excess
    2082              :          include 'formats'
    2083           10 :          check_dt_div_min_dr_div_cs = keep_going
    2084           10 :          dt_limit_ratio = 0d0
    2085           10 :          if (s% doing_relax) return
    2086           10 :          if (s% dt_div_min_dr_div_cs_limit <= 0d0) return
    2087            0 :          dt_x = min_dr_div_cs(s, s% Tlim_dt_div_min_dr_div_cs_cell)
    2088              :          !write(*,2) 'log min_dr_div_cs, q, m', s% Tlim_dt_div_min_dr_div_cs_cell, &
    2089              :          !   safe_log10(dt_x), s% q(s% Tlim_dt_div_min_dr_div_cs_cell), s% m(s% Tlim_dt_div_min_dr_div_cs_cell)/Msun
    2090            0 :          ratio = dt/dt_x
    2091              :          !write(*,3) 'dt/dx_x log', s% Tlim_dt_div_min_dr_div_cs_cell, s% model_number, ratio, safe_log10(ratio)
    2092              :          check_dt_div_min_dr_div_cs = check_change(s, ratio, &
    2093              :             s% dt_div_min_dr_div_cs_limit, s% dt_div_min_dr_div_cs_hard_limit, &
    2094              :             s% Tlim_dt_div_min_dr_div_cs_cell, 'dt_div_min_dr_div_cs', &
    2095            0 :             skip_hard_limit, dt_limit_ratio, relative_excess)
    2096            0 :          if ((check_dt_div_min_dr_div_cs /= keep_going .and. s% report_dt_hard_limit_retries) .or. &
    2097              :              (ratio > 1d0 .and. s% report_min_dr_div_cs)) then
    2098            0 :             write(*,2) 'min_dr_div_cs', s% Tlim_dt_div_min_dr_div_cs_cell, dt_x
    2099              :          end if
    2100              :       end function check_dt_div_min_dr_div_cs
    2101              : 
    2102              : 
    2103           10 :       integer function check_dX_nuc_drop(s, skip_hard_limit, dt, dt_limit_ratio)
    2104              :          use rates_def, only: i_rate
    2105              :          type (star_info), pointer :: s
    2106              :          logical, intent(in) :: skip_hard_limit
    2107              :          real(dp), intent(in) :: dt
    2108              :          real(dp), intent(inout) :: dt_limit_ratio
    2109              : 
    2110              :          integer :: k, nz, max_k, max_j
    2111           10 :          real(dp), pointer, dimension(:) :: sig
    2112              :          real(dp) :: max_dx_nuc_drop, X_limit, A_limit, min_dt, limit, hard_limit
    2113              : 
    2114              :          logical, parameter :: dbg = .false.
    2115              : 
    2116              :          include 'formats'
    2117              : 
    2118           10 :          check_dX_nuc_drop = keep_going
    2119           10 :          if (s% mix_factor == 0d0 .and. s% dxdt_nuc_factor == 0d0) return
    2120              : 
    2121           10 :          X_limit = s% dX_nuc_drop_min_X_limit
    2122           10 :          A_limit = s% dX_nuc_drop_max_A_limit
    2123           10 :          nz = s% nz
    2124           10 :          sig => s% sig
    2125              : 
    2126           10 :          max_dx_nuc_drop = 0
    2127           10 :          max_k = 0
    2128           10 :          max_j = 0
    2129        10633 :          do k = 1, nz
    2130        10633 :             call do1(k)
    2131              :          end do
    2132              : 
    2133           10 :          s% Tlim_dXnuc_drop_cell = max_k
    2134           10 :          s% Tlim_dXnuc_drop_species = max_j
    2135              : 
    2136           10 :          hard_limit = s% dX_nuc_drop_hard_limit*s% time_delta_coeff
    2137           10 :          if (hard_limit > 0 .and. (.not. skip_hard_limit) .and. &
    2138              :                max_dx_nuc_drop > hard_limit) then
    2139            0 :             if (s% report_dt_hard_limit_retries) then
    2140            0 :                write(*,2) trim(chem_isos% name(s% chem_id(max_j))), max_k, s% xa(max_j,max_k)
    2141            0 :                write(*,2) trim(chem_isos% name(s% chem_id(max_j))) // ' old', max_k, s% xa_old(max_j,max_k)
    2142            0 :                write(*,2) 'drop', max_k, max_dx_nuc_drop
    2143              :             end if
    2144            0 :             s% retry_message = 'dX_nuc_drop_hard_limit'
    2145            0 :             s% retry_message_k = max_k
    2146            0 :             check_dX_nuc_drop = retry
    2147            0 :             return
    2148              :          end if
    2149              : 
    2150           10 :          limit = s% dX_nuc_drop_limit*s% time_delta_coeff
    2151           10 :          if (s% log_max_temperature >= 9.45d0 .and. s% dX_nuc_drop_limit_at_high_T > 0) &
    2152           10 :             limit = s% dX_nuc_drop_limit_at_high_T
    2153           10 :          if (limit <= 0 .or. max_dx_nuc_drop <= 0) return
    2154              : 
    2155           10 :          if (dt < secyer*s% dX_nuc_drop_min_yrs_for_dt) return
    2156           10 :          min_dt = secyer*s% dX_nuc_drop_min_yrs_for_dt
    2157              :          dt_limit_ratio = min( &
    2158              :             max_dx_nuc_drop/limit, &
    2159           10 :             1d0*dt/min_dt)
    2160           10 :          if (dt_limit_ratio <= 1d0) dt_limit_ratio = 0
    2161              : 
    2162           10 :          s% dX_nuc_drop_max_j = max_j
    2163           10 :          s% dX_nuc_drop_max_k = max_k
    2164              : 
    2165              :          contains
    2166              : 
    2167        10623 :          subroutine do1(k)
    2168              :             integer, intent(in) :: k
    2169              : 
    2170              :             integer :: j
    2171              :             real(dp) :: dx, dx_drop, dm, dt_dm, dx_burning, dx_inflow, dxdt_nuc
    2172              :             real(dp) :: dx00, dxp1, sig00, sigp1, flux00, fluxp1
    2173              : 
    2174              :             include 'formats'
    2175              : 
    2176        10623 :             dm = s% dm(k)
    2177        10623 :             dt_dm = dt/dm
    2178              : 
    2179        95607 :             do j=1, s% species
    2180              : 
    2181        84984 :                if (chem_isos% W(s% chem_id(j)) > A_limit) then
    2182              :                   if (dbg .and. k == 1387) &
    2183              :                      write(*,2) 'dX_nuc_max_A_limit ' // trim(chem_isos% name(s% chem_id(j))), &
    2184              :                         k, s% xa(j,k), chem_isos% W(s% chem_id(j)), A_limit
    2185              :                   cycle
    2186              :                end if
    2187              : 
    2188        84984 :                if (chem_isos% Z(s% chem_id(j)) <= 2) then
    2189              :                   cycle  ! skip the little guys
    2190              :                end if
    2191              : 
    2192        53115 :                if (s% xa(j,k) < X_limit) then
    2193              :                   if (dbg .and. k == 1387) &
    2194              :                      write(*,2) &
    2195              :                         'dX_nuc_drop_min_X_limit ' // trim(chem_isos% name(s% chem_id(j))), &
    2196              :                         k, s% xa(j,k), X_limit
    2197              :                   cycle
    2198              :                end if
    2199              : 
    2200        51836 :                dxdt_nuc = s% dxdt_nuc(j,k)
    2201        51836 :                if (dxdt_nuc >= 0) cycle
    2202              : 
    2203         7651 :                dx_burning = dxdt_nuc*dt
    2204         7651 :                sig00 = sig(k)
    2205              : 
    2206         7651 :                if (k < s% nz) then
    2207         7631 :                   sigp1 = sig(k+1)
    2208              :                else
    2209              :                   sigp1 = 0
    2210              :                end if
    2211              : 
    2212         7651 :                if (k > 1) then
    2213         7651 :                   dx00 = s% xa(j,k-1) - s% xa(j,k)
    2214         7651 :                   flux00 = -sig00*dx00
    2215              :                else
    2216              :                   flux00 = 0
    2217              :                end if
    2218              : 
    2219         7651 :                if (k < s% nz) then
    2220         7631 :                   dxp1 = s% xa(j,k) - s% xa(j,k+1)
    2221         7631 :                   fluxp1 = -sigp1*dxp1
    2222              :                else
    2223              :                   fluxp1 = 0
    2224              :                end if
    2225              : 
    2226         7651 :                dx_inflow = max(0d0, fluxp1, -flux00)*dt_dm
    2227              : 
    2228         7651 :                dx_drop = -(dx_burning + dx_inflow)  ! dx_burning < 0 for drop
    2229              : 
    2230         7651 :                dx = s% xa_old(j,k) - s% xa(j,k)  ! the actual drop
    2231              :                if (dx < dx_drop) dx_drop = dx
    2232              : 
    2233        18274 :                if (dx_drop > max_dx_nuc_drop) then
    2234         2607 :                   max_dx_nuc_drop = dx_drop
    2235         2607 :                   max_k = k
    2236         2607 :                   max_j = j
    2237              :                end if
    2238              : 
    2239              :             end do
    2240              : 
    2241        10623 :          end subroutine do1
    2242              : 
    2243              : 
    2244              :       end function check_dX_nuc_drop
    2245              : 
    2246              : 
    2247           11 :       integer function check_varcontrol_limit(s, dt_limit_ratio)
    2248              :          type (star_info), pointer :: s
    2249              :          real(dp), intent(inout) :: dt_limit_ratio
    2250              : 
    2251              :          real(dp) :: varcontrol, vc_target
    2252              :          integer :: ierr
    2253              : 
    2254              :          include 'formats'
    2255              : 
    2256              :          ierr = 0
    2257           11 :          check_varcontrol_limit = keep_going
    2258              : 
    2259           11 :          varcontrol = eval_varcontrol(s, ierr)
    2260           11 :          if (ierr /= 0) then
    2261            0 :             check_varcontrol_limit = retry
    2262            0 :             s% retry_message = 'varcontrol hard limit'
    2263            0 :             if (s% report_ierr) write(*, *) 'check_varcontrol_limit: eval_varcontrol ierr', ierr
    2264            0 :             s% result_reason = nonzero_ierr
    2265            0 :             return
    2266              :          end if
    2267              : 
    2268           11 :          if (s% varcontrol_target < s% min_allowed_varcontrol_target) then
    2269            0 :             check_varcontrol_limit = terminate
    2270            0 :             write(*, *) 'ERROR: timestep varcontrol_target < min_allowed_varcontrol_target'
    2271            0 :             s% result_reason = nonzero_ierr
    2272            0 :             return
    2273              :          end if
    2274              : 
    2275           11 :          vc_target = max(1d-99,s% varcontrol_target)
    2276           11 :          dt_limit_ratio = varcontrol/vc_target
    2277              : 
    2278           11 :          if (s% report_solver_dt_info) then
    2279            0 :             write(*, 1) 's% varcontrol_target', s% varcontrol_target
    2280            0 :             write(*, 1) 'vc_target', vc_target
    2281            0 :             write(*, 1) 'varcontrol', varcontrol
    2282            0 :             write(*, 1) 'dt_limit_ratio', dt_limit_ratio
    2283            0 :             write(*, *)
    2284              :          end if
    2285              : 
    2286           11 :          if (dt_limit_ratio > s% varcontrol_dt_limit_ratio_hard_max) then
    2287            0 :             write(*, '(a50, f20.10, 99e20.10)') 'varcontrol_dt_limit_ratio too large', &
    2288            0 :                dt_limit_ratio, varcontrol, vc_target
    2289            0 :             check_varcontrol_limit = retry
    2290            0 :             s% retry_message = 'varcontrol_dt_limit_ratio_hard_max'
    2291            0 :             return
    2292              :          end if
    2293              : 
    2294              :       end function check_varcontrol_limit
    2295              : 
    2296              : 
    2297           11 :       real(dp) function eval_varcontrol(s, ierr) result(varcontrol)
    2298              :          type (star_info), pointer :: s
    2299              :          integer, intent(out) :: ierr
    2300              : 
    2301              :          integer :: j, nterms, nvar_hydro, nz, k
    2302           22 :          real(dp) :: sumj, sumvar, sumscales, sumterm(s% nvar_total)
    2303              :          real(dp), parameter :: xscale_min = 1
    2304              : 
    2305              :          include 'formats'
    2306           11 :          ierr = 0
    2307              : 
    2308           11 :          varcontrol = 1d99
    2309           11 :          nvar_hydro = s% nvar_hydro
    2310           11 :          nz = s% nz
    2311              : 
    2312           11 :          nterms = 0
    2313           11 :          sumvar = 0
    2314           11 :          sumscales = 0
    2315          143 :          sumterm(:) = 0
    2316              : 
    2317           11 :          if (.not. associated(s% xh_old)) then
    2318            0 :             call mesa_error(__FILE__,__LINE__,'not associated xh_old')
    2319              :          end if
    2320              : 
    2321              :          ! use differences in smoothed old and new to filter out high frequency noise.
    2322           55 :          do j = 1, nvar_hydro
    2323              : 
    2324              :             if (j == s% i_lum .or. &
    2325              :                 j == s% i_u .or. &
    2326              :                 j == s% i_v .or. &
    2327              :                 j == s% i_w .or. &
    2328              :                 j == s% i_Hp .or. &
    2329              :                 j == s% i_j_rot .or. &
    2330              :                 j == s% i_w_div_wc .or. &
    2331           44 :                 j == s% i_alpha_RTI .or. &
    2332              :                 j == s% i_Et_RSP) cycle
    2333              : 
    2334           33 :             nterms = nterms + nz
    2335        39162 :             do k = 3, nz-2
    2336       430419 :                sumj = abs(sum(s% xh(j,k-2:k+2)) - sum(s% xh_old(j,k-2:k+2)))/5
    2337        39162 :                sumterm(j) = sumterm(j) + sumj
    2338              :             end do
    2339              :             sumterm(j) = sumterm(j) + &
    2340              :                abs((2*s% xh(j,1) + s% xh(j,2)) - (2*s% xh_old(j,1) + s% xh_old(j,2)))/3 + &
    2341           33 :                abs((2*s% xh(j,nz) + s% xh(j,nz-1)) - (2*s% xh_old(j,nz) + s% xh_old(j,nz-1)))/3
    2342           33 :             k = 2
    2343          231 :             sumj = abs(sum(s% xh(j,k-1:k+1)) - sum(s% xh_old(j,k-1:k+1)))/3
    2344           33 :             sumterm(j) = sumterm(j) + sumj
    2345           33 :             k = nz-1
    2346           33 :             sumj = abs(sum(s% xh(j,k-1:k+1)) - sum(s% xh_old(j,k-1:k+1)))/3
    2347              : 
    2348           33 :             if (j == s% i_lnd) then
    2349           11 :                sumterm(j) = sumterm(j)/3  ! Seems to help. from Eggleton.
    2350              :             end if
    2351              : 
    2352           33 :             sumvar = sumvar + sumterm(j)
    2353           55 :             sumscales = sumscales + max(xscale_min, abs(s% xh_old(j,1)))
    2354              : 
    2355              :          end do
    2356              : 
    2357              :          sumterm(:) = sumterm(:)/sumscales
    2358           11 :          sumvar = sumvar/sumscales
    2359              : 
    2360           11 :          varcontrol = sumvar/nterms
    2361              : 
    2362           11 :       end function eval_varcontrol
    2363              : 
    2364              : 
    2365           11 :       subroutine filter_dt_next(s, order, dt_limit_ratio_in)
    2366              :          ! H211b "low pass" controller.
    2367              :          ! Soderlind & Wang, J of Computational and Applied Math 185 (2006) 225 – 243.
    2368              :          use num_def
    2369              :          type (star_info), pointer :: s
    2370              :          real(dp), intent(in) :: order, dt_limit_ratio_in
    2371              : 
    2372              :          real(dp) :: ratio, ratio_prev, limtr, dt_limit_ratio_target, &
    2373              :             dt_limit_ratio, beta1, beta2, alpha2
    2374              : 
    2375              :          include 'formats'
    2376              : 
    2377           11 :          beta1 = 0.25d0/order
    2378           11 :          beta2 = 0.25d0/order
    2379           11 :          alpha2 = 0.25d0
    2380              : 
    2381           11 :          dt_limit_ratio = max(1d-10, dt_limit_ratio_in)
    2382           11 :          s% dt_limit_ratio = dt_limit_ratio
    2383           11 :          dt_limit_ratio_target = 1d0
    2384              : 
    2385              :          if (s% use_dt_low_pass_controller .and. &
    2386           11 :                s% dt_limit_ratio_old > 0 .and. s% dt_old > 0) then  ! use 2 values to do "low pass" controller
    2387           10 :             ratio = limiter(dt_limit_ratio_target/dt_limit_ratio)
    2388           10 :             ratio_prev = limiter(dt_limit_ratio_target/s% dt_limit_ratio_old)
    2389              :             limtr = limiter( &
    2390           10 :                pow(ratio,beta1) * pow(ratio_prev,beta2) * pow(s% dt/s% dt_old,-alpha2))
    2391           10 :             s% dt_next = s% dt*limtr
    2392              : 
    2393           10 :             if (s% report_solver_dt_info) then
    2394            0 :                write(*,2) 'dt_limit_ratio_target', s% model_number, dt_limit_ratio_target
    2395            0 :                write(*,2) 'dt_limit_ratio', s% model_number, dt_limit_ratio
    2396            0 :                write(*,2) 's% dt_limit_ratio_old', s% model_number, s% dt_limit_ratio_old
    2397            0 :                write(*,2) 'order', s% model_number, order
    2398            0 :                write(*,2) 'ratio', s% model_number, ratio
    2399            0 :                write(*,2) 'ratio_prev', s% model_number, ratio_prev
    2400            0 :                write(*,2) 'limtr', s% model_number, limtr
    2401            0 :                write(*,2) 's% dt_next', s% model_number, s% dt_next
    2402            0 :                write(*,'(A)')
    2403              :             end if
    2404              : 
    2405              :          else  ! no history available, so fall back to the 1st order controller
    2406            1 :             s% dt_next = s% dt*dt_limit_ratio_target/dt_limit_ratio
    2407            1 :             if (s% report_solver_dt_info) then
    2408            0 :                write(*,2) 's% dt', s% model_number, s% dt
    2409            0 :                write(*,2) 'dt_limit_ratio_target', s% model_number, dt_limit_ratio_target
    2410            0 :                write(*,2) 'dt_limit_ratio', s% model_number, dt_limit_ratio
    2411            0 :                write(*,2) 'filter_dt_next', s% model_number, s% dt_next
    2412              :             end if
    2413            1 :             if (s% dt_next == 0d0) call mesa_error(__FILE__,__LINE__,'filter_dt_next')
    2414              :          end if
    2415              : 
    2416              : 
    2417              :          contains
    2418              : 
    2419              : 
    2420           30 :          real(dp) function limiter(x)
    2421              :             real(dp), intent(in) :: x
    2422              :             real(dp), parameter :: kappa = 10  ! 2
    2423              :             ! for x >= 0 and kappa = 2, limiter value is between 0.07 and 4.14
    2424              :             ! for x >= 0 and kappa = 10, limiter value is between 0.003 and 16.7
    2425              :             ! for x = 1, limiter = 1
    2426           30 :             limiter = 1 + kappa*atan((x-1)/kappa)
    2427           30 :          end function limiter
    2428              : 
    2429              : 
    2430              :       end subroutine filter_dt_next
    2431              : 
    2432              : 
    2433              :       end module timestep
    2434              : 
    2435              : 
        

Generated by: LCOV version 2.0-1