Line data Source code
1 : ! ***********************************************************************
2 : !
3 : ! Copyright (C) 2012-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 solver_support
21 :
22 : use star_private_def
23 : use utils_lib, only: is_bad
24 : use const_def, only: dp, msun, secyer, ln10, four_thirds_pi
25 : use num_def
26 :
27 : implicit none
28 :
29 : private
30 : public :: force_another_iteration
31 : public :: eval_equations
32 : public :: set_xscale_info
33 : public :: sizequ
34 : public :: sizeB
35 : public :: inspectb
36 : public :: bdomain
37 :
38 : logical, parameter :: dbg = .false.
39 :
40 : contains
41 :
42 11 : subroutine set_xscale_info(s, nvar, ierr)
43 : type (star_info), pointer :: s
44 : integer, intent(in) :: nvar
45 : integer, intent(out) :: ierr
46 :
47 : integer :: i, k, nz, nvar_hydro
48 : real(dp), parameter :: xscale_min = 1
49 :
50 : include 'formats'
51 :
52 11 : ierr = 0
53 :
54 : if (dbg) write(*, *) 'set_xscale'
55 11 : nvar_hydro = s% nvar_hydro
56 11 : nz = s% nz
57 13098 : do k=1,nz
58 170142 : do i=1,nvar
59 170131 : if (i <= nvar_hydro) then ! structure variable
60 52348 : if (i == s% i_j_rot) then
61 0 : s% x_scale(i,k) = 10d0*sqrt(s% cgrav(k)*s% m(k)*s% r_start(k))
62 : else
63 52348 : s% x_scale(i,k) = max(xscale_min, abs(s% xh_start(i,k)))
64 : end if
65 : else ! abundance variable
66 104696 : s% x_scale(i,k) = max(s% xa_scale, s% xa_start(i-nvar_hydro,k))
67 : end if
68 : end do
69 : end do
70 :
71 : contains
72 :
73 : subroutine dump_xscale
74 : integer :: k, j
75 : include 'formats'
76 : !write(*,1) 's% xa_scale', s% xa_scale
77 : do k=1,s% nz
78 : do j=1,nvar
79 : write(*,2) 'xscale ' // trim(s% nameofvar(j)), k, s% x_scale(j,k)
80 : end do
81 : write(*,'(A)')
82 : end do
83 : call mesa_error(__FILE__,__LINE__,'set_xscale')
84 : end subroutine dump_xscale
85 :
86 : end subroutine set_xscale_info
87 :
88 :
89 42 : subroutine eval_equations(s, nvar, ierr)
90 : use hydro_eqns, only: eval_equ
91 : use mix_info, only: set_dxdt_mix
92 : use star_utils, only: update_time, total_times
93 : type (star_info), pointer :: s
94 : integer, intent(in) :: nvar
95 : integer, intent(out) :: ierr
96 :
97 : integer :: cnt, i, j, k, nz
98 : real(dp) :: dt
99 : include 'formats'
100 :
101 42 : ierr = 0
102 42 : nz = s% nz
103 :
104 : if (dbg) write(*, *) 'eval_equations'
105 :
106 42 : dt = s% dt
107 :
108 42 : if (s% solver_iter == 0) then
109 : if (dbg) write(*, *) 'skip set_solver_vars on call before 1st iter'
110 : else
111 : if (dbg) write(*, *) 'call set_solver_vars'
112 31 : call set_solver_vars(s, nvar, dt, ierr)
113 31 : if (ierr /= 0) then
114 0 : if (s% report_ierr) &
115 0 : write(*,2) 'eval_equations: set_solver_vars returned ierr', ierr
116 0 : return
117 : end if
118 : end if
119 :
120 42 : call set_dxdt_mix(s)
121 :
122 42 : if (ierr == 0) then
123 49710 : do k=1,nz
124 645726 : do j=1,nvar
125 596016 : s% equ(j,k) = 0d0
126 596016 : s% residual_weight(j,k) = 1d0
127 645684 : s% correction_weight(j,k) = 1d0
128 : end do
129 : end do
130 : if (dbg) write(*, *) 'call eval_equ'
131 42 : call eval_equ(s, nvar, ierr)
132 42 : if (ierr /= 0) then
133 0 : if (s% report_ierr) &
134 0 : write(*, *) 'eval_equations: eval_equ returned ierr', ierr
135 0 : return
136 : end if
137 : end if
138 :
139 42 : if (ierr /= 0) return
140 :
141 42 : if (.not. s% stop_for_bad_nums) return
142 :
143 42 : cnt = 0
144 49710 : do i=1,nz
145 645726 : do j=1, nvar
146 645684 : if (is_bad_num(s% equ(j, i))) then
147 0 : cnt = cnt + 1
148 0 : s% retry_message = 'eval_equations: equ has a bad num'
149 0 : if (s% report_ierr) then
150 0 : write(*,4) 'eval_equations: equ has a bad num ' // trim(s% nameofequ(j)), &
151 0 : j, i, nvar, s% equ(j, i)
152 0 : write(*,2) 'cell', i
153 0 : write(*,2) 'nz', s% nz
154 : end if
155 0 : if (s% stop_for_bad_nums) call mesa_error(__FILE__,__LINE__,'eval_equations')
156 : end if
157 : end do
158 : end do
159 :
160 42 : if (cnt > 0) then
161 0 : ierr = -1
162 0 : return
163 : end if
164 :
165 :
166 : contains
167 :
168 :
169 : subroutine dump_eval_equ
170 : integer :: k, j
171 : include 'formats'
172 : do k=1,s% nz
173 : do j=1,nvar
174 : write(*,2) 'dx ' // trim(s% nameofvar(j)), k, s% solver_dx(j, k)
175 : end do
176 : write(*,'(A)')
177 : end do
178 : call mesa_error(__FILE__,__LINE__,'dump_eval_equ')
179 : end subroutine dump_eval_equ
180 :
181 :
182 : end subroutine eval_equations
183 :
184 :
185 42 : subroutine sizequ(s, nvar, equ_norm, equ_max, k_max, j_max, ierr) ! equ = residuals
186 : type (star_info), pointer :: s
187 : integer, intent(in) :: nvar
188 : real(dp), intent(out) :: equ_norm, equ_max
189 : integer, intent(out) :: k_max, j_max, ierr
190 :
191 : integer :: j, k, num_terms, n, nz, nvar_hydro, skip_eqn1, skip_eqn2, skip_eqn3
192 : real(dp) :: sumequ, absq
193 :
194 : logical :: dbg
195 :
196 : include 'formats'
197 :
198 42 : ierr = 0
199 :
200 42 : equ_norm = 0d0
201 42 : equ_max = 0d0
202 42 : k_max = 0
203 42 : j_max = 0
204 :
205 42 : dbg = s% solver_check_everything
206 :
207 42 : nvar_hydro = min(nvar, s% nvar_hydro)
208 :
209 42 : nz = s% nz
210 42 : n = nz
211 42 : num_terms = 0
212 42 : sumequ = 0
213 42 : skip_eqn1 = 0
214 42 : skip_eqn2 = 0
215 42 : skip_eqn3 = 0
216 42 : if (s% convergence_ignore_equL_residuals) skip_eqn1 = s% i_equL
217 42 : if (s% convergence_ignore_alpha_RTI_residuals) skip_eqn2 = s% i_dalpha_RTI_dt
218 42 : if (s% do_burn .or. s% do_mix) then
219 : ! Count each equation once. The loop over all variables replaced the
220 : ! structural sum, so a separate composition pass would count those rows twice.
221 42 : num_terms = num_terms + nvar*nz
222 42 : if (skip_eqn1 > 0) num_terms = num_terms - nz
223 42 : if (skip_eqn2 > 0) num_terms = num_terms - nz
224 : if (skip_eqn3 > 0) num_terms = num_terms - nz
225 49710 : do k = 1, nz
226 645726 : do j = 1, nvar
227 596016 : if (j == skip_eqn1 .or. j == skip_eqn2 .or. j == skip_eqn3) cycle
228 596016 : if (is_bad(s% equ(j,k)) .or. is_bad(s% residual_weight(j,k))) then
229 0 : ierr = 1
230 0 : return
231 : end if
232 596016 : absq = abs(s% equ(j,k)*s% residual_weight(j,k))
233 596016 : sumequ = sumequ + absq
234 645684 : if (absq > equ_max) then
235 3414 : equ_max = absq
236 3414 : j_max = j
237 3414 : k_max = k
238 : end if
239 : end do
240 : end do
241 : else
242 0 : if (skip_eqn1 == 0 .and. skip_eqn2 == 0) then
243 0 : num_terms = num_terms + nvar_hydro*nz
244 0 : else if (skip_eqn1 > 0 .and. skip_eqn2 > 0) then
245 0 : num_terms = num_terms + (nvar_hydro-2)*nz
246 : else
247 0 : num_terms = num_terms + (nvar_hydro-1)*nz
248 : end if
249 0 : do k = 1, nz
250 0 : do j = 1, nvar_hydro
251 0 : if (j == skip_eqn1 .or. j == skip_eqn2) cycle
252 0 : absq = abs(s% equ(j,k)*s% residual_weight(j,k))
253 0 : sumequ = sumequ + absq
254 0 : if (is_bad(sumequ)) then
255 0 : if (dbg) then
256 0 : write(*,3) trim(s% nameofequ(j)) // ' sumequ', j, k, sumequ
257 0 : call mesa_error(__FILE__,__LINE__,'sizeq 1')
258 : end if
259 0 : ierr = -1
260 0 : if (s% report_ierr) &
261 0 : write(*,3) 'bad equ(j,k)*s% residual_weight(j,k) ' // trim(s% nameofequ(j)), &
262 0 : j, k, s% equ(j,k)*s% residual_weight(j,k)
263 0 : if (s% stop_for_bad_nums) call mesa_error(__FILE__,__LINE__,'sizeq 2')
264 : return
265 : end if
266 0 : if (absq > equ_max) then
267 0 : equ_max = absq
268 0 : j_max = j
269 0 : k_max = k
270 : end if
271 : end do
272 : end do
273 : end if
274 42 : equ_norm = sumequ/num_terms
275 42 : if (dbg) write(*,4) trim(s% nameofequ(j_max)) // ' sizequ equ_max norm', &
276 0 : k_max, s% solver_iter, s% model_number, equ_max, equ_norm
277 :
278 0 : if (dbg) call dump_equ
279 :
280 : return
281 : call dump_equ
282 : call mesa_error(__FILE__,__LINE__,'sizequ')
283 :
284 : contains
285 :
286 0 : subroutine dump_equ
287 : integer :: k, j
288 : include 'formats'
289 0 : do k=1,s% nz
290 0 : do j=1,nvar
291 0 : write(*,3) 'equ ' // trim(s% nameofequ(j)), &
292 0 : k, s% solver_iter, s% equ(j, k)
293 : end do
294 0 : write(*,'(A)')
295 : !if (k == 6) exit
296 : end do
297 0 : end subroutine dump_equ
298 :
299 : end subroutine sizequ
300 :
301 :
302 31 : subroutine sizeB(s, nvar, B, max_correction, correction_norm, max_zone, max_var, ierr)
303 : type (star_info), pointer :: s
304 : integer, intent(in) :: nvar
305 : real(dp), pointer, dimension(:,:) :: B ! (nvar, nz)
306 : real(dp), intent(out) :: correction_norm ! a measure of the average correction
307 : real(dp), intent(out) :: max_correction ! magnitude of the max correction
308 : integer, intent(out) :: max_zone, max_var, ierr
309 :
310 : integer :: k, i, nz, num_terms, j, n, nvar_hydro, jmax, num_xa_terms, &
311 : skip1, skip2, skip3, skip4, skip5
312 : real(dp) :: abs_corr, sum_corr, sum_xa_corr, x_limit, &
313 : max_abs_correction, max_abs_corr_for_k, max_abs_xa_corr_for_k
314 : logical :: found_NaN, found_bad_num, report
315 : real(dp), parameter :: frac = 0.1d0
316 : logical, parameter :: dbg = .false.
317 : logical, parameter :: check_for_bad_nums = .true.
318 : logical, parameter :: save_max_abs_corr_for_k = .true.
319 :
320 : include 'formats'
321 :
322 : if (dbg) write(*, *) 'enter sizeB'
323 :
324 31 : ierr = 0
325 31 : nz = s% nz
326 31 : n = nz
327 31 : nvar_hydro = min(nvar, s% nvar_hydro)
328 :
329 31 : if (s% include_L_in_correction_limits) then
330 31 : skip1 = 0
331 36612 : do k=1,nz
332 36612 : s% correction_weight(s% i_lum,k) = 1d0/(frac*s% L_start(1) + abs(s% L(k)))
333 : end do
334 : else
335 0 : skip1 = s% i_lum
336 : end if
337 :
338 31 : if (s% u_flag .and. s% include_u_in_correction_limits) then
339 0 : skip2 = 0
340 0 : do k=1,nz
341 0 : s% correction_weight(s% i_u,k) = 1d0/(frac*s% csound_start(k) + abs(s% u(k)))
342 : end do
343 31 : else if (s% v_flag .and. s% include_v_in_correction_limits) then
344 0 : skip2 = 0
345 0 : do k=1,nz
346 0 : s% correction_weight(s% i_v,k) = 1d0/(frac*s% csound_start(k) + abs(s% v(k)))
347 : end do
348 31 : else if (s% u_flag) then
349 0 : skip2 = s% i_u
350 : else
351 31 : skip2 = s% i_v
352 : end if
353 :
354 31 : if (s% RSP2_flag .and. s% include_w_in_correction_limits) then
355 0 : skip3 = 0
356 0 : do k=1,nz
357 0 : if (abs(s% w(k)) < 1d0) then
358 0 : s% correction_weight(s% i_w,k) = 0d0
359 : else
360 0 : s% correction_weight(s% i_w,k) = 1d0/(1d3 + abs(s% w(k))) ! don't sweat the small stuff
361 : end if
362 : end do
363 : else
364 31 : skip3 = s% i_w
365 : end if
366 31 : skip4 = s% i_Hp
367 :
368 31 : skip5 = 0
369 :
370 31 : max_zone = 0
371 31 : max_var = 0
372 31 : num_terms = 0
373 31 : sum_corr = 0
374 31 : max_correction = 0
375 31 : max_abs_correction = 0
376 31 : x_limit = s% correction_xa_limit
377 31 : found_NaN = .false.
378 31 : found_bad_num = .false.
379 31 : report = s% report_ierr
380 36612 : cell_loop: do k = 1, nz
381 : ! Average composition corrections within each cell.
382 36581 : num_xa_terms = 0
383 36581 : sum_xa_corr = 0
384 36581 : max_abs_corr_for_k = 0
385 36581 : max_abs_xa_corr_for_k = 0
386 :
387 36581 : if (s% do_burn .or. s% do_mix) then
388 : jmax = nvar
389 : else
390 36581 : jmax = nvar_hydro
391 : end if
392 475553 : var_loop: do j = 1, jmax
393 : if (j == skip1 .or. &
394 : j == skip2 .or. &
395 : j == skip3 .or. &
396 : j == skip4 .or. &
397 438972 : j == skip5 .or. &
398 : j == s% i_alpha_RTI) cycle var_loop
399 : if (check_for_bad_nums) then
400 438972 : if (is_bad_num(B(j,k)*s% correction_weight(j,k))) then
401 0 : found_bad_num = .true.
402 0 : if (report) write(*,2) 'sizeB: bad num for correction ' // &
403 0 : s% nameofvar(j), k, B(j,k)*s% correction_weight(j,k)
404 0 : if (s% stop_for_bad_nums) then
405 0 : found_NaN = .true.
406 0 : write(*,3) s% nameofvar(j) // ' B(j,k)*s% correction_weight(j,k)', &
407 0 : j, k, B(j,k)*s% correction_weight(j,k)
408 0 : call mesa_error(__FILE__,__LINE__,'sizeB')
409 : end if
410 :
411 0 : max_zone = k
412 0 : max_var = j
413 0 : exit cell_loop
414 :
415 : cycle var_loop
416 : end if
417 : end if
418 438972 : if (j > nvar_hydro) then
419 292648 : if (s% xa_start(j-nvar_hydro,k) < x_limit) cycle var_loop
420 : end if
421 :
422 260989 : abs_corr = abs(B(j,k)*s% correction_weight(j,k))
423 260989 : if (is_bad_num(abs_corr)) then
424 0 : found_bad_num = .true.
425 0 : if (report) write(*,3) 'B(j,k)*s% correction_weight(j,k)', &
426 0 : j, k, B(j,k)*s% correction_weight(j,k)
427 0 : if (s% stop_for_bad_nums) found_NaN = .true.
428 : end if
429 : if (abs_corr > max_abs_corr_for_k &
430 : .and. .not. (j > nvar_hydro .and. s% ignore_species_in_max_correction)) &
431 : max_abs_corr_for_k = abs_corr
432 : if (abs_corr > max_abs_correction &
433 260989 : .and. .not. (j > nvar_hydro .and. s% ignore_species_in_max_correction)) then
434 11440 : max_correction = B(j,k)*s% correction_weight(j,k)
435 11440 : max_abs_correction = abs_corr
436 11440 : max_zone = k
437 11440 : max_var = j
438 : end if
439 297570 : if (j > nvar_hydro) then
440 114665 : num_xa_terms = num_xa_terms + 1
441 114665 : sum_xa_corr = sum_xa_corr + abs_corr
442 : if (abs_corr > max_abs_xa_corr_for_k) &
443 : max_abs_xa_corr_for_k = abs_corr
444 : else
445 146324 : num_terms = num_terms + 1
446 146324 : sum_corr = sum_corr + abs_corr
447 : end if
448 : end do var_loop
449 36581 : if (num_xa_terms > 0) then
450 36581 : num_terms = num_terms + 1
451 36581 : sum_corr = sum_corr + sum_xa_corr/num_xa_terms
452 : end if
453 36581 : if (s% do_burn .or. s% do_mix) then
454 329229 : species_loop: do j = nvar_hydro+1, nvar
455 292648 : i = j - s% nvar_hydro
456 : if (check_for_bad_nums) then
457 292648 : if (is_bad_num(B(j,k)*s% correction_weight(j,k))) then
458 0 : found_bad_num = .true.
459 0 : if (report) write(*,3) 'chem B(j,k)*s% correction_weight(j,k)', j, k, &
460 0 : B(j,k)*s% correction_weight(j,k)
461 0 : if (s% stop_for_bad_nums) then
462 0 : found_NaN = .true.
463 0 : write(*,3) 'chem B(j,k)*s% correction_weight(j,k)', &
464 0 : j, k, B(j,k)*s% correction_weight(j,k)
465 0 : call mesa_error(__FILE__,__LINE__,'sizeB')
466 : max_zone = k
467 : max_var = j
468 : exit cell_loop
469 : end if
470 : end if
471 : end if
472 : ! recall that correction dx = B*xscale, so B is a relative correction
473 329229 : if (s% xa_start(i,k) >= x_limit) then
474 114665 : abs_corr = abs(B(j,k)*s% correction_weight(j,k))
475 114665 : if (.not. s% ignore_species_in_max_correction) then
476 : if (abs_corr > max_abs_corr_for_k) max_abs_corr_for_k = abs_corr
477 114665 : if (abs_corr > max_abs_correction) then
478 0 : max_abs_correction = abs_corr
479 0 : max_correction = B(j,k)*s% correction_weight(j,k)
480 0 : max_zone = k
481 0 : max_var = j
482 : end if
483 : end if
484 114665 : sum_corr = sum_corr + abs_corr
485 114665 : num_terms = num_terms + 1
486 : end if
487 : end do species_loop
488 : end if
489 36612 : s% max_abs_xa_corr(k) = max_abs_xa_corr_for_k
490 : end do cell_loop
491 :
492 31 : if (found_bad_num) then
493 0 : ierr = -1
494 0 : if (found_NaN .and. s% stop_for_bad_nums) then
495 0 : write(*,*) 'found bad num'
496 0 : call mesa_error(__FILE__,__LINE__,'sizeB')
497 : end if
498 22 : if (.not. dbg) return
499 : end if
500 :
501 31 : if (is_bad_num(sum_corr)) then
502 0 : ierr = -1
503 0 : if (s% stop_for_bad_nums) then
504 0 : if (report) write(*,*) 'sum_corr', sum_corr
505 0 : call mesa_error(__FILE__,__LINE__,'sizeB')
506 : end if
507 : if (.not. dbg) return
508 : write(*,*) 'sum_corr', sum_corr
509 : call mesa_error(__FILE__,__LINE__,'sizeB')
510 : end if
511 :
512 31 : correction_norm = sum_corr/num_terms !sqrt(sum_corr/num_terms)
513 : if (dbg) then
514 : write(*,2) 'sizeB: iter, correction_norm, max_correction', &
515 : s% solver_iter, correction_norm, max_correction
516 : if (max_correction > 1d50 .or. is_bad_num(correction_norm)) then
517 : call show_stuff
518 : call mesa_error(__FILE__,__LINE__,'sizeB')
519 : end if
520 : end if
521 :
522 31 : if (s% solver_show_correction_info) call show_stuff
523 :
524 : abs_corr = max_abs_correction
525 :
526 31 : s% abs_max_corr2 = s% abs_max_corr1; s% abs_max_corr1 = abs_corr
527 31 : s% max_var2 = s% max_var1; s% max_var1 = max_var
528 31 : s% max_zone2 = s% max_zone1; s% max_zone1 = max_zone
529 :
530 31 : if (ierr /= 0) call mesa_error(__FILE__,__LINE__,'ierr in sizeB')
531 :
532 31 : if (is_bad_num(max_correction)) then
533 0 : ierr = -1
534 0 : if (s% stop_for_bad_nums) then
535 0 : if (report) write(*,*) 'max_correction', max_correction
536 0 : call mesa_error(__FILE__,__LINE__,'sizeB')
537 : end if
538 : if (.not. dbg) return
539 : write(*,*) 'max_correction', max_correction
540 : call mesa_error(__FILE__,__LINE__,'sizeB')
541 : end if
542 :
543 31 : if (s% solver_iter < 3) return
544 : ! check for flailing
545 : if ( &
546 : abs_corr > s% tol_max_correction .and. &
547 : abs_corr > s% abs_max_corr1 .and. s% abs_max_corr1 > s% abs_max_corr2 .and. &
548 : max_zone == s% max_zone1 .and. s% max_zone1 == s% max_zone2 .and. &
549 9 : max_var == s% max_var1 .and. s% max_var1 == s% max_var2) then
550 0 : if (s% solver_show_correction_info) then
551 0 : write(*,*) 'give up because diverging'
552 : end if
553 0 : max_correction = 1d99
554 : end if
555 :
556 :
557 : contains
558 :
559 :
560 0 : subroutine show_stuff
561 : integer :: j, k
562 : real(dp) :: dx, prev, new
563 : include 'formats'
564 0 : if (s% solver_iter == 1) then
565 0 : write(*,'(A)')
566 : write(*,'(4a7,12a16,99a13)') &
567 0 : 'model', 'iter', 'var', 'zone', &
568 0 : 'corr norm', 'max corr', 'xscale', &
569 0 : 'dx', 'new-prev', 'new', 'prev', &
570 0 : 'mass loc', 'log dt/yr', 'lgE', 'lgT', 'lgRho'
571 : end if
572 0 : k = max_zone
573 0 : j = max_var
574 0 : if (j > nvar_hydro) then
575 0 : prev = s% xa_start(j - nvar_hydro,k)
576 : else
577 0 : prev = s% xh_start(j,k)
578 : end if
579 0 : dx = B(j,k)*s% correction_weight(j,k)*s% x_scale(j,k)
580 0 : new = prev + dx
581 : write(*,'(2i7,a7,i7,12e16.8,99f13.8)') &
582 0 : s% model_number, s% solver_iter, trim(s% nameofvar(max_var)), k, &
583 0 : correction_norm, B(j,k)*s% correction_weight(j,k), s% x_scale(j,k), &
584 0 : dx, new - prev, new, prev, &
585 0 : s% m(k)/Msun, log10(s% dt/secyer), &
586 0 : s% lnE(k)/ln10, s% lnT(k)/ln10, &
587 0 : s% lnd(k)/ln10
588 0 : end subroutine show_stuff
589 :
590 :
591 : subroutine dump_B
592 : integer :: k, j
593 : include 'formats'
594 : do k=1,s% nz
595 : do j=1,nvar
596 : write(*,2) 'B ' // trim(s% nameofequ(j)), k, B(j, k)
597 : end do
598 : write(*,'(A)')
599 : end do
600 : call mesa_error(__FILE__,__LINE__,'dump_equ')
601 : end subroutine dump_B
602 :
603 :
604 : end subroutine sizeB
605 :
606 :
607 : ! the proposed change to dx is B*xscale*correction_factor
608 : ! edit correction_factor and/or B as necessary so that the new dx will be valid.
609 : ! set ierr nonzero if things are beyond repair.
610 31 : subroutine Bdomain(s, nvar, B, correction_factor, ierr)
611 : use const_def, only: dp
612 : use chem_def, only: chem_isos
613 : use star_utils, only: current_min_xa_hard_limit, rand
614 : type (star_info), pointer :: s
615 : integer, intent(in) :: nvar
616 : real(dp), pointer, dimension(:,:) :: B ! (nvar, nz)
617 : real(dp), intent(inout) :: correction_factor
618 : integer, intent(out) :: ierr
619 : integer :: i, j, k, nz, species, bad_j, bad_k
620 : real(dp) :: alpha, min_alpha, new_xa, old_xa, dxa, eps, min_xa_hard_limit, &
621 : dlum_surf, old_lum_surf, new_lum_surf
622 : include 'formats'
623 31 : ierr = 0
624 31 : min_alpha = 1d0
625 31 : nz = s% nz
626 :
627 31 : if (s% RSP2_flag) & ! clip change in w to maintain non-negativity.
628 0 : call clip_so_non_negative(s% i_w, 0d0)
629 :
630 31 : if (s% RTI_flag) & ! clip change in alpha_RTI to maintain non-negativity.
631 0 : call clip_so_non_negative(s% i_alpha_RTI, 0d0)
632 :
633 31 : if (s% i_lum>=0 .and. s% scale_max_correction_for_negative_surf_lum) then
634 : !ensure surface luminosity does not become negative
635 0 : dlum_surf = B(s% i_lum,1)*s% x_scale(s% i_lum,1)
636 0 : old_lum_surf = s% xh_start(s% i_lum,1) + s% solver_dx(s% i_lum,1)
637 0 : new_lum_surf = old_lum_surf + dlum_surf
638 0 : if (new_lum_surf < 0d0 .and. old_lum_surf > 0d0) then
639 : correction_factor = min(correction_factor, &
640 0 : -s% max_frac_for_negative_surf_lum*old_lum_surf/dlum_surf)
641 : end if
642 : end if
643 :
644 : !if (s% w_div_wc_flag) then
645 : ! do k=1,nz
646 : ! old_xa = s% xh_start(s% i_w_div_wc,k) + dx(s% i_w_div_wc,k)
647 : ! dxa = B(s% i_w_div_wc,k)*xscale(s% i_w_div_wc,k)*correction_factor
648 : ! if (dxa > 0.9d0*(1-old_xa)) then
649 : ! if(k==91)write(*,*) "problems", old_xa, dxa, old_xa+dxa
650 : ! correction_factor = min(correction_factor,(0.9d0*(1-old_xa))/B(s% i_w_div_wc,k)*xscale(s%i_w_div_wc,k))
651 : ! dxa = B(s% i_w_div_wc,k)*xscale(s% i_w_div_wc,k)*correction_factor
652 : ! if(k==91)write(*,*) "need to reduce correction", k, old_xa+dxa
653 : ! end if
654 : ! end do
655 : !end if
656 :
657 31 : if ((.not. s% do_solver_damping_for_neg_xa) .or. &
658 : (.not. (s% do_mix .or. s% do_burn))) then
659 : if (min_alpha < 1d0) then
660 : min_alpha = max(min_alpha, s% corr_coeff_limit)
661 : correction_factor = min_alpha*correction_factor
662 : end if
663 0 : return
664 : end if
665 :
666 31 : bad_j = 0
667 31 : bad_k = 0
668 31 : if (nvar > s% nvar_hydro) then
669 31 : species = s% species
670 31 : min_xa_hard_limit = current_min_xa_hard_limit(s)
671 31 : eps = -0.5d0*min_xa_hard_limit ! allow xa to be this much below 0d0
672 36612 : do k=1,nz
673 329260 : do j=1,species
674 292648 : i = j + s% nvar_hydro
675 292648 : old_xa = s% xa_start(j,k) + s% solver_dx(i,k)
676 292648 : if (old_xa <= 1d-90) cycle
677 292648 : dxa = B(i,k)*s% x_scale(i,k)*correction_factor
678 292648 : new_xa = old_xa + dxa
679 292648 : if (new_xa >= 0d0) cycle
680 0 : alpha = -(old_xa + eps)/dxa
681 : ! so dxa*alpha = -old_xa - eps,
682 : ! and therefore old_xa + alpha*dxa = -eps = 0.5*min_xa_hard_limit
683 36581 : if (alpha < min_alpha) then
684 0 : min_alpha = alpha
685 0 : bad_j = j
686 0 : bad_k = k
687 : end if
688 : end do
689 : end do
690 : end if
691 :
692 31 : min_alpha = max(min_alpha, s% corr_coeff_limit)
693 31 : correction_factor = min_alpha*correction_factor
694 31 : if (s% trace_solver_damping .and. min_alpha < 1d0 .and. bad_j > 0) then
695 : write(*,4) 'solver damping to avoid negative mass fractions: ' // &
696 0 : trim(chem_isos% name(s% chem_id(bad_j))), bad_k, &
697 0 : s% model_number, s% solver_iter, min_alpha
698 : end if
699 :
700 : contains
701 :
702 0 : subroutine clip_so_non_negative(i,minval)
703 : integer, intent(in) :: i
704 : real(dp), intent(in) :: minval
705 : real(dp) :: dval, old_val, new_val
706 0 : do k = 1, s% nz
707 0 : dval = B(i,k)*s% x_scale(i,k)*correction_factor
708 0 : old_val = s% xh_start(i,k) + s% solver_dx(i,k)
709 0 : new_val = old_val + dval
710 0 : if (dval >= 0) cycle
711 0 : if (new_val >= 0d0) cycle
712 0 : dval = minval - old_val
713 0 : B(i,k) = dval/(s% x_scale(i,k)*correction_factor)
714 : end do
715 0 : end subroutine clip_so_non_negative
716 :
717 : end subroutine Bdomain
718 :
719 :
720 31 : subroutine inspectB(s, nvar, B, ierr)
721 : type (star_info), pointer :: s
722 : integer, intent(in) :: nvar
723 : real(dp), pointer, dimension(:,:) :: B ! (nvar, nz)
724 : integer, intent(out) :: ierr
725 :
726 : integer, parameter :: inspectB_iter_stop = -1
727 : include 'formats'
728 :
729 : if (dbg) write(*, *) 'inspectB', s% solver_iter
730 31 : ierr = 0
731 31 : if (s% solver_iter == inspectB_iter_stop) then
732 0 : call dumpB
733 : call mesa_error(__FILE__,__LINE__,'debug: inspectB')
734 : end if
735 :
736 : contains
737 :
738 0 : subroutine dumpB
739 : integer :: k, j
740 : include 'formats'
741 0 : do k=1,s% nz
742 0 : do j=1,nvar
743 0 : write(*,2) 'B ' // trim(s% nameofvar(j)), k, B(j, k)
744 0 : write(*,2) 'xscale ' // trim(s% nameofvar(j)), k, s% x_scale(j, k)
745 0 : write(*,2) 'dx ' // trim(s% nameofvar(j)), k, s% solver_dx(j, k)
746 : end do
747 0 : write(*,'(A)')
748 : end do
749 0 : call mesa_error(__FILE__,__LINE__,'dumpB')
750 : end subroutine dumpB
751 :
752 : end subroutine inspectB
753 :
754 :
755 : ! about to declare victory... but may want to do another iteration
756 : ! 1 means force another iteration
757 : ! 0 means don't need to force another
758 : ! -1 means failure. solver returns with non-convergence.
759 11 : integer function force_another_iteration(s, iter, itermin)
760 : type (star_info), pointer :: s
761 : integer, intent(in) :: iter ! have finished this many iterations and have converged
762 : integer, intent(in) :: itermin ! this is the requested minimum. iter may be < itermin.
763 :
764 : include 'formats'
765 :
766 11 : if (iter < itermin) then
767 0 : force_another_iteration = 1
768 : return
769 : end if
770 11 : force_another_iteration = 0
771 :
772 : if (s% k_below_just_added > 1 .and. &
773 11 : s% num_surf_revisions < s% max_num_surf_revisions .and. &
774 : abs(s% lnS(1) - s% surf_lnS) > &
775 : s% max_abs_rel_change_surf_lnS*max(s% lnS(1),s% surf_lnS)) then
776 0 : s% surf_lnS = s% lnS(1)
777 0 : s% num_surf_revisions = s% num_surf_revisions + 1
778 0 : force_another_iteration = 1
779 0 : s% used_extra_iter_in_solver_for_accretion = .true.
780 0 : return
781 : end if
782 :
783 : end function force_another_iteration
784 :
785 :
786 31 : subroutine set_solver_vars(s, nvar, dt, ierr)
787 : type (star_info), pointer :: s
788 : integer, intent(in) :: nvar
789 : real(dp), intent(in) :: dt
790 : integer, intent(out) :: ierr
791 31 : s% num_solver_setvars = s% num_solver_setvars + 1
792 31 : call set_vars_for_solver(s, nvar, 1, s% nz, dt, ierr)
793 31 : end subroutine set_solver_vars
794 :
795 :
796 31 : subroutine set_vars_for_solver(s, nvar, nzlo, nzhi, dt, ierr)
797 : use const_def, only: secyer, Msun, Lsun, Rsun
798 : use star_utils, only: set_rmid, set_dm_bar, set_m_and_dm, set_rv_info
799 : use star_utils, only: current_min_xa_hard_limit, current_sum_xa_hard_limit, &
800 : lookup_nameofvar
801 : use hydro_vars, only: set_hydro_vars
802 : use hydro_rotation, only: set_i_rot, set_omega
803 : use mix_info, only: get_convection_sigmas
804 : use chem_def
805 : type (star_info), pointer :: s
806 : integer, intent(in) :: nvar, nzlo, nzhi
807 : real(dp), intent(in) :: dt
808 : integer, intent(out) :: ierr
809 :
810 : logical, parameter :: &
811 : skip_basic_vars = .true., &
812 : skip_micro_vars = .false., &
813 : skip_kap = .false., &
814 : skip_neu = .false., &
815 : skip_net = .false., &
816 : skip_eos = .false., &
817 : skip_mlt = .false., &
818 : skip_grads = .true., &
819 : skip_rotation = .true., &
820 : skip_m_grav_and_grav = .true., &
821 : skip_brunt = .true., &
822 : skip_mixing_info = .true., &
823 : skip_set_cz_bdy_mass = .true., &
824 : skip_other_cgrav = .true.
825 : logical :: do_chem, try_again, do_edit_lnR, report_dx
826 : integer :: j, k, kk, klo, khi, i_var, &
827 : i_lnd, i_lnT, i_lnR, i_lum, i_w, i_Hp, i_v, &
828 : i_u, i_alpha_RTI, i_w_div_wc, i_j_rot, &
829 : fe56, nvar_chem, species, nz, nvar_hydro
830 31 : real(dp), dimension(:, :), pointer :: xh_start, xa_start
831 : integer :: op_err, kbad, &
832 : cnt, max_fixes, loc(2), k_lo, k_hi
833 : real(dp) :: xavg, dx_for_i_var, x_for_i_var, &
834 : dq_sum, d_dxdt_dx, min_xa_hard_limit, sum_xa_hard_limit
835 : logical :: do_lnd, do_lnT, do_lnR, do_lum, do_w, &
836 : do_u, do_v, do_alpha_RTI, do_w_div_wc, do_j_rot
837 :
838 : include 'formats'
839 :
840 31 : ierr = 0
841 :
842 31 : nz = s% nz
843 31 : nvar_chem = s% nvar_chem
844 31 : species = s% species
845 31 : nvar_hydro = s% nvar_hydro
846 31 : d_dxdt_dx = 1d0/s% dt
847 :
848 31 : xh_start => s% xh_start
849 31 : xa_start => s% xa_start
850 :
851 : report_dx = &
852 : s% solver_test_partials_dx_0 > 0d0 .and. &
853 : s% solver_test_partials_k > 0 .and. &
854 : s% solver_call_number == s% solver_test_partials_call_number .and. &
855 : s% solver_test_partials_iter_number == s% solver_iter .and. &
856 0 : len_trim(s% solver_test_partials_show_dx_var_name) > 0
857 :
858 : if (report_dx) then
859 0 : k = s% solver_test_partials_k
860 0 : i_var = lookup_nameofvar(s, s% solver_test_partials_show_dx_var_name)
861 0 : if (i_var > 0) then
862 0 : if (i_var > nvar_hydro) then
863 0 : dx_for_i_var = s% solver_dx(i_var,k)
864 0 : x_for_i_var = xa_start(i_var-nvar_hydro,k) + s% solver_dx(i_var,k)
865 : else
866 0 : dx_for_i_var = s% solver_dx(i_var,k)
867 0 : x_for_i_var = xh_start(i_var,k) + s% solver_dx(i_var,k)
868 : end if
869 : write(*,3) 'dx, x for var name ' // &
870 0 : trim(s% solver_test_partials_show_dx_var_name), &
871 0 : k, s% solver_iter, dx_for_i_var, x_for_i_var
872 : end if
873 : end if
874 :
875 31 : do_chem = (s% do_burn .or. s% do_mix)
876 :
877 : if (dbg .and. .not. skip_mixing_info) write(*,2) 'redo mix info iter', s% solver_iter
878 :
879 31 : min_xa_hard_limit = current_min_xa_hard_limit(s)
880 31 : sum_xa_hard_limit = current_sum_xa_hard_limit(s)
881 :
882 31 : i_lnd = s% i_lnd
883 31 : i_lnT = s% i_lnT
884 31 : i_lnR = s% i_lnR
885 31 : i_lum = s% i_lum
886 31 : i_w = s% i_w
887 31 : i_Hp = s% i_Hp
888 31 : i_v = s% i_v
889 31 : i_u = s% i_u
890 31 : i_alpha_RTI = s% i_alpha_RTI
891 31 : i_w_div_wc = s% i_w_div_wc
892 31 : i_j_rot = s% i_j_rot
893 :
894 31 : do_lnd = i_lnd > 0 .and. i_lnd <= nvar
895 31 : do_lnT = i_lnT > 0 .and. i_lnT <= nvar
896 31 : do_lnR = i_lnR > 0 .and. i_lnR <= nvar
897 31 : do_lum = i_lum > 0 .and. i_lum <= nvar
898 31 : do_w = i_w > 0 .and. i_w <= nvar
899 31 : do_v = i_v > 0 .and. i_v <= nvar
900 31 : do_u = i_u > 0 .and. i_u <= nvar
901 31 : do_alpha_RTI = i_alpha_RTI > 0 .and. i_alpha_RTI <= nvar
902 31 : do_w_div_wc = i_w_div_wc > 0 .and. i_w_div_wc <= nvar
903 31 : do_j_rot = i_j_rot > 0 .and. i_j_rot <= nvar
904 :
905 31 : fe56 = s% net_iso(ife56)
906 : if (fe56 /= 0) fe56 = nvar_hydro+fe56
907 :
908 31 : if (nvar > nvar_hydro) then
909 36612 : do k=1,nz
910 329260 : do j=1,species
911 292648 : s% xa_sub_xa_start(j,k) = s% solver_dx(j+nvar_hydro,k)
912 329229 : s% xa(j,k) = xa_start(j,k) + s% solver_dx(j+nvar_hydro,k)
913 : end do
914 : end do
915 : max_fixes = 0 !5
916 : do cnt=1,max_fixes
917 : loc = minloc(s% xa(1:species,1:nz))
918 : j = loc(1)
919 : k = loc(2)
920 : if (s% xa(j,k) >= 1d-3*min_xa_hard_limit) exit ! too good to fix
921 : if (s% xa(j,k) < min_xa_hard_limit) then
922 : if (s% report_ierr) then
923 : khi = nz
924 : do kk=k+1,nz
925 : if (s% xa(j,kk) < min_xa_hard_limit) cycle
926 : khi = kk-1; exit
927 : end do
928 : klo = 1
929 : do kk=k-1,1,-1
930 : if (s% xa(j,kk) < min_xa_hard_limit) cycle
931 : klo = kk+1; exit
932 : end do
933 : do k=klo,khi
934 : write(*,2) &
935 : 'negative ' // trim(chem_isos% name(s% chem_id(j))), &
936 : k, s% xa(j,k), xa_start(j,k), s% solver_dx(nvar_hydro+j,k), s% m(k)/Msun
937 : end do
938 : end if
939 : s% retry_message = 'some abundance < min_xa_hard_limit'
940 : ierr = -1
941 0 : return
942 : exit ! too bad to fix
943 : end if
944 : if (k == 1) then
945 : k_lo = 1; k_hi = 2
946 : else if (k == nz) then
947 : k_lo = nz-1; k_hi = nz
948 : else if (s% sig(k) > s% sig(k+1)) then
949 : k_lo = k-1; k_hi = k
950 : else if (s% sig(k+1) > 0) then
951 : k_lo = k; k_hi = k+1
952 : else
953 : exit
954 : end if
955 : try_again = .true.
956 : do while (try_again .and. sum(s% xa(j,k_lo:k_hi)*s% dq(k_lo:k_hi)) < 0)
957 : try_again = .false.
958 : if (k_lo > 1) then
959 : if (s% sig(k_lo) > 0) then
960 : k_lo = k_lo-1
961 : try_again = .true.
962 : end if
963 : end if
964 : if (k_hi < nz) then
965 : if (s% sig(k_hi+1) > 0) then
966 : k_hi = k_hi+1
967 : try_again = .true.
968 : end if
969 : end if
970 : end do
971 : !write(*,3) 'no extend', k_lo, k_hi
972 : if (.not. try_again) exit
973 : dq_sum = sum(s% dq(k_lo:k_hi))
974 : if (s% report_ierr) then
975 : write(*,5) 'fix xa(j,k_lo:k_hi)', j, k_lo, k, k_hi, s% xa(j,k), &
976 : sum(s% xa(j,k_lo:k_hi)*s% dq(k_lo:k_hi))/dq_sum, dq_sum
977 : !stop
978 : end if
979 : do j=1,species
980 : xavg = sum(s% xa(j,k_lo:k_hi)*s% dq(k_lo:k_hi))/dq_sum
981 : do kk=k_lo,k_hi
982 : s% xa(j,kk) = xavg
983 : end do
984 : end do
985 : end do
986 : end if
987 :
988 31 : if (s% solver_test_partials_k > 0 .and. &
989 : s% solver_test_partials_k <= nz) then
990 0 : k = s% solver_test_partials_k
991 : end if
992 :
993 31 : kbad = 0
994 31 : !$OMP PARALLEL DO PRIVATE(k,op_err) SCHEDULE(dynamic,2)
995 : do k=1,nz
996 : op_err = 0
997 : call set1(k,.false.,op_err)
998 : if (op_err /= 0) then
999 : kbad = k; ierr = op_err
1000 : end if
1001 : end do
1002 : !$OMP END PARALLEL DO
1003 :
1004 31 : if (ierr /= 0) then
1005 0 : if (s% report_ierr) then
1006 0 : do k=1,nz ! report the errors sequentially
1007 0 : call set1(k,.true.,op_err)
1008 : end do
1009 0 : write(*,3) 'set_vars_for_solver failed: model, nz', &
1010 0 : s% model_number, nz ! kbad depends on num threads
1011 : end if
1012 0 : return
1013 : end if
1014 :
1015 31 : if (s% solver_test_partials_k > 0 .and. &
1016 : s% solver_test_partials_k <= nz) then
1017 0 : k = s% solver_test_partials_k
1018 : end if
1019 :
1020 31 : if (do_lum) then
1021 36612 : do k=1,nz
1022 36612 : if (is_bad_num(s% L(k))) then
1023 0 : if (s% report_ierr) write(*,2) 'set_vars_for_solver L', k, s% L(k), &
1024 0 : xh_start(i_lum,k) + s% solver_dx(i_lum,k), &
1025 0 : xh_start(i_lum,k), s% solver_dx(i_lum,k)
1026 0 : s% retry_message = 'bad num for some L'
1027 0 : ierr = -1
1028 0 : if (s% stop_for_bad_nums) then
1029 0 : write(*,2) 'set_vars_for_solver L', k, s% L(k)
1030 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1031 : end if
1032 : return
1033 : stop
1034 : end if
1035 : end do
1036 : end if
1037 :
1038 31 : if (ierr /= 0) then
1039 0 : if (s% report_ierr) then
1040 :
1041 0 : do k=1,nz
1042 0 : if (abs(1d0 - sum(s% xa(:,k))) > 1d-3) then
1043 0 : write(*,2) 'set_vars_for_solver: bad xa sum', k, &
1044 0 : sum(s% xa(:,k)), sum(xa_start(:,k)), sum(s% solver_dx(nvar_hydro+1:nvar,k))
1045 0 : write(*,'(51x,a)') 'xa, xa_start+dx, xa_start, dx'
1046 0 : do j=1,species
1047 0 : write(*,2) trim(chem_isos% name(s% chem_id(j))), k, &
1048 0 : s% xa(j,k), xa_start(j,k) + s% solver_dx(nvar_hydro+j,k), &
1049 0 : xa_start(j,k), s% solver_dx(nvar_hydro+j,k)
1050 : end do
1051 :
1052 : exit
1053 :
1054 : end if
1055 : end do
1056 0 : write(*,'(A)')
1057 :
1058 : end if
1059 0 : return
1060 : end if
1061 :
1062 31 : do_edit_lnR = do_lnR .and. (.not. s% doing_check_partials)
1063 31 : if (do_edit_lnR) call edit_lnR(s, xh_start, s% solver_dx)
1064 36612 : do k=1,nz
1065 36581 : if (do_edit_lnR) s% r(k) = exp(s% lnR(k))
1066 36581 : call set_rv_info(s,k)
1067 : ! note: m_grav is held constant during solver iterations
1068 36612 : s% grav(k) = s% cgrav(k)*s% m_grav(k)/(s% r(k)*s% r(k))
1069 : end do
1070 :
1071 31 : if (do_lnR) then
1072 31 : call set_rmid(s, 1, nz, ierr)
1073 31 : if (ierr /= 0) then
1074 0 : if (s% report_ierr) write(*, *) 'set_rmid returned ierr', ierr
1075 0 : return
1076 : end if
1077 : end if
1078 :
1079 : if (dbg) write(*, *) 'call set_hydro_vars'
1080 : call set_hydro_vars( &
1081 : s, 1, nz, skip_basic_vars, skip_micro_vars, &
1082 : skip_m_grav_and_grav, skip_eos, skip_net, skip_neu, skip_kap, &
1083 : skip_grads, skip_rotation, skip_brunt, skip_other_cgrav, &
1084 31 : skip_mixing_info, skip_set_cz_bdy_mass, skip_mlt, ierr)
1085 31 : if (ierr /= 0) then
1086 0 : if (s% report_ierr) write(*,2) 'set_hydro_vars returned ierr', ierr
1087 0 : return
1088 : end if
1089 :
1090 62 : if (s% rotation_flag) then
1091 : ! Moments of inertia are kept fixed as those at the start of the hydro solver
1092 : ! neither omege nor irot enter the equations directly
1093 : ! call set_i_rot(s)
1094 0 : call set_omega(s, 'hydro_mtx')
1095 : end if
1096 :
1097 :
1098 : contains
1099 :
1100 :
1101 36581 : subroutine set1(k,report,ierr)
1102 : !use chem_def, only: chem_isos
1103 : integer, intent(in) :: k
1104 : logical, intent(in) :: report
1105 : integer, intent(out) :: ierr
1106 :
1107 0 : real(dp) :: x(nvar)
1108 : ! setting x = xh_start + dx is necessary because of numerical issues.
1109 : ! we want to ensure that we've calculated the variables using exactly the
1110 : ! same values for x as will be returned as the final result.
1111 : integer :: j, k_below_just_added
1112 : real(dp) :: v
1113 :
1114 : include 'formats'
1115 36581 : ierr = 0
1116 36581 : v = 0
1117 :
1118 36581 : k_below_just_added = 1
1119 :
1120 182905 : do j=1,min(nvar, nvar_hydro)
1121 182905 : x(j) = xh_start(j,k) + s% solver_dx(j,k)
1122 : !write(*,2) 'new ' // s% nameofvar(j), k, x(j)
1123 : end do
1124 :
1125 36581 : if (do_lnT) then
1126 :
1127 36581 : s% lnT(k) = x(i_lnT)
1128 36581 : s% dxh_lnT(k) = s% solver_dx(i_lnT,k)
1129 : if (abs(s% lnT(k) - s% lnT_start(k)) > &
1130 36581 : ln10*s% hydro_mtx_max_allowed_abs_dlogT .and. &
1131 : s% min_logT_for_hydro_mtx_max_allowed < &
1132 : ln10*min(s% lnT(k),s% lnT_start(k))) then
1133 0 : if (report) &
1134 0 : write(*,4) 'hydro_mtx: change too large, dlogT, logT, logT_start', &
1135 0 : s% model_number, k, s% solver_iter, &
1136 0 : (s% lnT(k) - s% lnT_start(k))/ln10, &
1137 0 : s% lnT(k)/ln10, s% lnT_start(k)/ln10
1138 0 : write(s% retry_message, *) 'abs(dlogT) > hydro_mtx_max_allowed_abs_dlogT', k
1139 0 : ierr = -1
1140 0 : return
1141 : end if
1142 36581 : if (s% lnT(k) > ln10*s% hydro_mtx_max_allowed_logT .and. &
1143 : s% min_logT_for_hydro_mtx_max_allowed < &
1144 : ln10*min(s% lnT(k),s% lnT_start(k))) then
1145 0 : if (report) &
1146 0 : write(*,4) 'hydro_mtx: logT too large', &
1147 0 : s% model_number, k, s% solver_iter, &
1148 0 : s% lnT(k)/ln10, s% lnT_start(k)/ln10
1149 0 : write(s% retry_message, *) 'logT > hydro_mtx_max_allowed_logT', k
1150 0 : ierr = -1
1151 0 : return
1152 : end if
1153 36581 : if (s% lnT(k) < ln10*s% hydro_mtx_min_allowed_logT) then
1154 0 : if (report) &
1155 0 : write(*,4) 'hydro_mtx: logT too small', &
1156 0 : s% model_number, k, s% solver_iter, &
1157 0 : s% lnT(k)/ln10, s% lnT_start(k)/ln10
1158 0 : write(s% retry_message, *) 'logT < hydro_mtx_min_allowed_logT', k
1159 0 : ierr = -1
1160 0 : return
1161 : end if
1162 36581 : if (s% solver_use_T_reference) then
1163 0 : s% T(k) = s% solver_T_reference(k)*exp(s% solver_dx(i_lnT,k))
1164 : else
1165 36581 : s% T(k) = exp(s% lnT(k))
1166 : end if
1167 36581 : if (is_bad_num(s% T(k))) then
1168 0 : s% retry_message = 'bad num for T'
1169 0 : if (s% stop_for_bad_nums) then
1170 0 : write(*,2) 'set_vars_for_solver T', k, s% T(k)
1171 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1172 : end if
1173 0 : if (report) write(*,2) 'bad num T', k, s% T(k)
1174 0 : ierr = -1
1175 : end if
1176 :
1177 : end if
1178 :
1179 36581 : if (do_lum) then
1180 36581 : s% L(k) = x(i_lum)
1181 36581 : if (is_bad_num(s% L(k))) then
1182 0 : s% retry_message = 'bad num for L'
1183 0 : ierr = -1
1184 0 : if (s% stop_for_bad_nums) then
1185 0 : write(*,2) 'set_vars_for_solver L', k, s% L(k)
1186 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1187 : end if
1188 0 : if (report) write(*,2) 'bad num L', k, s% L(k)
1189 : end if
1190 : end if
1191 :
1192 36581 : if (do_w) then
1193 0 : s% w(k) = x(i_w)
1194 0 : if (s% w(k) < 0d0) then
1195 : !write(*,4) 'set_vars_for_solver: fix w < 0', k, &
1196 : ! s% solver_iter, s% model_number, s% w(k)
1197 0 : s% w(k) = s% RSP2_w_fix_if_neg
1198 : end if
1199 0 : if (is_bad_num(s% w(k))) then
1200 0 : s% retry_message = 'bad num for w'
1201 0 : ierr = -1
1202 0 : if (s% stop_for_bad_nums) then
1203 0 : write(*,2) 'set_vars_for_solver w', k, s% w(k)
1204 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1205 : end if
1206 0 : if (report) write(*,2) 'bad num w', k, s% w(k)
1207 : end if
1208 : ! Keep the solver increment consistent with the corrected w.
1209 0 : if (s% w(k) /= x(i_w)) then
1210 0 : s% solver_dx(i_w,k) = s% w(k) - xh_start(i_w,k)
1211 : end if
1212 0 : s% Hp_face(k) = x(i_Hp)
1213 0 : if (is_bad_num(s% Hp_face(k))) then
1214 0 : s% retry_message = 'bad num for Hp_face'
1215 0 : ierr = -1
1216 0 : if (s% stop_for_bad_nums) then
1217 0 : write(*,2) 'set_vars_for_solver Hp_face', k, s% Hp_face(k)
1218 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1219 : end if
1220 0 : if (report) write(*,2) 'bad num Hp_face', k, s% Hp_face(k)
1221 : end if
1222 : end if
1223 :
1224 36581 : if (do_v) then
1225 0 : s% v(k) = x(i_v)
1226 0 : s% dxh_v(k) = s% solver_dx(i_v,k)
1227 0 : if (is_bad_num(s% v(k))) then
1228 0 : s% retry_message = 'bad num for v'
1229 0 : if (s% stop_for_bad_nums) then
1230 0 : write(*,2) 'set_vars_for_solver v', k, s% v(k)
1231 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1232 : end if
1233 0 : if (report) write(*,2) 'bad num v', k, s% v(k)
1234 0 : ierr = -1
1235 : end if
1236 : end if
1237 :
1238 36581 : if (do_u) then
1239 0 : s% u(k) = x(i_u)
1240 0 : s% dxh_u(k) = s% solver_dx(i_u,k)
1241 0 : if (is_bad_num(s% u(k))) then
1242 0 : s% retry_message = 'bad num for u'
1243 0 : if (s% stop_for_bad_nums) then
1244 0 : write(*,2) 'set_vars_for_solver u', k, s% u(k)
1245 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1246 : end if
1247 0 : if (report) write(*,2) 'bad num u', k, s% u(k)
1248 0 : ierr = -1
1249 : end if
1250 : end if
1251 :
1252 36581 : if (do_alpha_RTI) then
1253 0 : s% alpha_RTI(k) = max(0d0, x(i_alpha_RTI))
1254 0 : s% dxh_alpha_RTI(k) = s% solver_dx(i_alpha_RTI,k)
1255 0 : if (is_bad_num(s% alpha_RTI(k))) then
1256 0 : s% retry_message = 'bad num for alpha_RTI'
1257 0 : if (s% stop_for_bad_nums) then
1258 0 : write(*,2) 'set_vars_for_solver alpha_RTI', k, s% alpha_RTI(k)
1259 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1260 : end if
1261 0 : if (report) write(*,2) 'bad num alpha_RTI', k, s% alpha_RTI(k)
1262 0 : ierr = -1
1263 : end if
1264 : ! Use the clipped alpha_RTI in both the solver and time increments.
1265 0 : if (s% alpha_RTI(k) /= x(i_alpha_RTI)) then
1266 0 : s% solver_dx(i_alpha_RTI,k) = s% alpha_RTI(k) - xh_start(i_alpha_RTI,k)
1267 0 : s% dxh_alpha_RTI(k) = s% solver_dx(i_alpha_RTI,k)
1268 : end if
1269 : end if
1270 :
1271 36581 : if (do_w_div_wc) then
1272 0 : s% w_div_w_crit_roche(k) = x(i_w_div_wc)
1273 0 : if (s% w_div_w_crit_roche(k) > 0.99d0) then
1274 0 : s% w_div_w_crit_roche(k) = 0.99d0
1275 : end if
1276 0 : if (s% w_div_w_crit_roche(k) < -0.99d0) then
1277 0 : s% w_div_w_crit_roche(k) = -0.99d0
1278 : end if
1279 0 : if (is_bad_num(s% w_div_w_crit_roche(k))) then
1280 0 : s% retry_message = 'bad num for w_div_w_crit_roche'
1281 0 : if (s% stop_for_bad_nums) then
1282 0 : write(*,2) 'set_vars_for_solver w_div_w_crit_roche', k, s% w_div_w_crit_roche(k)
1283 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1284 : end if
1285 0 : if (report) write(*,2) 'bad num w_div_w_crit_roche', k, s% w_div_w_crit_roche(k)
1286 0 : ierr = -1
1287 : end if
1288 : ! Keep the solver increment consistent with the clipped rotation fraction.
1289 0 : if (s% w_div_w_crit_roche(k) /= x(i_w_div_wc)) then
1290 0 : s% solver_dx(i_w_div_wc,k) = s% w_div_w_crit_roche(k) - xh_start(i_w_div_wc,k)
1291 : end if
1292 : end if
1293 :
1294 36581 : if (do_j_rot) then
1295 0 : s% j_rot(k) = x(i_j_rot)
1296 0 : if (is_bad_num(s% j_rot(k))) then
1297 0 : s% retry_message = 'bad num for j_rot'
1298 0 : if (s% stop_for_bad_nums) then
1299 0 : write(*,2) 'set_vars_for_solver j_rot', k, s% j_rot(k)
1300 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1301 : end if
1302 0 : if (report) write(*,2) 'bad num j_rot', k, s% j_rot(k)
1303 0 : ierr = -1
1304 : end if
1305 : end if
1306 :
1307 36581 : if (do_lnR) then
1308 36581 : s% lnR(k) = x(i_lnR)
1309 36581 : s% r(k) = exp(s% lnR(k))
1310 36581 : s% dxh_lnR(k) = s% solver_dx(i_lnR,k)
1311 36581 : if (is_bad_num(s% lnR(k))) then
1312 0 : s% retry_message = 'bad num for lnR'
1313 0 : if (s% stop_for_bad_nums) then
1314 0 : write(*,2) 'set_vars_for_solver r lnR solver_dx', &
1315 0 : k, s% r(k), s% lnR(k), s% solver_dx(i_lnR,k)
1316 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1317 : end if
1318 0 : if (report) write(*,2) 'bad num r lnR solver_dx', &
1319 0 : k, s% r(k), s% lnR(k), s% solver_dx(i_lnR,k)
1320 0 : ierr = -1
1321 : end if
1322 : end if
1323 :
1324 36581 : if (do_lnd) then
1325 36581 : s% lnd(k) = x(i_lnd)
1326 36581 : s% rho(k) = exp(s% lnd(k))
1327 36581 : s% dxh_lnd(k) = s% solver_dx(i_lnd,k)
1328 36581 : if (s% lnd(k) < ln10*s% hydro_mtx_min_allowed_logRho) then
1329 0 : write(s% retry_message, *) 'logRho < hydro_mtx_min_allowed_logRho', k
1330 0 : if (report) &
1331 0 : write(*,4) 'hydro_mtx: logRho too small', &
1332 0 : s% model_number, k, s% solver_iter, &
1333 0 : s% lnd(k)/ln10, s% lnd_start(k)/ln10
1334 0 : ierr = -1
1335 0 : return
1336 : end if
1337 36581 : if (s% lnd(k) > ln10*s% hydro_mtx_max_allowed_logRho .and. &
1338 : s% min_logT_for_hydro_mtx_max_allowed < &
1339 : ln10*min(s% lnT(k),s% lnT_start(k))) then
1340 0 : write(s% retry_message, *) 'logRho > hydro_mtx_max_allowed_logRho', k
1341 0 : if (s% report_ierr .or. report) &
1342 0 : write(*,4) 'hydro_mtx: logRho too large', &
1343 0 : s% model_number, k, s% solver_iter, &
1344 0 : s% lnd(k)/ln10, s% lnd_start(k)/ln10
1345 0 : ierr = -1
1346 0 : return
1347 : end if
1348 : if (abs(s% lnd(k) - s% lnd_start(k)) > &
1349 36581 : ln10*s% hydro_mtx_max_allowed_abs_dlogRho .and. &
1350 : s% min_logT_for_hydro_mtx_max_allowed < &
1351 : ln10*min(s% lnT(k),s% lnT_start(k))) then
1352 0 : write(s% retry_message, *) 'abs(dlogRho) > hydro_mtx_max_allowed_abs_dlogRho', k
1353 0 : if (s% report_ierr .or. report) &
1354 : write(*,4) &
1355 0 : 'hydro_mtx: dlogRho, logRho, logRho_start', &
1356 0 : s% model_number, k, s% solver_iter, &
1357 0 : (s% lnd(k) - s% lnd_start(k))/ln10, &
1358 0 : s% lnd(k)/ln10, s% lnd_start(k)/ln10
1359 0 : ierr = -1
1360 0 : return
1361 : end if
1362 36581 : if (is_bad_num(s% rho(k))) then
1363 0 : write(s% retry_message, *) 'bad num for rho', k
1364 0 : if (s% stop_for_bad_nums) then
1365 0 : write(*,2) 'set_vars_for_solver rho', k, s% rho(k)
1366 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1367 : end if
1368 0 : if (report) write(*,2) 'bad num rho', k, s% rho(k)
1369 0 : ierr = -1
1370 : end if
1371 : end if
1372 :
1373 36581 : if (do_chem) &
1374 : call check1_chem( &
1375 36581 : s, k, min_xa_hard_limit, sum_xa_hard_limit, report, ierr)
1376 :
1377 36581 : end subroutine set1
1378 :
1379 :
1380 : end subroutine set_vars_for_solver
1381 :
1382 :
1383 36581 : subroutine check1_chem( &
1384 : s, k, min_xa_hard_limit, sum_xa_hard_limit, report, ierr)
1385 : use chem_def, only: chem_isos
1386 : type (star_info), pointer :: s
1387 : integer, intent(in) :: k
1388 : real(dp), intent(in) :: min_xa_hard_limit, sum_xa_hard_limit
1389 : logical, intent(in) :: report
1390 : integer, intent(out) :: ierr
1391 :
1392 : integer :: j, species
1393 : real(dp) :: sum_xa
1394 : logical :: okay
1395 :
1396 : include 'formats'
1397 :
1398 36581 : ierr = 0
1399 36581 : species = s% species
1400 36581 : okay = .true.
1401 36581 : if (min_xa_hard_limit > -1d50) then
1402 329229 : do j=1,species
1403 329229 : if (s% xa(j,k) < min_xa_hard_limit) then
1404 0 : s% retry_message = 'some xa < min_xa_hard_limit'
1405 0 : if (report .or. s% report_bad_negative_xa) then
1406 : write(*,3) &
1407 : 'bad negative xa, min_xa_hard_limit, sig, logT ' // &
1408 0 : trim(chem_isos% name(s% chem_id(j))), j, k, &
1409 0 : s% xa(j,k), min_xa_hard_limit, s% sig(k), s% lnT(k)/ln10
1410 0 : okay = .false.
1411 : end if
1412 0 : ierr = -1
1413 0 : !$omp critical (hydro_mtx_crit1)
1414 0 : s% why_Tlim = Tlim_neg_X
1415 : !$omp end critical (hydro_mtx_crit1)
1416 0 : if (.not. report) return
1417 : end if
1418 : end do
1419 : end if
1420 :
1421 329229 : do j=1,species
1422 329229 : s% xa(j,k) = max(0d0, min(1d0, s% xa(j,k)))
1423 : end do
1424 :
1425 329229 : sum_xa = sum(s% xa(1:species,k))
1426 36581 : if (is_bad_num(sum_xa)) then
1427 0 : ierr = -1
1428 0 : s% retry_message = 'bad num for mass fractions'
1429 0 : if (s% stop_for_bad_nums) then
1430 0 : write(*,2) 'set_vars_for_solver sum_xa', k, sum_xa
1431 0 : call mesa_error(__FILE__,__LINE__,'set_vars_for_solver')
1432 : end if
1433 0 : if (report) then
1434 0 : write(*,'(a60,i8,99f20.10)') 'bad num sum X', k, s% m(k)/Msun, sum_xa
1435 : end if
1436 0 : !$omp critical (hydro_mtx_crit2)
1437 0 : if (s% why_Tlim /= Tlim_neg_X) s% why_Tlim = Tlim_bad_Xsum
1438 : !$omp end critical (hydro_mtx_crit2)
1439 0 : if (.not. report) return
1440 : end if
1441 36581 : if (abs(sum_xa - 1d0) > sum_xa_hard_limit) then
1442 0 : s% retry_message = 'mass fractions > sum_xa_hard_limit'
1443 0 : if (report) then
1444 : write(*,2) &
1445 0 : 'bad sumX', k, &
1446 0 : sum_xa - 1d0, sum_xa_hard_limit, &
1447 0 : sum(s% xa_start(1:species,k)), &
1448 0 : sum_xa - sum(s% xa_start(1:species,k))
1449 : end if
1450 0 : ierr = -1
1451 0 : !$omp critical (hydro_mtx_crit3)
1452 0 : if (s% why_Tlim /= Tlim_neg_X) s% why_Tlim = Tlim_bad_Xsum
1453 : !$omp end critical (hydro_mtx_crit3)
1454 0 : okay = .false.
1455 0 : if (.not. report) return
1456 : end if
1457 :
1458 36581 : if (abs(sum_xa - 1d0) > 1d-12) then
1459 : !jmax = maxloc(s% xa(1:species,k),dim=1)
1460 : !xsum = sum(s% xa(1:species,k)) - s% xa(jmax,k)
1461 : !if (1d0 > xsum) then
1462 : ! s% xa(jmax,k) = 1d0 - xsum
1463 : !else
1464 3816 : do j=1,species
1465 3816 : s% xa(j,k) = s% xa(j,k)/sum_xa
1466 : end do
1467 : !end if
1468 : end if
1469 :
1470 36581 : if (s% xa_clip_limit > 0) then
1471 329229 : do j=1,species
1472 329229 : if (s% xa(j,k) < s% xa_clip_limit) s% xa(j,k) = 0d0
1473 : end do
1474 : end if
1475 :
1476 36581 : if (ierr /= 0) return
1477 : ! Keep the solver increments consistent with the corrected abundances.
1478 329229 : do j=1,species
1479 292648 : if (s% xa(j,k) == s% xa_start(j,k) + s% solver_dx(s% nvar_hydro+j,k)) cycle
1480 3392 : s% solver_dx(s% nvar_hydro+j,k) = s% xa(j,k) - s% xa_start(j,k)
1481 329229 : s% xa_sub_xa_start(j,k) = s% solver_dx(s% nvar_hydro+j,k)
1482 : end do
1483 :
1484 : end subroutine check1_chem
1485 :
1486 :
1487 : subroutine dump_struct(s)
1488 : type (star_info), pointer :: s
1489 : integer :: k, j
1490 :
1491 : include 'formats'
1492 :
1493 : do k=1,s% nz
1494 : write(*,2) 'dq', k, s% dq(k)
1495 : write(*,2) 'm', k, s% m(k)
1496 : write(*,2) 'T', k, s% T(k)
1497 : write(*,2) 'rho', k, s% rho(k)
1498 : write(*,2) 'Pgas', k, s% Pgas(k)
1499 : write(*,2) 'L', k, s% L(k)
1500 : write(*,2) 'r', k, s% r(k)
1501 : write(*,2) 'grada', k, s% grada(k)
1502 : write(*,2) 'opacity', k, s% opacity(k)
1503 : write(*,2) 'd_opacity_dlnd', k, s% d_opacity_dlnd(k)
1504 : write(*,2) 'd_opacity_dlnT', k, s% d_opacity_dlnT(k)
1505 : write(*,2) 'eps_nuc', k, s% eps_nuc(k)
1506 : write(*,2) 'd_epsnuc_dlnd', k, s% d_epsnuc_dlnd(k)
1507 : write(*,2) 'd_epsnuc_dlnT', k, s% d_epsnuc_dlnT(k)
1508 : write(*,2) 'non_nuc_neu', k, s% non_nuc_neu(k)
1509 : write(*,2) 'd_nonnucneu_dlnd', k, s% d_nonnucneu_dlnd(k)
1510 : write(*,2) 'd_nonnucneu_dlnT', k, s% d_nonnucneu_dlnT(k)
1511 : write(*,2) 'eps_grav', k, s% eps_grav_ad(k)% val
1512 : write(*,2) 'gradT', k, s% gradT(k)
1513 : !write(*,2) '', k, s% (k)
1514 : do j=1,s% species
1515 : write(*,3) 'xa(j,k)', j, k, s% xa(j,k)
1516 : end do
1517 : end do
1518 :
1519 :
1520 : end subroutine dump_struct
1521 :
1522 :
1523 31 : subroutine edit_lnR(s, xh_start, dx)
1524 : ! uses mass and density to set radius
1525 : type (star_info), pointer :: s
1526 : real(dp), dimension(:, :) :: xh_start
1527 : real(dp), dimension(:, :) :: dx
1528 : real(dp) :: vol00, volp1, cell_vol
1529 : integer :: k, nz
1530 : include 'formats'
1531 31 : vol00 = four_thirds_pi*s% R_center*s% R_center*s% R_center
1532 31 : nz = s% nz
1533 36612 : do k=nz, 1, -1
1534 36581 : volp1 = vol00
1535 36581 : cell_vol = s% dm(k)/s% rho(k)
1536 36581 : vol00 = volp1 + cell_vol
1537 36581 : s% lnR(k) = log(vol00/four_thirds_pi)/3
1538 36581 : dx(s% i_lnR,k) = s% lnR(k) - xh_start(s% i_lnR,k)
1539 36612 : s% dxh_lnR(k) = dx(s% i_lnR,k)
1540 : end do
1541 31 : call edit_dlnR_dt_above_k_below_just_added(s, xh_start)
1542 31 : end subroutine edit_lnR
1543 :
1544 :
1545 31 : subroutine edit_dlnR_dt_above_k_below_just_added(s, xh_start)
1546 : type (star_info), pointer :: s
1547 : real(dp), dimension(:, :) :: xh_start
1548 : integer :: k_below_just_added
1549 : real(dp) :: lnR_start
1550 31 : k_below_just_added = s% k_below_just_added
1551 : if (k_below_just_added == 1) return
1552 : lnR_start = xh_start(s% i_lnR,1)
1553 : end subroutine edit_dlnR_dt_above_k_below_just_added
1554 :
1555 : end module solver_support
|