MPI-AMRVAC 3.2
The MPI - Adaptive Mesh Refinement - Versatile Advection Code (development version)
Loading...
Searching...
No Matches
mod_hd_phys.t
Go to the documentation of this file.
1!> Hydrodynamics physics module
6 use mod_fld, only: fld_fluid
8 use mod_eos
9 use mod_rt_sc_slab, only: rt_sc_activate, rt_sc_setup, rt_sc_add_source
10 use mod_comm_lib, only: mpistop
11 implicit none
12 private
13
14 !> Whether an energy equation is used
15 logical, public, protected :: hd_energy = .true.
16
17 !> Whether thermal conduction is added
18 logical, public, protected :: hd_thermal_conduction = .false.
19 !> Whether hyperbolic thermal conduction (Cattaneo relaxation) is used.
20 !> 1D only : the q-variable is treated as a scalar carrying the
21 !> heat flux along the only spatial direction.
22 logical, public, protected :: hd_hyperbolic_thermal_conduction = .false.
23 !> Whether saturation is considered for hyperbolic TC
24 logical, public, protected :: hd_htc_sat = .false.
25 type(tc_fluid), allocatable, public :: tc_fl
26 type(te_fluid), allocatable, public :: te_fl_hd
27
28 !> Whether radiative cooling is added
29 logical, public, protected :: hd_radiative_cooling = .false.
30 !> Whether the short-characteristics radiative transfer is used, with its settings in &rt_list
31 logical, public, protected :: hd_radiative_transfer = .false.
32 type(rc_fluid), allocatable, public :: rc_fl
33
34 !> Whether dust is added
35 logical, public, protected :: hd_dust = .false.
36
37 !> Whether dust is added using and implicit update in IMEX
38 logical, public, protected :: hd_dust_implicit = .false.
39
40 !> Whether radiation-gas interaction is handled using flux limited diffusion
41 logical, public, protected :: hd_radiation_fld = .false.
42 logical, public, protected :: hd_fld_pradtensor= .true.
43 !> Radiation fluid object (gas-EoS callbacks for FLD), wired in hd_link_eos
44 type(fld_fluid), allocatable, public :: fld_fl
45
46 !> Whether viscosity is added
47 logical, public, protected :: hd_viscosity = .false.
48
49 !> Whether gravity is added
50 logical, public, protected :: hd_gravity = .false.
51
52 !> Whether particles module is added
53 logical, public, protected :: hd_particles = .false.
54
55 !> Whether rotating frame is activated
56 logical, public, protected :: hd_rotating_frame = .false.
57
58 !> Whether CAK radiation line force is activated
59 logical, public, protected :: hd_cak_force = .false.
60
61 !> Number of tracer species
62 integer, public, protected :: hd_n_tracer = 0
63
64 !> Whether plasma is partially ionized
65
66 !> Index of the density (in the w array)
67 integer, public, protected :: rho_
68
69 !> Indices of the momentum density
70 integer, allocatable, public, protected :: mom(:)
71
72 !> Indices of the momentum density for the form of better vectorization
73 integer, public, protected :: ^c&m^C_
74
75 !> Indices of the tracers
76 integer, allocatable, public, protected :: tracer(:)
77
78 !> Index of the energy density (-1 if not present)
79 integer, public, protected :: e_
80
81 !> Index of the gas pressure (-1 if not present) should equal e_
82 integer, public, protected :: p_
83
84 !> Index of the electron number density for LTE module
85 integer, public, protected :: ne_
86
87 !> Index of the radiation energy (when fld active)
88 integer, public, protected :: r_e
89
90 !> Indices of temperature
91 integer, public, protected :: te_
92
93 !> Index of the FIP passive scalar rho*fip in conserved form, fip in primitive form
94 integer, public, protected :: fip_ = -1
95
96 !> Whether FIP passive scalar is enabled
97 logical, public, protected :: hd_fip = .false.
98
99 !> Index of the cutoff temperature for the TRAC method
100 integer, public, protected :: tcoff_
101
102 !> Index of the hyperbolic-TC heat-flux variable (-1 if not present)
103 integer, public, protected :: q_
104
105 !> Thermal-conductivity prefactor in hyperbolic TC, set in hd_physical_units.
106 !> Spitzer form: κ(T) = hypertc_kappa · T^{5/2}.
107 double precision, public :: hypertc_kappa
108
109 !> Optional parfile override for hypertc_kappa (e.g. to match a constant-κ
110 !> parabolic TC run for benchmarking). Default -1.0 leaves the Spitzer
111 !> value computed from physical units.
112 double precision, public, protected :: hd_htc_kappa_override = -1.0d0
113
114 !> Hyperdiffusion coefficient applied to the cell-refreshed q at the
115 !> end of each face-recipe substep. 4th-order undivided difference
116 !> smoother: q -= alpha * (qdt/dt) * (q_{i+2} + q_{i-2}
117 !> - 4(q_{i+1} + q_{i-1})
118 !> + 6 q_i).
119 !> MURaM (Rempel 2017) uses 0.02. Damps cell-scale q oscillations
120 !> arising from kappa(T) amplifying small persistent T bumps in the
121 !> EoS-derived cell-centred T profile; without it, the face-recipe
122 !> q field has visible 5-cell-scale wiggles even where T is smooth.
123 !> This is the only one of the OLD code's three q-smoothers (HLL
124 !> diffusion, Koren reconstruction, hyperdiff) that the face-recipe
125 !> needs -- the architectural problems with the other two are not
126 !> reintroduced.
127 double precision, public, protected :: hd_htc_hyp_diff = 0.02d0
128
129 !> Face-recipe heat-wave speed scaling: c_HTC,f = hd_htc_beta * c_max,f.
130 !> Higher value -> closer to diffusion limit (q tracks Spitzer noise
131 !> aggressively, AMR-triggering corona noise). Lower value -> more
132 !> hyperbolic (q lags Spitzer target by Delta_t/tau ~ 1/beta^2 per
133 !> step, dampening high-frequency noise). At our resolution beta=2-3
134 !> is the practical sweet spot: q evolves slowly enough that T-table
135 !> round-off noise doesn't propagate, but fast enough that real TR
136 !> conduction equilibrates within O(100) hydro timesteps.
137 double precision, public, protected :: hd_htc_beta = 2.0d0
138
139 !> Cowie-McKee saturation coefficient: q_sat = hd_htc_sat_alpha *
140 !> rho * c_s^3. Standard convention is alpha ~ 1 (absorbs the
141 !> sqrt(m_p/m_e) factor that would appear if c_s were replaced by
142 !> the electron thermal speed). Default 1.0.
143 double precision, public, protected :: hd_htc_sat_alpha = 1.0d0
144
145 !> Per-face energy-positivity safety fraction: |q_f^{n+1/2} dt A_f|
146 !> <= hd_htc_pos_eta * min(e_int_L V_L, e_int_R V_R). Default 0.5
147 !> leaves headroom against simultaneous PdV and cooling decrements.
148 double precision, public, protected :: hd_htc_pos_eta = 0.5d0
149
150 !> Validity-monitor threshold for l_r,f / Delta_x_f. Warn if any face
151 !> exceeds this in a given block (printed once per dtsave_log step).
152 !> Default 0.1: above this, HTC is modifying the physics beyond pure
153 !> Spitzer; above 1.0 the local Spitzer approximation breaks down.
154 double precision, public, protected :: hd_htc_validity_warn = 0.1d0
155
156 !> Gradient deadband: zero out the Spitzer face flux when
157 !> abs(T_R - T_L) / max(T_L, T_R) < hd_htc_gradT_floor
158 !> This suppresses sign-flipping q noise driven by EoS-table round-off
159 !> (~1e-4 in T) being amplified by huge coronal kappa. Default 1.0e-3
160 !> sits 10x above the table noise floor but 5x below typical coronal
161 !> gradients (Delta_x / L_T ~ 5e-3 at our resolution), so it kills the
162 !> noise without suppressing real conduction. Set to 0.0 to disable.
163 double precision, public, protected :: hd_htc_gradt_floor = 1.0d-3
164
165 !> Running max of l_r,f / dx_f across all face-recipe calls since
166 !> simulation start. Inspect post-hoc via debugger or dump alongside
167 !> dat files. Not reset per timestep -- monotonic non-decreasing.
168 double precision, public :: hd_htc_validity_max_runtime = 0.0d0
169
170 !> Index into wextra for escape probability column mass
171 integer, public, protected :: iw_colmass = -1
172
173 !> gamma is set in &eos_list and accessed via eos%gamma
174
175 !> The adiabatic constant
176 double precision, public :: hd_adiab = 1.0d0
177
178 !> Whether TRAC method is used
179 logical, public, protected :: hd_trac = .false.
180 integer, public, protected :: hd_trac_type = 1
181 integer, public, protected :: hd_trac_nzones = 1
182 double precision, public, protected :: hd_trac_zone_splits(10) = -1.d0
183 !> Johnston 2021 resolution parameter delta (default 0.5)
184 double precision, public, protected :: hd_trac_delta = 0.5d0
185 !> fixed cutoff temperature in K for hd_trac_type=0 (Lionello et al. 2009, Mikic et al. 2013)
186 double precision, public, protected :: hd_trac_tfix = 3.d5
187 !> Whether well-balanced reconstruction is used (Kaeppeli & Mishra style)
188 logical, public, protected :: hd_well_balanced = .false.
189
190
191 !> Helium abundance over Hydrogen
192 !> He_abundance is set in &eos_list and accessed via eos%He_abundance
193 !> Ionization fraction of H
194 !> H_ion_fr = H+/(H+ + H)
195 double precision, public, protected :: h_ion_fr=1d0
196 !> Ionization fraction of He
197 !> He_ion_fr = (He2+ + He+)/(He2+ + He+ + He)
198 double precision, public, protected :: he_ion_fr=1d0
199 !> Ratio of number He2+ / number He+ + He2+
200 !> He_ion_fr2 = He2+/(He2+ + He+)
201 double precision, public, protected :: he_ion_fr2=1d0
202 ! used for eq of state when it is not defined by units,
203 ! the units do not contain terms related to ionization fraction
204 ! and it is p = RR * rho * T
205 double precision, public, protected :: rr=1d0
206 ! remove the below flag and assume default value = .false.
207 ! procedure(sub_get_pthermal), pointer :: hd_get_Rfactor => null()
208 ! Public methods
209 public :: hd_phys_init
210 public :: hd_kin_en
211 public :: hd_get_csound2
212 public :: hd_check_params
213 public :: hd_check_w
214 public :: hd_handle_small_values
215 public :: hd_e_to_ei
216 public :: hd_ei_to_e
217 ! hd_get_Rfactor was the FI pointer; FLD uses phys_get_Rfactor which is
218 ! bound by mod_hd_eos:bind_eos_to_source to eos%get_Rfactor.
219 ! Begin: following relevant for radiative hydro using FLD
220 ! first four are local and only of interest for mod_usr applications
221 ! where they can be used in diagnostics
222 ! NOTE those with _prim expect primitives on entry
224 public :: hd_get_csrad2
225 public :: hd_get_trad
227 ! the following used in FLD modules
228 ! as pointer phys_get_csrad2
229 public :: hd_get_csrad2_prim
230 ! End: following relevant for radiative hydro using FLD
232
233contains
234
235 !> Read this module's parameters from a file
236 subroutine hd_read_params(files)
238 character(len=*), intent(in) :: files(:)
239 integer :: n
240
241 namelist /hd_list/ hd_energy, hd_n_tracer, hd_adiab, &
253
254 do n = 1, size(files)
255 open(unitpar, file=trim(files(n)), status="old")
256 read(unitpar, hd_list, end=111)
257111 close(unitpar)
258 end do
259
260 end subroutine hd_read_params
261
262 !> Write this module's parameters to a snapsoht
263 subroutine hd_write_info(fh)
265 integer, intent(in) :: fh
266 integer, parameter :: n_par = 1
267 double precision :: values(n_par)
268 character(len=name_len) :: names(n_par)
269 integer, dimension(MPI_STATUS_SIZE) :: st
270 integer :: er
271
272 call mpi_file_write(fh, n_par, 1, mpi_integer, st, er)
273
274 names(1) = "gamma"
275 values(1) = eos%gamma
276 call mpi_file_write(fh, values, n_par, mpi_double_precision, st, er)
277 call mpi_file_write(fh, names, n_par * name_len, mpi_character, st, er)
278 end subroutine hd_write_info
279
280 !> Initialize the module
281 subroutine hd_phys_init()
285 use mod_dust, only: dust_init
287 use mod_gravity, only: gravity_init
289 use mod_cak_force, only: cak_init
295 use mod_fld
296
297 integer :: itr, idir
298
299 call hd_read_params(par_files)
300
301 physics_type = "hd"
302 phys_energy = hd_energy
303 phys_total_energy = hd_energy
304 phys_internal_e = .false.
305 phys_gamma = eos%gamma
306
308 if(phys_trac) then
309 if(ndim .eq. 1) then
310 if(hd_trac_type .gt. 2 .and. hd_trac_type .ne. 7) then
312 if(mype==0) write(*,*) 'WARNING: set hd_trac_type=1'
313 end if
314 if(hd_trac_type == 7) then
315 if(.not. associated(usr_get_heating)) then
316 call mpistop("hd_trac_type=7 requires usr_get_heating to be set in mod_usr.t")
317 end if
318 end if
322 else
323 ! multi-D: only the local (per-cell) Johnston-2021 TRAC (type 7) is supported here;
324 ! the column-sweep variants (1,2) need field-line/vertical tracing not implemented for HD.
325 if(hd_trac_type == 7) then
326 if(.not. associated(usr_get_heating)) then
327 call mpistop("hd_trac_type=7 requires usr_get_heating to be set in mod_usr.t")
328 end if
330 else
331 phys_trac=.false.
332 if(mype==0) write(*,*) 'WARNING: hd_trac disabled for ndim>=2 (only trac_type=7 supported)'
333 end if
334 end if
335 end if
336
337 ! set default gamma for polytropic/isothermal process
338 if(.not.hd_energy) then
339 if(hd_thermal_conduction) then
341 if(mype==0) write(*,*) 'WARNING: set hd_thermal_conduction=F when hd_energy=F'
342 end if
345 if(mype==0) write(*,*) 'WARNING: set hd_hyperbolic_thermal_conduction=F when hd_energy=F'
346 end if
347 if(hd_radiative_cooling) then
349 if(mype==0) write(*,*) 'WARNING: set hd_radiative_cooling=F when hd_energy=F'
350 end if
351 end if
353
354 allocate(start_indices(number_species),stop_indices(number_species))
355
356 ! set the index of the first flux variable for species 1
357 start_indices(1)=1
358 ! Determine flux variables
359 rho_ = var_set_rho()
360
361 allocate(mom(ndir))
362 mom(:) = var_set_momentum(ndir)
363 m^c_=mom(^c);
364
365 ! Set index of energy variable
366 if (hd_energy) then
367 e_ = var_set_energy()
368 p_ = e_
369 else
370 e_ = -1
371 p_ = -1
372 end if
373
374 ! HTC q-variable must be allocated BEFORE any wextra slots (Ne, Te, ...)
375 ! so its iw_q index doesn't collide with the wextra indices, which are
376 ! assigned from nw (not nwflux). ffHD does the same ordering.
378 q_ = var_set_q()
379 need_global_cmax=.true.
380 ! q is a noisy diagnostic-like quantity (face-recipe construction
381 ! produces cell-to-cell sign flips in nearly-uniform-T regions
382 ! where -kappa*grad(T) is round-off-dominated). Excluding it from
383 ! AMR refinement avoids triggering full-domain refinement everywhere.
384 ! Lohner already refines on rho and E.
385 if (allocated(w_refine_weight)) w_refine_weight(q_) = 0.0d0
386 else
387 q_=-1
388 end if
389
390 ! fip and the tracers are flux variables (index nwflux) and must precede the extra slots Ne/Te
391 ! (index nw): registered after them, the first one shared Ne's index and the EoS overwrote it
392 if (hd_fip) then
393 fip_ = var_set_fluxvar('rho_fip', 'fip', need_bc=.false.)
394 else
395 fip_ = -1
396 end if
397
398 allocate(tracer(hd_n_tracer))
399
400 ! Set starting index of tracers
401 do itr = 1, hd_n_tracer
402 tracer(itr) = var_set_fluxvar("trc", "trp", itr, need_bc=.false.)
403 end do
404
405 if (eos%eos_type == 'LTE') then
406 ne_ = var_set_ne()
407 te_ = var_set_te()
408 else if (eos%eos_type == 'PI') then ! PI stores Te via var_set_te (sets iw_te) so the generic mod_eos_pi getters address it like LTE
409 ne_ = -1
410 te_ = var_set_te()
411 else
412 ne_ = -1
413 te_ = -1
414 end if
415
416 ! The radiative transfer reads &rt_list and registers its output variables here, and is set up
417 ! in hd_check_params once the units and the domain are known
418 if(hd_radiative_transfer) then
419 if(.not. hd_energy) call mpistop("radiative transfer needs hd_energy=T")
420 call rt_sc_activate()
421 end if
422
423 if(hd_radiation_fld)then
424 if(hd_cak_force)then
425 if(mype==0) then
426 write(*,*)'Warning: CAK force addition together with FLD radiation'
427 endif
428 endif
430 if(mype==0) then
431 write(*,*)'Warning: Optically thin cooling together with FLD radiation'
432 endif
433 endif
434 if(hd_dust.and.hd_dust_implicit)then
435 call mpistop('implicit dust addition not compatible with FLD radiation')
436 endif
437 if(.not.hd_energy)then
438 call mpistop('using FLD implies the use of an energy equation, set hd_energy=T')
439 else
440 !> set added variable and equation for radiation energy
441 r_e = var_set_radiation_energy()
442 phys_get_csrad2 => hd_get_csrad2_prim
443 !> Radiation fluid object: its EoS callbacks are wired in hd_link_eos
444 allocate(fld_fl)
445 !> Initiate radiation-closure module
446 call fld_init(eos%eos_type, eos%ionE, eos%pi_table)
447 !> The implicit (MG diffusion) hooks need the fld_fl object, so they
448 !> are wired here to physics-module wrappers that inject it.
449 if(use_multigrid)then
450 phys_implicit_update => hd_fld_implicit_update
451 phys_evaluate_implicit => hd_fld_evaluate_implicit
452 endif
453 endif
454 else
455 r_e=-1
456 endif
457
458 phys_get_dt => hd_get_dt
459 phys_get_cmax => hd_get_cmax
460 phys_get_tcutoff => hd_get_tcutoff
461 phys_get_cbounds => hd_get_cbounds
462 phys_get_flux => hd_get_flux
463 phys_add_source_geom => hd_add_source_geom
464 phys_add_source => hd_add_source
465 phys_modify_wlr => hd_modify_wlr
466 phys_check_params => hd_check_params
467 phys_check_w => hd_check_w
468 ! phys_get_pthermal is set by hd_link_eos
469 phys_get_v => hd_get_v
470 ! phys_get_rho => hd_get_rho
471 phys_write_info => hd_write_info
472 phys_handle_small_values => hd_handle_small_values
473 phys_e_to_ei => hd_e_to_ei
474 phys_ei_to_e => hd_ei_to_e
475 phys_get_ei => hd_get_ei
476
477 ! derive units from basic units
478 call hd_physical_units()
479
480 ! Spitzer prefactor for hyperbolic TC in code units. κ(T) = κ_0 · T^{5/2}
481 ! with κ_0 = 8e-12 (SI) or 8e-7 (CGS), unless the user provides an override
482 ! (e.g. to match a constant-κ parabolic TC run for benchmarking).
484 if (hd_htc_kappa_override > 0.0d0) then
486 if (mype == 0) write(*,*) ' HTC: using hd_htc_kappa_override =', hypertc_kappa
487 else if(si_unit) then
489 else
491 end if
492 end if
493
494 if (hd_dust) then
495 call dust_init(rho_, mom(:), e_)
496 endif
497
498
499 ! set number of variables which need update ghostcells.
500 ! The EoS-derived slots Ne/Te are derived state that must stay consistent with (rho,e)
501 ! wherever the conserved state is valid, so they have to travel with it. var_set_ne /
502 ! var_set_te bump nw but neither nwflux nor nwaux, so the historic nwflux+nwaux silently
503 ! excluded them: they were never communicated, only derived, and every ghost cell held
504 ! zero until something derived it. Extend the window to whichever of them exist (they are
505 ! registered contiguously just above); FI leaves both at -1 and the window is unchanged.
506 ! bc_phys is unaffected -- it iterates nwflux+nwaux independently.
507 nwgc=nwflux+nwaux
508 if (iw_ne > 0) nwgc = max(nwgc, iw_ne)
509 if (iw_te > 0) nwgc = max(nwgc, iw_te)
510
511 ! set the index of the last flux variable for species 1
512 stop_indices(1)=nwflux
513
514 if(hd_trac) then
515 tcoff_ = var_set_wextra()
516 iw_tcoff=tcoff_
517 else
518 tcoff_ = -1
519 end if
520
521 !> Cache log10(nH) in wextra for LTE+IonE TC (density invariant during STS)
522 if (eos%eos_type == 'LTE' .and. eos%ionE .and. hd_thermal_conduction) then
523 iw_log_nh = var_set_wextra()
524 end if
525
526 ! phys_get_Rfactor is bound by mod_hd_eos:bind_eos_to_source to eos%get_Rfactor.
527
528 ! initialize thermal conduction module
529 if (hd_thermal_conduction) then
530 if (.not. hd_energy) &
531 call mpistop("thermal conduction needs hd_energy=T")
532
533 call sts_init()
534 call tc_init_params(eos%gamma)
535
536 allocate(tc_fl)
537 call tc_get_hd_params(tc_fl,tc_params_read_hd)
538 call add_sts_method(hd_get_tc_dt_hd,hd_sts_set_source_tc_hd,e_,1,e_,1,.false.)
539 if (iw_log_nh > 0) then
540 call set_conversion_methods_to_head(hd_e_to_ei_and_cache_log_nh, hd_ei_to_e)
541 else
543 end if
544 call set_error_handling_to_head(hd_tc_handle_small_e)
545 tc_fl%e_ = e_
546 tc_fl%Tcoff_ = tcoff_
547 else if (hd_hyperbolic_thermal_conduction .and. hd_trac) then
548 ! HTC + TRAC: allocate a stub tc_fl so TRAC type 7 can read tc_k_para.
549 ! Use hypertc_kappa (Spitzer prefactor, same form as tc_k_para) so the
550 ! TRAC kappa-effective formula reduces to the same expression as in
551 ! the PTC branch. No STS init; no flux routine; this is read-only.
552 call tc_init_params(eos%gamma)
553 allocate(tc_fl)
554 tc_fl%tc_k_para = hypertc_kappa
555 tc_fl%e_ = e_
556 tc_fl%Tcoff_ = tcoff_
557 end if
558
559 ! Initialize radiative cooling module
560 if (hd_radiative_cooling) then
561 if (.not. hd_energy) &
562 call mpistop("radiative cooling needs hd_energy=T")
563 call radiative_cooling_init_params(eos%gamma,eos%He_abundance)
564 allocate(rc_fl)
565 rc_fl%fip_ = fip_
566 call radiative_cooling_init(rc_fl,rc_params_read)
567 rc_fl%e_ = e_
568 rc_fl%Tcoff_ = tcoff_
569 ! Initialize escape probability if requested
570 if (rc_fl%rad_escape_prob) then
571 iw_colmass = var_set_wextra()
572 rc_fl%iw_colmass_ = iw_colmass
573 phys_escape_prob = .true.
574 call escape_prob_init(iw_colmass, rc_fl%rad_modify_sym, rc_fl%rad_escape_height)
575 end if
576 end if
577 allocate(te_fl_hd)
578
579{^ifthreed
580 phys_te_images => hd_te_images
581}
582 ! Initialize viscosity module
583 if (hd_viscosity) call viscosity_init(phys_wider_stencil)
584
585 ! Initialize gravity module
586 if (hd_gravity) call gravity_init()
587
588 ! Well-balanced reconstruction: only meaningful with gravity
589 if (hd_well_balanced) then
590 if (.not. hd_gravity) then
591 hd_well_balanced = .false.
592 if(mype==0) write(*,*) 'WARNING: set hd_well_balanced=F (requires hd_gravity=T)'
593 else
594 phys_wb_transform => hd_wb_transform
595 phys_wb_inverse => hd_wb_inverse
596 phys_wb_prolong => hd_wb_prolong
597 ! the WB transform needs p -> e to invert e -> p exactly, which the bisection does for
598 ! the tables and the entropy method alike
599 if (eos%ionE .and. eos%p2eint_method /= 'bisect') then
600 eos%p2eint_method = 'bisect'
601 if(mype==0) write(*,*) 'WB + ionE: forcing p2eint_method = bisect'
602 end if
603 if(mype==0) write(*,*) 'Well-balanced reconstruction enabled'
604 end if
605 end if
606
607 ! Initialize rotating_frame module
609
610 ! Initialize CAK radiation force module
611 if (hd_cak_force) call cak_init(eos%gamma)
612
613
614 ! Check whether custom flux types have been defined
615 if (.not. allocated(flux_type)) then
616 allocate(flux_type(ndir, nw))
617 flux_type = flux_default
618 else if (any(shape(flux_type) /= [ndir, nw])) then
619 call mpistop("phys_check error: flux_type has wrong shape")
620 end if
621
622 nvector = 1 ! No. vector vars
623 allocate(iw_vector(nvector))
624 iw_vector(1) = mom(1) - 1
625 ! ionization-degree table init now lives in eos_finalise (eos% owns
626 ! thermodynamic-backend init); see mod_eos_pi.
627
628 end subroutine hd_phys_init
629
630 !> allow the user to control the left/right states (and hence the flux) at
631 !> physical boundary interfaces, as in the MHD module
632 subroutine hd_modify_wlr(ixI^L,ixO^L,qt,wLC,wRC,wLp,wRp,s,idir)
635 integer, intent(in) :: ixi^l, ixo^l, idir
636 double precision, intent(in) :: qt
637 double precision, intent(inout) :: wlc(ixi^s,1:nw), wrc(ixi^s,1:nw)
638 double precision, intent(inout) :: wlp(ixi^s,1:nw), wrp(ixi^s,1:nw)
639 type(state) :: s
640
641 if(associated(usr_set_wlr)) call usr_set_wlr(ixi^l,ixo^l,qt,wlc,wrc,wlp,wrp,s,idir)
642
643 end subroutine hd_modify_wlr
644
645{^ifthreed
646 subroutine hd_te_images
649 select case(convert_type)
650 case('EIvtiCCmpi','EIvtuCCmpi')
652 case('ESvtiCCmpi','ESvtuCCmpi')
654 case('SIvtiCCmpi','SIvtuCCmpi')
656 case('WIvtiCCmpi','WIvtuCCmpi')
658 case default
659 call mpistop("Error in synthesize emission: Unknown convert_type")
660 end select
661 end subroutine hd_te_images
662}
663!!start th cond
664 ! wrappers for STS functions in thermal_conductivity module
665 ! which take as argument the tc_fluid (defined in the physics module)
666 subroutine hd_sts_set_source_tc_hd(ixI^L,ixO^L,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux)
670 integer, intent(in) :: ixi^l, ixo^l, igrid, nflux
671 double precision, intent(in) :: x(ixi^s,1:ndim)
672 double precision, intent(inout) :: wres(ixi^s,1:nw), w(ixi^s,1:nw)
673 double precision, intent(in) :: my_dt
674 logical, intent(in) :: fix_conserve_at_step
675 call sts_set_source_tc_hd(ixi^l,ixo^l,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux,tc_fl)
676 end subroutine hd_sts_set_source_tc_hd
677
678 function hd_get_tc_dt_hd(w,ixI^L,ixO^L,dx^D,x) result(dtnew)
679 !Check diffusion time limit dt < dx_i**2/((gamma-1)*tc_k_para/rho)
680 !and tc_k_para can depend on T=p/rho
683
684 integer, intent(in) :: ixi^l, ixo^l
685 double precision, intent(in) :: dx^d, x(ixi^s,1:ndim)
686 double precision, intent(in) :: w(ixi^s,1:nw)
687 double precision :: dtnew
688
689 dtnew=get_tc_dt_hd(w,ixi^l,ixo^l,dx^d,x,tc_fl)
690 end function hd_get_tc_dt_hd
691
692 subroutine hd_tc_handle_small_e(w, x, ixI^L, ixO^L, step)
693 ! move this in a different routine as in mhd if needed in more places
696
697 integer, intent(in) :: ixi^l,ixo^l
698 double precision, intent(inout) :: w(ixi^s,1:nw)
699 double precision, intent(in) :: x(ixi^s,1:ndim)
700 integer, intent(in) :: step
701
702 integer :: idir
703 logical :: flag(ixi^s,1:nw)
704 character(len=140) :: error_msg
705
706 flag=.false.
707 where(w(ixo^s,e_)<small_e) flag(ixo^s,e_)=.true.
708 if(any(flag(ixo^s,e_))) then
709 select case (small_values_method)
710 case ("replace")
711 where(flag(ixo^s,e_)) w(ixo^s,e_)=small_e
712 case ("average")
713 call small_values_average(ixi^l, ixo^l, w, x, flag, e_)
714 case default
715 ! small values error shows primitive variables
716 w(ixo^s,e_)=w(ixo^s,e_)*(eos%gamma - 1.0d0)
717 do idir = 1, ndir
718 w(ixo^s, iw_mom(idir)) = w(ixo^s, iw_mom(idir))/w(ixo^s,rho_)
719 end do
720 write(error_msg,"(a,i3)") "Thermal conduction step ", step
721 call small_values_error(w, x, ixi^l, ixo^l, flag, error_msg)
722 end select
723 end if
724 end subroutine hd_tc_handle_small_e
725
726 ! fill in tc_fluid fields from namelist
727 subroutine tc_params_read_hd(fl)
729 type(tc_fluid), intent(inout) :: fl
730 integer :: n
731 logical :: tc_saturate=.false.
732 logical :: tc_patch_eint=.false.
733 logical :: tc_frozen_ion=.true.
734 double precision :: tc_k_para=0d0
735 double precision :: trac_t_floor=0.d0
736 double precision :: tc_sat_coef=-1.d0
737
738 namelist /tc_list/ tc_saturate, tc_k_para, trac_t_floor, tc_patch_eint, tc_frozen_ion, tc_sat_coef
739
740 do n = 1, size(par_files)
741 open(unitpar, file=trim(par_files(n)), status="old")
742 read(unitpar, tc_list, end=111)
743111 close(unitpar)
744 end do
745 fl%tc_saturate = tc_saturate
746 fl%tc_sat_coef = tc_sat_coef
747 fl%tc_patch_eint = tc_patch_eint
748 fl%frozen_ion = tc_frozen_ion
749 fl%tc_k_para = tc_k_para
750 fl%trac_T_floor = trac_t_floor / unit_temperature
751
752 end subroutine tc_params_read_hd
753
754 ! subroutine hd_get_rho(w,x,ixI^L,ixO^L,rho)
755 ! use mod_global_parameters
756 ! integer, intent(in) :: ixI^L, ixO^L
757 ! double precision, intent(in) :: w(ixI^S,1:nw),x(ixI^S,1:ndim)
758 ! double precision, intent(out) :: rho(ixI^S)
759
760 ! rho(ixO^S) = w(ixO^S,rho_) * eos%nH2rhoFactor
761
762 ! end subroutine hd_get_rho
763
764!!end th cond
765!!rad cool
766 subroutine rc_params_read(fl)
768 use mod_constants, only: bigdouble
769 use mod_basic_types, only: std_len
770 type(rc_fluid), intent(inout) :: fl
771 integer :: n
772 ! list parameters
773 integer :: ncool = 4000
774
775 !> Name of cooling curve
776 character(len=std_len) :: coolcurve='JCcorona'
777
778 !> Fixed temperature not lower than tlow
779 logical :: tfix=.false.
780
781 !> Lower limit of temperature
782 double precision :: tlow=bigdouble
783
784 !> Add cooling source in a split way (.true.) or un-split way (.false.)
785 logical :: rc_split=.false.
786
787 !> Cooling fraction (HEAD addition; used for explicit-mode dt scaling, kept for compat)
788 double precision :: cfrac=0.1d0
789
790 !> Master switch for radiative loss modification (spatial + density taper)
791 logical :: rad_modify=.false.
792 !> Apply spatial taper at both boundaries (default: lower only)
793 logical :: rad_modify_sym=.false.
794 !> Spatial taper: height from boundary below which taper applies
795 double precision :: rad_cut_hgt=0.0d0
796 !> Spatial taper: Gaussian decay width
797 double precision :: rad_cut_dey=0.15d0
798 !> Density taper: threshold above which taper applies
799 double precision :: rad_taper_rho=bigdouble
800 !> Density taper: Gaussian decay width
801 double precision :: rad_taper_dey=0.0d0
802 !> Suppress cooling below this temperature (Kelvin) within rad_cut_hgt.
803 !> Cells inside rad_cut_hgt with T < rad_suppress_temp get factor=0.
804 double precision :: rad_suppress_temp=0.0d0
805 !> Enable escape probability cooling modification
806 logical :: rad_escape_prob=.false.
807 !> Effective opacity for escape probability (code units)
808 double precision :: rad_kappa_eff=0.0d0
809 !> Temperature above which kappa goes to 0 (Kelvin); 0 = constant kappa
810 double precision :: rad_kappa_tcutoff=0.0d0
811 !> Sigmoid sharpness exponent for kappa(T) cutoff
812 double precision :: rad_kappa_alpha=4.0d0
813 !> Escape probability type: 'slab' or 'voigt'
814 character(len=10) :: rad_escape_type='slab'
815 !> Exponential cutoff scale: E *= exp(-tau/tau_cutoff); 0 = disabled
816 double precision :: rad_escape_tau_cutoff=0.0d0
817 !> Max height from footpoint for escape probability column mass (cm); 0 = no limit
818 double precision :: rad_escape_height=0.0d0
819 !> Variable-c_V Townsend extension (Y_mod): quadrature and sub-intervals
820 character(len=8) :: rc_y_mod_quadrature='boole'
821 integer :: rc_y_mod_n_sub=16
822 !> Upstream: cutoff radiative cooling below rad_damp_height (Gaussian damp)
823 logical :: rad_damp=.false.
824 double precision :: rad_damp_height=0.5d0
825 double precision :: rad_damp_scale=0.15d0
826 !> Upstream: Newton-radiative damping (surface treatment)
827 logical :: rad_newton=.false.
828 double precision :: rad_newton_trad=0.006d0
829 double precision :: rad_newton_rhosurf=1.d4
830 double precision :: rad_newton_pthick=25.d0
831
832 namelist /rc_list/ coolcurve, ncool, cfrac, tlow, tfix, rc_split, &
833 rad_modify, rad_modify_sym, rad_suppress_temp, &
834 rad_cut_hgt, rad_cut_dey, rad_taper_rho, rad_taper_dey, &
835 rad_escape_prob, rad_kappa_eff, rad_kappa_tcutoff, rad_kappa_alpha, &
836 rad_escape_type, rad_escape_tau_cutoff, rad_escape_height, &
837 rc_y_mod_quadrature, rc_y_mod_n_sub, &
838 rad_damp, rad_damp_height, rad_damp_scale, &
839 rad_newton, rad_newton_trad, rad_newton_rhosurf, rad_newton_pthick
840
841 do n = 1, size(par_files)
842 open(unitpar, file=trim(par_files(n)), status="old")
843 read(unitpar, rc_list, end=111)
844111 close(unitpar)
845 end do
846
847 fl%ncool=ncool
848 fl%coolcurve=coolcurve
849 fl%tlow=tlow
850 fl%Tfix=tfix
851 fl%rc_split=rc_split
852 fl%cfrac=cfrac
853 fl%rad_modify=rad_modify
854 fl%rad_modify_sym=rad_modify_sym
855 fl%rad_suppress_temp=rad_suppress_temp
856 fl%rad_cut_hgt=rad_cut_hgt
857 fl%rad_cut_dey=rad_cut_dey
858 fl%rad_taper_rho=rad_taper_rho
859 fl%rad_taper_dey=rad_taper_dey
860 fl%rad_escape_prob=rad_escape_prob
861 fl%rad_kappa_eff=rad_kappa_eff
862 fl%rad_kappa_Tcutoff=rad_kappa_tcutoff/unit_temperature
863 fl%rad_kappa_alpha=rad_kappa_alpha
864 fl%rad_escape_type=rad_escape_type
865 fl%rad_escape_tau_cutoff=rad_escape_tau_cutoff
866 fl%rad_escape_height=rad_escape_height/unit_length
867 fl%Y_mod_quadrature=rc_y_mod_quadrature
868 fl%Y_mod_N_sub=rc_y_mod_n_sub
869 fl%rad_damp = rad_damp
870 fl%rad_damp_height = rad_damp_height
871 fl%rad_damp_scale = rad_damp_scale
872 fl%rad_newton = rad_newton
873 fl%rad_newton_trad = rad_newton_trad
874 fl%rad_newton_rhosurf = rad_newton_rhosurf
875 fl%rad_newton_pthick = rad_newton_pthick
876 end subroutine rc_params_read
877!! end rad cool
878
881 use mod_geometry, only: coordinate
884 use mod_particles, only: npayload,nusrpayload,ngridvars,num_particles,physics_type_particles
885 use mod_fld
886
887 double precision :: a,b,xfrac,yfrac
888
889 ! Initialize particles module, put here so additional gridvars and user payloads are known
890 if (hd_particles) then
891 call particles_init()
892 end if
893
894 if (hd_radiative_transfer) call rt_sc_setup()
895
896 if (.not. hd_energy) then
897 if (eos%gamma <= 0.0d0) call mpistop ("Error: eos%gamma <= 0")
898 if (hd_adiab < 0.0d0) call mpistop ("Error: hd_adiab < 0")
900 else
901 if (eos%gamma <= 0.0d0 .or. eos%gamma == 1.0d0) &
902 call mpistop ("Error: eos%gamma <= 0 or eos%gamma == 1.0")
903 ! For LTE+ionE, this floor excludes ionisation energy. At tlow (~1000 K)
904 ! ionisation is negligible, so the thermal-only floor is adequate.
905 small_e = small_pressure * eos%inv_gamma_minus_1
906 small_r_e = small_pressure * eos%inv_gamma_minus_1
907 ! gamma_minus_1 and inv_gamma_minus_1 are set by eos_init
908 end if
909
910 if (hd_dust) call dust_check_params()
911
912 if(hd_dust_implicit) then
913 if(.not.use_imex_scheme)then
914 call mpistop('select IMEX scheme for implicit dust update')
915 endif
916 ! implicit dust update
917 phys_implicit_update => dust_implicit_update
918 phys_evaluate_implicit => dust_evaluate_implicit
919 endif
920
921 ! Hyperbolic TC in HD is only implemented for 1D: q is a scalar carrying
922 ! the heat flux along the single spatial direction. For ndim>1 the user
923 ! should use the ffHD module (B-aligned conduction) or the parabolic
924 ! tc_init path. Restrict here rather than silently giving wrong fluxes.
925 if (hd_hyperbolic_thermal_conduction .and. ndim /= 1) then
926 call mpistop("hd_hyperbolic_thermal_conduction is implemented for ndim=1 only;" // &
927 " for ndim>1 use mod_ffhd or parabolic mod_thermal_conduction.")
928 end if
930 call mpistop("hd_hyperbolic_thermal_conduction and hd_thermal_conduction are mutually exclusive;" // &
931 " choose one TC implementation.")
932 end if
933
934 if(hd_radiation_fld) then
935 if(.not.use_imex_scheme)then
936 call mpistop('select IMEX scheme for FLD radiation use')
937 endif
938 if(use_multigrid)then
939 call phys_set_mg_bounds()
940 else
941 if(.not.fld_no_mg)call mpistop('multigrid must have BCs for IMEX and FLD radiation use')
942 endif
943 if(mype==0)then
944 write(*,*)'==FLD SETUP======================'
945 write(*,*)'Using FLD with settings:'
946 write(*,*)'Using FLD with settings: hd_radiation_fld=',hd_radiation_fld
947 write(*,*)'Using FLD with settings: hd_fld_pradtensor=',hd_fld_pradtensor
948 write(*,*)'Using FLD with settings: fld_fluxlimiter=',fld_fluxlimiter
949 write(*,*)'Using FLD with settings: fld_bound_diff=',fld_bound_diff
950 write(*,*)'Using FLD with settings: fld_interaction_method=',fld_interaction_method
951 write(*,*)'Using FLD with settings: fld_opacity_law=',fld_opacity_law
952 write(*,*)'Using FLD with settings: fld_kappa0=',fld_kappa0
953 write(*,*)'Using FLD with settings: fld_opal_table=',fld_opal_table
954 write(*,*)'Using FLD with settings: fld_Radforce_split=',fld_radforce_split
955 write(*,*)'Using FLD with settings: fld_bisect_tol=',fld_bisect_tol
956 write(*,*)'Using FLD with settings: fld_diff_tol=',fld_diff_tol
957 write(*,*)'Using FLD with settings: nth_for_diff_mg=',nth_for_diff_mg
958 write(*,*)' FLD has use_imex_scheme and use_multigrid=',use_imex_scheme,use_multigrid
959 write(*,*)' FLD has fld_no_mg=',fld_no_mg
960 if(fld_no_mg)then
961 print *,'WARNING: cheating with FLD diffusion ***********************'
962 print *,'WARNING: No MG-diffusion for radiative energy at all!!!!!!!'
963 print *,'WARNING: cheating with FLD diffusion ***********************'
964 endif
965 print *,'const_rad_a =',const_rad_a
966 print *,'NORMALIZED arad_norm=',arad_norm
967 print *,'NORMALIZED c_norm=',c_norm
968 if(fld_cnorm>0.0d0)then
969 print *,'WARNING: cheating with c_norm ***********************'
970 print *,'WARNING: c_norm reset to=',fld_cnorm
972 print *,'WARNING: cheating with c_norm ***********************'
973 endif
974 print *,'const_kappae =',const_kappae
975 if(trim(fld_opacity_law).eq.'const_norm')then
976 print *,'NORMALIZED fld_kappa0 =',fld_kappa0
977 print *,'physical value (in cgs or SI) =',fld_kappa0*unit_opacity
978 endif
979 if(trim(fld_opacity_law).eq.'const')then
980 print *,'physical fld_kappa (in cgs or SI) =',fld_kappa0
981 print *,'NORMALIZED value =',fld_kappa0/unit_opacity
982 endif
983 write(*,*)'===FLD SETUP====================='
984 endif
985 endif
986 if(mype==0)then
987 write(*,*)'====HD run with settings===================='
988 write(*,*)'Using mod_hd_phys with settings:'
989 write(*,*)'SI_unit=',si_unit
990 write(*,*)'Dimensionality :',ndim
991 write(*,*)'vector components:',ndir
992 write(*,*)'coordinate set to type,slab:',coordinate,slab
993 write(*,*)'number of variables nw=',nw
994 write(*,*)' start index iwstart=',iwstart
995 write(*,*)'number of vector variables=',nvector
996 write(*,*)'number of stagger variables nws=',nws
997 write(*,*)'number of variables with BCs=',nwgc
998 write(*,*)'number of vars with fluxes=',nwflux
999 write(*,*)'number of vars with flux + BC=',nwfluxbc
1000 write(*,*)'number of auxiliary variables=',nwaux
1001 write(*,*)'number of extra vars without flux=',nwextra
1002 write(*,*)'number of extra vars for wextra=',nw_extra
1003 write(*,*)'number of auxiliary I/O variables=',nwauxio
1004 write(*,*)'number of hd_n_tracer=',hd_n_tracer
1005 write(*,*)' hd_energy=',hd_energy
1006 write(*,*)' hd_gravity=',hd_gravity
1007 write(*,*)' hd_viscosity=',hd_viscosity
1008 write(*,*)' hd_radiative_cooling=',hd_radiative_cooling
1009 write(*,*)' hd_cak_force=',hd_cak_force
1010 write(*,*)' hd_radiation_fld=',hd_radiation_fld
1011 write(*,*)' hd_thermal_conduction=',hd_thermal_conduction
1012 write(*,*)' hd_hyperbolic_thermal_conduction=',hd_hyperbolic_thermal_conduction
1013 write(*,*)' hd_trac=',hd_trac
1014 write(*,*)' hd_dust=',hd_dust
1015 write(*,*)' hd_rotating_frame=',hd_rotating_frame
1016 write(*,*)' hd_particles=',hd_particles
1017 if(hd_particles) then
1018 write(*,*) '*****Using particles: npayload,ngridvars :', npayload,ngridvars
1019 write(*,*) '*****Using particles: nusrpayload :', nusrpayload
1020 write(*,*) '*****Using particles: num_particles :', num_particles
1021 write(*,*) '*****Using particles: physics_type_particles=',physics_type_particles
1022 end if
1023 write(*,*)'number of ghostcells=',nghostcells
1024 write(*,*)'number due to phys_wider_stencil=',phys_wider_stencil
1025 write(*,*)'==========================================='
1026 print *,'========EOS and UNITS==========='
1027 print *,'SI_unit =',si_unit
1028 print *,'gamma=',eos%gamma
1029 print *,'He_abundance =',eos%He_abundance
1030 print *,'RR =',rr
1031 print *,'========EOS and UNITS==========='
1032 print *,'unit_time =',unit_time
1033 print *,'unit_length =',unit_length
1034 print *,'unit_velocity =',unit_velocity
1035 print *,'unit_pressure =',unit_pressure
1036 print *,'unit_numberdensity =',unit_numberdensity
1037 print *,'unit_density =',unit_density
1038 print *,'unit_temperature =',unit_temperature
1039 print *,'unit_mass =',unit_mass
1040 print *,'unit_Erad =',unit_erad
1041 print *,'unit_radflux =',unit_radflux
1042 print *, 'CHECK that p_u ',unit_pressure,' equals ',unit_density*unit_velocity**2
1043 print *, 'CHECK that L_u ',unit_length,' equals ',unit_velocity*unit_time
1044 print *, 'CHECK that M_u',unit_mass,' equals ',unit_density*unit_length**3
1045 print *, 'density to numberdensity has factor ',unit_density/unit_numberdensity
1046 if(si_unit)then
1047 print *, ' compare this to ',mp_si*(1.d0+4.d0*eos%He_abundance)
1048 else
1049 print *, ' compare this to ',mp_cgs*(1.d0+4.d0*eos%He_abundance)
1050 endif
1051 print *, 'pressure to n T has factor ',unit_pressure/(unit_numberdensity*unit_temperature)
1052 if(si_unit)then
1053 print *, ' compare this to ',kb_si*(2.d0+3.d0*eos%He_abundance)
1056 else
1057 print *, ' compare this to ',kb_cgs*(2.d0+3.d0*eos%He_abundance)
1060 endif
1061 if(eos%eos_type /= 'LTE')then
1062 print *, 'mean molecular weight mu is =',a/b,' = ', (1.d0+4.d0*eos%He_abundance)/(2.d0+3.d0*eos%He_abundance)
1063 xfrac=1.d0/a
1064 yfrac=4.d0*eos%He_abundance/(1.d0+4.d0*eos%He_abundance)
1065 print *, 'mass fraction hydrogen X is =',1/a,' and this equals ', 1.d0/(1.d0+4.d0*eos%He_abundance)
1066 print *, 'mass fraction helium Y is =',yfrac
1067 print *, ' check that 1/mu', b/a,' is equal to 2X+3Y/4=',2.d0*xfrac+3.d0*yfrac/4.d0
1068 print *, ' ratio n_e/n_p=',1.d0+2.0d0*eos%He_abundance
1069 endif
1070 print *,'========UNITS==========='
1071 endif
1072
1073 end subroutine hd_check_params
1074
1075 subroutine hd_physical_units
1077 double precision :: mp,kb,c_lightspeed,xfrac,sigma_telectron
1078 double precision :: a,b
1079 ! Derive scaling units
1080 if(si_unit) then
1081 mp=mp_si
1082 kb=kb_si
1083 const_sigmasb=sigma_sb_si
1084 c_lightspeed=c_si
1085 sigma_telectron=sigma_te_si
1086 else
1087 mp=mp_cgs
1088 kb=kb_cgs
1089 const_sigmasb=sigma_sb_cgs
1090 c_lightspeed=const_c
1091 sigma_telectron=sigma_te_cgs
1092 end if
1093 ! Normalisation dispatch keyed solely on eos%eos_type (FI is the default, so
1094 ! legacy parfiles land in the FI/PI absorbed-(a,b), RR=1 branch -- the former
1095 ! eq_state_units=.true. result).
1096 if (eos%eos_type == 'LTE') then
1097 !> Remove the assumed FI normalisation from the units and handle in EoS
1098 a=1d0
1099 b=1d0
1100 eos%nH2rhoFactor = 1d0+4d0*eos%He_abundance
1101 rr=(2d0+3d0*eos%He_abundance) / (1d0+4d0*eos%He_abundance)
1102 xfrac=1.d0/(1.d0+4.d0*eos%He_abundance)
1103 else
1104 !> FI / PI: absorbed-(a,b), RR=1 (a=b=1 with RR=1 would be wrong physics
1105 !> for He>0). PI shares FI's normalisation exactly.
1106 a=1d0+4d0*eos%He_abundance
1107 if(eos%eos_type=='PI') then
1108 b=1d0+h_ion_fr+eos%He_abundance*(he_ion_fr*(he_ion_fr2+1d0)+1d0)
1109 else
1110 b=2d0+3d0*eos%He_abundance
1111 end if
1112 rr=1d0
1113 xfrac=1.d0/a
1114 end if
1115 if(unit_density/=1.d0 .or. unit_numberdensity/=1.d0) then
1116 if(unit_density/=1.d0) then
1118 else if(unit_numberdensity/=1.d0) then
1120 end if
1121 if(unit_temperature/=1.d0) then
1124 if(unit_length/=1.d0) then
1126 else if(unit_time/=1.d0) then
1128 end if
1129 else if(unit_pressure/=1.d0) then
1132 if(unit_length/=1.d0) then
1134 else if(unit_time/=1.d0) then
1136 end if
1137 else if(unit_velocity/=1.d0) then
1140 if(unit_length/=1.d0) then
1142 else if(unit_time/=1.d0) then
1144 end if
1145 else if(unit_time/=1.d0) then
1149 end if
1150 else if(unit_temperature/=1.d0) then
1151 ! units of temperature and velocity are dependent
1152 if(unit_pressure/=1.d0) then
1156 if(unit_length/=1.d0) then
1158 else if(unit_time/=1.d0) then
1160 end if
1161 end if
1162 else if(unit_pressure/=1.d0) then
1163 if(unit_velocity/=1.d0) then
1167 if(unit_length/=1.d0) then
1169 else if(unit_time/=1.d0) then
1171 end if
1172 else if(unit_time/=0.d0) then
1177 end if
1178 end if
1180
1181 !> Units needed for radiative flux and opacity as used in FLD
1182 ! normalized light speed
1183 c_norm=c_lightspeed/unit_velocity
1184 ! this is the radiation constant in either cgs or SI units
1185 const_rad_a=4.d0*const_sigmasb/c_lightspeed
1186 ! this is the dimensionless conversion factor for Erad to Trad
1188 ! This is the Thomson scattering opacity in the correct units
1189 ! hydrogen mass fraction X=1/a in the absorbed-(a,b) normalisation
1190 const_kappae=sigma_telectron*(1.d0+xfrac)/(2.0d0*mp)
1191 ! these are the units
1195
1196 end subroutine hd_physical_units
1197
1198 !> Returns logical argument flag where values are ok
1199 subroutine hd_check_w(primitive, ixI^L, ixO^L, w, flag)
1201 use mod_dust, only: dust_check_w
1202
1203 logical, intent(in) :: primitive
1204 integer, intent(in) :: ixi^l, ixo^l
1205 double precision, intent(in) :: w(ixi^s, nw)
1206 logical, intent(inout) :: flag(ixi^s,1:nw)
1207
1208 double precision :: tmp(ixi^s)
1209 double precision :: x(ixi^s, 1:ndim)
1210
1211 flag=.false.
1212 x(ixi^s,1:ndim)=block%x(ixi^s,1:ndim) !> Rather than redefining the hd_check_w and limiter procedure interfaces
1213 if (hd_energy) then
1214 if (primitive) then
1215 where(w(ixo^s, e_) < small_pressure) flag(ixo^s,e_) = .true.
1216 else
1217 ! Inline (gamma-1)*(e - KE) to avoid eos%get_thermal_pressure side
1218 ! effects (fix_small_values clipping / crash=.true.) that would
1219 ! suppress the flag or abort before remediation could run.
1220 tmp(ixo^s)=(eos%gamma-1.0d0)*(w(ixo^s,e_)-&
1221 half*(^c&w(ixo^s,m^c_)**2+)/w(ixo^s,rho_))
1222 where(tmp(ixo^s) < small_pressure) flag(ixo^s,e_) = .true.
1223 endif
1224 if(hd_radiation_fld)then
1225 where(w(ixo^s, r_e) < small_r_e) flag(ixo^s,r_e) = .true.
1226 endif
1227 end if
1228
1229 where(w(ixo^s, rho_) < small_density) flag(ixo^s,rho_) = .true.
1230
1231 if(hd_dust) call dust_check_w(ixi^l,ixo^l,w,x,flag)
1232
1233 end subroutine hd_check_w
1234
1235 subroutine hd_bound_fip(primitive, ixI^L, ixO^L, w)
1237 logical, intent(in) :: primitive
1238 integer, intent(in) :: ixi^l, ixo^l
1239 double precision, intent(inout) :: w(ixi^s,1:nw)
1240
1241 double precision :: rho_safe(ixi^s), fip_prim(ixi^s)
1242
1243 if (.not. hd_fip) return
1244
1245 if (primitive) then
1246 w(ixo^s,fip_) = min(maxfip, max(minfip, w(ixo^s,fip_)))
1247 else
1248 rho_safe(ixo^s) = max(w(ixo^s,rho_), small_density)
1249 fip_prim(ixo^s) = w(ixo^s,fip_) / rho_safe(ixo^s)
1250 fip_prim(ixo^s) = min(maxfip, max(minfip, fip_prim(ixo^s)))
1251 w(ixo^s,fip_) = rho_safe(ixo^s) * fip_prim(ixo^s)
1252 end if
1253 end subroutine hd_bound_fip
1254
1255 ! !> Transform primitive variables into conservative ones
1256 ! subroutine hd_to_conserved(ixI^L, ixO^L, w, x)
1257 ! use mod_global_parameters
1258 ! use mod_dust, only: dust_to_conserved
1259 ! integer, intent(in) :: ixI^L, ixO^L
1260 ! double precision, intent(inout) :: w(ixI^S, nw)
1261 ! double precision, intent(in) :: x(ixI^S, 1:ndim)
1262
1263 ! integer :: ix^D
1264
1265 ! {do ix^DB=ixOmin^DB,ixOmax^DB\}
1266 ! if (hd_energy) then
1267 ! ! Calculate total energy from pressure and kinetic energy
1268 ! w(ix^D,e_)=w(ix^D, e_)*inv_gamma_1+&
1269 ! half*(^C&w(ix^D,m^C_)**2+)*w(ix^D,rho_)
1270 ! end if
1271 ! ! Convert velocity to momentum
1272 ! ^C&w(ix^D,m^C_)=w(ix^D,rho_)*w(ix^D,m^C_)\
1273 ! {end do\}
1274
1275 ! if (hd_dust) then
1276 ! call dust_to_conserved(ixI^L, ixO^L, w, x)
1277 ! end if
1278
1279 ! end subroutine hd_to_conserved
1280
1281 ! !> Transform conservative variables into primitive ones
1282 ! subroutine hd_to_primitive(ixI^L, ixO^L, w, x)
1283 ! use mod_global_parameters
1284 ! use mod_dust, only: dust_to_primitive
1285 ! integer, intent(in) :: ixI^L, ixO^L
1286 ! double precision, intent(inout) :: w(ixI^S, nw)
1287 ! double precision, intent(in) :: x(ixI^S, 1:ndim)
1288
1289 ! double precision :: inv_rho
1290 ! integer :: ix^D
1291
1292 ! if (fix_small_values) then
1293 ! call hd_handle_small_values(.false., w, x, ixI^L, ixO^L, 'hd_to_primitive')
1294 ! end if
1295
1296 ! {do ix^DB=ixOmin^DB,ixOmax^DB\}
1297 ! inv_rho = 1.d0/w(ix^D,rho_)
1298 ! ! Convert momentum to velocity
1299 ! ^C&w(ix^D,m^C_)=w(ix^D,m^C_)*inv_rho\
1300 ! ! Calculate pressure = (gamma-1) * (e-ek)
1301 ! if(hd_energy) then
1302 ! ! Compute pressure
1303 ! w(ix^D,p_)=(hd_gamma-1.d0)*(w(ix^D,e_)&
1304 ! -half*w(ix^D,rho_)*(^C&w(ix^D,m^C_)**2+))
1305 ! end if
1306 ! {end do\}
1307
1308 ! ! Convert dust momentum to dust velocity
1309 ! if (hd_dust) then
1310 ! call dust_to_primitive(ixI^L, ixO^L, w, x)
1311 ! end if
1312
1313 ! end subroutine hd_to_primitive
1314
1315 !> Transform internal energy to total energy
1316 subroutine hd_ei_to_e(ixI^L,ixO^L,w,x)
1318 integer, intent(in) :: ixi^l, ixo^l
1319 double precision, intent(inout) :: w(ixi^s, nw)
1320 double precision, intent(in) :: x(ixi^s, 1:ndim)
1321
1322 ! Calculate total energy from internal and kinetic energy
1323 w(ixo^s,e_)=w(ixo^s,e_)+half*(^c&w(ixo^s,m^c_)**2+)/w(ixo^s,rho_)
1324
1325 end subroutine hd_ei_to_e
1326
1327 !> Transform total energy to internal energy
1328 subroutine hd_e_to_ei(ixI^L,ixO^L,w,x)
1330 integer, intent(in) :: ixi^l, ixo^l
1331 double precision, intent(inout) :: w(ixi^s, nw)
1332 double precision, intent(in) :: x(ixi^s, 1:ndim)
1333
1334 ! Calculate ei = e - ek
1335 w(ixo^s,e_)=w(ixo^s,e_)-half*(^c&w(ixo^s,m^c_)**2+)/w(ixo^s,rho_)
1336
1337 end subroutine hd_e_to_ei
1338
1339 !> Wrapper: e_to_ei + cache log10(nH) in wextra for LTE TC fast path.
1340 !> During STS substeps density is invariant, so log10(nH) is computed once
1341 !> per STS cycle (in sts_before_first_cycle hook) and reused across all substeps.
1342 subroutine hd_e_to_ei_and_cache_log_nh(ixI^L,ixO^L,w,x)
1344 integer, intent(in) :: ixi^l, ixo^l
1345 double precision, intent(inout) :: w(ixi^s, nw)
1346 double precision, intent(in) :: x(ixi^s, 1:ndim)
1347
1348 call hd_e_to_ei(ixi^l,ixo^l,w,x)
1349 block%wextra(ixo^s, iw_log_nh) = dlog10(w(ixo^s, rho_) / eos%nH2rhoFactor)
1350 end subroutine hd_e_to_ei_and_cache_log_nh
1351
1352 !> Calculate internal energy from total energy (non-modifying version)
1353 function hd_get_ei(w, ixI^L, ixO^L) result(ei)
1355 integer, intent(in) :: ixi^l, ixo^l
1356 double precision, intent(in) :: w(ixi^s, nw)
1357 double precision :: ei(ixo^s)
1358
1359 ! ei = e_total - e_kinetic
1360 ei(ixo^s) = w(ixo^s,e_) - half*(^c&w(ixo^s,m^c_)**2+)/w(ixo^s,rho_)
1361 end function hd_get_ei
1362
1363 !> Calculate v_i = m_i / rho within ixO^L
1364 subroutine hd_get_v_idim(w, x, ixI^L, ixO^L, idim, v)
1366 integer, intent(in) :: ixi^l, ixo^l, idim
1367 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s, 1:ndim)
1368 double precision, intent(out) :: v(ixi^s)
1369
1370 v(ixo^s) = w(ixo^s, mom(idim)) / w(ixo^s, rho_)
1371 end subroutine hd_get_v_idim
1372
1373 !> Calculate velocity vector v_i = m_i / rho within ixO^L
1374 subroutine hd_get_v(w,x,ixI^L,ixO^L,v)
1376
1377 integer, intent(in) :: ixi^l, ixo^l
1378 double precision, intent(in) :: w(ixi^s,nw), x(ixi^s,1:^nd)
1379 double precision, intent(out) :: v(ixi^s,1:ndir)
1380
1381 integer :: idir
1382
1383 do idir=1,ndir
1384 v(ixo^s,idir) = w(ixo^s, mom(idir)) / w(ixo^s, rho_)
1385 end do
1386
1387 end subroutine hd_get_v
1388
1389 !> Calculate cmax_idim = csound + abs(v_idim) within ixO^L
1390 subroutine hd_get_cmax(w, x, ixI^L, ixO^L, idim, cmax)
1392 use mod_dust, only: dust_get_cmax_prim
1394
1395 integer, intent(in) :: ixi^l, ixo^l, idim
1396 ! w in primitive form
1397 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s, 1:ndim)
1398 double precision, intent(inout) :: cmax(ixi^s)
1399 double precision :: csound2(ixi^s)
1400
1401 if(hd_energy) then
1402 call eos%get_csound2(w, x, ixi^l, ixo^l, csound2)
1403 cmax(ixo^s)=dabs(w(ixo^s,mom(idim)))+dsqrt(csound2(ixo^s))
1404 else
1405 if (.not. associated(usr_set_pthermal)) then
1406 cmax(ixo^s) = hd_adiab * w(ixo^s, rho_)**eos%gamma
1407 else
1408 call usr_set_pthermal(w,x,ixi^l,ixo^l,cmax)
1409 end if
1410 cmax(ixo^s)=dabs(w(ixo^s,mom(idim)))+dsqrt(eos%gamma*cmax(ixo^s)/w(ixo^s,rho_))
1411 end if
1412
1413 if (hd_dust) then
1414 call dust_get_cmax_prim(w, x, ixi^l, ixo^l, idim, cmax)
1415 end if
1416 end subroutine hd_get_cmax
1417
1418 !> get adaptive cutoff temperature for TRAC (Johnston 2019 ApJL, 873, L22)
1419 subroutine hd_get_tcutoff(ixI^L,ixO^L,w,x,tco_local,Tmax_local)
1423 use mod_eos, only: eos
1424 use mod_geometry, only: gradient
1425 integer, intent(in) :: ixi^l,ixo^l
1426 double precision, intent(in) :: x(ixi^s,1:ndim)
1427 ! in primitive form
1428 double precision, intent(inout) :: w(ixi^s,1:nw)
1429 double precision, intent(out) :: tco_local, tmax_local
1430
1431 double precision, parameter :: trac_delta=0.25d0
1432 double precision :: tmp1(ixi^s),te(ixi^s),lts(ixi^s), r(ixi^s)
1433 double precision :: ltrc,ltrp
1434 integer :: jxo^l,hxo^l
1435 integer :: jxp^l,hxp^l,ixp^l
1436 logical :: lrlt(ixi^s)
1437 ! Johnston 2021 type 7 variables
1438 double precision :: dtdx, l_t, a_coeff, l1, cooling, net_cool
1439 double precision :: kappa_par, disc, kappa_trac, kappa_eff, tcoff_eff
1440 double precision :: dx_over_delta, v_abs, v_n, gnorm, dl_eff
1441 double precision :: q_heat(ixi^s), ne(ixi^s), nh_arr(ixi^s)
1442 double precision :: gradtd(ixi^s,1:ndim), nhat(1:ndim)
1443 integer :: ix^d, idims
1444
1445 call eos%get_Rfactor(w,x,ixi^l,ixi^l,r)
1446 te(ixi^s)=w(ixi^s,p_)/(r(ixi^s)*w(ixi^s,rho_))
1447
1448 if (eos%eos_type == 'LTE') then
1449 te(ixi^s) = w(ixi^s, te_)
1450 endif
1451
1452 tco_local=zero
1453 tmax_local=maxval(te(ixo^s))
1454 select case(hd_trac_type)
1455 {^ifoned
1456 case(0)
1458 ! below trac_T_floor conductivity, losses and heating are left unmodified (Tcoff = Te)
1459 where(te(ixi^s) < tc_fl%trac_T_floor) block%wextra(ixi^s,tcoff_)=te(ixi^s)
1460 case(1)
1461 hxo^l=ixo^l-1;
1462 jxo^l=ixo^l+1;
1463 lts(ixo^s)=0.5d0*dabs(te(jxo^s)-te(hxo^s))/te(ixo^s)
1464 lrlt=.false.
1465 where(lts(ixo^s) > trac_delta)
1466 lrlt(ixo^s)=.true.
1467 end where
1468 if(any(lrlt(ixo^s))) then
1469 tco_local=maxval(te(ixo^s), mask=lrlt(ixo^s))
1470 end if
1471 case(2)
1472 !> iijima et al. 2021, LTRAC method
1473 ltrc=1.5d0
1474 ltrp=2.5d0
1475 ixp^l=ixo^l^ladd1;
1476 hxo^l=ixo^l-1;
1477 jxo^l=ixo^l+1;
1478 hxp^l=ixp^l-1;
1479 jxp^l=ixp^l+1;
1480 lts(ixp^s)=0.5d0*abs(te(jxp^s)-te(hxp^s))/te(ixp^s)
1481 lts(ixp^s)=max(one, (exp(lts(ixp^s))/ltrc)**ltrp)
1482 ! Smoothed Tcoff for interior cells
1483 lts(ixo^s)=0.25d0*(lts(jxo^s)+two*lts(ixo^s)+lts(hxo^s))
1484 block%wextra(ixo^s,tcoff_)=te(ixo^s)*lts(ixo^s)**0.4d0
1485 ! Fill one ghost cell on each side with unsmoothed Tcoff.
1486 ! The thermal conduction routine reads Tcoff at ixO +/- 1;
1487 ! wextra ghost cells are NOT halo-communicated, so without this
1488 ! the conduction sees stale values and breaks symmetry.
1489 block%wextra(ixomin1-1,tcoff_)=te(ixomin1-1)*lts(ixomin1-1)**0.4d0
1490 block%wextra(ixomax1+1,tcoff_)=te(ixomax1+1)*lts(ixomax1+1)**0.4d0
1491 }
1492 case(7)
1493 {^ifoned
1494 !> Johnston et al. 2021 local TRAC (A&A 654, A2) -- 1D
1495 !> Per-cell kappa_TRAC from steady-state energy balance
1496
1497 ! Tcoff also on the first ghost layer, so both blocks at a face see the same conductivity
1498 ixp^l=ixo^l^ladd1;
1499
1500 ! Get background heating Q (once per block)
1501 call usr_get_heating(q_heat, ixi^l, ixp^l, w, x)
1502
1503 ! Get n_e and n_H for cooling rate: Q = n_e * n_H * Lambda(T)
1504 ! For FI: n_e = n_H * neOnH_FI. For LTE: n_e from Saha EoS.
1505 call eos%get_ne_nH(ixi^l, ixp^l, w, x, ne, nh_arr)
1506
1507 hxo^l=ixo^l-1;
1508 jxo^l=ixo^l+1;
1509 dx_over_delta = dxlevel(1) / hd_trac_delta
1510
1511 do ix1=ixpmin1,ixpmax1
1512 ! Temperature gradient: L_T = T / abs(dT/dx)
1513 dtdx = abs(te(ix1+1) - te(ix1-1)) / (2.d0 * dxlevel(1))
1514 ! Uniform temperature, or below trac_T_floor: no broadening (Tcoff = Te)
1515 if(dtdx < smalldouble .or. te(ix1) < tc_fl%trac_T_floor) then
1516 block%wextra(ix1,tcoff_) = te(ix1)
1517 cycle
1518 end if
1519 l_t = te(ix1) / dtdx
1520
1521 ! Eq.11 mass flux coefficient a = (5/2) p v / T, which is (5/2) kB J for
1522 ! an ideal gas since p = n kB T.
1523 v_abs = abs(w(ix1,m1_))
1524 a_coeff = 2.5d0 * w(ix1,p_) * v_abs / te(ix1)
1525
1526 ! Radiative cooling: n_e * n_H * Lambda(T), guarded to the table range (positive in-range
1527 ! test so a non-finite Te falls to zero rather than indexing findL with int(NaN)).
1528 if(te(ix1) > rc_fl%tcoolmin .and. te(ix1) < rc_fl%tcoolmax) then
1529 call findl(te(ix1), l1, rc_fl); cooling = ne(ix1) * nh_arr(ix1) * l1
1530 else if(te(ix1) >= rc_fl%tcoolmax) then
1531 call calc_l_extended(te(ix1), l1, rc_fl); cooling = ne(ix1) * nh_arr(ix1) * l1
1532 else
1533 cooling = 0.d0
1534 end if
1535
1536 ! Net cooling - heating
1537 net_cool = abs(cooling - q_heat(ix1))
1538
1539 ! Spitzer conductivity at this T
1540 kappa_par = tc_fl%tc_k_para * te(ix1)**2.5d0
1541
1542 ! Johnston Eq. 11 discriminant
1543 disc = a_coeff**2 + 4.d0 * tc_fl%tc_k_para * te(ix1)**1.5d0 * net_cool
1544
1545 if(l_t <= 2.d0 * dx_over_delta) then
1546 ! Under-resolved: full TRAC formula (Eq. 11)
1547 kappa_trac = (a_coeff + dsqrt(disc)) / (2.d0 / dx_over_delta)
1548 else
1549 ! Over-resolved: limiter only (Eq. 12, drops mass flux)
1550 kappa_trac = dsqrt(4.d0 * tc_fl%tc_k_para * te(ix1)**1.5d0 * net_cool) &
1551 / (2.d0 / dx_over_delta)
1552 end if
1553
1554 ! kappa' = max(kappa_TRAC, kappa_par)
1555 kappa_eff = max(kappa_trac, kappa_par)
1556
1557 ! Convert to effective Tcoff: Tcoff = (kappa'/kappa_0)**(2/5)
1558 tcoff_eff = (kappa_eff / tc_fl%tc_k_para)**0.4d0
1559
1560 ! Store max(Te, Tcoff_eff) -- Tcoff must be >= Te
1561 block%wextra(ix1,tcoff_) = max(te(ix1), tcoff_eff)
1562 end do
1563 }
1564 {^nooned
1565 ! Johnston et al. 2021 local TRAC, A and A 654 A2 -- multi-D, per-cell.
1566 ! Isotropic Spitzer conduction, so the TR normal is the unit temperature
1567 ! gradient direction nhat, and the broadening is applied along it.
1568 call usr_get_heating(q_heat, ixi^l, ixo^l, w, x)
1569 call eos%get_ne_nH(ixi^l, ixo^l, w, x, ne, nh_arr)
1570 ! default (incl. non-communicated ghost layer): Tcoff=Te -> no broadening; interior overwritten
1571 block%wextra(ixi^s,tcoff_) = te(ixi^s)
1572 do idims=1,ndim
1573 call gradient(te,ixi^l,ixo^l,idims,gradtd(ixi^s,idims))
1574 end do
1575 {do ix^db=ixomin^db,ixomax^db\}
1576 gnorm = dsqrt(^d&gradtd({ix^d},^d)**2+ )
1577 if(gnorm < smalldouble .or. te(ix^d) < tc_fl%trac_T_floor) then
1578 ! locally uniform T, or below trac_T_floor: no broadening
1579 block%wextra(ix^d,tcoff_) = te(ix^d)
1580 else
1581 ^d&nhat(^d)=gradtd({ix^d},^d)/gnorm\
1582 ! grid spacing measured along the gradient direction
1583 dl_eff = 1.d0/dsqrt(^d&(nhat(^d)/block%ds({ix^d},^d))**2+ )
1584 l_t = te(ix^d)/gnorm
1585 ! Eq.16 enthalpy (mass) flux along the gradient
1586 v_n = ^d&w({ix^d},mom(^d))*nhat(^d)+
1587 a_coeff = 2.5d0*w(ix^d,p_)*dabs(v_n)/te(ix^d)
1588 ! optically-thin cooling n_e n_H Lambda(T), guarded to the table range with a positive
1589 ! in-range test so a non-finite Te (NaN from an under-resolved collapse) falls to zero
1590 ! rather than indexing findL with int(NaN) -> out of bounds.
1591 if(te(ix^d) > rc_fl%tcoolmin .and. te(ix^d) < rc_fl%tcoolmax) then
1592 call findl(te(ix^d),l1,rc_fl); cooling = l1*ne(ix^d)*nh_arr(ix^d)
1593 else if(te(ix^d) >= rc_fl%tcoolmax) then
1594 call calc_l_extended(te(ix^d),l1,rc_fl); cooling = l1*ne(ix^d)*nh_arr(ix^d)
1595 else
1596 cooling = 0.d0
1597 end if
1598 net_cool = dabs(cooling-q_heat(ix^d))
1599 kappa_par = tc_fl%tc_k_para*te(ix^d)**2.5d0
1600 disc = a_coeff**2 + 4.d0*tc_fl%tc_k_para*te(ix^d)**1.5d0*net_cool
1601 dx_over_delta = dl_eff/hd_trac_delta
1602 if(l_t <= 2.d0*dx_over_delta) then
1603 kappa_trac = (a_coeff+dsqrt(disc))/(2.d0/dx_over_delta)
1604 else
1605 kappa_trac = dsqrt(4.d0*tc_fl%tc_k_para*te(ix^d)**1.5d0*net_cool)/(2.d0/dx_over_delta)
1606 end if
1607 kappa_eff = max(kappa_trac,kappa_par)
1608 tcoff_eff = (kappa_eff/tc_fl%tc_k_para)**0.4d0
1609 block%wextra(ix^d,tcoff_) = max(te(ix^d),tcoff_eff)
1610 end if
1611 {end do\}
1612 }
1613 case default
1614 call mpistop("hd_trac_type not allowed")
1615 end select
1616 end subroutine hd_get_tcutoff
1617
1618 !> Calculate cmax_idim = csound + abs(v_idim) within ixO^L
1619 subroutine hd_get_cbounds(wLC, wRC, wLp, wRp, x, ixI^L, ixO^L, idim,Hspeed,cmax, cmin)
1621 use mod_dust, only: dust_get_cmax
1622 use mod_fld, only: fld_bound_diff
1623 use mod_variables
1624
1625 integer, intent(in) :: ixi^l, ixo^l, idim
1626 ! conservative left and right status
1627 double precision, intent(in) :: wlc(ixi^s, nw), wrc(ixi^s, nw)
1628 ! primitive left and right status
1629 double precision, intent(in) :: wlp(ixi^s, nw), wrp(ixi^s, nw)
1630 double precision, intent(in) :: x(ixi^s, 1:ndim)
1631 double precision, intent(inout) :: cmax(ixi^s,1:number_species)
1632 double precision, intent(inout), optional :: cmin(ixi^s,1:number_species)
1633 double precision, intent(in) :: hspeed(ixi^s,1:number_species)
1634
1635 double precision :: wmean(ixi^s,nw)
1636 double precision, dimension(ixI^S) :: umean, dmean, csoundl, csoundr, tmp1,tmp2,tmp3
1637 integer :: ix^d
1638
1639 select case(boundspeed)
1640 case (1)
1641 ! This implements formula (10.52) from "Riemann Solvers and Numerical
1642 ! Methods for Fluid Dynamics" by Toro.
1643
1644 tmp1(ixo^s)=dsqrt(wlp(ixo^s,rho_))
1645 tmp2(ixo^s)=dsqrt(wrp(ixo^s,rho_))
1646 tmp3(ixo^s)=1.d0/(tmp1(ixo^s)+tmp2(ixo^s))
1647 umean(ixo^s)=(wlp(ixo^s,mom(idim))*tmp1(ixo^s)+wrp(ixo^s,mom(idim))*tmp2(ixo^s))*tmp3(ixo^s)
1648
1649 if(hd_energy) then
1650 call eos%get_csound2(wlp, x, ixi^l, ixo^l, csoundl)
1651 call eos%get_csound2(wrp, x, ixi^l, ixo^l, csoundr)
1653 csoundl(ixo^s)=csoundl(ixo^s)+(4.0d0/9.0d0)*wlp(ixo^s,r_e)/wlp(ixo^s,rho_)
1654 csoundr(ixo^s)=csoundr(ixo^s)+(4.0d0/9.0d0)*wrp(ixo^s,r_e)/wrp(ixo^s,rho_)
1655 endif
1656 else
1657 ! note usage of conservatives here
1658 call hd_get_csound2(wlc,x,ixi^l,ixo^l,csoundl)
1659 call hd_get_csound2(wrc,x,ixi^l,ixo^l,csoundr)
1660 end if
1661
1662 dmean(ixo^s) = (tmp1(ixo^s)*csoundl(ixo^s)+tmp2(ixo^s)*csoundr(ixo^s)) * &
1663 tmp3(ixo^s) + 0.5d0*tmp1(ixo^s)*tmp2(ixo^s)*tmp3(ixo^s)**2 * &
1664 (wrp(ixo^s,mom(idim))-wlp(ixo^s,mom(idim)))**2
1665
1666 dmean(ixo^s)=dsqrt(dmean(ixo^s))
1667 if(present(cmin)) then
1668 cmin(ixo^s,1)=umean(ixo^s)-dmean(ixo^s)
1669 cmax(ixo^s,1)=umean(ixo^s)+dmean(ixo^s)
1670 if(h_correction) then
1671 {do ix^db=ixomin^db,ixomax^db\}
1672 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
1673 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
1674 {end do\}
1675 end if
1676 else
1677 cmax(ixo^s,1)=dabs(umean(ixo^s))+dmean(ixo^s)
1678 end if
1679
1680 if (hd_dust) then
1681 wmean(ixo^s,1:nwflux)=0.5d0*(wlc(ixo^s,1:nwflux)+wrc(ixo^s,1:nwflux))
1682 call dust_get_cmax(wmean, x, ixi^l, ixo^l, idim, cmax, cmin)
1683 end if
1684
1685 case (2)
1686 !if(hd_energy) then
1687 ! ! note usage of primitives here
1688 ! wmean(ixO^S,1:nwflux)=0.5d0*(wLp(ixO^S,1:nwflux)+wRp(ixO^S,1:nwflux))
1689 ! tmp1(ixO^S)=wmean(ixO^S,mom(idim))
1690 ! csoundR(ixO^S)=hd_gamma*wmean(ixO^S,p_)/wmean(ixO^S,rho_)
1691 !else
1692 ! note usage of conservatives here
1693 wmean(ixo^s,1:nwflux)=0.5d0*(wlc(ixo^s,1:nwflux)+wrc(ixo^s,1:nwflux))
1694 tmp1(ixo^s)=wmean(ixo^s,mom(idim))/wmean(ixo^s,rho_)
1695 call hd_get_csound2(wmean,x,ixi^l,ixo^l,csoundr)
1696 if(hd_radiation_fld.and.fld_bound_diff)csoundr(ixo^s)=csoundr(ixo^s)+(4.0d0/9.0d0)*wmean(ixo^s,r_e)/wmean(ixo^s,rho_)
1697 !endif
1698 csoundr(ixo^s) = dsqrt(csoundr(ixo^s))
1699
1700 if(present(cmin)) then
1701 cmax(ixo^s,1)=max(tmp1(ixo^s)+csoundr(ixo^s),zero)
1702 cmin(ixo^s,1)=min(tmp1(ixo^s)-csoundr(ixo^s),zero)
1703 if(h_correction) then
1704 {do ix^db=ixomin^db,ixomax^db\}
1705 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
1706 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
1707 {end do\}
1708 end if
1709 else
1710 cmax(ixo^s,1)=dabs(tmp1(ixo^s))+csoundr(ixo^s)
1711 end if
1712
1713 if (hd_dust) then
1714 call dust_get_cmax(wmean, x, ixi^l, ixo^l, idim, cmax, cmin)
1715 end if
1716 case (3)
1717 ! Miyoshi 2005 JCP 208, 315 equation (67)
1718 if(hd_energy) then
1719 call eos%get_csound2(wlp, x, ixi^l, ixo^l, csoundl)
1720 call eos%get_csound2(wrp, x, ixi^l, ixo^l, csoundr)
1721 if(hd_radiation_fld.and.fld_bound_diff)then
1722 csoundl(ixo^s)=csoundl(ixo^s)+(4.0d0/9.0d0)*wlp(ixo^s,r_e)/wlp(ixo^s,rho_)
1723 csoundr(ixo^s)=csoundr(ixo^s)+(4.0d0/9.0d0)*wrp(ixo^s,r_e)/wrp(ixo^s,rho_)
1724 endif
1725 else
1726 ! note usage of conservatives here
1727 call hd_get_csound2(wlc,x,ixi^l,ixo^l,csoundl)
1728 call hd_get_csound2(wrc,x,ixi^l,ixo^l,csoundr)
1729 end if
1730 csoundl(ixo^s)=max(dsqrt(csoundl(ixo^s)),dsqrt(csoundr(ixo^s)))
1731 if(present(cmin)) then
1732 cmin(ixo^s,1)=min(wlp(ixo^s,mom(idim)),wrp(ixo^s,mom(idim)))-csoundl(ixo^s)
1733 cmax(ixo^s,1)=max(wlp(ixo^s,mom(idim)),wrp(ixo^s,mom(idim)))+csoundl(ixo^s)
1734 if(h_correction) then
1735 {do ix^db=ixomin^db,ixomax^db\}
1736 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
1737 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
1738 {end do\}
1739 end if
1740 else
1741 cmax(ixo^s,1)=max(dabs(wlp(ixo^s,mom(idim))),dabs(wrp(ixo^s,mom(idim))))+csoundl(ixo^s)
1742 end if
1743 if (hd_dust) then
1744 wmean(ixo^s,1:nwflux)=0.5d0*(wlc(ixo^s,1:nwflux)+wrc(ixo^s,1:nwflux))
1745 call dust_get_cmax(wmean, x, ixi^l, ixo^l, idim, cmax, cmin)
1746 end if
1747 case (4)
1748 !> PVRS pressure-based wave speed estimate (Toro 1999, Section 10.5.2)
1749 !> Recommended by Coleman 2020 for general EoS given limitations of constant gamma approximation.
1750 !> Estimates star pressure from linearised Riemann problem, then uses
1751 !> RH to detect shock vs rarefaction on each side.
1752
1753 if(hd_energy) then
1754 call eos%get_csound2(wlp, x, ixi^l, ixo^l, csoundl)
1755 call eos%get_csound2(wrp, x, ixi^l, ixo^l, csoundr)
1756 if(hd_radiation_fld.and.fld_bound_diff)then
1757 csoundl(ixo^s)=csoundl(ixo^s)+(4.0d0/9.0d0)*wlp(ixo^s,r_e)/wlp(ixo^s,rho_)
1758 csoundr(ixo^s)=csoundr(ixo^s)+(4.0d0/9.0d0)*wrp(ixo^s,r_e)/wrp(ixo^s,rho_)
1759 endif
1760 else
1761 call hd_get_csound2(wlc,x,ixi^l,ixo^l,csoundl)
1762 call hd_get_csound2(wrc,x,ixi^l,ixo^l,csoundr)
1763 end if
1764 csoundl(ixo^s) = dsqrt(csoundl(ixo^s))
1765 csoundr(ixo^s) = dsqrt(csoundr(ixo^s))
1766 if(present(cmin)) then
1767 {do ix^db=ixomin^db,ixomax^db\}
1768 !> PVRS star pressure estimate (Toro Eq. 9.28)
1769 !> cup = rho_bar * a_bar
1770 tmp1(ix^d) = 0.25d0*(wlp(ix^d,rho_)+wrp(ix^d,rho_)) &
1771 *(csoundl(ix^d)+csoundr(ix^d))
1772 !> p* = max(0, p_avg + 0.5*(u_L - u_R)*cup)
1773 tmp2(ix^d) = max(zero, 0.5d0*(wlp(ix^d,e_)+wrp(ix^d,e_)) &
1774 + 0.5d0*(wlp(ix^d,mom(idim))-wrp(ix^d,mom(idim))) &
1775 *tmp1(ix^d))
1776 !> Left wave speed: S_L = u_L - a_L * q_L
1777 if(tmp2(ix^d) > wlp(ix^d,e_) .and. wlp(ix^d,e_) > zero) then
1778 !> Left shock: q_L from R-H with local Gamma1 = a^2*rho/p
1779 tmp3(ix^d) = csoundl(ix^d)**2*wlp(ix^d,rho_)/wlp(ix^d,e_)
1780 dmean(ix^d) = dsqrt(1.0d0 + (tmp3(ix^d)+1.0d0) &
1781 /(2.0d0*tmp3(ix^d)) &
1782 *(tmp2(ix^d)/wlp(ix^d,e_) - 1.0d0))
1783 else
1784 !> Left rarefaction
1785 dmean(ix^d) = 1.0d0
1786 end if
1787 cmin(ix^d,1) = wlp(ix^d,mom(idim)) - csoundl(ix^d)*dmean(ix^d)
1788 !> Right wave speed: S_R = u_R + a_R * q_R
1789 if(tmp2(ix^d) > wrp(ix^d,e_) .and. wrp(ix^d,e_) > zero) then
1790 !> Right shock: q_R from R-H with local Gamma1
1791 tmp3(ix^d) = csoundr(ix^d)**2*wrp(ix^d,rho_)/wrp(ix^d,e_)
1792 dmean(ix^d) = dsqrt(1.0d0 + (tmp3(ix^d)+1.0d0) &
1793 /(2.0d0*tmp3(ix^d)) &
1794 *(tmp2(ix^d)/wrp(ix^d,e_) - 1.0d0))
1795 else
1796 !> Right rarefaction
1797 dmean(ix^d) = 1.0d0
1798 end if
1799 cmax(ix^d,1) = wrp(ix^d,mom(idim)) + csoundr(ix^d)*dmean(ix^d)
1800 {end do\}
1801 if(h_correction) then
1802 {do ix^db=ixomin^db,ixomax^db\}
1803 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
1804 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
1805 {end do\}
1806 end if
1807 else
1808 {do ix^db=ixomin^db,ixomax^db\}
1809 tmp1(ix^d) = 0.25d0*(wlp(ix^d,rho_)+wrp(ix^d,rho_)) &
1810 *(csoundl(ix^d)+csoundr(ix^d))
1811 tmp2(ix^d) = max(zero, 0.5d0*(wlp(ix^d,e_)+wrp(ix^d,e_)) &
1812 + 0.5d0*(wlp(ix^d,mom(idim))-wrp(ix^d,mom(idim))) &
1813 *tmp1(ix^d))
1814 if(tmp2(ix^d) > wlp(ix^d,e_) .and. wlp(ix^d,e_) > zero) then
1815 tmp3(ix^d) = csoundl(ix^d)**2*wlp(ix^d,rho_)/wlp(ix^d,e_)
1816 dmean(ix^d) = dsqrt(1.0d0 + (tmp3(ix^d)+1.0d0) &
1817 /(2.0d0*tmp3(ix^d)) &
1818 *(tmp2(ix^d)/wlp(ix^d,e_) - 1.0d0))
1819 else
1820 dmean(ix^d) = 1.0d0
1821 end if
1822 umean(ix^d) = dabs(wlp(ix^d,mom(idim)) &
1823 - csoundl(ix^d)*dmean(ix^d))
1824 if(tmp2(ix^d) > wrp(ix^d,e_) .and. wrp(ix^d,e_) > zero) then
1825 tmp3(ix^d) = csoundr(ix^d)**2*wrp(ix^d,rho_)/wrp(ix^d,e_)
1826 dmean(ix^d) = dsqrt(1.0d0 + (tmp3(ix^d)+1.0d0) &
1827 /(2.0d0*tmp3(ix^d)) &
1828 *(tmp2(ix^d)/wrp(ix^d,e_) - 1.0d0))
1829 else
1830 dmean(ix^d) = 1.0d0
1831 end if
1832 cmax(ix^d,1) = max(umean(ix^d), &
1833 wrp(ix^d,mom(idim))+csoundr(ix^d)*dmean(ix^d))
1834 {end do\}
1835 end if
1836 if (hd_dust) then
1837 wmean(ixo^s,1:nwflux)=0.5d0*(wlc(ixo^s,1:nwflux)+wrc(ixo^s,1:nwflux))
1838 call dust_get_cmax(wmean, x, ixi^l, ixo^l, idim, cmax, cmin)
1839 end if
1840 end select
1841
1842 end subroutine hd_get_cbounds
1843
1844 !> Calculate the square of the thermal sound speed csound2 within ixO^L.
1845 !> For conserved w: extracts pthermal first, then applies Gamma_1.
1846 !> Squared sound speed of a conserved face state, Gamma_1 p/rho. Face states carry no Ne/Te, so
1847 !> under LTE the pressure comes from the internal energy through lte_p_from_eint, and Gamma_1
1848 !> from the pressure-indexed table, since p is continuous at contact discontinuities
1849 subroutine hd_get_csound2(w,x,ixI^L,ixO^L,csound2)
1854 integer, intent(in) :: ixi^l, ixo^l
1855 double precision, intent(in) :: w(ixi^s,nw)
1856 double precision, intent(in) :: x(ixi^s,1:ndim)
1857 double precision, intent(out) :: csound2(ixi^s)
1858 double precision :: pthermal(ixi^s)
1859 double precision :: inv_rho, nh_val, pres, g1
1860 logical :: tab_g1
1861 integer :: ix^d
1862
1863 ! called directly by the wave speeds, so timed here; the pressure inside counts once
1864 call eos_timer_start()
1865 if (hd_energy .and. eos%type_id == eos_type_lte) then
1866 tab_g1 = eos%ionE .and. eos%gamma1_method /= 'constant'
1867 {do ix^db=ixomin^db,ixomax^db\}
1868 inv_rho = 1.0d0/w(ix^d,rho_)
1869 nh_val = w(ix^d,rho_)/eos%nH2rhoFactor
1870 pres = lte_p_from_eint(w(ix^d,e_) - half*(^c&w(ix^d,m^c_)**2+)*inv_rho, &
1871 nh_val, dlog10(nh_val), inv_rho)
1872 g1 = eos%gamma
1873 if (tab_g1) g1 = lte_gamma1_from_p(pres, w(ix^d,rho_))
1874 csound2(ix^d) = g1*pres*inv_rho
1875 {end do\}
1876 else
1877 call eos%get_thermal_pressure(w, x, ixi^l, ixo^l, pthermal)
1878 csound2(ixo^s) = eos%gamma * pthermal(ixo^s) / w(ixo^s,rho_)
1879 end if
1880 call eos_timer_stop(timeeos_csound)
1881
1882 end subroutine hd_get_csound2
1883
1884
1885 !> Calculate modified squared sound speed for FLD
1886 !> NOTE: only for diagnostic purposes, unused subroutine
1887 subroutine hd_get_csrad2(w,x,ixI^L,ixO^L,csound)
1889
1890 integer, intent(in) :: ixi^l, ixo^l
1891 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
1892 double precision, intent(out):: csound(ixi^s)
1893
1894 double precision :: wprim(ixi^s, nw)
1895
1896 wprim(ixi^s,1:nw)=w(ixi^s,1:nw)
1897 call eos%to_primitive(ixi^l,ixo^l,wprim,x)
1898 call hd_get_csrad2_prim(wprim,x,ixi^l,ixo^l,csound)
1899
1900 end subroutine hd_get_csrad2
1901
1902 !> Calculate modified squared sound speed for FLD
1903 !> NOTE: w is primitive on entry here!
1904 !> NOTE: used in FLD module as phys_get_csrad2
1905 subroutine hd_get_csrad2_prim(w,x,ixI^L,ixO^L,csound)
1907
1908 integer, intent(in) :: ixi^l, ixo^l
1909 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
1910 double precision, intent(out):: csound(ixi^s)
1911
1912 integer :: ix^d
1913 double precision :: inv_rho
1914 double precision :: prad_tensor(ixi^s, 1:ndim, 1:ndim)
1915 double precision :: prad_max(ixi^s)
1916 integer :: idim
1917
1918 if(hd_fld_pradtensor) then
1919 call hd_get_pradiation_from_prim(w, x, ixi^l, ixo^l, prad_tensor)
1920 else
1921 prad_tensor=zero
1922 do idim=1,ndim
1923 prad_tensor(ixo^s,idim,idim)=w(ixo^s,r_e)/3.0d0
1924 enddo
1925 endif
1926
1927 {do ix^db=ixomin^db,ixomax^db \}
1928 inv_rho=1.d0/w(ix^d,rho_)
1929 prad_max(ix^d) = (4.0d0/3.0d0)*maxval(prad_tensor(ix^d,:,:))
1930 csound(ix^d)=(eos%gamma*w(ix^d,p_)+prad_max(ix^d))*inv_rho
1931 {end do\}
1932
1933 if(minval(csound(ixo^s))<smalldouble)then
1934 print *,'issue with squared speed and rad pressure'
1935 print *,minval(csound(ixo^s))
1936 print *,minval(prad_max(ixo^s))
1937 call mpistop("negative squared speed in get_csrad2 for dt")
1938 endif
1939
1940 end subroutine hd_get_csrad2_prim
1941
1942 !> Calculate radiation pressure within ixO^L
1943 !> NOTE: w is primitive on entry here!
1944 !> NOTE: used in FLD module as it is called from phys_get_csrad2
1945 subroutine hd_get_pradiation_from_prim(w, x, ixI^L, ixO^L, prad)
1947 use mod_fld
1948 integer, intent(in) :: ixi^l, ixo^l
1949 double precision, intent(in) :: w(ixi^s, 1:nw)
1950 double precision, intent(in) :: x(ixi^s, 1:ndim)
1951 double precision, intent(out):: prad(ixi^s, 1:ndim, 1:ndim)
1952
1953 call fld_get_radpress(w, x, ixi^l, ixo^l, prad, fld_fl)
1954
1955 end subroutine hd_get_pradiation_from_prim
1956
1957 !> calculates the sum of the gas pressure and max Prad tensor element
1958 !> NOTE: only for diagnostic purposes, unused subroutine
1959 subroutine hd_get_pthermal_plus_pradiation(w, x, ixI^L, ixO^L, pth_plus_prad)
1961 integer, intent(in) :: ixi^l, ixo^l
1962 double precision, intent(in) :: w(ixi^s, 1:nw)
1963 double precision, intent(in) :: x(ixi^s, 1:ndim)
1964 double precision, intent(out):: pth_plus_prad(ixi^s)
1965
1966 double precision :: wprim(ixi^s, 1:nw)
1967 double precision :: prad_tensor(ixi^s, 1:ndim, 1:ndim)
1968 double precision :: prad_max(ixi^s)
1969 integer :: ix^d
1970
1971 wprim(ixi^s,1:nw)=w(ixi^s,1:nw)
1972 call eos%to_primitive(ixi^l,ixo^l,wprim,x)
1973 call hd_get_pradiation_from_prim(wprim, x, ixi^l, ixo^l, prad_tensor)
1974 {do ix^d = ixomin^d,ixomax^d\}
1975 prad_max(ix^d) = maxval(prad_tensor(ix^d,:,:))
1976 {enddo\}
1977 pth_plus_prad(ixo^s) = wprim(ixo^s,p_) + prad_max(ixo^s)
1978 end subroutine hd_get_pthermal_plus_pradiation
1979
1980 !> Calculates radiation temperature
1981 subroutine hd_get_trad(w, x, ixI^L, ixO^L, trad)
1983 use mod_constants
1984
1985 integer, intent(in) :: ixi^l, ixo^l
1986 double precision, intent(in) :: w(ixi^s, 1:nw)
1987 double precision, intent(in) :: x(ixi^s, 1:ndim)
1988 double precision, intent(out):: trad(ixi^s)
1989
1990 trad(ixi^s) = (w(ixi^s,r_e)/arad_norm)**(1.d0/4.d0)
1991
1992 end subroutine hd_get_trad
1993
1994 !> Calculate temperature=p/rho when in e_ the total energy is stored
1995 subroutine hd_get_temperature_from_etot(w, x, ixI^L, ixO^L, res)
1997 integer, intent(in) :: ixi^l, ixo^l
1998 double precision, intent(in) :: w(ixi^s, 1:nw)
1999 double precision, intent(in) :: x(ixi^s, 1:ndim)
2000 double precision, intent(out):: res(ixi^s)
2001
2002 double precision :: r(ixi^s)
2003
2004 call eos%get_Rfactor(w,x,ixi^l,ixo^l,r)
2005 call eos%get_thermal_pressure(w, x, ixi^l, ixo^l, res)
2006 res(ixo^s)=res(ixo^s)/(r(ixo^s)*w(ixo^s,rho_))
2007 end subroutine hd_get_temperature_from_etot
2008
2009 !> Calculate temperature=p/rho when in e_ the internal energy is stored
2010 subroutine hd_get_temperature_from_eint(w, x, ixI^L, ixO^L, res)
2012 integer, intent(in) :: ixi^l, ixo^l
2013 double precision, intent(in) :: w(ixi^s, 1:nw)
2014 double precision, intent(in) :: x(ixi^s, 1:ndim)
2015 double precision, intent(out):: res(ixi^s)
2016
2017 double precision :: r(ixi^s)
2018
2019 call eos%get_Rfactor(w,x,ixi^l,ixo^l,r)
2020 res(ixo^s) = (eos%gamma - 1.0d0) * w(ixo^s, e_)/(w(ixo^s,rho_)*r(ixo^s))
2021 end subroutine hd_get_temperature_from_eint
2022
2023 ! Calculate flux f_idim[iw]
2024 subroutine hd_get_flux(wC, w, x, ixI^L, ixO^L, idim, f)
2026 use mod_dust, only: dust_get_flux_prim
2027
2028 integer, intent(in) :: ixi^l, ixo^l, idim
2029 ! conservative w
2030 double precision, intent(in) :: wc(ixi^s, 1:nw)
2031 ! primitive w
2032 double precision, intent(in) :: w(ixi^s, 1:nw)
2033 double precision, intent(in) :: x(ixi^s, 1:ndim)
2034 double precision, intent(out) :: f(ixi^s, nwflux)
2035
2036 double precision :: pth(ixi^s)
2037 integer :: ix^db
2038
2039 if (hd_energy) then
2040 {do ix^db=ixomin^db,ixomax^db\}
2041 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
2042 ! Momentum flux is v_i*m_i, +p in direction idim
2043 ^c&f(ix^d,m^c_)=w(ix^d,mom(idim))*wc(ix^d,m^c_)\
2044 f(ix^d,mom(idim))=f(ix^d,mom(idim))+w(ix^d,p_)
2045 ! Energy flux is v_i*(e + p)
2046 f(ix^d,e_)=w(ix^d,mom(idim))*(wc(ix^d,e_)+w(ix^d,p_))
2047 {end do\}
2048
2049 ! FACE-RECIPE: q is NOT contributing to the Riemann energy flux.
2050 ! The conductive heat-flux contribution is added by the face-
2051 ! recipe in add_hypertc_source as a post-Riemann sweep, computed
2052 ! from cell-centred Te (read from the cached Te_ field, which
2053 ! update_eos sets correctly each substep using the proper
2054 ! internal-energy/EoS path). q's own advective flux is zero.
2056 {do ix^db=ixomin^db,ixomax^db\}
2057 f(ix^d,q_)=zero
2058 {end do\}
2059 end if
2060 else
2061 call eos%get_thermal_pressure(wc, x, ixi^l, ixo^l, pth)
2062 {do ix^db=ixomin^db,ixomax^db\}
2063 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
2064 ! Momentum flux is v_i*m_i, +p in direction idim
2065 ^c&f(ix^d,m^c_)=w(ix^d,mom(idim))*wc(ix^d,m^c_)\
2066 f(ix^d,mom(idim))=f(ix^d,mom(idim))+pth(ix^d)
2067 {end do\}
2068 end if
2069
2070 if(hd_radiation_fld)then
2071 {do ix^db=ixomin^db,ixomax^db\}
2072 ! advection of radiation enery v_i*r_e
2073 f(ix^d,r_e)=w(ix^d,mom(idim))*wc(ix^d,r_e)
2074 {end do\}
2075 endif
2076
2077 do ix1 = 1, hd_n_tracer
2078 f(ixo^s, tracer(ix1)) = w(ixo^s,mom(idim)) * w(ixo^s, tracer(ix1))
2079 end do
2080
2081 if (hd_fip) then
2082 f(ixo^s,fip_) = w(ixo^s,mom(idim)) * wc(ixo^s,fip_)
2083 end if
2084
2085 ! Dust fluxes
2086 if (hd_dust) then
2087 call dust_get_flux_prim(w, x, ixi^l, ixo^l, idim, f)
2088 end if
2089
2090 end subroutine hd_get_flux
2091
2092 !> Add geometrical source terms to w
2093 !>
2094 !> Notice that the expressions of the geometrical terms depend only on ndir,
2095 !> not ndim. Eg, they are the same in 2.5D and in 3D, for any geometry.
2096 !>
2097 subroutine hd_add_source_geom(qdt, dtfactor, ixI^L, ixO^L, wCT, wprim, w, x)
2102 use mod_geometry
2103 integer, intent(in) :: ixi^l, ixo^l
2104 double precision, intent(in) :: qdt, dtfactor, x(ixi^s, 1:ndim)
2105 double precision, intent(inout) :: wct(ixi^s, 1:nw), wprim(ixi^s,1:nw),w(ixi^s, 1:nw)
2106 double precision :: pth(ixi^s), source(ixi^s), minrho
2107 integer :: iw,idir, h1x^l{^nooned, h2x^l}
2108 integer :: mr_,mphi_ ! Polar var. names
2109 integer :: irho, ifluid, n_fluids
2110 double precision :: exp_factor(ixi^s), del_exp_factor(ixi^s), exp_factor_primitive(ixi^s)
2111
2112 if (hd_dust) then
2113 n_fluids = 1 + dust_n_species
2114 else
2115 n_fluids = 1
2116 end if
2117
2118 select case (coordinate)
2119
2121 !the user provides the functions of exp_factor and del_exp_factor
2122 if(associated(usr_set_surface)) call usr_set_surface(ixi^l,x,block%dx,exp_factor,del_exp_factor,exp_factor_primitive)
2123 if(hd_energy) then
2124 source(ixo^s)=wprim(ixo^s, p_)
2125 else
2126 if(.not. associated(usr_set_pthermal)) then
2127 source(ixo^s)=hd_adiab * wprim(ixo^s, rho_)**eos%gamma
2128 else
2129 call usr_set_pthermal(wct,x,ixi^l,ixo^l,source)
2130 end if
2131 end if
2132 source(ixo^s) = source(ixo^s)*del_exp_factor(ixo^s)/exp_factor(ixo^s)
2133 w(ixo^s,mom(1)) = w(ixo^s,mom(1)) + qdt*source(ixo^s)
2134
2135 case (cylindrical)
2136 do ifluid = 0, n_fluids-1
2137 ! s[mr]=(pthermal+mphi**2/rho)/radius
2138 if (ifluid == 0) then
2139 ! gas
2140 irho = rho_
2141 mr_ = mom(r_)
2142 if(phi_>0) mphi_ = mom(phi_)
2143 if(hd_energy) then
2144 source(ixo^s)=wprim(ixo^s, p_)
2145 else
2146 if(.not. associated(usr_set_pthermal)) then
2147 source(ixo^s)=hd_adiab * wprim(ixo^s, rho_)**eos%gamma
2148 else
2149 call usr_set_pthermal(wct,x,ixi^l,ixo^l,source)
2150 end if
2151 end if
2152 minrho = 0.0d0
2153 else
2154 ! dust : no pressure
2155 irho = dust_rho(ifluid)
2156 mr_ = dust_mom(r_, ifluid)
2157 if(phi_>0) mphi_ = dust_mom(phi_, ifluid)
2158 source(ixi^s) = zero
2159 minrho = 0.0d0
2160 end if
2161 if(phi_ > 0) then
2162 where (wct(ixo^s, irho) > minrho)
2163 source(ixo^s) = source(ixo^s) + wct(ixo^s,mphi_)*wprim(ixo^s,mphi_)
2164 w(ixo^s, mr_) = w(ixo^s, mr_) + qdt*source(ixo^s)/x(ixo^s,r_)
2165 end where
2166 ! s[mphi]=(-mphi*vr)/radius
2167 where (wct(ixo^s, irho) > minrho)
2168 source(ixo^s) = -wct(ixo^s, mphi_) * wprim(ixo^s, mr_)
2169 w(ixo^s, mphi_) = w(ixo^s, mphi_) + qdt * source(ixo^s) / x(ixo^s, r_)
2170 end where
2171 else
2172 ! s[mr]=2pthermal/radius
2173 w(ixo^s, mr_) = w(ixo^s, mr_) + qdt * source(ixo^s) / x(ixo^s, r_)
2174 end if
2175 end do
2176 case (spherical)
2177 if (hd_dust) then
2178 call mpistop("Dust geom source terms not implemented yet with spherical geometries")
2179 end if
2180 mr_ = mom(r_)
2181 if(phi_>0) mphi_ = mom(phi_)
2182 h1x^l=ixo^l-kr(1,^d); {^nooned h2x^l=ixo^l-kr(2,^d);}
2183 if(hd_energy) then
2184 pth(ixo^s)=wprim(ixo^s, p_)
2185 else
2186 if(.not. associated(usr_set_pthermal)) then
2187 pth(ixo^s)=hd_adiab * wprim(ixo^s, rho_)**eos%gamma
2188 else
2189 call usr_set_pthermal(wct,x,ixi^l,ixo^l,pth)
2190 end if
2191 end if
2192 ! s[mr]=((vtheta**2+vphi**2)*rho+2*p)/r
2193 source(ixo^s) = pth(ixo^s) * x(ixo^s, 1) &
2194 *(block%surfaceC(ixo^s, 1) - block%surfaceC(h1x^s, 1)) &
2195 /block%dvolume(ixo^s)
2196 do idir = 2, ndir
2197 source(ixo^s) = source(ixo^s) + wprim(ixo^s, mom(idir))**2 * wprim(ixo^s, rho_)
2198 end do
2199 w(ixo^s, mr_) = w(ixo^s, mr_) + qdt * source(ixo^s) / x(ixo^s, 1)
2200
2201 {^nooned
2202 ! s[mtheta]=-(vr*vtheta*rho)/r+cot(theta)*(vphi**2*rho+p)/r
2203 source(ixo^s) = pth(ixo^s) * x(ixo^s, 1) &
2204 * (block%surfaceC(ixo^s, 2) - block%surfaceC(h2x^s, 2)) &
2205 / block%dvolume(ixo^s)
2206 if (ndir == 3) then
2207 source(ixo^s) = source(ixo^s) + (wprim(ixo^s, mom(3))**2 * wprim(ixo^s, rho_)) / tan(x(ixo^s, 2))
2208 end if
2209 source(ixo^s) = source(ixo^s) - (wprim(ixo^s, mom(2)) * wprim(ixo^s, mr_)) * wprim(ixo^s, rho_)
2210 w(ixo^s, mom(2)) = w(ixo^s, mom(2)) + qdt * source(ixo^s) / x(ixo^s, 1)
2211
2212 if (ndir == 3) then
2213 ! s[mphi]=-(vphi*vr/rho)/r-cot(theta)*(vtheta*vphi/rho)/r
2214 source(ixo^s) = -(wprim(ixo^s, mom(3)) * wprim(ixo^s, mr_)) * wprim(ixo^s, rho_)&
2215 - (wprim(ixo^s, mom(2)) * wprim(ixo^s, mom(3))) * wprim(ixo^s, rho_) / tan(x(ixo^s, 2))
2216 w(ixo^s, mom(3)) = w(ixo^s, mom(3)) + qdt * source(ixo^s) / x(ixo^s, 1)
2217 end if
2218 }
2219 end select
2220
2221 if (hd_rotating_frame) then
2222 if (hd_dust) then
2223 call mpistop("Rotating frame not implemented yet with dust")
2224 else
2225 call rotating_frame_add_source(qdt,dtfactor,ixi^l,ixo^l,wprim,w,x)
2226 end if
2227 end if
2228
2229 end subroutine hd_add_source_geom
2230
2231 ! w[iw]= w[iw]+qdt*S[wCT, qtC, x] where S is the source based on wCT within ixO
2232 subroutine hd_add_source(qdt,dtfactor, ixI^L,ixO^L,wCT,wCTprim,w,x,qsourcesplit,active)
2237 use mod_usr_methods, only: usr_gravity
2239 use mod_cak_force, only: cak_add_source
2240
2241 integer, intent(in) :: ixi^l, ixo^l
2242 double precision, intent(in) :: qdt, dtfactor
2243 double precision, intent(in) :: wct(ixi^s, 1:nw),wctprim(ixi^s,1:nw), x(ixi^s, 1:ndim)
2244 double precision, intent(inout) :: w(ixi^s, 1:nw)
2245 logical, intent(in) :: qsourcesplit
2246 logical, intent(inout) :: active
2247
2248 double precision :: gravity_field(ixi^s, 1:ndim)
2249 integer :: idust, idim, ix^d
2250
2251 if(hd_dust .and. .not. hd_dust_implicit) then
2252 call dust_add_source(qdt,ixi^l,ixo^l,wct,w,x,qsourcesplit,active)
2253 end if
2254
2255
2256 ! if (mype == 0) then
2257 ! {do ix^DB = ixI^LIM^DB\}
2258 ! if (abs(x(ix^D,1) - xprobmax1/2.0d0) > (xprobmax1/2.0d0 - 1.0d8/unit_length)) then
2259 ! write(*,*) x(ix^D,1), ' ' , wCT(ix^D,e_)
2260 ! endif
2261 ! {end do\}
2262 ! endif
2263 if(hd_radiative_cooling) then
2264 call radiative_cooling_add_source(qdt,ixi^l,ixo^l,wct,wctprim,w,x,&
2265 qsourcesplit,active, rc_fl)
2266 end if
2267
2268 ! The radiative transfer heating, solved once per step, is added unsplit at every stage
2269 if(hd_radiative_transfer .and. .not. qsourcesplit) then
2270 active = .true.
2271 call rt_sc_add_source(qdt,ixi^l,ixo^l,wct,w,x,hd_radiative_cooling,.false.)
2272 end if
2273
2274 if(hd_viscosity) then
2275 call viscosity_add_source(qdt,ixi^l,ixo^l,wct,wctprim,w,x,&
2276 hd_energy,qsourcesplit,active)
2277 end if
2278
2279 if (hd_gravity) then
2280 call gravity_add_source(qdt,ixi^l,ixo^l,wct,wctprim,w,x,&
2281 hd_energy,qsourcesplit,active)
2282
2283 if (hd_dust .and. qsourcesplit .eqv. grav_split) then
2284 active = .true.
2285
2286 call usr_gravity(ixi^l, ixo^l, wct, x, gravity_field)
2287 do idust = 1, dust_n_species
2288 do idim = 1, ndim
2289 w(ixo^s, dust_mom(idim, idust)) = w(ixo^s, dust_mom(idim, idust)) &
2290 + qdt * gravity_field(ixo^s, idim) * wct(ixo^s, dust_rho(idust))
2291 end do
2292 end do
2293 end if
2294 end if
2295
2296 if (hd_cak_force) then
2297 call cak_add_source(qdt,ixi^l,ixo^l,wct,w,x,hd_energy,qsourcesplit,active)
2298 end if
2299
2300 ! This is where the radiation force and heating/cooling are added
2301 if (hd_radiation_fld) then
2302 call hd_add_radiation_source(qdt,ixi^l,ixo^l,wct,wctprim,w,x,qsourcesplit,active)
2303 endif
2304
2305 if(eos%eos_type == 'PI') then
2306 if(.not.qsourcesplit) then
2307 active = .true.
2308 call eos%update_eos(ixi^l,ixo^l,w,x)
2309 end if
2310 end if
2311
2312 ! Hyperbolic TC: cell-centred Cattaneo relaxation source for q.
2313 if(hd_hyperbolic_thermal_conduction .and. .not. qsourcesplit) then
2314 active = .true.
2315 call add_hypertc_source(qdt,ixi^l,ixo^l,wct,w,x,wctprim)
2316 end if
2317
2318 end subroutine hd_add_source
2319
2320 !> FACE-RECIPE for hyperbolic thermal conduction.
2321 !>
2322 !> Replaces the OLD cell-centred Cattaneo source. Architecture:
2323 !> 1. q is removed from the Riemann pipeline (f(e_) does NOT get +q
2324 !> in hd_get_flux); q has no advective flux of its own.
2325 !> 2. Each face i+1/2 computes a Cattaneo-relaxed face heat flux
2326 !> q_f from the local centred T gradient. Te is read from the
2327 !> cached Te_ field (set by update_eos using the correct
2328 !> internal-energy/EoS path; calling get_temperature_from_eint
2329 !> directly on the conservative wCT inflates Te by KE/(R*rho)
2330 !> at fast-flow cells, hence the cache read).
2331 !> 3. Energy update: E_i -= qdt * (q_{i+1/2} - q_{i-1/2}) / dx_i.
2332 !> 4. Cell-centred q refreshed from the face values for next step.
2333 !>
2334 !> Why face-recipe vs HLL-on-q: avoids reconstruction (Koren limiter
2335 !> applied to q at faces) and HLL wave-speed bias (q weighted by
2336 !> hydro signal speeds during HLL combination, which has no physical
2337 !> justification for a scalar conductive flux).
2338 !>
2339 !> 1D only.
2340 subroutine add_hypertc_source(qdt,ixI^L,ixO^L,wCT,w,x,wCTprim)
2342
2343 integer, intent(in) :: ixi^l,ixo^l
2344 double precision, intent(in) :: qdt
2345 double precision, dimension(ixI^S,1:ndim), intent(in) :: x
2346 double precision, dimension(ixI^S,1:nw), intent(in) :: wct,wctprim
2347 double precision, dimension(ixI^S,1:nw), intent(inout) :: w
2348
2349 double precision :: te(ixi^s), rfactor(ixi^s)
2350 double precision :: qf_half(ixglo1:ixghi1)
2351 double precision :: qf_full(ixglo1:ixghi1)
2352 double precision :: t_l, t_r, t_cool_l, t_cool_r, t_kap_l, t_kap_r
2353 double precision :: kappa_l, kappa_r, kappa_f
2354 double precision :: sigma_t7_f, dt_face, dx_f
2355 double precision :: rho_f, p_l, p_r, p_f, cs_f, q_sat, q_sp, q_sp_star
2356 double precision :: f_sat, eint_loc_l, eint_loc_r, eint_loc_f
2357 double precision :: qn_face, tau_f, ratio, decay, ave_factor
2358 double precision :: q_face_max
2359 double precision, parameter :: ratio_cap = 50.0d0
2360 integer :: ix1, nface_lo, nface_hi
2361
2362 ! Te via cached field (correct EoS path) or primitives (FI fallback).
2363 if (te_ > 0) then
2364 te(ixi^s) = wct(ixi^s, te_)
2365 else
2366 call eos%get_Rfactor(wctprim, x, ixi^l, ixi^l, rfactor)
2367 te(ixi^s) = wctprim(ixi^s, p_) / (rfactor(ixi^s) * wctprim(ixi^s, rho_))
2368 end if
2369
2370 {^ifoned
2371 nface_lo = ixomin1 - 1
2372 nface_hi = ixomax1
2373 qf_half(:) = 0.0d0
2374 qf_full(:) = 0.0d0
2375
2376 do ix1 = nface_lo, nface_hi
2377 ! Cell-centred T on either side of face, with TRAC clip
2378 t_l = te(ix1)
2379 t_r = te(ix1 + 1)
2380 if (hd_trac) then
2381 t_cool_l = block%wextra(ix1, tcoff_)
2382 t_cool_r = block%wextra(ix1 + 1, tcoff_)
2383 else
2384 t_cool_l = 0.0d0
2385 t_cool_r = 0.0d0
2386 end if
2387 t_kap_l = max(t_l, t_cool_l)
2388 t_kap_r = max(t_r, t_cool_r)
2389 kappa_l = hypertc_kappa * dsqrt(t_kap_l**5)
2390 kappa_r = hypertc_kappa * dsqrt(t_kap_r**5)
2391
2392 ! Harmonic-mean face kappa: limits to cold side at sharp jumps,
2393 ! preserves flux conservation across kappa discontinuities.
2394 if (kappa_l + kappa_r > smalldouble) then
2395 kappa_f = 2.0d0 * kappa_l * kappa_r / (kappa_l + kappa_r)
2396 else
2397 kappa_f = 0.0d0
2398 end if
2399
2400 dx_f = 0.5d0 * (block%ds(ix1, 1) + block%ds(ix1 + 1, 1))
2401 dt_face = t_r - t_l
2402 q_sp = -kappa_f * dt_face / dx_f
2403
2404 ! Face-averaged primitives for saturation
2405 rho_f = 0.5d0 * (wctprim(ix1, rho_) + wctprim(ix1 + 1, rho_))
2406 p_l = wctprim(ix1, p_)
2407 p_r = wctprim(ix1 + 1, p_)
2408 p_f = 0.5d0 * (p_l + p_r)
2409 cs_f = dsqrt(max(smalldouble, eos%gamma * p_f / max(rho_f, smalldouble)))
2410
2411 ! Cowie-McKee saturation cap on the Spitzer target
2412 if (hd_htc_sat) then
2413 q_sat = 1.5d0 * rho_f * (p_f / max(rho_f, smalldouble))**1.5d0
2414 if (q_sat > smalldouble) then
2415 f_sat = 1.0d0 / (1.0d0 + dabs(q_sp) / q_sat)
2416 else
2417 f_sat = 1.0d0
2418 end if
2419 q_sp_star = f_sat * q_sp
2420 else
2421 q_sp_star = q_sp
2422 end if
2423
2424 ! Internal energy from conservatives (matches eint fix)
2425 eint_loc_l = wct(ix1, e_) &
2426 - 0.5d0 * wct(ix1, m1_)**2 / max(wct(ix1, rho_), smalldouble)
2427 eint_loc_r = wct(ix1 + 1, e_) &
2428 - 0.5d0 * wct(ix1 + 1, m1_)**2 / max(wct(ix1 + 1, rho_), smalldouble)
2429 eint_loc_l = max(eint_loc_l, smalldouble)
2430 eint_loc_r = max(eint_loc_r, smalldouble)
2431 eint_loc_f = 0.5d0 * (eint_loc_l + eint_loc_r)
2432
2433 ! MHD-style tau formula, using face quantities + global cmax.
2434 ! sigma_T7_f = kappa_f * T_f acts as the conductivity*temperature
2435 ! product entering tau. Floor tau at 4*dt for stability.
2436 sigma_t7_f = kappa_f * 0.5d0 * (t_l + t_r)
2437 tau_f = max(4.0d0 * dt, &
2438 sigma_t7_f * courantpar**2 / (eint_loc_f * cmax_global**2))
2439
2440 ! Cattaneo relaxation: closed-form exponential
2441 ratio = min(qdt / tau_f, ratio_cap)
2442 decay = dexp(-ratio)
2443 if (ratio > 1.0d-6) then
2444 ave_factor = (1.0d0 - decay) / ratio
2445 else
2446 ave_factor = 1.0d0 - 0.5d0 * ratio + ratio * ratio / 6.0d0
2447 end if
2448
2449 qn_face = 0.5d0 * (wct(ix1, q_) + wct(ix1 + 1, q_))
2450
2451 qf_full(ix1) = q_sp_star + (qn_face - q_sp_star) * decay
2452 qf_half(ix1) = q_sp_star + (qn_face - q_sp_star) * ave_factor
2453
2454 ! Energy-positivity clip on the n+1/2 flux
2455 q_face_max = hd_htc_pos_eta * min(eint_loc_l, eint_loc_r) * &
2456 block%ds(ix1, 1) / max(qdt, smalldouble)
2457 qf_half(ix1) = sign(min(dabs(qf_half(ix1)), q_face_max), qf_half(ix1))
2458 end do
2459
2460 ! Conservative energy update + cell-centred q refresh
2461 do ix1 = ixomin1, ixomax1
2462 w(ix1, e_) = w(ix1, e_) &
2463 - qdt * (qf_half(ix1) - qf_half(ix1 - 1)) / block%ds(ix1, 1)
2464 w(ix1, q_) = 0.5d0 * (qf_full(ix1 - 1) + qf_full(ix1))
2465 end do
2466 }
2467
2468 end subroutine add_hypertc_source
2469
2470 subroutine hd_add_radiation_source(qdt,ixI^L,ixO^L,wCT,wCTprim,w,x,qsourcesplit,active)
2471 use mod_constants
2473 use mod_usr_methods
2474 use mod_fld
2475
2476 integer, intent(in) :: ixi^l, ixo^l
2477 double precision, intent(in) :: qdt, x(ixi^s,1:ndim)
2478 double precision, intent(in) :: wct(ixi^s,1:nw),wctprim(ixi^s,1:nw)
2479 double precision, intent(inout) :: w(ixi^s,1:nw)
2480 logical, intent(in) :: qsourcesplit
2481 logical, intent(inout) :: active
2482
2483 ! add radiation force and work done by it, changes momentum and gas energy
2484 ! handle photon tiring, heating and cooling exchange between gas and radiation field
2485 call add_fld_rad_force(qdt,ixi^l,ixo^l,wct,wctprim,w,x,qsourcesplit,active,fld_fl)
2486
2487 end subroutine hd_add_radiation_source
2488
2489 subroutine hd_get_dt(wprim, ixI^L, ixO^L, dtnew, dx^D, x)
2491 use mod_dust, only: dust_get_dt
2493 use mod_gravity, only: gravity_get_dt
2494 use mod_cak_force, only: cak_get_dt
2495 use mod_fld, only: fld_radforce_get_dt
2496
2497 integer, intent(in) :: ixi^l, ixo^l
2498 double precision, intent(in) :: dx^d, x(ixi^s, 1:^nd)
2499 double precision, intent(in) :: wprim(ixi^s, 1:nw)
2500 double precision, intent(inout) :: dtnew
2501
2502 dtnew = bigdouble
2503
2504 if(hd_dust) then
2505 call dust_get_dt(wprim, ixi^l, ixo^l, dtnew, dx^d, x)
2506 end if
2507
2508 if(hd_viscosity) then
2509 call viscosity_get_dt(wprim,ixi^l,ixo^l,dtnew,dx^d,x)
2510 end if
2511
2512 if(hd_gravity) then
2513 call gravity_get_dt(wprim,ixi^l,ixo^l,dtnew,dx^d,x)
2514 end if
2515
2516 if (hd_cak_force) then
2517 call cak_get_dt(wprim,ixi^l,ixo^l,dtnew,dx^d,x)
2518 end if
2519
2520 if(hd_radiation_fld) then
2521 call fld_radforce_get_dt(wprim,ixi^l,ixo^l,dtnew,dx^d,x,fld_fl)
2522 endif
2523
2524 end subroutine hd_get_dt
2525
2526 !> Wrappers for the FLD implicit (MG diffusion) hooks: phys_implicit_update /
2527 !> phys_evaluate_implicit have fixed interfaces with no fluid argument, so
2528 !> these inject the module's fld_fl object into the threaded fld routines.
2529 subroutine hd_fld_implicit_update(dtfactor,qdt,qtC,psa,psb)
2531 use mod_fld, only: fld_implicit_update
2532 type(state), target :: psa(max_blocks)
2533 type(state), target :: psb(max_blocks)
2534 double precision, intent(in) :: qdt
2535 double precision, intent(in) :: qtc
2536 double precision, intent(in) :: dtfactor
2537
2538 call fld_implicit_update(dtfactor,qdt,qtc,psa,psb,fld_fl)
2539 end subroutine hd_fld_implicit_update
2540
2541 subroutine hd_fld_evaluate_implicit(qtC,psa)
2544 type(state), target :: psa(max_blocks)
2545 double precision, intent(in) :: qtc
2546
2547 call fld_evaluate_implicit(qtc,psa,fld_fl)
2548 end subroutine hd_fld_evaluate_implicit
2549
2550 function hd_kin_en(w, ixI^L, ixO^L, inv_rho) result(ke)
2551 use mod_global_parameters, only: nw, ndim
2552 integer, intent(in) :: ixi^l, ixo^l
2553 double precision, intent(in) :: w(ixi^s, nw)
2554 double precision :: ke(ixo^s)
2555 double precision, intent(in), optional :: inv_rho(ixo^s)
2556
2557 if (present(inv_rho)) then
2558 ke = 0.5d0 * sum(w(ixo^s, mom(:))**2, dim=ndim+1) * inv_rho
2559 else
2560 ke = 0.5d0 * sum(w(ixo^s, mom(:))**2, dim=ndim+1) / w(ixo^s, rho_)
2561 end if
2562 end function hd_kin_en
2563
2564 function hd_inv_rho(w, ixI^L, ixO^L) result(inv_rho)
2565 use mod_global_parameters, only: nw, ndim
2566 integer, intent(in) :: ixi^l, ixo^l
2567 double precision, intent(in) :: w(ixi^s, nw)
2568 double precision :: inv_rho(ixo^s)
2569
2570 ! Can make this more robust
2571 inv_rho = 1.0d0 / w(ixo^s, rho_)
2572 end function hd_inv_rho
2573
2574 subroutine hd_handle_small_values(primitive, w, x, ixI^L, ixO^L, subname)
2575 ! handles hydro (density,pressure,velocity) bootstrapping
2576 ! any negative dust density is flagged as well (and throws an error)
2577 ! small_values_method=replace also for dust
2581 logical, intent(in) :: primitive
2582 integer, intent(in) :: ixi^l,ixo^l
2583 double precision, intent(inout) :: w(ixi^s,1:nw)
2584 double precision, intent(in) :: x(ixi^s,1:ndim)
2585 character(len=*), intent(in) :: subname
2586
2587 integer :: n,idir
2588 logical :: flag(ixi^s,1:nw)
2589
2590 call hd_check_w(primitive, ixi^l, ixo^l, w, flag)
2591
2592 if (any(flag)) then
2593 select case (small_values_method)
2594 case ("replace")
2595 where(flag(ixo^s,rho_)) w(ixo^s,rho_) = small_density
2596 do idir = 1, ndir
2597 if(small_values_fix_iw(mom(idir))) then
2598 where(flag(ixo^s,rho_)) w(ixo^s, mom(idir)) = 0.0d0
2599 end if
2600 end do
2601 if(hd_radiation_fld)then
2602 if (small_values_fix_iw(r_e)) then
2603 where(flag(ixo^s,r_e)) w(ixo^s,r_e) = small_r_e
2604 end if
2605 end if
2606 if(hd_energy)then
2607 if(small_values_fix_iw(e_)) then
2608 if(primitive) then
2609 where(flag(ixo^s,rho_)) w(ixo^s, p_) = small_pressure
2610 else
2611 where(flag(ixo^s,rho_)) w(ixo^s, e_) = small_e + hd_kin_en(w,ixi^l,ixo^l)
2612 endif
2613 end if
2614 endif
2615
2616 if(hd_energy) then
2617 if(primitive) then
2618 where(flag(ixo^s,e_)) w(ixo^s,p_) = small_pressure
2619 else
2620 where(flag(ixo^s,e_))
2621 ! Add kinetic energy
2622 w(ixo^s,e_) = small_e + hd_kin_en(w,ixi^l,ixo^l)
2623 end where
2624 end if
2625 end if
2626
2627 if(hd_dust)then
2628 do n=1,dust_n_species
2629 where(flag(ixo^s,dust_rho(n))) w(ixo^s,dust_rho(n)) = 0.0d0
2630 do idir = 1, ndir
2631 where(flag(ixo^s,dust_rho(n))) w(ixo^s,dust_mom(idir,n)) = 0.0d0
2632 enddo
2633 enddo
2634 endif
2635 case ("average")
2636 if(primitive)then
2637 ! averaging for all primitive fields, including dust
2638 call small_values_average(ixi^l, ixo^l, w, x, flag)
2639 else
2640 ! do averaging of density
2641 call small_values_average(ixi^l, ixo^l, w, x, flag, rho_)
2642 if(hd_energy) then
2643 ! do averaging of pressure
2644 ! w(ixI^S,p_)=(hd_gamma-1.d0)*(w(ixI^S,e_) &
2645 ! -0.5d0*sum(w(ixI^S, mom(:))**2, dim=ndim+1)/w(ixI^S,rho_))
2646 call eos%get_thermal_pressure(w, x, ixi^l, ixo^l, w(ixo^s,p_))
2647 call small_values_average(ixi^l, ixo^l, w, x, flag, p_)
2648 do idir = 1, ndir
2649 w(ixo^s,mom(idir)) = w(ixo^s,mom(idir))/w(ixo^s,rho_) !> Convert to velocity to be compliant with p_to_e
2650 end do
2651 call eos%p_to_e(ixi^l, ixo^l, w, x)
2652 do idir = 1, ndir
2653 w(ixo^s,mom(idir)) = w(ixo^s,mom(idir))*w(ixo^s,rho_) !> Restore momentum (conserved-form invariant)
2654 end do
2655 ! w(ixI^S,e_)=w(ixI^S,p_)/(hd_gamma-1.d0) &
2656 ! +0.5d0*sum(w(ixI^S, mom(:))**2, dim=ndim+1)/w(ixI^S,rho_)
2657 end if
2658 if(hd_radiation_fld) then
2659 ! do averaging of radiative energy density
2660 call small_values_average(ixi^l, ixo^l, w, x, flag, r_e)
2661 endif
2662 if(hd_dust)then
2663 do n=1,dust_n_species
2664 where(flag(ixo^s,dust_rho(n))) w(ixo^s,dust_rho(n)) = 0.0d0
2665 do idir = 1, ndir
2666 where(flag(ixo^s,dust_rho(n))) w(ixo^s,dust_mom(idir,n)) = 0.0d0
2667 enddo
2668 enddo
2669 endif
2670 endif
2671 case default
2672 if(.not.primitive) then
2673 !convert w to primitive
2674 ! Calculate pressure = (gamma-1) * (e-ek)
2675 if(hd_energy) then
2676 call eos%get_thermal_pressure(w, x, ixi^l, ixo^l, w(ixo^s,p_))
2677 ! w(ixO^S,p_)=(hd_gamma-1.d0)*(w(ixO^S,e_)-hd_kin_en(w,ixI^L,ixO^L))
2678 end if
2679 ! Convert gas momentum to velocity
2680 do idir = 1, ndir
2681 w(ixo^s, mom(idir)) = w(ixo^s, mom(idir))/w(ixo^s,rho_)
2682 end do
2683 end if
2684 ! NOTE: dust entries may still have conserved values here
2685 call small_values_error(w, x, ixi^l, ixo^l, flag, subname)
2686 end select
2687 end if
2688 if (hd_fip) call hd_bound_fip(primitive, ixi^l, ixo^l, w)
2689 end subroutine hd_handle_small_values
2690
2691 !> Well-balanced transform: (T, v, q) variable change.
2692 !>
2693 !> Replaces (ρ, v, p) with (T, v, q) where:
2694 !> T(i) = p(i) / ρ(i) [temperature — smooth, locally computed]
2695 !> q(i) = p(i) / p_eq(i) [pressure ratio — ≈1 in HSE]
2696 !> Well-balanced post-prolongation correction for AMR.
2697 !>
2698 !> After prolongation interpolates to a fine grid, the pressure does not
2699 !> satisfy the discrete HSE recurrence at the fine resolution (linear
2700 !> interpolation of an exponential profile introduces O(dx^2/H^2) error).
2701 !>
2702 !> This routine rebuilds p from the multiplicative HSE recurrence using the
2703 !> interpolated T = p/rho (which IS smooth and well-interpolated). The
2704 !> density is updated as rho = p_new / T for consistency.
2705 !>
2706 !> On entry: w contains primitive (rho, v, p).
2707 !> On exit: w contains HSE-corrected primitive (rho_new, v, p_new).
2708 !>
2709 !> The recurrence is anchored at the block midpoint, where the parent
2710 !> cell-centre value is exact (no interpolation error).
2711 subroutine hd_wb_prolong(ixI^L, ixO^L, w, x)
2713 use mod_usr_methods, only: usr_gravity
2714
2715 integer, intent(in) :: ixi^l, ixo^l
2716 double precision, intent(inout) :: w(ixi^s, 1:nw)
2717 double precision, intent(in) :: x(ixi^s, 1:ndim)
2718
2719 double precision :: gravity_field(ixi^s, 1:ndim)
2720 double precision :: wb_t(ixi^s), p_eq(ixi^s)
2721 double precision :: dx_idims
2722 double precision :: alpha(ixi^s), beta(ixi^s)
2723 {^ifoned
2724 integer :: ix1, ix_mid
2725 }
2726
2727 ! T = p/rho at all fine cells (smooth from interpolation)
2728 wb_t(ixo^s) = w(ixo^s, p_) / w(ixo^s, rho_)
2729
2730 ! Get gravity at fine cell centres
2731 call usr_gravity(ixi^l, ixo^l, w, x, gravity_field)
2732
2733 dx_idims = dxlevel(1)
2734
2735 {^ifoned
2736 ! α_i = (dx/2) · g_i / T_i (vectorised over ixO)
2737 alpha(ixomin1:ixomax1) = 0.5d0 * dx_idims &
2738 * gravity_field(ixomin1:ixomax1, 1) / wb_t(ixomin1:ixomax1)
2739
2740 ! Anchor at block midpoint (least interpolation error)
2741 ix_mid = (ixomin1 + ixomax1) / 2
2742 p_eq(ix_mid) = w(ix_mid, p_)
2743
2744 ! Forward: β_i = (1 + α_i) / (1 - α_{i+1}) (vectorised)
2745 beta(ix_mid:ixomax1-1) = (1.0d0 + alpha(ix_mid:ixomax1-1)) &
2746 / (1.0d0 - alpha(ix_mid+1:ixomax1))
2747 do ix1 = ix_mid + 1, ixomax1
2748 p_eq(ix1) = p_eq(ix1 - 1) * beta(ix1 - 1)
2749 end do
2750
2751 ! Backward: β_i = (1 - α_{i+1}) / (1 + α_i) (vectorised)
2752 beta(ixomin1:ix_mid-1) = (1.0d0 - alpha(ixomin1+1:ix_mid)) &
2753 / (1.0d0 + alpha(ixomin1:ix_mid-1))
2754 do ix1 = ix_mid - 1, ixomin1, -1
2755 p_eq(ix1) = p_eq(ix1 + 1) * beta(ix1)
2756 end do
2757
2758 ! Replace interpolated p with recurrence-derived p, update rho = p/T
2759 w(ixomin1:ixomax1, p_) = p_eq(ixomin1:ixomax1)
2760 w(ixomin1:ixomax1, rho_) = p_eq(ixomin1:ixomax1) / wb_t(ixomin1:ixomax1)
2761 }
2762
2763 end subroutine hd_wb_prolong
2764
2765 !> Well-balanced transform for reconstruction.
2766 !>
2767 !> On entry: w contains primitive (rho, v, p).
2768 !> On exit: w(rho_) = T = p/rho, w(p_) = q = p/p_eq.
2769 !> wb_T returns the saved cell-centre temperature.
2770 !> wb_phi returns the cell-centre equilibrium pressure.
2771 !> wb_phi_face returns the face-centre equilibrium pressure.
2772 !>
2773 !> p_eq from multiplicative trapezoidal recurrence (always positive).
2774 !> In HSE: q = 1, T varies smoothly -> limiter sees flat q -> exact balance.
2775 subroutine hd_wb_transform(ixI^L, ixO^L, idims, w, x, wb_phi, &
2776 wb_phi_face, wb_T)
2778 use mod_usr_methods, only: usr_gravity
2779
2780 integer, intent(in) :: ixi^l, ixo^l, idims
2781 double precision, intent(inout) :: w(ixi^s, 1:nw)
2782 double precision, intent(in) :: x(ixi^s, 1:ndim)
2783 double precision, intent(out) :: wb_phi(ixi^s)
2784 double precision, intent(out) :: wb_phi_face(ixi^s)
2785 double precision, intent(out) :: wb_t(ixi^s)
2786
2787 double precision :: gravity_field(ixi^s, 1:ndim)
2788 double precision :: dx_idims
2789 double precision :: alpha(ixi^s), beta(ixi^s)
2790 {^ifoned
2791 integer :: ix1
2792 }
2793
2794 ! Save T = p/ρ at all cell centers
2795 wb_t(ixi^s) = w(ixi^s, p_) / w(ixi^s, rho_)
2796
2797 ! Get gravity acceleration at all cell centers
2798 call usr_gravity(ixi^l, ixi^l, w, x, gravity_field)
2799
2800 dx_idims = dxlevel(idims)
2801
2802 ! Multiplicative trapezoidal recurrence for p_eq.
2803 ! p_eq(i+1) = p_eq(i) · (1 + α(i)) / (1 - α(i+1))
2804 ! Always positive for dx < 2H.
2805 !
2806 ! Vectorised: precompute α and β factors, then sequential cumulative product.
2807 {^ifoned
2808 ! α_i = (dx/2) · g_i / T_i (vectorised)
2809 alpha(iximin1:iximax1) = 0.5d0 * dx_idims &
2810 * gravity_field(iximin1:iximax1, idims) / wb_t(iximin1:iximax1)
2811
2812 ! β_i = (1 + α_i) / (1 - α_{i+1}) (vectorised)
2813 beta(iximin1:iximax1-1) = (1.0d0 + alpha(iximin1:iximax1-1)) &
2814 / (1.0d0 - alpha(iximin1+1:iximax1))
2815
2816 ! Cumulative product (sequential, unavoidable data dependency)
2817 wb_phi(iximin1) = w(iximin1, p_)
2818 do ix1 = iximin1 + 1, iximax1
2819 wb_phi(ix1) = wb_phi(ix1 - 1) * beta(ix1 - 1)
2820 end do
2821 }
2822
2823 ! Face equilibrium pressure: isothermal half-step from cell center
2824 wb_phi_face(ixi^s) = wb_phi(ixi^s) * (1.0d0 + 0.5d0 * dx_idims * &
2825 gravity_field(ixi^s, idims) / wb_t(ixi^s))
2826
2827 ! Transform: w(rho_) = T, w(p_) = q = p/p_eq
2828 w(ixi^s, rho_) = wb_t(ixi^s)
2829 w(ixi^s, p_) = w(ixi^s, p_) / wb_phi(ixi^s)
2830
2831 end subroutine hd_wb_transform
2832
2833 !> Well-balanced inverse: restore physical (rho, v, p) at interfaces.
2834 !>
2835 !> On entry:
2836 !> wLp/wRp(rho_) = T (reconstructed), wLp/wRp(p_) = q (reconstructed)
2837 !> w(rho_) = T (cell-centre), w(p_) = q (cell-centre)
2838 !> wb_phi/wb_phi_face = cell/face equilibrium pressures from transform
2839 !> wb_T = saved cell-centre temperature from transform
2840 !>
2841 !> On exit:
2842 !> wLp/wRp(rho_) = rho_face, wLp/wRp(p_) = p_face (physical primitives)
2843 !> w(rho_) = rho_cell, w(p_) = p_cell (restored cell-centre primitives)
2844 !>
2845 !> Pressure: p_face = q_face * p_eq_face (multiplicative, well-balanced)
2846 !> Density: rho_face = p_face / T_blended
2847 !>
2848 !> Blends between shared T (well-balanced, zero HLL dissipation) and
2849 !> individual limiter-reconstructed T (non-WB, full dissipation) using
2850 !> a contact detector sigma = |T_faceL - T_faceR| / T_avg.
2851 !> In HSE: T is smooth → sigma ≈ 0 → pure WB (ρ_L = ρ_R).
2852 !> At contacts: T jump → sigma > 0 → ρ_L ≠ ρ_R → dissipation restored.
2853 subroutine hd_wb_inverse(ixI^L, ixL^L, ixR^L, idims, wLp, wRp, w, &
2854 wb_phi, wb_phi_face, wb_T)
2856
2857 integer, intent(in) :: ixi^l, ixl^l, ixr^l, idims
2858 double precision, intent(inout) :: wlp(ixi^s, 1:nw), wrp(ixi^s, 1:nw)
2859 double precision, intent(inout) :: w(ixi^s, 1:nw)
2860 double precision, intent(in) :: wb_phi(ixi^s), wb_phi_face(ixi^s)
2861 double precision, intent(in) :: wb_t(ixi^s)
2862
2863 double precision :: t_shared(ixi^s)
2864 double precision :: t_face_l(ixi^s), t_face_r(ixi^s)
2865 double precision :: sigma(ixi^s), t_for_rhol(ixi^s), t_for_rhor(ixi^s)
2866
2867 ! Shared face temperature: T_face(i) = ½(T(i) + T(i+1))
2868 {^ifoned
2869 t_shared(iximin1:iximax1-1) = 0.5d0 * (wb_t(iximin1:iximax1-1) &
2870 + wb_t(iximin1+1:iximax1))
2871 t_shared(iximax1) = wb_t(iximax1)
2872 }
2873
2874 ! Save limiter-reconstructed T before rho_ slot is overwritten
2875 ! (rho_ still holds T from the WB transform at this point)
2876 t_face_l(ixl^s) = wlp(ixl^s, rho_)
2877 t_face_r(ixr^s) = wrp(ixr^s, rho_)
2878
2879 ! Contact detector: relative T jump across interface
2880 ! sigma = 0 in HSE (smooth T), sigma > 0 at contacts (T discontinuity)
2881 sigma(ixl^s) = dabs(t_face_l(ixl^s) - t_face_r(ixr^s)) &
2882 / (0.5d0 * (t_face_l(ixl^s) + t_face_r(ixr^s)) + smalldouble)
2883 sigma(ixl^s) = min(sigma(ixl^s), 1.0d0)
2884
2885 ! Blended T for density recovery
2886 t_for_rhol(ixl^s) = (1.d0 - sigma(ixl^s)) * t_shared(ixl^s) &
2887 + sigma(ixl^s) * t_face_l(ixl^s)
2888 t_for_rhor(ixr^s) = (1.d0 - sigma(ixl^s)) * t_shared(ixr^s) &
2889 + sigma(ixl^s) * t_face_r(ixr^s)
2890
2891 ! Pressure inverse: p = q · p_eq_face (multiplicative)
2892 wlp(ixl^s, p_) = wlp(ixl^s, p_) * wb_phi_face(ixl^s)
2893 wrp(ixr^s, p_) = wrp(ixr^s, p_) * wb_phi_face(ixr^s)
2894
2895 ! Density inverse: ρ = p / T_blended
2896 wlp(ixl^s, rho_) = wlp(ixl^s, p_) / t_for_rhol(ixl^s)
2897 wrp(ixr^s, rho_) = wrp(ixr^s, p_) / t_for_rhor(ixr^s)
2898
2899 ! Restore cell-center values
2900 w(ixi^s, p_) = w(ixi^s, p_) * wb_phi(ixi^s)
2901 w(ixi^s, rho_) = w(ixi^s, p_) / wb_t(ixi^s)
2902
2903 end subroutine hd_wb_inverse
2904
2905end module mod_hd_phys
Calculate w(iw)=w(iw)+qdt*SOURCE[wCT,qtC,x] within ixO for all indices iw=iwmin......
Module with basic data types used in amrvac.
integer, parameter std_len
Default length for strings.
Module to include CAK radiation line force in (magneto)hydrodynamic models Computes both the force fr...
subroutine, public cak_add_source(qdt, ixil, ixol, wct, w, x, energy, qsourcesplit, active)
w[iw]=w[iw]+qdt*S[wCT,qtC,x] where S is the source based on wCT within ixO
subroutine, public cak_get_dt(wprim, ixil, ixol, dtnew, dxd, x)
Check time step for total radiation contribution.
subroutine, public cak_init(phys_gamma)
Initialize the module.
subroutine, public mpistop(message)
Exit MPI-AMRVAC with an error message.
Module for physical and numeric constants.
double precision, parameter bigdouble
A very large real number.
Module for including dust species, which interact with the gas through a drag force.
Definition mod_dust.t:3
subroutine, public dust_add_source(qdt, ixil, ixol, wct, w, x, qsourcesplit, active)
w[iw]= w[iw]+qdt*S[wCT, x] where S is the source based on wCT within ixO
Definition mod_dust.t:533
subroutine, public dust_evaluate_implicit(qtc, psa)
inplace update of psa==>F_im(psa)
Definition mod_dust.t:586
integer, dimension(:, :), allocatable, public, protected dust_mom
Indices of the dust momentum densities.
Definition mod_dust.t:47
integer, public, protected dust_n_species
The number of dust species.
Definition mod_dust.t:37
subroutine, public dust_get_flux_prim(w, x, ixil, ixol, idim, f)
Definition mod_dust.t:276
integer, dimension(:), allocatable, public, protected dust_rho
Indices of the dust densities.
Definition mod_dust.t:44
subroutine, public dust_get_cmax(w, x, ixil, ixol, idim, cmax, cmin)
Definition mod_dust.t:1019
subroutine, public dust_check_w(ixil, ixol, w, x, flag)
Definition mod_dust.t:194
subroutine, public dust_check_params()
Definition mod_dust.t:154
subroutine, public dust_get_cmax_prim(w, x, ixil, ixol, idim, cmax, cmin)
Definition mod_dust.t:1043
subroutine, public dust_get_dt(wprim, ixil, ixol, dtnew, dxd, x)
Get dt related to dust and gas stopping time (Laibe 2011).
Definition mod_dust.t:901
subroutine, public dust_init(g_rho, g_mom, g_energy)
Definition mod_dust.t:95
subroutine, public dust_implicit_update(dtfactor, qdt, qtc, psb, psa)
Implicit solve of psb=psa+dtfactor*dt*F_im(psb).
Definition mod_dust.t:655
EoS state container – the single thermodynamic authority for AMRVAC.
type(eos_container), allocatable, public eos
The single EoS state object, allocated in eos_init and shared (read-mostly) across all EoS sub-module...
integer, parameter, public eos_type_lte
LTE (Saha-table) EoS kernels and finalise for the eos% family.
Definition mod_eos_lte.t:12
double precision function, public lte_gamma1_from_p(pres, rho_val)
Gamma_1 of one cell from (p, rho): the ideal value where fully ionised, else the pressure- indexed ta...
double precision function, public lte_p_from_eint(eint_val, nh_val, log_nh_val, inv_rho)
LTE pressure from internal energy in one cell: ideal without ionE, else the table floor,...
PI (partial-ionisation) ionisation-degree backend for the eos% family.
Equation of state for AMRVAC, handled through a single eos_container object.
Definition mod_eos.t:30
Module for escape probability radiative cooling modification.
subroutine, public escape_prob_init(iw_colmass, escape_sym, escape_height)
Register escape probability parameters. Called during hd_phys_init (before mesh parameters are availa...
Module for flux conservation near refinement boundaries.
Module for flux limited diffusion (FLD)-approximation in Radiation-(Magneto)hydrodynamics simulations...
Definition mod_fld.t:13
logical fld_no_mg
Definition mod_fld.t:35
double precision, public fld_bisect_tol
Tolerance for bisection method for Energy sourceterms This is a percentage of the minimum of gas- and...
Definition mod_fld.t:25
subroutine, public fld_radforce_get_dt(w, ixil, ixol, dtnew, dxd, x, fl)
get dt limit for radiation force and FLD explicit source additions NOTE: w is primitive on entry
Definition mod_fld.t:397
double precision, public fld_diff_tol
Tolerance for radiative Energy diffusion.
Definition mod_fld.t:27
character(len=40) fld_fluxlimiter
flux limiter choice
Definition mod_fld.t:41
character(len=40) fld_opal_table
Definition mod_fld.t:39
double precision, public fld_cnorm
Definition mod_fld.t:36
double precision, public fld_kappa0
Opacity value when using constant opacity.
Definition mod_fld.t:22
subroutine, public add_fld_rad_force(qdt, ixil, ixol, wct, wctprim, w, x, qsourcesplit, active, fl)
w[iw]=w[iw]+qdt*S[wCT,qtC,x] where S is the source based on wCT within ixO This subroutine handles th...
Definition mod_fld.t:240
character(len=40) fld_opacity_law
switches for opacity
Definition mod_fld.t:38
character(len=40) fld_interaction_method
Which method to find the root for the energy interaction polynomial.
Definition mod_fld.t:47
subroutine, public fld_get_radpress(w, x, ixil, ixol, rad_pressure, fl)
Returns Radiation Pressure as tensor NOTE: w is primitive on entry.
Definition mod_fld.t:540
logical fld_radforce_split
source split for energy interact and radforce:
Definition mod_fld.t:18
logical fld_bound_diff
switches for using changed cmax-cmin bounds
Definition mod_fld.t:32
subroutine, public fld_implicit_update(dtfactor, qdt, qtc, psa, psb, fl)
Calling all subroutines to perform the multigrid method Communicates rad_e and diff_coeff to multigri...
Definition mod_fld.t:800
subroutine, public fld_evaluate_implicit(qtc, psa, fl)
inplace update of psa==>F_im(psa)
Definition mod_fld.t:948
subroutine, public fld_init(gas_eos_type, gas_ion_energy, gas_pi_table)
Initialising FLD-module Read opacities Initialise Multigrid and adimensionalise kappa.
Definition mod_fld.t:116
integer nth_for_diff_mg
diffusion coefficient stencil control
Definition mod_fld.t:45
Module with geometry-related routines (e.g., divergence, curl).
Definition mod_geometry.t:2
integer coordinate
Definition mod_geometry.t:7
integer, parameter spherical
integer, parameter cylindrical
integer, parameter cartesian_expansion
subroutine gradient(q, ixil, ixol, idir, gradq, nth_in)
This module contains definitions of global parameters and variables and some generic functions/subrou...
type(state), pointer block
Block pointer for using one block and its previous state.
logical h_correction
If true, do H-correction to fix the carbuncle problem at grid-aligned shocks.
double precision arad_norm
Normalised radiation constant.
double precision unit_time
Physical scaling factor for time.
double precision unit_density
Physical scaling factor for density.
double precision unit_opacity
Physical scaling factor for Opacity.
integer, parameter unitpar
file handle for IO
double precision unit_mass
Physical scaling factor for mass.
logical use_imex_scheme
whether IMEX in use or not
integer, dimension(3, 3) kr
Kronecker delta tensor.
double precision unit_numberdensity
Physical scaling factor for number density.
character(len=std_len) convert_type
Which format to use when converting.
double precision unit_pressure
Physical scaling factor for pressure.
integer, parameter ndim
Number of spatial dimensions for grid variables.
double precision unit_length
Physical scaling factor for length.
logical phys_escape_prob
Use escape probability for radiative cooling modification.
double precision const_rad_a
Physical factors useful for radiation fld.
double precision cmax_global
global fastest wave speed needed in fd scheme and glm method
logical use_particles
Use particles module or not.
character(len=std_len), dimension(:), allocatable par_files
Which par files are used as input.
integer mype
The rank of the current MPI task.
double precision dt
global time step
integer ndir
Number of spatial dimensions (components) for vector variables.
double precision courantpar
The Courant (CFL) number used for the simulation.
double precision, dimension(:), allocatable, parameter d
logical slab
Cartesian geometry or not.
integer nwauxio
Number of auxiliary variables that are only included in output.
double precision unit_velocity
Physical scaling factor for velocity.
double precision c_norm
Normalised speed of light.
double precision unit_temperature
Physical scaling factor for temperature.
double precision unit_radflux
Physical scaling factor for radiation flux.
double precision, dimension(10) phys_trac_zone_splits
logical si_unit
Use SI units (.true.) or use cgs units (.false.).
double precision, dimension(:,:), allocatable dx
spatial steps for all dimensions at all levels
integer nghostcells
Number of ghost cells surrounding a grid.
double precision, dimension(:), allocatable w_refine_weight
Weights of variables used to calculate error for mesh refinement.
logical phys_trac
Use TRAC for MHD or 1D HD.
logical need_global_cmax
need global maximal wave speed
double precision, dimension(^nd) dxlevel
store unstretched cell size of current level
logical use_multigrid
Use multigrid (only available in 2D and 3D).
integer max_blocks
The maximum number of grid blocks in a processor.
integer r_
Indices for cylindrical coordinates FOR TESTS, negative value when not used:
integer boundspeed
bound (left/min and right.max) speed of Riemann fan
integer, parameter unitconvert
double precision unit_erad
Physical scaling factor for radiation energy density.
Module for including gravity in (magneto)hydrodynamics simulations.
Definition mod_gravity.t:2
logical grav_split
source split or not
Definition mod_gravity.t:6
subroutine gravity_get_dt(wprim, ixil, ixol, dtnew, dxd, x)
Definition mod_gravity.t:81
subroutine gravity_init()
Initialize the module.
Definition mod_gravity.t:26
subroutine gravity_add_source(qdt, ixil, ixol, wct, wctprim, w, x, energy, qsourcesplit, active)
w[iw]=w[iw]+qdt*S[wCT,qtC,x] where S is the source based on wCT within ixO
Definition mod_gravity.t:43
Hydrodynamics physics module.
Definition mod_hd_phys.t:2
integer, public, protected m
Definition mod_hd_phys.t:73
subroutine, public hd_check_params
logical, public, protected hd_energy
Whether an energy equation is used.
Definition mod_hd_phys.t:15
logical, public, protected hd_dust
Whether dust is added.
Definition mod_hd_phys.t:35
subroutine, public hd_get_pthermal_plus_pradiation(w, x, ixil, ixol, pth_plus_prad)
calculates the sum of the gas pressure and max Prad tensor element NOTE: only for diagnostic purposes...
subroutine, public hd_ei_to_e(ixil, ixol, w, x)
Transform internal energy to total energy.
integer, public, protected e_
Index of the energy density (-1 if not present).
Definition mod_hd_phys.t:79
logical, public, protected hd_radiative_cooling
Whether radiative cooling is added.
Definition mod_hd_phys.t:29
double precision, public, protected rr
subroutine, public hd_get_temperature_from_etot(w, x, ixil, ixol, res)
Calculate temperature=p/rho when in e_ the total energy is stored.
logical, public, protected hd_fld_pradtensor
Definition mod_hd_phys.t:42
integer, public, protected hd_trac_type
logical, public, protected hd_particles
Whether particles module is added.
Definition mod_hd_phys.t:53
logical, public, protected hd_fip
Whether FIP passive scalar is enabled.
Definition mod_hd_phys.t:97
double precision, public hypertc_kappa
Thermal-conductivity prefactor in hyperbolic TC, set in hd_physical_units. Spitzer form: κ(T) = hyper...
logical, public, protected hd_radiation_fld
Whether radiation-gas interaction is handled using flux limited diffusion.
Definition mod_hd_phys.t:41
double precision, public, protected hd_htc_beta
Face-recipe heat-wave speed scaling: c_HTC,f = hd_htc_beta * c_max,f. Higher value -> closer to diffu...
double precision, public, protected hd_trac_delta
Johnston 2021 resolution parameter delta (default 0.5).
type(tc_fluid), allocatable, public tc_fl
Definition mod_hd_phys.t:25
subroutine, public hd_check_w(primitive, ixil, ixol, w, flag)
Returns logical argument flag where values are ok.
logical, public, protected hd_viscosity
Whether viscosity is added.
Definition mod_hd_phys.t:47
integer, public, protected r_e
Index of the radiation energy (when fld active).
Definition mod_hd_phys.t:88
integer, public, protected c
Indices of the momentum density for the form of better vectorization.
Definition mod_hd_phys.t:73
subroutine, public hd_get_csound2(w, x, ixil, ixol, csound2)
Calculate the square of the thermal sound speed csound2 within ixO^L. For conserved w: extracts pther...
integer, public, protected tcoff_
Index of the cutoff temperature for the TRAC method.
double precision, public, protected he_ion_fr2
Ratio of number He2+ / number He+ + He2+ He_ion_fr2 = He2+/(He2+ + He+).
double precision, public, protected hd_htc_gradt_floor
Gradient deadband: zero out the Spitzer face flux when abs(T_R - T_L) / max(T_L, T_R) < hd_htc_gradT_...
integer, public, protected te_
Indices of temperature.
Definition mod_hd_phys.t:91
integer, dimension(:), allocatable, public, protected mom
Indices of the momentum density.
Definition mod_hd_phys.t:70
subroutine, public hd_get_pradiation_from_prim(w, x, ixil, ixol, prad)
Calculate radiation pressure within ixO^L NOTE: w is primitive on entry here! NOTE: used in FLD modul...
double precision, public, protected hd_trac_tfix
fixed cutoff temperature in K for hd_trac_type=0 (Lionello et al. 2009, Mikic et al....
double precision, public, protected hd_htc_hyp_diff
Hyperdiffusion coefficient applied to the cell-refreshed q at the end of each face-recipe substep....
logical, public, protected hd_hyperbolic_thermal_conduction
Whether hyperbolic thermal conduction (Cattaneo relaxation) is used. 1D only : the q-variable is trea...
Definition mod_hd_phys.t:22
double precision, public, protected h_ion_fr
Helium abundance over Hydrogen He_abundance is set in &eos_list and accessed via eosHe_abundance Ioni...
double precision function, dimension(ixo^s), public hd_kin_en(w, ixil, ixol, inv_rho)
logical, public, protected hd_cak_force
Whether CAK radiation line force is activated.
Definition mod_hd_phys.t:59
subroutine, public hd_phys_init()
Initialize the module.
integer, dimension(:), allocatable, public, protected tracer
Indices of the tracers.
Definition mod_hd_phys.t:76
subroutine, public hd_get_csrad2(w, x, ixil, ixol, csound)
Calculate modified squared sound speed for FLD NOTE: only for diagnostic purposes,...
logical, public, protected hd_thermal_conduction
Whether thermal conduction is added.
Definition mod_hd_phys.t:18
integer, public, protected q_
Index of the hyperbolic-TC heat-flux variable (-1 if not present).
double precision, public hd_htc_validity_max_runtime
Running max of l_r,f / dx_f across all face-recipe calls since simulation start. Inspect post-hoc via...
double precision, public hd_adiab
gamma is set in &eos_list and accessed via eosgamma
subroutine, public hd_get_trad(w, x, ixil, ixol, trad)
Calculates radiation temperature.
subroutine, public hd_get_csrad2_prim(w, x, ixil, ixol, csound)
Calculate modified squared sound speed for FLD NOTE: w is primitive on entry here!...
integer, public, protected rho_
Whether plasma is partially ionized.
Definition mod_hd_phys.t:67
subroutine, public hd_handle_small_values(primitive, w, x, ixil, ixol, subname)
double precision, public, protected hd_htc_sat_alpha
Cowie-McKee saturation coefficient: q_sat = hd_htc_sat_alpha * rho * c_s^3. Standard convention is al...
double precision, public, protected hd_htc_pos_eta
Per-face energy-positivity safety fraction: |q_f^{n+1/2} dt A_f| <= hd_htc_pos_eta * min(e_int_L V_L,...
logical, public, protected hd_radiative_transfer
Whether the short-characteristics radiative transfer is used, with its settings in &rt_list.
Definition mod_hd_phys.t:31
double precision, public, protected he_ion_fr
Ionization fraction of He He_ion_fr = (He2+ + He+)/(He2+ + He+ + He).
logical, public, protected hd_gravity
Whether gravity is added.
Definition mod_hd_phys.t:50
type(fld_fluid), allocatable, public fld_fl
Radiation fluid object (gas-EoS callbacks for FLD), wired in hd_link_eos.
Definition mod_hd_phys.t:44
double precision, public, protected hd_htc_kappa_override
Optional parfile override for hypertc_kappa (e.g. to match a constant-κ parabolic TC run for benchmar...
integer, public, protected hd_trac_nzones
integer, public, protected c_
Definition mod_hd_phys.t:73
type(rc_fluid), allocatable, public rc_fl
Definition mod_hd_phys.t:32
logical, public, protected hd_dust_implicit
Whether dust is added using and implicit update in IMEX.
Definition mod_hd_phys.t:38
logical, public, protected hd_trac
Whether TRAC method is used.
integer, public, protected fip_
Index of the FIP passive scalar rho*fip in conserved form, fip in primitive form.
Definition mod_hd_phys.t:94
double precision, public, protected hd_htc_validity_warn
Validity-monitor threshold for l_r,f / Delta_x_f. Warn if any face exceeds this in a given block (pri...
integer, public, protected hd_n_tracer
Number of tracer species.
Definition mod_hd_phys.t:62
type(te_fluid), allocatable, public te_fl_hd
Definition mod_hd_phys.t:26
double precision, dimension(10), public, protected hd_trac_zone_splits
logical, public, protected hd_rotating_frame
Whether rotating frame is activated.
Definition mod_hd_phys.t:56
logical, public, protected hd_htc_sat
Whether saturation is considered for hyperbolic TC.
Definition mod_hd_phys.t:24
logical, public, protected hd_well_balanced
Whether well-balanced reconstruction is used (Kaeppeli & Mishra style).
integer, public, protected iw_colmass
Index into wextra for escape probability column mass.
integer, public, protected p_
Index of the gas pressure (-1 if not present) should equal e_.
Definition mod_hd_phys.t:82
integer, public, protected ne_
Index of the electron number density for LTE module.
Definition mod_hd_phys.t:85
subroutine, public hd_e_to_ei(ixil, ixol, w, x)
Transform total energy to internal energy.
Module containing all the particle routines.
subroutine particles_init()
Initialize particle data and parameters.
This module defines the procedures of a physics module. It contains function pointers for the various...
Definition mod_physics.t:4
module radiative cooling – add optically thin radiative cooling
subroutine radiative_cooling_init_params(phys_gamma, he_abund)
Radiative cooling initialization.
subroutine findl(tpoint, lpoint, fl)
subroutine radiative_cooling_init(fl, read_params)
subroutine radiative_cooling_add_source(qdt, ixil, ixol, wct, wctprim, w, x, qsourcesplit, active, fl)
subroutine calc_l_extended(tpoint, lpoint, fl)
Module to include Coriolis and centrifugal forces in (radiation-)(magneto) hydrodynamics simulations ...
subroutine rotating_frame_add_source(qdt, dtfactor, ixil, ixol, wct, w, x)
w[iw]=w[iw]+qdt*S[wCT,qtC,x] where S is the source based on wCT within ixO
subroutine rotating_frame_init()
Initialize the module.
Public face of the short-characteristics (SC) radiative transfer and its coupling to the (M)HD.
subroutine, public rt_sc_add_source(qdt, ixil, ixol, wct, w, x, thin_share, e_internal)
Unsplit heating source of one block and stage, e += qdt Q, with Q read from i_qrad,...
subroutine, public rt_sc_activate()
Switch the RT on. The physics module calls this from its initialisation when hd_radiative_transfer or...
Module for handling problematic values in simulations, such as negative pressures.
subroutine, public small_values_average(ixil, ixol, w, x, w_flag, windex)
subroutine, public small_values_error(wprim, x, ixil, ixol, w_flag, subname)
logical, dimension(:), allocatable, public small_values_fix_iw
Whether to apply small value fixes to certain variables.
character(len=20), public small_values_method
How to handle small values.
Generic supertimestepping method which can be used for multiple source terms in the governing equatio...
subroutine, public add_sts_method(sts_getdt, sts_set_sources, startvar, nflux, startwbc, nwbc, evolve_b)
subroutine which added programatically a term to be calculated using STS Params: sts_getdt function c...
subroutine, public set_conversion_methods_to_head(sts_before_first_cycle, sts_after_last_cycle)
Set the hooks called before the first cycle and after the last cycle in the STS update This method sh...
subroutine, public set_error_handling_to_head(sts_error_handling)
Set the hook of error handling in the STS update. This method is called before updating the BC....
subroutine, public sts_init()
Initialize sts module.
Thermal conduction for HD and MHD or RHD and RMHD or twofl (plasma-neutral) module Adaptation of mod_...
subroutine, public tc_get_hd_params(fl, read_hd_params)
Init TC coefficients: HD case.
double precision function, public get_tc_dt_hd(w, ixil, ixol, dxd, x, fl)
Get the explicit timestep for the TC (hd implementation) Note: also used in 1D MHD (or for neutrals i...
subroutine tc_init_params(phys_gamma)
subroutine, public sts_set_source_tc_hd(ixil, ixol, w, x, wres, fix_conserve_at_step, my_dt, igrid, nflux, fl)
subroutine get_euv_image(qunit, fl)
subroutine get_sxr_image(qunit, fl)
subroutine get_euv_spectrum(qunit, fl)
subroutine get_whitelight_image(qunit, fl)
subroutine eos_timer_start()
Open an EoS timing window; inside an OpenMP parallel region the EoS is not timed.
Definition mod_timing.t:45
subroutine eos_timer_stop(acc)
Close an EoS timing window, adding its time to acc when it is the outermost.
Definition mod_timing.t:52
double precision timeeos_csound
Definition mod_timing.t:13
Module with all the methods that users can customize in AMRVAC.
procedure(rfactor), pointer usr_rfactor
procedure(sub_get_heating), pointer usr_get_heating
procedure(set_surface), pointer usr_set_surface
procedure(phys_gravity), pointer usr_gravity
procedure(hd_pthermal), pointer usr_set_pthermal
procedure(set_wlr), pointer usr_set_wlr
integer nw
Total number of variables.
integer number_species
number of species: each species has different characterictic speeds and should be used accordingly in...
The module add viscous source terms and check time step.
subroutine, public viscosity_get_dt(wprim, ixil, ixol, dtnew, dxd, x)
procedure(sub_add_source), pointer, public viscosity_add_source
subroutine, public viscosity_init(phys_wider_stencil)
Initialize the module.
Radiation fluid object: gas-EoS callbacks the FLD module needs, wired by the physics module at link t...
Definition mod_fld.t:63