LCOV - code coverage report
Current view: top level - star/private - init.f90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 70.2 % 675 474
Test Date: 2026-03-31 17:48:03 Functions: 40.7 % 27 11

            Line data    Source code
       1              : ! ***********************************************************************
       2              : !
       3              : !   Copyright (C) 2010  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 init
      21              : 
      22              :       use star_private_def
      23              :       use const_def, only: dp, msun, secyer
      24              : 
      25              :       implicit none
      26              : 
      27              :       private
      28              :       public :: alloc_star_data
      29              :       public :: set_starting_star_data
      30              :       public :: do_star_init
      31              :       public :: do_starlib_shutdown
      32              :       public :: set_kap_and_eos_handles
      33              :       public :: set_colors_handles
      34              :       public :: load_zams_model
      35              :       public :: create_pre_ms_model
      36              :       public :: create_initial_model
      37              :       public :: create_RSP_model
      38              :       public :: create_RSP2_model
      39              :       public :: doing_restart
      40              :       public :: load_restart_photo
      41              :       public :: load_saved_model
      42              :       public :: do_garbage_collection
      43              : 
      44              :       integer, parameter :: do_create_pre_ms_model = 0
      45              :       integer, parameter :: do_load_zams_model = 1
      46              :       integer, parameter :: do_load_saved_model = 2
      47              :       integer, parameter :: do_create_initial_model = 3
      48              :       integer, parameter :: do_create_RSP_model = 4
      49              :       integer, parameter :: do_create_RSP2_model = 5
      50              : 
      51              :       logical :: have_done_starlib_init = .false.
      52              : 
      53              :       contains
      54              : 
      55            1 :       subroutine set_kap_and_eos_handles(id, ierr)
      56              :          use kap_lib, only: alloc_kap_handle_using_inlist, kap_ptr
      57              :          use eos_lib, only: alloc_eos_handle_using_inlist, eos_ptr
      58              :          integer, intent(in) :: id
      59              :          integer, intent(out) :: ierr  ! 0 means AOK.
      60              :          type (star_info), pointer :: s
      61            1 :          call get_star_ptr(id, s, ierr)
      62            1 :          if (ierr /= 0) then
      63            0 :             write(*,*) 'get_star_ptr failed in alloc_eos_handle'
      64            0 :             return
      65              :          end if
      66            1 :          if (s% eos_handle == 0) then
      67            1 :             s% eos_handle = alloc_eos_handle_using_inlist(s% inlist_fname, ierr)
      68            1 :             if (ierr /= 0) then
      69            0 :                write(*,*) 'set_kap_and_eos_handles failed in alloc_eos_handle_using_inlist'
      70            0 :                return
      71              :             end if
      72            1 :             call eos_ptr(s% eos_handle, s% eos_rq, ierr)
      73            1 :             if (ierr /= 0) then
      74            0 :                write(*,*) 'eos_ptr failed in alloc_eos_handle'
      75            0 :                return
      76              :             end if
      77              :          end if
      78            1 :          if (s% kap_handle == 0) then
      79            1 :             s% kap_handle = alloc_kap_handle_using_inlist(s% inlist_fname, ierr)
      80            1 :             if (ierr /= 0) then
      81            0 :                write(*,*) 'set_kap_and_eos_handles failed in alloc_kap_handle_using_inlist'
      82            0 :                return
      83              :             end if
      84            1 :             call kap_ptr(s% kap_handle,s% kap_rq,ierr)
      85            1 :             if (ierr /= 0) then
      86            0 :                write(*,*) 'kap_ptr failed in alloc_kap_handle'
      87            0 :                return
      88              :             end if
      89              :          end if
      90              :       end subroutine set_kap_and_eos_handles
      91              : 
      92              : 
      93            1 :       subroutine set_colors_handles(id, ierr)
      94              :          use colors_lib, only: alloc_colors_handle_using_inlist, colors_ptr
      95              :          use colors_def, only: Colors_General_Info
      96              :          integer, intent(in) :: id
      97              :          integer, intent(out) :: ierr  ! 0 means AOK.
      98              :          type (star_info), pointer :: s
      99              :          type(Colors_General_Info), pointer :: colors_settings
     100            1 :          call get_star_ptr(id, s, ierr)
     101            1 :          if (ierr /= 0) then
     102            0 :             write(*,*) 'get_star_ptr failed in alloc_colors_handle'
     103            0 :             return
     104              :          end if
     105            1 :          if (s% colors_handle == 0) then
     106            1 :             s% colors_handle = alloc_colors_handle_using_inlist(s% inlist_fname, ierr)
     107            1 :             if (ierr /= 0) then
     108            0 :                write(*,*) 'set_colors_handles failed in alloc_colors_handle'
     109            0 :                return
     110              :             end if
     111            1 :             call colors_ptr(s% colors_handle, colors_settings, ierr)
     112            1 :             if (ierr /= 0) then
     113            0 :                write(*,*) 'colors_ptr failed in alloc_colors_handle'
     114            0 :                return
     115              :             end if
     116              :          end if
     117              :       end subroutine set_colors_handles
     118              : 
     119            2 :       subroutine do_star_init( &
     120              :             my_mesa_dir, chem_isotopes_filename, &
     121              :             net_reaction_filename, jina_reaclib_filename, &
     122              :             use_suzuki_weak_rates, &
     123              :             use_special_weak_rates, special_weak_states_file, special_weak_transitions_file, &
     124              :             reaclib_min_T9, &
     125              :             rate_tables_dir, rates_cache_suffix, &
     126              :             ionization_file_prefix, ionization_Z1_suffix, &
     127              :             eosDT_cache_dir, &
     128              :             ionization_cache_dir, kap_cache_dir, rates_cache_dir, &
     129              :             ierr)
     130              :          use paquette_coeffs, only: initialise_collision_integrals
     131              :          use alloc, only: init_alloc
     132              :          character (len=*), intent(in) :: &
     133              :             my_mesa_dir, chem_isotopes_filename, net_reaction_filename, &
     134              :             jina_reaclib_filename, rate_tables_dir, &
     135              :             special_weak_states_file, special_weak_transitions_file, &
     136              :             rates_cache_suffix, &
     137              :             ionization_file_prefix, ionization_Z1_suffix, &
     138              :             eosDT_cache_dir, &
     139              :             ionization_cache_dir, kap_cache_dir, rates_cache_dir
     140              :          logical, intent(in) :: use_suzuki_weak_rates, use_special_weak_rates
     141              :          real(dp), intent(in) :: reaclib_min_T9
     142              :          integer, intent(out) :: ierr
     143              :          include 'formats'
     144            1 :          ierr = 0
     145            1 :          if (have_done_starlib_init) return
     146            1 :          call initialise_collision_integrals
     147            1 :          call init_alloc
     148              :          call stardata_init( &
     149              :             my_mesa_dir, chem_isotopes_filename, &
     150              :             net_reaction_filename, jina_reaclib_filename, &
     151              :             use_suzuki_weak_rates, &
     152              :             use_special_weak_rates, special_weak_states_file, special_weak_transitions_file, &
     153              :             reaclib_min_T9, &
     154              :             rate_tables_dir, rates_cache_suffix, &
     155              :             ionization_file_prefix, ionization_Z1_suffix, &
     156              :             eosDT_cache_dir, &
     157              :             ionization_cache_dir, kap_cache_dir, rates_cache_dir, &
     158            1 :             ierr)
     159            1 :          if (ierr /= 0) then
     160            0 :             write(*,*) 'failed in stardata_init'
     161            0 :             return
     162              :          end if
     163              : 
     164            1 :          have_done_starlib_init = .true.
     165              : 
     166              :       end subroutine do_star_init
     167              : 
     168              : 
     169            1 :       subroutine do_starlib_shutdown
     170              : 
     171              :         use alloc, only: shutdown_alloc
     172              :         use micro, only: shutdown_microphys
     173              :         use atm_lib, only: atm_shutdown
     174              :         use colors_lib, only: colors_shutdown
     175              :         use chem_lib, only: chem_shutdown
     176              :         use rates_lib, only: rates_shutdown
     177              :         use star_profile_def, only: profile_column_names_shutdown
     178              :         use star_history_def, only: history_column_names_shutdown
     179              :         use paquette_coeffs, only: free_collision_integrals
     180              : 
     181            1 :         call rates_shutdown()
     182            1 :         call atm_shutdown()
     183            1 :         call shutdown_microphys()
     184            1 :         call colors_shutdown()
     185            1 :         call chem_shutdown()
     186            1 :         call profile_column_names_shutdown()
     187            1 :         call history_column_names_shutdown()
     188              : 
     189            1 :         call free_collision_integrals()
     190              : 
     191            1 :         call shutdown_alloc()
     192              : 
     193            1 :         have_done_starlib_init = .false.
     194              : 
     195            1 :       end subroutine do_starlib_shutdown
     196              : 
     197              : 
     198            2 :       subroutine alloc_star_data(id, ierr)
     199              :          use chem_def, only: num_categories
     200              :          use net, only: default_set_rate_factors, &
     201              :             default_set_op_mono_factors
     202              : 
     203              : 
     204              :          integer, intent(out) :: id, ierr
     205              : 
     206              :          type (star_info), pointer :: s
     207              :          integer, parameter :: init_alloc_nvar = 20
     208              :          character (len=32) :: extra_name
     209              :          integer :: i
     210              : 
     211              :          ierr = 0
     212              : 
     213            2 :          call alloc_star(id, ierr)
     214            2 :          if (ierr /= 0) then
     215            0 :             write(*,*) 'alloc_star_data failed in alloc_star'
     216            0 :             return
     217              :          end if
     218              : 
     219            2 :          call get_star_ptr(id, s, ierr)
     220            2 :          if (ierr /= 0) then
     221            0 :             write(*,*) 'alloc_star_data failed in get_star_ptr'
     222            0 :             return
     223              :          end if
     224              : 
     225            2 :          nullify(s% dq)
     226            2 :          nullify(s% xa)
     227            2 :          nullify(s% xh)
     228              : 
     229              :          nullify( &
     230            2 :             s% op_mono_umesh1, s% op_mono_semesh1, s% op_mono_ff1, &
     231            2 :             s% op_mono_rs1)
     232              : 
     233            2 :          nullify(s% atm_structure)
     234            2 :          s% atm_structure_num_pts = 0
     235              : 
     236            2 :          nullify(s% chem_id)
     237            2 :          nullify(s% xa_removed)
     238              : 
     239            2 :          nullify(s% rate_factors)
     240            2 :          s% set_rate_factors => default_set_rate_factors
     241              : 
     242            2 :          nullify(s% op_mono_factors)
     243            2 :          s% set_op_mono_factors => default_set_op_mono_factors
     244              : 
     245            2 :          allocate(s% nameofvar(init_alloc_nvar),stat=ierr)
     246            2 :          if (ierr /= 0) return
     247              : 
     248            2 :          allocate(s% nameofequ(init_alloc_nvar),stat=ierr)
     249            2 :          if (ierr /= 0) return
     250              : 
     251            2 :          nullify(s% history_values)
     252            2 :          nullify(s% history_value_is_integer)
     253            2 :          nullify(s% history_names)
     254            2 :          nullify(s% history_names_dict)
     255              : 
     256            2 :          nullify(s% prev_mesh_xh)
     257            2 :          nullify(s% prev_mesh_xa)
     258            2 :          nullify(s% prev_mesh_j_rot)
     259            2 :          nullify(s% prev_mesh_omega)
     260            2 :          nullify(s% prev_mesh_dq)
     261              : 
     262            2 :          nullify(s% D_ST_start)
     263            2 :          nullify(s% prev_mesh_D_ST_start)
     264              : 
     265            2 :          nullify(s% other_star_info)
     266              : 
     267            2 :          nullify(s% bcyclic_odd_storage)
     268            2 :          nullify(s% bcyclic_odd_storage_qp)
     269              : 
     270            2 :          nullify(s% solver_iwork)
     271            2 :          nullify(s% solver_work)
     272            2 :          nullify(s% AF1)
     273              : 
     274            2 :          s% net_name = ''
     275            2 :          s% species = 0
     276            2 :          s% num_reactions = 0
     277              : 
     278            2 :          s% M_center = 0
     279            2 :          s% R_center = 0
     280            2 :          s% v_center = 0
     281            2 :          s% L_center = 0
     282              : 
     283            2 :          nullify(s% profile_column_spec)
     284            2 :          nullify(s% history_column_spec)
     285              : 
     286            2 :          s% num_conv_boundaries = 0
     287            2 :          nullify(s% conv_bdy_q)
     288            2 :          nullify(s% conv_bdy_loc)
     289            2 :          nullify(s% top_conv_bdy)
     290              : 
     291            2 :          s% num_mix_boundaries = 0
     292            2 :          nullify(s% mix_bdy_q)
     293            2 :          nullify(s% mix_bdy_loc)
     294            2 :          nullify(s% top_mix_bdy)
     295              : 
     296            2 :          nullify(s% burn_h_conv_region)
     297            2 :          nullify(s% burn_he_conv_region)
     298            2 :          nullify(s% burn_z_conv_region)
     299              : 
     300            2 :          s% have_burner_storage = .false.
     301            2 :          s% burner_num_threads = 0
     302            2 :          nullify(s% burner_storage)
     303              : 
     304            2 :          s% doing_timing = .false.
     305            2 :          s% time_evolve_step = 0
     306            2 :          s% time_remesh = 0
     307            2 :          s% time_adjust_mass = 0
     308            2 :          s% time_conv_premix = 0
     309            2 :          s% time_element_diffusion = 0
     310            2 :          s% time_struct_burn_mix = 0
     311            2 :          s% time_solver_matrix = 0
     312            2 :          s% time_solve_mix = 0
     313            2 :          s% time_solve_burn = 0
     314            2 :          s% time_solve_omega_mix = 0
     315            2 :          s% time_eos = 0
     316            2 :          s% time_neu_kap = 0
     317            2 :          s% time_nonburn_net = 0
     318            2 :          s% time_mlt = 0
     319            2 :          s% time_set_hydro_vars = 0
     320            2 :          s% time_set_mixing_info = 0
     321            2 :          s% time_total = 0
     322              : 
     323            2 :          s% timing_num_get_eos_calls = 0
     324            2 :          s% timing_num_solve_eos_calls = 0
     325            2 :          s% timing_num_get_kap_calls = 0
     326              : 
     327            2 :          s% model_profile_filename = ''
     328            2 :          s% most_recent_profile_filename = ''
     329              : 
     330            2 :          s% model_controls_filename = ''
     331            2 :          s% most_recent_controls_filename = ''
     332              : 
     333            2 :          s% most_recent_photo_name = ''
     334              : 
     335            2 :          s% doing_flash_wind = .false.
     336            2 :          s% doing_rlo_wind = .false.
     337              : 
     338            2 :          s% phase_of_evolution = phase_starting
     339            2 :          s% recent_log_header = -1000
     340              : 
     341            2 :          s% tau_base = 2d0/3d0
     342            2 :          s% tau_factor = 1
     343              : 
     344            2 :          s% solver_iter = 0
     345              : 
     346            2 :          s% using_gold_tolerances = .false.
     347            2 :          s% using_velocity_time_centering = .false.
     348              : 
     349            2 :          s% using_revised_net_name = .false.
     350            2 :          s% revised_net_name = ''
     351              : 
     352            2 :          s% using_revised_max_yr_dt = .false.
     353            2 :          s% revised_max_yr_dt = 0
     354              : 
     355            2 :          s% astero_using_revised_max_yr_dt = .false.
     356            2 :          s% astero_revised_max_yr_dt = 0
     357              : 
     358            2 :          s% cumulative_energy_error = 0
     359            2 :          s% cumulative_extra_heating = 0
     360              : 
     361            2 :          s% have_initial_energy_integrals = .false.
     362              : 
     363            2 :          s% num_solver_iterations = 0
     364            2 :          s% mesh_call_number = 0
     365            2 :          s% solver_call_number = 0
     366            2 :          s% diffusion_call_number = 0
     367            2 :          s% model_number = 0
     368            2 :          s% RSP_have_set_velocities = .false.
     369            2 :          s% RSP_just_set_velocities = .false.
     370            2 :          s% rsp_period = 0d0
     371              : 
     372            2 :          s% dt = 0d0
     373            2 :          s% mstar_dot = 0d0
     374              : 
     375            2 :          s% power_nuc_burn = -1
     376            2 :          s% power_h_burn = -1
     377            2 :          s% power_he_burn = -1
     378            2 :          s% power_z_burn = -1
     379            2 :          s% power_photo = -1
     380              : 
     381            2 :          s% k_const_mass = 1
     382            2 :          s% k_below_just_added = 1
     383            2 :          s% k_below_const_q = 1
     384              : 
     385            2 :          s% why_Tlim = Tlim_struc
     386          118 :          s% dt_why_count(:) = 0
     387          118 :          s% dt_why_retry_count(:) = 0
     388              : 
     389            2 :          s% len_extra_iwork = 0
     390            2 :          s% len_extra_work = 0
     391              : 
     392            2 :          s% eos_handle = 0
     393            2 :          s% kap_handle = 0
     394            2 :          s% colors_handle = 0
     395              : 
     396          202 :          do i = 1, max_num_profile_extras
     397          200 :             if (i < 10) then
     398           18 :                write(extra_name,'(a,i1)') 'profile_extra_', i
     399          182 :             else if (i < 100) then
     400          180 :                write(extra_name,'(a,i2)') 'profile_extra_', i
     401              :             else
     402            2 :                write(extra_name,'(a,i3)') 'profile_extra_', i
     403              :             end if
     404          202 :             s% profile_extra_name(i) = trim(extra_name)
     405              :          end do
     406              : 
     407            2 :          call set_starting_star_data(s, ierr)
     408            2 :          if (ierr /= 0) then
     409            0 :             write(*,*) 'alloc_star_data failed in set_starting_star_data'
     410            0 :             return
     411              :          end if
     412              : 
     413            6 :       end subroutine alloc_star_data
     414              : 
     415              : 
     416           11 :       subroutine null_other_new_generation(id, ierr)
     417              :          integer, intent(in) :: id
     418              :          integer, intent(out) :: ierr
     419           11 :          ierr = 0
     420           11 :       end subroutine null_other_new_generation
     421              : 
     422              : 
     423           11 :       subroutine null_other_set_current_to_old(id)
     424              :          integer, intent(in) :: id
     425           11 :       end subroutine null_other_set_current_to_old
     426              : 
     427              : 
     428            2 :       subroutine set_starting_star_data(s, ierr)
     429              :          use other_wind, only: null_other_wind
     430              :          use other_accreting_state, only: null_other_accreting_state
     431              :          use other_adjust_mdot, only: null_other_adjust_mdot
     432              :          use other_j_for_adjust_J_lost, only: null_other_j_for_adjust_J_lost
     433              :          use other_eval_fp_ft, only: null_other_eval_fp_ft
     434              :          use other_eval_i_rot, only: null_other_eval_i_rot
     435              :          use other_torque, only: default_other_torque
     436              :          use other_torque_implicit, only: default_other_torque_implicit
     437              :          use other_remove_surface, only: default_other_remove_surface
     438              :          use other_momentum, only: default_other_momentum
     439              :          use other_momentum_implicit, only: default_other_momentum_implicit
     440              :          use other_pressure, only: default_other_pressure
     441              :          use other_energy, only: default_other_energy
     442              :          use other_energy_implicit, only: default_other_energy_implicit
     443              :          use other_D_mix, only: null_other_D_mix
     444              :          use other_am_mixing, only: null_other_am_mixing
     445              :          use other_brunt, only: default_other_brunt
     446              :          use other_brunt_smoothing, only: null_other_brunt_smoothing
     447              :          use other_adjust_mlt_gradT_fraction, only: &
     448              :             default_other_adjust_mlt_gradT_fraction
     449              :          use other_after_set_mixing_info, only: &
     450              :             default_other_after_set_mixing_info
     451              :          use other_after_solver_setmatrix, only: &
     452              :             default_other_after_solver_setmatrix
     453              :          use other_diffusion, only: null_other_diffusion
     454              :          use other_diffusion_factor, only: default_other_diffusion_factor
     455              :          use other_neu, only: null_other_neu
     456              :          use other_net_get, only: null_other_net_get
     457              :          use other_cgrav, only: default_other_cgrav
     458              :          use other_mesh_delta_coeff_factor, only: default_other_mesh_delta_coeff_factor
     459              :          use other_alpha_mlt, only: default_other_alpha_mlt
     460              :          use other_mlt_results, only: null_other_mlt_results
     461              :          use other_opacity_factor, only: default_other_opacity_factor
     462              :          use other_pgstar_plots, only: null_other_pgstar_plots_info
     463              :          use other_mesh_functions
     464              :          use other_eps_grav, only: null_other_eps_grav
     465              :          use other_rsp_build_model, only: null_other_rsp_build_model
     466              :          use other_rsp_linear_analysis, only: null_other_rsp_linear_analysis
     467              :          use other_gradr_factor, only: null_other_gradr_factor
     468              :          use other_surface_PT, only: null_other_surface_PT
     469              :          use other_timestep_limit, only: null_other_timestep_limit
     470              :          use other_overshooting_scheme, only: null_other_overshooting_scheme
     471              :          use other_photo_write, only: default_other_photo_write
     472              :          use other_photo_read, only: default_other_photo_read
     473              :          use other_set_pgstar_controls, only: default_other_set_pgstar_controls
     474              :          use other_kap
     475              :          use star_utils, only: init_random
     476              : 
     477              :          type (star_info), pointer :: s
     478              :          integer, intent(out) :: ierr
     479              : 
     480              :          ! note: keep the handles for eos, kap, and net
     481              : 
     482            2 :          ierr = 0
     483              : 
     484            2 :          s% model_number = 0
     485            2 :          s% time = 0
     486            2 :          s% dt = 0
     487              : 
     488            2 :          s% total_num_solver_iterations = 0
     489            2 :          s% total_num_solver_relax_iterations = 0
     490            2 :          s% total_num_solver_calls_made = 0
     491            2 :          s% total_num_solver_relax_calls_made = 0
     492            2 :          s% total_num_solver_calls_converged = 0
     493            2 :          s% total_num_solver_relax_calls_converged = 0
     494              : 
     495            2 :          s% num_solver_iterations = 0
     496            2 :          s% num_skipped_setvars = 0
     497            2 :          s% num_setvars = 0
     498            2 :          s% num_solver_setvars = 0
     499            2 :          s% num_retries = 0
     500            2 :          s% num_hydro_merges = 0
     501            2 :          s% num_hydro_splits = 0
     502            2 :          s% timestep_hold = -1
     503              : 
     504            2 :          s% mesh_call_number = 0
     505            2 :          s% solver_call_number = 0
     506            2 :          s% diffusion_call_number = 0
     507            2 :          s% model_number_for_last_retry = 0
     508            2 :          s% dt_limit_ratio = 0
     509            2 :          s% force_tau_factor = 0
     510            2 :          s% force_Tsurf_factor = 0
     511            2 :          s% force_opacity_factor = 0
     512              : 
     513            2 :          s% generations = 0
     514              : 
     515            2 :          s% nvar_hydro = 0
     516            2 :          s% nvar_chem = 0
     517            2 :          s% nvar_total = 0
     518              : 
     519            2 :          s% nz = 0
     520            2 :          s% prev_mesh_nz = 0
     521            2 :          s% net_name = ''
     522            2 :          s% species = 0
     523            2 :          s% num_reactions = 0
     524              : 
     525            2 :          s% fix_Pgas = .false.
     526            2 :          s% v_flag = .false.
     527            2 :          s% u_flag = .false.
     528            2 :          s% rotation_flag = .false.
     529            2 :          s% RTI_flag = .false.
     530            2 :          s% w_div_wc_flag = .false.
     531            2 :          s% D_omega_flag = .false.
     532            2 :          s% am_nu_rot_flag = .false.
     533            2 :          s% RSP_flag = .false.
     534            2 :          s% RSP2_flag = .false.
     535              : 
     536            2 :          s% have_mixing_info = .false.
     537            2 :          s% doing_solver_iterations = .false.
     538            2 :          s% need_to_setvars = .true.
     539            2 :          s% okay_to_set_mixing_info = .true.
     540            2 :          s% okay_to_set_mlt_vc = .false.  ! not until have set mlt_cv_old
     541            2 :          s% have_mlt_vc = .false.
     542              : 
     543            2 :          s% have_ST_start_info = .false.
     544            2 :          s% prev_mesh_have_ST_start_info = .false.
     545              : 
     546            2 :          s% just_wrote_terminal_header = .false.
     547            2 :          s% doing_relax = .false.
     548            2 :          s% mstar_dot = 0
     549            2 :          s% surf_lnS = 0
     550              : 
     551            2 :          s% termination_code = -1
     552              : 
     553            2 :          s% screening_mode_value = -1
     554              : 
     555            2 :          s% dt = -1
     556            2 :          s% dt_next = -1
     557            2 :          s% dt_next_unclipped = -1
     558              : 
     559            2 :          s% i_lnd = 0
     560            2 :          s% i_lnT = 0
     561            2 :          s% i_lnR = 0
     562            2 :          s% i_lum = 0
     563            2 :          s% i_v = 0
     564            2 :          s% i_u = 0
     565            2 :          s% i_alpha_RTI = 0
     566              : 
     567            2 :          s% i_dv_dt = 0
     568            2 :          s% i_du_dt = 0
     569            2 :          s% i_equL = 0
     570            2 :          s% i_dlnd_dt = 0
     571            2 :          s% i_dlnE_dt = 0
     572            2 :          s% i_dlnR_dt = 0
     573            2 :          s% i_dalpha_RTI_dt = 0
     574              : 
     575            2 :          s% op_mono_nptot = 0
     576            2 :          s% op_mono_ipe = 0
     577            2 :          s% op_mono_nrad = 0
     578            2 :          s% op_mono_n = 0
     579              : 
     580            2 :          s% number_of_history_columns = -1
     581            2 :          s% model_number_of_history_values = -1
     582            2 :          s% need_to_set_history_names_etc = .true.
     583            2 :          s% doing_finish_load_model = .false.
     584              : 
     585            2 :          nullify(s% finish_relax_step)
     586            2 :          nullify(s% finished_relax)
     587              : 
     588              :          s% how_many_extra_profile_header_items => &
     589            2 :             null_how_many_extra_header_items
     590              :          s% data_for_extra_profile_header_items => &
     591            2 :             null_data_for_extra_header_items
     592              : 
     593              :          s% how_many_extra_history_header_items => &
     594            2 :             null_how_many_extra_header_items
     595              :          s% data_for_extra_history_header_items => &
     596            2 :             null_data_for_extra_header_items
     597              : 
     598            2 :          s% other_wind => null_other_wind
     599            2 :          s% other_accreting_state => null_other_accreting_state
     600            2 :          s% other_adjust_mdot => null_other_adjust_mdot
     601            2 :          s% other_j_for_adjust_J_lost => null_other_j_for_adjust_J_lost
     602            2 :          s% other_D_mix => null_other_D_mix
     603            2 :          s% other_am_mixing => null_other_am_mixing
     604            2 :          s% other_eval_fp_ft => null_other_eval_fp_ft
     605            2 :          s% other_eval_i_rot => null_other_eval_i_rot
     606            2 :          s% other_torque => default_other_torque
     607            2 :          s% other_torque_implicit => default_other_torque_implicit
     608            2 :          s% other_remove_surface => default_other_remove_surface
     609            2 :          s% other_momentum => default_other_momentum
     610            2 :          s% other_momentum_implicit => default_other_momentum_implicit
     611            2 :          s% other_pressure => default_other_pressure
     612            2 :          s% other_energy => default_other_energy
     613            2 :          s% other_energy_implicit => default_other_energy_implicit
     614            2 :          s% other_cgrav => default_other_cgrav
     615            2 :          s% other_mesh_delta_coeff_factor => default_other_mesh_delta_coeff_factor
     616            2 :          s% other_alpha_mlt => default_other_alpha_mlt
     617            2 :          s% other_opacity_factor => default_other_opacity_factor
     618            2 :          s% other_brunt => default_other_brunt
     619            2 :          s% other_brunt_smoothing => null_other_brunt_smoothing
     620            2 :          s% other_adjust_mlt_gradT_fraction => default_other_adjust_mlt_gradT_fraction
     621            2 :          s% other_after_set_mixing_info => default_other_after_set_mixing_info
     622            2 :          s% other_after_solver_setmatrix => default_other_after_solver_setmatrix
     623            2 :          s% other_diffusion => null_other_diffusion
     624            2 :          s% other_diffusion_factor => default_other_diffusion_factor
     625            2 :          s% other_mlt_results => null_other_mlt_results
     626            2 :          s% other_neu => null_other_neu
     627            2 :          s% other_net_get => null_other_net_get
     628            2 :          s% other_eps_grav => null_other_eps_grav
     629            2 :          s% other_rsp_build_model => null_other_rsp_build_model
     630            2 :          s% other_rsp_linear_analysis => null_other_rsp_linear_analysis
     631            2 :          s% other_gradr_factor => null_other_gradr_factor
     632              : 
     633            2 :          s% other_kap_get => null_other_kap_get
     634            2 :          s% other_kap_get_op_mono => null_other_kap_get_op_mono
     635              : 
     636            2 :          s% other_surface_PT => null_other_surface_PT
     637              : 
     638            2 :          s% other_timestep_limit => null_other_timestep_limit
     639            2 :          s% other_overshooting_scheme => null_other_overshooting_scheme
     640              : 
     641            2 :          s% other_pgstar_plots_info => null_other_pgstar_plots_info
     642            2 :          s% how_many_other_mesh_fcns => null_how_many_other_mesh_fcns
     643            2 :          s% other_mesh_fcn_data => null_other_mesh_fcn_data
     644              : 
     645            2 :          s% other_photo_write => default_other_photo_write
     646            2 :          s% other_photo_read => default_other_photo_read
     647              : 
     648            2 :          s% other_set_pgstar_controls => default_other_set_pgstar_controls
     649              : 
     650            2 :          s% other_new_generation => null_other_new_generation
     651            2 :          s% other_set_current_to_old => null_other_set_current_to_old
     652              : 
     653            2 :          nullify(s% extra_profile_col_names)
     654            2 :          nullify(s% extra_profile_col_vals)
     655            2 :          s% num_extra_profile_cols = 0
     656              : 
     657            2 :          s% binary_id = 0
     658            2 :          s% include_binary_history_in_log_file = .false.
     659            2 :          s% how_many_binary_history_columns => null_how_many_binary_history_columns
     660            2 :          s% data_for_binary_history_columns => null_data_for_binary_history_columns
     661            2 :          s% how_many_extra_binary_history_columns => null_how_many_extra_binary_history_columns
     662            2 :          s% data_for_extra_binary_history_columns => null_data_for_extra_binary_history_columns
     663              : 
     664            2 :          s% generations = 0
     665              : 
     666            2 :          s% nz = 0
     667              : 
     668            2 :          s% nvar_hydro = 0
     669            2 :          s% nvar_chem = 0
     670            2 :          s% nvar_total = 0
     671              : 
     672            2 :          s% species = 0
     673            2 :          s% num_reactions = 0
     674              : 
     675            2 :          s% model_number = 0
     676            2 :          s% mstar = 0
     677            2 :          s% xmstar = 0
     678            2 :          s% M_center = 0
     679            2 :          s% v_center = 0
     680            2 :          s% R_center = 0
     681            2 :          s% L_center = 0
     682            2 :          s% time = 0
     683            2 :          s% total_angular_momentum = 0
     684              : 
     685            2 :          s% dt = 0
     686              : 
     687            2 :          s% have_previous_conv_vel = .false.
     688              : 
     689            2 :          s% net_name = ''
     690              : 
     691            2 :          s% mstar_dot = 0
     692            2 :          s% v_surf = 0
     693            2 :          s% L_nuc_burn_total = 0
     694           50 :          s% L_by_category = 0
     695            2 :          s% dt_limit_ratio = 0
     696            2 :          s% L_phot = 0
     697            2 :          s% T_surf = 0
     698            2 :          s% P_surf = 0
     699              : 
     700            2 :          s% gradT_excess_alpha = 0
     701            2 :          s% gradT_excess_alpha_old = 0
     702              : 
     703            2 :          s% h1_czb_mass = 0
     704              : 
     705            2 :          s% he_core_mass = 0
     706            2 :          s% co_core_mass = 0
     707            2 :          s% Teff = -1  ! need to calculate it
     708            2 :          s% center_eps_nuc = 0
     709            2 :          s% Lrad_div_Ledd_avg_surf = 0
     710            2 :          s% w_div_w_crit_avg_surf = 0
     711            2 :          s% total_internal_energy = 0d0
     712            2 :          s% total_gravitational_energy = 0d0
     713            2 :          s% total_radial_kinetic_energy = 0d0
     714            2 :          s% total_turbulent_energy = 0d0
     715            2 :          s% total_rotational_kinetic_energy = 0d0
     716            2 :          s% total_energy = 0d0
     717              : 
     718            2 :          s% n_conv_regions = 0
     719          202 :          s% cz_bot_mass(:) = 0
     720          202 :          s% cz_top_mass(:) = 0
     721            2 :          s% dt_next = 0
     722              : 
     723            2 :          s% model_profile_filename = ''
     724            2 :          s% model_controls_filename = ''
     725            2 :          s% model_data_filename = ''
     726              : 
     727            2 :          s% most_recent_profile_filename = ''
     728            2 :          s% most_recent_controls_filename = ''
     729              : 
     730            2 :          s% most_recent_model_data_filename = ''
     731              : 
     732            2 :          s% recent_log_header = -1000
     733            2 :          s% phase_of_evolution = 0
     734              : 
     735            2 :          s% num_solver_iterations = 0
     736            2 :          s% num_skipped_setvars = 0
     737            2 :          s% num_setvars = 0
     738            2 :          s% num_retries = 0
     739              : 
     740            2 :          s% num_hydro_merges = 0
     741            2 :          s% num_hydro_splits = 0
     742              : 
     743            2 :          s% timestep_hold = 0
     744            2 :          s% model_number_for_last_retry = 0
     745              : 
     746            2 :          s% mesh_call_number = 0
     747            2 :          s% solver_call_number = 0
     748            2 :          s% diffusion_call_number = 0
     749              : 
     750            2 :          s% num_rotation_solver_steps = 0
     751            2 :          s% num_diffusion_solver_steps = 0
     752            2 :          s% initial_timestep = 0
     753            2 :          s% why_Tlim = 0
     754            2 :          s% result_reason = 0
     755              : 
     756            2 :          s% have_new_generation = .false.
     757            2 :          s% have_new_cz_bdy_info = .false.
     758            2 :          s% need_to_update_history_now = .false.
     759            2 :          s% need_to_save_profiles_now = .false.
     760            2 :          s% save_profiles_model_priority = 0
     761              : 
     762            2 :          s% doing_flash_wind = .false.
     763            2 :          s% doing_rlo_wind = .false.
     764            2 :          s% most_recent_photo_name = ''
     765              : 
     766            2 :          s% len_extra_iwork = 0
     767            2 :          s% len_extra_work = 0
     768              : 
     769            2 :          s% crystal_core_boundary_mass = -1
     770            2 :          s% phase_sep_mixing_mass = -1
     771              : 
     772            2 :          call init_random(s)
     773              : 
     774            2 :       end subroutine set_starting_star_data
     775              : 
     776              : 
     777            0 :       subroutine create_pre_ms_model(id, ierr)
     778              :          integer, intent(in) :: id
     779              :          integer, intent(out) :: ierr
     780              :          character (len=0) :: model_dir
     781              :          call model_builder( &
     782              :             id, model_dir, do_create_pre_ms_model, &
     783            0 :             .false., 'restart_photo', ierr)
     784            0 :       end subroutine create_pre_ms_model
     785              : 
     786              : 
     787            0 :       subroutine create_initial_model(id, ierr)
     788              :          integer, intent(in) :: id
     789              :          integer, intent(out) :: ierr
     790              :          character (len=0) :: model_dir
     791              :          call model_builder( &
     792              :             id, model_dir, do_create_initial_model, &
     793            0 :             .false., 'restart_photo', ierr)
     794            0 :       end subroutine create_initial_model
     795              : 
     796              : 
     797            0 :       subroutine create_RSP_model(id, ierr)
     798              :          integer, intent(in) :: id
     799              :          integer, intent(out) :: ierr
     800              :          character (len=0) :: model_dir
     801              :          call model_builder( &
     802              :             id, model_dir, do_create_RSP_model, &
     803            0 :             .false., 'restart_photo', ierr)
     804            0 :       end subroutine create_RSP_model
     805              : 
     806              : 
     807            0 :       subroutine create_RSP2_model(id, ierr)
     808              :          integer, intent(in) :: id
     809              :          integer, intent(out) :: ierr
     810              :          character (len=0) :: model_dir
     811              :          call model_builder( &
     812              :             id, model_dir, do_create_RSP2_model, &
     813            0 :             .false., 'restart_photo', ierr)
     814            0 :       end subroutine create_RSP2_model
     815              : 
     816              : 
     817            1 :       subroutine load_zams_model(id, ierr)
     818              :          integer, intent(in) :: id
     819              :          integer, intent(out) :: ierr
     820              :          call model_builder( &
     821              :             id, '', do_load_zams_model, &
     822            1 :             .false., 'restart_photo', ierr)
     823            1 :       end subroutine load_zams_model
     824              : 
     825              : 
     826            0 :       subroutine load_saved_model(id, model_fname, ierr)
     827              :          integer, intent(in) :: id
     828              :          character (len=*), intent(in) :: model_fname
     829              :          integer, intent(out) :: ierr
     830              :          integer :: l
     831              :          l = len_trim(model_fname)
     832              :          call model_builder( &
     833              :             id, model_fname, do_load_saved_model, &
     834            0 :             .false., 'restart_photo', ierr)
     835            0 :       end subroutine load_saved_model
     836              : 
     837              : 
     838            0 :       subroutine load_restart_photo(id, restart_filename, ierr)
     839              :          integer, intent(in) :: id
     840              :          character (len=*), intent(in) :: restart_filename
     841              :          integer, intent(out) :: ierr
     842              :          call model_builder( &
     843            0 :             id, '', do_load_zams_model, .true., restart_filename, ierr)
     844            0 :       end subroutine load_restart_photo
     845              : 
     846              : 
     847              :       ! for both zams and pre-main-sequence
     848            1 :       subroutine model_builder( &
     849              :             id, model_info, do_which, restart, restart_filename, ierr)
     850              :          use net, only: set_net, do_micro_change_net
     851              :          use alloc, only: set_var_info
     852              :          use photo_in, only: read_star_photo
     853              :          use init_model, only: get_zams_model
     854              :          use star_utils, only: set_phase_of_evolution, yrs_for_init_timestep, &
     855              :             eval_deltaM_total_energy_integrals
     856              :          use adjust_xyz, only: set_z, set_y
     857              :          use pre_ms_model, only: build_pre_ms_model
     858              :          use create_initial_model, only: build_initial_model
     859              :          use rsp, only: build_rsp_model, rsp_total_energy_integrals
     860              :          use rsp_def, only: init_def
     861              :          use read_model, only: do_read_saved_model, &
     862              :             finish_load_model
     863              :          use relax, only: do_relax_to_limit, do_relax_mass, &
     864              :             do_relax_mass_scale, do_relax_num_steps, do_relax_to_radiative_core
     865              :          use auto_diff_support
     866              :          integer, intent(in) :: id, do_which
     867              :          character (len=*), intent(in) :: model_info, restart_filename
     868              :          logical, intent(in) :: restart
     869              :          integer, intent(out) :: ierr
     870              : 
     871              :          type (star_info), pointer :: s
     872              :          real(dp) :: initial_mass, initial_z
     873              :          real(dp), parameter :: lg_max_abs_mdot = -1000  ! use default
     874              :          real(dp), parameter :: change_mass_years_for_dt = 1
     875              :          real(dp), parameter :: min_mass_for_create_pre_ms = 0.03d0
     876              :          real(dp) :: total_radiation, warning_limit_for_max_residual
     877              :          integer :: k, num_trace_history_values
     878              :          real(dp) :: save_Pextra_factor
     879              :          character (len=256):: save_atm_option, &
     880              :             save_atm_T_tau_relation, save_atm_T_tau_opacity
     881            1 :          real(dp), allocatable, dimension(:) :: total_energy_profile
     882              : 
     883              :          include 'formats'
     884              : 
     885              :          ierr = 0
     886              : 
     887            1 :          call get_star_ptr(id, s, ierr)
     888            1 :          if (ierr /= 0) return
     889              : 
     890            1 :          initial_mass = s% initial_mass
     891            1 :          initial_z = s% initial_z
     892              : 
     893            1 :          if (s% initial_y < 0) s% initial_y = max(0d0, min(1d0, 0.24d0 + 2*initial_z))
     894              : 
     895            1 :          s% dt = 0
     896            1 :          s% termination_code = -1
     897              : 
     898            1 :          if (restart) then
     899            0 :             s% doing_first_model_of_run = .false.
     900            0 :             s% doing_first_model_after_restart = .true.
     901            0 :             call read_star_photo(s, restart_filename, ierr)
     902            0 :             if (ierr /= 0) return
     903            0 :             call check_initials
     904            0 :             call set_net(s, s% net_name, ierr)
     905            0 :             if (ierr /= 0) return
     906            0 :             if (s% rotation_flag) s% have_j_rot = .true.
     907            0 :             call init_def(s)  ! RSP
     908            0 :             call finish_load_model(s, restart, ierr)
     909            0 :             if (s% max_years_for_timestep > 0) &
     910            0 :                s% dt_next = min(s% dt_next, secyer*s% max_years_for_timestep)
     911            0 :             return
     912              :          end if
     913              : 
     914            1 :          num_trace_history_values = s% num_trace_history_values
     915            1 :          s% num_trace_history_values = 0
     916              : 
     917            1 :          warning_limit_for_max_residual = s% warning_limit_for_max_residual
     918            1 :          s% warning_limit_for_max_residual = 1d0
     919              : 
     920            1 :          s% doing_first_model_of_run = .true.
     921            1 :          s% doing_first_model_after_restart = .false.
     922              : 
     923            1 :          if (do_which == do_load_saved_model) then
     924            0 :             s% dt_next = -1
     925            0 :             call do_read_saved_model(s, model_info, ierr)
     926            0 :             if (ierr /= 0) then
     927            0 :                write(*,*) 'load failed in do_read_saved_model'
     928            0 :                return
     929              :             end if
     930            0 :             call check_initials
     931            0 :             if (s% dt_next < 0) s% dt_next = yrs_for_init_timestep(s)*secyer
     932              :          else
     933            1 :             s% net_name = s% default_net_name
     934            1 :             s% species = 0
     935            1 :             s% v_flag = .false.
     936            1 :             s% u_flag = .false.
     937            1 :             s% rotation_flag = .false.
     938            1 :             s% D_omega_flag = .false.
     939            1 :             s% am_nu_rot_flag = .false.
     940            1 :             s% star_mass = s% initial_mass
     941            1 :             s% mstar = s% initial_mass*Msun
     942            1 :             s% M_center = s% mstar - s% xmstar
     943            1 :             call set_var_info(s, ierr)
     944            1 :             if (ierr /= 0) then
     945            0 :                write(*,*) 'failed in set_var_info'
     946            0 :                return
     947              :             end if
     948            0 :             select case (do_which)
     949              :                case (do_create_initial_model)
     950            0 :                   if (s% initial_model_change_net) then
     951            0 :                      call do_micro_change_net(s, s% initial_model_new_net_name, ierr)
     952              :                   else
     953            0 :                      call set_net(s, s% net_name, ierr)
     954              :                   end if
     955            0 :                   if (ierr /= 0) then
     956            0 :                      write(*,*) 'failed in set_net'
     957            0 :                      return
     958              :                   end if
     959            0 :                   call build_initial_model(s, ierr)
     960            0 :                   if (ierr /= 0) then
     961            0 :                      write(*,*) 'failed in build_initial_model'
     962            0 :                      return
     963              :                   end if
     964            0 :                   s% generations = 1
     965            0 :                   s% dt_next = 1d-5*secyer
     966              :                case (do_create_pre_ms_model)
     967            0 :                   if (s% initial_mass < min_mass_for_create_pre_ms) then
     968            0 :                      write(*,'(A)')
     969            0 :                      write(*,'(A)')
     970            0 :                      write(*,'(A)')
     971            0 :                      write(*,'(a,1x,f5.2)') 'sorry: cannot create pre-ms smaller than', &
     972            0 :                         min_mass_for_create_pre_ms
     973              :                      write(*,'(a)') &
     974            0 :                         'please create pre-ms and then relax to lower mass as a separate operation'
     975            0 :                      write(*,'(A)')
     976            0 :                      write(*,'(a)') 'here is an example:'
     977            0 :                      write(*,'(a)') 'in your inlist &controls section, set initial_mass = 0.03'
     978            0 :                      write(*,'(a)') 'in the &star_job section, add something like these lines'
     979            0 :                      write(*,'(a)') '  relax_mass_scale = .true.'
     980            0 :                      write(*,'(a)') '  dlgm_per_step = 1d-3 ! log10(delta M/Msun/step)'
     981            0 :                      write(*,'(a)') '  new_mass = 2.863362d-3 ! 3 Mjupiter in Msun units'
     982            0 :                      write(*,'(a)') '  change_mass_years_for_dt = 1'
     983            0 :                      write(*,'(A)')
     984            0 :                      write(*,'(A)')
     985            0 :                      write(*,'(A)')
     986            0 :                      ierr = -1
     987            0 :                      return
     988              :                   end if
     989            0 :                   if (s% pre_ms_change_net) then
     990            0 :                      call do_micro_change_net(s, s% pre_ms_new_net_name, ierr)
     991              :                   else
     992            0 :                      call set_net(s, s% net_name, ierr)
     993              :                   end if
     994            0 :                   if (ierr /= 0) then
     995            0 :                      write(*,*) 'failed in set_net'
     996            0 :                      return
     997              :                   end if
     998            0 :                   write(*,2) 'species  mass', s% species, s% mstar/Msun
     999              : 
    1000            0 :                   call build_pre_ms_model(id, s, s% nvar_hydro, s% species, ierr)
    1001            0 :                   if (ierr /= 0) then
    1002            0 :                      write(*,*) 'failed in build_pre_ms_model'
    1003            0 :                      return
    1004              :                   end if
    1005            0 :                   s% generations = 1
    1006            0 :                   s% dt_next = 1d-5*secyer
    1007              :                case (do_load_zams_model)
    1008            1 :                   s% generations = 1
    1009            1 :                   call get_zams_model(s, s% zams_filename, ierr)
    1010            1 :                   if (ierr /= 0) then
    1011            0 :                      write(*,*) 'failed in get_zams_model'
    1012            0 :                      return
    1013              :                   end if
    1014            1 :                   if (s% dt_next <= 0d0) then
    1015            1 :                      s% dt_next = yrs_for_init_timestep(s)*secyer
    1016              :                   end if
    1017              :                case (do_create_RSP_model)
    1018            0 :                   call build_rsp_model(s, ierr)  ! like build_pre_ms_model
    1019            0 :                   if (ierr /= 0) then
    1020            0 :                      write(*,*) 'failed in build_rsp_model'
    1021            0 :                      return
    1022              :                   end if
    1023            0 :                   s% generations = 1
    1024            0 :                   s% dt_next = 1d-2*secyer
    1025              :                case (do_create_RSP2_model)
    1026              :                   !call build_rsp2_model(s, ierr) ! like build_pre_ms_model
    1027            0 :                   call mesa_error(__FILE__,__LINE__,'need to add build_rsp2_model')
    1028              :                   if (ierr /= 0) then
    1029              :                      write(*,*) 'failed in build_rsp2_model'
    1030              :                      return
    1031              :                   end if
    1032              :                   s% generations = 1
    1033            0 :                   s% dt_next = 1d-2*secyer
    1034              :                case default
    1035            0 :                   write(*,*) 'bad value for do_which in build_model'
    1036            0 :                   ierr = -1
    1037            1 :                   return
    1038              :             end select
    1039              :          end if
    1040              : 
    1041         2465 :          do k=1,s% nz
    1042         2465 :             s% extra_heat(k) = 0
    1043              :          end do
    1044              : 
    1045            1 :          call finish_load_model(s, restart, ierr)
    1046            1 :          if (ierr /= 0) then
    1047            0 :             write(*,*) 'failed in finish_load_model'
    1048            0 :             return
    1049              :          end if
    1050              : 
    1051            1 :          if (s% max_years_for_timestep > 0) &
    1052            0 :             s% dt_next = min(s% dt_next, secyer*s% max_years_for_timestep)
    1053            1 :          call set_phase_of_evolution(s)
    1054              : 
    1055            1 :          if (s% RSP_flag) then
    1056              :             call rsp_total_energy_integrals(s, &
    1057              :                s% total_internal_energy, &
    1058              :                s% total_gravitational_energy, &
    1059              :                s% total_radial_kinetic_energy, &
    1060              :                s% total_rotational_kinetic_energy, &
    1061              :                s% total_turbulent_energy, &
    1062            0 :                s% total_energy, total_radiation)
    1063              :          else
    1064            3 :             allocate(total_energy_profile(1:s% nz))
    1065              :             call eval_deltaM_total_energy_integrals( &
    1066              :                s, 1, s% nz, s% mstar, .false., &
    1067              :                total_energy_profile, &
    1068              :                s% total_internal_energy, &
    1069              :                s% total_gravitational_energy, &
    1070              :                s% total_radial_kinetic_energy, &
    1071              :                s% total_rotational_kinetic_energy, &
    1072              :                s% total_turbulent_energy, &
    1073            1 :                s% total_energy)
    1074              :          end if
    1075              : 
    1076            1 :          if (do_which == do_create_pre_ms_model) then
    1077            0 :             call setup_for_relax_after_create_pre_ms_model
    1078            0 :             if (s% mstar > s% initial_mass*Msun) then  ! need to reduce mass
    1079            0 :                write(*,1) 'reduce mass to', s% initial_mass
    1080            0 :                call do_relax_mass(s% id, s% initial_mass, lg_max_abs_mdot, ierr)
    1081            0 :                if (ierr /= 0) then
    1082            0 :                   write(*,*) 'failed in do_relax_mass'
    1083            0 :                   return
    1084              :                end if
    1085            0 :             else if (s% mstar < s% initial_mass*Msun) then  ! need to increase mass
    1086            0 :                write(*,1) 'increase mass to', s% initial_mass
    1087              :                call do_relax_mass_scale( &
    1088            0 :                   s% id, s% initial_mass, s% job% dlgm_per_step, s% job% change_mass_years_for_dt, ierr)
    1089            0 :                if (ierr /= 0) then
    1090            0 :                   write(*,*) 'failed in do_relax_mass'
    1091            0 :                   return
    1092              :                end if
    1093              :             end if
    1094              :             call do_relax_num_steps( &
    1095            0 :                s% id, s% pre_ms_relax_num_steps, s% dt_next, ierr)
    1096            0 :             if (ierr /= 0) then
    1097            0 :                write(*,*) 'failed in do_relax_num_steps'
    1098            0 :                return
    1099              :             end if
    1100              : 
    1101            0 :             if (s% job% pre_ms_relax_to_start_radiative_core) then
    1102            0 :                call do_relax_to_radiative_core(s% id, ierr)
    1103            0 :                if (ierr /= 0) then
    1104            0 :                   write(*,*) 'failed in do_relax_to_radiative_core'
    1105            0 :                   return
    1106              :                end if
    1107              :             end if
    1108            0 :             call done_relax_after_create_pre_ms_model
    1109            1 :          else if (do_which == do_create_initial_model .and. &
    1110              :                   s% initial_model_relax_num_steps > 0) then
    1111              :             call do_relax_num_steps( &
    1112            0 :                s% id, s% initial_model_relax_num_steps, s% dt_next, ierr)
    1113            0 :             if (ierr /= 0) then
    1114            0 :                write(*,*) 'failed in do_relax_num_steps'
    1115            0 :                return
    1116              :             end if
    1117              :          end if
    1118              : 
    1119            1 :          s% doing_first_model_of_run = .true.
    1120            1 :          s% num_trace_history_values = num_trace_history_values
    1121            1 :          s% warning_limit_for_max_residual = warning_limit_for_max_residual
    1122              : 
    1123              :          contains
    1124              : 
    1125            0 :          subroutine setup_for_relax_after_create_pre_ms_model
    1126            0 :             save_atm_option = s% atm_option
    1127            0 :             save_atm_T_tau_relation = s% atm_T_tau_relation
    1128            0 :             save_atm_T_tau_opacity = s% atm_T_tau_opacity
    1129            0 :             save_Pextra_factor = s% Pextra_factor
    1130            0 :             s% atm_option = 'T_tau'
    1131            0 :             s% atm_T_tau_relation = 'Eddington'
    1132            0 :             s% atm_T_tau_opacity = 'fixed'
    1133            0 :             s% Pextra_factor = 2
    1134            0 :          end subroutine setup_for_relax_after_create_pre_ms_model
    1135              : 
    1136            0 :          subroutine done_relax_after_create_pre_ms_model
    1137            0 :             s% atm_option = save_atm_option
    1138            0 :             s% atm_T_tau_relation = save_atm_T_tau_relation
    1139            0 :             s% atm_T_tau_opacity = save_atm_T_tau_opacity
    1140            0 :             s% Pextra_factor = save_Pextra_factor
    1141            0 :          end subroutine done_relax_after_create_pre_ms_model
    1142              : 
    1143            0 :          subroutine check_initials
    1144              :             include 'formats'
    1145              :             if (abs(initial_mass - s% initial_mass) > &
    1146            0 :                   1d-3*initial_mass .and. initial_mass > 0) then
    1147            0 :                write(*,1) "WARNING -- inlist initial_mass ignored", initial_mass
    1148            0 :                write(*,1) "using saved initial_mass instead", s% initial_mass
    1149            0 :                write(*,'(A)')
    1150              :             end if
    1151              :             if (abs(initial_z - s% initial_z) > &
    1152            0 :                   1d-3*initial_z .and. initial_z > 0) then
    1153            0 :                write(*,1) "WARNING -- inlist initial_z ignored", initial_z
    1154            0 :                write(*,1) "using saved initial_z instead", s% initial_z
    1155            0 :                write(*,'(A)')
    1156              :             end if
    1157            0 :          end subroutine check_initials
    1158              : 
    1159              :       end subroutine model_builder
    1160              : 
    1161              : 
    1162            1 :       logical function doing_restart(restart_filename)
    1163              :          character (len=*) :: restart_filename
    1164            1 :          inquire(file=restart_filename, exist=doing_restart)
    1165            1 :       end function doing_restart
    1166              : 
    1167              : 
    1168            0 :       integer function null_how_many_extra_header_items(id)
    1169              :          integer, intent(in) :: id
    1170            0 :          null_how_many_extra_header_items = 0
    1171            0 :       end function null_how_many_extra_header_items
    1172              : 
    1173              : 
    1174            0 :       subroutine null_data_for_extra_header_items(id, n, names, vals, ierr)
    1175              :          integer, intent(in) :: id, n
    1176              :          character (len=80) :: names(n)
    1177              :          real(dp) :: vals(n)
    1178              :          integer, intent(out) :: ierr
    1179            0 :          ierr = 0
    1180            0 :       end subroutine null_data_for_extra_header_items
    1181              : 
    1182              : 
    1183            0 :       integer function null_how_many_binary_history_columns(binary_id)
    1184              :          integer, intent(in) :: binary_id
    1185            0 :          null_how_many_binary_history_columns = 0
    1186            0 :       end function null_how_many_binary_history_columns
    1187              : 
    1188              : 
    1189            0 :       subroutine null_data_for_binary_history_columns( &
    1190              :             binary_id, n, names, vals, ierr)
    1191              :          use const_def, only: dp
    1192              :          integer, intent(in) :: binary_id, n
    1193              :          character (len=80) :: names(n)
    1194              :          real(dp) :: vals(n)
    1195              :          integer, intent(out) :: ierr
    1196            0 :          ierr = 0
    1197            0 :       end subroutine null_data_for_binary_history_columns
    1198              : 
    1199              : 
    1200            0 :       integer function null_how_many_extra_binary_history_columns(binary_id)
    1201              :          integer, intent(in) :: binary_id
    1202            0 :          null_how_many_extra_binary_history_columns = 0
    1203            0 :       end function null_how_many_extra_binary_history_columns
    1204              : 
    1205              : 
    1206            0 :       subroutine null_data_for_extra_binary_history_columns( &
    1207              :             binary_id, n, names, vals, ierr)
    1208              :          use const_def, only: dp
    1209              :          integer, intent(in) :: binary_id, n
    1210              :          character (len=80) :: names(n)
    1211              :          real(dp) :: vals(n)
    1212              :          integer, intent(out) :: ierr
    1213            0 :          ierr = 0
    1214            0 :       end subroutine null_data_for_extra_binary_history_columns
    1215              : 
    1216              : 
    1217            0 :       subroutine do_garbage_collection(eosDT_cache_dir, ierr)
    1218              :          use eos_lib, only: eos_init, eos_shutdown
    1219              :          use eos_def, only: use_cache_for_eos
    1220              :          integer, intent(inout) :: ierr
    1221              :          character (len=*), intent(in) :: eosDT_cache_dir
    1222              :          ! Remove existing eos data
    1223            0 :          call eos_shutdown()
    1224              :          ! Re-initialize eos
    1225              :          call eos_init(eosDT_cache_dir,&
    1226              :                ! After first init this is set in eos_def
    1227              :                use_cache_for_eos,&
    1228            0 :                ierr)
    1229            0 :       end subroutine do_garbage_collection
    1230              : 
    1231              :       end module init
        

Generated by: LCOV version 2.0-1