LCOV - code coverage report
Current view: top level - star/private - star_solver.f90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 30.6 % 1019 312
Test Date: 2026-12-03 14:59:23 Functions: 40.0 % 35 14

            Line data    Source code
       1              : ! ***********************************************************************
       2              : !
       3              : !   Copyright (C) 2013-2019  The MESA Team
       4              : !
       5              : !   This program is free software: you can redistribute it and/or modify
       6              : !   it under the terms of the GNU Lesser General Public License
       7              : !   as published by the Free Software Foundation,
       8              : !   either version 3 of the License, or (at your option) any later version.
       9              : !
      10              : !   This program is distributed in the hope that it will be useful,
      11              : !   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12              : !   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      13              : !   See the GNU Lesser General Public License for more details.
      14              : !
      15              : !   You should have received a copy of the GNU Lesser General Public License
      16              : !   along with this program. If not, see <https://www.gnu.org/licenses/>.
      17              : !
      18              : ! ***********************************************************************
      19              : 
      20              : 
      21              :       module star_solver
      22              : 
      23              :       use star_private_def
      24              :       use const_def, only: dp, i8
      25              :       use num_def
      26              :       use mtx_def
      27              :       use mtx_lib, only: block_multiply_xa
      28              :       use utils_lib, only: is_bad
      29              :       use solver_support
      30              : 
      31              :       implicit none
      32              : 
      33              :       private
      34              :       public :: solver
      35              : 
      36              :       contains
      37              : 
      38           22 :       subroutine solver( &
      39              :             s, nvar, skip_global_corr_coeff_limit, &
      40              :             gold_tolerances_level, tol_max_correction, tol_correction_norm, &
      41              :             convergence_failure, ierr)
      42              :          use alloc, only: non_crit_get_quad_array, non_crit_return_quad_array
      43              :          use utils_lib, only: realloc_if_needed_1, quad_realloc_if_needed_1, fill_with_NaNs
      44              : 
      45              :          type (star_info), pointer :: s
      46              :          ! the primary variables
      47              :          integer, intent(in) :: nvar  ! number of variables per zone
      48              :          logical, intent(in) :: skip_global_corr_coeff_limit
      49              : 
      50              :          ! convergence criteria
      51              :          integer, intent(in) :: gold_tolerances_level  ! 0, 1, or 2
      52              :          real(dp), intent(in) :: tol_max_correction, tol_correction_norm
      53              :             ! a trial solution is considered to have converged if
      54              :             ! max_correction <= tol_max_correction and
      55              :             !
      56              :             ! either
      57              :             !          (correction_norm <= tol_correction_norm)
      58              :             !    .and. (residual_norm <= tol_residual_norm)
      59              :             ! or
      60              :             !          (correction_norm*residual_norm <= tol_corr_resid_product)
      61              :             !    .and. (abs(slope) <= tol_abs_slope_min)
      62              :             !
      63              :             ! where "slope" is slope of the line for line search in the solver,
      64              :             ! and is analogous to the slope of df/ddx in a 1D solver root finder.
      65              : 
      66              :          ! output
      67              :          logical, intent(out) :: convergence_failure
      68              :          integer, intent(out) :: ierr  ! 0 means okay.
      69              : 
      70              :          integer :: ldAF, neqns
      71              : 
      72              :          include 'formats'
      73              : 
      74              :          ierr = 0
      75              : 
      76           11 :          neqns = nvar*s% nz
      77           11 :          ldAF = 3*nvar
      78              : 
      79           11 :          call realloc_if_needed_1(s% AF1,ldAF*neqns,(ldAF+2)*200,ierr)
      80           11 :          if (ierr /= 0) return
      81              : 
      82           11 :          if (s% fill_arrays_with_NaNs) call fill_with_NaNs(s% AF1)
      83              : 
      84              :          call do_solver( &
      85              :             s, nvar, s% AF1, ldAF, neqns, skip_global_corr_coeff_limit, &
      86              :             gold_tolerances_level, tol_max_correction, tol_correction_norm, &
      87           11 :             convergence_failure, ierr)
      88              : 
      89              :       end subroutine solver
      90              : 
      91              : 
      92           11 :       subroutine do_solver( &
      93              :             s, nvar, AF1, ldAF, neq, skip_global_corr_coeff_limit, &
      94              :             gold_tolerances_level, tol_max_correction, tol_correction_norm, &
      95              :             convergence_failure, ierr)
      96              : 
      97              :          type (star_info), pointer :: s
      98              : 
      99              :          integer, intent(in) :: nvar, ldAF, neq
     100              :          logical, intent(in) :: skip_global_corr_coeff_limit
     101              : 
     102              :          real(dp), pointer, dimension(:) :: AF1  ! =(ldAF, neq)
     103              : 
     104              :          ! controls
     105              :          integer, intent(in) :: gold_tolerances_level
     106              :          real(dp), intent(in) :: tol_max_correction, tol_correction_norm
     107              : 
     108              :          ! output
     109              :          logical, intent(out) :: convergence_failure
     110              :          integer, intent(out) :: ierr
     111              : 
     112              :          ! info saved in work arrays
     113              : 
     114              :          real(dp), dimension(:,:), pointer :: dxsave=>null(), ddxsave=>null(), B=>null(), grad_f=>null(), soln=>null()
     115              :          real(dp), dimension(:), pointer :: dxsave1=>null(), ddxsave1=>null(), B1=>null(), grad_f1=>null(), &
     116              :             row_scale_factors1=>null(), col_scale_factors1=>null(), soln1=>null(), &
     117              :             save_ublk1=>null(), save_dblk1=>null(), save_lblk1=>null(), band1=>null()
     118              :          real(dp), dimension(:,:), pointer :: rhs=>null()
     119              :          integer, dimension(:), pointer :: ipiv1=>null()
     120              :          real(dp), dimension(:,:), pointer :: ddx=>null(), xder=>null()
     121              : 
     122              :          integer, dimension(:), pointer :: ipiv_blk1=>null()
     123           11 :          character (len=s%nz) :: equed1
     124              : 
     125              :          real(dp), dimension(:), pointer :: lblk1=>null(), dblk1=>null(), ublk1=>null()
     126              :          real(dp), dimension(:), pointer :: lblkF1=>null(), dblkF1=>null(), ublkF1=>null()
     127              : 
     128              :          ! locals
     129              :          real(dp)  ::  &
     130              :             coeff, f, slope, residual_norm, max_residual, &
     131              :             corr_norm_min, resid_norm_min, correction_factor, temp_correction_factor, &
     132              :             correction_norm, max_correction, &
     133              :             tol_residual_norm, tol_max_residual, &
     134              :             tol_residual_norm2, tol_max_residual2, &
     135              :             tol_residual_norm3, tol_max_residual3, &
     136              :             tol_abs_slope_min, tol_corr_resid_product, &
     137              :             min_corr_coeff, max_corr_min, max_resid_min, max_abs_correction
     138              :          integer :: nz, iter, max_tries, tiny_corr_cnt, i, &
     139              :             force_iter_value, iter_for_resid_tol2, iter_for_resid_tol3, &
     140              :             max_corr_k, max_corr_j, max_resid_k, max_resid_j, &
     141              :             band_kl, band_ku, band_ldab, band_diag
     142              :          integer(i8) :: time0
     143              :          character (len=strlen) :: err_msg
     144              :          logical :: first_try, dbg_msg, passed_tol_tests, &
     145              :             doing_extra, disabled_resid_tests, pass_resid_tests, &
     146              :             pass_corr_tests_without_coeff, pass_corr_tests_with_coeff
     147              : 
     148              :          integer, parameter :: num_tol_msgs = 15
     149              :          character (len=32) :: tol_msg(num_tol_msgs)
     150              :          character (len=64) :: message
     151              : 
     152              :          real(dp), pointer, dimension(:) :: equ1=>null()
     153              :          real(dp), pointer, dimension(:,:) :: equ=>null()  ! (nvar,nz)
     154              :          real(dp), pointer, dimension(:,:) :: AF=>null()  ! (ldAF,neq)
     155              :          real(dp), pointer, dimension(:,:) :: band=>null()  ! (band_ldab,neq)
     156              :          real(dp), pointer, dimension(:,:,:) :: ublk=>null(), dblk=>null(), lblk=>null()  ! (nvar,nvar,nz)
     157              :          real(dp), dimension(:,:,:), pointer :: lblkF=>null(), dblkF=>null(), ublkF=>null()  ! (nvar,nvar,nz)
     158              : 
     159           11 :          call do_solver_work()
     160              :          ! Split it this way so we can guarantee cleanup() gets called once do_solver_work finishes
     161              :          ! Otherwise all the pointers will leak memory.
     162           11 :          call cleanup()
     163              : 
     164              :          contains
     165              : 
     166           55 :          subroutine do_solver_work
     167              :             integer :: i_lnT, k_max_dlnT
     168              :             real(dp) :: max_dlnT, dlnT_limit
     169              :             include 'formats'
     170              : 
     171           11 :             err_msg = ''
     172              : 
     173           11 :             nz = s% nz
     174              : 
     175           11 :             AF(1:ldAF,1:neq) => AF1(1:ldAF*neq)
     176              : 
     177           11 :             tol_msg(1) = 'avg corr'
     178           11 :             tol_msg(2) = 'max corr '
     179           11 :             tol_msg(3) = 'avg+max corr'
     180           11 :             tol_msg(4) = 'avg resid'
     181           11 :             tol_msg(5) = 'avg corr+resid'
     182           11 :             tol_msg(6) = 'max corr, avg resid'
     183           11 :             tol_msg(7) = 'avg+max corr, avg resid'
     184           11 :             tol_msg(8) = 'max resid'
     185           11 :             tol_msg(9) = 'avg corr, max resid'
     186           11 :             tol_msg(10) = 'max corr+resid'
     187           11 :             tol_msg(11) = 'avg+max corr, max resid'
     188           11 :             tol_msg(12) = 'avg+max resid'
     189           11 :             tol_msg(13) = 'avg corr, avg+max resid'
     190           11 :             tol_msg(14) = 'max corr, avg+max resid'
     191           11 :             tol_msg(15) = 'avg+max corr+resid'
     192              : 
     193           11 :             ierr = 0
     194           11 :             iter = 0
     195           11 :             s% solver_iter = iter
     196              : 
     197           11 :             call set_param_defaults
     198           11 :             dbg_msg = s% report_solver_progress
     199              : 
     200           11 :             if (gold_tolerances_level == 2) then
     201            0 :                tol_residual_norm = s% gold2_tol_residual_norm1
     202            0 :                tol_max_residual = s% gold2_tol_max_residual1
     203            0 :                tol_residual_norm2 = s% gold2_tol_residual_norm2
     204            0 :                tol_max_residual2 = s% gold2_tol_max_residual2
     205            0 :                tol_residual_norm3 = s% gold2_tol_residual_norm3
     206            0 :                tol_max_residual3 = s% gold2_tol_max_residual3
     207           11 :             else if (gold_tolerances_level == 1) then
     208           11 :                tol_residual_norm = s% gold_tol_residual_norm1
     209           11 :                tol_max_residual = s% gold_tol_max_residual1
     210           11 :                tol_residual_norm2 = s% gold_tol_residual_norm2
     211           11 :                tol_max_residual2 = s% gold_tol_max_residual2
     212           11 :                tol_residual_norm3 = s% gold_tol_residual_norm3
     213           11 :                tol_max_residual3 = s% gold_tol_max_residual3
     214              :             else
     215            0 :                tol_residual_norm = s% tol_residual_norm1
     216            0 :                tol_max_residual = s% tol_max_residual1
     217            0 :                tol_residual_norm2 = s% tol_residual_norm2
     218            0 :                tol_max_residual2 = s% tol_max_residual2
     219            0 :                tol_residual_norm3 = s% tol_residual_norm3
     220            0 :                tol_max_residual3 = s% tol_max_residual3
     221              :             end if
     222              : 
     223           11 :             tol_abs_slope_min = -1  ! unused
     224           11 :             tol_corr_resid_product = -1  ! unused
     225           11 :             if (skip_global_corr_coeff_limit) then
     226           11 :                min_corr_coeff = 1
     227              :             else
     228            0 :                min_corr_coeff = s% corr_coeff_limit
     229              :             end if
     230              : 
     231           11 :             if (gold_tolerances_level == 2) then
     232            0 :                iter_for_resid_tol2 = s% gold2_iter_for_resid_tol2
     233            0 :                iter_for_resid_tol3 = s% gold2_iter_for_resid_tol3
     234           11 :             else if (gold_tolerances_level == 1) then
     235           11 :                iter_for_resid_tol2 = s% gold_iter_for_resid_tol2
     236           11 :                iter_for_resid_tol3 = s% gold_iter_for_resid_tol3
     237              :             else
     238            0 :                iter_for_resid_tol2 = s% iter_for_resid_tol2
     239            0 :                iter_for_resid_tol3 = s% iter_for_resid_tol3
     240              :             end if
     241              : 
     242           11 :             call pointers(ierr)
     243           11 :             if (ierr /= 0) then
     244            0 :                return
     245              :             end if
     246              : 
     247           11 :             doing_extra = .false.
     248           11 :             passed_tol_tests = .false.  ! goes true when pass the tests
     249           11 :             convergence_failure = .false.  ! goes true when time to give up
     250           11 :             coeff = 1.d0
     251              : 
     252           11 :             residual_norm=0
     253           11 :             max_residual=0
     254           11 :             corr_norm_min=1d99
     255           11 :             max_corr_min=1d99
     256           11 :             max_resid_min=1d99
     257           11 :             resid_norm_min=1d99
     258           11 :             correction_factor=0
     259           11 :             f=0d0
     260           11 :             slope=0d0
     261              : 
     262           11 :             call set_xscale_info(s, nvar, ierr)
     263           11 :             if (ierr /= 0) then
     264            0 :                if (dbg_msg) &
     265            0 :                   write(*, *) 'solver failure: set_xscale_info returned ierr', ierr
     266            0 :                convergence_failure = .true.
     267            0 :                return
     268              :             end if
     269              : 
     270           11 :             call do_equations(ierr)
     271           11 :             if (ierr /= 0) then
     272            0 :                if (dbg_msg) &
     273            0 :                   write(*, *) 'solver failure: eval_equations returned ierr', ierr
     274            0 :                convergence_failure = .true.
     275            0 :                return
     276              :             end if
     277              : 
     278           11 :             call sizequ(s, nvar, residual_norm, max_residual, max_resid_k, max_resid_j, ierr)
     279           11 :             if (ierr /= 0) then
     280            0 :                if (dbg_msg) &
     281            0 :                   write(*, *) 'solver failure: sizequ returned ierr', ierr
     282            0 :                convergence_failure = .true.
     283            0 :                return
     284              :             end if
     285              : 
     286           11 :             first_try = .true.
     287           11 :             iter = 1
     288           11 :             s% solver_iter = iter
     289           11 :             if (s% doing_first_model_of_run) then
     290            1 :                max_tries = s% max_tries1
     291           10 :             else if (s% retry_cnt > 20) then
     292            0 :                max_tries = s% max_tries_after_20_retries
     293           10 :             else if (s% retry_cnt > 10) then
     294            0 :                max_tries = s% max_tries_after_10_retries
     295           10 :             else if (s% retry_cnt > 5) then
     296            0 :                max_tries = s% max_tries_after_5_retries
     297           10 :             else if (s% retry_cnt > 0) then
     298            0 :                max_tries = s% max_tries_for_retry
     299              :             else
     300           10 :                max_tries = s% solver_max_tries_before_reject
     301              :             end if
     302           11 :             tiny_corr_cnt = 0
     303              : 
     304           11 :             s% num_solver_iterations = 0
     305              : 
     306           42 :          iter_loop: do while (.not. passed_tol_tests)
     307              : 
     308           31 :                if (dbg_msg .and. first_try) write(*, *)
     309              : 
     310           31 :                max_resid_j = -1
     311           31 :                max_corr_j = -1
     312              : 
     313           31 :                if (iter >= iter_for_resid_tol2) then
     314            0 :                   if (iter < iter_for_resid_tol3) then
     315            0 :                      tol_residual_norm = tol_residual_norm2
     316            0 :                      tol_max_residual = tol_max_residual2
     317            0 :                      if (dbg_msg .and. iter == iter_for_resid_tol2) &
     318            0 :                         write(*,1) 'tol2 residual tolerances: norm, max', &
     319            0 :                            tol_residual_norm, tol_max_residual
     320              :                   else
     321            0 :                      tol_residual_norm = tol_residual_norm3
     322            0 :                      tol_max_residual = tol_max_residual3
     323            0 :                      if (dbg_msg .and. iter == iter_for_resid_tol3) &
     324            0 :                         write(*,1) 'tol3 residual tolerances: norm, max', &
     325            0 :                            tol_residual_norm, tol_max_residual
     326              :                   end if
     327           31 :                else if (dbg_msg .and. iter == 1) then
     328            0 :                   write(*,2) 'solver_call_number', s% solver_call_number
     329            0 :                   write(*,2) 'gold tolerances level', gold_tolerances_level
     330            0 :                   write(*,1) 'correction tolerances: norm, max', &
     331            0 :                      tol_correction_norm, tol_max_correction
     332            0 :                   write(*,1) 'tol1 residual tolerances: norm, max', &
     333            0 :                      tol_residual_norm, tol_max_residual
     334              :                end if
     335              : 
     336           31 :                call solver_test_partials(nvar, xder, ierr)
     337           31 :                if (ierr /= 0) then
     338            0 :                   call write_msg('solver_test_partials returned ierr /= 0')
     339            0 :                   convergence_failure = .true.
     340            0 :                   exit iter_loop
     341              :                end if
     342              : 
     343           31 :                s% num_solver_iterations = s% num_solver_iterations + 1
     344              :                if (s% model_number == 1 .and. &
     345           31 :                   s% num_solver_iterations > 60 .and. &
     346              :                   mod(s% num_solver_iterations,10) == 0) &
     347            0 :                      write(*,*) 'first model is slow to converge: num tries', &
     348            0 :                         s% num_solver_iterations
     349              : 
     350           31 :                if (.not. solve_equ()) then  ! either singular or horribly ill-conditioned
     351            0 :                   write(err_msg, '(a, i5, 3x, a)') 'info', ierr, 'bad_matrix'
     352            0 :                   call oops(err_msg)
     353            0 :                   exit iter_loop
     354              :                end if
     355              : 
     356           31 :                call inspectB(s, nvar, soln, ierr)
     357           31 :                if (ierr /= 0) then
     358            0 :                   call oops('inspectB returned ierr')
     359            0 :                   exit iter_loop
     360              :                end if
     361              : 
     362              :                ! compute size of scaled correction B
     363              :                call sizeB(s, nvar, soln, &
     364           31 :                      max_correction, correction_norm, max_corr_k, max_corr_j, ierr)
     365           31 :                if (ierr /= 0) then
     366            0 :                   call oops('correction rejected by sizeB')
     367            0 :                   exit iter_loop
     368              :                end if
     369              : 
     370           31 :                correction_norm = abs(correction_norm)
     371           31 :                max_abs_correction = abs(max_correction)
     372           31 :                corr_norm_min = min(correction_norm, corr_norm_min)
     373           31 :                max_corr_min = min(max_abs_correction, max_corr_min)
     374              : 
     375           31 :                if (is_bad_num(correction_norm) .or. is_bad_num(max_abs_correction)) then
     376              :                   ! bad news -- bogus correction
     377            0 :                   call oops('bad result from sizeB -- correction info either NaN or Inf')
     378            0 :                   if (s% stop_for_bad_nums) then
     379            0 :                      write(*,1) 'correction_norm', correction_norm
     380            0 :                      write(*,1) 'max_correction', max_correction
     381            0 :                      call mesa_error(__FILE__,__LINE__,'solver')
     382              :                   end if
     383              :                   exit iter_loop
     384              :                end if
     385              : 
     386           31 :                if (.not. s% ignore_too_large_correction) then
     387           31 :                   if ((correction_norm > s% corr_param_factor*s% scale_correction_norm) .and. &
     388              :                         .not. s% doing_first_model_of_run) then
     389            0 :                      call oops('avg corr too large')
     390            0 :                      exit iter_loop
     391              :                   end if
     392              :                end if
     393              : 
     394              :                ! shrink the correction if it is too large
     395           31 :                correction_factor = 1d0
     396           31 :                temp_correction_factor = 1d0
     397              : 
     398           31 :                if (correction_norm*correction_factor > s% scale_correction_norm) then
     399            0 :                   correction_factor = min(correction_factor,s% scale_correction_norm/correction_norm)
     400              :                end if
     401              : 
     402           31 :                if (max_abs_correction*correction_factor > s% scale_max_correction) then
     403            0 :                   temp_correction_factor = s% scale_max_correction/max_abs_correction
     404              :                end if
     405              : 
     406           31 :                if (iter > s% solver_itermin_until_reduce_min_corr_coeff) then
     407            0 :                   if (min_corr_coeff == 1d0 .and. &
     408              :                      s% solver_reduced_min_corr_coeff < 1d0) then
     409            0 :                         min_corr_coeff = s% solver_reduced_min_corr_coeff
     410              :                   end if
     411              :                end if
     412              : 
     413           31 :                correction_factor = max(min_corr_coeff, correction_factor)
     414           31 :                if (.not. s% ignore_min_corr_coeff_for_scale_max_correction) then
     415           31 :                   temp_correction_factor = max(min_corr_coeff, temp_correction_factor)
     416              :                end if
     417           31 :                correction_factor = min(correction_factor, temp_correction_factor)
     418              : 
     419              :                ! fix B if out of definition domain
     420           31 :                call Bdomain(s, nvar, soln, correction_factor, ierr)
     421           31 :                if (ierr /= 0) then  ! correction cannot be fixed
     422            0 :                   call oops('correction rejected by Bdomain')
     423            0 :                   exit iter_loop
     424              :                end if
     425              : 
     426           31 :                i_lnT = s% i_lnT
     427           31 :                if (s% solver_max_T_correction_factor > 1d0 .and. i_lnT > 0 .and. i_lnT <= nvar) then
     428            0 :                   k_max_dlnT = maxloc(abs(soln(i_lnT,1:nz)*s% x_scale(i_lnT,1:nz)), dim=1)
     429            0 :                   max_dlnT = abs(soln(i_lnT,k_max_dlnT)*s% x_scale(i_lnT,k_max_dlnT))
     430            0 :                   dlnT_limit = log(s% solver_max_T_correction_factor)
     431            0 :                   if (correction_factor*max_dlnT > dlnT_limit) then
     432            0 :                      correction_factor = dlnT_limit/max_dlnT
     433            0 :                      if (s% trace_solver_damping) write(*,4) 'temperature correction limit', &
     434            0 :                         s% model_number, iter, k_max_dlnT, correction_factor
     435              :                   end if
     436              :                end if
     437              : 
     438           31 :                if (min_corr_coeff < 1d0) then
     439              :                   ! compute gradient of f = equ<dot>jacobian
     440              :                   ! NOTE: NOT jacobian<dot>equ
     441            0 :                   call block_multiply_xa(nvar, nz, lblk1, dblk1, ublk1, equ1, grad_f1)
     442              : 
     443            0 :                   slope = eval_slope(nvar, nz, grad_f, soln)
     444            0 :                   if (is_bad_num(slope) .or. slope > 0d0) then  ! a very bad sign
     445            0 :                      if (is_bad_num(slope) .and. s% stop_for_bad_nums) then
     446            0 :                         write(*,1) 'slope', slope
     447            0 :                         call mesa_error(__FILE__,__LINE__,'solver')
     448              :                      end if
     449            0 :                      slope = 0d0
     450            0 :                      min_corr_coeff = 1d0
     451              :                   end if
     452              : 
     453              :                else
     454              : 
     455           31 :                   slope = 0d0
     456              : 
     457              :                end if
     458              : 
     459           31 :                f = 0d0
     460              :                call adjust_correction( &
     461           31 :                   min_corr_coeff, correction_factor, grad_f1, f, slope, coeff, err_msg, ierr)
     462           31 :                if (ierr /= 0) then
     463            0 :                   call oops(err_msg)
     464            0 :                   exit iter_loop
     465              :                end if
     466           31 :                s% solver_adjust_iter = 0
     467              : 
     468              :                ! coeff is factor by which adjust_correction rescaled the correction vector
     469           31 :                if (coeff > s% tiny_corr_factor*min_corr_coeff .or. min_corr_coeff >= 1d0) then
     470           31 :                   tiny_corr_cnt = 0
     471              :                else
     472            0 :                   tiny_corr_cnt = tiny_corr_cnt + 1
     473              :                end if
     474              : 
     475              :                ! check the residuals for the equations
     476              : 
     477           31 :                call sizequ(s, nvar, residual_norm, max_residual, max_resid_k, max_resid_j, ierr)
     478           31 :                if (ierr /= 0) then
     479            0 :                   call oops('sizequ returned ierr')
     480            0 :                   exit iter_loop
     481              :                end if
     482              : 
     483           31 :                if (is_bad_num(residual_norm)) then
     484            0 :                   call oops('residual_norm is a bad number (NaN or Infinity)')
     485            0 :                   if (s% stop_for_bad_nums) then
     486            0 :                      write(*,1) 'residual_norm', residual_norm
     487            0 :                      call mesa_error(__FILE__,__LINE__,'solver')
     488              :                   end if
     489              :                   exit iter_loop
     490              :                end if
     491              : 
     492           31 :                if (is_bad_num(max_residual)) then
     493            0 :                   call oops('max_residual is a bad number (NaN or Infinity)')
     494            0 :                   if (s% stop_for_bad_nums) then
     495            0 :                      write(*,1) 'max_residual', max_residual
     496            0 :                      call mesa_error(__FILE__,__LINE__,'solver')
     497              :                   end if
     498              :                   exit iter_loop
     499              :                end if
     500              : 
     501           31 :                residual_norm = abs(residual_norm)
     502           31 :                max_residual = abs(max_residual)
     503           31 :                s% residual_norm = residual_norm
     504           31 :                s% max_residual = max_residual
     505           31 :                resid_norm_min = min(residual_norm, resid_norm_min)
     506           31 :                max_resid_min = min(max_residual, max_resid_min)
     507              : 
     508              :                disabled_resid_tests = &
     509           31 :                   tol_max_residual > 1d2 .and. tol_residual_norm > 1d2
     510              :                pass_resid_tests = &
     511              :                   .not. disabled_resid_tests .and. &
     512              :                   max_residual <= tol_max_residual .and. &
     513           31 :                   residual_norm <= tol_residual_norm
     514              :                pass_corr_tests_without_coeff = &
     515              :                   max_abs_correction <= tol_max_correction .and. &
     516           31 :                   correction_norm <= tol_correction_norm
     517              :                pass_corr_tests_with_coeff = &
     518              :                   max_abs_correction <= tol_max_correction*coeff .and. &
     519           31 :                   correction_norm <= tol_correction_norm*coeff
     520              : 
     521              :                passed_tol_tests = &
     522              :                   (pass_resid_tests .and. pass_corr_tests_with_coeff) .or. &
     523           31 :                   (disabled_resid_tests .and. pass_corr_tests_without_coeff)
     524              : 
     525           31 :                if (.not. passed_tol_tests) then
     526              : 
     527           20 :                   if (iter >= max_tries) then
     528            0 :                      call get_message
     529            0 :                      message = trim(message) // ' -- give up'
     530            0 :                      if (len_trim(s% retry_message) == 0) &
     531            0 :                         s% retry_message = trim(message) // ' in solver'
     532            0 :                      if (dbg_msg) call write_msg(message)
     533            0 :                      if (.not. pass_resid_tests .and. .not. disabled_resid_tests) then
     534            0 :                         if (residual_norm > tol_residual_norm) &
     535            0 :                            write(*,2) 'residual_norm > tol_residual_norm', &
     536            0 :                               s% model_number, residual_norm, tol_residual_norm
     537            0 :                         if (max_residual > tol_max_residual) &
     538            0 :                            write(*,2) 'max_residual > tol_max_residual', &
     539            0 :                               s% model_number, max_residual, tol_max_residual
     540              :                      end if
     541            0 :                      if (disabled_resid_tests) then  ! no coeff for corrections
     542            0 :                         if (correction_norm > tol_correction_norm) &
     543            0 :                            write(*,2) 'correction_norm > tol_correction_norm', &
     544            0 :                               s% model_number, correction_norm, tol_correction_norm, coeff
     545            0 :                         if (max_abs_correction > tol_max_correction) &
     546            0 :                            write(*,2) 'max_abs_correction > tol_max_correction', &
     547            0 :                               s% model_number, max_abs_correction, tol_max_correction, coeff
     548              :                      else  ! include coeff for corrections
     549            0 :                         if (correction_norm > tol_correction_norm*coeff) &
     550            0 :                            write(*,2) 'correction_norm > tol_correction_norm*coeff', &
     551            0 :                               s% model_number, correction_norm, tol_correction_norm*coeff, coeff
     552            0 :                         if (max_abs_correction > tol_max_correction*coeff) &
     553            0 :                            write(*,2) 'max_abs_correction > tol_max_correction*coeff', &
     554            0 :                               s% model_number, max_abs_correction, tol_max_correction*coeff, coeff
     555              :                      end if
     556            0 :                      convergence_failure = .true.
     557            0 :                      exit iter_loop
     558           20 :                   else if (.not. first_try .and. .not. s% doing_first_model_of_run) then
     559            8 :                      if (correction_norm > s% corr_norm_jump_limit*corr_norm_min) then
     560            0 :                         call oops('avg correction jumped')
     561            0 :                         exit iter_loop
     562            8 :                      else if (residual_norm > s% resid_norm_jump_limit*resid_norm_min) then
     563            0 :                         call oops('avg residual jumped')
     564            0 :                         exit iter_loop
     565            8 :                      else if (max_abs_correction > s% max_corr_jump_limit*max_corr_min) then
     566            0 :                         call oops('max correction jumped')
     567            0 :                         exit iter_loop
     568            8 :                      else if (max_residual > s% max_resid_jump_limit*max_resid_min) then
     569            0 :                         call oops('max residual jumped')
     570            0 :                         exit iter_loop
     571              :                      else if (tiny_corr_cnt >= s% tiny_corr_coeff_limit &
     572            8 :                            .and. min_corr_coeff < 1) then
     573            0 :                         call oops('tiny corrections')
     574            0 :                         exit iter_loop
     575              :                      end if
     576           12 :                   else if (.not. s% doing_first_model_of_run) then
     577           10 :                      if (coeff < min(min_corr_coeff,correction_factor)) then
     578            0 :                         call oops('coeff too small')
     579            0 :                         exit iter_loop
     580              :                      end if
     581              :                   end if
     582              :                end if
     583              : 
     584           31 :                if (dbg_msg) then
     585            0 :                   if (.not. passed_tol_tests) then
     586            0 :                      call get_message
     587              :                   end if
     588            0 :                   if (.not. passed_tol_tests) then
     589            0 :                      call write_msg(message)
     590            0 :                   else if (iter < s% solver_itermin) then
     591            0 :                      call write_msg('iter < itermin')
     592              :                   else
     593            0 :                      call write_msg('okay!')
     594              :                   end if
     595              :                end if
     596              : 
     597           31 :                if (passed_tol_tests .and. (iter+1 < max_tries)) then
     598              :                   ! about to declare victory... but may want to do another iteration
     599           11 :                   force_iter_value = force_another_iteration(s, iter, s% solver_itermin)
     600           11 :                   if (force_iter_value > 0) then
     601            0 :                      passed_tol_tests = .false.  ! force another
     602            0 :                      tiny_corr_cnt = 0  ! reset the counter
     603            0 :                      corr_norm_min = 1d99
     604            0 :                      resid_norm_min = 1d99
     605            0 :                      max_corr_min = 1d99
     606            0 :                      max_resid_min = 1d99
     607           11 :                   else if (force_iter_value < 0) then  ! failure
     608            0 :                      call oops('force iter')
     609            0 :                      exit iter_loop
     610              :                   end if
     611              :                end if
     612              : 
     613           31 :                if (s% use_other_solver_monitor .and. &
     614              :                      associated(s% other_solver_monitor)) then
     615              :                   call s% other_solver_monitor( &
     616              :                      s% id, iter, passed_tol_tests, &
     617              :                      correction_norm, max_correction, &
     618            0 :                      residual_norm, max_residual, ierr)
     619            0 :                   if (ierr /= 0) then
     620            0 :                      call oops('other_solver_monitor')
     621            0 :                      exit iter_loop
     622              :                   end if
     623              :                end if
     624              : 
     625           31 :                iter=iter+1
     626           31 :                s% solver_iter = iter
     627           31 :                first_try = .false.
     628              : 
     629              :             end do iter_loop
     630              : 
     631           11 :             if (max_residual > s% warning_limit_for_max_residual .and. .not. convergence_failure) &
     632            0 :                write(*,2) 'WARNING: max_residual > warning_limit_for_max_residual', &
     633            0 :                   s% model_number, max_residual, s% warning_limit_for_max_residual
     634              : 
     635              :          end subroutine do_solver_work
     636              : 
     637              : 
     638           31 :          subroutine solver_test_partials(nvar, xder, ierr)
     639              :             ! create jacobian by using numerical differences for partial derivatives
     640              :             integer, intent(in) :: nvar
     641              :             real(dp), pointer, dimension(:,:) :: xder  ! (nvar, nz)
     642              :             integer, intent(out) :: ierr
     643              : 
     644              :             integer :: j, k, k_lo, k_hi
     645           31 :             real(dp), dimension(:,:), pointer :: save_equ, save_dx
     646              :             logical :: testing_partial
     647              : 
     648              :             include 'formats'
     649              : 
     650           31 :             ierr = 0
     651              :             testing_partial = &  ! check inlist parameters
     652              :                s% solver_test_partials_dx_0 > 0d0 .and. &
     653              :                s% solver_test_partials_k > 0 .and. &
     654              :                s% solver_call_number == s% solver_test_partials_call_number .and. &
     655           31 :                s% solver_test_partials_iter_number == iter
     656              :             if (.not. testing_partial) return
     657              : 
     658            0 :             call do_equations(ierr)
     659            0 :             if (ierr /= 0) return
     660              : 
     661            0 :             allocate(save_dx(nvar,nz), save_equ(nvar,nz))
     662              : 
     663            0 :             do k=1,nz
     664            0 :                do j=1,nvar
     665            0 :                   save_dx(j,k) = s% solver_dx(j,k)
     666            0 :                   save_equ(j,k) = equ(j,k)
     667              :                end do
     668              :             end do
     669              : 
     670            0 :             s% doing_check_partials = .true.  ! let set_vars_for_solver know
     671            0 :             k_lo = s% solver_test_partials_k_low
     672            0 :             if (k_lo > 0 .and. k_lo <= s% nz) then
     673            0 :                k_hi = s% solver_test_partials_k_high
     674            0 :                if (k_hi <= 0) then
     675              :                   k_hi = s% nz
     676              :                else
     677            0 :                   k_hi = min(k_hi,s% nz)
     678              :                end if
     679            0 :                do k = k_lo, k_hi
     680            0 :                   call test_cell_partials(s, k, save_dx, save_equ, ierr)
     681            0 :                   if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
     682              :                end do
     683              :             else
     684            0 :                k = s% solver_test_partials_k
     685            0 :                call test_cell_partials(s, k, save_dx, save_equ, ierr)
     686            0 :                if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
     687              :             end if
     688            0 :             deallocate(save_dx, save_equ)
     689            0 :             call mesa_error(__FILE__,__LINE__,'done solver_test_partials')
     690              : 
     691            0 :          end subroutine solver_test_partials
     692              : 
     693              : 
     694            0 :          subroutine get_message
     695              :             include 'formats'
     696            0 :             i = 0
     697            0 :             if (correction_norm > tol_correction_norm*coeff) i = i+1
     698            0 :             if (max_abs_correction > tol_max_correction*coeff) i = i+2
     699            0 :             if (residual_norm > tol_residual_norm*coeff) i = i+4
     700            0 :             if (max_residual > tol_max_residual*coeff) i = i+8
     701            0 :             if (i == 0) then
     702            0 :                message = 'out of tries'
     703              :             else
     704            0 :                message = tol_msg(i)
     705              :             end if
     706            0 :          end subroutine get_message
     707              : 
     708              : 
     709           11 :          subroutine set_param_defaults
     710           11 :             if (s% corr_param_factor == 0) s% corr_param_factor = 10d0
     711           11 :             if (s% scale_max_correction == 0) s% scale_max_correction = 1d99
     712           11 :             if (s% corr_norm_jump_limit == 0) s% corr_norm_jump_limit = 1d99
     713           11 :             if (s% max_corr_jump_limit == 0) s% max_corr_jump_limit = 1d99
     714           11 :          end subroutine set_param_defaults
     715              : 
     716              : 
     717            0 :          subroutine oops(msg)
     718              :             character (len=*), intent(in) :: msg
     719              :             character (len=strlen) :: full_msg
     720              :             include 'formats'
     721            0 :             full_msg = trim(msg) // ' -- give up'
     722            0 :             if (len_trim(s% retry_message) == 0) s% retry_message = trim(full_msg) // ' in solver'
     723            0 :             call write_msg(full_msg)
     724            0 :             convergence_failure = .true.
     725            0 :          end subroutine oops
     726              : 
     727              : 
     728          126 :          subroutine do_equations(ierr)
     729              :             integer, intent(out) :: ierr
     730           42 :             call prepare_solver_matrix(nvar, xder, ierr)
     731           42 :             if (ierr /= 0) return
     732           42 :             call eval_equations(s, nvar, ierr)
     733           42 :             if (ierr /= 0) return
     734           42 :             call s% other_after_solver_setmatrix(s% id, ierr)
     735              :          end subroutine do_equations
     736              : 
     737              : 
     738           42 :          subroutine prepare_solver_matrix(nvar, xder, ierr)
     739              :             integer, intent(in) :: nvar
     740              :             real(dp), pointer, dimension(:,:) :: xder  ! (nvar, nz)
     741              :             integer, intent(out) :: ierr
     742              :             integer :: nz, neqns
     743              :             include 'formats'
     744           42 :             ierr = 0
     745           42 :             nz = s% nz
     746           42 :             neqns = nvar*nz
     747           42 :             s% ublk(1:nvar,1:nvar,1:nz) => ublk1
     748           42 :             s% dblk(1:nvar,1:nvar,1:nz) => dblk1
     749           42 :             s% lblk(1:nvar,1:nvar,1:nz) => lblk1
     750           42 :          end subroutine prepare_solver_matrix
     751              : 
     752              : 
     753           31 :          subroutine adjust_correction( &
     754              :                min_corr_coeff_in, max_corr_coeff, grad_f, f, slope, coeff,  &
     755              :                err_msg, ierr)
     756              :             real(dp), intent(in) :: min_corr_coeff_in
     757              :             real(dp), intent(in) :: max_corr_coeff
     758              :             real(dp), intent(in) :: grad_f(:)  ! (neq) ! gradient df/ddx at xold
     759              :             real(dp), intent(out) :: f  ! 1/2 fvec^2. minimize this.
     760              :             real(dp), intent(in) :: slope
     761              :             real(dp), intent(out) :: coeff
     762              : 
     763              :             ! the new correction is coeff*xscale*soln
     764              :             ! with min_corr_coeff <= coeff <= max_corr_coeff
     765              :             ! if all goes well, the new x will give an improvement in f
     766              : 
     767              :             character (len=*), intent(out) :: err_msg
     768              :             integer, intent(out) :: ierr
     769              : 
     770              :             integer :: i, k, iter
     771              :             logical :: first_time
     772              :             real(dp) :: a1, alam, alam2, a2, disc, f2, &
     773              :                rhs1, rhs2, tmplam, fold, min_corr_coeff
     774              :             real(dp) :: f_target
     775              :             logical :: skip_eval_f, dbg_adjust
     776              : 
     777              :             real(dp), parameter :: alf = 1d-2  ! ensures sufficient decrease in f
     778              : 
     779              :             real(dp), parameter :: alam_factor = 0.2d0
     780              : 
     781              :             include 'formats'
     782              : 
     783           31 :             ierr = 0
     784           31 :             coeff = 0
     785           31 :             dbg_adjust = .false.
     786           31 :             err_msg = ''
     787              : 
     788           31 :             skip_eval_f = (min_corr_coeff_in == 1)
     789           31 :             if (skip_eval_f) then
     790           31 :                f = 0
     791              :             else
     792            0 :                do k=1,nz
     793            0 :                   do i=1,nvar
     794            0 :                      dxsave(i,k) = s% solver_dx(i,k)
     795            0 :                      ddxsave(i,k) = ddx(i,k)
     796              :                   end do
     797              :                end do
     798            0 :                f = eval_f(nvar,nz,equ)
     799            0 :                if (is_bad_num(f)) then
     800            0 :                   ierr = -1
     801            0 :                   write(err_msg,*) 'adjust_correction failed in eval_f'
     802            0 :                   if (dbg_msg) write(*,*) &
     803            0 :                      'adjust_correction: eval_f(nvar,nz,equ)', eval_f(nvar,nz,equ)
     804            0 :                   if (s% stop_for_bad_nums) then
     805            0 :                      write(*,1) 'f', f
     806            0 :                      call mesa_error(__FILE__,__LINE__,'solver adjust_correction')
     807              :                   end if
     808           31 :                   return
     809              :                end if
     810              :             end if
     811           31 :             fold = f
     812           31 :             min_corr_coeff = min(min_corr_coeff_in, max_corr_coeff)  ! make sure min <= max
     813           31 :             alam = max_corr_coeff
     814           31 :             first_time = .true.
     815           31 :             f2 = 0
     816           31 :             alam2 = 0
     817              :             if (dbg_adjust) then
     818              :                write(*,4) 'max_corr_coeff', k, s% solver_iter, &
     819              :                   s% model_number, max_corr_coeff
     820              :                write(*,4) 'slope', k, s% solver_iter, &
     821              :                   s% model_number, slope
     822              :                write(*,4) 'f', k, s% solver_iter, &
     823              :                   s% model_number, f
     824              :             end if
     825              : 
     826           31 :          search_loop: do iter = 1, 1000
     827              : 
     828           31 :                coeff = max(min_corr_coeff, alam)
     829           31 :                s% solver_adjust_iter = iter
     830              : 
     831           31 :                call apply_coeff(nvar, nz, dxsave, soln, coeff, skip_eval_f)
     832              : 
     833           31 :                call do_equations(ierr)
     834           31 :                if (ierr /= 0) then
     835            0 :                   if (alam > min_corr_coeff .and. s% model_number == 1) then
     836              :                      ! try again with smaller correction vector.
     837              :                      ! need this to rescue create pre-main-sequence model in some nasty cases.
     838            0 :                      alam = max(alam/10, min_corr_coeff)
     839            0 :                      ierr = 0
     840            0 :                      cycle search_loop
     841              :                   end if
     842            0 :                   write(err_msg,*) 'adjust_correction failed in eval_equations'
     843            0 :                   if (dbg_msg .or. dbg_adjust) &
     844            0 :                      write(*,2) 'adjust_correction: eval_equations returned ierr', &
     845            0 :                         ierr, min_corr_coeff, max_corr_coeff
     846              :                   exit search_loop
     847              :                end if
     848              : 
     849           31 :                if (min_corr_coeff == 1) return
     850              : 
     851              :                if (dbg_adjust) then
     852              :                   do k=1,nz
     853              :                      do i=1,nvar
     854              :                         write(*,5) trim(s% nameofequ(i)), k, iter, s% solver_iter, &
     855              :                            s% model_number, equ(i,k)
     856              :                      end do
     857              :                   end do
     858              :                end if
     859              : 
     860            0 :                f = eval_f(nvar,nz,equ)
     861            0 :                if (is_bad_num(f)) then
     862            0 :                   if (s% stop_for_bad_nums) then
     863            0 :                      write(*,1) 'f', f
     864            0 :                      call mesa_error(__FILE__,__LINE__,'solver adjust_correction eval_f')
     865              :                   end if
     866            0 :                   if (alam > min_corr_coeff) then
     867            0 :                      alam = max(alam/10, min_corr_coeff)
     868            0 :                      ierr = 0
     869            0 :                      cycle search_loop
     870              :                   end if
     871            0 :                   err_msg = 'equ norm is NaN or other bad num'
     872            0 :                   ierr = -1
     873            0 :                   exit search_loop
     874              :                end if
     875              : 
     876            0 :                f_target = max(fold/2, fold + alf*coeff*slope)
     877            0 :                if (f <= f_target) then
     878              :                   return  ! sufficient decrease in f
     879              :                end if
     880              : 
     881            0 :                if (alam <= min_corr_coeff) then
     882              :                   return  ! time to give up
     883              :                end if
     884              : 
     885              :                ! reduce alam and try again
     886            0 :                if (first_time) then
     887            0 :                   tmplam = -slope*alam*alam/(2*(f-fold-alam*slope))
     888            0 :                   first_time = .false.
     889              :                   if (dbg_adjust) then
     890              :                      write(*,5) 'slope', k, iter, s% solver_iter, &
     891              :                         s% model_number, slope
     892              :                      write(*,5) 'f', k, iter, s% solver_iter, &
     893              :                         s% model_number, f
     894              :                      write(*,5) 'fold', k, iter, s% solver_iter, &
     895              :                         s% model_number, fold
     896              :                      write(*,5) '2*(f-fold-alam*slope)', k, iter, s% solver_iter, &
     897              :                         s% model_number, 2*(f-fold-alam*slope)
     898              :                   end if
     899              :                else  ! have two prior f values to work with
     900            0 :                   rhs1 = f - fold - alam*slope
     901            0 :                   rhs2 = f2 - fold - alam2*slope
     902            0 :                   a1 = (rhs1/(alam*alam) - rhs2/(alam2*alam2))/(alam - alam2)
     903            0 :                   a2 = (-alam2*rhs1/(alam*alam) + alam*rhs2/(alam2*alam2))/(alam - alam2)
     904              :                   if (dbg_adjust) then
     905              :                      write(*,5) 'slope', k, iter, s% solver_iter, &
     906              :                         s% model_number, slope
     907              :                      write(*,5) 'f', k, iter, s% solver_iter, &
     908              :                         s% model_number, f
     909              :                      write(*,5) 'f2', k, iter, s% solver_iter, &
     910              :                         s% model_number, f2
     911              :                      write(*,5) 'fold', k, iter, s% solver_iter, &
     912              :                         s% model_number, fold
     913              :                      write(*,5) 'alam', k, iter, s% solver_iter, &
     914              :                         s% model_number, alam
     915              :                      write(*,5) 'alam2', k, iter, s% solver_iter, &
     916              :                         s% model_number, alam2
     917              :                      write(*,5) 'rhs1', k, iter, s% solver_iter, &
     918              :                         s% model_number, rhs1
     919              :                      write(*,5) 'rhs2', k, iter, s% solver_iter, &
     920              :                         s% model_number, rhs2
     921              :                      write(*,5) 'a1', k, iter, s% solver_iter, &
     922              :                         s% model_number, a1
     923              :                      write(*,5) 'a2', k, iter, s% solver_iter, &
     924              :                         s% model_number, a2
     925              :                   end if
     926            0 :                   if (a1 == 0) then
     927            0 :                      tmplam = -slope/(2*a2)
     928              :                   else
     929            0 :                      disc = a2*a2-3*a1*slope
     930            0 :                      if (disc < 0) then
     931            0 :                         tmplam = alam*alam_factor
     932            0 :                      else if (a2 <= 0) then
     933            0 :                         tmplam = (-a2+sqrt(disc))/(3*a1)
     934              :                      else
     935            0 :                         tmplam = -slope/(a2+sqrt(disc))
     936              :                      end if
     937              :                      if (dbg_adjust) then
     938              :                         write(*,5) 'disc', k, iter, s% solver_iter, &
     939              :                            s% model_number, disc
     940              :                      end if
     941              :                   end if
     942            0 :                   if (tmplam > alam*alam_factor) tmplam = alam*alam_factor
     943              :                end if
     944              : 
     945            0 :                alam2 = alam
     946            0 :                f2 = f
     947            0 :                alam = max(tmplam, alam*alam_factor, min_corr_coeff)
     948              : 
     949           31 :                if (dbg_adjust) then
     950              :                   write(*,5) 'tmplam', k, iter, s% solver_iter, &
     951              :                      s% model_number, tmplam
     952              :                   write(*,5) 'min_corr_coeff', k, iter, s% solver_iter, &
     953              :                      s% model_number, min_corr_coeff
     954              :                   write(*,5) 'alam_factor', k, iter, s% solver_iter, &
     955              :                      s% model_number, alam_factor
     956              :                end if
     957              : 
     958              :             end do search_loop
     959              : 
     960              :             ! Restore corrections only if they were saved for the line search.
     961            0 :             if (skip_eval_f) return
     962            0 :             do k=1,nz
     963            0 :                do i=1,nvar
     964            0 :                   s% solver_dx(i,k) = dxsave(i,k)
     965            0 :                   ddx(i,k) = ddxsave(i,k)
     966              :                end do
     967              :             end do
     968              : 
     969           31 :          end subroutine adjust_correction
     970              : 
     971              : 
     972           31 :          subroutine apply_coeff(nvar, nz, dxsave, soln, coeff, just_use_dx)
     973              :             integer, intent(in) :: nvar, nz
     974              :             real(dp), intent(in), dimension(:,:) :: dxsave, soln
     975              :             real(dp), intent(in) :: coeff
     976              :             logical, intent(in) :: just_use_dx
     977              :             integer :: i, k
     978              :             include 'formats'
     979              : 
     980           31 :             if (just_use_dx) then
     981           31 :                if (coeff == 1d0) then
     982        36612 :                   do k=1,nz
     983       475584 :                      do i=1,nvar
     984       475553 :                         s% solver_dx(i,k) = s% solver_dx(i,k) + s% x_scale(i,k)*soln(i,k)
     985              :                      end do
     986              :                   end do
     987              :                else
     988            0 :                   do k=1,nz
     989            0 :                      do i=1,nvar
     990            0 :                         s% solver_dx(i,k) = s% solver_dx(i,k) + coeff*s% x_scale(i,k)*soln(i,k)
     991              :                      end do
     992              :                   end do
     993              :                end if
     994           31 :                return
     995              :             end if
     996              :             ! else use dxsave instead of dx
     997            0 :             if (coeff == 1d0) then
     998            0 :                do k=1,nz
     999            0 :                   do i=1,nvar
    1000            0 :                      s% solver_dx(i,k) = dxsave(i,k) + s% x_scale(i,k)*soln(i,k)
    1001              :                   end do
    1002              :                end do
    1003              :                return
    1004              :             end if
    1005            0 :             do k=1,nz
    1006            0 :                do i=1,nvar
    1007            0 :                   s% solver_dx(i,k) = dxsave(i,k) + coeff*s% x_scale(i,k)*soln(i,k)
    1008              :                end do
    1009              :             end do
    1010              :          end subroutine apply_coeff
    1011              : 
    1012              : 
    1013           31 :          logical function solve_equ()
    1014              :             use star_utils, only: start_time, update_time
    1015              :             use rsp_def, only: NV, MAX_NZN
    1016              :             integer ::  i, ierr
    1017              :             real(dp) :: total_time
    1018              : 
    1019              :             include 'formats'
    1020              :             ierr = 0
    1021           31 :             solve_equ = .true.
    1022              : 
    1023           31 :             if (s% doing_timing) then
    1024            0 :                call start_time(s, time0, total_time)
    1025              :             end if
    1026              : 
    1027           31 :             !$OMP PARALLEL DO SIMD
    1028              :             do i=1,neq
    1029              :                b1(i) = -equ1(i)
    1030              :             end do
    1031              :             !$OMP END PARALLEL DO SIMD
    1032              : 
    1033           31 :             if (s% use_DGESVX_in_bcyclic) then
    1034            0 :                !$OMP PARALLEL DO SIMD
    1035              :                do i = 1, nvar*nvar*nz
    1036              :                   save_ublk1(i) = ublk1(i)
    1037              :                   save_dblk1(i) = dblk1(i)
    1038              :                   save_lblk1(i) = lblk1(i)
    1039              :                end do
    1040              :                !$OMP END PARALLEL DO SIMD
    1041              :             end if
    1042              : 
    1043           62 :             select case (trim(s% hydro_matrix_solver))
    1044              :             case ('bcyclic')
    1045           31 :                call factor_bcyclic_mtx(ierr)
    1046           31 :                if (ierr == 0) call solve_bcyclic_mtx(ierr)
    1047              :             case ('banded')
    1048            0 :                call solve_banded_mtx(ierr)
    1049              :             case default
    1050              :                write(*,*) 'bad value for hydro_matrix_solver: ' // &
    1051            0 :                   trim(s% hydro_matrix_solver)
    1052           31 :                ierr = -1
    1053              :             end select
    1054              : 
    1055           31 :             if (s% use_DGESVX_in_bcyclic) then
    1056            0 :                !$OMP PARALLEL DO SIMD
    1057              :                do i = 1, nvar*nvar*nz
    1058              :                   ublk1(i) = save_ublk1(i)
    1059              :                   dblk1(i) = save_dblk1(i)
    1060              :                   lblk1(i) = save_lblk1(i)
    1061              :                end do
    1062              :                !$OMP END PARALLEL DO SIMD
    1063              :             end if
    1064              : 
    1065           31 :             if (s% doing_timing) then
    1066            0 :                call update_time(s, time0, total_time, s% time_solver_matrix)
    1067              :             end if
    1068           31 :             if (ierr /= 0) then
    1069            0 :                solve_equ = .false.
    1070            0 :                b(1:nvar,1:nz) = 0d0
    1071              :             end if
    1072              : 
    1073           31 :          end function solve_equ
    1074              : 
    1075              : 
    1076           31 :          subroutine factor_bcyclic_mtx(ierr)
    1077              :             use star_bcyclic, only: bcyclic_factor
    1078              :             integer, intent(out) :: ierr
    1079              :             call bcyclic_factor( &
    1080              :                s, nvar, nz, lblk1, dblk1, ublk1, lblkF1, dblkF1, ublkF1, ipiv_blk1, &
    1081              :                B1, row_scale_factors1, col_scale_factors1, &
    1082           31 :                equed1, iter, ierr)
    1083           31 :          end subroutine factor_bcyclic_mtx
    1084              : 
    1085              : 
    1086           31 :          subroutine solve_bcyclic_mtx(ierr)
    1087              :             use star_bcyclic, only: bcyclic_solve
    1088              :             integer, intent(out) :: ierr
    1089              :             call bcyclic_solve( &
    1090              :                s, nvar, nz, lblk1, dblk1, ublk1, lblkF1, dblkF1, ublkF1, ipiv_blk1, &
    1091              :                B1, soln1, row_scale_factors1, col_scale_factors1, equed1, &
    1092           31 :                iter, ierr)
    1093           31 :          end subroutine solve_bcyclic_mtx
    1094              : 
    1095              : 
    1096            0 :          subroutine solve_banded_mtx(ierr)
    1097              :             integer, intent(out) :: ierr
    1098              :             integer :: info, k, i_equ, i_var, irow, jcol, row0, col0, iband
    1099              : 
    1100            0 :             ierr = 0
    1101            0 :             if (.not. associated(band)) then
    1102            0 :                write(*,*) 'band storage is not allocated for hydro_matrix_solver=banded'
    1103            0 :                ierr = -1
    1104            0 :                return
    1105              :             end if
    1106              : 
    1107            0 :             band(1:band_ldab,1:neq) = 0d0
    1108            0 :             soln1(1:neq) = B1(1:neq)
    1109              : 
    1110            0 :             do k = 1, nz
    1111            0 :                row0 = nvar*(k-1)
    1112            0 :                do i_equ = 1, nvar
    1113            0 :                   irow = row0 + i_equ
    1114              : 
    1115            0 :                   do i_var = 1, nvar
    1116            0 :                      jcol = row0 + i_var
    1117            0 :                      iband = band_diag + irow - jcol
    1118            0 :                      if (iband < 1 .or. iband > band_ldab) then
    1119            0 :                         write(*,*) 'hydro banded matrix entry outside band', &
    1120            0 :                            irow, jcol, iband, band_ldab
    1121            0 :                         ierr = -1
    1122            0 :                         return
    1123              :                      end if
    1124            0 :                      band(iband,jcol) = dblk(i_equ,i_var,k)
    1125              :                   end do
    1126              : 
    1127            0 :                   if (k > 1) then
    1128            0 :                      col0 = nvar*(k-2)
    1129            0 :                      do i_var = 1, nvar
    1130            0 :                         jcol = col0 + i_var
    1131            0 :                         iband = band_diag + irow - jcol
    1132            0 :                         if (iband < 1 .or. iband > band_ldab) then
    1133            0 :                            write(*,*) 'hydro banded matrix entry outside band', &
    1134            0 :                               irow, jcol, iband, band_ldab
    1135            0 :                            ierr = -1
    1136            0 :                            return
    1137              :                         end if
    1138            0 :                         band(iband,jcol) = lblk(i_equ,i_var,k)
    1139              :                      end do
    1140              :                   end if
    1141              : 
    1142            0 :                   if (k < nz) then
    1143            0 :                      col0 = nvar*k
    1144            0 :                      do i_var = 1, nvar
    1145            0 :                         jcol = col0 + i_var
    1146            0 :                         iband = band_diag + irow - jcol
    1147            0 :                         if (iband < 1 .or. iband > band_ldab) then
    1148            0 :                            write(*,*) 'hydro banded matrix entry outside band', &
    1149            0 :                               irow, jcol, iband, band_ldab
    1150            0 :                            ierr = -1
    1151            0 :                            return
    1152              :                         end if
    1153            0 :                         band(iband,jcol) = ublk(i_equ,i_var,k)
    1154              :                      end do
    1155              :                   end if
    1156              :                end do
    1157              :             end do
    1158              : 
    1159            0 :             call DGBTRF(neq, neq, band_kl, band_ku, band, band_ldab, ipiv1, info)
    1160            0 :             if (info /= 0) then
    1161            0 :                write(*,*) 'hydro banded DGBTRF failed', info
    1162            0 :                ierr = info
    1163            0 :                return
    1164              :             end if
    1165              : 
    1166              :             call DGBTRS('N', neq, band_kl, band_ku, 1, band, band_ldab, &
    1167            0 :                ipiv1, soln1, neq, info)
    1168            0 :             if (info /= 0) then
    1169            0 :                write(*,*) 'hydro banded DGBTRS failed', info
    1170            0 :                ierr = info
    1171            0 :                return
    1172              :             end if
    1173              : 
    1174              :          end subroutine solve_banded_mtx
    1175              : 
    1176              : 
    1177            0 :          subroutine test_cell_partials(s, k, save_dx, save_equ, ierr)
    1178              :             use star_utils, only: lookup_nameofvar, lookup_nameofequ
    1179              :             type (star_info), pointer :: s
    1180              :             integer, intent(in) :: k
    1181              :             real(dp), pointer, dimension(:,:) :: save_dx, save_equ
    1182              :             integer, intent(out) :: ierr
    1183              :             integer :: i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1184              :             include 'formats'
    1185            0 :             ierr = 0
    1186            0 :             write(*,'(A)')
    1187            0 :             i_equ = lookup_nameofequ(s, s% solver_test_partials_equ_name)
    1188            0 :             if (i_equ == 0 .and. len_trim(s% solver_test_partials_equ_name) > 0) then
    1189            0 :                if (s% solver_test_partials_equ_name == 'lnE') then  ! testing eos
    1190            0 :                   i_equ = -1
    1191            0 :                else if (s% solver_test_partials_equ_name == 'eps_nuc') then
    1192            0 :                   i_equ = -2
    1193            0 :                else if (s% solver_test_partials_equ_name == 'opacity') then
    1194            0 :                   i_equ = -3
    1195            0 :                else if (s% solver_test_partials_equ_name == 'lnP') then
    1196            0 :                   i_equ = -4
    1197            0 :                else if (s% solver_test_partials_equ_name == 'non_nuc_neu') then
    1198            0 :                   i_equ = -5
    1199            0 :                else if (s% solver_test_partials_equ_name == 'gradT') then
    1200            0 :                   i_equ = -6
    1201            0 :                else if (s% solver_test_partials_equ_name == 'mlt_vc') then
    1202            0 :                   i_equ = -7
    1203            0 :                else if (s% solver_test_partials_equ_name == 'grad_ad') then
    1204            0 :                   i_equ = -8
    1205              :                end if
    1206            0 :             else if (i_equ /= 0) then
    1207            0 :                write(*,1) 'equ name ' // trim(s% solver_test_partials_equ_name)
    1208              :             end if
    1209            0 :             i_var = lookup_nameofvar(s, s% solver_test_partials_var_name)
    1210            0 :             if (i_var /= 0) write(*,1) 'var name ' // trim(s% solver_test_partials_var_name)
    1211            0 :             if (i_var > s% nvar_hydro) then  ! get index in xa
    1212            0 :                i_var_xa_index = i_var - s% nvar_hydro
    1213              :             else
    1214            0 :                i_var_xa_index = 0
    1215              :             end if
    1216            0 :             i_var_sink = lookup_nameofvar(s, s% solver_test_partials_sink_name)
    1217            0 :             i_var_sink_xa_index  = 0
    1218            0 :             if (i_var_sink > 0 .and. i_var > s% nvar_hydro) then
    1219            0 :                write(*,1) 'sink name ' // trim(s% solver_test_partials_sink_name)
    1220            0 :                if (i_var_sink > s% nvar_hydro) then  ! get index in xa
    1221            0 :                   i_var_sink_xa_index = i_var_sink - s% nvar_hydro
    1222              :                else
    1223            0 :                   write(*,*) 'ERROR: sink name must be a chem name for the current net'
    1224            0 :                   ierr = -1
    1225            0 :                   return
    1226              :                end if
    1227              :             end if
    1228            0 :             if (s% solver_test_partials_equ_name == 'all') then
    1229            0 :                do i_equ = 1, s% nvar_hydro
    1230              :                   call test_equ_partials(s, &
    1231              :                      i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1232            0 :                      k, save_dx, save_equ, ierr)
    1233            0 :                   if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
    1234              :                end do
    1235              :             else
    1236              :                call test_equ_partials(s, &
    1237              :                   i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1238            0 :                   k, save_dx, save_equ, ierr)
    1239            0 :                if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
    1240              :             end if
    1241              :          end subroutine test_cell_partials
    1242              : 
    1243              : 
    1244            0 :          subroutine test_equ_partials(s, &
    1245              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1246              :                k, save_dx, save_equ, ierr)
    1247              :             type (star_info), pointer :: s
    1248              :             integer, intent(in) :: &
    1249              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, k
    1250              :             real(dp), pointer, dimension(:,:) :: save_dx, save_equ
    1251              :             integer, intent(out) :: ierr
    1252              :             integer :: i, j_var_xa_index, j_var_sink_xa_index
    1253              :             include 'formats'
    1254            0 :             if (i_equ /= 0) then
    1255            0 :                if (s% solver_test_partials_var_name == 'all') then
    1256            0 :                   do i = 1, s% nvar_hydro
    1257              :                      call test3_partials(s, &
    1258              :                         i_equ, i, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1259            0 :                         k, save_dx, save_equ, ierr)
    1260            0 :                      if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
    1261            0 :                      write(*,'(A)')
    1262              :                   end do
    1263            0 :                else if (i_var == 0) then
    1264            0 :                   write(*,*) 'failed to recognize variable name'
    1265              :                else
    1266              :                   call test3_partials(s, &
    1267              :                      i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1268            0 :                      k, save_dx, save_equ, ierr)
    1269            0 :                   if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
    1270              :                end if
    1271              :             else  ! i_equ == 0
    1272            0 :                if (i_var /= 0) then
    1273              :                   call test1_partial(s, &
    1274              :                      i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1275            0 :                      k, 0, s% solver_test_partials_dval_dx, save_dx, save_equ, ierr)
    1276              :                else  ! i_var == 0
    1277            0 :                   if (s% solver_test_partials_var <= 0) then
    1278            0 :                      write(*,2) 'need to set solver_test_partials_var', s% solver_test_partials_var
    1279            0 :                      write(*,2) 'for solver_test_partials_k', s% solver_test_partials_k
    1280            0 :                      call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
    1281              :                   end if
    1282            0 :                   if (s% solver_test_partials_var > s% nvar_hydro) then
    1283            0 :                      j_var_xa_index = s% solver_test_partials_var - s% nvar_hydro
    1284            0 :                      if (s% solver_test_partials_dx_sink > s% nvar_hydro) then
    1285            0 :                         j_var_sink_xa_index = s% solver_test_partials_dx_sink - s% nvar_hydro
    1286              :                      else
    1287            0 :                         write(*,*) 'set solver_test_partials_dx_sink to variable index, not to xa index', &
    1288            0 :                            s% solver_test_partials_dx_sink
    1289            0 :                         call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
    1290              :                      end if
    1291              :                   else
    1292            0 :                      j_var_xa_index = 0
    1293            0 :                      j_var_sink_xa_index = 0
    1294              :                   end if
    1295              :                   call test1_partial(s, &
    1296              :                      i_equ, s% solver_test_partials_var, s% solver_test_partials_dx_sink, &
    1297              :                      j_var_xa_index, j_var_sink_xa_index, &
    1298            0 :                      k, 0, s% solver_test_partials_dval_dx, save_dx, save_equ, ierr)
    1299              :                end if
    1300            0 :                if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'failed solver_test_partials')
    1301              :             end if
    1302            0 :             write(*,'(A)')
    1303            0 :          end subroutine test_equ_partials
    1304              : 
    1305              : 
    1306            0 :          subroutine get_lnE_partials(s, &
    1307              :                k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1308              :                dvardx0_m1, dvardx0_00, dvardx0_p1)
    1309              :             use eos_def, only: i_lnE
    1310              :             type (star_info), pointer :: s
    1311              :             integer, intent(in) :: k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1312              :             real(dp), intent(out) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1313            0 :             dvardx0_m1 = 0d0; dvardx0_00 = 0d0; dvardx0_p1 = 0d0
    1314            0 :             if (i_var_xa_index > 0) then
    1315              :                dvardx0_00 = s% d_eos_dxa(i_lnE,i_var_xa_index,k) - &
    1316            0 :                         s% d_eos_dxa(i_lnE,i_var_sink_xa_index,k)
    1317            0 :             else if (i_var == s% i_lnd) then
    1318            0 :                dvardx0_00 = s% dE_dRho_for_partials(k)*s% rho(k)/s% energy(k)
    1319            0 :             else if (i_var == s% i_lnT) then
    1320            0 :                dvardx0_00 = s% Cv_for_partials(k)*s% T(k)/s% energy(k)
    1321              :             end if
    1322            0 :          end subroutine get_lnE_partials
    1323              : 
    1324              : 
    1325            0 :          subroutine get_lnP_partials(s, &
    1326              :                k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1327              :                dvardx0_m1, dvardx0_00, dvardx0_p1)
    1328              :             use eos_def, only: i_lnPgas
    1329              :             type (star_info), pointer :: s
    1330              :             integer, intent(in) :: k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1331              :             real(dp), intent(out) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1332            0 :             dvardx0_m1 = 0d0; dvardx0_00 = 0d0; dvardx0_p1 = 0d0
    1333            0 :             if (i_var_xa_index > 0) then
    1334              :                dvardx0_00 = s% Pgas(k)/s% Peos(k) * &
    1335            0 :                   (s% d_eos_dxa(i_lnPgas,i_var_xa_index,k) - s% d_eos_dxa(i_lnPgas,i_var_sink_xa_index,k))
    1336            0 :             else if (i_var == s% i_lnd) then
    1337            0 :                dvardx0_00 = s% chiRho_for_partials(k)
    1338            0 :             else if (i_var == s% i_lnT) then
    1339            0 :                dvardx0_00 = s% chiT_for_partials(k)
    1340              :             end if
    1341            0 :          end subroutine get_lnP_partials
    1342              : 
    1343              : 
    1344            0 :          subroutine get_grad_ad_partials(s, &
    1345              :                k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1346              :                dvardx0_m1, dvardx0_00, dvardx0_p1)
    1347              :             use eos_def, only: i_grad_ad
    1348              :             type (star_info), pointer :: s
    1349              :             integer, intent(in) :: k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1350              :             real(dp), intent(out) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1351            0 :             dvardx0_m1 = 0d0; dvardx0_00 = 0d0; dvardx0_p1 = 0d0
    1352            0 :             if (i_var_xa_index > 0) then
    1353              :                dvardx0_00 = &
    1354            0 :                   (s% d_eos_dxa(i_grad_ad,i_var_xa_index,k) - s% d_eos_dxa(i_grad_ad,i_var_sink_xa_index,k))
    1355            0 :             else if (i_var == s% i_lnd) then
    1356            0 :                dvardx0_00 = s% d_eos_dlnd(i_grad_ad,k)
    1357            0 :             else if (i_var == s% i_lnT) then
    1358            0 :                dvardx0_00 = s% d_eos_dlnT(i_grad_ad,k)
    1359              :             end if
    1360            0 :          end subroutine get_grad_ad_partials
    1361              : 
    1362              : 
    1363            0 :          subroutine get_eps_nuc_partials(s, &
    1364              :                k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1365              :                dvardx0_m1, dvardx0_00, dvardx0_p1)
    1366              :             type (star_info), pointer :: s
    1367              :             integer, intent(in) :: k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1368              :             real(dp), intent(out) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1369            0 :             dvardx0_m1 = 0d0; dvardx0_00 = 0d0; dvardx0_p1 = 0d0
    1370            0 :             if (i_var > s% nvar_hydro) then
    1371            0 :                dvardx0_00 = s% d_epsnuc_dx(i_var_xa_index,k) - s% d_epsnuc_dx(i_var_sink_xa_index,k)
    1372            0 :             else if (i_var == s% i_lnd) then
    1373            0 :                dvardx0_00 = s% d_epsnuc_dlnd(k)
    1374            0 :             else if (i_var == s% i_lnT) then
    1375            0 :                dvardx0_00 = s% d_epsnuc_dlnT(k)
    1376              :             end if
    1377            0 :          end subroutine get_eps_nuc_partials
    1378              : 
    1379              : 
    1380            0 :          subroutine get_non_nuc_neu_partials(s, &
    1381              :                k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1382              :                dvardx0_m1, dvardx0_00, dvardx0_p1)
    1383              :             type (star_info), pointer :: s
    1384              :             integer, intent(in) :: k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1385              :             real(dp), intent(out) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1386            0 :             dvardx0_m1 = 0d0; dvardx0_00 = 0d0; dvardx0_p1 = 0d0
    1387            0 :             if (i_var > s% nvar_hydro) then
    1388              :                dvardx0_00 = 0d0
    1389            0 :             else if (i_var == s% i_lnd) then
    1390            0 :                dvardx0_00 = s% d_nonnucneu_dlnd(k)
    1391            0 :             else if (i_var == s% i_lnT) then
    1392            0 :                dvardx0_00 = s% d_nonnucneu_dlnT(k)
    1393              :             end if
    1394            0 :          end subroutine get_non_nuc_neu_partials
    1395              : 
    1396              : 
    1397            0 :          subroutine get_gradT_partials(s, &
    1398              :                k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1399              :                dvardx0_m1, dvardx0_00, dvardx0_p1)
    1400              :             type (star_info), pointer :: s
    1401              :             integer, intent(in) :: k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1402              :             real(dp), intent(out) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1403            0 :             dvardx0_m1 = 0d0; dvardx0_00 = 0d0; dvardx0_p1 = 0d0
    1404            0 :             if (i_var > s% nvar_hydro) then
    1405              :                dvardx0_00 = 0d0
    1406            0 :             else if (i_var == s% i_lnd) then
    1407            0 :                dvardx0_m1 = s% gradT_ad(k)%d1Array(i_lnd_m1)
    1408            0 :                dvardx0_00 = s% gradT_ad(k)%d1Array(i_lnd_00)
    1409            0 :             else if (i_var == s% i_lnT) then
    1410            0 :                dvardx0_m1 = s% gradT_ad(k)%d1Array(i_lnT_m1)
    1411            0 :                dvardx0_00 = s% gradT_ad(k)%d1Array(i_lnT_00)
    1412            0 :             else if (i_var == s% i_lnR) then
    1413            0 :                dvardx0_00 = s% gradT_ad(k)%d1Array(i_lnR_00)
    1414            0 :             else if (i_var == s% i_lum) then
    1415            0 :                dvardx0_00 = s% gradT_ad(k)%d1Array(i_L_00)
    1416            0 :             else if (i_var == s% i_w_div_wc) then
    1417            0 :                dvardx0_00 = s% gradT_ad(k)%d1Array(i_w_00)
    1418              :             end if
    1419            0 :          end subroutine get_gradT_partials
    1420              : 
    1421              : 
    1422            0 :          subroutine get_mlt_vc_partials(s, &
    1423              :                k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1424              :                dvardx0_m1, dvardx0_00, dvardx0_p1)
    1425              :             type (star_info), pointer :: s
    1426              :             integer, intent(in) :: k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1427              :             real(dp), intent(out) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1428            0 :             dvardx0_m1 = 0d0; dvardx0_00 = 0d0; dvardx0_p1 = 0d0
    1429            0 :             if (i_var > s% nvar_hydro) then
    1430              :                dvardx0_00 = 0d0
    1431            0 :             else if (i_var == s% i_lnd) then
    1432            0 :                dvardx0_m1 = s% mlt_vc_ad(k)%d1Array(i_lnd_m1)
    1433            0 :                dvardx0_00 = s% mlt_vc_ad(k)%d1Array(i_lnd_00)
    1434            0 :             else if (i_var == s% i_lnT) then
    1435            0 :                dvardx0_m1 = s% mlt_vc_ad(k)%d1Array(i_lnT_m1)
    1436            0 :                dvardx0_00 = s% mlt_vc_ad(k)%d1Array(i_lnT_00)
    1437            0 :             else if (i_var == s% i_lnR) then
    1438            0 :                dvardx0_00 = s% mlt_vc_ad(k)%d1Array(i_lnR_00)
    1439            0 :             else if (i_var == s% i_lum) then
    1440            0 :                dvardx0_00 = s% mlt_vc_ad(k)%d1Array(i_L_00)
    1441              :             end if
    1442            0 :          end subroutine get_mlt_vc_partials
    1443              : 
    1444              : 
    1445            0 :          subroutine get_opacity_partials(s, &
    1446              :                k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1447              :                dvardx0_m1, dvardx0_00, dvardx0_p1)
    1448              :             type (star_info), pointer :: s
    1449              :             integer, intent(in) :: k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index
    1450              :             real(dp), intent(out) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1451            0 :             dvardx0_m1 = 0d0; dvardx0_00 = 0d0; dvardx0_p1 = 0d0
    1452            0 :             if (i_var > s% nvar_hydro) then
    1453              :                dvardx0_00 = 0d0  ! s% d_opacity_dx(i_var_xa_index,k) - s% d_opacity_dx(i_var_sink_xa_index,k)
    1454            0 :             else if (i_var == s% i_lnd) then
    1455            0 :                dvardx0_00 = s% d_opacity_dlnd(k)
    1456            0 :             else if (i_var == s% i_lnT) then
    1457            0 :                dvardx0_00 = s% d_opacity_dlnT(k)
    1458              :             end if
    1459            0 :          end subroutine get_opacity_partials
    1460              : 
    1461              : 
    1462            0 :          subroutine test3_partials(s, &
    1463              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1464              :                k, save_dx, save_equ, ierr)
    1465              :             type (star_info), pointer :: s
    1466              :             integer, intent(in) :: &
    1467              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, k
    1468              :             real(dp), pointer, dimension(:,:) :: save_dx, save_equ
    1469              :             integer, intent(out) :: ierr
    1470              :             real(dp) :: dvardx0_m1, dvardx0_00, dvardx0_p1
    1471            0 :             dvardx0_m1 = 0d0
    1472            0 :             dvardx0_00 = 0d0
    1473            0 :             dvardx0_p1 = 0d0
    1474            0 :             if (i_equ > 0) then
    1475            0 :                if (i_var > s% nvar_hydro) then  ! testing abundance
    1476            0 :                   if (k > 1) dvardx0_m1 = &
    1477            0 :                      s% lblk(i_equ,i_var,k)/s% x_scale(i_var,k-1) - s% lblk(i_equ,i_var_sink,k)/s% x_scale(i_var_sink,k-1)
    1478              :                   dvardx0_00 = &
    1479            0 :                      s% dblk(i_equ,i_var,k)/s% x_scale(i_var,k) - s% dblk(i_equ,i_var_sink,k)/s% x_scale(i_var_sink,k)
    1480            0 :                   if (k < s% nz) dvardx0_p1 = &
    1481            0 :                      s% ublk(i_equ,i_var,k)/s% x_scale(i_var,k+1) - s% ublk(i_equ,i_var_sink,k)/s% x_scale(i_var_sink,k+1)
    1482              :                else
    1483            0 :                   if (k > 1) dvardx0_m1 = s% lblk(i_equ,i_var,k)/s% x_scale(i_var,k-1)
    1484            0 :                   dvardx0_00 = s% dblk(i_equ,i_var,k)/s% x_scale(i_var,k)
    1485            0 :                   if (k < s% nz) dvardx0_p1 = s% ublk(i_equ,i_var,k)/s% x_scale(i_var,k+1)
    1486              :                end if
    1487            0 :             else if (i_equ == -1) then  ! 'lnE'
    1488              :                call get_lnE_partials(s, &
    1489              :                   k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1490            0 :                   dvardx0_m1, dvardx0_00, dvardx0_p1)
    1491            0 :             elseif (i_equ == -2) then  ! 'eps_nuc'
    1492              :                call get_eps_nuc_partials(s, &
    1493              :                   k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1494            0 :                   dvardx0_m1, dvardx0_00, dvardx0_p1)
    1495            0 :             else if (i_equ == -3) then  ! 'opacity'
    1496              :                call get_opacity_partials(s, &
    1497              :                   k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1498            0 :                   dvardx0_m1, dvardx0_00, dvardx0_p1)
    1499            0 :             else if (i_equ == -4) then  ! 'lnP'
    1500              :                call get_lnP_partials(s, &
    1501              :                   k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1502            0 :                   dvardx0_m1, dvardx0_00, dvardx0_p1)
    1503            0 :             else if (i_equ == -5) then  ! 'non_nuc_neu'
    1504              :                call get_non_nuc_neu_partials(s, &
    1505              :                   k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1506            0 :                   dvardx0_m1, dvardx0_00, dvardx0_p1)
    1507            0 :             else if (i_equ == -6) then  ! 'gradT'
    1508              :                call get_gradT_partials(s, &
    1509              :                   k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1510            0 :                   dvardx0_m1, dvardx0_00, dvardx0_p1)
    1511            0 :             else if (i_equ == -7) then  ! 'mlt_vc'
    1512              :                call get_mlt_vc_partials(s, &
    1513              :                   k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1514            0 :                   dvardx0_m1, dvardx0_00, dvardx0_p1)
    1515            0 :             else if (i_equ == -8) then  ! 'grad_ad'
    1516              :                call get_grad_ad_partials(s, &
    1517              :                   k, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1518            0 :                   dvardx0_m1, dvardx0_00, dvardx0_p1)
    1519              :             end if
    1520            0 :             if (k > 1) then
    1521              :                call test1_partial(s, &
    1522              :                   i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1523            0 :                   k, -1, dvardx0_m1, save_dx, save_equ, ierr)
    1524            0 :                if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'test3_partials')
    1525              :             end if
    1526              :             call test1_partial(s, &
    1527              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1528            0 :                k, 0, dvardx0_00, save_dx, save_equ, ierr)
    1529            0 :             if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'test3_partials')
    1530            0 :             if (k < s% nz) then
    1531              :                call test1_partial(s, &
    1532              :                   i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1533            0 :                   k, 1, dvardx0_p1, save_dx, save_equ, ierr)
    1534            0 :                if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'test3_partials')
    1535              :             end if
    1536            0 :          end subroutine test3_partials
    1537              : 
    1538              : 
    1539            0 :          subroutine test1_partial(s, &
    1540              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1541              :                k, k_off, dvardx_0, save_dx, save_equ, ierr)
    1542              :             use chem_def, only: chem_isos
    1543              :             type (star_info), pointer :: s
    1544              :             integer, intent(in) :: &
    1545              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, k, k_off
    1546              :             real(dp), intent(in) :: dvardx_0
    1547              :             real(dp), pointer, dimension(:,:) :: save_dx, save_equ
    1548              :             character (len=3) :: k_off_str
    1549              :             integer, intent(out) :: ierr
    1550              :             character (len = 32) :: equ_str
    1551              :             real(dp) :: dx_0, err, dvardx, xdum, uncertainty
    1552              :             include 'formats'
    1553            0 :             ierr = 0
    1554              : 
    1555            0 :             if (i_var > s% nvar_hydro) then  ! testing abundance
    1556              :                dx_0 = s% solver_test_partials_dx_0 * &
    1557              :                   max(abs(s% xa_start(i_var_xa_index,k) + s% solver_dx(i_var,k)), &
    1558              :                       abs(s% xa_start(i_var_xa_index,k)), &
    1559            0 :                       1d-99)
    1560            0 :                write(*,1) 'var name ' // chem_isos% name(s% chem_id(i_var_xa_index))
    1561            0 :                write(*,1) 'sink name ' // chem_isos% name(s% chem_id(i_var_sink_xa_index))
    1562              :             else
    1563              :                dx_0 = s% solver_test_partials_dx_0 * &
    1564              :                   max(abs(s% xh_start(i_var,k) + s% solver_dx(i_var,k)), &
    1565            0 :                       abs(s% xh_start(i_var,k)))
    1566            0 :                if (dx_0 == 0d0) dx_0 = s% solver_test_partials_dx_0
    1567              :             end if
    1568              :             dvardx = dfridr(s, &
    1569              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1570            0 :                k, k_off, dx_0, save_dx, err)
    1571            0 :             if (dvardx == 0d0 .and. abs(dvardx_0) < 1d-14) then
    1572            0 :                xdum = 0d0
    1573            0 :             else if (dvardx_0 == 0d0 .and. abs(dvardx) < 1d-14) then
    1574            0 :                xdum = 0d0
    1575            0 :             else if (dvardx == 0d0 .or. dvardx_0 == 0d0) then
    1576            0 :                xdum = 1d0
    1577              :             else
    1578            0 :                xdum = abs(dvardx - dvardx_0)/min(abs(dvardx),abs(dvardx_0))
    1579              :             end if
    1580            0 :             if (ierr /= 0) then
    1581            0 :                write(*,*) 'test1_partial failed'
    1582            0 :                call mesa_error(__FILE__,__LINE__,'setmatrix')
    1583              :             end if
    1584            0 :             if (i_equ /= 0) then
    1585            0 :                if (k_off == 0) then
    1586            0 :                   k_off_str = ')  '
    1587            0 :                else if (k_off == -1) then
    1588            0 :                   k_off_str = '-1)'
    1589            0 :                else if (k_off == 1) then
    1590            0 :                   k_off_str = '+1)'
    1591              :                end if
    1592            0 :                if (dvardx /= 0d0) then
    1593            0 :                   uncertainty = abs(err/dvardx)
    1594              :                else
    1595            0 :                   uncertainty = 0d0
    1596              :                end if
    1597            0 :                if (xdum > 1d-5 .and. uncertainty < 1d-6) then
    1598            0 :                   write(*, '(a5,1x)', advance='no') '*****'
    1599            0 :                else if (uncertainty > 1d-7) then
    1600            0 :                   write(*, '(a5,1x)', advance='no') '?????'
    1601              :                else
    1602            0 :                   write(*, '(6x)', advance='no')
    1603              :                end if
    1604            0 :                if (i_equ > 0) then
    1605            0 :                   equ_str = s% nameofequ(i_equ)
    1606              :                else if (i_equ == -1) then
    1607            0 :                   equ_str = 'lnE'
    1608              :                else if (i_equ == -2) then
    1609            0 :                   equ_str = 'eps_nuc'
    1610              :                else if (i_equ == -3) then
    1611            0 :                   equ_str = 'opacity'
    1612              :                else if (i_equ == -4) then
    1613            0 :                   equ_str = 'lnP'
    1614              :                else if (i_equ == -5) then
    1615            0 :                   equ_str = 'non_nuc_neu'
    1616              :                else if (i_equ == -6) then
    1617            0 :                   equ_str = 'gradT'
    1618              :                else if (i_equ == -7) then
    1619            0 :                   equ_str = 'mlt_vc'
    1620              :                else if (i_equ == -8) then
    1621            0 :                   equ_str = 'grad_ad'
    1622              :                else
    1623            0 :                   equ_str = 'unknown'
    1624              :                end if
    1625              :                write(*,'(a25,3x,i5,4x,a,f12.5,4x,a,f12.5,99(4x,a,1pe22.12))') &
    1626              :                   'd_' // trim(equ_str) // '(k)/d_' // trim(s% nameofvar(i_var)) // &
    1627            0 :                   '(k' // trim(k_off_str), &
    1628            0 :                   k, 'lg rel diff', safe_log10(xdum), 'lg uncertainty', safe_log10(uncertainty), &
    1629            0 :                   'Analytic', dvardx_0, 'Numeric', dvardx
    1630              : !               write(*,'(a70,2x,i5,f10.3,3x,a,f10.3,99(3x,a,1pe26.16))') &
    1631              : !                  'log dfridr rel_diff partials wrt  '  // trim(s% nameofvar(i_var)) // &
    1632              : !                  '(k' // k_off_str // ' of ' // trim(equ_str) // '(k)', &
    1633              : !                  k, safe_log10(xdum), 'log uncertainty', safe_log10(uncertainty), &
    1634              : !                  'analytic', dvardx_0, 'numeric', dvardx, &
    1635              : !                  'analytic/numeric', abs(dvardx_0)/max(1d-99,abs(dvardx))
    1636              : 
    1637              :             else
    1638            0 :                write(*,'(A)')
    1639            0 :                write(*,1) 'analytic and numeric partials wrt ' // trim(s% nameofvar(i_var)), &
    1640            0 :                   dvardx_0, dvardx
    1641            0 :                write(*,1) 'log dfridr relative uncertainty for numeric partial', &
    1642            0 :                   safe_log10(err/max(1d-99,abs(dvardx)))
    1643            0 :                if (dvardx_0 /= 0d0) write(*,1) 'rel_diff', xdum
    1644              :             end if
    1645            0 :          end subroutine test1_partial
    1646              : 
    1647              : 
    1648            0 :          real(dp) function dfridr_func(s, &
    1649              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1650              :                k, k_off, delta_x, save_dx) result(val)
    1651              :             type (star_info), pointer :: s
    1652              :             integer, intent(in) :: &
    1653              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, k, k_off
    1654              :             real(dp), intent(in) :: delta_x
    1655              :             real(dp), pointer, dimension(:,:) :: save_dx
    1656              :             include 'formats'
    1657            0 :             s% solver_dx(i_var,k+k_off) = save_dx(i_var,k+k_off) + delta_x
    1658            0 :             if (i_var_xa_index > 0) then  ! changing abundance
    1659              :                !write(*,2) 'new dx, x for abundance', i_var, &
    1660              :                !     dx(i_var,k+k_off), s% xa(i_var - s% nvar_hydro,k+k_off)
    1661            0 :                if (i_var_sink_xa_index <= 0 .or. i_var_sink_xa_index > s% species) then
    1662            0 :                   write(*,2) 'bad i_var_sink_xa_index', i_var_sink_xa_index
    1663            0 :                   call mesa_error(__FILE__,__LINE__,'star_solver dfridr_func')
    1664              :                end if
    1665            0 :                s% solver_dx(i_var_sink,k+k_off) = save_dx(i_var_sink,k+k_off) - delta_x
    1666              :             end if
    1667            0 :             call do_equations(ierr)
    1668            0 :             if (ierr /= 0) then
    1669              :                !exit
    1670            0 :                write(*,3) 'call eval_equations failed in dfridr_func'
    1671            0 :                call mesa_error(__FILE__,__LINE__,'setmatrix')
    1672              :             end if
    1673            0 :             if (i_equ > 0) then
    1674            0 :                val = equ(i_equ,k)  ! testing partial of residual for cell k equation
    1675              :             else if (i_equ == 0) then
    1676            0 :                val = s% solver_test_partials_val
    1677              :             else if (i_equ == -1) then
    1678            0 :                val = s% lnE(k)
    1679              :             else if (i_equ == -2) then
    1680            0 :                val = s% eps_nuc(k)
    1681              :             else if (i_equ == -3) then
    1682            0 :                val = s% opacity(k)
    1683              :             else if (i_equ == -4) then
    1684            0 :                val = s% lnPeos(k)
    1685              :             else if (i_equ == -5) then
    1686            0 :                val = s% non_nuc_neu(k)
    1687              :             else if (i_equ == -6) then
    1688            0 :                val = s% gradT(k)
    1689              :             else if (i_equ == -7) then
    1690            0 :                val = s% mlt_vc(k)
    1691              :             else if (i_equ == -8) then
    1692            0 :                val = s% grada(k)
    1693              :             else
    1694              :                val = 0d0
    1695              :             end if
    1696            0 :             s% solver_dx(i_var,k+k_off) = save_dx(i_var,k+k_off)
    1697            0 :             if (i_var_sink > 0) &  ! restore sink abundance
    1698            0 :                s% solver_dx(i_var_sink,k+k_off) = save_dx(i_var_sink,k+k_off)
    1699            0 :          end function dfridr_func
    1700              : 
    1701              : 
    1702            0 :          real(dp) function dfridr(s, &
    1703              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1704              :                k, k_off, hx, save_dx, err)
    1705              :             type (star_info), pointer :: s
    1706              :             integer, intent(in) :: &
    1707              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, k, k_off
    1708              :             real(dp), intent(in) :: hx
    1709              :             real(dp), pointer, dimension(:,:) :: save_dx
    1710              :             real(dp), intent(out) :: err
    1711              :             !  this routine returns the first derivative of a function func(x)
    1712              :             !  at the point x, by ridders method of polynomial extrapolation.
    1713              :             !  value hx is the initial step size;
    1714              :             !  it should be an increment for which func changes substantially.
    1715              :             !  an estimate of the error in the first derivative is returned in err.
    1716              :             integer, parameter :: ntab = 20
    1717              :             integer :: i,j
    1718              :             real(dp) :: errt,fac,hh,a(ntab,ntab),f1,f2
    1719              :             real(dp), parameter :: con2=2d0, con=sqrt(con2), big=1d50, safe=2d0
    1720              :             include 'formats'
    1721            0 :             dfridr = 0d0
    1722            0 :             hh = hx
    1723              :             ! 2nd order central difference
    1724              :             f1 = dfridr_func(s, &
    1725              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1726            0 :                k, k_off, hh, save_dx)
    1727              :             !write(*,2) 'f1', 1, f1, save_dx(i_var,k) + hh
    1728              :             f2 = dfridr_func(s, &
    1729              :                i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1730            0 :                k, k_off, -hh, save_dx)
    1731              :             !write(*,2) 'f2', 1, f2, save_dx(i_var,k) - hh
    1732            0 :             a(1,1) = (f1 - f2)/(2d0*hh)
    1733              :             !write(*,2) 'dfdx', 1, a(1,1), &
    1734              :             !   hh, (save_dx(s% solver_test_partials_var,s% solver_test_partials_k) + hh)/ln10, &
    1735              :             !   save_dx(s% solver_test_partials_var,s% solver_test_partials_k)/ln10
    1736            0 :             err = big
    1737              :             ! successive columns in the neville tableu will go to smaller stepsizes
    1738              :             ! and higher orders of extrapolation
    1739            0 :             do i=2,ntab
    1740            0 :                hh = hh/con
    1741              :                f1 = dfridr_func(s, &
    1742              :                   i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1743            0 :                   k, k_off, hh, save_dx)
    1744              :                !write(*,2) 'f1', i, f1, save_dx(i_var,k) + hh
    1745              :                f2 = dfridr_func(s, &
    1746              :                   i_equ, i_var, i_var_sink, i_var_xa_index, i_var_sink_xa_index, &
    1747            0 :                   k, k_off, -hh, save_dx)
    1748              :                !write(*,2) 'f2', i, f2, save_dx(i_var,k) - hh
    1749            0 :                a(1,i) = (f1 - f2)/(2d0*hh)
    1750              :                !write(*,2) 'dfdx', i, a(1,i), &
    1751              :                !   hh, (save_dx(s% solver_test_partials_var,s% solver_test_partials_k) + hh)/ln10, &
    1752              :                !   save_dx(s% solver_test_partials_var,s% solver_test_partials_k)/ln10
    1753              :                ! compute extrapolations of various orders; the error strategy is to compare
    1754              :                ! each new extrapolation to one order lower but both at the same stepsize
    1755              :                ! and at the previous stepsize
    1756            0 :                fac = con2
    1757            0 :                do j=2,i
    1758            0 :                   a(j,i) = (a(j-1,i)*fac - a(j-1,i-1))/(fac-1d0)
    1759            0 :                   fac = con2*fac
    1760            0 :                   errt = max(abs(a(j,i)-a(j-1,i)),abs(a(j,i)-a(j-1,i-1)))
    1761            0 :                   if (errt <= err) then
    1762            0 :                      err = errt
    1763            0 :                      dfridr = a(j,i)
    1764              :                      !write(*,1) 'dfridr', err/dfridr
    1765              :                   end if
    1766              :                end do
    1767              :                ! if higher order is worse by a significant factor safe, then bail
    1768            0 :                if (abs(a(i,i) - a(i-1,i-1)) >= safe*err) then
    1769              :                   !write(*,1) 'higher order is worse'
    1770              :                   return
    1771              :                end if
    1772              :             end do
    1773              :          end function dfridr
    1774              : 
    1775              : 
    1776              :          subroutine set_xtras(x,num_xtra)
    1777              :             real(dp) :: x(:,:)
    1778              :             integer, intent(in) :: num_xtra
    1779              :             integer :: k
    1780              :             include 'formats'
    1781              :             if (.not. s% u_flag) then
    1782              :                x(1,1:nz) = 0
    1783              :                x(2,1:nz) = 0
    1784              :                return
    1785              :             end if
    1786              :             do k=1,nz
    1787              :                x(1,k) = s% u_face_ad(k)%val
    1788              :                if (is_bad_num(x(1,k))) then
    1789              :                   write(*,2) 'exit_setmatrix x(1,k)', k, x(1,k)
    1790              :                   stop
    1791              :                end if
    1792              :             end do
    1793              :             do k=1,nz
    1794              :                x(2,k) = s% P_face_ad(k)%val
    1795              :                if (is_bad_num(x(2,k))) then
    1796              :                   write(*,2) 'exit_setmatrix x(2,k)', k, x(2,k)
    1797              :                   stop
    1798              :                end if
    1799              :             end do
    1800              :          end subroutine set_xtras
    1801              : 
    1802              : 
    1803            0 :          subroutine store_mix_type_str(str, integer_string, i, k)
    1804              :             character (len=5) :: str
    1805              :             character (len=10) :: integer_string
    1806              :             integer, intent(in) :: i, k
    1807              :             integer :: mix_type, j
    1808            0 :             if (k < 1 .or. k > s% nz) then
    1809            0 :                str(i:i) = 'x'
    1810            0 :                return
    1811              :             end if
    1812            0 :             mix_type = s% mixing_type(k)
    1813            0 :             if (mix_type < 10) then
    1814            0 :                j = mix_type+1
    1815            0 :                str(i:i) = integer_string(j:j)
    1816              :             else
    1817            0 :                str(i:i) = '?'
    1818              :             end if
    1819              :          end subroutine store_mix_type_str
    1820              : 
    1821              : 
    1822            0 :          subroutine write_msg(msg)
    1823              :             use const_def, only: secyer
    1824              :             character(*)  :: msg
    1825              : 
    1826              :             integer :: k
    1827              :             character (len=64) :: max_resid_str, max_corr_str
    1828              :             character (len=5) :: max_resid_mix_type_str, max_corr_mix_type_str
    1829              :             character (len=10) :: integer_string
    1830              :             include 'formats'
    1831              : 
    1832            0 :             if (.not. dbg_msg) return
    1833              : 
    1834            0 :             if (max_resid_j < 0) then
    1835            0 :                call sizequ(s, nvar, residual_norm, max_residual, max_resid_k, max_resid_j, ierr)
    1836              :             end if
    1837              : 
    1838            0 :             if (max_resid_j > 0) then
    1839            0 :                write(max_resid_str,*) 'max resid ' // trim(s% nameofequ(max_resid_j))
    1840              :             else
    1841            0 :                max_resid_str = ''
    1842              :             end if
    1843              : 
    1844            0 :             if (max_corr_j < 0) then
    1845              :                call sizeB(s, nvar, B, &
    1846            0 :                   max_correction, correction_norm, max_corr_k, max_corr_j, ierr)
    1847              :             end if
    1848              : 
    1849            0 :             if (max_corr_j > 0) then
    1850            0 :                write(max_corr_str,*) 'max corr ' // trim(s% nameofvar(max_corr_j))
    1851              :             else
    1852            0 :                max_corr_str = ''
    1853              :             end if
    1854              : 
    1855            0 :             integer_string = '0123456789'
    1856            0 :             k = max_corr_k
    1857            0 :             call store_mix_type_str(max_corr_mix_type_str, integer_string, 1, k-2)
    1858            0 :             call store_mix_type_str(max_corr_mix_type_str, integer_string, 2, k-1)
    1859            0 :             call store_mix_type_str(max_corr_mix_type_str, integer_string, 3, k)
    1860            0 :             call store_mix_type_str(max_corr_mix_type_str, integer_string, 4, k+1)
    1861            0 :             call store_mix_type_str(max_corr_mix_type_str, integer_string, 5, k+2)
    1862              : 
    1863            0 :             k = max_resid_k
    1864            0 :             call store_mix_type_str(max_resid_mix_type_str, integer_string, 1, k-2)
    1865            0 :             call store_mix_type_str(max_resid_mix_type_str, integer_string, 2, k-1)
    1866            0 :             call store_mix_type_str(max_resid_mix_type_str, integer_string, 3, k)
    1867            0 :             call store_mix_type_str(max_resid_mix_type_str, integer_string, 4, k+1)
    1868            0 :             call store_mix_type_str(max_resid_mix_type_str, integer_string, 5, k+2)
    1869              : 
    1870              :   111       format(i6, i3, 2x, a, f7.4, &
    1871              :                1x, a, 1x, e10.3, 2x, a18, 1x, i5, e13.5, a, &
    1872              :                1x, a, 1x, e10.3, 2x, a16, 1x, i5, e13.5, a, &
    1873              :                1x, a)
    1874              : !  111       format(i6, 2x, i3, 2x, a, f8.4, &
    1875              : !               2x, a, 1x, e10.3, 2x, a19, 1x, i5, e11.3, 2x, a, &
    1876              : !               2x, a, 1x, e10.3, 2x, a14, 1x, i5, e11.3, 2x, a, &
    1877              : !               2x, a)
    1878              :             write(*,111) &
    1879            0 :                s% model_number, iter, &
    1880            0 :                'coeff', coeff,  &
    1881            0 :                'avg resid', residual_norm,  &
    1882              : !               '   avg resid', residual_norm,  &
    1883            0 :                trim(max_resid_str), max_resid_k, max_residual, &
    1884            0 :                ' mix type ' // trim(max_resid_mix_type_str),  &
    1885            0 :                'avg corr', correction_norm,  &
    1886              : !               'mix type ' // trim(max_resid_mix_type_str),  &
    1887              : !               '   avg corr', correction_norm,  &
    1888            0 :                trim(max_corr_str), max_corr_k, max_correction,  &
    1889            0 :                ' mix type ' // trim(max_corr_mix_type_str),  &
    1890            0 :                ' ' // trim(msg)
    1891              : !               'mix type ' // trim(max_corr_mix_type_str),  &
    1892              : !               '   ' // trim(msg)
    1893              : 
    1894            0 :             if (is_bad(slope)) call mesa_error(__FILE__,__LINE__,'write_msg')
    1895              : 
    1896              :          end subroutine write_msg
    1897              : 
    1898              : 
    1899           11 :          subroutine pointers(ierr)
    1900              :             use utils_lib, only: fill_with_NaNs, fill_with_NaNs_2D
    1901              : 
    1902              :             integer, intent(out) :: ierr
    1903              : 
    1904              :             integer :: i
    1905              : 
    1906           11 :             ierr = 0
    1907              : 
    1908           11 :             i = 1
    1909              : 
    1910           11 :             if(allocated(s% equ1)) then
    1911           10 :                if(size(s% equ1,dim=1) /= neq) then
    1912            4 :                   deallocate(s% equ1)
    1913           12 :                   allocate(s% equ1(1:neq))
    1914              :                end if
    1915              :             else
    1916            3 :                allocate(s% equ1(1:neq))
    1917              :             end if
    1918              : 
    1919           11 :             s% equ(1:nvar,1:nz) => s% equ1(1:neq)
    1920           11 :             equ1 => s% equ1
    1921           11 :             equ => s% equ
    1922              : 
    1923           33 :             allocate(dxsave1(1:neq))
    1924           33 :             allocate(ddxsave1(1:neq))
    1925           33 :             allocate(B1(1:neq))
    1926           33 :             allocate(soln1(1:neq))
    1927           33 :             allocate(grad_f1(1:neq))
    1928           44 :             allocate(rhs(1:nvar,1:nz))
    1929           44 :             allocate(xder(1:nvar,1:nz))
    1930           44 :             allocate(ddx(1:nvar,1:nz))
    1931           33 :             allocate(row_scale_factors1(1:neq))
    1932           33 :             allocate(col_scale_factors1(1:neq))
    1933           33 :             allocate(save_ublk1(1:nvar*neq))
    1934           33 :             allocate(save_dblk1(1:nvar*neq))
    1935           33 :             allocate(save_lblk1(1:nvar*neq))
    1936              : 
    1937           11 :             band_kl = 2*nvar
    1938           11 :             band_ku = 2*nvar
    1939           11 :             band_ldab = 2*band_kl + band_ku + 1
    1940           11 :             band_diag = band_kl + band_ku + 1
    1941           11 :             if (trim(s% hydro_matrix_solver) == 'banded') then
    1942            0 :                allocate(band1(1:band_ldab*neq))
    1943            0 :                band(1:band_ldab,1:neq) => band1(1:band_ldab*neq)
    1944              :             end if
    1945              : 
    1946           11 :             if (s% fill_arrays_with_NaNs) then
    1947            0 :                call fill_with_NaNs(dxsave1)
    1948            0 :                call fill_with_NaNs(ddxsave1)
    1949            0 :                call fill_with_NaNs(B1)
    1950            0 :                call fill_with_NaNs(soln1)
    1951            0 :                call fill_with_NaNs_2D(rhs)
    1952            0 :                call fill_with_NaNs_2D(xder)
    1953            0 :                call fill_with_NaNs_2D(ddx)
    1954            0 :                call fill_with_NaNs(row_scale_factors1)
    1955            0 :                call fill_with_NaNs(col_scale_factors1)
    1956            0 :                call fill_with_NaNs(save_ublk1)
    1957            0 :                call fill_with_NaNs(save_dblk1)
    1958            0 :                call fill_with_NaNs(save_lblk1)
    1959            0 :                if (associated(band1)) call fill_with_NaNs(band1)
    1960              :             end if
    1961              : 
    1962       170142 :             ddx = 0d0
    1963           11 :             dxsave(1:nvar,1:nz) => dxsave1(1:neq)
    1964           11 :             ddxsave(1:nvar,1:nz) => ddxsave1(1:neq)
    1965           11 :             B(1:nvar,1:nz) => B1(1:neq)
    1966           11 :             soln(1:nvar,1:nz) => soln1(1:neq)
    1967           11 :             grad_f(1:nvar,1:nz) => grad_f1(1:neq)
    1968              : 
    1969           33 :             allocate(ipiv1(1:neq))
    1970              : 
    1971           11 :             ipiv_blk1(1:neq) => ipiv1(1:neq)
    1972              : 
    1973           77 :             allocate(ublk1(1:nvar*neq),dblk1(1:nvar*neq),lblk1(1:nvar*neq))
    1974              : 
    1975           11 :             lblk(1:nvar,1:nvar,1:nz) => lblk1(1:nvar*neq)
    1976           11 :             dblk(1:nvar,1:nvar,1:nz) => dblk1(1:nvar*neq)
    1977           11 :             ublk(1:nvar,1:nvar,1:nz) => ublk1(1:nvar*neq)
    1978              : 
    1979           77 :             allocate(ublkF1(1:nvar*neq),dblkF1(1:nvar*neq),lblkF1(1:nvar*neq))
    1980              : 
    1981           11 :             lblkF(1:nvar,1:nvar,1:nz) => lblkF1(1:nvar*neq)
    1982           11 :             dblkF(1:nvar,1:nvar,1:nz) => dblkF1(1:nvar*neq)
    1983           11 :             ublkF(1:nvar,1:nvar,1:nz) => ublkF1(1:nvar*neq)
    1984              : 
    1985           11 :             if (s% fill_arrays_with_NaNs) then
    1986            0 :                call fill_with_NaNs(lblk1)
    1987            0 :                call fill_with_NaNs(dblk1)
    1988            0 :                call fill_with_NaNs(ublk1)
    1989            0 :                call fill_with_NaNs(lblkF1)
    1990            0 :                call fill_with_NaNs(dblkF1)
    1991            0 :                call fill_with_NaNs(ublkF1)
    1992              :             end if
    1993              : 
    1994           11 :          end subroutine pointers
    1995              : 
    1996           11 :          subroutine cleanup()
    1997              : 
    1998           11 :             if(associated(dxsave1)) deallocate(dxsave1)
    1999           11 :             if(associated(ddxsave)) deallocate(ddxsave)
    2000           11 :             if(associated(ddxsave)) deallocate(ddxsave)
    2001           11 :             if(associated(B1)) deallocate(B1)
    2002           11 :             if(associated(soln1)) deallocate(soln1)
    2003           11 :             if(associated(grad_f1)) deallocate(grad_f1)
    2004           11 :             if(associated(rhs)) deallocate(rhs)
    2005           11 :             if(associated(xder)) deallocate(xder)
    2006           11 :             if(associated(ddx)) deallocate(ddx)
    2007           11 :             if(associated(row_scale_factors1)) deallocate(row_scale_factors1)
    2008           11 :             if(associated(col_scale_factors1)) deallocate(col_scale_factors1)
    2009           11 :             if(associated(save_ublk1)) deallocate(save_ublk1)
    2010           11 :             if(associated(save_dblk1)) deallocate(save_dblk1)
    2011           11 :             if(associated(save_lblk1)) deallocate(save_lblk1)
    2012           11 :             if(associated(band1)) deallocate(band1)
    2013           11 :             if(associated(ipiv1)) deallocate(ipiv1)
    2014              : 
    2015           11 :             if(associated(ublk1)) deallocate(ublk1)
    2016           11 :             if(associated(dblk1)) deallocate(dblk1)
    2017           11 :             if(associated(lblk1)) deallocate(lblk1)
    2018           11 :             if(associated(ublkF1)) deallocate(ublkF1)
    2019           11 :             if(associated(dblkF1)) deallocate(dblkF1)
    2020           11 :             if(associated(lblkF1)) deallocate(lblkF1)
    2021              : 
    2022              : 
    2023           11 :             nullify(equ, equ1, dxsave1,dxsave, ddxsave, B1, &
    2024           11 :                      soln1, grad_f1, rhs, xder, ddx, row_scale_factors1,&
    2025           11 :                      col_scale_factors1, save_ublk1, save_dblk1, save_lblk1, band1,&
    2026           11 :                      B, soln, grad_f,ipiv1, ublk1, dblk1, lblk1, ublkF1,dblkF1, lblkF1, band)
    2027              : 
    2028           11 :          end subroutine cleanup
    2029              : 
    2030              : 
    2031            0 :          real(dp) function eval_slope(nvar, nz, grad_f, B)
    2032              :             integer, intent(in) :: nvar, nz
    2033              :             real(dp), intent(in), dimension(:,:) :: grad_f, B
    2034              :             integer :: i
    2035            0 :             eval_slope = 0
    2036            0 :             do i=1,nvar
    2037            0 :                eval_slope = eval_slope + dot_product(grad_f(i,1:nz),B(i,1:nz))
    2038              :             end do
    2039            0 :          end function eval_slope
    2040              : 
    2041              : 
    2042            0 :          real(dp) function eval_f(nvar, nz, equ)
    2043              :             integer, intent(in) :: nvar, nz
    2044              :             real(dp), intent(in), dimension(:,:) :: equ
    2045              :             integer :: k, i
    2046              :             real(dp) :: q
    2047              :             include 'formats'
    2048            0 :             eval_f = 0
    2049            0 :             do k = 1, nz
    2050            0 :                do i = 1, nvar
    2051            0 :                   q = equ(i,k)
    2052            0 :                   eval_f = eval_f + q*q
    2053              :                end do
    2054              :             end do
    2055            0 :             eval_f = eval_f/2
    2056            0 :          end function eval_f
    2057              : 
    2058              :       end subroutine do_solver
    2059              : 
    2060              :       end module star_solver
        

Generated by: LCOV version 2.0-1