Skip to content

Commit

Permalink
remove allocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmontoya committed Sep 10, 2023
1 parent 760ce83 commit 48ca782
Show file tree
Hide file tree
Showing 11 changed files with 2,249 additions and 2,295 deletions.
4 changes: 0 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ authors = ["Tristan Montoya <[email protected]>"]
version = "0.2.3"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Arpack = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
Expand All @@ -20,7 +19,6 @@ LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LinearMaps = "7a12625a-238d-50fd-b39a-03d52299707e"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
MethodAnalysis = "85b6ec6f-f7df-4429-9514-a64bcd9ee824"
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"
NodesAndModes = "7aca2e03-f7e2-4192-9ec8-f4ca66d597fb"
Octavian = "6fd5a793-0b7e-452c-907f-f8bfe9c57db4"
Expand All @@ -32,11 +30,9 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
SnoopCompile = "aa65fe97-06da-5843-b5b1-d5d13cad87d2"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StartUpDG = "472ebc20-7c99-4d4b-9470-8fde4e9faa0f"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StrideArrays = "d1fa6d79-ef01-42a6-86c9-f7c551f8593b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TetGen = "c5d3f3f7-f850-59f6-8a2e-ffc6dc1317ea"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
Expand Down
2,376 changes: 1,188 additions & 1,188 deletions examples/advection_2d.ipynb

Large diffs are not rendered by default.

2,083 changes: 1,003 additions & 1,080 deletions examples/euler_vortex_2d.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Analysis/conservation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function evaluate_conservation_residual(
return dEdt
end

function evaluate_conservation_residual(
@inline @views function evaluate_conservation_residual(
analysis::EntropyConservationAnalysis,
u::Array{Float64,3},
dudt::Array{Float64,3})
Expand All @@ -185,7 +185,7 @@ function evaluate_conservation_residual(
M = mass_matrix(mass_solver, k)
mul!(u_q,V,u[:,:,k])
for i in axes(u_q,1)
w_q[i,:] = conservative_to_entropy(conservation_law, u_q[i,:])
conservative_to_entropy!(w_q[i,:], conservation_law, u_q[i,:])
end
P = mass_matrix_inverse(mass_solver, k) * V' * WJ[k]
for e in 1:N_c
Expand Down
14 changes: 13 additions & 1 deletion src/ConservationLaws/ConservationLaws.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module ConservationLaws

import ..GridFunctions: evaluate

export physical_flux, physical_flux!, numerical_flux!, entropy, conservative_to_primitive, conservative_to_entropy, entropy_to_conservative, compute_two_point_flux, wave_speed, logmean, inv_logmean, AbstractConservationLaw, AbstractPDEType, FirstOrder, SecondOrder, AbstractInviscidNumericalFlux, AbstractViscousNumericalFlux, NoInviscidFlux, NoViscousFlux, LaxFriedrichsNumericalFlux, CentralNumericalFlux, BR1, EntropyConservativeNumericalFlux, AbstractTwoPointFlux, ConservativeFlux, EntropyConservativeFlux, NoTwoPointFlux, ExactSolution
export physical_flux, physical_flux!, numerical_flux!, entropy, conservative_to_entropy!, entropy_to_conservative!, compute_two_point_flux, wave_speed, logmean, inv_logmean, AbstractConservationLaw, AbstractPDEType, FirstOrder, SecondOrder, AbstractInviscidNumericalFlux, AbstractViscousNumericalFlux, NoInviscidFlux, NoViscousFlux, LaxFriedrichsNumericalFlux, CentralNumericalFlux, BR1, EntropyConservativeNumericalFlux, AbstractTwoPointFlux, ConservativeFlux, EntropyConservativeFlux, NoTwoPointFlux, ExactSolution

abstract type AbstractConservationLaw{d, PDEType, N_c} end
abstract type AbstractPDEType end
Expand Down Expand Up @@ -128,6 +128,18 @@ module ConservationLaws
end
end

@inbounds function entropy_to_conservative!(u::AbstractVector{Float64},
::AbstractConservationLaw, w::AbstractVector{Float64})
copyto!(u,w)
return
end

@inbounds function conservative_to_entropy(w::AbstractVector{Float64},
::AbstractConservationLaw, u::AbstractVector{Float64})
copyto!(w,u)
return
end

export LinearAdvectionEquation, LinearAdvectionDiffusionEquation
include("linear_advection_diffusion.jl")

Expand Down
3 changes: 0 additions & 3 deletions src/ConservationLaws/burgers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ F*(u⁻, u⁺, q⁻, q⁺, n) = ½(F²(u⁻,q⁻) + F²(u⁺, q⁺))⋅n
for m in 1:d)
end

@inline conservative_to_primitive(::BurgersType, u) = u
@inline entropy(::BurgersType, u) = 0.5*u[1]^2
@inline entropy_to_conservative(::BurgersType, u) = u
@inline conservative_to_entropy(::BurgersType, u) = u

@inline function wave_speed(conservation_law::BurgersType{d},
u_in::AbstractVector{Float64}, u_out::AbstractVector{Float64},
Expand Down
38 changes: 33 additions & 5 deletions src/ConservationLaws/euler_navierstokes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,60 @@ end
u::AbstractVector{Float64}) where {d}
(; γ_minus_1) = conservation_law
return vcat(u[1], SVector{d}(u[m+1] / u[1] for m in 1:d),
γ_minus_1 * (u[end] - (0.5/u[1]) * (sum(u[m+1]^2 for m in 1:d))))
γ_minus_1 * (u[d+2] - (0.5/u[1]) * (sum(u[m+1]^2 for m in 1:d))))
end

@inline function conservative_to_entropy(conservation_law::EulerType{d},
u::AbstractVector{Float64}) where {d}
(; γ, γ_minus_1, inv_γ_minus_1) = conservation_law
k = (0.5/u[1]) * (sum(u[m+1]^2 for m in 1:d))
p = γ_minus_1 * (u[end] - k)
p = γ_minus_1 * (u[d+2] - k)
inv_p = 1.0/p
return SVector{d+2}(inv_γ_minus_1*-log(p/(u[1]^γ))) - k*inv_p,
(u[m+1]*inv_p for m in 1:d)...,
-u[1]*inv_p)
end

