Path: blob/main/src/callbacks_step/stepsize_dg1d.jl
5586 views
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs).1# Since these FMAs can increase the performance of many numerical algorithms,2# we need to opt-in explicitly.3# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details.4@muladd begin5#! format: noindent67function max_dt(u, t, mesh::TreeMesh{1},8constant_speed::False, equations,9dg::DG, cache)10# Avoid division by zero if the speed vanishes everywhere11max_scaled_speed = nextfloat(zero(t))1213@batch reduction=(max, max_scaled_speed) for element in eachelement(dg, cache)14max_lambda1 = zero(max_scaled_speed)15for i in eachnode(dg)16u_node = get_node_vars(u, equations, dg, i, element)17lambda1, = max_abs_speeds(u_node, equations)18max_lambda1 = Base.max(max_lambda1, lambda1)19end20inv_jacobian = cache.elements.inverse_jacobian[element] # 2 / Δx21# Use `Base.max` to prevent silent failures, as `max` from `@fastmath` doesn't propagate22# `NaN`s properly. See https://github.com/trixi-framework/Trixi.jl/pull/2445#discussion_r233681232323max_scaled_speed = Base.max(max_scaled_speed, inv_jacobian * max_lambda1)24end2526# Factor 2 cancels with 2 from `inv_jacobian`, resulting in Δx27return 2 / (nnodes(dg) * max_scaled_speed)28end2930function max_dt(u, t, mesh::TreeMesh{1},31constant_diffusivity::False, equations,32equations_parabolic::AbstractEquationsParabolic,33dg::DG, cache)34# Avoid division by zero if the diffusivity vanishes everywhere35max_scaled_diffusivity = nextfloat(zero(t))3637@batch reduction=(max, max_scaled_diffusivity) for element in eachelement(dg, cache)38max_diffusivity_ = zero(max_scaled_diffusivity)39for i in eachnode(dg)40u_node = get_node_vars(u, equations, dg, i, element)41diffusivity = max_diffusivity(u_node, equations_parabolic)42max_diffusivity_ = Base.max(max_diffusivity_, diffusivity)43end44inv_jacobian = cache.elements.inverse_jacobian[element] # 2 / Δx45# Use `Base.max` to prevent silent failures, as `max` from `@fastmath` doesn't propagate46# `NaN`s properly. See https://github.com/trixi-framework/Trixi.jl/pull/2445#discussion_r233681232347max_scaled_diffusivity = Base.max(max_scaled_diffusivity,48inv_jacobian^2 * max_diffusivity_)49end5051# Factor 4 cancels with 2^2 from `inv_jacobian^2`, resulting in Δx^252return 4 / (nnodes(dg) * max_scaled_diffusivity)53end5455function max_dt(u, t, mesh::TreeMesh{1},56constant_speed::True, equations,57dg::DG, cache)58# Avoid division by zero if the speed vanishes everywhere,59# e.g. for steady-state linear advection60max_scaled_speed = nextfloat(zero(t))6162max_lambda1, = max_abs_speeds(equations)6364@batch reduction=(max, max_scaled_speed) for element in eachelement(dg, cache)65inv_jacobian = cache.elements.inverse_jacobian[element] # 2 / Δx66# Use `Base.max` to prevent silent failures, as `max` from `@fastmath` doesn't propagate67# `NaN`s properly. See https://github.com/trixi-framework/Trixi.jl/pull/2445#discussion_r233681232368max_scaled_speed = Base.max(max_scaled_speed, inv_jacobian * max_lambda1)69end7071# Factor 2 cancels with 2 from `inv_jacobian`, resulting in Δx72return 2 / (nnodes(dg) * max_scaled_speed)73end7475function max_dt(u, t, mesh::TreeMesh, # for all dimensions76constant_diffusivity::True, equations,77equations_parabolic::AbstractEquationsParabolic,78dg::DG, cache)79# Avoid division by zero if the diffusivity vanishes everywhere80max_scaled_diffusivity = nextfloat(zero(t))8182diffusivity = max_diffusivity(equations_parabolic)8384@batch reduction=(max, max_scaled_diffusivity) for element in eachelement(dg, cache)85inv_jacobian = cache.elements.inverse_jacobian[element] # 2 / Δx86# Note: For the currently supported parabolic equations87# Diffusion & Navier-Stokes, we only have one isotropic diffusivity,88# so this is valid for 1D, 2D and 3D.89max_scaled_diffusivity = Base.max(max_scaled_diffusivity,90inv_jacobian^2 * diffusivity)91end9293# Factor 4 cancels with 2^2 from `inv_jacobian^2`, resulting in Δx^294return 4 / (nnodes(dg) * max_scaled_diffusivity)95end9697function max_dt(u, t, mesh::StructuredMesh{1},98constant_speed::False, equations,99dg::DG, cache)100# Avoid division by zero if the speed vanishes everywhere101max_scaled_speed = nextfloat(zero(t))102103@batch reduction=(max, max_scaled_speed) for element in eachelement(dg, cache)104max_lambda1 = zero(max_scaled_speed)105106for i in eachnode(dg)107u_node = get_node_vars(u, equations, dg, i, element)108lambda1, = max_abs_speeds(u_node, equations)109110inv_jacobian = cache.elements.inverse_jacobian[i, element] # 2 / Δx111112max_lambda1 = Base.max(max_lambda1, inv_jacobian * lambda1)113end114115# Use `Base.max` to prevent silent failures, as `max` from `@fastmath` doesn't propagate116# `NaN`s properly. See https://github.com/trixi-framework/Trixi.jl/pull/2445#discussion_r2336812323117max_scaled_speed = Base.max(max_scaled_speed, max_lambda1)118end119120# Factor 2 cancels with 2 from `inv_jacobian`, resulting in Δx121return 2 / (nnodes(dg) * max_scaled_speed)122end123124function max_dt(u, t, mesh::StructuredMesh{1},125constant_speed::True, equations,126dg::DG, cache)127# Avoid division by zero if the speed vanishes everywhere,128# e.g. for steady-state linear advection129max_scaled_speed = nextfloat(zero(t))130131max_lambda1, = max_abs_speeds(equations)132133@batch reduction=(max, max_scaled_speed) for element in eachelement(dg, cache)134for i in eachnode(dg)135inv_jacobian = cache.elements.inverse_jacobian[i, element] # 2 / Δx136# Use `Base.max` to prevent silent failures, as `max` from `@fastmath` doesn't propagate137# `NaN`s properly. See https://github.com/trixi-framework/Trixi.jl/pull/2445#discussion_r2336812323138max_scaled_speed = Base.max(max_scaled_speed, inv_jacobian * max_lambda1)139end140end141142# Factor 2 cancels with 2 from `inv_jacobian`, resulting in Δx143return 2 / (nnodes(dg) * max_scaled_speed)144end145146# Note: `max_dt` is not implemented for `StructuredMesh{1}` and `equations_parabolic` since147# for the `StructuredMesh` type there is no support of parabolic terms (yet), see the overview in the docs:148# https://trixi-framework.github.io/Trixi.jl/stable/overview/#overview-semidiscretizations149150end # @muladd151152153