MPI-AMRVAC 3.2
The MPI - Adaptive Mesh Refinement - Versatile Advection Code (development version)
Loading...
Searching...
No Matches
mod_thermal_conduction.t
Go to the documentation of this file.
1!> Thermal conduction for HD and MHD or RHD and RMHD or twofl (plasma-neutral) module
2!> Adaptation of mod_thermal_conduction for the mod_supertimestepping
3!>
4!> The TC is set by calling
5!> tc_init_params()
6!>
7!> Organized such that it can call either isotropic (HD) or anisotropic (MHD) variants
8!> it adds a heat conduction source to each energy equation
9!> and can be recycled within a multi-fluid context (such as plasma-neutral twofl module)
10!>
11!>
12!> 10.07.2011 developed by Chun Xia and Rony Keppens
13!> 01.09.2012 moved to modules folder by Oliver Porth
14!> 13.10.2013 optimized further by Chun Xia
15!> 12.03.2014 implemented RKL2 super timestepping scheme to reduce iterations
16!> and improve stability and accuracy up to second order in time by Chun Xia.
17!> 23.08.2014 implemented saturation and perpendicular TC by Chun Xia
18!> 12.01.2017 modulized by Chun Xia
19!> adapted by Beatrice Popescu to twofluid settings
20!> 06.09.2024 cleaned up for use in rhd and rmhd modules (Nishant Narechania and Rony Keppens)
21!> 30.11.2025 Minor cleanup (for consistency between hd and mhd)
22!>
23!> PURPOSE:
24!> IN MHD ADD THE HEAT CONDUCTION SOURCE TO THE ENERGY EQUATION
25!> S=DIV(KAPPA_i,j . GRAD_j T)
26!> where KAPPA_i,j = tc_k_para b_i b_j + tc_k_perp (I - b_i b_j)
27!> b_i b_j = B_i B_j / B**2, I is the unit matrix, and i, j= 1, 2, 3 for 3D
28!> IN HD ADD THE HEAT CONDUCTION SOURCE TO THE ENERGY EQUATION
29!> S=DIV(tc_k_para . GRAD T)
30!> USAGE:
31!> 1. in mod_usr.t -> subroutine usr_init(), add
32!> unit_length=your length unit
33!> unit_numberdensity=your number density unit
34!> unit_velocity=your velocity unit
35!> unit_temperature=your temperature unit
36!> before call (m)hd_activate()
37!> 2. to switch on thermal conduction in the (r)(m)hd_list of amrvac.par add:
38!> (r)(m)hd_thermal_conduction=.true.
39!> 3. in the tc_list of amrvac.par :
40!> tc_perpendicular=.true. ! (default .false.) turn on thermal conduction perpendicular to magnetic field
41!> tc_saturate=.true. ! (default .false. ) turn on thermal conduction saturate effect
42!> tc_sat_coef=1.1 ! phi in the saturated flux 5 phi rho c**3, Cowie & McKee (1977). Unset: 0.3 for
43!> ! FI/PI; for LTE their eq 7 with the local ne (0.97 pure H, 1.11 He/H 0.1)
44!> tc_slope_limiter='MC' ! choose limiter for slope-limited anisotropic thermal conduction in MHD
45!> note: twofl_list incorporates instances for charges and neutrals
46
48 use mod_global_parameters, only: std_len
49 use mod_geometry
50 use mod_comm_lib, only: mpistop
51 implicit none
52
53 !> The adiabatic index
54 double precision :: tc_gamma_1
55
56 abstract interface
57 subroutine get_var_subr(w,x,ixI^L,ixO^L,res)
59 integer, intent(in) :: ixI^L, ixO^L
60 double precision, intent(in) :: w(ixI^S,nw)
61 double precision, intent(in) :: x(ixI^S,1:ndim)
62 double precision, intent(out):: res(ixI^S)
63 end subroutine get_var_subr
64
65 subroutine get_2var_subr(ixI^L, ixO^L, w, x, ne, nH)
67 integer, intent(in) :: ixI^L, ixO^L
68 double precision, intent(in) :: w(ixI^S, nw)
69 double precision, intent(in) :: x(ixI^S, 1:ndim)
70 double precision, intent(out):: ne(ixI^S), nH(ixI^S)
71 end subroutine get_2var_subr
72
73 !> Scalar EoS inverse, e.g. eint_nH_from_T(log_nH, log_T)
74 double precision function eos_scalar2_func(a, b)
75 double precision, intent(in) :: a, b
76 end function eos_scalar2_func
77 end interface
78
80
81 ! BEGIN the following are read from param file or set in tc_read_hd_params or tc_read_mhd_params
82 !> Coefficient of thermal conductivity (parallel to magnetic field)
83 double precision :: tc_k_para
84
85 !> Coefficient of thermal conductivity perpendicular to magnetic field
86 double precision :: tc_k_perp
87
88 !> Indices of the variables
89 integer :: e_=-1
90 !> Index of cut off temperature for TRAC
91 integer :: tcoff_
92 !> Name of slope limiter for transverse component of thermal flux
93 integer :: tc_slope_limiter
94
95 ! if subtract_equi = .true. get_temperature_equi and get_rho_equi have to be set
96 logical :: subtract_equi=.false.
97
98 !> Logical switch for test constant conductivity
99 logical :: tc_constant=.false.
100
101 !> Calculate thermal conduction perpendicular to magnetic field (.true.) or not (.false.)
102 logical :: tc_perpendicular=.false.
103
104 !> Consider thermal conduction saturation effect (.true.) or not (.false.)
105 logical :: tc_saturate=.false.
106
107 !> Patch cells where e_int < 0 during STS Chebyshev substeps by
108 !> neighbor-averaging the temperature. Prevents NaN from sqrt(T<0)
109 !> in the Spitzer conductivity when the RKL2 polynomial overshoots.
110 logical :: tc_patch_eint=.false.
111
112 !> LTE with ionisation energy: hold the ionisation state fixed through each conduction
113 !> step. T comes from the full EoS and R = p/(rho T) from the cached ionisation at the
114 !> first STS stage; the later stages map the internal energy to T with the frozen-composition
115 !> heat capacity. The physics modules set it from tc_frozen_ion, default true, and their EoS
116 !> binding clears it for every other closure
117 logical :: frozen_ion=.false.
118
119 !> phi in the saturated flux 5 phi rho c**3 (Cowie & McKee 1977 eq 8), tc_sat_coef in tc_list.
120 !> Negative means unset: phi = 0.3 for FI/PI, Cowie & McKee eq 7 with the local ne for LTE
121 double precision :: tc_sat_coef=-1.d0
122
123 !> Saturated flux per face, sat_fac (rhos_A+rhos_B) T**1.5, set only by tc_set_saturation
124 double precision :: sat_fac
125
126 !> Minimum temperature (code units) below which TRAC does not modify conductivity.
127 !> Below this T, the energy balance is dominated by optically thick radiation
128 !> and recombination, not optically thin cooling + Spitzer conduction, so TRAC's
129 !> broadening assumption does not apply. Read in Kelvin via tc_list (trac_T_floor),
130 !> converted to code units at init. Default 0 (no floor). Only the HD TRAC cutoff (types 0
131 !> and 7) reads it, with Tcoff = Te below it, so conduction, losses and heating are left
132 !> unmodified there
133 double precision :: trac_t_floor=0.d0
134 ! END the following are read from param file or set in tc_read_hd_params or tc_read_mhd_params
135 procedure(get_var_subr), pointer, nopass :: get_rho => null()
136 procedure(get_var_subr), pointer, nopass :: get_rho_equi => null()
137 procedure(get_var_subr), pointer,nopass :: get_temperature_from_eint => null()
138 procedure(get_var_subr), pointer,nopass :: get_temperature_from_conserved => null()
139 procedure(get_var_subr), pointer,nopass :: get_temperature_equi => null()
140 procedure(get_2var_subr), pointer,nopass :: get_ne_nh => null()
141 procedure(get_var_subr), pointer,nopass :: get_var_rfactor => null()
142 !> EoS snapshots + inverse accessor (set in bind_eos_to_source); let TC reach
143 !> thermodynamics only through this object, never mod_eos directly.
144 double precision :: inv_gamma_minus_1
145 double precision :: nh2rhofactor
146 double precision :: log_t_floor
147 procedure(eos_scalar2_func), pointer, nopass :: eint_from_t => null()
148 !> Fully ionised electrons and particles per H nucleus, and the mass per H nucleus in m_p.
149 !> The defaults are pure hydrogen, for fluids not bound to an EoS (twofl)
150 double precision :: neonh_fi=1.d0, n_per_nh_fi=2.d0, mass_per_nh=1.d0
151 !> LTE closure: code T is kT/m_p (R not absorbed in the units) and ne/nH is the local value
152 logical :: eos_lte=.false.
153 end type tc_fluid
154
155 public :: tc_get_mhd_params
156 public :: tc_get_hd_params
157 public :: tc_set_saturation
158 public :: get_tc_dt_mhd
159 public :: get_tc_dt_hd
160 public :: sts_set_source_tc_mhd
161 public :: sts_set_source_tc_hd
162 public :: tc_patch_negative_eint
163
164 !> Per-block anchor of the frozen-ionisation conduction step: T and the internal energy
165 !> at the step start, and the inverse frozen-composition heat capacity per volume
167 integer :: sts_id = -1 !< STS application the anchor belongs to
168 double precision, allocatable :: t0(:^d&), e0(:^D&), cvinv(:^D&)
169 end type tc_anchor_t
170 type(tc_anchor_t), allocatable :: tc_anchor(:)
171
172contains
173
174 subroutine tc_init_params(phys_gamma)
176 double precision, intent(in) :: phys_gamma
177
178 tc_gamma_1=phys_gamma-1d0
179 end subroutine tc_init_params
180
181 !> Init TC coefficients: MHD case
182 subroutine tc_get_mhd_params(fl,read_mhd_params)
184
185 interface
186 subroutine read_mhd_params(fl)
188 import tc_fluid
189 type(tc_fluid), intent(inout) :: fl
190
191 end subroutine read_mhd_params
192 end interface
193 type(tc_fluid), intent(inout) :: fl
194
195 fl%tc_slope_limiter=1
196 fl%tc_k_para=0.d0
197 fl%tc_k_perp=0.d0
198
199 !> Read tc module parameters from par file: MHD case
200 call read_mhd_params(fl)
201
202 if(fl%tc_k_para==0.d0 .and. fl%tc_k_perp==0.d0) then
203 fl%tc_k_para=tc_spitzer_k_para()
204 ! thermal conductivity perpendicular to magnetic field
205 if(si_unit) then
206 fl%tc_k_perp=4.d-30*unit_numberdensity**2/unit_magneticfield**2/unit_temperature**3*fl%tc_k_para
207 else
208 fl%tc_k_perp=4.d-10*unit_numberdensity**2/unit_magneticfield**2/unit_temperature**3*fl%tc_k_para
209 end if
210 if(mype .eq. 0) print*, "Spitzer MHD: par: ",fl%tc_k_para, &
211 " ,perp: ",fl%tc_k_perp
212 else
213 fl%tc_constant=.true.
214 if(mype .eq. 0) print*, "Constant thermal conduction coefficients with values: ",fl%tc_k_para,fl%tc_k_perp
215 end if
216 call tc_set_saturation(fl,.false.)
217 end subroutine tc_get_mhd_params
218
219 !> Init TC coefficients: HD case
220 subroutine tc_get_hd_params(fl,read_hd_params)
222
223 interface
224 subroutine read_hd_params(fl)
226 import tc_fluid
227 type(tc_fluid), intent(inout) :: fl
228
229 end subroutine read_hd_params
230 end interface
231 type(tc_fluid), intent(inout) :: fl
232
233 fl%tc_k_para=0.d0
234
235 !> Read tc parameters from par file: HD case
236 call read_hd_params(fl)
237
238 if(fl%tc_k_para==0.d0) then
239 fl%tc_k_para=tc_spitzer_k_para()
240 if(mype .eq. 0) print*, "Spitzer HD par: ",fl%tc_k_para
241 else
242 fl%tc_constant=.true.
243 if(mype .eq. 0) print*, "Constant thermal conduction coefficient at value: ",fl%tc_k_para
244 end if
245 call tc_set_saturation(fl,.false.)
246
247 end subroutine tc_get_hd_params
248
249 !> Spitzer conductivity coefficient parallel to the field, in code units
257 end function tc_spitzer_k_para
258
259 !> Saturated flux factor. q_sat = 5 phi rho c**3 (Cowie & McKee 1977 eq 8), c**2 = xt kT/m_p with
260 !> xt the fully ionised particles per proton mass. Per face rho = (rho_A+rho_B)/2, so the factor
261 !> is 2.5 phi. FI/PI code T is xt kT/m_p, so T**1.5 is c**3. LTE code T is kT/m_p, so
262 !> c**3 = xt**1.5 T**1.5, and tc_sat_rho scales rho by the local over the fully ionised ne/nH.
263 !> Their eq 7, 0.4 (2kT/pi m_e)^(1/2) ne kT, is phi = 0.4 sqrt(2/pi) sqrt(m_p/m_e) xe/(5 xt**1.5),
264 !> xe the fully ionised electrons per proton mass. Unset tc_sat_coef gives phi = 0.3 for FI/PI and
265 !> eq 7 for LTE, which with the local ne is self-consistent; a set tc_sat_coef is phi for all
266 subroutine tc_set_saturation(fl,report)
268 use mod_global_parameters, only: mype
269 type(tc_fluid), intent(inout) :: fl
270 !> print phi once the EoS has set the composition
271 logical, intent(in) :: report
272 double precision :: xe, xt, phi, phi_cm77
273
274 xe = fl%neOnH_FI/fl%mass_per_nH
275 xt = fl%n_per_nH_FI/fl%mass_per_nH
276 phi_cm77 = 0.4d0*dsqrt(2.d0/dpi)*dsqrt(const_mp/const_me)*xe/(5.d0*xt**1.5d0)
277 if(fl%tc_sat_coef >= 0.d0) then
278 phi = fl%tc_sat_coef
279 else if(fl%eos_lte) then
280 phi = phi_cm77
281 else
282 phi = 0.3d0
283 end if
284 fl%sat_fac = 2.5d0*phi
285 if(fl%eos_lte) fl%sat_fac = fl%sat_fac*xt**1.5d0
286 if(report .and. mype == 0 .and. fl%tc_saturate) print '(a,f8.4,a,f8.4)', &
287 ' TC saturation: phi = ', phi, ', Cowie & McKee eq 7 gives phi = ', phi_cm77
288 end subroutine tc_set_saturation
289
290 !> Get the explicit timestep for the TC (mhd implementation)
291 !> Note: for multi-D MHD (1D MHD will use HD fall-back)
292 function get_tc_dt_mhd(w,ixI^L,ixO^L,dx^D,x,fl) result(dtnew)
293 !Check diffusion time limit dt < dx_i**2/((gamma-1)*tc_k_para_i/(rho R))
294 !where tc_k_para_i=tc_k_para*B_i**2/B**2
295 !and T=p/rho
297
298 type(tc_fluid), intent(in) :: fl
299 integer, intent(in) :: ixi^l, ixo^l
300 double precision, intent(in) :: dx^d, x(ixi^s,1:ndim)
301 double precision, intent(in) :: w(ixi^s,1:nw)
302 double precision :: dtnew
303
304 double precision :: mf(ixo^s,1:ndir),te(ixi^s),rho(ixi^s),gradt(ixi^s)
305 double precision :: ne(ixi^s), nh_arr(ixi^s), rf(ixi^s)
306 double precision :: tmp(ixo^s),hfs(ixo^s),blocal(1:ndir),bmag
307 double precision :: dtdiff_tcond,maxtmp2
308 integer :: idims,ix^d
309
310
311 ! B
312 if(allocated(iw_mag)) then
313 if(b0field) then
314 {do ix^db=ixomin^db,ixomax^db\}
315 ^c&blocal(^c)=w({ix^d},iw_mag(^c))+block%B0({ix^d},^c,0)\
316 bmag=dsqrt(^c&blocal(^c)**2+)+smalldouble
317 ^c&mf(ix^d,^c)=blocal(^c)/bmag\
318 {end do\}
319 else
320 {do ix^db=ixomin^db,ixomax^db\}
321 bmag=dsqrt(^c&w(ix^d,iw_mag(^c))**2+)+smalldouble
322 ^c&mf(ix^d,^c)=w(ix^d,iw_mag(^c))/bmag\
323 {end do\}
324 end if
325 else
326 ! this if for ffhd or other physics modules without B components
327 mf(ixo^s,1:ndim)=block%B0(ixo^s,1:ndim,0)
328 end if
329
330 ! Constant conductivity does not use Te; retain ghost temperatures when
331 ! needed by the temperature-dependent conductivity and saturation gradient.
332 if(.not.fl%tc_constant) then
333 call fl%get_temperature_from_conserved(w,x,ixi^l,ixi^l,te)
334 end if
335 call fl%get_rho(w,x,ixi^l,ixo^l,rho)
336 ! Electron density is used only by the non-constant saturation correction.
337 if(.not.fl%tc_constant .and. fl%tc_saturate) then
338 call fl%get_ne_nH(ixi^l, ixo^l, w, x, ne, nh_arr)
339 end if
340 ! the diffusivity is kappa/(rho c_v) with c_v = R/(gamma-1): weight rho by R
341 if(associated(fl%get_var_Rfactor)) then
342 call fl%get_var_Rfactor(w, x, ixi^l, ixo^l, rf)
343 rho(ixo^s)=rho(ixo^s)*rf(ixo^s)
344 end if
345
346 !tc_k_para_i
347 if(fl%tc_constant) then
348 tmp(ixo^s)=fl%tc_k_para
349 else
350 if(fl%tc_saturate) then
351 ! Kannan 2016 MN 458, 410
352 ! l_mfpe = 3^1.5*kB^2/(4*sqrt(pi)*e^4*lnLambda) * T^2/n_e
353 if(si_unit) then
354 ! 5.730205638843984e27 = 3^1.5*kB_SI^2/(4*sqrt(pi)*e_SI^4*37)
355 tmp(ixo^s)=te(ixo^s)**2/ne(ixo^s)*5.730205638843984d27*unit_temperature**2/(unit_numberdensity*unit_length)
356 else
357 ! 7093.9239487765044 = 3^1.5*kB_cgs^2/(4*sqrt(pi)*e_cgs^4*37)
358 tmp(ixo^s)=te(ixo^s)**2/ne(ixo^s)*7093.9239487765044d0*unit_temperature**2/(unit_numberdensity*unit_length)
359 end if
360 do idims=1,ndim
361 call gradient(te,ixi^l,ixo^l,idims,gradt)
362 if(idims==1) then
363 hfs(ixo^s)=gradt(ixo^s)*mf(ixo^s,idims)
364 else
365 hfs(ixo^s)=hfs(ixo^s)+gradt(ixo^s)*mf(ixo^s,idims)
366 end if
367 end do
368 ! kappa=kappa_Spitzer/(1+4.2*l_mfpe/(T/|gradT.b|))
369 tmp(ixo^s)=fl%tc_k_para*te(ixo^s)*te(ixo^s)*dsqrt(te(ixo^s))/(1.d0+4.2d0*tmp(ixo^s)*dabs(hfs(ixo^s))/te(ixo^s))
370 else
371 ! kappa=kappa_Spitzer
372 tmp(ixo^s)=fl%tc_k_para*te(ixo^s)*te(ixo^s)*dsqrt(te(ixo^s))
373 end if
374 end if
375
376 dtnew=bigdouble
377 do idims=1,ndim
378 ! approximate thermal conduction flux: tc_k_para_i/rho/dx*B_i**2/B**2
379 maxtmp2=maxval(tmp(ixo^s)*mf(ixo^s,idims)**2/(rho(ixo^s)*block%ds(ixo^s,idims)**2))
380 ! dt< dx_idim**2/((gamma-1)*tc_k_para_i/rho*B_i**2/B**2)
381 dtdiff_tcond=1.d0/(tc_gamma_1*maxtmp2+smalldouble)
382 ! limit the time step
383 dtnew=min(dtnew,dtdiff_tcond)
384 end do
385 dtnew=dtnew/dble(ndim)
386 end function get_tc_dt_mhd
387
388 !> anisotropic thermal conduction with slope limited symmetric scheme
389 !> Sharma 2007 Journal of Computational Physics 227, 123
390 subroutine sts_set_source_tc_mhd(ixI^L,ixO^L,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux,fl)
393 integer, intent(in) :: ixi^l, ixo^l, igrid, nflux
394 double precision, intent(in) :: x(ixi^s,1:ndim)
395 ! intent(inout) so tc_patch_negative_eint can repair w(:, ie) in place
396 ! when tc_patch_eint = .true.; legacy path (patch off) leaves w untouched.
397 double precision, intent(inout) :: w(ixi^s,1:nw)
398 double precision, intent(inout) :: wres(ixi^s,1:nw)
399 double precision, intent(in) :: my_dt
400 logical, intent(in) :: fix_conserve_at_step
401 type(tc_fluid), intent(in) :: fl
402
403 !! qd store the heat conduction energy changing rate
404 double precision :: qd(ixo^s)
405 double precision :: rho(ixi^s),te(ixi^s)
406 double precision :: qvec(ixi^s,1:ndim)
407 double precision :: fluxall(ixi^s,1,1:ndim)
408 double precision :: alpha,dxinv(ndim)
409 double precision, allocatable, dimension(:^D&,:) :: qvec_equi
410 integer :: idims,ixa^l
411
412 ! coefficient of limiting on normal component
413 if(ndim<3) then
414 alpha=0.75d0
415 else
416 alpha=0.85d0
417 end if
418
419 dxinv=1.d0/dxlevel
420
421 call tc_get_te(ixi^l,w,x,fl,igrid,te)
422 call fl%get_rho(w, x, ixi^l, ixi^l, rho)
423 call tc_flux_mhd(ixi^l,ixo^l,w,x,fl,qvec,rho,te,alpha)
424 if(fl%subtract_equi) then
425 allocate(qvec_equi(ixi^s,1:ndim))
426 call fl%get_temperature_equi(w, x, ixi^l, ixi^l, te)
427 call fl%get_rho_equi(w, x, ixi^l, ixi^l, rho)
428 call tc_flux_mhd(ixi^l,ixo^l,w,x,fl,qvec_equi,rho,te,alpha)
429 do idims=1,ndim
430 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
431 qvec(ixa^s,idims)=qvec(ixa^s,idims)-qvec_equi(ixa^s,idims)
432 end do
433 deallocate(qvec_equi)
434 end if
435
436 if(slab_uniform) then
437 do idims=1,ndim
438 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
439 qvec(ixa^s,idims)=dxinv(idims)*qvec(ixa^s,idims)
440 ixa^l=ixo^l-kr(idims,^d);
441 if(idims==1) then
442 qd(ixo^s)=qvec(ixo^s,idims)-qvec(ixa^s,idims)
443 else
444 qd(ixo^s)=qd(ixo^s)+qvec(ixo^s,idims)-qvec(ixa^s,idims)
445 end if
446 end do
447 else
448 do idims=1,ndim
449 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
450 qvec(ixa^s,idims)=qvec(ixa^s,idims)*block%surfaceC(ixa^s,idims)
451 ixa^l=ixo^l-kr(idims,^d);
452 if(idims==1) then
453 qd(ixo^s)=qvec(ixo^s,idims)-qvec(ixa^s,idims)
454 else
455 qd(ixo^s)=qd(ixo^s)+qvec(ixo^s,idims)-qvec(ixa^s,idims)
456 end if
457 end do
458 qd(ixo^s)=qd(ixo^s)/block%dvolume(ixo^s)
459 end if
460
461 if(fix_conserve_at_step) then
462 do idims=1,ndim
463 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
464 fluxall(ixa^s,1,idims)=my_dt*qvec(ixa^s,idims)
465 end do
466 call store_flux(igrid,fluxall,1,ndim,nflux)
467 end if
468
469 wres(ixo^s,fl%e_)=qd(ixo^s)
470 end subroutine sts_set_source_tc_mhd
471
472 subroutine set_source_tc_mhd(ixI^L,ixO^L,w,x,fl,qvec,rho,Te,alpha)
474 integer, intent(in) :: ixI^L, ixO^L
475 double precision, intent(in) :: x(ixI^S,1:ndim)
476 double precision, intent(in) :: w(ixI^S,1:nw)
477 type(tc_fluid), intent(in) :: fl
478 double precision, intent(in) :: rho(ixI^S),Te(ixI^S)
479 double precision, intent(in) :: alpha
480 double precision, intent(out) :: qvec(ixI^S,1:ndim)
481 double precision :: rhos(ixI^S)
482
483 !! qdd store the heat conduction energy changing rate
484 double precision, dimension(ixI^S,1:ndim) :: mf,Bc,Bcf,gradT
485 double precision, dimension(ixI^S) :: ka,kaf,ke,kef,qdd,Bnorm
486 double precision :: minq,maxq,qd(ixI^S,2**(ndim-1)), blocal(ndir)
487 integer :: idims,idir,ix^D,ix^L,ixC^L,ixA^L,ixB^L
488
489 ix^l=ixo^l^ladd1;
490 if(fl%tc_saturate) call tc_sat_rho(ixi^l,w,x,fl,rho,rhos)
491
492 ! T gradient at cell faces
493 ! b unit vector mf: magnetic field direction vector
494 if(allocated(iw_mag)) then
495 if(b0field) then
496 {do ix^db=ixmin^db,ixmax^db\}
497 ^c&blocal(^c)=w({ix^d},iw_mag(^c))+block%B0({ix^d},^c,0)\
498 {^iftwod
499 if(blocal(1)/=0.d0) then
500 mf(ix^d,1)=sign(1.d0,blocal(1))/dsqrt(1.d0+(^ce&(blocal(^ce)/blocal(1))**2+))
501 else
502 mf(ix^d,1)=0.d0
503 end if
504 if(blocal(2)/=0.d0) then
505 mf(ix^d,2)=sign(1.d0,blocal(2))/dsqrt(1.d0+(^cf&(blocal(^cf)/blocal(2))**2+))
506 else
507 mf(ix^d,2)=0.d0
508 end if
509 }
510 {^ifthreed
511 if(blocal(1)/=0.d0) then
512 mf(ix^d,1)=sign(1.d0,blocal(1))/dsqrt(1.d0+(blocal(2)/blocal(1))**2+(blocal(3)/blocal(1))**2)
513 else
514 mf(ix^d,1)=0.d0
515 end if
516 if(blocal(2)/=0.d0) then
517 mf(ix^d,2)=sign(1.d0,blocal(2))/dsqrt(1.d0+(blocal(1)/blocal(2))**2+(blocal(3)/blocal(2))**2)
518 else
519 mf(ix^d,2)=0.d0
520 end if
521 if(blocal(3)/=0.d0) then
522 mf(ix^d,3)=sign(1.d0,blocal(3))/dsqrt(1.d0+(blocal(1)/blocal(3))**2+(blocal(2)/blocal(3))**2)
523 else
524 mf(ix^d,3)=0.d0
525 end if
526 }
527 {end do\}
528 else
529 {do ix^db=ixmin^db,ixmax^db\}
530 {^iftwod
531 if(w(ix^d,iw_mag(1))/=0.d0) then
532 mf(ix^d,1)=sign(1.d0,w(ix^d,iw_mag(1)))/dsqrt(1.d0+(^ce&(w(ix^d,iw_mag(^ce))/w(ix^d,iw_mag(1)))**2+))
533 else
534 mf(ix^d,1)=0.d0
535 end if
536 if(w(ix^d,iw_mag(2))/=0.d0) then
537 mf(ix^d,2)=sign(1.d0,w(ix^d,iw_mag(2)))/dsqrt(1.d0+(^cf&(w(ix^d,iw_mag(^cf))/w(ix^d,iw_mag(2)))**2+))
538 else
539 mf(ix^d,2)=0.d0
540 end if
541 }
542 {^ifthreed
543 if(w(ix^d,iw_mag(1))/=0.d0) then
544 mf(ix^d,1)=sign(1.d0,w(ix^d,iw_mag(1)))/dsqrt(1.d0+(w(ix^d,iw_mag(2))/w(ix^d,iw_mag(1)))**2+&
545 (w(ix^d,iw_mag(3))/w(ix^d,iw_mag(1)))**2)
546 else
547 mf(ix^d,1)=0.d0
548 end if
549 if(w(ix^d,iw_mag(2))/=0.d0) then
550 mf(ix^d,2)=sign(1.d0,w(ix^d,iw_mag(2)))/dsqrt(1.d0+(w(ix^d,iw_mag(1))/w(ix^d,iw_mag(2)))**2+&
551 (w(ix^d,iw_mag(3))/w(ix^d,iw_mag(2)))**2)
552 else
553 mf(ix^d,2)=0.d0
554 end if
555 if(w(ix^d,iw_mag(3))/=0.d0) then
556 mf(ix^d,3)=sign(1.d0,w(ix^d,iw_mag(3)))/dsqrt(1.d0+(w(ix^d,iw_mag(1))/w(ix^d,iw_mag(3)))**2+&
557 (w(ix^d,iw_mag(2))/w(ix^d,iw_mag(3)))**2)
558 else
559 mf(ix^d,3)=0.d0
560 end if
561 }
562 {end do\}
563 end if
564 else
565 mf(ix^s,1:ndim)=block%B0(ix^s,1:ndim,0)
566 endif
567 ! ixC is cell-corner index
568 ixcmax^d=ixomax^d; ixcmin^d=ixomin^d-1;
569 ! b unit vector at cell corner
570 {^ifthreed
571 do idims=1,3
572 {do ix^db=ixcmin^db,ixcmax^db\}
573 bc(ix^d,idims)=0.125d0*(mf(ix1,ix2,ix3,idims)+mf(ix1+1,ix2,ix3,idims)&
574 +mf(ix1,ix2+1,ix3,idims)+mf(ix1+1,ix2+1,ix3,idims)&
575 +mf(ix1,ix2,ix3+1,idims)+mf(ix1+1,ix2,ix3+1,idims)&
576 +mf(ix1,ix2+1,ix3+1,idims)+mf(ix1+1,ix2+1,ix3+1,idims))
577 {end do\}
578 end do
579 }
580 {^iftwod
581 do idims=1,2
582 {do ix^db=ixcmin^db,ixcmax^db\}
583 bc(ix^d,idims)=0.25d0*(mf(ix1,ix2,idims)+mf(ix1+1,ix2,idims)&
584 +mf(ix1,ix2+1,idims)+mf(ix1+1,ix2+1,idims))
585 {end do\}
586 end do
587 }
588 ! T gradient at cell faces
589 do idims=1,ndim
590 ixbmin^d=ixmin^d;
591 ixbmax^d=ixmax^d-kr(idims,^d);
592 call gradientf(te,x,ixi^l,ixb^l,idims,gradt(ixi^s,idims))
593 end do
594 if(fl%tc_constant) then
595 if(fl%tc_perpendicular) then
596 ka(ixc^s)=fl%tc_k_para-fl%tc_k_perp
597 ke(ixc^s)=fl%tc_k_perp
598 else
599 ka(ixc^s)=fl%tc_k_para
600 end if
601 else
602 ! conductivity at cell center
603 if(phys_trac) then
604 {do ix^db=ixmin^db,ixmax^db\}
605 if(te(ix^d) < block%wextra(ix^d,fl%Tcoff_)) then
606 qdd(ix^d)=fl%tc_k_para*dsqrt(block%wextra(ix^d,fl%Tcoff_)**5)
607 else
608 qdd(ix^d)=fl%tc_k_para*dsqrt(te(ix^d)**5)
609 end if
610 {end do\}
611 else
612 qdd(ix^s)=fl%tc_k_para*dsqrt(te(ix^s)**5)
613 end if
614 ! cell corner parallel conductivity in ka
615 {^ifthreed
616 {do ix^db=ixcmin^db,ixcmax^db\}
617 ka(ix^d)=0.125d0*(qdd(ix1,ix2,ix3)+qdd(ix1+1,ix2,ix3)&
618 +qdd(ix1,ix2+1,ix3)+qdd(ix1+1,ix2+1,ix3)&
619 +qdd(ix1,ix2,ix3+1)+qdd(ix1+1,ix2,ix3+1)&
620 +qdd(ix1,ix2+1,ix3+1)+qdd(ix1+1,ix2+1,ix3+1))
621 {end do\}
622 }
623 {^iftwod
624 {do ix^db=ixcmin^db,ixcmax^db\}
625 ka(ix^d)=0.25d0*(qdd(ix1,ix2)+qdd(ix1+1,ix2)&
626 +qdd(ix1,ix2+1)+qdd(ix1+1,ix2+1))
627 {end do\}
628 }
629 ! compensate with perpendicular conductivity
630 if(fl%tc_perpendicular) then
631 if(b0field) then
632 qdd(ix^s)=fl%tc_k_perp*rho(ix^s)**2/((^c&(w(ix^s,iw_mag(^c))+block%B0(ix^s,^c,0))**2+)*dsqrt(te(ix^s))+smalldouble)
633 else
634 qdd(ix^s)=fl%tc_k_perp*rho(ix^s)**2/((^c&w(ix^s,iw_mag(^c))**2+)*dsqrt(te(ix^s))+smalldouble)
635 end if
636 {^ifthreed
637 {do ix^db=ixcmin^db,ixcmax^db\}
638 ke(ix^d)=0.125d0*(qdd(ix1,ix2,ix3)+qdd(ix1+1,ix2,ix3)&
639 +qdd(ix1,ix2+1,ix3)+qdd(ix1+1,ix2+1,ix3)&
640 +qdd(ix1,ix2,ix3+1)+qdd(ix1+1,ix2,ix3+1)&
641 +qdd(ix1,ix2+1,ix3+1)+qdd(ix1+1,ix2+1,ix3+1))
642 if(ke(ix^d)<ka(ix^d)) then
643 ka(ix^d)=ka(ix^d)-ke(ix^d)
644 else
645 ke(ix^d)=ka(ix^d)
646 ka(ix^d)=0.d0
647 end if
648 {end do\}
649 }
650 {^iftwod
651 {do ix^db=ixcmin^db,ixcmax^db\}
652 ke(ix^d)=0.25d0*(qdd(ix1,ix2)+qdd(ix1+1,ix2)&
653 +qdd(ix1,ix2+1)+qdd(ix1+1,ix2+1))
654 if(ke(ix^d)<ka(ix^d)) then
655 ka(ix^d)=ka(ix^d)-ke(ix^d)
656 else
657 ke(ix^d)=ka(ix^d)
658 ka(ix^d)=0.d0
659 end if
660 {end do\}
661 }
662 end if
663 end if
664 ! calculate thermal conduction flux with slope-limited symmetric scheme
665 do idims=1,ndim
666 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
667 {^ifthreed
668 if(idims==1) then
669 {do ix^db=ixamin^db,ixamax^db\}
670 ! averaged b at face centers
671 ^d&bcf({ix^d},^d)=0.25d0*(bc({ix^d},^d)+bc(ix1,ix2-1,ix3,^d)&
672 +bc(ix1,ix2,ix3-1,^d)+bc(ix1,ix2-1,ix3-1,^d))\
673 kaf(ix^d)=0.25d0*(ka(ix1,ix2,ix3)+ka(ix1,ix2-1,ix3)&
674 +ka(ix1,ix2,ix3-1)+ka(ix1,ix2-1,ix3-1))
675 ! averaged thermal conductivity at face centers
676 if(fl%tc_perpendicular) &
677 kef(ix^d)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1,ix2-1,ix3)&
678 +ke(ix1,ix2,ix3-1)+ke(ix1,ix2-1,ix3-1))
679 {end do\}
680 else if(idims==2) then
681 {do ix^db=ixamin^db,ixamax^db\}
682 ^d&bcf({ix^d},^d)=0.25d0*(bc({ix^d},^d)+bc(ix1-1,ix2,ix3,^d)&
683 +bc(ix1,ix2,ix3-1,^d)+bc(ix1-1,ix2,ix3-1,^d))\
684 kaf(ix^d)=0.25d0*(ka(ix1,ix2,ix3)+ka(ix1-1,ix2,ix3)&
685 +ka(ix1,ix2,ix3-1)+ka(ix1-1,ix2,ix3-1))
686 if(fl%tc_perpendicular) &
687 kef(ix^d)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1-1,ix2,ix3)&
688 +ke(ix1,ix2,ix3-1)+ke(ix1-1,ix2,ix3-1))
689 {end do\}
690 else
691 {do ix^db=ixamin^db,ixamax^db\}
692 ^d&bcf({ix^d},^d)=0.25d0*(bc({ix^d},^d)+bc(ix1,ix2-1,ix3,^d)&
693 +bc(ix1-1,ix2,ix3,^d)+bc(ix1-1,ix2-1,ix3,^d))\
694 kaf(ix^d)=0.25d0*(ka(ix1,ix2,ix3)+ka(ix1,ix2-1,ix3)&
695 +ka(ix1-1,ix2,ix3)+ka(ix1-1,ix2-1,ix3))
696 if(fl%tc_perpendicular) &
697 kef(ix^d)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1,ix2-1,ix3)&
698 +ke(ix1-1,ix2,ix3)+ke(ix1-1,ix2-1,ix3))
699 {end do\}
700 end if
701 }
702 {^iftwod
703 if(idims==1) then
704 {do ix^db=ixamin^db,ixamax^db\}
705 ^d&bcf({ix^d},^d)=0.5d0*(bc(ix1,ix2,^d)+bc(ix1,ix2-1,^d))\
706 kaf(ix^d)=0.5d0*(ka(ix1,ix2)+ka(ix1,ix2-1))
707 if(fl%tc_perpendicular) &
708 kef(ix^d)=0.5d0*(ke(ix1,ix2)+ke(ix1,ix2-1))
709 {end do\}
710 else
711 {do ix^db=ixamin^db,ixamax^db\}
712 ^d&bcf({ix^d},^d)=0.5d0*(bc(ix1,ix2,^d)+bc(ix1-1,ix2,^d))\
713 kaf(ix^d)=0.5d0*(ka(ix1,ix2)+ka(ix1-1,ix2))
714 if(fl%tc_perpendicular) &
715 kef(ix^d)=0.5d0*(ke(ix1,ix2)+ke(ix1-1,ix2))
716 {end do\}
717 end if
718 }
719 ! eq (19)
720 ! temperature gradient at cell corner
721 {^ifthreed
722 if(idims==1) then
723 {do ix^db=ixcmin^db,ixcmax^db\}
724 qdd(ix^d)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1,ix2+1,ix3,idims)&
725 +gradt(ix1,ix2,ix3+1,idims)+gradt(ix1,ix2+1,ix3+1,idims))
726 {end do\}
727 else if(idims==2) then
728 {do ix^db=ixcmin^db,ixcmax^db\}
729 qdd(ix^d)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1+1,ix2,ix3,idims)&
730 +gradt(ix1,ix2,ix3+1,idims)+gradt(ix1+1,ix2,ix3+1,idims))
731 {end do\}
732 else
733 {do ix^db=ixcmin^db,ixcmax^db\}
734 qdd(ix^d)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1+1,ix2,ix3,idims)&
735 +gradt(ix1,ix2+1,ix3,idims)+gradt(ix1+1,ix2+1,ix3,idims))
736 {end do\}
737 end if
738 }
739 {^iftwod
740 if(idims==1) then
741 {do ix^db=ixcmin^db,ixcmax^db\}
742 qdd(ix^d)=0.5d0*(gradt(ix1,ix2,idims)+gradt(ix1,ix2+1,idims))
743 {end do\}
744 else
745 {do ix^db=ixcmin^db,ixcmax^db\}
746 qdd(ix^d)=0.5d0*(gradt(ix1,ix2,idims)+gradt(ix1+1,ix2,idims))
747 {end do\}
748 end if
749 }
750 ! eq (21)
751 {^ifthreed
752 if(idims==1) then
753 {do ix^db=ixamin^db,ixamax^db\}
754 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
755 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
756 if(qdd(ix^d)<minq) then
757 qd(ix^d,1)=minq
758 else if(qdd(ix^d)>maxq) then
759 qd(ix^d,1)=maxq
760 else
761 qd(ix^d,1)=qdd(ix^d)
762 end if
763 if(qdd(ix1,ix2-1,ix3)<minq) then
764 qd(ix^d,2)=minq
765 else if(qdd(ix1,ix2-1,ix3)>maxq) then
766 qd(ix^d,2)=maxq
767 else
768 qd(ix^d,2)=qdd(ix1,ix2-1,ix3)
769 end if
770 if(qdd(ix1,ix2,ix3-1)<minq) then
771 qd(ix^d,3)=minq
772 else if(qdd(ix1,ix2,ix3-1)>maxq) then
773 qd(ix^d,3)=maxq
774 else
775 qd(ix^d,3)=qdd(ix1,ix2,ix3-1)
776 end if
777 if(qdd(ix1,ix2-1,ix3-1)<minq) then
778 qd(ix^d,4)=minq
779 else if(qdd(ix1,ix2-1,ix3-1)>maxq) then
780 qd(ix^d,4)=maxq
781 else
782 qd(ix^d,4)=qdd(ix1,ix2-1,ix3-1)
783 end if
784 qvec(ix^d,idims)=kaf(ix^d)*0.25d0*(bc(ix^d,idims)**2*qd(ix^d,1)+bc(ix1,ix2-1,ix3,idims)**2*qd(ix^d,2)&
785 +bc(ix1,ix2,ix3-1,idims)**2*qd(ix^d,3)+bc(ix1,ix2-1,ix3-1,idims)**2*qd(ix^d,4))
786 if(fl%tc_perpendicular) &
787 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.25d0*(qd(ix^d,1)+qd(ix^d,2)+qd(ix^d,3)+qd(ix^d,4))
788 {end do\}
789 else if(idims==2) then
790 {do ix^db=ixamin^db,ixamax^db\}
791 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
792 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
793 if(qdd(ix^d)<minq) then
794 qd(ix^d,1)=minq
795 else if(qdd(ix^d)>maxq) then
796 qd(ix^d,1)=maxq
797 else
798 qd(ix^d,1)=qdd(ix^d)
799 end if
800 if(qdd(ix1-1,ix2,ix3)<minq) then
801 qd(ix^d,2)=minq
802 else if(qdd(ix1-1,ix2,ix3)>maxq) then
803 qd(ix^d,2)=maxq
804 else
805 qd(ix^d,2)=qdd(ix1-1,ix2,ix3)
806 end if
807 if(qdd(ix1,ix2,ix3-1)<minq) then
808 qd(ix^d,3)=minq
809 else if(qdd(ix1,ix2,ix3-1)>maxq) then
810 qd(ix^d,3)=maxq
811 else
812 qd(ix^d,3)=qdd(ix1,ix2,ix3-1)
813 end if
814 if(qdd(ix1-1,ix2,ix3-1)<minq) then
815 qd(ix^d,4)=minq
816 else if(qdd(ix1-1,ix2,ix3-1)>maxq) then
817 qd(ix^d,4)=maxq
818 else
819 qd(ix^d,4)=qdd(ix1-1,ix2,ix3-1)
820 end if
821 qvec(ix^d,idims)=kaf(ix^d)*0.25d0*(bc(ix^d,idims)**2*qd(ix^d,1)+bc(ix1-1,ix2,ix3,idims)**2*qd(ix^d,2)&
822 +bc(ix1,ix2,ix3-1,idims)**2*qd(ix^d,3)+bc(ix1-1,ix2,ix3-1,idims)**2*qd(ix^d,4))
823 if(fl%tc_perpendicular) &
824 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.25d0*(qd(ix^d,1)+qd(ix^d,2)+qd(ix^d,3)+qd(ix^d,4))
825 {end do\}
826 else
827 {do ix^db=ixamin^db,ixamax^db\}
828 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
829 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
830 if(qdd(ix^d)<minq) then
831 qd(ix^d,1)=minq
832 else if(qdd(ix^d)>maxq) then
833 qd(ix^d,1)=maxq
834 else
835 qd(ix^d,1)=qdd(ix^d)
836 end if
837 if(qdd(ix1-1,ix2,ix3)<minq) then
838 qd(ix^d,2)=minq
839 else if(qdd(ix1-1,ix2,ix3)>maxq) then
840 qd(ix^d,2)=maxq
841 else
842 qd(ix^d,2)=qdd(ix1-1,ix2,ix3)
843 end if
844 if(qdd(ix1,ix2-1,ix3)<minq) then
845 qd(ix^d,3)=minq
846 else if(qdd(ix1,ix2-1,ix3)>maxq) then
847 qd(ix^d,3)=maxq
848 else
849 qd(ix^d,3)=qdd(ix1,ix2-1,ix3)
850 end if
851 if(qdd(ix1-1,ix2-1,ix3)<minq) then
852 qd(ix^d,4)=minq
853 else if(qdd(ix1-1,ix2-1,ix3)>maxq) then
854 qd(ix^d,4)=maxq
855 else
856 qd(ix^d,4)=qdd(ix1-1,ix2-1,ix3)
857 end if
858 qvec(ix^d,idims)=kaf(ix^d)*0.25d0*(bc(ix^d,idims)**2*qd(ix^d,1)+bc(ix1-1,ix2,ix3,idims)**2*qd(ix^d,2)&
859 +bc(ix1,ix2-1,ix3,idims)**2*qd(ix^d,3)+bc(ix1-1,ix2-1,ix3,idims)**2*qd(ix^d,4))
860 if(fl%tc_perpendicular) &
861 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.25d0*(qd(ix^d,1)+qd(ix^d,2)+qd(ix^d,3)+qd(ix^d,4))
862 {end do\}
863 end if
864 }
865 {^iftwod
866 if(idims==1) then
867 {do ix^db=ixamin^db,ixamax^db\}
868 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
869 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
870 if(qdd(ix^d)<minq) then
871 qd(ix^d,1)=minq
872 else if(qdd(ix^d)>maxq) then
873 qd(ix^d,1)=maxq
874 else
875 qd(ix^d,1)=qdd(ix^d)
876 end if
877 if(qdd(ix1,ix2-1)<minq) then
878 qd(ix^d,2)=minq
879 else if(qdd(ix1,ix2-1)>maxq) then
880 qd(ix^d,2)=maxq
881 else
882 qd(ix^d,2)=qdd(ix1,ix2-1)
883 end if
884 qvec(ix^d,idims)=kaf(ix^d)*0.5d0*(bc(ix1,ix2,idims)**2*qd(ix^d,1)+bc(ix1,ix2-1,idims)**2*qd(ix^d,2))
885 if(fl%tc_perpendicular) &
886 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.5d0*(qd(ix^d,1)+qd(ix^d,2))
887 {end do\}
888 else
889 {do ix^db=ixamin^db,ixamax^db\}
890 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
891 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
892 if(qdd(ix^d)<minq) then
893 qd(ix^d,1)=minq
894 else if(qdd(ix^d)>maxq) then
895 qd(ix^d,1)=maxq
896 else
897 qd(ix^d,1)=qdd(ix^d)
898 end if
899 if(qdd(ix1-1,ix2)<minq) then
900 qd(ix^d,2)=minq
901 else if(qdd(ix1-1,ix2)>maxq) then
902 qd(ix^d,2)=maxq
903 else
904 qd(ix^d,2)=qdd(ix1-1,ix2)
905 end if
906 qvec(ix^d,idims)=kaf(ix^d)*0.5d0*(bc(ix1,ix2,idims)**2*qd(ix^d,1)+bc(ix1-1,ix2,idims)**2*qd(ix^d,2))
907 if(fl%tc_perpendicular) &
908 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.5d0*(qd(ix^d,1)+qd(ix^d,2))
909 {end do\}
910 end if
911 }
912 ! calculate normal of magnetic field
913 ixb^l=ixa^l+kr(idims,^d);
914 bnorm(ixa^s)=0.5d0*(mf(ixa^s,idims)+mf(ixb^s,idims))
915 ! limited transverse component, eq (17)
916 ixbmin^d=ixamin^d;
917 ixbmax^d=ixamax^d+kr(idims,^d);
918 do idir=1,ndim
919 if(idir==idims) cycle
920 qdd(ixi^s)=slope_limiter(gradt(ixi^s,idir),ixi^l,ixb^l,idir,-1,fl%tc_slope_limiter)
921 qdd(ixi^s)=slope_limiter(qdd,ixi^l,ixa^l,idims,1,fl%tc_slope_limiter)
922 qvec(ixa^s,idims)=qvec(ixa^s,idims)+kaf(ixa^s)*bnorm(ixa^s)*bcf(ixa^s,idir)*qdd(ixa^s)
923 end do
924 if(fl%tc_saturate) then
925 ! saturated flux 5 phi rho c**3 (Cowie and McKee 1977 eq 8), phi and the LTE c**3 factor in sat_fac
926 ! (tc_set_saturation); under LTE rho is scaled by the local over the fully ionised ne/nH
927 ixb^l=ixa^l+kr(idims,^d);
928 qdd(ixa^s)=fl%sat_fac*(rhos(ixa^s)+rhos(ixb^s))*dsqrt(0.5d0*(te(ixa^s)+te(ixb^s)))**3*dabs(bnorm(ixa^s))
929 {do ix^db=ixamin^db,ixamax^db\}
930 if(dabs(qvec(ix^d,idims))>qdd(ix^d)) then
931 qvec(ix^d,idims)=sign(1.d0,qvec(ix^d,idims))*qdd(ix^d)
932 end if
933 {end do\}
934 end if
935 end do
936 end subroutine set_source_tc_mhd
937 subroutine set_source_tc_mhd_geo(ixI^L,ixO^L,w,x,fl,qvec,rho,Te,alpha)
939 integer, intent(in) :: ixI^L, ixO^L
940 double precision, intent(in) :: x(ixI^S,1:ndim)
941 double precision, intent(in) :: w(ixI^S,1:nw)
942 type(tc_fluid), intent(in) :: fl
943 double precision, intent(in) :: rho(ixI^S),Te(ixI^S)
944 double precision, intent(in) :: alpha
945 double precision, intent(out) :: qvec(ixI^S,1:ndim)
946 double precision :: rhos(ixI^S)
947
948 !! qdd store the heat conduction energy changing rate
949 double precision, dimension(ixI^S,1:ndim) :: mf,Bc,Bcf,gradT
950 double precision, dimension(ixI^S) :: ka,kaf,ke,kef,qdd,Bnorm
951 double precision :: minq,maxq,qd(ixI^S,2**(ndim-1)), blocal(ndir)
952 integer :: idims,idir,ix^D,ix^L,ixC^L,ixA^L,ixB^L
953
954 ix^l=ixo^l^ladd1;
955 if(fl%tc_saturate) call tc_sat_rho(ixi^l,w,x,fl,rho,rhos)
956
957 ! T gradient at cell faces
958 ! b unit vector mf: magnetic field direction vector
959 if(allocated(iw_mag)) then
960 if(b0field) then
961 {do ix^db=ixmin^db,ixmax^db\}
962 ^c&blocal(^c)=w({ix^d},iw_mag(^c))+block%B0({ix^d},^c,0)\
963 {^iftwod
964 if(blocal(1)/=0.d0) then
965 mf(ix^d,1)=sign(1.d0,blocal(1))/dsqrt(1.d0+(^ce&(blocal(^ce)/blocal(1))**2+))
966 else
967 mf(ix^d,1)=0.d0
968 end if
969 if(blocal(2)/=0.d0) then
970 mf(ix^d,2)=sign(1.d0,blocal(2))/dsqrt(1.d0+(^cf&(blocal(^cf)/blocal(2))**2+))
971 else
972 mf(ix^d,2)=0.d0
973 end if
974 }
975 {^ifthreed
976 if(blocal(1)/=0.d0) then
977 mf(ix^d,1)=sign(1.d0,blocal(1))/dsqrt(1.d0+(blocal(2)/blocal(1))**2+(blocal(3)/blocal(1))**2)
978 else
979 mf(ix^d,1)=0.d0
980 end if
981 if(blocal(2)/=0.d0) then
982 mf(ix^d,2)=sign(1.d0,blocal(2))/dsqrt(1.d0+(blocal(1)/blocal(2))**2+(blocal(3)/blocal(2))**2)
983 else
984 mf(ix^d,2)=0.d0
985 end if
986 if(blocal(3)/=0.d0) then
987 mf(ix^d,3)=sign(1.d0,blocal(3))/dsqrt(1.d0+(blocal(1)/blocal(3))**2+(blocal(2)/blocal(3))**2)
988 else
989 mf(ix^d,3)=0.d0
990 end if
991 }
992 {end do\}
993 else
994 {do ix^db=ixmin^db,ixmax^db\}
995 {^iftwod
996 if(w(ix^d,iw_mag(1))/=0.d0) then
997 mf(ix^d,1)=sign(1.d0,w(ix^d,iw_mag(1)))/dsqrt(1.d0+(^ce&(w(ix^d,iw_mag(^ce))/w(ix^d,iw_mag(1)))**2+))
998 else
999 mf(ix^d,1)=0.d0
1000 end if
1001 if(w(ix^d,iw_mag(2))/=0.d0) then
1002 mf(ix^d,2)=sign(1.d0,w(ix^d,iw_mag(2)))/dsqrt(1.d0+(^cf&(w(ix^d,iw_mag(^cf))/w(ix^d,iw_mag(2)))**2+))
1003 else
1004 mf(ix^d,2)=0.d0
1005 end if
1006 }
1007 {^ifthreed
1008 if(w(ix^d,iw_mag(1))/=0.d0) then
1009 mf(ix^d,1)=sign(1.d0,w(ix^d,iw_mag(1)))/dsqrt(1.d0+(w(ix^d,iw_mag(2))/w(ix^d,iw_mag(1)))**2+&
1010 (w(ix^d,iw_mag(3))/w(ix^d,iw_mag(1)))**2)
1011 else
1012 mf(ix^d,1)=0.d0
1013 end if
1014 if(w(ix^d,iw_mag(2))/=0.d0) then
1015 mf(ix^d,2)=sign(1.d0,w(ix^d,iw_mag(2)))/dsqrt(1.d0+(w(ix^d,iw_mag(1))/w(ix^d,iw_mag(2)))**2+&
1016 (w(ix^d,iw_mag(3))/w(ix^d,iw_mag(2)))**2)
1017 else
1018 mf(ix^d,2)=0.d0
1019 end if
1020 if(w(ix^d,iw_mag(3))/=0.d0) then
1021 mf(ix^d,3)=sign(1.d0,w(ix^d,iw_mag(3)))/dsqrt(1.d0+(w(ix^d,iw_mag(1))/w(ix^d,iw_mag(3)))**2+&
1022 (w(ix^d,iw_mag(2))/w(ix^d,iw_mag(3)))**2)
1023 else
1024 mf(ix^d,3)=0.d0
1025 end if
1026 }
1027 {end do\}
1028 end if
1029 else
1030 mf(ix^s,1:ndim)=block%B0(ix^s,1:ndim,0)
1031 endif
1032 ! ixC is cell-corner index
1033 ixcmax^d=ixomax^d; ixcmin^d=ixomin^d-1;
1034 ! b unit vector at cell corner
1035 {^ifthreed
1036 do idims=1,3
1037 {do ix^db=ixcmin^db,ixcmax^db\}
1038 bc(ix^d,idims)=(mf(ix1,ix2,ix3,idims)*block%dvolume(ix1,ix2,ix3)+mf(ix1+1,ix2,ix3,idims)*block%dvolume(ix1+1,ix2,ix3)&
1039 +mf(ix1,ix2+1,ix3,idims)*block%dvolume(ix1,ix2+1,ix3)+mf(ix1+1,ix2+1,ix3,idims)*block%dvolume(ix1+1,ix2+1,ix3)&
1040 +mf(ix1,ix2,ix3+1,idims)*block%dvolume(ix1,ix2,ix3+1)+mf(ix1+1,ix2,ix3+1,idims)*block%dvolume(ix1+1,ix2,ix3+1)&
1041 +mf(ix1,ix2+1,ix3+1,idims)*block%dvolume(ix1,ix2+1,ix3+1)+mf(ix1+1,ix2+1,ix3+1,idims)*block%dvolume(ix1+1,ix2+1,ix3+1))&
1042 /(block%dvolume(ix1,ix2,ix3)+block%dvolume(ix1+1,ix2,ix3)+block%dvolume(ix1,ix2+1,ix3)+block%dvolume(ix1+1,ix2+1,ix3)&
1043 +block%dvolume(ix1,ix2,ix3+1)+block%dvolume(ix1+1,ix2,ix3+1)+block%dvolume(ix1,ix2+1,ix3+1)+block%dvolume(ix1+1,ix2+1,ix3+1))
1044 {end do\}
1045 end do
1046 }
1047 {^iftwod
1048 do idims=1,2
1049 {do ix^db=ixcmin^db,ixcmax^db\}
1050 bc(ix^d,idims)=(mf(ix1,ix2,idims)*block%dvolume(ix1,ix2)+mf(ix1+1,ix2,idims)*block%dvolume(ix1+1,ix2)&
1051 +mf(ix1,ix2+1,idims)*block%dvolume(ix1,ix2+1)+mf(ix1+1,ix2+1,idims)*block%dvolume(ix1+1,ix2+1))&
1052 /(block%dvolume(ix1,ix2)+block%dvolume(ix1+1,ix2)+block%dvolume(ix1,ix2+1)+block%dvolume(ix1+1,ix2+1))
1053 {end do\}
1054 end do
1055 }
1056 ! T gradient at cell faces
1057 do idims=1,ndim
1058 ixbmin^d=ixmin^d;
1059 ixbmax^d=ixmax^d-kr(idims,^d);
1060 call gradientf(te,x,ixi^l,ixb^l,idims,gradt(ixi^s,idims))
1061 end do
1062 if(fl%tc_constant) then
1063 if(fl%tc_perpendicular) then
1064 ka(ixc^s)=fl%tc_k_para-fl%tc_k_perp
1065 ke(ixc^s)=fl%tc_k_perp
1066 else
1067 ka(ixc^s)=fl%tc_k_para
1068 end if
1069 else
1070 ! conductivity at cell center
1071 if(phys_trac) then
1072 {do ix^db=ixmin^db,ixmax^db\}
1073 if(te(ix^d) < block%wextra(ix^d,fl%Tcoff_)) then
1074 qdd(ix^d)=fl%tc_k_para*dsqrt(block%wextra(ix^d,fl%Tcoff_)**5)
1075 else
1076 qdd(ix^d)=fl%tc_k_para*dsqrt(te(ix^d)**5)
1077 end if
1078 {end do\}
1079 else
1080 qdd(ix^s)=fl%tc_k_para*dsqrt(te(ix^s)**5)
1081 end if
1082 ! cell corner parallel conductivity in ka
1083 {^ifthreed
1084 {do ix^db=ixcmin^db,ixcmax^db\}
1085 ka(ix^d)=(qdd(ix1,ix2,ix3)*block%dvolume(ix1,ix2,ix3)+qdd(ix1+1,ix2,ix3)*block%dvolume(ix1+1,ix2,ix3)&
1086 +qdd(ix1,ix2+1,ix3)*block%dvolume(ix1,ix2+1,ix3)+qdd(ix1+1,ix2+1,ix3)*block%dvolume(ix1+1,ix2+1,ix3)&
1087 +qdd(ix1,ix2,ix3+1)*block%dvolume(ix1,ix2,ix3+1)+qdd(ix1+1,ix2,ix3+1)*block%dvolume(ix1+1,ix2,ix3+1)&
1088 +qdd(ix1,ix2+1,ix3+1)*block%dvolume(ix1,ix2+1,ix3+1)+qdd(ix1+1,ix2+1,ix3+1)*block%dvolume(ix1+1,ix2+1,ix3+1))&
1089 /(block%dvolume(ix1,ix2,ix3)+block%dvolume(ix1+1,ix2,ix3)+block%dvolume(ix1,ix2+1,ix3)+block%dvolume(ix1+1,ix2+1,ix3)&
1090 +block%dvolume(ix1,ix2,ix3+1)+block%dvolume(ix1+1,ix2,ix3+1)+block%dvolume(ix1,ix2+1,ix3+1)+block%dvolume(ix1+1,ix2+1,ix3+1))
1091 {end do\}
1092 }
1093 {^iftwod
1094 {do ix^db=ixcmin^db,ixcmax^db\}
1095 ka(ix^d)=(qdd(ix1,ix2)*block%dvolume(ix1,ix2)+qdd(ix1+1,ix2)*block%dvolume(ix1+1,ix2)&
1096 +qdd(ix1,ix2+1)*block%dvolume(ix1,ix2+1)+qdd(ix1+1,ix2+1)*block%dvolume(ix1+1,ix2+1))&
1097 /(block%dvolume(ix1,ix2)+block%dvolume(ix1+1,ix2)+block%dvolume(ix1,ix2+1)+block%dvolume(ix1+1,ix2+1))
1098 {end do\}
1099 }
1100 ! compensate with perpendicular conductivity
1101 if(fl%tc_perpendicular) then
1102 if(b0field) then
1103 qdd(ix^s)=fl%tc_k_perp*rho(ix^s)**2/((^c&(w(ix^s,iw_mag(^c))+block%B0(ix^s,^c,0))**2+)*dsqrt(te(ix^s))+smalldouble)
1104 else
1105 qdd(ix^s)=fl%tc_k_perp*rho(ix^s)**2/((^c&w(ix^s,iw_mag(^c))**2+)*dsqrt(te(ix^s))+smalldouble)
1106 end if
1107 {^ifthreed
1108 {do ix^db=ixcmin^db,ixcmax^db\}
1109 ke(ix^d)=(qdd(ix1,ix2,ix3)*block%dvolume(ix1,ix2,ix3)+qdd(ix1+1,ix2,ix3)*block%dvolume(ix1+1,ix2,ix3)&
1110 +qdd(ix1,ix2+1,ix3)*block%dvolume(ix1,ix2+1,ix3)+qdd(ix1+1,ix2+1,ix3)*block%dvolume(ix1+1,ix2+1,ix3)&
1111 +qdd(ix1,ix2,ix3+1)*block%dvolume(ix1,ix2,ix3+1)+qdd(ix1+1,ix2,ix3+1)*block%dvolume(ix1+1,ix2,ix3+1)&
1112 +qdd(ix1,ix2+1,ix3+1)*block%dvolume(ix1,ix2+1,ix3+1)+qdd(ix1+1,ix2+1,ix3+1)*block%dvolume(ix1+1,ix2+1,ix3+1))&
1113 /(block%dvolume(ix1,ix2,ix3)+block%dvolume(ix1+1,ix2,ix3)+block%dvolume(ix1,ix2+1,ix3)+block%dvolume(ix1+1,ix2+1,ix3)&
1114 +block%dvolume(ix1,ix2,ix3+1)+block%dvolume(ix1+1,ix2,ix3+1)+block%dvolume(ix1,ix2+1,ix3+1)+block%dvolume(ix1+1,ix2+1,ix3+1))
1115 if(ke(ix^d)<ka(ix^d)) then
1116 ka(ix^d)=ka(ix^d)-ke(ix^d)
1117 else
1118 ke(ix^d)=ka(ix^d)
1119 ka(ix^d)=0.d0
1120 end if
1121 {end do\}
1122 }
1123 {^iftwod
1124 {do ix^db=ixcmin^db,ixcmax^db\}
1125 ke(ix^d)=(qdd(ix1,ix2)*block%dvolume(ix1,ix2)+qdd(ix1+1,ix2)*block%dvolume(ix1+1,ix2)&
1126 +qdd(ix1,ix2+1)*block%dvolume(ix1,ix2+1)+qdd(ix1+1,ix2+1)*block%dvolume(ix1+1,ix2+1))&
1127 /(block%dvolume(ix1,ix2)+block%dvolume(ix1+1,ix2)+block%dvolume(ix1,ix2+1)+block%dvolume(ix1+1,ix2+1))
1128 if(ke(ix^d)<ka(ix^d)) then
1129 ka(ix^d)=ka(ix^d)-ke(ix^d)
1130 else
1131 ke(ix^d)=ka(ix^d)
1132 ka(ix^d)=0.d0
1133 end if
1134 {end do\}
1135 }
1136 end if
1137 end if
1138 ! calculate thermal conduction flux with slope-limited symmetric scheme
1139 do idims=1,ndim
1140 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
1141 {^ifthreed
1142 if(idims==1) then
1143 {do ix^db=ixamin^db,ixamax^db\}
1144 ! averaged b at face centers
1145 ^d&bcf({ix^d},^d)=0.25d0*(bc({ix^d},^d)+bc(ix1,ix2-1,ix3,^d)&
1146 +bc(ix1,ix2,ix3-1,^d)+bc(ix1,ix2-1,ix3-1,^d))\
1147 kaf(ix^d)=0.25d0*(ka(ix1,ix2,ix3)+ka(ix1,ix2-1,ix3)&
1148 +ka(ix1,ix2,ix3-1)+ka(ix1,ix2-1,ix3-1))
1149 ! averaged thermal conductivity at face centers
1150 if(fl%tc_perpendicular) &
1151 kef(ix^d)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1,ix2-1,ix3)&
1152 +ke(ix1,ix2,ix3-1)+ke(ix1,ix2-1,ix3-1))
1153 {end do\}
1154 else if(idims==2) then
1155 {do ix^db=ixamin^db,ixamax^db\}
1156 ^d&bcf({ix^d},^d)=0.25d0*(bc({ix^d},^d)+bc(ix1-1,ix2,ix3,^d)&
1157 +bc(ix1,ix2,ix3-1,^d)+bc(ix1-1,ix2,ix3-1,^d))\
1158 kaf(ix^d)=0.25d0*(ka(ix1,ix2,ix3)+ka(ix1-1,ix2,ix3)&
1159 +ka(ix1,ix2,ix3-1)+ka(ix1-1,ix2,ix3-1))
1160 if(fl%tc_perpendicular) &
1161 kef(ix^d)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1-1,ix2,ix3)&
1162 +ke(ix1,ix2,ix3-1)+ke(ix1-1,ix2,ix3-1))
1163 {end do\}
1164 else
1165 {do ix^db=ixamin^db,ixamax^db\}
1166 ^d&bcf({ix^d},^d)=0.25d0*(bc({ix^d},^d)+bc(ix1,ix2-1,ix3,^d)&
1167 +bc(ix1-1,ix2,ix3,^d)+bc(ix1-1,ix2-1,ix3,^d))\
1168 kaf(ix^d)=0.25d0*(ka(ix1,ix2,ix3)+ka(ix1,ix2-1,ix3)&
1169 +ka(ix1-1,ix2,ix3)+ka(ix1-1,ix2-1,ix3))
1170 if(fl%tc_perpendicular) &
1171 kef(ix^d)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1,ix2-1,ix3)&
1172 +ke(ix1-1,ix2,ix3)+ke(ix1-1,ix2-1,ix3))
1173 {end do\}
1174 end if
1175 }
1176 {^iftwod
1177 if(idims==1) then
1178 {do ix^db=ixamin^db,ixamax^db\}
1179 ^d&bcf({ix^d},^d)=0.5d0*(bc(ix1,ix2,^d)+bc(ix1,ix2-1,^d))\
1180 kaf(ix^d)=0.5d0*(ka(ix1,ix2)+ka(ix1,ix2-1))
1181 if(fl%tc_perpendicular) &
1182 kef(ix^d)=0.5d0*(ke(ix1,ix2)+ke(ix1,ix2-1))
1183 {end do\}
1184 else
1185 {do ix^db=ixamin^db,ixamax^db\}
1186 ^d&bcf({ix^d},^d)=0.5d0*(bc(ix1,ix2,^d)+bc(ix1-1,ix2,^d))\
1187 kaf(ix^d)=0.5d0*(ka(ix1,ix2)+ka(ix1-1,ix2))
1188 if(fl%tc_perpendicular) &
1189 kef(ix^d)=0.5d0*(ke(ix1,ix2)+ke(ix1-1,ix2))
1190 {end do\}
1191 end if
1192 }
1193 ! eq (19)
1194 ! temperature gradient at cell corner
1195 {^ifthreed
1196 if(idims==1) then
1197 {do ix^db=ixcmin^db,ixcmax^db\}
1198 qdd(ix^d)=(gradt(ix1,ix2,ix3,idims)*block%surfaceC(ix1,ix2,ix3,1)&
1199 +gradt(ix1,ix2+1,ix3,idims)*block%surfaceC(ix1,ix2+1,ix3,1)&
1200 +gradt(ix1,ix2,ix3+1,idims)*block%surfaceC(ix1,ix2,ix3+1,1)&
1201 +gradt(ix1,ix2+1,ix3+1,idims)*block%surfaceC(ix1,ix2+1,ix3+1,1))/&
1202 (block%surfaceC(ix1,ix2,ix3,1)+block%surfaceC(ix1,ix2+1,ix3,1)&
1203 +block%surfaceC(ix1,ix2,ix3+1,1)+block%surfaceC(ix1,ix2+1,ix3+1,1)+smalldouble**2)
1204 {end do\}
1205 else if(idims==2) then
1206 {do ix^db=ixcmin^db,ixcmax^db\}
1207 qdd(ix^d)=(gradt(ix1,ix2,ix3,idims)*block%surfaceC(ix1,ix2,ix3,2)&
1208 +gradt(ix1+1,ix2,ix3,idims)*block%surfaceC(ix1+1,ix2,ix3,2)&
1209 +gradt(ix1,ix2,ix3+1,idims)*block%surfaceC(ix1,ix2,ix3+1,2)&
1210 +gradt(ix1+1,ix2,ix3+1,idims)*block%surfaceC(ix1+1,ix2,ix3+1,2))/&
1211 (block%surfaceC(ix1,ix2,ix3,2)+block%surfaceC(ix1+1,ix2,ix3,2)&
1212 +block%surfaceC(ix1,ix2,ix3+1,2)+block%surfaceC(ix1+1,ix2,ix3+1,2)+smalldouble**2)
1213 {end do\}
1214 else
1215 {do ix^db=ixcmin^db,ixcmax^db\}
1216 qdd(ix^d)=(gradt(ix1,ix2,ix3,idims)*block%surfaceC(ix1,ix2,ix3,3)&
1217 +gradt(ix1+1,ix2,ix3,idims)*block%surfaceC(ix1+1,ix2,ix3,3)&
1218 +gradt(ix1,ix2+1,ix3,idims)*block%surfaceC(ix1,ix2+1,ix3,3)&
1219 +gradt(ix1+1,ix2+1,ix3,idims)*block%surfaceC(ix1+1,ix2+1,ix3,3))/&
1220 (block%surfaceC(ix1,ix2,ix3,3)+block%surfaceC(ix1+1,ix2,ix3,3)&
1221 +block%surfaceC(ix1,ix2+1,ix3,3)+block%surfaceC(ix1+1,ix2+1,ix3,3))
1222 {end do\}
1223 end if
1224 }
1225 {^iftwod
1226 if(idims==1) then
1227 {do ix^db=ixcmin^db,ixcmax^db\}
1228 qdd(ix^d)=(gradt(ix1,ix2,idims)*block%surfaceC(ix1,ix2,1)&
1229 +gradt(ix1,ix2+1,idims)*block%surfaceC(ix1,ix2+1,1))/&
1230 (block%surfaceC(ix1,ix2,1)+block%surfaceC(ix1,ix2+1,1))
1231 {end do\}
1232 else
1233 {do ix^db=ixcmin^db,ixcmax^db\}
1234 qdd(ix^d)=(gradt(ix1,ix2,idims)*block%surfaceC(ix1,ix2,2)&
1235 +gradt(ix1+1,ix2,idims)*block%surfaceC(ix1+1,ix2,2))/&
1236 (block%surfaceC(ix1,ix2,2)+block%surfaceC(ix1+1,ix2,2)+smalldouble)
1237 {end do\}
1238 end if
1239 }
1240 ! eq (21)
1241 {^ifthreed
1242 if(idims==1) then
1243 {do ix^db=ixamin^db,ixamax^db\}
1244 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1245 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1246 if(qdd(ix^d)<minq) then
1247 qd(ix^d,1)=minq
1248 else if(qdd(ix^d)>maxq) then
1249 qd(ix^d,1)=maxq
1250 else
1251 qd(ix^d,1)=qdd(ix^d)
1252 end if
1253 if(qdd(ix1,ix2-1,ix3)<minq) then
1254 qd(ix^d,2)=minq
1255 else if(qdd(ix1,ix2-1,ix3)>maxq) then
1256 qd(ix^d,2)=maxq
1257 else
1258 qd(ix^d,2)=qdd(ix1,ix2-1,ix3)
1259 end if
1260 if(qdd(ix1,ix2,ix3-1)<minq) then
1261 qd(ix^d,3)=minq
1262 else if(qdd(ix1,ix2,ix3-1)>maxq) then
1263 qd(ix^d,3)=maxq
1264 else
1265 qd(ix^d,3)=qdd(ix1,ix2,ix3-1)
1266 end if
1267 if(qdd(ix1,ix2-1,ix3-1)<minq) then
1268 qd(ix^d,4)=minq
1269 else if(qdd(ix1,ix2-1,ix3-1)>maxq) then
1270 qd(ix^d,4)=maxq
1271 else
1272 qd(ix^d,4)=qdd(ix1,ix2-1,ix3-1)
1273 end if
1274 qvec(ix^d,idims)=kaf(ix^d)*0.25d0*(bc(ix^d,idims)**2*qd(ix^d,1)+bc(ix1,ix2-1,ix3,idims)**2*qd(ix^d,2)&
1275 +bc(ix1,ix2,ix3-1,idims)**2*qd(ix^d,3)+bc(ix1,ix2-1,ix3-1,idims)**2*qd(ix^d,4))
1276 if(fl%tc_perpendicular) &
1277 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.25d0*(qd(ix^d,1)+qd(ix^d,2)+qd(ix^d,3)+qd(ix^d,4))
1278 {end do\}
1279 else if(idims==2) then
1280 {do ix^db=ixamin^db,ixamax^db\}
1281 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1282 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1283 if(qdd(ix^d)<minq) then
1284 qd(ix^d,1)=minq
1285 else if(qdd(ix^d)>maxq) then
1286 qd(ix^d,1)=maxq
1287 else
1288 qd(ix^d,1)=qdd(ix^d)
1289 end if
1290 if(qdd(ix1-1,ix2,ix3)<minq) then
1291 qd(ix^d,2)=minq
1292 else if(qdd(ix1-1,ix2,ix3)>maxq) then
1293 qd(ix^d,2)=maxq
1294 else
1295 qd(ix^d,2)=qdd(ix1-1,ix2,ix3)
1296 end if
1297 if(qdd(ix1,ix2,ix3-1)<minq) then
1298 qd(ix^d,3)=minq
1299 else if(qdd(ix1,ix2,ix3-1)>maxq) then
1300 qd(ix^d,3)=maxq
1301 else
1302 qd(ix^d,3)=qdd(ix1,ix2,ix3-1)
1303 end if
1304 if(qdd(ix1-1,ix2,ix3-1)<minq) then
1305 qd(ix^d,4)=minq
1306 else if(qdd(ix1-1,ix2,ix3-1)>maxq) then
1307 qd(ix^d,4)=maxq
1308 else
1309 qd(ix^d,4)=qdd(ix1-1,ix2,ix3-1)
1310 end if
1311 qvec(ix^d,idims)=kaf(ix^d)*0.25d0*(bc(ix^d,idims)**2*qd(ix^d,1)+bc(ix1-1,ix2,ix3,idims)**2*qd(ix^d,2)&
1312 +bc(ix1,ix2,ix3-1,idims)**2*qd(ix^d,3)+bc(ix1-1,ix2,ix3-1,idims)**2*qd(ix^d,4))
1313 if(fl%tc_perpendicular) &
1314 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.25d0*(qd(ix^d,1)+qd(ix^d,2)+qd(ix^d,3)+qd(ix^d,4))
1315 {end do\}
1316 else
1317 {do ix^db=ixamin^db,ixamax^db\}
1318 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1319 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1320 if(qdd(ix^d)<minq) then
1321 qd(ix^d,1)=minq
1322 else if(qdd(ix^d)>maxq) then
1323 qd(ix^d,1)=maxq
1324 else
1325 qd(ix^d,1)=qdd(ix^d)
1326 end if
1327 if(qdd(ix1-1,ix2,ix3)<minq) then
1328 qd(ix^d,2)=minq
1329 else if(qdd(ix1-1,ix2,ix3)>maxq) then
1330 qd(ix^d,2)=maxq
1331 else
1332 qd(ix^d,2)=qdd(ix1-1,ix2,ix3)
1333 end if
1334 if(qdd(ix1,ix2-1,ix3)<minq) then
1335 qd(ix^d,3)=minq
1336 else if(qdd(ix1,ix2-1,ix3)>maxq) then
1337 qd(ix^d,3)=maxq
1338 else
1339 qd(ix^d,3)=qdd(ix1,ix2-1,ix3)
1340 end if
1341 if(qdd(ix1-1,ix2-1,ix3)<minq) then
1342 qd(ix^d,4)=minq
1343 else if(qdd(ix1-1,ix2-1,ix3)>maxq) then
1344 qd(ix^d,4)=maxq
1345 else
1346 qd(ix^d,4)=qdd(ix1-1,ix2-1,ix3)
1347 end if
1348 qvec(ix^d,idims)=kaf(ix^d)*0.25d0*(bc(ix^d,idims)**2*qd(ix^d,1)+bc(ix1-1,ix2,ix3,idims)**2*qd(ix^d,2)&
1349 +bc(ix1,ix2-1,ix3,idims)**2*qd(ix^d,3)+bc(ix1-1,ix2-1,ix3,idims)**2*qd(ix^d,4))
1350 if(fl%tc_perpendicular) &
1351 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.25d0*(qd(ix^d,1)+qd(ix^d,2)+qd(ix^d,3)+qd(ix^d,4))
1352 {end do\}
1353 end if
1354 }
1355 {^iftwod
1356 if(idims==1) then
1357 {do ix^db=ixamin^db,ixamax^db\}
1358 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1359 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1360 if(qdd(ix^d)<minq) then
1361 qd(ix^d,1)=minq
1362 else if(qdd(ix^d)>maxq) then
1363 qd(ix^d,1)=maxq
1364 else
1365 qd(ix^d,1)=qdd(ix^d)
1366 end if
1367 if(qdd(ix1,ix2-1)<minq) then
1368 qd(ix^d,2)=minq
1369 else if(qdd(ix1,ix2-1)>maxq) then
1370 qd(ix^d,2)=maxq
1371 else
1372 qd(ix^d,2)=qdd(ix1,ix2-1)
1373 end if
1374 qvec(ix^d,idims)=kaf(ix^d)*0.5d0*(bc(ix1,ix2,idims)**2*qd(ix^d,1)+bc(ix1,ix2-1,idims)**2*qd(ix^d,2))
1375 if(fl%tc_perpendicular) &
1376 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.5d0*(qd(ix^d,1)+qd(ix^d,2))
1377 {end do\}
1378 else
1379 {do ix^db=ixamin^db,ixamax^db\}
1380 minq=min(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1381 maxq=max(alpha*gradt(ix^d,idims),gradt(ix^d,idims)/alpha)
1382 if(qdd(ix^d)<minq) then
1383 qd(ix^d,1)=minq
1384 else if(qdd(ix^d)>maxq) then
1385 qd(ix^d,1)=maxq
1386 else
1387 qd(ix^d,1)=qdd(ix^d)
1388 end if
1389 if(qdd(ix1-1,ix2)<minq) then
1390 qd(ix^d,2)=minq
1391 else if(qdd(ix1-1,ix2)>maxq) then
1392 qd(ix^d,2)=maxq
1393 else
1394 qd(ix^d,2)=qdd(ix1-1,ix2)
1395 end if
1396 qvec(ix^d,idims)=kaf(ix^d)*0.5d0*(bc(ix1,ix2,idims)**2*qd(ix^d,1)+bc(ix1-1,ix2,idims)**2*qd(ix^d,2))
1397 if(fl%tc_perpendicular) &
1398 qvec(ix^d,idims)=qvec(ix^d,idims)+kef(ix^d)*0.5d0*(qd(ix^d,1)+qd(ix^d,2))
1399 {end do\}
1400 end if
1401 }
1402 ! calculate normal of magnetic field
1403 ixb^l=ixa^l+kr(idims,^d);
1404 bnorm(ixa^s)=0.5d0*(mf(ixa^s,idims)+mf(ixb^s,idims))
1405 ! limited transverse component, eq (17)
1406 ixbmin^d=ixamin^d;
1407 ixbmax^d=ixamax^d+kr(idims,^d);
1408 do idir=1,ndim
1409 if(idir==idims) cycle
1410 qdd(ixi^s)=slope_limiter(gradt(ixi^s,idir),ixi^l,ixb^l,idir,-1,fl%tc_slope_limiter)
1411 qdd(ixi^s)=slope_limiter(qdd,ixi^l,ixa^l,idims,1,fl%tc_slope_limiter)
1412 qvec(ixa^s,idims)=qvec(ixa^s,idims)+kaf(ixa^s)*bnorm(ixa^s)*bcf(ixa^s,idir)*qdd(ixa^s)
1413 end do
1414 if(fl%tc_saturate) then
1415 ! saturated flux 5 phi rho c**3 (Cowie and McKee 1977 eq 8), phi and the LTE c**3 factor in sat_fac
1416 ! (tc_set_saturation); under LTE rho is scaled by the local over the fully ionised ne/nH
1417 ixb^l=ixa^l+kr(idims,^d);
1418 qdd(ixa^s)=fl%sat_fac*(rhos(ixa^s)+rhos(ixb^s))*dsqrt(0.5d0*(te(ixa^s)+te(ixb^s)))**3*dabs(bnorm(ixa^s))
1419 {do ix^db=ixamin^db,ixamax^db\}
1420 if(dabs(qvec(ix^d,idims))>qdd(ix^d)) then
1421 qvec(ix^d,idims)=sign(1.d0,qvec(ix^d,idims))*qdd(ix^d)
1422 end if
1423 {end do\}
1424 end if
1425 end do
1426 end subroutine set_source_tc_mhd_geo
1427
1428 function slope_limiter(f,ixI^L,ixO^L,idims,pm,tc_slope_limiter) result(lf)
1430 integer, intent(in) :: ixi^l, ixo^l, idims, pm
1431 double precision, intent(in) :: f(ixi^s)
1432 double precision :: lf(ixi^s)
1433 integer, intent(in) :: tc_slope_limiter
1434
1435 double precision, parameter :: qsmall=1.d-12
1436 double precision :: signf(ixi^s)
1437 integer :: ixb^l
1438
1439 ixb^l=ixo^l+pm*kr(idims,^d);
1440 signf(ixo^s)=sign(1.d0,f(ixo^s))
1441 select case(tc_slope_limiter)
1442 case(1)
1443 ! 'MC' monotonized central limiter Woodward and Collela limiter (eq.3.51h), a factor of 2 is pulled out
1444 lf(ixo^s)=two*signf(ixo^s)* &
1445 max(zero,min(dabs(f(ixo^s)),signf(ixo^s)*f(ixb^s),&
1446 signf(ixo^s)*quarter*(f(ixb^s)+f(ixo^s))))
1447 case(2)
1448 ! 'minmod' limiter
1449 lf(ixo^s)=signf(ixo^s)*max(0.d0,min(abs(f(ixo^s)),signf(ixo^s)*f(ixb^s)))
1450 case(3)
1451 ! 'superbee' Roe superbee limiter (eq.3.51i)
1452 lf(ixo^s)=signf(ixo^s)* &
1453 max(zero,min(two*dabs(f(ixo^s)),signf(ixo^s)*f(ixb^s)),&
1454 min(dabs(f(ixo^s)),two*signf(ixo^s)*f(ixb^s)))
1455 case(4)
1456 ! 'koren' Barry Koren Right variant
1457 lf(ixo^s)=signf(ixo^s)* &
1458 max(zero,min(two*dabs(f(ixo^s)),two*signf(ixo^s)*f(ixb^s),&
1459 (two*f(ixb^s)*signf(ixo^s)+dabs(f(ixo^s)))*third))
1460 case(5)
1461 ! van Leer limiter
1462 lf(ixo^s)=two*max(f(ixb^s)*f(ixo^s),zero)/(f(ixo^s)+f(ixb^s)+qsmall)
1463 case default
1464 call mpistop("Unknown slope limiter for thermal conduction")
1465 end select
1466 end function slope_limiter
1467
1468 !> Get the explicit timestep for the TC (hd implementation)
1469 !> Note: also used in 1D MHD (or for neutrals in twofl)
1470 function get_tc_dt_hd(w,ixI^L,ixO^L,dx^D,x,fl) result(dtnew)
1471 ! Check diffusion time limit dt < dx_i**2 / ((gamma-1)*tc_k_para/(rho R))
1473
1474 integer, intent(in) :: ixi^l, ixo^l
1475 double precision, intent(in) :: dx^d, x(ixi^s,1:ndim)
1476 double precision, intent(in) :: w(ixi^s,1:nw)
1477 type(tc_fluid), intent(in) :: fl
1478 double precision :: dtnew
1479
1480 double precision :: tmp(ixo^s),tmp2(ixo^s),te(ixi^s),rho(ixi^s),hfs(ixo^s),gradt(ixi^s)
1481 double precision :: ne(ixi^s), nh_arr(ixi^s), rf(ixi^s)
1482 double precision :: dtdiff_tcond,maxtmp2
1483 integer :: idim
1484
1485 ! Constant conductivity does not use Te; retain ghost temperatures when
1486 ! needed by the temperature-dependent conductivity and saturation gradient.
1487 if(.not.fl%tc_constant) then
1488 call fl%get_temperature_from_conserved(w,x,ixi^l,ixi^l,te)
1489 end if
1490 call fl%get_rho(w,x,ixi^l,ixo^l,rho)
1491 ! Electron density is used only by the non-constant saturation correction.
1492 if(.not.fl%tc_constant .and. fl%tc_saturate) then
1493 call fl%get_ne_nH(ixi^l, ixo^l, w, x, ne, nh_arr)
1494 end if
1495 ! the diffusivity is kappa/(rho c_v) with c_v = R/(gamma-1): weight rho by R
1496 if(associated(fl%get_var_Rfactor)) then
1497 call fl%get_var_Rfactor(w, x, ixi^l, ixo^l, rf)
1498 rho(ixo^s)=rho(ixo^s)*rf(ixo^s)
1499 end if
1500
1501 if(fl%tc_constant) then
1502 tmp(ixo^s)=fl%tc_k_para/rho(ixo^s)
1503 else
1504 if(fl%tc_saturate) then
1505 ! Kannan 2016 MN 458, 410
1506 ! l_mfpe = 3^1.5*kB^2/(4*sqrt(pi)*e^4*lnLambda) * T^2/n_e
1507 if(si_unit) then
1508 ! 5.730205638843984e27 = 3^1.5*kB_SI^2/(4*sqrt(pi)*e_SI^4*37)
1509 tmp2(ixo^s)=te(ixo^s)**2/ne(ixo^s)*5.730205638843984d27*unit_temperature**2/(unit_numberdensity*unit_length)
1510 else
1511 ! 7093.9239487765044 = 3^1.5*kB_cgs^2/(4*sqrt(pi)*e_cgs^4*37)
1512 tmp2(ixo^s)=te(ixo^s)**2/ne(ixo^s)*7093.9239487765044d0*unit_temperature**2/(unit_numberdensity*unit_length)
1513 end if
1514 hfs=0.d0
1515 do idim=1,ndim
1516 call gradient(te,ixi^l,ixo^l,idim,gradt)
1517 hfs(ixo^s)=hfs(ixo^s)+gradt(ixo^s)**2
1518 end do
1519 ! kappa=kappa_Spitzer/(1+4.2*l_mfpe/(T/|gradT|))
1520 tmp(ixo^s)=fl%tc_k_para*te(ixo^s)*te(ixo^s)*dsqrt(te(ixo^s))/(rho(ixo^s)*(1.d0+4.2d0*tmp2(ixo^s)*dsqrt(hfs(ixo^s))/te(ixo^s)))
1521 else
1522 tmp(ixo^s)=fl%tc_k_para*te(ixo^s)*te(ixo^s)*dsqrt(te(ixo^s))/rho(ixo^s)
1523 end if
1524 end if
1525
1526 dtnew = bigdouble
1527 do idim=1,ndim
1528 ! approximate thermal conduction flux: tc_k_para/rho/dx**2
1529 maxtmp2=maxval(tmp(ixo^s)/(block%ds(ixo^s,idim)**2))
1530 ! dt< dx_idim**2/((gamma-1)*tc_k_para/rho)
1531 dtdiff_tcond=1.d0/(tc_gamma_1*maxtmp2+smalldouble)
1532 ! limit the time step
1533 dtnew=min(dtnew,dtdiff_tcond)
1534 end do
1535 dtnew=dtnew/dble(ndim)
1536 end function get_tc_dt_hd
1537
1538 !> rho in the saturated flux: under LTE scaled by the local over the fully ionised ne/nH
1539 subroutine tc_sat_rho(ixI^L,w,x,fl,rho,rhos)
1541 integer, intent(in) :: ixI^L
1542 double precision, intent(in) :: w(ixI^S,1:nw),x(ixI^S,1:ndim),rho(ixI^S)
1543 type(tc_fluid), intent(in) :: fl
1544 double precision, intent(out) :: rhos(ixI^S)
1545 double precision :: ne(ixI^S),nH(ixI^S)
1546
1547 if(fl%eos_lte) then
1548 call fl%get_ne_nH(ixi^l,ixi^l,w,x,ne,nh)
1549 rhos(ixi^s)=rho(ixi^s)*ne(ixi^s)/(nh(ixi^s)*fl%neOnH_FI)
1550 else
1551 rhos(ixi^s)=rho(ixi^s)
1552 end if
1553 end subroutine tc_sat_rho
1554
1555 !> Te over the block (+ghosts): the frozen-ionisation map within a conduction step with
1556 !> frozen_ion, else the full EoS with the optional negative-eint repair
1557 subroutine tc_get_te(ixI^L,w,x,fl,igrid,Te)
1560 integer, intent(in) :: ixI^L, igrid
1561 double precision, intent(inout) :: w(ixI^S,1:nw)
1562 double precision, intent(in) :: x(ixI^S,1:ndim)
1563 type(tc_fluid), intent(in) :: fl
1564 double precision, intent(out) :: Te(ixI^S)
1565
1566 if(fl%frozen_ion) then
1567 ! The anchors are allocated once, by the first thread to arrive
1568 !$OMP CRITICAL (tc_anchor_alloc)
1569 if(.not. allocated(tc_anchor)) allocate(tc_anchor(max_blocks))
1570 !$OMP END CRITICAL (tc_anchor_alloc)
1571 ! the first stage of each conduction step anchors on the full EoS
1572 if(tc_anchor(igrid)%sts_id /= sts_app_id) then
1573 tc_anchor(igrid)%sts_id = sts_app_id
1574 call tc_frozen_anchor(ixi^l, w, x, fl, igrid)
1575 end if
1576 associate(a => tc_anchor(igrid))
1577 te(ixi^s) = a%T0(ixi^s) + (w(ixi^s,fl%e_) - a%e0(ixi^s))*a%cvinv(ixi^s)
1578 end associate
1579 if(all(te(ixi^s) > 0.d0)) return
1580 ! a non-positive frozen T: this block takes the full EoS for this stage
1581 end if
1582
1583 call fl%get_temperature_from_eint(w, x, ixi^l, ixi^l, te)
1584 if(fl%tc_patch_eint) call tc_patch_negative_eint(w, x, ixi^l, ixi^l, te, fl%e_, fl)
1585 end subroutine tc_get_te
1586
1587 !> Anchor of the frozen-ionisation conduction step for block igrid, on the step-start
1588 !> state (internal energy in w(:,e_)): T from the full EoS, the energy, and the inverse
1589 !> frozen-composition heat capacity with R from the cached ionisation. Within the step
1590 !> T = T0 + (e - e0)/(R rho/(gamma-1)), exact where the gas is fully ionised or neutral
1591 subroutine tc_frozen_anchor(ixI^L, w, x, fl, igrid)
1593 integer, intent(in) :: ixI^L, igrid
1594 double precision, intent(in) :: w(ixI^S,1:nw), x(ixI^S,1:ndim)
1595 type(tc_fluid), intent(in) :: fl
1596 double precision :: rho(ixI^S), R0(ixI^S)
1597
1598 associate(a => tc_anchor(igrid))
1599 if(.not. allocated(a%T0)) allocate(a%T0(ixi^s), a%e0(ixi^s), a%cvinv(ixi^s))
1600 call fl%get_temperature_from_eint(w, x, ixi^l, ixi^l, a%T0)
1601 call fl%get_var_Rfactor(w, x, ixi^l, ixi^l, r0)
1602 call fl%get_rho(w, x, ixi^l, ixi^l, rho)
1603 a%e0(ixi^s) = w(ixi^s,fl%e_)
1604 a%cvinv(ixi^s) = 1.d0/(fl%inv_gamma_minus_1*r0(ixi^s)*rho(ixi^s))
1605 end associate
1606 end subroutine tc_frozen_anchor
1607
1608 !> Face fluxes for the HD STS source, uniform or general geometry
1609 subroutine tc_flux_hd(ixI^L,ixO^L,w,x,fl,qvec,rho,Te)
1611 integer, intent(in) :: ixI^L, ixO^L
1612 double precision, intent(in) :: x(ixI^S,1:ndim), w(ixI^S,1:nw)
1613 type(tc_fluid), intent(in) :: fl
1614 double precision, intent(in) :: rho(ixI^S), Te(ixI^S)
1615 double precision, intent(out) :: qvec(ixI^S,1:ndim)
1616
1617 if(slab_uniform) then
1618 call set_source_tc_hd(ixi^l,ixo^l,w,x,fl,qvec,rho,te)
1619 else
1620 call set_source_tc_hd_geo(ixi^l,ixo^l,w,x,fl,qvec,rho,te)
1621 end if
1622 end subroutine tc_flux_hd
1623
1624 !> Face fluxes for the MHD STS source, uniform or general geometry
1625 subroutine tc_flux_mhd(ixI^L,ixO^L,w,x,fl,qvec,rho,Te,alpha)
1627 integer, intent(in) :: ixI^L, ixO^L
1628 double precision, intent(in) :: x(ixI^S,1:ndim), w(ixI^S,1:nw)
1629 type(tc_fluid), intent(in) :: fl
1630 double precision, intent(in) :: rho(ixI^S), Te(ixI^S), alpha
1631 double precision, intent(out) :: qvec(ixI^S,1:ndim)
1632
1633 if(slab_uniform) then
1634 call set_source_tc_mhd(ixi^l,ixo^l,w,x,fl,qvec,rho,te,alpha)
1635 else
1636 call set_source_tc_mhd_geo(ixi^l,ixo^l,w,x,fl,qvec,rho,te,alpha)
1637 end if
1638 end subroutine tc_flux_mhd
1639
1640 subroutine sts_set_source_tc_hd(ixI^L,ixO^L,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux,fl)
1643
1644 integer, intent(in) :: ixi^l, ixo^l, igrid, nflux
1645 double precision, intent(in) :: x(ixi^s,1:ndim)
1646 ! intent(inout) so tc_patch_negative_eint can repair w(:, ie) in place
1647 ! when tc_patch_eint = .true.; legacy path (patch off) leaves w untouched.
1648 double precision, intent(inout) :: w(ixi^s,1:nw)
1649 double precision, intent(inout) :: wres(ixi^s,1:nw)
1650 double precision, intent(in) :: my_dt
1651 logical, intent(in) :: fix_conserve_at_step
1652 type(tc_fluid), intent(in) :: fl
1653
1654 double precision :: te(ixi^s),rho(ixi^s)
1655 double precision :: qvec(ixi^s,1:ndim),qd(ixi^s)
1656 double precision, allocatable, dimension(:^D&,:) :: qvec_equi
1657 double precision :: fluxall(ixi^s,1,1:ndim)
1658
1659 double precision :: dxinv(ndim)
1660 integer :: idims,ix^l,ixb^l,ixa^l
1661
1662 ix^l=ixo^l^ladd1;
1663
1664 dxinv=1.d0/dxlevel
1665
1666 call tc_get_te(ixi^l,w,x,fl,igrid,te)
1667 call fl%get_rho(w, x, ixi^l, ixi^l, rho)
1668 call tc_flux_hd(ixi^l,ixo^l,w,x,fl,qvec,rho,te)
1669 if(fl%subtract_equi) then
1670 allocate(qvec_equi(ixi^s,1:ndim))
1671 call fl%get_temperature_equi(w, x, ixi^l, ixi^l, te)
1672 call fl%get_rho_equi(w, x, ixi^l, ixi^l, rho)
1673 call tc_flux_hd(ixi^l,ixo^l,w,x,fl,qvec_equi,rho,te)
1674 do idims=1,ndim
1675 ! operate only on the per-idims face range that set_source_tc_hd fills
1676 ! (ixOmin-kr .. ixOmax); ix^S (=ixO+1) over-reaches into the
1677 ! uninitialised ixOmax+1 layer -> snan/Inf -> SIGFPE. Matches tc_mhd.
1678 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
1679 qvec(ixa^s,idims)=qvec(ixa^s,idims) - qvec_equi(ixa^s,idims)
1680 end do
1681 deallocate(qvec_equi)
1682 endif
1683
1684 qd=0.d0
1685 if(slab_uniform) then
1686 do idims=1,ndim
1687 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
1688 qvec(ixa^s,idims)=dxinv(idims)*qvec(ixa^s,idims)
1689 ixb^l=ixo^l-kr(idims,^d);
1690 qd(ixo^s)=qd(ixo^s)+qvec(ixo^s,idims)-qvec(ixb^s,idims)
1691 end do
1692 else
1693 do idims=1,ndim
1694 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
1695 qvec(ixa^s,idims)=qvec(ixa^s,idims)*block%surfaceC(ixa^s,idims)
1696 ixb^l=ixo^l-kr(idims,^d);
1697 qd(ixo^s)=qd(ixo^s)+qvec(ixo^s,idims)-qvec(ixb^s,idims)
1698 end do
1699 qd(ixo^s)=qd(ixo^s)/block%dvolume(ixo^s)
1700 end if
1701
1702 if(fix_conserve_at_step) then
1703 do idims=1,ndim
1704 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
1705 fluxall(ixa^s,1,idims)=my_dt*qvec(ixa^s,idims)
1706 end do
1707 call store_flux(igrid,fluxall,1,ndim,nflux)
1708 end if
1709
1710 wres(ixo^s,fl%e_)=qd(ixo^s)
1711 end subroutine sts_set_source_tc_hd
1712
1713 subroutine set_source_tc_hd(ixI^L,ixO^L,w,x,fl,qvec,rho,Te)
1715 integer, intent(in) :: ixI^L, ixO^L
1716 double precision, intent(in) :: x(ixI^S,1:ndim)
1717 double precision, intent(in) :: w(ixI^S,1:nw)
1718 type(tc_fluid), intent(in) :: fl
1719 double precision, intent(in) :: Te(ixI^S),rho(ixI^S)
1720 double precision, intent(out) :: qvec(ixI^S,1:ndim)
1721 double precision :: rhos(ixI^S)
1722 double precision :: gradT(ixI^S,1:ndim),ke(ixI^S),qd(ixI^S)
1723 integer :: idims,ix^D,ix^L,ixC^L,ixA^L,ixB^L
1724
1725 ix^l=ixo^l^ladd1;
1726 if(fl%tc_saturate) call tc_sat_rho(ixi^l,w,x,fl,rho,rhos)
1727 ! ixC is cell-corner index
1728 ixcmax^d=ixomax^d; ixcmin^d=ixomin^d-1;
1729
1730 ! calculate thermal conduction flux with symmetric scheme
1731 ! T gradient (central difference) at cell corners
1732 do idims=1,ndim
1733 ixbmin^d=ixmin^d;
1734 ixbmax^d=ixmax^d-kr(idims,^d);
1735 call gradientf(te,x,ixi^l,ixb^l,idims,ke)
1736 {^ifthreed
1737 if(idims==1) then
1738 {do ix^db=ixcmin^db,ixcmax^db\}
1739 qvec(ix^d,idims)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1,ix2+1,ix3)&
1740 +ke(ix1,ix2,ix3+1)+ke(ix1,ix2+1,ix3+1))
1741 {end do\}
1742 else if(idims==2) then
1743 {do ix^db=ixcmin^db,ixcmax^db\}
1744 qvec(ix^d,idims)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1+1,ix2,ix3)&
1745 +ke(ix1,ix2,ix3+1)+ke(ix1+1,ix2,ix3+1))
1746 {end do\}
1747 else
1748 {do ix^db=ixcmin^db,ixcmax^db\}
1749 qvec(ix^d,idims)=0.25d0*(ke(ix1,ix2,ix3)+ke(ix1+1,ix2,ix3)&
1750 +ke(ix1,ix2+1,ix3)+ke(ix1+1,ix2+1,ix3))
1751 {end do\}
1752 end if
1753 }
1754 {^iftwod
1755 if(idims==1) then
1756 {do ix^db=ixcmin^db,ixcmax^db\}
1757 qvec(ix^d,idims)=0.5d0*(ke(ix1,ix2)+ke(ix1,ix2+1))
1758 {end do\}
1759 else
1760 {do ix^db=ixcmin^db,ixcmax^db\}
1761 qvec(ix^d,idims)=0.5d0*(ke(ix1,ix2)+ke(ix1+1,ix2))
1762 {end do\}
1763 end if
1764 }
1765 {^ifoned
1766 do ix1=ixcmin1,ixcmax1
1767 qvec(ix1,idims)=ke(ix1)
1768 end do
1769 }
1770 end do
1771 ! conductivity at cell center
1772 if(fl%tc_constant) then
1773 qd(ix^s)=fl%tc_k_para
1774 else
1775 if(phys_trac) then
1776 {do ix^db=ixmin^db,ixmax^db\}
1777 if(te(ix^d) < block%wextra(ix^d,fl%Tcoff_)) then
1778 qd(ix^d)=fl%tc_k_para*dsqrt(block%wextra(ix^d,fl%Tcoff_)**5)
1779 else
1780 qd(ix^d)=fl%tc_k_para*dsqrt(te(ix^d)**5)
1781 end if
1782 {end do\}
1783 else
1784 qd(ix^s)=fl%tc_k_para*dsqrt(te(ix^s)**5)
1785 end if
1786 end if
1787 ! conductivity Ke at cell corner
1788 ! cell corner conduction flux gradT
1789 {^ifthreed
1790 {do ix^db=ixcmin^db,ixcmax^db\}
1791 ke(ix^d)=0.125d0*(qd(ix1,ix2,ix3)+qd(ix1+1,ix2,ix3)&
1792 +qd(ix1,ix2+1,ix3)+qd(ix1+1,ix2+1,ix3)&
1793 +qd(ix1,ix2,ix3+1)+qd(ix1+1,ix2,ix3+1)&
1794 +qd(ix1,ix2+1,ix3+1)+qd(ix1+1,ix2+1,ix3+1))
1795 gradt(ix^d,1)=ke(ix^d)*qvec(ix^d,1)
1796 gradt(ix^d,2)=ke(ix^d)*qvec(ix^d,2)
1797 gradt(ix^d,3)=ke(ix^d)*qvec(ix^d,3)
1798 {end do\}
1799 }
1800 {^iftwod
1801 {do ix^db=ixcmin^db,ixcmax^db\}
1802 ke(ix^d)=0.25d0*(qd(ix1,ix2)+qd(ix1+1,ix2)+qd(ix1,ix2+1)+qd(ix1+1,ix2+1))
1803 gradt(ix^d,1)=ke(ix^d)*qvec(ix^d,1)
1804 gradt(ix^d,2)=ke(ix^d)*qvec(ix^d,2)
1805 {end do\}
1806 }
1807 {^ifoned
1808 do ix1=ixcmin1,ixcmax1
1809 gradt(ix^d,1)=0.5d0*(qd(ix1)+qd(ix1+1))*qvec(ix^d,1)
1810 end do
1811 }
1812
1813 ! conduction flux qvec at cell face
1814 do idims=1,ndim
1815 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
1816 {^ifthreed
1817 if(idims==1) then
1818 {do ix^db=ixamin^db,ixamax^db\}
1819 qvec(ix^d,idims)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1,ix2-1,ix3,idims)&
1820 +gradt(ix1,ix2,ix3-1,idims)+gradt(ix1,ix2-1,ix3-1,idims))
1821 {end do\}
1822 else if(idims==2) then
1823 {do ix^db=ixamin^db,ixamax^db\}
1824 qvec(ix^d,idims)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1-1,ix2,ix3,idims)&
1825 +gradt(ix1,ix2,ix3-1,idims)+gradt(ix1-1,ix2,ix3-1,idims))
1826 {end do\}
1827 else
1828 {do ix^db=ixamin^db,ixamax^db\}
1829 qvec(ix^d,idims)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1,ix2-1,ix3,idims)&
1830 +gradt(ix1-1,ix2,ix3,idims)+gradt(ix1-1,ix2-1,ix3,idims))
1831 {end do\}
1832 end if
1833 }
1834 {^iftwod
1835 if(idims==1) then
1836 {do ix^db=ixamin^db,ixamax^db\}
1837 qvec(ix^d,idims)=0.5d0*(gradt(ix1,ix2,idims)+gradt(ix1,ix2-1,idims))
1838 {end do\}
1839 else
1840 {do ix^db=ixamin^db,ixamax^db\}
1841 qvec(ix^d,idims)=0.5d0*(gradt(ix1,ix2,idims)+gradt(ix1-1,ix2,idims))
1842 {end do\}
1843 end if
1844 }
1845 {^ifoned
1846 do ix1=ixamin1,ixamax1
1847 qvec(ix1,idims)=gradt(ix1,idims)
1848 end do
1849 }
1850 if(fl%tc_saturate) then
1851 ! saturated flux 5 phi rho c**3 (Cowie and McKee 1977 eq 8), phi and the LTE c**3 factor in sat_fac
1852 ! (tc_set_saturation); under LTE rho is scaled by the local over the fully ionised ne/nH
1853 ixb^l=ixa^l+kr(idims,^d);
1854 qd(ixa^s)=fl%sat_fac*(rhos(ixa^s)+rhos(ixb^s))*dsqrt(0.5d0*(te(ixa^s)+te(ixb^s)))**3
1855 {do ix^db=ixamin^db,ixamax^db\}
1856 if(dabs(qvec(ix^d,idims))>qd(ix^d)) then
1857 qvec(ix^d,idims)=sign(1.d0,qvec(ix^d,idims))*qd(ix^d)
1858 end if
1859 {end do\}
1860 end if
1861 end do
1862 end subroutine set_source_tc_hd
1863 subroutine set_source_tc_hd_geo(ixI^L,ixO^L,w,x,fl,qvec,rho,Te)
1865 integer, intent(in) :: ixI^L, ixO^L
1866 double precision, intent(in) :: x(ixI^S,1:ndim)
1867 double precision, intent(in) :: w(ixI^S,1:nw)
1868 type(tc_fluid), intent(in) :: fl
1869 double precision, intent(in) :: Te(ixI^S),rho(ixI^S)
1870 double precision, intent(out) :: qvec(ixI^S,1:ndim)
1871 double precision :: rhos(ixI^S)
1872 double precision :: gradT(ixI^S,1:ndim),ke(ixI^S),qd(ixI^S)
1873 integer :: idims,ix^D,ix^L,ixC^L,ixA^L,ixB^L
1874
1875 ix^l=ixo^l^ladd1;
1876 if(fl%tc_saturate) call tc_sat_rho(ixi^l,w,x,fl,rho,rhos)
1877 ! ixC is cell-corner index
1878 ixcmax^d=ixomax^d; ixcmin^d=ixomin^d-1;
1879
1880 ! calculate thermal conduction flux with symmetric scheme
1881 ! T gradient from face centers to cell corners: surface weighted average
1882 do idims=1,ndim
1883 ixbmin^d=ixmin^d;
1884 ixbmax^d=ixmax^d-kr(idims,^d);
1885 call gradientf(te,x,ixi^l,ixb^l,idims,ke)
1886 {^ifthreed
1887 if(idims==1) then
1888 {do ix^db=ixcmin^db,ixcmax^db\}
1889 qvec(ix^d,1)=(ke(ix1,ix2,ix3)*block%surfaceC(ix1,ix2,ix3,1)&
1890 +ke(ix1,ix2+1,ix3)*block%surfaceC(ix1,ix2+1,ix3,1)&
1891 +ke(ix1,ix2,ix3+1)*block%surfaceC(ix1,ix2,ix3+1,1)&
1892 +ke(ix1,ix2+1,ix3+1)*block%surfaceC(ix1,ix2+1,ix3+1,1))/&
1893 (block%surfaceC(ix1,ix2,ix3,1)+block%surfaceC(ix1,ix2+1,ix3,1)&
1894 +block%surfaceC(ix1,ix2,ix3+1,1)+block%surfaceC(ix1,ix2+1,ix3+1,1)+smalldouble**2)
1895 {end do\}
1896 else if(idims==2) then
1897 {do ix^db=ixcmin^db,ixcmax^db\}
1898 qvec(ix^d,2)=(ke(ix1,ix2,ix3)*block%surfaceC(ix1,ix2,ix3,2)&
1899 +ke(ix1+1,ix2,ix3)*block%surfaceC(ix1+1,ix2,ix3,2)&
1900 +ke(ix1,ix2,ix3+1)*block%surfaceC(ix1,ix2,ix3+1,2)&
1901 +ke(ix1+1,ix2,ix3+1)*block%surfaceC(ix1+1,ix2,ix3+1,2))/&
1902 (block%surfaceC(ix1,ix2,ix3,2)+block%surfaceC(ix1+1,ix2,ix3,2)&
1903 +block%surfaceC(ix1,ix2,ix3+1,2)+block%surfaceC(ix1+1,ix2,ix3+1,2)+smalldouble**2)
1904 ! zero theta-normal surface area at pole axis
1905 {end do\}
1906 else
1907 {do ix^db=ixcmin^db,ixcmax^db\}
1908 qvec(ix^d,3)=(ke(ix1,ix2,ix3)*block%surfaceC(ix1,ix2,ix3,3)&
1909 +ke(ix1+1,ix2,ix3)*block%surfaceC(ix1+1,ix2,ix3,3)&
1910 +ke(ix1,ix2+1,ix3)*block%surfaceC(ix1,ix2+1,ix3,3)&
1911 +ke(ix1+1,ix2+1,ix3)*block%surfaceC(ix1+1,ix2+1,ix3,3))/&
1912 (block%surfaceC(ix1,ix2,ix3,3)+block%surfaceC(ix1+1,ix2,ix3,3)&
1913 +block%surfaceC(ix1,ix2+1,ix3,3)+block%surfaceC(ix1+1,ix2+1,ix3,3))
1914 {end do\}
1915 end if
1916 }
1917 {^iftwod
1918 if(idims==1) then
1919 {do ix^db=ixcmin^db,ixcmax^db\}
1920 qvec(ix^d,1)=(ke(ix1,ix2)*block%surfaceC(ix1,ix2,1)+ke(ix1,ix2+1)*block%surfaceC(ix1,ix2+1,1))&
1921 /(block%surfaceC(ix1,ix2,1)+block%surfaceC(ix1,ix2+1,1))
1922 {end do\}
1923 else
1924 {do ix^db=ixcmin^db,ixcmax^db\}
1925 qvec(ix^d,2)=(ke(ix1,ix2)*block%surfaceC(ix1,ix2,2)+ke(ix1+1,ix2)*block%surfaceC(ix1+1,ix2,2))&
1926 /(block%surfaceC(ix1,ix2,2)+block%surfaceC(ix1+1,ix2,2))
1927 {end do\}
1928 end if
1929 }
1930 {^ifoned
1931 do ix1=ixcmin1,ixcmax1
1932 qvec(ix1,idims)=ke(ix1)
1933 end do
1934 }
1935 end do
1936 ! conductivity at cell center
1937 if(fl%tc_constant) then
1938 qd(ix^s)=fl%tc_k_para
1939 else
1940 if(phys_trac) then
1941 {do ix^db=ixmin^db,ixmax^db\}
1942 if(te(ix^d) < block%wextra(ix^d,fl%Tcoff_)) then
1943 qd(ix^d)=fl%tc_k_para*dsqrt(block%wextra(ix^d,fl%Tcoff_)**5)
1944 else
1945 qd(ix^d)=fl%tc_k_para*dsqrt(te(ix^d)**5)
1946 end if
1947 {end do\}
1948 else
1949 qd(ix^s)=fl%tc_k_para*dsqrt(te(ix^s)**5)
1950 end if
1951 end if
1952 ! conductivity Ke at cell corner
1953 ! cell corner conduction flux gradT
1954 {^ifthreed
1955 {do ix^db=ixcmin^db,ixcmax^db\}
1956 ke(ix^d)=(qd(ix1,ix2,ix3)*block%dvolume(ix1,ix2,ix3)+qd(ix1+1,ix2,ix3)*block%dvolume(ix1+1,ix2,ix3)&
1957 +qd(ix1,ix2+1,ix3)*block%dvolume(ix1,ix2+1,ix3)+qd(ix1+1,ix2+1,ix3)*block%dvolume(ix1+1,ix2+1,ix3)&
1958 +qd(ix1,ix2,ix3+1)*block%dvolume(ix1,ix2,ix3+1)+qd(ix1+1,ix2,ix3+1)*block%dvolume(ix1+1,ix2,ix3+1)&
1959 +qd(ix1,ix2+1,ix3+1)*block%dvolume(ix1,ix2+1,ix3+1)+qd(ix1+1,ix2+1,ix3+1)*block%dvolume(ix1+1,ix2+1,ix3+1))&
1960 /(block%dvolume(ix1,ix2,ix3)+block%dvolume(ix1+1,ix2,ix3)+block%dvolume(ix1,ix2+1,ix3)+block%dvolume(ix1+1,ix2+1,ix3)&
1961 +block%dvolume(ix1,ix2,ix3+1)+block%dvolume(ix1+1,ix2,ix3+1)+block%dvolume(ix1,ix2+1,ix3+1)+block%dvolume(ix1+1,ix2+1,ix3+1))
1962 gradt(ix^d,1)=ke(ix^d)*qvec(ix^d,1)
1963 gradt(ix^d,2)=ke(ix^d)*qvec(ix^d,2)
1964 gradt(ix^d,3)=ke(ix^d)*qvec(ix^d,3)
1965 {end do\}
1966 }
1967 {^iftwod
1968 {do ix^db=ixcmin^db,ixcmax^db\}
1969 ke(ix^d)=(qd(ix1,ix2)*block%dvolume(ix1,ix2)+qd(ix1+1,ix2)*block%dvolume(ix1+1,ix2)&
1970 +qd(ix1,ix2+1)*block%dvolume(ix1,ix2+1)+qd(ix1+1,ix2+1)*block%dvolume(ix1+1,ix2+1))&
1971 /(block%dvolume(ix1,ix2)+block%dvolume(ix1+1,ix2)+block%dvolume(ix1,ix2+1)+block%dvolume(ix1+1,ix2+1))
1972 gradt(ix^d,1)=ke(ix^d)*qvec(ix^d,1)
1973 gradt(ix^d,2)=ke(ix^d)*qvec(ix^d,2)
1974 {end do\}
1975 }
1976 {^ifoned
1977 do ix1=ixcmin1,ixcmax1
1978 gradt(ix^d,1)=(qd(ix1)*block%dvolume(ix1)+qd(ix1+1)*block%dvolume(ix1+1))/(block%dvolume(ix1)+block%dvolume(ix1+1))*qvec(ix^d,1)
1979 end do
1980 }
1981
1982 ! conduction flux qvec at cell face
1983 do idims=1,ndim
1984 ixamax^d=ixomax^d; ixamin^d=ixomin^d-kr(idims,^d);
1985 {^ifthreed
1986 if(idims==1) then
1987 {do ix^db=ixamin^db,ixamax^db\}
1988 qvec(ix^d,idims)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1,ix2-1,ix3,idims)&
1989 +gradt(ix1,ix2,ix3-1,idims)+gradt(ix1,ix2-1,ix3-1,idims))
1990 {end do\}
1991 else if(idims==2) then
1992 {do ix^db=ixamin^db,ixamax^db\}
1993 qvec(ix^d,idims)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1-1,ix2,ix3,idims)&
1994 +gradt(ix1,ix2,ix3-1,idims)+gradt(ix1-1,ix2,ix3-1,idims))
1995 {end do\}
1996 else
1997 {do ix^db=ixamin^db,ixamax^db\}
1998 qvec(ix^d,idims)=0.25d0*(gradt(ix1,ix2,ix3,idims)+gradt(ix1,ix2-1,ix3,idims)&
1999 +gradt(ix1-1,ix2,ix3,idims)+gradt(ix1-1,ix2-1,ix3,idims))
2000 {end do\}
2001 end if
2002 }
2003 {^iftwod
2004 if(idims==1) then
2005 {do ix^db=ixamin^db,ixamax^db\}
2006 qvec(ix^d,idims)=0.5d0*(gradt(ix1,ix2,idims)+gradt(ix1,ix2-1,idims))
2007 {end do\}
2008 else
2009 {do ix^db=ixamin^db,ixamax^db\}
2010 qvec(ix^d,idims)=0.5d0*(gradt(ix1,ix2,idims)+gradt(ix1-1,ix2,idims))
2011 {end do\}
2012 end if
2013 }
2014 {^ifoned
2015 do ix1=ixamin1,ixamax1
2016 qvec(ix1,idims)=gradt(ix1,idims)
2017 end do
2018 }
2019 if(fl%tc_saturate) then
2020 ! saturated flux 5 phi rho c**3 (Cowie and McKee 1977 eq 8), phi and the LTE c**3 factor in sat_fac
2021 ! (tc_set_saturation); under LTE rho is scaled by the local over the fully ionised ne/nH
2022 ixb^l=ixa^l+kr(idims,^d);
2023 qd(ixa^s)=fl%sat_fac*(rhos(ixa^s)+rhos(ixb^s))*dsqrt(0.5d0*(te(ixa^s)+te(ixb^s)))**3
2024 {do ix^db=ixamin^db,ixamax^db\}
2025 if(dabs(qvec(ix^d,idims))>qd(ix^d)) then
2026 qvec(ix^d,idims)=sign(1.d0,qvec(ix^d,idims))*qd(ix^d)
2027 end if
2028 {end do\}
2029 end if
2030 end do
2031 end subroutine set_source_tc_hd_geo
2032
2033 !> Patch cells where e_int <= 0 by neighbor-averaging the temperature
2034 !> AND repairing the conserved internal energy w(:, ie) in place.
2035 !> Called after get_temperature_from_eint in the TC source routines when
2036 !> tc_patch_eint is .true. During STS RKL2 Chebyshev substeps the
2037 !> polynomial can overshoot e_int to negative values at low-density
2038 !> coronal cells. A negative T would produce NaN via sqrt(T) in the
2039 !> Spitzer conductivity. This routine replaces those cells' T with
2040 !> the average of valid (e_int > 0) neighbors and writes the matching
2041 !> e_int back to w(ix, ie) via the EoS inverse helper eint_nH_from_T,
2042 !> so the next STS substep does not re-read negative w and amplify it.
2043 subroutine tc_patch_negative_eint(w, x, ixI^L, ixO^L, Te, ie, fl)
2045 use iso_fortran_env, only: int64
2047 integer, intent(in) :: ixi^l, ixo^l, ie
2048 double precision, intent(inout) :: w(ixi^s, 1:nw)
2049 double precision, intent(in) :: x(ixi^s, 1:ndim)
2050 double precision, intent(inout) :: te(ixi^s)
2051 type(tc_fluid), intent(in) :: fl
2052
2053 integer :: ix^d, count
2054 integer :: ipatch(ixi^s)
2055 double precision :: t_avg, t_use, rho_cell, nh_cell, log_nh, log_t
2056 double precision :: eint_new, small_e_local, log_t_min, t_floor
2057 double precision :: rho(ixi^s)
2058
2059 type(repair_warning_state), save :: warning_state
2060 integer(int64) :: repair_events, edge_events
2061 double precision :: first_x(ndim), first_before, first_after
2062
2063 repair_events=0_int64
2064 edge_events=0_int64
2065 ipatch(ixi^s) = 0
2066
2067 ! Mark cells with negative e_int
2068 {do ix^db=ixomin^db,ixomax^db\}
2069 if (w(ix^d, ie) <= 0.0d0) ipatch(ix^d) = 1
2070 {end do\}
2071
2072 ! Quick exit if no cells need patching
2073 if (.not. any(ipatch(ixo^s) /= 0)) return
2074
2075 ! Need rho per-cell to back out e_int from a prescribed T via the EoS.
2076 call fl%get_rho(w, x, ixi^l, ixi^l, rho)
2077
2078 ! Floor on log_T for the (rho, T) inverse table, from the EoS via the port
2079 ! (eos_get_log_T_floor encapsulates the method->container choice; picking the
2080 ! wrong container would leave var2_min=0 and clobber cold cells).
2081 log_t_min = fl%log_T_floor
2082
2083 ! Local minimum e_int (code units): equivalent to small_pressure / (gamma-1).
2084 small_e_local = small_pressure * fl%inv_gamma_minus_1
2085
2086 ! Fallback T (code units) for cells that have no valid neighbour.
2087 t_floor = 1.0d0 / unit_temperature
2088
2089 ! Replace marked cells with neighbor-averaged T, then back out e_int
2090 ! and floor w(:, ie) so subsequent STS substeps see a positive state.
2091 {do ix^db=ixomin^db+1,ixomax^db-1\}
2092 if (ipatch(ix^d) == 1) then
2093 repair_events=repair_events+1_int64
2094 if(repair_events==1_int64) then
2095 first_x=x(ix^d,1:ndim)
2096 first_before=w(ix^d,ie)
2097 end if
2098 t_avg = 0.0d0
2099 count = 0
2100 {^ifoned
2101 if (ipatch(ix1-1)==0) then; t_avg=t_avg+te(ix1-1); count=count+1; end if
2102 if (ipatch(ix1+1)==0) then; t_avg=t_avg+te(ix1+1); count=count+1; end if
2103 }
2104 {^iftwod
2105 if (ipatch(ix1-1,ix2)==0) then; t_avg=t_avg+te(ix1-1,ix2); count=count+1; end if
2106 if (ipatch(ix1+1,ix2)==0) then; t_avg=t_avg+te(ix1+1,ix2); count=count+1; end if
2107 if (ipatch(ix1,ix2-1)==0) then; t_avg=t_avg+te(ix1,ix2-1); count=count+1; end if
2108 if (ipatch(ix1,ix2+1)==0) then; t_avg=t_avg+te(ix1,ix2+1); count=count+1; end if
2109 }
2110 {^ifthreed
2111 if (ipatch(ix1-1,ix2,ix3)==0) then; t_avg=t_avg+te(ix1-1,ix2,ix3); count=count+1; end if
2112 if (ipatch(ix1+1,ix2,ix3)==0) then; t_avg=t_avg+te(ix1+1,ix2,ix3); count=count+1; end if
2113 if (ipatch(ix1,ix2-1,ix3)==0) then; t_avg=t_avg+te(ix1,ix2-1,ix3); count=count+1; end if
2114 if (ipatch(ix1,ix2+1,ix3)==0) then; t_avg=t_avg+te(ix1,ix2+1,ix3); count=count+1; end if
2115 if (ipatch(ix1,ix2,ix3-1)==0) then; t_avg=t_avg+te(ix1,ix2,ix3-1); count=count+1; end if
2116 if (ipatch(ix1,ix2,ix3+1)==0) then; t_avg=t_avg+te(ix1,ix2,ix3+1); count=count+1; end if
2117 }
2118 if (count > 0) then
2119 t_use = t_avg / dble(count)
2120 else
2121 t_use = t_floor
2122 end if
2123
2124 ! Repair the conserved internal energy in place. Use the same
2125 ! EoS-aware inverse used by hd/mhd_from_prolong_LTE: e_int =
2126 ! nH * eint_nH_from_T(log_nH, log_T). Floor with small_e.
2127 rho_cell = max(rho(ix^d), small_density)
2128 nh_cell = rho_cell / fl%nH2rhoFactor
2129 log_nh = dlog10(max(nh_cell, smalldouble))
2130 log_t = dlog10(max(t_use, 10.0d0**log_t_min))
2131 eint_new = nh_cell * fl%eint_from_T(log_nh, log_t)
2132 w(ix^d, ie) = max(small_e_local, eint_new)
2133
2134 ! Keep Te consistent with the floored w(:, ie): if small_e
2135 ! kicked in, the implied T is slightly different from T_use.
2136 ! Use the prescribed T_use here (Te is only consumed within
2137 ! this TC substep; the next call recomputes it from w).
2138 te(ix^d) = t_use
2139
2140 if(repair_events==1_int64) first_after=w(ix^d,ie)
2141 ipatch(ix^d) = 2 ! mark as handled
2142 end if
2143 {end do\}
2144
2145 ! Cells at the block boundary that the interior loop above skipped
2146 ! (ixOmin/ixOmax edges). Fall back to T = 1/unit_temperature and
2147 ! repair w(:, ie) consistently so STS does not re-read negative e.
2148 {do ix^db=ixomin^db,ixomax^db\}
2149 if (ipatch(ix^d) == 1) then
2150 repair_events=repair_events+1_int64
2151 edge_events=edge_events+1_int64
2152 if(repair_events==1_int64) then
2153 first_x=x(ix^d,1:ndim)
2154 first_before=w(ix^d,ie)
2155 end if
2156 t_use = t_floor
2157 rho_cell = max(rho(ix^d), small_density)
2158 nh_cell = rho_cell / fl%nH2rhoFactor
2159 log_nh = dlog10(max(nh_cell, smalldouble))
2160 log_t = dlog10(max(t_use, 10.0d0**log_t_min))
2161 eint_new = nh_cell * fl%eint_from_T(log_nh, log_t)
2162 w(ix^d, ie) = max(small_e_local, eint_new)
2163 te(ix^d) = t_use
2164 if(repair_events==1_int64) first_after=w(ix^d,ie)
2165 end if
2166 {end do\}
2167
2168 call report_repair_warning(warning_state,'tc_patch_eint',repair_events, &
2169 edge_events,ie,first_x,first_before,first_after)
2170 end subroutine tc_patch_negative_eint
2171
2172end module mod_thermal_conduction
Scalar EoS inverse, e.g. eint_nH_from_T(log_nH, log_T).
subroutine, public mpistop(message)
Exit MPI-AMRVAC with an error message.
Module for physical and numeric constants.
double precision, parameter const_me
double precision, parameter const_mp
double precision, parameter dpi
Pi.
Module for flux conservation near refinement boundaries.
subroutine, public store_flux(igrid, fc, idimlim, nwfluxin)
Module with geometry-related routines (e.g., divergence, curl).
Definition mod_geometry.t:2
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.
double precision unit_density
Physical scaling factor for density.
integer, parameter unitpar
file handle for IO
integer, dimension(3, 3) kr
Kronecker delta tensor.
double precision unit_numberdensity
Physical scaling factor for number density.
integer, parameter ndim
Number of spatial dimensions for grid variables.
double precision unit_length
Physical scaling factor for length.
character(len=std_len), dimension(:), allocatable par_files
Which par files are used as input.
integer mype
The rank of the current MPI task.
integer ndir
Number of spatial dimensions (components) for vector variables.
double precision, dimension(:), allocatable, parameter d
double precision unit_velocity
Physical scaling factor for velocity.
logical b0field
split magnetic field as background B0 field
double precision unit_temperature
Physical scaling factor for temperature.
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
double precision, dimension(^nd) dxlevel
store unstretched cell size of current level
logical slab_uniform
uniform Cartesian geometry or not (stretched Cartesian)
integer max_blocks
The maximum number of grid blocks in a processor.
Module for handling problematic values in simulations, such as negative pressures.
subroutine, public report_repair_warning(state, label, events, edges, iw, xfirst, before, after)
Generic supertimestepping method which can be used for multiple source terms in the governing equatio...
integer, public sts_app_id
Counts STS applications (one per term per call of sts_add_source), so a term can tell the first stage...
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.
subroutine tc_flux_hd(ixil, ixol, w, x, fl, qvec, rho, te)
Face fluxes for the HD STS source, uniform or general geometry.
double precision function, public get_tc_dt_mhd(w, ixil, ixol, dxd, x, fl)
Get the explicit timestep for the TC (mhd implementation) Note: for multi-D MHD (1D MHD will use HD f...
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 tc_frozen_anchor(ixil, w, x, fl, igrid)
Anchor of the frozen-ionisation conduction step for block igrid, on the step-start state (internal en...
subroutine tc_sat_rho(ixil, w, x, fl, rho, rhos)
rho in the saturated flux: under LTE scaled by the local over the fully ionised ne/nH
double precision function tc_spitzer_k_para()
Spitzer conductivity coefficient parallel to the field, in code units.
subroutine, public tc_patch_negative_eint(w, x, ixil, ixol, te, ie, fl)
Patch cells where e_int <= 0 by neighbor-averaging the temperature AND repairing the conserved intern...
subroutine, public sts_set_source_tc_hd(ixil, ixol, w, x, wres, fix_conserve_at_step, my_dt, igrid, nflux, fl)
subroutine, public sts_set_source_tc_mhd(ixil, ixol, w, x, wres, fix_conserve_at_step, my_dt, igrid, nflux, fl)
anisotropic thermal conduction with slope limited symmetric scheme Sharma 2007 Journal of Computation...
subroutine, public tc_set_saturation(fl, report)
Saturated flux factor. q_sat = 5 phi rho c**3 (Cowie & McKee 1977 eq 8), c**2 = xt kT/m_p with xt the...
type(tc_anchor_t), dimension(:), allocatable tc_anchor
double precision tc_gamma_1
The adiabatic index.
subroutine, public tc_get_mhd_params(fl, read_mhd_params)
Init TC coefficients: MHD case.
subroutine set_source_tc_hd_geo(ixil, ixol, w, x, fl, qvec, rho, te)
double precision function, dimension(ixi^s) slope_limiter(f, ixil, ixol, idims, pm, tc_slope_limiter)
subroutine tc_flux_mhd(ixil, ixol, w, x, fl, qvec, rho, te, alpha)
Face fluxes for the MHD STS source, uniform or general geometry.
subroutine tc_get_te(ixil, w, x, fl, igrid, te)
Te over the block (+ghosts): the frozen-ionisation map within a conduction step with frozen_ion,...
subroutine set_source_tc_hd(ixil, ixol, w, x, fl, qvec, rho, te)
subroutine set_source_tc_mhd_geo(ixil, ixol, w, x, fl, qvec, rho, te, alpha)
subroutine set_source_tc_mhd(ixil, ixol, w, x, fl, qvec, rho, te, alpha)
Per-block anchor of the frozen-ionisation conduction step: T and the internal energy at the step star...