Path: blob/main/ext/TrixiSparseConnectivityTracerExt.jl
5582 views
# Package extension for overloading of branching (if-clauses) base functions such as sqrt, log, etc.1module TrixiSparseConnectivityTracerExt23import Trixi4import SparseConnectivityTracer: AbstractTracer56# For the default package preference "sqrt_Trixi_NaN" we overload the `Base.sqrt` function7# to first check if the argument is < 0 and then return `NaN` instead of an error.8# To turn this behaviour off for the datatype `AbstractTracer` used in sparsity detection,9# we switch back to the Base implementation here which does not contain an if-clause.10Trixi.sqrt(x::AbstractTracer) = Base.sqrt(x)1112# if-clause free (i.e., non-optimized) implementations of some helper functions13# that compute specialized mean values used in advanced flux functions1415@inline function Trixi.ln_mean(x::AbstractTracer, y::AbstractTracer)16return (y - x) / log(y / x)17end1819@inline function Trixi.inv_ln_mean(x::AbstractTracer, y::AbstractTracer)20return log(y / x) / (y - x)21end2223@inline function Trixi.stolarsky_mean(x::AbstractTracer, y::AbstractTracer, gamma::Real)24yg = exp((gamma - 1) * log(y)) # equivalent to y^(gamma - 1) but faster for non-integers25xg = exp((gamma - 1) * log(x)) # equivalent to x^(gamma - 1) but faster for non-integers26return (gamma - 1) * (yg * y - xg * x) / (gamma * (yg - xg))27end2829end303132