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 lengthscale functions to mesh and spaces #739

Merged
merged 2 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ agents:

env:
JULIA_LOAD_PATH: "${JULIA_LOAD_PATH}:${BUILDKITE_BUILD_CHECKOUT_PATH}/.buildkite"
JULIA_DEPOT_PATH: "${BUILDKITE_BUILD_PATH}/${BUILDKITE_PIPELINE_SLUG}/depot/default"
# JULIA_DEPOT_PATH: "${BUILDKITE_BUILD_PATH}/${BUILDKITE_PIPELINE_SLUG}/depot/default"
Copy link
Member

Choose a reason for hiding this comment

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

Just noticed this. Is this intentional?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sort of..

JULIA_MAX_NUM_PRECOMPILE_FILES: 100
JULIA_CPU_TARGET: 'broadwell;skylake'
JULIA_NVTX_CALLBACKS: gc
Expand Down
5 changes: 5 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Meshes.SharedVertices
Meshes.face_connectivity_matrix
Meshes.vertex_connectivity_matrix
Meshes.linearindices
Meshes.element_horizontal_length_scale
```

## Topologies
Expand Down Expand Up @@ -195,6 +196,10 @@ Spaces.SpectralElementSpace2D
Spaces.SpectralElementSpaceSlab
```

```@docs
Spaces.node_horizontal_length_scale
```

### Quadratures


Expand Down
6 changes: 4 additions & 2 deletions examples/bickleyjet/bickleyjet_cg_invariant_hypervisc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const parameters = (
ρ₀ = 1.0, # reference density
c = 2,
g = 10,
D₄ = 1e-4, # hyperdiffusion coefficient
)

