LCOV - code coverage report
Current view: top level - star/private - pulse_utils.f90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 138 0
Test Date: 2026-12-03 14:59:23 Functions: 0.0 % 9 0

            Line data    Source code
       1              : ! ***********************************************************************
       2              : !
       3              : !   Copyright (C) 2010-2019  The MESA Team
       4              : !
       5              : !   This program is free software: you can redistribute it and/or modify
       6              : !   it under the terms of the GNU Lesser General Public License
       7              : !   as published by the Free Software Foundation,
       8              : !   either version 3 of the License, or (at your option) any later version.
       9              : !
      10              : !   This program is distributed in the hope that it will be useful,
      11              : !   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12              : !   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      13              : !   See the GNU Lesser General Public License for more details.
      14              : !
      15              : !   You should have received a copy of the GNU Lesser General Public License
      16              : !   along with this program. If not, see <https://www.gnu.org/licenses/>.
      17              : !
      18              : ! ***********************************************************************
      19              : 
      20              : module pulse_utils
      21              : 
      22              :   use star_private_def
      23              :   use const_def, only: dp, pi, pi4
      24              :   use num_lib
      25              :   use star_utils
      26              : 
      27              :   implicit none
      28              : 
      29              :   private
      30              :   public :: set_segment_indices
      31              :   public :: eval_face
      32              :   public :: eval_face_X
      33              :   public :: eval_face_A_ast
      34              :   public :: eval_face_rho
      35              :   public :: eval_center
      36              :   public :: eval_center_X
      37              :   public :: eval_center_rho
      38              :   public :: eval_center_d2
      39              : 
      40              : contains
      41              : 
      42            0 :   subroutine set_segment_indices (s, k_a, k_b, include_last_face)
      43              : 
      44              :     type(star_info), intent(in)       :: s
      45              :     integer, allocatable, intent(out) :: k_a(:)
      46              :     integer, allocatable, intent(out) :: k_b(:)
      47              :     logical, intent(in)               :: include_last_face
      48              : 
      49              :     logical, parameter :: dbg = .false.
      50              : 
      51            0 :     real(dp) :: mu_comp(s%nz)
      52            0 :     real(dp) :: grad_mu(s%nz)
      53            0 :     logical  :: mask(s%nz)
      54              :     integer  :: k
      55            0 :     integer  :: i(s%nz)
      56              :     integer  :: n_mk
      57              :     integer  :: n_sg
      58              :     integer  :: sg
      59              : 
      60              :     ! Set up index ranges for segments which are delineated by
      61              :     ! composition jumps
      62              : 
      63            0 :     if (s%add_double_points_to_pulse_data) then
      64              : 
      65              :        ! Calculate grad_mu from the mean molecular weight of the
      66              :        ! composition alone, assuming full ionization. Using s%mu
      67              :        ! (which includes the free electrons supplied by the EOS)
      68              :        ! would also flag ionization zones, where the composition is
      69              :        ! uniform; double points placed there introduce spurious
      70              :        ! density discontinuities
      71              : 
      72            0 :        mu_comp = s%abar(1:s%nz)/(1d0 + s%zbar(1:s%nz))
      73              : 
      74            0 :        grad_mu(1) = 0d0
      75              : 
      76            0 :        do k = 2, s%nz-1
      77            0 :           grad_mu(k) = log(mu_comp(k)/mu_comp(k-1))/log(s%Peos(k)/s%Peos(k-1))
      78              :        end do
      79              : 
      80            0 :        if (include_last_face) then
      81            0 :           grad_mu(k) = log(mu_comp(s%nz)/mu_comp(s%nz-1))/log(s%Peos(s%nz)/s%Peos(s%nz-1))
      82              :        else
      83            0 :           grad_mu(k) = 0d0
      84              :        end if
      85              : 
      86              :        ! Set up the mask marking faces which will have a double point
      87              : 
      88            0 :        mask = ABS(grad_mu) > s%threshold_grad_mu_for_double_point
      89              : 
      90            0 :        if (s%max_number_of_double_points > 0) then
      91              : 
      92              :           ! Limit the number of marked faces
      93              : 
      94            0 :           n_mk = MIN(s%max_number_of_double_points, s%nz)
      95              : 
      96            0 :           call qsort(i, s%nz, -ABS(grad_mu))
      97              : 
      98            0 :           mask(i(n_mk+1:)) = .false.
      99              : 
     100              :        end if
     101              : 
     102              :     else
     103              : 
     104            0 :        mask = .false.
     105              : 
     106              :     end if
     107              : 
     108              :     ! Use the mask to set up the index ranges
     109              : 
     110            0 :     n_sg = COUNT(mask) + 1
     111              : 
     112            0 :     allocate(k_a(n_sg))
     113            0 :     allocate(k_b(n_sg))
     114              : 
     115            0 :     sg = 1
     116              : 
     117            0 :     k_a(sg) = 1
     118              : 
     119            0 :     do k = 1, s%nz
     120              : 
     121            0 :        if (mask(k)) then
     122              : 
     123            0 :           k_b(sg) = k - 1
     124            0 :           sg = sg + 1
     125            0 :           k_a(sg) = k
     126              : 
     127              :           if (dbg) then
     128              :              write(*, 100) k, grad_mu(k)
     129              : 100          format('placing double point at k=', I6, 1X, '(grad_mu=', 1PE10.3, ')')
     130              :           end if
     131              : 
     132              :        end if
     133              : 
     134              :     end do
     135              : 
     136            0 :     k_b(sg) = s%nz
     137              : 
     138              : 
     139            0 :     return
     140              : 
     141              :   end subroutine set_segment_indices
     142              : 
     143              : 
     144            0 :   real(dp) function eval_face (dq, v, k, k_a, k_b, v_lo, v_hi) result (v_face)
     145              : 
     146              :     real(dp), intent(in)           :: dq(:)
     147              :     real(dp), intent(in)           :: v(:)
     148              :     integer, intent(in)            :: k
     149              :     integer, intent(in)            :: k_a
     150              :     integer, intent(in)            :: k_b
     151              :     real(dp), intent(in), optional :: v_lo
     152              :     real(dp), intent(in), optional :: v_hi
     153              : 
     154              :     ! Evaluate v at face k, by interpolating (or extrapolating, if
     155              :     ! k==k_a or k==k_b+1) from cells k_a:k_b
     156              : 
     157            0 :     if (k_b < k_a) call mesa_error(__FILE__,__LINE__,'eval_face: invalid segment indices')
     158            0 :     if (k < k_a .OR. k > k_b+1) call mesa_error(__FILE__,__LINE__,'eval_face: out-of-bounds interpolation')
     159              : 
     160            0 :     if (k_b == k_a) then
     161              : 
     162              :        ! Using a single cell
     163              : 
     164            0 :        v_face = v(k_a)
     165              : 
     166              :     else
     167              : 
     168              :        ! Using multiple cells
     169              : 
     170            0 :        if (k == k_a) then
     171            0 :           v_face = v(k_a) - dq(k_a)*(v(k_a+1) - v(k_a))/(dq(k_a+1) + dq(k_a))
     172            0 :        elseif (k == k_a+1) then
     173            0 :           v_face = v(k_a) + dq(k_a)*(v(k_a+1) - v(k_a))/(dq(k_a+1) + dq(k_a))
     174            0 :        elseif (k == k_b) then
     175            0 :           v_face = v(k_b) - dq(k_b)*(v(k_b) - v(k_b-1))/(dq(k_b) + dq(k_b-1))
     176            0 :        elseif (k == k_b+1) then
     177            0 :           v_face = v(k_b) + dq(k_b)*(v(k_b) - v(k_b-1))/(dq(k_b) + dq(k_b-1))
     178              :        else
     179            0 :           v_face = interp_val_to_pt(v(k_a:k_b), k-k_a+1, k_b-k_a+1, dq(k_a:k_b), 'pulse_utils : eval_face')
     180              :        end if
     181              : 
     182              :     end if
     183              : 
     184              :     ! Apply limits
     185              : 
     186            0 :     if (PRESENT(v_lo)) then
     187            0 :        v_face = MAX(v_face, v_lo)
     188              :     end if
     189              : 
     190            0 :     if (PRESENT(v_hi)) then
     191            0 :        v_face = MIN(v_face, v_hi)
     192              :     end if
     193              : 
     194              : 
     195              :     return
     196              : 
     197              :   end function eval_face
     198              : 
     199              : 
     200            0 :   real(dp) function eval_face_X (s, i, k, k_a, k_b) result (X_face)
     201              : 
     202              :     type(star_info), intent(in)    :: s
     203              :     integer, intent(in)            :: i
     204              :     integer, intent(in)            :: k
     205              :     integer, intent(in)            :: k_a
     206              :     integer, intent(in)            :: k_b
     207              : 
     208              :     ! Evaluate the abundance for species i at face k, by interpolating
     209              :     ! (or extrapolating, if k==k_a or k==k_b+1) from cells k_a:k_b
     210              : 
     211            0 :     if (k_b < k_a) call mesa_error(__FILE__,__LINE__,'eval_face_X: invalid segment indices')
     212            0 :     if (k < k_a .OR. k > k_b+1) call mesa_error(__FILE__,__LINE__,'eval_face_X: out-of-bounds interpolation')
     213              : 
     214            0 :     if (i >= 1) then
     215              : 
     216            0 :        if (k_b == k_a) then
     217              : 
     218              :           ! Using a single cell
     219              : 
     220            0 :           X_face = s%xa(i,k_a)
     221              : 
     222              :        else
     223              : 
     224              :           ! Using multiple cells
     225              : 
     226            0 :           if (k == k_a) then
     227            0 :              X_face = s%xa(i,k_a) - s%dq(k_a)*(s%xa(i,k_a+1) - s%xa(i,k_a))/(s%dq(k_a+1) + s%dq(k_a))
     228            0 :           elseif (k == k_a+1) then
     229            0 :              X_face = s%xa(i,k_a) + s%dq(k_a)*(s%xa(i,k_a+1) - s%xa(i,k_a))/(s%dq(k_a+1) + s%dq(k_a))
     230            0 :           elseif (k == k_b) then
     231            0 :              X_face = s%xa(i,k_b) - s%dq(k_b)*(s%xa(i,k_b) - s%xa(i,k_b-1))/(s%dq(k_b) + s%dq(k_b-1))
     232            0 :           elseif (k == k_b+1) then
     233            0 :              X_face = s%xa(i,k_b) + s%dq(k_b)*(s%xa(i,k_b) - s%xa(i,k_b-1))/(s%dq(k_b) + s%dq(k_b-1))
     234              :           else
     235            0 :              X_face = interp_val_to_pt(s%xa(i,k_a:k_b), k-k_a+1, k_b-k_a+1, s%dq(k_a:k_b), 'pulse_utils : eval_face_X')
     236              :           end if
     237              : 
     238              :        end if
     239              : 
     240              :        ! Apply limits
     241              : 
     242            0 :        X_face = MIN(1d0, MAX(0d0, X_face))
     243              : 
     244              :     else
     245              : 
     246              :        X_face = 0d0
     247              : 
     248              :     end if
     249              : 
     250              : 
     251              :     return
     252              : 
     253              :   end function eval_face_X
     254              : 
     255              : 
     256            0 :   real(dp) function eval_face_A_ast (s, k, k_a, k_b) result (A_ast_face)
     257              : 
     258              :     type(star_info), intent(in) :: s
     259              :     integer, intent(in)         :: k
     260              :     integer, intent(in)         :: k_a
     261              :     integer, intent(in)         :: k_b
     262              : 
     263              :     real(dp) :: A_ast_1
     264              :     real(dp) :: A_ast_2
     265              : 
     266              :     ! Evaluate A*=N2*r/g (A_ast) at face k, using data from faces
     267              :     ! k_a:k_b+1. If k==k_a or k==k_b+1, then use extrapolation from
     268              :     ! neighboring faces
     269              : 
     270            0 :     if (k_b < k_a) call mesa_error(__FILE__,__LINE__,'eval_face_A_ast: invalid segment indices')
     271            0 :     if (k < k_a .OR. k > k_b+1) call mesa_error(__FILE__,__LINE__,'eval_face_A_ast: out-of-bounds interpolation')
     272              : 
     273            0 :     if (.not. s% calculate_Brunt_N2) call mesa_error(__FILE__,__LINE__,'eval_face_A_ast: must have calculate_Brunt_N2 = .true.')
     274              : 
     275            0 :     if (k_b == k_a) then
     276              : 
     277            0 :        A_ast_face = s%brunt_N2(k)*s%r(k)/s%grav(k)
     278              : 
     279              :     else
     280              : 
     281            0 :        if (k == k_a) then
     282              : 
     283            0 :           A_ast_1 = s%brunt_N2(k_a+1)*s%r(k_a+1)/s%grav(k_a+1)
     284            0 :           A_ast_2 = s%brunt_N2(k_a+2)*s%r(k_a+2)/s%grav(k_a+2)
     285              : 
     286            0 :           A_ast_face = A_ast_1 - s%dq(k_a)*(A_ast_2 - A_ast_1)/s%dq(k_a+1)
     287              : 
     288            0 :        elseif (k == k_b+1) then
     289              : 
     290            0 :           A_ast_1 = s%brunt_N2(k_b-1)*s%r(k_b-1)/s%grav(k_b)
     291            0 :           A_ast_2 = s%brunt_N2(k_b)*s%r(k_b)/s%grav(k_b)
     292              : 
     293            0 :           A_ast_face = A_ast_2 + s%dq(k_b)*(A_ast_2 - A_ast_1)/s%dq(k_b-1)
     294              : 
     295              :        else
     296              : 
     297            0 :           A_ast_face = s%brunt_N2(k)*s%r(k)/s%grav(k)
     298              : 
     299              :        end if
     300              : 
     301              :     end if
     302              : 
     303              : 
     304              :     return
     305              : 
     306              :   end function eval_face_A_ast
     307              : 
     308              : 
     309            0 :   real(dp) function eval_face_rho (s, k, k_a, k_b) result (rho_face)
     310              : 
     311              : 
     312              :     type(star_info), intent(in) :: s
     313              :     integer, intent(in)         :: k
     314              :     integer, intent(in)         :: k_a
     315              :     integer, intent(in)         :: k_b
     316              : 
     317              :     real(dp) :: r
     318              :     real(dp) :: dm
     319              :     real(dp) :: dlnr
     320              : 
     321              :     ! Evaluate rho at face k, using data from cells k_a:k_b
     322              : 
     323            0 :     if (k_b < k_a) call mesa_error(__FILE__,__LINE__,'eval_face_rho: invalid segment indices')
     324            0 :     if (k < k_a .OR. k > k_b+1) call mesa_error(__FILE__,__LINE__,'eval_face_rho: out-of-bounds interpolation')
     325              : 
     326            0 :     if (k_b == k_a) then
     327              : 
     328            0 :        rho_face = s%rho(k)
     329              : 
     330              :     else
     331              : 
     332            0 :        if (k == k_a) then
     333              : 
     334            0 :           r = s%r(k_a)
     335              : 
     336            0 :           dm = 0.5d0*s%dm(k_a)
     337            0 :           dlnr = 1d0 - s%rmid(k_a)/r
     338              : 
     339            0 :        elseif (k == k_b+1) then
     340              : 
     341            0 :           r = s%r(k_b+1)
     342              : 
     343            0 :           dm = 0.5d0*s%dm(k_b)
     344            0 :           dlnr = s%rmid(k_b)/r - 1d0
     345              : 
     346              :        else
     347              : 
     348            0 :           r = s%r(k)
     349              : 
     350            0 :           dm = 0.5d0*(s%dm(k) + s%dm(k-1))
     351            0 :           dlnr = (s%rmid(k-1) - s%rmid(k))/r
     352              : 
     353              :        end if
     354              : 
     355            0 :        rho_face = dm/(pi4*r*r*r*dlnr)
     356              : 
     357              :     end if
     358              : 
     359            0 :   end function eval_face_rho
     360              : 
     361              : 
     362            0 :   real(dp) function eval_center (r, v, k_a, k_b, v_lo, v_hi) result (v_center)
     363              : 
     364              :     real(dp), intent(in)           :: r(:)
     365              :     real(dp), intent(in)           :: v(:)
     366              :     integer, intent(in)            :: k_a
     367              :     integer, intent(in)            :: k_b
     368              :     real(dp), intent(in), optional :: v_lo
     369              :     real(dp), intent(in), optional :: v_hi
     370              : 
     371              :     real(dp) :: r_1
     372              :     real(dp) :: r_2
     373              :     real(dp) :: v_1
     374              :     real(dp) :: v_2
     375              : 
     376              :     ! Evaluate v at the center, by extrapolating from cells/faces
     377              :     ! k_a:k_b
     378              : 
     379            0 :     if (k_b < k_a) call mesa_error(__FILE__,__LINE__,'eval_center: invalid segment indices')
     380              : 
     381            0 :     if (k_a == k_b) then
     382              : 
     383              :        ! Using a single cell/face
     384              : 
     385            0 :        v_center = v(k_a)
     386              : 
     387              :     else
     388              : 
     389              :        ! Using the innermost two cells/faces in k_a:k_b; fit a parabola,
     390              :        ! with dv/dr = 0 at the center
     391              : 
     392            0 :        r_1 = r(k_b)
     393            0 :        r_2 = r(k_b-1)
     394              : 
     395            0 :        v_1 = v(k_b)
     396            0 :        v_2 = v(k_b-1)
     397              : 
     398            0 :        v_center = (v_1*r_2*r_2 - v_2*r_1*r_1)/(r_2*r_2 - r_1*r_1)
     399              : 
     400              :     end if
     401              : 
     402              :     ! Apply limits
     403              : 
     404            0 :     if (PRESENT(v_lo)) then
     405            0 :        v_center = MAX(v_center, v_lo)
     406              :     end if
     407              : 
     408            0 :     if (PRESENT(v_hi)) then
     409            0 :        v_center = MIN(v_center, v_hi)
     410              :     end if
     411              : 
     412            0 :   end function eval_center
     413              : 
     414              : 
     415            0 :   real(dp) function eval_center_X (s, i, k_a, k_b) result (X_center)
     416              : 
     417              :     type(star_info), intent(in) :: s
     418              :     integer, intent(in)         :: i
     419              :     integer, intent(in)         :: k_a
     420              :     integer, intent(in)         :: k_b
     421              : 
     422              :     real(dp) :: r_1
     423              :     real(dp) :: r_2
     424              :     real(dp) :: X_1
     425              :     real(dp) :: X_2
     426              : 
     427              :     ! Evaluate the abundance for species i at the center, by
     428              :     ! extrapolating from cells k_a:k_b
     429              : 
     430            0 :     if (i >= 1) then
     431              : 
     432            0 :        if (k_b < k_a) call mesa_error(__FILE__,__LINE__,'eval_center: invalid segment indices')
     433              : 
     434            0 :        if (k_a == k_b) then
     435              : 
     436              :           ! Using a single cell
     437              : 
     438            0 :           X_center = s%xa(i,k_a)
     439              : 
     440              :        else
     441              : 
     442              :           ! Using the innermost two cells/faces in k_a:k_b; fit a parabola,
     443              :           ! with dv/dr = 0 at the center
     444              : 
     445            0 :           r_1 = s%rmid(k_b)
     446            0 :           r_2 = s%rmid(k_b-1)
     447              : 
     448            0 :           X_1 = s%xa(i,k_b)
     449            0 :           X_2 = s%xa(i,k_b-1)
     450              : 
     451            0 :           X_center = (X_1*r_2*r_2 - X_2*r_1*r_1)/(r_2*r_2 - r_1*r_1)
     452              : 
     453              :        end if
     454              : 
     455              :        ! Apply limits
     456              : 
     457            0 :        X_center = MIN(1d0, MAX(0d0, X_center))
     458              : 
     459              :     else
     460              : 
     461              :        X_center = 0d0
     462              : 
     463              :     end if
     464              : 
     465              : 
     466              :     return
     467              : 
     468              :   end function eval_center_X
     469              : 
     470              : 
     471            0 :   real(dp) function eval_center_rho (s, k_b) result (rho_center)
     472              : 
     473              :     type(star_info), intent(in) :: s
     474              :     integer, intent(in)         :: k_b
     475              : 
     476              :     real(dp) :: r_1
     477              :     real(dp) :: rho_1
     478              :     real(dp) :: M_1
     479              : 
     480              :     ! Evaluate rho at the center, by extrapolating from cell k_b
     481              : 
     482              :     ! Fit a parabola with drho/dr = 0 at the center, which conserves mass
     483              : 
     484            0 :     r_1 = s%rmid(k_b)
     485            0 :     rho_1 = s%rho(k_b)
     486            0 :     M_1 = s%m(k_b) - 0.5d0*s%dm(k_b)
     487              : 
     488            0 :     rho_center = 3d0*(5d0*M_1/(pi*r_1*r_1*r_1) - 4d0*rho_1)/8d0
     489              : 
     490            0 :   end function eval_center_rho
     491              : 
     492              : 
     493            0 :   real(dp) function eval_center_d2 (r, v, k_a, k_b) result (d2v_center)
     494              : 
     495              :     real(dp), intent(in) :: r(:)
     496              :     real(dp), intent(in) :: v(:)
     497              :     integer, intent(in)  :: k_a
     498              :     integer, intent(in)  :: k_b
     499              : 
     500              :     real(dp) :: r_1
     501              :     real(dp) :: r_2
     502              :     real(dp) :: v_1
     503              :     real(dp) :: v_2
     504              : 
     505              :     ! Evaluate d2v/dr2 at the center, by extrapolating from
     506              :     ! cells/faces k_a:k_b
     507              : 
     508            0 :     if (k_b < k_a) call mesa_error(__FILE__,__LINE__,'eval_center_d2: invalid segment indices')
     509              : 
     510            0 :     if (k_a == k_b) then
     511              : 
     512              :        ! Using a single cell/face
     513              : 
     514              :        d2v_center = 0d0
     515              : 
     516              :     else
     517              : 
     518              :        ! Using the innermost two cells/faces; fit a parabola, with
     519              :        ! dv/dq = 0 at the center
     520              : 
     521            0 :        r_1 = r(k_b)
     522            0 :        r_2 = r(k_b-1)
     523              : 
     524            0 :        v_1 = v(k_b)
     525            0 :        v_2 = v(k_b-1)
     526              : 
     527            0 :        d2v_center = 2d0*(v_2 - v_1)/(r_2*r_2 - r_1*r_1)
     528              : 
     529              :     end if
     530              : 
     531            0 :   end function eval_center_d2
     532              : 
     533              : end module pulse_utils
        

Generated by: LCOV version 2.0-1