MPI-AMRVAC 3.2
The MPI - Adaptive Mesh Refinement - Versatile Advection Code (development version)
Loading...
Searching...
No Matches
mod_mhd_phys.t
Go to the documentation of this file.
1!> Magneto-hydrodynamics module
3
4#include "amrvac.h"
5
6 use mod_global_parameters, only: std_len, const_c
10 use mod_fld, only: fld_fluid
11 use mod_physics
12 use mod_eos
13 use mod_rt_sc_slab, only: rt_sc_activate, rt_sc_setup, rt_sc_add_source
14 use mod_comm_lib, only: mpistop
16
17 implicit none
18 private
19
20 !> The adiabatic constant
21 double precision, public :: mhd_adiab = 1.0d0
22 !> The MHD resistivity
23 double precision, public :: mhd_eta = 0.0d0
24 !> The MHD hyper-resistivity
25 double precision, public :: mhd_eta_hyper = 0.0d0
26 !> Hall resistivity
27 double precision, public :: mhd_etah = 0.0d0
28 !> The MHD ambipolar coefficient
29 double precision, public :: mhd_eta_ambi = 0.0d0
30 !> Height of the mask used in the TRAC method
31 double precision, public, protected :: mhd_trac_mask = 0.d0
32 !> GLM-MHD parameter: ratio of the diffusive and advective time scales for div b
33 !> taking values within [0, 1]
34 double precision, public :: mhd_glm_alpha = 0.5d0
35 !> Reduced speed of light for semirelativistic MHD: 2% of light speed
36 double precision, public, protected :: mhd_reduced_c = 0.02d0*const_c
37 !> The thermal conductivity kappa in hyperbolic thermal conduction
38 double precision, public :: mhd_hyperbolic_tc_kappa = 0.d0
39 !> Relative perpendicular hyperbolic-TC coefficient in fixed/strong-field limit:
40 !> kappa_perp0 = mhd_hyperbolic_tc_kappa_perp_factor * kappa_parallel
41 double precision, public, protected :: mhd_hyperbolic_tc_kappa_perp_factor = 0.d0
42 !> Field-strength transition scale for perpendicular closure
43 double precision, public, protected :: mhd_hyperbolic_tc_bmin = 0.d0
44 !> Constant Coulomb logarithm used by the simplified electron-magnetization
45 !> closure. It is a namelist parameter so changing it has no per-cell cost.
46 double precision, public, protected :: mhd_hyperbolic_tc_coulomb_log = 20.d0
47 !> Coefficient of diffusive divB cleaning
48 double precision, public :: divbdiff = 0.8d0
49 !> Ionization fraction of H
50 !> H_ion_fr = H+/(H+ + H)
51 double precision, public, protected :: h_ion_fr=1d0
52 !> Ionization fraction of He
53 !> He_ion_fr = (He2+ + He+)/(He2+ + He+ + He)
54 double precision, public, protected :: he_ion_fr=1d0
55 !> Ratio of number He2+ / number He+ + He2+
56 !> He_ion_fr2 = He2+/(He2+ + He+)
57 double precision, public, protected :: he_ion_fr2=1d0
58 ! used for eq of state when it is not defined by units,
59 ! the units do not contain terms related to ionization fraction
60 ! and it is p = RR * rho * T
61 double precision, public, protected :: rr=1d0
62 !> inverse of squared speed of light c0 and reduced speed of light c
63 double precision :: inv_squared_c0=0.d0, inv_squared_c=0.d0
64 !> squared reduced speed of light c
65 double precision, public, protected :: mhd_squared_c
66 !> Dimensionless multiplier in the one_dimensional_gradient source only
67 double precision, public, protected :: mhd_uawsom_sigma = 0.d0
68 !> Base transverse density contrast and filling factor
69 double precision, public, protected :: mhd_uawsom_zeta0 = 5.d0
70 double precision, public, protected :: mhd_uawsom_filling_factor = 0.1d0
71 !> Physical input scales (lengths and magnetic field); converted to code units at init
72 double precision, public, protected :: mhd_uawsom_zeta_scale = -1.d0
73 double precision, public, protected :: mhd_uawsom_thread_radius0 = -1.d0
74 double precision, public, protected :: mhd_uawsom_bref = -1.d0
75 double precision, public, protected :: mhd_uawsom_alfven_corr_length0 = -1.d0
76 !> equi vars indices in the state%equi_vars array
77 integer, public :: equi_rho0_ = -1
78 integer, public :: equi_pe0_ = -1
79 !> Number of tracer species
80 integer, public, protected :: mhd_n_tracer = 0
81 !> Index of the density (in the w array)
82 integer, public, protected :: rho_
83 !> Indices of the momentum density
84 integer, allocatable, public, protected :: mom(:)
85 !> Indices of the momentum density for the form of better vectorization
86 integer, public, protected :: ^c&m^C_
87 !> Index of the energy density (-1 if not present)
88 integer, public, protected :: e_
89 !> Indices of the magnetic field for the form of better vectorization
90 integer, public, protected :: ^c&b^C_
91 !> Index of the gas pressure (-1 if not present) should equal e_
92 integer, public, protected :: p_
93 !> Index of the electron number density for LTE module
94 integer, public, protected :: ne_
95 !> Index of the field-aligned heat flux q_parallel
96 integer, public, protected :: qpar_
97 !> Index of the perpendicular heat flux q_perp
98 integer, public, protected :: qperp_
99 !> Indices of the GLM psi
100 integer, public, protected :: psi_
101 !> Index of the radiation energy
102 integer, public, protected :: r_e
103 !> Indices of temperature
104 integer, public, protected :: te_
105 !> Index of the FIP passive scalar rho*fip in conserved form, fip in primitive form
106 integer, public, protected :: fip_ = -1
107 ! Parse the input mode once; source kernels use the private integer ID.
108 integer, parameter :: uawsom_reflection_1d=1, uawsom_reflection_cartesian=2
109 integer :: uawsom_reflection_id=uawsom_reflection_1d
110 !> Enable conservative kink-wave reflection from the kink-speed gradient
111 !> Cartesian direction used for the prescribed density contrast and reflection gradient
112 integer, public, protected :: mhd_uawsom_height_dim = -1
113 !> Conserved wave-energy indices. The plus variables propagate against B.
114 integer, public, protected :: waplus_=-1, waminus_=-1, wkplus_=-1, wkminus_=-1
115 !> Index of the cutoff temperature for the TRAC method
116 integer, public, protected :: tcoff_
117 integer, public, protected :: tweight_
118 !> Indices of the tracers
119 integer, allocatable, public, protected :: tracer(:)
120 !> The number of waves
121 integer :: nwwave=8
122 !> Method type of divb in a integer for good performance
123 integer :: type_divb
124 !> To skip * layer of ghost cells during divB=0 fix for boundary
125 integer, public, protected :: boundary_divbfix_skip(2*^nd)=0
126 ! DivB cleaning methods
127 integer, parameter :: divb_none = 0
128 integer, parameter :: divb_multigrid = -1
129 integer, parameter :: divb_glm = 1
130 integer, parameter :: divb_powel = 2
131 integer, parameter :: divb_janhunen = 3
132 integer, parameter :: divb_linde = 4
133 integer, parameter :: divb_lindejanhunen = 5
134 integer, parameter :: divb_lindepowel = 6
135 integer, parameter :: divb_lindeglm = 7
136 integer, parameter :: divb_ct = 8
137 !> Whether divB is computed with a fourth order approximation
138 integer, public, protected :: mhd_divb_nth = 1
139 ! Parsed once during initialization; runtime kernels do not inspect strings.
140 integer, parameter :: htc_perp_disabled=0, htc_perp_fixed=1, &
141 htc_perp_weak_field=2, htc_perp_electron=3
142 integer :: htc_perp_mode_id=htc_perp_disabled
143 !> Whether an energy equation is used
144 logical, public, protected :: mhd_energy = .true.
145 !> Whether thermal conduction is used
146 logical, public, protected :: mhd_thermal_conduction = .false.
147 !> Whether radiative cooling is added
148 logical, public, protected :: mhd_radiative_cooling = .false.
149 !> Whether the short-characteristics radiative transfer is used, with its settings in &rt_list
150 logical, public, protected :: mhd_radiative_transfer = .false.
151 !> Whether thermal conduction is used
152 logical, public, protected :: mhd_hyperbolic_tc = .false.
153 !> Whether saturation is considered for hyperbolic TC. When the perpendicular
154 !> channel is active, the limiter uses sqrt(q_parallel^2+q_perp^2).
155 logical, public, protected :: mhd_hyperbolic_tc_sat = .false.
156 !> Whether the perpendicular hyperbolic-TC channel is enabled
157 logical, public, protected :: mhd_hyperbolic_tc_use_perp = .false.
158 logical, public :: mhd_hyperbolic_tc_constant = .false.
159 !> Whether viscosity is added
160 logical, public, protected :: mhd_viscosity = .false.
161 !> Whether gravity is added
162 logical, public, protected :: mhd_gravity = .false.
163 !> Whether rotating frame is activated
164 logical, public, protected :: mhd_rotating_frame = .false.
165 !> Whether Hall-MHD is used
166 logical, public, protected :: mhd_hall = .false.
167 !> Whether Ambipolar term is used
168 logical, public, protected :: mhd_ambipolar = .false.
169 !> Whether Ambipolar term is implemented using supertimestepping
170 logical, public, protected :: mhd_ambipolar_sts = .false.
171 !> Whether Ambipolar term is implemented explicitly
172 logical, public, protected :: mhd_ambipolar_exp = .false.
173 !> Whether particles module is added
174 logical, public, protected :: mhd_particles = .false.
175 !> Whether magnetofriction is added
176 logical, public, protected :: mhd_magnetofriction = .false.
177 !> Whether GLM-MHD is used to control div B
178 logical, public, protected :: mhd_glm = .false.
179 !> Whether extended GLM-MHD is used with additional sources
180 logical, public, protected :: mhd_glm_extended = .true.
181 !> Whether TRAC method is used
182 logical, public, protected :: mhd_trac = .false.
183 !> Which TRAC method is used
184 integer, public, protected :: mhd_trac_type=1
185 !> Distance between two adjacent traced magnetic field lines (in finest cell size)
186 integer, public, protected :: mhd_trac_finegrid=4
187 !> TRAC-7 (Johnston 2021, A&A 654 A2): target number of cells resolving the TR
188 double precision, public, protected :: mhd_trac_delta=0.5d0
189 !> Whether internal energy is solved instead of total energy
190 logical, public, protected :: mhd_internal_e = .false.
191 !> Whether hydrodynamic energy is solved instead of total energy
192 logical, public, protected :: mhd_hydrodynamic_e = .false.
193 !> Whether divB cleaning sources are added splitting from fluid solver
194 logical, public, protected :: source_split_divb = .false.
195 !> Whether semirelativistic MHD equations (Gombosi 2002 JCP) are solved
196 logical, public, protected :: mhd_semirelativistic = .false.
197 !> Whether plasma is partially ionized
198 !> Whether CAK radiation line force is activated
199 logical, public, protected :: mhd_cak_force = .false.
200 !> Whether radiation-gas interaction is handled using flux limited diffusion
201 logical, public, protected :: mhd_radiation_fld = .false.
202 logical, public, protected :: mhd_fld_pradtensor= .true.
203 !> Radiation fluid object (gas-EoS callbacks for FLD), wired in mhd_link_eos
204 type(fld_fluid), allocatable, public :: fld_fl
205 !> whether split off equilibrium density and pressure
206 logical, public :: has_equi_rho_and_p = .false.
207 logical, public :: mhd_equi_thermal = .false.
208 !> whether dump full variables (when splitting is used) in a separate dat file
209 logical, public, protected :: mhd_dump_full_vars = .false.
210 !> Whether FIP passive scalar is enabled
211 logical, public, protected :: mhd_fip = .false.
212 !> Enable the Uniturbulence and Alfven Wave Solar Model extension
213 logical, public, protected :: mhd_uawsom = .false.
214 !> Enable conservative Alfvén reflection (one-dimensional or Cartesian gradient-vorticity)
215 logical, public, protected :: mhd_uawsom_reflection = .false.
216 logical, public, protected :: mhd_uawsom_kink_reflection = .false.
217 !> Add divB wave in Roe solver
218 logical, public :: divbwave = .true.
219 !> clean initial divB
220 logical, public :: clean_initial_divb = .false.
221 !> To control divB=0 fix for boundary
222 logical, public, protected :: boundary_divbfix(2*^nd)=.true.
223 !> B0 field is force-free
224 logical, public, protected :: b0field_forcefree=.true.
225 !> Whether an total energy equation is used
226 logical :: total_energy = .true.
227 !> Whether numerical resistive heating is included when solving partial energy equation
228 logical, public :: numerical_resistive_heating = .false.
229 !> Whether gravity work is included in energy equation
230 logical :: gravity_energy
231 !> Keep E x curl(E) in the semirelativistic source: eq (26) of Gombosi et al. 2002 keeps it,
232 !> their eq (28) and wave tests drop it (steady-state limit)
233 logical, public, protected :: mhd_semirelati_curl_e = .true.
234 !> exclude Maxwell stress flux from momentum flux (use hydrodynamic momentum flux) for conservation fix at coarse-fine block boundaries to avoid abnormal large velocity there in strong magnetic field
235 logical, public, protected :: mhd_hd_mom_conserve_fix = .false.
236 !> Algebra used by the Alfven-wave reflection source. one_dimensional_gradient
237 !> retains the original one-dimensional source; cartesian_gradient_vorticity
238 !> is the multidimensional gradient/vorticity closure.
239 character(len=std_len), public, protected :: mhd_uawsom_reflection_mode = 'one_dimensional_gradient'
240 !> Perpendicular hyperbolic-TC closure mode:
241 !> Enable/disable with mhd_hyperbolic_tc_use_perp, not the mode string.
242 !> 'fixed_reference' = fixed classical ratio at the reference normalisation
243 !> 'weak_field_isotropization' = empirical transition using a prescribed Bmin
244 !> 'electron_magnetization' = simplified ratio 1/(1+chi_e^2)
245 character(len=std_len), public, protected :: mhd_hyperbolic_tc_perp_mode = 'fixed_reference'
246 !> Method type to clean divergence of B
247 character(len=std_len), public, protected :: typedivbfix = 'linde'
248 !> Method type of constrained transport
249 character(len=std_len), public, protected :: type_ct = 'uct_contact'
250 !> Update all equations due to divB cleaning
251 character(len=std_len) :: typedivbdiff = 'all'
252 !> type of fluid for thermal conduction
253 type(tc_fluid), public, allocatable :: tc_fl
254 !> type of fluid for thermal emission synthesis
255 type(te_fluid), public, allocatable :: te_fl_mhd
256 !> type of fluid for radiative cooling
257 type(rc_fluid), public, allocatable :: rc_fl
258
259 !define the subroutine interface for the ambipolar mask
260 abstract interface
261
262 subroutine mask_subroutine(ixI^L,ixO^L,w,x,res)
264 integer, intent(in) :: ixi^l, ixo^l
265 double precision, intent(in) :: x(ixi^s,1:ndim)
266 double precision, intent(in) :: w(ixi^s,1:nw)
267 double precision, intent(inout) :: res(ixi^s)
268 end subroutine mask_subroutine
269
270 end interface
271
272 procedure(mask_subroutine), pointer :: usr_mask_ambipolar => null()
273 procedure(sub_small_values), pointer :: mhd_handle_small_values => null()
274 !> The energy conversion that mhd_e_to_ei_cache_log_nH wraps
275 procedure(sub_e_to_ei), pointer :: mhd_e_to_ei_base => null()
276 ! Public methods
277 public :: usr_mask_ambipolar
278 public :: mhd_phys_init
279 public :: mhd_get_v
280 public :: mhd_get_rho
281 public :: mhd_e_to_ei
282 public :: mhd_ei_to_e
284 public :: mhd_face_to_center
285 public :: get_divb
286 public :: get_current
287 !> needed public if we want to use the ambipolar coefficient in the user file
288 public :: multiplyambicoef
289 public :: get_normalized_divb
291 public :: mhd_mag_en_all
293 public :: mhd_uawsom_rho2_factor
296 {^nooned
298 }
299 ! Begin: following relevant for radiative MHD using FLD
300 ! first four are local and only of interest for mod_usr applications
301 ! where they can be used in diagnostics
302 ! NOTE those with _prim expect primitives on entry
304 public :: mhd_get_csrad2
305 public :: mhd_get_trad
307 ! as pointer phys_get_csrad2
308 public :: mhd_get_csrad2_prim
309 ! End: following relevant for radiative MHD using FLD
310 ! Removed orphan public declarations: mhd_get_Rfactor, mhd_get_temperature_from_prim,
311 ! mhd_get_temperature_from_etot. These functions live in mod_mhd_eos.t and are
312 ! reached via the eos% / phys_get_Rfactor / phys_get_tgas / phys_get_temperature
313 ! procedure pointers bound by mod_mhd_eos:bind_eos_to_source.
314
315contains
316
317 !> Read this module"s parameters from a file
318 subroutine mhd_read_params(files)
320 use mod_particles, only: particles_eta, particles_etah
321 character(len=*), intent(in) :: files(:)
322 integer :: n
323
324 namelist /mhd_list/ mhd_energy, mhd_n_tracer, mhd_adiab,&
328 typedivbdiff, type_ct, divbwave, &
331 particles_eta, particles_etah,has_equi_rho_and_p,mhd_equi_thermal,&
346
347 do n = 1, size(files)
348 open(unitpar, file=trim(files(n)), status="old")
349 read(unitpar, mhd_list, end=111)
350111 close(unitpar)
351 end do
352
353 ! He_abundance is set in eos_list and accessed via eos%He_abundance
354
355 end subroutine mhd_read_params
356
357 !> Write this module's parameters to a snapsoht
358 subroutine mhd_write_info(fh)
360 integer, intent(in) :: fh
361
362 integer :: er
363 integer, parameter :: n_par = 1
364 double precision :: values(n_par)
365 integer, dimension(MPI_STATUS_SIZE) :: st
366 character(len=name_len) :: names(n_par)
367
368 call mpi_file_write(fh, n_par, 1, mpi_integer, st, er)
369
370 names(1) = "gamma"
371 values(1) = eos%gamma
372 call mpi_file_write(fh, values, n_par, mpi_double_precision, st, er)
373 call mpi_file_write(fh, names, n_par * name_len, mpi_character, st, er)
374 end subroutine mhd_write_info
375
376 subroutine mhd_phys_init()
381 use mod_gravity, only: gravity_init
386 use mod_cak_force, only: cak_init
388 use mod_geometry
390 {^nooned
392 }
393 use mod_fld
394
395 integer :: itr, idir
396
397 call mhd_read_params(par_files)
399
400 if(mhd_internal_e) then
401 if(mhd_hydrodynamic_e) then
402 mhd_hydrodynamic_e=.false.
403 if(mype==0) write(*,*) 'WARNING: set mhd_hydrodynamic_e=F when mhd_internal_e=T'
404 end if
405 if(has_equi_rho_and_p) then
406 has_equi_rho_and_p=.false.
407 if(mype==0) write(*,*) 'WARNING: set has_equi_rho_and_p=F when mhd_internal_e=T'
408 end if
409 end if
410
411 if(mhd_hydrodynamic_e) then
412 if(mhd_internal_e) then
413 mhd_internal_e=.false.
414 if(mype==0) write(*,*) 'WARNING: set mhd_internal_e=F when mhd_hydrodynamic_e=T'
415 end if
416 if(b0field) then
417 b0field=.false.
418 if(mype==0) write(*,*) 'WARNING: set B0field=F when mhd_hydrodynamic_e=T'
419 end if
420 if(has_equi_rho_and_p) then
421 has_equi_rho_and_p=.false.
422 if(mype==0) write(*,*) 'WARNING: set has_equi_rho_and_p=F when mhd_hydrodynamic_e=T'
423 end if
424 end if
425
426 if(mhd_semirelativistic) then
427 ! B0 splitting follows Gombosi et al. 2002 eqs (92)-(97); its j0xB0 and E.j0 sources carry
428 ! no factor of c and are the ones add_source_B0split applies
429 if(b0field .and. .not.mhd_energy) &
430 call mpistop('B0 splitting needs mhd_energy=T when mhd_semirelativistic=T')
431 ! mhd_add_source_geom_semirelati carries no eq (97) split of the BB stress
432 if(b0field .and. coordinate/=cartesian) &
433 call mpistop('B0 splitting with mhd_semirelativistic=T needs Cartesian geometry')
434 ! the momentum inversion reads B0, and bc_prolong reaches it on the coarse buffer
435 if(b0field) b0fieldalloccoarse=.true.
436 if(has_equi_rho_and_p) then
437 has_equi_rho_and_p=.false.
438 if(mype==0) write(*,*) 'WARNING: set has_equi_rho_and_p=F when mhd_semirelativistic=T'
439 end if
440 if(mhd_hydrodynamic_e) then
441 mhd_hydrodynamic_e=.false.
442 if(mype==0) write(*,*) 'WARNING: set mhd_hydrodynamic_e=F when mhd_semirelativistic=T'
443 end if
444 end if
445
446 if(.not. mhd_energy) then
447 if(mhd_internal_e) then
448 mhd_internal_e=.false.
449 if(mype==0) write(*,*) 'WARNING: set mhd_internal_e=F when mhd_energy=F'
450 end if
451 if(mhd_hydrodynamic_e) then
452 mhd_hydrodynamic_e=.false.
453 if(mype==0) write(*,*) 'WARNING: set mhd_hydrodynamic_e=F when mhd_energy=F'
454 end if
457 if(mype==0) write(*,*) 'WARNING: set mhd_thermal_conduction=F when mhd_energy=F'
458 end if
459 if(mhd_hyperbolic_tc) then
460 mhd_hyperbolic_tc=.false.
461 if(mype==0) write(*,*) 'WARNING: set mhd_hyperbolic_tc=F when mhd_energy=F'
462 end if
463 if(mhd_radiative_cooling) then
465 if(mype==0) write(*,*) 'WARNING: set mhd_radiative_cooling=F when mhd_energy=F'
466 end if
467 if(mhd_trac) then
468 mhd_trac=.false.
469 if(mype==0) write(*,*) 'WARNING: set mhd_trac=F when mhd_energy=F'
470 end if
471 if(b0field) then
472 b0field=.false.
473 if(mype==0) write(*,*) 'WARNING: set B0field=F when mhd_energy=F'
474 end if
475 if(has_equi_rho_and_p) then
476 has_equi_rho_and_p=.false.
477 if(mype==0) write(*,*) 'WARNING: set has_equi_rho_and_p=F when mhd_energy=F'
478 end if
479 end if
480
483 if(mype==0) write(*,*) 'WARNING: set either parabolic TC or hyperbolic TC to F'
484 if(mype==0) write(*,*) 'WARNING: defaulting to only mhd_hyperbolic_tc=T'
485 end if
486 {^ifoned
488 call mpistop("mhd_hyperbolic_tc_use_perp is not supported in 1D")
489 end if
490 }
491
492 physics_type = "mhd"
493 phys_energy=mhd_energy
494 phys_internal_e=mhd_internal_e
497 phys_hd_mom_conserve_fix=mhd_hd_mom_conserve_fix
498
499 phys_gamma = eos%gamma
501
502 if(mhd_energy) then
504 total_energy=.false.
505 else
507 total_energy=.true.
508 end if
509 else
510 total_energy=.false.
511 end if
512 phys_total_energy=total_energy
513 if(mhd_energy) then
514 if(mhd_internal_e) then
515 gravity_energy=.false.
516 else
517 gravity_energy=.true.
518 end if
519 else
520 gravity_energy=.false.
521 end if
522
523 {^ifoned
524 if(mhd_trac .and. mhd_trac_type .gt. 2) then
526 if(mype==0) write(*,*) 'WARNING: reset mhd_trac_type=1 for 1D simulation'
527 end if
528 }
529 if(mhd_trac .and. mhd_trac_type .le. 4) then
530 mhd_trac_mask=bigdouble
531 if(mype==0) write(*,*) 'WARNING: set mhd_trac_mask==bigdouble for global TRAC method'
532 end if
533 if(mhd_trac .and. mhd_trac_type==7) then
534 ! per-cell local TRAC (Johnston 2021) needs the background heating rate for the
535 ! steady-state balance; it is field-aligned and per-cell, so no global trac_mask is used.
536 if(.not. associated(usr_get_heating)) &
537 call mpistop("mhd_trac_type=7 requires usr_get_heating to be set in mod_usr.t")
538 end if
540
542 if(ndim==1) typedivbfix='none'
543 select case (typedivbfix)
544 case ('none')
545 type_divb = divb_none
546 {^nooned
547 case ('multigrid')
548 if(mhd_radiation_fld) call mpistop('To verify whether mg usage for FLD versus divB can be combined')
549 type_divb = divb_multigrid
550 use_multigrid = .true.
551 mg%operator_type = mg_laplacian
552 phys_global_source_after => mhd_clean_divb_multigrid
553 }
554 case ('glm')
555 mhd_glm = .true.
556 need_global_cmax = .true.
557 type_divb = divb_glm
558 case ('powel', 'powell')
559 type_divb = divb_powel
560 case ('janhunen')
561 type_divb = divb_janhunen
562 case ('linde')
563 type_divb = divb_linde
564 case ('lindejanhunen')
565 type_divb = divb_lindejanhunen
566 case ('lindepowel')
567 type_divb = divb_lindepowel
568 case ('lindeglm')
569 mhd_glm = .true.
570 need_global_cmax = .true.
571 type_divb = divb_lindeglm
572 case ('ct')
573 type_divb = divb_ct
574 stagger_grid = .true.
575 case default
576 call mpistop('Unknown divB fix')
577 end select
578
579 allocate(start_indices(number_species),stop_indices(number_species))
580 ! set the index of the first flux variable for species 1
581 start_indices(1)=1
582 ! Determine flux variables
583 rho_ = var_set_rho()
584
585 allocate(mom(ndir))
586 mom(:) = var_set_momentum(ndir)
587 m^c_=mom(^c);
588
589 ! Set index of energy variable
590 if (mhd_energy) then
591 nwwave = 8
592 e_ = var_set_energy() ! energy density
593 p_ = e_ ! gas pressure
594 else
595 nwwave = 7
596 e_ = -1
597 p_ = -1
598 end if
599
600 allocate(mag(ndir))
601 mag(:) = var_set_bfield(ndir)
602 b^c_=mag(^c);
603
604 if (mhd_glm) then
605 psi_ = var_set_fluxvar('psi', 'psi', need_bc=.false.)
606 else
607 psi_ = -1
608 end if
609
610 if(mhd_hyperbolic_tc) then
611 qpar_ = var_set_fluxvar('q', 'q', need_bc=.false.)
613 qperp_ = var_set_fluxvar('qperp', 'qperp', need_bc=.false.)
614 else
615 qperp_ = -1
616 end if
617 !need_global_cmax=.true.
618 else
619 qpar_ = -1
620 qperp_ = -1
621 end if
622
623 if (mhd_fip) then
624 fip_ = var_set_fluxvar('rho_fip', 'fip', need_bc=.false.)
625 else
626 fip_ = -1
627 end if
628
629 if (mhd_uawsom) then
630 waplus_ = var_set_fluxvar('wAplus', 'wAplus')
631 waminus_ = var_set_fluxvar('wAminus', 'wAminus')
632 wkplus_ = var_set_fluxvar('wkplus', 'wkplus')
633 wkminus_ = var_set_fluxvar('wkminus', 'wkminus')
634 else
635 waplus_=-1; waminus_=-1; wkplus_=-1; wkminus_=-1
636 end if
637
638 allocate(tracer(mhd_n_tracer))
639 ! Set starting index of tracers
640 do itr = 1, mhd_n_tracer
641 tracer(itr) = var_set_fluxvar("trc", "trp", itr, need_bc=.false.)
642 end do
643
644 if(mhd_radiation_fld)then
645 if(mhd_cak_force)then
646 if(mype==0) then
647 write(*,*)'Warning: CAK force addition together with FLD radiation'
648 endif
649 endif
651 if(mype==0) then
652 write(*,*)'Warning: Optically thin cooling together with FLD radiation'
653 endif
654 endif
655 if(.not.mhd_energy)then
656 call mpistop('using FLD implies the use of an energy equation, set mhd_energy=T')
657 else
659 call mpistop('using FLD not yet with semirelativistic energy formalism')
660 endif
662 call mpistop('using FLD not yet with hydrodynamic or internal energy formalism')
663 endif
664 if(has_equi_rho_and_p)then
665 call mpistop('using FLD not yet with split off rho and p')
666 endif
667 ! Note: so far ok with total energy equation but allow both split or unsplit B0
668 !> set added variable and equation for radiation energy
669 r_e = var_set_radiation_energy()
670 phys_get_csrad2 => mhd_get_csrad2_prim
671 !> Radiation fluid object: its EoS callbacks are wired in mhd_link_eos
672 allocate(fld_fl)
673 !> Initiate radiation-closure module
674 call fld_init(eos%eos_type, eos%ionE, eos%pi_table)
675 !> The implicit (MG diffusion) hooks need the fld_fl object, so they
676 !> are wired here to physics-module wrappers that inject it.
677 if(use_multigrid)then
678 phys_implicit_update => mhd_fld_implicit_update
679 phys_evaluate_implicit => mhd_fld_evaluate_implicit
680 endif
681 endif
682 else
683 r_e=-1
684 endif
685
686 ! LTE/PI electron-density and temperature aux variables. These must be registered
687 ! after every flux variable (including the FLD radiation energy r_e and any tracers):
688 ! var_set_radiation_energy indexes by nwflux, so if r_e is registered after these
689 ! nw-indexed aux vars its nwflux slot collides with Ne_ (iw_r_e==iw_ne) and overwrites
690 ! its name. Registering them here keeps the flux block contiguous.
691 if (eos%eos_type == 'LTE') then
692 ne_ = var_set_ne()
693 te_ = var_set_te()
694 else if (eos%eos_type == 'PI') then
695 ne_ = -1
696 te_ = var_set_te()
697 else
698 ne_ = -1
699 te_ = -1
700 end if
701
702 ! The radiative transfer reads &rt_list and registers its output variables here, and is set up
703 ! in mhd_check_params once the units and the domain are known
705 if(.not. mhd_energy) call mpistop("radiative transfer needs mhd_energy=T")
706 call rt_sc_activate()
707 end if
708
709 ! set number of variables which need update ghostcells
710 ! set number of variables which need update ghostcells.
711 ! The EoS-derived slots Ne/Te are derived state that must stay consistent with (rho,e)
712 ! wherever the conserved state is valid, so they have to travel with it. var_set_ne /
713 ! var_set_te bump nw but neither nwflux nor nwaux, so the historic nwflux+nwaux silently
714 ! excluded them: they were never communicated, only derived, and every ghost cell held
715 ! zero until something derived it. Extend the window to whichever of them exist (they are
716 ! registered contiguously just above); FI leaves both at -1 and the window is unchanged.
717 ! bc_phys is unaffected -- it iterates nwflux+nwaux independently.
718 nwgc=nwflux+nwaux
719 if (iw_ne > 0) nwgc = max(nwgc, iw_ne)
720 if (iw_te > 0) nwgc = max(nwgc, iw_te)
721
722 ! set the index of the last flux variable for species 1
723 stop_indices(1)=nwflux
724
725 ! set cutoff temperature when using the TRAC method, as well as an auxiliary weight
726 tweight_ = -1
727 if(mhd_trac) then
728 tcoff_ = var_set_wextra()
729 iw_tcoff=tcoff_
730 if(mhd_trac_type .ge. 3) then
731 tweight_ = var_set_wextra()
732 endif
733 else
734 tcoff_ = -1
735 end if
736
737 ! set indices of equi vars and update number_equi_vars
739 if(has_equi_rho_and_p) then
742 iw_equi_rho = equi_rho0_
745 iw_equi_p = equi_pe0_
746 endif
747 ! determine number of stagger variables
748 nws=ndim
749
750 nvector = 2 ! No. vector vars
751 allocate(iw_vector(nvector))
752 iw_vector(1) = mom(1) - 1
753 iw_vector(2) = mag(1) - 1
754
755 ! Check whether custom flux types have been defined
756 if (.not. allocated(flux_type)) then
757 allocate(flux_type(ndir, nwflux))
758 flux_type = flux_default
759 else if (any(shape(flux_type) /= [ndir, nwflux])) then
760 call mpistop("phys_check error: flux_type has wrong shape")
761 end if
762
763 if(nwflux>mag(ndir)) then
764 ! for flux of tracers, using hll flux
765 flux_type(:,mag(ndir)+1:nwflux)=flux_hll
766 end if
767
768 if(ndim>1) then
769 if(mhd_glm) then
770 flux_type(:,psi_)=flux_special
771 do idir=1,ndir
772 flux_type(idir,mag(idir))=flux_special
773 end do
774 else
775 do idir=1,ndir
776 flux_type(idir,mag(idir))=flux_tvdlf
777 end do
778 end if
779 end if
780
781 phys_get_rho => mhd_get_rho
782 phys_get_dt => mhd_get_dt
783 if(mhd_semirelativistic) then
784 if(mhd_energy) then
785 phys_get_cmax => mhd_get_cmax_semirelati
786 else
787 phys_get_cmax => mhd_get_cmax_semirelati_noe
788 end if
789 else
790 if(mhd_energy) then
791 phys_get_cmax => mhd_get_cmax_origin
792 else
793 phys_get_cmax => mhd_get_cmax_origin_noe
794 end if
795 end if
796 phys_get_tcutoff => mhd_get_tcutoff
797 phys_get_h_speed => mhd_get_h_speed
798 if(has_equi_rho_and_p) then
799 phys_get_cbounds => mhd_get_cbounds_split_rho
800 else if(mhd_semirelativistic) then
801 phys_get_cbounds => mhd_get_cbounds_semirelati
802 else
803 phys_get_cbounds => mhd_get_cbounds
804 end if
805
806 !> EOS module: phys_to_primitive / phys_to_conserved are bound by
807 !> mod_mhd_eos:bind_eos_to_source to route through the EoS layer;
808 !> mhd_to_primitive / mhd_to_conserved pointers are NOT used - every
809 !> call to convert state goes through eos%to_primitive / eos%to_conserved.
810 !> eos%inv_squared_c{0,} are set in mhd_physical_units (called below)
811 !> after inv_squared_c{0,} are computed.
812 if(mhd_hydrodynamic_e) then
813 phys_get_flux => mhd_get_flux_hde
814 else if(mhd_semirelativistic) then
815 if(mhd_energy) then
816 phys_get_flux => mhd_get_flux_semirelati
817 else
818 phys_get_flux => mhd_get_flux_semirelati_noe
819 end if
820 else
821 if(b0field.or.has_equi_rho_and_p) then
822 phys_get_flux => mhd_get_flux_split
823 else if(mhd_energy) then
824 phys_get_flux => mhd_get_flux
825 else
826 phys_get_flux => mhd_get_flux_noe
827 end if
828 end if
829 phys_get_v => mhd_get_v
830 if(mhd_semirelativistic) then
831 phys_add_source_geom => mhd_add_source_geom_semirelati
832 else if(b0field.or.has_equi_rho_and_p) then
833 phys_add_source_geom => mhd_add_source_geom_split
834 else
835 phys_add_source_geom => mhd_add_source_geom
836 end if
837 phys_add_source => mhd_add_source
838 phys_check_params => mhd_check_params
839 phys_write_info => mhd_write_info
840
841 if(mhd_internal_e) then
842 phys_handle_small_values => mhd_handle_small_values_inte
843 mhd_handle_small_values => mhd_handle_small_values_inte
844 phys_check_w => mhd_check_w_inte
845 else if(mhd_hydrodynamic_e) then
846 phys_handle_small_values => mhd_handle_small_values_hde
847 mhd_handle_small_values => mhd_handle_small_values_hde
848 phys_check_w => mhd_check_w_hde
849 else if(mhd_semirelativistic) then
850 phys_handle_small_values => mhd_handle_small_values_semirelati
851 mhd_handle_small_values => mhd_handle_small_values_semirelati
852 phys_check_w => mhd_check_w_semirelati
853 else if(has_equi_rho_and_p) then
854 phys_handle_small_values => mhd_handle_small_values_split
855 mhd_handle_small_values => mhd_handle_small_values_split
856 phys_check_w => mhd_check_w_split
857 else if(mhd_energy) then
858 phys_handle_small_values => mhd_handle_small_values_origin
859 mhd_handle_small_values => mhd_handle_small_values_origin
860 phys_check_w => mhd_check_w_origin
861 else
862 phys_handle_small_values => mhd_handle_small_values_noe
863 mhd_handle_small_values => mhd_handle_small_values_noe
864 phys_check_w => mhd_check_w_noe
865 end if
866
867 ! phys_get_pthermal is set by mhd_link_eos
868
869 if(number_equi_vars>0) then
870 phys_set_equi_vars => set_equi_vars_grid
871 endif
872
873 if(type_divb==divb_glm) then
874 phys_modify_wlr => mhd_modify_wlr
875 end if
876
877 ! Rfactor / temperature / pthermal pointers are bound by
878 ! mod_mhd_eos:bind_eos_to_source (called by mhd_link_eos immediately
879 ! after mhd_phys_init). No EoS machinery in mod_mhd_phys.t.
880
881 ! if using ct stagger grid, boundary divb=0 is not done here
882 if(stagger_grid) then
883 select case(type_ct)
884 case('average')
885 transverse_ghost_cells = 1
886 phys_get_ct_velocity => mhd_get_ct_velocity_average
887 phys_update_faces => mhd_update_faces_average
888 case('uct_contact')
889 transverse_ghost_cells = 1
890 phys_get_ct_velocity => mhd_get_ct_velocity_contact
891 phys_update_faces => mhd_update_faces_contact
892 case('uct_hll')
893 transverse_ghost_cells = 2
894 phys_get_ct_velocity => mhd_get_ct_velocity_hll
895 phys_update_faces => mhd_update_faces_hll
896 case default
897 call mpistop('choose average, uct_contact,or uct_hll for type_ct!')
898 end select
899 phys_face_to_center => mhd_face_to_center
900 phys_modify_wlr => mhd_modify_wlr
901 else if(ndim>1) then
902 phys_boundary_adjust => mhd_boundary_adjust
903 end if
904
905 {^nooned
906 ! clean initial divb
908 call mpistop('To verify whether mg usage for FLD versus divB can be combined')
909 if(clean_initial_divb) phys_clean_divb => mhd_clean_divb_multigrid
910 }
911
912 ! derive units from basic units
913 call mhd_physical_units()
914
915 if(mhd_uawsom) then
916 ! Namelist inputs are physical quantities, following the normal AMRVAC
917 ! convention for this module (cm/G in cgs, m/T in SI).
918 if(mhd_uawsom_zeta_scale == -one) &
919 mhd_uawsom_zeta_scale=merge(3.4805d9,3.4805d11,si_unit)
920 if(mhd_uawsom_thread_radius0 == -one) &
921 mhd_uawsom_thread_radius0=merge(1.d5,1.d7,si_unit)
922 if(mhd_uawsom_bref == -one) &
923 mhd_uawsom_bref=merge(1.d-3,10.d0,si_unit)
925 mhd_uawsom_alfven_corr_length0=merge(1.5d7,1.5d9,si_unit)
931 end if
932
933 htc_perp_mode_id=htc_perp_disabled
934 if(mhd_hyperbolic_tc) then
935 if(mhd_hyperbolic_tc_kappa==0.d0) then
936 if(si_unit) then
938 else
940 end if
941 else
943 end if
944
946 select case(trim(mhd_hyperbolic_tc_perp_mode))
947 case('fixed_reference')
948 htc_perp_mode_id=htc_perp_fixed
950 if(si_unit) then
953 else
956 end if
957 end if
958 case('weak_field_isotropization')
959 htc_perp_mode_id=htc_perp_weak_field
960 if(mhd_hyperbolic_tc_bmin==0.d0) then
962 end if
963 case('electron_magnetization')
964 htc_perp_mode_id=htc_perp_electron
965 if(mhd_hyperbolic_tc_coulomb_log<=zero) then
966 call mpistop("mhd_hyperbolic_tc_coulomb_log must be positive")
967 end if
968 case default
969 call mpistop("invalid mhd_hyperbolic_tc_perp_mode: "// &
971 end select
972 end if
973 end if
974 if(.not. mhd_energy .and. mhd_thermal_conduction) then
975 call mpistop("thermal conduction needs mhd_energy=T")
976 end if
977 if(.not. mhd_energy .and. mhd_hyperbolic_tc) then
978 call mpistop("hyperbolic thermal conduction needs mhd_energy=T")
979 end if
980 if(.not. mhd_energy .and. mhd_radiative_cooling) then
981 call mpistop("radiative cooling needs mhd_energy=T")
982 end if
983
984 !> Cache log10(nH) in wextra for LTE+IonE TC (density invariant during STS)
985 if (eos%eos_type == 'LTE' .and. eos%ionE .and. mhd_thermal_conduction) then
986 iw_log_nh = var_set_wextra()
987 end if
988
989 if(mhd_equi_thermal)then
990 if((.not.has_equi_rho_and_p).or.(.not.total_energy))then
991 mhd_equi_thermal=.false.
992 if(mype==0) write(*,*) 'WARNING: turning mhd_equi_thermal=F as no splitting or total e in use'
993 else
995 if(mype==0) write(*,*) 'Will subtract thermal balance in TC or RC with mhd_equi_thermal=T'
996 else
997 mhd_equi_thermal=.false.
998 if(mype==0) write(*,*) 'WARNING: turning mhd_equi_thermal=F as no TC or RC in use'
999 endif
1000 endif
1001 endif
1002
1003 ! Energy conversion pointers needed by EOS module regardless of TC method
1004 if(mhd_internal_e) then
1005 phys_e_to_ei => mhd_ei_unchanged
1006 phys_ei_to_e => mhd_ei_unchanged
1007 else if(mhd_hydrodynamic_e) then
1008 phys_e_to_ei => mhd_e_to_ei_hde
1009 phys_ei_to_e => mhd_ei_to_e_hde
1010 else if(mhd_semirelativistic) then
1011 phys_e_to_ei => mhd_e_to_ei_semirelati
1012 phys_ei_to_e => mhd_ei_to_e_semirelati
1013 else
1014 phys_e_to_ei => mhd_e_to_ei
1015 phys_ei_to_e => mhd_ei_to_e
1016 end if
1017 ! The LTE conduction temperature reads a log10(nH) cache written with each conversion
1018 if(iw_log_nh > 0 .and. .not.mhd_hydrodynamic_e) then
1019 mhd_e_to_ei_base => phys_e_to_ei
1020 phys_e_to_ei => mhd_e_to_ei_cache_log_nh
1021 end if
1022
1023 ! initialize thermal conduction module
1024 if (mhd_thermal_conduction) then
1025 call sts_init()
1026 call tc_init_params(eos%gamma)
1027
1028 allocate(tc_fl)
1029 call tc_get_mhd_params(tc_fl,tc_params_read_mhd)
1030 if(ndim==1) then
1031 call add_sts_method(mhd_get_tc_dt_hd,mhd_sts_set_source_tc_hd,e_,1,e_,1,.false.)
1032 else
1033 call add_sts_method(mhd_get_tc_dt_mhd,mhd_sts_set_source_tc_mhd,e_,1,e_,1,.false.)
1034 end if
1035 if(.not.mhd_internal_e) call set_conversion_methods_to_head(phys_e_to_ei, phys_ei_to_e)
1036 call set_error_handling_to_head(mhd_tc_handle_small_e)
1037 tc_fl%e_ = e_
1038 tc_fl%Tcoff_ = tcoff_
1039 end if
1040
1041
1042 ! Initialize radiative cooling module
1043 if (mhd_radiative_cooling) then
1044 call radiative_cooling_init_params(eos%gamma,eos%He_abundance)
1045 allocate(rc_fl)
1046 rc_fl%fip_ = fip_
1047 call radiative_cooling_init(rc_fl,rc_params_read)
1048 rc_fl%e_ = e_
1049 rc_fl%Tcoff_ = tcoff_
1050 rc_fl%subtract_equi = has_equi_rho_and_p
1051 ! rc_fl EoS pointers (get_rho, get_pthermal, get_var_Rfactor,
1052 ! equi accessors, subtract_equi) are bound by bind_eos_to_source
1053 ! in mod_mhd_eos.t. No EoS machinery in mod_mhd_phys.t.
1054 end if
1055 allocate(te_fl_mhd)
1056 ! te_fl_mhd EoS pointers are bound by bind_eos_to_source in mod_mhd_eos.t
1057{^ifthreed
1058 phys_te_images => mhd_te_images
1059}
1060
1061 ! consistency check for hyperresistivity implementation
1062 if (mhd_eta_hyper>0.0d0) then
1063 if(mype==0) then
1064 write(*,*) '*****Using hyperresistivity: with mhd_eta_hyper :', mhd_eta_hyper
1065 endif
1066 if(b0field) then
1067 ! hyperresistivity not ok yet with splitting
1068 call mpistop("Must have B0field=F when using hyperresistivity")
1069 end if
1070 endif
1071 if (mhd_eta_hyper<0.0d0) then
1072 call mpistop("Must have mhd_eta_hyper positive when using hyperresistivity")
1073 endif
1074
1075 ! Initialize viscosity module
1076 if (mhd_viscosity) then
1077 call viscosity_init(phys_wider_stencil)
1078 end if
1079
1080 ! Initialize gravity module
1081 if(mhd_gravity) then
1082 call gravity_init()
1083 end if
1084
1085 ! Initialize rotating frame module
1086 if(mhd_rotating_frame) then
1087 if(has_equi_rho_and_p) then
1088 ! mod_rotating_frame does not handle splitting of density
1089 call mpistop("Must have has_equi_rho_and_p=F when mhd_rotating_frame=T")
1090 end if
1091 call rotating_frame_init()
1092 endif
1093
1094
1095 ! initialize magnetofriction module
1096 if(mhd_magnetofriction) then
1098 end if
1099
1100 if(mhd_hall) then
1101 if(mhd_semirelativistic) then
1102 ! semirelativistic does not incorporate hall terms
1103 call mpistop("Must have mhd_hall=F when mhd_semirelativistic=T")
1104 end if
1105 if(coordinate>1)then
1106 ! normal unsplit case or split cases do not have geometric sources for Hall included
1107 call mpistop("Must have Cartesian coordinates for Hall")
1108 endif
1109 ! For Hall, we need one more reconstructed layer since currents are computed
1110 ! in mhd_get_flux: assuming one additional ghost layer added in nghostcells.
1111 phys_wider_stencil = 1
1112 end if
1113
1114 ! The perpendicular HTC geometry evaluates a fourth-order centred
1115 ! temperature gradient inside the reconstructed flux layer. It therefore
1116 ! needs one layer beyond the default two ghost cells.
1118 phys_wider_stencil=max(phys_wider_stencil,1)
1119 end if
1120
1121 if(mhd_ambipolar) then
1122 if(mhd_ambipolar_sts) then
1123 call sts_init()
1125 call add_sts_method(get_ambipolar_dt,sts_set_source_ambipolar,mag(1),&
1126 ndir,mag(1),ndir,.true.)
1127 else
1128 ! any total energy or no energy at all case is handled here
1129 call add_sts_method(get_ambipolar_dt,sts_set_source_ambipolar,mom(ndir)+1,&
1130 mag(ndir)-mom(ndir),mag(1),ndir,.true.)
1131 end if
1132 else
1133 mhd_ambipolar_exp=.true.
1134 ! For flux ambipolar term, we need one more reconstructed layer since currents are computed
1135 ! in mhd_get_flux: assuming one additional ghost layer added in nghostcells.
1136 phys_wider_stencil = 1
1137 end if
1138 end if
1139
1140 ! ionization-degree table init now lives in eos_finalise (eos% owns
1141 ! thermodynamic-backend init, parallel to LTE tables); see mod_eos_pi.
1142
1143 ! Initialize CAK radiation force module
1144 if (mhd_cak_force) then
1146 call mpistop("CAK implementation not available in internal or semirelativistic variants")
1147 endif
1148 if(has_equi_rho_and_p) then
1149 call mpistop("CAK force implementation not available for split off pressure and density")
1150 endif
1151 call cak_init(eos%gamma)
1152 endif
1153
1154 ! get hydrodynamic momentum flux for conservation fix in strong magnetic field
1155 if (mhd_hd_mom_conserve_fix) then
1156 if(mhd_energy) then
1157 phys_get_hd_mom_flux => get_hd_mom_flux
1158 else
1159 phys_get_hd_mom_flux => get_hd_mom_flux_noe
1160 end if
1161 end if
1162
1163 end subroutine mhd_phys_init
1164
1165{^ifthreed
1166 subroutine mhd_te_images
1169
1170 select case(convert_type)
1171 case('EIvtiCCmpi','EIvtuCCmpi')
1173 case('ESvtiCCmpi','ESvtuCCmpi')
1175 case('SIvtiCCmpi','SIvtuCCmpi')
1177 case('WIvtiCCmpi','WIvtuCCmpi')
1179 case default
1180 call mpistop("Error in synthesize emission: Unknown convert_type")
1181 end select
1182 end subroutine mhd_te_images
1183}
1184
1185!!start th cond
1186 ! wrappers for STS functions in thermal_conductivity module
1187 ! which take as argument the tc_fluid (defined in the physics module)
1188 subroutine mhd_sts_set_source_tc_mhd(ixI^L,ixO^L,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux)
1192 integer, intent(in) :: ixi^l, ixo^l, igrid, nflux
1193 double precision, intent(in) :: x(ixi^s,1:ndim)
1194 double precision, intent(inout) :: wres(ixi^s,1:nw), w(ixi^s,1:nw)
1195 double precision, intent(in) :: my_dt
1196 logical, intent(in) :: fix_conserve_at_step
1197 call sts_set_source_tc_mhd(ixi^l,ixo^l,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux,tc_fl)
1198 end subroutine mhd_sts_set_source_tc_mhd
1199
1200 subroutine mhd_sts_set_source_tc_hd(ixI^L,ixO^L,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux)
1204 integer, intent(in) :: ixi^l, ixo^l, igrid, nflux
1205 double precision, intent(in) :: x(ixi^s,1:ndim)
1206 double precision, intent(inout) :: wres(ixi^s,1:nw), w(ixi^s,1:nw)
1207 double precision, intent(in) :: my_dt
1208 logical, intent(in) :: fix_conserve_at_step
1209 call sts_set_source_tc_hd(ixi^l,ixo^l,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux,tc_fl)
1210 end subroutine mhd_sts_set_source_tc_hd
1211
1212 function mhd_get_tc_dt_mhd(w,ixI^L,ixO^L,dx^D,x) result(dtnew)
1213 !Check diffusion time limit dt < dx_i**2/((gamma-1)*tc_k_para_i/rho)
1214 !where tc_k_para_i=tc_k_para*B_i**2/B**2
1215 !and T=p/rho
1218
1219 integer, intent(in) :: ixi^l, ixo^l
1220 double precision, intent(in) :: dx^d, x(ixi^s,1:ndim)
1221 double precision, intent(in) :: w(ixi^s,1:nw)
1222 double precision :: dtnew
1223
1224 dtnew=get_tc_dt_mhd(w,ixi^l,ixo^l,dx^d,x,tc_fl)
1225 end function mhd_get_tc_dt_mhd
1226
1227 function mhd_get_tc_dt_hd(w,ixI^L,ixO^L,dx^D,x) result(dtnew)
1228 !Check diffusion time limit dt < dx_i**2/((gamma-1)*tc_k_para_i/rho)
1229 !where tc_k_para_i=tc_k_para*B_i**2/B**2
1230 !and T=p/rho
1233
1234 integer, intent(in) :: ixi^l, ixo^l
1235 double precision, intent(in) :: dx^d, x(ixi^s,1:ndim)
1236 double precision, intent(in) :: w(ixi^s,1:nw)
1237 double precision :: dtnew
1238
1239 dtnew=get_tc_dt_hd(w,ixi^l,ixo^l,dx^d,x,tc_fl)
1240 end function mhd_get_tc_dt_hd
1241
1242 subroutine mhd_tc_handle_small_e(w, x, ixI^L, ixO^L, step)
1244
1245 integer, intent(in) :: ixi^l,ixo^l
1246 double precision, intent(inout) :: w(ixi^s,1:nw)
1247 double precision, intent(in) :: x(ixi^s,1:ndim)
1248 integer, intent(in) :: step
1249 character(len=140) :: error_msg
1250
1251 ! tc_patch_eint repairs w(:, e_) in place during the source-term call,
1252 ! but the Chebyshev recurrence can still produce residual negative
1253 ! e_int in the updated state. Run mhd_handle_small_ei every substep
1254 ! (patch on or off) so STS does not propagate negative e through.
1255 write(error_msg,"(a,i3)") "Thermal conduction step ", step
1256 call mhd_handle_small_ei(w,x,ixi^l,ixo^l,e_,error_msg)
1257 end subroutine mhd_tc_handle_small_e
1258
1259 ! fill in tc_fluid fields from namelist
1260 subroutine tc_params_read_mhd(fl)
1262 type(tc_fluid), intent(inout) :: fl
1263
1264 double precision :: tc_k_para=0d0
1265 double precision :: tc_k_perp=0d0
1266 integer :: n
1267 ! list parameters
1268 logical :: tc_perpendicular=.false.
1269 logical :: tc_saturate=.false.
1270 logical :: tc_patch_eint=.false.
1271 logical :: tc_frozen_ion=.true.
1272 double precision :: trac_t_floor=0.d0
1273 character(len=std_len) :: tc_slope_limiter="MC"
1274
1275 namelist /tc_list/ tc_perpendicular, tc_saturate, tc_slope_limiter, tc_k_para, tc_k_perp, &
1276 tc_patch_eint, trac_t_floor, tc_frozen_ion
1277
1278 do n = 1, size(par_files)
1279 open(unitpar, file=trim(par_files(n)), status="old")
1280 read(unitpar, tc_list, end=111)
1281111 close(unitpar)
1282 end do
1283
1284 fl%tc_perpendicular = tc_perpendicular
1285 fl%tc_saturate = tc_saturate
1286 fl%tc_patch_eint = tc_patch_eint
1287 fl%frozen_ion = tc_frozen_ion
1288 fl%tc_k_para = tc_k_para
1289 fl%tc_k_perp = tc_k_perp
1290 fl%trac_T_floor = trac_t_floor / unit_temperature
1291 select case(tc_slope_limiter)
1292 case ('no','none')
1293 fl%tc_slope_limiter = 0
1294 case ('MC')
1295 ! monotonized central limiter Woodward and Collela limiter (eq.3.51h)
1296 fl%tc_slope_limiter = 1
1297 case('minmod')
1298 ! minmod limiter
1299 fl%tc_slope_limiter = 2
1300 case ('superbee')
1301 ! Roes superbee limiter (eq.3.51i)
1302 fl%tc_slope_limiter = 3
1303 case ('koren')
1304 ! Barry Koren Right variant
1305 fl%tc_slope_limiter = 4
1306 case ('vanleer')
1307 ! van Leer limiter
1308 fl%tc_slope_limiter = 5
1309 case default
1310 call mpistop("Unknown tc_slope_limiter, choose MC, minmod, superbee, koren, vanleer")
1311 end select
1312 end subroutine tc_params_read_mhd
1313!!end th cond
1314
1315!!rad cool
1316 subroutine rc_params_read(fl)
1318 use mod_constants, only: bigdouble
1319 type(rc_fluid), intent(inout) :: fl
1320
1321 !> Lower limit of temperature
1322 double precision :: tlow=bigdouble
1323 double precision :: rad_damp_height=0.5d0
1324 double precision :: rad_damp_scale=0.15d0
1325 integer :: n
1326 ! list parameters
1327 integer :: ncool = 4000
1328 !> Fixed temperature not lower than tlow
1329 logical :: tfix=.false.
1330 !> Add cooling source in a split way (.true.) or un-split way (.false.)
1331 logical :: rc_split=.false.
1332 logical :: rad_damp=.false.
1333 !> Name of cooling curve
1334 character(len=std_len) :: coolcurve='JCcorona'
1335 logical :: rad_newton = .false.
1336 double precision :: rad_newton_trad = 0.006d0
1337 double precision :: rad_newton_rhosurf = 1.d4
1338 double precision :: rad_newton_pthick = 25.d0
1339 !> HEAD-side cooling parameters (missing from common content after merge)
1340 double precision :: cfrac=0.1d0
1341 double precision :: rad_cut_hgt=0.5d0
1342 double precision :: rad_cut_dey=0.15d0
1343 !> Variable-c_V Townsend extension (Y_mod): quadrature and sub-intervals
1344 character(len=8) :: rc_y_mod_quadrature='boole'
1345 integer :: rc_y_mod_n_sub=16
1346 !> Smooth SC<->thin blend (mirrors hd): thin cooling *= 0.5(1+tanh((T-T0)/dT))*0.5(1+tanh((y-y0)/dy))
1347
1348 namelist /rc_list/ coolcurve, ncool, cfrac, tlow, tfix, rc_split, &
1349 rad_cut_hgt, rad_cut_dey, &
1350 rc_y_mod_quadrature, rc_y_mod_n_sub, &
1351 rad_newton, rad_newton_trad, rad_newton_rhosurf, &
1352 rad_newton_pthick, rad_damp, rad_damp_height, rad_damp_scale
1353
1354 do n = 1, size(par_files)
1355 open(unitpar, file=trim(par_files(n)), status="old")
1356 read(unitpar, rc_list, end=111)
1357111 close(unitpar)
1358 end do
1359
1360 fl%ncool=ncool
1361 fl%coolcurve=coolcurve
1362 fl%tlow=tlow
1363 fl%Tfix=tfix
1364 fl%rc_split=rc_split
1365 fl%cfrac=cfrac
1366 fl%rad_cut_hgt=rad_cut_hgt
1367 fl%rad_cut_dey=rad_cut_dey
1368 fl%Y_mod_quadrature=rc_y_mod_quadrature
1369 fl%Y_mod_N_sub=rc_y_mod_n_sub
1370 fl%rad_damp=rad_damp
1371 fl%rad_damp_height=rad_damp_height
1372 fl%rad_damp_scale=rad_damp_scale
1373 fl%rad_newton=rad_newton
1374 fl%rad_newton_trad=rad_newton_trad
1375 fl%rad_newton_rhosurf=rad_newton_rhosurf
1376 fl%rad_newton_pthick=rad_newton_pthick
1377 end subroutine rc_params_read
1378
1379 !> sets the equilibrium variables
1380 subroutine set_equi_vars_grid_faces(igrid,x,ixI^L,ixO^L)
1382 use mod_usr_methods
1383 integer, intent(in) :: igrid, ixi^l, ixo^l
1384 double precision, intent(in) :: x(ixi^s,1:ndim)
1385
1386 double precision :: delx(ixi^s,1:ndim)
1387 double precision :: xc(ixi^s,1:ndim),xshift^d
1388 integer :: idims, ixc^l, hxo^l, ix, idims2
1389
1390 if(slab_uniform)then
1391 ^d&delx(ixi^s,^d)=rnode(rpdx^d_,igrid)\
1392 else
1393 ! for all non-cartesian and stretched cartesian coordinates
1394 delx(ixi^s,1:ndim)=ps(igrid)%dx(ixi^s,1:ndim)
1395 endif
1396
1397 do idims=1,ndim
1398 hxo^l=ixo^l-kr(idims,^d);
1399 if(stagger_grid) then
1400 ! ct needs all transverse cells
1401 ixcmax^d=ixomax^d+nghostcells-nghostcells*kr(idims,^d); ixcmin^d=hxomin^d-nghostcells+nghostcells*kr(idims,^d);
1402 else
1403 ! ixC is centered index in the idims direction from ixOmin-1/2 to ixOmax+1/2
1404 ixcmax^d=ixomax^d; ixcmin^d=hxomin^d;
1405 end if
1406 ! always xshift=0 or 1/2
1407 xshift^d=half*(one-kr(^d,idims));
1408 do idims2=1,ndim
1409 select case(idims2)
1410 {case(^d)
1411 do ix = ixc^lim^d
1412 ! xshift=half: this is the cell center coordinate
1413 ! xshift=0: this is the cell edge i+1/2 coordinate
1414 xc(ix^d%ixC^s,^d)=x(ix^d%ixC^s,^d)+(half-xshift^d)*delx(ix^d%ixC^s,^d)
1415 end do\}
1416 end select
1417 end do
1418 call usr_set_equi_vars(ixi^l,ixc^l,xc,ps(igrid)%equi_vars(ixi^s,1:number_equi_vars,idims))
1419 end do
1420 end subroutine set_equi_vars_grid_faces
1421
1422 !> sets the equilibrium variables
1423 subroutine set_equi_vars_grid(igrid)
1425 use mod_usr_methods
1426
1427 integer, intent(in) :: igrid
1428
1429 !values at the center
1430 call usr_set_equi_vars(ixg^ll,ixg^ll,ps(igrid)%x,ps(igrid)%equi_vars(ixg^t,1:number_equi_vars,0))
1431
1432 !values at the interfaces
1433 call set_equi_vars_grid_faces(igrid,ps(igrid)%x,ixg^ll,ixm^ll)
1434
1435 end subroutine set_equi_vars_grid
1436
1437 ! w, wnew conserved, add splitted variables back to wnew
1438 function convert_vars_splitting(ixI^L,ixO^L, w, x, nwc) result(wnew)
1440 integer, intent(in) :: ixi^l,ixo^l, nwc
1441 double precision, intent(in) :: w(ixi^s, 1:nw)
1442 double precision, intent(in) :: x(ixi^s,1:ndim)
1443 double precision :: wnew(ixo^s, 1:nwc)
1444
1445 if(has_equi_rho_and_p) then
1446 wnew(ixo^s,rho_)=w(ixo^s,rho_)+block%equi_vars(ixo^s,equi_rho0_,0)
1447 else
1448 wnew(ixo^s,rho_)=w(ixo^s,rho_)
1449 endif
1450 wnew(ixo^s,mom(:))=w(ixo^s,mom(:))
1451
1452 if (b0field) then
1453 ! add background magnetic field B0 to B
1454 wnew(ixo^s,mag(1:ndir))=w(ixo^s,mag(1:ndir))+block%B0(ixo^s,1:ndir,0)
1455 else
1456 wnew(ixo^s,mag(1:ndir))=w(ixo^s,mag(1:ndir))
1457 end if
1458
1459 if(mhd_energy) then
1460 wnew(ixo^s,e_)=w(ixo^s,e_)
1461 if(has_equi_rho_and_p) then
1462 wnew(ixo^s,e_)=wnew(ixo^s,e_)+block%equi_vars(ixo^s,equi_pe0_,0)*eos%inv_gamma_minus_1
1463 end if
1464 if(b0field .and. total_energy) then
1465 wnew(ixo^s,e_)=wnew(ixo^s,e_)+0.5d0*sum(block%B0(ixo^s,:,0)**2,dim=ndim+1) &
1466 + sum(w(ixo^s,mag(:))*block%B0(ixo^s,:,0),dim=ndim+1)
1467 end if
1468 end if
1469
1470 end function convert_vars_splitting
1471
1472 subroutine mhd_check_params
1474 use mod_usr_methods
1477 use mod_particles, only: particles_init, particles_eta, particles_etah
1478 use mod_particles, only: npayload,nusrpayload, &
1479 ngridvars,num_particles,physics_type_particles
1480 use mod_fld
1481
1482 double precision :: a,b,xfrac,yfrac
1483
1484 if(mhd_radiative_transfer) call rt_sc_setup()
1485
1486 ! mhd_get_cbounds_semirelati has a single estimator, and pvrs is hd only
1487 if(mhd_semirelativistic .and. mype==0 .and. (boundspeed/=2 .or. h_correction)) write(*,*) &
1488 'WARNING: typeboundspeed and H_correction are ignored', ' when mhd_semirelativistic=T'
1489 if(boundspeed==4) call mpistop("typeboundspeed='pvrs' is not implemented for mhd")
1490
1491 if(mhd_uawsom) then
1492 if(coordinate /= cartesian .or. .not.slab_uniform) &
1493 call mpistop('mhd_uawsom currently requires a uniform Cartesian grid')
1494 if(.not.mhd_energy .or. .not.total_energy) &
1495 call mpistop('mhd_uawsom requires the standard total-energy MHD formulation')
1498 call mpistop('mhd_uawsom: unsupported MHD option')
1499 if(trim(eos%eos_type) /= 'FI') &
1500 call mpistop("mhd_uawsom currently supports eos_type='FI' only")
1502 call mpistop('mhd_uawsom_height_dim must be between 1 and ndim')
1503 if(mhd_uawsom_zeta0 <= one .or. mhd_uawsom_filling_factor <= zero .or. &
1505 call mpistop('mhd_uawsom requires zeta0>1 and 0<filling_factor<1')
1506 if(mhd_uawsom_zeta_scale <= zero .or. &
1507 mhd_uawsom_thread_radius0 <= zero .or. &
1508 mhd_uawsom_alfven_corr_length0 <= zero .or. mhd_uawsom_bref <= zero) &
1509 call mpistop('mhd_uawsom: scales must be positive')
1510 if(mhd_uawsom_sigma < zero) &
1511 call mpistop('mhd_uawsom_sigma must be non-negative')
1512 select case(trim(mhd_uawsom_reflection_mode))
1513 case('one_dimensional_gradient')
1514 uawsom_reflection_id=uawsom_reflection_1d
1515 case('cartesian_gradient_vorticity')
1516 uawsom_reflection_id=uawsom_reflection_cartesian
1517 case default
1518 call mpistop('invalid mhd_uawsom_reflection_mode')
1519 end select
1520 end if
1521
1522 ! Initialize particles module here, so all extra and user vars are sample
1523 if(mhd_particles) then
1524 call particles_init()
1525 if (particles_eta < zero) particles_eta = mhd_eta
1526 if (particles_etah < zero) particles_eta = mhd_etah
1527 end if
1528
1529 ! gamma, gamma_minus_1, inv_gamma_minus_1 are set by eos_init
1530 if (.not. mhd_energy) then
1531 if (eos%gamma <= 0.0d0) call mpistop ("Error: gamma <= 0")
1532 if (mhd_adiab < 0.0d0) call mpistop ("Error: mhd_adiab < 0")
1534 else
1535 if (eos%gamma <= 0.0d0 .or. eos%gamma == 1.0d0) &
1536 call mpistop ("Error: gamma <= 0 or gamma == 1")
1537 small_e = small_pressure * eos%inv_gamma_minus_1
1538 small_r_e = small_pressure * eos%inv_gamma_minus_1
1539 end if
1540
1541 if (number_equi_vars > 0 .and. .not. associated(usr_set_equi_vars)) then
1542 call mpistop("usr_set_equi_vars has to be implemented in the user file")
1543 endif
1544
1545 if(has_equi_rho_and_p) then
1546 ! The Roe and HLLC solvers read the rho slot as a physical density, which
1547 ! under splitting holds only the perturbation. Neither has a split variant,
1548 ! so refuse the combination rather than return a quietly wrong wave speed.
1549 if(any(flux_method(:)==fs_tvdmu) .or. any(flux_method(:)==fs_hllc) &
1550 .or. any(flux_method(:)==fs_hllcd)) then
1551 call mpistop("Must have has_equi_rho_and_p=F with the roe/hllc/hllcd flux schemes")
1552 end if
1553 ! mhd_get_tcutoff forms Te from the perturbation pressure and density.
1554 if(mhd_trac) then
1555 call mpistop("Must have has_equi_rho_and_p=F when mhd_trac=T")
1556 end if
1557 end if
1558 if(convert .or. autoconvert) then
1559 if(convert_type .eq. 'dat_generic_mpi') then
1560 if(mhd_dump_full_vars) then
1561 if(mype .eq. 0) print*, " add conversion method: split -> full "
1562 call add_convert_method(convert_vars_splitting, nw, cons_wnames, "new")
1563 endif
1564 endif
1565 endif
1566
1567 if(mhd_radiation_fld) then
1568 if(.not.use_imex_scheme)then
1569 call mpistop('select IMEX scheme for FLD radiation use')
1570 endif
1571 if(use_multigrid)then
1572 call phys_set_mg_bounds()
1573 else
1574 if(.not.fld_no_mg)call mpistop('multigrid must have BCs for IMEX and FLD radiation use')
1575 endif
1576 if(mype==0)then
1577 write(*,*)'==FLD SETUP======================'
1578 write(*,*)'Using FLD with settings:'
1579 write(*,*)'Using FLD with settings: mhd_radiation_fld=',mhd_radiation_fld
1580 write(*,*)'Using FLD with settings: mhd_fld_pradtensor=',mhd_fld_pradtensor
1581 write(*,*)'Using FLD with settings: fld_fluxlimiter=',fld_fluxlimiter
1582 write(*,*)'Using FLD with settings: fld_bound_diff=',fld_bound_diff
1583 write(*,*)'Using FLD with settings: fld_interaction_method=',fld_interaction_method
1584 write(*,*)'Using FLD with settings: fld_opacity_law=',fld_opacity_law
1585 write(*,*)'Using FLD with settings: fld_kappa0=',fld_kappa0
1586 write(*,*)'Using FLD with settings: fld_opal_table=',fld_opal_table
1587 write(*,*)'Using FLD with settings: fld_Radforce_split=',fld_radforce_split
1588 write(*,*)'Using FLD with settings: fld_bisect_tol=',fld_bisect_tol
1589 write(*,*)'Using FLD with settings: fld_diff_tol=',fld_diff_tol
1590 write(*,*)'Using FLD with settings: nth_for_diff_mg=',nth_for_diff_mg
1591 write(*,*)' FLD has use_imex_scheme and use_multigrid=',use_imex_scheme,use_multigrid
1592 write(*,*)' FLD has fld_no_mg=',fld_no_mg
1593 if(fld_no_mg)then
1594 print *,'WARNING: cheating with FLD diffusion ***********************'
1595 print *,'WARNING: No MG-diffusion for radiative energy at all!!!!!!!'
1596 print *,'WARNING: cheating with FLD diffusion ***********************'
1597 endif
1598 print *,'const_rad_a =',const_rad_a
1599 print *,'NORMALIZED arad_norm=',arad_norm
1600 print *,'NORMALIZED c_norm=',c_norm
1601 if(fld_cnorm>0.0d0)then
1602 print *,'WARNING: cheating with c_norm ***********************'
1603 print *,'WARNING: c_norm reset to=',fld_cnorm
1605 print *,'WARNING: cheating with c_norm ***********************'
1606 endif
1607 print *,'const_kappae =',const_kappae
1608 if(trim(fld_opacity_law).eq.'const_norm')then
1609 print *,'NORMALIZED fld_kappa0 =',fld_kappa0
1610 print *,'physical value (in cgs or SI) =',fld_kappa0*unit_opacity
1611 endif
1612 if(trim(fld_opacity_law).eq.'const')then
1613 print *,'physical fld_kappa (in cgs or SI) =',fld_kappa0
1614 print *,'NORMALIZED value =',fld_kappa0/unit_opacity
1615 endif
1616 write(*,*)'===FLD SETUP====================='
1617 endif
1618 endif
1619
1620 if(mype==0)then
1621 write(*,*)'====MHD run with settings===================='
1622 write(*,*)'Using mod_mhd_phys with settings:'
1623 write(*,*)'SI_unit=',si_unit
1624 write(*,*)'Dimensionality :',ndim
1625 write(*,*)'vector components:',ndir
1626 write(*,*)'coordinate set to type,slab:',coordinate,slab
1627 write(*,*)'number of variables nw=',nw
1628 write(*,*)' start index iwstart=',iwstart
1629 write(*,*)'number of vector variables=',nvector
1630 write(*,*)'number of stagger variables nws=',nws
1631 write(*,*)'number of variables with BCs=',nwgc
1632 write(*,*)'number of vars with fluxes=',nwflux
1633 write(*,*)'number of vars with flux + BC=',nwfluxbc
1634 write(*,*)'number of auxiliary variables=',nwaux
1635 write(*,*)'number of extra vars without flux=',nwextra
1636 write(*,*)'number of extra vars for wextra=',nw_extra
1637 write(*,*)'number of auxiliary I/O variables=',nwauxio
1638 write(*,*)'number of mhd_n_tracer=',mhd_n_tracer
1639 write(*,*)' mhd_energy=',mhd_energy,' with total_energy=',total_energy
1640 write(*,*)' mhd_semirelativistic=',mhd_semirelativistic
1641 write(*,*)' mhd_internal_e=',mhd_internal_e
1642 write(*,*)' mhd_hydrodynamic_e=',mhd_hydrodynamic_e
1643 write(*,*)' mhd_gravity=',mhd_gravity
1644 write(*,*)' mhd_eta=',mhd_eta,' nonzero implies resistivity'
1645 write(*,*)' mhd_viscosity=',mhd_viscosity
1646 write(*,*)' mhd_radiative_cooling=',mhd_radiative_cooling
1647 write(*,*)' mhd_cak_force=',mhd_cak_force
1648 write(*,*)' mhd_radiation_fld=',mhd_radiation_fld
1649 write(*,*)' mhd_thermal_conduction=',mhd_thermal_conduction
1650 write(*,*)' mhd_hyperbolic_tc=',mhd_hyperbolic_tc
1651 write(*,*)' mhd_trac=',mhd_trac
1652 write(*,*)' mhd_hall=',mhd_hall
1653 write(*,*)' mhd_ambipolar=',mhd_ambipolar
1654 write(*,*)' mhd_eta_hyper=',mhd_eta_hyper
1655 write(*,*)' mhd_rotating_frame=',mhd_rotating_frame
1656 write(*,*)' mhd_particles=',mhd_particles
1657 if(mhd_particles) then
1658 write(*,*) '*****Using particles: with mhd_eta, mhd_etah :', mhd_eta, mhd_etah
1659 write(*,*) '*****Using particles: particles_eta, particles_etah :', particles_eta, particles_etah
1660 write(*,*) '*****Using particles: npayload,ngridvars :', npayload,ngridvars
1661 write(*,*) '*****Using particles: nusrpayload :', nusrpayload
1662 write(*,*) '*****Using particles: num_particles :', num_particles
1663 write(*,*) '*****Using particles: physics_type_particles=',physics_type_particles
1664 end if
1665 write(*,*)'number of ghostcells=',nghostcells
1666 write(*,*)'number due to phys_wider_stencil=',phys_wider_stencil
1667 write(*,*)'==========================================='
1668 print *,'========EOS and UNITS==========='
1669 print *,'SI_unit =',si_unit
1670 print *,'gamma=',eos%gamma
1671 print *,'He_abundance =',eos%He_abundance
1672 print *,'RR =',rr
1673 print *,'========EOS and UNITS==========='
1674 print *,'unit_time =',unit_time
1675 print *,'unit_length =',unit_length
1676 print *,'unit_velocity =',unit_velocity
1677 print *,'unit_pressure =',unit_pressure
1678 print *,'unit_numberdensity =',unit_numberdensity
1679 print *,'unit_density =',unit_density
1680 print *,'unit_temperature =',unit_temperature
1681 print *,'unit_mass =',unit_mass
1682 print *,'unit_Erad =',unit_erad
1683 print *,'unit_radflux =',unit_radflux
1684 print *,'unit_magneticfield =',unit_magneticfield
1685 if(si_unit)then
1686 print *,'CHECK that p_u',unit_pressure,' equals ',unit_magneticfield**2/miu0_si
1687 else
1688 print *,'CHECK that p_u',unit_pressure,' equals ',unit_magneticfield**2/(4.0d0*dpi)
1689 endif
1690 print *, 'CHECK that p_u ',unit_pressure,' equals ',unit_density*unit_velocity**2
1691 print *, 'CHECK that L_u ',unit_length,' equals ',unit_velocity*unit_time
1692 print *, 'CHECK that M_u',unit_mass,' equals ',unit_density*unit_length**3
1693 print *, 'density to numberdensity has factor ',unit_density/unit_numberdensity
1694 if(si_unit)then
1695 print *, ' compare this to ',mp_si*(1.d0+4.d0*eos%He_abundance)
1696 else
1697 print *, ' compare this to ',mp_cgs*(1.d0+4.d0*eos%He_abundance)
1698 endif
1699 print *, 'pressure to n T has factor ',unit_pressure/(unit_numberdensity*unit_temperature)
1700 if(si_unit)then
1701 print *, ' compare this to ',kb_si*(2.d0+3.d0*eos%He_abundance)
1704 else
1705 print *, ' compare this to ',kb_cgs*(2.d0+3.d0*eos%He_abundance)
1708 endif
1709 if(eos%eos_type /= 'LTE')then
1710 print *, 'mean molecular weight mu is =',a/b,' = ', (1.d0+4.d0*eos%He_abundance)/(2.d0+3.d0*eos%He_abundance)
1711 xfrac=1.d0/a
1712 yfrac=4.d0*eos%He_abundance/(1.d0+4.d0*eos%He_abundance)
1713 print *, 'mass fraction hydrogen X is =',1/a,' and this equals ', 1.d0/(1.d0+4.d0*eos%He_abundance)
1714 print *, 'mass fraction helium Y is =',yfrac
1715 print *, ' check that 1/mu', b/a,' is equal to 2X+3Y/4=',2.d0*xfrac+3.d0*yfrac/4.d0
1716 print *, ' ratio n_e/n_p=',1.d0+2.0d0*eos%He_abundance
1717 endif
1718 print *,'========UNITS==========='
1719 endif
1720
1721 end subroutine mhd_check_params
1722
1723 subroutine mhd_physical_units()
1725 double precision :: mp,kb,miu0,c_lightspeed,xfrac,sigma_telectron
1726 double precision :: a,b
1727 ! Derive scaling units
1728 if(si_unit) then
1729 mp=mp_si
1730 kb=kb_si
1731 miu0=miu0_si
1732 const_sigmasb=sigma_sb_si
1733 c_lightspeed=c_si
1734 sigma_telectron=sigma_te_si
1735 else
1736 mp=mp_cgs
1737 kb=kb_cgs
1738 miu0=4.d0*dpi ! G^2 cm^2 dyne^-1
1739 const_sigmasb=sigma_sb_cgs
1740 c_lightspeed=const_c
1741 sigma_telectron=sigma_te_cgs
1742 end if
1743 ! Normalisation dispatch keyed solely on eos%eos_type (FI is the default, so
1744 ! legacy parfiles land in the FI/PI absorbed-(a,b), RR=1 branch -- the former
1745 ! eq_state_units=.true. result).
1746 if (eos%eos_type == 'LTE') then
1747 !> Remove the assumed FI normalisation from the units and handle in EoS
1748 a=1d0
1749 b=1d0
1750 eos%nH2rhoFactor = 1d0+4d0*eos%He_abundance
1751 rr=(2d0+3d0*eos%He_abundance) / (1d0+4d0*eos%He_abundance)
1752 xfrac=1.d0/(1.d0+4.d0*eos%He_abundance)
1753 else
1754 !> FI / PI: absorbed-(a,b), RR=1 (a=b=1 with RR=1 would be wrong physics
1755 !> for He>0). PI shares FI's normalisation exactly; the partial b lives here.
1756 a=1d0+4d0*eos%He_abundance
1757 if(eos%eos_type=='PI') then
1758 b=1d0+h_ion_fr+eos%He_abundance*(he_ion_fr*(he_ion_fr2+1d0)+1d0)
1759 else
1760 b=2d0+3d0*eos%He_abundance
1761 end if
1762 rr=1d0
1763 xfrac=1.d0/a
1764 end if
1765 if(unit_density/=1.d0 .or. unit_numberdensity/=1.d0) then
1766 if(unit_density/=1.d0) then
1768 else if(unit_numberdensity/=1.d0) then
1770 end if
1771 if(unit_temperature/=1.d0) then
1775 if(unit_length/=1.d0) then
1777 else if(unit_time/=1.d0) then
1779 end if
1780 else if(unit_magneticfield/=1.d0) then
1784 if(unit_length/=1.d0) then
1786 else if(unit_time/=1.d0) then
1788 end if
1789 else if(unit_pressure/=1.d0) then
1793 if(unit_length/=1.d0) then
1795 else if(unit_time/=1.d0) then
1797 end if
1798 else if(unit_velocity/=1.d0) then
1802 if(unit_length/=1.d0) then
1804 else if(unit_time/=1.d0) then
1806 end if
1807 else if(unit_time/=1.d0) then
1812 end if
1813 else if(unit_temperature/=1.d0) then
1814 ! units of temperature and velocity are dependent
1815 if(unit_magneticfield/=1.d0) then
1820 if(unit_length/=1.d0) then
1822 else if(unit_time/=1.d0) then
1824 end if
1825 else if(unit_pressure/=1.d0) then
1830 if(unit_length/=1.d0) then
1832 else if(unit_time/=1.d0) then
1834 end if
1835 end if
1836 else if(unit_magneticfield/=1.d0) then
1837 ! units of magnetic field and pressure are dependent
1838 if(unit_velocity/=1.d0) then
1843 if(unit_length/=1.d0) then
1845 else if(unit_time/=1.d0) then
1847 end if
1848 else if(unit_time/=0.d0) then
1854 end if
1855 else if(unit_pressure/=1.d0) then
1856 if(unit_velocity/=1.d0) then
1861 if(unit_length/=1.d0) then
1863 else if(unit_time/=1.d0) then
1865 end if
1866 else if(unit_time/=0.d0) then
1872 end if
1873 end if
1874 ! Additional units needed for the particles
1875 c_norm=c_lightspeed/unit_velocity
1877 if (.not. si_unit) unit_charge = unit_charge*const_c
1879
1880 if(mhd_semirelativistic) then
1881 if(mhd_reduced_c<1.d0) then
1882 ! dimensionless speed
1883 inv_squared_c0=1.d0
1884 inv_squared_c=1.d0/mhd_reduced_c**2
1885 else
1886 inv_squared_c0=(unit_velocity/c_lightspeed)**2
1887 inv_squared_c=(unit_velocity/mhd_reduced_c)**2
1888 end if
1890 ! Propagate to the EoS container. Must happen AFTER inv_squared_c{0,}
1891 ! are set above; the assignment earlier in mhd_phys_init runs before
1892 ! mhd_physical_units and would store uninitialised values.
1893 eos%inv_squared_c0 = inv_squared_c0
1894 eos%inv_squared_c = inv_squared_c
1895 end if
1896
1897 !> Units for radiative flux and opacity as used in FLD
1898 ! this is the radiation constant in either cgs or SI units
1899 const_rad_a=4.d0*const_sigmasb/c_lightspeed
1900 ! this is the dimensionless conversion factor for Erad to Trad
1902 ! This is the Thomson scattering opacity in the correct units
1903 ! note that the hydrogen mass fraction X=1/a in eq_state_units
1904 const_kappae=sigma_telectron*(1.d0+xfrac)/(2.0d0*mp)
1905 ! these are the units
1909
1910 end subroutine mhd_physical_units
1911
1912 subroutine mhd_check_w_semirelati(primitive,ixI^L,ixO^L,w,flag)
1914
1915 logical, intent(in) :: primitive
1916 logical, intent(inout) :: flag(ixi^s,1:nw)
1917 integer, intent(in) :: ixi^l, ixo^l
1918 double precision, intent(in) :: w(ixi^s,nw)
1919
1920 double precision :: vel(ixo^s,1:ndir),emech(ixo^s)
1921
1922 flag=.false.
1923 where(w(ixo^s,rho_) < small_density) flag(ixo^s,rho_) = .true.
1924
1925 if(mhd_energy) then
1926 if(primitive) then
1927 where(w(ixo^s,p_) < small_pressure) flag(ixo^s,e_) = .true.
1928 else
1929 call mhd_semirelati_vel_emech(w,ixi^l,ixo^l,vel,emech)
1930 where(w(ixo^s,e_)-emech(ixo^s) < small_e) flag(ixo^s,e_) = .true.
1931 end if
1932 end if
1933
1934 end subroutine mhd_check_w_semirelati
1935
1936 subroutine mhd_check_w_origin(primitive,ixI^L,ixO^L,w,flag)
1938
1939 logical, intent(in) :: primitive
1940 integer, intent(in) :: ixi^l, ixo^l
1941 double precision, intent(in) :: w(ixi^s,nw)
1942 logical, intent(inout) :: flag(ixi^s,1:nw)
1943
1944 integer :: ix^d, igrp
1945
1946 flag=.false.
1947 {do ix^db=ixomin^db,ixomax^db\}
1948 if(w(ix^d,rho_)<small_density) flag(ix^d,rho_) = .true.
1949 if(primitive) then
1950 if(w(ix^d,p_)<small_pressure) flag(ix^d,e_) = .true.
1951 else
1952 if(w(ix^d,e_)-half*((^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)+&
1953 (^c&w(ix^d,b^c_)**2+))-mhd_uawsom_wave_energy_cell(w(ix^d,:))&
1954 <small_e) flag(ix^d,e_) = .true.
1955 if(mhd_uawsom) then
1956 if(w(ix^d,waplus_)<zero) flag(ix^d,waplus_)=.true.
1957 if(w(ix^d,waminus_)<zero) flag(ix^d,waminus_)=.true.
1958 if(w(ix^d,wkplus_)<zero) flag(ix^d,wkplus_)=.true.
1959 if(w(ix^d,wkminus_)<zero) flag(ix^d,wkminus_)=.true.
1960 end if
1961 end if
1962 if(mhd_radiation_fld)then
1963 if(w(ix^d,r_e)<small_r_e) flag(ix^d,r_e) = .true.
1964 endif
1965 {end do\}
1966
1967 end subroutine mhd_check_w_origin
1968
1969 subroutine mhd_check_w_split(primitive,ixI^L,ixO^L,w,flag)
1971
1972 logical, intent(in) :: primitive
1973 integer, intent(in) :: ixi^l, ixo^l
1974 double precision, intent(in) :: w(ixi^s,nw)
1975 logical, intent(inout) :: flag(ixi^s,1:nw)
1976
1977 double precision :: tmp
1978 integer :: ix^d
1979
1980 flag=.false.
1981 {do ix^db=ixomin^db,ixomax^db\}
1982 tmp=w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,0)
1983 if(tmp<small_density) flag(ix^d,rho_) = .true.
1984 if(primitive) then
1985 if(w(ix^d,p_)+block%equi_vars(ix^d,equi_pe0_,0)<small_pressure) flag(ix^d,e_) = .true.
1986 else
1987 tmp=w(ix^d,e_)-half*((^c&w(ix^d,m^c_)**2+)/tmp+(^c&w(ix^d,b^c_)**2+))
1988 if(tmp+block%equi_vars(ix^d,equi_pe0_,0)*eos%inv_gamma_minus_1<small_e) flag(ix^d,e_) = .true.
1989 end if
1990 {end do\}
1991
1992 end subroutine mhd_check_w_split
1993
1994 subroutine mhd_check_w_noe(primitive,ixI^L,ixO^L,w,flag)
1996
1997 logical, intent(in) :: primitive
1998 integer, intent(in) :: ixi^l, ixo^l
1999 double precision, intent(in) :: w(ixi^s,nw)
2000 logical, intent(inout) :: flag(ixi^s,1:nw)
2001
2002 integer :: ix^d
2003
2004 flag=.false.
2005 {do ix^db=ixomin^db,ixomax^db\}
2006 if(w(ix^d,rho_)<small_density) flag(ix^d,rho_) = .true.
2007 {end do\}
2008
2009 end subroutine mhd_check_w_noe
2010
2011 subroutine mhd_check_w_inte(primitive,ixI^L,ixO^L,w,flag)
2013
2014 logical, intent(in) :: primitive
2015 integer, intent(in) :: ixi^l, ixo^l
2016 double precision, intent(in) :: w(ixi^s,nw)
2017 logical, intent(inout) :: flag(ixi^s,1:nw)
2018
2019 integer :: ix^d
2020
2021 flag=.false.
2022 {do ix^db=ixomin^db,ixomax^db\}
2023 if(w(ix^d,rho_)<small_density) flag(ix^d,rho_) = .true.
2024 if(primitive) then
2025 if(w(ix^d,p_) < small_pressure) flag(ix^d,e_) = .true.
2026 else
2027 if(w(ix^d,e_)<small_e) flag(ix^d,e_) = .true.
2028 end if
2029 {end do\}
2030
2031 end subroutine mhd_check_w_inte
2032
2033 subroutine mhd_check_w_hde(primitive,ixI^L,ixO^L,w,flag)
2035
2036 logical, intent(in) :: primitive
2037 integer, intent(in) :: ixi^l, ixo^l
2038 double precision, intent(in) :: w(ixi^s,nw)
2039 logical, intent(inout) :: flag(ixi^s,1:nw)
2040
2041 integer :: ix^d
2042
2043 flag=.false.
2044 {do ix^db=ixomin^db,ixomax^db\}
2045 if(w(ix^d,rho_)<small_density) flag(ix^d,rho_) = .true.
2046 if(primitive) then
2047 if(w(ix^d,p_)<small_pressure) flag(ix^d,e_) = .true.
2048 else
2049 if(w(ix^d,e_)-half*(^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)<small_e) flag(ix^d,e_) = .true.
2050 end if
2051 {end do\}
2052
2053 end subroutine mhd_check_w_hde
2054
2055 subroutine mhd_bound_fip(primitive, ixI^L, ixO^L, w)
2057 logical, intent(in) :: primitive
2058 integer, intent(in) :: ixi^l, ixo^l
2059 double precision, intent(inout) :: w(ixi^s,1:nw)
2060
2061 double precision :: rho_safe(ixi^s), fip_prim(ixi^s)
2062
2063 if (.not. mhd_fip) return
2064
2065 if (primitive) then
2066 w(ixo^s,fip_) = min(maxfip, max(minfip, w(ixo^s,fip_)))
2067 else
2068 if (has_equi_rho_and_p) then
2069 rho_safe(ixo^s) = max(w(ixo^s,rho_) + block%equi_vars(ixo^s,equi_rho0_,b0i), small_density)
2070 else
2071 rho_safe(ixo^s) = max(w(ixo^s,rho_), small_density)
2072 end if
2073 fip_prim(ixo^s) = w(ixo^s,fip_) / rho_safe(ixo^s)
2074 fip_prim(ixo^s) = min(maxfip, max(minfip, fip_prim(ixo^s)))
2075 w(ixo^s,fip_) = rho_safe(ixo^s) * fip_prim(ixo^s)
2076 end if
2077 end subroutine mhd_bound_fip
2078
2079 !> Return local UAWSoM closure coefficients in code units. User cases may
2080 !> replace all three profiles through usr_uawsom_coefficients.
2081 subroutine mhd_uawsom_get_coefficients(w,x,ixI^L,ixO^L,primitive,zeta,radius,lperp_A)
2084 integer, intent(in) :: ixi^l, ixo^l
2085 double precision, intent(in) :: w(ixi^s,1:nw), x(ixi^s,1:ndim)
2086 logical, intent(in) :: primitive
2087 double precision, intent(out) :: zeta(ixi^s), radius(ixi^s), lperp_a(ixi^s)
2088 double precision :: bmag(ixi^s), btotal(ixi^s,1:ndir), height_base
2089 integer :: idir
2090
2091 if(associated(usr_uawsom_coefficients)) then
2092 call usr_uawsom_coefficients(w,x,ixi^l,ixo^l,primitive,zeta,radius,lperp_a)
2093 else
2094 do idir=1,ndir
2095 if(b0field) then
2096 btotal(ixo^s,idir)=w(ixo^s,mag(idir))+block%B0(ixo^s,idir,b0i)
2097 else
2098 btotal(ixo^s,idir)=w(ixo^s,mag(idir))
2099 end if
2100 end do
2101 bmag(ixo^s)=dsqrt(sum(btotal(ixo^s,1:ndir)**2,dim=ndim+1))
2102 select case(mhd_uawsom_height_dim)
2103 case(1)
2104 height_base=xprobmin1
2105 {^nooned
2106 case(2)
2107 height_base=xprobmin2
2108 }
2109 {^ifthreed
2110 case(3)
2111 height_base=xprobmin3
2112 }
2113 end select
2114 zeta(ixo^s)=one+(mhd_uawsom_zeta0-one)*dexp(&
2115 -max(x(ixo^s,mhd_uawsom_height_dim)-height_base,zero)/&
2117 radius(ixo^s)=mhd_uawsom_thread_radius0*dsqrt(&
2118 mhd_uawsom_bref/max(bmag(ixo^s),smalldouble))
2119 lperp_a(ixo^s)=mhd_uawsom_alfven_corr_length0*dsqrt(&
2120 mhd_uawsom_bref/max(bmag(ixo^s),smalldouble))
2121 end if
2122 if(any(zeta(ixo^s)<=one) .or. any(radius(ixo^s)<=zero) .or. &
2123 any(lperp_a(ixo^s)<=zero)) &
2124 call mpistop('mhd_uawsom coefficients require zeta>1 and positive lengths')
2125 end subroutine mhd_uawsom_get_coefficients
2126
2127 subroutine mhd_uawsom_rho2_factor(ixI^L,ixO^L,w,x,factor)
2129 integer, intent(in) :: ixi^l, ixo^l
2130 double precision, intent(in) :: w(ixi^s,1:nw), x(ixi^s,1:ndim)
2131 double precision, intent(out) :: factor(ixi^s)
2132 double precision :: zeta(ixi^s), radius(ixi^s), lperp_a(ixi^s)
2133 call mhd_uawsom_get_coefficients(w,x,ixi^l,ixo^l,.false.,zeta,radius,lperp_a)
2134 factor(ixo^s)=mhd_uawsom_rho2_factor_cell(zeta(ixo^s))
2135 end subroutine mhd_uawsom_rho2_factor
2136
2137 elemental pure double precision function mhd_uawsom_rho2_factor_cell(zeta)
2138 double precision, intent(in) :: zeta
2139 double precision :: f
2141 mhd_uawsom_rho2_factor_cell=1.d0+f*(1.d0-f)*(zeta-1.d0)**2/&
2142 (1.d0+f*zeta-f)**2
2143 end function mhd_uawsom_rho2_factor_cell
2144
2145 pure double precision function mhd_uawsom_wave_pressure_cell(wcell,zeta)
2146 double precision, intent(in) :: wcell(:),zeta
2147 mhd_uawsom_wave_pressure_cell=0.5d0*(wcell(waplus_)+wcell(waminus_))+&
2148 0.25d0*(zeta+1.d0)*(wcell(wkplus_)+wcell(wkminus_))
2150
2151 pure double precision function mhd_uawsom_wave_energy_cell(wcell)
2152 use mod_constants, only: zero
2153 double precision, intent(in) :: wcell(:)
2154 if(mhd_uawsom) then
2156 wcell(wkplus_)+wcell(wkminus_)
2157 else
2159 end if
2160 end function mhd_uawsom_wave_energy_cell
2161
2162 !> Signed population imbalance used by the Cartesian gradient-vorticity reflection model.
2163 !> A positive value transfers W_A^+ (or W_k^+) to the minus population;
2164 !> a negative value transfers the minus population to the plus population.
2165 !> The factor is zero for a balanced state and remains bounded in [-1,1]
2166 !> for an arbitrarily imbalanced state.
2167 elemental pure double precision function mhd_uawsom_imbalance_factor(wplus,wminus)
2168 double precision, intent(in) :: wplus,wminus
2169
2170 if(wplus<=0.d0 .or. wminus<=0.d0) then
2171 mhd_uawsom_imbalance_factor=0.d0
2172 else if(4.d0*wminus<=wplus) then
2173 mhd_uawsom_imbalance_factor=1.d0-2.d0*dsqrt(wminus/wplus)
2174 else if(4.d0*wplus<=wminus) then
2175 mhd_uawsom_imbalance_factor=-(1.d0-2.d0*dsqrt(wplus/wminus))
2176 else
2177 mhd_uawsom_imbalance_factor=0.d0
2178 end if
2179 end function mhd_uawsom_imbalance_factor
2180
2181 !> UAWSoM compression, nonlinear damping, and optional Alfven reflection.
2182 !> Total energy is intentionally unchanged by damping: because it contains
2183 !> the four W variables, their loss is recovered as gas internal energy.
2184 subroutine mhd_add_source_uawsom(qdt,ixI^L,ixO^L,wCT,wCTprim,w,x)
2187 integer, intent(in) :: ixi^l, ixo^l
2188 double precision, intent(in) :: qdt, wct(ixi^s,1:nw), &
2189 wctprim(ixi^s,1:nw), x(ixi^s,1:ndim)
2190 double precision, intent(inout) :: w(ixi^s,1:nw)
2191 double precision :: v(ixi^s,1:ndir), divv(ixi^s)
2192 double precision :: zeta(ixi^s), radius(ixi^s), lperp_a(ixi^s)
2193 double precision :: lperp_k(ixi^s), rho_e(ixi^s)
2194 double precision :: gamma_plus(ixi^s), gamma_minus(ixi^s)
2195 double precision :: gamma_kplus(ixi^s), gamma_kminus(ixi^s)
2196 double precision :: dampap(ixi^s), dampam(ixi^s), dampkp(ixi^s), dampkm(ixi^s)
2197 double precision :: btotal(ixi^s,1:ndir), bunit(ixi^s,1:ndir), bmag(ixi^s)
2198 double precision :: va(ixi^s), vk(ixi^s), lnva(ixi^s), lnvk(ixi^s)
2199 double precision :: grad_component(ixi^s), grad_a(ixi^s), grad_k(ixi^s)
2200 double precision :: curlv(ixi^s,1:3), vorticity(ixi^s)
2201 double precision :: rimb_a(ixi^s), rlim_a(ixi^s), rlim_k(ixi^s)
2202 double precision :: refl_rate(ixi^s), donor_reflection(ixi^s)
2203 double precision :: transfer_a(ixi^s), transfer_k(ixi^s), donor(ixi^s)
2204 double precision :: imbalance_a(ixi^s), imbalance_k(ixi^s)
2205 double precision :: wave_plus(ixi^s), wave_minus(ixi^s)
2206 double precision :: wave_kplus(ixi^s), wave_kminus(ixi^s)
2207 double precision :: f, kink_pressure(ixi^s)
2208 integer :: idir, idirmin
2209 logical :: have_reflection, have_alfven_reflection, have_kink_reflection
2210
2211 v(ixi^s,1:ndir)=wctprim(ixi^s,mom(1:ndir))
2212 call divvector(v,ixi^l,ixo^l,divv)
2213 ! Reflection gradients need one-cell ghost values, so obtain the closure
2214 ! profiles on ixI rather than only on the source update region.
2215 call mhd_uawsom_get_coefficients(wct,x,ixi^l,ixi^l,.false.,zeta,radius,lperp_a)
2217 rho_e(ixi^s)=max(wct(ixi^s,rho_),small_density)/&
2218 max(one+f*zeta(ixi^s)-f,smalldouble)
2219 lperp_k(ixi^s)=dsqrt(10.d0)*dsqrt(f*dpi)*radius(ixi^s)*&
2220 (zeta(ixi^s)+one-f)**1.5d0/&
2221 ((zeta(ixi^s)-one)*(one-f**2.5d0))
2222 gamma_plus(ixo^s)=two/lperp_a(ixo^s)*dsqrt(&
2223 max(wct(ixo^s,waminus_),zero)/max(wct(ixo^s,rho_),small_density))
2224 gamma_minus(ixo^s)=two/lperp_a(ixo^s)*dsqrt(&
2225 max(wct(ixo^s,waplus_),zero)/max(wct(ixo^s,rho_),small_density))
2226 dampap(ixo^s)=gamma_plus(ixo^s)*max(wct(ixo^s,waplus_),zero)
2227 dampam(ixo^s)=gamma_minus(ixo^s)*max(wct(ixo^s,waminus_),zero)
2228 dampkp(ixo^s)=max(wct(ixo^s,wkplus_),zero)**1.5d0/&
2229 (dsqrt(rho_e(ixo^s))*lperp_k(ixo^s))
2230 dampkm(ixo^s)=max(wct(ixo^s,wkminus_),zero)**1.5d0/&
2231 (dsqrt(rho_e(ixo^s))*lperp_k(ixo^s))
2232 ! These rates limit kink reflection; ordinary kink damping uses dampkp/km.
2234 gamma_kplus(ixo^s)=dsqrt(max(wct(ixo^s,wkplus_),zero)/&
2235 max(rho_e(ixo^s),small_density))/max(lperp_k(ixo^s),smalldouble)
2236 gamma_kminus(ixo^s)=dsqrt(max(wct(ixo^s,wkminus_),zero)/&
2237 max(rho_e(ixo^s),small_density))/max(lperp_k(ixo^s),smalldouble)
2238 end if
2239
2240 w(ixo^s,waplus_)=w(ixo^s,waplus_)-qdt*(&
2241 half*divv(ixo^s)*wct(ixo^s,waplus_)+dampap(ixo^s))
2242 w(ixo^s,waminus_)=w(ixo^s,waminus_)-qdt*(&
2243 half*divv(ixo^s)*wct(ixo^s,waminus_)+dampam(ixo^s))
2244 w(ixo^s,wkplus_)=w(ixo^s,wkplus_)-qdt*(&
2245 half*divv(ixo^s)*wct(ixo^s,wkplus_)+dampkp(ixo^s))
2246 w(ixo^s,wkminus_)=w(ixo^s,wkminus_)-qdt*(&
2247 half*divv(ixo^s)*wct(ixo^s,wkminus_)+dampkm(ixo^s))
2248
2249 kink_pressure(ixo^s)=quarter*(zeta(ixo^s)+one)*&
2250 (wct(ixo^s,wkplus_)+wct(ixo^s,wkminus_))
2251 w(ixo^s,e_)=w(ixo^s,e_)+qdt*(zeta(ixo^s)-one)/&
2252 (zeta(ixo^s)+one)*kink_pressure(ixo^s)*divv(ixo^s)
2253
2254 have_alfven_reflection=.false.
2255 if(mhd_uawsom_reflection) then
2256 select case(uawsom_reflection_id)
2257 case(uawsom_reflection_1d)
2258 have_alfven_reflection=(mhd_uawsom_sigma>zero)
2259 case(uawsom_reflection_cartesian)
2260 have_alfven_reflection=.true.
2261 end select
2262 end if
2263 have_kink_reflection=mhd_uawsom_kink_reflection
2264 have_reflection=have_alfven_reflection .or. have_kink_reflection
2265 if(have_reflection) then
2266 do idir=1,ndir
2267 if(b0field) then
2268 btotal(ixi^s,idir)=wct(ixi^s,mag(idir))+block%B0(ixi^s,idir,b0i)
2269 else
2270 btotal(ixi^s,idir)=wct(ixi^s,mag(idir))
2271 end if
2272 end do
2273 bmag(ixi^s)=dsqrt(sum(btotal(ixi^s,1:ndir)**2,dim=ndim+1))
2274 if(have_kink_reflection .or. &
2275 (have_alfven_reflection .and. uawsom_reflection_id==uawsom_reflection_cartesian)) then
2276 do idir=1,ndir
2277 bunit(ixi^s,idir)=btotal(ixi^s,idir)/max(bmag(ixi^s),smalldouble)
2278 end do
2279 end if
2280 if(have_alfven_reflection) then
2281 va(ixi^s)=bmag(ixi^s)/dsqrt(max(wct(ixi^s,rho_),small_density))
2282 if(uawsom_reflection_id==uawsom_reflection_cartesian) &
2283 lnva(ixi^s)=dlog(max(va(ixi^s),smalldouble))
2284 end if
2285 if(have_kink_reflection) then
2286 vk(ixi^s)=bmag(ixi^s)/dsqrt(max(rho_e(ixi^s)*&
2287 (zeta(ixi^s)+one)/two,smalldouble))
2288 lnvk(ixi^s)=dlog(max(vk(ixi^s),smalldouble))
2289 end if
2290
2291 ! Apply exchange after the ordinary source update. The donor cap below
2292 ! uses that actual post-damping state; it does not silently repair a
2293 ! negative state produced by compression or damping. The standard
2294 ! small-value handling remains responsible for such a state.
2295 if(have_alfven_reflection) then
2296 wave_plus(ixo^s)=max(wct(ixo^s,waplus_),zero)
2297 wave_minus(ixo^s)=max(wct(ixo^s,waminus_),zero)
2298
2299 select case(uawsom_reflection_id)
2300 case(uawsom_reflection_1d)
2301 ! Retain the original one-dimensional source and sign convention:
2302 ! a positive transfer removes W_A^+ and adds W_A^-.
2303 call gradient(va,ixi^l,ixo^l,mhd_uawsom_height_dim,grad_component)
2304 refl_rate(ixo^s)=mhd_uawsom_sigma*&
2305 (wctprim(ixo^s,mom(mhd_uawsom_height_dim))+va(ixo^s))/&
2306 max(va(ixo^s),smalldouble)*grad_component(ixo^s)
2307 donor_reflection(ixo^s)=merge(max(wct(ixo^s,waplus_),zero),&
2308 max(wct(ixo^s,waminus_),zero),refl_rate(ixo^s)>=zero)
2309 transfer_a(ixo^s)=refl_rate(ixo^s)*donor_reflection(ixo^s)
2310 case(uawsom_reflection_cartesian)
2311 grad_a(ixo^s)=zero
2312 do idir=1,ndim
2313 call gradient(lnva,ixi^l,ixo^l,idir,grad_component)
2314 grad_a(ixo^s)=grad_a(ixo^s)+va(ixo^s)*bunit(ixo^s,idir)*&
2315 grad_component(ixo^s)
2316 end do
2317 call curlvector(v,ixi^l,ixo^l,curlv,idirmin,1,ndir)
2318 vorticity(ixo^s)=zero
2319 do idir=1,ndir
2320 vorticity(ixo^s)=vorticity(ixo^s)+bunit(ixo^s,idir)*&
2321 curlv(ixo^s,idir)
2322 end do
2323 ! Sigma belongs to the one-dimensional gradient source only; it is
2324 ! absent from the Cartesian gradient-vorticity R_imb/R_lim closure.
2325 rimb_a(ixo^s)=dsqrt(grad_a(ixo^s)**2+vorticity(ixo^s)**2)
2326 rlim_a(ixo^s)=min(rimb_a(ixo^s),&
2327 max(gamma_plus(ixo^s),gamma_minus(ixo^s)))
2328 imbalance_a(ixo^s)=mhd_uawsom_imbalance_factor(&
2329 wave_plus(ixo^s),wave_minus(ixo^s))
2330 transfer_a(ixo^s)=rlim_a(ixo^s)*imbalance_a(ixo^s)*&
2331 dsqrt(wave_plus(ixo^s)*wave_minus(ixo^s))
2332 end select
2333
2334 if(qdt>zero) then
2335 donor(ixo^s)=merge(max(w(ixo^s,waplus_),zero),&
2336 max(w(ixo^s,waminus_),zero),transfer_a(ixo^s)>=zero)
2337 transfer_a(ixo^s)=sign(min(abs(transfer_a(ixo^s)),&
2338 donor(ixo^s)/qdt),transfer_a(ixo^s))
2339 else
2340 transfer_a(ixo^s)=zero
2341 end if
2342 w(ixo^s,waplus_)=w(ixo^s,waplus_)-qdt*transfer_a(ixo^s)
2343 w(ixo^s,waminus_)=w(ixo^s,waminus_)+qdt*transfer_a(ixo^s)
2344 end if
2345
2346 if(have_kink_reflection) then
2347 wave_kplus(ixo^s)=max(wct(ixo^s,wkplus_),zero)
2348 wave_kminus(ixo^s)=max(wct(ixo^s,wkminus_),zero)
2349 grad_k(ixo^s)=zero
2350 do idir=1,ndim
2351 call gradient(lnvk,ixi^l,ixo^l,idir,grad_component)
2352 grad_k(ixo^s)=grad_k(ixo^s)+vk(ixo^s)*bunit(ixo^s,idir)*&
2353 grad_component(ixo^s)
2354 end do
2355 ! Kink reflection deliberately uses only the kink-speed gradient;
2356 ! the field-aligned velocity-vorticity term belongs to Alfvén waves,
2357 ! and sigma is not part of this limiter.
2358 rlim_k(ixo^s)=min(abs(grad_k(ixo^s)),&
2359 max(gamma_kplus(ixo^s),gamma_kminus(ixo^s)))
2360 imbalance_k(ixo^s)=mhd_uawsom_imbalance_factor(&
2361 wave_kplus(ixo^s),wave_kminus(ixo^s))
2362 transfer_k(ixo^s)=rlim_k(ixo^s)*imbalance_k(ixo^s)*&
2363 dsqrt(wave_kplus(ixo^s)*wave_kminus(ixo^s))
2364 if(qdt>zero) then
2365 donor(ixo^s)=merge(max(w(ixo^s,wkplus_),zero),&
2366 max(w(ixo^s,wkminus_),zero),transfer_k(ixo^s)>=zero)
2367 transfer_k(ixo^s)=sign(min(abs(transfer_k(ixo^s)),&
2368 donor(ixo^s)/qdt),transfer_k(ixo^s))
2369 else
2370 transfer_k(ixo^s)=zero
2371 end if
2372 w(ixo^s,wkplus_)=w(ixo^s,wkplus_)-qdt*transfer_k(ixo^s)
2373 w(ixo^s,wkminus_)=w(ixo^s,wkminus_)+qdt*transfer_k(ixo^s)
2374 end if
2375 end if
2376 end subroutine mhd_add_source_uawsom
2377
2378 !> Transform internal energy to total energy
2379 subroutine mhd_ei_to_e(ixI^L,ixO^L,w,x)
2381 integer, intent(in) :: ixi^l, ixo^l
2382 double precision, intent(inout) :: w(ixi^s, nw)
2383 double precision, intent(in) :: x(ixi^s, 1:ndim)
2384
2385 integer :: ix^d
2386
2387 if(has_equi_rho_and_p) then
2388 {do ix^db=ixomin^db,ixomax^db\}
2389 ! Calculate e = ei + ek + eb
2390 w(ix^d,e_)=w(ix^d,e_)&
2391 +half*((^c&w(ix^d,m^c_)**2+)/&
2392 (w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,0))&
2393 +(^c&w(ix^d,b^c_)**2+))
2394 {end do\}
2395 else
2396 {!dir$ ivdep
2397 do ix^db=ixomin^db,ixomax^db\}
2398 ! Calculate e = ei + ek + eb
2399 w(ix^d,e_)=w(ix^d,e_)&
2400 +half*((^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)&
2401 +(^c&w(ix^d,b^c_)**2+))&
2402 +mhd_uawsom_wave_energy_cell(w(ix^d,:))
2403 {end do\}
2404 end if
2405 end subroutine mhd_ei_to_e
2406
2407 !> Transform internal energy to hydrodynamic energy
2408 subroutine mhd_ei_to_e_hde(ixI^L,ixO^L,w,x)
2410 integer, intent(in) :: ixi^l, ixo^l
2411 double precision, intent(inout) :: w(ixi^s, nw)
2412 double precision, intent(in) :: x(ixi^s, 1:ndim)
2413
2414 integer :: ix^d
2415
2416 {do ix^db=ixomin^db,ixomax^db\}
2417 ! Calculate e = ei + ek
2418 w(ix^d,e_)=w(ix^d,e_)&
2419 +half*(^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)
2420 {end do\}
2421
2422 end subroutine mhd_ei_to_e_hde
2423
2424 !> Velocity from the semirelativistic momentum (Gombosi et al. 2002 eqs 15-16) and emech, the
2425 !> energy variable less the internal energy: ekin + B^2/2 + E^2/(2c^2) for total energy and zero
2426 !> with mhd_internal_e. The Alfven factor and E take the total field, the magnetic energy the
2427 !> stored slot (B1 under splitting, eq 92)
2428 subroutine mhd_semirelati_vel_emech(w,ixI^L,ixO^L,vel,emech)
2430 integer, intent(in) :: ixi^l, ixo^l
2431 double precision, intent(in) :: w(ixi^s,nw)
2432 double precision, intent(out) :: vel(ixo^s,1:ndir)
2433 double precision, intent(out), optional :: emech(ixo^s)
2434
2435 double precision :: btot(1:ndir), efd(1:ndir)
2436 double precision :: b2, rhoc2, tmp, inv_rho
2437 integer :: ix^d
2438
2439 {do ix^db=ixomin^db,ixomax^db\}
2440 ^c&btot(^c)=w(ix^d,b^c_)\
2441 if(b0field) then
2442 ^c&btot(^c)=btot(^c)+block%B0(ix^d,^c,b0i)\
2443 end if
2444 ! Keppens's formula to convert momentum to velocity
2445 b2=(^c&btot(^c)**2+)
2446 tmp=(^c&btot(^c)*w(ix^d,m^c_)+)
2447 rhoc2=w(ix^d,rho_)*mhd_squared_c
2448 inv_rho=1.d0/(w(ix^d,rho_)*(rhoc2+b2))
2449 ^c&vel(ix^d,^c)=(rhoc2*w(ix^d,m^c_)+btot(^c)*tmp)*inv_rho\
2450 if(present(emech)) then
2451 if(mhd_internal_e) then
2452 emech(ix^d)=zero
2453 else
2454 ! E=Bxv
2455 {^ifthreec
2456 efd(1)=btot(2)*vel(ix^d,3)-btot(3)*vel(ix^d,2)
2457 efd(2)=btot(3)*vel(ix^d,1)-btot(1)*vel(ix^d,3)
2458 efd(3)=btot(1)*vel(ix^d,2)-btot(2)*vel(ix^d,1)
2459 }
2460 {^iftwoc
2461 efd(1)=zero
2462 efd(2)=btot(1)*vel(ix^d,2)-btot(2)*vel(ix^d,1)
2463 }
2464 {^ifonec
2465 efd(1)=zero
2466 }
2467 emech(ix^d)=half*((^c&vel(ix^d,^c)**2+)*w(ix^d,rho_)&
2468 +(^c&w(ix^d,b^c_)**2+)&
2469 +(^c&efd(^c)**2+)*inv_squared_c)
2470 end if
2471 end if
2472 {end do\}
2473
2474 end subroutine mhd_semirelati_vel_emech
2475
2476 !> Thermal pressure from internal energy in one cell for the active EoS
2477 double precision function mhd_semirelati_p(eint,rho) result(p)
2479 use mod_eos_lte, only: lte_p_from_eint
2481 double precision, intent(in) :: eint, rho
2482 double precision :: nh, t, rfac
2483
2484 if(eos%type_id==eos_type_lte) then
2485 nh=rho/eos%nH2rhoFactor
2486 p=lte_p_from_eint(eint,nh,dlog10(nh),1.d0/rho)
2487 else if(eos%type_id==eos_type_pi .and. eos%ionE) then
2488 call state_from_eint_pi(rho,eint,t,p,rfac)
2489 else
2490 p=eos%gamma_minus_1*eint
2491 end if
2492
2493 end function mhd_semirelati_p
2494
2495 !> Internal energy from thermal pressure in one cell, the inverse of mhd_semirelati_p
2496 double precision function mhd_semirelati_eint(p,rho) result(eint)
2498 use mod_eos_lte, only: lte_eint_from_p
2500 double precision, intent(in) :: p, rho
2501 double precision :: nh
2502
2503 if(eos%type_id==eos_type_lte) then
2504 nh=rho/eos%nH2rhoFactor
2505 eint=lte_eint_from_p(p,nh,dlog10(nh),rho)
2506 else if(eos%type_id==eos_type_pi .and. eos%ionE) then
2507 call eint_from_rho_p_pi(rho,p,eint)
2508 else
2509 eint=p*eos%inv_gamma_minus_1
2510 end if
2511
2512 end function mhd_semirelati_eint
2513
2514 !> Transform internal energy to total energy
2515 subroutine mhd_ei_to_e_semirelati(ixI^L,ixO^L,w,x)
2517 integer, intent(in) :: ixi^l, ixo^l
2518 double precision, intent(inout) :: w(ixi^s, nw)
2519 double precision, intent(in) :: x(ixi^s, 1:ndim)
2520
2521 double precision :: vel(ixo^s,1:ndir), emech(ixo^s)
2522
2523 call mhd_semirelati_vel_emech(w,ixi^l,ixo^l,vel,emech)
2524 w(ixo^s,e_)=w(ixo^s,e_)+emech(ixo^s)
2525
2526 end subroutine mhd_ei_to_e_semirelati
2527
2528 !> Transform total energy to internal energy
2529 subroutine mhd_e_to_ei(ixI^L,ixO^L,w,x)
2531 integer, intent(in) :: ixi^l, ixo^l
2532 double precision, intent(inout) :: w(ixi^s, nw)
2533 double precision, intent(in) :: x(ixi^s, 1:ndim)
2534
2535 integer :: ix^d
2536
2537 if(has_equi_rho_and_p) then
2538 {do ix^db=ixomin^db,ixomax^db\}
2539 ! Calculate ei = e - ek - eb
2540 w(ix^d,e_)=w(ix^d,e_)&
2541 -half*((^c&w(ix^d,m^c_)**2+)/&
2542 (w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,0))&
2543 +(^c&w(ix^d,b^c_)**2+))
2544 {end do\}
2545 else
2546 {!dir$ ivdep
2547 do ix^db=ixomin^db,ixomax^db\}
2548 ! Calculate ei = e - ek - eb
2549 w(ix^d,e_)=w(ix^d,e_)&
2550 -half*((^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)&
2551 +(^c&w(ix^d,b^c_)**2+))&
2552 -mhd_uawsom_wave_energy_cell(w(ix^d,:))
2553 {end do\}
2554 end if
2555
2556 if(fix_small_values) then
2557 call mhd_handle_small_ei(w,x,ixi^l,ixi^l,e_,'mhd_e_to_ei')
2558 end if
2559
2560 end subroutine mhd_e_to_ei
2561
2562 !> phys_e_to_ei plus the log10(nH) cache read by the LTE conduction temperature. During STS
2563 !> substeps the density is invariant, so the cache is written once per STS cycle (in the
2564 !> sts_before_first_cycle hook) and reused across all substeps
2565 subroutine mhd_e_to_ei_cache_log_nh(ixI^L,ixO^L,w,x)
2567 integer, intent(in) :: ixi^l, ixo^l
2568 double precision, intent(inout) :: w(ixi^s, nw)
2569 double precision, intent(in) :: x(ixi^s, 1:ndim)
2570
2571 call mhd_e_to_ei_base(ixi^l,ixo^l,w,x)
2572 block%wextra(ixo^s, iw_log_nh) = dlog10(w(ixo^s, rho_) / eos%nH2rhoFactor)
2573 end subroutine mhd_e_to_ei_cache_log_nh
2574
2575 !> no conversion: with mhd_internal_e the energy variable is already internal
2576 subroutine mhd_ei_unchanged(ixI^L,ixO^L,w,x)
2578 integer, intent(in) :: ixi^l, ixo^l
2579 double precision, intent(inout) :: w(ixi^s, nw)
2580 double precision, intent(in) :: x(ixi^s, 1:ndim)
2581 end subroutine mhd_ei_unchanged
2582
2583 !> Transform hydrodynamic energy to internal energy
2584 subroutine mhd_e_to_ei_hde(ixI^L,ixO^L,w,x)
2586 integer, intent(in) :: ixi^l, ixo^l
2587 double precision, intent(inout) :: w(ixi^s, nw)
2588 double precision, intent(in) :: x(ixi^s, 1:ndim)
2589
2590 integer :: ix^d
2591
2592 {do ix^db=ixomin^db,ixomax^db\}
2593 ! Calculate ei = e - ek
2594 w(ix^d,e_)=w(ix^d,e_)&
2595 -half*(^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)
2596 {end do\}
2597
2598 if(fix_small_values) then
2599 call mhd_handle_small_ei(w,x,ixi^l,ixi^l,e_,'mhd_e_to_ei_hde')
2600 end if
2601
2602 end subroutine mhd_e_to_ei_hde
2603
2604 !> Transform total energy to internal energy
2605 subroutine mhd_e_to_ei_semirelati(ixI^L,ixO^L,w,x)
2607 integer, intent(in) :: ixi^l, ixo^l
2608 double precision, intent(inout) :: w(ixi^s, nw)
2609 double precision, intent(in) :: x(ixi^s, 1:ndim)
2610
2611 double precision :: vel(ixo^s,1:ndir), emech(ixo^s)
2612
2613 call mhd_semirelati_vel_emech(w,ixi^l,ixo^l,vel,emech)
2614 w(ixo^s,e_)=w(ixo^s,e_)-emech(ixo^s)
2615
2616 if(fix_small_values) then
2617 call mhd_handle_small_ei(w,x,ixi^l,ixo^l,e_,'mhd_e_to_ei_semirelati')
2618 end if
2619
2620 end subroutine mhd_e_to_ei_semirelati
2621
2622 subroutine mhd_handle_small_values_semirelati(primitive, w, x, ixI^L, ixO^L, subname)
2625 logical, intent(in) :: primitive
2626 integer, intent(in) :: ixi^l,ixo^l
2627 double precision, intent(inout) :: w(ixi^s,1:nw)
2628 double precision, intent(in) :: x(ixi^s,1:ndim)
2629 character(len=*), intent(in) :: subname
2630
2631 double precision :: v(ixi^s,1:ndir), emech(ixi^s), small_rho(ixi^s)
2632 double precision, allocatable :: work(:^d&,:)
2633 integer :: ix^d, ixc^l
2634 logical :: flag(ixi^s,1:nw)
2635
2636 ! Averaging may read donors outside ixO; their flags and pressures must
2637 ! refer to the same representation as the cells being repaired.
2638 ixc^l=ixo^l;
2639 if(small_values_method=="average") then
2640 ixc^l=ixi^l;
2641 end if
2642 flag=.false.
2643 where(w(ixc^s,rho_) < small_density)
2644 small_rho(ixc^s)=w(ixc^s,rho_)
2645 w(ixc^s,rho_)=small_density+smalldouble
2646 flag(ixc^s,rho_) = .true.
2647 end where
2648
2649 if(.not.primitive) then
2650 call mhd_semirelati_vel_emech(w,ixi^l,ixc^l,v(ixc^s,1:ndir),emech(ixc^s))
2651 end if
2652 if(mhd_energy) then
2653 if(primitive) then
2654 where(w(ixc^s,p_) < small_pressure) flag(ixc^s,e_) = .true.
2655 else
2656 {do ix^db=ixcmin^db,ixcmax^db\}
2657 if(w(ix^d,e_)-emech(ix^d) < small_e) flag(ix^d,e_) = .true.
2658 {end do\}
2659 end if
2660 end if
2661
2662 if(any(flag(ixo^s,:))) then
2663 select case (small_values_method)
2664 case ("replace")
2665 {do ix^db=ixomin^db,ixomax^db\}
2666 if(flag(ix^d,rho_)) then
2667 ^c&w(ix^d,m^c_)=zero\
2668 end if
2669 if(mhd_energy .and. flag(ix^d,e_)) then
2670 if(primitive) then
2671 w(ix^d,p_) = small_pressure
2672 else
2673 w(ix^d,e_) = small_e + emech(ix^d)
2674 end if
2675 end if
2676 {end do\}
2677 case ("average")
2678 call small_values_average(ixi^l,ixo^l,w,x,flag,rho_)
2679 if(mhd_energy) then
2680 if(primitive) then
2681 call small_values_average(ixi^l,ixo^l,w,x,flag,p_)
2682 else
2683 ! go to internal energy
2684 w(ixc^s,e_)=w(ixc^s,e_)-emech(ixc^s)
2685 ! averaging internal energy
2686 call small_values_average(ixi^l,ixo^l,work,x,flag,e_)
2687 ! back to total energy
2688 w(ixc^s,e_)=w(ixc^s,e_)+emech(ixc^s)
2689 deallocate(work)
2690 end if
2691 end if
2692 case default
2693 if(.not.primitive) then
2694 ! to show velocity and internal energy
2695 w(ixo^s,mom(1:ndir))=v(ixo^s,1:ndir)
2696 if(mhd_energy) w(ixo^s,e_)=w(ixo^s,e_)-emech(ixo^s)
2697 end if
2698 where(flag(ixc^s,rho_))
2699 w(ixc^s,rho_)=small_rho(ixc^s)
2700 end where
2701 call small_values_error(w,x,ixi^l,ixo^l,flag,subname)
2702 end select
2703 end if
2704 if (mhd_fip) call mhd_bound_fip(primitive, ixi^l, ixo^l, w)
2705 end subroutine mhd_handle_small_values_semirelati
2706
2707 subroutine mhd_handle_small_values_origin(primitive, w, x, ixI^L, ixO^L, subname)
2710 logical, intent(in) :: primitive
2711 integer, intent(in) :: ixi^l,ixo^l
2712 double precision, intent(inout) :: w(ixi^s,1:nw)
2713 double precision, intent(in) :: x(ixi^s,1:ndim)
2714 character(len=*), intent(in) :: subname
2715
2716 integer :: ix^d, igrp
2717 logical :: flag(ixi^s,1:nw)
2718
2719 call phys_check_w(primitive, ixi^l, ixo^l, w, flag)
2720
2721 if(any(flag)) then
2722 select case (small_values_method)
2723 case ("replace")
2724 {do ix^db=ixomin^db,ixomax^db\}
2725 if(flag(ix^d,rho_)) w(ix^d,rho_)=small_density
2726 {
2727 if(small_values_fix_iw(m^c_)) then
2728 if(flag({ix^d},rho_)) w({ix^d},m^c_)=0.0d0
2729 end if
2730 \}
2731 if(primitive) then
2732 if(flag(ix^d,e_)) w(ix^d,p_)=small_pressure
2733 else
2734 if(mhd_uawsom) then
2735 if(flag(ix^d,waplus_)) w(ix^d,waplus_)=zero
2736 if(flag(ix^d,waminus_)) w(ix^d,waminus_)=zero
2737 if(flag(ix^d,wkplus_)) w(ix^d,wkplus_)=zero
2738 if(flag(ix^d,wkminus_)) w(ix^d,wkminus_)=zero
2739 end if
2740 if(flag(ix^d,e_)) &
2741 w(ix^d,e_)=small_e+half*((^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)+(^c&w(ix^d,b^c_)**2+))&
2743 end if
2744 if(mhd_radiation_fld)then
2745 if(small_values_fix_iw(r_e)) then
2746 if(flag(ix^d,r_e)) w(ix^d,r_e)=small_r_e
2747 endif
2748 endif
2749 {end do\}
2750 case ("average")
2751 ! do averaging of density
2752 call small_values_average(ixi^l, ixo^l, w, x, flag, rho_)
2753 if(primitive)then
2754 call small_values_average(ixi^l, ixo^l, w, x, flag, p_)
2755 else
2756 if(mhd_uawsom) then
2757 call small_values_average(ixi^l,ixo^l,w,x,flag,waplus_)
2758 call small_values_average(ixi^l,ixo^l,w,x,flag,waminus_)
2759 call small_values_average(ixi^l,ixo^l,w,x,flag,wkplus_)
2760 call small_values_average(ixi^l,ixo^l,w,x,flag,wkminus_)
2761 end if
2762 ! do averaging of internal energy
2763 {do ix^db=iximin^db,iximax^db\}
2764 w(ix^d,e_)=w(ix^d,e_)&
2765 -half*((^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)+(^c&w(ix^d,b^c_)**2+))&
2766 -mhd_uawsom_wave_energy_cell(w(ix^d,:))
2767 {end do\}
2768 call small_values_average(ixi^l, ixo^l, w, x, flag, e_)
2769 ! convert back
2770 {do ix^db=iximin^db,iximax^db\}
2771 w(ix^d,e_)=w(ix^d,e_)&
2772 +half*((^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)+(^c&w(ix^d,b^c_)**2+))&
2773 +mhd_uawsom_wave_energy_cell(w(ix^d,:))
2774 {end do\}
2775 end if
2776 if(mhd_radiation_fld) then
2777 call small_values_average(ixi^l, ixo^l, w, x, flag, r_e)
2778 endif
2779 case default
2780 if(.not.primitive) then
2781 !convert w to primitive
2782 {do ix^db=ixomin^db,ixomax^db\}
2783 ^c&w(ix^d,m^c_)=w(ix^d,m^c_)/w(ix^d,rho_)\
2784 w(ix^d,p_)=eos%gamma_minus_1*(w(ix^d,e_)&
2785 -half*((^c&w(ix^d,m^c_)**2+)*w(ix^d,rho_)+(^c&w(ix^d,b^c_)**2+))&
2786 -mhd_uawsom_wave_energy_cell(w(ix^d,:)))
2787 {end do\}
2788 end if
2789 call small_values_error(w, x, ixi^l, ixo^l, flag, subname)
2790 end select
2791 end if
2792 if (mhd_fip) call mhd_bound_fip(primitive, ixi^l, ixo^l, w)
2793 end subroutine mhd_handle_small_values_origin
2794
2795 subroutine mhd_handle_small_values_split(primitive, w, x, ixI^L, ixO^L, subname)
2798 logical, intent(in) :: primitive
2799 integer, intent(in) :: ixi^l,ixo^l
2800 double precision, intent(inout) :: w(ixi^s,1:nw)
2801 double precision, intent(in) :: x(ixi^s,1:ndim)
2802 character(len=*), intent(in) :: subname
2803
2804 double precision :: rho
2805 integer :: ix^d, igrp
2806 logical :: flag(ixi^s,1:nw)
2807
2808 call phys_check_w(primitive, ixi^l, ixo^l, w, flag)
2809
2810 if(any(flag)) then
2811 select case (small_values_method)
2812 case ("replace")
2813 {do ix^db=ixomin^db,ixomax^db\}
2814 rho=w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,0)
2815 if(flag(ix^d,rho_)) w(ix^d,rho_)=small_density-block%equi_vars(ix^d,equi_rho0_,0)
2816 {
2817 if(small_values_fix_iw(m^c_)) then
2818 if(flag({ix^d},rho_)) w({ix^d},m^c_)=0.0d0
2819 end if
2820 \}
2821 if(primitive) then
2822 if(flag(ix^d,e_)) w(ix^d,p_)=small_pressure-block%equi_vars(ix^d,equi_pe0_,0)
2823 else
2824 if(flag(ix^d,e_)) &
2825 w(ix^d,e_)=small_e+half*((^c&w(ix^d,m^c_)**2+)/rho+(^c&w(ix^d,b^c_)**2+))&
2826 -block%equi_vars(ix^d,equi_pe0_,0)*eos%inv_gamma_minus_1
2827 end if
2828 {end do\}
2829 case ("average")
2830 ! do averaging of density
2831 call small_values_average(ixi^l, ixo^l, w, x, flag, rho_)
2832 if(primitive)then
2833 call small_values_average(ixi^l, ixo^l, w, x, flag, p_)
2834 else
2835 ! do averaging of internal energy
2836 {do ix^db=iximin^db,iximax^db\}
2837 rho=w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,0)
2838 w(ix^d,e_)=w(ix^d,e_)&
2839 -half*((^c&w(ix^d,m^c_)**2+)/rho+(^c&w(ix^d,b^c_)**2+))
2840 {end do\}
2841 call small_values_average(ixi^l, ixo^l, w, x, flag, e_)
2842 ! convert back
2843 {do ix^db=iximin^db,iximax^db\}
2844 rho=w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,0)
2845 w(ix^d,e_)=w(ix^d,e_)&
2846 +half*((^c&w(ix^d,m^c_)**2+)/rho+(^c&w(ix^d,b^c_)**2+))
2847 {end do\}
2848 end if
2849 case default
2850 if(.not.primitive) then
2851 !convert w to primitive
2852 {do ix^db=ixomin^db,ixomax^db\}
2853 rho=w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,0)
2854 ^c&w(ix^d,m^c_)=w(ix^d,m^c_)/rho\
2855 w(ix^d,p_)=eos%gamma_minus_1*(w(ix^d,e_)&
2856 -half*((^c&w(ix^d,m^c_)**2+)*rho+(^c&w(ix^d,b^c_)**2+)))
2857 {end do\}
2858 end if
2859 call small_values_error(w, x, ixi^l, ixo^l, flag, subname)
2860 end select
2861 end if
2862 if (mhd_fip) call mhd_bound_fip(primitive, ixi^l, ixo^l, w)
2863 end subroutine mhd_handle_small_values_split
2864
2865 subroutine mhd_handle_small_values_inte(primitive, w, x, ixI^L, ixO^L, subname)
2868 logical, intent(in) :: primitive
2869 integer, intent(in) :: ixi^l,ixo^l
2870 double precision, intent(inout) :: w(ixi^s,1:nw)
2871 double precision, intent(in) :: x(ixi^s,1:ndim)
2872 character(len=*), intent(in) :: subname
2873
2874 integer :: ix^d, igrp
2875 logical :: flag(ixi^s,1:nw)
2876
2877 call phys_check_w(primitive, ixi^l, ixo^l, w, flag)
2878
2879 if(any(flag)) then
2880 select case (small_values_method)
2881 case ("replace")
2882 {do ix^db=ixomin^db,ixomax^db\}
2883 if(flag(ix^d,rho_)) then
2884 w(ix^d,rho_)=small_density
2885 ^c&w(ix^d,m^c_)=0.d0\
2886 end if
2887 if(primitive) then
2888 if(flag(ix^d,e_)) w(ix^d,p_)=small_pressure
2889 else
2890 if(flag(ix^d,e_)) w(ix^d,e_)=small_e
2891 end if
2892 {end do\}
2893 case ("average")
2894 ! do averaging of density
2895 call small_values_average(ixi^l, ixo^l, w, x, flag, rho_)
2896 ! do averaging of internal energy
2897 call small_values_average(ixi^l, ixo^l, w, x, flag, p_)
2898 case default
2899 if(.not.primitive) then
2900 !convert w to primitive
2901 {do ix^db=ixomin^db,ixomax^db\}
2902 ^c&w(ix^d,m^c_)=w(ix^d,m^c_)/w(ix^d,rho_)\
2903 w(ix^d,p_)=eos%gamma_minus_1*w(ix^d,e_)
2904 {end do\}
2905 end if
2906 call small_values_error(w, x, ixi^l, ixo^l, flag, subname)
2907 end select
2908 end if
2909 if (mhd_fip) call mhd_bound_fip(primitive, ixi^l, ixo^l, w)
2910 end subroutine mhd_handle_small_values_inte
2911
2912 subroutine mhd_handle_small_values_noe(primitive, w, x, ixI^L, ixO^L, subname)
2915 logical, intent(in) :: primitive
2916 integer, intent(in) :: ixi^l,ixo^l
2917 double precision, intent(inout) :: w(ixi^s,1:nw)
2918 double precision, intent(in) :: x(ixi^s,1:ndim)
2919 character(len=*), intent(in) :: subname
2920
2921 integer :: ix^d, igrp
2922 logical :: flag(ixi^s,1:nw)
2923
2924 call phys_check_w(primitive, ixi^l, ixo^l, w, flag)
2925
2926 if(any(flag)) then
2927 select case (small_values_method)
2928 case ("replace")
2929 {do ix^db=ixomin^db,ixomax^db\}
2930 if(flag(ix^d,rho_)) w(ix^d,rho_)=small_density
2931 {
2932 if(small_values_fix_iw(m^c_)) then
2933 if(flag({ix^d},rho_)) w({ix^d},m^c_)=0.0d0
2934 end if
2935 \}
2936 {end do\}
2937 case ("average")
2938 ! do averaging of density
2939 call small_values_average(ixi^l, ixo^l, w, x, flag, rho_)
2940 case default
2941 if(.not.primitive) then
2942 !convert w to primitive
2943 {do ix^db=ixomin^db,ixomax^db\}
2944 ^c&w(ix^d,m^c_)=w(ix^d,m^c_)/w(ix^d,rho_)\
2945 {end do\}
2946 end if
2947 call small_values_error(w, x, ixi^l, ixo^l, flag, subname)
2948 end select
2949 end if
2950 if (mhd_fip) call mhd_bound_fip(primitive, ixi^l, ixo^l, w)
2951 end subroutine mhd_handle_small_values_noe
2952
2953 subroutine mhd_handle_small_values_hde(primitive, w, x, ixI^L, ixO^L, subname)
2956 logical, intent(in) :: primitive
2957 integer, intent(in) :: ixi^l,ixo^l
2958 double precision, intent(inout) :: w(ixi^s,1:nw)
2959 double precision, intent(in) :: x(ixi^s,1:ndim)
2960 character(len=*), intent(in) :: subname
2961
2962 integer :: ix^d, igrp
2963 logical :: flag(ixi^s,1:nw)
2964
2965 call phys_check_w(primitive, ixi^l, ixo^l, w, flag)
2966
2967 if(any(flag)) then
2968 select case (small_values_method)
2969 case ("replace")
2970 {do ix^db=ixomin^db,ixomax^db\}
2971 if(flag(ix^d,rho_)) then
2972 w(ix^d,rho_)=small_density
2973 ^c&w(ix^d,m^c_)=0.d0\
2974 end if
2975 if(primitive) then
2976 if(flag(ix^d,e_)) w(ix^d,p_)=small_pressure
2977 else
2978 if(flag(ix^d,e_)) w(ix^d,e_)=small_e+half*(^c&w(ix^d,m^c_)**2+)/w(ix^d,rho_)
2979 end if
2980 {end do\}
2981 case ("average")
2982 ! do averaging of density
2983 call small_values_average(ixi^l, ixo^l, w, x, flag, rho_)
2984 ! do averaging of energy
2985 call small_values_average(ixi^l, ixo^l, w, x, flag, e_)
2986 case default
2987 if(.not.primitive) then
2988 !convert w to primitive
2989 {do ix^db=ixomin^db,ixomax^db\}
2990 ^c&w(ix^d,m^c_)=w(ix^d,m^c_)/w(ix^d,rho_)\
2991 w(ix^d,p_)=eos%gamma_minus_1*(w(ix^d,e_)-half*(^c&w(ix^d,m^c_)**2+)*w(ix^d,rho_))
2992 {end do\}
2993 end if
2994 call small_values_error(w, x, ixi^l, ixo^l, flag, subname)
2995 end select
2996 end if
2997 if (mhd_fip) call mhd_bound_fip(primitive, ixi^l, ixo^l, w)
2998 end subroutine mhd_handle_small_values_hde
2999
3000 !> Calculate v vector
3001 subroutine mhd_get_v(w,x,ixI^L,ixO^L,v)
3003
3004 integer, intent(in) :: ixi^l, ixo^l
3005 double precision, intent(in) :: w(ixi^s,nw), x(ixi^s,1:ndim)
3006 double precision, intent(out) :: v(ixi^s,ndir)
3007
3008 double precision :: rho(ixi^s)
3009 integer :: idir
3010
3011 if(mhd_semirelativistic) then
3012 call mhd_semirelati_vel_emech(w,ixi^l,ixo^l,v(ixo^s,1:ndir))
3013 return
3014 end if
3015
3016 call mhd_get_rho(w,x,ixi^l,ixo^l,rho)
3017
3018 rho(ixo^s)=1.d0/rho(ixo^s)
3019 ! Convert momentum to velocity
3020 do idir = 1, ndir
3021 v(ixo^s, idir) = w(ixo^s, mom(idir))*rho(ixo^s)
3022 end do
3023
3024 end subroutine mhd_get_v
3025
3026 !> Comoving UAWSoM signal-speed bound shared by CFL and HLL (primitive input).
3027 !> For locally fixed closure coefficients the ideal-MHD/wave principal matrix
3028 !> is symmetric after rescaling. Its blocks have bounds cfast and awave,
3029 !> and coupling norm sqrt(Pwave/rho). The final expression bounds both signs.
3030 !> This does not bound reflection-gradient closures or state-dependent zeta.
3031 subroutine mhd_uawsom_get_signal_speed(w,x,ixI^L,ixO^L,idim,csignal)
3033 integer, intent(in) :: ixi^l, ixo^l, idim
3034 double precision, intent(in) :: w(ixi^s,nw), x(ixi^s,1:ndim)
3035 double precision, intent(out) :: csignal(ixo^s)
3036 double precision :: cs2(ixi^s), zeta(ixi^s), radius(ixi^s), lperp(ixi^s)
3037 double precision :: btotal(1:ndir), b2, inv_rho, qwave, sound2
3038 double precision :: cfast2, discriminant, cfast, awave, kmax
3039 integer :: ix^d
3040
3041 call eos%get_csound2(w,x,ixi^l,ixo^l,cs2)
3042 call mhd_uawsom_get_coefficients(w,x,ixi^l,ixo^l,.true.,zeta,radius,lperp)
3043 if(mhd_hall) kmax=dpi/min({dxlevel(^d)},bigdouble)*half
3044 ! UAWSoM parameter checks exclude equilibrium density/pressure splitting.
3045 {do ix^db=ixomin^db,ixomax^db\}
3046 inv_rho=one/w(ix^d,rho_)
3047 ^c&btotal(^c)=w(ix^d,b^c_)\
3048 if(b0field) then
3049 ^c&btotal(^c)=btotal(^c)+block%B0(ix^d,^c,b0i)\
3050 end if
3051 b2=(^c&btotal(^c)**2+)
3052 qwave=mhd_uawsom_wave_pressure_cell(w(ix^d,:),zeta(ix^d))*inv_rho
3053 ! Only the signal estimate changes; the thermal EOS sound speed is intact.
3054 sound2=cs2(ix^d)+half*qwave
3055 cfast2=b2*inv_rho+sound2
3056 discriminant=max(zero,cfast2**2-4.d0*sound2*btotal(idim)**2*inv_rho)
3057 cfast=sqrt(half*(cfast2+sqrt(discriminant)))
3058 ! Retain the existing Hall estimate; the ideal-MHD proof excludes Hall.
3059 if(mhd_hall) cfast=max(cfast,mhd_etah*sqrt(b2)*inv_rho*kmax)
3060 ! Use the same filling-factor dependence as the kink energy flux.
3061 awave=abs(btotal(idim))*sqrt(inv_rho)*max(one,&
3062 sqrt(two*(one+mhd_uawsom_filling_factor*zeta(ix^d)-&
3063 mhd_uawsom_filling_factor)/(zeta(ix^d)+one)))
3064 csignal(ix^d)=half*(cfast+awave+sqrt((cfast-awave)**2+4.d0*qwave))
3065 {end do\}
3066 end subroutine mhd_uawsom_get_signal_speed
3067
3068 !> Calculate cmax_idim=csound+abs(v_idim) within ixO^L
3069 subroutine mhd_get_cmax_origin(w,x,ixI^L,ixO^L,idim,cmax)
3071
3072 integer, intent(in) :: ixi^l, ixo^l, idim
3073 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
3074 double precision, intent(inout) :: cmax(ixi^s)
3075
3076 double precision :: rho, inv_rho, cfast2, avmincs2, b2, kmax
3077 double precision :: cs2(ixi^s)
3078 integer :: ix^d
3079
3080 if(mhd_uawsom) then
3081 call mhd_uawsom_get_signal_speed(w,x,ixi^l,ixo^l,idim,cmax(ixo^s))
3082 cmax(ixo^s)=abs(w(ixo^s,mom(idim)))+cmax(ixo^s)
3083 return
3084 end if
3085
3086 if(mhd_hall) kmax = dpi/min({dxlevel(^d)},bigdouble)*half
3087
3088 ! Sound speed squared via EoS dispatch (LTE+ionE -> Gamma_1 table; FI -> const gamma).
3089 call eos%get_csound2(w, x, ixi^l, ixo^l, cs2)
3090
3091 if(b0field) then
3092 {do ix^db=ixomin^db,ixomax^db \}
3093 if(has_equi_rho_and_p) then
3094 rho=(w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
3095 else
3096 rho=w(ix^d,rho_)
3097 end if
3098 inv_rho=1.d0/rho
3099 ! store |B|^2 in v
3100 b2=(^c&(w(ix^d,b^c_)+block%B0(ix^d,^c,b0i))**2+)
3101 cfast2=b2*inv_rho+cs2(ix^d)
3102 avmincs2=cfast2**2-4.0d0*cs2(ix^d)*(w(ix^d,mag(idim))+block%B0(ix^d,idim,b0i))**2*inv_rho
3103 if(avmincs2<zero) avmincs2=zero
3104 cmax(ix^d)=sqrt(half*(cfast2+sqrt(avmincs2)))
3105 if(mhd_hall) then
3106 ! take the Hall velocity into account: most simple estimate, high k limit:
3107 ! largest wavenumber supported by grid: Nyquist (in practise can reduce by some factor)
3108 cmax(ix^d)=max(cmax(ix^d),mhd_etah*sqrt(b2)*inv_rho*kmax)
3109 end if
3110 cmax(ix^d)=abs(w(ix^d,mom(idim)))+cmax(ix^d)
3111 {end do\}
3112 else
3113 {do ix^db=ixomin^db,ixomax^db \}
3114 if(has_equi_rho_and_p) then
3115 rho=(w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
3116 else
3117 rho=w(ix^d,rho_)
3118 end if
3119 inv_rho=1.d0/rho
3120 ! store |B|^2 in v
3121 b2=(^c&w(ix^d,b^c_)**2+)
3122 cfast2=b2*inv_rho+cs2(ix^d)
3123 avmincs2=cfast2**2-4.0d0*cs2(ix^d)*w(ix^d,mag(idim))**2*inv_rho
3124 if(avmincs2<zero) avmincs2=zero
3125 cmax(ix^d)=sqrt(half*(cfast2+sqrt(avmincs2)))
3126 if(mhd_hall) then
3127 ! take the Hall velocity into account: most simple estimate, high k limit:
3128 ! largest wavenumber supported by grid: Nyquist (in practise can reduce by some factor)
3129 cmax(ix^d)=max(cmax(ix^d),mhd_etah*sqrt(b2)*inv_rho*kmax)
3130 end if
3131 cmax(ix^d)=abs(w(ix^d,mom(idim)))+cmax(ix^d)
3132 {end do\}
3133 end if
3134
3135 end subroutine mhd_get_cmax_origin
3136
3137 !> Calculate cmax_idim=csound+abs(v_idim) within ixO^L
3138 subroutine mhd_get_cmax_origin_noe(w,x,ixI^L,ixO^L,idim,cmax)
3141
3142 integer, intent(in) :: ixi^l, ixo^l, idim
3143 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
3144 double precision, intent(inout) :: cmax(ixi^s)
3145
3146 double precision :: inv_rho, cfast2, avmincs2, b2, kmax
3147 double precision :: adiabs(ixi^s), gammas(ixi^s)
3148 integer :: ix^d
3149
3150 if(mhd_hall) kmax = dpi/min({dxlevel(^d)},bigdouble)*half
3151
3152 if(associated(usr_set_adiab)) then
3153 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
3154 else
3155 adiabs=mhd_adiab
3156 end if
3157 if(associated(usr_set_gamma)) then
3158 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
3159 else
3160 gammas=eos%gamma
3161 end if
3162 {do ix^db=ixomin^db,ixomax^db \}
3163 inv_rho=1.d0/w(ix^d,rho_)
3164 ! sound speed**2
3165 cmax(ix^d)=gammas(ix^d)*adiabs(ix^d)*w(ix^d,rho_)**(gammas(ix^d)-1.d0)
3166 ! store |B|^2 in v
3167 b2=(^c&w(ix^d,b^c_)**2+)
3168 cfast2=b2*inv_rho+cmax(ix^d)
3169 avmincs2=cfast2**2-4.0d0*cmax(ix^d)*w(ix^d,mag(idim))**2*inv_rho
3170 if(avmincs2<zero) avmincs2=zero
3171 cmax(ix^d)=sqrt(half*(cfast2+sqrt(avmincs2)))
3172 if(mhd_hall) then
3173 ! take the Hall velocity into account: most simple estimate, high k limit:
3174 ! largest wavenumber supported by grid: Nyquist (in practise can reduce by some factor)
3175 cmax(ix^d)=max(cmax(ix^d),mhd_etah*sqrt(b2)*inv_rho*kmax)
3176 end if
3177 cmax(ix^d)=abs(w(ix^d,mom(idim)))+cmax(ix^d)
3178 {end do\}
3179
3180 end subroutine mhd_get_cmax_origin_noe
3181
3182 !> Calculate cmax_idim for semirelativistic MHD, the fast speed plus the advection speed
3183 subroutine mhd_get_cmax_semirelati(w,x,ixI^L,ixO^L,idim,cmax)
3185
3186 integer, intent(in) :: ixi^l, ixo^l, idim
3187 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
3188 double precision, intent(inout):: cmax(ixi^s)
3189
3190 double precision :: csound(ixo^s), gamma2(ixo^s)
3191
3192 call mhd_get_csound_semirelati(w,x,ixi^l,ixo^l,idim,csound,gamma2)
3193 ! equation (68), cmax = gamma^2 abs(v_idim) + c_fast
3194 cmax(ixo^s)=gamma2(ixo^s)*abs(w(ixo^s,mom(idim)))+csound(ixo^s)
3195
3196 end subroutine mhd_get_cmax_semirelati
3197
3198 !> Calculate cmax_idim for semirelativistic MHD
3199 subroutine mhd_get_cmax_semirelati_noe(w,x,ixI^L,ixO^L,idim,cmax)
3202
3203 integer, intent(in) :: ixi^l, ixo^l, idim
3204 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
3205 double precision, intent(inout):: cmax(ixi^s)
3206
3207 double precision :: adiabs(ixi^s), gammas(ixi^s)
3208 double precision :: csound, avmincs2, idim_alfven_speed2
3209 double precision :: inv_rho, alfven_speed2, gamma2
3210 integer :: ix^d
3211
3212 if(associated(usr_set_adiab)) then
3213 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
3214 else
3215 adiabs=mhd_adiab
3216 end if
3217 if(associated(usr_set_gamma)) then
3218 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
3219 else
3220 gammas=eos%gamma
3221 end if
3222
3223 {do ix^db=ixomin^db,ixomax^db \}
3224 inv_rho=1.d0/w(ix^d,rho_)
3225 alfven_speed2=(^c&w(ix^d,b^c_)**2+)*inv_rho
3226 gamma2=1.0d0/(1.d0+alfven_speed2*inv_squared_c)
3227 cmax(ix^d)=1.d0-gamma2*w(ix^d,mom(idim))**2*inv_squared_c
3228 ! sound speed squared
3229 csound=gammas(ix^d)*adiabs(ix^d)*w(ix^d,rho_)**(gammas(ix^d)-1.d0)
3230 idim_alfven_speed2=w(ix^d,mag(idim))**2*inv_rho
3231 ! Va_hat^2+a_hat^2 equation (57)
3232 ! equation (69)
3233 alfven_speed2=alfven_speed2*cmax(ix^d)+csound*(1.d0+idim_alfven_speed2*inv_squared_c)
3234 avmincs2=(gamma2*alfven_speed2)**2-4.0d0*gamma2*csound*idim_alfven_speed2*cmax(ix^d)
3235 if(avmincs2<zero) avmincs2=zero
3236 ! equation (68) fast magnetosonic wave speed
3237 cmax(ix^d)=gamma2*abs(w(ix^d,mom(idim)))+sqrt(half*(gamma2*alfven_speed2+sqrt(avmincs2)))
3238 {end do\}
3239
3240 end subroutine mhd_get_cmax_semirelati_noe
3241
3242 !> get adaptive cutoff temperature for TRAC (Johnston 2019 ApJL, 873, L22)
3243 subroutine mhd_get_tcutoff(ixI^L,ixO^L,w,x,Tco_local,Tmax_local)
3245 use mod_geometry
3248 integer, intent(in) :: ixi^l,ixo^l
3249 double precision, intent(in) :: x(ixi^s,1:ndim)
3250 ! in primitive form
3251 double precision, intent(inout) :: w(ixi^s,1:nw)
3252 double precision, intent(out) :: tco_local,tmax_local
3253
3254 double precision, parameter :: trac_delta=0.25d0
3255 double precision :: te(ixi^s),lts(ixi^s)
3256 double precision, dimension(1:ndim) :: bdir, bunitvec
3257 double precision, dimension(ixI^S,1:ndim) :: gradt
3258 double precision :: ltrc,ltrp,altr
3259 ! TRAC-7 (Johnston et al. 2021, A&A 654 A2) field-aligned local method
3260 double precision :: q_heat(ixi^s), ne(ixi^s), nh_arr(ixi^s)
3261 double precision :: bvec(1:ndir)
3262 double precision :: bmin_c, bmag, gnorm, bgtd, l_t, dl_eff, vmag, bdotv, binp2
3263 double precision :: v_n, a_coeff, l1, cooling, net_cool
3264 double precision :: kappa_par, disc, dx_over_delta, kappa_trac, kappa_eff, tcoff_eff
3265 integer :: idims,ix^d,jxo^l,hxo^l,ixa^d,ixb^d
3266 integer :: jxp^l,hxp^l,ixp^l,ixq^l
3267
3268 if (eos%eos_type == 'LTE' .or. eos%eos_type == 'PI') then
3269 te(ixi^s) = w(ixi^s, te_)
3270 else
3271 call mhd_get_rho(w,x,ixi^l,ixi^l,ne)
3272 call eos%get_Rfactor(w,x,ixi^l,ixi^l,te)
3273 te(ixi^s)=w(ixi^s,p_)/(te(ixi^s)*ne(ixi^s))
3274 end if
3275 tco_local=zero
3276 tmax_local=maxval(te(ixo^s))
3277
3278 {^ifoned
3279 select case(mhd_trac_type)
3280 case(0)
3281 !> test case, fixed cutoff temperature
3282 block%wextra(ixi^s,tcoff_)=2.5d5/unit_temperature
3283 case(1)
3284 do ix1=ixomin1,ixomax1
3285 lts(ix1)=0.5d0*abs(te(ix1+1)-te(ix1-1))/te(ix1)
3286 if(lts(ix1)>trac_delta) then
3287 tco_local=max(tco_local,te(ix1))
3288 end if
3289 end do
3290 case(2)
3291 !> iijima et al. 2021, LTRAC method
3292 ltrc=1.5d0
3293 ltrp=4.d0
3294 ixp^l=ixo^l^ladd1;
3295 hxo^l=ixo^l-1;
3296 jxo^l=ixo^l+1;
3297 hxp^l=ixp^l-1;
3298 jxp^l=ixp^l+1;
3299 lts(ixp^s)=0.5d0*abs(te(jxp^s)-te(hxp^s))/te(ixp^s)
3300 lts(ixp^s)=max(one, (exp(lts(ixp^s))/ltrc)**ltrp)
3301 lts(ixo^s)=0.25d0*(lts(jxo^s)+two*lts(ixo^s)+lts(hxo^s))
3302 block%wextra(ixo^s,tcoff_)=te(ixo^s)*lts(ixo^s)**0.4d0
3303 case default
3304 call mpistop("mhd_trac_type not allowed for 1D simulation")
3305 end select
3306 }
3307 {^nooned
3308 select case(mhd_trac_type)
3309 case(0)
3310 !> test case, fixed cutoff temperature
3311 block%wextra(ixi^s,tcoff_)=2.5d5/unit_temperature
3312 case(1,4,6)
3313 ! temperature gradient at cell centers
3314 do idims=1,ndim
3315 call gradient(te,ixi^l,ixo^l,idims,gradt(ixi^s,idims))
3316 end do
3317 if(mhd_trac_type .gt. 1) then
3318 ! B direction at block center
3319 bdir=zero
3320 if(b0field) then
3321 {do ixa^d=0,1\}
3322 ixb^d=(ixomin^d+ixomax^d-1)/2+ixa^d;
3323 bdir(1:ndim)=bdir(1:ndim)+w(ixb^d,iw_mag(1:ndim))+block%B0(ixb^d,1:ndim,0)
3324 {end do\}
3325 else
3326 {do ixa^d=0,1\}
3327 ixb^d=(ixomin^d+ixomax^d-1)/2+ixa^d;
3328 bdir(1:ndim)=bdir(1:ndim)+w(ixb^d,iw_mag(1:ndim))
3329 {end do\}
3330 end if
3331 {^iftwod
3332 if(bdir(1)/=0.d0) then
3333 block%special_values(3)=sign(1.d0,bdir(1))/dsqrt(1.d0+(bdir(2)/bdir(1))**2)
3334 else
3335 block%special_values(3)=0.d0
3336 end if
3337 if(bdir(2)/=0.d0) then
3338 block%special_values(4)=sign(1.d0,bdir(2))/dsqrt(1.d0+(bdir(1)/bdir(2))**2)
3339 else
3340 block%special_values(4)=0.d0
3341 end if
3342 }
3343 {^ifthreed
3344 if(bdir(1)/=0.d0) then
3345 block%special_values(3)=sign(1.d0,bdir(1))/dsqrt(1.d0+(bdir(2)/bdir(1))**2+&
3346 (bdir(3)/bdir(1))**2)
3347 else
3348 block%special_values(3)=0.d0
3349 end if
3350 if(bdir(2)/=0.d0) then
3351 block%special_values(4)=sign(1.d0,bdir(2))/dsqrt(1.d0+(bdir(1)/bdir(2))**2+&
3352 (bdir(3)/bdir(2))**2)
3353 else
3354 block%special_values(4)=0.d0
3355 end if
3356 if(bdir(3)/=0.d0) then
3357 block%special_values(5)=sign(1.d0,bdir(3))/dsqrt(1.d0+(bdir(1)/bdir(3))**2+&
3358 (bdir(2)/bdir(3))**2)
3359 else
3360 block%special_values(5)=0.d0
3361 end if
3362 }
3363 end if
3364 ! b unit vector: magnetic field direction vector
3365 block%special_values(1)=zero
3366 {do ix^db=ixomin^db,ixomax^db\}
3367 if(b0field) then
3368 ^d&bdir(^d)=w({ix^d},iw_mag(^d))+block%B0({ix^d},^d,0)\
3369 else
3370 ^d&bdir(^d)=w({ix^d},iw_mag(^d))\
3371 end if
3372 {^iftwod
3373 if(bdir(1)/=0.d0) then
3374 bunitvec(1)=sign(1.d0,bdir(1))/dsqrt(1.d0+(bdir(2)/bdir(1))**2)
3375 else
3376 bunitvec(1)=0.d0
3377 end if
3378 if(bdir(2)/=0.d0) then
3379 bunitvec(2)=sign(1.d0,bdir(2))/dsqrt(1.d0+(bdir(1)/bdir(2))**2)
3380 else
3381 bunitvec(2)=0.d0
3382 end if
3383 ! temperature length scale inversed
3384 lts(ix^d)=min(block%ds(ix^d,1),block%ds(ix^d,2))*&
3385 abs(^d&gradt({ix^d},^d)*bunitvec(^d)+)/te(ix^d)
3386 }
3387 {^ifthreed
3388 if(bdir(1)/=0.d0) then
3389 bunitvec(1)=sign(1.d0,bdir(1))/dsqrt(1.d0+(bdir(2)/bdir(1))**2+(bdir(3)/bdir(1))**2)
3390 else
3391 bunitvec(1)=0.d0
3392 end if
3393 if(bdir(2)/=0.d0) then
3394 bunitvec(2)=sign(1.d0,bdir(2))/dsqrt(1.d0+(bdir(1)/bdir(2))**2+(bdir(3)/bdir(2))**2)
3395 else
3396 bunitvec(2)=0.d0
3397 end if
3398 if(bdir(3)/=0.d0) then
3399 bunitvec(3)=sign(1.d0,bdir(3))/dsqrt(1.d0+(bdir(1)/bdir(3))**2+(bdir(2)/bdir(3))**2)
3400 else
3401 bunitvec(3)=0.d0
3402 end if
3403 ! temperature length scale inversed
3404 lts(ix^d)=min(block%ds(ix^d,1),block%ds(ix^d,2),block%ds(ix^d,3))*&
3405 abs(^d&gradt({ix^d},^d)*bunitvec(^d)+)/te(ix^d)
3406 }
3407 if(lts(ix^d)>trac_delta) then
3408 block%special_values(1)=max(block%special_values(1),te(ix^d))
3409 end if
3410 {end do\}
3411 block%special_values(2)=tmax_local
3412 case(2)
3413 !> iijima et al. 2021, LTRAC method
3414 ltrc=1.5d0
3415 ltrp=4.d0
3416 ixp^l=ixo^l^ladd2;
3417 ! temperature gradient at cell centers
3418 do idims=1,ndim
3419 ixq^l=ixp^l;
3420 hxp^l=ixp^l;
3421 jxp^l=ixp^l;
3422 select case(idims)
3423 {case(^d)
3424 ixqmin^d=ixqmin^d+1
3425 ixqmax^d=ixqmax^d-1
3426 hxpmax^d=ixpmin^d
3427 jxpmin^d=ixpmax^d
3428 \}
3429 end select
3430 call gradient(te,ixi^l,ixq^l,idims,gradt(ixi^s,idims))
3431 ! The outermost ghost cell has no room for a fourth-order one-sided
3432 ! stencil. A first-order one-sided gradient keeps the two-ghost-layer
3433 ! LTRAC coverage without reading outside ixI.
3434 call gradientf(te,x,ixi^l,hxp^l,idims,gradt(ixi^s,idims),1,.true.)
3435 call gradientf(te,x,ixi^l,jxp^l,idims,gradt(ixi^s,idims),1,.false.)
3436 end do
3437 ! b unit vector: magnetic field direction vector
3438 if(b0field) then
3439 {do ix^db=ixpmin^db,ixpmax^db\}
3440 ^d&bdir(^d)=w({ix^d},iw_mag(^d))+block%B0({ix^d},^d,0)\
3441 {^iftwod
3442 if(bdir(1)/=0.d0) then
3443 bunitvec(1)=sign(1.d0,bdir(1))/dsqrt(1.d0+(bdir(2)/bdir(1))**2)
3444 else
3445 bunitvec(1)=0.d0
3446 end if
3447 if(bdir(2)/=0.d0) then
3448 bunitvec(2)=sign(1.d0,bdir(2))/dsqrt(1.d0+(bdir(1)/bdir(2))**2)
3449 else
3450 bunitvec(2)=0.d0
3451 end if
3452 }
3453 {^ifthreed
3454 if(bdir(1)/=0.d0) then
3455 bunitvec(1)=sign(1.d0,bdir(1))/dsqrt(1.d0+(bdir(2)/bdir(1))**2+(bdir(3)/bdir(1))**2)
3456 else
3457 bunitvec(1)=0.d0
3458 end if
3459 if(bdir(2)/=0.d0) then
3460 bunitvec(2)=sign(1.d0,bdir(2))/dsqrt(1.d0+(bdir(1)/bdir(2))**2+(bdir(3)/bdir(2))**2)
3461 else
3462 bunitvec(2)=0.d0
3463 end if
3464 if(bdir(3)/=0.d0) then
3465 bunitvec(3)=sign(1.d0,bdir(3))/dsqrt(1.d0+(bdir(1)/bdir(3))**2+(bdir(2)/bdir(3))**2)
3466 else
3467 bunitvec(3)=0.d0
3468 end if
3469 }
3470 ! temperature length scale inversed
3471 lts(ix^d)=abs(^d&gradt({ix^d},^d)*bunitvec(^d)+)/te(ix^d)
3472 ! fraction of cells size to temperature length scale
3473 lts(ix^d)=min(^d&block%ds({ix^d},^d))*lts(ix^d)
3474 lts(ix^d)=max(one,(exp(lts(ix^d))/ltrc)**ltrp)
3475 {end do\}
3476 else
3477 {do ix^db=ixpmin^db,ixpmax^db\}
3478 {^iftwod
3479 if(w(ix^d,iw_mag(1))/=0.d0) then
3480 bunitvec(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)
3481 else
3482 bunitvec(1)=0.d0
3483 end if
3484 if(w(ix^d,iw_mag(2))/=0.d0) then
3485 bunitvec(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)
3486 else
3487 bunitvec(2)=0.d0
3488 end if
3489 }
3490 {^ifthreed
3491 if(w(ix^d,iw_mag(1))/=0.d0) then
3492 bunitvec(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+&
3493 (w(ix^d,iw_mag(3))/w(ix^d,iw_mag(1)))**2)
3494 else
3495 bunitvec(1)=0.d0
3496 end if
3497 if(w(ix^d,iw_mag(2))/=0.d0) then
3498 bunitvec(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+&
3499 (w(ix^d,iw_mag(3))/w(ix^d,iw_mag(2)))**2)
3500 else
3501 bunitvec(2)=0.d0
3502 end if
3503 if(w(ix^d,iw_mag(3))/=0.d0) then
3504 bunitvec(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+&
3505 (w(ix^d,iw_mag(2))/w(ix^d,iw_mag(3)))**2)
3506 else
3507 bunitvec(3)=0.d0
3508 end if
3509 }
3510 ! temperature length scale inversed
3511 lts(ix^d)=abs(^d&gradt({ix^d},^d)*bunitvec(^d)+)/te(ix^d)
3512 ! fraction of cells size to temperature length scale
3513 lts(ix^d)=min(^d&block%ds({ix^d},^d))*lts(ix^d)
3514 lts(ix^d)=max(one,(exp(lts(ix^d))/ltrc)**ltrp)
3515 {end do\}
3516 end if
3517
3518 ! need one ghost layer for thermal conductivity
3519 ixp^l=ixo^l^ladd1;
3520 {do ix^db=ixpmin^db,ixpmax^db\}
3521 {^iftwod
3522 altr=0.25d0*((lts(ix1-1,ix2)+two*lts(ix^d)+lts(ix1+1,ix2))*bunitvec(1)**2+&
3523 (lts(ix1,ix2-1)+two*lts(ix^d)+lts(ix1,ix2+1))*bunitvec(2)**2)
3524 block%wextra(ix^d,tcoff_)=te(ix^d)*altr**0.4d0
3525 }
3526 {^ifthreed
3527 altr=0.25d0*((lts(ix1-1,ix2,ix3)+two*lts(ix^d)+lts(ix1+1,ix2,ix3))*bunitvec(1)**2+&
3528 (lts(ix1,ix2-1,ix3)+two*lts(ix^d)+lts(ix1,ix2+1,ix3))*bunitvec(2)**2+&
3529 (lts(ix1,ix2,ix3-1)+two*lts(ix^d)+lts(ix1,ix2,ix3+1))*bunitvec(3)**2)
3530 block%wextra(ix^d,tcoff_)=te(ix^d)*altr**0.4d0
3531 }
3532 {end do\}
3533 case(7)
3534 ! Johnston et al. 2021 (A&A 654, A2) local field-aligned TRAC for MHD.
3535 ! Anisotropic conduction broadens the TR along B, so the length scale (Eq.18) and the
3536 ! enthalpy flux (Eq.16) are projected on the field, regularized by b_min=0.1 G (paper
3537 ! value) as B->0 or B perpendicular to grad T. Per-cell adaptive parallel conductivity from the
3538 ! steady-state balance (Eq.11/12) with the Eq.13 selection; general-EoS cooling n_e n_H L.
3539 ! (HD isotropic version used the grad-T direction; here it is the magnetic field.)
3540 call usr_get_heating(q_heat, ixi^l, ixo^l, w, x)
3541 call eos%get_ne_nH(ixi^l, ixo^l, w, x, ne, nh_arr)
3542 block%wextra(ixi^s,tcoff_) = te(ixi^s) ! default (incl. ghost layer): no broadening
3543 do idims=1,ndim
3544 call gradient(te,ixi^l,ixo^l,idims,gradt(ixi^s,idims))
3545 end do
3546 bmin_c = 0.1d0/unit_magneticfield ! 0.1 G in code units
3547 {do ix^db=ixomin^db,ixomax^db\}
3548 ! full field B-magnitude (Eq.18 numerator, regularized), and B.v, v-magnitude over ndir
3549 bmag=bmin_c**2; bdotv=0.d0; vmag=0.d0
3550 do idims=1,ndir
3551 bvec(idims)=w(ix^d,iw_mag(idims))
3552 if(b0field) bvec(idims)=bvec(idims)+block%B0(ix^d,idims,0)
3553 bmag =bmag +bvec(idims)**2
3554 bdotv=bdotv+bvec(idims)*w(ix^d,mom(idims))
3555 vmag =vmag +w(ix^d,mom(idims))**2
3556 end do
3557 bmag=dsqrt(bmag); vmag=dsqrt(vmag)
3558 ! in-plane grad-T norm, in-plane field^2, and in-plane B.grad T (grad T has no z-part in 2D)
3559 gnorm=0.d0; binp2=0.d0; bgtd=0.d0
3560 do idims=1,ndim
3561 gnorm=gnorm+gradt(ix^d,idims)**2
3562 binp2=binp2+bvec(idims)**2
3563 bgtd =bgtd +bvec(idims)*gradt(ix^d,idims)
3564 end do
3565 gnorm=dsqrt(gnorm)
3566 if(gnorm<smalldouble .or. binp2<smalldouble) then
3567 block%wextra(ix^d,tcoff_)=te(ix^d) ! uniform T or no in-plane field: no TR
3568 else
3569 ! Eq.18 field-aligned length scale (dabs: conduction is bidirectional along B)
3570 l_t=te(ix^d)*bmag/(dabs(bgtd)+bmin_c*gnorm)
3571 ! grid spacing along the (in-plane) field direction b = Bvec/Bmag
3572 dl_eff=0.d0
3573 do idims=1,ndim
3574 dl_eff=dl_eff+(bvec(idims)/block%ds(ix^d,idims))**2
3575 end do
3576 dl_eff=bmag/dsqrt(dl_eff)
3577 ! Eq.16 field-aligned enthalpy (mass) flux
3578 v_n=(dabs(bdotv)+bmin_c*vmag)/bmag
3579 a_coeff=2.5d0*w(ix^d,p_)*v_n/te(ix^d)
3580 ! optically-thin cooling n_e n_H Lambda(T), guarded to the table range (positive
3581 ! in-range test so a non-finite Te falls to zero rather than indexing findL with NaN)
3582 if(te(ix^d)>rc_fl%tcoolmin .and. te(ix^d)<rc_fl%tcoolmax) then
3583 call findl(te(ix^d),l1,rc_fl); cooling=l1*ne(ix^d)*nh_arr(ix^d)
3584 else if(te(ix^d)>=rc_fl%tcoolmax) then
3585 call calc_l_extended(te(ix^d),l1,rc_fl); cooling=l1*ne(ix^d)*nh_arr(ix^d)
3586 else
3587 cooling=0.d0
3588 end if
3589 net_cool=dabs(cooling-q_heat(ix^d))
3590 kappa_par=tc_fl%tc_k_para*te(ix^d)**2.5d0
3591 disc=a_coeff**2+4.d0*tc_fl%tc_k_para*te(ix^d)**1.5d0*net_cool
3592 dx_over_delta=dl_eff/mhd_trac_delta
3593 ! Eq.13 selection: under-resolved (Eq.11, keep mass flux) vs over-resolved (Eq.12, limiter)
3594 if(l_t<=2.d0*dx_over_delta) then
3595 kappa_trac=(a_coeff+dsqrt(disc))/(2.d0/dx_over_delta)
3596 else
3597 kappa_trac=dsqrt(4.d0*tc_fl%tc_k_para*te(ix^d)**1.5d0*net_cool)/(2.d0/dx_over_delta)
3598 end if
3599 kappa_eff=max(kappa_trac,kappa_par)
3600 tcoff_eff=(kappa_eff/tc_fl%tc_k_para)**0.4d0
3601 block%wextra(ix^d,tcoff_)=max(te(ix^d),tcoff_eff)
3602 end if
3603 {end do\}
3604 case(3,5)
3605 !> do nothing here
3606 case default
3607 call mpistop("unknown mhd_trac_type")
3608 end select
3609 }
3610 end subroutine mhd_get_tcutoff
3611
3612 !> get H speed for H-correction to fix the carbuncle problem at grid-aligned shock front
3613 subroutine mhd_get_h_speed(wprim,x,ixI^L,ixO^L,idim,Hspeed)
3615
3616 integer, intent(in) :: ixi^l, ixo^l, idim
3617 double precision, intent(in) :: wprim(ixi^s, nw)
3618 double precision, intent(in) :: x(ixi^s,1:ndim)
3619 double precision, intent(out) :: hspeed(ixi^s,1:number_species)
3620
3621 double precision :: csound(ixi^s,ndim)
3622 double precision, allocatable :: tmp(:^d&)
3623 integer :: jxc^l, ixc^l, ixa^l, id, ix^d
3624
3625 hspeed=0.d0
3626 ixa^l=ixo^l^ladd1;
3627 allocate(tmp(ixa^s))
3628 do id=1,ndim
3629 if(has_equi_rho_and_p) then
3630 call mhd_get_csound_prim_split(wprim,x,ixi^l,ixa^l,id,tmp)
3631 else
3632 call mhd_get_csound_prim(wprim,x,ixi^l,ixa^l,id,tmp)
3633 endif
3634 csound(ixa^s,id)=tmp(ixa^s)
3635 end do
3636 ixcmax^d=ixomax^d;
3637 ixcmin^d=ixomin^d+kr(idim,^d)-1;
3638 jxcmax^d=ixcmax^d+kr(idim,^d);
3639 jxcmin^d=ixcmin^d+kr(idim,^d);
3640 hspeed(ixc^s,1)=0.5d0*abs(wprim(jxc^s,mom(idim))+csound(jxc^s,idim)-wprim(ixc^s,mom(idim))+csound(ixc^s,idim))
3641
3642 do id=1,ndim
3643 if(id==idim) cycle
3644 ixamax^d=ixcmax^d+kr(id,^d);
3645 ixamin^d=ixcmin^d+kr(id,^d);
3646 hspeed(ixc^s,1)=max(hspeed(ixc^s,1),0.5d0*abs(wprim(ixa^s,mom(id))+csound(ixa^s,id)-wprim(ixc^s,mom(id))+csound(ixc^s,id)))
3647 ixamax^d=ixcmax^d-kr(id,^d);
3648 ixamin^d=ixcmin^d-kr(id,^d);
3649 hspeed(ixc^s,1)=max(hspeed(ixc^s,1),0.5d0*abs(wprim(ixc^s,mom(id))+csound(ixc^s,id)-wprim(ixa^s,mom(id))+csound(ixa^s,id)))
3650 end do
3651
3652 do id=1,ndim
3653 if(id==idim) cycle
3654 ixamax^d=jxcmax^d+kr(id,^d);
3655 ixamin^d=jxcmin^d+kr(id,^d);
3656 hspeed(ixc^s,1)=max(hspeed(ixc^s,1),0.5d0*abs(wprim(ixa^s,mom(id))+csound(ixa^s,id)-wprim(jxc^s,mom(id))+csound(jxc^s,id)))
3657 ixamax^d=jxcmax^d-kr(id,^d);
3658 ixamin^d=jxcmin^d-kr(id,^d);
3659 hspeed(ixc^s,1)=max(hspeed(ixc^s,1),0.5d0*abs(wprim(jxc^s,mom(id))+csound(jxc^s,id)-wprim(ixa^s,mom(id))+csound(ixa^s,id)))
3660 end do
3661 deallocate(tmp)
3662
3663 end subroutine mhd_get_h_speed
3664
3665 !> Estimating bounds for the minimum and maximum signal velocities without split
3666 subroutine mhd_get_cbounds(wLC,wRC,wLp,wRp,x,ixI^L,ixO^L,idim,Hspeed,cmax,cmin)
3668 !!use mod_fld, only: fld_bound_diff, fld_get_local_invtauc
3669
3670 integer, intent(in) :: ixi^l, ixo^l, idim
3671 double precision, intent(in) :: wlc(ixi^s, nw), wrc(ixi^s, nw)
3672 double precision, intent(in) :: wlp(ixi^s, nw), wrp(ixi^s, nw)
3673 double precision, intent(in) :: x(ixi^s,1:ndim)
3674 double precision, intent(inout) :: cmax(ixi^s,1:number_species)
3675 double precision, intent(inout), optional :: cmin(ixi^s,1:number_species)
3676 double precision, intent(in) :: hspeed(ixi^s,1:number_species)
3677
3678 double precision :: wmean(ixi^s,nw), csoundl(ixo^s), csoundr(ixo^s)
3679 !!double precision :: invtaucL(ixI^S),invtaucR(ixI^S),invtauc
3680 double precision :: umean, dmean, tmp1, tmp2, tmp3
3681 integer :: ix^d
3682
3683 select case (boundspeed)
3684 case (1)
3685 ! This implements formula (10.52) from "Riemann Solvers and Numerical
3686 ! Methods for Fluid Dynamics" by Toro.
3687 call mhd_get_csound_prim(wlp,x,ixi^l,ixo^l,idim,csoundl)
3688 call mhd_get_csound_prim(wrp,x,ixi^l,ixo^l,idim,csoundr)
3689 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3690 !! call fld_get_local_invtauc(wLp,ixI^L,ixO^L,{dxlevel(^D)},x,invtaucL,fld_fl)
3691 !! call fld_get_local_invtauc(wRp,ixI^L,ixO^L,{dxlevel(^D)},x,invtaucR,fld_fl)
3692 !!endif
3693 if(present(cmin)) then
3694 {do ix^db=ixomin^db,ixomax^db\}
3695 tmp1=sqrt(wlp(ix^d,rho_))
3696 tmp2=sqrt(wrp(ix^d,rho_))
3697 tmp3=1.d0/(tmp1+tmp2)
3698 umean=(wlp(ix^d,mom(idim))*tmp1+wrp(ix^d,mom(idim))*tmp2)*tmp3
3699 dmean=sqrt((tmp1*csoundl(ix^d)**2+tmp2*csoundr(ix^d)**2)*tmp3+&
3700 half*tmp1*tmp2*tmp3**2*(wrp(ix^d,mom(idim))-wlp(ix^d,mom(idim)))**2)
3701 cmin(ix^d,1)=umean-dmean
3702 cmax(ix^d,1)=umean+dmean
3703 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3704 !! invtauc=min(invtaucL(ix^D),invtaucR(ix^D))
3705 !! cmax(ix^D,1)=min(cmax(ix^D,1),+invtauc)
3706 !! cmin(ix^D,1)=max(cmin(ix^D,1),-invtauc)
3707 !!endif
3708 {end do\}
3709 if(h_correction) then
3710 {do ix^db=ixomin^db,ixomax^db\}
3711 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
3712 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
3713 {end do\}
3714 end if
3715 else
3716 {do ix^db=ixomin^db,ixomax^db\}
3717 tmp1=sqrt(wlp(ix^d,rho_))
3718 tmp2=sqrt(wrp(ix^d,rho_))
3719 tmp3=1.d0/(tmp1+tmp2)
3720 umean=(wlp(ix^d,mom(idim))*tmp1+wrp(ix^d,mom(idim))*tmp2)*tmp3
3721 dmean=sqrt((tmp1*csoundl(ix^d)**2+tmp2*csoundr(ix^d)**2)*tmp3+&
3722 half*tmp1*tmp2*tmp3**2*(wrp(ix^d,mom(idim))-wlp(ix^d,mom(idim)))**2)
3723 cmax(ix^d,1)=abs(umean)+dmean
3724 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3725 !! invtauc=min(invtaucL(ix^D),invtaucR(ix^D))
3726 !! cmax(ix^D,1)=min(cmax(ix^D,1),+invtauc)
3727 !!endif
3728 {end do\}
3729 end if
3730 case (2)
3731 wmean(ixo^s,1:nwflux)=0.5d0*(wlp(ixo^s,1:nwflux)+wrp(ixo^s,1:nwflux))
3732 call mhd_get_csound_prim(wmean,x,ixi^l,ixo^l,idim,csoundr)
3733 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3734 !! call fld_get_local_invtauc(wmean,ixI^L,ixO^L,{dxlevel(^D)},x,invtaucL,fld_fl)
3735 !!endif
3736 if(present(cmin)) then
3737 {do ix^db=ixomin^db,ixomax^db\}
3738 cmax(ix^d,1)=max(wmean(ix^d,mom(idim))+csoundr(ix^d),zero)
3739 cmin(ix^d,1)=min(wmean(ix^d,mom(idim))-csoundr(ix^d),zero)
3740 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3741 !! invtauc=invtaucL(ix^D)
3742 !! cmax(ix^D,1)=min(cmax(ix^D,1),+invtauc)
3743 !! cmin(ix^D,1)=max(cmin(ix^D,1),-invtauc)
3744 !!endif
3745 {end do\}
3746 if(h_correction) then
3747 {do ix^db=ixomin^db,ixomax^db\}
3748 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
3749 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
3750 {end do\}
3751 end if
3752 else
3753 cmax(ixo^s,1)=abs(wmean(ixo^s,mom(idim)))+csoundr(ixo^s)
3754 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3755 !! invtauc=invtaucL(ix^D)
3756 !! cmax(ix^D,1)=min(cmax(ix^D,1),+invtauc)
3757 !!endif
3758 end if
3759 case (3)
3760 ! Miyoshi 2005 JCP 208, 315 equation (67)
3761 call mhd_get_csound_prim(wlp,x,ixi^l,ixo^l,idim,csoundl)
3762 call mhd_get_csound_prim(wrp,x,ixi^l,ixo^l,idim,csoundr)
3763 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3764 !! call fld_get_local_invtauc(wLp,ixI^L,ixO^L,{dxlevel(^D)},x,invtaucL,fld_fl)
3765 !! call fld_get_local_invtauc(wRp,ixI^L,ixO^L,{dxlevel(^D)},x,invtaucR,fld_fl)
3766 !!endif
3767 if(present(cmin)) then
3768 {do ix^db=ixomin^db,ixomax^db\}
3769 csoundl(ix^d)=max(csoundl(ix^d),csoundr(ix^d))
3770 cmin(ix^d,1)=min(wlp(ix^d,mom(idim)),wrp(ix^d,mom(idim)))-csoundl(ix^d)
3771 cmax(ix^d,1)=max(wlp(ix^d,mom(idim)),wrp(ix^d,mom(idim)))+csoundl(ix^d)
3772 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3773 !! invtauc=min(invtaucL(ix^D),invtaucR(ix^D))
3774 !! cmax(ix^D,1)=min(cmax(ix^D,1),+invtauc)
3775 !! cmin(ix^D,1)=max(cmin(ix^D,1),-invtauc)
3776 !!endif
3777 {end do\}
3778 if(h_correction) then
3779 {do ix^db=ixomin^db,ixomax^db\}
3780 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
3781 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
3782 {end do\}
3783 end if
3784 else
3785 {do ix^db=ixomin^db,ixomax^db\}
3786 csoundl(ix^d)=max(csoundl(ix^d),csoundr(ix^d))
3787 cmax(ix^d,1)=max(wlp(ix^d,mom(idim)),wrp(ix^d,mom(idim)))+csoundl(ix^d)
3788 !!if(mhd_radiation_fld.and.fld_bound_diff)then
3789 !! invtauc=min(invtaucL(ix^D),invtaucR(ix^D))
3790 !! cmax(ix^D,1)=min(cmax(ix^D,1),+invtauc)
3791 !!endif
3792 {end do\}
3793 end if
3794 end select
3795
3796 end subroutine mhd_get_cbounds
3797
3798 !> Estimating bounds for the minimum and maximum signal velocities without split
3799 subroutine mhd_get_cbounds_semirelati(wLC,wRC,wLp,wRp,x,ixI^L,ixO^L,idim,Hspeed,cmax,cmin)
3801
3802 integer, intent(in) :: ixi^l, ixo^l, idim
3803 double precision, intent(in) :: wlc(ixi^s, nw), wrc(ixi^s, nw)
3804 double precision, intent(in) :: wlp(ixi^s, nw), wrp(ixi^s, nw)
3805 double precision, intent(in) :: x(ixi^s,1:ndim)
3806 double precision, intent(inout) :: cmax(ixi^s,1:number_species)
3807 double precision, intent(inout), optional :: cmin(ixi^s,1:number_species)
3808 double precision, intent(in) :: hspeed(ixi^s,1:number_species)
3809
3810 double precision, dimension(ixO^S) :: csoundl, csoundr, gamma2l, gamma2r
3811 integer :: ix^d
3812
3813 ! Miyoshi 2005 JCP 208, 315 equation (67)
3814 if(mhd_energy) then
3815 call mhd_get_csound_semirelati(wlp,x,ixi^l,ixo^l,idim,csoundl,gamma2l)
3816 call mhd_get_csound_semirelati(wrp,x,ixi^l,ixo^l,idim,csoundr,gamma2r)
3817 else
3818 call mhd_get_csound_semirelati_noe(wlp,x,ixi^l,ixo^l,idim,csoundl,gamma2l)
3819 call mhd_get_csound_semirelati_noe(wrp,x,ixi^l,ixo^l,idim,csoundr,gamma2r)
3820 end if
3821 if(present(cmin)) then
3822 {do ix^db=ixomin^db,ixomax^db\}
3823 csoundl(ix^d)=max(csoundl(ix^d),csoundr(ix^d))
3824 cmin(ix^d,1)=min(gamma2l(ix^d)*wlp(ix^d,mom(idim)),gamma2r(ix^d)*wrp(ix^d,mom(idim)))-csoundl(ix^d)
3825 cmax(ix^d,1)=max(gamma2l(ix^d)*wlp(ix^d,mom(idim)),gamma2r(ix^d)*wrp(ix^d,mom(idim)))+csoundl(ix^d)
3826 {end do\}
3827 else
3828 {do ix^db=ixomin^db,ixomax^db\}
3829 csoundl(ix^d)=max(csoundl(ix^d),csoundr(ix^d))
3830 cmax(ix^d,1)=max(gamma2l(ix^d)*wlp(ix^d,mom(idim)),gamma2r(ix^d)*wrp(ix^d,mom(idim)))+csoundl(ix^d)
3831 {end do\}
3832 end if
3833
3834 end subroutine mhd_get_cbounds_semirelati
3835
3836 !> Estimating bounds for the minimum and maximum signal velocities with rho split
3837 subroutine mhd_get_cbounds_split_rho(wLC,wRC,wLp,wRp,x,ixI^L,ixO^L,idim,Hspeed,cmax,cmin)
3839
3840 integer, intent(in) :: ixi^l, ixo^l, idim
3841 double precision, intent(in) :: wlc(ixi^s, nw), wrc(ixi^s, nw)
3842 double precision, intent(in) :: wlp(ixi^s, nw), wrp(ixi^s, nw)
3843 double precision, intent(in) :: x(ixi^s,1:ndim)
3844 double precision, intent(inout) :: cmax(ixi^s,1:number_species)
3845 double precision, intent(inout), optional :: cmin(ixi^s,1:number_species)
3846 double precision, intent(in) :: hspeed(ixi^s,1:number_species)
3847
3848 double precision :: wmean(ixi^s,nw), csoundl(ixo^s), csoundr(ixo^s)
3849 double precision :: umean, dmean, tmp1, tmp2, tmp3
3850 integer :: ix^d
3851
3852 select case (boundspeed)
3853 case (1)
3854 ! This implements formula (10.52) from "Riemann Solvers and Numerical
3855 ! Methods for Fluid Dynamics" by Toro.
3856 call mhd_get_csound_prim_split(wlp,x,ixi^l,ixo^l,idim,csoundl)
3857 call mhd_get_csound_prim_split(wrp,x,ixi^l,ixo^l,idim,csoundr)
3858 if(present(cmin)) then
3859 {do ix^db=ixomin^db,ixomax^db\}
3860 tmp1=sqrt(wlp(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
3861 tmp2=sqrt(wrp(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
3862 tmp3=1.d0/(tmp1+tmp2)
3863 umean=(wlp(ix^d,mom(idim))*tmp1+wrp(ix^d,mom(idim))*tmp2)*tmp3
3864 dmean=sqrt((tmp1*csoundl(ix^d)**2+tmp2*csoundr(ix^d)**2)*tmp3+&
3865 half*tmp1*tmp2*tmp3**2*(wrp(ix^d,mom(idim))-wlp(ix^d,mom(idim)))**2)
3866 cmin(ix^d,1)=umean-dmean
3867 cmax(ix^d,1)=umean+dmean
3868 {end do\}
3869 if(h_correction) then
3870 {do ix^db=ixomin^db,ixomax^db\}
3871 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
3872 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
3873 {end do\}
3874 end if
3875 else
3876 {do ix^db=ixomin^db,ixomax^db\}
3877 tmp1=sqrt(wlp(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
3878 tmp2=sqrt(wrp(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
3879 tmp3=1.d0/(tmp1+tmp2)
3880 umean=(wlp(ix^d,mom(idim))*tmp1+wrp(ix^d,mom(idim))*tmp2)*tmp3
3881 dmean=sqrt((tmp1*csoundl(ix^d)**2+tmp2*csoundr(ix^d)**2)*tmp3+&
3882 half*tmp1*tmp2*tmp3**2*(wrp(ix^d,mom(idim))-wlp(ix^d,mom(idim)))**2)
3883 cmax(ix^d,1)=abs(umean)+dmean
3884 {end do\}
3885 end if
3886 case (2)
3887 wmean(ixo^s,1:nwflux)=0.5d0*(wlp(ixo^s,1:nwflux)+wrp(ixo^s,1:nwflux))
3888 call mhd_get_csound_prim_split(wmean,x,ixi^l,ixo^l,idim,csoundr)
3889 if(present(cmin)) then
3890 {do ix^db=ixomin^db,ixomax^db\}
3891 cmax(ix^d,1)=max(wmean(ix^d,mom(idim))+csoundr(ix^d),zero)
3892 cmin(ix^d,1)=min(wmean(ix^d,mom(idim))-csoundr(ix^d),zero)
3893 {end do\}
3894 if(h_correction) then
3895 {do ix^db=ixomin^db,ixomax^db\}
3896 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
3897 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
3898 {end do\}
3899 end if
3900 else
3901 cmax(ixo^s,1)=abs(wmean(ixo^s,mom(idim)))+csoundr(ixo^s)
3902 end if
3903 case (3)
3904 ! Miyoshi 2005 JCP 208, 315 equation (67)
3905 call mhd_get_csound_prim_split(wlp,x,ixi^l,ixo^l,idim,csoundl)
3906 call mhd_get_csound_prim_split(wrp,x,ixi^l,ixo^l,idim,csoundr)
3907 if(present(cmin)) then
3908 {do ix^db=ixomin^db,ixomax^db\}
3909 csoundl(ix^d)=max(csoundl(ix^d),csoundr(ix^d))
3910 cmin(ix^d,1)=min(wlp(ix^d,mom(idim)),wrp(ix^d,mom(idim)))-csoundl(ix^d)
3911 cmax(ix^d,1)=max(wlp(ix^d,mom(idim)),wrp(ix^d,mom(idim)))+csoundl(ix^d)
3912 {end do\}
3913 if(h_correction) then
3914 {do ix^db=ixomin^db,ixomax^db\}
3915 cmin(ix^d,1)=sign(one,cmin(ix^d,1))*max(abs(cmin(ix^d,1)),hspeed(ix^d,1))
3916 cmax(ix^d,1)=sign(one,cmax(ix^d,1))*max(abs(cmax(ix^d,1)),hspeed(ix^d,1))
3917 {end do\}
3918 end if
3919 else
3920 {do ix^db=ixomin^db,ixomax^db\}
3921 csoundl(ix^d)=max(csoundl(ix^d),csoundr(ix^d))
3922 cmax(ix^d,1)=max(wlp(ix^d,mom(idim)),wrp(ix^d,mom(idim)))+csoundl(ix^d)
3923 {end do\}
3924 end if
3925 end select
3926
3927 end subroutine mhd_get_cbounds_split_rho
3928
3929 !> prepare velocities for ct methods
3930 subroutine mhd_get_ct_velocity_average(vcts,wLp,wRp,ixI^L,ixO^L,idim,cmax,cmin)
3932
3933 integer, intent(in) :: ixi^l, ixo^l, idim
3934 double precision, intent(in) :: wlp(ixi^s, nw), wrp(ixi^s, nw)
3935 double precision, intent(in) :: cmax(ixi^s)
3936 double precision, intent(in), optional :: cmin(ixi^s)
3937 type(ct_velocity), intent(inout):: vcts
3938
3939 end subroutine mhd_get_ct_velocity_average
3940
3941 subroutine mhd_get_ct_velocity_contact(vcts,wLp,wRp,ixI^L,ixO^L,idim,cmax,cmin)
3943
3944 integer, intent(in) :: ixi^l, ixo^l, idim
3945 double precision, intent(in) :: wlp(ixi^s, nw), wrp(ixi^s, nw)
3946 double precision, intent(in) :: cmax(ixi^s)
3947 double precision, intent(in), optional :: cmin(ixi^s)
3948 type(ct_velocity), intent(inout):: vcts
3949
3950 if(.not.allocated(vcts%vnorm)) allocate(vcts%vnorm(ixi^s,1:ndim))
3951 ! get average normal velocity at cell faces
3952 vcts%vnorm(ixo^s,idim)=0.5d0*(wlp(ixo^s,mom(idim))+wrp(ixo^s,mom(idim)))
3953
3954 end subroutine mhd_get_ct_velocity_contact
3955
3956 subroutine mhd_get_ct_velocity_hll(vcts,wLp,wRp,ixI^L,ixO^L,idim,cmax,cmin)
3958
3959 integer, intent(in) :: ixi^l, ixo^l, idim
3960 double precision, intent(in) :: wlp(ixi^s, nw), wrp(ixi^s, nw)
3961 double precision, intent(in) :: cmax(ixi^s)
3962 double precision, intent(in), optional :: cmin(ixi^s)
3963 type(ct_velocity), intent(inout):: vcts
3964
3965 integer :: idime,idimn
3966
3967 if(.not.allocated(vcts%vbarC)) then
3968 allocate(vcts%vbarC(ixi^s,1:ndir,2),vcts%vbarLC(ixi^s,1:ndir,2),vcts%vbarRC(ixi^s,1:ndir,2))
3969 allocate(vcts%cbarmin(ixi^s,1:ndim),vcts%cbarmax(ixi^s,1:ndim))
3970 end if
3971 ! Store magnitude of characteristics
3972 if(present(cmin)) then
3973 vcts%cbarmin(ixo^s,idim)=max(-cmin(ixo^s),zero)
3974 vcts%cbarmax(ixo^s,idim)=max( cmax(ixo^s),zero)
3975 else
3976 vcts%cbarmax(ixo^s,idim)=max( cmax(ixo^s),zero)
3977 vcts%cbarmin(ixo^s,idim)=vcts%cbarmax(ixo^s,idim)
3978 end if
3979
3980 idimn=mod(idim,ndir)+1 ! 'Next' direction
3981 idime=mod(idim+1,ndir)+1 ! Electric field direction
3982 ! Store velocities
3983 vcts%vbarLC(ixo^s,idim,1)=wlp(ixo^s,mom(idimn))
3984 vcts%vbarRC(ixo^s,idim,1)=wrp(ixo^s,mom(idimn))
3985 vcts%vbarC(ixo^s,idim,1)=(vcts%cbarmax(ixo^s,idim)*vcts%vbarLC(ixo^s,idim,1) &
3986 +vcts%cbarmin(ixo^s,idim)*vcts%vbarRC(ixo^s,idim,1))&
3987 /(vcts%cbarmax(ixo^s,idim)+vcts%cbarmin(ixo^s,idim))
3988
3989 vcts%vbarLC(ixo^s,idim,2)=wlp(ixo^s,mom(idime))
3990 vcts%vbarRC(ixo^s,idim,2)=wrp(ixo^s,mom(idime))
3991 vcts%vbarC(ixo^s,idim,2)=(vcts%cbarmax(ixo^s,idim)*vcts%vbarLC(ixo^s,idim,2) &
3992 +vcts%cbarmin(ixo^s,idim)*vcts%vbarRC(ixo^s,idim,2))&
3993 /(vcts%cbarmax(ixo^s,idim)+vcts%cbarmin(ixo^s,idim))
3994
3995 end subroutine mhd_get_ct_velocity_hll
3996
3997 !> Calculate modified squared sound speed for FLD
3998 !> NOTE: only for diagnostic purposes, unused subroutine
3999 subroutine mhd_get_csrad2(w,x,ixI^L,ixO^L,csound)
4001
4002 integer, intent(in) :: ixi^l, ixo^l
4003 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
4004 double precision, intent(out):: csound(ixi^s)
4005
4006 double precision :: wprim(ixi^s, nw)
4007
4008 wprim(ixi^s,1:nw)=w(ixi^s,1:nw)
4009 call eos%to_primitive(ixi^l,ixo^l,wprim,x)
4010 call mhd_get_csrad2_prim(wprim,x,ixi^l,ixo^l,csound)
4011
4012 end subroutine mhd_get_csrad2
4013
4014
4015 !> Calculate modified squared fast wave speed for FLD
4016 !> NOTE: w is primitive on entry here!
4017 !> NOTE: used in FLD module as phys_get_csrad2
4018 subroutine mhd_get_csrad2_prim(w,x,ixI^L,ixO^L,csound)
4020
4021 integer, intent(in) :: ixi^l, ixo^l
4022 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
4023 double precision, intent(out):: csound(ixi^s)
4024
4025 double precision :: inv_rho, b2
4026 double precision :: prad_tensor(ixi^s, 1:ndim, 1:ndim)
4027 double precision :: prad_max(ixi^s)
4028 integer :: ix^d,idim
4029
4030 if(mhd_fld_pradtensor) then
4031 call mhd_get_pradiation_from_prim(w, x, ixi^l, ixo^l, prad_tensor)
4032 else
4033 prad_tensor=zero
4034 do idim=1,ndim
4035 prad_tensor(ixo^s,idim,idim)=w(ixo^s,r_e)/3.0d0
4036 enddo
4037 endif
4038
4039 if(b0field) then
4040 {do ix^db=ixomin^db,ixomax^db \}
4041 inv_rho=1.d0/w(ix^d,rho_)
4042 prad_max(ix^d) = (4.0d0/3.0d0)*maxval(prad_tensor(ix^d,:,:))
4043 b2=(^c&(w(ix^d,b^c_)+block%B0(ix^d,^c,b0i))**2+)
4044 csound(ix^d)=(eos%gamma*w(ix^d,p_)+b2+prad_max(ix^d))*inv_rho
4045 {end do\}
4046 else
4047 {do ix^db=ixomin^db,ixomax^db \}
4048 inv_rho=1.d0/w(ix^d,rho_)
4049 prad_max(ix^d) = (4.0d0/3.0d0)*maxval(prad_tensor(ix^d,:,:))
4050 b2=(^c&w(ix^d,b^c_)**2+)
4051 csound(ix^d)=(eos%gamma*w(ix^d,p_)+b2+prad_max(ix^d))*inv_rho
4052 {end do\}
4053 end if
4054
4055 if(minval(csound(ixo^s))<smalldouble)then
4056 print *,'issue with squared speed and rad pressure'
4057 print *,minval(csound(ixo^s))
4058 print *,minval(prad_max(ixo^s))
4059 call mpistop("negative squared speed in get_csrad2 for dt")
4060 endif
4061
4062 end subroutine mhd_get_csrad2_prim
4063
4064 !> Calculate fast magnetosonic wave speed
4065 subroutine mhd_get_csound_prim(w,x,ixI^L,ixO^L,idim,csound)
4068 use mod_fld, only: fld_bound_diff
4069
4070 integer, intent(in) :: ixi^l, ixo^l, idim
4071 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
4072 double precision, intent(out):: csound(ixo^s)
4073
4074 double precision :: adiabs(ixi^s), gammas(ixi^s)
4075 double precision :: inv_rho, cfast2, avmincs2, b2, kmax
4076 double precision :: cs2(ixi^s)
4077 integer :: ix^d
4078
4079 if(mhd_uawsom) then
4080 call mhd_uawsom_get_signal_speed(w,x,ixi^l,ixo^l,idim,csound)
4081 return
4082 end if
4083
4084 if(mhd_hall) kmax = dpi/min({dxlevel(^d)},bigdouble)*half
4085
4086 if(mhd_energy) then
4087 ! Sound speed squared via EoS dispatch (honours LTE+ionE Gamma_1 table).
4088 call eos%get_csound2(w, x, ixi^l, ixo^l, cs2)
4089 else
4090 if(associated(usr_set_adiab)) then
4091 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
4092 else
4093 adiabs=mhd_adiab
4094 end if
4095 if(associated(usr_set_gamma)) then
4096 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
4097 else
4098 gammas=eos%gamma
4099 end if
4100 end if
4101
4102 ! store |B|^2 in v
4103 if(b0field) then
4104 {do ix^db=ixomin^db,ixomax^db \}
4105 inv_rho=1.d0/w(ix^d,rho_)
4106 if(mhd_energy) then
4107 if(mhd_radiation_fld.and.fld_bound_diff)cs2(ix^d)=cs2(ix^d)+4.0d0*w(ix^d,r_e)*inv_rho/9.0d0
4108 else
4109 cs2(ix^d)=gammas(ix^d)*adiabs(ix^d)*w(ix^d,rho_)**(gammas(ix^d)-1.d0)
4110 end if
4111 b2=(^c&(w(ix^d,b^c_)+block%B0(ix^d,^c,b0i))**2+)
4112 cfast2=b2*inv_rho+cs2(ix^d)
4113 avmincs2=cfast2**2-4.0d0*cs2(ix^d)*(w(ix^d,mag(idim))+&
4114 block%B0(ix^d,idim,b0i))**2*inv_rho
4115 if(avmincs2<zero) avmincs2=zero
4116 csound(ix^d)=sqrt(half*(cfast2+sqrt(avmincs2)))
4117 if(mhd_hall) then
4118 csound(ix^d)=max(csound(ix^d),mhd_etah*sqrt(b2)*inv_rho*kmax)
4119 end if
4120 {end do\}
4121 else
4122 {do ix^db=ixomin^db,ixomax^db \}
4123 inv_rho=1.d0/w(ix^d,rho_)
4124 if(mhd_energy) then
4125 if(mhd_radiation_fld.and.fld_bound_diff)cs2(ix^d)=cs2(ix^d)+4.0d0*w(ix^d,r_e)*inv_rho/9.0d0
4126 else
4127 cs2(ix^d)=gammas(ix^d)*adiabs(ix^d)*w(ix^d,rho_)**(gammas(ix^d)-1.d0)
4128 end if
4129 b2=(^c&w(ix^d,b^c_)**2+)
4130 cfast2=b2*inv_rho+cs2(ix^d)
4131 avmincs2=cfast2**2-4.0d0*cs2(ix^d)*w(ix^d,mag(idim))**2*inv_rho
4132 if(avmincs2<zero) avmincs2=zero
4133 csound(ix^d)=sqrt(half*(cfast2+sqrt(avmincs2)))
4134 if(mhd_hall) then
4135 csound(ix^d)=max(csound(ix^d),mhd_etah*sqrt(b2)*inv_rho*kmax)
4136 end if
4137 {end do\}
4138 end if
4139
4140 end subroutine mhd_get_csound_prim
4141
4142 !> Calculate fast magnetosonic wave speed when rho and p are split
4143 !> hence has_equi_rho_and_p=T
4144 subroutine mhd_get_csound_prim_split(w,x,ixI^L,ixO^L,idim,csound)
4146
4147 integer, intent(in) :: ixi^l, ixo^l, idim
4148 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
4149 double precision, intent(out):: csound(ixo^s)
4150
4151 double precision :: rho, inv_rho, cfast2, avmincs2, b2, kmax
4152 integer :: ix^d
4153
4154 if(mhd_hall) kmax = dpi/min({dxlevel(^d)},bigdouble)*half
4155
4156 ! store |B|^2 in v
4157 if(b0field) then
4158 {do ix^db=ixomin^db,ixomax^db \}
4159 rho=(w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
4160 inv_rho=1.d0/rho
4161 csound(ix^d)=eos%gamma*(w(ix^d,p_)+block%equi_vars(ix^d,equi_pe0_,b0i))*inv_rho
4162 b2=(^c&(w(ix^d,b^c_)+block%B0(ix^d,^c,b0i))**2+)
4163 cfast2=b2*inv_rho+csound(ix^d)
4164 avmincs2=cfast2**2-4.0d0*csound(ix^d)*(w(ix^d,mag(idim))+&
4165 block%B0(ix^d,idim,b0i))**2*inv_rho
4166 if(avmincs2<zero) avmincs2=zero
4167 csound(ix^d)=sqrt(half*(cfast2+sqrt(avmincs2)))
4168 if(mhd_hall) then
4169 csound(ix^d)=max(csound(ix^d),mhd_etah*sqrt(b2)*inv_rho*kmax)
4170 end if
4171 {end do\}
4172 else
4173 {do ix^db=ixomin^db,ixomax^db \}
4174 rho=(w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
4175 inv_rho=1.d0/rho
4176 csound(ix^d)=eos%gamma*(w(ix^d,p_)+block%equi_vars(ix^d,equi_pe0_,b0i))*inv_rho
4177 b2=(^c&w(ix^d,b^c_)**2+)
4178 cfast2=b2*inv_rho+csound(ix^d)
4179 avmincs2=cfast2**2-4.0d0*csound(ix^d)*w(ix^d,mag(idim))**2*inv_rho
4180 if(avmincs2<zero) avmincs2=zero
4181 csound(ix^d)=sqrt(half*(cfast2+sqrt(avmincs2)))
4182 if(mhd_hall) then
4183 csound(ix^d)=max(csound(ix^d),mhd_etah*sqrt(b2)*inv_rho*kmax)
4184 end if
4185 {end do\}
4186 end if
4187
4188 end subroutine mhd_get_csound_prim_split
4189
4190 !> Calculate cmax_idim for semirelativistic MHD
4191 subroutine mhd_get_csound_semirelati(w,x,ixI^L,ixO^L,idim,csound,gamma2)
4193
4194 integer, intent(in) :: ixi^l, ixo^l, idim
4195 ! here w is primitive variables
4196 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
4197 double precision, intent(out):: csound(ixo^s), gamma2(ixo^s)
4198
4199 double precision :: avmincs2, inv_rho, alfven_speed2, idim_alfven_speed2
4200 double precision :: cs2(ixi^s)
4201 integer :: ix^d
4202
4203 ! Sound speed squared via EoS dispatch (LTE+ionE -> Gamma_1 table; FI -> const gamma).
4204 call eos%get_csound2(w, x, ixi^l, ixo^l, cs2)
4205
4206 {do ix^db=ixomin^db,ixomax^db\}
4207 inv_rho = 1.d0/w(ix^d,rho_)
4208 ! V_A and V_A,idim take the total field
4209 if(b0field) then
4210 alfven_speed2=(^c&(w(ix^d,b^c_)+block%B0(ix^d,^c,b0i))**2+)*inv_rho
4211 idim_alfven_speed2=(w(ix^d,mag(idim))+block%B0(ix^d,idim,b0i))**2*inv_rho
4212 else
4213 alfven_speed2=(^c&w(ix^d,b^c_)**2+)*inv_rho
4214 idim_alfven_speed2=w(ix^d,mag(idim))**2*inv_rho
4215 end if
4216 gamma2(ix^d) = 1.0d0/(1.d0+alfven_speed2*inv_squared_c)
4217 avmincs2=1.d0-gamma2(ix^d)*w(ix^d,mom(idim))**2*inv_squared_c
4218 ! Va_hat^2+a_hat^2 equation (57)
4219 ! equation (69)
4220 alfven_speed2=alfven_speed2*avmincs2+cs2(ix^d)*(1.d0+idim_alfven_speed2*inv_squared_c)
4221 avmincs2=(gamma2(ix^d)*alfven_speed2)**2-4.0d0*gamma2(ix^d)*cs2(ix^d)*idim_alfven_speed2*avmincs2
4222 if(avmincs2<zero) avmincs2=zero
4223 ! equation (68) fast magnetosonic speed
4224 csound(ix^d) = sqrt(half*(gamma2(ix^d)*alfven_speed2+sqrt(avmincs2)))
4225 {end do\}
4226
4227 end subroutine mhd_get_csound_semirelati
4228
4229 !> Calculate cmax_idim for semirelativistic MHD
4230 subroutine mhd_get_csound_semirelati_noe(w,x,ixI^L,ixO^L,idim,csound,gamma2)
4233
4234 integer, intent(in) :: ixi^l, ixo^l, idim
4235 ! here w is primitive variables
4236 double precision, intent(in) :: w(ixi^s, nw), x(ixi^s,1:ndim)
4237 double precision, intent(out):: csound(ixo^s), gamma2(ixo^s)
4238
4239 double precision :: adiabs(ixi^s), gammas(ixi^s)
4240 double precision :: avmincs2, inv_rho, alfven_speed2, idim_alfven_speed2
4241 integer :: ix^d
4242
4243 if(associated(usr_set_adiab)) then
4244 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
4245 else
4246 adiabs=mhd_adiab
4247 end if
4248 if(associated(usr_set_gamma)) then
4249 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
4250 else
4251 gammas=eos%gamma
4252 end if
4253 {do ix^db=ixomin^db,ixomax^db\}
4254 inv_rho = 1.d0/w(ix^d,rho_)
4255 ! squared sound speed
4256 csound(ix^d)=gammas(ix^d)*adiabs(ix^d)*w(ix^d,rho_)**(gammas(ix^d)-1.d0)
4257 alfven_speed2=(^c&w(ix^d,b^c_)**2+)*inv_rho
4258 gamma2(ix^d) = 1.0d0/(1.d0+alfven_speed2*inv_squared_c)
4259 avmincs2=1.d0-gamma2(ix^d)*w(ix^d,mom(idim))**2*inv_squared_c
4260 idim_alfven_speed2=w(ix^d,mag(idim))**2*inv_rho
4261 ! Va_hat^2+a_hat^2 equation (57)
4262 ! equation (69)
4263 alfven_speed2=alfven_speed2*avmincs2+csound(ix^d)*(1.d0+idim_alfven_speed2*inv_squared_c)
4264 avmincs2=(gamma2(ix^d)*alfven_speed2)**2-4.0d0*gamma2(ix^d)*csound(ix^d)*idim_alfven_speed2*avmincs2
4265 if(avmincs2<zero) avmincs2=zero
4266 ! equation (68) fast magnetosonic speed
4267 csound(ix^d) = sqrt(half*(gamma2(ix^d)*alfven_speed2+sqrt(avmincs2)))
4268 {end do\}
4269
4270 end subroutine mhd_get_csound_semirelati_noe
4271
4272 ! Thermal pressure and temperature subroutines (mhd_get_pthermal_noe,
4273 ! _inte, _origin, _semirelati, _hde, _LTE, mhd_get_temperature_from_eint,
4274 ! _from_etot, _from_etot_LTE, _with_equi, _equi, mhd_get_rho_equi,
4275 ! mhd_get_pe_equi) are defined in mod_mhd_eos.t. eos%get_temperature_from_{etot,eint}
4276 ! and tc_fl / rc_fl / te_fl_mhd hooks are bound by bind_eos_to_source.
4277
4278
4279 !> Calculate radiation pressure within ixO^L
4280 subroutine mhd_get_pradiation_from_prim(w, x, ixI^L, ixO^L, prad)
4282 use mod_fld
4283 integer, intent(in) :: ixi^l, ixo^l
4284 double precision, intent(in) :: w(ixi^s, 1:nw)
4285 double precision, intent(in) :: x(ixi^s, 1:ndim)
4286 double precision, intent(out):: prad(ixi^s, 1:ndim, 1:ndim)
4287
4288 call fld_get_radpress(w, x, ixi^l, ixo^l, prad, fld_fl)
4289
4290 end subroutine mhd_get_pradiation_from_prim
4291
4292 !> Calculates the sum of the gas pressure and the max Prad tensor element
4293 subroutine mhd_get_pthermal_plus_pradiation(w, x, ixI^L, ixO^L, pth_plus_prad)
4295 integer, intent(in) :: ixi^l, ixo^l
4296 double precision, intent(in) :: w(ixi^s, 1:nw)
4297 double precision, intent(in) :: x(ixi^s, 1:ndim)
4298 double precision, intent(out) :: pth_plus_prad(ixi^s)
4299
4300 double precision :: wprim(ixi^s, 1:nw)
4301 double precision :: prad_tensor(ixi^s, 1:ndim, 1:ndim)
4302 double precision :: prad_max(ixi^s)
4303 integer :: ix^d
4304
4305 wprim(ixi^s,1:nw)=w(ixi^s,1:nw)
4306 call eos%to_primitive(ixi^l,ixo^l,wprim,x)
4307 call mhd_get_pradiation_from_prim(wprim, x, ixi^l, ixo^l, prad_tensor)
4308 {do ix^d = ixomin^d,ixomax^d\}
4309 prad_max(ix^d) = maxval(prad_tensor(ix^d,:,:))
4310 {enddo\}
4311 pth_plus_prad(ixo^s) = wprim(ixo^s,p_) + prad_max(ixo^s)
4313
4314 !> Calculates radiation temperature
4315 subroutine mhd_get_trad(w, x, ixI^L, ixO^L, trad)
4317 use mod_constants
4318
4319 integer, intent(in) :: ixi^l, ixo^l
4320 double precision, intent(in) :: w(ixi^s, 1:nw)
4321 double precision, intent(in) :: x(ixi^s, 1:ndim)
4322 double precision, intent(out):: trad(ixi^s)
4323
4324 trad(ixi^s) = (w(ixi^s,r_e)/arad_norm)**(1.d0/4.d0)
4325
4326 end subroutine mhd_get_trad
4327
4328 !> Calculate fluxes within ixO^L without any splitting
4329 subroutine mhd_get_flux(wC,w,x,ixI^L,ixO^L,idim,f)
4331 use mod_geometry
4332
4333 integer, intent(in) :: ixi^l, ixo^l, idim
4334 ! conservative w
4335 double precision, intent(in) :: wc(ixi^s,nw)
4336 ! primitive w
4337 double precision, intent(in) :: w(ixi^s,nw)
4338 double precision, intent(in) :: x(ixi^s,1:ndim)
4339 double precision,intent(out) :: f(ixi^s,nwflux)
4340
4341 double precision :: vhall(ixi^s,1:ndir)
4342 double precision :: ptotal
4343 double precision :: nperp(ixi^s,1:ndir)
4344 double precision :: uawsom_zeta(ixi^s), uawsom_radius(ixi^s)
4345 double precision :: uawsom_lperp(ixi^s), uawsom_denom
4346 double precision :: uawsom_pwave, uawsom_bi
4347 double precision :: bunit_tc(ndir)
4348 integer :: iw, ix^d
4349
4350 if(mhd_uawsom) call mhd_uawsom_get_coefficients(w,x,ixi^l,ixo^l,.true.,&
4351 uawsom_zeta,uawsom_radius,uawsom_lperp)
4352
4353 if(mhd_internal_e) then
4354 {do ix^db=ixomin^db,ixomax^db\}
4355 ! Get flux of density
4356 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
4357 ! f_i[m_k]=v_i*m_k-b_k*b_i
4358 ^c&f(ix^d,m^c_)=wc(ix^d,mom(idim))*w(ix^d,m^c_)-w(ix^d,mag(idim))*w(ix^d,b^c_)\
4359 ! normal one includes total pressure
4360 f(ix^d,mom(idim))=f(ix^d,mom(idim))+w(ix^d,p_)+half*(^c&w(ix^d,b^c_)**2+)
4361 ! Get flux of internal energy
4362 f(ix^d,e_)=w(ix^d,mom(idim))*wc(ix^d,e_)
4363 ! f_i[b_k]=v_i*b_k-v_k*b_i
4364 ^c&f(ix^d,b^c_)=w(ix^d,mom(idim))*w(ix^d,b^c_)-w(ix^d,mag(idim))*w(ix^d,m^c_)\
4365 {end do\}
4366 else
4367 {do ix^db=ixomin^db,ixomax^db\}
4368 ! Get flux of density
4369 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
4370 ! f_i[m_k]=v_i*m_k-b_k*b_i
4371 ^c&f(ix^d,m^c_)=wc(ix^d,mom(idim))*w(ix^d,m^c_)-w(ix^d,mag(idim))*w(ix^d,b^c_)\
4372 ptotal=w(ix^d,p_)+half*(^c&w(ix^d,b^c_)**2+)
4373 if(mhd_uawsom) then
4374 uawsom_pwave=mhd_uawsom_wave_pressure_cell(w(ix^d,:),uawsom_zeta(ix^d))
4375 ptotal=ptotal+uawsom_pwave
4376 end if
4377 ! normal one includes total pressure
4378 f(ix^d,mom(idim))=f(ix^d,mom(idim))+ptotal
4379 ! Get flux of total energy
4380 ! f_i[e]=v_i*e+v_i*ptotal-b_i*(b_k*v_k)
4381 f(ix^d,e_)=w(ix^d,mom(idim))*(wc(ix^d,e_)+ptotal)&
4382 -w(ix^d,mag(idim))*(^c&w(ix^d,b^c_)*w(ix^d,m^c_)+)
4383 if(mhd_uawsom) then
4384 uawsom_denom=dsqrt(w(ix^d,rho_)*(uawsom_zeta(ix^d)+one)/&
4385 (two*(one+mhd_uawsom_filling_factor*uawsom_zeta(ix^d)-&
4387 uawsom_bi=w(ix^d,mag(idim))
4388 f(ix^d,e_)=f(ix^d,e_)+uawsom_bi*(&
4389 (w(ix^d,waminus_)-w(ix^d,waplus_))/dsqrt(w(ix^d,rho_))+&
4390 (w(ix^d,wkminus_)-w(ix^d,wkplus_))/uawsom_denom)
4391 end if
4392 ! f_i[b_k]=v_i*b_k-v_k*b_i
4393 ^c&f(ix^d,b^c_)=w(ix^d,mom(idim))*w(ix^d,b^c_)-w(ix^d,mag(idim))*w(ix^d,m^c_)\
4394 {end do\}
4395 end if
4396 if(mhd_hall) then
4397 call mhd_getv_hall(w,x,ixi^l,ixo^l,vhall)
4398 {do ix^db=ixomin^db,ixomax^db\}
4399 if(total_energy) then
4400 ! f_i[e]= f_i[e] + vHall_i*(b_k*b_k) - b_i*(vHall_k*b_k)
4401 f(ix^d,e_)=f(ix^d,e_)+vhall(ix^d,idim)*(^c&w(ix^d,b^c_)**2+)&
4402 -w(ix^d,mag(idim))*(^c&vhall(ix^d,^c)*w(ix^d,b^c_)+)
4403 end if
4404 ! f_i[b_k] = f_i[b_k] + vHall_i*b_k - vHall_k*b_i
4405 ^c&f(ix^d,b^c_)=f(ix^d,b^c_)+vhall(ix^d,idim)*w(ix^d,b^c_)-vhall(ix^d,^c)*w(ix^d,mag(idim))\
4406 {end do\}
4407 end if
4408
4409 if(mhd_glm) then
4410 {do ix^db=ixomin^db,ixomax^db\}
4411 f(ix^d,mag(idim))=w(ix^d,psi_)
4412 !f_i[psi]=Ch^2*b_{i} Eq. 24e and Eq. 38c Dedner et al 2002 JCP, 175, 645
4413 f(ix^d,psi_) = cmax_global**2*w(ix^d,mag(idim))
4414 {end do\}
4415 end if
4416
4417 if(mhd_radiation_fld) then
4418 {do ix^db=ixomin^db,ixomax^db\}
4419 f(ix^d,r_e)=w(ix^d,mom(idim))*wc(ix^d,r_e)
4420 {end do\}
4421 endif
4422
4423 if (mhd_fip) then
4424 f(ixo^s,fip_) = w(ixo^s,mom(idim)) * wc(ixo^s,fip_)
4425 end if
4426 if(mhd_uawsom) then
4427 {do ix^db=ixomin^db,ixomax^db\}
4428 uawsom_denom=dsqrt(w(ix^d,rho_)*(uawsom_zeta(ix^d)+one)/&
4429 (two*(one+mhd_uawsom_filling_factor*uawsom_zeta(ix^d)-&
4431 uawsom_bi=w(ix^d,mag(idim))
4432 f(ix^d,waplus_)=w(ix^d,waplus_)*(w(ix^d,mom(idim))-&
4433 uawsom_bi/dsqrt(w(ix^d,rho_)))
4434 f(ix^d,waminus_)=w(ix^d,waminus_)*(w(ix^d,mom(idim))+&
4435 uawsom_bi/dsqrt(w(ix^d,rho_)))
4436 f(ix^d,wkplus_)=w(ix^d,wkplus_)*(w(ix^d,mom(idim))-uawsom_bi/uawsom_denom)
4437 f(ix^d,wkminus_)=w(ix^d,wkminus_)*(w(ix^d,mom(idim))+uawsom_bi/uawsom_denom)
4438 {end do\}
4439 end if
4440 ! Get flux of tracer
4441 do iw=1,mhd_n_tracer
4442 {do ix^db=ixomin^db,ixomax^db\}
4443 f(ix^d,tracer(iw))=w(ix^d,mom(idim))*w(ix^d,tracer(iw))
4444 {end do\}
4445 end do
4446
4447 if(mhd_hyperbolic_tc) then
4448 {do ix^db=ixomin^db,ixomax^db\}
4449 bunit_tc=b_unit_vector(w(ix^d,mag(1:ndir)))
4450 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,qpar_)*bunit_tc(idim)
4451 f(ix^d,qpar_)=zero
4452 {end do\}
4454 call mhd_get_hyperbolic_tc_nperp(w,x,ixi^l,ixo^l,w(ixi^s,mag(1:ndir)),nperp)
4455 {do ix^db=ixomin^db,ixomax^db\}
4456 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,qperp_)*nperp(ix^d,idim)
4457 f(ix^d,qperp_)=zero
4458 {end do\}
4459 end if
4460 end if
4461 end subroutine mhd_get_flux
4462
4463 !> Calculate fluxes within ixO^L for case without energy equation, hence without splitting
4464 !> and assuming polytropic closure
4465 subroutine mhd_get_flux_noe(wC,w,x,ixI^L,ixO^L,idim,f)
4467 use mod_geometry
4469
4470 integer, intent(in) :: ixi^l, ixo^l, idim
4471 ! conservative w
4472 double precision, intent(in) :: wc(ixi^s,nw)
4473 ! primitive w
4474 double precision, intent(in) :: w(ixi^s,nw)
4475 double precision, intent(in) :: x(ixi^s,1:ndim)
4476 double precision,intent(out) :: f(ixi^s,nwflux)
4477
4478 double precision :: vhall(ixi^s,1:ndir)
4479 double precision :: adiabs(ixi^s), gammas(ixi^s)
4480 integer :: iw, ix^d
4481
4482 if(associated(usr_set_adiab)) then
4483 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
4484 else
4485 adiabs=mhd_adiab
4486 end if
4487 if(associated(usr_set_gamma)) then
4488 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
4489 else
4490 gammas=eos%gamma
4491 end if
4492 {do ix^db=ixomin^db,ixomax^db\}
4493 ! Get flux of density
4494 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
4495 ! f_i[m_k]=v_i*m_k-b_k*b_i
4496 ^c&f(ix^d,m^c_)=wc(ix^d,mom(idim))*w(ix^d,m^c_)-w(ix^d,mag(idim))*w(ix^d,b^c_)\
4497 ! normal one includes total pressure
4498 f(ix^d,mom(idim))=f(ix^d,mom(idim))+adiabs(ix^d)*w(ix^d,rho_)**gammas(ix^d)+half*(^c&w(ix^d,b^c_)**2+)
4499 ! f_i[b_k]=v_i*b_k-v_k*b_i
4500 ^c&f(ix^d,b^c_)=w(ix^d,mom(idim))*w(ix^d,b^c_)-w(ix^d,mag(idim))*w(ix^d,m^c_)\
4501 {end do\}
4502 if(mhd_hall) then
4503 call mhd_getv_hall(w,x,ixi^l,ixo^l,vhall)
4504 {do ix^db=ixomin^db,ixomax^db\}
4505 ! f_i[b_k] = f_i[b_k] + vHall_i*b_k - vHall_k*b_i
4506 ^c&f(ix^d,b^c_)=f(ix^d,b^c_)+vhall(ix^d,idim)*w(ix^d,b^c_)-vhall(ix^d,^c)*w(ix^d,mag(idim))\
4507 {end do\}
4508 end if
4509 if(mhd_glm) then
4510 {do ix^db=ixomin^db,ixomax^db\}
4511 f(ix^d,mag(idim))=w(ix^d,psi_)
4512 !f_i[psi]=Ch^2*b_{i} Eq. 24e and Eq. 38c Dedner et al 2002 JCP, 175, 645
4513 f(ix^d,psi_) = cmax_global**2*w(ix^d,mag(idim))
4514 {end do\}
4515 end if
4516 if (mhd_fip) then
4517 f(ixo^s,fip_) = w(ixo^s,mom(idim)) * wc(ixo^s,fip_)
4518 end if
4519 ! Get flux of tracer
4520 do iw=1,mhd_n_tracer
4521 {do ix^db=ixomin^db,ixomax^db\}
4522 f(ix^d,tracer(iw))=w(ix^d,mom(idim))*w(ix^d,tracer(iw))
4523 {end do\}
4524 end do
4525 end subroutine mhd_get_flux_noe
4526
4527 !> Calculate fluxes with hydrodynamic energy equation
4528 subroutine mhd_get_flux_hde(wC,w,x,ixI^L,ixO^L,idim,f)
4530 use mod_geometry
4531
4532 integer, intent(in) :: ixi^l, ixo^l, idim
4533 ! conservative w
4534 double precision, intent(in) :: wc(ixi^s,nw)
4535 ! primitive w
4536 double precision, intent(in) :: w(ixi^s,nw)
4537 double precision, intent(in) :: x(ixi^s,1:ndim)
4538 double precision,intent(out) :: f(ixi^s,nwflux)
4539
4540 double precision :: vhall(ixi^s,1:ndir)
4541 double precision :: nperp(ixi^s,1:ndir)
4542 double precision :: bunit_tc(ndir)
4543 integer :: iw, ix^d
4544
4545 {do ix^db=ixomin^db,ixomax^db\}
4546 ! Get flux of density
4547 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
4548 ! f_i[m_k]=v_i*m_k-b_k*b_i
4549 ^c&f(ix^d,m^c_)=wc(ix^d,mom(idim))*w(ix^d,m^c_)-w(ix^d,mag(idim))*w(ix^d,b^c_)\
4550 ! normal one includes total pressure
4551 f(ix^d,mom(idim))=f(ix^d,mom(idim))+w(ix^d,p_)+half*(^c&w(ix^d,b^c_)**2+)
4552 ! Get flux of energy
4553 f(ix^d,e_)=w(ix^d,mom(idim))*(wc(ix^d,e_)+w(ix^d,p_))
4554 ! f_i[b_k]=v_i*b_k-v_k*b_i
4555 ^c&f(ix^d,b^c_)=w(ix^d,mom(idim))*w(ix^d,b^c_)-w(ix^d,mag(idim))*w(ix^d,m^c_)\
4556 {end do\}
4557 if(mhd_hall) then
4558 call mhd_getv_hall(w,x,ixi^l,ixo^l,vhall)
4559 {do ix^db=ixomin^db,ixomax^db\}
4560 ! f_i[b_k] = f_i[b_k] + vHall_i*b_k - vHall_k*b_i
4561 ^c&f(ix^d,b^c_)=f(ix^d,b^c_)+vhall(ix^d,idim)*w(ix^d,b^c_)-vhall(ix^d,^c)*w(ix^d,mag(idim))\
4562 {end do\}
4563 end if
4564 if(mhd_glm) then
4565 {do ix^db=ixomin^db,ixomax^db\}
4566 f(ix^d,mag(idim))=w(ix^d,psi_)
4567 !f_i[psi]=Ch^2*b_{i} Eq. 24e and Eq. 38c Dedner et al 2002 JCP, 175, 645
4568 f(ix^d,psi_) = cmax_global**2*w(ix^d,mag(idim))
4569 {end do\}
4570 end if
4571 if (mhd_fip) then
4572 f(ixo^s,fip_) = w(ixo^s,mom(idim)) * wc(ixo^s,fip_)
4573 end if
4574 ! Get flux of tracer
4575 do iw=1,mhd_n_tracer
4576 {do ix^db=ixomin^db,ixomax^db\}
4577 f(ix^d,tracer(iw))=w(ix^d,mom(idim))*w(ix^d,tracer(iw))
4578 {end do\}
4579 end do
4580
4581 if(mhd_hyperbolic_tc) then
4582 {do ix^db=ixomin^db,ixomax^db\}
4583 bunit_tc=b_unit_vector(w(ix^d,mag(1:ndir)))
4584 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,qpar_)*bunit_tc(idim)
4585 f(ix^d,qpar_)=zero
4586 {end do\}
4588 call mhd_get_hyperbolic_tc_nperp(w,x,ixi^l,ixo^l,w(ixi^s,mag(1:ndir)),nperp)
4589 {do ix^db=ixomin^db,ixomax^db\}
4590 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,qperp_)*nperp(ix^d,idim)
4591 f(ix^d,qperp_)=zero
4592 {end do\}
4593 end if
4594 end if
4595
4596 end subroutine mhd_get_flux_hde
4597
4598 !> Calculate fluxes within ixO^L with possible splitting
4599 !> this covers four cases: B0field=T and mhd_internal_e=T (where has_equi_rho_and_p=F)
4600 !> B0field=T and has_equi_rho_and_p=F for total_energy=T
4601 !> B0field=F and has_equi_rho_and_p=T for total_energy=T
4602 !> B0field=T and has_equi_rho_and_p=T for total_energy=T
4603 subroutine mhd_get_flux_split(wC,w,x,ixI^L,ixO^L,idim,f)
4605 use mod_geometry
4606
4607 integer, intent(in) :: ixi^l, ixo^l, idim
4608 ! conservative w
4609 double precision, intent(in) :: wc(ixi^s,nw)
4610 ! primitive w
4611 double precision, intent(in) :: w(ixi^s,nw)
4612 double precision, intent(in) :: x(ixi^s,1:ndim)
4613 double precision,intent(out) :: f(ixi^s,nwflux)
4614
4615 double precision :: vhall(ixi^s,1:ndir)
4616 double precision :: ptotal, btotal(ixo^s,1:ndir)
4617 double precision :: nperp(ixi^s,1:ndir)
4618 double precision :: uawsom_zeta(ixi^s), uawsom_radius(ixi^s)
4619 double precision :: uawsom_lperp(ixi^s), uawsom_denom
4620 double precision :: uawsom_pwave, uawsom_bi
4621 double precision :: bunit_tc(ndir)
4622 logical :: use_perp_flux
4623 integer :: iw, ix^d
4624
4625 if(mhd_uawsom) call mhd_uawsom_get_coefficients(w,x,ixi^l,ixo^l,.true.,&
4626 uawsom_zeta,uawsom_radius,uawsom_lperp)
4627
4628 {do ix^db=ixomin^db,ixomax^db\}
4629 ! Get flux of density
4630 if(has_equi_rho_and_p) then
4631 f(ix^d,rho_)=w(ix^d,mom(idim))*(w(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,b0i))
4632 else
4633 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
4634 end if
4635
4636 ptotal=w(ix^d,p_)+half*(^c&w(ix^d,b^c_)**2+)
4637
4638 if(b0field) then
4639 ^c&btotal(ix^d,^c)=w(ix^d,b^c_)+block%B0(ix^d,^c,idim)\
4640 ptotal=ptotal+(^c&w(ix^d,b^c_)*block%B0(ix^d,^c,idim)+)
4641 ! Get flux of momentum and magnetic field
4642 ! f_i[m_k]=v_i*m_k-b_k*b_i
4643 ^c&f(ix^d,m^c_)=wc(ix^d,mom(idim))*w(ix^d,m^c_)-&
4644 btotal(ix^d,idim)*w(ix^d,b^c_)-w(ix^d,mag(idim))*block%B0(ix^d,^c,idim)\
4645 f(ix^d,mom(idim))=f(ix^d,mom(idim))+ptotal
4646 else
4647 ^c&btotal(ix^d,^c)=w(ix^d,b^c_)\
4648 ! Get flux of momentum and magnetic field
4649 ! f_i[m_k]=v_i*m_k-b_k*b_i
4650 ^c&f(ix^d,m^c_)=wc(ix^d,mom(idim))*w(ix^d,m^c_)-w(ix^d,mag(idim))*w(ix^d,b^c_)\
4651 f(ix^d,mom(idim))=f(ix^d,mom(idim))+ptotal
4652 end if
4653 if(mhd_uawsom) then
4654 uawsom_pwave=mhd_uawsom_wave_pressure_cell(w(ix^d,:),uawsom_zeta(ix^d))
4655 f(ix^d,mom(idim))=f(ix^d,mom(idim))+uawsom_pwave
4656 end if
4657 ! f_i[b_k]=v_i*b_k-v_k*b_i
4658 ^c&f(ix^d,b^c_)=w(ix^d,mom(idim))*btotal(ix^d,^c)-btotal(ix^d,idim)*w(ix^d,m^c_)\
4659
4660 ! Get flux of energy
4661 ! f_i[e]=v_i*e+v_i*ptotal-b_i*(b_k*v_k)
4662 if(mhd_internal_e) then
4663 f(ix^d,e_)=w(ix^d,mom(idim))*wc(ix^d,e_)
4664 else
4665 f(ix^d,e_)=w(ix^d,mom(idim))*(wc(ix^d,e_)+ptotal)&
4666 -btotal(ix^d,idim)*(^c&w(ix^d,b^c_)*w(ix^d,m^c_)+)
4667 if(mhd_uawsom) then
4668 uawsom_denom=dsqrt(w(ix^d,rho_)*(uawsom_zeta(ix^d)+one)/&
4669 (two*(one+mhd_uawsom_filling_factor*uawsom_zeta(ix^d)-&
4671 uawsom_bi=btotal(ix^d,idim)
4672 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,mom(idim))*uawsom_pwave+uawsom_bi*(&
4673 (w(ix^d,waminus_)-w(ix^d,waplus_))/dsqrt(w(ix^d,rho_))+&
4674 (w(ix^d,wkminus_)-w(ix^d,wkplus_))/uawsom_denom)
4675 end if
4676 end if
4677 {end do\}
4678
4679 if(mhd_glm) then
4680 {do ix^db=ixomin^db,ixomax^db\}
4681 f(ix^d,mag(idim))=w(ix^d,psi_)
4682 !f_i[psi]=Ch^2*b_{i} Eq. 24e and Eq. 38c Dedner et al 2002 JCP, 175, 645
4683 f(ix^d,psi_) = cmax_global**2*w(ix^d,mag(idim))
4684 {end do\}
4685 end if
4686
4687 if(mhd_radiation_fld) then
4688 {do ix^db=ixomin^db,ixomax^db\}
4689 f(ix^d,r_e)=w(ix^d,mom(idim))*wc(ix^d,r_e)
4690 {end do\}
4691 endif
4692
4693 if(mhd_hall) then
4694 call mhd_getv_hall(w,x,ixi^l,ixo^l,vhall)
4695 {do ix^db=ixomin^db,ixomax^db\}
4696 ! f_i[b_k] = f_i[b_k] + vHall_i*b_k - vHall_k*b_i
4697 ^c&f(ix^d,b^c_)=f(ix^d,b^c_)+vhall(ix^d,idim)*btotal(ix^d,^c)-btotal(ix^d,idim)*vhall(ix^d,^c)\
4698 if(total_energy) then
4699 ! f_i[e]= f_i[e] + vHall_i*(b_k*b_k) - b_i*(vHall_k*b_k)
4700 f(ix^d,e_)=f(ix^d,e_)+vhall(ix^d,idim)*(^c&w(ix^d,b^c_)*btotal(ix^d,^c)+)&
4701 -btotal(ix^d,idim)*(^c&vhall(ix^d,^c)*w(ix^d,b^c_)+)
4702 end if
4703 {end do\}
4704 end if
4705 if (mhd_fip) then
4706 f(ixo^s,fip_) = w(ixo^s,mom(idim)) * wc(ixo^s,fip_)
4707 end if
4708 if(mhd_uawsom) then
4709 {do ix^db=ixomin^db,ixomax^db\}
4710 uawsom_denom=dsqrt(w(ix^d,rho_)*(uawsom_zeta(ix^d)+one)/&
4711 (two*(one+mhd_uawsom_filling_factor*uawsom_zeta(ix^d)-&
4713 uawsom_bi=btotal(ix^d,idim)
4714 f(ix^d,waplus_)=w(ix^d,waplus_)*(w(ix^d,mom(idim))-&
4715 uawsom_bi/dsqrt(w(ix^d,rho_)))
4716 f(ix^d,waminus_)=w(ix^d,waminus_)*(w(ix^d,mom(idim))+&
4717 uawsom_bi/dsqrt(w(ix^d,rho_)))
4718 f(ix^d,wkplus_)=w(ix^d,wkplus_)*(w(ix^d,mom(idim))-uawsom_bi/uawsom_denom)
4719 f(ix^d,wkminus_)=w(ix^d,wkminus_)*(w(ix^d,mom(idim))+uawsom_bi/uawsom_denom)
4720 {end do\}
4721 end if
4722 ! Get flux of tracer
4723 do iw=1,mhd_n_tracer
4724 {do ix^db=ixomin^db,ixomax^db\}
4725 f(ix^d,tracer(iw))=w(ix^d,mom(idim))*w(ix^d,tracer(iw))
4726 {end do\}
4727 end do
4728
4729 if(mhd_hyperbolic_tc) then
4730 {do ix^db=ixomin^db,ixomax^db\}
4731 bunit_tc=b_unit_vector(btotal(ix^d,1:ndir))
4732 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,qpar_)*bunit_tc(idim)
4733 f(ix^d,qpar_)=zero
4734 {end do\}
4736 call mhd_get_hyperbolic_tc_nperp(w,x,ixi^l,ixo^l,btotal,nperp)
4737 {do ix^db=ixomin^db,ixomax^db\}
4738 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,qperp_)*nperp(ix^d,idim)
4739 f(ix^d,qperp_)=zero
4740 {end do\}
4741 end if
4742 end if
4743
4744 end subroutine mhd_get_flux_split
4745
4746 !> Semirelativistic fluxes within ixO^L, with B0 splitting per Gombosi et al. 2002 eqs (92)-(97)
4747 subroutine mhd_get_flux_semirelati(wC,w,x,ixI^L,ixO^L,idim,f)
4749 use mod_geometry
4750
4751 integer, intent(in) :: ixi^l, ixo^l, idim
4752 ! conservative w
4753 double precision, intent(in) :: wc(ixi^s,nw)
4754 ! primitive w
4755 double precision, intent(in) :: w(ixi^s,nw)
4756 double precision, intent(in) :: x(ixi^s,1:ndim)
4757 double precision,intent(out) :: f(ixi^s,nwflux)
4758 double precision :: sa(ixo^s,1:ndir),e(ixo^s,1:ndir),e2
4759 double precision :: btf(ixi^s,1:ndir),b0sq,btsq
4760 double precision :: nperp(ixi^s,1:ndir)
4761 double precision :: bunit_tc(ndir)
4762 integer :: iw, ix^d
4763
4764 {do ix^db=ixomin^db,ixomax^db\}
4765 ! Get flux of density
4766 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
4767 ! total field for E, the induction flux and BB; the stored slot (B1 under splitting)
4768 ! for the magnetic energy and the Poynting vector, Gombosi et al. 2002 eqs (92), (96)
4769 ^c&btf(ix^d,^c)=w(ix^d,b^c_)\
4770 if(b0field) then
4771 ^c&btf(ix^d,^c)=btf(ix^d,^c)+block%B0(ix^d,^c,idim)\
4772 end if
4773 ! E=Bxv
4774 {^ifthreec
4775 e(ix^d,1)=btf(ix^d,2)*w(ix^d,m3_)-btf(ix^d,3)*w(ix^d,m2_)
4776 e(ix^d,2)=btf(ix^d,3)*w(ix^d,m1_)-btf(ix^d,1)*w(ix^d,m3_)
4777 e(ix^d,3)=btf(ix^d,1)*w(ix^d,m2_)-btf(ix^d,2)*w(ix^d,m1_)
4778 }
4779 {^iftwoc
4780 e(ix^d,1)=zero
4781 ! switch 2 and 3 to add 3 when ^C is from 1 to 2
4782 e(ix^d,2)=btf(ix^d,1)*w(ix^d,m2_)-btf(ix^d,2)*w(ix^d,m1_)
4783 }
4784 {^ifonec
4785 e(ix^d,1)=zero
4786 }
4787 e2=(^c&e(ix^d,^c)**2+)
4788 if(mhd_internal_e) then
4789 ! Get flux of internal energy
4790 f(ix^d,e_)=w(ix^d,mom(idim))*wc(ix^d,e_)
4791 else
4792 ! S=ExB
4793 {^ifthreec
4794 sa(ix^d,1)=e(ix^d,2)*w(ix^d,b3_)-e(ix^d,3)*w(ix^d,b2_)
4795 sa(ix^d,2)=e(ix^d,3)*w(ix^d,b1_)-e(ix^d,1)*w(ix^d,b3_)
4796 sa(ix^d,3)=e(ix^d,1)*w(ix^d,b2_)-e(ix^d,2)*w(ix^d,b1_)
4797 }
4798 {^iftwoc
4799 sa(ix^d,1)=-e(ix^d,2)*w(ix^d,b2_)
4800 sa(ix^d,2)=e(ix^d,2)*w(ix^d,b1_)
4801 ! set E2 back to 0, after e^2 is stored
4802 e(ix^d,2)=zero
4803 }
4804 {^ifonec
4805 sa(ix^d,1)=zero
4806 }
4807 ! v*(eK+eint+p) + Poynting, with eK+eint = e-eB-eE from the conserved state so that
4808 ! the flux holds for any EoS
4809 f(ix^d,e_)=w(ix^d,mom(idim))*(wc(ix^d,e_)&
4810 -half*((^c&w(ix^d,b^c_)**2+)+e2*inv_squared_c)&
4811 +w(ix^d,p_))+sa(ix^d,idim)
4812 end if
4813 ! Get flux of momentum
4814 if(b0field) then
4815 ! eq (97): -(BB-B0B0) and 0.5*(B^2-B0^2); the electric terms are not split
4816 ^c&f(ix^d,m^c_)=w(ix^d,rho_)*w(ix^d,mom(idim))*w(ix^d,m^c_)&
4817 -btf(ix^d,idim)*btf(ix^d,^c)+block%B0(ix^d,idim,idim)*block%B0(ix^d,^c,idim)&
4818 -e(ix^d,idim)*e(ix^d,^c)*inv_squared_c\
4819 b0sq=(^c&block%B0(ix^d,^c,idim)**2+)
4820 btsq=(^c&btf(ix^d,^c)**2+)
4821 f(ix^d,mom(idim))=f(ix^d,mom(idim))+w(ix^d,p_)+half*(btsq-b0sq+e2*inv_squared_c)
4822 else
4823 ^c&f(ix^d,m^c_)=w(ix^d,rho_)*w(ix^d,mom(idim))*w(ix^d,m^c_)&
4824 -w(ix^d,mag(idim))*w(ix^d,b^c_)-e(ix^d,idim)*e(ix^d,^c)*inv_squared_c\
4825 ! gas pressure + magnetic pressure + electric pressure
4826 f(ix^d,mom(idim))=f(ix^d,mom(idim))+w(ix^d,p_)+half*((^c&w(ix^d,b^c_)**2+)+e2*inv_squared_c)
4827 end if
4828 ! compute flux of magnetic field
4829 ! f_i[b_k]=v_i*b_k-v_k*b_i
4830 ^c&f(ix^d,b^c_)=w(ix^d,mom(idim))*btf(ix^d,^c)-btf(ix^d,idim)*w(ix^d,m^c_)\
4831 {end do\}
4832
4833 if(mhd_glm) then
4834 {do ix^db=ixomin^db,ixomax^db\}
4835 f(ix^d,mag(idim))=w(ix^d,psi_)
4836 !f_i[psi]=Ch^2*b_{i} Eq. 24e and Eq. 38c Dedner et al 2002 JCP, 175, 645
4837 f(ix^d,psi_)=cmax_global**2*w(ix^d,mag(idim))
4838 {end do\}
4839 end if
4840 if (mhd_fip) then
4841 f(ixo^s,fip_) = w(ixo^s,mom(idim)) * wc(ixo^s,fip_)
4842 end if
4843 ! Get flux of tracer
4844 do iw=1,mhd_n_tracer
4845 {do ix^db=ixomin^db,ixomax^db\}
4846 f(ix^d,tracer(iw))=w(ix^d,mom(idim))*w(ix^d,tracer(iw))
4847 {end do\}
4848 end do
4849
4850 if(mhd_hyperbolic_tc) then
4851 {do ix^db=ixomin^db,ixomax^db\}
4852 bunit_tc=b_unit_vector(btf(ix^d,1:ndir))
4853 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,qpar_)*bunit_tc(idim)
4854 f(ix^d,qpar_)=zero
4855 {end do\}
4857 call mhd_get_hyperbolic_tc_nperp(w,x,ixi^l,ixo^l,btf,nperp)
4858 {do ix^db=ixomin^db,ixomax^db\}
4859 f(ix^d,e_)=f(ix^d,e_)+w(ix^d,qperp_)*nperp(ix^d,idim)
4860 f(ix^d,qperp_)=zero
4861 {end do\}
4862 end if
4863 end if
4864 end subroutine mhd_get_flux_semirelati
4865
4866 subroutine mhd_get_flux_semirelati_noe(wC,w,x,ixI^L,ixO^L,idim,f)
4868 use mod_geometry
4870
4871 integer, intent(in) :: ixi^l, ixo^l, idim
4872 ! conservative w
4873 double precision, intent(in) :: wc(ixi^s,nw)
4874 ! primitive w
4875 double precision, intent(in) :: w(ixi^s,nw)
4876 double precision, intent(in) :: x(ixi^s,1:ndim)
4877 double precision,intent(out) :: f(ixi^s,nwflux)
4878
4879 double precision :: adiabs(ixi^s), gammas(ixi^s)
4880 double precision :: e(ixo^s,1:ndir),e2
4881 integer :: iw, ix^d
4882
4883 if(associated(usr_set_adiab)) then
4884 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
4885 else
4886 adiabs=mhd_adiab
4887 end if
4888 if(associated(usr_set_gamma)) then
4889 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
4890 else
4891 gammas=eos%gamma
4892 end if
4893 {do ix^db=ixomin^db,ixomax^db\}
4894 ! Get flux of density
4895 f(ix^d,rho_)=w(ix^d,mom(idim))*w(ix^d,rho_)
4896 ! E=Bxv
4897 {^ifthreec
4898 e(ix^d,1)=w(ix^d,b2_)*w(ix^d,m3_)-w(ix^d,b3_)*w(ix^d,m2_)
4899 e(ix^d,2)=w(ix^d,b3_)*w(ix^d,m1_)-w(ix^d,b1_)*w(ix^d,m3_)
4900 e(ix^d,3)=w(ix^d,b1_)*w(ix^d,m2_)-w(ix^d,b2_)*w(ix^d,m1_)
4901 e2=(^c&e(ix^d,^c)**2+)
4902 }
4903 {^iftwoc
4904 e(ix^d,1)=zero
4905 ! switch 2 and 3 to add 3 when ^C is from 1 to 2
4906 e(ix^d,2)=w(ix^d,b1_)*w(ix^d,m2_)-w(ix^d,b2_)*w(ix^d,m1_)
4907 e2=e(ix^d,2)**2
4908 e(ix^d,2)=zero
4909 }
4910 {^ifonec
4911 e(ix^d,1)=zero
4912 e2=zero
4913 }
4914 ! Get flux of momentum
4915 ^c&f(ix^d,m^c_)=w(ix^d,rho_)*w(ix^d,mom(idim))*w(ix^d,m^c_)&
4916 -w(ix^d,mag(idim))*w(ix^d,b^c_)-e(ix^d,idim)*e(ix^d,^c)*inv_squared_c\
4917 ! gas pressure + magnetic pressure + electric pressure
4918 f(ix^d,mom(idim))=f(ix^d,mom(idim))+adiabs(ix^d)*w(ix^d,rho_)**gammas(ix^d)+half*((^c&w(ix^d,b^c_)**2+)+e2*inv_squared_c)
4919 ! compute flux of magnetic field
4920 ! f_i[b_k]=v_i*b_k-v_k*b_i
4921 ^c&f(ix^d,b^c_)=w(ix^d,mom(idim))*w(ix^d,b^c_)-w(ix^d,mag(idim))*w(ix^d,m^c_)\
4922 {end do\}
4923
4924 if(mhd_glm) then
4925 {do ix^db=ixomin^db,ixomax^db\}
4926 f(ix^d,mag(idim))=w(ix^d,psi_)
4927 !f_i[psi]=Ch^2*b_{i} Eq. 24e and Eq. 38c Dedner et al 2002 JCP, 175, 645
4928 f(ix^d,psi_)=cmax_global**2*w(ix^d,mag(idim))
4929 {end do\}
4930 end if
4931 if (mhd_fip) then
4932 f(ixo^s,fip_) = w(ixo^s,mom(idim)) * wc(ixo^s,fip_)
4933 end if
4934 ! Get flux of tracer
4935 do iw=1,mhd_n_tracer
4936 {do ix^db=ixomin^db,ixomax^db\}
4937 f(ix^d,tracer(iw))=w(ix^d,mom(idim))*w(ix^d,tracer(iw))
4938 {end do\}
4939 end do
4940 end subroutine mhd_get_flux_semirelati_noe
4941
4942 !> Source term J.E_ambi in internal energy
4943 !> For the ambipolar electric field we have E_ambi = -eta_A * JxBxB= eta_A * B^2 (J_perpB)
4944 !> and eta_A is mhd_ambi_coef/rho^2 or is user-defined
4945 !> the source term J.E_ambi = eta_A * B^2 * J_perpB^2 = eta_A * [(JxB)xB]^2/B^2
4946 !> note that J_perpB= - (JxB)xB/B^2
4947 !> multiplyAmbiCoef is actually doing multiplication with -mhd_ambi_coef/rho^2
4948 subroutine add_source_ambipolar_internal_energy(qdt,ixI^L,ixO^L,wCT,w,x)
4950 integer, intent(in) :: ixi^l, ixo^l
4951 double precision, intent(in) :: qdt
4952 double precision, intent(in) :: wct(ixi^s,1:nw), x(ixi^s,1:ndim)
4953 double precision, intent(inout) :: w(ixi^s,1:nw)
4954
4955 double precision :: tmp(ixi^s),btot2(ixi^s)
4956 double precision :: jxbxb(ixi^s,1:3)
4957
4958 call mhd_get_jxbxb(wct,x,ixi^l,ixo^l,jxbxb)
4959 ! avoiding nulls here
4960 btot2(ixo^s)=mhd_mag_en_all(wct,ixi^l,ixo^l)
4961 where (btot2(ixo^s)>smalldouble )
4962 tmp(ixo^s) = sum(jxbxb(ixo^s,1:3)**2,dim=ndim+1) / btot2(ixo^s)
4963 elsewhere
4964 tmp(ixo^s) = zero
4965 endwhere
4966 call multiplyambicoef(ixi^l,ixo^l,tmp,wct,x)
4967 ! multiplyAmbiCoef is actually doing multiplication with -mhd_ambi_coef/rho^2
4968 ! hence minus sign here
4969 w(ixo^s,e_)=w(ixo^s,e_)- qdt*tmp(ixo^s)
4970
4971 end subroutine add_source_ambipolar_internal_energy
4972
4973 !> this subroutine computes -J_perpB= (J x B) x B= B(J.B) - J B^2
4974 subroutine mhd_get_jxbxb(w,x,ixI^L,ixO^L,res)
4976
4977 integer, intent(in) :: ixi^l, ixo^l
4978 double precision, intent(in) :: w(ixi^s,nw)
4979 double precision, intent(in) :: x(ixi^s,1:ndim)
4980 double precision, intent(out) :: res(ixi^s,1:3)
4981
4982 double precision :: btot(ixi^s,1:3)
4983 double precision :: current(ixi^s,7-2*ndir:3)
4984 double precision :: tmp(ixi^s),b2(ixi^s)
4985 integer :: idir, idirmin
4986
4987 res=0.d0
4988 ! Calculate current density and idirmin
4989 ! current has nonzero values only for components in the range idirmin, 3
4990 call get_current(w,ixi^l,ixo^l,idirmin,current)
4991
4992 btot=0.d0
4993 if(b0field) then
4994 do idir=1,ndir
4995 btot(ixo^s, idir) = w(ixo^s,mag(idir)) + block%B0(ixo^s,idir,b0i)
4996 enddo
4997 else
4998 do idir=1,ndir
4999 btot(ixo^s, idir) = w(ixo^s,mag(idir))
5000 enddo
5001 endif
5002
5003 tmp(ixo^s)= sum(current(ixo^s,idirmin:3)*btot(ixo^s,idirmin:3),dim=ndim+1) !J.B
5004 b2(ixo^s) = sum(btot(ixo^s,1:3)**2,dim=ndim+1) !B^2
5005 do idir=1,idirmin-1
5006 res(ixo^s,idir) = btot(ixo^s,idir) * tmp(ixo^s)
5007 enddo
5008 do idir=idirmin,3
5009 res(ixo^s,idir) = btot(ixo^s,idir) * tmp(ixo^s) - current(ixo^s,idir) * b2(ixo^s)
5010 enddo
5011
5012 ! avoid possible issues at nulls
5013 do idir=1,3
5014 where (b2(ixo^s)<smalldouble )
5015 res(ixo^s,idir) = zero
5016 endwhere
5017 enddo
5018 end subroutine mhd_get_jxbxb
5019
5020 !> Sets the sources for the ambipolar terms for the STS method
5021 !> The sources are added directly (instead of fluxes as in the explicit)
5022 !> at the corresponding indices
5023 !> store_flux_var is explicitly called for each of the fluxes one by one
5024 subroutine sts_set_source_ambipolar(ixI^L,ixO^L,w,x,wres,fix_conserve_at_step,my_dt,igrid,nflux)
5027
5028 integer, intent(in) :: ixi^l,ixo^l,igrid,nflux
5029 double precision, intent(in) :: x(ixi^s,1:ndim)
5030 double precision, intent(inout) :: wres(ixi^s,1:nw), w(ixi^s,1:nw)
5031 double precision, intent(in) :: my_dt
5032 logical, intent(in) :: fix_conserve_at_step
5033
5034 double precision, dimension(ixI^S,1:3) :: tmp,ff
5035 double precision :: fluxall(ixi^s,1:nflux,1:ndim)
5036 double precision :: fe(ixi^s,sdim:3)
5037 double precision :: btot(ixi^s,1:3),tmp2(ixi^s)
5038 integer :: i, ixa^l, ie_
5039
5040 ixa^l=ixo^l^ladd1;
5041
5042 fluxall=zero
5043
5044 ! here we compute (JxB)xB= - B^2 J_perpB
5045 call mhd_get_jxbxb(w,x,ixi^l,ixa^l,tmp)
5046
5047 ! set ambipolar electric field in tmp: E_ambi = -eta_A * JxBxB= eta_A * B^2 (J_perpB)
5048 ! and eta_A is mhd_ambi_coef/rho^2 or is user-defined
5049 ! multiplyAmbiCoef is actually doing multiplication with -mhd_ambi_coef/rho^2
5050 do i=1,3
5051 call multiplyambicoef(ixi^l,ixa^l,tmp(ixi^s,i),w,x)
5052 enddo
5053
5054 ! Note: internal energy case is handled through add_source_internal_e
5055 ! Note: hydrodynamic energy case is handled through add_source_hydrodynamic_e
5056 ! both of the above use add_source_ambipolar_internal_energy
5057 !
5058 ! Note: total energy case without B0field split is ok here and adds div(BxE_ambi)
5059 ! Note: total energy case in semirelativistic variant (hence no B0field split) is ok here
5060 ! Note: total energy with B0field=T here adds div(B_1xE_ambi) which needs correction in add_source_B0split
5061 if(mhd_energy .and. .not.(mhd_internal_e.or.mhd_hydrodynamic_e)) then
5062 btot(ixa^s,1:3) = 0.d0
5063 ! HERE: only uses B_1 if split, otherwise this is B
5064 btot(ixa^s,1:ndir) = w(ixa^s,mag(1:ndir))
5065 ! compute ff= E_ambi x B (where B can be B_1 if B0field=T)
5066 call cross_product(ixi^l,ixa^l,tmp,btot,ff)
5067 ! compute actual cell face fluxes in ff and their divergence in tmp2
5068 call get_flux_on_cell_face(ixi^l,ixo^l,ff,tmp2)
5069 if(fix_conserve_at_step) fluxall(ixi^s,1,1:ndim)=ff(ixi^s,1:ndim)
5070 ! - sign as the source is actually div(BxE_ambi) and we have div(E_ambi x B) in tmp2
5071 wres(ixo^s,e_)=-tmp2(ixo^s)
5072 endif
5073
5074 if(stagger_grid) then
5075 ! always 2D or more (2.5/3D)
5076 if(ndir>ndim) then
5077 !!!Bz
5078 ff(ixa^s,1) = tmp(ixa^s,2)
5079 ff(ixa^s,2) = -tmp(ixa^s,1)
5080 ff(ixa^s,3) = 0.d0
5081 call get_flux_on_cell_face(ixi^l,ixo^l,ff,tmp2)
5082 if(fix_conserve_at_step) fluxall(ixi^s,1+ndir,1:ndim)=ff(ixi^s,1:ndim)
5083 wres(ixo^s,mag(ndir))=-tmp2(ixo^s)
5084 end if
5085 fe=0.d0
5086 call update_faces_ambipolar(ixi^l,ixo^l,w,x,tmp,fe,btot)
5087 ixamax^d=ixomax^d;
5088 ixamin^d=ixomin^d-1;
5089 wres(ixa^s,mag(1:ndim))=-btot(ixa^s,1:ndim)
5090 else
5091 !write curl(ele) as the divergence
5092 !m1={0,ele[[3]],-ele[[2]]}
5093 !m2={-ele[[3]],0,ele[[1]]}
5094 !m3={ele[[2]],-ele[[1]],0}
5095
5096 {^ifoned
5097 !!!Bx
5098 ff(ixa^s,1) = 0.d0
5099 ff(ixa^s,2) = tmp(ixa^s,3)
5100 ff(ixa^s,3) = -tmp(ixa^s,2)
5101 call get_flux_on_cell_face(ixi^l,ixo^l,ff,tmp2)
5102 if(fix_conserve_at_step) fluxall(ixi^s,2,1:ndim)=ff(ixi^s,1:ndim)
5103 !flux divergence is a source now
5104 wres(ixo^s,mag(1))=-tmp2(ixo^s)
5105 if(ndir==2.or.ndir==3)then
5106 !!!By
5107 ff(ixa^s,1) = -tmp(ixa^s,3)
5108 ff(ixa^s,2) = 0.d0
5109 ff(ixa^s,3) = tmp(ixa^s,1)
5110 call get_flux_on_cell_face(ixi^l,ixo^l,ff,tmp2)
5111 if(fix_conserve_at_step) fluxall(ixi^s,3,1:ndim)=ff(ixi^s,1:ndim)
5112 wres(ixo^s,mag(2))=-tmp2(ixo^s)
5113 endif
5114 }
5115 {^nooned
5116 !!!Bx
5117 ff(ixa^s,1) = 0.d0
5118 ff(ixa^s,2) = tmp(ixa^s,3)
5119 ff(ixa^s,3) = -tmp(ixa^s,2)
5120 call get_flux_on_cell_face(ixi^l,ixo^l,ff,tmp2)
5121 if(fix_conserve_at_step) fluxall(ixi^s,2,1:ndim)=ff(ixi^s,1:ndim)
5122 !flux divergence is a source now
5123 wres(ixo^s,mag(1))=-tmp2(ixo^s)
5124 !!!By
5125 ff(ixa^s,1) = -tmp(ixa^s,3)
5126 ff(ixa^s,2) = 0.d0
5127 ff(ixa^s,3) = tmp(ixa^s,1)
5128 call get_flux_on_cell_face(ixi^l,ixo^l,ff,tmp2)
5129 if(fix_conserve_at_step) fluxall(ixi^s,3,1:ndim)=ff(ixi^s,1:ndim)
5130 wres(ixo^s,mag(2))=-tmp2(ixo^s)
5131 }
5132
5133 if(ndir==3) then
5134 !!!Bz
5135 ff(ixa^s,1) = tmp(ixa^s,2)
5136 ff(ixa^s,2) = -tmp(ixa^s,1)
5137 ff(ixa^s,3) = 0.d0
5138 call get_flux_on_cell_face(ixi^l,ixo^l,ff,tmp2)
5139 if(fix_conserve_at_step) fluxall(ixi^s,1+ndir,1:ndim)=ff(ixi^s,1:ndim)
5140 wres(ixo^s,mag(ndir))=-tmp2(ixo^s)
5141 end if
5142
5143 end if
5144
5145 if(fix_conserve_at_step) then
5146 fluxall=my_dt*fluxall
5147 call store_flux(igrid,fluxall,1,ndim,nflux)
5148 if(stagger_grid) then
5149 call store_edge(igrid,ixi^l,my_dt*fe,1,ndim)
5150 end if
5151 end if
5152
5153 end subroutine sts_set_source_ambipolar
5154
5155 !> get ambipolar electric field and the integrals around cell faces
5156 subroutine update_faces_ambipolar(ixI^L,ixO^L,w,x,ECC,fE,circ)
5158
5159 integer, intent(in) :: ixi^l, ixo^l
5160 double precision, intent(in) :: w(ixi^s,1:nw)
5161 double precision, intent(in) :: x(ixi^s,1:ndim)
5162 ! amibipolar electric field at cell centers
5163 double precision, intent(in) :: ecc(ixi^s,1:3)
5164 double precision, intent(out) :: fe(ixi^s,sdim:3)
5165 double precision, intent(out) :: circ(ixi^s,1:ndim)
5166
5167 integer :: hxc^l,ixc^l,ixa^l
5168 integer :: idim1,idim2,idir,ix^d
5169
5170 fe=zero
5171 ! calculate ambipolar electric field on cell edges from cell centers
5172 do idir=sdim,3
5173 ixcmax^d=ixomax^d;
5174 ixcmin^d=ixomin^d+kr(idir,^d)-1;
5175 {do ix^db=0,1\}
5176 if({ ix^d==1 .and. ^d==idir | .or.}) cycle
5177 ixamin^d=ixcmin^d+ix^d;
5178 ixamax^d=ixcmax^d+ix^d;
5179 fe(ixc^s,idir)=fe(ixc^s,idir)+ecc(ixa^s,idir)
5180 {end do\}
5181 fe(ixc^s,idir)=fe(ixc^s,idir)*0.25d0*block%dsC(ixc^s,idir)
5182 end do
5183
5184 ! Calculate circulation on each face to get value of line integral of
5185 ! electric field in the positive idir direction.
5186 ixcmax^d=ixomax^d;
5187 ixcmin^d=ixomin^d-1;
5188
5189 circ=zero
5190 do idim1=1,ndim ! Coordinate perpendicular to face
5191 do idim2=1,ndim
5192 do idir=sdim,3 ! Direction of line integral
5193 ! Assemble indices
5194 hxc^l=ixc^l-kr(idim2,^d);
5195 ! Add line integrals in direction idir
5196 circ(ixc^s,idim1)=circ(ixc^s,idim1)&
5197 +lvc(idim1,idim2,idir)&
5198 *(fe(ixc^s,idir)&
5199 -fe(hxc^s,idir))
5200 end do
5201 end do
5202 circ(ixc^s,idim1)=circ(ixc^s,idim1)/block%surfaceC(ixc^s,idim1)
5203 end do
5204
5205 end subroutine update_faces_ambipolar
5206
5207 !> use cell-center flux vector to get cell-face flux vector
5208 !> which will be used to add the source term as the divergence of the flux
5209 !> we return fluxes at all faces as well as the divergence of the flux
5210 !> Note that for ndir>ndim, we do not modify the input cell center flux
5211 subroutine get_flux_on_cell_face(ixI^L,ixO^L,ff,src)
5213
5214 integer, intent(in) :: ixi^l, ixo^l
5215 double precision, dimension(ixI^S,1:3), intent(inout) :: ff
5216 double precision, intent(out) :: src(ixi^s)
5217
5218 double precision :: ffc(ixi^s,1:ndim)
5219 double precision :: dxinv(ndim)
5220 integer :: idims, ix^d, ixa^l, ixb^l, ixc^l
5221
5222 ixa^l=ixo^l^ladd1;
5223 dxinv=1.d0/dxlevel
5224 ! cell corner flux in ffc
5225 ! TO BE GENERALIZED FOR NON-UNIFORM NON-CARTESIAN MESH
5226 if (slab_uniform)then
5227 ffc=0.d0
5228 ixcmax^d=ixomax^d; ixcmin^d=ixomin^d-1;
5229 {do ix^db=0,1\}
5230 ixbmin^d=ixcmin^d+ix^d;
5231 ixbmax^d=ixcmax^d+ix^d;
5232 ffc(ixc^s,1:ndim)=ffc(ixc^s,1:ndim)+ff(ixb^s,1:ndim)
5233 {end do\}
5234 ffc(ixc^s,1:ndim)=0.5d0**ndim*ffc(ixc^s,1:ndim)
5235 else
5236 call mpistop("to generalize using volume averaging")
5237 endif
5238 ! now get flux at cell face from corner fluxes in fcc
5239 ff(ixi^s,1:ndim)=0.d0
5240 do idims=1,ndim
5241 ixb^l=ixo^l-kr(idims,^d);
5242 ixcmax^d=ixomax^d; ixcmin^d=ixbmin^d;
5243 {do ix^db=0,1 \}
5244 if({ ix^d==0 .and. ^d==idims | .or.}) then
5245 ixbmin^d=ixcmin^d-ix^d;
5246 ixbmax^d=ixcmax^d-ix^d;
5247 ff(ixc^s,idims)=ff(ixc^s,idims)+ffc(ixb^s,idims)
5248 end if
5249 {end do\}
5250 ff(ixc^s,idims)=ff(ixc^s,idims)*0.5d0**(ndim-1)
5251 end do
5252 src=0.d0
5253 if(slab_uniform) then
5254 do idims=1,ndim
5255 ff(ixa^s,idims)=dxinv(idims)*ff(ixa^s,idims)
5256 ixb^l=ixo^l-kr(idims,^d);
5257 src(ixo^s)=src(ixo^s)+ff(ixo^s,idims)-ff(ixb^s,idims)
5258 end do
5259 else
5260 do idims=1,ndim
5261 ff(ixa^s,idims)=ff(ixa^s,idims)*block%surfaceC(ixa^s,idims)
5262 ixb^l=ixo^l-kr(idims,^d);
5263 src(ixo^s)=src(ixo^s)+ff(ixo^s,idims)-ff(ixb^s,idims)
5264 end do
5265 src(ixo^s)=src(ixo^s)/block%dvolume(ixo^s)
5266 end if
5267 end subroutine get_flux_on_cell_face
5268
5269 !> Calculates the explicit dt for the ambipolar term
5270 !> This function is used by both explicit scheme and STS method
5271 function get_ambipolar_dt(w,ixI^L,ixO^L,dx^D,x) result(dtnew)
5273
5274 integer, intent(in) :: ixi^l, ixo^l
5275 double precision, intent(in) :: dx^d, x(ixi^s,1:ndim)
5276 double precision, intent(in) :: w(ixi^s,1:nw)
5277 double precision :: dtnew
5278
5279 double precision :: coef
5280 double precision :: dxarr(ndim)
5281 double precision :: tmp(ixi^s)
5282
5283 ^d&dxarr(^d)=dx^d;
5284 tmp(ixo^s) = mhd_mag_en_all(w, ixi^l, ixo^l)
5285 call multiplyambicoef(ixi^l,ixo^l,tmp,w,x)
5286 ! now we have -mhd_eta_ambi B^2 /rho^2 in tmp
5287 coef = maxval(dabs(tmp(ixo^s)))
5288 if(coef/=0.d0) then
5289 coef=1.d0/coef
5290 else
5291 coef=bigdouble
5292 end if
5293 if(slab_uniform) then
5294 dtnew=minval(dxarr(1:ndim))**2.0d0*coef
5295 else
5296 dtnew=minval(block%ds(ixo^s,1:ndim))**2.0d0*coef
5297 end if
5298
5299 end function get_ambipolar_dt
5300
5301 !> multiply res by the ambipolar coefficient
5302 !> The ambipolar coefficient is calculated as -mhd_eta_ambi/rho^2
5303 !> The user may mask its value in the user file
5304 !> by implementing usr_mask_ambipolar subroutine
5305 subroutine multiplyambicoef(ixI^L,ixO^L,res,w,x)
5307 integer, intent(in) :: ixi^l, ixo^l
5308 double precision, intent(in) :: w(ixi^s,1:nw), x(ixi^s,1:ndim)
5309 double precision, intent(inout) :: res(ixi^s)
5310 double precision :: tmp(ixi^s)
5311 double precision :: rho(ixi^s)
5312
5313 call mhd_get_rho(w,x,ixi^l,ixi^l,rho)
5314 tmp(ixi^s)=-mhd_eta_ambi/rho(ixi^s)**2
5315 if (associated(usr_mask_ambipolar)) then
5316 call usr_mask_ambipolar(ixi^l,ixo^l,w,x,tmp)
5317 end if
5318 res(ixo^s) = tmp(ixo^s) * res(ixo^s)
5319
5320 end subroutine multiplyambicoef
5321
5322 !> w[iws]=w[iws]+qdt*S[iws,wCT] where S is the source based on wCT within ixO
5323 subroutine mhd_add_source(qdt,dtfactor,ixI^L,ixO^L,wCT,wCTprim,w,x,qsourcesplit,active)
5328 use mod_cak_force, only: cak_add_source
5329
5330 integer, intent(in) :: ixi^l, ixo^l
5331 double precision, intent(in) :: qdt,dtfactor
5332 double precision, intent(in) :: wct(ixi^s,1:nw),wctprim(ixi^s,1:nw), x(ixi^s,1:ndim)
5333 double precision, intent(inout) :: w(ixi^s,1:nw)
5334 logical, intent(in) :: qsourcesplit
5335 logical, intent(inout) :: active
5336
5337 !TODO local_timestep support is only added for splitting
5338 ! but not for other nonideal terms such gravity, RC, viscosity,..
5339 ! it will also only work for divbfix 'linde', which does not require
5340 ! modification as it does not use dt in the update
5341
5342 if (.not. qsourcesplit) then
5343 if(mhd_uawsom) then
5344 active = .true.
5345 call mhd_add_source_uawsom(qdt,ixi^l,ixo^l,wct,wctprim,w,x)
5346 end if
5347 if(mhd_internal_e) then
5348 ! Source for solving internal energy
5349 active = .true.
5350 call add_source_internal_e(qdt,ixi^l,ixo^l,wct,wctprim,w,x)
5351 else
5352 if(has_equi_rho_and_p) then
5353 active = .true.
5354 call add_equi_terms(qdt,dtfactor,ixi^l,ixo^l,wct,wctprim,w,x)
5355 end if
5356 end if
5357
5358 if(mhd_hyperbolic_tc) then
5359 active = .true.
5360 call add_source_hyperbolic_tc(qdt,ixi^l,ixo^l,wct,wctprim,w,x)
5361 end if
5362
5363 ! Source for B0 splitting
5364 if (b0field) then
5365 active = .true.
5366 ! this adds source to momentum of type J0 x B0 and to energy equation
5367 ! latter always + J0 * E (electric field being E_ideal, E_hall, E_ambi)
5368 ! used for total energy variants
5369 call add_source_b0split(qdt,dtfactor,ixi^l,ixo^l,wct,wctprim,w,x)
5370 end if
5371
5372 ! Sources for resistivity in eqs. for e, B1, B2 and B3
5373 if (abs(mhd_eta)>smalldouble)then
5374 active = .true.
5375 call add_source_res_exp(qdt,ixi^l,ixo^l,wct,wctprim,w,x)
5376 end if
5377
5378 if (mhd_ambipolar_exp)then
5379 active = .true.
5380 call add_source_ambi_exp(qdt,ixi^l,ixo^l,wct,w,x)
5381 end if
5382
5383 if (mhd_eta_hyper>0.d0)then
5384 active = .true.
5385 call add_source_hyperres(qdt,ixi^l,ixo^l,wct,w,x)
5386 end if
5387
5388 if(mhd_hydrodynamic_e) then
5389 ! Source for solving hydrodynamic energy
5390 active = .true.
5391 call add_source_hydrodynamic_e(qdt,ixi^l,ixo^l,wct,wctprim,w,x)
5392 else if (mhd_semirelativistic) then
5393 ! add sources for semirelativistic MHD
5394 active = .true.
5395 call add_source_semirelativistic(qdt,ixi^l,ixo^l,wct,wctprim,w,x)
5396 end if
5397 end if
5398
5399 {^nooned
5400 if(source_split_divb .eqv. qsourcesplit) then
5401 ! Sources related to div B
5402 select case (type_divb)
5403 case (divb_ct)
5404 continue ! Do nothing
5405 case (divb_linde)
5406 active = .true.
5407 call add_source_linde(qdt,ixi^l,ixo^l,wct,w,x)
5408 case (divb_glm)
5409 active = .true.
5410 call add_source_glm(qdt,ixi^l,ixo^l,wct,w,x)
5411 case (divb_powel)
5412 active = .true.
5413 call add_source_powel(qdt,ixi^l,ixo^l,wctprim,w,x)
5414 case (divb_janhunen)
5415 active = .true.
5416 call add_source_janhunen(qdt,ixi^l,ixo^l,wctprim,w,x)
5417 case (divb_lindejanhunen)
5418 active = .true.
5419 call add_source_linde(qdt,ixi^l,ixo^l,wct,w,x)
5420 call add_source_janhunen(qdt,ixi^l,ixo^l,wctprim,w,x)
5421 case (divb_lindepowel)
5422 active = .true.
5423 call add_source_linde(qdt,ixi^l,ixo^l,wct,w,x)
5424 call add_source_powel(qdt,ixi^l,ixo^l,wctprim,w,x)
5425 case (divb_lindeglm)
5426 active = .true.
5427 call add_source_linde(qdt,ixi^l,ixo^l,wct,w,x)
5428 call add_source_glm(qdt,ixi^l,ixo^l,wct,w,x)
5429 case (divb_multigrid)
5430 continue ! Do nothing
5431 case (divb_none)
5432 ! Do nothing
5433 case default
5434 call mpistop('Unknown divB fix')
5435 end select
5436 end if
5437 }
5438
5439 if(mhd_radiative_cooling) then
5440 call radiative_cooling_add_source(qdt,ixi^l,ixo^l,wct,wctprim,&
5441 w,x,qsourcesplit,active, rc_fl)
5442 end if
5443
5444 ! The radiative transfer heating, solved once per step, is added unsplit at every stage
5445 if(mhd_radiative_transfer .and. .not. qsourcesplit) then
5446 active = .true.
5447 call rt_sc_add_source(qdt,ixi^l,ixo^l,wct,w,x,mhd_radiative_cooling,mhd_internal_e)
5448 end if
5449
5450 if(mhd_viscosity) then
5451 call viscosity_add_source(qdt,ixi^l,ixo^l,wct,wctprim,&
5452 w,x,mhd_energy,qsourcesplit,active)
5453 end if
5454
5455 if(mhd_gravity) then
5456 call gravity_add_source(qdt,ixi^l,ixo^l,wct,wctprim,&
5457 w,x,gravity_energy,qsourcesplit,active)
5458 end if
5459
5460 if (mhd_cak_force) then
5461 call cak_add_source(qdt,ixi^l,ixo^l,wct,w,x,mhd_energy,qsourcesplit,active)
5462 end if
5463
5464 ! This is where the radiation force and heating/cooling are added
5465 if (mhd_radiation_fld) then
5466 call mhd_add_radiation_source(qdt,ixi^l,ixo^l,wct,wctprim,w,x,qsourcesplit,active)
5467 endif
5468
5469 end subroutine mhd_add_source
5470
5471 subroutine mhd_add_radiation_source(qdt,ixI^L,ixO^L,wCT,wCTprim,w,x,qsourcesplit,active)
5472 use mod_constants
5474 use mod_usr_methods
5475 use mod_fld
5476
5477 integer, intent(in) :: ixi^l, ixo^l
5478 double precision, intent(in) :: qdt, x(ixi^s,1:ndim)
5479 double precision, intent(in) :: wct(ixi^s,1:nw),wctprim(ixi^s,1:nw)
5480 double precision, intent(inout) :: w(ixi^s,1:nw)
5481 logical, intent(in) :: qsourcesplit
5482 logical, intent(inout) :: active
5483
5484 ! add radiation force and work done by it, changes momentum and gas energy
5485 ! handle photon tiring, heating and cooling exchange between gas and radiation field
5486 call add_fld_rad_force(qdt,ixi^l,ixo^l,wct,wctprim,w,x,qsourcesplit,active,fld_fl)
5487
5488 end subroutine mhd_add_radiation_source
5489
5490 !> add some source terms to total energy related to has_equi_rho_and_p=T
5491 subroutine add_equi_terms(qdt,dtfactor,ixI^L,ixO^L,wCT,wCTprim,w,x)
5493 use mod_geometry
5494 use mod_usr_methods
5495
5496 integer, intent(in) :: ixi^l, ixo^l
5497 double precision, intent(in) :: qdt,dtfactor
5498 double precision, intent(in) :: wct(ixi^s,1:nw), x(ixi^s,1:ndim)
5499 double precision, intent(in) :: wctprim(ixi^s,1:nw)
5500 double precision, intent(inout) :: w(ixi^s,1:nw)
5501
5502 double precision :: divv(ixi^s)
5503 double precision :: a(ixi^s,3), b(ixi^s,3), axb(ixi^s,3)
5504 double precision :: gravity_field(ixi^s,1:ndim)
5505 integer :: idir
5506
5507 if(slab_uniform) then
5508 if(nghostcells .gt. 2) then
5509 call divvector(wctprim(ixi^s,mom(1:ndir)),ixi^l,ixo^l,divv,3)
5510 else
5511 call divvector(wctprim(ixi^s,mom(1:ndir)),ixi^l,ixo^l,divv,2)
5512 end if
5513 else
5514 call divvector(wctprim(ixi^s,mom(1:ndir)),ixi^l,ixo^l,divv)
5515 end if
5516 divv(ixo^s)=divv(ixo^s)*eos%gamma*eos%inv_gamma_minus_1
5517 if(local_timestep) then
5518 w(ixo^s,e_)=w(ixo^s,e_)-dtfactor*block%dt(ixo^s)*block%equi_vars(ixo^s,equi_pe0_,0)*divv(ixo^s)
5519 else
5520 w(ixo^s,e_)=w(ixo^s,e_)-qdt*block%equi_vars(ixo^s,equi_pe0_,0)*divv(ixo^s)
5521 end if
5522 if(b0field)then
5523 if(b0field_forcefree.and.mhd_gravity)then
5524 ! add -v dot(rho_0 g)/(gamma-1)
5525 call usr_gravity(ixi^l,ixo^l,wct,x,gravity_field)
5526 do idir=1,ndim
5527 w(ixo^s,e_)=w(ixo^s,e_)-qdt*wctprim(ixo^s,mom(idir))*block%equi_vars(ixo^s,equi_rho0_,0)*gravity_field(ixo^s,idir)*eos%inv_gamma_minus_1
5528 enddo
5529 else
5530 a=0.d0
5531 b=0.d0
5532 ! store B0 magnetic field in b
5533 b(ixo^s,1:ndir)=block%B0(ixo^s,1:ndir,0)
5534 ! store J0 current in a
5535 do idir=7-2*ndir,3
5536 a(ixo^s,idir)=block%J0(ixo^s,idir)
5537 end do
5538 call cross_product(ixi^l,ixo^l,a,b,axb)
5539 ! add -v dot(rho_0 g + J0 x B_0)/(gamma-1)
5540 do idir=1,ndir
5541 w(ixo^s,e_)=w(ixo^s,e_)-qdt*wctprim(ixo^s,mom(idir))*axb(ixo^s,idir)*eos%inv_gamma_minus_1
5542 enddo
5543 if(mhd_gravity)then
5544 ! add -v dot(rho_0 g)/(gamma-1)
5545 call usr_gravity(ixi^l,ixo^l,wct,x,gravity_field)
5546 do idir=1,ndim
5547 w(ixo^s,e_)=w(ixo^s,e_)-qdt*wctprim(ixo^s,mom(idir))*block%equi_vars(ixo^s,equi_rho0_,0)*gravity_field(ixo^s,idir)*eos%inv_gamma_minus_1
5548 enddo
5549 endif
5550 endif
5551 else
5552 if(mhd_gravity)then
5553 ! add -v dot(rho_0 g)/(gamma-1)
5554 call usr_gravity(ixi^l,ixo^l,wct,x,gravity_field)
5555 do idir=1,ndim
5556 w(ixo^s,e_)=w(ixo^s,e_)-qdt*wctprim(ixo^s,mom(idir))*block%equi_vars(ixo^s,equi_rho0_,0)*gravity_field(ixo^s,idir)*eos%inv_gamma_minus_1
5557 enddo
5558 endif
5559 endif
5560 end subroutine add_equi_terms
5561
5562 !> Common direction for hyperbolic heat-flux relaxation and energy transport.
5563 !> B must contain the total field, including B0 when splitting is enabled.
5564 !> Normalize every finite nonzero field with max-component scaling.
5565 !> Only the zero field has no direction; no weak-field cutoff is applied.
5566 pure function b_unit_vector(B) result(bunit)
5567 ! Accept strided cell slices without forcing a contiguous input copy.
5568 ! Callers supply exactly ndir total-field components.
5569 double precision, intent(in) :: b(:)
5570 double precision :: bunit(^nc), maxm, scaled_norm
5571
5572 maxm=maxval(abs(b))
5573 if(maxm==0.d0) then
5574 bunit=0.d0
5575 else
5576 ^c&bunit(^c)=b(^c)/maxm\
5577 scaled_norm=dsqrt(^c&bunit(^c)**2+)
5578 ! At least one scaled component has magnitude one, so scaled_norm >= 1.
5579 ^c&bunit(^c)=bunit(^c)/scaled_norm\
5580 end if
5581 end function b_unit_vector
5582
5583 subroutine mhd_get_hyperbolic_tc_nperp(w,x,ixI^L,ixO^L,Bvec,nperp)
5585 use mod_geometry, only: gradient
5586 integer, intent(in) :: ixi^l,ixo^l
5587 double precision, intent(in) :: w(ixi^s,nw) ! primitive w
5588 double precision, intent(in) :: x(ixi^s,1:ndim)
5589 double precision, intent(in) :: bvec(ixi^s,1:ndir)
5590 double precision, intent(out) :: nperp(ixi^s,1:ndir)
5591
5592 double precision :: r(ixi^s), te(ixi^s), bgradt
5593 double precision :: bunitvec(ndir), gradt(ixi^s,ndir), gradt_perp(ndir)
5594 integer :: ix^d, idir
5595
5596 call mhd_get_rho(w,x,ixi^l,ixi^l,te)
5597 call eos%get_Rfactor(w,x,ixi^l,ixi^l,r)
5598 te(ixi^s)=w(ixi^s,p_)/(te(ixi^s)*r(ixi^s))
5599 if(.not. slab_uniform) then
5600 do idir=1,ndim
5601 call gradient(te,ixi^l,ixo^l,idir,gradt(ixi^s,idir))
5602 end do
5603 if(ndir>ndim) gradt(ixi^s,ndir)=0.d0
5604 end if
5605
5606 {^iftwod
5607 do ix2=ixomin2,ixomax2
5608 do ix1=ixomin1,ixomax1
5609 bunitvec=b_unit_vector(bvec(ix^d,1:ndir))
5610 if(slab_uniform) then
5611 gradt(ix^d,1)=((8.d0*(te(ix1+1,ix2)-te(ix1-1,ix2))-te(ix1+2,ix2)+te(ix1-2,ix2))/12.d0)/block%ds(ix^d,1)
5612 gradt(ix^d,2)=((8.d0*(te(ix1,ix2+1)-te(ix1,ix2-1))-te(ix1,ix2+2)+te(ix1,ix2-2))/12.d0)/block%ds(ix^d,2)
5613 if(ndir>2) gradt(ix^d,ndir)=zero
5614 end if
5615
5616 bgradt=(^c&bunitvec(^c)*gradt(ix^d,^c)+)
5617 {^c& gradt_perp(^c)=gradt(ix^d,^c)-bgradt*bunitvec(^c)\}
5618 nperp(ix^d,1:ndir)=b_unit_vector(gradt_perp(1:ndir))
5619 end do
5620 end do
5621 }
5622 {^ifthreed
5623 do ix3=ixomin3,ixomax3
5624 do ix2=ixomin2,ixomax2
5625 do ix1=ixomin1,ixomax1
5626 bunitvec=b_unit_vector(bvec(ix^d,1:ndir))
5627
5628 if(slab_uniform) then
5629 gradt(ix^d,1)=((8.d0*(te(ix1+1,ix2,ix3)-te(ix1-1,ix2,ix3))-te(ix1+2,ix2,ix3)+te(ix1-2,ix2,ix3))/12.d0)/block%ds(ix^d,1)
5630 gradt(ix^d,2)=((8.d0*(te(ix1,ix2+1,ix3)-te(ix1,ix2-1,ix3))-te(ix1,ix2+2,ix3)+te(ix1,ix2-2,ix3))/12.d0)/block%ds(ix^d,2)
5631 gradt(ix^d,3)=((8.d0*(te(ix1,ix2,ix3+1)-te(ix1,ix2,ix3-1))-te(ix1,ix2,ix3+2)+te(ix1,ix2,ix3-2))/12.d0)/block%ds(ix^d,3)
5632 end if
5633
5634 bgradt=(^c&bunitvec(^c)*gradt(ix^d,^c)+)
5635 {^c& gradt_perp(^c)=gradt(ix^d,^c)-bgradt*bunitvec(^c)\}
5636 nperp(ix^d,1:ndir)=b_unit_vector(gradt_perp(1:ndir))
5637 end do
5638 end do
5639 end do
5640 }
5641 end subroutine mhd_get_hyperbolic_tc_nperp
5642
5643 subroutine mhd_get_hyperbolic_tc_geometry(ixI^L,ixO^L,Te,Bvec,bgradT,gradTperp_mag,nperp)
5645 use mod_geometry, only: gradient
5646 integer, intent(in) :: ixi^l,ixo^l
5647 double precision, intent(in) :: te(ixi^s)
5648 double precision, intent(in) :: bvec(ixi^s,1:ndir)
5649 double precision, intent(out) :: bgradt(ixi^s), gradtperp_mag(ixi^s)
5650 double precision, intent(out) :: nperp(ixi^s,1:ndir)
5651
5652 double precision :: bunitvec(ndir), gradt(ixi^s,ndir), gradt_perp(ndir)
5653 double precision :: maxm
5654 integer :: ix^d, idir
5655
5656 if(.not. slab_uniform) then
5657 do idir=1,ndim
5658 call gradient(te,ixi^l,ixo^l,idir,gradt(ixi^s,idir))
5659 end do
5660 if(ndir>ndim) gradt(ixi^s,ndir)=0.d0
5661 end if
5662
5663 {^iftwod
5664 do ix2=ixomin2,ixomax2
5665 do ix1=ixomin1,ixomax1
5666 bunitvec=b_unit_vector(bvec(ix^d,1:ndir))
5667 if(slab_uniform) then
5668 gradt(ix^d,1)=((8.d0*(te(ix1+1,ix2)-te(ix1-1,ix2))-te(ix1+2,ix2)+te(ix1-2,ix2))/12.d0)/block%ds(ix^d,1)
5669 gradt(ix^d,2)=((8.d0*(te(ix1,ix2+1)-te(ix1,ix2-1))-te(ix1,ix2+2)+te(ix1,ix2-2))/12.d0)/block%ds(ix^d,2)
5670 if(ndir>2) gradt(ix^d,3)=zero
5671 end if
5672
5673 bgradt(ix^d)=(^c&bunitvec(^c)*gradt(ix^d,^c)+)
5674 {^c& gradt_perp(^c)=gradt(ix^d,^c)-bgradt(ix^d)*bunitvec(^c)\}
5675 maxm=maxval(abs(gradt_perp))
5676 if(maxm==0.d0) then
5677 gradtperp_mag(ix^d)=zero
5678 {^c& nperp(ix^d,^c)=zero\}
5679 else
5680 ^c&nperp(ix^d,^c)=gradt_perp(^c)/maxm\
5681 gradtperp_mag(ix^d)=dsqrt(^c&nperp(ix^d,^c)**2+)
5682 ^c&nperp(ix^d,^c)=nperp(ix^d,^c)/gradtperp_mag(ix^d)\
5683 gradtperp_mag(ix^d)=gradtperp_mag(ix^d)*maxm
5684 end if
5685 end do
5686 end do
5687 }
5688 {^ifthreed
5689 do ix3=ixomin3,ixomax3
5690 do ix2=ixomin2,ixomax2
5691 do ix1=ixomin1,ixomax1
5692 bunitvec=b_unit_vector(bvec(ix^d,1:ndir))
5693
5694 if(slab_uniform) then
5695 gradt(ix^d,1)=((8.d0*(te(ix1+1,ix2,ix3)-te(ix1-1,ix2,ix3))-te(ix1+2,ix2,ix3)+te(ix1-2,ix2,ix3))/12.d0)/block%ds(ix^d,1)
5696 gradt(ix^d,2)=((8.d0*(te(ix1,ix2+1,ix3)-te(ix1,ix2-1,ix3))-te(ix1,ix2+2,ix3)+te(ix1,ix2-2,ix3))/12.d0)/block%ds(ix^d,2)
5697 gradt(ix^d,3)=((8.d0*(te(ix1,ix2,ix3+1)-te(ix1,ix2,ix3-1))-te(ix1,ix2,ix3+2)+te(ix1,ix2,ix3-2))/12.d0)/block%ds(ix^d,3)
5698 end if
5699
5700 bgradt(ix^d)=(^c&bunitvec(^c)*gradt(ix^d,^c)+)
5701 {^c& gradt_perp(^c)=gradt(ix^d,^c)-bgradt(ix^d)*bunitvec(^c)\}
5702 maxm=maxval(abs(gradt_perp))
5703 if(maxm==0.d0) then
5704 gradtperp_mag(ix^d)=zero
5705 {^c& nperp(ix^d,^c)=zero\}
5706 else
5707 ^c&nperp(ix^d,^c)=gradt_perp(^c)/maxm\
5708 gradtperp_mag(ix^d)=dsqrt(^c&nperp(ix^d,^c)**2+)
5709 ^c&nperp(ix^d,^c)=nperp(ix^d,^c)/gradtperp_mag(ix^d)\
5710 gradtperp_mag(ix^d)=gradtperp_mag(ix^d)*maxm
5711 end if
5712 end do
5713 end do
5714 end do
5715 }
5716 end subroutine mhd_get_hyperbolic_tc_geometry
5717
5718 subroutine add_source_hyperbolic_tc(qdt,ixI^L,ixO^L,wCT,wCTprim,w,x)
5720 use mod_geometry, only: gradient
5721 integer, intent(in) :: ixi^l,ixo^l
5722 double precision, intent(in) :: qdt
5723 double precision, dimension(ixI^S,1:ndim), intent(in) :: x
5724 double precision, dimension(ixI^S,1:nw), intent(in) :: wct,wctprim
5725 double precision, dimension(ixI^S,1:nw), intent(inout) :: w
5726
5727 double precision, dimension(ixI^S) :: r,te,rho_loc,pth_loc,cs2_loc
5728 double precision, dimension(ixI^S) :: ne_loc,nh_dummy
5729 double precision, dimension(ixI^S,1:ndir) :: bvec
5730 double precision, dimension(ixI^S) :: bgradt, gradtperp_mag
5731 double precision, dimension(ixI^S,1:ndir) :: nperp
5732 double precision, dimension(ixI^S) :: gradt_geom
5733 double precision, parameter :: xe_prefac_cgs = 4.753567596681522d6
5734 double precision :: kappa_t5,kappa_t5_perp,kappa_t5_perp_eff
5735 double precision :: kappa_t7,f_sat,kappat5_bgradt,kappat5_gradtperp,tau,b2,fb,gradt1
5736 double precision :: bunit_tc(ndir)
5737 double precision :: qclass_diss
5738 double precision :: bmag_loc,tloc,tcond,nloc_code,cchi,chi
5739 double precision :: cmax(ndim),c2,cfast2,avmincs2(ndim),inv_rho
5740 integer :: ix^d,idir
5741 logical :: use_perp_source
5742
5743 ! xe_prefac_cgs expects B [G], T [K], and ne [cm^-3]. Convert the
5744 ! normalisation units explicitly in SI mode, while retaining code-unit
5745 ! state variables in the cell loop below.
5746 cchi=zero
5747 if(htc_perp_mode_id==htc_perp_electron) then
5748 if(si_unit) then
5749 cchi = (xe_prefac_cgs/mhd_hyperbolic_tc_coulomb_log) * &
5750 (1.d4*unit_magneticfield)*unit_temperature**1.5d0 / &
5751 (1.d-6*unit_numberdensity)
5752 else
5753 cchi = (xe_prefac_cgs/mhd_hyperbolic_tc_coulomb_log) * &
5755 end if
5756 end if
5757 if (eos%eos_type == 'LTE' .or. eos%eos_type == 'PI') then
5758 te(ixi^s) = wct(ixi^s, te_)
5759 else
5760 call eos%get_Rfactor(wct,x,ixi^l,ixi^l,r)
5761 {do ix^db=iximin^db,iximax^db\}
5762 if(has_equi_rho_and_p) then
5763 rho_loc(ix^d)=wctprim(ix^d,rho_)+block%equi_vars(ix^d,equi_rho0_,0)
5764 pth_loc(ix^d)=wctprim(ix^d,p_)+block%equi_vars(ix^d,equi_pe0_,0)
5765 else
5766 rho_loc(ix^d)=wctprim(ix^d,rho_)
5767 pth_loc(ix^d)=wctprim(ix^d,p_)
5768 end if
5769 te(ix^d)=pth_loc(ix^d)/(r(ix^d)*rho_loc(ix^d))
5770 {end do\}
5771 end if
5772 ! sound speed from the EoS (table Gamma_1 under LTE); the equi split keeps gamma p/rho
5773 if(has_equi_rho_and_p) then
5774 cs2_loc(ixi^s)=eos%gamma*pth_loc(ixi^s)/rho_loc(ixi^s)
5775 else
5776 call eos%get_csound2(wctprim,x,ixi^l,ixi^l,cs2_loc)
5777 end if
5778 ! Electron number density in code units. Under the FI/PI normalisation,
5779 ! rho_code equals nH_code. FI uses the composition-dependent fully ionised
5780 ! electron count. PI obtains the local electron count from its R factor:
5781 ! R*(2+3 A_He) = (n_nuclei+n_e)/n_H. LTE stores ne explicitly.
5782 if(htc_perp_mode_id==htc_perp_electron) then
5783 if(eos%eos_type=='LTE') then
5784 call eos%get_ne_nH(ixi^l,ixi^l,wct, x,ne_loc,nh_dummy)
5785 else if(eos%eos_type=='PI') then
5786 ne_loc(ixi^s)=rho_loc(ixi^s)*max(r(ixi^s)*(2.d0+3.d0*eos%He_abundance) &
5787 -(1.d0+eos%He_abundance),smalldouble)
5788 else
5789 ne_loc(ixi^s)=rho_loc(ixi^s)*(1.d0+2.d0*eos%He_abundance)
5790 end if
5791 end if
5792 use_perp_source = mhd_hyperbolic_tc_use_perp
5793 if(b0field) then
5794 {do ix^db=ixomin^db,ixomax^db\}
5795 {^c& bvec(ix^d,^c)=wct(ix^d,mag(^c))+block%B0(ix^d,^c,0)\}
5796 {end do\}
5797 else
5798 {do ix^db=ixomin^db,ixomax^db\}
5799 {^c& bvec(ix^d,^c)=wct(ix^d,mag(^c))\}
5800 {end do\}
5801 end if
5802 {^nooned
5803 call mhd_get_hyperbolic_tc_geometry(ixi^l,ixo^l,te,bvec,bgradt,gradtperp_mag,nperp)
5804 }
5805 {^ifoned
5806 gradt_geom=zero
5807 if(.not.slab_uniform) then
5808 call gradient(te,ixi^l,ixo^l,1,gradt_geom)
5809 end if
5810 do ix1=ixomin1,ixomax1
5813 kappa_t7=kappa_t5*te(ix1)
5814 else
5815 tcond = te(ix1)
5816 if(mhd_trac) then
5817 tcond = max(tcond, block%wextra(ix1,tcoff_))
5818 end if
5819 kappa_t5=mhd_hyperbolic_tc_kappa*sqrt(tcond**5)
5820 kappa_t7=kappa_t5*tcond
5821 end if
5822 if(slab_uniform) then
5823 gradt1=((8.d0*(te(ix1+1)-te(ix1-1))-te(ix1+2)+te(ix1-2))/12.d0)/block%ds(ix1,1)
5824 else
5825 gradt1=gradt_geom(ix1)
5826 end if
5827 b2=zero
5828 {^c& b2=b2+bvec(ix1,^c)**2\}
5829 bunit_tc=b_unit_vector(bvec(ix1,1:ndir))
5830 bgradt(ix1)=bunit_tc(1)*gradt1
5831 kappat5_bgradt=kappa_t5*bgradt(ix1)
5832 inv_rho=1.d0/rho_loc(ix1)
5833 c2=cs2_loc(ix1)
5834 cfast2 = b2*inv_rho + c2
5835 avmincs2(1) = cfast2**2 - 4.0d0*c2*bvec(ix1,1)**2*inv_rho
5836 cmax(1) = sqrt(half*(cfast2 + sqrt(dabs(avmincs2(1)))))
5837 if(mhd_hyperbolic_tc_sat) then
5838 f_sat=one/(one+dabs(kappat5_bgradt)/(1.5d0*rho_loc(ix^d)*(pth_loc(ix^d)/rho_loc(ix^d))**1.5d0))
5839 tau=max(4.d0*dt, f_sat*kappa_t7*courantpar**2/(pth_loc(ix^d)*eos%inv_gamma_minus_1*cmax(1)**2))
5840 w(ix^d,qpar_)=w(ix^d,qpar_)-qdt*(f_sat*kappat5_bgradt+wct(ix^d,qpar_))/tau
5841 else
5842 w(ix^d,qpar_)=w(ix^d,qpar_)-qdt*(kappat5_bgradt+wct(ix^d,qpar_))/&
5843 max(4.d0*dt, kappa_t7*courantpar**2/(pth_loc(ix^d)*eos%inv_gamma_minus_1*cmax(1)**2))
5844 end if
5845 end do
5846 }
5847 {^iftwod
5848 do ix2=ixomin2,ixomax2
5849 do ix1=ixomin1,ixomax1
5852 kappa_t7=kappa_t5*te(ix^d)
5853 else
5854 tcond=te(ix^d)
5855 if(mhd_trac) then
5856 tcond=max(tcond, block%wextra(ix^d,tcoff_))
5857 end if
5858 kappa_t5=mhd_hyperbolic_tc_kappa*sqrt(tcond**5)
5859 kappa_t7 = kappa_t5*tcond
5860 end if
5861 kappat5_bgradt=kappa_t5*bgradt(ix^d)
5862 b2 = zero
5863 {^c& b2 = b2 + bvec(ix^d,^c)**2\}
5864 if(use_perp_source) then
5865 select case(htc_perp_mode_id)
5866 case(htc_perp_fixed)
5867 kappa_t5_perp=mhd_hyperbolic_tc_kappa_perp_factor*kappa_t5
5868 case(htc_perp_weak_field)
5869 if(mhd_hyperbolic_tc_bmin>zero) then
5870 fb=b2/(b2+mhd_hyperbolic_tc_bmin**2)
5871 else
5872 fb=one
5873 end if
5874 kappa_t5_perp_eff=(one-fb)*kappa_t5
5875 kappa_t5_perp=kappa_t5_perp_eff
5876 case(htc_perp_electron)
5877 bmag_loc = dsqrt(b2)
5878 tloc = max(te(ix^d), smalldouble)
5879 nloc_code = max(ne_loc(ix^d), smalldouble)
5880 chi = cchi*bmag_loc*tloc**1.5d0/nloc_code
5881 kappa_t5_perp_eff = kappa_t5/(one+chi**2)
5882 kappa_t5_perp = kappa_t5_perp_eff
5883 case default
5884 kappa_t5_perp=zero
5885 end select
5886 kappat5_gradtperp=kappa_t5_perp*gradtperp_mag(ix^d)
5887 end if
5888 inv_rho=1.d0/rho_loc(ix^d)
5889 c2=cs2_loc(ix^d)
5890 cfast2 = b2*inv_rho + c2
5891 {avmincs2(^d)=cfast2**2-4.0d0*c2*bvec(ix^dd,^d)**2*inv_rho; cmax(^d)=sqrt(half*(cfast2+sqrt(dabs(avmincs2(^d)))))\}
5892 if(mhd_hyperbolic_tc_sat) then
5893 qclass_diss=dabs(kappat5_bgradt)
5894 if(use_perp_source) &
5895 qclass_diss=dsqrt(kappat5_bgradt**2+kappat5_gradtperp**2)
5896 f_sat=one/(one+qclass_diss/(1.5d0*rho_loc(ix^d)*(pth_loc(ix^d)/rho_loc(ix^d))**1.5d0))
5897 tau=max(4.d0*dt, f_sat*kappa_t7*courantpar**2/(pth_loc(ix^d)*eos%inv_gamma_minus_1*maxval(cmax(:))**2))
5898 w(ix^d,qpar_)=w(ix^d,qpar_)-qdt*(f_sat*kappat5_bgradt+wct(ix^d,qpar_))/tau
5899 if(use_perp_source) then
5900 w(ix^d,qperp_)=w(ix^d,qperp_)-qdt*(f_sat*kappat5_gradtperp+wct(ix^d,qperp_))/tau
5901 end if
5902 else
5903 tau=max(4.d0*dt, kappa_t7*courantpar**2/(pth_loc(ix^d)*eos%inv_gamma_minus_1*maxval(cmax(:))**2))
5904 w(ix^d,qpar_)=w(ix^d,qpar_)-qdt*(kappat5_bgradt+wct(ix^d,qpar_))/tau
5905 if(use_perp_source) then
5906 w(ix^d,qperp_)=w(ix^d,qperp_)-qdt*(kappat5_gradtperp+wct(ix^d,qperp_))/tau
5907 end if
5908 end if
5909 end do
5910 end do
5911 }
5912 {^ifthreed
5913 do ix3=ixomin3,ixomax3
5914 do ix2=ixomin2,ixomax2
5915 do ix1=ixomin1,ixomax1
5918 kappa_t7=kappa_t5*te(ix^d)
5919 else
5920 tcond=te(ix^d)
5921 if(mhd_trac) then
5922 tcond=max(tcond, block%wextra(ix^d,tcoff_))
5923 end if
5924 kappa_t5=mhd_hyperbolic_tc_kappa*sqrt(tcond**5)
5925 kappa_t7 = kappa_t5*tcond
5926 end if
5927 kappat5_bgradt=kappa_t5*bgradt(ix^d)
5928 b2 = zero
5929 {^c& b2 = b2 + bvec(ix^d,^c)**2\}
5930 if(use_perp_source) then
5931 select case(htc_perp_mode_id)
5932 case(htc_perp_fixed)
5933 kappa_t5_perp=mhd_hyperbolic_tc_kappa_perp_factor*kappa_t5
5934 case(htc_perp_weak_field)
5935 if(mhd_hyperbolic_tc_bmin>zero) then
5936 fb=b2/(b2+mhd_hyperbolic_tc_bmin**2)
5937 else
5938 fb=one
5939 end if
5940 kappa_t5_perp_eff=(one-fb)*kappa_t5
5941 kappa_t5_perp=kappa_t5_perp_eff
5942 case(htc_perp_electron)
5943 bmag_loc = dsqrt(b2)
5944 tloc = max(te(ix^d), smalldouble)
5945 nloc_code = max(ne_loc(ix^d), smalldouble)
5946 chi = cchi*bmag_loc*tloc**1.5d0/nloc_code
5947 kappa_t5_perp_eff = kappa_t5/(one+chi**2)
5948 kappa_t5_perp = kappa_t5_perp_eff
5949 case default
5950 kappa_t5_perp=zero
5951 end select
5952 kappat5_gradtperp=kappa_t5_perp*gradtperp_mag(ix^d)
5953 end if
5954 inv_rho=1.d0/rho_loc(ix^d)
5955 c2=cs2_loc(ix^d)
5956 cfast2 = b2*inv_rho + c2
5957 {avmincs2(^d)=cfast2**2-4.0d0*c2*bvec(ix^dd,^d)**2*inv_rho; cmax(^d)=sqrt(half*(cfast2+sqrt(dabs(avmincs2(^d)))))\}
5958 if(mhd_hyperbolic_tc_sat) then
5959 qclass_diss=dabs(kappat5_bgradt)
5960 if(use_perp_source) &
5961 qclass_diss=dsqrt(kappat5_bgradt**2+kappat5_gradtperp**2)
5962 f_sat=one/(one+qclass_diss/(1.5d0*rho_loc(ix^d)*(pth_loc(ix^d)/rho_loc(ix^d))**1.5d0))
5963 tau=max(4.d0*dt, f_sat*kappa_t7*courantpar**2/(pth_loc(ix^d)*eos%inv_gamma_minus_1*maxval(cmax(:))**2))
5964 w(ix^d,qpar_)=w(ix^d,qpar_)-qdt*(f_sat*kappat5_bgradt+wct(ix^d,qpar_))/tau
5965 if(use_perp_source) then
5966 w(ix^d,qperp_)=w(ix^d,qperp_)-qdt*(f_sat*kappat5_gradtperp+wct(ix^d,qperp_))/tau
5967 end if
5968 else
5969 tau=max(4.d0*dt, kappa_t7*courantpar**2/(pth_loc(ix^d)*eos%inv_gamma_minus_1*maxval(cmax(:))**2))
5970 w(ix^d,qpar_)=w(ix^d,qpar_)-qdt*(kappat5_bgradt+wct(ix^d,qpar_))/tau
5971 if(use_perp_source) then
5972 w(ix^d,qperp_)=w(ix^d,qperp_)-qdt*(kappat5_gradtperp+wct(ix^d,qperp_))/tau
5973 end if
5974 end if
5975 end do
5976 end do
5977 end do
5978 }
5979 end subroutine add_source_hyperbolic_tc
5980
5981 !> Compute the Lorentz force (JxB) Note: Unused subroutine
5982 !> perhaps useful for post-processing when made public
5983 subroutine get_lorentz_force(ixI^L,ixO^L,w,JxB)
5985 integer, intent(in) :: ixi^l, ixo^l
5986 double precision, intent(in) :: w(ixi^s,1:nw)
5987 double precision, intent(inout) :: jxb(ixi^s,3)
5988 double precision :: a(ixi^s,3), b(ixi^s,3)
5989 ! For ndir=2 only 3rd component of J can exist, ndir=1 is impossible for MHD
5990 double precision :: current(ixi^s,7-2*ndir:3)
5991 integer :: idir, idirmin
5992
5993 b=0.0d0
5994 if(b0field) then
5995 do idir = 1, ndir
5996 b(ixo^s, idir) = w(ixo^s,mag(idir))+block%B0(ixo^s,idir,0)
5997 end do
5998 else
5999 do idir = 1, ndir
6000 b(ixo^s, idir) = w(ixo^s,mag(idir))
6001 end do
6002 end if
6003
6004 ! store J current in a
6005 call get_current(w,ixi^l,ixo^l,idirmin,current)
6006
6007 a=0.0d0
6008 do idir=7-2*ndir,3
6009 a(ixo^s,idir)=current(ixo^s,idir)
6010 end do
6011
6012 call cross_product(ixi^l,ixo^l,a,b,jxb)
6013 end subroutine get_lorentz_force
6014
6015 subroutine mhd_get_rho(w,x,ixI^L,ixO^L,rho)
6017 integer, intent(in) :: ixi^l, ixo^l
6018 double precision, intent(in) :: w(ixi^s,1:nw),x(ixi^s,1:ndim)
6019 double precision, intent(out) :: rho(ixi^s)
6020
6021 if(has_equi_rho_and_p) then
6022 rho(ixo^s) = w(ixo^s,rho_) + block%equi_vars(ixo^s,equi_rho0_,b0i)
6023 else
6024 rho(ixo^s) = w(ixo^s,rho_)
6025 endif
6026
6027 end subroutine mhd_get_rho
6028
6029 !> handle small or negative internal energy
6030 subroutine mhd_handle_small_ei(w, x, ixI^L, ixO^L, ie, subname)
6033 integer, intent(in) :: ixi^l,ixo^l, ie
6034 double precision, intent(inout) :: w(ixi^s,1:nw)
6035 double precision, intent(in) :: x(ixi^s,1:ndim)
6036 character(len=*), intent(in) :: subname
6037
6038 double precision :: rho(ixi^s)
6039 integer :: idir
6040 logical :: flag(ixi^s,1:nw)
6041
6042 flag=.false.
6043 if(has_equi_rho_and_p) then
6044 where(w(ixo^s,ie)+block%equi_vars(ixo^s,equi_pe0_,0)*eos%inv_gamma_minus_1<small_e)&
6045 flag(ixo^s,ie)=.true.
6046 else
6047 where(w(ixo^s,ie)<small_e) flag(ixo^s,ie)=.true.
6048 endif
6049 if(any(flag(ixo^s,ie))) then
6050 select case (small_values_method)
6051 case ("replace")
6052 if(has_equi_rho_and_p) then
6053 where(flag(ixo^s,ie)) w(ixo^s,ie)=small_e - &
6054 block%equi_vars(ixo^s,equi_pe0_,0)*eos%inv_gamma_minus_1
6055 else
6056 where(flag(ixo^s,ie)) w(ixo^s,ie)=small_e
6057 endif
6058 case ("average")
6059 call small_values_average(ixi^l, ixo^l, w, x, flag, ie)
6060 case default
6061 ! small values error shows primitive variables
6062 w(ixo^s,e_)=w(ixo^s,e_)*eos%gamma_minus_1
6063 call mhd_get_rho(w,x,ixi^l,ixo^l,rho)
6064 do idir = 1, ndir
6065 w(ixo^s, mom(idir)) = w(ixo^s, mom(idir))/rho(ixo^s)
6066 end do
6067 call small_values_error(w, x, ixi^l, ixo^l, flag, subname)
6068 end select
6069 end if
6070
6071 end subroutine mhd_handle_small_ei
6072
6073
6074 !> Source terms after split off time-independent magnetic field
6075 subroutine add_source_b0split(qdt,dtfactor,ixI^L,ixO^L,wCT,wCTprim,w,x)
6077
6078 integer, intent(in) :: ixi^l, ixo^l
6079 double precision, intent(in) :: qdt, dtfactor,wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6080 double precision, intent(in) :: wctprim(ixi^s,1:nw)
6081 double precision, intent(inout) :: w(ixi^s,1:nw)
6082
6083 double precision :: a(ixi^s,3), b(ixi^s,3), axb(ixi^s,3)
6084 integer :: idir
6085
6086 a=0.d0
6087 b=0.d0
6088 ! for force-free field J0xB0 =0
6089 if((.not.b0field_forcefree).and.(.not.has_equi_rho_and_p)) then
6090 ! store B0 magnetic field in b
6091 b(ixo^s,1:ndir)=block%B0(ixo^s,1:ndir,0)
6092
6093 ! store J0 current in a
6094 do idir=7-2*ndir,3
6095 a(ixo^s,idir)=block%J0(ixo^s,idir)
6096 end do
6097 call cross_product(ixi^l,ixo^l,a,b,axb)
6098 if(local_timestep) then
6099 do idir=1,3
6100 axb(ixo^s,idir)=axb(ixo^s,idir)*block%dt(ixo^s)*dtfactor
6101 enddo
6102 else
6103 axb(ixo^s,:)=axb(ixo^s,:)*qdt
6104 endif
6105 ! add J0xB0 source term in momentum equations
6106 w(ixo^s,mom(1:ndir))=w(ixo^s,mom(1:ndir))+axb(ixo^s,1:ndir)
6107 end if
6108
6109 if(total_energy) then
6110 a=0.d0
6111 ! for free-free field -(vxB0) dot J0 =0
6112 b(ixo^s,:)=wctprim(ixo^s,mag(:))
6113 ! store full magnetic field B0+B1 in b
6114 if((.not.b0field_forcefree).and.(.not.has_equi_rho_and_p)) b(ixo^s,:)=b(ixo^s,:)+block%B0(ixo^s,:,0)
6115 ! store velocity in a
6116 a(ixi^s,1:ndir)=wctprim(ixi^s,mom(1:ndir))
6117 ! -E = a x b
6118 call cross_product(ixi^l,ixo^l,a,b,axb)
6119 if(local_timestep) then
6120 do idir=1,3
6121 axb(ixo^s,idir)=axb(ixo^s,idir)*block%dt(ixo^s)*dtfactor
6122 enddo
6123 else
6124 axb(ixo^s,:)=axb(ixo^s,:)*qdt
6125 endif
6126 ! add -(vxB) dot J0 source term in energy equation
6127 ! where it is adding -J0 dot (vxB_1) when appropriate
6128 do idir=7-2*ndir,3
6129 w(ixo^s,e_)=w(ixo^s,e_)-axb(ixo^s,idir)*block%J0(ixo^s,idir)
6130 end do
6131 if(mhd_hall) then
6132 ! store hall velocity in a, only partial current is needed
6133 call mhd_getv_hall(wct,x,ixi^l,ixo^l,a,.true.)
6134 ! -E = a x b
6135 call cross_product(ixi^l,ixo^l,a,b,axb)
6136 if(local_timestep) then
6137 do idir=1,3
6138 axb(ixo^s,idir)=axb(ixo^s,idir)*block%dt(ixo^s)*dtfactor
6139 enddo
6140 else
6141 axb(ixo^s,:)=axb(ixo^s,:)*qdt
6142 endif
6143 ! add -(vxB) dot J0 source term in energy equation
6144 do idir=7-2*ndir,3
6145 w(ixo^s,e_)=w(ixo^s,e_)-axb(ixo^s,idir)*block%J0(ixo^s,idir)
6146 end do
6147 endif
6148 if(mhd_ambipolar_sts) then
6149 ! in STS variant of ambipolar, we added for split B the term div(B_1xE_ambi)
6150 ! hence needs to add J_0 dot E_ambi
6151 ! to get finally the term etaA (J_perpB)^/B^2-B_1 dot (curl Eambi)
6152 !reuse axb
6153 call mhd_get_jxbxb(wct,x,ixi^l,ixo^l,axb)
6154 ! source J0 * E
6155 do idir=sdim,3
6156 !set electric field in jxbxb: E=nuA * jxbxb, where nuA=-etaA/rho^2
6157 call multiplyambicoef(ixi^l,ixo^l,axb(ixi^s,idir),wct,x)
6158 w(ixo^s,e_)=w(ixo^s,e_)+qdt*axb(ixo^s,idir)*block%J0(ixo^s,idir)
6159 enddo
6160 endif
6161 end if
6162
6163
6164 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_B0')
6165
6166 end subroutine add_source_b0split
6167
6168 !> Source terms for semirelativistic MHD Gombosi 2002 JCP 177, 176
6169 subroutine add_source_semirelativistic(qdt,ixI^L,ixO^L,wCT,wCTprim,w,x)
6171 use mod_geometry
6172
6173 integer, intent(in) :: ixi^l, ixo^l
6174 double precision, intent(in) :: qdt, wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6175 double precision, intent(in) :: wctprim(ixi^s,1:nw)
6176 double precision, intent(inout) :: w(ixi^s,1:nw)
6177
6178 double precision :: e(ixi^s,1:3),curle(ixi^s,1:3),dive(ixi^s),btot(1:3)
6179 integer :: idir, idirmin, ix^d
6180
6181 ! if ndir<3 the source is zero
6182 {^ifthreec
6183 {do ix^db=iximin^db,iximax^db\}
6184 ! E=Bxv with the total field, B taken from wCT like the velocity in wCTprim
6185 ^c&btot(^c)=wct(ix^d,b^c_)\
6186 if(b0field) then
6187 ^c&btot(^c)=btot(^c)+block%B0(ix^d,^c,0)\
6188 end if
6189 e(ix^d,1)=btot(2)*wctprim(ix^d,m3_)-btot(3)*wctprim(ix^d,m2_)
6190 e(ix^d,2)=btot(3)*wctprim(ix^d,m1_)-btot(1)*wctprim(ix^d,m3_)
6191 e(ix^d,3)=btot(1)*wctprim(ix^d,m2_)-btot(2)*wctprim(ix^d,m1_)
6192 {end do\}
6193 call divvector(e,ixi^l,ixo^l,dive)
6194 if(mhd_semirelati_curl_e) then
6195 call curlvector(e,ixi^l,ixo^l,curle,idirmin,1,3)
6196 else
6197 ! eq (28): E divE only, the steady-state limit of eq (26)
6198 curle=zero
6199 end if
6200 ! add source term in momentum equations (1/c0^2-1/c^2)(E divE - E x curlE)
6201 ! equation (26) and (27)
6202 {do ix^db=ixomin^db,ixomax^db\}
6203 w(ix^d,m1_)=w(ix^d,m1_)+qdt*(inv_squared_c0-inv_squared_c)*&
6204 (e(ix^d,1)*dive(ix^d)-e(ix^d,2)*curle(ix^d,3)+e(ix^d,3)*curle(ix^d,2))
6205 w(ix^d,m2_)=w(ix^d,m2_)+qdt*(inv_squared_c0-inv_squared_c)*&
6206 (e(ix^d,2)*dive(ix^d)-e(ix^d,3)*curle(ix^d,1)+e(ix^d,1)*curle(ix^d,3))
6207 w(ix^d,m3_)=w(ix^d,m3_)+qdt*(inv_squared_c0-inv_squared_c)*&
6208 (e(ix^d,3)*dive(ix^d)-e(ix^d,1)*curle(ix^d,2)+e(ix^d,2)*curle(ix^d,1) )
6209 {end do\}
6210 }
6211
6212 end subroutine add_source_semirelativistic
6213
6214 !> Source terms for internal energy version of MHD
6215 subroutine add_source_internal_e(qdt,ixI^L,ixO^L,wCT,wCTprim,w,x)
6217 use mod_geometry
6218
6219 integer, intent(in) :: ixi^l, ixo^l
6220 double precision, intent(in) :: qdt
6221 double precision, intent(in) :: wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6222 double precision, intent(in) :: wctprim(ixi^s,1:nw)
6223 double precision, intent(inout) :: w(ixi^s,1:nw)
6224
6225 double precision :: divv(ixi^s), tmp
6226 integer :: ix^d
6227
6228 if(slab_uniform) then
6229 if(nghostcells .gt. 2) then
6230 call divvector(wctprim(ixi^s,mom(:)),ixi^l,ixo^l,divv,3)
6231 else
6232 call divvector(wctprim(ixi^s,mom(:)),ixi^l,ixo^l,divv,2)
6233 end if
6234 else
6235 call divvector(wctprim(ixi^s,mom(:)),ixi^l,ixo^l,divv)
6236 end if
6237 {do ix^db=ixomin^db,ixomax^db\}
6238 tmp=w(ix^d,e_)
6239 w(ix^d,e_)=w(ix^d,e_)-qdt*wctprim(ix^d,p_)*divv(ix^d)
6240 if(w(ix^d,e_)<small_e) then
6241 w(ix^d,e_)=tmp
6242 end if
6243 {end do\}
6244 if(mhd_ambipolar_sts)then
6245 call add_source_ambipolar_internal_energy(qdt,ixi^l,ixo^l,wct,w,x)
6246 end if
6247
6248 if(fix_small_values) then
6249 call mhd_handle_small_ei(w,x,ixi^l,ixo^l,e_,'add_source_internal_e')
6250 end if
6251 end subroutine add_source_internal_e
6252
6253 !> Source terms for hydrodynamic energy version of MHD
6254 subroutine add_source_hydrodynamic_e(qdt,ixI^L,ixO^L,wCT,wCTprim,w,x)
6256 use mod_geometry
6257 use mod_usr_methods, only: usr_gravity
6258
6259 integer, intent(in) :: ixi^l, ixo^l
6260 double precision, intent(in) :: qdt, wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6261 double precision, intent(in) :: wctprim(ixi^s,1:nw)
6262 double precision, intent(inout) :: w(ixi^s,1:nw)
6263
6264 double precision :: b(ixi^s,3), j(ixi^s,3), jxb(ixi^s,3)
6265 double precision :: current(ixi^s,7-2*ndir:3)
6266 double precision :: bu(ixo^s,1:ndir), tmp(ixo^s), b2(ixo^s)
6267 double precision :: gravity_field(ixi^s,1:ndir), vaoc
6268 integer :: idir, idirmin, idims, ix^d
6269
6270 {^nothreed
6271 b=0.0d0
6272 do idir = 1, ndir
6273 b(ixo^s, idir) = wct(ixo^s,mag(idir))
6274 end do
6275
6276 if(slab_uniform)then
6277 ! get current in fourth order accuracy in Cartesian
6278 call curlvector(wct(ixi^s,mag(1:ndir)),ixi^l,ixo^l,current,idirmin,7-2*ndir,ndir,.true.)
6279 else
6280 call get_current(wct,ixi^l,ixo^l,idirmin,current)
6281 endif
6282
6283 j=0.0d0
6284 do idir=7-2*ndir,3
6285 j(ixo^s,idir)=current(ixo^s,idir)
6286 end do
6287
6288 ! get Lorentz force JxB
6289 call cross_product(ixi^l,ixo^l,j,b,jxb)
6290 }
6291 {^ifthreed
6292 if(slab_uniform)then
6293 ! get current in fourth order accuracy in Cartesian
6294 call curlvector(wct(ixi^s,mag(1:ndir)),ixi^l,ixo^l,current,idirmin,1,ndir,.true.)
6295 else
6296 call get_current(wct,ixi^l,ixo^l,idirmin,current)
6297 endif
6298 ! get Lorentz force JxB
6299 call cross_product(ixi^l,ixo^l,current,wct(ixi^s,mag(1:ndir)),jxb)
6300 }
6301
6302 ! mhd_semirelativistic does not combine with mhd_hydrodynamic_e
6303 !!if(mhd_semirelativistic) then
6304 !! ! (v . nabla) v
6305 !! do idir=1,ndir
6306 !! do idims=1,ndim
6307 !! call gradient(wCTprim(ixI^S,mom(idir)),ixI^L,ixO^L,idims,J(ixI^S,idims))
6308 !! end do
6309 !! B(ixO^S,idir)=sum(wCTprim(ixO^S,mom(1:ndir))*J(ixO^S,1:ndir),dim=ndim+1)
6310 !! end do
6311 !! ! nabla p
6312 !! do idir=1,ndir
6313 !! call gradient(wCTprim(ixI^S,p_),ixI^L,ixO^L,idir,J(ixI^S,idir))
6314 !! end do
6315 !! if(mhd_gravity) then
6316 !! gravity_field=0.d0
6317 !! call usr_gravity(ixI^L,ixO^L,wCT,x,gravity_field(ixI^S,1:ndim))
6318 !! do idir=1,ndir
6319 !! B(ixO^S,idir)=wCT(ixO^S,rho_)*(B(ixO^S,idir)-gravity_field(ixO^S,idir))+J(ixO^S,idir)-JxB(ixO^S,idir)
6320 !! end do
6321 !! else
6322 !! do idir=1,ndir
6323 !! B(ixO^S,idir)=wCT(ixO^S,rho_)*B(ixO^S,idir)+J(ixO^S,idir)-JxB(ixO^S,idir)
6324 !! end do
6325 !! end if
6326 !! b2(ixO^S)=sum(wCT(ixO^S,mag(:))**2,dim=ndim+1)
6327 !! tmp(ixO^S)=sqrt(b2(ixO^S))
6328 !! where(tmp(ixO^S)>smalldouble)
6329 !! tmp(ixO^S)=1.d0/tmp(ixO^S)
6330 !! else where
6331 !! tmp(ixO^S)=0.d0
6332 !! end where
6333 !! ! unit vector of magnetic field
6334 !! do idir=1,ndir
6335 !! bu(ixO^S,idir)=wCT(ixO^S,mag(idir))*tmp(ixO^S)
6336 !! end do
6337 !! !b2(ixO^S)=b2(ixO^S)/w(ixO^S,rho_)*inv_squared_c
6338 !! !b2(ixO^S)=b2(ixO^S)/(1.d0+b2(ixO^S))
6339 !! {do ix^DB=ixOmin^DB,ixOmax^DB\}
6340 !! ! Va^2/c^2
6341 !! Vaoc=b2(ix^D)/w(ix^D,rho_)*inv_squared_c
6342 !! ! Va^2/c^2 / (1+Va^2/c^2)
6343 !! b2(ix^D)=Vaoc/(1.d0+Vaoc)
6344 !! {end do\}
6345 !! ! bu . F
6346 !! tmp(ixO^S)=sum(bu(ixO^S,1:ndir)*B(ixO^S,1:ndir),dim=ndim+1)
6347 !! ! Rempel 2017 ApJ 834, 10 equation (54)
6348 !! do idir=1,ndir
6349 !! J(ixO^S,idir)=b2(ixO^S)*(B(ixO^S,idir)-bu(ixO^S,idir)*tmp(ixO^S))
6350 !! end do
6351 !! !! Rempel 2017 ApJ 834, 10 equation (29) add SR force at momentum equation
6352 !! do idir=1,ndir
6353 !! w(ixO^S,mom(idir))=w(ixO^S,mom(idir))+qdt*J(ixO^S,idir)
6354 !! end do
6355 !! ! Rempel 2017 ApJ 834, 10 equation (30) add work of Lorentz force and SR force
6356 !! w(ixO^S,e_)=w(ixO^S,e_)+qdt*sum(wCTprim(ixO^S,mom(1:ndir))*&
6357 !! (JxB(ixO^S,1:ndir)+J(ixO^S,1:ndir)),dim=ndim+1)
6358 !!else
6359 ! add work of Lorentz force
6360 w(ixo^s,e_)=w(ixo^s,e_)+qdt*sum(wctprim(ixo^s,mom(1:ndir))*jxb(ixo^s,1:ndir),dim=ndim+1)
6361 !!end if
6362
6363 if(mhd_ambipolar_sts)then
6364 call add_source_ambipolar_internal_energy(qdt,ixi^l,ixo^l,wct,w,x)
6365 end if
6366
6367 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_hydrodynamic_e')
6368
6369 end subroutine add_source_hydrodynamic_e
6370
6371 !> Add resistive source to w within ixO Uses 3 point stencil (1 neighbour) in
6372 !> each direction, non-conservative. Uses the generic Laplacian
6373 !> with fourth order central difference (on uniform cartesian) for the laplacian. Then the
6374 !> stencil is 5 (2 neighbours). NOTE: Unused subroutine!
6375 subroutine add_source_res1(qdt,ixI^L,ixO^L,wCT,w,x)
6377 use mod_usr_methods
6378 use mod_geometry
6379
6380 integer, intent(in) :: ixi^l, ixo^l
6381 double precision, intent(in) :: qdt
6382 double precision, intent(in) :: wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6383 double precision, intent(inout) :: w(ixi^s,1:nw)
6384
6385 integer :: ixa^l,idir,jdir,kdir,idirmin,idim
6386 double precision :: tmp(ixi^s),tmp2(ixi^s)
6387
6388 ! For ndir=2 only 3rd component of J can exist, ndir=1 is impossible for MHD
6389 double precision :: current(ixi^s,7-2*ndir:3),eta(ixi^s)
6390 double precision :: gradeta(ixi^s,1:ndim), bf(ixi^s,1:ndir)
6391 double precision :: lapl_vec(ixi^s,1:ndir)
6392
6393 ! Calculating resistive sources involves one extra layer
6394 ! asking here for two, so Cartesian works with 4th order CD
6395 ixa^l=ixo^l^ladd2;
6396
6397 if (iximin^d>ixamin^d.or.iximax^d<ixamax^d|.or.) &
6398 call mpistop("Error in add_source_res1: Non-conforming input limits")
6399
6400 ! Calculate current density and idirmin
6401 call get_current(wct,ixi^l,ixo^l,idirmin,current)
6402
6403 if (mhd_eta>zero)then
6404 eta(ixa^s)=mhd_eta
6405 gradeta(ixo^s,1:ndim)=zero
6406 else
6407 call usr_special_resistivity(wct,ixi^l,ixa^l,idirmin,x,current,eta)
6408 do idim=1,ndim
6409 call gradient(eta,ixi^l,ixo^l,idim,tmp)
6410 gradeta(ixo^s,idim)=tmp(ixo^s)
6411 end do
6412 end if
6413
6414 if(b0field) then
6415 bf(ixi^s,1:ndir)=wct(ixi^s,mag(1:ndir))+block%B0(ixi^s,1:ndir,0)
6416 else
6417 bf(ixi^s,1:ndir)=wct(ixi^s,mag(1:ndir))
6418 end if
6419
6420 call laplacian_of_vector(bf,ixi^l,ixo^l,lapl_vec)
6421
6422 do idir=1,ndir
6423 ! Multiply by eta to store eta*Laplace B_idir
6424 tmp(ixo^s)=lapl_vec(ixo^s,idir)*eta(ixo^s)
6425
6426 ! Subtract grad(eta) x J = eps_ijk d_j eta J_k if eta is non-constant
6427 if (mhd_eta<zero)then
6428 do jdir=1,ndim; do kdir=idirmin,3
6429 if (lvc(idir,jdir,kdir)/=0)then
6430 if (lvc(idir,jdir,kdir)==1)then
6431 tmp(ixo^s)=tmp(ixo^s)-gradeta(ixo^s,jdir)*current(ixo^s,kdir)
6432 else
6433 tmp(ixo^s)=tmp(ixo^s)+gradeta(ixo^s,jdir)*current(ixo^s,kdir)
6434 end if
6435 end if
6436 end do; end do
6437 end if
6438
6439 ! Add sources related to eta*laplB-grad(eta) x J to B and e
6440 w(ixo^s,mag(idir))=w(ixo^s,mag(idir))+qdt*tmp(ixo^s)
6441 if(total_energy) then
6442 w(ixo^s,e_)=w(ixo^s,e_)+qdt*tmp(ixo^s)*bf(ixo^s,idir)
6443 end if
6444 end do ! idir
6445
6446 if(mhd_energy) then
6447 ! de/dt+=eta*J**2
6448 w(ixo^s,e_)=w(ixo^s,e_)+qdt*eta(ixo^s)*sum(current(ixo^s,:)**2,dim=ndim+1)
6449 end if
6450
6451 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_res1')
6452
6453 end subroutine add_source_res1
6454
6455 !> Add resistive source to w within ixO in an explicit fashion
6456 !> Uses 5 point stencil (2 neighbours) in each direction, conservative
6457 subroutine add_source_res_exp(qdt,ixI^L,ixO^L,wCT,wCTprim,w,x)
6459 use mod_usr_methods
6460 use mod_geometry
6461
6462 integer, intent(in) :: ixi^l, ixo^l
6463 double precision, intent(in) :: qdt
6464 double precision, intent(in) :: wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6465 double precision, intent(in) :: wctprim(ixi^s,1:nw)
6466 double precision, intent(inout) :: w(ixi^s,1:nw)
6467
6468 ! For ndir=2 only 3rd component of J can exist, ndir=1 is impossible for MHD
6469 double precision :: current(ixi^s,7-2*ndir:3),eta(ixi^s),curlj(ixi^s,1:3)
6470 double precision :: tmpvec(ixi^s,1:3),tmp(ixo^s)
6471 integer :: ixa^l,idir,idirmin,idirmin1,ix^d
6472
6473 ixa^l=ixo^l^ladd2;
6474
6475 if (iximin^d>ixamin^d.or.iximax^d<ixamax^d|.or.) &
6476 call mpistop("Error in add_source_res_exp: Non-conforming input limits")
6477
6478 ixa^l=ixo^l^ladd1;
6479 ! Calculate current density within ixL: J=curl B, thus J_i=eps_ijk*d_j B_k
6480 ! Determine exact value of idirmin while doing the loop.
6481 call get_current(wct,ixi^l,ixa^l,idirmin,current)
6482
6483 tmpvec=zero
6484 if(mhd_eta>zero)then
6485 do idir=idirmin,3
6486 tmpvec(ixa^s,idir)=current(ixa^s,idir)*mhd_eta
6487 end do
6488 else
6489 call usr_special_resistivity(wctprim,ixi^l,ixa^l,idirmin,x,current,eta)
6490 do idir=idirmin,3
6491 tmpvec(ixa^s,idir)=current(ixa^s,idir)*eta(ixa^s)
6492 end do
6493 end if
6494
6495 ! dB/dt= -curl(J*eta), thus B_i=B_i-eps_ijk d_j Jeta_k
6496 call curlvector(tmpvec,ixi^l,ixo^l,curlj,idirmin1,1,3)
6497 {do ix^db=ixomin^db,ixomax^db\}
6498 if(stagger_grid) then
6499 ! resistive electric field is already included in CT-MHD
6500 if(ndim<ndir) then
6501 ! if 2.5D update the third component of B
6502 w(ix^d,mag(ndir))=w(ix^d,mag(ndir))-qdt*curlj(ix^d,ndir)
6503 end if
6504 else
6505 ^c&w(ix^d,b^c_)=w(ix^d,b^c_)-qdt*curlj(ix^d,^c)\
6506 end if
6507 if(mhd_energy) then
6508 {^ifthreed
6509 if(mhd_eta>zero)then
6510 w(ix^d,e_)=w(ix^d,e_)+qdt*mhd_eta*(^c&current(ix^d,^c)**2+)
6511 else
6512 w(ix^d,e_)=w(ix^d,e_)+qdt*eta(ix^d)*(^c&current(ix^d,^c)**2+)
6513 end if
6514 }
6515 {^iftwod
6516 if(mhd_eta>zero)then
6517 w(ix^d,e_)=w(ix^d,e_)+qdt*mhd_eta*current(ix^d,3)**2
6518 else
6519 w(ix^d,e_)=w(ix^d,e_)+qdt*eta(ix^d)*current(ix^d,3)**2
6520 end if
6521 }
6522 if(total_energy) then
6523 ! de/dt= +div(B x Jeta) = eta J^2 - B dot curl(eta J)
6524 ! de1/dt= eta J^2 - B1 dot curl(eta J)
6525 w(ix^d,e_)=w(ix^d,e_)-qdt*(^c&wct(ix^d,b^c_)*curlj(ix^d,^c)+)
6526 end if
6527 end if
6528 {end do\}
6529
6530 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_res_exp')
6531 end subroutine add_source_res_exp
6532
6533
6534 !> Add ambipolar source to w within ixO in an explicit fashion
6535 !> Uses 5 point stencil (2 neighbours) in each direction, conservative
6536 subroutine add_source_ambi_exp(qdt,ixI^L,ixO^L,wCT,w,x)
6538 use mod_usr_methods
6539 use mod_geometry
6540
6541 integer, intent(in) :: ixi^l, ixo^l
6542 double precision, intent(in) :: qdt
6543 double precision, intent(in) :: wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6544 double precision, intent(inout) :: w(ixi^s,1:nw)
6545
6546 double precision :: current(ixi^s,1:3),curlj(ixi^s,1:3)
6547 double precision :: tmpvec(ixi^s,1:3),tmp(ixi^s),btot2(ixi^s)
6548 integer :: ixa^l,idir,idirmin1
6549
6550 ixa^l=ixo^l^ladd2;
6551
6552 if (iximin^d>ixamin^d.or.iximax^d<ixamax^d|.or.) &
6553 call mpistop("Error in add_source_ambi_exp: Non-conforming input limits")
6554
6555 ixa^l=ixo^l^ladd1;
6556 ! Calculate -J_perpB = (JxB)xB
6557 call mhd_get_jxbxb(wct,x,ixi^l,ixa^l,current)
6558
6559 tmpvec=current
6560 do idir=1,3
6561 !set electric field in tmpvec : E=nuA * jxbxb, where nuA=-etaA/rho^2
6562 !tmpvec(ixA^S,i) = -(mhd_eta_ambi/w(ixA^S, rho_)**2) * jxbxb(ixA^S,i)
6563 call multiplyambicoef(ixi^l,ixa^l,tmpvec(ixi^s,idir),wct,x)
6564 end do
6565
6566 ! dB/dt= -curl(J_perpB*etaA), thus B_i=B_i-eps_ijk d_j Jeta_k
6567 call curlvector(tmpvec,ixi^l,ixo^l,curlj,idirmin1,1,3)
6568 if(stagger_grid) then
6569 if(ndim==2.and.ndir==3) then
6570 ! if 2.5D
6571 w(ixo^s,mag(ndir)) = w(ixo^s,mag(ndir))-qdt*curlj(ixo^s,ndir)
6572 end if
6573 else
6574 w(ixo^s,mag(1:ndir)) = w(ixo^s,mag(1:ndir))-qdt*curlj(ixo^s,1:ndir)
6575 end if
6576
6577 if(mhd_energy) then
6578 ! compute ambipolar heating term: nuA* J_perpB^2/ B^2
6579 ! avoiding nulls here
6580 btot2(ixa^s)=mhd_mag_en_all(wct,ixi^l,ixa^l)
6581 where (btot2(ixa^s)>smalldouble )
6582 tmp(ixa^s) = sum(current(ixa^s,1:3)**2,dim=ndim+1) / btot2(ixa^s)
6583 elsewhere
6584 tmp(ixa^s) = zero
6585 endwhere
6586 ! multiply with nuA where nuA=-etaA/rho^2
6587 call multiplyambicoef(ixi^l,ixa^l,tmp,wct,x)
6588 ! compensate - sign and add timestep
6589 tmp(ixo^s)=-qdt*tmp(ixo^s)
6590 if(total_energy) then
6591 ! de/dt= +div(B x E_ambi) = eta J^2 - B dot curl(eta J)
6592 ! de1/dt= eta J^2 - B1 dot curl(eta J)
6593 w(ixo^s,e_)=w(ixo^s,e_)+tmp(ixo^s)-&
6594 qdt*sum(wct(ixo^s,mag(1:ndir))*curlj(ixo^s,1:ndir),dim=ndim+1)
6595 else
6596 ! add eta*J**2 source term in the internal or hydrodynamic energy equation
6597 w(ixo^s,e_)=w(ixo^s,e_)+tmp(ixo^s)
6598 end if
6599 end if
6600
6601 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_ambi_exp')
6602 end subroutine add_source_ambi_exp
6603
6604 !> Add Hyper-resistive source to w within ixO
6605 !> Uses 9 point stencil (4 neighbours) in each direction.
6606 subroutine add_source_hyperres(qdt,ixI^L,ixO^L,wCT,w,x)
6608 use mod_geometry
6609
6610 integer, intent(in) :: ixi^l, ixo^l
6611 double precision, intent(in) :: qdt
6612 double precision, intent(in) :: wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6613 double precision, intent(inout) :: w(ixi^s,1:nw)
6614 !.. local ..
6615 double precision :: current(ixi^s,7-2*ndir:3)
6616 double precision :: tmpvec(ixi^s,1:3),tmpvec2(ixi^s,1:3),tmp(ixi^s),ehyper(ixi^s,1:3)
6617 integer :: ixa^l,idir,jdir,kdir,idirmin,idirmin1
6618
6619 ixa^l=ixo^l^ladd3;
6620 if (iximin^d>ixamin^d.or.iximax^d<ixamax^d|.or.) &
6621 call mpistop("Error in add_source_hyperres: Non-conforming input limits")
6622
6623 call get_current(wct,ixi^l,ixa^l,idirmin,current)
6624 tmpvec(ixa^s,1:ndir)=zero
6625 do jdir=idirmin,3
6626 tmpvec(ixa^s,jdir)=current(ixa^s,jdir)
6627 end do
6628
6629 ixa^l=ixo^l^ladd2;
6630 call curlvector(tmpvec,ixi^l,ixa^l,tmpvec2,idirmin1,1,3)
6631
6632 ixa^l=ixo^l^ladd1;
6633 tmpvec(ixa^s,1:ndir)=zero
6634 call curlvector(tmpvec2,ixi^l,ixa^l,tmpvec,idirmin1,1,3)
6635 ehyper(ixa^s,1:ndir) = - tmpvec(ixa^s,1:ndir)*mhd_eta_hyper
6636
6637 ixa^l=ixo^l;
6638 tmpvec2(ixa^s,1:ndir)=zero
6639 call curlvector(ehyper,ixi^l,ixa^l,tmpvec2,idirmin1,1,3)
6640
6641 do idir=1,ndir
6642 w(ixo^s,mag(idir)) = w(ixo^s,mag(idir))-tmpvec2(ixo^s,idir)*qdt
6643 end do
6644
6645 if(total_energy) then
6646 ! de/dt= +div(B x Ehyper)
6647 ixa^l=ixo^l^ladd1;
6648 tmpvec2(ixa^s,1:ndir)=zero
6649 do idir=1,ndir; do jdir=1,ndir; do kdir=idirmin,3
6650 tmpvec2(ixa^s,idir) = tmpvec(ixa^s,idir)&
6651 + lvc(idir,jdir,kdir)*wct(ixa^s,mag(jdir))*ehyper(ixa^s,kdir)
6652 end do; end do; end do
6653 tmp(ixo^s)=zero
6654 call divvector(tmpvec2,ixi^l,ixo^l,tmp)
6655 w(ixo^s,e_)=w(ixo^s,e_)+tmp(ixo^s)*qdt
6656 end if
6657
6658 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_hyperres')
6659
6660 end subroutine add_source_hyperres
6661
6662 subroutine add_source_glm(qdt,ixI^L,ixO^L,wCT,w,x)
6663 ! Add divB related sources to w within ixO
6664 ! corresponding to Dedner JCP 2002, 175, 645 _equation 24_
6665 ! giving the EGLM-MHD scheme or GLM-MHD scheme
6667 use mod_geometry
6668
6669 integer, intent(in) :: ixi^l, ixo^l
6670 double precision, intent(in) :: qdt, wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6671 double precision, intent(inout) :: w(ixi^s,1:nw)
6672
6673 double precision:: divb(ixi^s), gradpsi(ixi^s), ba(ixo^s,1:ndir)
6674 integer :: idir
6675
6676
6677 ! dPsi/dt = - Ch^2/Cp^2 Psi
6678 if (mhd_glm_alpha < zero) then
6679 w(ixo^s,psi_) = abs(mhd_glm_alpha)*wct(ixo^s,psi_)
6680 else
6681 ! implicit update of Psi variable
6682 ! equation (27) in Mignone 2010 J. Com. Phys. 229, 2117
6683 if(slab_uniform) then
6684 w(ixo^s,psi_) = dexp(-qdt*cmax_global*mhd_glm_alpha/minval(dxlevel(:)))*w(ixo^s,psi_)
6685 else
6686 w(ixo^s,psi_) = dexp(-qdt*cmax_global*mhd_glm_alpha/minval(block%ds(ixo^s,:),dim=ndim+1))*w(ixo^s,psi_)
6687 end if
6688 end if
6689
6690 if(mhd_glm_extended) then
6691 if(b0field) then
6692 ba(ixo^s,1:ndir)=wct(ixo^s,mag(1:ndir))+block%B0(ixo^s,1:ndir,0)
6693 else
6694 ba(ixo^s,1:ndir)=wct(ixo^s,mag(1:ndir))
6695 end if
6696 ! gradient of Psi
6697 if(total_energy) then
6698 do idir=1,ndim
6699 select case(typegrad)
6700 case("central")
6701 call gradient(wct(ixi^s,psi_),ixi^l,ixo^l,idir,gradpsi)
6702 case("limited")
6703 call gradientl(wct(ixi^s,psi_),ixi^l,ixo^l,idir,gradpsi)
6704 end select
6705 ! e = e - qdt (b1 . grad(Psi)): e carries only the b1 magnetic energy
6706 w(ixo^s,e_) = w(ixo^s,e_)-qdt*wct(ixo^s,mag(idir))*gradpsi(ixo^s)
6707 end do
6708 end if
6709
6710 ! We calculate now div B
6711 call get_divb(wct,ixi^l,ixo^l,divb, mhd_divb_nth)
6712
6713 ! m = m - qdt b div b
6714 do idir=1,ndir
6715 w(ixo^s,mom(idir))=w(ixo^s,mom(idir))-qdt*ba(ixo^s,idir)*divb(ixo^s)
6716 end do
6717 end if
6718
6719 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_glm')
6720
6721 end subroutine add_source_glm
6722
6723 !> Add divB related sources to w within ixO corresponding to Powel
6724 subroutine add_source_powel(qdt,ixI^L,ixO^L,wCT,w,x)
6726
6727 integer, intent(in) :: ixi^l, ixo^l
6728 double precision, intent(in) :: qdt, wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6729 double precision, intent(inout) :: w(ixi^s,1:nw)
6730
6731 double precision :: divb(ixi^s), ba(1:ndir)
6732 integer :: idir, ix^d
6733
6734 ! calculate div B
6735 call get_divb(wct,ixi^l,ixo^l,divb, mhd_divb_nth)
6736
6737 if(b0field) then
6738 {do ix^db=ixomin^db,ixomax^db\}
6739 ! b = b - qdt v * div b
6740 ^c&w(ix^d,b^c_)=w(ix^d,b^c_)-qdt*wct(ix^d,m^c_)*divb(ix^d)\
6741 ! m = m - qdt b div b
6742 ^c&w(ix^d,m^c_)=w(ix^d,m^c_)-qdt*(wct(ix^d,b^c_)+block%B0(ix^d,^c,0))*divb(ix^d)\
6743 if (total_energy) then
6744 ! e = e - qdt (v . b) * div b
6745 w(ix^d,e_)=w(ix^d,e_)-qdt*(^c&wct(ix^d,m^c_)*(wct(ix^d,b^c_)+block%B0(ix^d,^c,0))+)*divb(ix^d)
6746 end if
6747 {end do\}
6748 else
6749 {do ix^db=ixomin^db,ixomax^db\}
6750 ! b = b - qdt v * div b
6751 ^c&w(ix^d,b^c_)=w(ix^d,b^c_)-qdt*wct(ix^d,m^c_)*divb(ix^d)\
6752 ! m = m - qdt b div b
6753 ^c&w(ix^d,m^c_)=w(ix^d,m^c_)-qdt*wct(ix^d,b^c_)*divb(ix^d)\
6754 if (total_energy) then
6755 ! e = e - qdt (v . b) * div b
6756 w(ix^d,e_)=w(ix^d,e_)-qdt*(^c&wct(ix^d,m^c_)*wct(ix^d,b^c_)+)*divb(ix^d)
6757 end if
6758 {end do\}
6759 end if
6760
6761 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_powel')
6762
6763 end subroutine add_source_powel
6764
6765 subroutine add_source_janhunen(qdt,ixI^L,ixO^L,wCT,w,x)
6766 ! Add divB related sources to w within ixO
6767 ! corresponding to Janhunen, just the term in the induction equation.
6769
6770 integer, intent(in) :: ixi^l, ixo^l
6771 double precision, intent(in) :: qdt, wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6772 double precision, intent(inout) :: w(ixi^s,1:nw)
6773
6774 double precision :: divb(ixi^s)
6775 integer :: idir, ix^d
6776
6777 ! calculate div B
6778 call get_divb(wct,ixi^l,ixo^l,divb, mhd_divb_nth)
6779
6780 {do ix^db=ixomin^db,ixomax^db\}
6781 ! b = b - qdt v * div b
6782 ^c&w(ix^d,b^c_)=w(ix^d,b^c_)-qdt*wct(ix^d,m^c_)*divb(ix^d)\
6783 {end do\}
6784
6785 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_janhunen')
6786
6787 end subroutine add_source_janhunen
6788
6789 subroutine add_source_linde(qdt,ixI^L,ixO^L,wCT,w,x)
6790 ! Add Linde's divB related sources to wnew within ixO
6792 use mod_geometry
6793
6794 integer, intent(in) :: ixi^l, ixo^l
6795 double precision, intent(in) :: qdt, wct(ixi^s,1:nw), x(ixi^s,1:ndim)
6796 double precision, intent(inout) :: w(ixi^s,1:nw)
6797
6798 double precision :: divb(ixi^s),graddivb(ixi^s)
6799 integer :: idim, idir, ixp^l, i^d, iside
6800 logical, dimension(-1:1^D&) :: leveljump
6801
6802 ! Calculate div B
6803 ixp^l=ixo^l^ladd1;
6804 call get_divb(wct,ixi^l,ixp^l,divb, mhd_divb_nth)
6805
6806 ! for AMR stability, retreat one cell layer from the boarders of level jump
6807 {do i^db=-1,1\}
6808 if(i^d==0|.and.) cycle
6809 if(neighbor_type(i^d,block%igrid)==2 .or. neighbor_type(i^d,block%igrid)==4) then
6810 leveljump(i^d)=.true.
6811 else
6812 leveljump(i^d)=.false.
6813 end if
6814 {end do\}
6815
6816 ixp^l=ixo^l;
6817 do idim=1,ndim
6818 select case(idim)
6819 {case(^d)
6820 do iside=1,2
6821 i^dd=kr(^dd,^d)*(2*iside-3);
6822 if (leveljump(i^dd)) then
6823 if (iside==1) then
6824 ixpmin^d=ixomin^d-i^d
6825 else
6826 ixpmax^d=ixomax^d-i^d
6827 end if
6828 end if
6829 end do
6830 \}
6831 end select
6832 end do
6833
6834 ! Add Linde's diffusive terms
6835 do idim=1,ndim
6836 ! Calculate grad_idim(divb)
6837 call gradient(divb,ixi^l,ixp^l,idim,graddivb)
6838
6839 {do i^db=ixpmin^db,ixpmax^db\}
6840 ! Multiply by Linde's eta*dt = divbdiff*(c_max*dx)*dt = divbdiff*dx**2
6841 graddivb(i^d)=graddivb(i^d)*divbdiff/(^d&1.0d0/block%ds({i^d},^d)**2+)
6842
6843 w(i^d,mag(idim))=w(i^d,mag(idim))+graddivb(i^d)
6844
6845 if (typedivbdiff=='all' .and. total_energy) then
6846 ! e += B_idim*eta*grad_idim(divb)
6847 ! the classical term, without the O(V_A^2/c^2) semirelativistic correction
6848 w(i^d,e_)=w(i^d,e_)+wct(i^d,mag(idim))*graddivb(i^d)
6849 end if
6850 {end do\}
6851 end do
6852
6853 if (fix_small_values) call mhd_handle_small_values(.false.,w,x,ixi^l,ixo^l,'add_source_linde')
6854
6855 end subroutine add_source_linde
6856
6857 !> get dimensionless div B = |divB| * volume / area / |B|
6858 subroutine get_normalized_divb(w,ixI^L,ixO^L,divb)
6859
6861
6862 integer, intent(in) :: ixi^l, ixo^l
6863 double precision, intent(in) :: w(ixi^s,1:nw)
6864 double precision :: divb(ixi^s), dsurface(ixi^s)
6865
6866 double precision :: invb(ixo^s)
6867 integer :: ixa^l,idims
6868
6869 call get_divb(w,ixi^l,ixo^l,divb)
6870 invb(ixo^s)=sqrt(mhd_mag_en_all(w,ixi^l,ixo^l))
6871 where(invb(ixo^s)/=0.d0)
6872 invb(ixo^s)=1.d0/invb(ixo^s)
6873 end where
6874 if(slab_uniform) then
6875 divb(ixo^s)=0.5d0*abs(divb(ixo^s))*invb(ixo^s)/sum(1.d0/dxlevel(:))
6876 else
6877 ixamin^d=ixomin^d-1;
6878 ixamax^d=ixomax^d-1;
6879 dsurface(ixo^s)= sum(block%surfaceC(ixo^s,:),dim=ndim+1)
6880 do idims=1,ndim
6881 ixa^l=ixo^l-kr(idims,^d);
6882 dsurface(ixo^s)=dsurface(ixo^s)+block%surfaceC(ixa^s,idims)
6883 end do
6884 divb(ixo^s)=abs(divb(ixo^s))*invb(ixo^s)*&
6885 block%dvolume(ixo^s)/dsurface(ixo^s)
6886 end if
6887
6888 end subroutine get_normalized_divb
6889
6890 !> Calculate idirmin and the idirmin:3 components of the common current array
6891 !> make sure that dxlevel(^D) is set correctly.
6892 subroutine get_current(w,ixI^L,ixO^L,idirmin,current)
6894 use mod_geometry
6895
6896 integer, intent(in) :: ixo^l, ixi^l
6897 double precision, intent(in) :: w(ixi^s,1:nw)
6898 integer, intent(out) :: idirmin
6899
6900 ! For ndir=2 only 3rd component of J can exist, ndir=1 is impossible for MHD
6901 double precision :: current(ixi^s,7-2*ndir:3)
6902 integer :: idir, idirmin0
6903
6904 idirmin0 = 7-2*ndir
6905
6906 call curlvector(w(ixi^s,mag(1:ndir)),ixi^l,ixo^l,current,idirmin,idirmin0,ndir)
6907
6908 if(b0field) current(ixo^s,idirmin0:3)=current(ixo^s,idirmin0:3)+&
6909 block%J0(ixo^s,idirmin0:3)
6910 end subroutine get_current
6911
6912 !> If resistivity is not zero, check diffusion time limit for dt and similar other effects
6913 subroutine mhd_get_dt(wprim,ixI^L,ixO^L,dtnew,dx^D,x)
6915 use mod_usr_methods
6917 use mod_gravity, only: gravity_get_dt
6918 use mod_cak_force, only: cak_get_dt
6919 use mod_fld, only: fld_radforce_get_dt
6920
6921 integer, intent(in) :: ixi^l, ixo^l
6922 double precision, intent(inout) :: dtnew
6923 double precision, intent(in) :: dx^d
6924 double precision, intent(in) :: wprim(ixi^s,1:nw)
6925 double precision, intent(in) :: x(ixi^s,1:ndim)
6926
6927 double precision :: dxarr(ndim)
6928 double precision :: current(ixi^s,7-2*ndir:3),eta(ixi^s)
6929 integer :: idirmin,idim
6930
6931 dtnew = bigdouble
6932
6933 ^d&dxarr(^d)=dx^d;
6934 if (mhd_eta>zero)then
6935 if(slab_uniform) then
6936 dtnew=dtdiffpar*minval(dxarr(1:ndim))**2/mhd_eta
6937 else
6938 dtnew=dtdiffpar*minval(block%ds(ixo^s,1:ndim))**2/mhd_eta
6939 end if
6940 else if (mhd_eta<zero)then
6941 call get_current(wprim,ixi^l,ixo^l,idirmin,current)
6942 call usr_special_resistivity(wprim,ixi^l,ixo^l,idirmin,x,current,eta)
6943 dtnew=bigdouble
6944 do idim=1,ndim
6945 if(slab_uniform) then
6946 dtnew=min(dtnew,&
6947 dtdiffpar/(smalldouble+maxval(eta(ixo^s)/dxarr(idim)**2)))
6948 else
6949 dtnew=min(dtnew,&
6950 dtdiffpar/(smalldouble+maxval(eta(ixo^s)/block%ds(ixo^s,idim)**2)))
6951 end if
6952 end do
6953 end if
6954
6955 if(mhd_eta_hyper>zero) then
6956 if(slab_uniform) then
6957 dtnew=min(dtdiffpar*minval(dxarr(1:ndim))**4/mhd_eta_hyper,dtnew)
6958 else
6959 dtnew=min(dtdiffpar*minval(block%ds(ixo^s,1:ndim))**4/mhd_eta_hyper,dtnew)
6960 end if
6961 end if
6962
6963 if(mhd_viscosity) then
6964 call viscosity_get_dt(wprim,ixi^l,ixo^l,dtnew,dx^d,x)
6965 end if
6966
6967 if(mhd_gravity) then
6968 call gravity_get_dt(wprim,ixi^l,ixo^l,dtnew,dx^d,x)
6969 end if
6970
6971 if(mhd_ambipolar_exp) then
6972 dtnew=min(dtdiffpar*get_ambipolar_dt(wprim,ixi^l,ixo^l,dx^d,x),dtnew)
6973 endif
6974
6975 if (mhd_cak_force) then
6976 call cak_get_dt(wprim,ixi^l,ixo^l,dtnew,dx^d,x)
6977 end if
6978
6979 if(mhd_radiation_fld) then
6980 call fld_radforce_get_dt(wprim,ixi^l,ixo^l,dtnew,dx^d,x,fld_fl)
6981 endif
6982
6983 end subroutine mhd_get_dt
6984
6985 !> Wrappers for the FLD implicit (MG diffusion) hooks: phys_implicit_update /
6986 !> phys_evaluate_implicit have fixed interfaces with no fluid argument, so
6987 !> these inject the module's fld_fl object into the threaded fld routines.
6988 subroutine mhd_fld_implicit_update(dtfactor,qdt,qtC,psa,psb)
6990 use mod_fld, only: fld_implicit_update
6991 type(state), target :: psa(max_blocks)
6992 type(state), target :: psb(max_blocks)
6993 double precision, intent(in) :: qdt
6994 double precision, intent(in) :: qtc
6995 double precision, intent(in) :: dtfactor
6996
6997 call fld_implicit_update(dtfactor,qdt,qtc,psa,psb,fld_fl)
6998 end subroutine mhd_fld_implicit_update
6999
7000 subroutine mhd_fld_evaluate_implicit(qtC,psa)
7003 type(state), target :: psa(max_blocks)
7004 double precision, intent(in) :: qtc
7005
7006 call fld_evaluate_implicit(qtc,psa,fld_fl)
7007 end subroutine mhd_fld_evaluate_implicit
7008
7009 ! Add geometrical source terms to w
7010 ! Geometric sources to momentum and induction
7011 ! for the regular case, not semi-relativistic, nor any splitting active
7012 ! but possibly no energy equation at all
7013 ! NOTE: Hall terms in induction not handled yet
7014 subroutine mhd_add_source_geom(qdt,dtfactor,ixI^L,ixO^L,wCT,wprim,w,x)
7016 use mod_geometry
7019
7020 integer, intent(in) :: ixi^l, ixo^l
7021 double precision, intent(in) :: qdt, dtfactor,x(ixi^s,1:ndim)
7022 double precision, intent(inout) :: wct(ixi^s,1:nw),wprim(ixi^s,1:nw),w(ixi^s,1:nw)
7023
7024 double precision :: adiabs(ixi^s), gammas(ixi^s)
7025 double precision :: tmp,tmp1,invr,cot
7026 integer :: ix^d
7027 integer :: mr_,mphi_ ! Polar var. names
7028 integer :: br_,bphi_
7029
7030 mr_=mom(1); mphi_=mom(1)-1+phi_ ! Polar var. names
7031 br_=mag(1); bphi_=mag(1)-1+phi_
7032
7033 if(.not.mhd_energy) then
7034 if(associated(usr_set_adiab)) then
7035 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
7036 else
7037 adiabs=mhd_adiab
7038 end if
7039 if(associated(usr_set_gamma)) then
7040 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
7041 else
7042 gammas=eos%gamma
7043 end if
7044 end if
7045
7046 select case (coordinate)
7047 case (cylindrical)
7048 {do ix^db=ixomin^db,ixomax^db\}
7049 ! include dt in invr, invr is always used with qdt
7050 if(local_timestep) then
7051 invr=block%dt(ix^d) * dtfactor/x(ix^d,1)
7052 else
7053 invr=qdt/x(ix^d,1)
7054 end if
7055 if(mhd_energy) then
7056 tmp=wprim(ix^d,p_)+half*(^c&wprim(ix^d,b^c_)**2+)
7057 else
7058 tmp=adiabs(ix^d)*wprim(ix^d,rho_)**gammas(ix^d)+half*(^c&wprim(ix^d,b^c_)**2+)
7059 end if
7060 if(phi_>0) then
7061 w(ix^d,mr_)=w(ix^d,mr_)+invr*(tmp-&
7062 wprim(ix^d,bphi_)**2+wprim(ix^d,mphi_)*wct(ix^d,mphi_))
7063 w(ix^d,mphi_)=w(ix^d,mphi_)+invr*(&
7064 -wct(ix^d,mphi_)*wprim(ix^d,mr_) &
7065 +wprim(ix^d,bphi_)*wprim(ix^d,br_))
7066 if(.not.stagger_grid) then
7067 w(ix^d,bphi_)=w(ix^d,bphi_)+invr*&
7068 (wprim(ix^d,bphi_)*wprim(ix^d,mr_) &
7069 -wprim(ix^d,br_)*wprim(ix^d,mphi_))
7070 end if
7071 else
7072 w(ix^d,mr_)=w(ix^d,mr_)+invr*tmp
7073 end if
7074 if(mhd_glm) w(ix^d,br_)=w(ix^d,br_)+wprim(ix^d,psi_)*invr
7075 {end do\}
7076 case (spherical)
7077 {do ix^db=ixomin^db,ixomax^db\}
7078 ! include dt in invr, invr is always used with qdt
7079 if(local_timestep) then
7080 invr=block%dt(ix^d) * dtfactor/x(ix^d,1)
7081 else
7082 invr=qdt/x(ix^d,1)
7083 end if
7084 if(mhd_energy) then
7085 tmp1=wprim(ix^d,p_)+half*(^c&wprim(ix^d,b^c_)**2+)
7086 else
7087 tmp1=adiabs(ix^d)*wprim(ix^d,rho_)**gammas(ix^d)+half*(^c&wprim(ix^d,b^c_)**2+)
7088 end if
7089 ! m1
7090 {^ifonec
7091 w(ix^d,mom(1))=w(ix^d,mom(1))+two*tmp1*invr
7092 }
7093 {^noonec
7094 w(ix^d,mom(1))=w(ix^d,mom(1))+invr*&
7095 (two*tmp1+(^ce&wprim(ix^d,m^ce_)*wct(ix^d,m^ce_)-wprim(ix^d,b^ce_)**2+))
7096 }
7097 ! b1
7098 if(mhd_glm) then
7099 w(ix^d,mag(1))=w(ix^d,mag(1))+invr*2.0d0*wprim(ix^d,psi_)
7100 end if
7101 {^ifoned
7102 cot=0.d0
7103 }
7104 {^nooned
7105 cot=1.d0/tan(x(ix^d,2))
7106 }
7107 {^iftwoc
7108 ! m2
7109 w(ix^d,mom(2))=w(ix^d,mom(2))+invr*(tmp1*cot-wprim(ix^d,m1_)*wct(ix^d,m2_)&
7110 +wprim(ix^d,b1_)*wprim(ix^d,b2_))
7111 ! b2
7112 if(.not.stagger_grid) then
7113 tmp=wprim(ix^d,m1_)*wprim(ix^d,b2_)-wprim(ix^d,m2_)*wprim(ix^d,b1_)
7114 if(mhd_glm) then
7115 tmp=tmp+wprim(ix^d,psi_)*cot
7116 end if
7117 w(ix^d,mag(2))=w(ix^d,mag(2))+tmp*invr
7118 end if
7119 }
7120 {^ifthreec
7121 ! m2
7122 w(ix^d,mom(2))=w(ix^d,mom(2))+invr*(tmp1*cot-wprim(ix^d,m1_)*wct(ix^d,m2_)&
7123 +wprim(ix^d,b1_)*wprim(ix^d,b2_)&
7124 +(wprim(ix^d,m3_)*wct(ix^d,m3_)-wprim(ix^d,b3_)**2)*cot)
7125 ! b2
7126 if(.not.stagger_grid) then
7127 tmp=wprim(ix^d,m1_)*wprim(ix^d,b2_)-wprim(ix^d,m2_)*wprim(ix^d,b1_)
7128 if(mhd_glm) then
7129 tmp=tmp+wprim(ix^d,psi_)*cot
7130 end if
7131 w(ix^d,mag(2))=w(ix^d,mag(2))+tmp*invr
7132 end if
7133 ! m3
7134 w(ix^d,mom(3))=w(ix^d,mom(3))-invr*&
7135 (wprim(ix^d,m3_)*wct(ix^d,m1_) &
7136 -wprim(ix^d,b3_)*wprim(ix^d,b1_) &
7137 +(wprim(ix^d,m2_)*wct(ix^d,m3_) &
7138 -wprim(ix^d,b2_)*wprim(ix^d,b3_))*cot)
7139 ! b3
7140 if(.not.stagger_grid) then
7141 w(ix^d,mag(3))=w(ix^d,mag(3))+invr*&
7142 (wprim(ix^d,m1_)*wprim(ix^d,b3_) &
7143 -wprim(ix^d,m3_)*wprim(ix^d,b1_) &
7144 -(wprim(ix^d,m3_)*wprim(ix^d,b2_) &
7145 -wprim(ix^d,m2_)*wprim(ix^d,b3_))*cot)
7146 end if
7147 }
7148 {end do\}
7149 end select
7150
7151 if (mhd_rotating_frame) then
7152 call rotating_frame_add_source(qdt,dtfactor,ixi^l,ixo^l,wprim,w,x)
7153 end if
7154
7155 end subroutine mhd_add_source_geom
7156
7157 ! Add geometrical source terms to w
7158 ! Geometric sources to momentum and induction
7159 ! for the semi-relativistic, hence no splitting active
7160 ! but possibly no energy equation at all
7161 ! NOTE: Hall terms in induction not handled yet
7162 subroutine mhd_add_source_geom_semirelati(qdt,dtfactor,ixI^L,ixO^L,wCT,wprim,w,x)
7164 use mod_geometry
7167
7168 integer, intent(in) :: ixi^l, ixo^l
7169 double precision, intent(in) :: qdt, dtfactor,x(ixi^s,1:ndim)
7170 double precision, intent(inout) :: wct(ixi^s,1:nw),wprim(ixi^s,1:nw),w(ixi^s,1:nw)
7171
7172 double precision :: adiabs(ixi^s), gammas(ixi^s)
7173 double precision :: tmp,tmp1,tmp2,invr,cot,ef(ixo^s,1:ndir)
7174 integer :: ix^d
7175 integer :: mr_,mphi_ ! Polar var. names
7176 integer :: br_,bphi_
7177
7178 mr_=mom(1); mphi_=mom(1)-1+phi_ ! Polar var. names
7179 br_=mag(1); bphi_=mag(1)-1+phi_
7180
7181 if(.not.mhd_energy) then
7182 if(associated(usr_set_adiab)) then
7183 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
7184 else
7185 adiabs=mhd_adiab
7186 end if
7187 if(associated(usr_set_gamma)) then
7188 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
7189 else
7190 gammas=eos%gamma
7191 end if
7192 end if
7193
7194 select case (coordinate)
7195 case (cylindrical)
7196 {do ix^db=ixomin^db,ixomax^db\}
7197 ! include dt in invr, invr is always used with qdt
7198 if(local_timestep) then
7199 invr=block%dt(ix^d) * dtfactor/x(ix^d,1)
7200 else
7201 invr=qdt/x(ix^d,1)
7202 end if
7203 if(mhd_energy) then
7204 tmp=wprim(ix^d,p_)
7205 else
7206 tmp=adiabs(ix^d)*wprim(ix^d,rho_)**gammas(ix^d)
7207 end if
7208 ! E=Bxv
7209 {^ifthreec
7210 ef(ix^d,1)=wprim(ix^d,b2_)*wprim(ix^d,m3_)-wprim(ix^d,b3_)*wprim(ix^d,m2_)
7211 ef(ix^d,2)=wprim(ix^d,b3_)*wprim(ix^d,m1_)-wprim(ix^d,b1_)*wprim(ix^d,m3_)
7212 ef(ix^d,3)=wprim(ix^d,b1_)*wprim(ix^d,m2_)-wprim(ix^d,b2_)*wprim(ix^d,m1_)
7213 }
7214 {^iftwoc
7215 ef(ix^d,1)=zero
7216 ! store e3 in e2 to count e3 when ^C is from 1 to 2
7217 ef(ix^d,2)=wprim(ix^d,b1_)*wprim(ix^d,m2_)-wprim(ix^d,b2_)*wprim(ix^d,m1_)
7218 }
7219 {^ifonec
7220 ef(ix^d,1)=zero
7221 }
7222 if(phi_>0) then
7223 w(ix^d,mr_)=w(ix^d,mr_)+invr*(tmp+&
7224 half*((^c&wprim(ix^d,b^c_)**2+)+(^c&ef(ix^d,^c)**2+)*inv_squared_c) -&
7225 wprim(ix^d,bphi_)**2+wprim(ix^d,rho_)*wprim(ix^d,mphi_)**2)
7226 w(ix^d,mphi_)=w(ix^d,mphi_)+invr*(&
7227 -wprim(ix^d,rho_)*wprim(ix^d,mphi_)*wprim(ix^d,mr_) &
7228 +wprim(ix^d,bphi_)*wprim(ix^d,br_)+ef(ix^d,phi_)*ef(ix^d,1)*inv_squared_c)
7229 if(.not.stagger_grid) then
7230 w(ix^d,bphi_)=w(ix^d,bphi_)+invr*&
7231 (wprim(ix^d,bphi_)*wprim(ix^d,mr_) &
7232 -wprim(ix^d,br_)*wprim(ix^d,mphi_))
7233 end if
7234 else
7235 w(ix^d,mr_)=w(ix^d,mr_)+invr*(tmp+half*((^c&wprim(ix^d,b^c_)**2+)+&
7236 (^c&ef(ix^d,^c)**2+)*inv_squared_c))
7237 end if
7238 if(mhd_glm) w(ix^d,br_)=w(ix^d,br_)+wprim(ix^d,psi_)*invr
7239 {end do\}
7240 case (spherical)
7241 {do ix^db=ixomin^db,ixomax^db\}
7242 ! include dt in invr, invr is always used with qdt
7243 if(local_timestep) then
7244 invr=block%dt(ix^d)*dtfactor/x(ix^d,1)
7245 else
7246 invr=qdt/x(ix^d,1)
7247 end if
7248 ! E=Bxv
7249 {^ifthreec
7250 ef(ix^d,1)=wprim(ix^d,b2_)*wprim(ix^d,m3_)-wprim(ix^d,b3_)*wprim(ix^d,m2_)
7251 ef(ix^d,2)=wprim(ix^d,b3_)*wprim(ix^d,m1_)-wprim(ix^d,b1_)*wprim(ix^d,m3_)
7252 ef(ix^d,3)=wprim(ix^d,b1_)*wprim(ix^d,m2_)-wprim(ix^d,b2_)*wprim(ix^d,m1_)
7253 }
7254 {^iftwoc
7255 ! store e3 in e1 to count e3 when ^C is from 1 to 2
7256 ef(ix^d,1)=wprim(ix^d,b1_)*wprim(ix^d,m2_)-wprim(ix^d,b2_)*wprim(ix^d,m1_)
7257 ef(ix^d,2)=zero
7258 }
7259 {^ifonec
7260 ef(ix^d,1)=zero
7261 }
7262 if(mhd_energy) then
7263 tmp1=wprim(ix^d,p_)+half*((^c&wprim(ix^d,b^c_)**2+)+(^c&ef(ix^d,^c)**2+)*inv_squared_c)
7264 else
7265 tmp1=adiabs(ix^d)*wprim(ix^d,rho_)**gammas(ix^d)+half*((^c&wprim(ix^d,b^c_)**2+)+(^c&ef(ix^d,^c)**2+)*inv_squared_c)
7266 end if
7267 ! m1
7268 {^ifonec
7269 w(ix^d,m1_)=w(ix^d,m1_)+two*tmp1*invr
7270 }
7271 {^noonec
7272 w(ix^d,m1_)=w(ix^d,m1_)+invr*&
7273 (two*tmp1+(^ce&wprim(ix^d,rho_)*wprim(ix^d,m^ce_)**2-&
7274 wprim(ix^d,b^ce_)**2-ef(ix^d,^ce)**2*inv_squared_c+))
7275 }
7276 ! b1
7277 if(mhd_glm) then
7278 w(ix^d,b1_)=w(ix^d,b1_)+invr*2.0d0*wprim(ix^d,psi_)
7279 end if
7280 {^ifoned
7281 cot=0.d0
7282 }
7283 {^nooned
7284 cot=1.d0/tan(x(ix^d,2))
7285 }
7286 {^iftwoc
7287 ! m2
7288 w(ix^d,m2_)=w(ix^d,m2_)+invr*(tmp1*cot-wprim(ix^d,rho_)*wprim(ix^d,m1_)*wprim(ix^d,m2_)&
7289 +wprim(ix^d,b1_)*wprim(ix^d,b2_)+ef(ix^d,1)*ef(ix^d,2)*inv_squared_c)
7290 ! b2
7291 if(.not.stagger_grid) then
7292 tmp=wprim(ix^d,m1_)*wprim(ix^d,b2_)-wprim(ix^d,m2_)*wprim(ix^d,b1_)
7293 if(mhd_glm) then
7294 tmp=tmp+wprim(ix^d,psi_)*cot
7295 end if
7296 w(ix^d,b2_)=w(ix^d,b2_)+tmp*invr
7297 end if
7298 }
7299
7300 {^ifthreec
7301 ! m2
7302 w(ix^d,m2_)=w(ix^d,m2_)+invr*(tmp1*cot-wprim(ix^d,rho_)*wprim(ix^d,m1_)*wprim(ix^d,m2_) &
7303 +wprim(ix^d,b1_)*wprim(ix^d,b2_)+ef(ix^d,1)*ef(ix^d,2)*inv_squared_c&
7304 +(wprim(ix^d,rho_)*wprim(ix^d,m3_)**2&
7305 -wprim(ix^d,b3_)**2-ef(ix^d,3)**2*inv_squared_c)*cot)
7306 ! b2
7307 if(.not.stagger_grid) then
7308 tmp=wprim(ix^d,m1_)*wprim(ix^d,b2_)-wprim(ix^d,m2_)*wprim(ix^d,b1_)
7309 if(mhd_glm) then
7310 tmp=tmp+wprim(ix^d,psi_)*cot
7311 end if
7312 w(ix^d,b2_)=w(ix^d,b2_)+tmp*invr
7313 end if
7314 ! m3
7315 w(ix^d,m3_)=w(ix^d,m3_)+invr*&
7316 (-wprim(ix^d,m3_)*wprim(ix^d,m1_)*wprim(ix^d,rho_) &
7317 +wprim(ix^d,b3_)*wprim(ix^d,b1_) &
7318 +ef(ix^d,3)*ef(ix^d,1)*inv_squared_c&
7319 +(-wprim(ix^d,m2_)*wprim(ix^d,m3_)*wprim(ix^d,rho_) &
7320 +wprim(ix^d,b2_)*wprim(ix^d,b3_)&
7321 +ef(ix^d,2)*ef(ix^d,3)*inv_squared_c)*cot)
7322 ! b3
7323 if(.not.stagger_grid) then
7324 w(ix^d,b3_)=w(ix^d,b3_)+invr*&
7325 (wprim(ix^d,m1_)*wprim(ix^d,b3_) &
7326 -wprim(ix^d,m3_)*wprim(ix^d,b1_) &
7327 -(wprim(ix^d,m3_)*wprim(ix^d,b2_) &
7328 -wprim(ix^d,m2_)*wprim(ix^d,b3_))*cot)
7329 end if
7330 }
7331 {end do\}
7332 end select
7333
7334 if (mhd_rotating_frame) then
7335 call rotating_frame_add_source(qdt,dtfactor,ixi^l,ixo^l,wprim,w,x)
7336 end if
7337
7338 end subroutine mhd_add_source_geom_semirelati
7339
7340 ! Add geometrical source terms to w
7341 ! Geometric sources to momentum and induction
7342 ! for those cases where any kind of splitting (B0field or has_equi_rho_and_p) is active
7343 ! This implies that there is an energy equation included for sure
7344 ! B0field impacts terms in induction equation and geometric sources for them
7345 ! both flags affect the terms in momentum equation, in three variants (TF, TT, FT)
7346 ! NOTE: Hall terms in induction not handled yet
7347 subroutine mhd_add_source_geom_split(qdt,dtfactor,ixI^L,ixO^L,wCT,wprim,w,x)
7349 use mod_geometry
7352
7353 integer, intent(in) :: ixi^l, ixo^l
7354 double precision, intent(in) :: qdt, dtfactor,x(ixi^s,1:ndim)
7355 double precision, intent(inout) :: wct(ixi^s,1:nw),wprim(ixi^s,1:nw),w(ixi^s,1:nw)
7356
7357 double precision :: tmp,tmp1,tmp2,invr,cot
7358 double precision :: adiabs(ixi^s), gammas(ixi^s)
7359 integer :: ix^d
7360 integer :: mr_,mphi_ ! Polar var. names
7361 integer :: br_,bphi_
7362
7363 if(.not.mhd_energy) then
7364 if(associated(usr_set_adiab)) then
7365 call usr_set_adiab(wprim,x,ixi^l,ixo^l,adiabs)
7366 else
7367 adiabs=mhd_adiab
7368 end if
7369 if(associated(usr_set_gamma)) then
7370 call usr_set_gamma(wprim,x,ixi^l,ixo^l,gammas)
7371 else
7372 gammas=eos%gamma
7373 end if
7374 end if
7375
7376 mr_=mom(1); mphi_=mom(1)-1+phi_ ! Polar var. names
7377 br_=mag(1); bphi_=mag(1)-1+phi_
7378
7379
7380 select case (coordinate)
7381 case (cylindrical)
7382 {do ix^db=ixomin^db,ixomax^db\}
7383 ! include dt in invr, invr is always used with qdt
7384 if(local_timestep) then
7385 invr=block%dt(ix^d) * dtfactor/x(ix^d,1)
7386 else
7387 invr=qdt/x(ix^d,1)
7388 end if
7389 if(mhd_energy) then
7390 tmp=wprim(ix^d,p_)+half*(^c&wprim(ix^d,b^c_)**2+)
7391 else
7392 tmp=adiabs(ix^d)*wprim(ix^d,rho_)**gammas(ix^d)+half*(^c&wprim(ix^d,b^c_)**2+)
7393 end if
7394 if(b0field) tmp=tmp+(^c&block%B0(ix^d,^c,0)*wprim(ix^d,b^c_)+)
7395 if(phi_>0) then
7396 w(ix^d,mr_)=w(ix^d,mr_)+invr*(tmp-&
7397 wprim(ix^d,bphi_)**2+wprim(ix^d,mphi_)*wct(ix^d,mphi_))
7398 if(b0field) then
7399 w(ix^d,mr_)=w(ix^d,mr_)+invr*(-block%B0(ix^d,phi_,0)*wprim(ix^d,bphi_)-wprim(ix^d,bphi_)*block%B0(ix^d,phi_,0))
7400 endif
7401 w(ix^d,mphi_)=w(ix^d,mphi_)+invr*(&
7402 -wct(ix^d,mphi_)*wprim(ix^d,mr_) &
7403 +wprim(ix^d,bphi_)*wprim(ix^d,br_))
7404 if(b0field) then
7405 w(ix^d,mphi_)=w(ix^d,mphi_)+invr*(block%B0(ix^d,phi_,0)*wprim(ix^d,br_)+wprim(ix^d,bphi_)*block%B0(ix^d,r_,0))
7406 endif
7407 if(.not.stagger_grid) then
7408 w(ix^d,bphi_)=w(ix^d,bphi_)+invr*&
7409 (wprim(ix^d,bphi_)*wprim(ix^d,mr_) &
7410 -wprim(ix^d,br_)*wprim(ix^d,mphi_))
7411 if(b0field) then
7412 w(ix^d,bphi_)=w(ix^d,bphi_)+invr*&
7413 (block%B0(ix^d,phi_,0)*wprim(ix^d,mr_) &
7414 -block%B0(ix^d,r_,0)*wprim(ix^d,mphi_))
7415 endif
7416 end if
7417 else
7418 w(ix^d,mr_)=w(ix^d,mr_)+invr*tmp
7419 end if
7420 if(mhd_glm) w(ix^d,br_)=w(ix^d,br_)+wprim(ix^d,psi_)*invr
7421 {end do\}
7422 case (spherical)
7423 {do ix^db=ixomin^db,ixomax^db\}
7424 ! include dt in invr, invr is always used with qdt
7425 if(local_timestep) then
7426 invr=block%dt(ix^d) * dtfactor/x(ix^d,1)
7427 else
7428 invr=qdt/x(ix^d,1)
7429 end if
7430 tmp1=wprim(ix^d,p_)+half*(^c&wprim(ix^d,b^c_)**2+)
7431 if(b0field) tmp2=(^c&block%B0(ix^d,^c,0)*wprim(ix^d,b^c_)+)
7432 ! m1
7433 {^ifonec
7434 w(ix^d,mom(1))=w(ix^d,mom(1))+two*tmp1*invr
7435 if(b0field) w(ix^d,mom(1))=w(ix^d,mom(1))+two*tmp2*invr
7436 }
7437 {^noonec
7438 if(b0field) then
7439 w(ix^d,mom(1))=w(ix^d,mom(1))+invr*&
7440 (two*(tmp1+tmp2)+(^ce&wprim(ix^d,m^ce_)*wct(ix^d,m^ce_)-wprim(ix^d,b^ce_)**2+)- &
7441 (^ce&two*block%B0(ix^d,^ce,0)*wprim(ix^d,b^ce_)+))
7442 else
7443 w(ix^d,mom(1))=w(ix^d,mom(1))+invr*&
7444 (two*tmp1+(^ce&wprim(ix^d,m^ce_)*wct(ix^d,m^ce_)-wprim(ix^d,b^ce_)**2+))
7445 end if
7446 }
7447 ! b1
7448 if(mhd_glm) then
7449 w(ix^d,mag(1))=w(ix^d,mag(1))+invr*2.0d0*wprim(ix^d,psi_)
7450 end if
7451 {^ifoned
7452 cot=0.d0
7453 }
7454 {^nooned
7455 cot=1.d0/tan(x(ix^d,2))
7456 }
7457 {^iftwoc
7458 ! m2
7459 if(b0field) then
7460 w(ix^d,mom(2))=w(ix^d,mom(2))+invr*((tmp1+tmp2)*cot-wprim(ix^d,m1_)*wct(ix^d,m2_)&
7461 +wprim(ix^d,b1_)*wprim(ix^d,b2_)+block%B0(ix^d,1,0)*wprim(ix^d,b2_)&
7462 +wprim(ix^d,b1_)*block%B0(ix^d,2,0))
7463 else
7464 w(ix^d,mom(2))=w(ix^d,mom(2))+invr*(tmp1*cot-wprim(ix^d,m1_)*wct(ix^d,m2_)&
7465 +wprim(ix^d,b1_)*wprim(ix^d,b2_))
7466 end if
7467 ! b2
7468 if(.not.stagger_grid) then
7469 if(b0field) then
7470 tmp=wprim(ix^d,m1_)*wprim(ix^d,b2_)-wprim(ix^d,m2_)*wprim(ix^d,b1_)&
7471 +wprim(ix^d,m1_)*block%B0(ix^d,2,0)-wprim(ix^d,m2_)*block%B0(ix^d,1,0)
7472 else
7473 tmp=wprim(ix^d,m1_)*wprim(ix^d,b2_)-wprim(ix^d,m2_)*wprim(ix^d,b1_)
7474 end if
7475 if(mhd_glm) then
7476 tmp=tmp+wprim(ix^d,psi_)*cot
7477 end if
7478 w(ix^d,mag(2))=w(ix^d,mag(2))+tmp*invr
7479 end if
7480 }
7481 {^ifthreec
7482 ! m2
7483 if(b0field) then
7484 w(ix^d,mom(2))=w(ix^d,mom(2))+invr*((tmp1+tmp2)*cot-wprim(ix^d,m1_)*wct(ix^d,m2_)&
7485 +wprim(ix^d,b1_)*wprim(ix^d,b2_)+block%B0(ix^d,1,0)*wprim(ix^d,b2_)&
7486 +wprim(ix^d,b1_)*block%B0(ix^d,2,0)&
7487 +(wprim(ix^d,m3_)*wct(ix^d,m3_)-wprim(ix^d,b3_)**2-two*block%B0(ix^d,3,0)*wprim(ix^d,b3_))*cot)
7488 else
7489 w(ix^d,mom(2))=w(ix^d,mom(2))+invr*(tmp1*cot-wprim(ix^d,m1_)*wct(ix^d,m2_)&
7490 +wprim(ix^d,b1_)*wprim(ix^d,b2_)&
7491 +(wprim(ix^d,m3_)*wct(ix^d,m3_)-wprim(ix^d,b3_)**2)*cot)
7492 end if
7493 ! b2
7494 if(.not.stagger_grid) then
7495 if(b0field) then
7496 tmp=wprim(ix^d,m1_)*wprim(ix^d,b2_)-wprim(ix^d,m2_)*wprim(ix^d,b1_)&
7497 +wprim(ix^d,m1_)*block%B0(ix^d,2,0)-wprim(ix^d,m2_)*block%B0(ix^d,1,0)
7498 else
7499 tmp=wprim(ix^d,m1_)*wprim(ix^d,b2_)-wprim(ix^d,m2_)*wprim(ix^d,b1_)
7500 end if
7501 if(mhd_glm) then
7502 tmp=tmp+wprim(ix^d,psi_)*cot
7503 end if
7504 w(ix^d,mag(2))=w(ix^d,mag(2))+tmp*invr
7505 end if
7506 ! m3
7507 if(b0field) then
7508 w(ix^d,mom(3))=w(ix^d,mom(3))-invr*&
7509 (wprim(ix^d,m3_)*wct(ix^d,m1_) &
7510 -wprim(ix^d,b3_)*wprim(ix^d,b1_) &
7511 +block%B0(ix^d,1,0)*wprim(ix^d,b3_) &
7512 +wprim(ix^d,b1_)*block%B0(ix^d,3,0) &
7513 +(wprim(ix^d,m2_)*wct(ix^d,m3_) &
7514 -wprim(ix^d,b2_)*wprim(ix^d,b3_) &
7515 +block%B0(ix^d,2,0)*wprim(ix^d,b3_) &
7516 +wprim(ix^d,b2_)*block%B0(ix^d,3,0))*cot)
7517 else
7518 w(ix^d,mom(3))=w(ix^d,mom(3))-invr*&
7519 (wprim(ix^d,m3_)*wct(ix^d,m1_) &
7520 -wprim(ix^d,b3_)*wprim(ix^d,b1_) &
7521 +(wprim(ix^d,m2_)*wct(ix^d,m3_) &
7522 -wprim(ix^d,b2_)*wprim(ix^d,b3_))*cot)
7523 end if
7524 ! b3
7525 if(.not.stagger_grid) then
7526 if(b0field) then
7527 w(ix^d,mag(3))=w(ix^d,mag(3))+invr*&
7528 (wprim(ix^d,m1_)*wprim(ix^d,b3_) &
7529 -wprim(ix^d,m3_)*wprim(ix^d,b1_) &
7530 +wprim(ix^d,m1_)*block%B0(ix^d,3,0) &
7531 -wprim(ix^d,m3_)*block%B0(ix^d,1,0) &
7532 -(wprim(ix^d,m3_)*wprim(ix^d,b2_) &
7533 -wprim(ix^d,m2_)*wprim(ix^d,b3_) &
7534 +wprim(ix^d,m3_)*block%B0(ix^d,2,0) &
7535 -wprim(ix^d,m2_)*block%B0(ix^d,3,0))*cot)
7536 else
7537 w(ix^d,mag(3))=w(ix^d,mag(3))+invr*&
7538 (wprim(ix^d,m1_)*wprim(ix^d,b3_) &
7539 -wprim(ix^d,m3_)*wprim(ix^d,b1_) &
7540 -(wprim(ix^d,m3_)*wprim(ix^d,b2_) &
7541 -wprim(ix^d,m2_)*wprim(ix^d,b3_))*cot)
7542 end if
7543 end if
7544 }
7545 {end do\}
7546 end select
7547
7548 if (mhd_rotating_frame) then
7549 call rotating_frame_add_source(qdt,dtfactor,ixi^l,ixo^l,wprim,w,x)
7550 end if
7551
7552 end subroutine mhd_add_source_geom_split
7553
7554 !> Compute 2 times total magnetic energy
7555 function mhd_mag_en_all(w, ixI^L, ixO^L) result(mge)
7557 integer, intent(in) :: ixi^l, ixo^l
7558 double precision, intent(in) :: w(ixi^s, nw)
7559 double precision :: mge(ixo^s)
7560
7561 if (b0field) then
7562 mge = sum((w(ixo^s, mag(:))+block%B0(ixo^s,:,b0i))**2, dim=ndim+1)
7563 else
7564 mge = sum(w(ixo^s, mag(:))**2, dim=ndim+1)
7565 end if
7566 end function mhd_mag_en_all
7567
7568 subroutine mhd_getv_hall(w,x,ixI^L,ixO^L,vHall,partial)
7570 use mod_geometry
7571
7572 integer, intent(in) :: ixi^l, ixo^l
7573 double precision, intent(in) :: w(ixi^s,nw)
7574 double precision, intent(in) :: x(ixi^s,1:ndim)
7575 double precision, intent(inout) :: vhall(ixi^s,1:ndir)
7576 logical, intent(in), optional :: partial
7577
7578 double precision :: current(ixi^s,7-2*ndir:3)
7579 double precision :: rho(ixi^s)
7580 integer :: idir, idirmin, ix^d
7581 logical :: use_partial
7582
7583 use_partial=.false.
7584 if(present(partial)) use_partial=partial
7585 call mhd_get_rho(w,x,ixi^l,ixo^l,rho)
7586 if(.not.use_partial)then
7587 ! Calculate current density and idirmin, including J0 when split
7588 call get_current(w,ixi^l,ixo^l,idirmin,current)
7589 else
7590 if(slab_uniform) then
7591 ! fourth order CD in cartesian
7592 call curlvector(w(ixi^s,mag(1:ndir)),ixi^l,ixo^l,current,idirmin,7-2*ndir,ndir,.true.)
7593 else
7594 call curlvector(w(ixi^s,mag(1:ndir)),ixi^l,ixo^l,current,idirmin,7-2*ndir,ndir)
7595 endif
7596 endif
7597 do idir = idirmin, ndir
7598 {do ix^db=ixomin^db,ixomax^db\}
7599 vhall(ix^d,idir)=-mhd_etah*current(ix^d,idir)/rho(ix^d)
7600 {end do\}
7601 end do
7602
7603 end subroutine mhd_getv_hall
7604
7605 subroutine mhd_modify_wlr(ixI^L,ixO^L,qt,wLC,wRC,wLp,wRp,s,idir)
7607 use mod_usr_methods
7608 integer, intent(in) :: ixi^l, ixo^l, idir
7609 double precision, intent(in) :: qt
7610 double precision, intent(inout) :: wlc(ixi^s,1:nw), wrc(ixi^s,1:nw)
7611 double precision, intent(inout) :: wlp(ixi^s,1:nw), wrp(ixi^s,1:nw)
7612 type(state) :: s
7613
7614 double precision :: db
7615 integer :: ix^d, saved_b0i
7616
7617 if(stagger_grid) then
7618 {do ix^db=ixomin^db,ixomax^db\}
7619 wlc(ix^d,mag(idir))=s%ws(ix^d,idir)
7620 wrc(ix^d,mag(idir))=s%ws(ix^d,idir)
7621 wlp(ix^d,mag(idir))=s%ws(ix^d,idir)
7622 wrp(ix^d,mag(idir))=s%ws(ix^d,idir)
7623 {end do\}
7624 else
7625 ! Solve the Riemann problem for the linear 2x2 system for normal
7626 ! B-field and GLM_Psi according to Dedner 2002:
7627 ! This implements eq. (42) in Dedner et al. 2002 JcP 175
7628 ! Gives the Riemann solution on the interface
7629 ! for the normal B component and Psi in the GLM-MHD system.
7630 ! 23/04/2013 Oliver Porth
7631 {do ix^db=ixomin^db,ixomax^db\}
7632 db=wrp(ix^d,mag(idir))-wlp(ix^d,mag(idir))
7633 wlp(ix^d,mag(idir))=half*(wrp(ix^d,mag(idir))+wlp(ix^d,mag(idir))-(wrp(ix^d,psi_)-wlp(ix^d,psi_))/cmax_global)
7634 wlp(ix^d,psi_)=half*(wrp(ix^d,psi_)+wlp(ix^d,psi_)-db*cmax_global)
7635 wrp(ix^d,mag(idir))=wlp(ix^d,mag(idir))
7636 wrp(ix^d,psi_)=wlp(ix^d,psi_)
7637 if(total_energy .and. .not.mhd_semirelativistic) then
7638 wrc(ix^d,e_)=wrc(ix^d,e_)-half*wrc(ix^d,mag(idir))**2
7639 wlc(ix^d,e_)=wlc(ix^d,e_)-half*wlc(ix^d,mag(idir))**2
7640 end if
7641 wrc(ix^d,mag(idir))=wlp(ix^d,mag(idir))
7642 wrc(ix^d,psi_)=wlp(ix^d,psi_)
7643 wlc(ix^d,mag(idir))=wlp(ix^d,mag(idir))
7644 wlc(ix^d,psi_)=wlp(ix^d,psi_)
7645 ! modify total energy according to the change of magnetic field
7646 if(total_energy .and. .not.mhd_semirelativistic) then
7647 wrc(ix^d,e_)=wrc(ix^d,e_)+half*wrc(ix^d,mag(idir))**2
7648 wlc(ix^d,e_)=wlc(ix^d,e_)+half*wlc(ix^d,mag(idir))**2
7649 end if
7650 {end do\}
7651 end if
7652
7653 if(mhd_semirelativistic) then
7654 ! CT/GLM changed the face field. Rebuild both momentum and energy from
7655 ! the same primitive state, including the electric terms and B0 split.
7656 saved_b0i=b0i
7657 b0i=idir
7658 wlc(ixo^s,1:nw)=wlp(ixo^s,1:nw)
7659 wrc(ixo^s,1:nw)=wrp(ixo^s,1:nw)
7660 call phys_to_conserved(ixi^l,ixo^l,wlc,s%x)
7661 call phys_to_conserved(ixi^l,ixo^l,wrc,s%x)
7662 b0i=saved_b0i
7663 end if
7664
7665 ! User overrides receive consistent states and remain responsible for
7666 ! keeping their primitive/conserved outputs mutually consistent.
7667 if(associated(usr_set_wlr)) call usr_set_wlr(ixi^l,ixo^l,qt,wlc,wrc,wlp,wrp,s,idir)
7668
7669 end subroutine mhd_modify_wlr
7670
7671 subroutine mhd_boundary_adjust(igrid,psb)
7673 integer, intent(in) :: igrid
7674 type(state), target :: psb(max_blocks)
7675
7676 integer :: ib, idims, iside, ixo^l, i^d
7677
7678 block=>ps(igrid)
7679 ^d&dxlevel(^d)=rnode(rpdx^d_,igrid);
7680 do idims=1,ndim
7681 ! to avoid using as yet unknown corner info in more than 1D, we
7682 ! fill only interior mesh ranges of the ghost cell ranges at first,
7683 ! and progressively enlarge the ranges to include corners later
7684 do iside=1,2
7685 i^d=kr(^d,idims)*(2*iside-3);
7686 if (neighbor_type(i^d,igrid)/=1) cycle
7687 ib=(idims-1)*2+iside
7688 if(.not.boundary_divbfix(ib)) cycle
7689 if(any(typeboundary(:,ib)==bc_special)) then
7690 ! MF nonlinear force-free B field extrapolation and data driven
7691 ! require normal B of the first ghost cell layer to be untouched by
7692 ! fixdivB=0 process, set boundary_divbfix_skip(iB)=1 in par file
7693 select case (idims)
7694 {case (^d)
7695 if (iside==2) then
7696 ! maximal boundary
7697 ixomin^dd=ixghi^d+1-nghostcells+boundary_divbfix_skip(2*^d)^d%ixOmin^dd=ixglo^dd;
7698 ixomax^dd=ixghi^dd;
7699 else
7700 ! minimal boundary
7701 ixomin^dd=ixglo^dd;
7702 ixomax^dd=ixglo^d-1+nghostcells-boundary_divbfix_skip(2*^d-1)^d%ixOmax^dd=ixghi^dd;
7703 end if \}
7704 end select
7705 call fixdivb_boundary(ixg^ll,ixo^l,psb(igrid)%w,psb(igrid)%x,ib)
7706 end if
7707 end do
7708 end do
7709
7710 end subroutine mhd_boundary_adjust
7711
7712 subroutine fixdivb_boundary(ixG^L,ixO^L,w,x,iB)
7714
7715 integer, intent(in) :: ixg^l,ixo^l,ib
7716 double precision, intent(inout) :: w(ixg^s,1:nw)
7717 double precision, intent(in) :: x(ixg^s,1:ndim)
7718
7719 double precision :: dx1x2,dx1x3,dx2x1,dx2x3,dx3x1,dx3x2
7720 integer :: ix^d,ixf^l
7721
7722 select case(ib)
7723 case(1)
7724 ! 2nd order CD for divB=0 to set normal B component better
7725 ! if(total_energy) call eos%to_primitive(ixG^L,ixO^L,w,x)
7726 if(total_energy) call eos%to_primitive(ixg^l,ixo^l,w,x)
7727 {^iftwod
7728 ixfmin1=ixomin1+1
7729 ixfmax1=ixomax1+1
7730 ixfmin2=ixomin2+1
7731 ixfmax2=ixomax2-1
7732 if(slab_uniform) then
7733 dx1x2=dxlevel(1)/dxlevel(2)
7734 do ix1=ixfmax1,ixfmin1,-1
7735 w(ix1-1,ixfmin2:ixfmax2,mag(1))=w(ix1+1,ixfmin2:ixfmax2,mag(1)) &
7736 +dx1x2*(w(ix1,ixfmin2+1:ixfmax2+1,mag(2))-&
7737 w(ix1,ixfmin2-1:ixfmax2-1,mag(2)))
7738 enddo
7739 else
7740 do ix1=ixfmax1,ixfmin1,-1
7741 w(ix1-1,ixfmin2:ixfmax2,mag(1))=( (w(ix1+1,ixfmin2:ixfmax2,mag(1))+&
7742 w(ix1,ixfmin2:ixfmax2,mag(1)))*block%surfaceC(ix1,ixfmin2:ixfmax2,1)&
7743 +(w(ix1,ixfmin2+1:ixfmax2+1,mag(2))+w(ix1,ixfmin2:ixfmax2,mag(2)))*&
7744 block%surfaceC(ix1,ixfmin2:ixfmax2,2)&
7745 -(w(ix1,ixfmin2:ixfmax2,mag(2))+w(ix1,ixfmin2-1:ixfmax2-1,mag(2)))*&
7746 block%surfaceC(ix1,ixfmin2-1:ixfmax2-1,2) )&
7747 /block%surfaceC(ix1-1,ixfmin2:ixfmax2,1)-w(ix1,ixfmin2:ixfmax2,mag(1))
7748 end do
7749 end if
7750 }
7751 {^ifthreed
7752 ixfmin1=ixomin1+1
7753 ixfmax1=ixomax1+1
7754 ixfmin2=ixomin2+1
7755 ixfmax2=ixomax2-1
7756 ixfmin3=ixomin3+1
7757 ixfmax3=ixomax3-1
7758 if(slab_uniform) then
7759 dx1x2=dxlevel(1)/dxlevel(2)
7760 dx1x3=dxlevel(1)/dxlevel(3)
7761 do ix1=ixfmax1,ixfmin1,-1
7762 w(ix1-1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1))=&
7763 w(ix1+1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1)) &
7764 +dx1x2*(w(ix1,ixfmin2+1:ixfmax2+1,ixfmin3:ixfmax3,mag(2))-&
7765 w(ix1,ixfmin2-1:ixfmax2-1,ixfmin3:ixfmax3,mag(2))) &
7766 +dx1x3*(w(ix1,ixfmin2:ixfmax2,ixfmin3+1:ixfmax3+1,mag(3))-&
7767 w(ix1,ixfmin2:ixfmax2,ixfmin3-1:ixfmax3-1,mag(3)))
7768 end do
7769 else
7770 do ix1=ixfmax1,ixfmin1,-1
7771 w(ix1-1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1))=&
7772 ( (w(ix1+1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1))+&
7773 w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1)))*&
7774 block%surfaceC(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,1)&
7775 +(w(ix1,ixfmin2+1:ixfmax2+1,ixfmin3:ixfmax3,mag(2))+&
7776 w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(2)))*&
7777 block%surfaceC(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,2)&
7778 -(w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(2))+&
7779 w(ix1,ixfmin2-1:ixfmax2-1,ixfmin3:ixfmax3,mag(2)))*&
7780 block%surfaceC(ix1,ixfmin2-1:ixfmax2-1,ixfmin3:ixfmax3,2)&
7781 +(w(ix1,ixfmin2:ixfmax2,ixfmin3+1:ixfmax3+1,mag(3))+&
7782 w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(3)))*&
7783 block%surfaceC(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,3)&
7784 -(w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(3))+&
7785 w(ix1,ixfmin2:ixfmax2,ixfmin3-1:ixfmax3-1,mag(3)))*&
7786 block%surfaceC(ix1,ixfmin2:ixfmax2,ixfmin3-1:ixfmax3-1,3) )&
7787 /block%surfaceC(ix1-1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,1)-&
7788 w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1))
7789 end do
7790 end if
7791 }
7792 ! if(total_energy) call eos%to_conserved(ixG^L,ixO^L,w,x)
7793 if(total_energy) call eos%to_conserved(ixg^l,ixo^l,w,x)
7794 case(2)
7795 ! if(total_energy) call eos%to_primitive(ixG^L,ixO^L,w,x)
7796 if(total_energy) call eos%to_primitive(ixg^l,ixo^l,w,x)
7797 {^iftwod
7798 ixfmin1=ixomin1-1
7799 ixfmax1=ixomax1-1
7800 ixfmin2=ixomin2+1
7801 ixfmax2=ixomax2-1
7802 if(slab_uniform) then
7803 dx1x2=dxlevel(1)/dxlevel(2)
7804 do ix1=ixfmin1,ixfmax1
7805 w(ix1+1,ixfmin2:ixfmax2,mag(1))=w(ix1-1,ixfmin2:ixfmax2,mag(1)) &
7806 -dx1x2*(w(ix1,ixfmin2+1:ixfmax2+1,mag(2))-&
7807 w(ix1,ixfmin2-1:ixfmax2-1,mag(2)))
7808 enddo
7809 else
7810 do ix1=ixfmin1,ixfmax1
7811 w(ix1+1,ixfmin2:ixfmax2,mag(1))=( (w(ix1-1,ixfmin2:ixfmax2,mag(1))+&
7812 w(ix1,ixfmin2:ixfmax2,mag(1)))*block%surfaceC(ix1-1,ixfmin2:ixfmax2,1)&
7813 -(w(ix1,ixfmin2+1:ixfmax2+1,mag(2))+w(ix1,ixfmin2:ixfmax2,mag(2)))*&
7814 block%surfaceC(ix1,ixfmin2:ixfmax2,2)&
7815 +(w(ix1,ixfmin2:ixfmax2,mag(2))+w(ix1,ixfmin2-1:ixfmax2-1,mag(2)))*&
7816 block%surfaceC(ix1,ixfmin2-1:ixfmax2-1,2) )&
7817 /block%surfaceC(ix1,ixfmin2:ixfmax2,1)-w(ix1,ixfmin2:ixfmax2,mag(1))
7818 end do
7819 end if
7820 }
7821 {^ifthreed
7822 ixfmin1=ixomin1-1
7823 ixfmax1=ixomax1-1
7824 ixfmin2=ixomin2+1
7825 ixfmax2=ixomax2-1
7826 ixfmin3=ixomin3+1
7827 ixfmax3=ixomax3-1
7828 if(slab_uniform) then
7829 dx1x2=dxlevel(1)/dxlevel(2)
7830 dx1x3=dxlevel(1)/dxlevel(3)
7831 do ix1=ixfmin1,ixfmax1
7832 w(ix1+1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1))=&
7833 w(ix1-1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1)) &
7834 -dx1x2*(w(ix1,ixfmin2+1:ixfmax2+1,ixfmin3:ixfmax3,mag(2))-&
7835 w(ix1,ixfmin2-1:ixfmax2-1,ixfmin3:ixfmax3,mag(2))) &
7836 -dx1x3*(w(ix1,ixfmin2:ixfmax2,ixfmin3+1:ixfmax3+1,mag(3))-&
7837 w(ix1,ixfmin2:ixfmax2,ixfmin3-1:ixfmax3-1,mag(3)))
7838 end do
7839 else
7840 do ix1=ixfmin1,ixfmax1
7841 w(ix1+1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1))=&
7842 ( (w(ix1-1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1))+&
7843 w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1)))*&
7844 block%surfaceC(ix1-1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,1)&
7845 -(w(ix1,ixfmin2+1:ixfmax2+1,ixfmin3:ixfmax3,mag(2))+&
7846 w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(2)))*&
7847 block%surfaceC(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,2)&
7848 +(w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(2))+&
7849 w(ix1,ixfmin2-1:ixfmax2-1,ixfmin3:ixfmax3,mag(2)))*&
7850 block%surfaceC(ix1,ixfmin2-1:ixfmax2-1,ixfmin3:ixfmax3,2)&
7851 -(w(ix1,ixfmin2:ixfmax2,ixfmin3+1:ixfmax3+1,mag(3))+&
7852 w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(3)))*&
7853 block%surfaceC(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,3)&
7854 +(w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(3))+&
7855 w(ix1,ixfmin2:ixfmax2,ixfmin3-1:ixfmax3-1,mag(3)))*&
7856 block%surfaceC(ix1,ixfmin2:ixfmax2,ixfmin3-1:ixfmax3-1,3) )&
7857 /block%surfaceC(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,1)-&
7858 w(ix1,ixfmin2:ixfmax2,ixfmin3:ixfmax3,mag(1))
7859 end do
7860 end if
7861 }
7862 ! if(total_energy) call eos%to_conserved(ixG^L,ixO^L,w,x)
7863 if(total_energy) call eos%to_conserved(ixg^l,ixo^l,w,x)
7864 case(3)
7865 ! if(total_energy) call eos%to_primitive(ixG^L,ixO^L,w,x)
7866 if(total_energy) call eos%to_primitive(ixg^l,ixo^l,w,x)
7867 {^iftwod
7868 ixfmin1=ixomin1+1
7869 ixfmax1=ixomax1-1
7870 ixfmin2=ixomin2+1
7871 ixfmax2=ixomax2+1
7872 if(slab_uniform) then
7873 dx2x1=dxlevel(2)/dxlevel(1)
7874 do ix2=ixfmax2,ixfmin2,-1
7875 w(ixfmin1:ixfmax1,ix2-1,mag(2))=w(ixfmin1:ixfmax1,ix2+1,mag(2)) &
7876 +dx2x1*(w(ixfmin1+1:ixfmax1+1,ix2,mag(1))-&
7877 w(ixfmin1-1:ixfmax1-1,ix2,mag(1)))
7878 enddo
7879 else
7880 do ix2=ixfmax2,ixfmin2,-1
7881 w(ixfmin1:ixfmax1,ix2-1,mag(2))=( (w(ixfmin1:ixfmax1,ix2+1,mag(2))+&
7882 w(ixfmin1:ixfmax1,ix2,mag(2)))*block%surfaceC(ixfmin1:ixfmax1,ix2,2)&
7883 +(w(ixfmin1+1:ixfmax1+1,ix2,mag(1))+w(ixfmin1:ixfmax1,ix2,mag(1)))*&
7884 block%surfaceC(ixfmin1:ixfmax1,ix2,1)&
7885 -(w(ixfmin1:ixfmax1,ix2,mag(1))+w(ixfmin1-1:ixfmax1-1,ix2,mag(1)))*&
7886 block%surfaceC(ixfmin1-1:ixfmax1-1,ix2,1) )&
7887 /block%surfaceC(ixfmin1:ixfmax1,ix2-1,2)-w(ixfmin1:ixfmax1,ix2,mag(2))
7888 end do
7889 end if
7890 }
7891 {^ifthreed
7892 ixfmin1=ixomin1+1
7893 ixfmax1=ixomax1-1
7894 ixfmin3=ixomin3+1
7895 ixfmax3=ixomax3-1
7896 ixfmin2=ixomin2+1
7897 ixfmax2=ixomax2+1
7898 if(slab_uniform) then
7899 dx2x1=dxlevel(2)/dxlevel(1)
7900 dx2x3=dxlevel(2)/dxlevel(3)
7901 do ix2=ixfmax2,ixfmin2,-1
7902 w(ixfmin1:ixfmax1,ix2-1,ixfmin3:ixfmax3,mag(2))=w(ixfmin1:ixfmax1,&
7903 ix2+1,ixfmin3:ixfmax3,mag(2)) &
7904 +dx2x1*(w(ixfmin1+1:ixfmax1+1,ix2,ixfmin3:ixfmax3,mag(1))-&
7905 w(ixfmin1-1:ixfmax1-1,ix2,ixfmin3:ixfmax3,mag(1))) &
7906 +dx2x3*(w(ixfmin1:ixfmax1,ix2,ixfmin3+1:ixfmax3+1,mag(3))-&
7907 w(ixfmin1:ixfmax1,ix2,ixfmin3-1:ixfmax3-1,mag(3)))
7908 end do
7909 else
7910 do ix2=ixfmax2,ixfmin2,-1
7911 w(ixfmin1:ixfmax1,ix2-1,ixfmin3:ixfmax3,mag(2))=&
7912 ( (w(ixfmin1:ixfmax1,ix2+1,ixfmin3:ixfmax3,mag(2))+&
7913 w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(2)))*&
7914 block%surfaceC(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,2)&
7915 +(w(ixfmin1+1:ixfmax1+1,ix2,ixfmin3:ixfmax3,mag(1))+&
7916 w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(1)))*&
7917 block%surfaceC(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,1)&
7918 -(w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(1))+&
7919 w(ixfmin1-1:ixfmax1-1,ix2,ixfmin3:ixfmax3,mag(1)))*&
7920 block%surfaceC(ixfmin1-1:ixfmax1-1,ix2,ixfmin3:ixfmax3,1)&
7921 +(w(ixfmin1:ixfmax1,ix2,ixfmin3+1:ixfmax3+1,mag(3))+&
7922 w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(3)))*&
7923 block%surfaceC(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,3)&
7924 -(w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(3))+&
7925 w(ixfmin1:ixfmax1,ix2,ixfmin3-1:ixfmax3-1,mag(3)))*&
7926 block%surfaceC(ixfmin1:ixfmax1,ix2,ixfmin3-1:ixfmax3-1,3) )&
7927 /block%surfaceC(ixfmin1:ixfmax1,ix2-1,ixfmin3:ixfmax3,2)-&
7928 w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(2))
7929 end do
7930 end if
7931 }
7932 ! if(total_energy) call eos%to_conserved(ixG^L,ixO^L,w,x)
7933 if(total_energy) call eos%to_conserved(ixg^l,ixo^l,w,x)
7934 case(4)
7935 ! if(total_energy) call eos%to_primitive(ixG^L,ixO^L,w,x)
7936 if(total_energy) call eos%to_primitive(ixg^l,ixo^l,w,x)
7937 {^iftwod
7938 ixfmin1=ixomin1+1
7939 ixfmax1=ixomax1-1
7940 ixfmin2=ixomin2-1
7941 ixfmax2=ixomax2-1
7942 if(slab_uniform) then
7943 dx2x1=dxlevel(2)/dxlevel(1)
7944 do ix2=ixfmin2,ixfmax2
7945 w(ixfmin1:ixfmax1,ix2+1,mag(2))=w(ixfmin1:ixfmax1,ix2-1,mag(2)) &
7946 -dx2x1*(w(ixfmin1+1:ixfmax1+1,ix2,mag(1))-&
7947 w(ixfmin1-1:ixfmax1-1,ix2,mag(1)))
7948 end do
7949 else
7950 do ix2=ixfmin2,ixfmax2
7951 w(ixfmin1:ixfmax1,ix2+1,mag(2))=( (w(ixfmin1:ixfmax1,ix2-1,mag(2))+&
7952 w(ixfmin1:ixfmax1,ix2,mag(2)))*block%surfaceC(ixfmin1:ixfmax1,ix2-1,2)&
7953 -(w(ixfmin1+1:ixfmax1+1,ix2,mag(1))+w(ixfmin1:ixfmax1,ix2,mag(1)))*&
7954 block%surfaceC(ixfmin1:ixfmax1,ix2,1)&
7955 +(w(ixfmin1:ixfmax1,ix2,mag(1))+w(ixfmin1-1:ixfmax1-1,ix2,mag(1)))*&
7956 block%surfaceC(ixfmin1-1:ixfmax1-1,ix2,1) )&
7957 /block%surfaceC(ixfmin1:ixfmax1,ix2,2)-w(ixfmin1:ixfmax1,ix2,mag(2))
7958 end do
7959 end if
7960 }
7961 {^ifthreed
7962 ixfmin1=ixomin1+1
7963 ixfmax1=ixomax1-1
7964 ixfmin3=ixomin3+1
7965 ixfmax3=ixomax3-1
7966 ixfmin2=ixomin2-1
7967 ixfmax2=ixomax2-1
7968 if(slab_uniform) then
7969 dx2x1=dxlevel(2)/dxlevel(1)
7970 dx2x3=dxlevel(2)/dxlevel(3)
7971 do ix2=ixfmin2,ixfmax2
7972 w(ixfmin1:ixfmax1,ix2+1,ixfmin3:ixfmax3,mag(2))=w(ixfmin1:ixfmax1,&
7973 ix2-1,ixfmin3:ixfmax3,mag(2)) &
7974 -dx2x1*(w(ixfmin1+1:ixfmax1+1,ix2,ixfmin3:ixfmax3,mag(1))-&
7975 w(ixfmin1-1:ixfmax1-1,ix2,ixfmin3:ixfmax3,mag(1))) &
7976 -dx2x3*(w(ixfmin1:ixfmax1,ix2,ixfmin3+1:ixfmax3+1,mag(3))-&
7977 w(ixfmin1:ixfmax1,ix2,ixfmin3-1:ixfmax3-1,mag(3)))
7978 end do
7979 else
7980 do ix2=ixfmin2,ixfmax2
7981 w(ixfmin1:ixfmax1,ix2+1,ixfmin3:ixfmax3,mag(2))=&
7982 ( (w(ixfmin1:ixfmax1,ix2-1,ixfmin3:ixfmax3,mag(2))+&
7983 w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(2)))*&
7984 block%surfaceC(ixfmin1:ixfmax1,ix2-1,ixfmin3:ixfmax3,2)&
7985 -(w(ixfmin1+1:ixfmax1+1,ix2,ixfmin3:ixfmax3,mag(1))+&
7986 w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(1)))*&
7987 block%surfaceC(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,1)&
7988 +(w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(1))+&
7989 w(ixfmin1-1:ixfmax1-1,ix2,ixfmin3:ixfmax3,mag(1)))*&
7990 block%surfaceC(ixfmin1-1:ixfmax1-1,ix2,ixfmin3:ixfmax3,1)&
7991 -(w(ixfmin1:ixfmax1,ix2,ixfmin3+1:ixfmax3+1,mag(3))+&
7992 w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(3)))*&
7993 block%surfaceC(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,3)&
7994 +(w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(3))+&
7995 w(ixfmin1:ixfmax1,ix2,ixfmin3-1:ixfmax3-1,mag(3)))*&
7996 block%surfaceC(ixfmin1:ixfmax1,ix2,ixfmin3-1:ixfmax3-1,3) )&
7997 /block%surfaceC(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,2)-&
7998 w(ixfmin1:ixfmax1,ix2,ixfmin3:ixfmax3,mag(2))
7999 end do
8000 end if
8001 }
8002 ! if(total_energy) call eos%to_conserved(ixG^L,ixO^L,w,x)
8003 if(total_energy) call eos%to_conserved(ixg^l,ixo^l,w,x)
8004 {^ifthreed
8005 case(5)
8006 ! if(total_energy) call eos%to_primitive(ixG^L,ixO^L,w,x)
8007 if(total_energy) call eos%to_primitive(ixg^l,ixo^l,w,x)
8008 ixfmin1=ixomin1+1
8009 ixfmax1=ixomax1-1
8010 ixfmin2=ixomin2+1
8011 ixfmax2=ixomax2-1
8012 ixfmin3=ixomin3+1
8013 ixfmax3=ixomax3+1
8014 if(slab_uniform) then
8015 dx3x1=dxlevel(3)/dxlevel(1)
8016 dx3x2=dxlevel(3)/dxlevel(2)
8017 do ix3=ixfmax3,ixfmin3,-1
8018 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3-1,mag(3))=w(ixfmin1:ixfmax1,&
8019 ixfmin2:ixfmax2,ix3+1,mag(3)) &
8020 +dx3x1*(w(ixfmin1+1:ixfmax1+1,ixfmin2:ixfmax2,ix3,mag(1))-&
8021 w(ixfmin1-1:ixfmax1-1,ixfmin2:ixfmax2,ix3,mag(1))) &
8022 +dx3x2*(w(ixfmin1:ixfmax1,ixfmin2+1:ixfmax2+1,ix3,mag(2))-&
8023 w(ixfmin1:ixfmax1,ixfmin2-1:ixfmax2-1,ix3,mag(2)))
8024 end do
8025 else
8026 do ix3=ixfmax3,ixfmin3,-1
8027 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3-1,mag(3))=&
8028 ( (w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3+1,mag(3))+&
8029 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(3)))*&
8030 block%surfaceC(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,3)&
8031 +(w(ixfmin1+1:ixfmax1+1,ixfmin2:ixfmax2,ix3,mag(1))+&
8032 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(1)))*&
8033 block%surfaceC(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,1)&
8034 -(w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(1))+&
8035 w(ixfmin1-1:ixfmax1-1,ixfmin2:ixfmax2,ix3,mag(1)))*&
8036 block%surfaceC(ixfmin1-1:ixfmax1-1,ixfmin2:ixfmax2,ix3,1)&
8037 +(w(ixfmin1:ixfmax1,ixfmin2+1:ixfmax2+1,ix3,mag(2))+&
8038 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(2)))*&
8039 block%surfaceC(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,2)&
8040 -(w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(2))+&
8041 w(ixfmin1:ixfmax1,ixfmin2-1:ixfmax2-1,ix3,mag(2)))*&
8042 block%surfaceC(ixfmin1:ixfmax1,ixfmin2-1:ixfmax2-1,ix3,2) )&
8043 /block%surfaceC(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3-1,3)-&
8044 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(3))
8045 end do
8046 end if
8047 ! if(total_energy) call eos%to_conserved(ixG^L,ixO^L,w,x)
8048 if(total_energy) call eos%to_conserved(ixg^l,ixo^l,w,x)
8049 case(6)
8050 ! if(total_energy) call eos%to_primitive(ixG^L,ixO^L,w,x)
8051 if(total_energy) call eos%to_primitive(ixg^l,ixo^l,w,x)
8052 ixfmin1=ixomin1+1
8053 ixfmax1=ixomax1-1
8054 ixfmin2=ixomin2+1
8055 ixfmax2=ixomax2-1
8056 ixfmin3=ixomin3-1
8057 ixfmax3=ixomax3-1
8058 if(slab_uniform) then
8059 dx3x1=dxlevel(3)/dxlevel(1)
8060 dx3x2=dxlevel(3)/dxlevel(2)
8061 do ix3=ixfmin3,ixfmax3
8062 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3+1,mag(3))=w(ixfmin1:ixfmax1,&
8063 ixfmin2:ixfmax2,ix3-1,mag(3)) &
8064 -dx3x1*(w(ixfmin1+1:ixfmax1+1,ixfmin2:ixfmax2,ix3,mag(1))-&
8065 w(ixfmin1-1:ixfmax1-1,ixfmin2:ixfmax2,ix3,mag(1))) &
8066 -dx3x2*(w(ixfmin1:ixfmax1,ixfmin2+1:ixfmax2+1,ix3,mag(2))-&
8067 w(ixfmin1:ixfmax1,ixfmin2-1:ixfmax2-1,ix3,mag(2)))
8068 end do
8069 else
8070 do ix3=ixfmin3,ixfmax3
8071 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3+1,mag(3))=&
8072 ( (w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3-1,mag(3))+&
8073 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(3)))*&
8074 block%surfaceC(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3-1,3)&
8075 -(w(ixfmin1+1:ixfmax1+1,ixfmin2:ixfmax2,ix3,mag(1))+&
8076 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(1)))*&
8077 block%surfaceC(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,1)&
8078 +(w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(1))+&
8079 w(ixfmin1-1:ixfmax1-1,ixfmin2:ixfmax2,ix3,mag(1)))*&
8080 block%surfaceC(ixfmin1-1:ixfmax1-1,ixfmin2:ixfmax2,ix3,1)&
8081 -(w(ixfmin1:ixfmax1,ixfmin2+1:ixfmax2+1,ix3,mag(2))+&
8082 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(2)))*&
8083 block%surfaceC(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,2)&
8084 +(w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(2))+&
8085 w(ixfmin1:ixfmax1,ixfmin2-1:ixfmax2-1,ix3,mag(2)))*&
8086 block%surfaceC(ixfmin1:ixfmax1,ixfmin2-1:ixfmax2-1,ix3,2) )&
8087 /block%surfaceC(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,3)-&
8088 w(ixfmin1:ixfmax1,ixfmin2:ixfmax2,ix3,mag(3))
8089 end do
8090 end if
8091 ! if(total_energy) call eos%to_conserved(ixG^L,ixO^L,w,x)
8092 if(total_energy) call eos%to_conserved(ixg^l,ixo^l,w,x)
8093 }
8094 case default
8095 call mpistop("Special boundary is not defined for this region")
8096 end select
8097
8098 end subroutine fixdivb_boundary
8099
8100 {^nooned
8101 subroutine mhd_clean_divb_multigrid(qdt, qt, active)
8102 use mod_forest
8105 use mod_geometry
8106
8107 double precision, intent(in) :: qdt !< Current time step
8108 double precision, intent(in) :: qt !< Current time
8109 logical, intent(inout) :: active !< Output if the source is active
8110
8111 integer :: id
8112 integer, parameter :: max_its = 50
8113 double precision :: residual_it(max_its), max_divb
8114 double precision :: tmp(ixg^t), grad(ixg^t, ndim)
8115 double precision :: res
8116 double precision, parameter :: max_residual = 1d-3
8117 double precision, parameter :: residual_reduction = 1d-10
8118 integer :: iigrid, igrid
8119 integer :: n, nc, lvl, ix^l, ixc^l, idim
8120 type(tree_node), pointer :: pnode
8121
8122 mg%operator_type = mg_laplacian
8123
8124 ! Set boundary conditions
8125 do n = 1, 2*ndim
8126 idim = (n+1)/2
8127 select case (typeboundary(mag(idim), n))
8128 case (bc_symm)
8129 ! d/dx B = 0, take phi = 0
8130 mg%bc(n, mg_iphi)%bc_type = mg_bc_dirichlet
8131 mg%bc(n, mg_iphi)%bc_value = 0.0_dp
8132 case (bc_asymm)
8133 ! B = 0, so grad(phi) = 0
8134 mg%bc(n, mg_iphi)%bc_type = mg_bc_neumann
8135 mg%bc(n, mg_iphi)%bc_value = 0.0_dp
8136 case (bc_cont)
8137 mg%bc(n, mg_iphi)%bc_type = mg_bc_dirichlet
8138 mg%bc(n, mg_iphi)%bc_value = 0.0_dp
8139 case (bc_special)
8140 ! Assume Dirichlet boundary conditions, derivative zero
8141 mg%bc(n, mg_iphi)%bc_type = mg_bc_dirichlet
8142 mg%bc(n, mg_iphi)%bc_value = 0.0_dp
8143 case (bc_periodic)
8144 ! Nothing to do here
8145 case default
8146 write(*,*) "mhd_clean_divb_multigrid warning: unknown boundary type"
8147 mg%bc(n, mg_iphi)%bc_type = mg_bc_dirichlet
8148 mg%bc(n, mg_iphi)%bc_value = 0.0_dp
8149 end select
8150 end do
8151
8152 ix^l=ixm^ll^ladd1;
8153 max_divb = 0.0d0
8154
8155 ! Store divergence of B as right-hand side
8156 do iigrid = 1, igridstail
8157 igrid = igrids(iigrid);
8158 pnode => igrid_to_node(igrid, mype)%node
8159 id = pnode%id
8160 lvl = mg%boxes(id)%lvl
8161 nc = mg%box_size_lvl(lvl)
8162
8163 ! Geometry subroutines expect this to be set
8164 block => ps(igrid)
8165 ^d&dxlevel(^d)=rnode(rpdx^d_,igrid);
8166
8167 call get_divb(ps(igrid)%w(ixg^t, 1:nw), ixg^ll, ixm^ll, tmp, &
8169 mg%boxes(id)%cc({1:nc}, mg_irhs) = tmp(ixm^t)
8170 max_divb = max(max_divb, maxval(abs(tmp(ixm^t))))
8171 end do
8172
8173 ! Solve laplacian(phi) = divB
8174 if(stagger_grid) then
8175 call mpi_allreduce(mpi_in_place, max_divb, 1, mpi_double_precision, &
8176 mpi_max, icomm, ierrmpi)
8177
8178 if (mype == 0) print *, "Performing multigrid divB cleaning"
8179 if (mype == 0) print *, "iteration vs residual"
8180 ! Solve laplacian(phi) = divB
8181 do n = 1, max_its
8182 call mg_fas_fmg(mg, n>1, max_res=residual_it(n))
8183 if (mype == 0) write(*, "(I4,E11.3)") n, residual_it(n)
8184 if (residual_it(n) < residual_reduction * max_divb) exit
8185 end do
8186 if (mype == 0 .and. n > max_its) then
8187 print *, "divb_multigrid warning: not fully converged"
8188 print *, "current amplitude of divb: ", residual_it(max_its)
8189 print *, "multigrid smallest grid: ", &
8190 mg%domain_size_lvl(:, mg%lowest_lvl)
8191 print *, "note: smallest grid ideally has <= 8 cells"
8192 print *, "multigrid dx/dy/dz ratio: ", mg%dr(:, 1)/mg%dr(1, 1)
8193 print *, "note: dx/dy/dz should be similar"
8194 end if
8195 else
8196 do n = 1, max_its
8197 call mg_fas_vcycle(mg, max_res=res)
8198 if (res < max_residual) exit
8199 end do
8200 if (res > max_residual) call mpistop("divb_multigrid: no convergence")
8201 end if
8202
8203
8204 ! Correct the magnetic field
8205 do iigrid = 1, igridstail
8206 igrid = igrids(iigrid);
8207 pnode => igrid_to_node(igrid, mype)%node
8208 id = pnode%id
8209
8210 ! Geometry subroutines expect this to be set
8211 block => ps(igrid)
8212 ^d&dxlevel(^d)=rnode(rpdx^d_,igrid);
8213
8214 ! Compute the gradient of phi
8215 tmp(ix^s) = mg%boxes(id)%cc({:,}, mg_iphi)
8216
8217 if(stagger_grid) then
8218 do idim =1, ndim
8219 ixcmin^d=ixmlo^d-kr(idim,^d);
8220 ixcmax^d=ixmhi^d;
8221 call gradientf(tmp,ps(igrid)%x,ixg^ll,ixc^l,idim,grad(ixg^t,idim))
8222 ! Apply the correction B* = B - gradient(phi)
8223 ps(igrid)%ws(ixc^s,idim)=ps(igrid)%ws(ixc^s,idim)-grad(ixc^s,idim)
8224 end do
8225 ! store cell-center magnetic energy
8226 tmp(ixm^t) = sum(ps(igrid)%w(ixm^t, mag(1:ndim))**2, dim=ndim+1)
8227 ! change cell-center magnetic field
8228 call mhd_face_to_center(ixm^ll,ps(igrid))
8229 else
8230 do idim = 1, ndim
8231 call gradient(tmp,ixg^ll,ixm^ll,idim,grad(ixg^t, idim))
8232 end do
8233 ! store cell-center magnetic energy
8234 tmp(ixm^t) = sum(ps(igrid)%w(ixm^t, mag(1:ndim))**2, dim=ndim+1)
8235 ! Apply the correction B* = B - gradient(phi)
8236 ps(igrid)%w(ixm^t, mag(1:ndim)) = &
8237 ps(igrid)%w(ixm^t, mag(1:ndim)) - grad(ixm^t, :)
8238 end if
8239
8240 if(total_energy) then
8241 ! Determine magnetic energy difference
8242 tmp(ixm^t) = 0.5_dp * (sum(ps(igrid)%w(ixm^t, &
8243 mag(1:ndim))**2, dim=ndim+1) - tmp(ixm^t))
8244 ! Keep thermal pressure the same
8245 ps(igrid)%w(ixm^t, e_) = ps(igrid)%w(ixm^t, e_) + tmp(ixm^t)
8246 end if
8247 end do
8248
8249 active = .true.
8250
8251 end subroutine mhd_clean_divb_multigrid
8252 }
8253
8254 !> get electric field through averaging neighors to update faces in CT
8255 subroutine mhd_update_faces_average(ixI^L,ixO^L,qt,qdt,wp,fC,fE,sCT,s,vcts)
8257 use mod_usr_methods
8258
8259 integer, intent(in) :: ixi^l, ixo^l
8260 double precision, intent(in) :: qt,qdt
8261 ! cell-center primitive variables
8262 double precision, intent(in) :: wp(ixi^s,1:nw)
8263 type(state) :: sct, s
8264 type(ct_velocity) :: vcts
8265 double precision, intent(in) :: fc(ixi^s,1:nwflux,1:ndim)
8266 double precision, intent(inout) :: fe(ixi^s,sdim:3)
8267
8268 double precision :: circ(ixi^s,1:ndim)
8269 ! non-ideal electric field on cell edges
8270 double precision, dimension(ixI^S,sdim:3) :: e_resi, e_ambi
8271 integer :: ix^d,ixc^l,ixa^l,i1kr^d,i2kr^d
8272 integer :: idim1,idim2,idir,iwdim1,iwdim2
8273
8274 associate(bfaces=>s%ws,x=>s%x)
8275
8276 ! Calculate contribution to FEM of each edge,
8277 ! that is, estimate value of line integral of
8278 ! electric field in the positive idir direction.
8279
8280 ! if there is resistivity, get eta J
8281 if(mhd_eta/=zero) call get_resistive_electric_field(ixi^l,ixo^l,wp,sct,s,e_resi)
8282
8283 ! if there is ambipolar diffusion, get E_ambi
8284 if(mhd_ambipolar_exp) call get_ambipolar_electric_field(ixi^l,ixo^l,sct%w,x,e_ambi)
8285
8286 do idim1=1,ndim
8287 iwdim1 = mag(idim1)
8288 i1kr^d=kr(idim1,^d);
8289 do idim2=1,ndim
8290 iwdim2 = mag(idim2)
8291 i2kr^d=kr(idim2,^d);
8292 do idir=sdim,3! Direction of line integral
8293 ! Allow only even permutations
8294 if (lvc(idim1,idim2,idir)==1) then
8295 ixcmax^d=ixomax^d;
8296 ixcmin^d=ixomin^d+kr(idir,^d)-1;
8297 ! average cell-face electric field to cell edges
8298 {do ix^db=ixcmin^db,ixcmax^db\}
8299 fe(ix^d,idir)=quarter*&
8300 (fc(ix^d,iwdim1,idim2)+fc({ix^d+i1kr^d},iwdim1,idim2)&
8301 -fc(ix^d,iwdim2,idim1)-fc({ix^d+i2kr^d},iwdim2,idim1))
8302 ! add resistive electric field at cell edges E=-vxB+eta J
8303 if(mhd_eta/=zero) fe(ix^d,idir)=fe(ix^d,idir)+e_resi(ix^d,idir)
8304 ! add ambipolar electric field
8305 if(mhd_ambipolar_exp) fe(ix^d,idir)=fe(ix^d,idir)+e_ambi(ix^d,idir)
8306
8307 ! times time step and edge length
8308 fe(ix^d,idir)=fe(ix^d,idir)*qdt*s%dsC(ix^d,idir)
8309 {end do\}
8310 end if
8311 end do
8312 end do
8313 end do
8314
8315 ! allow user to change inductive electric field, especially for boundary driven applications
8316 if(associated(usr_set_electric_field)) &
8317 call usr_set_electric_field(ixi^l,ixo^l,qt,qdt,fe,sct)
8318
8319 circ(ixi^s,1:ndim)=zero
8320
8321 ! Calculate circulation on each face
8322 do idim1=1,ndim ! Coordinate perpendicular to face
8323 ixcmax^d=ixomax^d;
8324 ixcmin^d=ixomin^d-kr(idim1,^d);
8325 do idim2=1,ndim
8326 ixa^l=ixc^l-kr(idim2,^d);
8327 do idir=sdim,3 ! Direction of line integral
8328 ! Assemble indices
8329 if(lvc(idim1,idim2,idir)==1) then
8330 ! Add line integrals in direction idir
8331 circ(ixc^s,idim1)=circ(ixc^s,idim1)&
8332 +(fe(ixc^s,idir)&
8333 -fe(ixa^s,idir))
8334 else if(lvc(idim1,idim2,idir)==-1) then
8335 ! Add line integrals in direction idir
8336 circ(ixc^s,idim1)=circ(ixc^s,idim1)&
8337 -(fe(ixc^s,idir)&
8338 -fe(ixa^s,idir))
8339 end if
8340 end do
8341 end do
8342 {do ix^db=ixcmin^db,ixcmax^db\}
8343 ! Divide by the area of the face to get dB/dt
8344 if(s%surfaceC(ix^d,idim1) > smalldouble) then
8345 ! Time update cell-face magnetic field component
8346 bfaces(ix^d,idim1)=bfaces(ix^d,idim1)-circ(ix^d,idim1)/s%surfaceC(ix^d,idim1)
8347 end if
8348 {end do\}
8349 end do
8350
8351 end associate
8352
8353 end subroutine mhd_update_faces_average
8354
8355 !> update faces using UCT contact mode by Gardiner and Stone 2005 JCP 205, 509
8356 subroutine mhd_update_faces_contact(ixI^L,ixO^L,qt,qdt,wp,fC,fE,sCT,s,vcts)
8358 use mod_usr_methods
8359 use mod_geometry
8360
8361 integer, intent(in) :: ixi^l, ixo^l
8362 double precision, intent(in) :: qt, qdt
8363 ! cell-center primitive variables
8364 double precision, intent(in) :: wp(ixi^s,1:nw)
8365 type(state) :: sct, s
8366 type(ct_velocity) :: vcts
8367 double precision, intent(in) :: fc(ixi^s,1:nwflux,1:ndim)
8368 double precision, intent(inout) :: fe(ixi^s,sdim:3)
8369
8370 double precision :: circ(ixi^s,1:ndim)
8371 ! electric field at cell centers
8372 double precision :: ecc(ixi^s,sdim:3)
8373 double precision :: ein(ixi^s,sdim:3)
8374 ! gradient of E at left and right side of a cell face
8375 double precision :: el(ixi^s),er(ixi^s)
8376 ! gradient of E at left and right side of a cell corner
8377 double precision :: elc,erc
8378 ! non-ideal electric field on cell edges
8379 double precision, dimension(ixI^S,sdim:3) :: e_resi, e_ambi
8380 ! current on cell edges
8381 double precision :: jce(ixi^s,sdim:3)
8382 ! location at cell faces
8383 double precision :: xs(ixgs^t,1:ndim)
8384 double precision :: gradi(ixgs^t)
8385 double precision :: resis_heat
8386 integer :: ixc^l,ixa^l
8387 integer :: idim1,idim2,idir,iwdim1,iwdim2,ix^d,i1kr^d,i2kr^d
8388
8389 associate(bfaces=>s%ws,x=>s%x,w=>s%w,vnorm=>vcts%vnorm,wcts=>sct%ws)
8390
8391 ! if there is resistivity, get eta J
8392 if(mhd_eta/=zero) call get_resistive_electric_field(ixi^l,ixo^l,wp,sct,s,e_resi)
8393
8394 ! if there is ambipolar diffusion, get E_ambi
8395 if(mhd_ambipolar_exp) call get_ambipolar_electric_field(ixi^l,ixo^l,sct%w,x,e_ambi)
8396
8397 if(b0field) then
8398 {do ix^db=iximin^db,iximax^db\}
8399 ! Calculate electric field at cell centers
8400 {^ifthreed
8401 ecc(ix^d,1)=(wp(ix^d,b2_)+block%B0(ix^d,2,0))*wp(ix^d,m3_)-(wp(ix^d,b3_)+block%B0(ix^d,3,0))*wp(ix^d,m2_)
8402 ecc(ix^d,2)=(wp(ix^d,b3_)+block%B0(ix^d,3,0))*wp(ix^d,m1_)-(wp(ix^d,b1_)+block%B0(ix^d,1,0))*wp(ix^d,m3_)
8403 ecc(ix^d,3)=(wp(ix^d,b1_)+block%B0(ix^d,1,0))*wp(ix^d,m2_)-(wp(ix^d,b2_)+block%B0(ix^d,2,0))*wp(ix^d,m1_)
8404 }
8405 {^iftwod
8406 ecc(ix^d,3)=(wp(ix^d,b1_)+block%B0(ix^d,1,0))*wp(ix^d,m2_) &
8407 -(wp(ix^d,b2_)+block%B0(ix^d,2,0))*wp(ix^d,m1_)
8408 }
8409 {^ifoned
8410 ecc(ix^d,3)=0.d0
8411 }
8412 {end do\}
8413 else
8414 {do ix^db=iximin^db,iximax^db\}
8415 ! Calculate electric field at cell centers
8416 {^ifthreed
8417 ecc(ix^d,1)=wp(ix^d,b2_)*wp(ix^d,m3_)-wp(ix^d,b3_)*wp(ix^d,m2_)
8418 ecc(ix^d,2)=wp(ix^d,b3_)*wp(ix^d,m1_)-wp(ix^d,b1_)*wp(ix^d,m3_)
8419 ecc(ix^d,3)=wp(ix^d,b1_)*wp(ix^d,m2_)-wp(ix^d,b2_)*wp(ix^d,m1_)
8420 }
8421 {^iftwod
8422 ecc(ix^d,3)=wp(ix^d,b1_)*wp(ix^d,m2_)-wp(ix^d,b2_)*wp(ix^d,m1_)
8423 }
8424 {^ifoned
8425 ecc(ix^d,3)=0.d0
8426 }
8427 {end do\}
8428 end if
8429
8430 ! Calculate contribution to FEM of each edge,
8431 ! that is, estimate value of line integral of
8432 ! electric field in the positive idir direction.
8433 ! evaluate electric field along cell edges according to equation (41)
8434 do idim1=1,ndim
8435 iwdim1 = mag(idim1)
8436 i1kr^d=kr(idim1,^d);
8437 do idim2=1,ndim
8438 iwdim2 = mag(idim2)
8439 i2kr^d=kr(idim2,^d);
8440 do idir=sdim,3 ! Direction of line integral
8441 ! Allow only even permutations
8442 if (lvc(idim1,idim2,idir)==1) then
8443 ixcmax^d=ixomax^d;
8444 ixcmin^d=ixomin^d+kr(idir,^d)-1;
8445 ! Assemble indices
8446 ! average cell-face electric field to cell edges
8447 {do ix^db=ixcmin^db,ixcmax^db\}
8448 fe(ix^d,idir)=quarter*&
8449 (fc(ix^d,iwdim1,idim2)+fc({ix^d+i1kr^d},iwdim1,idim2)&
8450 -fc(ix^d,iwdim2,idim1)-fc({ix^d+i2kr^d},iwdim2,idim1))
8451 if(numerical_resistive_heating) ein(ix^d,idir)=fe(ix^d,idir)
8452 {end do\}
8453 ! add slope in idim2 direction from equation (50)
8454 ixamin^d=ixcmin^d;
8455 ixamax^d=ixcmax^d+i1kr^d;
8456 {do ix^db=ixamin^db,ixamax^db\}
8457 el(ix^d)=fc(ix^d,iwdim1,idim2)-ecc(ix^d,idir)
8458 er(ix^d)=fc(ix^d,iwdim1,idim2)-ecc({ix^d+i2kr^d},idir)
8459 {end do\}
8460 {!dir$ ivdep
8461 do ix^db=ixcmin^db,ixcmax^db\}
8462 if(vnorm(ix^d,idim1)>0.d0) then
8463 elc=el(ix^d)
8464 else if(vnorm(ix^d,idim1)<0.d0) then
8465 elc=el({ix^d+i1kr^d})
8466 else
8467 elc=0.5d0*(el(ix^d)+el({ix^d+i1kr^d}))
8468 end if
8469 if(vnorm({ix^d+i2kr^d},idim1)>0.d0) then
8470 erc=er(ix^d)
8471 else if(vnorm({ix^d+i2kr^d},idim1)<0.d0) then
8472 erc=er({ix^d+i1kr^d})
8473 else
8474 erc=0.5d0*(er(ix^d)+er({ix^d+i1kr^d}))
8475 end if
8476 fe(ix^d,idir)=fe(ix^d,idir)+0.25d0*(elc+erc)
8477 {end do\}
8478
8479 ! add slope in idim1 direction from equation (50)
8480 ixamin^d=ixcmin^d;
8481 ixamax^d=ixcmax^d+i2kr^d;
8482 {do ix^db=ixamin^db,ixamax^db\}
8483 el(ix^d)=-fc(ix^d,iwdim2,idim1)-ecc(ix^d,idir)
8484 er(ix^d)=-fc(ix^d,iwdim2,idim1)-ecc({ix^d+i1kr^d},idir)
8485 {end do\}
8486 {!dir$ ivdep
8487 do ix^db=ixcmin^db,ixcmax^db\}
8488 if(vnorm(ix^d,idim2)>0.d0) then
8489 elc=el(ix^d)
8490 else if(vnorm(ix^d,idim2)<0.d0) then
8491 elc=el({ix^d+i2kr^d})
8492 else
8493 elc=0.5d0*(el(ix^d)+el({ix^d+i2kr^d}))
8494 end if
8495 if(vnorm({ix^d+i1kr^d},idim2)>0.d0) then
8496 erc=er(ix^d)
8497 else if(vnorm({ix^d+i1kr^d},idim2)<0.d0) then
8498 erc=er({ix^d+i2kr^d})
8499 else
8500 erc=0.5d0*(er(ix^d)+er({ix^d+i2kr^d}))
8501 end if
8502 fe(ix^d,idir)=fe(ix^d,idir)+0.25d0*(elc+erc)
8503 ! difference between average and upwind interpolated E
8504 if(numerical_resistive_heating) ein(ix^d,idir)=fe(ix^d,idir)-ein(ix^d,idir)
8505 ! add resistive electric field at cell edges E=-vxB+eta J
8506 if(mhd_eta/=zero) fe(ix^d,idir)=fe(ix^d,idir)+e_resi(ix^d,idir)
8507 ! add ambipolar electric field
8508 if(mhd_ambipolar_exp) fe(ix^d,idir)=fe(ix^d,idir)+e_ambi(ix^d,idir)
8509
8510 ! times time step and edge length
8511 fe(ix^d,idir)=fe(ix^d,idir)*qdt*s%dsC(ix^d,idir)
8512 {end do\}
8513 end if
8514 end do
8515 end do
8516 end do
8517
8519 ! add upwind diffused magnetic energy back to energy
8520 ! calculate current density at cell edges
8521 jce=0.d0
8522 do idim1=1,ndim
8523 do idim2=1,ndim
8524 do idir=sdim,3
8525 if (lvc(idim1,idim2,idir)==0) cycle
8526 ixcmax^d=ixomax^d;
8527 ixcmin^d=ixomin^d+kr(idir,^d)-1;
8528 ixamax^d=ixcmax^d-kr(idir,^d)+1;
8529 ixamin^d=ixcmin^d;
8530 ! current at transverse faces
8531 xs(ixa^s,:)=x(ixa^s,:)
8532 xs(ixa^s,idim2)=x(ixa^s,idim2)+half*s%dx(ixa^s,idim2)
8533 call gradientf(wcts(ixgs^t,idim2),xs,ixgs^ll,ixc^l,idim1,gradi)
8534 if (lvc(idim1,idim2,idir)==1) then
8535 jce(ixc^s,idir)=jce(ixc^s,idir)+gradi(ixc^s)
8536 else
8537 jce(ixc^s,idir)=jce(ixc^s,idir)-gradi(ixc^s)
8538 end if
8539 end do
8540 end do
8541 end do
8542 {^ifthreed
8543 {do ix^db=ixomin^db,ixomax^db\}
8544 resis_heat=0.25d0*(ein(ix^d,1)*jce(ix^d,1)+ein(ix1,ix2-1,ix3,1)*jce(ix1,ix2-1,ix3,1)&
8545 +ein(ix1,ix2,ix3-1,1)*jce(ix1,ix2,ix3-1,1)+ein(ix1,ix2-1,ix3-1,1)*jce(ix1,ix2-1,ix3-1,1))&
8546 +0.25d0*(ein(ix^d,2)*jce(ix^d,2)+ein(ix1-1,ix2,ix3,2)*jce(ix1-1,ix2,ix3,2)&
8547 +ein(ix1,ix2,ix3-1,2)*jce(ix1,ix2,ix3-1,2)+ein(ix1-1,ix2,ix3-1,2)*jce(ix1-1,ix2,ix3-1,2))&
8548 +0.25d0*(ein(ix^d,3)*jce(ix^d,3)+ein(ix1-1,ix2,ix3,3)*jce(ix1-1,ix2,ix3,3)&
8549 +ein(ix1,ix2-1,ix3,3)*jce(ix1,ix2-1,ix3,3)+ein(ix1-1,ix2-1,ix3,3)*jce(ix1-1,ix2-1,ix3,3))
8550 if(resis_heat<0.d0) resis_heat=0.d0
8551 w(ix^d,e_)=w(ix^d,e_)+qdt*resis_heat
8552 !if(nwextra>0) block%w(ix^D,nw)=resis_heat
8553 {end do\}
8554 }
8555 {^iftwod
8556 {do ix^db=ixomin^db,ixomax^db\}
8557 resis_heat=0.25d0*(ein(ix^d,3)*jce(ix^d,3)+ein(ix1-1,ix2,3)*jce(ix1-1,ix2,3)&
8558 +ein(ix1,ix2-1,3)*jce(ix1,ix2-1,3)+ein(ix1-1,ix2-1,3)*jce(ix1-1,ix2-1,3))
8559 if(resis_heat<0.d0) resis_heat=0.d0
8560 w(ix^d,e_)=w(ix^d,e_)+qdt*resis_heat
8561 !if(nwextra>0) block%w(ix^D,nw)=resis_heat
8562 {end do\}
8563 }
8564 end if
8565
8566 ! allow user to change inductive electric field, especially for boundary driven applications
8567 if(associated(usr_set_electric_field)) &
8568 call usr_set_electric_field(ixi^l,ixo^l,qt,qdt,fe,sct)
8569
8570 circ(ixi^s,1:ndim)=zero
8571
8572 ! Calculate circulation on each face
8573 do idim1=1,ndim ! Coordinate perpendicular to face
8574 ixcmax^d=ixomax^d;
8575 ixcmin^d=ixomin^d-kr(idim1,^d);
8576 do idim2=1,ndim
8577 ixa^l=ixc^l-kr(idim2,^d);
8578 do idir=sdim,3 ! Direction of line integral
8579 ! Assemble indices
8580 if(lvc(idim1,idim2,idir)==1) then
8581 ! Add line integrals in direction idir
8582 circ(ixc^s,idim1)=circ(ixc^s,idim1)&
8583 +(fe(ixc^s,idir)&
8584 -fe(ixa^s,idir))
8585 else if(lvc(idim1,idim2,idir)==-1) then
8586 ! Add line integrals in direction idir
8587 circ(ixc^s,idim1)=circ(ixc^s,idim1)&
8588 -(fe(ixc^s,idir)&
8589 -fe(ixa^s,idir))
8590 end if
8591 end do
8592 end do
8593 {do ix^db=ixcmin^db,ixcmax^db\}
8594 ! Divide by the area of the face to get dB/dt
8595 if(s%surfaceC(ix^d,idim1) > smalldouble) then
8596 ! Time update cell-face magnetic field component
8597 bfaces(ix^d,idim1)=bfaces(ix^d,idim1)-circ(ix^d,idim1)/s%surfaceC(ix^d,idim1)
8598 end if
8599 {end do\}
8600 end do
8601
8602 end associate
8603
8604 end subroutine mhd_update_faces_contact
8605
8606 !> update faces
8607 subroutine mhd_update_faces_hll(ixI^L,ixO^L,qt,qdt,wp,fC,fE,sCT,s,vcts)
8609 use mod_usr_methods
8611
8612 integer, intent(in) :: ixi^l, ixo^l
8613 double precision, intent(in) :: qt, qdt
8614 ! cell-center primitive variables
8615 double precision, intent(in) :: wp(ixi^s,1:nw)
8616 type(state) :: sct, s
8617 type(ct_velocity) :: vcts
8618 double precision, intent(in) :: fc(ixi^s,1:nwflux,1:ndim)
8619 double precision, intent(inout) :: fe(ixi^s,sdim:3)
8620
8621 double precision :: vtill(ixi^s,2)
8622 double precision :: vtilr(ixi^s,2)
8623 double precision :: bfacetot(ixi^s,ndim)
8624 double precision :: btill(ixi^s,ndim)
8625 double precision :: btilr(ixi^s,ndim)
8626 double precision :: cp(ixi^s,2)
8627 double precision :: cm(ixi^s,2)
8628 double precision :: circ(ixi^s,1:ndim)
8629 ! non-ideal electric field on cell edges
8630 double precision, dimension(ixI^S,sdim:3) :: e_resi, e_ambi
8631 integer :: hxc^l,ixc^l,ixcp^l,jxc^l,ixcm^l
8632 integer :: idim1,idim2,idir,ix^d
8633
8634 associate(bfaces=>s%ws,bfacesct=>sct%ws,x=>s%x,vbarc=>vcts%vbarC,cbarmin=>vcts%cbarmin,&
8635 cbarmax=>vcts%cbarmax)
8636
8637 ! Calculate contribution to FEM of each edge,
8638 ! that is, estimate value of line integral of
8639 ! electric field in the positive idir direction.
8640
8641 ! Loop over components of electric field
8642
8643 ! idir: electric field component we need to calculate
8644 ! idim1: directions in which we already performed the reconstruction
8645 ! idim2: directions in which we perform the reconstruction
8646
8647 ! if there is resistivity, get eta J
8648 if(mhd_eta/=zero) call get_resistive_electric_field(ixi^l,ixo^l,wp,sct,s,e_resi)
8649
8650 ! if there is ambipolar diffusion, get E_ambi
8651 if(mhd_ambipolar_exp) call get_ambipolar_electric_field(ixi^l,ixo^l,sct%w,x,e_ambi)
8652
8653 do idir=sdim,3
8654 ! Indices
8655 ! idir: electric field component
8656 ! idim1: one surface
8657 ! idim2: the other surface
8658 ! cyclic permutation: idim1,idim2,idir=1,2,3
8659 ! Velocity components on the surface
8660 ! follow cyclic premutations:
8661 ! Sx(1),Sx(2)=y,z ; Sy(1),Sy(2)=z,x ; Sz(1),Sz(2)=x,y
8662
8663 ixcmax^d=ixomax^d;
8664 ixcmin^d=ixomin^d-1+kr(idir,^d);
8665
8666 ! Set indices and directions
8667 idim1=mod(idir,3)+1
8668 idim2=mod(idir+1,3)+1
8669
8670 jxc^l=ixc^l+kr(idim1,^d);
8671 ixcp^l=ixc^l+kr(idim2,^d);
8672
8673 ! Reconstruct transverse transport velocities
8674 call reconstruct(ixi^l,ixc^l,idim2,vbarc(ixi^s,idim1,1),&
8675 vtill(ixi^s,2),vtilr(ixi^s,2))
8676
8677 call reconstruct(ixi^l,ixc^l,idim1,vbarc(ixi^s,idim2,2),&
8678 vtill(ixi^s,1),vtilr(ixi^s,1))
8679
8680 ! Reconstruct magnetic fields
8681 ! Eventhough the arrays are larger, reconstruct works with
8682 ! the limits ixG.
8683 if(b0field) then
8684 bfacetot(ixi^s,idim1)=bfacesct(ixi^s,idim1)+block%B0(ixi^s,idim1,idim1)
8685 bfacetot(ixi^s,idim2)=bfacesct(ixi^s,idim2)+block%B0(ixi^s,idim2,idim2)
8686 else
8687 bfacetot(ixi^s,idim1)=bfacesct(ixi^s,idim1)
8688 bfacetot(ixi^s,idim2)=bfacesct(ixi^s,idim2)
8689 end if
8690 call reconstruct(ixi^l,ixc^l,idim2,bfacetot(ixi^s,idim1),&
8691 btill(ixi^s,idim1),btilr(ixi^s,idim1))
8692
8693 call reconstruct(ixi^l,ixc^l,idim1,bfacetot(ixi^s,idim2),&
8694 btill(ixi^s,idim2),btilr(ixi^s,idim2))
8695
8696 ! Take the maximum characteristic
8697
8698 cm(ixc^s,1)=max(cbarmin(ixcp^s,idim1),cbarmin(ixc^s,idim1))
8699 cp(ixc^s,1)=max(cbarmax(ixcp^s,idim1),cbarmax(ixc^s,idim1))
8700
8701 cm(ixc^s,2)=max(cbarmin(jxc^s,idim2),cbarmin(ixc^s,idim2))
8702 cp(ixc^s,2)=max(cbarmax(jxc^s,idim2),cbarmax(ixc^s,idim2))
8703
8704
8705 ! Calculate eletric field
8706 fe(ixc^s,idir)=-(cp(ixc^s,1)*vtill(ixc^s,1)*btill(ixc^s,idim2) &
8707 + cm(ixc^s,1)*vtilr(ixc^s,1)*btilr(ixc^s,idim2) &
8708 - cp(ixc^s,1)*cm(ixc^s,1)*(btilr(ixc^s,idim2)-btill(ixc^s,idim2)))&
8709 /(cp(ixc^s,1)+cm(ixc^s,1)) &
8710 +(cp(ixc^s,2)*vtill(ixc^s,2)*btill(ixc^s,idim1) &
8711 + cm(ixc^s,2)*vtilr(ixc^s,2)*btilr(ixc^s,idim1) &
8712 - cp(ixc^s,2)*cm(ixc^s,2)*(btilr(ixc^s,idim1)-btill(ixc^s,idim1)))&
8713 /(cp(ixc^s,2)+cm(ixc^s,2))
8714
8715 ! add resistive electric field at cell edges E=-vxB+eta J
8716 if(mhd_eta/=zero) fe(ixc^s,idir)=fe(ixc^s,idir)+e_resi(ixc^s,idir)
8717 ! add ambipolar electric field
8718 if(mhd_ambipolar_exp) fe(ixc^s,idir)=fe(ixc^s,idir)+e_ambi(ixc^s,idir)
8719
8720 fe(ixc^s,idir)=qdt*s%dsC(ixc^s,idir)*fe(ixc^s,idir)
8721
8722 if (.not.slab) then
8723 where(abs(x(ixc^s,r_)+half*dxlevel(r_)).lt.1.0d-9)
8724 fe(ixc^s,idir)=zero
8725 end where
8726 end if
8727
8728 end do
8729
8730 ! allow user to change inductive electric field, especially for boundary driven applications
8731 if(associated(usr_set_electric_field)) &
8732 call usr_set_electric_field(ixi^l,ixo^l,qt,qdt,fe,sct)
8733
8734 circ(ixi^s,1:ndim)=zero
8735
8736 ! Calculate circulation on each face: interal(fE dot dl)
8737 do idim1=1,ndim ! Coordinate perpendicular to face
8738 ixcmax^d=ixomax^d;
8739 ixcmin^d=ixomin^d-kr(idim1,^d);
8740 do idim2=1,ndim
8741 do idir=sdim,3 ! Direction of line integral
8742 ! Assemble indices
8743 if(lvc(idim1,idim2,idir)/=0) then
8744 hxc^l=ixc^l-kr(idim2,^d);
8745 ! Add line integrals in direction idir
8746 circ(ixc^s,idim1)=circ(ixc^s,idim1)&
8747 +lvc(idim1,idim2,idir)&
8748 *(fe(ixc^s,idir)&
8749 -fe(hxc^s,idir))
8750 end if
8751 end do
8752 end do
8753 {do ix^db=ixcmin^db,ixcmax^db\}
8754 ! Divide by the area of the face to get dB/dt
8755 if(s%surfaceC(ix^d,idim1) > smalldouble) then
8756 ! Time update cell-face magnetic field component
8757 bfaces(ix^d,idim1)=bfaces(ix^d,idim1)-circ(ix^d,idim1)/s%surfaceC(ix^d,idim1)
8758 end if
8759 {end do\}
8760 end do
8761
8762 end associate
8763 end subroutine mhd_update_faces_hll
8764
8765 !> calculate eta J at cell edges
8766 subroutine get_resistive_electric_field(ixI^L,ixO^L,wp,sCT,s,jce)
8768 use mod_usr_methods
8769 use mod_geometry
8770
8771 integer, intent(in) :: ixi^l, ixo^l
8772 ! cell-center primitive variables
8773 double precision, intent(in) :: wp(ixi^s,1:nw)
8774 type(state), intent(in) :: sct, s
8775 ! current on cell edges
8776 double precision :: jce(ixi^s,sdim:3)
8777
8778 ! current on cell centers
8779 double precision :: jcc(ixi^s,7-2*ndir:3)
8780 ! location at cell faces
8781 double precision :: xs(ixgs^t,1:ndim)
8782 ! resistivity
8783 double precision :: eta(ixi^s)
8784 double precision :: gradi(ixgs^t)
8785 integer :: ix^d,ixc^l,ixa^l,ixb^l,idir,idirmin,idim1,idim2
8786
8787 associate(x=>s%x,dx=>s%dx,w=>s%w,wct=>sct%w,wcts=>sct%ws)
8788 ! calculate current density at cell edges
8789 jce=0.d0
8790 do idim1=1,ndim
8791 do idim2=1,ndim
8792 do idir=sdim,3
8793 if (lvc(idim1,idim2,idir)==0) cycle
8794 ixcmax^d=ixomax^d;
8795 ixcmin^d=ixomin^d+kr(idir,^d)-1;
8796 ixbmax^d=ixcmax^d-kr(idir,^d)+1;
8797 ixbmin^d=ixcmin^d;
8798 ! current at transverse faces
8799 xs(ixb^s,:)=x(ixb^s,:)
8800 xs(ixb^s,idim2)=x(ixb^s,idim2)+half*dx(ixb^s,idim2)
8801 call gradientf(wcts(ixgs^t,idim2),xs,ixgs^ll,ixc^l,idim1,gradi,2)
8802 if (lvc(idim1,idim2,idir)==1) then
8803 jce(ixc^s,idir)=jce(ixc^s,idir)+gradi(ixc^s)
8804 else
8805 jce(ixc^s,idir)=jce(ixc^s,idir)-gradi(ixc^s)
8806 end if
8807 end do
8808 end do
8809 end do
8810 ! get resistivity
8811 if(mhd_eta>zero)then
8812 jce(ixi^s,:)=jce(ixi^s,:)*mhd_eta
8813 else
8814 ixa^l=ixo^l^ladd1;
8815 call get_current(wct,ixi^l,ixa^l,idirmin,jcc)
8816 call usr_special_resistivity(wp,ixi^l,ixa^l,idirmin,x,jcc,eta)
8817 ! calculate eta on cell edges
8818 do idir=sdim,3
8819 ixcmax^d=ixomax^d;
8820 ixcmin^d=ixomin^d+kr(idir,^d)-1;
8821 jcc(ixc^s,idir)=0.d0
8822 {do ix^db=0,1\}
8823 if({ ix^d==1 .and. ^d==idir | .or.}) cycle
8824 ixamin^d=ixcmin^d+ix^d;
8825 ixamax^d=ixcmax^d+ix^d;
8826 jcc(ixc^s,idir)=jcc(ixc^s,idir)+eta(ixa^s)
8827 {end do\}
8828 jcc(ixc^s,idir)=jcc(ixc^s,idir)*0.25d0
8829 jce(ixc^s,idir)=jce(ixc^s,idir)*jcc(ixc^s,idir)
8830 end do
8831 end if
8832
8833 end associate
8834 end subroutine get_resistive_electric_field
8835
8836 !> get ambipolar electric field on cell edges
8837 subroutine get_ambipolar_electric_field(ixI^L,ixO^L,w,x,fE)
8839
8840 integer, intent(in) :: ixi^l, ixo^l
8841 double precision, intent(in) :: w(ixi^s,1:nw)
8842 double precision, intent(in) :: x(ixi^s,1:ndim)
8843 double precision, intent(out) :: fe(ixi^s,sdim:3)
8844
8845 double precision :: jxbxb(ixi^s,1:3)
8846 integer :: idir,ixa^l,ixc^l,ix^d
8847
8848 ixa^l=ixo^l^ladd1;
8849 call mhd_get_jxbxb(w,x,ixi^l,ixa^l,jxbxb)
8850 ! calculate electric field on cell edges from cell centers
8851 do idir=sdim,3
8852 ! set ambipolar electric field in jxbxb: E=nuA * jxbxb, where nuA=-etaA/rho^2
8853 ! E_ambi(ixA^S,i) = -(mhd_eta_ambi/w(ixA^S, rho_)**2) * jxbxb(ixA^S,i)
8854 call multiplyambicoef(ixi^l,ixa^l,jxbxb(ixi^s,idir),w,x)
8855 ixcmax^d=ixomax^d;
8856 ixcmin^d=ixomin^d+kr(idir,^d)-1;
8857 fe(ixc^s,idir)=0.d0
8858 {do ix^db=0,1\}
8859 if({ ix^d==1 .and. ^d==idir | .or.}) cycle
8860 ixamin^d=ixcmin^d+ix^d;
8861 ixamax^d=ixcmax^d+ix^d;
8862 fe(ixc^s,idir)=fe(ixc^s,idir)+jxbxb(ixa^s,idir)
8863 {end do\}
8864 fe(ixc^s,idir)=fe(ixc^s,idir)*0.25d0
8865 end do
8866
8867 end subroutine get_ambipolar_electric_field
8868
8869 !> calculate cell-center values from face-center values
8870 subroutine mhd_face_to_center(ixO^L,s)
8872 ! Non-staggered interpolation range
8873 integer, intent(in) :: ixo^l
8874 type(state) :: s
8875
8876 integer :: ix^d
8877
8878 ! calculate cell-center values from face-center values in 2nd order
8879 ! because the staggered arrays have an additional place to the left.
8880 ! Interpolate to cell barycentre using arithmetic average
8881 ! This might be done better later, to make the method less diffusive.
8882 {!dir$ ivdep
8883 do ix^db=ixomin^db,ixomax^db\}
8884 {^ifthreed
8885 s%w(ix^d,b1_)=half/s%surface(ix^d,1)*(s%ws(ix^d,1)*s%surfaceC(ix^d,1)&
8886 +s%ws(ix1-1,ix2,ix3,1)*s%surfaceC(ix1-1,ix2,ix3,1))
8887 s%w(ix^d,b2_)=half/s%surface(ix^d,2)*(s%ws(ix^d,2)*s%surfaceC(ix^d,2)&
8888 +s%ws(ix1,ix2-1,ix3,2)*s%surfaceC(ix1,ix2-1,ix3,2))
8889 s%w(ix^d,b3_)=half/s%surface(ix^d,3)*(s%ws(ix^d,3)*s%surfaceC(ix^d,3)&
8890 +s%ws(ix1,ix2,ix3-1,3)*s%surfaceC(ix1,ix2,ix3-1,3))
8891 }
8892 {^iftwod
8893 s%w(ix^d,b1_)=half/s%surface(ix^d,1)*(s%ws(ix^d,1)*s%surfaceC(ix^d,1)&
8894 +s%ws(ix1-1,ix2,1)*s%surfaceC(ix1-1,ix2,1))
8895 s%w(ix^d,b2_)=half/s%surface(ix^d,2)*(s%ws(ix^d,2)*s%surfaceC(ix^d,2)&
8896 +s%ws(ix1,ix2-1,2)*s%surfaceC(ix1,ix2-1,2))
8897 }
8898 {end do\}
8899
8900 ! calculate cell-center values from face-center values in 4th order
8901 !do idim=1,ndim
8902 ! gxO^L=ixO^L-2*kr(idim,^D);
8903 ! hxO^L=ixO^L-kr(idim,^D);
8904 ! jxO^L=ixO^L+kr(idim,^D);
8905
8906 ! ! Interpolate to cell barycentre using fourth order central formula
8907 ! w(ixO^S,mag(idim))=(0.0625d0/s%surface(ixO^S,idim))*&
8908 ! ( -ws(gxO^S,idim)*s%surfaceC(gxO^S,idim) &
8909 ! +9.0d0*ws(hxO^S,idim)*s%surfaceC(hxO^S,idim) &
8910 ! +9.0d0*ws(ixO^S,idim)*s%surfaceC(ixO^S,idim) &
8911 ! -ws(jxO^S,idim)*s%surfaceC(jxO^S,idim) )
8912 !end do
8913
8914 ! calculate cell-center values from face-center values in 6th order
8915 !do idim=1,ndim
8916 ! fxO^L=ixO^L-3*kr(idim,^D);
8917 ! gxO^L=ixO^L-2*kr(idim,^D);
8918 ! hxO^L=ixO^L-kr(idim,^D);
8919 ! jxO^L=ixO^L+kr(idim,^D);
8920 ! kxO^L=ixO^L+2*kr(idim,^D);
8921
8922 ! ! Interpolate to cell barycentre using sixth order central formula
8923 ! w(ixO^S,mag(idim))=(0.00390625d0/s%surface(ixO^S,idim))* &
8924 ! ( +3.0d0*ws(fxO^S,idim)*s%surfaceC(fxO^S,idim) &
8925 ! -25.0d0*ws(gxO^S,idim)*s%surfaceC(gxO^S,idim) &
8926 ! +150.0d0*ws(hxO^S,idim)*s%surfaceC(hxO^S,idim) &
8927 ! +150.0d0*ws(ixO^S,idim)*s%surfaceC(ixO^S,idim) &
8928 ! -25.0d0*ws(jxO^S,idim)*s%surfaceC(jxO^S,idim) &
8929 ! +3.0d0*ws(kxO^S,idim)*s%surfaceC(kxO^S,idim) )
8930 !end do
8931
8932 end subroutine mhd_face_to_center
8933
8934 !> calculate magnetic field from vector potential
8935 subroutine b_from_vector_potential(ixIs^L, ixI^L, ixO^L, ws, x)
8938
8939 integer, intent(in) :: ixis^l, ixi^l, ixo^l
8940 double precision, intent(inout) :: ws(ixis^s,1:nws)
8941 double precision, intent(in) :: x(ixi^s,1:ndim)
8942
8943 double precision :: adummy(ixis^s,1:3)
8944
8945 call b_from_vector_potentiala(ixis^l, ixi^l, ixo^l, ws, x, adummy)
8946
8947 end subroutine b_from_vector_potential
8948
8949 subroutine get_hd_mom_flux(wC,w,x,ixI^L,ixO^L,idim,f)
8951
8952 integer, intent(in) :: ixi^l, ixo^l, idim
8953 ! conservative w
8954 double precision, intent(in) :: wc(ixi^s,nw)
8955 ! primitive w
8956 double precision, intent(in) :: w(ixi^s,nw)
8957 double precision, intent(in) :: x(ixi^s,1:ndim)
8958 double precision,intent(out) :: f(ixi^s,1:ndir)
8959
8960 integer :: ix^d
8961
8962 {do ix^db=ixomin^db,ixomax^db\}
8963 ! Get flux of momentum
8964 ! f_i[m_k]=v_i*m_k
8965 ^c&f(ix^d,^c)=wc(ix^d,mom(idim))*w(ix^d,mom(^c))\
8966 f(ix^d,idim)=f(ix^d,idim)+w(ix^d,p_)
8967 {end do\}
8968
8969 end subroutine get_hd_mom_flux
8970
8971 subroutine get_hd_mom_flux_noe(wC,w,x,ixI^L,ixO^L,idim,f)
8974
8975 integer, intent(in) :: ixi^l, ixo^l, idim
8976 ! conservative w
8977 double precision, intent(in) :: wc(ixi^s,nw)
8978 ! primitive w
8979 double precision, intent(in) :: w(ixi^s,nw)
8980 double precision, intent(in) :: x(ixi^s,1:ndim)
8981 double precision,intent(out) :: f(ixi^s,1:ndir)
8982
8983 double precision :: adiabs(ixi^s), gammas(ixi^s)
8984 integer :: ix^d
8985
8986 if(associated(usr_set_adiab)) then
8987 call usr_set_adiab(w,x,ixi^l,ixo^l,adiabs)
8988 else
8989 adiabs=mhd_adiab
8990 end if
8991 if(associated(usr_set_gamma)) then
8992 call usr_set_gamma(w,x,ixi^l,ixo^l,gammas)
8993 else
8994 gammas=eos%gamma
8995 end if
8996
8997 {do ix^db=ixomin^db,ixomax^db\}
8998 ! Get flux of momentum
8999 ! f_i[m_k]=v_i*m_k
9000 ^c&f(ix^d,^c)=wc(ix^d,mom(idim))*w(ix^d,mom(^c))\
9001 f(ix^d,idim)=f(ix^d,idim)+adiabs(ix^d)*w(ix^d,rho_)**gammas(ix^d)
9002 {end do\}
9003
9004 end subroutine get_hd_mom_flux_noe
9005
9006end module mod_mhd_phys
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.
double precision, parameter zero
some frequently used numbers
subroutine reconstruct(ixil, ixcl, idir, q, ql, qr)
Reconstruct scalar q within ixO^L to 1/2 dx in direction idir Return both left and right reconstructe...
subroutine b_from_vector_potentiala(ixisl, ixil, ixol, ws, x, a)
calculate magnetic field from vector potential A at cell edges
subroutine add_convert_method(phys_convert_vars, nwc, dataset_names, file_suffix)
Definition mod_convert.t:59
EoS state container – the single thermodynamic authority for AMRVAC.
integer, parameter, public eos_type_pi
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_eint_from_p(pres, nh_val, log_nh_val, rho_val)
LTE internal energy from pressure in one cell: FI bypass, else eint(nH, p).
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.
PI (partial-ionisation, eos_type='PI') arm of the eos% family.
Definition mod_eos_pi.t:27
subroutine, public eint_from_rho_p_pi(rho, p, eint)
Primitive pressure -> GAS internal energy (prim -> conserved direction): invert (rho,...
Definition mod_eos_pi.t:285
subroutine, public state_from_eint_pi(rho, eint, t, p, rfactor, iz_h, iz_he)
Conserved gas internal energy -> temperature, thermal pressure, R. Inverse of p_eint_from_rho_T_PI....
Definition mod_eos_pi.t:260
Equation of state for AMRVAC, handled through a single eos_container object.
Definition mod_eos.t:30
Module for flux conservation near refinement boundaries.
subroutine, public store_flux(igrid, fc, idimlim, nwfluxin)
subroutine, public store_edge(igrid, ixil, fe, idimlim)
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 basic grid data structures.
Definition mod_forest.t:2
type(tree_node_ptr), dimension(:,:), allocatable, save igrid_to_node
Array to go from an [igrid, ipe] index to a node pointer.
Definition mod_forest.t:35
subroutine, public get_divb(w, ixil, ixol, divb, nth_in)
Calculate div B within ixO.
integer, dimension(:), allocatable, public mag
Indices of the magnetic field.
Module with geometry-related routines (e.g., divergence, curl).
Definition mod_geometry.t:2
subroutine divvector(qvec, ixil, ixol, divq, nth_in)
integer coordinate
Definition mod_geometry.t:7
integer, parameter cartesian
Definition mod_geometry.t:8
subroutine laplacian_of_vector(qvec, ixil, ixol, lapl_qvec)
integer, parameter cylindrical
subroutine curlvector(qvec, ixil, ixol, curlvec, idirmin, idirmin0, ndir0, fourthorder)
Calculate curl of a vector qvec within ixL Options to employ standard second order CD evaluations use...
subroutine gradient(q, ixil, ixol, idir, gradq, nth_in)
subroutine gradientf(q, x, ixil, ixol, idir, gradq, nth_in, pm_in)
subroutine gradientl(q, ixil, ixol, idir, gradq)
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 dtdiffpar
For resistive MHD, the time step is also limited by the diffusion time: .
character(len=std_len) typegrad
double precision unit_charge
Physical scaling factor for charge.
integer ixghi
Upper index of grid block arrays.
pure subroutine cross_product(ixil, ixol, a, b, axb)
Cross product of two vectors.
integer, dimension(3, 3, 3) lvc
Levi-Civita tensor.
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.
integer, dimension(:, :), allocatable typeboundary
Array indicating the type of boundary condition per variable and per physical boundary.
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 stagger_grid
True for using stagger grid.
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 icomm
The MPI communicator.
double precision bdip
amplitude of background dipolar, quadrupolar, octupolar, user's field
integer b0i
background magnetic field location indicator
integer mype
The rank of the current MPI task.
logical local_timestep
each cell has its own timestep or not
integer ndir
Number of spatial dimensions (components) for vector variables.
integer ixm
the mesh range of a physical block without ghost cells
integer ierrmpi
A global MPI error return code.
logical autoconvert
If true, already convert to output format during the run.
integer, dimension(:), allocatable flux_method
Which flux scheme of spatial discretization to use (per grid level).
double precision, dimension(:), allocatable, parameter d
logical slab
Cartesian geometry or not.
integer, parameter bc_periodic
integer, parameter bc_special
boundary condition types
double precision unit_magneticfield
Physical scaling factor for magnetic field.
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.
logical b0field
split magnetic field as background B0 field
double precision, dimension(:,:), allocatable rnode
Corner coordinates.
double precision unit_temperature
Physical scaling factor for temperature.
double precision unit_radflux
Physical scaling factor for radiation flux.
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.
integer, parameter sdim
starting dimension for electric field
logical phys_trac
Use TRAC for MHD or 1D HD.
logical need_global_cmax
need global maximal wave speed
logical convert
If true and restart_from_file is given, convert snapshots to other file formats.
logical fix_small_values
fix small values with average or replace methods
double precision, dimension(^nd) dxlevel
store unstretched cell size of current level
logical use_multigrid
Use multigrid (only available in 2D and 3D).
logical slab_uniform
uniform Cartesian geometry or not (stretched Cartesian)
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.
integer number_equi_vars
number of equilibrium set variables, besides the mag field
integer, parameter ixglo
Lower index of grid block arrays (always 1).
Module for including gravity in (magneto)hydrodynamics simulations.
Definition mod_gravity.t:2
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
module mod_magnetofriction.t Purpose: use magnetofrictional method to relax 3D magnetic field to forc...
subroutine magnetofriction_init()
Initialize the module.
Magneto-hydrodynamics module.
Definition mod_mhd_phys.t:2
subroutine, public mhd_get_trad(w, x, ixil, ixol, trad)
Calculates radiation temperature.
integer, public, protected c_
logical, public, protected mhd_gravity
Whether gravity is added.
integer, public, protected fip_
Index of the FIP passive scalar rho*fip in conserved form, fip in primitive form.
logical, public, protected mhd_internal_e
Whether internal energy is solved instead of total energy.
double precision, public, protected mhd_trac_delta
TRAC-7 (Johnston 2021, A&A 654 A2): target number of cells resolving the TR.
logical, public, protected mhd_glm_extended
Whether extended GLM-MHD is used with additional sources.
logical, public mhd_hyperbolic_tc_constant
character(len=std_len), public, protected type_ct
Method type of constrained transport.
integer, dimension(:), allocatable, public, protected mom
Indices of the momentum density.
subroutine, public mhd_clean_divb_multigrid(qdt, qt, active)
integer, public, protected qpar_
Index of the field-aligned heat flux q_parallel.
logical, public, protected mhd_radiative_cooling
Whether radiative cooling is added.
subroutine, public mhd_e_to_ei(ixil, ixol, w, x)
Transform total energy to internal energy.
double precision, public mhd_adiab
The adiabatic constant.
double precision, public divbdiff
Coefficient of diffusive divB cleaning.
double precision, public mhd_eta_hyper
The MHD hyper-resistivity.
character(len=std_len), public, protected mhd_uawsom_reflection_mode
Algebra used by the Alfven-wave reflection source. one_dimensional_gradient retains the original one-...
double precision, public, protected mhd_hyperbolic_tc_bmin
Field-strength transition scale for perpendicular closure.
double precision, public, protected rr
double precision, public, protected h_ion_fr
Ionization fraction of H H_ion_fr = H+/(H+ + H).
double precision, public, protected mhd_uawsom_sigma
Dimensionless multiplier in the one_dimensional_gradient source only.
integer, public, protected mhd_trac_finegrid
Distance between two adjacent traced magnetic field lines (in finest cell size).
integer, public, protected wkminus_
double precision, public, protected mhd_uawsom_thread_radius0
double precision function, public mhd_semirelati_eint(p, rho)
Internal energy from thermal pressure in one cell, the inverse of mhd_semirelati_p.
subroutine, public get_normalized_divb(w, ixil, ixol, divb)
get dimensionless div B = |divB| * volume / area / |B|
integer, public, protected mhd_uawsom_height_dim
Enable conservative kink-wave reflection from the kink-speed gradient Cartesian direction used for th...
logical, public numerical_resistive_heating
Whether numerical resistive heating is included when solving partial energy equation.
type(tc_fluid), allocatable, public tc_fl
type of fluid for thermal conduction
logical, public, protected mhd_hd_mom_conserve_fix
exclude Maxwell stress flux from momentum flux (use hydrodynamic momentum flux) for conservation fix ...
logical, public, protected mhd_rotating_frame
Whether rotating frame is activated.
logical, public, protected mhd_semirelativistic
Whether semirelativistic MHD equations (Gombosi 2002 JCP) are solved.
integer, public, protected mhd_divb_nth
Whether divB is computed with a fourth order approximation.
integer, public, protected mhd_n_tracer
Number of tracer species.
integer, public, protected te_
Indices of temperature.
integer, public, protected m
integer, public equi_rho0_
equi vars indices in the stateequi_vars array
integer, public, protected mhd_trac_type
Which TRAC method is used.
logical, public, protected mhd_cak_force
Whether plasma is partially ionized Whether CAK radiation line force is activated.
logical, public, protected source_split_divb
Whether divB cleaning sources are added splitting from fluid solver.
double precision, public, protected mhd_uawsom_zeta0
Base transverse density contrast and filling factor.
integer, public, protected ne_
Index of the electron number density for LTE module.
logical, public, protected mhd_hall
Whether Hall-MHD is used.
type(te_fluid), allocatable, public te_fl_mhd
type of fluid for thermal emission synthesis
logical, public, protected mhd_ambipolar
Whether Ambipolar term is used.
logical, public, protected mhd_hyperbolic_tc
Whether thermal conduction is used.
logical, public, protected mhd_hyperbolic_tc_sat
Whether saturation is considered for hyperbolic TC. When the perpendicular channel is active,...
double precision, public, protected mhd_hyperbolic_tc_kappa_perp_factor
Relative perpendicular hyperbolic-TC coefficient in fixed/strong-field limit: kappa_perp0 = mhd_hyper...
logical, public has_equi_rho_and_p
whether split off equilibrium density and pressure
double precision, public mhd_glm_alpha
GLM-MHD parameter: ratio of the diffusive and advective time scales for div b taking values within [0...
double precision function, dimension(ixo^s), public mhd_mag_en_all(w, ixil, ixol)
Compute 2 times total magnetic energy.
pure double precision function, public mhd_uawsom_wave_energy_cell(wcell)
logical, public, protected mhd_radiation_fld
Whether radiation-gas interaction is handled using flux limited diffusion.
subroutine, public multiplyambicoef(ixil, ixol, res, w, x)
multiply res by the ambipolar coefficient The ambipolar coefficient is calculated as -mhd_eta_ambi/rh...
subroutine, public b_from_vector_potential(ixisl, ixil, ixol, ws, x)
calculate magnetic field from vector potential
double precision, public, protected he_ion_fr
Ionization fraction of He He_ion_fr = (He2+ + He+)/(He2+ + He+ + He).
double precision, public, protected mhd_squared_c
squared reduced speed of light c
integer, public, protected waminus_
logical, public, protected mhd_viscosity
Whether viscosity is added.
subroutine, public mhd_get_pradiation_from_prim(w, x, ixil, ixol, prad)
Calculate radiation pressure within ixO^L.
double precision, public, protected mhd_reduced_c
Reduced speed of light for semirelativistic MHD: 2% of light speed.
logical, public, protected mhd_energy
Whether an energy equation is used.
logical, public, protected mhd_ambipolar_exp
Whether Ambipolar term is implemented explicitly.
logical, public, protected mhd_radiative_transfer
Whether the short-characteristics radiative transfer is used, with its settings in &rt_list.
double precision, public mhd_hyperbolic_tc_kappa
The thermal conductivity kappa in hyperbolic thermal conduction.
logical, public, protected mhd_glm
Whether GLM-MHD is used to control div B.
double precision, public, protected mhd_uawsom_zeta_scale
Physical input scales (lengths and magnetic field); converted to code units at init.
type(fld_fluid), allocatable, public fld_fl
Radiation fluid object (gas-EoS callbacks for FLD), wired in mhd_link_eos.
logical, public clean_initial_divb
clean initial divB
double precision, public mhd_eta
The MHD resistivity.
logical, public divbwave
Add divB wave in Roe solver.
logical, public, protected mhd_uawsom_reflection
Enable conservative Alfvén reflection (one-dimensional or Cartesian gradient-vorticity).
logical, public, protected mhd_magnetofriction
Whether magnetofriction is added.
double precision, public, protected mhd_trac_mask
Height of the mask used in the TRAC method.
subroutine, public mhd_semirelati_vel_emech(w, ixil, ixol, vel, emech)
Velocity from the semirelativistic momentum (Gombosi et al. 2002 eqs 15-16) and emech,...
procedure(mask_subroutine), pointer, public usr_mask_ambipolar
logical, public, protected mhd_semirelati_curl_e
Keep E x curl(E) in the semirelativistic source: eq (26) of Gombosi et al. 2002 keeps it,...
elemental pure double precision function, public mhd_uawsom_rho2_factor_cell(zeta)
character(len=std_len), public, protected typedivbfix
Method type to clean divergence of B.
logical, public, protected mhd_uawsom_kink_reflection
double precision, public, protected mhd_uawsom_filling_factor
integer, public, protected waplus_
Conserved wave-energy indices. The plus variables propagate against B.
subroutine, public mhd_uawsom_rho2_factor(ixil, ixol, w, x, factor)
double precision, public, protected mhd_uawsom_alfven_corr_length0
logical, public, protected mhd_thermal_conduction
Whether thermal conduction is used.
integer, public equi_pe0_
subroutine, public mhd_get_csrad2_prim(w, x, ixil, ixol, csound)
Calculate modified squared fast wave speed for FLD NOTE: w is primitive on entry here!...
integer, public, protected qperp_
Index of the perpendicular heat flux q_perp.
integer, public, protected p_
Index of the gas pressure (-1 if not present) should equal e_.
integer, public, protected c
Indices of the momentum density for the form of better vectorization.
character(len=std_len), public, protected mhd_hyperbolic_tc_perp_mode
Perpendicular hyperbolic-TC closure mode: Enable/disable with mhd_hyperbolic_tc_use_perp,...
double precision, public, protected he_ion_fr2
Ratio of number He2+ / number He+ + He2+ He_ion_fr2 = He2+/(He2+ + He+).
pure double precision function, public mhd_uawsom_wave_pressure_cell(wcell, zeta)
logical, public, protected mhd_dump_full_vars
whether dump full variables (when splitting is used) in a separate dat file
logical, public, protected mhd_particles
Whether particles module is added.
integer, public, protected b
subroutine, public mhd_face_to_center(ixol, s)
calculate cell-center values from face-center values
logical, dimension(2 *^nd), public, protected boundary_divbfix
To control divB=0 fix for boundary.
subroutine, public get_current(w, ixil, ixol, idirmin, current)
Calculate idirmin and the idirmin:3 components of the common current array make sure that dxlevel(^D)...
double precision, public mhd_etah
Hall resistivity.
subroutine, public mhd_get_v(w, x, ixil, ixol, v)
Calculate v vector.
double precision, public mhd_eta_ambi
The MHD ambipolar coefficient.
logical, public, protected mhd_fip
Whether FIP passive scalar is enabled.
logical, public, protected mhd_hydrodynamic_e
Whether hydrodynamic energy is solved instead of total energy.
integer, public, protected r_e
Index of the radiation energy.
subroutine, public mhd_phys_init()
logical, public, protected mhd_trac
Whether TRAC method is used.
subroutine, public mhd_get_csrad2(w, x, ixil, ixol, csound)
Calculate modified squared sound speed for FLD NOTE: only for diagnostic purposes,...
subroutine, public mhd_get_pthermal_plus_pradiation(w, x, ixil, ixol, pth_plus_prad)
Calculates the sum of the gas pressure and the max Prad tensor element.
type(rc_fluid), allocatable, public rc_fl
type of fluid for radiative cooling
integer, public, protected wkplus_
integer, dimension(:), allocatable, public, protected tracer
Indices of the tracers.
double precision, public, protected mhd_hyperbolic_tc_coulomb_log
Constant Coulomb logarithm used by the simplified electron-magnetization closure. It is a namelist pa...
integer, public, protected rho_
Index of the density (in the w array).
double precision, public, protected mhd_uawsom_bref
logical, public, protected b0field_forcefree
B0 field is force-free.
integer, dimension(2 *^nd), public, protected boundary_divbfix_skip
To skip * layer of ghost cells during divB=0 fix for boundary.
integer, public, protected tweight_
logical, public, protected mhd_ambipolar_sts
Whether Ambipolar term is implemented using supertimestepping.
logical, public, protected mhd_hyperbolic_tc_use_perp
Whether the perpendicular hyperbolic-TC channel is enabled.
subroutine, public mhd_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).
double precision function, public mhd_semirelati_p(eint, rho)
Thermal pressure from internal energy in one cell for the active EoS.
integer, public, protected tcoff_
Index of the cutoff temperature for the TRAC method.
subroutine, public mhd_get_rho(w, x, ixil, ixol, rho)
logical, public, protected mhd_uawsom
Enable the Uniturbulence and Alfven Wave Solar Model extension.
logical, public, protected mhd_fld_pradtensor
integer, public, protected psi_
Indices of the GLM psi.
logical, public mhd_equi_thermal
Module to couple the octree-mg library to AMRVAC. This file uses the VACPP preprocessor,...
type(mg_t) mg
Data structure containing the multigrid tree.
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 for including rotating frame in (magneto)hydrodynamics simulations The rotation vector is assu...
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_...
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, 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_get_mhd_params(fl, read_mhd_params)
Init TC coefficients: MHD case.
subroutine get_euv_image(qunit, fl)
subroutine get_sxr_image(qunit, fl)
subroutine get_euv_spectrum(qunit, fl)
subroutine get_whitelight_image(qunit, fl)
Module with all the methods that users can customize in AMRVAC.
procedure(rfactor), pointer usr_rfactor
procedure(special_resistivity), pointer usr_special_resistivity
procedure(set_adiab), pointer usr_set_adiab
procedure(set_adiab), pointer usr_set_gamma
procedure(sub_get_heating), pointer usr_get_heating
procedure(phys_gravity), pointer usr_gravity
procedure(uawsom_coefficients), pointer usr_uawsom_coefficients
Optionally provide local UAWSoM closure coefficients. The returned thread radius and Alfven correlati...
procedure(set_equi_vars), pointer usr_set_equi_vars
procedure(set_electric_field), pointer usr_set_electric_field
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
The data structure that contains information about a tree node/grid block.
Definition mod_forest.t:14