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 eval_weak
21 :
22 : use const_def, only: dp
23 : use math_lib
24 : use utils_lib, only: mesa_error
25 : use rates_def
26 : use suzuki_tables
27 :
28 : implicit none
29 :
30 : contains
31 :
32 86293 : subroutine do_eval_weak_reaction_info( &
33 86293 : n, ids, reaction_ids, T9, YeRho, &
34 : eta, d_eta_dlnT, d_eta_dlnRho, &
35 86293 : lambda, dlambda_dlnT, dlambda_dlnRho, &
36 86293 : Q, dQ_dlnT, dQ_dlnRho, &
37 86293 : Qneu, dQneu_dlnT, dQneu_dlnRho, &
38 : ierr)
39 : use const_def, only: ln10, kerg, mev_to_ergs
40 : use chem_def
41 : use interp_1d_def
42 : use utils_lib, only: is_bad
43 : integer, intent(in) :: n, ids(:), reaction_ids(:)
44 : real(dp), intent(in) :: T9, YeRho, eta, d_eta_dlnT, d_eta_dlnRho
45 : real(dp), dimension(:), intent(inout) :: &
46 : lambda, dlambda_dlnT, dlambda_dlnRho, &
47 : Q, dQ_dlnT, dQ_dlnRho, &
48 : Qneu, dQneu_dlnT, dQneu_dlnRho
49 : integer, intent(out) :: ierr
50 :
51 : real(dp) :: alfa, beta, d_alfa_dlnT, alfa_hi_Z, beta_hi_Z, d_alfa_hi_Z_dlnT
52 : integer :: i, ir, cid
53 :
54 : include 'formats'
55 :
56 : call do_eval_weaklib_reaction_info( &
57 : n, ids, T9, YeRho, &
58 : eta, d_eta_dlnT, d_eta_dlnRho, &
59 : lambda, dlambda_dlnT, dlambda_dlnRho, &
60 : Q, dQ_dlnT, dQ_dlnRho, &
61 : Qneu, dQneu_dlnT, dQneu_dlnRho, &
62 86293 : ierr)
63 86293 : if (ierr /= 0) then
64 : !write(*,*) 'failed in do_eval_weaklib_reaction_info'
65 : return
66 : end if
67 :
68 : !if (T9 >= max(T9_weaklib_full_on, T9_weaklib_full_on_hi_Z)) return
69 :
70 : ! revise lambda using rate for low T
71 : ! alfa is fraction from weaklib
72 86293 : if (T9 >= T9_weaklib_full_on) then
73 : alfa = 1d0
74 : d_alfa_dlnT = 0d0
75 70165 : else if (T9 > T9_weaklib_full_off) then
76 : alfa = (T9 - T9_weaklib_full_off)/ &
77 18499 : (T9_weaklib_full_on - T9_weaklib_full_off)
78 : d_alfa_dlnT = T9 / &
79 18499 : (T9_weaklib_full_on - T9_weaklib_full_off)
80 : else
81 : alfa = 0d0
82 : d_alfa_dlnT = 0d0
83 : end if
84 86293 : beta = 1d0 - alfa ! beta is fraction for low T
85 :
86 86293 : if (T9 >= T9_weaklib_full_on_hi_Z) then
87 : alfa_hi_Z = 1d0
88 : d_alfa_hi_Z_dlnT = 0d0
89 77321 : else if (T9 > T9_weaklib_full_off_hi_Z) then
90 : alfa_hi_Z = (T9 - T9_weaklib_full_off_hi_Z)/ &
91 4 : (T9_weaklib_full_on_hi_Z - T9_weaklib_full_off_hi_Z)
92 : d_alfa_hi_Z_dlnT = T9 / &
93 4 : (T9_weaklib_full_on_hi_Z - T9_weaklib_full_off_hi_Z)
94 : else
95 : alfa_hi_Z = 0d0
96 : d_alfa_hi_Z_dlnT = 0d0
97 : end if
98 86293 : beta_hi_Z = 1d0 - alfa_hi_Z
99 :
100 800571 : do i = 1, n
101 714278 : ir = reaction_ids(i)
102 714278 : if (ir == 0) cycle
103 714274 : cid = weak_reaction_info(1,ir)
104 714274 : if (weak_lowT_rate(ir) <= 0d0) cycle
105 13471 : if (cid <= 0) cycle
106 99764 : if (ids(i) <= 0) then
107 8972 : lambda(i) = weak_lowT_rate(ir)
108 8972 : dlambda_dlnT(i) = 0d0
109 8972 : dlambda_dlnRho(i) = 0d0
110 4499 : else if (chem_isos% Z(cid) >= weaklib_blend_hi_Z) then
111 : ! lambda(i) currently holds the tabulated weak rate, so
112 : ! compute the blended dlambda_dlnT first, before we
113 : ! overwrite lamnbda(i) with the blended rate
114 : dlambda_dlnT(i) = alfa_hi_Z*dlambda_dlnT(i) + &
115 4494 : d_alfa_hi_Z_dlnT * (lambda(i) - weak_lowT_rate(ir))
116 4494 : lambda(i) = alfa_hi_Z*lambda(i) + beta_hi_Z*weak_lowT_rate(ir)
117 4494 : dlambda_dlnRho(i) = alfa_hi_Z*dlambda_dlnRho(i)
118 : else
119 : dlambda_dlnT(i) = alfa*dlambda_dlnT(i) + &
120 5 : d_alfa_dlnT * (lambda(i) - weak_lowT_rate(ir))
121 5 : lambda(i) = alfa*lambda(i) + beta*weak_lowT_rate(ir)
122 5 : dlambda_dlnRho(i) = alfa*dlambda_dlnRho(i)
123 : end if
124 : end do
125 :
126 : end subroutine do_eval_weak_reaction_info
127 :
128 :
129 86293 : subroutine do_eval_weaklib_reaction_info( &
130 86293 : n, ids, T9_in, YeRho_in, &
131 : eta, d_eta_dlnT, d_eta_dlnRho, &
132 86293 : lambda, dlambda_dlnT, dlambda_dlnRho, &
133 86293 : Q, dQ_dlnT, dQ_dlnRho, &
134 86293 : Qneu, dQneu_dlnT, dQneu_dlnRho, &
135 : ierr)
136 : use const_def, only: ln10, kerg, mev_to_ergs
137 : use chem_def
138 : use interp_1d_def
139 : use utils_lib, only: is_bad, integer_dict_lookup
140 : integer, intent(in) :: n, ids(:)
141 : real(dp), intent(in) :: T9_in, YeRho_in, eta, d_eta_dlnT, d_eta_dlnRho
142 : real(dp), dimension(:), intent(inout) :: &
143 : lambda, dlambda_dlnT, dlambda_dlnRho, &
144 : Q, dQ_dlnT, dQ_dlnRho, &
145 : Qneu, dQneu_dlnT, dQneu_dlnRho
146 : integer, intent(out) :: ierr
147 :
148 : logical, parameter :: dbg = .false.
149 :
150 : real(dp) :: T, T9, YeRho, lYeRho
151 : integer :: i, ir, in, out, rxn_idx
152 : logical :: neg
153 : real(dp) :: Qx, conv, mue, d_mue_dlnRho, d_mue_dlnT
154 : character(len=iso_name_length) :: weak_lhs, weak_rhs
155 : integer, parameter :: nwork = pm_work_size
156 :
157 : real(dp) :: &
158 : s_lambda, s_dlambda_dlnT, s_dlambda_dlnRho, &
159 : s_Qneu, s_dQneu_dlnT, s_dQneu_dlnRho
160 :
161 : character(len=2*iso_name_length+1) :: key
162 :
163 : class(weak_rate_table), pointer :: table
164 :
165 : include 'formats'
166 :
167 86293 : ierr = 0
168 :
169 86293 : T9 = T9_in
170 86293 : YeRho = YeRho_in
171 86293 : lYeRho = log10(YeRho_in)
172 86293 : if (is_bad(lYeRho)) then
173 0 : ierr = -1
174 0 : return
175 :
176 : write(*,1) 'lYeRho', lYeRho
177 : write(*,1) 'YeRho_in', YeRho_in
178 : write(*,1) 'log10(YeRho_in)', log10(YeRho_in)
179 : !call mesa_error(__FILE__,__LINE__,'weak lYeRho')
180 : end if
181 :
182 86293 : if (n == 0) then
183 0 : write(*,*) 'problem in eval_weak_reaction_info: n == 0'
184 0 : write(*,2) 'n', n
185 0 : write(*,1) 'T9', T9
186 0 : return
187 : end if
188 :
189 800571 : do i = 1, n
190 :
191 714278 : lambda(i) = 0d0
192 714278 : dlambda_dlnT(i) = 0d0
193 714278 : dlambda_dlnRho(i) = 0d0
194 714278 : Q(i) = 0d0
195 714278 : dQ_dlnT(i) = 0d0
196 714278 : dQ_dlnRho(i) = 0d0
197 714278 : Qneu(i) = 0d0
198 714278 : dQneu_dlnT(i) = 0d0
199 714278 : dQneu_dlnRho(i) = 0d0
200 :
201 714278 : ir = ids(i)
202 714278 : if (ir <= 0) cycle
203 4962 : if (ir > size(weak_reactions_tables)) then
204 0 : call show_stuff
205 : write(*,*) 'bad ir', ir, i
206 : call mesa_error(__FILE__,__LINE__)
207 : ierr = -1
208 : return
209 : end if
210 :
211 4962 : table => weak_reactions_tables(ir) % t
212 :
213 4962 : T9 = T9_in
214 4962 : YeRho = YeRho_in
215 4962 : lYeRho = log10(YeRho_in)
216 :
217 : ! convert to MeV
218 4962 : conv = kerg/mev_to_ergs
219 4962 : T = T9*1d9
220 4962 : mue = eta*conv*T
221 4962 : d_mue_dlnRho = d_eta_dlnRho*conv*T
222 4962 : if (d_eta_dlnT == 0) then
223 : d_mue_dlnT = 0
224 : else
225 : d_mue_dlnT = d_eta_dlnT*conv*T + mue
226 : end if
227 :
228 : ! clip small values to edge of table
229 4962 : if (T9 < table % T9s(1)) &
230 4 : T9 = table % T9s(1)
231 4962 : if (lYeRho < table % lYeRhos(1)) &
232 0 : lYeRho = table % lYeRhos(1)
233 :
234 : ! clip large values to edge of table
235 4962 : if (T9 > table % T9s(table % num_T9)) &
236 0 : T9 = table % T9s(table % num_T9)
237 4962 : if (lYeRho > table % lYeRhos(table % num_lYeRho)) &
238 1 : lYeRho = table % lYeRhos(table % num_lYeRho)
239 :
240 : call table% interpolate(T9, lYeRho, &
241 : lambda(i), dlambda_dlnT(i), dlambda_dlnRho(i), &
242 4962 : Qneu(i), dQneu_dlnT(i), dQneu_dlnRho(i), ierr)
243 :
244 4962 : in = weak_lhs_nuclide_id(ir)
245 4962 : out = weak_rhs_nuclide_id(ir)
246 4962 : Qx = chem_isos% mass_excess(in) - chem_isos% mass_excess(out)
247 :
248 4962 : if (use_suzuki_tables) then
249 : ! now, if there's a suzuki reaction, use that one instead
250 465 : call create_weak_dict_key(weak_lhs_nuclide_name(ir), weak_rhs_nuclide_name(ir), key)
251 465 : call integer_dict_lookup(suzuki_reactions_dict, key, rxn_idx, ierr)
252 465 : if (ierr /=0) then
253 : if (dbg) write(*,*) key, "is not a reaction included in the Suzuki tables"
254 394 : ierr = 0
255 : else
256 : if (dbg) write(*,*) key, "is a reaction included in the Suzuki tables"
257 71 : table => suzuki_reactions_tables(rxn_idx) %t
258 : call table % interpolate(T9, lYeRho, &
259 : s_lambda, s_dlambda_dlnT, s_dlambda_dlnRho, &
260 71 : s_Qneu, s_dQneu_dlnT, s_dQneu_dlnRho, ierr)
261 71 : if (ierr == 0) then
262 2 : lambda(i) = s_lambda
263 2 : dlambda_dlnT(i) = s_dlambda_dlnT
264 2 : dlambda_dlnRho(i) = s_dlambda_dlnRho
265 2 : Qneu(i) = s_Qneu
266 2 : dQneu_dlnT(i) = s_dQneu_dlnT
267 2 : dQneu_dlnRho(i) = s_dQneu_dlnRho
268 : !write(*,*) lYeRho, T9, s_lambda, s_Qneu
269 : end if
270 71 : ierr = 0
271 : end if
272 : end if
273 :
274 : ! neg is true for electron capture and positron emission
275 4962 : neg = ((chem_isos% Z(in) - chem_isos% Z(out)) == 1.0d0)
276 :
277 : ! in the past, these Q values used to include terms
278 : ! associated with the electron and ion chemical potentials
279 : ! these terms are now handled elsewhere, so Q is just the change in rest mass.
280 : ! since Qx is made from atomic mass excesses, it includes the electron rest mass.
281 :
282 4962 : if (neg) then ! electron capture and positron emission
283 4729 : Q(i) = Qx
284 4729 : dQ_dlnT(i) = 0
285 4729 : dQ_dlnRho(i) = 0
286 : else ! positron capture and electron emission
287 233 : Q(i) = Qx
288 233 : dQ_dlnT(i) = 0
289 233 : dQ_dlnRho(i) = 0
290 : end if
291 :
292 4962 : if (lambda(i) < 1d-30) then
293 134 : Qneu(i) = 0
294 134 : dQneu_dlnT(i) = 0
295 134 : dQneu_dlnRho(i) = 0
296 : end if
297 :
298 91255 : if (is_bad(Qneu(i))) then
299 0 : ierr = -1
300 0 : return
301 :
302 : write(*,2) 'lambda', i, lambda(i)
303 : write(*,2) 'Qneu', i, Qneu(i)
304 : call show_stuff
305 : end if
306 :
307 : end do
308 :
309 86293 : if (is_bad(lYeRho)) then
310 0 : ierr = -1
311 0 : return
312 : call show_stuff
313 : end if
314 :
315 :
316 : contains
317 :
318 0 : subroutine show_stuff
319 : integer :: i
320 : include 'formats'
321 0 : write(*,1) 'T9', T9
322 0 : write(*,1) 'lYeRho', lYeRho
323 0 : write(*,1) 'eta', eta
324 0 : write(*,1) 'mue', mue
325 0 : write(*,'(A)')
326 0 : do i = 1, n
327 0 : ir = ids(i)
328 0 : in = weak_lhs_nuclide_id(i)
329 0 : out = weak_rhs_nuclide_id(i)
330 0 : if (.true.) then
331 0 : write(*,4) 'ir, i, n', ir, i, n
332 0 : if (ir <= 0 .or. ir > ubound(weak_lhs_nuclide_id,dim=1)) cycle
333 0 : weak_lhs = chem_isos% name(weak_lhs_nuclide_id(ir))
334 0 : weak_rhs = chem_isos% name(weak_rhs_nuclide_id(ir))
335 0 : write(*,'(a30,3i5)') weak_lhs // weak_rhs, ir, i, n
336 : !write(*,1) 'chem_isos% mass_excess(in)', chem_isos% mass_excess(in)
337 : !write(*,1) 'chem_isos% mass_excess(out)', chem_isos% mass_excess(out)
338 0 : write(*,2) 'Qx', i, chem_isos% mass_excess(in) - chem_isos% mass_excess(out)
339 0 : write(*,2) 'Q', i, Q(i)
340 0 : write(*,2) 'Qneu', i, Qneu(i)
341 0 : write(*,'(A)')
342 : end if
343 : end do
344 0 : call mesa_error(__FILE__,__LINE__)
345 : end subroutine show_stuff
346 :
347 : end subroutine do_eval_weaklib_reaction_info
348 :
349 : end module eval_weak
|