LCOV - code coverage report
Current view: top level - star/private - turb_support.f90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 44.7 % 206 92
Test Date: 2025-12-14 16:52:03 Functions: 50.0 % 4 2

            Line data    Source code
       1              : ! ***********************************************************************
       2              : !
       3              : !   Copyright (C) 2010-2021  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 turb_support
      21              : 
      22              : use star_private_def
      23              : use const_def, only: dp, crad, no_mixing
      24              : use num_lib
      25              : use utils_lib
      26              : use auto_diff_support
      27              : use star_utils
      28              : use turb
      29              : 
      30              : implicit none
      31              : 
      32              : private
      33              : public :: get_gradT
      34              : public :: do1_mlt_eval
      35              : public :: Get_results
      36              : 
      37              : contains
      38              : 
      39              :    !> Determines if it is safe (physically) to use TDC instead of MLT.
      40              :    !!
      41              :    !! Currently we only know we have to fall back to MLT in cells that get touched
      42              :    !! by adjust_mass, because there the convection speeds at the start of the
      43              :    !! step can be badly out of whack.
      44              :    !!
      45              :    !! @param s star pointer
      46              :    !! @param k face index
      47              :    !! @param fallback False if we can use TDC, True if we can fall back to MLT.
      48        65210 :    logical function check_if_must_fall_back_to_MLT(s, k) result(fallback)
      49              :       type (star_info), pointer :: s
      50              :       integer, intent(in) :: k
      51              : 
      52        65210 :       fallback = .false.
      53            0 :       if (abs(s%mstar_dot) > 1d-99 .and. k < s% k_const_mass) then
      54              :          fallback = .true.
      55              :       end if
      56              :    end function check_if_must_fall_back_to_MLT
      57              : 
      58            0 :    subroutine get_gradT(s, MLT_option, &  ! used to create models
      59              :          r, L, T, P, opacity, rho, chiRho, chiT, Cp, gradr, grada, scale_height, &
      60              :          iso, XH1, cgrav, m, gradL_composition_term, mixing_length_alpha, &
      61              :          mixing_type, gradT, Y_face, conv_vel, D, Gamma, ierr)
      62              :       type (star_info), pointer :: s
      63              :       character (len=*), intent(in) :: MLT_option
      64              :       real(dp), intent(in) :: &
      65              :          r, L, T, P, opacity, rho, chiRho, chiT, Cp, gradr, grada, scale_height, &
      66              :          XH1, cgrav, m, gradL_composition_term, mixing_length_alpha
      67              :       integer, intent(in) :: iso
      68              :       real(dp), intent(out) :: gradT, Y_face, conv_vel, D, Gamma
      69              :       integer, intent(out) :: mixing_type, ierr
      70              :       type(auto_diff_real_star_order1) :: &
      71              :          gradr_ad, grada_ad, scale_height_ad, gradT_ad, Y_face_ad, mlt_vc_ad, D_ad, &
      72              :          Gamma_ad, r_ad, L_ad, T_ad, P_ad, opacity_ad, rho_ad, dV_ad, chiRho_ad, chiT_ad, Cp_ad, energy_ad
      73            0 :       ierr = 0
      74            0 :       r_ad = r
      75            0 :       L_ad = L
      76            0 :       T_ad = T
      77            0 :       P_ad = P
      78            0 :       opacity_ad = opacity
      79            0 :       rho_ad = rho
      80            0 :       dV_ad = 0d0
      81            0 :       chiRho_ad = chiRho
      82            0 :       chiT_ad = chiT
      83            0 :       Cp_ad = Cp
      84            0 :       gradr_ad = gradr
      85            0 :       grada_ad = grada
      86            0 :       energy_ad = 0d0 ! correct to a value
      87            0 :       scale_height_ad = scale_height
      88            0 :       if (s% use_other_mlt_results) then
      89              :          call s% other_mlt_results(s% id, 0, MLT_option, &
      90              :             r_ad, L_ad, T_ad, P_ad, opacity_ad, rho_ad, dV_ad, chiRho_ad, &
      91              :             chiT_ad, Cp_ad, gradr_ad, grada_ad, scale_height_ad, &
      92              :             iso, XH1, cgrav, m, gradL_composition_term, mixing_length_alpha, &
      93              :             s% alpha_semiconvection, s% thermohaline_coeff, &
      94            0 :             mixing_type, gradT_ad, Y_face_ad, mlt_vc_ad, D_ad, Gamma_ad, energy_ad, ierr)
      95              :       else
      96              :          call Get_results(s, 0, MLT_option, &
      97              :             r_ad, L_ad, T_ad, P_ad, opacity_ad, rho_ad, dV_ad, chiRho_ad, &
      98              :             chiT_ad, Cp_ad, gradr_ad, grada_ad, scale_height_ad, &
      99              :             iso, XH1, cgrav, m, gradL_composition_term, mixing_length_alpha, &
     100              :             s% alpha_semiconvection, s% thermohaline_coeff, &
     101            0 :             mixing_type, gradT_ad, Y_face_ad, mlt_vc_ad, D_ad, Gamma_ad, energy_ad, ierr)
     102              :       end if
     103            0 :       gradT = gradT_ad%val
     104            0 :       Y_face = Y_face_ad%val
     105            0 :       conv_vel = mlt_vc_ad%val
     106            0 :       D = D_ad%val
     107            0 :       Gamma = Gamma_ad%val
     108            0 :    end subroutine get_gradT
     109              : 
     110              : 
     111        79994 :    subroutine do1_mlt_eval( &
     112              :          s, k, MLT_option, gradL_composition_term, &
     113              :          gradr_in, grada, scale_height, mixing_length_alpha, &
     114              :          mixing_type, gradT, Y_face, mlt_vc, D, Gamma, ierr)
     115              :       use chem_def, only: ih1
     116              :       use const_def, only: ln10
     117              :       use starspots, only: starspot_tweak_gradr
     118              :       type (star_info), pointer :: s
     119              :       integer, intent(in) :: k
     120              :       character (len=*), intent(in) :: MLT_option
     121              :       type(auto_diff_real_star_order1), intent(in) :: gradr_in, grada, scale_height
     122              :       real(dp), intent(in) :: gradL_composition_term, mixing_length_alpha
     123              :       integer, intent(out) :: mixing_type
     124              :       type(auto_diff_real_star_order1), intent(out) :: &
     125              :          gradT, Y_face, mlt_vc, D, Gamma
     126              :       integer, intent(out) :: ierr
     127              : 
     128        79994 :       real(dp) :: cgrav, m, XH1, P_theta, L_theta
     129              :       integer :: iso
     130              :       type(auto_diff_real_star_order1) :: gradr, r, L, T, P, opacity, rho, dV, &
     131              :          chiRho, chiT, Cp, rho_start, energy
     132              :       include 'formats'
     133        79994 :       ierr = 0
     134              : 
     135              : 
     136        79994 :       P = get_Peos_face(s,k) ! if u_flag, should this be P_face_ad? (time centered in riemann)
     137        79994 :       if (s% include_mlt_in_velocity_time_centering) then
     138              :           ! could be cleaner with a wrapper for time_centered P and L
     139              :           if (s% using_velocity_time_centering .and. &
     140            0 :             s% include_P_in_velocity_time_centering .and. &
     141              :             s% lnT(k)/ln10 <= s% max_logT_for_include_P_and_L_in_velocity_time_centering) then
     142            0 :              P_theta = s% P_theta_for_velocity_time_centering
     143              :           else
     144            0 :              P_theta = 1d0
     145              :           end if
     146              :           ! consder building a wrapper : wrap_opt_time_center_L_00(s,k)
     147              :           if (s% using_velocity_time_centering .and. &
     148            0 :             s% include_L_in_velocity_time_centering .and. &
     149              :             s% lnT(k)/ln10 <= s% max_logT_for_include_P_and_L_in_velocity_time_centering) then
     150            0 :              L_theta = s% L_theta_for_velocity_time_centering
     151              :           else
     152            0 :              L_theta = 1d0
     153              :           end if
     154            0 :           L = L_theta*wrap_L_00(s, k) + (1d0 - L_theta)*s% L_start(k)
     155            0 :           P = P_theta*P + (1d0-P_theta)*s% Peos_face_start(k)
     156            0 :           r = wrap_opt_time_center_r_00(s,k)
     157              :       else
     158        79994 :           L = wrap_L_00(s,k)
     159        79994 :           r = wrap_r_00(s,k)
     160              :       end if
     161        79994 :       gradr = gradr_in
     162        79994 :       cgrav = s% cgrav(k)
     163        79994 :       m = s% m_grav(k)
     164        79994 :       T = get_T_face(s,k)
     165        79994 :       opacity = get_kap_face(s,k)
     166        79994 :       rho = get_rho_face(s,k)
     167        79994 :       rho_start = get_rho_start_face(s,k)
     168        79994 :       dV = 1d0/rho - 1d0/rho_start ! both variables are face wrapped.
     169        79994 :       chiRho = get_ChiRho_face(s,k)
     170        79994 :       chiT = get_ChiT_face(s,k)
     171        79994 :       Cp = get_Cp_face(s,k)
     172        79994 :       energy = get_e_face(s,k)
     173        79994 :       iso = s% dominant_iso_for_thermohaline(k)
     174        79994 :       XH1 = s% xa(s% net_iso(ih1),k)
     175              : 
     176        79994 :       if (s% use_other_mlt_results) then
     177              :          call s% other_mlt_results(s% id, k, MLT_option, &
     178              :             r, L, T, P, opacity, rho, dV, chiRho, chiT, Cp, gradr, grada, scale_height, &
     179              :             iso, XH1, cgrav, m, gradL_composition_term, mixing_length_alpha, &
     180              :             s% alpha_semiconvection, s% thermohaline_coeff, &
     181            0 :             mixing_type, gradT, Y_face, mlt_vc, D, Gamma, energy, ierr)
     182              :       else
     183              :          ! starspot YREC routine
     184        79994 :          if (s% do_starspots) then
     185              :             !dV = 0d0 ! dV = 1/rho - 1/rho_start and we assume rho = rho_start.
     186            0 :             call starspot_tweak_gradr(s, P, gradr_in, gradr)
     187              :          end if
     188              :          call Get_results(s, k, MLT_option, &
     189              :             r, L, T, P, opacity, rho, dV, chiRho, chiT, Cp, gradr, grada, scale_height, &
     190              :             iso, XH1, cgrav, m, gradL_composition_term, mixing_length_alpha, &
     191              :             s% alpha_semiconvection, s% thermohaline_coeff, &
     192        79994 :             mixing_type, gradT, Y_face, mlt_vc, D, Gamma, energy, ierr)
     193              :       end if
     194              : 
     195        79994 :    end subroutine do1_mlt_eval
     196              : 
     197              : 
     198        79994 :    subroutine Get_results(s, k, MLT_option, &  ! NOTE: k=0 is a valid arg
     199              :          r, L, T, P, opacity, rho, dV, chiRho, chiT, Cp, gradr, grada, scale_height, &
     200              :          iso, XH1, cgrav, m, gradL_composition_term, mixing_length_alpha, &
     201              :          alpha_semiconvection, thermohaline_coeff, &
     202              :          mixing_type, gradT, Y_face, conv_vel, D, Gamma, energy, ierr)
     203        79994 :       use star_utils
     204              :       use tdc_hydro, only: compute_tdc_Eq_div_w_face
     205              :       type (star_info), pointer :: s
     206              :       integer, intent(in) :: k
     207              :       character (len=*), intent(in) :: MLT_option
     208              :       type(auto_diff_real_star_order1), intent(in) :: &
     209              :          r, L, T, P, opacity, rho, dV, chiRho, chiT, Cp, gradr, grada, scale_height, energy
     210              :       integer, intent(in) :: iso
     211              :       real(dp), intent(in) :: &
     212              :          XH1, cgrav, m, gradL_composition_term, &
     213              :          mixing_length_alpha, alpha_semiconvection, thermohaline_coeff
     214              :       integer, intent(out) :: mixing_type
     215              :       type(auto_diff_real_star_order1), intent(out) :: gradT, Y_face, conv_vel, D, Gamma
     216              :       integer, intent(out) :: ierr
     217              : 
     218              :       type(auto_diff_real_star_order1) :: Pr, Pg, grav, Lambda, gradL, beta
     219        79994 :       real(dp) :: conv_vel_start, scale, max_conv_vel
     220              : 
     221              :       ! these are used by use_superad_reduction
     222        79994 :       real(dp) :: Gamma_limit, scale_value1, scale_value2, diff_grads_limit, reduction_limit, lambda_limit
     223              :       type(auto_diff_real_star_order1) :: Lrad_div_Ledd, Gamma_inv_threshold, Gamma_factor, alfa0, &
     224              :          diff_grads_factor, Gamma_term, exp_limit, grad_scale, gradr_scaled, Eq_div_w, check_Eq, mlt_Pturb, Ptot
     225              :       logical ::  test_partials, using_TDC
     226              :       logical, parameter :: report = .false.
     227              :       include 'formats'
     228              : 
     229              :       ! check if this particular k can be done with TDC
     230        79994 :       using_TDC = .false.
     231        79994 :       if (s% MLT_option == 'TDC') using_TDC = .true.
     232        79994 :       if (.not. s% have_mlt_vc) using_TDC = .false.
     233        79994 :       if (k <= 0 .or. s%dt <= 0d0) using_TDC = .false.
     234       140276 :       if (using_TDC) using_TDC = .not. check_if_must_fall_back_to_MLT(s, k)
     235              : 
     236              :       ! Pre-calculate some things.
     237        79994 :       Eq_div_w = 0d0
     238        79994 :       if ((s% v_flag .or. s% u_flag) .and. k > 0 ) then ! only include Eq_div_w if v_flag or u_flag is true.
     239            0 :          if (using_TDC .and. s% TDC_alpha_M > 0) then
     240            0 :              check_Eq = compute_tdc_Eq_div_w_face(s, k, ierr)
     241            0 :              Eq_div_w = check_Eq
     242              :          end if
     243              :       end if
     244              : 
     245              :       ! Wrap Pturb into P
     246        79994 :       if (s% okay_to_set_mlt_vc .and. s% include_mlt_Pturb_in_thermodynamic_gradients .and. k > 0) then
     247            0 :          mlt_Pturb = s% mlt_Pturb_factor*pow2(s% mlt_vc_old(k))*get_rho_face(s,k)/3d0
     248            0 :          Ptot = P + mlt_Pturb
     249              :       else
     250        79994 :          Ptot = P
     251              :       end if
     252              : 
     253        79994 :       Pr = crad*pow4(T)/3d0
     254        79994 :       Pg = Ptot - Pr
     255        79994 :       beta = Pg / Ptot
     256        79994 :       Lambda = mixing_length_alpha*scale_height
     257              : 
     258        79994 :       if (k == 0) then
     259            0 :          grav = cgrav*m/pow2(r)
     260              :       else
     261        79994 :          grav = cgrav*m/pow2(r) !try replacing with wrap_geff_face(s,k)
     262              :       end if
     263              : 
     264        79994 :       if (s% use_Ledoux_criterion) then
     265            0 :          gradL = grada + gradL_composition_term  ! Ledoux temperature gradient
     266              :       else
     267              :          gradL = grada
     268              :       end if
     269              : 
     270              :       ! maximum convection velocity.
     271        79994 :       if (k > 0) then
     272        79994 :          if (s% q(k) <= s% max_conv_vel_div_csound_maxq) then
     273            0 :              max_conv_vel = s% csound_face(k) * s% max_conv_vel_div_csound
     274              :          else
     275        79994 :             max_conv_vel = 1d99
     276              :          end if
     277              :       else ! if k == 0
     278            0 :          max_conv_vel = 1d99
     279              :       end if
     280              : 
     281              : 
     282              :       ! Initialize with no mixing
     283        79994 :       mixing_type = no_mixing
     284        79994 :       gradT = gradr
     285        79994 :       Y_face = gradT - gradL
     286        79994 :       conv_vel = 0d0
     287        79994 :       D = 0d0
     288        79994 :       Gamma = 0d0
     289        79994 :       if (k /= 0) s% superad_reduction_factor(k) = 1d0
     290              : 
     291              :       ! Bail if we asked for no mixing, or if parameters are bad.
     292              :       if (MLT_option == 'none' .or. beta < 1d-10 .or. mixing_length_alpha <= 0d0 .or. &
     293              :             opacity%val < 1d-10 .or. P%val < 1d-20 .or. T%val < 1d-10 .or. Rho%val < 1d-20 &
     294        79994 :             .or. m < 1d-10 .or. r%val < 1d-10 .or. cgrav < 1d-10) return
     295              : 
     296              :       !test_partials = (k == s% solver_test_partials_k)
     297        79994 :       test_partials = .false.
     298        79994 :       ierr = 0
     299        79994 :       if (k > 0) then
     300        79994 :          s% tdc_num_iters(k) = 0
     301              :       end if
     302              : 
     303              :       if (report) then
     304              :          write(*,'(A)')
     305              :          write(*,4) 'enter Get_results k slvr_itr model gradr grada scale_height ' // trim(MLT_option), &
     306              :             k, s% solver_iter, s% model_number, gradr%val, grada%val, scale_height%val
     307              :       end if
     308              : 
     309        79994 :       if (k >= 1) then
     310        79994 :          s% dvc_dt_TDC(k) = 0d0
     311              :       end if
     312        79994 :       if (using_TDC) then
     313              :          if (report) write(*,3) 'call set_TDC', k, s% solver_iter
     314        65210 :          if (s% okay_to_set_mlt_vc) then
     315        42492 :             conv_vel_start = s% mlt_vc_old(k)
     316              :          else
     317        22718 :             conv_vel_start = s% mlt_vc(k)
     318              :          end if
     319              : 
     320              :          ! Set scale for judging the TDC luminosity equation Q(Y)=0.
     321              :          ! Q has units of a luminosity, so the scale should be a luminosity.
     322        65210 :          if (s% solver_iter == 0) then
     323     39866797 :             scale = max(abs(s% L(k)), 1d-3*maxval(s% L(1:s% nz)))
     324              :          else
     325     34776621 :             scale = max(abs(s% L_start(k)), 1d-3*maxval(s% L_start(1:s% nz)))
     326              :          end if
     327              : 
     328              :          call set_TDC(&
     329              :             conv_vel_start, mixing_length_alpha, s%TDC_alpha_D, s%TDC_alpha_R, s%TDC_alpha_Pt, &
     330              :             s%dt, cgrav, m, report, &
     331              :             mixing_type, scale, chiT, chiRho, gradr, r, Ptot, T, rho, dV, Cp, opacity, &
     332              :             scale_height, gradL, grada, conv_vel, D, Y_face, gradT, s%tdc_num_iters(k), max_conv_vel, &
     333              :             Eq_div_w, grav, &
     334        65210 :             s% include_mlt_corr_to_TDC, s% TDC_alpha_C, s% TDC_alpha_S, s% use_TDC_enthalpy_flux_limiter, energy, ierr)
     335        65210 :          s% dvc_dt_TDC(k) = (conv_vel%val - conv_vel_start) / s%dt
     336              : 
     337        65210 :             if (ierr /= 0) then
     338            0 :                if (s% report_ierr) write(*,*) 'ierr from set_TDC'
     339            0 :                return
     340              :             end if
     341              : 
     342              :          ! Experimental method to lower superadiabaticity. Call TDC again with an artificially reduced
     343              :          ! gradr if the resulting gradT would lead to the radiative luminosity approaching the Eddington
     344              :          ! limit, or when a density inversion is expected to happen.
     345              :          ! This is meant as an implicit alternative to okay_to_reduce_gradT_excess
     346        65210 :          if (s% use_superad_reduction) then
     347            0 :             call set_superad_reduction
     348            0 :             if (Gamma_factor > 1d0) then
     349              :                call set_TDC(&
     350              :                   conv_vel_start, mixing_length_alpha, s%TDC_alpha_D, s%TDC_alpha_R, s%TDC_alpha_Pt, &
     351              :                   s%dt, cgrav, m, report, &
     352              :                   mixing_type, scale, chiT, chiRho, gradr_scaled, r, Ptot, T, rho, dV, Cp, opacity, &
     353              :                   scale_height, gradL, grada, conv_vel, D, Y_face, gradT, s%tdc_num_iters(k), max_conv_vel, &
     354              :                   Eq_div_w, grav, &
     355            0 :                   s% include_mlt_corr_to_TDC, s% TDC_alpha_C, s% TDC_alpha_S, s% use_TDC_enthalpy_flux_limiter, energy, ierr)
     356            0 :                s% dvc_dt_TDC(k) = (conv_vel%val - conv_vel_start) / s%dt
     357            0 :                if (ierr /= 0) then
     358            0 :                   if (s% report_ierr) write(*,*) 'ierr from set_TDC when using superad_reduction'
     359            0 :                   return
     360              :                end if
     361              :             end if
     362              :          end if
     363              : 
     364        14784 :       else if (gradr > gradL) then
     365              :          if (report) write(*,3) 'call set_MLT', k, s% solver_iter
     366              :          call set_MLT(MLT_option, mixing_length_alpha, s% Henyey_MLT_nu_param, s% Henyey_MLT_y_param, &
     367              :                         chiT, chiRho, Cp, grav, Lambda, rho, Ptot, T, opacity, &
     368              :                         gradr, grada, gradL, &
     369         2729 :                         Gamma, gradT, Y_face, conv_vel, D, mixing_type, max_conv_vel, ierr)
     370              : 
     371              : 
     372         2729 :          if (ierr /= 0) then
     373            0 :             if (s% report_ierr) write(*,*) 'ierr from set_MLT'
     374            0 :             return
     375              :          end if
     376              : 
     377              :          ! Experimental method to lower superadiabaticity. Call MLT again with an artificially reduced
     378              :          ! gradr if the resulting gradT would lead to the radiative luminosity approaching the Eddington
     379              :          ! limit, or when a density inversion is expected to happen.
     380              :          ! This is meant as an implicit alternative to okay_to_reduce_gradT_excess
     381         2729 :          if (s% use_superad_reduction) then
     382            0 :             call set_superad_reduction
     383            0 :             if (Gamma_factor > 1d0) then
     384              :                call set_MLT(MLT_option, mixing_length_alpha, s% Henyey_MLT_nu_param, s% Henyey_MLT_y_param, &
     385              :                               chiT, chiRho, Cp, grav, Lambda, rho, Ptot, T, opacity, &
     386              :                               gradr_scaled, grada, gradL, &
     387            0 :                               Gamma, gradT, Y_face, conv_vel, D, mixing_type, max_conv_vel, ierr)
     388              : 
     389            0 :                if (ierr /= 0) then
     390            0 :                   if (s% report_ierr) write(*,*) 'ierr from set_MLT when using superad_reduction'
     391            0 :                   return
     392              :                end if
     393              :             end if
     394              :          end if
     395              :       end if
     396              : 
     397              :       ! If we're not convecting, try thermohaline and semiconvection.
     398        79994 :       if (mixing_type == no_mixing) then
     399        68285 :          if (gradL_composition_term < 0) then
     400              :             if (report) write(*,3) 'call set_thermohaline', k, s% solver_iter
     401              :             call set_thermohaline(s%thermohaline_option, Lambda, grada, gradr, T, opacity, rho, Cp, gradL_composition_term, &
     402              :                               iso, XH1, thermohaline_coeff, &
     403            0 :                               D, gradT, Y_face, conv_vel, mixing_type, ierr)
     404            0 :             if (ierr /= 0) then
     405            0 :                if (s% report_ierr) write(*,*) 'ierr from set_thermohaline'
     406            0 :                return
     407              :             end if
     408        68285 :          else if (gradr > grada) then
     409              :             if (report) write(*,3) 'call set_semiconvection', k, s% solver_iter
     410              :             call set_semiconvection(L, Lambda, m, T, Ptot, Pr, beta, opacity, rho, alpha_semiconvection, &
     411              :                                     s% semiconvection_option, cgrav, Cp, gradr, grada, gradL, &
     412              :                                     gradL_composition_term, &
     413            0 :                                     gradT, Y_face, conv_vel, D, mixing_type, ierr)
     414            0 :             if (ierr /= 0) then
     415            0 :                if (s% report_ierr) write(*,*) 'ierr from set_semiconvection'
     416            0 :                return
     417              :             end if
     418              :          end if
     419              :       end if
     420              : 
     421              :       ! If there's too-little mixing to bother, or we hit a bad value, fall back on no mixing.
     422        79994 :       if (D%val < s% remove_small_D_limit .or. is_bad(D%val)) then
     423              :          if (report) write(*,2) 'D < s% remove_small_D_limit', k, D%val, s% remove_small_D_limit
     424        68285 :          mixing_type = no_mixing
     425        68285 :          gradT = gradr
     426        68285 :          Y_face = gradT - gradL
     427        68285 :          conv_vel = 0d0
     428        68285 :          D = 0d0
     429        68285 :          Gamma = 0d0
     430              :       end if
     431              : 
     432              :       ! Prevent convection near center of model for MLT or TDC pulsations
     433              :       ! We don't check for the using_TDC flag, because mlt is sometimes called when using TDC
     434        79994 :       if (k > s% nz - s% TDC_num_innermost_cells_forced_nonturbulent .or. &
     435        79994 :             k < s% TDC_num_outermost_cells_forced_nonturbulent) then
     436              :          if (report) write(*,2) 'make TDC center cells non-turbulent', k
     437            0 :          mixing_type = no_mixing
     438            0 :          gradT = gradr
     439            0 :          Y_face = gradT - gradL
     440            0 :          conv_vel = 0d0
     441            0 :          D = 0d0
     442            0 :          Gamma = 0d0
     443              :       end if
     444              : 
     445              : 
     446              :       contains
     447              : 
     448            0 :       subroutine set_superad_reduction()
     449            0 :          Gamma_limit = s% superad_reduction_Gamma_limit
     450            0 :          scale_value1 = s% superad_reduction_Gamma_limit_scale
     451            0 :          scale_value2 = s% superad_reduction_Gamma_inv_scale
     452            0 :          diff_grads_limit = s% superad_reduction_diff_grads_limit
     453            0 :          reduction_limit = s% superad_reduction_limit
     454            0 :          Lrad_div_Ledd = 4d0*crad/3d0*pow4(T)/P*gradT
     455            0 :          Gamma_inv_threshold = 4d0*(1d0-beta)/(4d0-3*beta)
     456              : 
     457            0 :          Gamma_factor = 1d0
     458            0 :          if (gradT > gradL) then
     459            0 :             if (Lrad_div_Ledd > Gamma_limit .or. Lrad_div_Ledd > Gamma_inv_threshold) then
     460            0 :                alfa0 = (gradT-gradL)/diff_grads_limit
     461            0 :                if (alfa0 < 1d0) then
     462            0 :                   diff_grads_factor = -alfa0*alfa0*alfa0*(-10d0 + alfa0*(15d0 - 6d0*alfa0))
     463              :                else
     464            0 :                   diff_grads_factor = 1d0
     465              :                end if
     466              : 
     467            0 :                Gamma_term = 0d0
     468              :                !if (Lrad_div_Ledd > Gamma_limit) then
     469              :                !   Gamma_term = Gamma_term + scale_value1*pow2(Lrad_div_Ledd/Gamma_limit-1d0)
     470              :                !end if
     471              :                !if (Lrad_div_Ledd% val > Gamma_inv_threshold) then
     472              :                !   Gamma_term = Gamma_term + scale_value2*pow2(Lrad_div_Ledd/Gamma_inv_threshold-1d0)
     473              :                !end if
     474            0 :                if (Lrad_div_Ledd > Gamma_limit) then
     475            0 :                   alfa0 = Lrad_div_Ledd/Gamma_limit-1d0
     476            0 :                   if (alfa0 < 1d0) then
     477            0 :                      Gamma_term = Gamma_term + scale_value1*(0.5d0*alfa0*alfa0)
     478              :                   else
     479            0 :                      Gamma_term = Gamma_term + scale_value1*(alfa0-0.5d0)
     480              :                   end if
     481              :                   !Gamma_term = Gamma_term + scale_value1*pow2(Lrad_div_Ledd/Gamma_limit-1d0)
     482              :                end if
     483            0 :                if (Lrad_div_Ledd% val > Gamma_inv_threshold) then
     484            0 :                   alfa0 = Lrad_div_Ledd/Gamma_inv_threshold-1d0
     485            0 :                   if (alfa0 < 1d0) then
     486            0 :                      Gamma_term = Gamma_term + scale_value1*(0.5d0*alfa0*alfa0)
     487              :                   else
     488            0 :                      Gamma_term = Gamma_term + scale_value1*(alfa0-0.5d0)
     489              :                   end if
     490              :                   !Gamma_term = Gamma_term + scale_value2*pow2(Lrad_div_Ledd/Gamma_inv_threshold-1d0)
     491              :                end if
     492              : 
     493            0 :                if (Gamma_term > 0d0) then
     494            0 :                   Gamma_factor = Gamma_term/pow(beta,0.5d0)*diff_grads_factor
     495            0 :                   Gamma_factor = Gamma_factor + 1d0
     496            0 :                   if (reduction_limit > 1d0) then
     497            0 :                      lambda_limit = 2d0/(reduction_limit-1d0)
     498            0 :                      exp_limit = exp(-lambda_limit*(Gamma_factor-1d0))
     499            0 :                      Gamma_factor = 2d0*(reduction_limit-1d0)*(1d0/(1d0+exp_limit)-0.5d0)+1d0
     500              :                   end if
     501              :                end if
     502              :             end if
     503              :          end if
     504            0 :          if (k /= 0) s% superad_reduction_factor(k) = Gamma_factor% val
     505            0 :          if (Gamma_factor > 1d0) then
     506            0 :             grad_scale = (gradr-gradL)/(Gamma_factor*gradr) + gradL/gradr
     507            0 :             gradr_scaled = grad_scale*gradr
     508              :          end if
     509        79994 :       end subroutine set_superad_reduction
     510              :    end subroutine Get_results
     511              : 
     512              : end module turb_support
        

Generated by: LCOV version 2.0-1