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

add gradient variable type parameter to AbstractEquationsParabolic #1409

Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
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
11 changes: 9 additions & 2 deletions docs/literate/src/files/adding_new_parabolic_terms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ equations_hyperbolic = LinearScalarAdvectionEquation2D(advection_velocity);
# `ConstantAnisotropicDiffusion2D` has a field for `equations_hyperbolic`. It is useful to have
# information about the hyperbolic system available to the parabolic part so that we can reuse
# functions defined for hyperbolic equations (such as `varnames`).

struct ConstantAnisotropicDiffusion2D{E, T} <: Trixi.AbstractEquationsParabolic{2, 1}
#
# The abstract type `Trixi.AbstractEquationsParabolic` has three parameters: `NDIMS` (the spatial dimension,
# e.g., 1D, 2D, or 3D), `NVARS` (the number of variables), and `GradientVariable`, which we set as
# `GradientVariablesConservative`. This indicates that the gradient should be taken with respect to the
# conservative variables (e.g., the same variables used in `equations_hyperbolic`). Users can also take
# the gradient with respect to a different set of variables; see, for example, the implementation of
# [`CompressibleNavierStokesDiffusion2D`](@ref), which can utilize either "primitive" or "entropy" variables.

struct ConstantAnisotropicDiffusion2D{E, T} <: Trixi.AbstractEquationsParabolic{2, 1, GradientVariablesConservative}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we find a way to make this choice automatic? Otherwise, it will be a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, no, since I think it would require the ability to set "default" parameters. https://discourse.julialang.org/t/type-parameters-by-default/45577

I can hold off on this PR until we are ready for a breaking release. The stuff that would require this is an optimization, and doesn't prevent me from implementing other features.

diffusivity::T
equations_hyperbolic::E
end
Expand Down
2 changes: 1 addition & 1 deletion src/Trixi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export AcousticPerturbationEquations2D,
export LaplaceDiffusion1D, LaplaceDiffusion2D,
CompressibleNavierStokesDiffusion2D, CompressibleNavierStokesDiffusion3D

export GradientVariablesPrimitive, GradientVariablesEntropy
export GradientVariablesConservative, GradientVariablesPrimitive, GradientVariablesEntropy

export flux, flux_central, flux_lax_friedrichs, flux_hll, flux_hllc, flux_hlle,
flux_godunov,
Expand Down
5 changes: 1 addition & 4 deletions src/equations/compressible_navier_stokes_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ w_2 = \frac{\rho v_1}{p},\, w_3 = \frac{\rho v_2}{p},\, w_4 = -\frac{\rho}{p}
"""
struct CompressibleNavierStokesDiffusion2D{GradientVariables, RealT <: Real,
E <: AbstractCompressibleEulerEquations{2}} <:
AbstractCompressibleNavierStokesDiffusion{2, 4}
AbstractCompressibleNavierStokesDiffusion{2, 4, GradientVariables}
# TODO: parabolic
# 1) For now save gamma and inv(gamma-1) again, but could potentially reuse them from the Euler equations
# 2) Add NGRADS as a type parameter here and in AbstractEquationsParabolic, add `ngradients(...)` accessor function
Expand Down Expand Up @@ -108,9 +108,6 @@ formulation from

Under `GradientVariablesEntropy`, the Navier-Stokes discretization is provably entropy stable.
"""
struct GradientVariablesPrimitive end
struct GradientVariablesEntropy end

# default to primitive gradient variables
function CompressibleNavierStokesDiffusion2D(equations::CompressibleEulerEquations2D;
mu, Prandtl,
Expand Down
2 changes: 1 addition & 1 deletion src/equations/compressible_navier_stokes_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ w_2 = \frac{\rho v_1}{p},\, w_3 = \frac{\rho v_2}{p},\, w_4 = \frac{\rho v_3}{p}
"""
struct CompressibleNavierStokesDiffusion3D{GradientVariables, RealT <: Real,
E <: AbstractCompressibleEulerEquations{3}} <:
AbstractCompressibleNavierStokesDiffusion{3, 5}
AbstractCompressibleNavierStokesDiffusion{3, 5, GradientVariables}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I understand correctly, this modification sets GradientVariablesConservative to be the default within equations_parabolic.jl only for the Laplace diffusion whereas for Navier-Stokes we still need to pass whichever gradient_variables we want into the constructor?

For completeness, would it be necessary to add an appropriate convert_derivative_to_primitive function for the GradientVariablesConservative in the Navier-Stokes file?

Copy link
Contributor Author

@jlchan jlchan Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, for Navier-Stokes the constructor defaults to GradientVariablesPrimitive.

https://github.com/jlchan/Trixi.jl/blob/c3ffffe0090a0913edc6e7829686b7a77a8d5bc0/src/equations/compressible_navier_stokes_2d.jl#L104-L106

I don't think we plan on adding conservative gradient variables, so I'd lean towards not implementing convert_derivative_to_primitive for conservative gradient variables.

# TODO: parabolic
# 1) For now save gamma and inv(gamma-1) again, but could potentially reuse them from the Euler equations
# 2) Add NGRADS as a type parameter here and in AbstractEquationsParabolic, add `ngradients(...)` accessor function
Expand Down
2 changes: 1 addition & 1 deletion src/equations/equations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,6 @@ abstract type AbstractLinearizedEulerEquations{NDIMS, NVARS} <:
AbstractEquations{NDIMS, NVARS} end
include("linearized_euler_2d.jl")

abstract type AbstractEquationsParabolic{NDIMS, NVARS} <:
abstract type AbstractEquationsParabolic{NDIMS, NVARS, GradientVariables} <:
AbstractEquations{NDIMS, NVARS} end
end # @muladd
13 changes: 10 additions & 3 deletions src/equations/equations_parabolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
# specialize this function to compute gradients e.g., of primitive variables instead of conservative
gradient_variable_transformation(::AbstractEquationsParabolic) = cons2cons

# By default, the gradients are taken with respect to the conservative variables.
# this is reflected by the type parameter `GradientVariablesConservative` in the abstract
# type `AbstractEquationsParabolic{NDIMS, NVARS, GradientVariablesConservative}`.
struct GradientVariablesConservative end
struct GradientVariablesPrimitive end
struct GradientVariablesEntropy end

# Linear scalar diffusion for use in linear scalar advection-diffusion problems
abstract type AbstractLaplaceDiffusion{NDIMS, NVARS} <:
AbstractEquationsParabolic{NDIMS, NVARS} end
AbstractEquationsParabolic{NDIMS, NVARS, GradientVariablesConservative} end
include("laplace_diffusion_1d.jl")
include("laplace_diffusion_2d.jl")

# Compressible Navier-Stokes equations
abstract type AbstractCompressibleNavierStokesDiffusion{NDIMS, NVARS} <:
AbstractEquationsParabolic{NDIMS, NVARS} end
abstract type AbstractCompressibleNavierStokesDiffusion{NDIMS, NVARS, GradientVariables} <:
AbstractEquationsParabolic{NDIMS, NVARS, GradientVariables} end
include("compressible_navier_stokes_2d.jl")
include("compressible_navier_stokes_3d.jl")