domain = Domains.RectangleDomain(
Expand Down Expand Up @@ -105,7 +104,10 @@ function energy(state, p, local_geometry)
end

function rhs!(dydt, y, _, t)
D₄ = parameters.D₄
space = axes(y)
c = sqrt(parameters.g * parameters.ρ₀)
D₄ = 0.0015 * c * Spaces.node_horizontal_length_scale(space)^3 # hyperdiffusion coefficient

g = parameters.g

sdiv = Operators.Divergence()
Expand Down
18 changes: 12 additions & 6 deletions examples/sphere/shallow_water.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Base.@kwdef struct PhysicalParameters{FT} # rename to PhysicalParameters
"Gravitational constant"
g::FT = FT(9.80616)
"Hyperdiffusion coefficient"
D₄::FT = FT(1.0e16)
ν₄::FT = FT(0.25)
end
#This example solves the shallow-water equations on a cubed-sphere manifold.
#This file contains five test cases:
Expand Down Expand Up @@ -256,7 +256,7 @@ end
# Set initial condition
function set_initial_condition(space, test::RossbyHaurwitzTest)
(; a, h0, ω, K, α, params) = test
(; R, Ω, g, D₄) = params
(; R, Ω, g) = params
Y = map(Fields.local_geometry_field(space)) do local_geometry
coord = local_geometry.coordinates
ϕ = coord.lat
Expand Down Expand Up @@ -296,7 +296,7 @@ end

function set_initial_condition(space, test::SteadyStateCompactTest)
(; u0, h0, ϕᵦ, ϕₑ, xₑ, α, params) = test
(; R, Ω, g, D₄) = params
(; R, Ω, g) = params
Y = map(Fields.local_geometry_field(space)) do local_geometry
coord = local_geometry.coordinates

Expand Down Expand Up @@ -369,7 +369,7 @@ function set_initial_condition(space, test::SteadyStateCompactTest)
end
function set_initial_condition(space, test::BarotropicInstabilityTest)
(; u_max, αₚ, βₚ, h0, h_hat, ϕ₀, ϕ₁, ϕ₂, eₙ, α, params) = test
(; R, Ω, g, D₄) = params
(; R, Ω, g) = params

Y = map(Fields.local_geometry_field(space)) do local_geometry
coord = local_geometry.coordinates
Expand Down Expand Up @@ -428,7 +428,7 @@ function set_initial_condition(
test::T,
) where {T <: Union{MountainTest, SteadyStateTest}}
(; u0, h0, α, params) = test
(; R, Ω, g, D₄) = params
(; R, Ω, g) = params
Y = map(Fields.local_geometry_field(space)) do local_geometry
coord = local_geometry.coordinates

Expand All @@ -455,7 +455,11 @@ end
function rhs!(dYdt, y, parameters, t)
@nvtx "rhs!" color = colorant"red" begin
(; f, h_s, ghost_buffer, params) = parameters
(; D₄, g) = params
(; ν₄, g) = params

space = axes(y)
D₄ = ν₄ * Spaces.node_horizontal_length_scale(space)^3 # hyperdiffusion coefficient


div = Operators.Divergence()
wdiv = Operators.WeakDivergence()
Expand Down Expand Up @@ -551,6 +555,8 @@ function shallow_water_driver(ARGS, ::Type{FT}) where {FT}
global_space =
space = Spaces.SpectralElementSpace2D(grid_topology, quad)
end
@show Spaces.node_horizontal_length_scale(space)^3

coords = Fields.coordinate_field(space)
f = set_coriolis_parameter(space, test)
h_s = surface_topography(space, test)
Expand Down
5 changes: 5 additions & 0 deletions src/Meshes/Meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ The element and face (`opelem`, `opface`) that oppose face `face` of element `el
"""
function opposing_face end

"""
Meshes.element_horizontal_length_scale(mesh::AbstractMesh)

The approximate length scale (in units of distance) of the elements of the mesh.
"""
function element_horizontal_length_scale end

include("common.jl")
include("interval.jl")
Expand Down
6 changes: 6 additions & 0 deletions src/Meshes/cubedsphere.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ end

domain(mesh::AbstractCubedSphere) = mesh.domain
elements(mesh::AbstractCubedSphere) = CartesianIndices((mesh.ne, mesh.ne, 6))
nelements(mesh::AbstractCubedSphere) = mesh.ne * mesh.ne * 6

function element_horizontal_length_scale(mesh::AbstractCubedSphere)
FT = typeof(mesh.domain.radius)
return FT(sqrt(4 * pi / 6)) * mesh.domain.radius / mesh.ne
end

is_boundary_face(mesh::AbstractCubedSphere, elem, face) = false
boundary_face_name(mesh::AbstractCubedSphere, elem, face) = nothing
Expand Down
5 changes: 5 additions & 0 deletions src/Meshes/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ function Base.show(io::IO, mesh::IntervalMesh)
print(io, " of ")
print(io, mesh.domain)
end
function element_horizontal_length_scale(mesh::IntervalMesh)
cmax = Geometry.component(mesh.domain.coord_max, 1)
cmin = Geometry.component(mesh.domain.coord_min, 1)
return (cmax - cmin) / nelements(mesh)
end

coordinates(mesh::IntervalMesh, elem::Integer, vert::Integer) =
mesh.faces[elem + vert - 1]
Expand Down
5 changes: 5 additions & 0 deletions src/Meshes/rectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ domain(mesh::RectilinearMesh) =
RectangleDomain(domain(mesh.intervalmesh1), domain(mesh.intervalmesh2))
nelements(mesh::RectilinearMesh) =
nelements(mesh.intervalmesh1) * nelements(mesh.intervalmesh2)

element_horizontal_length_scale(mesh::RectilinearMesh) = sqrt(
element_horizontal_length_scale(mesh.intervalmesh1) *
element_horizontal_length_scale(mesh.intervalmesh2),
)
function elements(mesh::RectilinearMesh)
# we use the Base Julia CartesianIndices object to index elements in the mesh
CartesianIndices((
Expand Down
2 changes: 2 additions & 0 deletions src/Quadratures/Quadratures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Base.show(io::IO, ::GLL{Nq}) where {Nq} =

@inline polynomial_degree(::GLL{Nq}) where {Nq} = Int(Nq - 1)
@inline degrees_of_freedom(::GLL{Nq}) where {Nq} = Int(Nq)
unique_degrees_of_freedom(::GLL{Nq}) where {Nq} = Int(Nq - 1)

@generated function quadrature_points(::Type{FT}, ::GLL{Nq}) where {FT, Nq}
points, weights = GaussQuadrature.legendre(FT, Nq, GaussQuadrature.both)
Expand All @@ -70,6 +71,7 @@ Base.show(io::IO, ::GL{Nq}) where {Nq} =

@inline polynomial_degree(::GL{Nq}) where {Nq} = Int(Nq - 1)
@inline degrees_of_freedom(::GL{Nq}) where {Nq} = Int(Nq)
unique_degrees_of_freedom(::GL{Nq}) where {Nq} = Int(Nq)

@generated function quadrature_points(::Type{FT}, ::GL{Nq}) where {FT, Nq}
points, weights = GaussQuadrature.legendre(FT, Nq, GaussQuadrature.neither)
Expand Down
13 changes: 13 additions & 0 deletions src/Spaces/spectralelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ nlevels(space::SpectralElementSpaceSlab1D) = 1
nlevels(space::SpectralElementSpaceSlab2D) = 1


"""
Spaces.node_horizontal_length_scale(space::AbstractSpectralElementSpace)

The approximate length scale of the distance between nodes. This is defined as the
length scale of the mesh (see [`Meshes.element_horizontal_length_scale`](@ref)), divided by the
number of unique quadrature points along each dimension.
"""
function node_horizontal_length_scale(space::AbstractSpectralElementSpace)
quad = quadrature_style(space)
Nu = Quadratures.unique_degrees_of_freedom(quad)
return Meshes.element_horizontal_length_scale(space.topology.mesh) / Nu
end




Expand Down
2 changes: 2 additions & 0 deletions test/Meshes/cubedsphere.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ end
end
end
end
@test Meshes.element_horizontal_length_scale(mesh) ≈
sqrt((4pi * 5^2) / Meshes.nelements(mesh))
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/Meshes/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ end
@test length(mesh.faces) == nelems + 1 # number of faces is +1 elements
@test Meshes.nelements(mesh) == nelems
@test Meshes.elements(mesh) == UnitRange(1, nelems)
@test Meshes.element_horizontal_length_scale(mesh) ≈ 1 / nelems
end
dom, mesh = unit_intervalmesh(nelems = 2)
@test Meshes.domain(mesh) isa Domains.IntervalDomain
Expand Down
7 changes: 7 additions & 0 deletions test/Meshes/rectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ meshes = [
CartesianIndices((2, 3))
end

@testset "element_horizontal_length_scale" begin
for mesh in meshes
@test Meshes.element_horizontal_length_scale(mesh) ≈
sqrt(1 / Meshes.nelements(mesh))
end
end

@testset "opposing face" begin
for mesh in meshes
for elem in Meshes.elements(mesh)
Expand Down
3 changes: 3 additions & 0 deletions test/Spaces/sphere.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ end
quad = Quadratures.GLL{Nq}()
horzspace = Spaces.SpectralElementSpace2D(horztopology, quad)

@test Spaces.node_horizontal_length_scale(horzspace) ≈
sqrt((4 * pi * radius^2) / (helem^2 * 6 * (Nq - 1)^2))

hv_center_space =
Spaces.ExtrudedFiniteDifferenceSpace(horzspace, vert_center_space)

Expand Down
Loading