Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Own sqrt and log returning NaN for "correct" multi-thread behaviour #1781

Merged
merged 75 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
562d79e
Introduce NaNMath for unsafe sqrt and log
DanielDoehring Dec 12, 2023
291b916
performance measurements
DanielDoehring Dec 12, 2023
0b68235
implement log myself
DanielDoehring Dec 12, 2023
dc839f7
Try out different log implementation
DanielDoehring Dec 13, 2023
9f3be79
remove NaNMath, own implementation
DanielDoehring Dec 18, 2023
f6de171
remove unrelated
DanielDoehring Dec 18, 2023
829c992
Update src/equations/compressible_euler_2d.jl
DanielDoehring Dec 18, 2023
7ee3742
Merge branch 'main' into NaNMath
DanielDoehring Dec 18, 2023
83e8b1e
NaNSqrt for quasi 1d CEE
DanielDoehring Dec 18, 2023
9931e04
fmt
DanielDoehring Dec 18, 2023
72fdb4a
Update src/auxiliary/math.jl
DanielDoehring Dec 18, 2023
ea9d394
Update src/auxiliary/math.jl
DanielDoehring Dec 18, 2023
8941e03
Update src/auxiliary/math.jl
DanielDoehring Dec 18, 2023
cac51a3
for comparison
DanielDoehring Dec 19, 2023
e9e633b
Merge branch 'NaNMath' of github.com:DanielDoehring/Trixi.jl into NaN…
DanielDoehring Dec 19, 2023
ead190b
Update src/auxiliary/math.jl
DanielDoehring Dec 19, 2023
d1131e6
Update src/auxiliary/math.jl
DanielDoehring Dec 19, 2023
8c7efd4
llvm version log
DanielDoehring Jan 10, 2024
91f5d3c
Catch ints in sqrt_
DanielDoehring Jan 10, 2024
7389439
Merge branch 'main' into NaNMath
DanielDoehring Jan 10, 2024
a9523d4
Use sqrt_ log_ everywhere
DanielDoehring Jan 10, 2024
af22566
docu
DanielDoehring Jan 10, 2024
7bbec8b
fmt
DanielDoehring Jan 10, 2024
35a32bb
replace in comment
DanielDoehring Jan 10, 2024
f308b77
try exporting nan funcs
DanielDoehring Jan 10, 2024
4f7fa49
enable SIMD again
DanielDoehring Jan 10, 2024
6547640
Bring back SIMD
DanielDoehring Jan 11, 2024
50fa9f1
doc
DanielDoehring Jan 11, 2024
2ce3fdd
Update src/auxiliary/math.jl
DanielDoehring Jan 11, 2024
8062e95
Update src/auxiliary/math.jl
DanielDoehring Jan 11, 2024
303c332
docstring fmt
DanielDoehring Jan 11, 2024
2242236
Merge branch 'main' into NaNMath
DanielDoehring Jan 11, 2024
23d3b45
Merge branch 'main' into NaNMath
DanielDoehring Jan 11, 2024
a77cfaf
remove redundant docstrings
DanielDoehring Jan 11, 2024
c2fb55c
no own names
DanielDoehring Jan 12, 2024
f9f46d3
fmt
DanielDoehring Jan 12, 2024
3a93ea3
revert unintended
DanielDoehring Jan 12, 2024
0e9ff77
revert
DanielDoehring Jan 12, 2024
a952ac4
remove unintended
DanielDoehring Jan 12, 2024
7d85892
revert
DanielDoehring Jan 12, 2024
c79ba8c
fmt
DanielDoehring Jan 12, 2024
5fd0563
comments
DanielDoehring Jan 12, 2024
4508c3c
Merge branch 'main' into NaNMath
DanielDoehring Jan 12, 2024
91c274e
Merge branch 'main' into NaNMath
DanielDoehring Jan 12, 2024
edc3e2d
update test vals
DanielDoehring Jan 14, 2024
4372108
test vals
DanielDoehring Jan 14, 2024
aec0375
test vals
DanielDoehring Jan 15, 2024
d74ffac
Preferences
DanielDoehring Jan 18, 2024
64751ed
Update Project.toml
DanielDoehring Jan 18, 2024
a415bc5
Update src/Trixi.jl
DanielDoehring Jan 18, 2024
f58b316
fmt
DanielDoehring Jan 18, 2024
5db9c6c
docstrings
DanielDoehring Jan 18, 2024
7a440c8
docstrings
DanielDoehring Jan 18, 2024
b8578db
docstrings
DanielDoehring Jan 18, 2024
ac94829
compat info
DanielDoehring Jan 18, 2024
0c7795c
Apply suggestions from code review
DanielDoehring Jan 19, 2024
35e0cf6
escape "
DanielDoehring Jan 19, 2024
847c014
fmt
DanielDoehring Jan 19, 2024
1b7a5d0
Merge branch 'main' into NaNMath
DanielDoehring Jan 19, 2024
df6222e
Merge branch 'main' into NaNMath
DanielDoehring Jan 22, 2024
2225446
Merge branch 'main' into NaNMath
DanielDoehring Jan 31, 2024
22a7cdc
Merge branch 'main' into NaNMath
DanielDoehring Jan 31, 2024
fc96d58
Merge branch 'main' into NaNMath
DanielDoehring Jan 31, 2024
74c72cb
Merge branch 'main' into NaNMath
DanielDoehring Feb 1, 2024
65d1b6d
Merge branch 'main' into NaNMath
ranocha Feb 6, 2024
a920e51
fix benchmarks configuration
ranocha Feb 7, 2024
e6cf76b
Merge branch 'main' into NaNMath
DanielDoehring Feb 7, 2024
2ca4984
Merge branch 'main' into NaNMath
DanielDoehring Feb 8, 2024
fef217d
Merge branch 'main' into NaNMath
ranocha Feb 21, 2024
74d9566
Merge branch 'main' into NaNMath
ranocha Feb 21, 2024
c5eee35
skip UUIDs in downgrade CI job
ranocha Feb 22, 2024
6c17cc8
Merge branch 'main' into NaNMath
ranocha Feb 22, 2024
3d0e108
Update src/auxiliary/math.jl
DanielDoehring Feb 22, 2024
fc22360
Update Project.toml
DanielDoehring Feb 22, 2024
0229017
Merge branch 'main' into NaNMath
DanielDoehring Feb 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/auxiliary/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
@muladd begin
#! format: noindent