@inline function conservative_to_entropy!(
w::AbstractVector{Float64}, conservation_law::EulerType{d},
u::AbstractVector{Float64}) where {d}
(; γ, γ_minus_1, inv_γ_minus_1) = conservation_law
k = (0.5/u[1]) * (sum(u[m+1]^2 for m in 1:d))
p = γ_minus_1 * (u[end] - k)
inv_p = 1.0/p
w[1] = inv_γ_minus_1*-log(p/(u[1]^γ))) - k*inv_p
@inbounds for m in 1:d w[m+1] = u[m+1]*inv_p end
w[d+2] = -u[1]*inv_p
return
end

@inline function entropy_to_conservative(conservation_law::EulerType{d},
w::AbstractVector{Float64}) where {d}
(; γ, γ_minus_1, inv_γ_minus_1) = conservation_law
w = w * γ_minus_1
k = sum(w[m+1]^2 for m in 1:d)/(2*w[end])
s = γ - w[1] + k
ρe = (γ_minus_1/((-w[end])^γ))^inv_γ_minus_1*exp(-s*inv_γ_minus_1)
return SVector{d+2}(-w[end]*ρe,
(w[m+1] * ρe for m in 1:d)...,
ρe*(1-k))
return SVector{d+2}(-w[end]*ρe, (w[m+1] * ρe for m in 1:d)..., ρe*(1-k))
end

@inline function entropy_to_conservative!(
u::AbstractVector{Float64},
conservation_law::EulerType{d},
w::AbstractVector{Float64}) where {d}
(; γ, γ_minus_1, inv_γ_minus_1) = conservation_law
w = w * γ_minus_1
k = sum(w[m+1]^2 for m in 1:d)/(2*w[d+2])
s = γ - w[1] + k
ρe = (γ_minus_1/((-w[d+2])^γ))^inv_γ_minus_1*exp(-s*inv_γ_minus_1)

