Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/ext/TrixiSparseConnectivityTracerExt.jl
5582 views
1
# Package extension for overloading of branching (if-clauses) base functions such as sqrt, log, etc.
2
module TrixiSparseConnectivityTracerExt
3
4
import Trixi
5
import SparseConnectivityTracer: AbstractTracer
6
7
# For the default package preference "sqrt_Trixi_NaN" we overload the `Base.sqrt` function
8
# to first check if the argument is < 0 and then return `NaN` instead of an error.
9
# To turn this behaviour off for the datatype `AbstractTracer` used in sparsity detection,
10
# we switch back to the Base implementation here which does not contain an if-clause.
11
Trixi.sqrt(x::AbstractTracer) = Base.sqrt(x)
12
13
# if-clause free (i.e., non-optimized) implementations of some helper functions
14
# that compute specialized mean values used in advanced flux functions
15
16
@inline function Trixi.ln_mean(x::AbstractTracer, y::AbstractTracer)
17
return (y - x) / log(y / x)
18
end
19
20
@inline function Trixi.inv_ln_mean(x::AbstractTracer, y::AbstractTracer)
21
return log(y / x) / (y - x)
22
end
23
24
@inline function Trixi.stolarsky_mean(x::AbstractTracer, y::AbstractTracer, gamma::Real)
25
yg = exp((gamma - 1) * log(y)) # equivalent to y^(gamma - 1) but faster for non-integers
26
xg = exp((gamma - 1) * log(x)) # equivalent to x^(gamma - 1) but faster for non-integers
27
return (gamma - 1) * (yg * y - xg * x) / (gamma * (yg - xg))
28
end
29
30
end
31
32