# `AbstractFloat` clashes with `ForwardDiff.Dual` => use `Real`
sqrt_(x::T) where {T <: Real} = x < zero(x) ? oftype(x, NaN) : Base.sqrt(x)
DanielDoehring marked this conversation as resolved.
Show resolved Hide resolved
#sqrt_(x) = Base.sqrt(x)
# < 0 suffices since log(0) = -Inf
# `AbstractFloat` clashes with `ForwardDiff.Dual` => use `Real`
log_(x::T) where {T <: Real} = x < zero(x) ? oftype(x, NaN) : Base.log(x)
DanielDoehring marked this conversation as resolved.
Show resolved Hide resolved
#log_(x) = Base.log(x)

"""
ln_mean(x, y)

Expand Down Expand Up @@ -59,7 +67,7 @@ Given ε = 1.0e-4, we use the following algorithm.
if f2 < epsilon_f2
return (x + y) / @evalpoly(f2, 2, 2/3, 2/5, 2/7)
else
return (y - x) / log(y / x)
return (y - x) / log_(y / x)
end
end

Expand All @@ -79,7 +87,7 @@ multiplication.
if f2 < epsilon_f2
return @evalpoly(f2, 2, 2/3, 2/5, 2/7) / (x + y)
else
return log(y / x) / (y - x)
return log_(y / x) / (y - x)
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/callbacks_step/averaging_dg2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ function calc_mean_values!(mean_values, averaging_callback_cache, u, u_prev,
rho, v1, v2, p = u_node_prim
rho_prev, v1_prev, v2_prev, p_prev = u_prev_node_prim

c = sqrt(equations.gamma * p / rho)
c_prev = sqrt(equations.gamma * p_prev / rho_prev)
c = sqrt_(equations.gamma * p / rho)
c_prev = sqrt_(equations.gamma * p_prev / rho_prev)

# Calculate the contribution to the mean values using the trapezoidal rule
vorticity_mean[i, j, element] += integration_constant *
Expand Down
54 changes: 27 additions & 27 deletions src/equations/compressible_euler_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ are available in the paper:
# Riemann Solvers and Numerical Methods for Fluid Dynamics: A Practical Introduction
# [DOI: 10.1007/b79761](https://doi.org/10.1007/b79761)
if v_normal <= 0.0
sound_speed = sqrt(equations.gamma * p_local / rho_local) # local sound speed
sound_speed = sqrt_(equations.gamma * p_local / rho_local) # local sound speed
p_star = p_local *
(1 + 0.5 * (equations.gamma - 1) * v_normal / sound_speed)^(2 *
equations.gamma *
Expand All @@ -240,7 +240,7 @@ are available in the paper:
B = p_local * (equations.gamma - 1) / (equations.gamma + 1)
p_star = p_local +
0.5 * v_normal / A *
(v_normal + sqrt(v_normal^2 + 4 * A * (p_local + B)))
(v_normal + sqrt_(v_normal^2 + 4 * A * (p_local + B)))
end

# For the slip wall we directly set the flux as the normal velocity is zero
Expand Down Expand Up @@ -449,7 +449,7 @@ end
rho, rho_v1, rho_e = u
v1 = rho_v1 / rho
p = (equations.gamma - 1) * (rho_e - 0.5 * rho_v1 * v1)
a = sqrt(equations.gamma * p / rho)
a = sqrt_(equations.gamma * p / rho)

lambda1 = v1
lambda2 = v1 + a
Expand All @@ -475,7 +475,7 @@ end
rho, rho_v1, rho_e = u
v1 = rho_v1 / rho
p = (equations.gamma - 1) * (rho_e - 0.5 * rho_v1 * v1)
a = sqrt(equations.gamma * p / rho)
a = sqrt_(equations.gamma * p / rho)

lambda1 = v1
lambda2 = v1 + a
Expand Down Expand Up @@ -544,7 +544,7 @@ end
p = (equations.gamma - 1) * (rho_e - 0.5 * rho_v1 * v1)

# sound speed and enthalpy
a = sqrt(equations.gamma * p / rho)
a = sqrt_(equations.gamma * p / rho)
H = (rho_e + p) / rho

# signed Mach number
Expand All @@ -566,7 +566,7 @@ end
p = (equations.gamma - 1) * (rho_e - 0.5 * rho_v1 * v1)

# sound speed and enthalpy
a = sqrt(equations.gamma * p / rho)
a = sqrt_(equations.gamma * p / rho)
H = (rho_e + p) / rho

# signed Mach number
Expand Down Expand Up @@ -632,7 +632,7 @@ end
p = (equations.gamma - 1) * (rho_e - 0.5 * rho_v1 * v1)

# sound speed and enthalpy
a = sqrt(equations.gamma * p / rho)
a = sqrt_(equations.gamma * p / rho)
H = (rho_e + p) / rho

# signed Mach number
Expand All @@ -659,7 +659,7 @@ end
p = (equations.gamma - 1) * (rho_e - 0.5 * rho_v1 * v1)

# sound speed and enthalpy
a = sqrt(equations.gamma * p / rho)
a = sqrt_(equations.gamma * p / rho)
H = (rho_e + p) / rho

# signed Mach number
Expand Down Expand Up @@ -690,11 +690,11 @@ end
v1_ll = rho_v1_ll / rho_ll
v_mag_ll = abs(v1_ll)
p_ll = (equations.gamma - 1) * (rho_e_ll - 1 / 2 * rho_ll * v_mag_ll^2)
c_ll = sqrt(equations.gamma * p_ll / rho_ll)
c_ll = sqrt_(equations.gamma * p_ll / rho_ll)
v1_rr = rho_v1_rr / rho_rr
v_mag_rr = abs(v1_rr)
p_rr = (equations.gamma - 1) * (rho_e_rr - 1 / 2 * rho_rr * v_mag_rr^2)
c_rr = sqrt(equations.gamma * p_rr / rho_rr)
c_rr = sqrt_(equations.gamma * p_rr / rho_rr)

λ_max = max(v_mag_ll, v_mag_rr) + max(c_ll, c_rr)
end
Expand All @@ -705,8 +705,8 @@ end
rho_ll, v1_ll, p_ll = cons2prim(u_ll, equations)
rho_rr, v1_rr, p_rr = cons2prim(u_rr, equations)

λ_min = v1_ll - sqrt(equations.gamma * p_ll / rho_ll)
λ_max = v1_rr + sqrt(equations.gamma * p_rr / rho_rr)
λ_min = v1_ll - sqrt_(equations.gamma * p_ll / rho_ll)
λ_max = v1_rr + sqrt_(equations.gamma * p_rr / rho_rr)

return λ_min, λ_max
end
Expand All @@ -717,8 +717,8 @@ end
rho_ll, v1_ll, p_ll = cons2prim(u_ll, equations)
rho_rr, v1_rr, p_rr = cons2prim(u_rr, equations)

c_ll = sqrt(equations.gamma * p_ll / rho_ll)
c_rr = sqrt(equations.gamma * p_rr / rho_rr)
c_ll = sqrt_(equations.gamma * p_ll / rho_ll)
c_rr = sqrt_(equations.gamma * p_rr / rho_rr)

λ_min = min(v1_ll - c_ll, v1_rr - c_rr)
λ_max = max(v1_ll + c_ll, v1_rr + c_rr)
Expand All @@ -742,20 +742,20 @@ function flux_hllc(u_ll, u_rr, orientation::Integer,
v1_ll = rho_v1_ll / rho_ll
e_ll = rho_e_ll / rho_ll
p_ll = (equations.gamma - 1) * (rho_e_ll - 1 / 2 * rho_ll * v1_ll^2)
c_ll = sqrt(equations.gamma * p_ll / rho_ll)
c_ll = sqrt_(equations.gamma * p_ll / rho_ll)

v1_rr = rho_v1_rr / rho_rr
e_rr = rho_e_rr / rho_rr
p_rr = (equations.gamma - 1) * (rho_e_rr - 1 / 2 * rho_rr * v1_rr^2)
c_rr = sqrt(equations.gamma * p_rr / rho_rr)
c_rr = sqrt_(equations.gamma * p_rr / rho_rr)

# Obtain left and right fluxes
f_ll = flux(u_ll, orientation, equations)
f_rr = flux(u_rr, orientation, equations)

# Compute Roe averages
sqrt_rho_ll = sqrt(rho_ll)
sqrt_rho_rr = sqrt(rho_rr)
sqrt_rho_ll = sqrt_(rho_ll)
sqrt_rho_rr = sqrt_(rho_rr)
sum_sqrt_rho = sqrt_rho_ll + sqrt_rho_rr
vel_L = v1_ll
vel_R = v1_rr
Expand All @@ -764,7 +764,7 @@ function flux_hllc(u_ll, u_rr, orientation::Integer,
H_ll = (rho_e_ll + p_ll) / rho_ll
H_rr = (rho_e_rr + p_rr) / rho_rr
H_roe = (sqrt_rho_ll * H_ll + sqrt_rho_rr * H_rr) / sum_sqrt_rho
c_roe = sqrt((equations.gamma - 1) * (H_roe - ekin_roe))
c_roe = sqrt_((equations.gamma - 1) * (H_roe - ekin_roe))

Ssl = min(vel_L - c_ll, vel_roe - c_roe)
Ssr = max(vel_R + c_rr, vel_roe + c_roe)
Expand Down Expand Up @@ -833,22 +833,22 @@ Compactly summarized:

# `u_ll[3]` is total energy `rho_e_ll` on the left
H_ll = (u_ll[3] + p_ll) / rho_ll
c_ll = sqrt(equations.gamma * p_ll / rho_ll)
c_ll = sqrt_(equations.gamma * p_ll / rho_ll)

# `u_rr[3]` is total energy `rho_e_rr` on the right
H_rr = (u_rr[3] + p_rr) / rho_rr
c_rr = sqrt(equations.gamma * p_rr / rho_rr)
c_rr = sqrt_(equations.gamma * p_rr / rho_rr)

# Compute Roe averages
sqrt_rho_ll = sqrt(rho_ll)
sqrt_rho_rr = sqrt(rho_rr)
sqrt_rho_ll = sqrt_(rho_ll)
sqrt_rho_rr = sqrt_(rho_rr)
inv_sum_sqrt_rho = inv(sqrt_rho_ll + sqrt_rho_rr)

v_roe = (sqrt_rho_ll * v_ll + sqrt_rho_rr * v_rr) * inv_sum_sqrt_rho
v_roe_mag = v_roe^2

H_roe = (sqrt_rho_ll * H_ll + sqrt_rho_rr * H_rr) * inv_sum_sqrt_rho
c_roe = sqrt((equations.gamma - 1) * (H_roe - 0.5 * v_roe_mag))
c_roe = sqrt_((equations.gamma - 1) * (H_roe - 0.5 * v_roe_mag))

# Compute convenience constant for positivity preservation, see
# https://doi.org/10.1016/0021-9991(91)90211-3
Expand All @@ -865,7 +865,7 @@ end
rho, rho_v1, rho_e = u
v1 = rho_v1 / rho
p = (equations.gamma - 1) * (rho_e - 1 / 2 * rho * v1^2)
c = sqrt(equations.gamma * p / rho)
c = sqrt_(equations.gamma * p / rho)

return (abs(v1) + c,)
end
Expand All @@ -887,7 +887,7 @@ end
v1 = rho_v1 / rho
v_square = v1^2
p = (equations.gamma - 1) * (rho_e - 0.5 * rho * v_square)
s = log(p) - equations.gamma * log(rho)
s = log_(p) - equations.gamma * log_(rho)
rho_p = rho / p

w1 = (equations.gamma - s) * equations.inv_gamma_minus_one - 0.5 * rho_p * v_square
Expand Down Expand Up @@ -951,7 +951,7 @@ end
p = (equations.gamma - 1) * (cons[3] - 1 / 2 * (cons[2]^2) / cons[1])

# Thermodynamic entropy
s = log(p) - equations.gamma * log(cons[1])
s = log_(p) - equations.gamma * log_(cons[1])

return s
end
Expand Down
Loading
Loading