u[1] = -w[d+2]*ρe
@inbounds for m in 1:d u[m+1] = w[m+1] * ρe end
u[d+2] = ρe*(1-k)
return
end


@inline function wave_speed(conservation_law::EulerType{d},
u_in::AbstractVector{Float64}, u_out::AbstractVector{Float64},
n_f) where {d}
Expand Down
3 changes: 0 additions & 3 deletions src/ConservationLaws/linear_advection_diffusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ function numerical_flux!(f_star::AbstractMatrix{Float64},
for m in 1:d)
end

@inline conservative_to_primitive(::AdvectionType, u) = u
@inline conservative_to_entropy(::AdvectionType, u) = u
@inline entropy_to_conservative(::AdvectionType, u) = u
@inline entropy(::AdvectionType, u) = 0.5*u[1]^2

@inline function wave_speed(conservation_law::AdvectionType{d},
Expand Down
5 changes: 3 additions & 2 deletions src/MatrixFreeOperators/octavian.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
struct OctavianMap{M_type} <: LinearMaps.LinearMap{Float64}
lmap::M_type
struct OctavianMap <: LinearMaps.LinearMap{Float64}
lmap::Matrix{Float64}
end

@inline function OctavianMap(L::LinearMap)
return OctavianMap(Matrix(L))
end

@inline Base.size(L::OctavianMap) = size(L.lmap)

function LinearAlgebra.transpose(L::OctavianMap)
return OctavianMap(transpose(L.lmap))
end
Expand Down
4 changes: 2 additions & 2 deletions src/MatrixFreeOperators/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ end

Base.size(R::SelectionMap) = (length(R.facet_ids),length(R.volume_ids))

function LinearAlgebra.mul!(y::AbstractVector,
@inline function LinearAlgebra.mul!(y::AbstractVector,
R::SelectionMap, x::AbstractVector)
LinearMaps.check_dim_mul(y, R, x)
y[:] = x[R.facet_ids]
return y
end

function LinearMaps._unsafe_mul!(y::AbstractVector,
@inline function LinearMaps._unsafe_mul!(y::AbstractVector,
transR::LinearMaps.TransposeMap{Float64, <:SelectionMap},
x::AbstractVector)

Expand Down
10 changes: 5 additions & 5 deletions src/Solvers/flux_differencing_form.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ end

mul!(u_q, V, u)
@inbounds for i in axes(u, 1)
w_q[i,:] .= conservative_to_entropy(conservation_law,u_q[i,:])
conservative_to_entropy!(w_q[i,:], conservation_law,u_q[i,:])
end
mul!(w_f, R, w_q)
@inbounds for i in axes(u_f, 1)
u_f[i,:] .= entropy_to_conservative(conservation_law,w_f[i,:])
entropy_to_conservative!(u_f[i,:], conservation_law,w_f[i,:])
end
end

Expand All @@ -226,7 +226,7 @@ end
# evaluate entropy variables in terms of nodal conservative variables
mul!(u_q, V, u)
@inbounds for i in axes(u_q, 1)
w_q[i,:] .= conservative_to_entropy(conservation_law, u_q[i,:])
conservative_to_entropy!(w_q[i,:], conservation_law, u_q[i,:])
end

# project entropy variables and store modal coeffs in w
Expand All @@ -240,10 +240,10 @@ end

# convert back to conservative variables
@inbounds for i in axes(u_q, 1)
u_q[i,:] .= entropy_to_conservative(conservation_law, w_q[i,:])
entropy_to_conservative!(u_q[i,:], conservation_law, w_q[i,:])
end
@inbounds for i in axes(u_f, 1)
u_f[i,:] .= entropy_to_conservative(conservation_law, w_f[i,:])
entropy_to_conservative!(u_f[i,:], conservation_law, w_f[i,:])
end
end

Expand Down

0 comments on commit 48ca782

Please sign in to comment.