From d4b19453d4036d3321be6c5aa52bf25621d87c7d Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 1 Mar 2023 10:23:44 -0800 Subject: [PATCH 01/42] Memoize SpectralElementSpace2D --- Project.toml | 2 + src/Spaces/Spaces.jl | 3 +- src/Spaces/extruded.jl | 85 ++++++++--------------------------- src/Spaces/spectralelement.jl | 4 +- 4 files changed, 25 insertions(+), 69 deletions(-) diff --git a/Project.toml b/Project.toml index fb45e9cf01..125ce4404d 100644 --- a/Project.toml +++ b/Project.toml @@ -19,6 +19,7 @@ HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Memoize = "c03570c3-d221-55d1-a50c-7939bbd78826" PkgVersion = "eebad327-c553-4316-9ea0-9fa01ccd7688" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Requires = "ae029012-a4dd-5104-9daa-d747884805df" @@ -28,6 +29,7 @@ Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" +WeakValueDicts = "897b6980-f191-5a31-bcb0-bf3c4585e0c1" [compat] Adapt = "3" diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index 2ff34a2094..3bfd230d4d 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -19,10 +19,11 @@ using Adapt using CUDA import ..slab, ..column, ..level -import ..Utilities: PlusHalf +import ..Utilities: PlusHalf, half import ..DataLayouts, ..Geometry, ..Domains, ..Meshes, ..Topologies import ClimaComms using StaticArrays, ForwardDiff, LinearAlgebra, UnPack, Adapt +using Memoize, WeakValueDicts abstract type AbstractSpace end diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 5aacf75358..5580fec58b 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -214,80 +214,33 @@ Base.@propagate_inbounds function column( ) end -Base.@propagate_inbounds function column( - space::ExtrudedFiniteDifferenceSpace, - i, - h, -) - FiniteDifferenceSpace( - space.staggering, - space.vertical_topology, - Geometry.CartesianGlobalGeometry(), - column(space.center_local_geometry, i, h), - column(space.face_local_geometry, i, h), - ) +struct LevelSpace{S, L} <: AbstractSpace + space::S + level::L end +level(space::CenterExtrudedFiniteDifferenceSpace, v::Integer) = + LevelSpace(space, v) +level(space::FaceExtrudedFiniteDifferenceSpace, v::PlusHalf) = + LevelSpace(space, v) -Base.@propagate_inbounds function level( - space::CenterExtrudedFiniteDifferenceSpace, - v::Integer, +function local_geometry_data( + levelspace::LevelSpace{<:CenterExtrudedFiniteDifferenceSpace, <:Integer}, ) - horizontal_space = space.horizontal_space - if horizontal_space isa SpectralElementSpace1D - SpectralElementSpace1D( - horizontal_space.topology, - horizontal_space.quadrature_style, - horizontal_space.global_geometry, - level(space.center_local_geometry, v), - horizontal_space.dss_weights, - ) - elseif horizontal_space isa SpectralElementSpace2D - SpectralElementSpace2D( - horizontal_space.topology, - horizontal_space.quadrature_style, - horizontal_space.global_geometry, - level(space.center_local_geometry, v), - level(space.center_ghost_geometry, v), - horizontal_space.local_dss_weights, - horizontal_space.ghost_dss_weights, - horizontal_space.internal_surface_geometry, - horizontal_space.boundary_surface_geometries, - ) - else - error("Unsupported horizontal space") - end + level(local_geometry_data(levelspace.space), levelspace.level) end -Base.@propagate_inbounds function level( - space::FaceExtrudedFiniteDifferenceSpace, - v::PlusHalf, +function local_geometry_data( + levelspace::LevelSpace{<:FaceExtrudedFiniteDifferenceSpace, <:PlusHalf}, ) - horizontal_space = space.horizontal_space - if horizontal_space isa SpectralElementSpace1D - @inbounds SpectralElementSpace1D( - horizontal_space.topology, - horizontal_space.quadrature_style, - horizontal_space.global_geometry, - level(space.face_local_geometry, v.i + 1), - horizontal_space.dss_weights, - ) - elseif horizontal_space isa SpectralElementSpace2D - @inbounds SpectralElementSpace2D( - horizontal_space.topology, - horizontal_space.quadrature_style, - horizontal_space.global_geometry, - level(space.face_local_geometry, v.i + 1), - level(space.face_ghost_geometry, v.i + 1), - horizontal_space.local_dss_weights, - horizontal_space.ghost_dss_weights, - horizontal_space.internal_surface_geometry, - horizontal_space.boundary_surface_geometries, - ) - else - error("Unsupported horizontal space") - end + level(local_geometry_data(levelspace.space), levelspace.level + half) end +function column(levelspace::LevelSpace, args...) + local_geometry = column(local_geometry_data(levelspace), args...) + PointSpace(local_geometry) +end + + nlevels(space::CenterExtrudedFiniteDifferenceSpace) = size(space.center_local_geometry, 4) diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 2d7212befb..60452fe33b 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -166,7 +166,7 @@ const IntervalSpectralElementSpace1D = SpectralElementSpace1D{ A two-dimensional space: within each element the space is represented as a polynomial. """ -struct SpectralElementSpace2D{ +mutable struct SpectralElementSpace2D{ T, Q, GG <: Geometry.AbstractGlobalGeometry, @@ -233,7 +233,7 @@ where ``\\tilde{A}^e`` is the approximated area given by the sum of the interior Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSphere`](@ref) and [`Meshes.EquidistantCubedSphere`](@ref) type, not for [`Meshes.ConformalCubedSphere`](@ref). """ -function SpectralElementSpace2D( +@memoize WeakValueDict function SpectralElementSpace2D( topology, quadrature_style; enable_bubble = false, From deb27b670268b1717da81b95735c67259d30e7e3 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 3 Mar 2023 17:18:13 -0800 Subject: [PATCH 02/42] memoize Topology2D --- src/Topologies/Topologies.jl | 2 ++ src/Topologies/topology2d.jl | 4 ++-- test/Topologies/rectangle.jl | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Topologies/Topologies.jl b/src/Topologies/Topologies.jl index 958470637f..31ae65e3f5 100644 --- a/src/Topologies/Topologies.jl +++ b/src/Topologies/Topologies.jl @@ -9,6 +9,8 @@ import ..Geometry import ..Domains: Domains, coordinate_type import ..Meshes: Meshes, domain, coordinates +using Memoize, WeakValueDicts + """ AbstractTopology diff --git a/src/Topologies/topology2d.jl b/src/Topologies/topology2d.jl index 3f858abce6..d74285cfb0 100644 --- a/src/Topologies/topology2d.jl +++ b/src/Topologies/topology2d.jl @@ -23,7 +23,7 @@ Internally, we can refer to elements in several different ways: - `ridx`: "receive index": an index into the receive buffer of a ghost element. - `recv_elem_gidx[ridx] == gidx` """ -struct Topology2D{ +mutable struct Topology2D{ C <: ClimaComms.AbstractCommsContext, M <: Meshes.AbstractMesh{2}, EO, @@ -194,7 +194,7 @@ function simple_partition(nelems::Int, npart::Int) return partition, ranges end -function Topology2D( +@memoize WeakValueDict function Topology2D( context::ClimaComms.AbstractCommsContext, mesh::Meshes.AbstractMesh{2}, elemorder = Meshes.elements(mesh), diff --git a/test/Topologies/rectangle.jl b/test/Topologies/rectangle.jl index 6918c925f2..a210951ca4 100644 --- a/test/Topologies/rectangle.jl +++ b/test/Topologies/rectangle.jl @@ -311,3 +311,11 @@ end @test getfield(c2, 2) == 0.0 end end + +@testset "memoization" begin + topology1 = rectangular_grid(3, 3, true, true) + topology2 = rectangular_grid(3, 3, true, true) + topology3 = rectangular_grid(3, 3, true, false) + @test topology1 === topology2 + @test topology1 !== topology3 +end From 10ce54605c3be08b44cdfc7e18ec20dfbc540374 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 6 Mar 2023 17:55:18 -0800 Subject: [PATCH 03/42] FD WIP --- src/Spaces/extruded.jl | 95 ++++++++++++++++++++++------------ src/Spaces/finitedifference.jl | 61 ++++++++++------------ 2 files changed, 87 insertions(+), 69 deletions(-) diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 5580fec58b..4ea312197a 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -12,8 +12,7 @@ No surface hypsography. struct Flat <: HypsographyAdaption end -struct ExtrudedFiniteDifferenceSpace{ - S <: Staggering, +mutable struct CenterExtrudedFiniteDifferenceSpace{ H <: AbstractSpace, T <: Topologies.AbstractIntervalTopology, A <: HypsographyAdaption, @@ -21,7 +20,6 @@ struct ExtrudedFiniteDifferenceSpace{ LG, LGG, } <: AbstractSpace - staggering::S horizontal_space::H vertical_topology::T hypsography::A @@ -65,33 +63,32 @@ const CenterExtrudedFiniteDifferenceSpace = ExtrudedFiniteDifferenceSpace{CellCenter} const FaceExtrudedFiniteDifferenceSpace = - ExtrudedFiniteDifferenceSpace{CellFace} + FaceSpace{<:CenterExtrudedFiniteDifferenceSpace} + +const ExtrudedFiniteDifferenceSpace = Union{ + CenterExtrudedFiniteDifferenceSpace, + FaceExtrudedFiniteDifferenceSpace, +} -const FaceExtrudedFiniteDifferenceSpace2D = - ExtrudedFiniteDifferenceSpace{CellFace, <:SpectralElementSpace1D} -const FaceExtrudedFiniteDifferenceSpace3D = - ExtrudedFiniteDifferenceSpace{CellFace, <:SpectralElementSpace2D} const CenterExtrudedFiniteDifferenceSpace2D = - ExtrudedFiniteDifferenceSpace{CellCenter, <:SpectralElementSpace1D} + ExtrudedFiniteDifferenceSpace{<:SpectralElementSpace1D} const CenterExtrudedFiniteDifferenceSpace3D = - ExtrudedFiniteDifferenceSpace{CellCenter, <:SpectralElementSpace2D} - + ExtrudedFiniteDifferenceSpace{<:SpectralElementSpace2D} +const FaceExtrudedFiniteDifferenceSpace2D = + FaceSpace{<:CenterExtrudedFiniteDifferenceSpace2D} +const FaceExtrudedFiniteDifferenceSpace3D = + FaceSpace{<:CenterExtrudedFiniteDifferenceSpace3D} + +CenterExtrudedFiniteDifferenceSpace( + space::CenterExtrudedFiniteDifferenceSpace, +) = space +CenterExtrudedFiniteDifferenceSpace(space::FaceExtrudedFiniteDifferenceSpace) = + space.center_space +FaceExtrudedFiniteDifferenceSpace(space::CenterExtrudedFiniteDifferenceSpace) = + FaceSpace(space) +FaceExtrudedFiniteDifferenceSpace(space::FaceExtrudedFiniteDifferenceSpace) = + space -function ExtrudedFiniteDifferenceSpace{S}( - space::ExtrudedFiniteDifferenceSpace, -) where {S <: Staggering} - ExtrudedFiniteDifferenceSpace( - S(), - space.horizontal_space, - space.vertical_topology, - space.hypsography, - space.global_geometry, - space.center_local_geometry, - space.face_local_geometry, - space.center_ghost_geometry, - space.face_ghost_geometry, - ) -end function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) @@ -125,19 +122,41 @@ local_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = space.center_local_geometry local_geometry_data(space::FaceExtrudedFiniteDifferenceSpace) = - space.face_local_geometry + space.center_space.face_local_geometry # TODO: will need to be defined for distributed ghost_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = space.center_ghost_geometry ghost_geometry_data(space::FaceExtrudedFiniteDifferenceSpace) = - space.face_ghost_geometry -function ExtrudedFiniteDifferenceSpace( + space.center_space.face_ghost_geometry + + +ExtrudedFiniteDifferenceSpace( + horizontal_space::AbstractSpace, + vertical_space::CenterFiniteDifferenceSpace, + hypsography::Flat = Flat(), +) = CenterExtrudedFiniteDifferenceSpace( + horizontal_space, + vertical_space, + hypsography, +) +ExtrudedFiniteDifferenceSpace( + horizontal_space::AbstractSpace, + vertical_space::FaceFiniteDifferenceSpace, + hypsography::Flat = Flat(), +) = FaceSpace( + CenterExtrudedFiniteDifferenceSpace( + horizontal_space, + vertical_space, + hypsography, + ), +) + +function CenterExtrudedFiniteDifferenceSpace( horizontal_space::H, vertical_space::V, hypsography::Flat = Flat(), ) where {H <: AbstractSpace, V <: FiniteDifferenceSpace} - staggering = vertical_space.staggering vertical_topology = vertical_space.topology global_geometry = horizontal_space.global_geometry center_local_geometry = @@ -166,8 +185,7 @@ function ExtrudedFiniteDifferenceSpace( center_ghost_geometry = nothing face_ghost_geometry = nothing end - return ExtrudedFiniteDifferenceSpace( - staggering, + return CenterExtrudedFiniteDifferenceSpace( horizontal_space, vertical_topology, hypsography, @@ -179,14 +197,23 @@ function ExtrudedFiniteDifferenceSpace( ) end -quadrature_style(space::ExtrudedFiniteDifferenceSpace) = +quadrature_style(space::CenterExtrudedFiniteDifferenceSpace) = space.horizontal_space.quadrature_style +quadrature_style(space::FaceExtrudedFiniteDifferenceSpace) = + quadrature_style(space.center_space) + +topology(space::CenterExtrudedFiniteDifferenceSpace) = + space.horizontal_space.topology +topology(space::FaceExtrudedFiniteDifferenceSpace) = + topology(space.center_space) topology(space::ExtrudedFiniteDifferenceSpace) = space.horizontal_space.topology ClimaComms.device(space::ExtrudedFiniteDifferenceSpace) = ClimaComms.device(topology(space)) -vertical_topology(space::ExtrudedFiniteDifferenceSpace) = +vertical_topology(space::CenterExtrudedFiniteDifferenceSpace) = space.vertical_topology +vertical_topology(space::FaceExtrudedFiniteDifferenceSpace) = + vertical_topology(space.center_space) Base.@propagate_inbounds function slab( space::ExtrudedFiniteDifferenceSpace, diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 38302bcba6..6d0232e836 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -1,26 +1,25 @@ abstract type AbstractFiniteDifferenceSpace <: AbstractSpace end -abstract type Staggering end - -""" Cell center location """ -struct CellCenter <: Staggering end - -""" Cell face location """ -struct CellFace <: Staggering end - -struct FiniteDifferenceSpace{ - S <: Staggering, +mutable struct CenterFiniteDifferenceSpace{ T <: Topologies.AbstractIntervalTopology, GG, LG, } <: AbstractFiniteDifferenceSpace - staggering::S topology::T global_geometry::GG center_local_geometry::LG face_local_geometry::LG end +struct FaceSpace{C <: AbstractSpace} + center_space::C +end + +const FaceFiniteDifferenceSpace = FaceSpace{C <: CenterFiniteDifferenceSpace} + +const FiniteDifferenceSpace = + Union{CenterFiniteDifferenceSpace, FaceFiniteDifferenceSpace} + function Base.show(io::IO, space::FiniteDifferenceSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) @@ -36,9 +35,9 @@ function Base.show(io::IO, space::FiniteDifferenceSpace) print(iio, " "^(indent + 2), "mesh: ", space.topology.mesh) end -function FiniteDifferenceSpace{S}( +@memoize WeakValueDict function CenterFiniteDifferenceSpace( topology::Topologies.IntervalTopology, -) where {S <: Staggering} +) global_geometry = Geometry.CartesianGlobalGeometry() mesh = topology.mesh CT = Meshes.coordinate_type(mesh) @@ -118,8 +117,7 @@ function FiniteDifferenceSpace{S}( ), ) end - return FiniteDifferenceSpace( - S(), + return CenterFiniteDifferenceSpace( topology, global_geometry, Adapt.adapt(ArrayType, center_local_geometry), @@ -127,8 +125,14 @@ function FiniteDifferenceSpace{S}( ) end -FiniteDifferenceSpace{S}(mesh::Meshes.IntervalMesh) where {S <: Staggering} = - FiniteDifferenceSpace{S}(Topologies.IntervalTopology(mesh)) +FaceFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = + FaceFiniteDifferenceSpace(CenterFiniteDifferenceSpace(topology)) + +CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = + CenterFiniteDifferenceSpace(Topologies.IntervalTopology(mesh)) +FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = + FaceFiniteDifferenceSpace(Topologies.IntervalTopology(mesh)) + ClimaComms.device(space::FiniteDifferenceSpace) = ClimaComms.device(space.topology) @@ -141,32 +145,19 @@ Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = FiniteDifferenceSpace( Adapt.adapt(to, space.face_local_geometry), ) -const CenterFiniteDifferenceSpace = FiniteDifferenceSpace{CellCenter} -const FaceFiniteDifferenceSpace = FiniteDifferenceSpace{CellFace} - -function FiniteDifferenceSpace{S}( - space::FiniteDifferenceSpace, -) where {S <: Staggering} - FiniteDifferenceSpace( - S(), - space.topology, - space.global_geometry, - space.center_local_geometry, - space.face_local_geometry, - ) -end - +CenterFiniteDifferenceSpace(face_space::FaceFiniteDifferenceSpace) = + face_space.center_space Base.length(space::FiniteDifferenceSpace) = length(coordinates_data(space)) topology(space::FiniteDifferenceSpace) = space.topology vertical_topology(space::FiniteDifferenceSpace) = space.topology nlevels(space::FiniteDifferenceSpace) = length(space) -local_geometry_data(space::CenterFiniteDifferenceSpace) = +local_geometry_data(center_space::CenterFiniteDifferenceSpace) = space.center_local_geometry -local_geometry_data(space::FaceFiniteDifferenceSpace) = - space.face_local_geometry +local_geometry_data(face_space::FaceFiniteDifferenceSpace) = + face_space.center_space.face_local_geometry Base.@deprecate z_component(::Type{T}) where {T} Δz_metric_component(T) false From da4bfa8b88e77125bdbc02918304ed5b0cf2c717 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Thu, 21 Sep 2023 16:37:37 -0700 Subject: [PATCH 04/42] fix type --- src/Spaces/finitedifference.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 6d0232e836..3a8c0206bb 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -15,7 +15,8 @@ struct FaceSpace{C <: AbstractSpace} center_space::C end -const FaceFiniteDifferenceSpace = FaceSpace{C <: CenterFiniteDifferenceSpace} +const FaceFiniteDifferenceSpace = + FaceSpace{C} where {C <: CenterFiniteDifferenceSpace} const FiniteDifferenceSpace = Union{CenterFiniteDifferenceSpace, FaceFiniteDifferenceSpace} From ccbb4ecca89553c73ac315545b76a42544835274 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 27 Sep 2023 15:59:20 -0700 Subject: [PATCH 05/42] more work --- src/Fields/Fields.jl | 13 +-- src/Fields/indices.jl | 20 +--- src/Hypsography/Hypsography.jl | 2 +- src/InputOutput/readers.jl | 2 - src/InputOutput/writers.jl | 2 +- src/MatrixFields/matrix_shape.jl | 4 +- src/Operators/finitedifference.jl | 73 +++++---------- src/Operators/spectralelement.jl | 4 +- src/Spaces/Spaces.jl | 9 +- src/Spaces/extruded.jl | 147 ++++++++++++++++++++++-------- src/Spaces/finitedifference.jl | 35 ++++--- test/Operators/hybrid/2d.jl | 8 +- test/Operators/hybrid/3d.jl | 6 +- test/runtests.jl | 3 +- 14 files changed, 182 insertions(+), 146 deletions(-) diff --git a/src/Fields/Fields.jl b/src/Fields/Fields.jl index ac10b1a2e6..9059347add 100644 --- a/src/Fields/Fields.jl +++ b/src/Fields/Fields.jl @@ -5,7 +5,7 @@ import ..slab, ..slab_args, ..column, ..column_args, ..level import ..DataLayouts: DataLayouts, AbstractData, DataStyle import ..Domains import ..Topologies -import ..Spaces: Spaces, AbstractSpace, AbstractPointSpace +import ..Spaces: Spaces, AbstractSpace, AbstractPointSpace, ColumnIndex import ..Geometry: Geometry, Cartesian12Vector import ..Utilities: PlusHalf, half @@ -38,13 +38,6 @@ Field(::Type{T}, space::S) where {T, S <: AbstractSpace} = ClimaComms.context(field::Field) = ClimaComms.context(axes(field)) -ClimaComms.context(space::Spaces.ExtrudedFiniteDifferenceSpace) = - ClimaComms.context(space.horizontal_space) -ClimaComms.context(space::Spaces.SpectralElementSpace2D) = - ClimaComms.context(space.topology) -ClimaComms.context(space::S) where {S <: Spaces.AbstractSpace} = - ClimaComms.context(space.topology) - ClimaComms.context(topology::Topologies.Topology2D) = topology.context ClimaComms.context(topology::T) where {T <: Topologies.AbstractTopology} = topology.context @@ -403,9 +396,7 @@ Create a buffer for communicating neighbour information of `field`. """ function Spaces.create_dss_buffer(field::Field) space = axes(field) - hspace = - space isa Spaces.ExtrudedFiniteDifferenceSpace ? - space.horizontal_space : space + hspace = Spaces.horizontal_space(space) Spaces.create_dss_buffer(field_values(field), hspace) end # Add definitions for backward compatibility diff --git a/src/Fields/indices.jl b/src/Fields/indices.jl index 92bfcc2b5f..4fa0c3ed58 100644 --- a/src/Fields/indices.jl +++ b/src/Fields/indices.jl @@ -1,19 +1,3 @@ -""" - ColumnIndex(ij,h) - -An index into a column of a field. This can be used as an argument to `getindex` -of a `Field`, to return a field on that column. - -# Example -```julia -colidx = ColumnIndex((1,1),1) -field[colidx] -``` -""" -struct ColumnIndex{N} - ij::NTuple{N, Int} - h::Int -end Base.@propagate_inbounds Base.getindex(field::Field, colidx::ColumnIndex) = column(field, colidx) @@ -145,7 +129,7 @@ bycolumn( fn, space::Spaces.ExtrudedFiniteDifferenceSpace, device::ClimaComms.AbstractCPUDevice, -) = bycolumn(fn, space.horizontal_space, device) +) = bycolumn(fn, Spaces.horizontal_space(space), device) function bycolumn(fn, space::AbstractSpace, ::ClimaComms.CUDADevice) @@ -164,7 +148,7 @@ Number of columns in a given space. ncolumns(field::Field) = ncolumns(axes(field)) ncolumns(space::Spaces.ExtrudedFiniteDifferenceSpace) = - ncolumns(space.horizontal_space) + ncolumns(Spaces.horizontal_space(space)) function ncolumns(space::Spaces.SpectralElementSpace1D) Nh = Topologies.nlocalelems(space) diff --git a/src/Hypsography/Hypsography.jl b/src/Hypsography/Hypsography.jl index 78603a00da..a1a788dd99 100644 --- a/src/Hypsography/Hypsography.jl +++ b/src/Hypsography/Hypsography.jl @@ -185,7 +185,7 @@ function ExtrudedFiniteDifferenceSpace( return Spaces.ExtrudedFiniteDifferenceSpace( space.staggering, - space.horizontal_space, + Spaces.horizontal_space(space), space.vertical_topology, adaption, space.global_geometry, diff --git a/src/InputOutput/readers.jl b/src/InputOutput/readers.jl index 6801cfb29c..fae681e17b 100644 --- a/src/InputOutput/readers.jl +++ b/src/InputOutput/readers.jl @@ -16,8 +16,6 @@ using ..Spaces: Spaces, Spaces.Quadratures, Spaces.Quadratures.GLL, - Spaces.CellCenter, - Spaces.CellFace, SpectralElementSpace1D, SpectralElementSpace2D, CenterExtrudedFiniteDifferenceSpace, diff --git a/src/InputOutput/writers.jl b/src/InputOutput/writers.jl index ac257d08d5..379ef5a095 100644 --- a/src/InputOutput/writers.jl +++ b/src/InputOutput/writers.jl @@ -366,7 +366,7 @@ function write_new!( write_attribute( group, "horizontal_space", - write!(writer, space.horizontal_space), + write!(writer, Spaces.horizontal_space(space)), ) write_attribute( group, diff --git a/src/MatrixFields/matrix_shape.jl b/src/MatrixFields/matrix_shape.jl index 6ebe32a1c8..64ae442c46 100644 --- a/src/MatrixFields/matrix_shape.jl +++ b/src/MatrixFields/matrix_shape.jl @@ -17,8 +17,8 @@ matrix_shape(matrix_field, matrix_space = axes(matrix_field)) = _matrix_shape( ) _matrix_shape(::Type{Int}, _) = Square() -_matrix_shape(::Type{PlusHalf{Int}}, ::Spaces.CellCenter) = FaceToCenter() -_matrix_shape(::Type{PlusHalf{Int}}, ::Spaces.CellFace) = CenterToFace() +#_matrix_shape(::Type{PlusHalf{Int}}, ::Spaces.CellCenter) = FaceToCenter() +#_matrix_shape(::Type{PlusHalf{Int}}, ::Spaces.CellFace) = CenterToFace() """ column_axes(matrix_field, [matrix_space]) diff --git a/src/Operators/finitedifference.jl b/src/Operators/finitedifference.jl index 31d3d6edc1..be03f28628 100644 --- a/src/Operators/finitedifference.jl +++ b/src/Operators/finitedifference.jl @@ -2,35 +2,35 @@ import ..Utilities: PlusHalf, half left_idx( space::Union{ - Spaces.CenterFiniteDifferenceSpace, + Spaces.AbstractFiniteDifferenceSpace, Spaces.CenterExtrudedFiniteDifferenceSpace, }, ) = left_center_boundary_idx(space) right_idx( space::Union{ - Spaces.CenterFiniteDifferenceSpace, + Spaces.AbstractFiniteDifferenceSpace, Spaces.CenterExtrudedFiniteDifferenceSpace, }, ) = right_center_boundary_idx(space) left_idx( space::Union{ - Spaces.FaceFiniteDifferenceSpace, + Spaces.FaceSpace{<:Spaces.AbstractFiniteDifferenceSpace}, Spaces.FaceExtrudedFiniteDifferenceSpace, }, ) = left_face_boundary_idx(space) right_idx( space::Union{ - Spaces.FaceFiniteDifferenceSpace, + Spaces.FaceSpace{<:Spaces.AbstractFiniteDifferenceSpace}, Spaces.FaceExtrudedFiniteDifferenceSpace, }, ) = right_face_boundary_idx(space) left_center_boundary_idx(space::Spaces.AbstractSpace) = 1 right_center_boundary_idx(space::Spaces.AbstractSpace) = - size(space.center_local_geometry, 4) + size(Spaces.local_geometry_data(Spaces.center_space(space)), 4) left_face_boundary_idx(space::Spaces.AbstractSpace) = half right_face_boundary_idx(space::Spaces.AbstractSpace) = - size(space.face_local_geometry, 4) - half + size(Spaces.local_geometry_data(Spaces.face_space(space)), 4) - half left_face_boundary_idx(arg) = left_face_boundary_idx(axes(arg)) @@ -52,7 +52,7 @@ Base.@propagate_inbounds function Geometry.LocalGeometry( v = mod1(v, length(space)) end i, j, h = hidx - return @inbounds space.center_local_geometry[CartesianIndex(i, j, 1, v, h)] + return @inbounds Spaces.center_space(space).center_local_geometry[CartesianIndex(i, j, 1, v, h)] end Base.@propagate_inbounds function Geometry.LocalGeometry( space::Union{ @@ -67,7 +67,7 @@ Base.@propagate_inbounds function Geometry.LocalGeometry( v = mod1(v, length(space)) end i, j, h = hidx - return @inbounds space.face_local_geometry[CartesianIndex(i, j, 1, v, h)] + return @inbounds Spaces.center_space(space).face_local_geometry[CartesianIndex(i, j, 1, v, h)] end @@ -349,12 +349,8 @@ struct InterpolateF2C{BCS} <: InterpolationOperator end InterpolateF2C(; kwargs...) = InterpolateF2C(NamedTuple(kwargs)) -return_space(::InterpolateF2C, space::Spaces.FaceFiniteDifferenceSpace) = - Spaces.CenterFiniteDifferenceSpace(space) -return_space( - ::InterpolateF2C, - space::Spaces.FaceExtrudedFiniteDifferenceSpace, -) = Spaces.CenterExtrudedFiniteDifferenceSpace(space) +return_space(::InterpolateF2C, space::Spaces.FaceSpace) = + Spaces.center_space(space) stencil_interior_width(::InterpolateF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -406,11 +402,11 @@ end InterpolateC2F(; kwargs...) = InterpolateC2F(NamedTuple(kwargs)) return_space(::InterpolateC2F, space::Spaces.CenterFiniteDifferenceSpace) = - Spaces.FaceFiniteDifferenceSpace(space) + Spaces.face_space(space) return_space( ::InterpolateC2F, space::Spaces.CenterExtrudedFiniteDifferenceSpace, -) = Spaces.FaceExtrudedFiniteDifferenceSpace(space) +) = Spaces.face_space(space) stencil_interior_width(::InterpolateC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -597,10 +593,8 @@ struct LeftBiasedF2C{BCS} <: InterpolationOperator end LeftBiasedF2C(; kwargs...) = LeftBiasedF2C(NamedTuple(kwargs)) -return_space(::LeftBiasedF2C, space::Spaces.FaceFiniteDifferenceSpace) = - Spaces.CenterFiniteDifferenceSpace(space) -return_space(::LeftBiasedF2C, space::Spaces.FaceExtrudedFiniteDifferenceSpace) = - Spaces.CenterExtrudedFiniteDifferenceSpace(space) +return_space(::LeftBiasedF2C, space::Spaces.FaceSpace) = + Spaces.center_space(space) stencil_interior_width(::LeftBiasedF2C, arg) = ((-half, -half),) Base.@propagate_inbounds stencil_interior( @@ -729,12 +723,8 @@ struct LeftBiased3rdOrderF2C{BCS} <: InterpolationOperator end LeftBiased3rdOrderF2C(; kwargs...) = LeftBiased3rdOrderF2C(NamedTuple(kwargs)) -return_space(::LeftBiased3rdOrderF2C, space::Spaces.FaceFiniteDifferenceSpace) = - Spaces.CenterFiniteDifferenceSpace(space) -return_space( - ::LeftBiased3rdOrderF2C, - space::Spaces.FaceExtrudedFiniteDifferenceSpace, -) = Spaces.CenterExtrudedFiniteDifferenceSpace(space) +return_space(::LeftBiased3rdOrderF2C, space::Spaces.FaceSpace) = + Spaces.center_space(space) stencil_interior_width(::LeftBiased3rdOrderF2C, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -860,12 +850,8 @@ struct RightBiasedF2C{BCS} <: InterpolationOperator end RightBiasedF2C(; kwargs...) = RightBiasedF2C(NamedTuple(kwargs)) -return_space(::RightBiasedF2C, space::Spaces.FaceFiniteDifferenceSpace) = - Spaces.CenterFiniteDifferenceSpace(space) -return_space( - ::RightBiasedF2C, - space::Spaces.FaceExtrudedFiniteDifferenceSpace, -) = Spaces.CenterExtrudedFiniteDifferenceSpace(space) +return_space(::RightBiasedF2C, space::Spaces.FaceSpace) = + Spaces.center_space(space) stencil_interior_width(::RightBiasedF2C, arg) = ((half, half),) Base.@propagate_inbounds stencil_interior( @@ -1051,14 +1037,9 @@ WeightedInterpolateF2C(; kwargs...) = WeightedInterpolateF2C(NamedTuple(kwargs)) return_space( ::WeightedInterpolateF2C, - weight_space::Spaces.FaceFiniteDifferenceSpace, - arg_space::Spaces.FaceFiniteDifferenceSpace, -) = Spaces.CenterFiniteDifferenceSpace(arg_space) -return_space( - ::WeightedInterpolateF2C, - weight_space::Spaces.FaceExtrudedFiniteDifferenceSpace, - arg_space::Spaces.FaceExtrudedFiniteDifferenceSpace, -) = Spaces.CenterExtrudedFiniteDifferenceSpace(arg_space) + weight_space::Spaces.FaceSpace, + arg_space::Spaces.FaceSpace, +) = Spaces.center_space(arg_space) stencil_interior_width(::WeightedInterpolateF2C, weight, arg) = ((-half, half), (-half, half)) @@ -2352,10 +2333,8 @@ struct GradientF2C{BCS} <: GradientOperator end GradientF2C(; kwargs...) = GradientF2C(NamedTuple(kwargs)) -return_space(::GradientF2C, space::Spaces.FaceFiniteDifferenceSpace) = - Spaces.CenterFiniteDifferenceSpace(space) -return_space(::GradientF2C, space::Spaces.FaceExtrudedFiniteDifferenceSpace) = - Spaces.CenterExtrudedFiniteDifferenceSpace(space) +return_space(::GradientF2C, space::Spaces.FaceSpace) = + Spaces.center_space(space) stencil_interior_width(::GradientF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -3177,10 +3156,7 @@ Base.Broadcast.BroadcastStyle( Base.eltype(bc::StencilBroadcasted) = return_eltype(bc.op, bc.args...) function vidx( - space::Union{ - Spaces.FaceFiniteDifferenceSpace, - Spaces.FaceExtrudedFiniteDifferenceSpace, - }, + space::Spaces.FaceSpace, idx, ) @assert idx isa PlusHalf @@ -3194,6 +3170,7 @@ function vidx( space::Union{ Spaces.CenterFiniteDifferenceSpace, Spaces.CenterExtrudedFiniteDifferenceSpace, + Spaces.CenterColumnSpace, }, idx, ) diff --git a/src/Operators/spectralelement.jl b/src/Operators/spectralelement.jl index d0e953c06c..c9b926e849 100644 --- a/src/Operators/spectralelement.jl +++ b/src/Operators/spectralelement.jl @@ -56,7 +56,7 @@ operator_axes(space::Spaces.SpectralElementSpace2D) = (1, 2) operator_axes(space::Spaces.SpectralElementSpaceSlab1D) = (1,) operator_axes(space::Spaces.SpectralElementSpaceSlab2D) = (1, 2) operator_axes(space::Spaces.ExtrudedFiniteDifferenceSpace) = - operator_axes(space.horizontal_space) + operator_axes(Spaces.horizontal_space(space)) function node_indices(space::Spaces.SpectralElementSpace1D) @@ -70,7 +70,7 @@ function node_indices(space::Spaces.SpectralElementSpace2D) CartesianIndices((Nq, Nq)) end node_indices(space::Spaces.ExtrudedFiniteDifferenceSpace) = - node_indices(space.horizontal_space) + node_indices(Spaces.horizontal_space(space)) """ diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index 3bfd230d4d..01b0bc1f4c 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -34,6 +34,10 @@ undertype(space::AbstractSpace) = coordinates_data(space::AbstractSpace) = local_geometry_data(space).coordinates + +ClimaComms.context(space::Spaces.AbstractSpace) = + ClimaComms.context(Spaces.topology(space)) + include("quadrature.jl") import .Quadratures @@ -45,7 +49,10 @@ include("triangulation.jl") include("dss_transform.jl") include("dss.jl") -horizontal_space(space::ExtrudedFiniteDifferenceSpace) = space.horizontal_space +horizontal_space(space::CenterExtrudedFiniteDifferenceSpace) = + space.horizontal_space +horizontal_space(space::FaceExtrudedFiniteDifferenceSpace) = + horizontal_space(center_space(space)) horizontal_space(space::AbstractSpace) = space weighted_jacobian(space::Spaces.AbstractSpace) = local_geometry_data(space).WJ diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 4ea312197a..0c259ddefe 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -30,25 +30,39 @@ mutable struct CenterExtrudedFiniteDifferenceSpace{ face_ghost_geometry::LGG end +face_space(space::CenterExtrudedFiniteDifferenceSpace) = + FaceSpace(space) +center_space(space::CenterExtrudedFiniteDifferenceSpace) = + space + + +const FaceExtrudedFiniteDifferenceSpace = + FaceSpace{<:CenterExtrudedFiniteDifferenceSpace} + +const ExtrudedFiniteDifferenceSpace = Union{ + CenterExtrudedFiniteDifferenceSpace, + FaceExtrudedFiniteDifferenceSpace, +} + function issubspace( hspace::AbstractSpectralElementSpace, extruded_space::ExtrudedFiniteDifferenceSpace, ) - if hspace === extruded_space.horizontal_space + if hspace === extruded_Spaces.horizontal_space(space) return true end # TODO: improve level handling return Spaces.topology(hspace) === - Spaces.topology(extruded_space.horizontal_space) && + Spaces.topology(Spaces.horizontal_space(extrued_space)) && quadrature_style(hspace) === - quadrature_style(extruded_space.horizontal_space) + quadrature_style(Spaces.horizontal_space(extrued_space)) end Adapt.adapt_structure(to, space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace( space.staggering, - Adapt.adapt(to, space.horizontal_space), + Adapt.adapt(to, Spaces.horizontal_space(space)), Adapt.adapt(to, space.vertical_topology), Adapt.adapt(to, space.hypsography), Adapt.adapt(to, space.global_geometry), @@ -59,21 +73,11 @@ Adapt.adapt_structure(to, space::ExtrudedFiniteDifferenceSpace) = ) -const CenterExtrudedFiniteDifferenceSpace = - ExtrudedFiniteDifferenceSpace{CellCenter} - -const FaceExtrudedFiniteDifferenceSpace = - FaceSpace{<:CenterExtrudedFiniteDifferenceSpace} - -const ExtrudedFiniteDifferenceSpace = Union{ - CenterExtrudedFiniteDifferenceSpace, - FaceExtrudedFiniteDifferenceSpace, -} const CenterExtrudedFiniteDifferenceSpace2D = - ExtrudedFiniteDifferenceSpace{<:SpectralElementSpace1D} + CenterExtrudedFiniteDifferenceSpace{<:SpectralElementSpace1D} const CenterExtrudedFiniteDifferenceSpace3D = - ExtrudedFiniteDifferenceSpace{<:SpectralElementSpace2D} + CenterExtrudedFiniteDifferenceSpace{<:SpectralElementSpace2D} const FaceExtrudedFiniteDifferenceSpace2D = FaceSpace{<:CenterExtrudedFiniteDifferenceSpace2D} const FaceExtrudedFiniteDifferenceSpace3D = @@ -89,7 +93,7 @@ FaceExtrudedFiniteDifferenceSpace(space::CenterExtrudedFiniteDifferenceSpace) = FaceExtrudedFiniteDifferenceSpace(space::FaceExtrudedFiniteDifferenceSpace) = space -function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) +function Base.show(io::IO, space::CenterExtrudedFiniteDifferenceSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) println( @@ -122,7 +126,7 @@ local_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = space.center_local_geometry local_geometry_data(space::FaceExtrudedFiniteDifferenceSpace) = - space.center_space.face_local_geometry + center_space(space).face_local_geometry # TODO: will need to be defined for distributed ghost_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = @@ -147,16 +151,16 @@ ExtrudedFiniteDifferenceSpace( ) = FaceSpace( CenterExtrudedFiniteDifferenceSpace( horizontal_space, - vertical_space, + center_space(vertical_space), hypsography, ), ) -function CenterExtrudedFiniteDifferenceSpace( - horizontal_space::H, - vertical_space::V, +@memoize WeakValueDict function CenterExtrudedFiniteDifferenceSpace( + horizontal_space::AbstractSpace, + vertical_space::CenterFiniteDifferenceSpace, hypsography::Flat = Flat(), -) where {H <: AbstractSpace, V <: FiniteDifferenceSpace} +) vertical_topology = vertical_space.topology global_geometry = horizontal_space.global_geometry center_local_geometry = @@ -207,7 +211,7 @@ topology(space::CenterExtrudedFiniteDifferenceSpace) = topology(space::FaceExtrudedFiniteDifferenceSpace) = topology(space.center_space) -topology(space::ExtrudedFiniteDifferenceSpace) = space.horizontal_space.topology +topology(space::ExtrudedFiniteDifferenceSpace) = topology(horizontal_space(space)) ClimaComms.device(space::ExtrudedFiniteDifferenceSpace) = ClimaComms.device(topology(space)) vertical_topology(space::CenterExtrudedFiniteDifferenceSpace) = @@ -221,25 +225,88 @@ Base.@propagate_inbounds function slab( h, ) SpectralElementSpaceSlab( - space.horizontal_space.quadrature_style, + Spaces.horizontal_space(space).quadrature_style, slab(local_geometry_data(space), v, h), ) end -Base.@propagate_inbounds function column( +""" + ColumnIndex(ij,h) + +An index into a column of a field. This can be used as an argument to `getindex` +of a `Field`, to return a field on that column. + +# Example +```julia +colidx = ColumnIndex((1,1),1) +field[colidx] +``` +""" +struct ColumnIndex{N} + ij::NTuple{N, Int} + h::Int +end + + + +struct CenterColumnSpace{S<:CenterExtrudedFiniteDifferenceSpace, C<:ColumnIndex} <: AbstractFiniteDifferenceSpace + space::S + column::C +end + +const FaceColumnSpace = FaceSpace{<:CenterColumnSpace} + +const ColumnSpace = Union{ + CenterColumnSpace, + FaceColumnSpace, +} + +face_space(space::CenterColumnSpace) = + FaceSpace(space) +center_space(space::CenterColumnSpace) = + space + +full_space(colspace::CenterColumnSpace) = colspace.space +full_space(colspace::FaceColumnSpace) = FaceSpace(center_space(colspace).space) + +column( + space::CenterExtrudedFiniteDifferenceSpace, + colidx::ColumnIndex +) = + CenterColumnSpace(space, colidx) + +column( + space::FaceExtrudedFiniteDifferenceSpace, + colidx::ColumnIndex +) = + FaceSpace(CenterColumnSpace(center_space(space), colidx)) + + +vertical_topology(space::ColumnSpace) = vertical_topology(full_space(space)) + +function local_geometry_data( + columnspace::ColumnSpace, +) + column(local_geometry_data(full_space(columnspace)), center_space(columnspace).column) +end + + + +ClimaComms.device(columnspace::ColumnSpace) = + ClimaComms.device(full_space(columnspace)) +ClimaComms.context(columnspace::ColumnSpace) = + ClimaComms.context(full_space(columnspace)) + + +# TODO: deprecate these +column( space::ExtrudedFiniteDifferenceSpace, i, j, h, -) - FiniteDifferenceSpace( - space.staggering, - space.vertical_topology, - Geometry.CartesianGlobalGeometry(), - column(space.center_local_geometry, i, j, h), - column(space.face_local_geometry, i, j, h), - ) -end +) = + column(space, ColumnIndex((i, j), h)) + struct LevelSpace{S, L} <: AbstractSpace space::S @@ -272,14 +339,14 @@ nlevels(space::CenterExtrudedFiniteDifferenceSpace) = size(space.center_local_geometry, 4) nlevels(space::FaceExtrudedFiniteDifferenceSpace) = - size(space.face_local_geometry, 4) + size(center_space(space).face_local_geometry, 4) function left_boundary_name(space::ExtrudedFiniteDifferenceSpace) - boundaries = Topologies.boundaries(space.vertical_topology) + boundaries = Topologies.boundaries(Spaces.vertical_topology(space)) propertynames(boundaries)[1] end function right_boundary_name(space::ExtrudedFiniteDifferenceSpace) - boundaries = Topologies.boundaries(space.vertical_topology) + boundaries = Topologies.boundaries(Spaces.vertical_topology(space)) propertynames(boundaries)[2] end function blockmat( @@ -368,12 +435,12 @@ function product_geometry( end function eachslabindex(cspace::CenterExtrudedFiniteDifferenceSpace) - h_iter = eachslabindex(cspace.horizontal_space) + h_iter = eachslabindex(cSpaces.horizontal_space(space)) Nv = size(cspace.center_local_geometry, 4) return Iterators.product(1:Nv, h_iter) end function eachslabindex(fspace::FaceExtrudedFiniteDifferenceSpace) - h_iter = eachslabindex(fspace.horizontal_space) + h_iter = eachslabindex(fSpaces.horizontal_space(space)) Nv = size(fspace.face_local_geometry, 4) return Iterators.product(1:Nv, h_iter) end diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 3a8c0206bb..5f0c073313 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -11,16 +11,27 @@ mutable struct CenterFiniteDifferenceSpace{ face_local_geometry::LG end -struct FaceSpace{C <: AbstractSpace} +struct FaceSpace{C <: AbstractSpace} <: AbstractSpace center_space::C end +face_space(space::CenterFiniteDifferenceSpace) = FaceSpace(space) +face_space(space::FaceSpace) = space + +center_space(space::CenterFiniteDifferenceSpace) = space +center_space(space::FaceSpace) = space.center_space + const FaceFiniteDifferenceSpace = FaceSpace{C} where {C <: CenterFiniteDifferenceSpace} const FiniteDifferenceSpace = Union{CenterFiniteDifferenceSpace, FaceFiniteDifferenceSpace} +FaceFiniteDifferenceSpace(space::FiniteDifferenceSpace) = + face_space(space) +CenterFiniteDifferenceSpace(space::FiniteDifferenceSpace) = + center_space(space) + function Base.show(io::IO, space::FiniteDifferenceSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) @@ -127,7 +138,7 @@ end end FaceFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = - FaceFiniteDifferenceSpace(CenterFiniteDifferenceSpace(topology)) + face_space(CenterFiniteDifferenceSpace(topology)) CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = CenterFiniteDifferenceSpace(Topologies.IntervalTopology(mesh)) @@ -136,7 +147,7 @@ FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = ClimaComms.device(space::FiniteDifferenceSpace) = - ClimaComms.device(space.topology) + ClimaComms.device(Spaces.center_space(space).topology) Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = FiniteDifferenceSpace( space.staggering, @@ -150,15 +161,15 @@ CenterFiniteDifferenceSpace(face_space::FaceFiniteDifferenceSpace) = face_space.center_space Base.length(space::FiniteDifferenceSpace) = length(coordinates_data(space)) -topology(space::FiniteDifferenceSpace) = space.topology -vertical_topology(space::FiniteDifferenceSpace) = space.topology +topology(space::FiniteDifferenceSpace) = center_space(space).topology +vertical_topology(space::FiniteDifferenceSpace) = center_space(space).topology nlevels(space::FiniteDifferenceSpace) = length(space) -local_geometry_data(center_space::CenterFiniteDifferenceSpace) = +local_geometry_data(space::CenterFiniteDifferenceSpace) = space.center_local_geometry -local_geometry_data(face_space::FaceFiniteDifferenceSpace) = - face_space.center_space.face_local_geometry +local_geometry_data(space::FaceFiniteDifferenceSpace) = + center_space(space).face_local_geometry Base.@deprecate z_component(::Type{T}) where {T} Δz_metric_component(T) false @@ -192,13 +203,13 @@ function Δz_data(space::AbstractSpace) ) end -function left_boundary_name(space::FiniteDifferenceSpace) - boundaries = Topologies.boundaries(Spaces.topology(space)) +function left_boundary_name(space::AbstractSpace) + boundaries = Topologies.boundaries(Spaces.vertical_topology(space)) propertynames(boundaries)[1] end -function right_boundary_name(space::FiniteDifferenceSpace) - boundaries = Topologies.boundaries(Spaces.topology(space)) +function right_boundary_name(space::AbstractSpace) + boundaries = Topologies.boundaries(Spaces.vertical_topology(space)) propertynames(boundaries)[2] end diff --git a/test/Operators/hybrid/2d.jl b/test/Operators/hybrid/2d.jl index 8ba1647d86..908e7f895a 100644 --- a/test/Operators/hybrid/2d.jl +++ b/test/Operators/hybrid/2d.jl @@ -77,18 +77,18 @@ end ccoord = Fields.coordinate_field(hv_center_space) @test parent(Fields.field_values(level(fcoord.x, half))) == parent( Fields.field_values( - Fields.coordinate_field(hv_face_space.horizontal_space).x, + Fields.coordinate_field(Spaces.horizontal_space(hv_face_space)).x, ), ) @test parent(Fields.field_values(level(ccoord.x, 1))) == parent( Fields.field_values( - Fields.coordinate_field(hv_center_space.horizontal_space).x, + Fields.coordinate_field(Spaces.horizontal_space(hv_center_space)).x, ), ) @test parent(Fields.field_values(level(fcoord.z, half))) == parent( Fields.field_values( - Fields.coordinate_field(hv_face_space.horizontal_space).x, + Fields.coordinate_field(Spaces.horizontal_space(hv_face_space)).x, ), ) .* 0 end @@ -308,7 +308,7 @@ end hv_center_space, hv_face_space = hvspace_2D(xlim = (-1, 1), zlim = (-1, 1), helem = n, velem = n) ccoords = Fields.coordinate_field(hv_center_space) - bcoords = Fields.coordinate_field(hv_center_space.horizontal_space) + bcoords = Fields.coordinate_field(Spaces.horizontal_space(hv_center_space)) Δh[k] = 1.0 / n diff --git a/test/Operators/hybrid/3d.jl b/test/Operators/hybrid/3d.jl index 5dc04f29b6..4aec9588c2 100644 --- a/test/Operators/hybrid/3d.jl +++ b/test/Operators/hybrid/3d.jl @@ -92,13 +92,13 @@ end @test parent(Fields.field_values(level(coord.x, half))) == parent( Fields.field_values( - Fields.coordinate_field(hv_face_space.horizontal_space).x, + Fields.coordinate_field(Spaces.horizontal_space(hv_face_space)).x, ), ) @test parent(Fields.field_values(level(coord.z, half))) == parent( Fields.field_values( - Fields.coordinate_field(hv_face_space.horizontal_space).x, + Fields.coordinate_field(Spaces.horizontal_space(hv_face_space)).x, ), ) .* 0 end @@ -192,7 +192,7 @@ end struct ZeroFieldFlux <: BCtag end function bc_divF2C_bottom!(::ZeroFieldFlux, dY, Y, p, t) - space = axes(Y.h).horizontal_space + space = Spaces.horizontal_space(axes(Y.h)) FT = Spaces.undertype(space) zeroflux = Fields.zeros(FT, space) return Operators.SetValue( diff --git a/test/runtests.jl b/test/runtests.jl index 43b43f52a0..10f9037ccb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,6 +11,7 @@ Stacktrace: [1] pkgerror(msg::String) =# if !Sys.iswindows() + #= @safetestset "Recursive" begin @time include("RecursiveApply/recursive_apply.jl") end @safetestset "PlusHalf" begin @time include("Utilities/plushalf.jl") end @@ -68,7 +69,7 @@ if !Sys.iswindows() # now part of buildkite # @time include("Operators/finitedifference/implicit_stencils.jl") # @time include("Operators/finitedifference/opt_implicit_stencils.jl") - +=# @safetestset "Hybrid - 2D" begin @time include("Operators/hybrid/2d.jl") end @safetestset "Hybrid - 3D" begin @time include("Operators/hybrid/3d.jl") end @safetestset "Hybrid - dss opt" begin @time include("Operators/hybrid/dss_opt.jl") end From 9b5b17851e45fdcee72affd34e83214b1d5e0a99 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Thu, 28 Sep 2023 13:58:40 -0700 Subject: [PATCH 06/42] WIP --- src/Spaces/extruded.jl | 192 ++++++++++++++++----------------- src/Spaces/finitedifference.jl | 145 +++++++++++++++---------- 2 files changed, 183 insertions(+), 154 deletions(-) diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 0c259ddefe..ad38526d81 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -12,16 +12,25 @@ No surface hypsography. struct Flat <: HypsographyAdaption end -mutable struct CenterExtrudedFiniteDifferenceSpace{ +""" + ExtrudedFiniteDifferenceGrid( + horizontal_space::AbstractSpace, + vertical_space::FiniteDifferenceSpace, + hypsography::HypsographyAdaption = Flat(), + ) + +Construct an `ExtrudedFiniteDifferenceGrid` from the horizontal and vertical spaces. +""" +mutable struct ExtrudedFiniteDifferenceGrid{ H <: AbstractSpace, T <: Topologies.AbstractIntervalTopology, A <: HypsographyAdaption, GG <: Geometry.AbstractGlobalGeometry, LG, LGG, -} <: AbstractSpace +} horizontal_space::H - vertical_topology::T + vertical_topology::T # should we cache the vertical grid? hypsography::A global_geometry::GG center_local_geometry::LG @@ -30,20 +39,81 @@ mutable struct CenterExtrudedFiniteDifferenceSpace{ face_ghost_geometry::LGG end -face_space(space::CenterExtrudedFiniteDifferenceSpace) = - FaceSpace(space) -center_space(space::CenterExtrudedFiniteDifferenceSpace) = - space +@memoize WeakValueDict function ExtrudedFiniteDifferenceGrid( + horizontal_space::AbstractSpace, + vertical_grid::FiniteDifferenceGrid, + hypsography::Flat = Flat(), +) + vertical_topology = vertical_grid.topology + global_geometry = horizontal_space.global_geometry + center_local_geometry = + product_geometry.( + horizontal_space.local_geometry, + vertical_space.center_local_geometry, + ) + face_local_geometry = + product_geometry.( + horizontal_space.local_geometry, + vertical_space.face_local_geometry, + ) + + if horizontal_space isa SpectralElementSpace2D + center_ghost_geometry = + product_geometry.( + horizontal_space.ghost_geometry, + vertical_space.center_local_geometry, + ) + face_ghost_geometry = + product_geometry.( + horizontal_space.ghost_geometry, + vertical_space.face_local_geometry, + ) + else + center_ghost_geometry = nothing + face_ghost_geometry = nothing + end + return ExtrudedFiniteDifferenceGrid( + horizontal_space, + vertical_topology, + hypsography, + global_geometry, + center_local_geometry, + face_local_geometry, + center_ghost_geometry, + face_ghost_geometry, + ) +end -const FaceExtrudedFiniteDifferenceSpace = - FaceSpace{<:CenterExtrudedFiniteDifferenceSpace} -const ExtrudedFiniteDifferenceSpace = Union{ - CenterExtrudedFiniteDifferenceSpace, - FaceExtrudedFiniteDifferenceSpace, -} + +struct ExtudedFiniteDifferenceSpace{S<:Staggering,G<:ExtrudedFiniteDifferenceGrid} <: AbstractSpace + space::S + grid::G +end + +const FaceExtrudedFiniteDifferenceSpace = ExtrudedFiniteDifferenceSpace{CellFace} +const CenterExtrudedFiniteDifferenceSpace = ExtrudedFiniteDifferenceSpace{CellCenter} + + +ExtrudedFiniteDifferenceSpace{S}(grid::ExtrudedFiniteDifferenceGrid) where {S<:Staggering}= + ExtrudedFiniteDifferenceSpace(S(), grid) +ExtrudedFiniteDifferenceSpace{S}(space::ExtrudedFiniteDifferenceSpace) where {S<:Staggering}= + ExtrudedFiniteDifferenceSpace{S}(space.grid) + +ExtrudedFiniteDifferenceSpace{S}( + horizontal_space::AbstractSpace, + vertical_space::FiniteDifferenceSpace, + hypsography::HypsographyAdaption = Flat(), +) = ExtrudedFiniteDifferenceSpace{S}( + ExtrudedFiniteDifferenceGrid( + horizontal_space, + vertical_space, + hypsography, + ) +) + function issubspace( hspace::AbstractSpectralElementSpace, extruded_space::ExtrudedFiniteDifferenceSpace, @@ -79,21 +149,11 @@ const CenterExtrudedFiniteDifferenceSpace2D = const CenterExtrudedFiniteDifferenceSpace3D = CenterExtrudedFiniteDifferenceSpace{<:SpectralElementSpace2D} const FaceExtrudedFiniteDifferenceSpace2D = - FaceSpace{<:CenterExtrudedFiniteDifferenceSpace2D} + FaceExtrudedFiniteDifferenceSpace{<:SpectralElementSpace1D} const FaceExtrudedFiniteDifferenceSpace3D = - FaceSpace{<:CenterExtrudedFiniteDifferenceSpace3D} - -CenterExtrudedFiniteDifferenceSpace( - space::CenterExtrudedFiniteDifferenceSpace, -) = space -CenterExtrudedFiniteDifferenceSpace(space::FaceExtrudedFiniteDifferenceSpace) = - space.center_space -FaceExtrudedFiniteDifferenceSpace(space::CenterExtrudedFiniteDifferenceSpace) = - FaceSpace(space) -FaceExtrudedFiniteDifferenceSpace(space::FaceExtrudedFiniteDifferenceSpace) = - space + FaceExtrudedFiniteDifferenceSpace{<:SpectralElementSpace2D} -function Base.show(io::IO, space::CenterExtrudedFiniteDifferenceSpace) +function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) println( @@ -122,86 +182,20 @@ function Base.show(io::IO, space::CenterExtrudedFiniteDifferenceSpace) println(iio, " "^(indent + 2), "vertical:") print(iio, " "^(indent + 4), "mesh: ", space.vertical_topology.mesh) end -local_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = - space.center_local_geometry +local_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = + space.grid.center_local_geometry local_geometry_data(space::FaceExtrudedFiniteDifferenceSpace) = - center_space(space).face_local_geometry + space.grid.face_local_geometry -# TODO: will need to be defined for distributed ghost_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = - space.center_ghost_geometry + space.grid.center_ghost_geometry ghost_geometry_data(space::FaceExtrudedFiniteDifferenceSpace) = - space.center_space.face_ghost_geometry - + space.grid.face_ghost_geometry -ExtrudedFiniteDifferenceSpace( - horizontal_space::AbstractSpace, - vertical_space::CenterFiniteDifferenceSpace, - hypsography::Flat = Flat(), -) = CenterExtrudedFiniteDifferenceSpace( - horizontal_space, - vertical_space, - hypsography, -) -ExtrudedFiniteDifferenceSpace( - horizontal_space::AbstractSpace, - vertical_space::FaceFiniteDifferenceSpace, - hypsography::Flat = Flat(), -) = FaceSpace( - CenterExtrudedFiniteDifferenceSpace( - horizontal_space, - center_space(vertical_space), - hypsography, - ), -) - -@memoize WeakValueDict function CenterExtrudedFiniteDifferenceSpace( - horizontal_space::AbstractSpace, - vertical_space::CenterFiniteDifferenceSpace, - hypsography::Flat = Flat(), -) - vertical_topology = vertical_space.topology - global_geometry = horizontal_space.global_geometry - center_local_geometry = - product_geometry.( - horizontal_space.local_geometry, - vertical_space.center_local_geometry, - ) - face_local_geometry = - product_geometry.( - horizontal_space.local_geometry, - vertical_space.face_local_geometry, - ) - if horizontal_space isa SpectralElementSpace2D - center_ghost_geometry = - product_geometry.( - horizontal_space.ghost_geometry, - vertical_space.center_local_geometry, - ) - face_ghost_geometry = - product_geometry.( - horizontal_space.ghost_geometry, - vertical_space.face_local_geometry, - ) - else - center_ghost_geometry = nothing - face_ghost_geometry = nothing - end - return CenterExtrudedFiniteDifferenceSpace( - horizontal_space, - vertical_topology, - hypsography, - global_geometry, - center_local_geometry, - face_local_geometry, - center_ghost_geometry, - face_ghost_geometry, - ) -end -quadrature_style(space::CenterExtrudedFiniteDifferenceSpace) = +quadrature_style(space::ExtrudedFiniteDifferenceGrid ) = space.horizontal_space.quadrature_style quadrature_style(space::FaceExtrudedFiniteDifferenceSpace) = quadrature_style(space.center_space) @@ -249,7 +243,7 @@ end -struct CenterColumnSpace{S<:CenterExtrudedFiniteDifferenceSpace, C<:ColumnIndex} <: AbstractFiniteDifferenceSpace +struct ColumnGrid{G<:, C<:ColumnIndex} <: AbstractFiniteDifferenceGrid space::S column::C end diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 5f0c073313..0c7335411c 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -1,53 +1,35 @@ -abstract type AbstractFiniteDifferenceSpace <: AbstractSpace end -mutable struct CenterFiniteDifferenceSpace{ +""" + FiniteDifferenceGrid(topology::Topologies.IntervalTopology) + FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) + +Construct a `FiniteDifferenceGrid` from an `IntervalTopology` (or an +`IntervalMesh`). + +This is an object which contains all the necessary geometric information. + +To avoid unnecessary duplication, we memoize the construction of the grid. +""" +mutable struct FiniteDifferenceGrid{ T <: Topologies.AbstractIntervalTopology, GG, LG, -} <: AbstractFiniteDifferenceSpace +} <: AbstractFiniteDifferenceGrid topology::T global_geometry::GG center_local_geometry::LG face_local_geometry::LG end -struct FaceSpace{C <: AbstractSpace} <: AbstractSpace - center_space::C -end - -face_space(space::CenterFiniteDifferenceSpace) = FaceSpace(space) -face_space(space::FaceSpace) = space +topology(grid::FiniteDifferenceGrid) = grid.topology -center_space(space::CenterFiniteDifferenceSpace) = space -center_space(space::FaceSpace) = space.center_space +ClimaComms.context(grid::FiniteDifferenceGrid) = + ClimaComms.context(topology(grid)) +ClimaComms.device(grid::FiniteDifferenceGrid) = + ClimaComms.device(topology(grid)) -const FaceFiniteDifferenceSpace = - FaceSpace{C} where {C <: CenterFiniteDifferenceSpace} - -const FiniteDifferenceSpace = - Union{CenterFiniteDifferenceSpace, FaceFiniteDifferenceSpace} - -FaceFiniteDifferenceSpace(space::FiniteDifferenceSpace) = - face_space(space) -CenterFiniteDifferenceSpace(space::FiniteDifferenceSpace) = - center_space(space) - -function Base.show(io::IO, space::FiniteDifferenceSpace) - indent = get(io, :indent, 0) - iio = IOContext(io, :indent => indent + 2) - println( - io, - space isa CenterFiniteDifferenceSpace ? "CenterFiniteDifferenceSpace" : - "FaceFiniteDifferenceSpace", - ":", - ) - print(iio, " "^(indent + 2), "context: ") - Topologies.print_context(iio, space.topology.context) - println(iio) - print(iio, " "^(indent + 2), "mesh: ", space.topology.mesh) -end -@memoize WeakValueDict function CenterFiniteDifferenceSpace( +@memoize WeakValueDict function FiniteDifferenceGrid( topology::Topologies.IntervalTopology, ) global_geometry = Geometry.CartesianGlobalGeometry() @@ -129,7 +111,7 @@ end ), ) end - return CenterFiniteDifferenceSpace( + return FiniteDifferenceGrid( topology, global_geometry, Adapt.adapt(ArrayType, center_local_geometry), @@ -137,17 +119,74 @@ end ) end -FaceFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = - face_space(CenterFiniteDifferenceSpace(topology)) +FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) = + FiniteDifferenceGrid(Topologies.IntervalTopology(mesh)) + + + + + + + +abstract type Staggering end + +""" + CellCenter() + +Cell center location +""" +struct CellCenter <: Staggering end + +""" + CellFace() + +Cell face location +""" +struct CellFace <: Staggering end + +abstract type AbstractFiniteDifferenceSpace<: AbstractSpace end + +""" + FiniteDifferenceSpace(staggering::Staggering, grid::FiniteDifferenceGrid) + +""" +struct FiniteDifferenceSpace{ + S <: Staggering, + G <: AbstractFiniteDifferenceGrid +} <: AbstractFiniteDifferenceSpace + staggering::S + grid::G +end + +const FaceFiniteDifferenceSpace = FiniteDifferenceSpace{CellFace} +const CenterFiniteDifferenceSpace = FiniteDifferenceSpace{CellCenter} + +function Base.show(io::IO, space::FiniteDifferenceSpace) + indent = get(io, :indent, 0) + iio = IOContext(io, :indent => indent + 2) + println( + io, + space isa CenterFiniteDifferenceSpace ? "CenterFiniteDifferenceSpace" : + "FaceFiniteDifferenceSpace", + ":", + ) + print(iio, " "^(indent + 2), "context: ") + Topologies.print_context(iio, ClimaComms.context(space)) + println(iio) + print(iio, " "^(indent + 2), "mesh: ", topology(space).mesh) +end + + + +FiniteDifferenceSpace{S}(grid::FiniteDifferenceGrid) where {S<:Staggering}= + FiniteDifferenceSpace(S(), grid) +FiniteDifferenceSpace{S}(topology::Topologies.IntervalTopology) where {S<:Staggering}= + FiniteDifferenceSpace{S}(FiniteDifferenceGrid(topology)) +FiniteDifferenceSpace{S}(mesh::Meshes.IntervalMesh) where {S<:Staggering}= + FiniteDifferenceSpace{S}(FiniteDifferenceGrid(mesh)) -CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = - CenterFiniteDifferenceSpace(Topologies.IntervalTopology(mesh)) -FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = - FaceFiniteDifferenceSpace(Topologies.IntervalTopology(mesh)) -ClimaComms.device(space::FiniteDifferenceSpace) = - ClimaComms.device(Spaces.center_space(space).topology) Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = FiniteDifferenceSpace( space.staggering, @@ -157,19 +196,15 @@ Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = FiniteDifferenceSpace( Adapt.adapt(to, space.face_local_geometry), ) -CenterFiniteDifferenceSpace(face_space::FaceFiniteDifferenceSpace) = - face_space.center_space +nlevels(space::FiniteDifferenceSpace) = length(space) +# TODO: deprecate? Base.length(space::FiniteDifferenceSpace) = length(coordinates_data(space)) -topology(space::FiniteDifferenceSpace) = center_space(space).topology -vertical_topology(space::FiniteDifferenceSpace) = center_space(space).topology -nlevels(space::FiniteDifferenceSpace) = length(space) -local_geometry_data(space::CenterFiniteDifferenceSpace) = - space.center_local_geometry +topology(space::FiniteDifferenceSpace) = topology(space.grid) +vertical_topology(space::FiniteDifferenceSpace) = topology(space.grid) + -local_geometry_data(space::FaceFiniteDifferenceSpace) = - center_space(space).face_local_geometry Base.@deprecate z_component(::Type{T}) where {T} Δz_metric_component(T) false From e16e591f8c76d59ed0653539bbd55746caccf556 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 2 Oct 2023 10:25:01 -0700 Subject: [PATCH 07/42] WIP --- src/Fields/Fields.jl | 3 +- src/Operators/finitedifference.jl | 271 +++++++++--------------------- src/Spaces/extruded.jl | 132 +++++++-------- src/Spaces/finitedifference.jl | 9 +- src/Spaces/spectralelement.jl | 77 ++++++--- 5 files changed, 198 insertions(+), 294 deletions(-) diff --git a/src/Fields/Fields.jl b/src/Fields/Fields.jl index 9059347add..caf204f445 100644 --- a/src/Fields/Fields.jl +++ b/src/Fields/Fields.jl @@ -86,11 +86,12 @@ const CenterExtrudedFiniteDifferenceField{V, S} = Field{ } where {V <: AbstractData, S <: Spaces.CenterExtrudedFiniteDifferenceSpace} # Cubed Sphere Fields +#= const CubedSphereSpectralElementField2D{V, S} = Field{ V, S, } where {V <: AbstractData, S <: Spaces.CubedSphereSpectralElementSpace2D} - +=# Base.propertynames(field::Field) = propertynames(getfield(field, :values)) @inline field_values(field::Field) = getfield(field, :values) diff --git a/src/Operators/finitedifference.jl b/src/Operators/finitedifference.jl index be03f28628..59f9d2af2f 100644 --- a/src/Operators/finitedifference.jl +++ b/src/Operators/finitedifference.jl @@ -1,35 +1,25 @@ import ..Utilities: PlusHalf, half -left_idx( - space::Union{ - Spaces.AbstractFiniteDifferenceSpace, - Spaces.CenterExtrudedFiniteDifferenceSpace, - }, -) = left_center_boundary_idx(space) -right_idx( - space::Union{ - Spaces.AbstractFiniteDifferenceSpace, - Spaces.CenterExtrudedFiniteDifferenceSpace, - }, -) = right_center_boundary_idx(space) -left_idx( - space::Union{ - Spaces.FaceSpace{<:Spaces.AbstractFiniteDifferenceSpace}, - Spaces.FaceExtrudedFiniteDifferenceSpace, - }, -) = left_face_boundary_idx(space) -right_idx( - space::Union{ - Spaces.FaceSpace{<:Spaces.AbstractFiniteDifferenceSpace}, - Spaces.FaceExtrudedFiniteDifferenceSpace, - }, -) = right_face_boundary_idx(space) +const AllFiniteDifferenceSpace = + Union{Spaces.FiniteDifferenceSpace, Spaces.ExtrudedFiniteDifferenceSpace} +const AllFaceFiniteDifferenceSpace = + Union{Spaces.FaceFiniteDifferenceSpace, Spaces.FaceExtrudedFiniteDifferenceSpace} +const AllCenterFiniteDifferenceSpace = + Union{Spaces.CenterFiniteDifferenceSpace, Spaces.CenterExtrudedFiniteDifferenceSpace} + -left_center_boundary_idx(space::Spaces.AbstractSpace) = 1 -right_center_boundary_idx(space::Spaces.AbstractSpace) = + +left_idx(space::AllCenterFiniteDifferenceSpace) = left_center_boundary_idx(space) +right_idx(space::AllCenterFiniteDifferenceSpace) = + right_center_boundary_idx(space) +left_idx(space::AllFaceFiniteDifferenceSpace) = left_face_boundary_idx(space) +right_idx(space::AllFaceFiniteDifferenceSpace) = right_face_boundary_idx(space) + +left_center_boundary_idx(space::AllFiniteDifferenceSpace) = 1 +right_center_boundary_idx(space::AllFiniteDifferenceSpace) = size(Spaces.local_geometry_data(Spaces.center_space(space)), 4) -left_face_boundary_idx(space::Spaces.AbstractSpace) = half -right_face_boundary_idx(space::Spaces.AbstractSpace) = +left_face_boundary_idx(space::AllFiniteDifferenceSpace) = half +right_face_boundary_idx(space::AllFiniteDifferenceSpace) = size(Spaces.local_geometry_data(Spaces.face_space(space)), 4) - half @@ -40,10 +30,7 @@ right_center_boundary_idx(arg) = right_center_boundary_idx(axes(arg)) # unlike getidx, we allow extracting the face local geometry from the center space, and vice-versa Base.@propagate_inbounds function Geometry.LocalGeometry( - space::Union{ - Spaces.FiniteDifferenceSpace, - Spaces.ExtrudedFiniteDifferenceSpace, - }, + space::AllFiniteDifferenceSpace, idx::Integer, hidx, ) @@ -52,13 +39,16 @@ Base.@propagate_inbounds function Geometry.LocalGeometry( v = mod1(v, length(space)) end i, j, h = hidx - return @inbounds Spaces.center_space(space).center_local_geometry[CartesianIndex(i, j, 1, v, h)] + return @inbounds Spaces.center_space(space).center_local_geometry[CartesianIndex( + i, + j, + 1, + v, + h, + )] end Base.@propagate_inbounds function Geometry.LocalGeometry( - space::Union{ - Spaces.FiniteDifferenceSpace, - Spaces.ExtrudedFiniteDifferenceSpace, - }, + space::AllFiniteDifferenceSpace, idx::PlusHalf, hidx, ) @@ -67,7 +57,13 @@ Base.@propagate_inbounds function Geometry.LocalGeometry( v = mod1(v, length(space)) end i, j, h = hidx - return @inbounds Spaces.center_space(space).face_local_geometry[CartesianIndex(i, j, 1, v, h)] + return @inbounds Spaces.center_space(space).face_local_geometry[CartesianIndex( + i, + j, + 1, + v, + h, + )] end @@ -349,7 +345,7 @@ struct InterpolateF2C{BCS} <: InterpolationOperator end InterpolateF2C(; kwargs...) = InterpolateF2C(NamedTuple(kwargs)) -return_space(::InterpolateF2C, space::Spaces.FaceSpace) = +return_space(::InterpolateF2C, space::AllFaceFiniteDifferenceSpace) = Spaces.center_space(space) stencil_interior_width(::InterpolateF2C, arg) = ((-half, half),) @@ -401,12 +397,8 @@ struct InterpolateC2F{BCS} <: InterpolationOperator end InterpolateC2F(; kwargs...) = InterpolateC2F(NamedTuple(kwargs)) -return_space(::InterpolateC2F, space::Spaces.CenterFiniteDifferenceSpace) = +return_space(::InterpolateC2F, space::AllCenterFiniteDifferenceSpace) = Spaces.face_space(space) -return_space( - ::InterpolateC2F, - space::Spaces.CenterExtrudedFiniteDifferenceSpace, -) = Spaces.face_space(space) stencil_interior_width(::InterpolateC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -530,12 +522,8 @@ struct LeftBiasedC2F{BCS} <: InterpolationOperator end LeftBiasedC2F(; kwargs...) = LeftBiasedC2F(NamedTuple(kwargs)) -return_space(::LeftBiasedC2F, space::Spaces.CenterFiniteDifferenceSpace) = - Spaces.FaceFiniteDifferenceSpace(space) -return_space( - ::LeftBiasedC2F, - space::Spaces.CenterExtrudedFiniteDifferenceSpace, -) = Spaces.FaceExtrudedFiniteDifferenceSpace(space) +return_space(::LeftBiasedC2F, space::AllCenterFiniteDifferenceSpace) = + Spaces.face_space(space) stencil_interior_width(::LeftBiasedC2F, arg) = ((-half, -half),) Base.@propagate_inbounds stencil_interior( @@ -593,7 +581,7 @@ struct LeftBiasedF2C{BCS} <: InterpolationOperator end LeftBiasedF2C(; kwargs...) = LeftBiasedF2C(NamedTuple(kwargs)) -return_space(::LeftBiasedF2C, space::Spaces.FaceSpace) = +return_space(::LeftBiasedF2C, space::AllFaceFiniteDifferenceSpace) = Spaces.center_space(space) stencil_interior_width(::LeftBiasedF2C, arg) = ((-half, -half),) @@ -653,14 +641,8 @@ struct LeftBiased3rdOrderC2F{BCS} <: InterpolationOperator end LeftBiased3rdOrderC2F(; kwargs...) = LeftBiased3rdOrderC2F(NamedTuple(kwargs)) -return_space( - ::LeftBiased3rdOrderC2F, - space::Spaces.CenterFiniteDifferenceSpace, -) = Spaces.FaceFiniteDifferenceSpace(space) -return_space( - ::LeftBiased3rdOrderC2F, - space::Spaces.CenterExtrudedFiniteDifferenceSpace, -) = Spaces.FaceExtrudedFiniteDifferenceSpace(space) +return_space(::LeftBiased3rdOrderC2F, space::AllCenterFiniteDifferenceSpace) = + Spaces.face_space(space) stencil_interior_width(::LeftBiased3rdOrderC2F, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -723,7 +705,7 @@ struct LeftBiased3rdOrderF2C{BCS} <: InterpolationOperator end LeftBiased3rdOrderF2C(; kwargs...) = LeftBiased3rdOrderF2C(NamedTuple(kwargs)) -return_space(::LeftBiased3rdOrderF2C, space::Spaces.FaceSpace) = +return_space(::LeftBiased3rdOrderF2C, space::AllFaceFiniteDifferenceSpace) = Spaces.center_space(space) stencil_interior_width(::LeftBiased3rdOrderF2C, arg) = ((-half - 1, half + 1),) @@ -787,12 +769,8 @@ struct RightBiasedC2F{BCS} <: InterpolationOperator end RightBiasedC2F(; kwargs...) = RightBiasedC2F(NamedTuple(kwargs)) -return_space(::RightBiasedC2F, space::Spaces.CenterFiniteDifferenceSpace) = - Spaces.FaceFiniteDifferenceSpace(space) -return_space( - ::RightBiasedC2F, - space::Spaces.CenterExtrudedFiniteDifferenceSpace, -) = Spaces.FaceExtrudedFiniteDifferenceSpace(space) +return_space(::RightBiasedC2F, space::AllCenterFiniteDifferenceSpace) = + Spaces.face_space(space) stencil_interior_width(::RightBiasedC2F, arg) = ((half, half),) Base.@propagate_inbounds stencil_interior( @@ -850,7 +828,7 @@ struct RightBiasedF2C{BCS} <: InterpolationOperator end RightBiasedF2C(; kwargs...) = RightBiasedF2C(NamedTuple(kwargs)) -return_space(::RightBiasedF2C, space::Spaces.FaceSpace) = +return_space(::RightBiasedF2C, space::AllFaceFiniteDifferenceSpace) = Spaces.center_space(space) stencil_interior_width(::RightBiasedF2C, arg) = ((half, half),) @@ -912,14 +890,8 @@ struct RightBiased3rdOrderC2F{BCS} <: InterpolationOperator end RightBiased3rdOrderC2F(; kwargs...) = RightBiased3rdOrderC2F(NamedTuple(kwargs)) -return_space( - ::RightBiased3rdOrderC2F, - space::Spaces.CenterFiniteDifferenceSpace, -) = Spaces.FaceFiniteDifferenceSpace(space) -return_space( - ::RightBiased3rdOrderC2F, - space::Spaces.CenterExtrudedFiniteDifferenceSpace, -) = Spaces.FaceExtrudedFiniteDifferenceSpace(space) +return_space(::RightBiased3rdOrderC2F, space::AllCenterFiniteDifferenceSpace) = + Spaces.face_space(space) stencil_interior_width(::RightBiased3rdOrderC2F, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -970,14 +942,8 @@ struct RightBiased3rdOrderF2C{BCS} <: InterpolationOperator end RightBiased3rdOrderF2C(; kwargs...) = RightBiased3rdOrderF2C(NamedTuple(kwargs)) -return_space( - ::RightBiased3rdOrderF2C, - space::Spaces.FaceFiniteDifferenceSpace, -) = Spaces.CenterFiniteDifferenceSpace(space) -return_space( - ::RightBiased3rdOrderF2C, - space::Spaces.FaceExtrudedFiniteDifferenceSpace, -) = Spaces.CenterExtrudedFiniteDifferenceSpace(space) +return_space(::RightBiased3rdOrderF2C, space::AllFaceFiniteDifferenceSpace) = + Spaces.center_space(space) stencil_interior_width(::RightBiased3rdOrderF2C, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -1037,8 +1003,8 @@ WeightedInterpolateF2C(; kwargs...) = WeightedInterpolateF2C(NamedTuple(kwargs)) return_space( ::WeightedInterpolateF2C, - weight_space::Spaces.FaceSpace, - arg_space::Spaces.FaceSpace, + weight_space::AllFaceFiniteDifferenceSpace, + arg_space::AllFaceFiniteDifferenceSpace, ) = Spaces.center_space(arg_space) stencil_interior_width(::WeightedInterpolateF2C, weight, arg) = @@ -1090,14 +1056,9 @@ WeightedInterpolateC2F(; kwargs...) = WeightedInterpolateC2F(NamedTuple(kwargs)) return_space( ::WeightedInterpolateC2F, - weight_space::Spaces.CenterFiniteDifferenceSpace, - arg_space::Spaces.CenterFiniteDifferenceSpace, -) = Spaces.FaceFiniteDifferenceSpace(arg_space) -return_space( - ::WeightedInterpolateC2F, - weight_space::Spaces.CenterExtrudedFiniteDifferenceSpace, - arg_space::Spaces.CenterExtrudedFiniteDifferenceSpace, -) = Spaces.FaceExtrudedFiniteDifferenceSpace(arg_space) + weight_space::AllCenterFiniteDifferenceSpace, + arg_space::AllCenterFiniteDifferenceSpace, +) = Spaces.face_space(arg_space) stencil_interior_width(::WeightedInterpolateC2F, weight, arg) = ((-half, half), (-half, half)) @@ -1258,13 +1219,8 @@ return_eltype(::UpwindBiasedProductC2F, V, A) = return_space( ::UpwindBiasedProductC2F, - velocity_space::Spaces.FaceFiniteDifferenceSpace, - arg_space::Spaces.CenterFiniteDifferenceSpace, -) = velocity_space -return_space( - ::UpwindBiasedProductC2F, - velocity_space::Spaces.FaceExtrudedFiniteDifferenceSpace, - arg_space::Spaces.CenterExtrudedFiniteDifferenceSpace, + velocity_space::AllFaceFiniteDifferenceSpace, + arg_space::AllCenterFiniteDifferenceSpace, ) = velocity_space function upwind_biased_product(v, a⁻, a⁺) @@ -1400,13 +1356,8 @@ return_eltype(::Upwind3rdOrderBiasedProductC2F, V, A) = return_space( ::Upwind3rdOrderBiasedProductC2F, - velocity_space::Spaces.FaceFiniteDifferenceSpace, - arg_space::Spaces.CenterFiniteDifferenceSpace, -) = velocity_space -return_space( - ::Upwind3rdOrderBiasedProductC2F, - velocity_space::Spaces.FaceExtrudedFiniteDifferenceSpace, - arg_space::Spaces.CenterExtrudedFiniteDifferenceSpace, + velocity_space::AllFaceFiniteDifferenceSpace, + arg_space::AllCenterFiniteDifferenceSpace, ) = velocity_space function upwind_3rdorder_biased_product(v, a⁻, a⁻⁻, a⁺, a⁺⁺) @@ -1562,13 +1513,8 @@ return_eltype(::FCTBorisBook, V, A) = return_space( ::FCTBorisBook, - velocity_space::Spaces.FaceFiniteDifferenceSpace, - arg_space::Spaces.CenterFiniteDifferenceSpace, -) = velocity_space -return_space( - ::FCTBorisBook, - velocity_space::Spaces.FaceExtrudedFiniteDifferenceSpace, - arg_space::Spaces.CenterExtrudedFiniteDifferenceSpace, + velocity_space::AllFaceFiniteDifferenceSpace, + arg_space::AllCenterFiniteDifferenceSpace, ) = velocity_space function fct_boris_book(v, a⁻⁻, a⁻, a⁺, a⁺⁺) @@ -1700,15 +1646,9 @@ return_eltype(::FCTZalesak, A, Φ, Φᵗᵈ) = return_space( ::FCTZalesak, - A_space::Spaces.FaceFiniteDifferenceSpace, - Φ_space::Spaces.CenterFiniteDifferenceSpace, - Φᵗᵈ_space::Spaces.CenterFiniteDifferenceSpace, -) = A_space -return_space( - ::FCTZalesak, - A_space::Spaces.FaceExtrudedFiniteDifferenceSpace, - Φ_space::Spaces.CenterExtrudedFiniteDifferenceSpace, - Φᵗᵈ_space::Spaces.CenterExtrudedFiniteDifferenceSpace, + A_space::AllFaceFiniteDifferenceSpace, + Φ_space::AllCenterFiniteDifferenceSpace, + Φᵗᵈ_space::AllCenterFiniteDifferenceSpace, ) = A_space function fct_zalesak( @@ -1872,13 +1812,8 @@ AdvectionF2F(; kwargs...) = AdvectionF2F(NamedTuple(kwargs)) return_space( ::AdvectionF2F, - velocity_space::Spaces.FaceFiniteDifferenceSpace, - arg_space::Spaces.FaceFiniteDifferenceSpace, -) = arg_space -return_space( - ::AdvectionF2F, - velocity_space::Spaces.FaceExtrudedFiniteDifferenceSpace, - arg_space::Spaces.FaceExtrudedFiniteDifferenceSpace, + velocity_space::AllFaceFiniteDifferenceSpace, + arg_space::AllFaceFiniteDifferenceSpace, ) = arg_space stencil_interior_width(::AdvectionF2F, velocity, arg) = ((0, 0), (-1, 1)) @@ -1934,13 +1869,8 @@ AdvectionC2C(; kwargs...) = AdvectionC2C(NamedTuple(kwargs)) return_space( ::AdvectionC2C, - velocity_space::Spaces.FaceFiniteDifferenceSpace, - arg_space::Spaces.CenterFiniteDifferenceSpace, -) = arg_space -return_space( - ::AdvectionC2C, - velocity_space::Spaces.FaceExtrudedFiniteDifferenceSpace, - arg_space::Spaces.CenterExtrudedFiniteDifferenceSpace, + velocity_space::AllFaceFiniteDifferenceSpace, + arg_space::AllCenterFiniteDifferenceSpace, ) = arg_space stencil_interior_width(::AdvectionC2C, velocity, arg) = @@ -2072,13 +2002,8 @@ FluxCorrectionC2C(; kwargs...) = FluxCorrectionC2C(NamedTuple(kwargs)) return_space( ::FluxCorrectionC2C, - velocity_space::Spaces.FaceFiniteDifferenceSpace, - arg_space::Spaces.CenterFiniteDifferenceSpace, -) = arg_space -return_space( - ::FluxCorrectionC2C, - velocity_space::Spaces.FaceExtrudedFiniteDifferenceSpace, - arg_space::Spaces.CenterExtrudedFiniteDifferenceSpace, + velocity_space::AllFaceFiniteDifferenceSpace, + arg_space::AllCenterFiniteDifferenceSpace, ) = arg_space stencil_interior_width(::FluxCorrectionC2C, velocity, arg) = @@ -2157,13 +2082,8 @@ FluxCorrectionF2F(; kwargs...) = FluxCorrectionF2F(NamedTuple(kwargs)) return_space( ::FluxCorrectionF2F, - velocity_space::Spaces.CenterFiniteDifferenceSpace, - arg_space::Spaces.FaceFiniteDifferenceSpace, -) = arg_space -return_space( - ::FluxCorrectionF2F, - velocity_space::Spaces.CenterExtrudedFiniteDifferenceSpace, - arg_space::Spaces.FaceExtrudedFiniteDifferenceSpace, + velocity_space::AllCenterFiniteDifferenceSpace, + arg_space::AllFaceFiniteDifferenceSpace, ) = arg_space stencil_interior_width(::FluxCorrectionF2F, velocity, arg) = @@ -2249,12 +2169,7 @@ struct SetBoundaryOperator{BCS} <: BoundaryOperator end SetBoundaryOperator(; kwargs...) = SetBoundaryOperator(NamedTuple(kwargs)) -return_space(::SetBoundaryOperator, space::Spaces.FaceFiniteDifferenceSpace) = - space -return_space( - ::SetBoundaryOperator, - space::Spaces.FaceExtrudedFiniteDifferenceSpace, -) = space +return_space(::SetBoundaryOperator, space::AllFaceFiniteDifferenceSpace) = space stencil_interior_width(::SetBoundaryOperator, arg) = ((0, 0),) Base.@propagate_inbounds stencil_interior( @@ -2333,7 +2248,7 @@ struct GradientF2C{BCS} <: GradientOperator end GradientF2C(; kwargs...) = GradientF2C(NamedTuple(kwargs)) -return_space(::GradientF2C, space::Spaces.FaceSpace) = +return_space(::GradientF2C, space::AllFaceFiniteDifferenceSpace) = Spaces.center_space(space) stencil_interior_width(::GradientF2C, arg) = ((-half, half),) @@ -2446,10 +2361,8 @@ struct GradientC2F{BC} <: GradientOperator end GradientC2F(; kwargs...) = GradientC2F(NamedTuple(kwargs)) -return_space(::GradientC2F, space::Spaces.CenterFiniteDifferenceSpace) = - Spaces.FaceFiniteDifferenceSpace(space) -return_space(::GradientC2F, space::Spaces.CenterExtrudedFiniteDifferenceSpace) = - Spaces.FaceExtrudedFiniteDifferenceSpace(space) +return_space(::GradientC2F, space::AllCenterFiniteDifferenceSpace) = + Spaces.face_space(space) stencil_interior_width(::GradientC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2572,10 +2485,8 @@ struct DivergenceF2C{BCS} <: DivergenceOperator end DivergenceF2C(; kwargs...) = DivergenceF2C(NamedTuple(kwargs)) -return_space(::DivergenceF2C, space::Spaces.FaceFiniteDifferenceSpace) = - Spaces.CenterFiniteDifferenceSpace(space) -return_space(::DivergenceF2C, space::Spaces.FaceExtrudedFiniteDifferenceSpace) = - Spaces.CenterExtrudedFiniteDifferenceSpace(space) +return_space(::DivergenceF2C, space::AllFaceFiniteDifferenceSpace) = + Spaces.center_space(space) stencil_interior_width(::DivergenceF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2697,12 +2608,8 @@ struct DivergenceC2F{BC} <: DivergenceOperator end DivergenceC2F(; kwargs...) = DivergenceC2F(NamedTuple(kwargs)) -return_space(::DivergenceC2F, space::Spaces.CenterFiniteDifferenceSpace) = - Spaces.FaceFiniteDifferenceSpace(space) -return_space( - ::DivergenceC2F, - space::Spaces.CenterExtrudedFiniteDifferenceSpace, -) = Spaces.FaceExtrudedFiniteDifferenceSpace(space) +return_space(::DivergenceC2F, space::AllCenterFiniteDifferenceSpace) = + Spaces.face_space(space) stencil_interior_width(::DivergenceC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2845,10 +2752,8 @@ struct CurlC2F{BC} <: CurlFiniteDifferenceOperator end CurlC2F(; kwargs...) = CurlC2F(NamedTuple(kwargs)) -return_space(::CurlC2F, space::Spaces.CenterFiniteDifferenceSpace) = - Spaces.FaceFiniteDifferenceSpace(space) -return_space(::CurlC2F, space::Spaces.CenterExtrudedFiniteDifferenceSpace) = - Spaces.FaceExtrudedFiniteDifferenceSpace(space) +return_space(::CurlC2F, space::AllCenterFiniteDifferenceSpace) = + Spaces.face_space(space) fd3_curl(u₊::Geometry.Covariant1Vector, u₋::Geometry.Covariant1Vector, invJ) = Geometry.Contravariant2Vector((u₊.u₁ - u₋.u₁) * invJ) @@ -3155,10 +3060,7 @@ Base.Broadcast.BroadcastStyle( Base.eltype(bc::StencilBroadcasted) = return_eltype(bc.op, bc.args...) -function vidx( - space::Spaces.FaceSpace, - idx, -) +function vidx(space::AllFaceFiniteDifferenceSpace, idx) @assert idx isa PlusHalf v = idx + half if Topologies.isperiodic(Spaces.vertical_topology(space)) @@ -3166,14 +3068,7 @@ function vidx( end return v end -function vidx( - space::Union{ - Spaces.CenterFiniteDifferenceSpace, - Spaces.CenterExtrudedFiniteDifferenceSpace, - Spaces.CenterColumnSpace, - }, - idx, -) +function vidx(space::AllCenterFiniteDifferenceSpace, idx) @assert idx isa Integer v = idx if Topologies.isperiodic(Spaces.vertical_topology(space)) diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index ad38526d81..ad82cccf09 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -88,32 +88,44 @@ end -struct ExtudedFiniteDifferenceSpace{S<:Staggering,G<:ExtrudedFiniteDifferenceGrid} <: AbstractSpace - space::S +struct ExtrudedFiniteDifferenceSpace{ + S <: Staggering, + G <: ExtrudedFiniteDifferenceGrid, +} <: AbstractSpace + staggering::S grid::G end -const FaceExtrudedFiniteDifferenceSpace = ExtrudedFiniteDifferenceSpace{CellFace} -const CenterExtrudedFiniteDifferenceSpace = ExtrudedFiniteDifferenceSpace{CellCenter} +const FaceExtrudedFiniteDifferenceSpace = + ExtrudedFiniteDifferenceSpace{CellFace} +const CenterExtrudedFiniteDifferenceSpace = + ExtrudedFiniteDifferenceSpace{CellCenter} + + +ExtrudedFiniteDifferenceSpace{S}( + grid::ExtrudedFiniteDifferenceGrid, +) where {S <: Staggering} = ExtrudedFiniteDifferenceSpace(S(), grid) +ExtrudedFiniteDifferenceSpace{S}( + space::ExtrudedFiniteDifferenceSpace, +) where {S <: Staggering} = ExtrudedFiniteDifferenceSpace{S}(space.grid) + + +face_space(space::ExtrudedFiniteDifferenceSpace) = + ExtrudedFiniteDifferenceSpace{CellFace}(space) +center_space(space::ExtrudedFiniteDifferenceSpace) = + ExtrudedFiniteDifferenceSpace{CellCenter}(space) + -ExtrudedFiniteDifferenceSpace{S}(grid::ExtrudedFiniteDifferenceGrid) where {S<:Staggering}= - ExtrudedFiniteDifferenceSpace(S(), grid) -ExtrudedFiniteDifferenceSpace{S}(space::ExtrudedFiniteDifferenceSpace) where {S<:Staggering}= - ExtrudedFiniteDifferenceSpace{S}(space.grid) ExtrudedFiniteDifferenceSpace{S}( horizontal_space::AbstractSpace, vertical_space::FiniteDifferenceSpace, hypsography::HypsographyAdaption = Flat(), -) = ExtrudedFiniteDifferenceSpace{S}( - ExtrudedFiniteDifferenceGrid( - horizontal_space, - vertical_space, - hypsography, - ) +) where {S <: Staggering} = ExtrudedFiniteDifferenceSpace{S}( + ExtrudedFiniteDifferenceGrid(horizontal_space, vertical_space, hypsography), ) - + function issubspace( hspace::AbstractSpectralElementSpace, extruded_space::ExtrudedFiniteDifferenceSpace, @@ -194,24 +206,18 @@ ghost_geometry_data(space::FaceExtrudedFiniteDifferenceSpace) = space.grid.face_ghost_geometry +quadrature_style(space::ExtrudedFiniteDifferenceGrid) = + space.grid.horizontal_space.quadrature_style +topology(space::ExtrudedFiniteDifferenceSpace) = + space.grid.horizontal_space.topology -quadrature_style(space::ExtrudedFiniteDifferenceGrid ) = - space.horizontal_space.quadrature_style -quadrature_style(space::FaceExtrudedFiniteDifferenceSpace) = - quadrature_style(space.center_space) - -topology(space::CenterExtrudedFiniteDifferenceSpace) = - space.horizontal_space.topology -topology(space::FaceExtrudedFiniteDifferenceSpace) = - topology(space.center_space) - -topology(space::ExtrudedFiniteDifferenceSpace) = topology(horizontal_space(space)) ClimaComms.device(space::ExtrudedFiniteDifferenceSpace) = - ClimaComms.device(topology(space)) -vertical_topology(space::CenterExtrudedFiniteDifferenceSpace) = - space.vertical_topology -vertical_topology(space::FaceExtrudedFiniteDifferenceSpace) = - vertical_topology(space.center_space) + ClimaComms.device(space.grid) +ClimaComms.context(space::ExtrudedFiniteDifferenceSpace) = + ClimaComms.context(space.grid) + +vertical_topology(space::ExtrudedFiniteDifferenceSpace) = + space.grid.vertical_topology Base.@propagate_inbounds function slab( space::ExtrudedFiniteDifferenceSpace, @@ -242,63 +248,39 @@ struct ColumnIndex{N} end - -struct ColumnGrid{G<:, C<:ColumnIndex} <: AbstractFiniteDifferenceGrid - space::S - column::C +struct ColumnGrid{G <: ExtrudedFiniteDifferenceGrid, C <: ColumnIndex} <: + AbstractFiniteDifferenceGrid + full_grid::G + colidx::C end -const FaceColumnSpace = FaceSpace{<:CenterColumnSpace} - -const ColumnSpace = Union{ - CenterColumnSpace, - FaceColumnSpace, -} - -face_space(space::CenterColumnSpace) = - FaceSpace(space) -center_space(space::CenterColumnSpace) = - space -full_space(colspace::CenterColumnSpace) = colspace.space -full_space(colspace::FaceColumnSpace) = FaceSpace(center_space(colspace).space) +column(grid::ExtrudedFiniteDifferenceGrid, colidx::ColumnIndex) = + ColumnGrid(grid, colidx) -column( - space::CenterExtrudedFiniteDifferenceSpace, - colidx::ColumnIndex -) = - CenterColumnSpace(space, colidx) -column( - space::FaceExtrudedFiniteDifferenceSpace, - colidx::ColumnIndex -) = - FaceSpace(CenterColumnSpace(center_space(space), colidx)) - +function column(space::ExtrudedFiniteDifferenceSpace, colidx::ColumnIndex) + column_grid = column(space.grid, colidx) + FiniteDifferenceSpace(space.staggering, column_grid) +end -vertical_topology(space::ColumnSpace) = vertical_topology(full_space(space)) +vertical_topology(colgrid::ColumnGrid) = colgrid.full_grid.vertical_topology -function local_geometry_data( - columnspace::ColumnSpace, -) - column(local_geometry_data(full_space(columnspace)), center_space(columnspace).column) +function center_local_geometry_data(colgrid::ColumnGrid) + column(center_local_geometry_data(colgrid.full_grid), colgrid.colidx) +end +function face_local_geometry_data(colgrid::ColumnGrid) + column(face_local_geometry(colgrid.full_grid), colgrid.colidx) end +topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) - -ClimaComms.device(columnspace::ColumnSpace) = - ClimaComms.device(full_space(columnspace)) -ClimaComms.context(columnspace::ColumnSpace) = - ClimaComms.context(full_space(columnspace)) +ClimaComms.device(colgrid::ColumnGrid) = ClimaComms.device(colgrid.full_grid) +ClimaComms.context(colgrid::ColumnGrid) = ClimaComms.context(colgrid.full_grid) # TODO: deprecate these -column( - space::ExtrudedFiniteDifferenceSpace, - i, - j, - h, -) = +column(space::ExtrudedFiniteDifferenceSpace, i, j, h) = column(space, ColumnIndex((i, j), h)) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 0c7335411c..6278a505f3 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -1,3 +1,4 @@ +abstract type AbstractFiniteDifferenceGrid end """ FiniteDifferenceGrid(topology::Topologies.IntervalTopology) @@ -180,13 +181,17 @@ end FiniteDifferenceSpace{S}(grid::FiniteDifferenceGrid) where {S<:Staggering}= FiniteDifferenceSpace(S(), grid) +FiniteDifferenceSpace{S}(space::FiniteDifferenceSpace) where {S<:Staggering}= + FiniteDifferenceSpace(S(), space.grid) FiniteDifferenceSpace{S}(topology::Topologies.IntervalTopology) where {S<:Staggering}= FiniteDifferenceSpace{S}(FiniteDifferenceGrid(topology)) FiniteDifferenceSpace{S}(mesh::Meshes.IntervalMesh) where {S<:Staggering}= FiniteDifferenceSpace{S}(FiniteDifferenceGrid(mesh)) - - +face_space(space::FiniteDifferenceSpace) = + FiniteDifferenceSpace{CellFace}(space) +center_space(space::FiniteDifferenceSpace) = + FiniteDifferenceSpace{CellCenter}(space) Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = FiniteDifferenceSpace( space.staggering, diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 60452fe33b..58d7af9aaf 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -75,12 +75,13 @@ nperimeter2d(Nq) = 4 + (Nq - 2) * 4 nperimeter(::Perimeter2D{Nq}) where {Nq} = nperimeter2d(Nq) Base.length(::Perimeter2D{Nq}) where {Nq} = nperimeter2d(Nq) + """ - SpectralElementSpace1D <: AbstractSpace + SpectralElementGrid1D A one-dimensional space: within each element the space is represented as a polynomial. """ -struct SpectralElementSpace1D{ +mutable struct SpectralElementGrid1D{ T, Q, GG <: Geometry.AbstractGlobalGeometry, @@ -94,9 +95,9 @@ struct SpectralElementSpace1D{ dss_weights::D end -function SpectralElementSpace1D( +@memoize WeakValueDict function SpectralElementGrid1D( topology::Topologies.IntervalTopology, - quadrature_style, + quadrature_style::Quadratures.QuadratureStyle, ) global_geometry = Geometry.CartesianGlobalGeometry() CoordType = Topologies.coordinate_type(topology) @@ -143,7 +144,7 @@ function SpectralElementSpace1D( dss_1d!(topology, dss_weights) dss_weights = one(FT) ./ dss_weights - return SpectralElementSpace1D( + return SpectralElementGrid1D( topology, quadrature_style, global_geometry, @@ -152,21 +153,34 @@ function SpectralElementSpace1D( ) end + +struct SpectralElementSpace1D{G} <: AbstractSpectralElementSpace + grid::G +end + +function SpectralElementSpace1D( + topology::Topologies.IntervalTopology, + quadrature_style::Quadratures.QuadratureStyle, +) + grid = SpectralElementGrid1D(topology, quadrature_style) + SpectralElementSpace1D(grid) +end + + + + nlevels(space::SpectralElementSpace1D) = 1 -const IntervalSpectralElementSpace1D = SpectralElementSpace1D{ - <:Topologies.IntervalTopology{ - <:ClimaComms.AbstractCommsContext, - <:Meshes.IntervalMesh, - }, -} + + + """ SpectralElementSpace2D <: AbstractSpace A two-dimensional space: within each element the space is represented as a polynomial. """ -mutable struct SpectralElementSpace2D{ +mutable struct SpectralElementGrid2D{ T, Q, GG <: Geometry.AbstractGlobalGeometry, @@ -174,7 +188,7 @@ mutable struct SpectralElementSpace2D{ D, IS, BS, -} <: AbstractSpectralElementSpace +} topology::T quadrature_style::Q global_geometry::GG @@ -186,18 +200,17 @@ mutable struct SpectralElementSpace2D{ boundary_surface_geometries::BS end -Adapt.adapt_structure(to, space::SpectralElementSpace2D) = - SpectralElementSpace2D( - nothing, # drop topology - Adapt.adapt(to, space.quadrature_style), - Adapt.adapt(to, space.global_geometry), - Adapt.adapt(to, space.local_geometry), - Adapt.adapt(to, space.ghost_geometry), - Adapt.adapt(to, space.local_dss_weights), - Adapt.adapt(to, space.ghost_dss_weights), - Adapt.adapt(to, space.internal_surface_geometry), - Adapt.adapt(to, space.boundary_surface_geometries), - ) +struct DeviceSpectralElementGrid2D{Q,GG,LG} + quadrature_style::Q + global_geometry::GG + local_geometry::LG +end +Adapt.adapt_structure(to, grid::SpectralElementGrid2D) = + DeviceSpectralElementGrid2D( + Adapt.adapt(to, grid.quadrature_style), + Adapt.adapt(to, grid.global_geometry), + Adapt.adapt(to, grid.local_geometry), + ) @@ -233,7 +246,7 @@ where ``\\tilde{A}^e`` is the approximated area given by the sum of the interior Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSphere`](@ref) and [`Meshes.EquidistantCubedSphere`](@ref) type, not for [`Meshes.ConformalCubedSphere`](@ref). """ -@memoize WeakValueDict function SpectralElementSpace2D( +@memoize WeakValueDict function SpectralElementGrid2D( topology, quadrature_style; enable_bubble = false, @@ -511,7 +524,7 @@ Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSp internal_surface_geometry = nothing boundary_surface_geometries = nothing end - return SpectralElementSpace2D( + return SpectralElementGrid2D( topology, quadrature_style, global_geometry, @@ -524,10 +537,18 @@ Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSp ) end +struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace + grid::G +end + +SpectralElementSpace2D(topology::Topologies.Topology2D, quadrature_style) = +SpectralElementSpace2D(SpectralElementGrid2D(topology, quadrature_style)) + nlevels(space::SpectralElementSpace2D) = 1 perimeter(space::SpectralElementSpace2D) = Perimeter2D(Quadratures.degrees_of_freedom(space.quadrature_style)) + #= const RectilinearSpectralElementSpace2D = SpectralElementSpace2D{ <:Topologies.Topology2D{ <:ClimaComms.AbstractCommsContext, @@ -541,7 +562,7 @@ const CubedSphereSpectralElementSpace2D = SpectralElementSpace2D{ <:Meshes.AbstractCubedSphere, }, } - +=# function compute_local_geometry( global_geometry::Geometry.SphericalGlobalGeometry, topology, From 8b74636b23d2bd7d8d2999ddf3e68c6de3631b35 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 2 Oct 2023 12:46:30 -0700 Subject: [PATCH 08/42] more --- src/Spaces/finitedifference.jl | 17 +++++++++++++++-- src/Spaces/spectralelement.jl | 13 +++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 6278a505f3..970bcae42a 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -127,8 +127,6 @@ FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) = - - abstract type Staggering end """ @@ -145,6 +143,17 @@ Cell face location """ struct CellFace <: Staggering end +# accessors + +local_geometry_data(::CellCenter, grid::FiniteDifferenceGrid) = + grid.center_local_geometry +local_geometry_data(::CellFace, grid::FiniteDifferenceGrid) = + grid.face_local_geometry + + + + + abstract type AbstractFiniteDifferenceSpace<: AbstractSpace end """ @@ -162,6 +171,10 @@ end const FaceFiniteDifferenceSpace = FiniteDifferenceSpace{CellFace} const CenterFiniteDifferenceSpace = FiniteDifferenceSpace{CellCenter} +local_geometry_data(space::AbstractFiniteDifferenceSpace) = + local_geometry_data(space.staggering, space.grid) + + function Base.show(io::IO, space::FiniteDifferenceSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 58d7af9aaf..bb1cbfdcd2 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -3,8 +3,8 @@ abstract type AbstractSpectralElementSpace <: AbstractSpace end Topologies.nlocalelems(space::AbstractSpectralElementSpace) = Topologies.nlocalelems(Spaces.topology(space)) -local_geometry_data(space::AbstractSpectralElementSpace) = space.local_geometry -ghost_geometry_data(space::AbstractSpectralElementSpace) = space.ghost_geometry +local_geometry_data(space::AbstractSpectralElementSpace) = local_geometry_data(space.grid) +ghost_geometry_data(space::AbstractSpectralElementSpace) = ghost_geometry_data(space.grid) eachslabindex(space::AbstractSpectralElementSpace) = 1:Topologies.nlocalelems(Spaces.topology(space)) @@ -153,6 +153,9 @@ end ) end +local_geometry_data(grid::SpectralElementGrid1D) = grid.local_geometry + + struct SpectralElementSpace1D{G} <: AbstractSpectralElementSpace grid::G @@ -537,6 +540,12 @@ Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSp ) end + +local_geometry_data(grid::SpectralElementGrid2D) = grid.local_geometry +ghost_geometry_data(grid::SpectralElementGrid2D) = grid.ghost_geometry + + + struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace grid::G end From 872aef1c0a4e63796f0e4cb0f137f4cb8c89a177 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 2 Oct 2023 14:02:32 -0700 Subject: [PATCH 09/42] more --- src/Fields/indices.jl | 2 +- src/Hypsography/Hypsography.jl | 2 +- src/Spaces/Spaces.jl | 3 + src/Spaces/dss.jl | 20 ++--- src/Spaces/extruded.jl | 78 +++++++++++++------- src/Spaces/spectralelement.jl | 131 ++++++++++++++++++--------------- test/Spaces/spaces.jl | 16 ++-- test/runtests.jl | 3 +- 8 files changed, 145 insertions(+), 110 deletions(-) diff --git a/src/Fields/indices.jl b/src/Fields/indices.jl index 4fa0c3ed58..3491a99962 100644 --- a/src/Fields/indices.jl +++ b/src/Fields/indices.jl @@ -203,7 +203,7 @@ function byslab( ::ClimaComms.CPUSingleThreaded, space::Spaces.AbstractSpectralElementSpace, ) - Nh = Topologies.nlocalelems(space.topology)::Int + Nh = Topologies.nlocalelems(Spaces.topology(space))::Int @inbounds for h in 1:Nh fn(SlabIndex(nothing, h)) end diff --git a/src/Hypsography/Hypsography.jl b/src/Hypsography/Hypsography.jl index a1a788dd99..77ce4ee44d 100644 --- a/src/Hypsography/Hypsography.jl +++ b/src/Hypsography/Hypsography.jl @@ -54,7 +54,7 @@ LinearAdaption() = LinearAdaption(nothing) ) # linear coordinates -function ExtrudedFiniteDifferenceSpace( +function ExtrudedFiniteDifferenceGrid( horizontal_space::Spaces.AbstractSpace, vertical_space::Spaces.FiniteDifferenceSpace, adaption::HypsographyAdaption, diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index 01b0bc1f4c..d0ecfc2898 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -25,6 +25,9 @@ import ClimaComms using StaticArrays, ForwardDiff, LinearAlgebra, UnPack, Adapt using Memoize, WeakValueDicts +abstract type AbstractGrid end + + abstract type AbstractSpace end issubspace(::AbstractSpace, ::AbstractSpace) = false diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl index ab448fae47..3915dd577c 100644 --- a/src/Spaces/dss.jl +++ b/src/Spaces/dss.jl @@ -43,10 +43,10 @@ function create_dss_buffer( data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, hspace::AbstractSpectralElementSpace, ) where {S, Nij} - @assert hspace.quadrature_style isa Spaces.Quadratures.GLL "DSS2 is only compatible with GLL quadrature" - topology = hspace.topology + @assert quadrature_style(hspace) isa Spaces.Quadratures.GLL "DSS2 is only compatible with GLL quadrature" + topology = topology(hspace) local_geometry = local_geometry_data(hspace) - local_weights = hspace.local_dss_weights + local_weights = hspace.grid.local_dss_weights perimeter = Spaces.perimeter(hspace) create_dss_buffer(data, topology, perimeter, local_geometry, local_weights) end @@ -303,7 +303,7 @@ function weighted_dss_start!( ) assert_same_eltype(data, dss_buffer) length(parent(data)) == 0 && return nothing - device = ClimaComms.device(hspace.topology) + device = ClimaComms.device(topology(hspace)) dss_transform!( device, dss_buffer, @@ -317,7 +317,7 @@ function weighted_dss_start!( device, dss_buffer.perimeter_data, Spaces.perimeter(hspace), - hspace.topology, + topology(hspace), ) fill_send_buffer!(device, dss_buffer) ClimaComms.start(dss_buffer.graph_context) @@ -373,13 +373,13 @@ function weighted_dss_internal!( length(parent(data)) == 0 && return nothing if hspace isa SpectralElementSpace1D dss_1d!( - hspace.topology, + topology(hspace), data, local_geometry_data(space), hspace.dss_weights, ) else - device = ClimaComms.device(hspace.topology) + device = ClimaComms.device(topology(hspace)) dss_transform!( device, dss_buffer, @@ -393,7 +393,7 @@ function weighted_dss_internal!( device, dss_buffer.perimeter_data, Spaces.perimeter(hspace), - hspace.topology, + topology(hspace), ) dss_untransform!( device, @@ -451,14 +451,14 @@ function weighted_dss_ghost!( ) assert_same_eltype(data, dss_buffer) length(parent(data)) == 0 && return data - device = ClimaComms.device(hspace.topology) + device = ClimaComms.device(topology(hspace)) ClimaComms.finish(dss_buffer.graph_context) load_from_recv_buffer!(device, dss_buffer) dss_ghost!( device, dss_buffer.perimeter_data, Spaces.perimeter(hspace), - hspace.topology, + topology(hspace), ) dss_untransform!( device, diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index ad82cccf09..01d8630bc7 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -22,14 +22,14 @@ struct Flat <: HypsographyAdaption end Construct an `ExtrudedFiniteDifferenceGrid` from the horizontal and vertical spaces. """ mutable struct ExtrudedFiniteDifferenceGrid{ - H <: AbstractSpace, + H <: AbstractGrid, T <: Topologies.AbstractIntervalTopology, A <: HypsographyAdaption, GG <: Geometry.AbstractGlobalGeometry, LG, LGG, -} - horizontal_space::H +} <: AbstractGrid + horizontal_grid::H vertical_topology::T # should we cache the vertical grid? hypsography::A global_geometry::GG @@ -40,40 +40,40 @@ mutable struct ExtrudedFiniteDifferenceGrid{ end @memoize WeakValueDict function ExtrudedFiniteDifferenceGrid( - horizontal_space::AbstractSpace, + horizontal_grid::Union{SpectralElementGrid1D,SpectralElementGrid2D}, vertical_grid::FiniteDifferenceGrid, hypsography::Flat = Flat(), ) vertical_topology = vertical_grid.topology - global_geometry = horizontal_space.global_geometry + global_geometry = horizontal_grid.global_geometry center_local_geometry = product_geometry.( - horizontal_space.local_geometry, - vertical_space.center_local_geometry, + horizontal_grid.local_geometry, + vertical_grid.center_local_geometry, ) face_local_geometry = product_geometry.( - horizontal_space.local_geometry, - vertical_space.face_local_geometry, + horizontal_grid.local_geometry, + vertical_grid.face_local_geometry, ) - if horizontal_space isa SpectralElementSpace2D + if horizontal_grid isa SpectralElementGrid2D center_ghost_geometry = product_geometry.( - horizontal_space.ghost_geometry, - vertical_space.center_local_geometry, + horizontal_grid.ghost_geometry, + vertical_grid.center_local_geometry, ) face_ghost_geometry = product_geometry.( - horizontal_space.ghost_geometry, - vertical_space.face_local_geometry, + horizontal_grid.ghost_geometry, + vertical_grid.face_local_geometry, ) else center_ghost_geometry = nothing face_ghost_geometry = nothing end return ExtrudedFiniteDifferenceGrid( - horizontal_space, + horizontal_grid, vertical_topology, hypsography, global_geometry, @@ -87,7 +87,6 @@ end - struct ExtrudedFiniteDifferenceSpace{ S <: Staggering, G <: ExtrudedFiniteDifferenceGrid, @@ -110,6 +109,21 @@ ExtrudedFiniteDifferenceSpace{S}( ) where {S <: Staggering} = ExtrudedFiniteDifferenceSpace{S}(space.grid) +function ExtrudedFiniteDifferenceSpace( + horizontal_space::AbstractSpace, + vertical_space::FiniteDifferenceSpace{S}, + hypsography::HypsographyAdaption = Flat(), +) where {S <: Staggering} + grid = ExtrudedFiniteDifferenceGrid( + horizontal_space.grid, + vertical_space.grid, + hypsography, + ) + return ExtrudedFiniteDifferenceSpace{S}(grid) +end + + + face_space(space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace{CellFace}(space) center_space(space::ExtrudedFiniteDifferenceSpace) = @@ -126,6 +140,7 @@ ExtrudedFiniteDifferenceSpace{S}( ExtrudedFiniteDifferenceGrid(horizontal_space, vertical_space, hypsography), ) +#= function issubspace( hspace::AbstractSpectralElementSpace, extruded_space::ExtrudedFiniteDifferenceSpace, @@ -139,7 +154,7 @@ function issubspace( quadrature_style(hspace) === quadrature_style(Spaces.horizontal_space(extrued_space)) end - +=# Adapt.adapt_structure(to, space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace( @@ -195,19 +210,28 @@ function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) print(iio, " "^(indent + 4), "mesh: ", space.vertical_topology.mesh) end -local_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = - space.grid.center_local_geometry -local_geometry_data(space::FaceExtrudedFiniteDifferenceSpace) = - space.grid.face_local_geometry -ghost_geometry_data(space::CenterExtrudedFiniteDifferenceSpace) = - space.grid.center_ghost_geometry -ghost_geometry_data(space::FaceExtrudedFiniteDifferenceSpace) = - space.grid.face_ghost_geometry +local_geometry_data(::CellCenter, grid::ExtrudedFiniteDifferenceGrid) = + grid.center_local_geometry +local_geometry_data(::CellFace, grid::ExtrudedFiniteDifferenceGrid) = + grid.face_local_geometry +local_geometry_data(space::ExtrudedFiniteDifferenceSpace) = + local_geometry_data(space.staggering, space.grid) + +ghost_geometry_data(::CellCenter, grid::ExtrudedFiniteDifferenceGrid) = + grid.center_ghost_geometry +ghost_geometry_data(::CellFace, grid::ExtrudedFiniteDifferenceGrid) = + grid.face_ghost_geometry +ghost_geometry_data(space::ExtrudedFiniteDifferenceSpace) = + ghost_geometry_data(space.staggering, space.grid) -quadrature_style(space::ExtrudedFiniteDifferenceGrid) = - space.grid.horizontal_space.quadrature_style +quadrature_style(grid::ExtrudedFiniteDifferenceGrid) = + quadrature_style(grid.horizontal_grid) +quadrature_style(space::ExtrudedFiniteDifferenceSpace) = + quadrature_style(space.grid) +topology(grid::ExtrudedFiniteDifferenceGrid) = + grid.horizontal_space.topology topology(space::ExtrudedFiniteDifferenceSpace) = space.grid.horizontal_space.topology diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index bb1cbfdcd2..1f1e68b225 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -5,6 +5,7 @@ Topologies.nlocalelems(space::AbstractSpectralElementSpace) = local_geometry_data(space::AbstractSpectralElementSpace) = local_geometry_data(space.grid) ghost_geometry_data(space::AbstractSpectralElementSpace) = ghost_geometry_data(space.grid) +quadrature_style(space::AbstractSpectralElementSpace) = quadrature_style(space.grid) eachslabindex(space::AbstractSpectralElementSpace) = 1:Topologies.nlocalelems(Spaces.topology(space)) @@ -13,68 +14,25 @@ function Base.show(io::IO, space::AbstractSpectralElementSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) println(io, nameof(typeof(space)), ":") - if hasfield(typeof(space), :topology) + grid = space.grid + if hasfield(typeof(grid), :topology) # some reduced spaces (like slab space) do not have topology print(iio, " "^(indent + 2), "context: ") - Topologies.print_context(iio, space.topology.context) + Topologies.print_context(iio, grid.topology.context) println(iio) - println(iio, " "^(indent + 2), "mesh: ", space.topology.mesh) + println(iio, " "^(indent + 2), "mesh: ", grid.topology.mesh) end - print(iio, " "^(indent + 2), "quadrature: ", space.quadrature_style) + print(iio, " "^(indent + 2), "quadrature: ", grid.quadrature_style) end ClimaComms.device(space::AbstractSpectralElementSpace) = ClimaComms.device(topology(space)) ClimaComms.array_type(space::AbstractSpectralElementSpace) = ClimaComms.array_type(ClimaComms.device(space)) -topology(space::AbstractSpectralElementSpace) = space.topology -quadrature_style(space::AbstractSpectralElementSpace) = space.quadrature_style +topology(space::AbstractSpectralElementSpace) = topology(space.grid) -abstract type AbstractPerimeter end - -""" - Perimeter2D <: AbstractPerimeter - -Iterate over the perimeter degrees of freedom of a 2D spectral element. -""" -struct Perimeter2D{Nq} <: AbstractPerimeter end - -""" - Perimeter2D(Nq) - -Construct a perimeter iterator for a 2D spectral element of degree `(Nq-1)`. -""" -Perimeter2D(Nq) = Perimeter2D{Nq}() -Adapt.adapt_structure(to, x::Perimeter2D) = x - -function Base.iterate(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} - if loc < 5 - return (Topologies.vertex_node_index(loc, Nq), loc + 1) - elseif loc ≤ nperimeter2d(Nq) - f = cld(loc - 4, Nq - 2) - n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) - return (Topologies.face_node_index(f, Nq, 1 + n), loc + 1) - else - return nothing - end -end - -function Base.getindex(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} - if loc < 1 || loc > nperimeter2d(Nq) - return (-1, -1) - elseif loc < 5 - return Topologies.vertex_node_index(loc, Nq) - else - f = cld(loc - 4, Nq - 2) - n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) - return Topologies.face_node_index(f, Nq, 1 + n) - end -end - -nperimeter2d(Nq) = 4 + (Nq - 2) * 4 -nperimeter(::Perimeter2D{Nq}) where {Nq} = nperimeter2d(Nq) -Base.length(::Perimeter2D{Nq}) where {Nq} = nperimeter2d(Nq) +abstract type AbstractSpectralElementGrid <: AbstractGrid end """ SpectralElementGrid1D @@ -87,7 +45,7 @@ mutable struct SpectralElementGrid1D{ GG <: Geometry.AbstractGlobalGeometry, LG, D, -} <: AbstractSpectralElementSpace +} <: AbstractSpectralElementGrid topology::T quadrature_style::Q global_geometry::GG @@ -154,7 +112,8 @@ end end local_geometry_data(grid::SpectralElementGrid1D) = grid.local_geometry - +topology(grid::SpectralElementGrid1D) = grid.topology +quadrature_style(grid::SpectralElementGrid1D) = grid.quadrature_style struct SpectralElementSpace1D{G} <: AbstractSpectralElementSpace @@ -191,7 +150,7 @@ mutable struct SpectralElementGrid2D{ D, IS, BS, -} +} <: AbstractSpectralElementGrid topology::T quadrature_style::Q global_geometry::GG @@ -203,11 +162,12 @@ mutable struct SpectralElementGrid2D{ boundary_surface_geometries::BS end -struct DeviceSpectralElementGrid2D{Q,GG,LG} +struct DeviceSpectralElementGrid2D{Q,GG,LG} <: AbstractGrid quadrature_style::Q global_geometry::GG local_geometry::LG -end +end + Adapt.adapt_structure(to, grid::SpectralElementGrid2D) = DeviceSpectralElementGrid2D( Adapt.adapt(to, grid.quadrature_style), @@ -543,7 +503,8 @@ end local_geometry_data(grid::SpectralElementGrid2D) = grid.local_geometry ghost_geometry_data(grid::SpectralElementGrid2D) = grid.ghost_geometry - +topology(grid::SpectralElementGrid2D) = grid.topology +quadrature_style(grid::SpectralElementGrid2D) = grid.quadrature_style struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace @@ -551,11 +512,13 @@ struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace end SpectralElementSpace2D(topology::Topologies.Topology2D, quadrature_style) = -SpectralElementSpace2D(SpectralElementGrid2D(topology, quadrature_style)) + SpectralElementSpace2D(SpectralElementGrid2D(topology, quadrature_style)) nlevels(space::SpectralElementSpace2D) = 1 perimeter(space::SpectralElementSpace2D) = - Perimeter2D(Quadratures.degrees_of_freedom(space.quadrature_style)) + Perimeter2D(Quadratures.degrees_of_freedom(quadrature_style(space))) + + #= const RectilinearSpectralElementSpace2D = SpectralElementSpace2D{ @@ -719,8 +682,8 @@ function setup_comms( end function all_nodes(space::SpectralElementSpace2D) - Nq = Quadratures.degrees_of_freedom(space.quadrature_style) - nelem = Topologies.nlocalelems(space.topology) + Nq = Quadratures.degrees_of_freedom(quadrature_style(space)) + nelem = Topologies.nlocalelems(topology(space)) Iterators.product(Iterators.product(1:Nq, 1:Nq), 1:nelem) end @@ -733,7 +696,7 @@ first `((i,j), e)` triple. This function is experimental, and may change in future. """ unique_nodes(space::SpectralElementSpace2D) = - unique_nodes(space, space.quadrature_style) + unique_nodes(space, quadrature_style(space)) unique_nodes(space::SpectralElementSpace2D, quad::Quadratures.QuadratureStyle) = UniqueNodeIterator(space) @@ -827,3 +790,49 @@ function Base.iterate( return ((i, j), e), ((i, j), e) end end + + +abstract type AbstractPerimeter end + +""" + Perimeter2D <: AbstractPerimeter + +Iterate over the perimeter degrees of freedom of a 2D spectral element. +""" +struct Perimeter2D{Nq} <: AbstractPerimeter end + +""" + Perimeter2D(Nq) + +Construct a perimeter iterator for a 2D spectral element of degree `(Nq-1)`. +""" +Perimeter2D(Nq) = Perimeter2D{Nq}() +Adapt.adapt_structure(to, x::Perimeter2D) = x + +function Base.iterate(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} + if loc < 5 + return (Topologies.vertex_node_index(loc, Nq), loc + 1) + elseif loc ≤ nperimeter2d(Nq) + f = cld(loc - 4, Nq - 2) + n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) + return (Topologies.face_node_index(f, Nq, 1 + n), loc + 1) + else + return nothing + end +end + +function Base.getindex(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} + if loc < 1 || loc > nperimeter2d(Nq) + return (-1, -1) + elseif loc < 5 + return Topologies.vertex_node_index(loc, Nq) + else + f = cld(loc - 4, Nq - 2) + n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) + return Topologies.face_node_index(f, Nq, 1 + n) + end +end + +nperimeter2d(Nq) = 4 + (Nq - 2) * 4 +nperimeter(::Perimeter2D{Nq}) where {Nq} = nperimeter2d(Nq) +Base.length(::Perimeter2D{Nq}) where {Nq} = nperimeter2d(Nq) diff --git a/test/Spaces/spaces.jl b/test/Spaces/spaces.jl index fe151942c9..e58dfe7e95 100644 --- a/test/Spaces/spaces.jl +++ b/test/Spaces/spaces.jl @@ -36,8 +36,8 @@ import ClimaCore.DataLayouts: IJFH, VF @test coord_slab[1] == Geometry.XPoint{FT}(-3) @test coord_slab[4] == Geometry.XPoint{FT}(5) - local_geometry_slab = slab(space.local_geometry, 1) - dss_weights_slab = slab(space.dss_weights, 1) + local_geometry_slab = slab(Spaces.local_geometry_data(space), 1) + dss_weights_slab = slab(space.grid.dss_weights, 1) for i in 1:4 @test Geometry.components(local_geometry_slab[i].∂x∂ξ) ≈ @@ -178,8 +178,8 @@ end @test coord_slab[1, 4] ≈ Geometry.XYPoint{FT}(-3.0, 8.0) @test coord_slab[4, 4] ≈ Geometry.XYPoint{FT}(5.0, 8.0) - local_geometry_slab = slab(space.local_geometry, 1) - dss_weights_slab = slab(space.local_dss_weights, 1) + local_geometry_slab = slab(space.grid.local_geometry, 1) + dss_weights_slab = slab(space.grid.local_dss_weights, 1) for i in 1:4, j in 1:4 @@ -197,10 +197,10 @@ end end end - @test length(space.boundary_surface_geometries) == 2 - @test keys(space.boundary_surface_geometries) == (:south, :north) - @test sum(parent(space.boundary_surface_geometries.north.sWJ)) ≈ 8 - @test parent(space.boundary_surface_geometries.north.normal)[1, :, 1] ≈ + @test length(space.grid.boundary_surface_geometries) == 2 + @test keys(space.grid.boundary_surface_geometries) == (:south, :north) + @test sum(parent(space.grid.boundary_surface_geometries.north.sWJ)) ≈ 8 + @test parent(space.grid.boundary_surface_geometries.north.normal)[1, :, 1] ≈ [0.0, 1.0] point_space = Spaces.column(space, 1, 1, 1) diff --git a/test/runtests.jl b/test/runtests.jl index 10f9037ccb..08810ce591 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -35,7 +35,7 @@ if !Sys.iswindows() @safetestset "Cubedsphere surface topology" begin @time include("Topologies/cubedsphere_sfc.jl") end # now part of buildkite # @safetestset "Distributed topology" begin @time include("Topologies/distributed.jl") end - + =# @safetestset "Quadrature" begin @time include("Spaces/quadrature.jl") end @safetestset "Spaces" begin @time include("Spaces/spaces.jl") end #= @@ -69,7 +69,6 @@ if !Sys.iswindows() # now part of buildkite # @time include("Operators/finitedifference/implicit_stencils.jl") # @time include("Operators/finitedifference/opt_implicit_stencils.jl") -=# @safetestset "Hybrid - 2D" begin @time include("Operators/hybrid/2d.jl") end @safetestset "Hybrid - 3D" begin @time include("Operators/hybrid/3d.jl") end @safetestset "Hybrid - dss opt" begin @time include("Operators/hybrid/dss_opt.jl") end From 4eb2b14c2096113a5152c0e2b01fa4fb5136bd4b Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 3 Oct 2023 09:59:05 -0700 Subject: [PATCH 10/42] more changes --- src/Fields/Fields.jl | 10 --- src/Hypsography/Hypsography.jl | 60 ++++++++-------- src/MatrixFields/matrix_shape.jl | 4 +- src/Operators/finitedifference.jl | 33 ++++----- src/Operators/remapping.jl | 26 ++----- src/Spaces/Spaces.jl | 9 +-- src/Spaces/dss.jl | 17 +++-- src/Spaces/extruded.jl | 70 ++++++------------ src/Spaces/finitedifference.jl | 25 ++++--- src/Spaces/spectralelement.jl | 72 +++++++++---------- test/Operators/finitedifference/column.jl | 26 +++---- test/Operators/hybrid/2d.jl | 3 +- test/Operators/remapping.jl | 16 ++--- test/Operators/spectralelement/rectilinear.jl | 12 ++-- test/runtests.jl | 4 +- 15 files changed, 173 insertions(+), 214 deletions(-) diff --git a/src/Fields/Fields.jl b/src/Fields/Fields.jl index caf204f445..ff6ebf5eb7 100644 --- a/src/Fields/Fields.jl +++ b/src/Fields/Fields.jl @@ -327,16 +327,6 @@ function interpcoord(elemrange, x::Real) return z, ξ end -""" - Spaces.variational_solve!(field) - -Divide `field` by the mass matrix. -""" -function Spaces.variational_solve!(field::Field) - Spaces.variational_solve!(field_values(field), axes(field)) - return field -end - """ Spaces.weighted_dss!(f::Field[, ghost_buffer = Spaces.create_dss_buffer(field)]) diff --git a/src/Hypsography/Hypsography.jl b/src/Hypsography/Hypsography.jl index 77ce4ee44d..7b1a843d18 100644 --- a/src/Hypsography/Hypsography.jl +++ b/src/Hypsography/Hypsography.jl @@ -2,9 +2,13 @@ module Hypsography import ..slab, ..column import ..Geometry, ..Domains, ..Topologies, ..Spaces, ..Fields, ..Operators -import ..Spaces: ExtrudedFiniteDifferenceSpace, HypsographyAdaption, Flat +import ..Spaces: + ExtrudedFiniteDifferenceSpace, + ExtrudedFiniteDifferenceGrid, + HypsographyAdaption, + Flat -using StaticArrays, LinearAlgebra +using StaticArrays, LinearAlgebra, Memoize, WeakValueDicts """ @@ -54,35 +58,35 @@ LinearAdaption() = LinearAdaption(nothing) ) # linear coordinates -function ExtrudedFiniteDifferenceGrid( - horizontal_space::Spaces.AbstractSpace, - vertical_space::Spaces.FiniteDifferenceSpace, +@memoize WeakValueDict function ExtrudedFiniteDifferenceGrid( + horizontal_grid::Spaces.AbstractGrid, + vertical_grid::Spaces.FiniteDifferenceGrid, adaption::HypsographyAdaption, ) if adaption isa LinearAdaption if isnothing(adaption.surface) error("LinearAdaption requires a Field argument") end - if axes(adaption.surface) !== horizontal_space + if axes(adaption.surface).grid !== horizontal_grid error("Terrain must be defined on the horizontal space") end end # construct initial flat space, then mutate - space = Spaces.ExtrudedFiniteDifferenceSpace( - horizontal_space, - vertical_space, + ref_grid = Spaces.ExtrudedFiniteDifferenceGrid( + horizontal_grid, + vertical_grid, Flat(), ) - face_space = Spaces.FaceExtrudedFiniteDifferenceSpace(space) - coord_type = eltype(Spaces.coordinates_data(face_space)) - z_ref = Spaces.coordinates_data(face_space).z + face_ref_space = Spaces.FaceExtrudedFiniteDifferenceSpace(ref_grid) + face_ref_coords = Spaces.coordinates_data(face_ref_space) + coord_type = eltype(face_ref_coords) + z_ref = face_ref_coords.z - vertical_domain = Topologies.domain(space.vertical_topology) + vertical_domain = Topologies.domain(vertical_grid) z_top = vertical_domain.coord_max.z grad = Operators.Gradient() - wdiv = Operators.WeakDivergence() z_surface = Fields.field_values(adaption.surface) FT = eltype(z_surface) @@ -90,7 +94,7 @@ function ExtrudedFiniteDifferenceGrid( # TODO: Function dispatch if adaption isa LinearAdaption fZ_data = @. z_ref + (1 - z_ref / z_top) * z_surface - fZ = Fields.Field(fZ_data, face_space) + fZ = Fields.Field(fZ_data, face_ref_space) elseif adaption isa SLEVEAdaption ηₕ = adaption.ηₕ s = adaption.s @@ -106,7 +110,7 @@ function ExtrudedFiniteDifferenceGrid( η * z_top + z_surface * (sinh((ηₕ - η) / s / ηₕ)) / (sinh(1 / s)), η * z_top, ) - fZ = Fields.Field(fZ_data, face_space) + fZ = Fields.Field(fZ_data, face_ref_space) end # Take the horizontal gradient for the Z surface field @@ -127,12 +131,15 @@ function ExtrudedFiniteDifferenceGrid( c∇Z = If2c.(f∇Z) Spaces.weighted_dss!(c∇Z) - Ni, Nj, _, Nv, Nh = size(space.center_local_geometry) + face_local_geometry = copy(ref_grid.face_local_geometry) + center_local_geometry = copy(ref_grid.center_local_geometry) + + Ni, Nj, _, Nv, Nh = size(center_local_geometry) for h in 1:Nh, j in 1:Nj, i in 1:Ni - face_column = column(space.face_local_geometry, i, j, h) + face_column = column(face_local_geometry, i, j, h) fZ_column = column(Fields.field_values(fZ), i, j, h) f∇Z_column = column(Fields.field_values(f∇Z), i, j, h) - center_column = column(space.center_local_geometry, i, j, h) + center_column = column(center_local_geometry, i, j, h) cZ_column = column(Fields.field_values(cZ), i, j, h) c∇Z_column = column(Fields.field_values(c∇Z), i, j, h) @@ -183,16 +190,13 @@ function ExtrudedFiniteDifferenceGrid( end end - return Spaces.ExtrudedFiniteDifferenceSpace( - space.staggering, - Spaces.horizontal_space(space), - space.vertical_topology, + return ExtrudedFiniteDifferenceGrid( + horizontal_grid, + Spaces.topology(vertical_grid), adaption, - space.global_geometry, - space.center_local_geometry, - space.face_local_geometry, - space.center_ghost_geometry, - space.face_ghost_geometry, + ref_grid.global_geometry, + center_local_geometry, + face_local_geometry, ) end diff --git a/src/MatrixFields/matrix_shape.jl b/src/MatrixFields/matrix_shape.jl index 64ae442c46..6ebe32a1c8 100644 --- a/src/MatrixFields/matrix_shape.jl +++ b/src/MatrixFields/matrix_shape.jl @@ -17,8 +17,8 @@ matrix_shape(matrix_field, matrix_space = axes(matrix_field)) = _matrix_shape( ) _matrix_shape(::Type{Int}, _) = Square() -#_matrix_shape(::Type{PlusHalf{Int}}, ::Spaces.CellCenter) = FaceToCenter() -#_matrix_shape(::Type{PlusHalf{Int}}, ::Spaces.CellFace) = CenterToFace() +_matrix_shape(::Type{PlusHalf{Int}}, ::Spaces.CellCenter) = FaceToCenter() +_matrix_shape(::Type{PlusHalf{Int}}, ::Spaces.CellFace) = CenterToFace() """ column_axes(matrix_field, [matrix_space]) diff --git a/src/Operators/finitedifference.jl b/src/Operators/finitedifference.jl index 59f9d2af2f..01f1975336 100644 --- a/src/Operators/finitedifference.jl +++ b/src/Operators/finitedifference.jl @@ -2,14 +2,19 @@ import ..Utilities: PlusHalf, half const AllFiniteDifferenceSpace = Union{Spaces.FiniteDifferenceSpace, Spaces.ExtrudedFiniteDifferenceSpace} -const AllFaceFiniteDifferenceSpace = - Union{Spaces.FaceFiniteDifferenceSpace, Spaces.FaceExtrudedFiniteDifferenceSpace} -const AllCenterFiniteDifferenceSpace = - Union{Spaces.CenterFiniteDifferenceSpace, Spaces.CenterExtrudedFiniteDifferenceSpace} +const AllFaceFiniteDifferenceSpace = Union{ + Spaces.FaceFiniteDifferenceSpace, + Spaces.FaceExtrudedFiniteDifferenceSpace, +} +const AllCenterFiniteDifferenceSpace = Union{ + Spaces.CenterFiniteDifferenceSpace, + Spaces.CenterExtrudedFiniteDifferenceSpace, +} -left_idx(space::AllCenterFiniteDifferenceSpace) = left_center_boundary_idx(space) +left_idx(space::AllCenterFiniteDifferenceSpace) = + left_center_boundary_idx(space) right_idx(space::AllCenterFiniteDifferenceSpace) = right_center_boundary_idx(space) left_idx(space::AllFaceFiniteDifferenceSpace) = left_face_boundary_idx(space) @@ -39,13 +44,8 @@ Base.@propagate_inbounds function Geometry.LocalGeometry( v = mod1(v, length(space)) end i, j, h = hidx - return @inbounds Spaces.center_space(space).center_local_geometry[CartesianIndex( - i, - j, - 1, - v, - h, - )] + local_geom = Spaces.local_geometry_data(Spaces.CellCenter(), space.grid) + return @inbounds local_geom[CartesianIndex(i, j, 1, v, h)] end Base.@propagate_inbounds function Geometry.LocalGeometry( space::AllFiniteDifferenceSpace, @@ -57,13 +57,8 @@ Base.@propagate_inbounds function Geometry.LocalGeometry( v = mod1(v, length(space)) end i, j, h = hidx - return @inbounds Spaces.center_space(space).face_local_geometry[CartesianIndex( - i, - j, - 1, - v, - h, - )] + local_geom = Spaces.local_geometry_data(Spaces.CellFace(), space.grid) + return @inbounds local_geom[CartesianIndex(i, j, 1, v, h)] end diff --git a/src/Operators/remapping.jl b/src/Operators/remapping.jl index 0b4d37faca..42a864d6e1 100644 --- a/src/Operators/remapping.jl +++ b/src/Operators/remapping.jl @@ -67,28 +67,16 @@ end Computes local weights of the overlap mesh for `source` to `target` spaces. """ -function overlap( - target::T, - source::S, -) where { - T <: SpectralElementSpace1D{<:IntervalTopology}, - S <: SpectralElementSpace1D{<:IntervalTopology}, -} +function overlap(target::SpectralElementSpace1D, source::SpectralElementSpace1D) return x_overlap(target, source) end -function overlap( - target::T, - source::S, -) where { - T <: - SpectralElementSpace2D{<:Topology2D{<:ClimaComms.SingletonCommsContext}}, - S <: - SpectralElementSpace2D{<:Topology2D{<:ClimaComms.SingletonCommsContext}}, -} +function overlap(target::SpectralElementSpace2D, source::SpectralElementSpace2D) @assert ( - typeof(Spaces.topology(target).mesh) <: Meshes.RectilinearMesh && - typeof(Spaces.topology(source).mesh) <: Meshes.RectilinearMesh + ClimaComms.context(target) isa ClimaComms.SingletonCommsContext && + ClimaComms.context(source) isa ClimaComms.SingletonCommsContext && + Spaces.topology(target).mesh isa Meshes.RectilinearMesh && + Spaces.topology(source).mesh isa Meshes.RectilinearMesh ) X_ov = x_overlap(target, source) Y_ov = y_overlap(target, source) @@ -290,6 +278,6 @@ associated basis function. See [Ullrich2015] section 2. """ function local_weights(space::AbstractSpace) - wj = space.local_geometry.WJ + wj = Spaces.local_geometry_data(space).WJ return vec(parent(wj)) end diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index d0ecfc2898..d66bb9c407 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -36,7 +36,9 @@ undertype(space::AbstractSpace) = Geometry.undertype(eltype(local_geometry_data(space))) coordinates_data(space::AbstractSpace) = local_geometry_data(space).coordinates - +coordinates_data(grid::AbstractGrid) = local_geometry_data(grid).coordinates +coordinates_data(staggering, grid::AbstractGrid) = + local_geometry_data(staggering, grid).coordinates ClimaComms.context(space::Spaces.AbstractSpace) = ClimaComms.context(Spaces.topology(space)) @@ -52,11 +54,6 @@ include("triangulation.jl") include("dss_transform.jl") include("dss.jl") -horizontal_space(space::CenterExtrudedFiniteDifferenceSpace) = - space.horizontal_space -horizontal_space(space::FaceExtrudedFiniteDifferenceSpace) = - horizontal_space(center_space(space)) -horizontal_space(space::AbstractSpace) = space weighted_jacobian(space::Spaces.AbstractSpace) = local_geometry_data(space).WJ diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl index 3915dd577c..4670bc7820 100644 --- a/src/Spaces/dss.jl +++ b/src/Spaces/dss.jl @@ -44,11 +44,16 @@ function create_dss_buffer( hspace::AbstractSpectralElementSpace, ) where {S, Nij} @assert quadrature_style(hspace) isa Spaces.Quadratures.GLL "DSS2 is only compatible with GLL quadrature" - topology = topology(hspace) local_geometry = local_geometry_data(hspace) - local_weights = hspace.grid.local_dss_weights + local_weights = local_dss_weights(hspace) perimeter = Spaces.perimeter(hspace) - create_dss_buffer(data, topology, perimeter, local_geometry, local_weights) + create_dss_buffer( + data, + topology(hspace), + perimeter, + local_geometry, + local_weights, + ) end function create_dss_buffer( @@ -309,7 +314,7 @@ function weighted_dss_start!( dss_buffer, data, local_geometry_data(space), - hspace.local_dss_weights, + local_dss_weights(hspace), Spaces.perimeter(hspace), dss_buffer.perimeter_elems, ) @@ -376,7 +381,7 @@ function weighted_dss_internal!( topology(hspace), data, local_geometry_data(space), - hspace.dss_weights, + local_dss_weights(space), ) else device = ClimaComms.device(topology(hspace)) @@ -385,7 +390,7 @@ function weighted_dss_internal!( dss_buffer, data, local_geometry_data(space), - hspace.local_dss_weights, + local_dss_weights(space), Spaces.perimeter(hspace), dss_buffer.internal_elems, ) diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 01d8630bc7..4cf806affb 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -27,7 +27,6 @@ mutable struct ExtrudedFiniteDifferenceGrid{ A <: HypsographyAdaption, GG <: Geometry.AbstractGlobalGeometry, LG, - LGG, } <: AbstractGrid horizontal_grid::H vertical_topology::T # should we cache the vertical grid? @@ -35,12 +34,10 @@ mutable struct ExtrudedFiniteDifferenceGrid{ global_geometry::GG center_local_geometry::LG face_local_geometry::LG - center_ghost_geometry::LGG - face_ghost_geometry::LGG end @memoize WeakValueDict function ExtrudedFiniteDifferenceGrid( - horizontal_grid::Union{SpectralElementGrid1D,SpectralElementGrid2D}, + horizontal_grid::Union{SpectralElementGrid1D, SpectralElementGrid2D}, vertical_grid::FiniteDifferenceGrid, hypsography::Flat = Flat(), ) @@ -57,21 +54,6 @@ end vertical_grid.face_local_geometry, ) - if horizontal_grid isa SpectralElementGrid2D - center_ghost_geometry = - product_geometry.( - horizontal_grid.ghost_geometry, - vertical_grid.center_local_geometry, - ) - face_ghost_geometry = - product_geometry.( - horizontal_grid.ghost_geometry, - vertical_grid.face_local_geometry, - ) - else - center_ghost_geometry = nothing - face_ghost_geometry = nothing - end return ExtrudedFiniteDifferenceGrid( horizontal_grid, vertical_topology, @@ -79,8 +61,6 @@ end global_geometry, center_local_geometry, face_local_geometry, - center_ghost_geometry, - face_ghost_geometry, ) end @@ -122,7 +102,10 @@ function ExtrudedFiniteDifferenceSpace( return ExtrudedFiniteDifferenceSpace{S}(grid) end - +local_dss_weights(grid::ExtrudedFiniteDifferenceGrid) = + local_dss_weights(grid.horizontal_grid) +local_dss_weights(space::ExtrudedFiniteDifferenceSpace) = + local_dss_weights(space.grid) face_space(space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace{CellFace}(space) @@ -165,8 +148,6 @@ Adapt.adapt_structure(to, space::ExtrudedFiniteDifferenceSpace) = Adapt.adapt(to, space.global_geometry), Adapt.adapt(to, space.center_local_geometry), Adapt.adapt(to, space.face_local_geometry), - Adapt.adapt(to, space.center_ghost_geometry), - Adapt.adapt(to, space.face_ghost_geometry), ) @@ -218,30 +199,30 @@ local_geometry_data(::CellFace, grid::ExtrudedFiniteDifferenceGrid) = local_geometry_data(space::ExtrudedFiniteDifferenceSpace) = local_geometry_data(space.staggering, space.grid) -ghost_geometry_data(::CellCenter, grid::ExtrudedFiniteDifferenceGrid) = - grid.center_ghost_geometry -ghost_geometry_data(::CellFace, grid::ExtrudedFiniteDifferenceGrid) = - grid.face_ghost_geometry -ghost_geometry_data(space::ExtrudedFiniteDifferenceSpace) = - ghost_geometry_data(space.staggering, space.grid) - - quadrature_style(grid::ExtrudedFiniteDifferenceGrid) = quadrature_style(grid.horizontal_grid) quadrature_style(space::ExtrudedFiniteDifferenceSpace) = quadrature_style(space.grid) -topology(grid::ExtrudedFiniteDifferenceGrid) = - grid.horizontal_space.topology -topology(space::ExtrudedFiniteDifferenceSpace) = - space.grid.horizontal_space.topology +topology(grid::ExtrudedFiniteDifferenceGrid) = topology(grid.horizontal_grid) +topology(space::ExtrudedFiniteDifferenceSpace) = topology(space.grid) + + +ClimaComms.device(grid::ExtrudedFiniteDifferenceGrid) = + ClimaComms.device(topology(grid)) +ClimaComms.context(grid::ExtrudedFiniteDifferenceGrid) = + ClimaComms.context(topology(grid)) ClimaComms.device(space::ExtrudedFiniteDifferenceSpace) = ClimaComms.device(space.grid) ClimaComms.context(space::ExtrudedFiniteDifferenceSpace) = ClimaComms.context(space.grid) +horizontal_space(space::ExtrudedFiniteDifferenceSpace) = + AbstractSpectralElementSpace(space.grid.horizontal_grid) + +vertical_topology(grid::ExtrudedFiniteDifferenceGrid) = grid.vertical_topology vertical_topology(space::ExtrudedFiniteDifferenceSpace) = - space.grid.vertical_topology + vertical_topology(space.grid) Base.@propagate_inbounds function slab( space::ExtrudedFiniteDifferenceSpace, @@ -290,12 +271,8 @@ end vertical_topology(colgrid::ColumnGrid) = colgrid.full_grid.vertical_topology -function center_local_geometry_data(colgrid::ColumnGrid) - column(center_local_geometry_data(colgrid.full_grid), colgrid.colidx) -end -function face_local_geometry_data(colgrid::ColumnGrid) - column(face_local_geometry(colgrid.full_grid), colgrid.colidx) -end +local_geometry_data(staggering::Staggering, colgrid::ColumnGrid) = + column(local_geometry_data(staggering, colgrid.full_grid), colgrid.colidx) topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) @@ -335,11 +312,8 @@ function column(levelspace::LevelSpace, args...) end -nlevels(space::CenterExtrudedFiniteDifferenceSpace) = - size(space.center_local_geometry, 4) - -nlevels(space::FaceExtrudedFiniteDifferenceSpace) = - size(center_space(space).face_local_geometry, 4) +nlevels(space::ExtrudedFiniteDifferenceSpace) = + size(local_geometry_data(space), 4) function left_boundary_name(space::ExtrudedFiniteDifferenceSpace) boundaries = Topologies.boundaries(Spaces.vertical_topology(space)) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 970bcae42a..6294dd5254 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -1,4 +1,4 @@ -abstract type AbstractFiniteDifferenceGrid end +abstract type AbstractFiniteDifferenceGrid <: AbstractGrid end """ FiniteDifferenceGrid(topology::Topologies.IntervalTopology) @@ -23,6 +23,7 @@ mutable struct FiniteDifferenceGrid{ end topology(grid::FiniteDifferenceGrid) = grid.topology +Meshes.domain(grid::FiniteDifferenceGrid) = Meshes.domain(topology(grid)) ClimaComms.context(grid::FiniteDifferenceGrid) = ClimaComms.context(topology(grid)) @@ -125,8 +126,6 @@ FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) = - - abstract type Staggering end """ @@ -154,7 +153,7 @@ local_geometry_data(::CellFace, grid::FiniteDifferenceGrid) = -abstract type AbstractFiniteDifferenceSpace<: AbstractSpace end +abstract type AbstractFiniteDifferenceSpace <: AbstractSpace end """ FiniteDifferenceSpace(staggering::Staggering, grid::FiniteDifferenceGrid) @@ -162,7 +161,7 @@ abstract type AbstractFiniteDifferenceSpace<: AbstractSpace end """ struct FiniteDifferenceSpace{ S <: Staggering, - G <: AbstractFiniteDifferenceGrid + G <: AbstractFiniteDifferenceGrid, } <: AbstractFiniteDifferenceSpace staggering::S grid::G @@ -171,9 +170,13 @@ end const FaceFiniteDifferenceSpace = FiniteDifferenceSpace{CellFace} const CenterFiniteDifferenceSpace = FiniteDifferenceSpace{CellCenter} -local_geometry_data(space::AbstractFiniteDifferenceSpace) = +local_geometry_data(space::FiniteDifferenceSpace) = local_geometry_data(space.staggering, space.grid) +ClimaComms.context(space::FiniteDifferenceSpace) = + ClimaComms.context(space.grid) +ClimaComms.device(space::FiniteDifferenceSpace) = ClimaComms.device(space.grid) + function Base.show(io::IO, space::FiniteDifferenceSpace) indent = get(io, :indent, 0) @@ -192,13 +195,15 @@ end -FiniteDifferenceSpace{S}(grid::FiniteDifferenceGrid) where {S<:Staggering}= +FiniteDifferenceSpace{S}(grid::FiniteDifferenceGrid) where {S <: Staggering} = FiniteDifferenceSpace(S(), grid) -FiniteDifferenceSpace{S}(space::FiniteDifferenceSpace) where {S<:Staggering}= +FiniteDifferenceSpace{S}(space::FiniteDifferenceSpace) where {S <: Staggering} = FiniteDifferenceSpace(S(), space.grid) -FiniteDifferenceSpace{S}(topology::Topologies.IntervalTopology) where {S<:Staggering}= +FiniteDifferenceSpace{S}( + topology::Topologies.IntervalTopology, +) where {S <: Staggering} = FiniteDifferenceSpace{S}(FiniteDifferenceGrid(topology)) -FiniteDifferenceSpace{S}(mesh::Meshes.IntervalMesh) where {S<:Staggering}= +FiniteDifferenceSpace{S}(mesh::Meshes.IntervalMesh) where {S <: Staggering} = FiniteDifferenceSpace{S}(FiniteDifferenceGrid(mesh)) face_space(space::FiniteDifferenceSpace) = diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 1f1e68b225..2565cfb98c 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -3,9 +3,11 @@ abstract type AbstractSpectralElementSpace <: AbstractSpace end Topologies.nlocalelems(space::AbstractSpectralElementSpace) = Topologies.nlocalelems(Spaces.topology(space)) -local_geometry_data(space::AbstractSpectralElementSpace) = local_geometry_data(space.grid) -ghost_geometry_data(space::AbstractSpectralElementSpace) = ghost_geometry_data(space.grid) -quadrature_style(space::AbstractSpectralElementSpace) = quadrature_style(space.grid) +local_geometry_data(space::AbstractSpectralElementSpace) = + local_geometry_data(space.grid) + +quadrature_style(space::AbstractSpectralElementSpace) = + quadrature_style(space.grid) eachslabindex(space::AbstractSpectralElementSpace) = 1:Topologies.nlocalelems(Spaces.topology(space)) @@ -30,6 +32,7 @@ ClimaComms.device(space::AbstractSpectralElementSpace) = ClimaComms.array_type(space::AbstractSpectralElementSpace) = ClimaComms.array_type(ClimaComms.device(space)) topology(space::AbstractSpectralElementSpace) = topology(space.grid) +horizontal_space(space::AbstractSpectralElementSpace) = space abstract type AbstractSpectralElementGrid <: AbstractGrid end @@ -115,6 +118,8 @@ local_geometry_data(grid::SpectralElementGrid1D) = grid.local_geometry topology(grid::SpectralElementGrid1D) = grid.topology quadrature_style(grid::SpectralElementGrid1D) = grid.quadrature_style +local_dss_weights(grid::SpectralElementGrid1D) = grid.dss_weights + struct SpectralElementSpace1D{G} <: AbstractSpectralElementSpace grid::G @@ -128,7 +133,8 @@ function SpectralElementSpace1D( SpectralElementSpace1D(grid) end - +AbstractSpectralElementSpace(grid::SpectralElementGrid1D) = + SpectralElementSpace1D(grid) nlevels(space::SpectralElementSpace1D) = 1 @@ -155,25 +161,23 @@ mutable struct SpectralElementGrid2D{ quadrature_style::Q global_geometry::GG local_geometry::LG - ghost_geometry::LG local_dss_weights::D - ghost_dss_weights::D internal_surface_geometry::IS boundary_surface_geometries::BS end -struct DeviceSpectralElementGrid2D{Q,GG,LG} <: AbstractGrid +struct DeviceSpectralElementGrid2D{Q, GG, LG} <: AbstractGrid quadrature_style::Q global_geometry::GG local_geometry::LG -end +end Adapt.adapt_structure(to, grid::SpectralElementGrid2D) = DeviceSpectralElementGrid2D( - Adapt.adapt(to, grid.quadrature_style), - Adapt.adapt(to, grid.global_geometry), - Adapt.adapt(to, grid.local_geometry), - ) + Adapt.adapt(to, grid.quadrature_style), + Adapt.adapt(to, grid.global_geometry), + Adapt.adapt(to, grid.local_geometry), + ) @@ -253,7 +257,6 @@ Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSp LG = Geometry.LocalGeometry{AIdx, CoordType2D, FT, SMatrix{2, 2, FT, 4}} local_geometry = DataLayouts.IJFH{LG, Nq}(Array{FT}, nlelems) - ghost_geometry = DataLayouts.IJFH{LG, Nq}(Array{FT}, ngelems) quad_points, quad_weights = Quadratures.quadrature_points(FT, quadrature_style) @@ -388,7 +391,6 @@ Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSp # alternatively, we could do a ghost exchange here? if topology isa Topologies.Topology2D for (ridx, elem) in enumerate(Topologies.ghostelems(topology)) - ghost_geometry_slab = slab(ghost_geometry, ridx) for i in 1:Nq, j in 1:Nq ξ = SVector(quad_points[i], quad_points[j]) u, ∂u∂ξ = compute_local_geometry( @@ -401,13 +403,8 @@ Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSp J = det(Geometry.components(∂u∂ξ)) WJ = J * quad_weights[i] * quad_weights[j] - ghost_geometry_slab[i, j] = - Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) end end - if !isnothing(ghost_geometry) && DA ≠ Array - ghost_geometry = DataLayouts.rebuild(ghost_geometry, DA) - end end # dss_weights = J ./ dss(J) J = DataLayouts.rebuild(local_geometry.J, DA) @@ -416,7 +413,6 @@ Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSp dss!(dss_local_weights, topology, quadrature_style) end dss_local_weights .= J ./ dss_local_weights - dss_ghost_weights = copy(J) # not currently used SG = Geometry.SurfaceGeometry{ FT, @@ -492,9 +488,7 @@ Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSp quadrature_style, global_geometry, DataLayouts.rebuild(local_geometry, DA), - ghost_geometry, dss_local_weights, - dss_ghost_weights, internal_surface_geometry, boundary_surface_geometries, ) @@ -502,15 +496,19 @@ end local_geometry_data(grid::SpectralElementGrid2D) = grid.local_geometry -ghost_geometry_data(grid::SpectralElementGrid2D) = grid.ghost_geometry topology(grid::SpectralElementGrid2D) = grid.topology quadrature_style(grid::SpectralElementGrid2D) = grid.quadrature_style +local_dss_weights(grid::SpectralElementGrid2D) = grid.local_dss_weights struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace grid::G end +AbstractSpectralElementSpace(grid::SpectralElementGrid2D) = + SpectralElementSpace2D(grid) + + SpectralElementSpace2D(topology::Topologies.Topology2D, quadrature_style) = SpectralElementSpace2D(SpectralElementGrid2D(topology, quadrature_style)) @@ -518,21 +516,27 @@ nlevels(space::SpectralElementSpace2D) = 1 perimeter(space::SpectralElementSpace2D) = Perimeter2D(Quadratures.degrees_of_freedom(quadrature_style(space))) +local_geometry_data(space::SpectralElementSpace2D) = + local_geometry_data(space.grid) +topology(space::SpectralElementSpace2D) = topology(space.grid) +quadrature_style(space::SpectralElementSpace2D) = quadrature_style(space.grid) +local_dss_weights(space::SpectralElementSpace2D) = local_dss_weights(space.grid) - #= + +#= const RectilinearSpectralElementSpace2D = SpectralElementSpace2D{ - <:Topologies.Topology2D{ - <:ClimaComms.AbstractCommsContext, - <:Meshes.RectilinearMesh, - }, +<:Topologies.Topology2D{ + <:ClimaComms.AbstractCommsContext, + <:Meshes.RectilinearMesh, +}, } const CubedSphereSpectralElementSpace2D = SpectralElementSpace2D{ - <:Topologies.Topology2D{ - <:ClimaComms.AbstractCommsContext, - <:Meshes.AbstractCubedSphere, - }, +<:Topologies.Topology2D{ + <:ClimaComms.AbstractCommsContext, + <:Meshes.AbstractCubedSphere, +}, } =# function compute_local_geometry( @@ -602,10 +606,6 @@ function compute_surface_geometry( return Geometry.SurfaceGeometry(sWJ, n) end -function variational_solve!(data, space::AbstractSpace) - data .= RecursiveApply.rdiv.(data, space.local_geometry.WJ) -end - """ SpectralElementSpaceSlab <: AbstractSpace diff --git a/test/Operators/finitedifference/column.jl b/test/Operators/finitedifference/column.jl index 53ff7fef0c..34411617f9 100644 --- a/test/Operators/finitedifference/column.jl +++ b/test/Operators/finitedifference/column.jl @@ -291,7 +291,7 @@ convergence_rate(err, Δh) = cent_field = operator.(face_field) wcent_field = woperator.(face_J, face_field) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] err[k] = norm(cent_field .- cent_field_exact) werr[k] = norm(wcent_field .- cent_field_exact) end @@ -345,7 +345,7 @@ end face_field = operator.(cent_field) wface_field = woperator.(cent_J, cent_field) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] err[k] = norm(face_field .- face_field_exact) werr[k] = norm(wface_field .- face_field_exact) end @@ -395,7 +395,7 @@ end face_field .= operator.(cent_field) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] err[k] = norm(face_field .- face_field_exact) end conv = convergence_rate(err, Δh) @@ -443,7 +443,7 @@ end cent_field .= operator.(face_field) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] err[k] = norm(cent_field .- cent_field_exact) end conv = convergence_rate(err, Δh) @@ -543,7 +543,7 @@ end curlsinᶠ = curlᶠ.(Geometry.Covariant1Vector.(sin.(centers))) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] # Errors err_grad_sin_c[k] = norm(gradsinᶜ .- Geometry.WVector.(cos.(centers))) err_div_sin_c[k] = norm(divsinᶜ .- cos.(centers)) @@ -663,7 +663,7 @@ end divf2c = Operators.DivergenceF2C() adv_wc = divf2c.(third_order_fluxsinᶠ) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] # Error err_adv_wc[k] = norm(adv_wc .- cos.(centers)) @@ -714,7 +714,7 @@ end divf2c = Operators.DivergenceF2C() adv_wc = divf2c.(third_order_fluxsinᶠ) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] # Error err_adv_wc[k] = @@ -776,7 +776,7 @@ end adv_wc = divf2c.(third_order_fluxᶠ.(w, c)) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] # Error err_adv_wc[k] = norm(adv_wc .- cos.(centers)) @@ -831,7 +831,7 @@ end ) adv_wc = divf2c.(third_order_fluxᶠ.(w, c)) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] # Errors err_adv_wc[k] = norm(adv_wc .- ((cos.(centers)) .^ 2 .- (sin.(centers)) .^ 2)) @@ -886,7 +886,7 @@ end @. divf2c(C * (third_order_fluxsinᶠ - first_order_fluxsinᶠ)) adv_wc = @. divf2c.(first_order_fluxsinᶠ) + corrected_antidiff_flux - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] # Error err_adv_wc[k] = norm(adv_wc .- cos.(centers)) @@ -957,7 +957,7 @@ end adv_wc = @. divf2c.(first_order_fluxᶠ(w, c)) + corrected_antidiff_flux - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] # Error err_adv_wc[k] = norm(adv_wc .- cos.(centers)) @@ -1020,7 +1020,7 @@ end adv_wc = @. divf2c.(first_order_fluxᶠ(w, c)) + corrected_antidiff_flux - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] # Errors err_adv_wc[k] = norm(adv_wc .- cos.(centers)) @@ -1127,7 +1127,7 @@ end # Call the advection operator adv = advection(c, f, cs) - Δh[k] = cs.face_local_geometry.J[1] + Δh[k] = Spaces.local_geometry_data(fs).J[1] err[k] = norm(adv .- cos.(Fields.coordinate_field(cs).z)) end # AdvectionC2C convergence rate diff --git a/test/Operators/hybrid/2d.jl b/test/Operators/hybrid/2d.jl index 908e7f895a..6931fb1a5d 100644 --- a/test/Operators/hybrid/2d.jl +++ b/test/Operators/hybrid/2d.jl @@ -308,7 +308,8 @@ end hv_center_space, hv_face_space = hvspace_2D(xlim = (-1, 1), zlim = (-1, 1), helem = n, velem = n) ccoords = Fields.coordinate_field(hv_center_space) - bcoords = Fields.coordinate_field(Spaces.horizontal_space(hv_center_space)) + bcoords = + Fields.coordinate_field(Spaces.horizontal_space(hv_center_space)) Δh[k] = 1.0 / n diff --git a/test/Operators/remapping.jl b/test/Operators/remapping.jl index 880040565f..ac7b29ee61 100644 --- a/test/Operators/remapping.jl +++ b/test/Operators/remapping.jl @@ -174,14 +174,14 @@ end @test monotone(R) @testset "Scalar Remap Operator Application" begin - n = length(source.local_geometry) + n = length(Spaces.local_geometry_data(source)) source_field = Fields.Field(IJFH{FT, 1}(ones(1, 1, n, 1)), source) # test consistent remap target_field = remap(R, source_field) @test vec(parent(target_field)) ≈ - ones(length(target.local_geometry)) + ones(length(Spaces.local_geometry_data(target))) # test simple remap vec(parent(source_field)) .= [1.0; 2.0; 3.0; 4.0] @@ -245,14 +245,14 @@ end @test monotone(R) @testset "Scalar Remap Operator Application" begin - n = length(source.local_geometry) + n = length(Spaces.local_geometry_data(source)) source_field = Fields.Field(IJFH{FT, 1}(ones(1, 1, n, 1)), source) # test consistent remap target_field = remap(R, source_field) @test vec(parent(target_field)) ≈ - ones(length(target.local_geometry)) + ones(length(Spaces.local_geometry_data(target))) # test simple remap vec(parent(source_field)) .= [1.0; 2.0; 3.0; 4.0] @@ -299,14 +299,14 @@ end @test monotone(R) @testset "Scalar Remap Operator Application" begin - n = length(source.local_geometry) + n = length(Spaces.local_geometry_data(source)) source_field = Fields.Field(IJFH{FT, 1}(ones(1, 1, n, 1)), source) # test consistent remap target_field = remap(R, source_field) @test vec(parent(target_field)) ≈ - ones(length(target.local_geometry)) + ones(length(Spaces.local_geometry_data(target))) # test simple remap vec(parent(source_field)) .= [1.0; 2.0; 3.0; 4.0] @@ -374,14 +374,14 @@ end @test monotone(R) @testset "Scalar Remap Operator Application" begin - n = length(source.local_geometry) + n = length(Spaces.local_geometry_data(source)) source_field = Fields.Field(IJFH{FT, 1}(ones(1, 1, n, 1)), source) # test consistent remap target_field = remap(R, source_field) @test vec(parent(target_field)) ≈ - ones(length(target.local_geometry)) + ones(length(Spaces.local_geometry_data(target))) # test simple remap vec(parent(source_field)) .= [1.0; 2.0; 3.0; 4.0] diff --git a/test/Operators/spectralelement/rectilinear.jl b/test/Operators/spectralelement/rectilinear.jl index c90ebbc610..935412c208 100644 --- a/test/Operators/spectralelement/rectilinear.jl +++ b/test/Operators/spectralelement/rectilinear.jl @@ -47,20 +47,20 @@ ts_test_setup = (ts_topology, ts_space, ts_coords) interpolated_field = I.(f) Spaces.weighted_dss!(interpolated_field) - @test axes(interpolated_field).quadrature_style == Iquad - @test axes(interpolated_field).topology == topology + @test Spaces.quadrature_style(axes(interpolated_field)) == Iquad + @test Spaces.topology(axes(interpolated_field)) == topology restrict_field = R.(f) Spaces.weighted_dss!(restrict_field) - @test axes(restrict_field).quadrature_style == quad - @test axes(restrict_field).topology == topology + @test Spaces.quadrature_style(axes(restrict_field)) == quad + @test Spaces.topology(axes(restrict_field)) == topology interp_restrict_field = R.(I.(f)) Spaces.weighted_dss!(interp_restrict_field) - @test axes(interp_restrict_field).quadrature_style == quad - @test axes(interp_restrict_field).topology == topology + @test Spaces.quadrature_style(axes(interp_restrict_field)) == quad + @test Spaces.topology(axes(interp_restrict_field)) == topology @test norm(interp_restrict_field .- f) ≤ 3.0e-4 end diff --git a/test/runtests.jl b/test/runtests.jl index 08810ce591..98f497faca 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -35,7 +35,7 @@ if !Sys.iswindows() @safetestset "Cubedsphere surface topology" begin @time include("Topologies/cubedsphere_sfc.jl") end # now part of buildkite # @safetestset "Distributed topology" begin @time include("Topologies/distributed.jl") end - =# + @safetestset "Quadrature" begin @time include("Spaces/quadrature.jl") end @safetestset "Spaces" begin @time include("Spaces/spaces.jl") end #= @@ -60,7 +60,7 @@ if !Sys.iswindows() @safetestset "Spectral elem - sphere diffusion vec" begin @time include("Operators/spectralelement/sphere_diffusion_vec.jl") end @safetestset "Spectral elem - sphere hyperdiffusion" begin @time include("Operators/spectralelement/sphere_hyperdiffusion.jl") end @safetestset "Spectral elem - sphere hyperdiffusion vec" begin @time include("Operators/spectralelement/sphere_hyperdiffusion_vec.jl") end - + =# @safetestset "FD ops - column" begin @time include("Operators/finitedifference/column.jl") end @safetestset "FD ops - opt" begin @time include("Operators/finitedifference/opt.jl") end @safetestset "FD ops - wfact" begin @time include("Operators/finitedifference/wfact.jl") end From 987515e832f44c763f81fdfa3400f97c39417a04 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 4 Oct 2023 10:10:04 -0700 Subject: [PATCH 11/42] IO --- Project.toml | 2 + src/Hypsography/Hypsography.jl | 2 +- src/InputOutput/readers.jl | 69 ++++++++++++++++++++++++++++-- src/InputOutput/writers.jl | 77 ++++++++++++++-------------------- src/Spaces/Spaces.jl | 6 +++ src/Spaces/extruded.jl | 24 ++++++----- src/Spaces/finitedifference.jl | 4 ++ src/Spaces/spectralelement.jl | 5 +-- 8 files changed, 126 insertions(+), 63 deletions(-) diff --git a/Project.toml b/Project.toml index 125ce4404d..dc1e292595 100644 --- a/Project.toml +++ b/Project.toml @@ -45,6 +45,7 @@ GaussQuadrature = "0.5" GilbertCurves = "0.1" HDF5 = "0.16, 0.17" IntervalSets = "0.5, 0.6, 0.7" +Memoize = "0.4" PkgVersion = "0.1, 0.2, 0.3" RecursiveArrayTools = "2" Requires = "1" @@ -52,6 +53,7 @@ RootSolvers = "0.3, 0.4" Static = "0.4, 0.5, 0.6, 0.7, 0.8" StaticArrays = "1" UnPack = "1" +WeakValueDicts = "0.1" julia = "1.8" [extras] diff --git a/src/Hypsography/Hypsography.jl b/src/Hypsography/Hypsography.jl index 7b1a843d18..34e2754e34 100644 --- a/src/Hypsography/Hypsography.jl +++ b/src/Hypsography/Hypsography.jl @@ -192,7 +192,7 @@ LinearAdaption() = LinearAdaption(nothing) return ExtrudedFiniteDifferenceGrid( horizontal_grid, - Spaces.topology(vertical_grid), + vertical_grid, adaption, ref_grid.global_geometry, center_local_geometry, diff --git a/src/InputOutput/readers.jl b/src/InputOutput/readers.jl index fae681e17b..5d33a83fc1 100644 --- a/src/InputOutput/readers.jl +++ b/src/InputOutput/readers.jl @@ -87,7 +87,7 @@ struct HDF5Reader{C <: ClimaComms.AbstractCommsContext} domain_cache::Dict{Any, Any} mesh_cache::Dict{Any, Any} topology_cache::Dict{Any, Any} - space_cache::Dict{Any, Any} + grid_cache::Dict{Any, Any} end @deprecate HDF5Reader(filename::AbstractString) HDF5Reader( @@ -128,7 +128,7 @@ function Base.close(hdfreader::HDF5Reader) empty!(hdfreader.domain_cache) empty!(hdfreader.mesh_cache) empty!(hdfreader.topology_cache) - empty!(hdfreader.space_cache) + empty!(hdfreader.grid_cache) close(hdfreader.file) return nothing end @@ -304,6 +304,58 @@ function read_topology_new(reader::HDF5Reader, name::AbstractString) end + +""" + read_grid(reader::AbstractReader, name) + +Reads a space named `name` from `reader`, or from the reader cache if it has +already been read. +""" +function read_grid(reader, name) + Base.get!(reader.grid_cache, name) do + read_grid_new(reader, name) + end +end + +function read_grid_new(reader, name) + group = reader.file["grids/$name"] + type = attrs(group)["type"] + if type in ("SpectralElementGrid1D", "SpectralElementGrid2D") + npts = attrs(group)["quadrature_num_points"] + quadrature_style = + _scan_quadrature_style(attrs(group)["quadrature_type"], npts) + topology = read_topology(reader, attrs(group)["topology"]) + if type == "SpectralElementGrid1D" + return Spaces.SpectralElementGrid1D(topology, quadrature_style) + else + return Spaces.SpectralElementGrid2D(topology, quadrature_style) + end + elseif type == "FiniteDifferenceGrid" + topology = read_topology(reader, attrs(group)["topology"]) + return Spaces.FiniteDifferenceGrid(topology) + elseif type == "ExtrudedFiniteDifferenceGrid" + vertical_grid = + read_grid(reader, attrs(group)["vertical_grid"]) + horizontal_grid = + read_grid(reader, attrs(group)["horizontal_grid"]) + hypsography_type = get(attrs(group), "hypsography_type", "Flat") + if hypsography_type == "Flat" + hypsography = Spaces.Flat() + elseif hypsography_type == "LinearAdaption" + hypsography = Hypsography.LinearAdaption( + read_field(reader, attrs(group)["hypsography_surface"]), + ) + else + error("Unsupported hypsography type $hypsography_type") + end + return Spaces.ExtrudedFiniteDifferenceGrid( + horizontal_grid, + vertical_grid, + hypsography, + ) + end +end + """ read_space(reader::AbstractReader, name) @@ -368,7 +420,18 @@ function read_field(reader::HDF5Reader, name::AbstractString) obj = reader.file["fields/$name"] type = attrs(obj)["type"] if type == "Field" - space = read_space(reader, attrs(obj)["space"]) + if haskey(attrs(obj), "grid") + grid = read_grid(reader, attrs(obj)["grid"]) + staggering = get(attrs(obj), "staggering", nothing) + if staggering == "CellCenter" + staggering = Spaces.CellCenter() + elseif staggering == "CellFace" + staggering = Spaces.CellFace() + end + space = Spaces.space(staggering, grid) + else + space = read_space(reader, attrs(obj)["space"]) + end topology = Spaces.topology(space) if topology isa Topologies.Topology2D nd = ndims(obj) diff --git a/src/InputOutput/writers.jl b/src/InputOutput/writers.jl index 379ef5a095..aebaea7375 100644 --- a/src/InputOutput/writers.jl +++ b/src/InputOutput/writers.jl @@ -286,15 +286,13 @@ function write_new!( return name end -# Spaces +# Grids # -defaultname(::Spaces.SpectralElementSpace1D) = "horizontal_space" -defaultname(::Spaces.SpectralElementSpace2D) = "horizontal_space" -defaultname(::Spaces.CenterExtrudedFiniteDifferenceSpace) = - "center_extruded_finite_difference_space" -defaultname(::Spaces.FaceExtrudedFiniteDifferenceSpace) = - "face_extruded_finite_difference_space" -defaultname(space::Spaces.FiniteDifferenceSpace) = defaultname(space.topology) +defaultname(::Spaces.SpectralElementGrid1D) = "horizontal_grid" +defaultname(::Spaces.SpectralElementGrid2D) = "horizontal_grid" +defaultname(::Spaces.ExtrudedFiniteDifferenceGrid) = + "extruded_finite_difference_grid" +defaultname(grid::Spaces.FiniteDifferenceGrid) = defaultname(grid.topology) """ @@ -304,11 +302,11 @@ Write `SpectralElementSpace1D` data to HDF5. """ function write_new!( writer::HDF5Writer, - space::Spaces.SpectralElementSpace1D, + space::Spaces.SpectralElementGrid1D, name::AbstractString = defaultname(space), ) - group = create_group(writer.file, "spaces/$name") - write_attribute(group, "type", "SpectralElementSpace1D") + group = create_group(writer.file, "grids/$name") + write_attribute(group, "type", "SpectralElementGrid1D") write_attribute( group, "quadrature_type", @@ -325,11 +323,11 @@ end function write_new!( writer::HDF5Writer, - space::Spaces.SpectralElementSpace2D, + space::Spaces.SpectralElementGrid2D, name::AbstractString = defaultname(space), ) - group = create_group(writer.file, "spaces/$name") - write_attribute(group, "type", "SpectralElementSpace2D") + group = create_group(writer.file, "grids/$name") + write_attribute(group, "type", "SpectralElementGrid2D") write_attribute( group, "quadrature_type", @@ -346,32 +344,31 @@ end function write_new!( writer::HDF5Writer, - space::Spaces.FiniteDifferenceSpace, + space::Spaces.FiniteDifferenceGrid, name::AbstractString = defaultname(space), ) - group = create_group(writer.file, "spaces/$name") - write_attribute(group, "type", "FiniteDifferenceSpace") + group = create_group(writer.file, "grids/$name") + write_attribute(group, "type", "FiniteDifferenceGrid") write_attribute(group, "topology", write!(writer, space.topology)) - return group + return name end function write_new!( writer::HDF5Writer, - space::Spaces.CenterExtrudedFiniteDifferenceSpace, + space::Spaces.ExtrudedFiniteDifferenceGrid, name::AbstractString = defaultname(space), ) - group = create_group(writer.file, "spaces/$name") - write_attribute(group, "type", "ExtrudedFiniteDifferenceSpace") - write_attribute(group, "staggering", "CellCenter") + group = create_group(writer.file, "grids/$name") + write_attribute(group, "type", "ExtrudedFiniteDifferenceGrid") write_attribute( group, - "horizontal_space", - write!(writer, Spaces.horizontal_space(space)), + "horizontal_grid", + write!(writer, space.horizontal_grid), ) write_attribute( group, - "vertical_topology", - write!(writer, space.vertical_topology), + "vertical_grid", + write!(writer, space.vertical_grid), ) if space.hypsography isa Hypsography.LinearAdaption write_attribute(group, "hypsography_type", "LinearAdaption") @@ -384,28 +381,12 @@ function write_new!( return name end -function write_new!( - writer::HDF5Writer, - space::Spaces.FaceExtrudedFiniteDifferenceSpace, - name::AbstractString = defaultname(space), -) - group = create_group(writer.file, name) - group = create_group(writer.file, "spaces/$name") - write_attribute(group, "type", "ExtrudedFiniteDifferenceSpace") - write_attribute(group, "staggering", "CellFace") - write_attribute( - group, - "center_space", - write!(writer, Spaces.CenterExtrudedFiniteDifferenceSpace(space)), - ) - return name -end - - # write fields function write!(writer::HDF5Writer, field::Fields.Field, name::AbstractString) space = axes(field) - space_name = write!(writer, space) + staggering = Spaces.staggering(space) + grid = Spaces.grid(space) + grid_name = write!(writer, grid) array = parent(field) topology = Spaces.topology(space) @@ -440,7 +421,11 @@ function write!(writer::HDF5Writer, field::Fields.Field, name::AbstractString) string(nameof(typeof(Fields.field_values(field)))), ) write_attribute(dataset, "value_type", string(eltype(field))) - write_attribute(dataset, "space", space_name) + write_attribute(dataset, "grid", grid_name) + if !isnothing(staggering) + write_attribute(dataset, "staggering", string(nameof(typeof(staggering)))) + end + return name end diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index d66bb9c407..0b8c0f8050 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -30,6 +30,12 @@ abstract type AbstractGrid end abstract type AbstractSpace end +grid(space::AbstractSpace) = space.grid +staggering(space::AbstractSpace) = nothing + +function space end + + issubspace(::AbstractSpace, ::AbstractSpace) = false undertype(space::AbstractSpace) = diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 4cf806affb..575069e08e 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -23,13 +23,13 @@ Construct an `ExtrudedFiniteDifferenceGrid` from the horizontal and vertical spa """ mutable struct ExtrudedFiniteDifferenceGrid{ H <: AbstractGrid, - T <: Topologies.AbstractIntervalTopology, + V <: FiniteDifferenceGrid, A <: HypsographyAdaption, GG <: Geometry.AbstractGlobalGeometry, LG, } <: AbstractGrid horizontal_grid::H - vertical_topology::T # should we cache the vertical grid? + vertical_grid::V hypsography::A global_geometry::GG center_local_geometry::LG @@ -41,7 +41,6 @@ end vertical_grid::FiniteDifferenceGrid, hypsography::Flat = Flat(), ) - vertical_topology = vertical_grid.topology global_geometry = horizontal_grid.global_geometry center_local_geometry = product_geometry.( @@ -56,7 +55,7 @@ end return ExtrudedFiniteDifferenceGrid( horizontal_grid, - vertical_topology, + vertical_grid, hypsography, global_geometry, center_local_geometry, @@ -75,6 +74,10 @@ struct ExtrudedFiniteDifferenceSpace{ grid::G end + +space(staggering::Staggering, grid::ExtrudedFiniteDifferenceGrid) = + ExtrudedFiniteDifferenceSpace(staggering, grid) + const FaceExtrudedFiniteDifferenceSpace = ExtrudedFiniteDifferenceSpace{CellFace} const CenterExtrudedFiniteDifferenceSpace = @@ -113,7 +116,7 @@ center_space(space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace{CellCenter}(space) - +staggering(space::ExtrudedFiniteDifferenceSpace) = space.staggering ExtrudedFiniteDifferenceSpace{S}( horizontal_space::AbstractSpace, @@ -217,10 +220,11 @@ ClimaComms.device(space::ExtrudedFiniteDifferenceSpace) = ClimaComms.context(space::ExtrudedFiniteDifferenceSpace) = ClimaComms.context(space.grid) -horizontal_space(space::ExtrudedFiniteDifferenceSpace) = - AbstractSpectralElementSpace(space.grid.horizontal_grid) +horizontal_space(full_space::ExtrudedFiniteDifferenceSpace) = + space(nothing, full_space.grid.horizontal_grid) -vertical_topology(grid::ExtrudedFiniteDifferenceGrid) = grid.vertical_topology +vertical_topology(grid::ExtrudedFiniteDifferenceGrid) = + topology(grid.vertical_grid) vertical_topology(space::ExtrudedFiniteDifferenceSpace) = vertical_topology(space.grid) @@ -269,12 +273,12 @@ function column(space::ExtrudedFiniteDifferenceSpace, colidx::ColumnIndex) FiniteDifferenceSpace(space.staggering, column_grid) end -vertical_topology(colgrid::ColumnGrid) = colgrid.full_grid.vertical_topology +topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) +vertical_topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) local_geometry_data(staggering::Staggering, colgrid::ColumnGrid) = column(local_geometry_data(staggering, colgrid.full_grid), colgrid.colidx) -topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) ClimaComms.device(colgrid::ColumnGrid) = ClimaComms.device(colgrid.full_grid) ClimaComms.context(colgrid::ColumnGrid) = ClimaComms.context(colgrid.full_grid) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 6294dd5254..4bf9746991 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -170,6 +170,10 @@ end const FaceFiniteDifferenceSpace = FiniteDifferenceSpace{CellFace} const CenterFiniteDifferenceSpace = FiniteDifferenceSpace{CellCenter} +staggering(space::FiniteDifferenceSpace) = space.staggering +space(staggering::Staggering, grid::AbstractFiniteDifferenceGrid) = + FiniteDifferenceSpace(staggering, grid) + local_geometry_data(space::FiniteDifferenceSpace) = local_geometry_data(space.staggering, space.grid) diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 2565cfb98c..159b657946 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -133,10 +133,9 @@ function SpectralElementSpace1D( SpectralElementSpace1D(grid) end -AbstractSpectralElementSpace(grid::SpectralElementGrid1D) = +space(::Nothing, grid::SpectralElementGrid1D) = SpectralElementSpace1D(grid) - nlevels(space::SpectralElementSpace1D) = 1 @@ -505,7 +504,7 @@ struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace grid::G end -AbstractSpectralElementSpace(grid::SpectralElementGrid2D) = +space(::Nothing, grid::SpectralElementGrid2D) = SpectralElementSpace2D(grid) From 6749231a0360816385788e0c320a86a982226a96 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 4 Oct 2023 16:58:23 -0700 Subject: [PATCH 12/42] create Grids submodule --- src/ClimaCore.jl | 2 + src/Fields/Fields.jl | 3 +- src/Hypsography/Hypsography.jl | 12 +- src/InputOutput/InputOutput.jl | 20 +- src/InputOutput/readers.jl | 10 +- src/InputOutput/writers.jl | 20 +- src/Operators/finitedifference.jl | 38 +- src/Spaces/Spaces.jl | 51 ++- src/Spaces/extruded.jl | 203 ++-------- src/Spaces/finitedifference.jl | 220 ++--------- src/Spaces/spectralelement.jl | 608 ++---------------------------- test/Spaces/quadrature.jl | 2 +- test/runtests.jl | 4 +- 13 files changed, 184 insertions(+), 1009 deletions(-) diff --git a/src/ClimaCore.jl b/src/ClimaCore.jl index 72ada48e34..97c94deaae 100644 --- a/src/ClimaCore.jl +++ b/src/ClimaCore.jl @@ -11,6 +11,8 @@ include("Geometry/Geometry.jl") include("Domains/Domains.jl") include("Meshes/Meshes.jl") include("Topologies/Topologies.jl") +include("Quadratures/Quadratures.jl") +include("Grids/Grids.jl") include("Spaces/Spaces.jl") include("Fields/Fields.jl") include("Operators/Operators.jl") diff --git a/src/Fields/Fields.jl b/src/Fields/Fields.jl index ff6ebf5eb7..51f6c01f3d 100644 --- a/src/Fields/Fields.jl +++ b/src/Fields/Fields.jl @@ -5,7 +5,8 @@ import ..slab, ..slab_args, ..column, ..column_args, ..level import ..DataLayouts: DataLayouts, AbstractData, DataStyle import ..Domains import ..Topologies -import ..Spaces: Spaces, AbstractSpace, AbstractPointSpace, ColumnIndex +import ..Grids: ColumnIndex +import ..Spaces: Spaces, AbstractSpace, AbstractPointSpace import ..Geometry: Geometry, Cartesian12Vector import ..Utilities: PlusHalf, half diff --git a/src/Hypsography/Hypsography.jl b/src/Hypsography/Hypsography.jl index 34e2754e34..abe5385eef 100644 --- a/src/Hypsography/Hypsography.jl +++ b/src/Hypsography/Hypsography.jl @@ -1,9 +1,11 @@ module Hypsography import ..slab, ..column -import ..Geometry, ..Domains, ..Topologies, ..Spaces, ..Fields, ..Operators +import ..Geometry, ..Domains, ..Topologies, ..Grids, ..Spaces, ..Fields, ..Operators import ..Spaces: - ExtrudedFiniteDifferenceSpace, + ExtrudedFiniteDifferenceSpace + +import ..Grids: ExtrudedFiniteDifferenceGrid, HypsographyAdaption, Flat @@ -59,8 +61,8 @@ LinearAdaption() = LinearAdaption(nothing) # linear coordinates @memoize WeakValueDict function ExtrudedFiniteDifferenceGrid( - horizontal_grid::Spaces.AbstractGrid, - vertical_grid::Spaces.FiniteDifferenceGrid, + horizontal_grid::Grids.AbstractGrid, + vertical_grid::Grids.FiniteDifferenceGrid, adaption::HypsographyAdaption, ) if adaption isa LinearAdaption @@ -73,7 +75,7 @@ LinearAdaption() = LinearAdaption(nothing) end # construct initial flat space, then mutate - ref_grid = Spaces.ExtrudedFiniteDifferenceGrid( + ref_grid = Grids.ExtrudedFiniteDifferenceGrid( horizontal_grid, vertical_grid, Flat(), diff --git a/src/InputOutput/InputOutput.jl b/src/InputOutput/InputOutput.jl index 088dec93d7..995b7ff341 100644 --- a/src/InputOutput/InputOutput.jl +++ b/src/InputOutput/InputOutput.jl @@ -1,15 +1,17 @@ module InputOutput using HDF5, ClimaComms -import ..Geometry -import ..DataLayouts -import ..Domains -import ..Meshes -import ..Topologies -import ..Spaces -import ..Fields -import ..Hypsography -import ..VERSION +import ..Geometry, + ..DataLayouts, + ..Domains, + ..Meshes, + ..Topologies, + ..Quadratures, + ..Grids, + ..Spaces, + ..Fields, + ..Hypsography +import .. VERSION include("writers.jl") include("readers.jl") diff --git a/src/InputOutput/readers.jl b/src/InputOutput/readers.jl index 5d33a83fc1..2050e2f5d8 100644 --- a/src/InputOutput/readers.jl +++ b/src/InputOutput/readers.jl @@ -14,8 +14,6 @@ using ..Meshes: using ..Topologies: Topologies, IntervalTopology using ..Spaces: Spaces, - Spaces.Quadratures, - Spaces.Quadratures.GLL, SpectralElementSpace1D, SpectralElementSpace2D, CenterExtrudedFiniteDifferenceSpace, @@ -142,10 +140,10 @@ end function _scan_quadrature_style(quadraturestring::AbstractString, npts) @assert quadraturestring ∈ ("GLL", "GL", "Uniform", "ClosedUniform") - quadraturestring == "GLL" && return Spaces.Quadratures.GLL{npts}() - quadraturestring == "GL" && return Spaces.Quadratures.GL{npts}() - quadraturestring == "Uniform" && return Spaces.Quadratures.Uniform{npts}() - return Spaces.Quadratures.ClosedUniform{npts}() + quadraturestring == "GLL" && return Quadratures.GLL{npts}() + quadraturestring == "GL" && return Quadratures.GL{npts}() + quadraturestring == "Uniform" && return Quadratures.Uniform{npts}() + return Quadratures.ClosedUniform{npts}() end function _scan_data_layout(layoutstring::AbstractString) diff --git a/src/InputOutput/writers.jl b/src/InputOutput/writers.jl index aebaea7375..04b9f1325f 100644 --- a/src/InputOutput/writers.jl +++ b/src/InputOutput/writers.jl @@ -288,11 +288,11 @@ end # Grids # -defaultname(::Spaces.SpectralElementGrid1D) = "horizontal_grid" -defaultname(::Spaces.SpectralElementGrid2D) = "horizontal_grid" -defaultname(::Spaces.ExtrudedFiniteDifferenceGrid) = +defaultname(::Grids.SpectralElementGrid1D) = "horizontal_grid" +defaultname(::Grids.SpectralElementGrid2D) = "horizontal_grid" +defaultname(::Grids.ExtrudedFiniteDifferenceGrid) = "extruded_finite_difference_grid" -defaultname(grid::Spaces.FiniteDifferenceGrid) = defaultname(grid.topology) +defaultname(grid::Grids.FiniteDifferenceGrid) = defaultname(grid.topology) """ @@ -302,7 +302,7 @@ Write `SpectralElementSpace1D` data to HDF5. """ function write_new!( writer::HDF5Writer, - space::Spaces.SpectralElementGrid1D, + space::Grids.SpectralElementGrid1D, name::AbstractString = defaultname(space), ) group = create_group(writer.file, "grids/$name") @@ -315,7 +315,7 @@ function write_new!( write_attribute( group, "quadrature_num_points", - Spaces.Quadratures.degrees_of_freedom(space.quadrature_style), + Quadratures.degrees_of_freedom(space.quadrature_style), ) write_attribute(group, "topology", write!(writer, space.topology)) return name @@ -323,7 +323,7 @@ end function write_new!( writer::HDF5Writer, - space::Spaces.SpectralElementGrid2D, + space::Grids.SpectralElementGrid2D, name::AbstractString = defaultname(space), ) group = create_group(writer.file, "grids/$name") @@ -336,7 +336,7 @@ function write_new!( write_attribute( group, "quadrature_num_points", - Spaces.Quadratures.degrees_of_freedom(space.quadrature_style), + Quadratures.degrees_of_freedom(space.quadrature_style), ) write_attribute(group, "topology", write!(writer, space.topology)) return name @@ -344,7 +344,7 @@ end function write_new!( writer::HDF5Writer, - space::Spaces.FiniteDifferenceGrid, + space::Grids.FiniteDifferenceGrid, name::AbstractString = defaultname(space), ) group = create_group(writer.file, "grids/$name") @@ -355,7 +355,7 @@ end function write_new!( writer::HDF5Writer, - space::Spaces.ExtrudedFiniteDifferenceGrid, + space::Grids.ExtrudedFiniteDifferenceGrid, name::AbstractString = defaultname(space), ) group = create_group(writer.file, "grids/$name") diff --git a/src/Operators/finitedifference.jl b/src/Operators/finitedifference.jl index 01f1975336..cf923c1015 100644 --- a/src/Operators/finitedifference.jl +++ b/src/Operators/finitedifference.jl @@ -22,10 +22,10 @@ right_idx(space::AllFaceFiniteDifferenceSpace) = right_face_boundary_idx(space) left_center_boundary_idx(space::AllFiniteDifferenceSpace) = 1 right_center_boundary_idx(space::AllFiniteDifferenceSpace) = - size(Spaces.local_geometry_data(Spaces.center_space(space)), 4) + size(Spaces.local_geometry_data(Spaces.space(space, Spaces.CallCenter())), 4) left_face_boundary_idx(space::AllFiniteDifferenceSpace) = half right_face_boundary_idx(space::AllFiniteDifferenceSpace) = - size(Spaces.local_geometry_data(Spaces.face_space(space)), 4) - half + size(Spaces.local_geometry_data(Spaces.space(space, Spaces.CallFace())), 4) - half left_face_boundary_idx(arg) = left_face_boundary_idx(axes(arg)) @@ -341,7 +341,7 @@ end InterpolateF2C(; kwargs...) = InterpolateF2C(NamedTuple(kwargs)) return_space(::InterpolateF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.center_space(space) + Spaces.space(space, Spaces.CallCenter()) stencil_interior_width(::InterpolateF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -393,7 +393,7 @@ end InterpolateC2F(; kwargs...) = InterpolateC2F(NamedTuple(kwargs)) return_space(::InterpolateC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.face_space(space) + Spaces.space(space, Spaces.CellFace()) stencil_interior_width(::InterpolateC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -518,7 +518,7 @@ end LeftBiasedC2F(; kwargs...) = LeftBiasedC2F(NamedTuple(kwargs)) return_space(::LeftBiasedC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.face_space(space) + Spaces.space(space, Spaces.CellFace()) stencil_interior_width(::LeftBiasedC2F, arg) = ((-half, -half),) Base.@propagate_inbounds stencil_interior( @@ -577,7 +577,7 @@ end LeftBiasedF2C(; kwargs...) = LeftBiasedF2C(NamedTuple(kwargs)) return_space(::LeftBiasedF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.center_space(space) + Spaces.space(space, Spaces.CallCenter()) stencil_interior_width(::LeftBiasedF2C, arg) = ((-half, -half),) Base.@propagate_inbounds stencil_interior( @@ -637,7 +637,7 @@ end LeftBiased3rdOrderC2F(; kwargs...) = LeftBiased3rdOrderC2F(NamedTuple(kwargs)) return_space(::LeftBiased3rdOrderC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.face_space(space) + Spaces.space(space, Spaces.CallFace()) stencil_interior_width(::LeftBiased3rdOrderC2F, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -701,7 +701,7 @@ end LeftBiased3rdOrderF2C(; kwargs...) = LeftBiased3rdOrderF2C(NamedTuple(kwargs)) return_space(::LeftBiased3rdOrderF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.center_space(space) + Spaces.space(space, Spaces.CallCenter()) stencil_interior_width(::LeftBiased3rdOrderF2C, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -765,7 +765,7 @@ end RightBiasedC2F(; kwargs...) = RightBiasedC2F(NamedTuple(kwargs)) return_space(::RightBiasedC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.face_space(space) + Spaces.space(space, Spaces.CallFace()) stencil_interior_width(::RightBiasedC2F, arg) = ((half, half),) Base.@propagate_inbounds stencil_interior( @@ -824,7 +824,7 @@ end RightBiasedF2C(; kwargs...) = RightBiasedF2C(NamedTuple(kwargs)) return_space(::RightBiasedF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.center_space(space) + Spaces.space(space, Spaces.CallCenter()) stencil_interior_width(::RightBiasedF2C, arg) = ((half, half),) Base.@propagate_inbounds stencil_interior( @@ -886,7 +886,7 @@ end RightBiased3rdOrderC2F(; kwargs...) = RightBiased3rdOrderC2F(NamedTuple(kwargs)) return_space(::RightBiased3rdOrderC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.face_space(space) + Spaces.space(space, Spaces.CallFace()) stencil_interior_width(::RightBiased3rdOrderC2F, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -938,7 +938,7 @@ end RightBiased3rdOrderF2C(; kwargs...) = RightBiased3rdOrderF2C(NamedTuple(kwargs)) return_space(::RightBiased3rdOrderF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.center_space(space) + Spaces.space(space, Spaces.CallCenter()) stencil_interior_width(::RightBiased3rdOrderF2C, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -1000,7 +1000,7 @@ return_space( ::WeightedInterpolateF2C, weight_space::AllFaceFiniteDifferenceSpace, arg_space::AllFaceFiniteDifferenceSpace, -) = Spaces.center_space(arg_space) +) = Spaces.space(arg_space, Spaces.CellCenter()) stencil_interior_width(::WeightedInterpolateF2C, weight, arg) = ((-half, half), (-half, half)) @@ -1053,7 +1053,7 @@ return_space( ::WeightedInterpolateC2F, weight_space::AllCenterFiniteDifferenceSpace, arg_space::AllCenterFiniteDifferenceSpace, -) = Spaces.face_space(arg_space) +) = Spaces.space(arg_space, Spaces.CellFace()) stencil_interior_width(::WeightedInterpolateC2F, weight, arg) = ((-half, half), (-half, half)) @@ -2244,7 +2244,7 @@ end GradientF2C(; kwargs...) = GradientF2C(NamedTuple(kwargs)) return_space(::GradientF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.center_space(space) + Spaces.space(space, Spaces.CallCenter()) stencil_interior_width(::GradientF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2357,7 +2357,7 @@ end GradientC2F(; kwargs...) = GradientC2F(NamedTuple(kwargs)) return_space(::GradientC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.face_space(space) + Spaces.space(space, Spaces.CallFace()) stencil_interior_width(::GradientC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2481,7 +2481,7 @@ end DivergenceF2C(; kwargs...) = DivergenceF2C(NamedTuple(kwargs)) return_space(::DivergenceF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.center_space(space) + Spaces.space(space, Spaces.CallCenter()) stencil_interior_width(::DivergenceF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2604,7 +2604,7 @@ end DivergenceC2F(; kwargs...) = DivergenceC2F(NamedTuple(kwargs)) return_space(::DivergenceC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.face_space(space) + Spaces.space(space, Spaces.CallFace()) stencil_interior_width(::DivergenceC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2748,7 +2748,7 @@ end CurlC2F(; kwargs...) = CurlC2F(NamedTuple(kwargs)) return_space(::CurlC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.face_space(space) + Spaces.space(space, Spaces.CallFace()) fd3_curl(u₊::Geometry.Covariant1Vector, u₋::Geometry.Covariant1Vector, invJ) = Geometry.Contravariant2Vector((u₊.u₁ - u₋.u₁) * invJ) diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index 0b8c0f8050..2f876fe3fd 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -20,20 +20,49 @@ using CUDA import ..slab, ..column, ..level import ..Utilities: PlusHalf, half -import ..DataLayouts, ..Geometry, ..Domains, ..Meshes, ..Topologies +import ..DataLayouts, ..Geometry, ..Domains, ..Meshes, ..Topologies, ..Grids, ..Quadratures + +import ..Grids: + Staggering, CellFace, CellCenter, + topology, local_geometry_data + import ClimaComms using StaticArrays, ForwardDiff, LinearAlgebra, UnPack, Adapt -using Memoize, WeakValueDicts -abstract type AbstractGrid end +""" + AbstractSpace + +Should define +- `grid` +- `staggering` +- `space` constructor + +""" abstract type AbstractSpace end -grid(space::AbstractSpace) = space.grid -staggering(space::AbstractSpace) = nothing +function grid end +function staggering end + + +ClimaComms.context(space::AbstractSpace) = + ClimaComms.context(grid(space)) +ClimaComms.device(space::AbstractSpace) = + ClimaComms.device(grid(space)) + +topology(space::AbstractSpace) = topology(grid(space)) +vertical_topology(space::AbstractSpace) = vertical_topology(grid(space)) + + +local_geometry_data(space::AbstractSpace) = + local_geometry_data(grid(space), staggering(space)) + +space(refspace::AbstractSpace, staggering::Staggering) = + space(grid(refspace), staggering) + + -function space end issubspace(::AbstractSpace, ::AbstractSpace) = false @@ -42,16 +71,10 @@ undertype(space::AbstractSpace) = Geometry.undertype(eltype(local_geometry_data(space))) coordinates_data(space::AbstractSpace) = local_geometry_data(space).coordinates -coordinates_data(grid::AbstractGrid) = local_geometry_data(grid).coordinates -coordinates_data(staggering, grid::AbstractGrid) = +coordinates_data(grid::Grids.AbstractGrid) = local_geometry_data(grid).coordinates +coordinates_data(staggering, grid::Grids.AbstractGrid) = local_geometry_data(staggering, grid).coordinates -ClimaComms.context(space::Spaces.AbstractSpace) = - ClimaComms.context(Spaces.topology(space)) - -include("quadrature.jl") -import .Quadratures - include("pointspace.jl") include("spectralelement.jl") include("finitedifference.jl") diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 575069e08e..96989f3cd4 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -1,130 +1,61 @@ -##### -##### Hybrid mesh -##### - -abstract type HypsographyAdaption end - -""" - Flat() - -No surface hypsography. -""" -struct Flat <: HypsographyAdaption end - - -""" - ExtrudedFiniteDifferenceGrid( - horizontal_space::AbstractSpace, - vertical_space::FiniteDifferenceSpace, - hypsography::HypsographyAdaption = Flat(), - ) - -Construct an `ExtrudedFiniteDifferenceGrid` from the horizontal and vertical spaces. -""" -mutable struct ExtrudedFiniteDifferenceGrid{ - H <: AbstractGrid, - V <: FiniteDifferenceGrid, - A <: HypsographyAdaption, - GG <: Geometry.AbstractGlobalGeometry, - LG, -} <: AbstractGrid - horizontal_grid::H - vertical_grid::V - hypsography::A - global_geometry::GG - center_local_geometry::LG - face_local_geometry::LG -end - -@memoize WeakValueDict function ExtrudedFiniteDifferenceGrid( - horizontal_grid::Union{SpectralElementGrid1D, SpectralElementGrid2D}, - vertical_grid::FiniteDifferenceGrid, - hypsography::Flat = Flat(), -) - global_geometry = horizontal_grid.global_geometry - center_local_geometry = - product_geometry.( - horizontal_grid.local_geometry, - vertical_grid.center_local_geometry, - ) - face_local_geometry = - product_geometry.( - horizontal_grid.local_geometry, - vertical_grid.face_local_geometry, - ) - - return ExtrudedFiniteDifferenceGrid( - horizontal_grid, - vertical_grid, - hypsography, - global_geometry, - center_local_geometry, - face_local_geometry, - ) -end - - struct ExtrudedFiniteDifferenceSpace{ + G <: Grids.ExtrudedFiniteDifferenceGrid, S <: Staggering, - G <: ExtrudedFiniteDifferenceGrid, } <: AbstractSpace - staggering::S grid::G + staggering::S end - -space(staggering::Staggering, grid::ExtrudedFiniteDifferenceGrid) = +space(staggering::Staggering, grid::Grids.ExtrudedFiniteDifferenceGrid) = ExtrudedFiniteDifferenceSpace(staggering, grid) -const FaceExtrudedFiniteDifferenceSpace = - ExtrudedFiniteDifferenceSpace{CellFace} -const CenterExtrudedFiniteDifferenceSpace = - ExtrudedFiniteDifferenceSpace{CellCenter} - +const FaceExtrudedFiniteDifferenceSpace{G} = + ExtrudedFiniteDifferenceSpace{G,CellFace} +const CenterExtrudedFiniteDifferenceSpace{G} = + ExtrudedFiniteDifferenceSpace{G,CellCenter} +#= ExtrudedFiniteDifferenceSpace{S}( - grid::ExtrudedFiniteDifferenceGrid, + grid::Grids.ExtrudedFiniteDifferenceGrid, ) where {S <: Staggering} = ExtrudedFiniteDifferenceSpace(S(), grid) ExtrudedFiniteDifferenceSpace{S}( space::ExtrudedFiniteDifferenceSpace, ) where {S <: Staggering} = ExtrudedFiniteDifferenceSpace{S}(space.grid) - +=# function ExtrudedFiniteDifferenceSpace( horizontal_space::AbstractSpace, - vertical_space::FiniteDifferenceSpace{S}, - hypsography::HypsographyAdaption = Flat(), -) where {S <: Staggering} - grid = ExtrudedFiniteDifferenceGrid( + vertical_space::FiniteDifferenceSpace, + hypsography::Grids.HypsographyAdaption = Grids.Flat(), +) + grid = Grids.ExtrudedFiniteDifferenceGrid( horizontal_space.grid, vertical_space.grid, hypsography, ) - return ExtrudedFiniteDifferenceSpace{S}(grid) + return ExtrudedFiniteDifferenceSpace(grid, vertical_space.staggering) end -local_dss_weights(grid::ExtrudedFiniteDifferenceGrid) = - local_dss_weights(grid.horizontal_grid) local_dss_weights(space::ExtrudedFiniteDifferenceSpace) = - local_dss_weights(space.grid) + local_dss_weights(grid(space)) -face_space(space::ExtrudedFiniteDifferenceSpace) = - ExtrudedFiniteDifferenceSpace{CellFace}(space) -center_space(space::ExtrudedFiniteDifferenceSpace) = - ExtrudedFiniteDifferenceSpace{CellCenter}(space) +staggering(space::ExtrudedFiniteDifferenceSpace) = space.staggering +space(space::ExtrudedFiniteDifferenceSpace, staggering::Staggering) = + ExtrudedFiniteDifferenceSpace(grid(space), staggering) -staggering(space::ExtrudedFiniteDifferenceSpace) = space.staggering +#= ExtrudedFiniteDifferenceSpace{S}( horizontal_space::AbstractSpace, vertical_space::FiniteDifferenceSpace, hypsography::HypsographyAdaption = Flat(), ) where {S <: Staggering} = ExtrudedFiniteDifferenceSpace{S}( - ExtrudedFiniteDifferenceGrid(horizontal_space, vertical_space, hypsography), + Grids.ExtrudedFiniteDifferenceGrid(horizontal_space, vertical_space, hypsography), ) +=# #= function issubspace( @@ -144,16 +75,11 @@ end Adapt.adapt_structure(to, space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace( + Adapt.adapt(to, space.grid), space.staggering, - Adapt.adapt(to, Spaces.horizontal_space(space)), - Adapt.adapt(to, space.vertical_topology), - Adapt.adapt(to, space.hypsography), - Adapt.adapt(to, space.global_geometry), - Adapt.adapt(to, space.center_local_geometry), - Adapt.adapt(to, space.face_local_geometry), ) - +#= const CenterExtrudedFiniteDifferenceSpace2D = CenterExtrudedFiniteDifferenceSpace{<:SpectralElementSpace1D} @@ -163,7 +89,7 @@ const FaceExtrudedFiniteDifferenceSpace2D = FaceExtrudedFiniteDifferenceSpace{<:SpectralElementSpace1D} const FaceExtrudedFiniteDifferenceSpace3D = FaceExtrudedFiniteDifferenceSpace{<:SpectralElementSpace2D} - +=# function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) @@ -194,40 +120,28 @@ function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) print(iio, " "^(indent + 4), "mesh: ", space.vertical_topology.mesh) end - -local_geometry_data(::CellCenter, grid::ExtrudedFiniteDifferenceGrid) = - grid.center_local_geometry -local_geometry_data(::CellFace, grid::ExtrudedFiniteDifferenceGrid) = - grid.face_local_geometry -local_geometry_data(space::ExtrudedFiniteDifferenceSpace) = - local_geometry_data(space.staggering, space.grid) - -quadrature_style(grid::ExtrudedFiniteDifferenceGrid) = - quadrature_style(grid.horizontal_grid) quadrature_style(space::ExtrudedFiniteDifferenceSpace) = quadrature_style(space.grid) -topology(grid::ExtrudedFiniteDifferenceGrid) = topology(grid.horizontal_grid) topology(space::ExtrudedFiniteDifferenceSpace) = topology(space.grid) -ClimaComms.device(grid::ExtrudedFiniteDifferenceGrid) = - ClimaComms.device(topology(grid)) -ClimaComms.context(grid::ExtrudedFiniteDifferenceGrid) = - ClimaComms.context(topology(grid)) - -ClimaComms.device(space::ExtrudedFiniteDifferenceSpace) = - ClimaComms.device(space.grid) -ClimaComms.context(space::ExtrudedFiniteDifferenceSpace) = - ClimaComms.context(space.grid) - horizontal_space(full_space::ExtrudedFiniteDifferenceSpace) = - space(nothing, full_space.grid.horizontal_grid) + space(full_space.grid.horizontal_grid, nothing) -vertical_topology(grid::ExtrudedFiniteDifferenceGrid) = - topology(grid.vertical_grid) vertical_topology(space::ExtrudedFiniteDifferenceSpace) = vertical_topology(space.grid) + + +function column(space::ExtrudedFiniteDifferenceSpace, colidx::Grids.ColumnIndex) + column_grid = column(space.grid, colidx) + FiniteDifferenceSpace(column_grid, space.staggering) +end + + + + + Base.@propagate_inbounds function slab( space::ExtrudedFiniteDifferenceSpace, v, @@ -239,49 +153,6 @@ Base.@propagate_inbounds function slab( ) end -""" - ColumnIndex(ij,h) - -An index into a column of a field. This can be used as an argument to `getindex` -of a `Field`, to return a field on that column. - -# Example -```julia -colidx = ColumnIndex((1,1),1) -field[colidx] -``` -""" -struct ColumnIndex{N} - ij::NTuple{N, Int} - h::Int -end - - -struct ColumnGrid{G <: ExtrudedFiniteDifferenceGrid, C <: ColumnIndex} <: - AbstractFiniteDifferenceGrid - full_grid::G - colidx::C -end - - -column(grid::ExtrudedFiniteDifferenceGrid, colidx::ColumnIndex) = - ColumnGrid(grid, colidx) - - -function column(space::ExtrudedFiniteDifferenceSpace, colidx::ColumnIndex) - column_grid = column(space.grid, colidx) - FiniteDifferenceSpace(space.staggering, column_grid) -end - -topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) -vertical_topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) - -local_geometry_data(staggering::Staggering, colgrid::ColumnGrid) = - column(local_geometry_data(staggering, colgrid.full_grid), colgrid.colidx) - - -ClimaComms.device(colgrid::ColumnGrid) = ClimaComms.device(colgrid.full_grid) -ClimaComms.context(colgrid::ColumnGrid) = ClimaComms.context(colgrid.full_grid) # TODO: deprecate these diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 4bf9746991..7989184e45 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -1,186 +1,25 @@ -abstract type AbstractFiniteDifferenceGrid <: AbstractGrid end - -""" - FiniteDifferenceGrid(topology::Topologies.IntervalTopology) - FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) - -Construct a `FiniteDifferenceGrid` from an `IntervalTopology` (or an -`IntervalMesh`). - -This is an object which contains all the necessary geometric information. - -To avoid unnecessary duplication, we memoize the construction of the grid. -""" -mutable struct FiniteDifferenceGrid{ - T <: Topologies.AbstractIntervalTopology, - GG, - LG, -} <: AbstractFiniteDifferenceGrid - topology::T - global_geometry::GG - center_local_geometry::LG - face_local_geometry::LG -end - -topology(grid::FiniteDifferenceGrid) = grid.topology -Meshes.domain(grid::FiniteDifferenceGrid) = Meshes.domain(topology(grid)) - -ClimaComms.context(grid::FiniteDifferenceGrid) = - ClimaComms.context(topology(grid)) -ClimaComms.device(grid::FiniteDifferenceGrid) = - ClimaComms.device(topology(grid)) - - -@memoize WeakValueDict function FiniteDifferenceGrid( - topology::Topologies.IntervalTopology, -) - global_geometry = Geometry.CartesianGlobalGeometry() - mesh = topology.mesh - CT = Meshes.coordinate_type(mesh) - AIdx = Geometry.coordinate_axis(CT) - # TODO: FD operators hardcoded to work over the 3-axis, need to generalize - # similar to spectral operators - @assert AIdx == (3,) "FiniteDifference operations only work over the 3-axis (ZPoint) domain" - FT = eltype(CT) - ArrayType = ClimaComms.array_type(topology) - face_coordinates = collect(mesh.faces) - LG = Geometry.LocalGeometry{AIdx, CT, FT, SMatrix{1, 1, FT, 1}} - nface = length(face_coordinates) - Topologies.isperiodic(topology) - ncent = length(face_coordinates) - 1 - # contstruct on CPU, copy to device at end - center_local_geometry = DataLayouts.VF{LG}(Array{FT}, ncent) - face_local_geometry = DataLayouts.VF{LG}(Array{FT}, nface) - for i in 1:ncent - # centers - coord⁻ = Geometry.component(face_coordinates[i], 1) - coord⁺ = Geometry.component(face_coordinates[i + 1], 1) - # at the moment we use a "discrete Jacobian" - # ideally we should use the continuous quantity via the derivative of the warp function - # could we just define this then as deriv on the mesh element coordinates? - coord = (coord⁺ + coord⁻) / 2 - Δcoord = coord⁺ - coord⁻ - J = Δcoord - WJ = Δcoord - ∂x∂ξ = SMatrix{1, 1}(J) - center_local_geometry[i] = Geometry.LocalGeometry( - CT(coord), - J, - WJ, - Geometry.AxisTensor( - (Geometry.LocalAxis{AIdx}(), Geometry.CovariantAxis{AIdx}()), - ∂x∂ξ, - ), - ) - end - for i in 1:nface - coord = Geometry.component(face_coordinates[i], 1) - if i == 1 - # bottom face - if Topologies.isperiodic(topology) - Δcoord⁺ = - Geometry.component(face_coordinates[2], 1) - - Geometry.component(face_coordinates[1], 1) - Δcoord⁻ = - Geometry.component(face_coordinates[end], 1) - - Geometry.component(face_coordinates[end - 1], 1) - J = (Δcoord⁺ + Δcoord⁻) / 2 - WJ = J - else - coord⁺ = Geometry.component(face_coordinates[2], 1) - J = coord⁺ - coord - WJ = J / 2 - end - elseif !Topologies.isperiodic(topology) && i == nface - # top face - coord⁻ = Geometry.component(face_coordinates[i - 1], 1) - J = coord - coord⁻ - WJ = J / 2 - else - coord⁺ = Geometry.component(face_coordinates[i + 1], 1) - coord⁻ = Geometry.component(face_coordinates[i - 1], 1) - J = (coord⁺ - coord⁻) / 2 - WJ = J - end - ∂x∂ξ = SMatrix{1, 1}(J) - ∂ξ∂x = SMatrix{1, 1}(inv(J)) - face_local_geometry[i] = Geometry.LocalGeometry( - CT(coord), - J, - WJ, - Geometry.AxisTensor( - (Geometry.LocalAxis{AIdx}(), Geometry.CovariantAxis{AIdx}()), - ∂x∂ξ, - ), - ) - end - return FiniteDifferenceGrid( - topology, - global_geometry, - Adapt.adapt(ArrayType, center_local_geometry), - Adapt.adapt(ArrayType, face_local_geometry), - ) -end - -FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) = - FiniteDifferenceGrid(Topologies.IntervalTopology(mesh)) - - - -abstract type Staggering end - -""" - CellCenter() - -Cell center location -""" -struct CellCenter <: Staggering end - -""" - CellFace() - -Cell face location -""" -struct CellFace <: Staggering end - -# accessors - -local_geometry_data(::CellCenter, grid::FiniteDifferenceGrid) = - grid.center_local_geometry -local_geometry_data(::CellFace, grid::FiniteDifferenceGrid) = - grid.face_local_geometry - - - - - abstract type AbstractFiniteDifferenceSpace <: AbstractSpace end """ - FiniteDifferenceSpace(staggering::Staggering, grid::FiniteDifferenceGrid) + FiniteDifferenceSpace(staggering::Staggering, grid::Grids.FiniteDifferenceGrid) """ struct FiniteDifferenceSpace{ + G <: Grids.AbstractFiniteDifferenceGrid, S <: Staggering, - G <: AbstractFiniteDifferenceGrid, } <: AbstractFiniteDifferenceSpace - staggering::S grid::G + staggering::S end -const FaceFiniteDifferenceSpace = FiniteDifferenceSpace{CellFace} -const CenterFiniteDifferenceSpace = FiniteDifferenceSpace{CellCenter} +const FaceFiniteDifferenceSpace{G} = FiniteDifferenceSpace{G,CellFace} +const CenterFiniteDifferenceSpace{G} = FiniteDifferenceSpace{G,CellCenter} +grid(space::AbstractFiniteDifferenceSpace) = space.grid staggering(space::FiniteDifferenceSpace) = space.staggering -space(staggering::Staggering, grid::AbstractFiniteDifferenceGrid) = - FiniteDifferenceSpace(staggering, grid) - -local_geometry_data(space::FiniteDifferenceSpace) = - local_geometry_data(space.staggering, space.grid) - -ClimaComms.context(space::FiniteDifferenceSpace) = - ClimaComms.context(space.grid) -ClimaComms.device(space::FiniteDifferenceSpace) = ClimaComms.device(space.grid) +space(grid::Grids.AbstractFiniteDifferenceGrid, staggering::Staggering) = + FiniteDifferenceSpace(staggering, grid) function Base.show(io::IO, space::FiniteDifferenceSpace) indent = get(io, :indent, 0) @@ -197,41 +36,40 @@ function Base.show(io::IO, space::FiniteDifferenceSpace) print(iio, " "^(indent + 2), "mesh: ", topology(space).mesh) end +FaceFiniteDifferenceSpace(grid::Grids.AbstractFiniteDifferenceGrid) = + FiniteDifferenceSpace(grid, CellFace()) +CenterFiniteDifferenceSpace(grid::Grids.AbstractFiniteDifferenceGrid) = + FiniteDifferenceSpace(grid, CellCenter()) + +FaceFiniteDifferenceSpace(space::FiniteDifferenceSpace) = + FiniteDifferenceSpace(space, CellFace()) +CenterFiniteDifferenceSpace(space::FiniteDifferenceSpace) = + FiniteDifferenceSpace(space, CellCenter()) +FaceFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = + FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(topology), CellFace()) +CenterFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = + FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(topology), CellCenter()) + +FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = + FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(Grids.FiniteDifferenceGrid(mesh)), CellFace()) +CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = + FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(Grids.FiniteDifferenceGrid(mesh)), CellCenter()) -FiniteDifferenceSpace{S}(grid::FiniteDifferenceGrid) where {S <: Staggering} = - FiniteDifferenceSpace(S(), grid) -FiniteDifferenceSpace{S}(space::FiniteDifferenceSpace) where {S <: Staggering} = - FiniteDifferenceSpace(S(), space.grid) -FiniteDifferenceSpace{S}( - topology::Topologies.IntervalTopology, -) where {S <: Staggering} = - FiniteDifferenceSpace{S}(FiniteDifferenceGrid(topology)) -FiniteDifferenceSpace{S}(mesh::Meshes.IntervalMesh) where {S <: Staggering} = - FiniteDifferenceSpace{S}(FiniteDifferenceGrid(mesh)) -face_space(space::FiniteDifferenceSpace) = - FiniteDifferenceSpace{CellFace}(space) -center_space(space::FiniteDifferenceSpace) = - FiniteDifferenceSpace{CellCenter}(space) Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = FiniteDifferenceSpace( + Adapt.adapt(to, space.grid), space.staggering, - Adapt.adapt(to, space.topology), - Adapt.adapt(to, space.global_geometry), - Adapt.adapt(to, space.center_local_geometry), - Adapt.adapt(to, space.face_local_geometry), ) + + nlevels(space::FiniteDifferenceSpace) = length(space) # TODO: deprecate? Base.length(space::FiniteDifferenceSpace) = length(coordinates_data(space)) -topology(space::FiniteDifferenceSpace) = topology(space.grid) -vertical_topology(space::FiniteDifferenceSpace) = topology(space.grid) - - Base.@deprecate z_component(::Type{T}) where {T} Δz_metric_component(T) false diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 159b657946..5e4b61024e 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -3,12 +3,16 @@ abstract type AbstractSpectralElementSpace <: AbstractSpace end Topologies.nlocalelems(space::AbstractSpectralElementSpace) = Topologies.nlocalelems(Spaces.topology(space)) -local_geometry_data(space::AbstractSpectralElementSpace) = - local_geometry_data(space.grid) + quadrature_style(space::AbstractSpectralElementSpace) = - quadrature_style(space.grid) + quadrature_style(grid(space)) +local_dss_weights(space::AbstractSpectralElementSpace) = + local_dss_weights(grid(space)) +horizontal_space(space::AbstractSpectralElementSpace) = space +nlevels(space::AbstractSpectralElementSpace) = 1 + eachslabindex(space::AbstractSpectralElementSpace) = 1:Topologies.nlocalelems(Spaces.topology(space)) @@ -27,583 +31,45 @@ function Base.show(io::IO, space::AbstractSpectralElementSpace) print(iio, " "^(indent + 2), "quadrature: ", grid.quadrature_style) end -ClimaComms.device(space::AbstractSpectralElementSpace) = - ClimaComms.device(topology(space)) -ClimaComms.array_type(space::AbstractSpectralElementSpace) = - ClimaComms.array_type(ClimaComms.device(space)) -topology(space::AbstractSpectralElementSpace) = topology(space.grid) -horizontal_space(space::AbstractSpectralElementSpace) = space - - -abstract type AbstractSpectralElementGrid <: AbstractGrid end - -""" - SpectralElementGrid1D - -A one-dimensional space: within each element the space is represented as a polynomial. -""" -mutable struct SpectralElementGrid1D{ - T, - Q, - GG <: Geometry.AbstractGlobalGeometry, - LG, - D, -} <: AbstractSpectralElementGrid - topology::T - quadrature_style::Q - global_geometry::GG - local_geometry::LG - dss_weights::D -end - -@memoize WeakValueDict function SpectralElementGrid1D( - topology::Topologies.IntervalTopology, - quadrature_style::Quadratures.QuadratureStyle, -) - global_geometry = Geometry.CartesianGlobalGeometry() - CoordType = Topologies.coordinate_type(topology) - AIdx = Geometry.coordinate_axis(CoordType) - FT = eltype(CoordType) - nelements = Topologies.nlocalelems(topology) - Nq = Quadratures.degrees_of_freedom(quadrature_style) - - LG = Geometry.LocalGeometry{AIdx, CoordType, FT, SMatrix{1, 1, FT, 1}} - local_geometry = DataLayouts.IFH{LG, Nq}(Array{FT}, nelements) - quad_points, quad_weights = - Quadratures.quadrature_points(FT, quadrature_style) - - for elem in 1:nelements - local_geometry_slab = slab(local_geometry, elem) - for i in 1:Nq - ξ = quad_points[i] - # TODO: we need to massage the coordinate points because the grid is assumed 2D - vcoords = Topologies.vertex_coordinates(topology, elem) - x = Geometry.linear_interpolate(vcoords, ξ) - ∂x∂ξ = - ( - Geometry.component(vcoords[2], 1) - - Geometry.component(vcoords[1], 1) - ) / 2 - J = abs(∂x∂ξ) - WJ = J * quad_weights[i] - local_geometry_slab[i] = Geometry.LocalGeometry( - x, - J, - WJ, - Geometry.AxisTensor( - ( - Geometry.LocalAxis{AIdx}(), - Geometry.CovariantAxis{AIdx}(), - ), - ∂x∂ξ, - ), - ) - end - end - dss_weights = copy(local_geometry.J) - dss_weights .= one(FT) - dss_1d!(topology, dss_weights) - dss_weights = one(FT) ./ dss_weights - - return SpectralElementGrid1D( - topology, - quadrature_style, - global_geometry, - local_geometry, - dss_weights, - ) -end - -local_geometry_data(grid::SpectralElementGrid1D) = grid.local_geometry -topology(grid::SpectralElementGrid1D) = grid.topology -quadrature_style(grid::SpectralElementGrid1D) = grid.quadrature_style - -local_dss_weights(grid::SpectralElementGrid1D) = grid.dss_weights +# 1D struct SpectralElementSpace1D{G} <: AbstractSpectralElementSpace grid::G end +space(grid::Grids.SpectralElementGrid1D, ::Nothing) = + SpectralElementSpace1D(grid) function SpectralElementSpace1D( topology::Topologies.IntervalTopology, quadrature_style::Quadratures.QuadratureStyle, ) - grid = SpectralElementGrid1D(topology, quadrature_style) + grid = Grids.SpectralElementGrid1D(topology, quadrature_style) SpectralElementSpace1D(grid) end -space(::Nothing, grid::SpectralElementGrid1D) = - SpectralElementSpace1D(grid) - -nlevels(space::SpectralElementSpace1D) = 1 - - - -""" - SpectralElementSpace2D <: AbstractSpace - -A two-dimensional space: within each element the space is represented as a polynomial. -""" -mutable struct SpectralElementGrid2D{ - T, - Q, - GG <: Geometry.AbstractGlobalGeometry, - LG, - D, - IS, - BS, -} <: AbstractSpectralElementGrid - topology::T - quadrature_style::Q - global_geometry::GG - local_geometry::LG - local_dss_weights::D - internal_surface_geometry::IS - boundary_surface_geometries::BS -end - -struct DeviceSpectralElementGrid2D{Q, GG, LG} <: AbstractGrid - quadrature_style::Q - global_geometry::GG - local_geometry::LG -end - -Adapt.adapt_structure(to, grid::SpectralElementGrid2D) = - DeviceSpectralElementGrid2D( - Adapt.adapt(to, grid.quadrature_style), - Adapt.adapt(to, grid.global_geometry), - Adapt.adapt(to, grid.local_geometry), - ) - - - -""" - SpectralElementSpace2D(topology, quadrature_style; enable_bubble) - -Construct a `SpectralElementSpace2D` instance given a `topology` and `quadrature`. The -flag `enable_bubble` enables the `bubble correction` for more accurate element areas. - -# Input arguments: -- topology: Topology2D -- quadrature_style: QuadratureStyle -- enable_bubble: Bool - -The idea behind the so-called `bubble_correction` is that the numerical area -of the domain (e.g., the sphere) is given by the sum of nodal integration weights -times their corresponding Jacobians. However, this discrete sum is not exactly -equal to the exact geometric area (4pi*radius^2 for the sphere). To make these equal, -the "epsilon bubble" approach modifies the inner weights in each element so that -geometric and numerical areas of each element match. - -Let ``\\Delta A^e := A^e_{exact} - A^e_{approx}``, then, in -the case of linear elements, we correct ``W_{i,j} J^e_{i,j}`` by: -```math -\\widehat{W_{i,j} J^e}_{i,j} = W_{i,j} J^e_{i,j} + \\Delta A^e * W_{i,j} / Nq^2 . -``` -and the case of non linear elements, by -```math -\\widehat{W_{i,j} J^e}_{i,j} = W_{i,j} J^e_{i,j} \\left( 1 + \\tilde{A}^e \\right) , -``` -where ``\\tilde{A}^e`` is the approximated area given by the sum of the interior nodal integration weights. - -Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSphere`](@ref) and -[`Meshes.EquidistantCubedSphere`](@ref) type, not for [`Meshes.ConformalCubedSphere`](@ref). -""" -@memoize WeakValueDict function SpectralElementGrid2D( - topology, - quadrature_style; - enable_bubble = false, -) - - # 1. compute localgeom for local elememts - # 2. ghost exchange of localgeom - # 3. do a round of dss on WJs - # 4. compute dss weights (WJ ./ dss(WJ)) (local and ghost) - - # DSS on a field would consist of - # 1. copy to send buffers - # 2. start exchange - # 3. dss of internal connections - # - option for weighting and transformation - # 4. finish exchange - # 5. dss of ghost connections - - ### How to DSS multiple fields? - # 1. allocate buffers externally - DA = ClimaComms.array_type(topology) - domain = Topologies.domain(topology) - if domain isa Domains.SphereDomain - CoordType3D = Topologies.coordinate_type(topology) - FT = Geometry.float_type(CoordType3D) - CoordType2D = Geometry.LatLongPoint{FT} # Domains.coordinate_type(topology) - global_geometry = - Geometry.SphericalGlobalGeometry(topology.mesh.domain.radius) - else - CoordType2D = Topologies.coordinate_type(topology) - FT = Geometry.float_type(CoordType2D) - global_geometry = Geometry.CartesianGlobalGeometry() - end - AIdx = Geometry.coordinate_axis(CoordType2D) - nlelems = Topologies.nlocalelems(topology) - ngelems = Topologies.nghostelems(topology) - Nq = Quadratures.degrees_of_freedom(quadrature_style) - high_order_quadrature_style = Spaces.Quadratures.GLL{Nq * 2}() - high_order_Nq = Quadratures.degrees_of_freedom(high_order_quadrature_style) - - LG = Geometry.LocalGeometry{AIdx, CoordType2D, FT, SMatrix{2, 2, FT, 4}} - - local_geometry = DataLayouts.IJFH{LG, Nq}(Array{FT}, nlelems) - - quad_points, quad_weights = - Quadratures.quadrature_points(FT, quadrature_style) - high_order_quad_points, high_order_quad_weights = - Quadratures.quadrature_points(FT, high_order_quadrature_style) - for (lidx, elem) in enumerate(Topologies.localelems(topology)) - elem_area = zero(FT) - high_order_elem_area = zero(FT) - Δarea = zero(FT) - interior_elem_area = zero(FT) - rel_interior_elem_area_Δ = zero(FT) - local_geometry_slab = slab(local_geometry, lidx) - # high-order quadrature loop for computing geometric element face area. - for i in 1:high_order_Nq, j in 1:high_order_Nq - ξ = SVector(high_order_quad_points[i], high_order_quad_points[j]) - u, ∂u∂ξ = - compute_local_geometry(global_geometry, topology, elem, ξ, AIdx) - J_high_order = det(Geometry.components(∂u∂ξ)) - WJ_high_order = - J_high_order * - high_order_quad_weights[i] * - high_order_quad_weights[j] - high_order_elem_area += WJ_high_order - end - # low-order quadrature loop for computing numerical element face area - for i in 1:Nq, j in 1:Nq - ξ = SVector(quad_points[i], quad_points[j]) - u, ∂u∂ξ = - compute_local_geometry(global_geometry, topology, elem, ξ, AIdx) - J = det(Geometry.components(∂u∂ξ)) - WJ = J * quad_weights[i] * quad_weights[j] - elem_area += WJ - if !enable_bubble - local_geometry_slab[i, j] = - Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) - end - end - - # If enabled, apply bubble correction - if enable_bubble - if abs(elem_area - high_order_elem_area) ≤ eps(FT) - for i in 1:Nq, j in 1:Nq - ξ = SVector(quad_points[i], quad_points[j]) - u, ∂u∂ξ = compute_local_geometry( - global_geometry, - topology, - elem, - ξ, - AIdx, - ) - J = det(Geometry.components(∂u∂ξ)) - WJ = J * quad_weights[i] * quad_weights[j] - local_geometry_slab[i, j] = - Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) - end - else - # The idea behind the so-called `bubble_correction` is that - # the numerical area of the domain (e.g., the sphere) is given by the sum - # of nodal integration weights times their corresponding Jacobians. However, - # this discrete sum is not exactly equal to the exact geometric area - # (4pi*radius^2 for the sphere). It is required that numerical area = geometric area. - # The "epsilon bubble" approach modifies the inner weights in each - # element so that geometric and numerical areas of each element match. - - # Compute difference between geometric area of an element and its approximate numerical area - Δarea = high_order_elem_area - elem_area - - # Linear elements: Nq == 2 (SpectralElementSpace2D cannot have Nq < 2) - # Use uniform bubble correction - if Nq == 2 - for i in 1:Nq, j in 1:Nq - ξ = SVector(quad_points[i], quad_points[j]) - u, ∂u∂ξ = compute_local_geometry( - global_geometry, - topology, - elem, - ξ, - AIdx, - ) - J = det(Geometry.components(∂u∂ξ)) - J += Δarea / Nq^2 - WJ = J * quad_weights[i] * quad_weights[j] - local_geometry_slab[i, j] = - Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) - end - else # Higher-order elements: Use HOMME bubble correction for the interior nodes - for i in 2:(Nq - 1), j in 2:(Nq - 1) - ξ = SVector(quad_points[i], quad_points[j]) - u, ∂u∂ξ = compute_local_geometry( - global_geometry, - topology, - elem, - ξ, - AIdx, - ) - J = det(Geometry.components(∂u∂ξ)) - WJ = J * quad_weights[i] * quad_weights[j] - interior_elem_area += WJ - end - # Check that interior_elem_area is not too small - if abs(interior_elem_area) ≤ sqrt(eps(FT)) - error( - "Bubble correction cannot be performed; sum of inner weights is too small.", - ) - end - rel_interior_elem_area_Δ = Δarea / interior_elem_area - - for i in 1:Nq, j in 1:Nq - ξ = SVector(quad_points[i], quad_points[j]) - u, ∂u∂ξ = compute_local_geometry( - global_geometry, - topology, - elem, - ξ, - AIdx, - ) - J = det(Geometry.components(∂u∂ξ)) - # Modify J only for interior nodes - if i != 1 && j != 1 && i != Nq && j != Nq - J *= (1 + rel_interior_elem_area_Δ) - end - WJ = J * quad_weights[i] * quad_weights[j] - # Finally allocate local geometry - local_geometry_slab[i, j] = - Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) - end - end - end - end - end - - # alternatively, we could do a ghost exchange here? - if topology isa Topologies.Topology2D - for (ridx, elem) in enumerate(Topologies.ghostelems(topology)) - for i in 1:Nq, j in 1:Nq - ξ = SVector(quad_points[i], quad_points[j]) - u, ∂u∂ξ = compute_local_geometry( - global_geometry, - topology, - elem, - ξ, - AIdx, - ) - J = det(Geometry.components(∂u∂ξ)) - WJ = J * quad_weights[i] * quad_weights[j] - - end - end - end - # dss_weights = J ./ dss(J) - J = DataLayouts.rebuild(local_geometry.J, DA) - dss_local_weights = copy(J) - if quadrature_style isa Quadratures.GLL - dss!(dss_local_weights, topology, quadrature_style) - end - dss_local_weights .= J ./ dss_local_weights - - SG = Geometry.SurfaceGeometry{ - FT, - Geometry.AxisVector{FT, Geometry.LocalAxis{AIdx}, SVector{2, FT}}, - } - interior_faces = Array(Topologies.interior_faces(topology)) - - if quadrature_style isa Quadratures.GLL - internal_surface_geometry = - DataLayouts.IFH{SG, Nq}(Array{FT}, length(interior_faces)) - for (iface, (lidx⁻, face⁻, lidx⁺, face⁺, reversed)) in - enumerate(interior_faces) - internal_surface_geometry_slab = - slab(internal_surface_geometry, iface) - - local_geometry_slab⁻ = slab(local_geometry, lidx⁻) - local_geometry_slab⁺ = slab(local_geometry, lidx⁺) - - for q in 1:Nq - sgeom⁻ = compute_surface_geometry( - local_geometry_slab⁻, - quad_weights, - face⁻, - q, - false, - ) - sgeom⁺ = compute_surface_geometry( - local_geometry_slab⁺, - quad_weights, - face⁺, - q, - reversed, - ) - - @assert sgeom⁻.sWJ ≈ sgeom⁺.sWJ - @assert sgeom⁻.normal ≈ -sgeom⁺.normal - - internal_surface_geometry_slab[q] = sgeom⁻ - end - end - internal_surface_geometry = - DataLayouts.rebuild(internal_surface_geometry, DA) - - boundary_surface_geometries = - map(Topologies.boundary_tags(topology)) do boundarytag - boundary_faces = - Topologies.boundary_faces(topology, boundarytag) - boundary_surface_geometry = - DataLayouts.IFH{SG, Nq}(Array{FT}, length(boundary_faces)) - for (iface, (elem, face)) in enumerate(boundary_faces) - boundary_surface_geometry_slab = - slab(boundary_surface_geometry, iface) - local_geometry_slab = slab(local_geometry, elem) - for q in 1:Nq - boundary_surface_geometry_slab[q] = - compute_surface_geometry( - local_geometry_slab, - quad_weights, - face, - q, - false, - ) - end - end - DataLayouts.rebuild(boundary_surface_geometry, DA) - end - else - internal_surface_geometry = nothing - boundary_surface_geometries = nothing - end - return SpectralElementGrid2D( - topology, - quadrature_style, - global_geometry, - DataLayouts.rebuild(local_geometry, DA), - dss_local_weights, - internal_surface_geometry, - boundary_surface_geometries, - ) -end - - -local_geometry_data(grid::SpectralElementGrid2D) = grid.local_geometry -topology(grid::SpectralElementGrid2D) = grid.topology -quadrature_style(grid::SpectralElementGrid2D) = grid.quadrature_style - -local_dss_weights(grid::SpectralElementGrid2D) = grid.local_dss_weights - +# 2D struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace grid::G end - -space(::Nothing, grid::SpectralElementGrid2D) = +space(grid::Grids.SpectralElementGrid2D, ::Nothing) = SpectralElementSpace2D(grid) -SpectralElementSpace2D(topology::Topologies.Topology2D, quadrature_style) = - SpectralElementSpace2D(SpectralElementGrid2D(topology, quadrature_style)) +function SpectralElementSpace2D( + topology::Topologies.Topology2D, + quadrature_style::Quadratures.QuadratureStyle, +) + grid = Grids.SpectralElementGrid2D(topology, quadrature_style) + SpectralElementSpace2D(grid) +end -nlevels(space::SpectralElementSpace2D) = 1 -perimeter(space::SpectralElementSpace2D) = - Perimeter2D(Quadratures.degrees_of_freedom(quadrature_style(space))) -local_geometry_data(space::SpectralElementSpace2D) = - local_geometry_data(space.grid) -topology(space::SpectralElementSpace2D) = topology(space.grid) -quadrature_style(space::SpectralElementSpace2D) = quadrature_style(space.grid) - -local_dss_weights(space::SpectralElementSpace2D) = local_dss_weights(space.grid) - - -#= -const RectilinearSpectralElementSpace2D = SpectralElementSpace2D{ -<:Topologies.Topology2D{ - <:ClimaComms.AbstractCommsContext, - <:Meshes.RectilinearMesh, -}, -} - -const CubedSphereSpectralElementSpace2D = SpectralElementSpace2D{ -<:Topologies.Topology2D{ - <:ClimaComms.AbstractCommsContext, - <:Meshes.AbstractCubedSphere, -}, -} -=# -function compute_local_geometry( - global_geometry::Geometry.SphericalGlobalGeometry, - topology, - elem, - ξ, - AIdx, -) - x = Meshes.coordinates(topology.mesh, elem, ξ) - u = Geometry.LatLongPoint(x, global_geometry) - ∂x∂ξ = Geometry.AxisTensor( - (Geometry.Cartesian123Axis(), Geometry.CovariantAxis{AIdx}()), - ForwardDiff.jacobian(ξ) do ξ - Geometry.components(Meshes.coordinates(topology.mesh, elem, ξ)) - end, - ) - G = Geometry.local_to_cartesian(global_geometry, u) - ∂u∂ξ = Geometry.project(Geometry.LocalAxis{AIdx}(), G' * ∂x∂ξ) - return u, ∂u∂ξ -end -function compute_local_geometry( - global_geometry::Geometry.AbstractGlobalGeometry, - topology, - elem, - ξ, - AIdx, -) - u = Meshes.coordinates(topology.mesh, elem, ξ) - ∂u∂ξ = Geometry.AxisTensor( - (Geometry.LocalAxis{AIdx}(), Geometry.CovariantAxis{AIdx}()), - ForwardDiff.jacobian(ξ) do ξ - Geometry.components(Meshes.coordinates(topology.mesh, elem, ξ)) - end, - ) - return u, ∂u∂ξ -end -function compute_surface_geometry( - local_geometry_slab, - quad_weights, - face, - q, - reversed = false, -) - Nq = length(quad_weights) - @assert size(local_geometry_slab) == (Nq, Nq, 1, 1, 1) - i, j = Topologies.face_node_index(face, Nq, q, reversed) - - local_geometry = local_geometry_slab[i, j] - @unpack J, ∂ξ∂x = local_geometry - - # surface mass matrix - n = if face == 4 - -J * ∂ξ∂x[1, :] * quad_weights[j] - elseif face == 2 - J * ∂ξ∂x[1, :] * quad_weights[j] - elseif face == 1 - -J * ∂ξ∂x[2, :] * quad_weights[i] - elseif face == 3 - J * ∂ξ∂x[2, :] * quad_weights[i] - end - sWJ = norm(n) - n = n / sWJ - return Geometry.SurfaceGeometry(sWJ, n) -end """ SpectralElementSpaceSlab <: AbstractSpace @@ -649,37 +115,6 @@ Base.@propagate_inbounds function column(space::SpectralElementSpace2D, i, j, h) PointSpace(local_geometry) end -# XXX: this cannot take `space` as it must be constructed beforehand so -# that the `space` constructor can do DSS (to compute DSS weights) -function setup_comms( - Context::Type{<:ClimaComms.AbstractCommsContext}, - topology::Topologies.AbstractDistributedTopology, - quad_style::Spaces.Quadratures.QuadratureStyle, - Nv, - Nf = 2, -) - Ni = Quadratures.degrees_of_freedom(quad_style) - Nj = Ni - AT = Array # XXX: get this from `space`/`topology`? - FT = Geometry.float_type(Topologies.coordinate_type(topology)) - - # Determine send and receive buffer dimensions for each neighbor PID - # and add the neighbors in the same order as they are stored in - # `neighbor_pids`! - nbrs = ClimaComms.Neighbor[] - for (nidx, npid) in enumerate(Topologies.neighbors(topology)) - nse = Topologies.nsendelems(topology, nidx) - nge = Topologies.nghostelems(topology, nidx) - send_dims = (Nv, Ni, Nj, Nf, nse) - recv_dims = (Nv, Ni, Nj, Nf, nge) - push!( - nbrs, - ClimaComms.Neighbor(Context, npid, AT, FT, send_dims, recv_dims), - ) - end - return Context(nbrs) -end - function all_nodes(space::SpectralElementSpace2D) Nq = Quadratures.degrees_of_freedom(quadrature_style(space)) nelem = Topologies.nlocalelems(topology(space)) @@ -793,6 +228,9 @@ end abstract type AbstractPerimeter end +perimeter(space::SpectralElementSpace2D) = + Perimeter2D(Quadratures.degrees_of_freedom(quadrature_style(space))) + """ Perimeter2D <: AbstractPerimeter diff --git a/test/Spaces/quadrature.jl b/test/Spaces/quadrature.jl index cd2951e8cd..82916a8fc2 100644 --- a/test/Spaces/quadrature.jl +++ b/test/Spaces/quadrature.jl @@ -1,6 +1,6 @@ using Test using LinearAlgebra, StaticArrays -import ClimaCore.Spaces: Quadratures +import ClimaCore.Quadratures f(x) = x^3 + 2x^2 + 3x + 4 diff --git a/test/runtests.jl b/test/runtests.jl index 98f497faca..c240378881 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -35,7 +35,7 @@ if !Sys.iswindows() @safetestset "Cubedsphere surface topology" begin @time include("Topologies/cubedsphere_sfc.jl") end # now part of buildkite # @safetestset "Distributed topology" begin @time include("Topologies/distributed.jl") end - +=# @safetestset "Quadrature" begin @time include("Spaces/quadrature.jl") end @safetestset "Spaces" begin @time include("Spaces/spaces.jl") end #= @@ -60,7 +60,7 @@ if !Sys.iswindows() @safetestset "Spectral elem - sphere diffusion vec" begin @time include("Operators/spectralelement/sphere_diffusion_vec.jl") end @safetestset "Spectral elem - sphere hyperdiffusion" begin @time include("Operators/spectralelement/sphere_hyperdiffusion.jl") end @safetestset "Spectral elem - sphere hyperdiffusion vec" begin @time include("Operators/spectralelement/sphere_hyperdiffusion_vec.jl") end - =# + @safetestset "FD ops - column" begin @time include("Operators/finitedifference/column.jl") end @safetestset "FD ops - opt" begin @time include("Operators/finitedifference/opt.jl") end @safetestset "FD ops - wfact" begin @time include("Operators/finitedifference/wfact.jl") end From a1e3b742555606ac49c1f19bf93a896cde8bcbef Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Thu, 19 Oct 2023 14:39:36 -0700 Subject: [PATCH 13/42] move DSS (WIP) --- src/Spaces/Spaces.jl | 3 +- src/Spaces/spectralelement.jl | 49 --------------------- src/Topologies/Topologies.jl | 4 ++ src/{Spaces => Topologies}/dss.jl | 0 src/{Spaces => Topologies}/dss_cuda.jl | 0 src/{Spaces => Topologies}/dss_transform.jl | 0 src/Topologies/topology2d.jl | 42 ++++++++++++++++++ 7 files changed, 47 insertions(+), 51 deletions(-) rename src/{Spaces => Topologies}/dss.jl (100%) rename src/{Spaces => Topologies}/dss_cuda.jl (100%) rename src/{Spaces => Topologies}/dss_transform.jl (100%) diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index 2f876fe3fd..abd383f41c 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -80,8 +80,7 @@ include("spectralelement.jl") include("finitedifference.jl") include("extruded.jl") include("triangulation.jl") -include("dss_transform.jl") -include("dss.jl") + weighted_jacobian(space::Spaces.AbstractSpace) = local_geometry_data(space).WJ diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 5e4b61024e..8003ce0a4b 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -224,52 +224,3 @@ function Base.iterate( return ((i, j), e), ((i, j), e) end end - - -abstract type AbstractPerimeter end - -perimeter(space::SpectralElementSpace2D) = - Perimeter2D(Quadratures.degrees_of_freedom(quadrature_style(space))) - -""" - Perimeter2D <: AbstractPerimeter - -Iterate over the perimeter degrees of freedom of a 2D spectral element. -""" -struct Perimeter2D{Nq} <: AbstractPerimeter end - -""" - Perimeter2D(Nq) - -Construct a perimeter iterator for a 2D spectral element of degree `(Nq-1)`. -""" -Perimeter2D(Nq) = Perimeter2D{Nq}() -Adapt.adapt_structure(to, x::Perimeter2D) = x - -function Base.iterate(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} - if loc < 5 - return (Topologies.vertex_node_index(loc, Nq), loc + 1) - elseif loc ≤ nperimeter2d(Nq) - f = cld(loc - 4, Nq - 2) - n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) - return (Topologies.face_node_index(f, Nq, 1 + n), loc + 1) - else - return nothing - end -end - -function Base.getindex(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} - if loc < 1 || loc > nperimeter2d(Nq) - return (-1, -1) - elseif loc < 5 - return Topologies.vertex_node_index(loc, Nq) - else - f = cld(loc - 4, Nq - 2) - n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) - return Topologies.face_node_index(f, Nq, 1 + n) - end -end - -nperimeter2d(Nq) = 4 + (Nq - 2) * 4 -nperimeter(::Perimeter2D{Nq}) where {Nq} = nperimeter2d(Nq) -Base.length(::Perimeter2D{Nq}) where {Nq} = nperimeter2d(Nq) diff --git a/src/Topologies/Topologies.jl b/src/Topologies/Topologies.jl index 31ae65e3f5..0bc713a06e 100644 --- a/src/Topologies/Topologies.jl +++ b/src/Topologies/Topologies.jl @@ -8,6 +8,7 @@ import ClimaComms import ..Geometry import ..Domains: Domains, coordinate_type import ..Meshes: Meshes, domain, coordinates +import ..DataLayouts using Memoize, WeakValueDicts @@ -339,6 +340,9 @@ function boundary_faces end include("interval.jl") include("topology2d.jl") +include("dss_transform.jl") +include("dss.jl") + # deprecate @deprecate boundaries(topology::AbstractTopology) boundary_tags(topology) diff --git a/src/Spaces/dss.jl b/src/Topologies/dss.jl similarity index 100% rename from src/Spaces/dss.jl rename to src/Topologies/dss.jl diff --git a/src/Spaces/dss_cuda.jl b/src/Topologies/dss_cuda.jl similarity index 100% rename from src/Spaces/dss_cuda.jl rename to src/Topologies/dss_cuda.jl diff --git a/src/Spaces/dss_transform.jl b/src/Topologies/dss_transform.jl similarity index 100% rename from src/Spaces/dss_transform.jl rename to src/Topologies/dss_transform.jl diff --git a/src/Topologies/topology2d.jl b/src/Topologies/topology2d.jl index d74285cfb0..06f31fa01d 100644 --- a/src/Topologies/topology2d.jl +++ b/src/Topologies/topology2d.jl @@ -718,3 +718,45 @@ boundary_tag(topology::Topology2D, boundary_name::Symbol) = findfirst(==(boundary_name), boundary_names(topology)) boundary_faces(topology::Topology2D, boundary) = topology.boundaries[boundary] + + + + +abstract type AbstractPerimeter end + +struct Perimeter2D{Nq} <: AbstractPerimeter end + +""" + Perimeter2D(Nq) + +Construct a perimeter iterator for a 2D spectral element with `Nq` nodes per +dimension (i.e. polynomial degree `Nq-1`). +""" +Perimeter2D(Nq) = Perimeter2D{Nq}() +Adapt.adapt_structure(to, x::Perimeter2D) = x + +Base.length(::Perimeter2D{Nq}) where {Nq} = 4 + (Nq - 1) * 4 + +function Base.iterate(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} + if loc <= 5 + return (vertex_node_index(loc, Nq), loc + 1) + elseif loc ≤ length(perimeter) + f = cld(loc - 4, Nq - 2) + n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) + return (Topologies.face_node_index(f, Nq, 1 + n), loc + 1) + else + return nothing + end +end + +function Base.getindex(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} + if loc < 1 || loc > nperimeter2d(Nq) + return (-1, -1) + elseif loc < 5 + return Topologies.vertex_node_index(loc, Nq) + else + f = cld(loc - 4, Nq - 2) + n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) + return Topologies.face_node_index(f, Nq, 1 + n) + end +end From aac67b8218bd8157384e3cd4932f2f04c7155749 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 20 Oct 2023 12:24:38 -0700 Subject: [PATCH 14/42] WIP --- src/Spaces/Spaces.jl | 2 +- src/Spaces/dss.jl | 300 ++++++++++++++++++++++++++++++ src/Topologies/Topologies.jl | 2 +- src/Topologies/dss.jl | 344 ++++------------------------------- src/Topologies/dss_cuda.jl | 18 +- 5 files changed, 343 insertions(+), 323 deletions(-) create mode 100644 src/Spaces/dss.jl diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index abd383f41c..0b2bcf9eac 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -80,7 +80,7 @@ include("spectralelement.jl") include("finitedifference.jl") include("extruded.jl") include("triangulation.jl") - +include("dss.jl") weighted_jacobian(space::Spaces.AbstractSpace) = local_geometry_data(space).WJ diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl new file mode 100644 index 0000000000..e0c6c7a324 --- /dev/null +++ b/src/Spaces/dss.jl @@ -0,0 +1,300 @@ +import ..Topologies: + DSSBuffer, + create_dss_buffer, + assert_same_eltype, + dss_1d!, + dss_transform!, + dss_untransform!, + dss_local!, + dss_local_ghost!, + fill_send_buffer!, + load_from_recv_buffer! + + +""" + create_dss_buffer( + data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, + hspace::AbstractSpectralElementSpace, + ) where {S, Nij} + +Creates a [`DSSBuffer`](@ref) for the field data corresponding to `data` +""" +function create_dss_buffer( + data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, + hspace::AbstractSpectralElementSpace, +) where {S, Nij} + create_dss_buffer(data, topology(hspace), local_geometry_data(hspace), local_dss_weights(hspace)) +end + + +""" + function weighted_dss!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{ + AbstractSpectralElementSpace, + ExtrudedFiniteDifferenceSpace, + }, + dss_buffer::Union{DSSBuffer, Nothing}, + ) + +Computes weighted dss of `data`. + +It comprises of the following steps: + +1). [`Spaces.weighted_dss_start!`](@ref) + +2). [`Spaces.weighted_dss_internal!`](@ref) + +3). [`Spaces.weighted_dss_ghost!`](@ref) +""" +function weighted_dss!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, + dss_buffer::Union{DSSBuffer, Nothing}, +) + assert_same_eltype(data, dss_buffer) + weighted_dss_start!(data, space, dss_buffer) + weighted_dss_internal!(data, space, dss_buffer) + weighted_dss_ghost!(data, space, dss_buffer) +end + + +""" + weighted_dss_start!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{ + AbstractSpectralElementSpace, + ExtrudedFiniteDifferenceSpace, + }, + dss_buffer::Union{DSSBuffer, Nothing}, + ) + +It comprises of the following steps: + +1). Apply [`Spaces.dss_transform!`](@ref) on perimeter elements. This weights and tranforms vector +fields to physical basis if needed. Scalar fields are weighted. The transformed and/or weighted +perimeter `data` is stored in `perimeter_data`. + +2). Apply [`Spaces.dss_local_ghost!`](@ref) +This computes partial weighted DSS on ghost vertices, using only the information from `local` vertices. + +3). [`Spaces.fill_send_buffer!`](@ref) +Loads the send buffer from `perimeter_data`. For unique ghost vertices, only data from the +representative ghost vertices which store result of "ghost local" DSS are loaded. + +4). Start DSS communication with neighboring processes +""" +weighted_dss_start!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, + dss_buffer::Union{DSSBuffer, Nothing}, +) = weighted_dss_start!(data, space, horizontal_space(space), dss_buffer) + + + +function weighted_dss_start!( + data::Union{DataLayouts.IJFH, DataLayouts.VIJFH}, + space::Union{ + Spaces.SpectralElementSpace2D, + Spaces.ExtrudedFiniteDifferenceSpace, + }, + hspace::SpectralElementSpace2D{<:Topologies.Topology2D}, + dss_buffer::DSSBuffer, +) + assert_same_eltype(data, dss_buffer) + length(parent(data)) == 0 && return nothing + device = ClimaComms.device(topology(hspace)) + dss_transform!( + device, + dss_buffer, + data, + local_geometry_data(space), + local_dss_weights(hspace), + Spaces.perimeter(hspace), + dss_buffer.perimeter_elems, + ) + dss_local_ghost!( + device, + dss_buffer.perimeter_data, + Spaces.perimeter(hspace), + topology(hspace), + ) + fill_send_buffer!(device, dss_buffer) + ClimaComms.start(dss_buffer.graph_context) + return nothing +end + +weighted_dss_start!(data, space, hspace, dss_buffer::Nothing) = nothing + +""" + weighted_dss_internal!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{ + AbstractSpectralElementSpace, + ExtrudedFiniteDifferenceSpace, + }, + dss_buffer::DSSBuffer, + ) + +1). Apply [`Spaces.dss_transform!`](@ref) on interior elements. Local elements are split into interior +and perimeter elements to facilitate overlapping of communication with computation. + +2). Probe communication + +3). [`Spaces.dss_local!`](@ref) computes the weighted DSS on local vertices and faces. +""" +weighted_dss_internal!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, + dss_buffer::Union{DSSBuffer, Nothing}, +) = weighted_dss_internal!(data, space, horizontal_space(space), dss_buffer) + + +function weighted_dss_internal!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, + hspace::AbstractSpectralElementSpace, + dss_buffer::Union{DSSBuffer, Nothing}, +) + assert_same_eltype(data, dss_buffer) + length(parent(data)) == 0 && return nothing + if hspace isa SpectralElementSpace1D + dss_1d!( + topology(hspace), + data, + local_geometry_data(space), + local_dss_weights(space), + ) + else + device = ClimaComms.device(topology(hspace)) + dss_transform!( + device, + dss_buffer, + data, + local_geometry_data(space), + local_dss_weights(space), + Spaces.perimeter(hspace), + dss_buffer.internal_elems, + ) + dss_local!( + device, + dss_buffer.perimeter_data, + Spaces.perimeter(hspace), + topology(hspace), + ) + dss_untransform!( + device, + dss_buffer, + data, + local_geometry_data(space), + Spaces.perimeter(hspace), + dss_buffer.internal_elems, + ) + end + return nothing +end + + +""" + weighted_dss_ghost!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{ + AbstractSpectralElementSpace, + ExtrudedFiniteDifferenceSpace, + }, + dss_buffer::Union{DSSBuffer, Nothing}, + ) + +1). Finish communications. + +2). Call [`Spaces.load_from_recv_buffer!`](@ref) +After the communication is complete, this adds data from the recv buffer to the corresponding location in +`perimeter_data`. For ghost vertices, this data is added only to the representative vertices. The values are +then scattered to other local vertices corresponding to each unique ghost vertex in `dss_local_ghost`. + +3). Call [`Spaces.dss_untransform!`](@ref) on all local elements. +This transforms the DSS'd local vectors back to Covariant12 vectors, and copies the DSS'd data from the +`perimeter_data` to `data`. +""" +weighted_dss_ghost!( + data::Union{ + DataLayouts.IFH, + DataLayouts.VIFH, + DataLayouts.IJFH, + DataLayouts.VIJFH, + }, + space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, + dss_buffer::Union{DSSBuffer, Nothing}, +) = weighted_dss_ghost!(data, space, horizontal_space(space), dss_buffer) + + + +function weighted_dss_ghost!( + data::Union{DataLayouts.IJFH, DataLayouts.VIJFH}, + space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, + hspace::SpectralElementSpace2D{<:Topologies.Topology2D}, + dss_buffer::DSSBuffer, +) + assert_same_eltype(data, dss_buffer) + length(parent(data)) == 0 && return data + device = ClimaComms.device(topology(hspace)) + ClimaComms.finish(dss_buffer.graph_context) + load_from_recv_buffer!(device, dss_buffer) + dss_ghost!( + device, + dss_buffer.perimeter_data, + Spaces.perimeter(hspace), + topology(hspace), + ) + dss_untransform!( + device, + dss_buffer, + data, + local_geometry_data(space), + Spaces.perimeter(hspace), + dss_buffer.perimeter_elems, + ) + return data +end + +weighted_dss_ghost!(data, space, hspace, dss_buffer) = data diff --git a/src/Topologies/Topologies.jl b/src/Topologies/Topologies.jl index 0bc713a06e..855d622f70 100644 --- a/src/Topologies/Topologies.jl +++ b/src/Topologies/Topologies.jl @@ -2,7 +2,7 @@ module Topologies using DocStringExtensions -import ClimaComms, Adapt +import ClimaComms, Adapt, CUDA import ClimaComms import ..Geometry diff --git a/src/Topologies/dss.jl b/src/Topologies/dss.jl index 4670bc7820..4f130b4bef 100644 --- a/src/Topologies/dss.jl +++ b/src/Topologies/dss.jl @@ -34,40 +34,25 @@ end """ create_dss_buffer( data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, - hspace::AbstractSpectralElementSpace, + topology::Topology2D, + local_geometry = nothing, + local_weights = nothing, ) where {S, Nij} Creates a [`DSSBuffer`](@ref) for the field data corresponding to `data` """ function create_dss_buffer( data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, - hspace::AbstractSpectralElementSpace, -) where {S, Nij} - @assert quadrature_style(hspace) isa Spaces.Quadratures.GLL "DSS2 is only compatible with GLL quadrature" - local_geometry = local_geometry_data(hspace) - local_weights = local_dss_weights(hspace) - perimeter = Spaces.perimeter(hspace) - create_dss_buffer( - data, - topology(hspace), - perimeter, - local_geometry, - local_weights, - ) -end - -function create_dss_buffer( - data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, - topology, - perimeter, + topology::Topology2D, local_geometry = nothing, local_weights = nothing, ) where {S, Nij} - context = topology.context + perimeter = Perimeter2D(Nij) + context = ClimaComms.context(topology) DA = ClimaComms.array_type(topology) convert_to_array = DA isa Array ? false : true (_, _, _, Nv, nelems) = Base.size(data) - Np = Spaces.nperimeter(perimeter) + Np =length(perimeter) Nf = length(parent(data)) == 0 ? 0 : cld(length(parent(data)), (Nij * Nij * Nv * nelems)) @@ -87,7 +72,7 @@ function create_dss_buffer( send_buf_idx, recv_buf_idx = Int[], Int[] send_data, recv_data = DA{T}(undef, 0), DA{T}(undef, 0) send_buf_idx, recv_buf_idx = DA{Int}(undef, 0), DA{Int}(undef, 0) - internal_elems = DA{Int}(1:Topologies.nelems(topology)) + internal_elems = DA{Int}(1:nelems(topology)) perimeter_elems = DA{Int}(undef, 0) else (; comm_vertex_lengths, comm_face_lengths) = topology @@ -109,7 +94,7 @@ function create_dss_buffer( persistent = true, ) send_buf_idx, recv_buf_idx = - Topologies.compute_ghost_send_recv_idx(topology, Nij) + compute_ghost_send_recv_idx(topology, Nij) internal_elems = DA(topology.internal_elems) perimeter_elems = DA(topology.perimeter_elems) end @@ -207,277 +192,12 @@ end Base.eltype(::DSSBuffer{S}) where {S} = S -create_dss_buffer(data::DataLayouts.AbstractData, hspace) = nothing - assert_same_eltype(::DataLayouts.AbstractData, ::DSSBuffer) = error("Incorrect buffer eltype") assert_same_eltype(::DataLayouts.AbstractData{S}, ::DSSBuffer{S}) where {S} = nothing assert_same_eltype(::DataLayouts.AbstractData, ::Nothing) = nothing -""" - function weighted_dss!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{ - AbstractSpectralElementSpace, - ExtrudedFiniteDifferenceSpace, - }, - dss_buffer::Union{DSSBuffer, Nothing}, - ) - -Computes weighted dss of `data`. - -It comprises of the following steps: - -1). [`Spaces.weighted_dss_start!`](@ref) - -2). [`Spaces.weighted_dss_internal!`](@ref) - -3). [`Spaces.weighted_dss_ghost!`](@ref) -""" -function weighted_dss!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, - dss_buffer::Union{DSSBuffer, Nothing}, -) - assert_same_eltype(data, dss_buffer) - weighted_dss_start!(data, space, dss_buffer) - weighted_dss_internal!(data, space, dss_buffer) - weighted_dss_ghost!(data, space, dss_buffer) -end - -""" - weighted_dss_start!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{ - AbstractSpectralElementSpace, - ExtrudedFiniteDifferenceSpace, - }, - dss_buffer::Union{DSSBuffer, Nothing}, - ) - -It comprises of the following steps: - -1). Apply [`Spaces.dss_transform!`](@ref) on perimeter elements. This weights and tranforms vector -fields to physical basis if needed. Scalar fields are weighted. The transformed and/or weighted -perimeter `data` is stored in `perimeter_data`. - -2). Apply [`Spaces.dss_local_ghost!`](@ref) -This computes partial weighted DSS on ghost vertices, using only the information from `local` vertices. - -3). [`Spaces.fill_send_buffer!`](@ref) -Loads the send buffer from `perimeter_data`. For unique ghost vertices, only data from the -representative ghost vertices which store result of "ghost local" DSS are loaded. - -4). Start DSS communication with neighboring processes -""" -weighted_dss_start!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, - dss_buffer::Union{DSSBuffer, Nothing}, -) = weighted_dss_start!(data, space, horizontal_space(space), dss_buffer) - -function weighted_dss_start!( - data::Union{DataLayouts.IJFH, DataLayouts.VIJFH}, - space::Union{ - Spaces.SpectralElementSpace2D, - Spaces.ExtrudedFiniteDifferenceSpace, - }, - hspace::SpectralElementSpace2D{<:Topology2D}, - dss_buffer::DSSBuffer, -) - assert_same_eltype(data, dss_buffer) - length(parent(data)) == 0 && return nothing - device = ClimaComms.device(topology(hspace)) - dss_transform!( - device, - dss_buffer, - data, - local_geometry_data(space), - local_dss_weights(hspace), - Spaces.perimeter(hspace), - dss_buffer.perimeter_elems, - ) - dss_local_ghost!( - device, - dss_buffer.perimeter_data, - Spaces.perimeter(hspace), - topology(hspace), - ) - fill_send_buffer!(device, dss_buffer) - ClimaComms.start(dss_buffer.graph_context) - return nothing -end - -weighted_dss_start!(data, space, hspace, dss_buffer) = nothing -""" - weighted_dss_internal!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{ - AbstractSpectralElementSpace, - ExtrudedFiniteDifferenceSpace, - }, - dss_buffer::DSSBuffer, - ) - -1). Apply [`Spaces.dss_transform!`](@ref) on interior elements. Local elements are split into interior -and perimeter elements to facilitate overlapping of communication with computation. - -2). Probe communication - -3). [`Spaces.dss_local!`](@ref) computes the weighted DSS on local vertices and faces. -""" -weighted_dss_internal!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, - dss_buffer::Union{DSSBuffer, Nothing}, -) = weighted_dss_internal!(data, space, horizontal_space(space), dss_buffer) - -function weighted_dss_internal!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, - hspace::AbstractSpectralElementSpace, - dss_buffer::Union{DSSBuffer, Nothing}, -) - assert_same_eltype(data, dss_buffer) - length(parent(data)) == 0 && return nothing - if hspace isa SpectralElementSpace1D - dss_1d!( - topology(hspace), - data, - local_geometry_data(space), - local_dss_weights(space), - ) - else - device = ClimaComms.device(topology(hspace)) - dss_transform!( - device, - dss_buffer, - data, - local_geometry_data(space), - local_dss_weights(space), - Spaces.perimeter(hspace), - dss_buffer.internal_elems, - ) - dss_local!( - device, - dss_buffer.perimeter_data, - Spaces.perimeter(hspace), - topology(hspace), - ) - dss_untransform!( - device, - dss_buffer, - data, - local_geometry_data(space), - Spaces.perimeter(hspace), - dss_buffer.internal_elems, - ) - end - return nothing -end -""" - weighted_dss_ghost!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{ - AbstractSpectralElementSpace, - ExtrudedFiniteDifferenceSpace, - }, - dss_buffer::Union{DSSBuffer, Nothing}, - ) - -1). Finish communications. - -2). Call [`Spaces.load_from_recv_buffer!`](@ref) -After the communication is complete, this adds data from the recv buffer to the corresponding location in -`perimeter_data`. For ghost vertices, this data is added only to the representative vertices. The values are -then scattered to other local vertices corresponding to each unique ghost vertex in `dss_local_ghost`. - -3). Call [`Spaces.dss_untransform!`](@ref) on all local elements. -This transforms the DSS'd local vectors back to Covariant12 vectors, and copies the DSS'd data from the -`perimeter_data` to `data`. -""" -weighted_dss_ghost!( - data::Union{ - DataLayouts.IFH, - DataLayouts.VIFH, - DataLayouts.IJFH, - DataLayouts.VIJFH, - }, - space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, - dss_buffer::Union{DSSBuffer, Nothing}, -) = weighted_dss_ghost!(data, space, horizontal_space(space), dss_buffer) - -function weighted_dss_ghost!( - data::Union{DataLayouts.IJFH, DataLayouts.VIJFH}, - space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, - hspace::SpectralElementSpace2D{<:Topology2D}, - dss_buffer::DSSBuffer, -) - assert_same_eltype(data, dss_buffer) - length(parent(data)) == 0 && return data - device = ClimaComms.device(topology(hspace)) - ClimaComms.finish(dss_buffer.graph_context) - load_from_recv_buffer!(device, dss_buffer) - dss_ghost!( - device, - dss_buffer.perimeter_data, - Spaces.perimeter(hspace), - topology(hspace), - ) - dss_untransform!( - device, - dss_buffer, - data, - local_geometry_data(space), - Spaces.perimeter(hspace), - dss_buffer.perimeter_elems, - ) - return data -end - -weighted_dss_ghost!(data, space, hspace, dss_buffer) = data - """ function dss_transform!( device::ClimaComms.AbstractDevice, @@ -823,7 +543,7 @@ end ::ClimaComms.AbstractCPUDevice, perimeter_data::DataLayouts.VIFH, perimeter::AbstractPerimeter, - topology::Topologies.AbstractTopology, + topology::AbstractTopology, ) Performs DSS on local vertices and faces. @@ -834,7 +554,7 @@ function dss_local!( ::ClimaComms.AbstractCPUDevice, perimeter_data::DataLayouts.VIFH, perimeter::Perimeter2D, - topology::Topologies.Topology2D, + topology::Topology2D, ) dss_local_vertices!(perimeter_data, perimeter, topology) dss_local_faces!(perimeter_data, perimeter, topology) @@ -845,7 +565,7 @@ end dss_local_vertices!( perimeter_data::DataLayouts.VIFH, perimeter::Perimeter2D, - topology::Topologies.Topology2D, + topology::Topology2D, ) Apply dss to local vertices. @@ -853,10 +573,10 @@ Apply dss to local vertices. function dss_local_vertices!( perimeter_data::DataLayouts.VIFH, perimeter::Perimeter2D, - topology::Topologies.Topology2D, + topology::Topology2D, ) Nv = size(perimeter_data, 4) - @inbounds for vertex in Topologies.local_vertices(topology) + @inbounds for vertex in local_vertices(topology) # for each level for level in 1:Nv # gather: compute sum over shared vertices @@ -865,14 +585,14 @@ function dss_local_vertices!( vertex; init = RecursiveApply.rzero(eltype(slab(perimeter_data, 1, 1))), ) do (lidx, vert) - ip = Topologies.perimeter_vertex_node_index(vert) + ip = perimeter_vertex_node_index(vert) perimeter_slab = slab(perimeter_data, level, lidx) perimeter_slab[ip] end # scatter: assign sum to shared vertices for (lidx, vert) in vertex perimeter_slab = slab(perimeter_data, level, lidx) - ip = Topologies.perimeter_vertex_node_index(vert) + ip = perimeter_vertex_node_index(vert) perimeter_slab[ip] = sum_data end end @@ -883,15 +603,15 @@ end function dss_local_faces!( perimeter_data::DataLayouts.VIFH, perimeter::Perimeter2D, - topology::Topologies.Topology2D, + topology::Topology2D, ) (Np, _, _, Nv, _) = size(perimeter_data) nfacedof = div(Np - 4, 4) @inbounds for (lidx1, face1, lidx2, face2, reversed) in - Topologies.interior_faces(topology) - pr1 = Topologies.perimeter_face_indices(face1, nfacedof, false) - pr2 = Topologies.perimeter_face_indices(face2, nfacedof, reversed) + interior_faces(topology) + pr1 = perimeter_face_indices(face1, nfacedof, false) + pr2 = perimeter_face_indices(face2, nfacedof, reversed) for level in 1:Nv perimeter_slab1 = slab(perimeter_data, level, lidx1) perimeter_slab2 = slab(perimeter_data, level, lidx2) @@ -909,7 +629,7 @@ end ::ClimaComms.AbstractCPUDevice, perimeter_data::DataLayouts.VIFH, perimeter::AbstractPerimeter, - topology::Topologies.AbstractTopology, + topology::AbstractTopology, ) Computes the "local" part of ghost vertex dss. (i.e. it computes the summation of all the shared local @@ -922,12 +642,12 @@ function dss_local_ghost!( ::ClimaComms.AbstractCPUDevice, perimeter_data::DataLayouts.VIFH, perimeter::AbstractPerimeter, - topology::Topologies.AbstractTopology, + topology::AbstractTopology, ) nghostvertices = length(topology.ghost_vertex_offset) - 1 if nghostvertices > 0 (Np, _, _, Nv, _) = size(perimeter_data) - @inbounds for vertex in Topologies.ghost_vertices(topology) + @inbounds for vertex in ghost_vertices(topology) for level in 1:Nv # gather: compute sum over shared vertices sum_data = mapreduce( @@ -937,7 +657,7 @@ function dss_local_ghost!( eltype(slab(perimeter_data, 1, 1)), ), ) do (isghost, idx, vert) - ip = Topologies.perimeter_vertex_node_index(vert) + ip = perimeter_vertex_node_index(vert) if !isghost lidx = idx perimeter_slab = slab(perimeter_data, level, lidx) @@ -948,7 +668,7 @@ function dss_local_ghost!( end for (isghost, idx, vert) in vertex if !isghost - ip = Topologies.perimeter_vertex_node_index(vert) + ip = perimeter_vertex_node_index(vert) lidx = idx perimeter_slab = slab(perimeter_data, level, lidx) perimeter_slab[ip] = sum_data @@ -964,7 +684,7 @@ end device::ClimaComms.AbstractCPUDevice, perimeter_data::DataLayouts.VIFH, perimeter::AbstractPerimeter, - topology::Topologies.AbstractTopology, + topology::AbstractTopology, ) Sets the value for all local vertices of each unique ghost vertex, in `perimeter_data`, to that of @@ -976,16 +696,16 @@ function dss_ghost!( device::ClimaComms.AbstractCPUDevice, perimeter_data::DataLayouts.VIFH, perimeter::AbstractPerimeter, - topology::Topologies.AbstractTopology, + topology::AbstractTopology, ) nghostvertices = length(topology.ghost_vertex_offset) - 1 if nghostvertices > 0 nlevels = size(perimeter_data, 4) - perimeter_vertex_node_index = Topologies.perimeter_vertex_node_index - perimeter_face_indices = Topologies.perimeter_face_indices + perimeter_vertex_node_index = perimeter_vertex_node_index + perimeter_face_indices = perimeter_face_indices (; repr_ghost_vertex) = topology @inbounds for (i, vertex) in - enumerate(Topologies.ghost_vertices(topology)) + enumerate(ghost_vertices(topology)) idxresult, lvertresult = repr_ghost_vertex[i] ipresult = perimeter_vertex_node_index(lvertresult) for level in 1:nlevels @@ -1098,7 +818,7 @@ end dss2!(data, topology, quadrature_style) = dss!(data, topology, quadrature_style) function dss_1d!( - htopology::Topologies.AbstractTopology, + htopology::AbstractTopology, data, local_geometry_data = nothing, dss_weights = nothing, @@ -1108,7 +828,7 @@ function dss_1d!( idx1 = CartesianIndex(1, 1, 1, 1, 1) idx2 = CartesianIndex(Nq, 1, 1, 1, 1) @inbounds for (elem1, face1, elem2, face2, reversed) in - Topologies.interior_faces(htopology) + interior_faces(htopology) for level in 1:Nv @assert face1 == 1 && face2 == 2 && !reversed local_geometry_slab1 = slab(local_geometry_data, level, elem1) diff --git a/src/Topologies/dss_cuda.jl b/src/Topologies/dss_cuda.jl index b649a56bc9..1b1a66b527 100644 --- a/src/Topologies/dss_cuda.jl +++ b/src/Topologies/dss_cuda.jl @@ -21,7 +21,7 @@ function dss_load_perimeter_data!( (nlevels, nperimeter, nfid, nelems) = size(pperimeter_data) nitems = nlevels * nperimeter * nfid * nelems nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_load_perimeter_data_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_load_perimeter_data_kernel!( pperimeter_data, pdata, perimeter, @@ -58,7 +58,7 @@ function dss_unload_perimeter_data!( (nlevels, nperimeter, nfid, nelems) = size(pperimeter_data) nitems = nlevels * nperimeter * nfid * nelems nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_unload_perimeter_data_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_unload_perimeter_data_kernel!( pdata, pperimeter_data, perimeter, @@ -98,7 +98,7 @@ function dss_local!( nitems = nlevels * nfid * (nlocalfaces + nlocalvertices) nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_local_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_local_kernel!( pperimeter_data, topology.local_vertices, topology.local_vertex_offset, @@ -184,7 +184,7 @@ function dss_transform!( (nlevels, nperimeter, _, _) = size(pperimeter_data) nitems = nlevels * nperimeter * nlocalelems nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_transform_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_transform_kernel!( pperimeter_data, pdata, p∂ξ∂x, @@ -290,7 +290,7 @@ function dss_untransform!( (nlevels, nperimeter, _, _) = size(pperimeter_data) nitems = nlevels * nperimeter * nlocalelems nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_untransform_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_untransform_kernel!( pperimeter_data, pdata, p∂ξ∂x, @@ -376,7 +376,7 @@ function dss_local_ghost!( max_threads = 256 nitems = nlevels * nfid * nghostvertices nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_local_ghost_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_local_ghost_kernel!( pperimeter_data, topology.ghost_vertices, topology.ghost_vertex_offset, @@ -428,7 +428,7 @@ function fill_send_buffer!(::ClimaComms.CUDADevice, dss_buffer::DSSBuffer) nitems = nsend * nlevels * nfid nthreads, nblocks = _configure_threadblock(nitems) CUDA.synchronize() # CUDA MPI uses a separate stream. This will synchronize across streams - @cuda threads = (nthreads) blocks = (nblocks) fill_send_buffer_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) fill_send_buffer_kernel!( send_data, send_buf_idx, pperimeter_data, @@ -469,7 +469,7 @@ function load_from_recv_buffer!(::ClimaComms.CUDADevice, dss_buffer::DSSBuffer) nitems = nrecv * nlevels * nfid nthreads, nblocks = _configure_threadblock(nitems) CUDA.synchronize() - @cuda threads = (nthreads) blocks = (nblocks) load_from_recv_buffer_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) load_from_recv_buffer_kernel!( pperimeter_data, recv_data, recv_buf_idx, @@ -514,7 +514,7 @@ function dss_ghost!( nlevels, _, nfidx, _ = size(pperimeter_data) nitems = nlevels * nfidx * nghostvertices nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_ghost_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_ghost_kernel!( pperimeter_data, topology.ghost_vertices, topology.ghost_vertex_offset, From b87e5898f3cc4fe9b1970c6263d1ca2063f0077b Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 20 Oct 2023 13:35:55 -0700 Subject: [PATCH 15/42] more fixes --- docs/src/api.md | 1 - examples/hybrid/hybrid3dcs_dss.jl | 6 ++--- src/Fields/Fields.jl | 23 +---------------- src/Hypsography/Hypsography.jl | 13 ++++------ src/InputOutput/InputOutput.jl | 2 +- src/InputOutput/readers.jl | 6 ++--- src/InputOutput/writers.jl | 12 ++++----- src/Limiters/quasimonotone.jl | 2 +- src/Operators/finitedifference.jl | 11 ++++++--- src/Spaces/Spaces.jl | 20 +++++++++------ src/Spaces/dss.jl | 9 +++++-- src/Spaces/extruded.jl | 9 +++---- src/Spaces/finitedifference.jl | 22 +++++++++-------- src/Spaces/spectralelement.jl | 5 ++-- src/Topologies/Topologies.jl | 3 ++- src/Topologies/dss.jl | 41 ++++++++++++++++++------------- src/Topologies/dss_transform.jl | 38 ++++++++++++++-------------- 17 files changed, 109 insertions(+), 114 deletions(-) diff --git a/docs/src/api.md b/docs/src/api.md index 9919baf1ed..b375df9730 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -219,7 +219,6 @@ Spaces.dss_ghost! Spaces.create_dss_buffer Spaces.fill_send_buffer! Spaces.DSSBuffer -Spaces.create_ghost_buffer Spaces.load_from_recv_buffer! Spaces.weighted_dss_start! Spaces.weighted_dss_internal! diff --git a/examples/hybrid/hybrid3dcs_dss.jl b/examples/hybrid/hybrid3dcs_dss.jl index 5b9e68a0e1..7cf1605444 100644 --- a/examples/hybrid/hybrid3dcs_dss.jl +++ b/examples/hybrid/hybrid3dcs_dss.jl @@ -85,10 +85,8 @@ function hybrid3dcubedsphere_dss_profiler( c = center_initial_condition(ᶜlocal_geometry), f = face_initial_condition(ᶠlocal_geometry), ) - ghost_buffer = ( - c = Spaces.create_ghost_buffer(Y.c), - f = Spaces.create_ghost_buffer(Y.f), - ) + ghost_buffer = + (c = Spaces.create_dss_buffer(Y.c), f = Spaces.create_dss_buffer(Y.f)) dss_buffer_f = Spaces.create_dss_buffer(Y.f) dss_buffer_c = Spaces.create_dss_buffer(Y.c) nsamples = 10000 diff --git a/src/Fields/Fields.jl b/src/Fields/Fields.jl index 51f6c01f3d..311aa28b6e 100644 --- a/src/Fields/Fields.jl +++ b/src/Fields/Fields.jl @@ -333,7 +333,7 @@ end Apply weighted direct stiffness summation (DSS) to `f`. This operates in-place (i.e. it modifies the `f`). `ghost_buffer` contains the necessary information -for communication in a distributed setting, see [`Spaces.create_ghost_buffer`](@ref). +for communication in a distributed setting, see [`Spaces.create_dss_buffer`](@ref). This is a projection operation from the piecewise polynomial space ``\\mathcal{V}_0`` to the continuous space ``\\mathcal{V}_1 = \\mathcal{V}_0 @@ -374,13 +374,6 @@ Spaces.weighted_dss_internal!(field::Field, dss_buffer) = Spaces.weighted_dss_ghost!(field::Field, dss_buffer) = Spaces.weighted_dss_ghost!(field_values(field), axes(field), dss_buffer) -""" - Spaces.create_ghost_buffer(field::Field) - -Create a buffer for communicating neighbour information of `field`. -""" -Spaces.create_ghost_buffer(field::Field) = Spaces.create_dss_buffer(field) - """ Spaces.create_dss_buffer(field::Field) @@ -391,20 +384,6 @@ function Spaces.create_dss_buffer(field::Field) hspace = Spaces.horizontal_space(space) Spaces.create_dss_buffer(field_values(field), hspace) end -# Add definitions for backward compatibility -Spaces.weighted_dss2!( - field::Field, - dss_buffer = Spaces.create_dss_buffer(field), -) = Spaces.weighted_dss!(field, dss_buffer) - -Spaces.weighted_dss_start2!(field::Field, ghost_buffer) = - Spaces.weighted_dss_start!(field, ghost_buffer) - -Spaces.weighted_dss_internal2!(field::Field, ghost_buffer) = - Spaces.weighted_dss_internal!(field, ghost_buffer) - -Spaces.weighted_dss_ghost2!(field, ghost_buffer) = - Spaces.weighted_dss_ghost!(field, ghost_buffer) Base.@propagate_inbounds function level( field::Union{ diff --git a/src/Hypsography/Hypsography.jl b/src/Hypsography/Hypsography.jl index abe5385eef..e759948a09 100644 --- a/src/Hypsography/Hypsography.jl +++ b/src/Hypsography/Hypsography.jl @@ -1,14 +1,11 @@ module Hypsography import ..slab, ..column -import ..Geometry, ..Domains, ..Topologies, ..Grids, ..Spaces, ..Fields, ..Operators -import ..Spaces: - ExtrudedFiniteDifferenceSpace - -import ..Grids: - ExtrudedFiniteDifferenceGrid, - HypsographyAdaption, - Flat +import ..Geometry, + ..Domains, ..Topologies, ..Grids, ..Spaces, ..Fields, ..Operators +import ..Spaces: ExtrudedFiniteDifferenceSpace + +import ..Grids: ExtrudedFiniteDifferenceGrid, HypsographyAdaption, Flat using StaticArrays, LinearAlgebra, Memoize, WeakValueDicts diff --git a/src/InputOutput/InputOutput.jl b/src/InputOutput/InputOutput.jl index 995b7ff341..57939d633b 100644 --- a/src/InputOutput/InputOutput.jl +++ b/src/InputOutput/InputOutput.jl @@ -11,7 +11,7 @@ import ..Geometry, ..Spaces, ..Fields, ..Hypsography -import .. VERSION +import ..VERSION include("writers.jl") include("readers.jl") diff --git a/src/InputOutput/readers.jl b/src/InputOutput/readers.jl index 2050e2f5d8..6d9b329b04 100644 --- a/src/InputOutput/readers.jl +++ b/src/InputOutput/readers.jl @@ -332,10 +332,8 @@ function read_grid_new(reader, name) topology = read_topology(reader, attrs(group)["topology"]) return Spaces.FiniteDifferenceGrid(topology) elseif type == "ExtrudedFiniteDifferenceGrid" - vertical_grid = - read_grid(reader, attrs(group)["vertical_grid"]) - horizontal_grid = - read_grid(reader, attrs(group)["horizontal_grid"]) + vertical_grid = read_grid(reader, attrs(group)["vertical_grid"]) + horizontal_grid = read_grid(reader, attrs(group)["horizontal_grid"]) hypsography_type = get(attrs(group), "hypsography_type", "Flat") if hypsography_type == "Flat" hypsography = Spaces.Flat() diff --git a/src/InputOutput/writers.jl b/src/InputOutput/writers.jl index 6e7a2a3b39..af2df79afb 100644 --- a/src/InputOutput/writers.jl +++ b/src/InputOutput/writers.jl @@ -374,11 +374,7 @@ function write_new!( "horizontal_grid", write!(writer, space.horizontal_grid), ) - write_attribute( - group, - "vertical_grid", - write!(writer, space.vertical_grid), - ) + write_attribute(group, "vertical_grid", write!(writer, space.vertical_grid)) if space.hypsography isa Hypsography.LinearAdaption write_attribute(group, "hypsography_type", "LinearAdaption") write_attribute( @@ -432,7 +428,11 @@ function write!(writer::HDF5Writer, field::Fields.Field, name::AbstractString) write_attribute(dataset, "value_type", string(eltype(field))) write_attribute(dataset, "grid", grid_name) if !isnothing(staggering) - write_attribute(dataset, "staggering", string(nameof(typeof(staggering)))) + write_attribute( + dataset, + "staggering", + string(nameof(typeof(staggering))), + ) end return name diff --git a/src/Limiters/quasimonotone.jl b/src/Limiters/quasimonotone.jl index 129702c3f6..17e9935019 100644 --- a/src/Limiters/quasimonotone.jl +++ b/src/Limiters/quasimonotone.jl @@ -51,7 +51,7 @@ end function QuasiMonotoneLimiter(ρq::Fields.Field; rtol = eps(eltype(parent(ρq)))) q_bounds = make_q_bounds(Fields.field_values(ρq)) ghost_buffer = - Spaces.create_ghost_buffer(q_bounds, Spaces.topology(axes(ρq))) + Topologies.create_ghost_buffer(q_bounds, Spaces.topology(axes(ρq))) return QuasiMonotoneLimiter(q_bounds, similar(q_bounds), ghost_buffer, rtol) end diff --git a/src/Operators/finitedifference.jl b/src/Operators/finitedifference.jl index 8aa987e5b4..a1f11c3272 100644 --- a/src/Operators/finitedifference.jl +++ b/src/Operators/finitedifference.jl @@ -21,11 +21,16 @@ left_idx(space::AllFaceFiniteDifferenceSpace) = left_face_boundary_idx(space) right_idx(space::AllFaceFiniteDifferenceSpace) = right_face_boundary_idx(space) left_center_boundary_idx(space::AllFiniteDifferenceSpace) = 1 -right_center_boundary_idx(space::AllFiniteDifferenceSpace) = - size(Spaces.local_geometry_data(Spaces.space(space, Spaces.CallCenter())), 4) +right_center_boundary_idx(space::AllFiniteDifferenceSpace) = size( + Spaces.local_geometry_data(Spaces.space(space, Spaces.CallCenter())), + 4, +) left_face_boundary_idx(space::AllFiniteDifferenceSpace) = half right_face_boundary_idx(space::AllFiniteDifferenceSpace) = - size(Spaces.local_geometry_data(Spaces.space(space, Spaces.CallFace())), 4) - half + size( + Spaces.local_geometry_data(Spaces.space(space, Spaces.CallFace())), + 4, + ) - half left_face_boundary_idx(arg) = left_face_boundary_idx(axes(arg)) diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index 0b2bcf9eac..caf287097b 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -20,11 +20,16 @@ using CUDA import ..slab, ..column, ..level import ..Utilities: PlusHalf, half -import ..DataLayouts, ..Geometry, ..Domains, ..Meshes, ..Topologies, ..Grids, ..Quadratures +import ..DataLayouts, + ..Geometry, ..Domains, ..Meshes, ..Topologies, ..Grids, ..Quadratures import ..Grids: - Staggering, CellFace, CellCenter, - topology, local_geometry_data + Staggering, + CellFace, + CellCenter, + topology, + local_geometry_data, + quadrature_style import ClimaComms using StaticArrays, ForwardDiff, LinearAlgebra, UnPack, Adapt @@ -46,10 +51,8 @@ function grid end function staggering end -ClimaComms.context(space::AbstractSpace) = - ClimaComms.context(grid(space)) -ClimaComms.device(space::AbstractSpace) = - ClimaComms.device(grid(space)) +ClimaComms.context(space::AbstractSpace) = ClimaComms.context(grid(space)) +ClimaComms.device(space::AbstractSpace) = ClimaComms.device(grid(space)) topology(space::AbstractSpace) = topology(grid(space)) vertical_topology(space::AbstractSpace) = vertical_topology(grid(space)) @@ -71,7 +74,8 @@ undertype(space::AbstractSpace) = Geometry.undertype(eltype(local_geometry_data(space))) coordinates_data(space::AbstractSpace) = local_geometry_data(space).coordinates -coordinates_data(grid::Grids.AbstractGrid) = local_geometry_data(grid).coordinates +coordinates_data(grid::Grids.AbstractGrid) = + local_geometry_data(grid).coordinates coordinates_data(staggering, grid::Grids.AbstractGrid) = local_geometry_data(staggering, grid).coordinates diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl index e0c6c7a324..c04709238e 100644 --- a/src/Spaces/dss.jl +++ b/src/Spaces/dss.jl @@ -1,4 +1,4 @@ -import ..Topologies: +import ..Topologies: DSSBuffer, create_dss_buffer, assert_same_eltype, @@ -23,7 +23,12 @@ function create_dss_buffer( data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, hspace::AbstractSpectralElementSpace, ) where {S, Nij} - create_dss_buffer(data, topology(hspace), local_geometry_data(hspace), local_dss_weights(hspace)) + create_dss_buffer( + data, + topology(hspace), + local_geometry_data(hspace), + local_dss_weights(hspace), + ) end diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index df4511396b..7cdbe49071 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -12,9 +12,9 @@ space(staggering::Staggering, grid::Grids.ExtrudedFiniteDifferenceGrid) = ExtrudedFiniteDifferenceSpace(staggering, grid) const FaceExtrudedFiniteDifferenceSpace{G} = - ExtrudedFiniteDifferenceSpace{G,CellFace} + ExtrudedFiniteDifferenceSpace{G, CellFace} const CenterExtrudedFiniteDifferenceSpace{G} = - ExtrudedFiniteDifferenceSpace{G,CellCenter} + ExtrudedFiniteDifferenceSpace{G, CellCenter} #= ExtrudedFiniteDifferenceSpace{S}( @@ -74,10 +74,7 @@ end =# Adapt.adapt_structure(to, space::ExtrudedFiniteDifferenceSpace) = - ExtrudedFiniteDifferenceSpace( - Adapt.adapt(to, space.grid), - space.staggering, - ) + ExtrudedFiniteDifferenceSpace(Adapt.adapt(to, space.grid), space.staggering) #= diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 7989184e45..83e967c07a 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -12,8 +12,8 @@ struct FiniteDifferenceSpace{ staggering::S end -const FaceFiniteDifferenceSpace{G} = FiniteDifferenceSpace{G,CellFace} -const CenterFiniteDifferenceSpace{G} = FiniteDifferenceSpace{G,CellCenter} +const FaceFiniteDifferenceSpace{G} = FiniteDifferenceSpace{G, CellFace} +const CenterFiniteDifferenceSpace{G} = FiniteDifferenceSpace{G, CellCenter} grid(space::AbstractFiniteDifferenceSpace) = space.grid staggering(space::FiniteDifferenceSpace) = space.staggering @@ -51,17 +51,19 @@ FaceFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = CenterFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(topology), CellCenter()) -FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = - FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(Grids.FiniteDifferenceGrid(mesh)), CellFace()) -CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = - FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(Grids.FiniteDifferenceGrid(mesh)), CellCenter()) +FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = FiniteDifferenceSpace( + Grids.FiniteDifferenceGrid(Grids.FiniteDifferenceGrid(mesh)), + CellFace(), +) +CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = FiniteDifferenceSpace( + Grids.FiniteDifferenceGrid(Grids.FiniteDifferenceGrid(mesh)), + CellCenter(), +) -Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = FiniteDifferenceSpace( - Adapt.adapt(to, space.grid), - space.staggering, -) +Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = + FiniteDifferenceSpace(Adapt.adapt(to, space.grid), space.staggering) diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 8003ce0a4b..fb03fe16f9 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -12,7 +12,7 @@ local_dss_weights(space::AbstractSpectralElementSpace) = horizontal_space(space::AbstractSpectralElementSpace) = space nlevels(space::AbstractSpectralElementSpace) = 1 - + eachslabindex(space::AbstractSpectralElementSpace) = 1:Topologies.nlocalelems(Spaces.topology(space)) @@ -39,6 +39,7 @@ struct SpectralElementSpace1D{G} <: AbstractSpectralElementSpace end space(grid::Grids.SpectralElementGrid1D, ::Nothing) = SpectralElementSpace1D(grid) +grid(space::Spaces.SpectralElementSpace1D) = space.grid function SpectralElementSpace1D( topology::Topologies.IntervalTopology, @@ -56,7 +57,7 @@ struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace end space(grid::Grids.SpectralElementGrid2D, ::Nothing) = SpectralElementSpace2D(grid) - +grid(space::Spaces.SpectralElementSpace2D) = space.grid function SpectralElementSpace2D( topology::Topologies.Topology2D, diff --git a/src/Topologies/Topologies.jl b/src/Topologies/Topologies.jl index 855d622f70..61bc33685d 100644 --- a/src/Topologies/Topologies.jl +++ b/src/Topologies/Topologies.jl @@ -4,11 +4,12 @@ using DocStringExtensions import ClimaComms, Adapt, CUDA -import ClimaComms import ..Geometry import ..Domains: Domains, coordinate_type import ..Meshes: Meshes, domain, coordinates import ..DataLayouts +import ..slab, ..column, ..level + using Memoize, WeakValueDicts diff --git a/src/Topologies/dss.jl b/src/Topologies/dss.jl index 4f130b4bef..ba402999a1 100644 --- a/src/Topologies/dss.jl +++ b/src/Topologies/dss.jl @@ -44,18 +44,26 @@ Creates a [`DSSBuffer`](@ref) for the field data corresponding to `data` function create_dss_buffer( data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, topology::Topology2D, - local_geometry = nothing, - local_weights = nothing, + local_geometry::Union{ + DataLayouts.IJFH{S, Nij}, + DataLayouts.VIJFH{S, Nij}, + Nothing, + } = nothing, + local_weights::Union{ + DataLayouts.IJFH{S, Nij}, + DataLayouts.VIJFH{S, Nij}, + Nothing, + } = nothing, ) where {S, Nij} - perimeter = Perimeter2D(Nij) + perimeter::Perimeter2D = Perimeter2D(Nij) context = ClimaComms.context(topology) DA = ClimaComms.array_type(topology) convert_to_array = DA isa Array ? false : true - (_, _, _, Nv, nelems) = Base.size(data) - Np =length(perimeter) + (_, _, _, Nv, Nh) = Base.size(data) + Np = length(perimeter) Nf = length(parent(data)) == 0 ? 0 : - cld(length(parent(data)), (Nij * Nij * Nv * nelems)) + cld(length(parent(data)), (Nij * Nij * Nv * Nh)) nfacedof = Nij - 2 T = eltype(parent(data)) TS = _transformed_type(data, local_geometry, local_weights, DA) # extract transformed type @@ -65,7 +73,7 @@ function create_dss_buffer( if eltype(data) <: Geometry.Covariant123Vector TS = Geometry.UVWVector{T} end - perimeter_data = DataLayouts.VIFH{TS, Np}(DA{T}(undef, Nv, Np, Nf, nelems)) + perimeter_data = DataLayouts.VIFH{TS, Np}(DA{T}(undef, Nv, Np, Nf, Nh)) if context isa ClimaComms.SingletonCommsContext graph_context = ClimaComms.SingletonGraphContext(context) send_data, recv_data = T[], T[] @@ -93,8 +101,7 @@ function create_dss_buffer( neighbor_pids, persistent = true, ) - send_buf_idx, recv_buf_idx = - compute_ghost_send_recv_idx(topology, Nij) + send_buf_idx, recv_buf_idx = compute_ghost_send_recv_idx(topology, Nij) internal_elems = DA(topology.internal_elems) perimeter_elems = DA(topology.perimeter_elems) end @@ -704,8 +711,7 @@ function dss_ghost!( perimeter_vertex_node_index = perimeter_vertex_node_index perimeter_face_indices = perimeter_face_indices (; repr_ghost_vertex) = topology - @inbounds for (i, vertex) in - enumerate(ghost_vertices(topology)) + @inbounds for (i, vertex) in enumerate(ghost_vertices(topology)) idxresult, lvertresult = repr_ghost_vertex[i] ipresult = perimeter_vertex_node_index(lvertresult) for level in 1:nlevels @@ -784,16 +790,19 @@ function load_from_recv_buffer!( end """ - dss!(data, topology, quadrature_style) + dss!(data, topology) Computed unweighted/pure DSS of `data`. """ -function dss!(data, topology, quadrature_style) +function dss!( + data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, + topology::Topology2D, +) where {S, Nij} length(parent(data)) == 0 && return nothing device = ClimaComms.device(topology) - perimeter = Perimeter2D(Quadratures.degrees_of_freedom(quadrature_style)) + perimeter = Perimeter2D(Nij) # create dss buffer - dss_buffer = create_dss_buffer(data, topology, perimeter) + dss_buffer = create_dss_buffer(data, topology) # load perimeter data from data dss_load_perimeter_data!(device, dss_buffer, data, perimeter) # compute local dss for ghost dof @@ -815,8 +824,6 @@ function dss!(data, topology, quadrature_style) return nothing end -dss2!(data, topology, quadrature_style) = dss!(data, topology, quadrature_style) - function dss_1d!( htopology::AbstractTopology, data, diff --git a/src/Topologies/dss_transform.jl b/src/Topologies/dss_transform.jl index cebd52266a..b6b0c44d92 100644 --- a/src/Topologies/dss_transform.jl +++ b/src/Topologies/dss_transform.jl @@ -235,13 +235,6 @@ function weighted_dss_start! end function weighted_dss_internal! end function weighted_dss_ghost! end -# for backward compatibility -function weighted_dss2! end -function weighted_dss_start2! end -function weighted_dss_internal2! end -function weighted_dss_ghost2! end -function dss2! end - # helper functions for DSS2 function _get_idx(sizet::NTuple{5, Int}, loc::NTuple{5, Int}) (n1, n2, n3, n4, n5) = sizet @@ -286,7 +279,10 @@ function _get_idx_metric(sizet::NTuple{5, Int}, loc::NTuple{4, Int}) return nothing end -function _representative_slab(data, ::Type{DA}) where {DA} +function _representative_slab( + data::Union{DataLayouts.AbstractData, Nothing}, + ::Type{DA}, +) where {DA} rebuild_flag = DA isa Array ? false : true if isnothing(data) return nothing @@ -297,17 +293,23 @@ function _representative_slab(data, ::Type{DA}) where {DA} end end -_transformed_type(data, local_geometry, local_weights, ::Type{DA}) where {DA} = - typeof( - dss_transform( - _representative_slab(data, DA), - _representative_slab(local_geometry, DA), - _representative_slab(local_weights, DA), - 1, - 1, - ), - ) +_transformed_type( + data::DataLayouts.AbstractData, + local_geometry::Union{DataLayouts.AbstractData, Nothing}, + local_weights::Union{DataLayouts.AbstractData, Nothing}, + ::Type{DA}, +) where {DA} = typeof( + dss_transform( + _representative_slab(data, DA), + _representative_slab(local_geometry, DA), + _representative_slab(local_weights, DA), + 1, + 1, + ), +) +# currently only used in limiters (but not actually functional) +# see https://github.com/CliMA/ClimaCore.jl/issues/1511 struct GhostBuffer{G, D} graph_context::G send_data::D From 4c7b89e4d597227915063a154bd9bae9cc9ca098 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 20 Oct 2023 14:38:04 -0700 Subject: [PATCH 16/42] more --- src/Geometry/localgeometry.jl | 86 ++++++ src/Grids/Grids.jl | 73 +++++ src/Grids/column.jl | 47 ++++ src/Grids/extruded.jl | 103 +++++++ src/Grids/finitedifference.jl | 148 ++++++++++ src/Grids/level.jl | 22 ++ src/Grids/spectralelement.jl | 498 +++++++++++++++++++++++++++++++++ src/Quadratures/Quadratures.jl | 285 +++++++++++++++++++ src/Spaces/dss.jl | 5 + src/Spaces/extruded.jl | 97 +------ src/Spaces/finitedifference.jl | 12 +- src/Spaces/spectralelement.jl | 2 + 12 files changed, 1282 insertions(+), 96 deletions(-) create mode 100644 src/Grids/Grids.jl create mode 100644 src/Grids/column.jl create mode 100644 src/Grids/extruded.jl create mode 100644 src/Grids/finitedifference.jl create mode 100644 src/Grids/level.jl create mode 100644 src/Grids/spectralelement.jl create mode 100644 src/Quadratures/Quadratures.jl diff --git a/src/Geometry/localgeometry.jl b/src/Geometry/localgeometry.jl index 98010215c6..098ef94f48 100644 --- a/src/Geometry/localgeometry.jl +++ b/src/Geometry/localgeometry.jl @@ -51,3 +51,89 @@ end undertype(::Type{LocalGeometry{I, C, FT, S}}) where {I, C, FT, S} = FT undertype(::Type{SurfaceGeometry{FT, N}}) where {FT, N} = FT + + +function blockmat( + a::Geometry.Axis2Tensor{ + FT, + Tuple{Geometry.UAxis, Geometry.Covariant1Axis}, + SMatrix{1, 1, FT, 1}, + }, + b::Geometry.Axis2Tensor{ + FT, + Tuple{Geometry.WAxis, Geometry.Covariant3Axis}, + SMatrix{1, 1, FT, 1}, + }, +) where {FT} + A = Geometry.components(a) + B = Geometry.components(b) + Geometry.AxisTensor( + (Geometry.UWAxis(), Geometry.Covariant13Axis()), + SMatrix{2, 2}(A[1, 1], zero(FT), zero(FT), B[1, 1]), + ) +end + +function blockmat( + a::Geometry.Axis2Tensor{ + FT, + Tuple{Geometry.VAxis, Geometry.Covariant2Axis}, + SMatrix{1, 1, FT, 1}, + }, + b::Geometry.Axis2Tensor{ + FT, + Tuple{Geometry.WAxis, Geometry.Covariant3Axis}, + SMatrix{1, 1, FT, 1}, + }, +) where {FT} + A = Geometry.components(a) + B = Geometry.components(b) + Geometry.AxisTensor( + (Geometry.VWAxis(), Geometry.Covariant23Axis()), + SMatrix{2, 2}(A[1, 1], zero(FT), zero(FT), B[1, 1]), + ) +end + +function blockmat( + a::Geometry.Axis2Tensor{ + FT, + Tuple{Geometry.UVAxis, Geometry.Covariant12Axis}, + SMatrix{2, 2, FT, 4}, + }, + b::Geometry.Axis2Tensor{ + FT, + Tuple{Geometry.WAxis, Geometry.Covariant3Axis}, + SMatrix{1, 1, FT, 1}, + }, +) where {FT} + A = Geometry.components(a) + B = Geometry.components(b) + Geometry.AxisTensor( + (Geometry.UVWAxis(), Geometry.Covariant123Axis()), + SMatrix{3, 3}( + A[1, 1], + A[2, 1], + zero(FT), + A[1, 2], + A[2, 2], + zero(FT), + zero(FT), + zero(FT), + B[1, 1], + ), + ) +end + +function product_geometry( + horizontal_local_geometry::Geometry.LocalGeometry, + vertical_local_geometry::Geometry.LocalGeometry, +) + coordinates = Geometry.product_coordinates( + horizontal_local_geometry.coordinates, + vertical_local_geometry.coordinates, + ) + J = horizontal_local_geometry.J * vertical_local_geometry.J + WJ = horizontal_local_geometry.WJ * vertical_local_geometry.WJ + ∂x∂ξ = + blockmat(horizontal_local_geometry.∂x∂ξ, vertical_local_geometry.∂x∂ξ) + return Geometry.LocalGeometry(coordinates, J, WJ, ∂x∂ξ) +end diff --git a/src/Grids/Grids.jl b/src/Grids/Grids.jl new file mode 100644 index 0000000000..915cdcdacf --- /dev/null +++ b/src/Grids/Grids.jl @@ -0,0 +1,73 @@ +module Grids + +import ClimaComms, Adapt, ForwardDiff, LinearAlgebra +import LinearAlgebra: det, norm +import Memoize: @memoize +import WeakValueDicts: WeakValueDict +import ..DataLayouts, + ..Domains, ..Meshes, ..Topologies, ..Geometry, ..Quadratures +import ..Utilities: PlusHalf, half +import ..slab, ..column, ..level + +using StaticArrays + +""" + Grids.AbstractGrid + +Grids should define the following + + +- [`topology`](@ref): the topology of the grid +- [`mesh`](@ref): the mesh of the grid +- [`domain`](@ref): the domain of the grid +- `ClimaComms.context` +- `ClimaComms.device` + +- [`local_geometry_data`](@ref): the `DataLayout` object containing the local geometry data information + +""" +abstract type AbstractGrid end + + +""" + Grids.topology(grid::AbstractGrid) + +Get the topology of a grid. +""" +function topology end + +function vertical_topology end + +""" + Grids.local_geometry_data( + grid :: AbstractGrid, + staggering :: Union{Staggering, Nothing}, + ) + +Get the `DataLayout` object containing the local geometry data information of +the `grid` with staggering `staggering`. + +If the grid is not staggered, `staggering` should be `nothing`. +""" +function local_geometry_data end + +function local_dss_weights end +function quadrature_style end +function vertical_topology end + + + +ClimaComms.context(grid::AbstractGrid) = ClimaComms.context(topology(grid)) +ClimaComms.device(grid::AbstractGrid) = ClimaComms.device(topology(grid)) + +Meshes.domain(grid::AbstractGrid) = Meshes.domain(Topologies.topology(grid)) + +include("finitedifference.jl") +include("spectralelement.jl") +include("extruded.jl") +include("column.jl") +include("level.jl") + + + +end # module diff --git a/src/Grids/column.jl b/src/Grids/column.jl new file mode 100644 index 0000000000..b7507460c3 --- /dev/null +++ b/src/Grids/column.jl @@ -0,0 +1,47 @@ + + + + +""" + ColumnIndex(ij,h) + +An index into a column of a field. This can be used as an argument to `getindex` +of a `Field`, to return a field on that column. + +# Example +```julia +colidx = ColumnIndex((1,1),1) +field[colidx] +``` +""" +struct ColumnIndex{N} + ij::NTuple{N, Int} + h::Int +end + + +""" + ColumnGrid( + full_grid :: ExtrudedFiniteDifferenceGrid, + colidx :: ColumnIndex, + ) + +A view into a column of a `ExtrudedFiniteDifferenceGrid`. This can be used as an +""" +struct ColumnGrid{G <: ExtrudedFiniteDifferenceGrid, C <: ColumnIndex} <: + AbstractFiniteDifferenceGrid + full_grid::G + colidx::C +end + + +column(grid::ExtrudedFiniteDifferenceGrid, colidx::ColumnIndex) = + ColumnGrid(grid, colidx) + +topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) +vertical_topology(colgrid::ColumnGrid) = vertical_topology(colgrid.full_grid) + +local_geometry_data(colgrid::ColumnGrid, staggering::Staggering) = column( + local_geometry_data(colgrid.full_grid, staggering::Staggering), + colgrid.colidx, +) diff --git a/src/Grids/extruded.jl b/src/Grids/extruded.jl new file mode 100644 index 0000000000..f1b32a7abe --- /dev/null +++ b/src/Grids/extruded.jl @@ -0,0 +1,103 @@ +##### +##### Hybrid mesh +##### + +abstract type HypsographyAdaption end + +""" + Flat() + +No surface hypsography. +""" +struct Flat <: HypsographyAdaption end + +abstract type AbstractExtrudedFiniteDifferenceGrid <: AbstractGrid end + +""" + ExtrudedFiniteDifferenceGrid( + horizontal_space::AbstractSpace, + vertical_space::FiniteDifferenceSpace, + hypsography::HypsographyAdaption = Flat(), + ) + +Construct an `ExtrudedFiniteDifferenceGrid` from the horizontal and vertical spaces. +""" +mutable struct ExtrudedFiniteDifferenceGrid{ + H <: AbstractGrid, + V <: FiniteDifferenceGrid, + A <: HypsographyAdaption, + GG <: Geometry.AbstractGlobalGeometry, + LG, +} <: AbstractExtrudedFiniteDifferenceGrid + horizontal_grid::H + vertical_grid::V + hypsography::A + global_geometry::GG + center_local_geometry::LG + face_local_geometry::LG +end + +@memoize WeakValueDict function ExtrudedFiniteDifferenceGrid( + horizontal_grid::Union{SpectralElementGrid1D, SpectralElementGrid2D}, + vertical_grid::FiniteDifferenceGrid, + hypsography::Flat = Flat(), +) + global_geometry = horizontal_grid.global_geometry + center_local_geometry = + Geometry.product_geometry.( + horizontal_grid.local_geometry, + vertical_grid.center_local_geometry, + ) + face_local_geometry = + Geometry.product_geometry.( + horizontal_grid.local_geometry, + vertical_grid.face_local_geometry, + ) + + return ExtrudedFiniteDifferenceGrid( + horizontal_grid, + vertical_grid, + hypsography, + global_geometry, + center_local_geometry, + face_local_geometry, + ) +end + +topology(grid::ExtrudedFiniteDifferenceGrid) = topology(grid.horizontal_grid) + +vertical_topology(grid::ExtrudedFiniteDifferenceGrid) = + topology(grid.vertical_grid) + +local_dss_weights(grid::ExtrudedFiniteDifferenceGrid) = + local_dss_weights(grid.horizontal_grid) + + +local_geometry_data(grid::AbstractExtrudedFiniteDifferenceGrid, ::CellCenter) = + grid.center_local_geometry +local_geometry_data(grid::AbstractExtrudedFiniteDifferenceGrid, ::CellFace) = + grid.face_local_geometry + +quadrature_style(grid::ExtrudedFiniteDifferenceGrid) = + quadrature_style(grid.horizontal_grid) + + +## GPU compatibility +struct DeviceExtrudedFiniteDifferenceGrid{Q, GG, LG} <: + AbstractSpectralElementGrid + quadrature_style::Q + global_geometry::GG + center_local_geometry::LG + face_local_geometry::LG +end + +Adapt.adapt_structure(to, grid::ExtrudedFiniteDifferenceGrid) = + DeviceExtrudedFiniteDifferenceGrid( + Adapt.adapt(to, grid.horizontal_grid.quadrature_style), + Adapt.adapt(to, grid.global_geometry), + Adapt.adapt(to, grid.center_local_geometry), + Adapt.adapt(to, grid.face_local_geometry), + ) + +quadrature_style(grid::DeviceExtrudedFiniteDifferenceGrid) = + grid.quadrature_style diff --git a/src/Grids/finitedifference.jl b/src/Grids/finitedifference.jl new file mode 100644 index 0000000000..2f7c346fee --- /dev/null +++ b/src/Grids/finitedifference.jl @@ -0,0 +1,148 @@ + +abstract type Staggering end + +""" + CellCenter() + +Cell center location +""" +struct CellCenter <: Staggering end + +""" + CellFace() + +Cell face location +""" +struct CellFace <: Staggering end + + + + + + +abstract type AbstractFiniteDifferenceGrid <: AbstractGrid end + +""" + FiniteDifferenceGrid(topology::Topologies.IntervalTopology) + FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) + +Construct a `FiniteDifferenceGrid` from an `IntervalTopology` (or an +`IntervalMesh`). + +This is an object which contains all the necessary geometric information. + +To avoid unnecessary duplication, we memoize the construction of the grid. +""" +mutable struct FiniteDifferenceGrid{ + T <: Topologies.AbstractIntervalTopology, + GG, + LG, +} <: AbstractFiniteDifferenceGrid + topology::T + global_geometry::GG + center_local_geometry::LG + face_local_geometry::LG +end + + +@memoize WeakValueDict function FiniteDifferenceGrid( + topology::Topologies.IntervalTopology, +) + global_geometry = Geometry.CartesianGlobalGeometry() + mesh = topology.mesh + CT = Meshes.coordinate_type(mesh) + AIdx = Geometry.coordinate_axis(CT) + # TODO: FD operators hardcoded to work over the 3-axis, need to generalize + # similar to spectral operators + @assert AIdx == (3,) "FiniteDifference operations only work over the 3-axis (ZPoint) domain" + FT = eltype(CT) + ArrayType = ClimaComms.array_type(topology) + face_coordinates = collect(mesh.faces) + LG = Geometry.LocalGeometry{AIdx, CT, FT, SMatrix{1, 1, FT, 1}} + nface = length(face_coordinates) - Topologies.isperiodic(topology) + ncent = length(face_coordinates) - 1 + # contstruct on CPU, copy to device at end + center_local_geometry = DataLayouts.VF{LG}(Array{FT}, ncent) + face_local_geometry = DataLayouts.VF{LG}(Array{FT}, nface) + for i in 1:ncent + # centers + coord⁻ = Geometry.component(face_coordinates[i], 1) + coord⁺ = Geometry.component(face_coordinates[i + 1], 1) + # at the moment we use a "discrete Jacobian" + # ideally we should use the continuous quantity via the derivative of the warp function + # could we just define this then as deriv on the mesh element coordinates? + coord = (coord⁺ + coord⁻) / 2 + Δcoord = coord⁺ - coord⁻ + J = Δcoord + WJ = Δcoord + ∂x∂ξ = SMatrix{1, 1}(J) + center_local_geometry[i] = Geometry.LocalGeometry( + CT(coord), + J, + WJ, + Geometry.AxisTensor( + (Geometry.LocalAxis{AIdx}(), Geometry.CovariantAxis{AIdx}()), + ∂x∂ξ, + ), + ) + end + for i in 1:nface + coord = Geometry.component(face_coordinates[i], 1) + if i == 1 + # bottom face + if Topologies.isperiodic(topology) + Δcoord⁺ = + Geometry.component(face_coordinates[2], 1) - + Geometry.component(face_coordinates[1], 1) + Δcoord⁻ = + Geometry.component(face_coordinates[end], 1) - + Geometry.component(face_coordinates[end - 1], 1) + J = (Δcoord⁺ + Δcoord⁻) / 2 + WJ = J + else + coord⁺ = Geometry.component(face_coordinates[2], 1) + J = coord⁺ - coord + WJ = J / 2 + end + elseif !Topologies.isperiodic(topology) && i == nface + # top face + coord⁻ = Geometry.component(face_coordinates[i - 1], 1) + J = coord - coord⁻ + WJ = J / 2 + else + coord⁺ = Geometry.component(face_coordinates[i + 1], 1) + coord⁻ = Geometry.component(face_coordinates[i - 1], 1) + J = (coord⁺ - coord⁻) / 2 + WJ = J + end + ∂x∂ξ = SMatrix{1, 1}(J) + ∂ξ∂x = SMatrix{1, 1}(inv(J)) + face_local_geometry[i] = Geometry.LocalGeometry( + CT(coord), + J, + WJ, + Geometry.AxisTensor( + (Geometry.LocalAxis{AIdx}(), Geometry.CovariantAxis{AIdx}()), + ∂x∂ξ, + ), + ) + end + return FiniteDifferenceGrid( + topology, + global_geometry, + Adapt.adapt(ArrayType, center_local_geometry), + Adapt.adapt(ArrayType, face_local_geometry), + ) +end + + +FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) = + FiniteDifferenceGrid(Topologies.IntervalTopology(mesh)) + +# accessors +topology(grid::FiniteDifferenceGrid) = grid.topology + +local_geometry_data(grid::FiniteDifferenceGrid, ::CellCenter) = + grid.center_local_geometry +local_geometry_data(grid::FiniteDifferenceGrid, ::CellFace) = + grid.face_local_geometry diff --git a/src/Grids/level.jl b/src/Grids/level.jl new file mode 100644 index 0000000000..7601bb12d9 --- /dev/null +++ b/src/Grids/level.jl @@ -0,0 +1,22 @@ +struct LevelGrid{ + G <: ExtrudedFiniteDifferenceGrid, + L <: Union{Int, PlusHalf{Int}}, +} <: AbstractGrid + full_grid::G + level::L +end + +level(grid::ExtrudedFiniteDifferenceGrid, level::Union{Int, PlusHalf{Int}}) = + LevelGrid(grid, level) + +topology(levelgrid::LevelGrid) = topology(levelgrid.full_grid) + +local_geometry_data(colgrid::LevelGrid{<:Any, Int}, ::Nothing) = level( + local_geometry_data(levelgrid.full_grid, CellCenter()), + levelgrid.level, +) +local_geometry_data(colgrid::LevelGrid{<:Any, PlusHalf{Int}}, ::Nothing) = + level( + local_geometry_data(levelgrid.full_grid, CellFace()), + levelgrid.level + half, + ) diff --git a/src/Grids/spectralelement.jl b/src/Grids/spectralelement.jl new file mode 100644 index 0000000000..7498047955 --- /dev/null +++ b/src/Grids/spectralelement.jl @@ -0,0 +1,498 @@ + + +abstract type AbstractSpectralElementGrid <: AbstractGrid end + +""" + SpectralElementGrid1D(mesh::Meshes.IntervalMesh, quadrature_style::Quadratures.QuadratureStyle) + +A one-dimensional space: within each element the space is represented as a polynomial. +""" +mutable struct SpectralElementGrid1D{ + T, + Q, + GG <: Geometry.AbstractGlobalGeometry, + LG, + D, +} <: AbstractSpectralElementGrid + topology::T + quadrature_style::Q + global_geometry::GG + local_geometry::LG + dss_weights::D +end + +@memoize WeakValueDict function SpectralElementGrid1D( + topology::Topologies.IntervalTopology, + quadrature_style::Quadratures.QuadratureStyle, +) + global_geometry = Geometry.CartesianGlobalGeometry() + CoordType = Topologies.coordinate_type(topology) + AIdx = Geometry.coordinate_axis(CoordType) + FT = eltype(CoordType) + nelements = Topologies.nlocalelems(topology) + Nq = Quadratures.degrees_of_freedom(quadrature_style) + + LG = Geometry.LocalGeometry{AIdx, CoordType, FT, SMatrix{1, 1, FT, 1}} + local_geometry = DataLayouts.IFH{LG, Nq}(Array{FT}, nelements) + quad_points, quad_weights = + Quadratures.quadrature_points(FT, quadrature_style) + + for elem in 1:nelements + local_geometry_slab = slab(local_geometry, elem) + for i in 1:Nq + ξ = quad_points[i] + # TODO: we need to massage the coordinate points because the grid is assumed 2D + vcoords = Topologies.vertex_coordinates(topology, elem) + x = Geometry.linear_interpolate(vcoords, ξ) + ∂x∂ξ = + ( + Geometry.component(vcoords[2], 1) - + Geometry.component(vcoords[1], 1) + ) / 2 + J = abs(∂x∂ξ) + WJ = J * quad_weights[i] + local_geometry_slab[i] = Geometry.LocalGeometry( + x, + J, + WJ, + Geometry.AxisTensor( + ( + Geometry.LocalAxis{AIdx}(), + Geometry.CovariantAxis{AIdx}(), + ), + ∂x∂ξ, + ), + ) + end + end + dss_weights = copy(local_geometry.J) + dss_weights .= one(FT) + Topologies.dss_1d!(topology, dss_weights) + dss_weights = one(FT) ./ dss_weights + + return SpectralElementGrid1D( + topology, + quadrature_style, + global_geometry, + local_geometry, + dss_weights, + ) +end + + + +""" + SpectralElementSpace2D <: AbstractSpace + +A two-dimensional space: within each element the space is represented as a polynomial. +""" +mutable struct SpectralElementGrid2D{ + T, + Q, + GG <: Geometry.AbstractGlobalGeometry, + LG, + D, + IS, + BS, +} <: AbstractSpectralElementGrid + topology::T + quadrature_style::Q + global_geometry::GG + local_geometry::LG + local_dss_weights::D + internal_surface_geometry::IS + boundary_surface_geometries::BS +end + + + + +""" + SpectralElementSpace2D(topology, quadrature_style; enable_bubble) + +Construct a `SpectralElementSpace2D` instance given a `topology` and `quadrature`. The +flag `enable_bubble` enables the `bubble correction` for more accurate element areas. + +# Input arguments: +- topology: Topology2D +- quadrature_style: QuadratureStyle +- enable_bubble: Bool + +The idea behind the so-called `bubble_correction` is that the numerical area +of the domain (e.g., the sphere) is given by the sum of nodal integration weights +times their corresponding Jacobians. However, this discrete sum is not exactly +equal to the exact geometric area (4pi*radius^2 for the sphere). To make these equal, +the "epsilon bubble" approach modifies the inner weights in each element so that +geometric and numerical areas of each element match. + +Let ``\\Delta A^e := A^e_{exact} - A^e_{approx}``, then, in +the case of linear elements, we correct ``W_{i,j} J^e_{i,j}`` by: +```math +\\widehat{W_{i,j} J^e}_{i,j} = W_{i,j} J^e_{i,j} + \\Delta A^e * W_{i,j} / Nq^2 . +``` +and the case of non linear elements, by +```math +\\widehat{W_{i,j} J^e}_{i,j} = W_{i,j} J^e_{i,j} \\left( 1 + \\tilde{A}^e \\right) , +``` +where ``\\tilde{A}^e`` is the approximated area given by the sum of the interior nodal integration weights. + +Note: This is accurate only for cubed-spheres of the [`Meshes.EquiangularCubedSphere`](@ref) and +[`Meshes.EquidistantCubedSphere`](@ref) type, not for [`Meshes.ConformalCubedSphere`](@ref). +""" +@memoize WeakValueDict function SpectralElementGrid2D( + topology, + quadrature_style; + enable_bubble = false, +) + + # 1. compute localgeom for local elememts + # 2. ghost exchange of localgeom + # 3. do a round of dss on WJs + # 4. compute dss weights (WJ ./ dss(WJ)) (local and ghost) + + # DSS on a field would consist of + # 1. copy to send buffers + # 2. start exchange + # 3. dss of internal connections + # - option for weighting and transformation + # 4. finish exchange + # 5. dss of ghost connections + + ### How to DSS multiple fields? + # 1. allocate buffers externally + DA = ClimaComms.array_type(topology) + domain = Topologies.domain(topology) + if domain isa Domains.SphereDomain + CoordType3D = Topologies.coordinate_type(topology) + FT = Geometry.float_type(CoordType3D) + CoordType2D = Geometry.LatLongPoint{FT} # Domains.coordinate_type(topology) + global_geometry = + Geometry.SphericalGlobalGeometry(topology.mesh.domain.radius) + else + CoordType2D = Topologies.coordinate_type(topology) + FT = Geometry.float_type(CoordType2D) + global_geometry = Geometry.CartesianGlobalGeometry() + end + AIdx = Geometry.coordinate_axis(CoordType2D) + nlelems = Topologies.nlocalelems(topology) + ngelems = Topologies.nghostelems(topology) + Nq = Quadratures.degrees_of_freedom(quadrature_style) + high_order_quadrature_style = Quadratures.GLL{Nq * 2}() + high_order_Nq = Quadratures.degrees_of_freedom(high_order_quadrature_style) + + LG = Geometry.LocalGeometry{AIdx, CoordType2D, FT, SMatrix{2, 2, FT, 4}} + + local_geometry = DataLayouts.IJFH{LG, Nq}(Array{FT}, nlelems) + + quad_points, quad_weights = + Quadratures.quadrature_points(FT, quadrature_style) + high_order_quad_points, high_order_quad_weights = + Quadratures.quadrature_points(FT, high_order_quadrature_style) + for (lidx, elem) in enumerate(Topologies.localelems(topology)) + elem_area = zero(FT) + high_order_elem_area = zero(FT) + Δarea = zero(FT) + interior_elem_area = zero(FT) + rel_interior_elem_area_Δ = zero(FT) + local_geometry_slab = slab(local_geometry, lidx) + # high-order quadrature loop for computing geometric element face area. + for i in 1:high_order_Nq, j in 1:high_order_Nq + ξ = SVector(high_order_quad_points[i], high_order_quad_points[j]) + u, ∂u∂ξ = + compute_local_geometry(global_geometry, topology, elem, ξ, AIdx) + J_high_order = det(Geometry.components(∂u∂ξ)) + WJ_high_order = + J_high_order * + high_order_quad_weights[i] * + high_order_quad_weights[j] + high_order_elem_area += WJ_high_order + end + # low-order quadrature loop for computing numerical element face area + for i in 1:Nq, j in 1:Nq + ξ = SVector(quad_points[i], quad_points[j]) + u, ∂u∂ξ = + compute_local_geometry(global_geometry, topology, elem, ξ, AIdx) + J = det(Geometry.components(∂u∂ξ)) + WJ = J * quad_weights[i] * quad_weights[j] + elem_area += WJ + if !enable_bubble + local_geometry_slab[i, j] = + Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) + end + end + + # If enabled, apply bubble correction + if enable_bubble + if abs(elem_area - high_order_elem_area) ≤ eps(FT) + for i in 1:Nq, j in 1:Nq + ξ = SVector(quad_points[i], quad_points[j]) + u, ∂u∂ξ = compute_local_geometry( + global_geometry, + topology, + elem, + ξ, + AIdx, + ) + J = det(Geometry.components(∂u∂ξ)) + WJ = J * quad_weights[i] * quad_weights[j] + local_geometry_slab[i, j] = + Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) + end + else + # The idea behind the so-called `bubble_correction` is that + # the numerical area of the domain (e.g., the sphere) is given by the sum + # of nodal integration weights times their corresponding Jacobians. However, + # this discrete sum is not exactly equal to the exact geometric area + # (4pi*radius^2 for the sphere). It is required that numerical area = geometric area. + # The "epsilon bubble" approach modifies the inner weights in each + # element so that geometric and numerical areas of each element match. + + # Compute difference between geometric area of an element and its approximate numerical area + Δarea = high_order_elem_area - elem_area + + # Linear elements: Nq == 2 (SpectralElementSpace2D cannot have Nq < 2) + # Use uniform bubble correction + if Nq == 2 + for i in 1:Nq, j in 1:Nq + ξ = SVector(quad_points[i], quad_points[j]) + u, ∂u∂ξ = compute_local_geometry( + global_geometry, + topology, + elem, + ξ, + AIdx, + ) + J = det(Geometry.components(∂u∂ξ)) + J += Δarea / Nq^2 + WJ = J * quad_weights[i] * quad_weights[j] + local_geometry_slab[i, j] = + Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) + end + else # Higher-order elements: Use HOMME bubble correction for the interior nodes + for i in 2:(Nq - 1), j in 2:(Nq - 1) + ξ = SVector(quad_points[i], quad_points[j]) + u, ∂u∂ξ = compute_local_geometry( + global_geometry, + topology, + elem, + ξ, + AIdx, + ) + J = det(Geometry.components(∂u∂ξ)) + WJ = J * quad_weights[i] * quad_weights[j] + interior_elem_area += WJ + end + # Check that interior_elem_area is not too small + if abs(interior_elem_area) ≤ sqrt(eps(FT)) + error( + "Bubble correction cannot be performed; sum of inner weights is too small.", + ) + end + rel_interior_elem_area_Δ = Δarea / interior_elem_area + + for i in 1:Nq, j in 1:Nq + ξ = SVector(quad_points[i], quad_points[j]) + u, ∂u∂ξ = compute_local_geometry( + global_geometry, + topology, + elem, + ξ, + AIdx, + ) + J = det(Geometry.components(∂u∂ξ)) + # Modify J only for interior nodes + if i != 1 && j != 1 && i != Nq && j != Nq + J *= (1 + rel_interior_elem_area_Δ) + end + WJ = J * quad_weights[i] * quad_weights[j] + # Finally allocate local geometry + local_geometry_slab[i, j] = + Geometry.LocalGeometry(u, J, WJ, ∂u∂ξ) + end + end + end + end + end + + # dss_weights = J ./ dss(J) + J = DataLayouts.rebuild(local_geometry.J, DA) + dss_local_weights = copy(J) + if quadrature_style isa Quadratures.GLL + Topologies.dss!(dss_local_weights, topology) + end + dss_local_weights .= J ./ dss_local_weights + + SG = Geometry.SurfaceGeometry{ + FT, + Geometry.AxisVector{FT, Geometry.LocalAxis{AIdx}, SVector{2, FT}}, + } + interior_faces = Array(Topologies.interior_faces(topology)) + + if quadrature_style isa Quadratures.GLL + internal_surface_geometry = + DataLayouts.IFH{SG, Nq}(Array{FT}, length(interior_faces)) + for (iface, (lidx⁻, face⁻, lidx⁺, face⁺, reversed)) in + enumerate(interior_faces) + internal_surface_geometry_slab = + slab(internal_surface_geometry, iface) + + local_geometry_slab⁻ = slab(local_geometry, lidx⁻) + local_geometry_slab⁺ = slab(local_geometry, lidx⁺) + + for q in 1:Nq + sgeom⁻ = compute_surface_geometry( + local_geometry_slab⁻, + quad_weights, + face⁻, + q, + false, + ) + sgeom⁺ = compute_surface_geometry( + local_geometry_slab⁺, + quad_weights, + face⁺, + q, + reversed, + ) + + @assert sgeom⁻.sWJ ≈ sgeom⁺.sWJ + @assert sgeom⁻.normal ≈ -sgeom⁺.normal + + internal_surface_geometry_slab[q] = sgeom⁻ + end + end + internal_surface_geometry = + DataLayouts.rebuild(internal_surface_geometry, DA) + + boundary_surface_geometries = + map(Topologies.boundary_tags(topology)) do boundarytag + boundary_faces = + Topologies.boundary_faces(topology, boundarytag) + boundary_surface_geometry = + DataLayouts.IFH{SG, Nq}(Array{FT}, length(boundary_faces)) + for (iface, (elem, face)) in enumerate(boundary_faces) + boundary_surface_geometry_slab = + slab(boundary_surface_geometry, iface) + local_geometry_slab = slab(local_geometry, elem) + for q in 1:Nq + boundary_surface_geometry_slab[q] = + compute_surface_geometry( + local_geometry_slab, + quad_weights, + face, + q, + false, + ) + end + end + DataLayouts.rebuild(boundary_surface_geometry, DA) + end + else + internal_surface_geometry = nothing + boundary_surface_geometries = nothing + end + return SpectralElementGrid2D( + topology, + quadrature_style, + global_geometry, + DataLayouts.rebuild(local_geometry, DA), + dss_local_weights, + internal_surface_geometry, + boundary_surface_geometries, + ) +end + +function compute_local_geometry( + global_geometry::Geometry.SphericalGlobalGeometry, + topology, + elem, + ξ, + AIdx, +) + x = Meshes.coordinates(topology.mesh, elem, ξ) + u = Geometry.LatLongPoint(x, global_geometry) + ∂x∂ξ = Geometry.AxisTensor( + (Geometry.Cartesian123Axis(), Geometry.CovariantAxis{AIdx}()), + ForwardDiff.jacobian(ξ) do ξ + Geometry.components(Meshes.coordinates(topology.mesh, elem, ξ)) + end, + ) + G = Geometry.local_to_cartesian(global_geometry, u) + ∂u∂ξ = Geometry.project(Geometry.LocalAxis{AIdx}(), G' * ∂x∂ξ) + + return u, ∂u∂ξ +end +function compute_local_geometry( + global_geometry::Geometry.AbstractGlobalGeometry, + topology, + elem, + ξ, + AIdx, +) + u = Meshes.coordinates(topology.mesh, elem, ξ) + ∂u∂ξ = Geometry.AxisTensor( + (Geometry.LocalAxis{AIdx}(), Geometry.CovariantAxis{AIdx}()), + ForwardDiff.jacobian(ξ) do ξ + Geometry.components(Meshes.coordinates(topology.mesh, elem, ξ)) + end, + ) + + return u, ∂u∂ξ +end + +function compute_surface_geometry( + local_geometry_slab, + quad_weights, + face, + q, + reversed = false, +) + Nq = length(quad_weights) + @assert size(local_geometry_slab) == (Nq, Nq, 1, 1, 1) + i, j = Topologies.face_node_index(face, Nq, q, reversed) + + local_geometry = local_geometry_slab[i, j] + (; J, ∂ξ∂x) = local_geometry + + # surface mass matrix + n = if face == 4 + -J * ∂ξ∂x[1, :] * quad_weights[j] + elseif face == 2 + J * ∂ξ∂x[1, :] * quad_weights[j] + elseif face == 1 + -J * ∂ξ∂x[2, :] * quad_weights[i] + elseif face == 3 + J * ∂ξ∂x[2, :] * quad_weights[i] + end + sWJ = norm(n) + n = n / sWJ + return Geometry.SurfaceGeometry(sWJ, n) +end + + +# accessors + +topology(grid::AbstractSpectralElementGrid) = grid.topology + +local_geometry_data(grid::AbstractSpectralElementGrid, ::Nothing) = + grid.local_geometry + +quadrature_style(grid::AbstractSpectralElementGrid) = grid.quadrature_style +local_dss_weights(grid::AbstractSpectralElementGrid) = grid.dss_weights + + + +## GPU compatibility + +struct DeviceSpectralElementGrid2D{Q, GG, LG} <: AbstractSpectralElementGrid + quadrature_style::Q + global_geometry::GG + local_geometry::LG +end + +Adapt.adapt_structure(to, grid::SpectralElementGrid2D) = + DeviceSpectralElementGrid2D( + Adapt.adapt(to, grid.quadrature_style), + Adapt.adapt(to, grid.global_geometry), + Adapt.adapt(to, grid.local_geometry), + ) diff --git a/src/Quadratures/Quadratures.jl b/src/Quadratures/Quadratures.jl new file mode 100644 index 0000000000..5cacad9eb7 --- /dev/null +++ b/src/Quadratures/Quadratures.jl @@ -0,0 +1,285 @@ + +module Quadratures + +import GaussQuadrature +import StaticArrays: SVector, SMatrix, MMatrix +import LinearAlgebra: Diagonal + +export QuadratureStyle, + GLL, GL, polynomial_degree, degrees_of_freedom, quadrature_points + +""" + QuadratureStyle + +Quadrature style supertype. See sub-types: + - [`GLL`](@ref) + - [`GL`](@ref) + - [`Uniform`](@ref) +""" +abstract type QuadratureStyle end + +""" + polynomial_degree(QuadratureStyle) -> Int + +Returns the polynomial degree of the `QuadratureStyle` concrete type +""" +function polynomial_degree end + + +""" + degrees_of_freedom(QuadratureStyle) -> Int + +Returns the degrees_of_freedom of the `QuadratureStyle` concrete type +""" +function degrees_of_freedom end + +""" + points, weights = quadrature_points(::Type{FT}, quadrature_style) + +The points and weights of the quadrature rule in floating point type `FT`. +""" +function quadrature_points end + + +""" + GLL{Nq}() + +Gauss-Legendre-Lobatto quadrature using `Nq` quadrature points. +""" +struct GLL{Nq} <: QuadratureStyle end + +Base.show(io::IO, ::GLL{Nq}) where {Nq} = + print(io, Nq, "-point Gauss-Legendre-Lobatto quadrature") + +@inline polynomial_degree(::GLL{Nq}) where {Nq} = Int(Nq - 1) +@inline degrees_of_freedom(::GLL{Nq}) where {Nq} = Int(Nq) + +@generated function quadrature_points(::Type{FT}, ::GLL{Nq}) where {FT, Nq} + points, weights = GaussQuadrature.legendre(FT, Nq, GaussQuadrature.both) + :($(SVector{Nq}(points)), $(SVector{Nq}(weights))) +end + +""" + GL{Nq}() + +Gauss-Legendre quadrature using `Nq` quadrature points. +""" +struct GL{Nq} <: QuadratureStyle end +Base.show(io::IO, ::GL{Nq}) where {Nq} = + print(io, Nq, "-point Gauss-Legendre quadrature") + +@inline polynomial_degree(::GL{Nq}) where {Nq} = Int(Nq - 1) +@inline 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) + :($(SVector{Nq}(points)), $(SVector{Nq}(weights))) +end + +""" + Uniform{Nq}() + +Uniformly-spaced quadrature. +""" +struct Uniform{Nq} <: QuadratureStyle end + +@inline polynomial_degree(::Uniform{Nq}) where {Nq} = Int(Nq - 1) +@inline degrees_of_freedom(::Uniform{Nq}) where {Nq} = Int(Nq) + +@generated function quadrature_points(::Type{FT}, ::Uniform{Nq}) where {FT, Nq} + points = SVector{Nq}(range(-1 + 1 / Nq, step = 2 / Nq, length = Nq)) + weights = SVector{Nq}(ntuple(i -> 2 / Nq, Nq)) + :($points, $weights) +end + +""" + ClosedUniform{Nq}() + +Uniformly-spaced quadrature including boundary. +""" +struct ClosedUniform{Nq} <: QuadratureStyle end + +@inline polynomial_degree(::ClosedUniform{Nq}) where {Nq} = Int(Nq - 1) +@inline degrees_of_freedom(::ClosedUniform{Nq}) where {Nq} = Int(Nq) + +@generated function quadrature_points( + ::Type{FT}, + ::ClosedUniform{Nq}, +) where {FT, Nq} + points = SVector{Nq}(range(FT(-1), FT(1), length = Nq)) + weights = SVector{Nq}( + 1 / (Nq - 1), + ntuple(i -> 2 / (Nq - 1), Nq - 2)..., + 1 / (Nq - 1), + ) + :($points, $weights) +end + + +""" + barycentric_weights(x::SVector{Nq}) where {Nq} + +The barycentric weights associated with the array of point locations `x`: + +```math +w_j = \\frac{1}{\\prod_{k \\ne j} (x_i - x_j)} +``` + +See [Berrut2004](@cite), equation 3.2. +""" +function barycentric_weights(r::SVector{Nq, T}) where {Nq, T} + SVector{Nq}(ntuple(Nq) do i + w = one(T) + for j in 1:Nq + if j != i + w *= (r[j] - r[i]) + end + end + inv(w) + end) +end +@generated function barycentric_weights( + ::Type{FT}, + quadstyle::QuadratureStyle, +) where {FT} + barycentric_weights(quadrature_points(FT, quadstyle())[1]) +end + +""" + interpolation_matrix(x::SVector, r::SVector{Nq}) + +The matrix which interpolates the Lagrange polynomial of degree `Nq-1` through +the points `r`, to points `x`. The matrix coefficients are computed using the +Barycentric formula of [Berrut2004](@cite), section 4: +```math +I_{ij} = \\begin{cases} +1 & \\text{if } x_i = r_j, \\\\ +0 & \\text{if } x_i = r_k \\text{ for } k \\ne j, \\\\ +\\frac{\\displaystyle \\frac{w_j}{x_i - r_j}}{\\displaystyle \\sum_k \\frac{w_k}{x_i - r_k}} & \\text{otherwise,} +\\end{cases} +``` +where ``w_j`` are the barycentric weights, see [`barycentric_weights`](@ref). +""" +function interpolation_matrix( + points_to::SVector{Nto}, + points_from::SVector{Nfrom}, +) where {Nto, Nfrom} + T = eltype(points_to) + bw = barycentric_weights(points_from) + M = zeros(MMatrix{Nto, Nfrom, T, Nto * Nfrom}) + for i in 1:Nto + x_to = points_to[i] + skip_row = false + for j in 1:Nfrom + if x_to == points_from[j] + # assign to one to avoid singularity condition + M[i, j] = one(T) + # skip over the equal boundry condition + skip_row = true + end + skip_row && break + end + skip_row && continue + w = bw ./ (x_to .- points_from) + M[i, :] .= w ./ sum(w) + end + return SMatrix(M) +end + +@generated function interpolation_matrix( + ::Type{FT}, + quadto::QuadratureStyle, + quadfrom::QuadratureStyle, +) where {FT} + interpolation_matrix( + quadrature_points(FT, quadto())[1], + quadrature_points(FT, quadfrom())[1], + ) +end + +""" + V = orthonormal_poly(points, quad) + +`V_{ij}` contains the `j-1`th Legendre polynomial evaluated at `points[i]`. +i.e. it is the mapping from the modal to the nodal representation. +""" +function orthonormal_poly( + points::SVector{Np, FT}, + quad::GLL{Nq}, +) where {FT, Np, Nq} + N = Nq - 1 + a, b = GaussQuadrature.legendre_coefs(FT, N) + if N == 0 + return SMatrix{Np, 1}(ntuple(x -> b[1], Np)) + end + return SMatrix{Np, Nq}(GaussQuadrature.orthonormal_poly(points, a, b)) +end + +function spectral_filter_matrix( + quad::GLL{Nq}, + Σ::SVector{Nq, FT}, +) where {Nq, FT} + points, _ = quadrature_points(FT, quad) + V = orthonormal_poly(points, quad) + return V * Diagonal(Σ) / V +end + +function cutoff_filter_matrix( + ::Type{FT}, + quad::GLL{Nq}, + Nc::Integer, +) where {FT, Nq} + Σ = SVector(ntuple(i -> i <= Nc ? FT(1) : FT(0), Nq)) + return spectral_filter_matrix(quad, Σ) +end + +""" + differentiation_matrix(r::SVector{Nq, T}) where {Nq, T} + +The spectral differentiation matrix for the Lagrange polynomial of degree `Nq-1` +interpolating at points `r`. + +The matrix coefficients are computed using the [Berrut2004](@cite), section 9.3: +```math +D_{ij} = \\begin{cases} + \\displaystyle + \\frac{w_j}{w_i (x_i - x_j)} &\\text{ if } i \\ne j \\\\ + -\\sum_{k \\ne j} D_{kj} &\\text{ if } i = j +\\end{cases} +``` +where ``w_j`` are the barycentric weights, see [`barycentric_weights`](@ref). +""" +function differentiation_matrix(r::SVector{Nq, T}) where {Nq, T} + wb = barycentric_weights(r) + SMatrix{Nq, Nq, T, Nq * Nq}( + begin + if i == j + D = zero(T) + for l in 1:Nq + if l != i + D += one(T) / (r[i] - r[l]) + end + end + D + else + (wb[i] / wb[j]) / (r[j] - r[i]) + end + end for j in 1:Nq, i in 1:Nq + ) +end + +""" + differentiation_matrix(FT, quadstyle::QuadratureStyle) + +The spectral differentiation matrix at the quadrature points of `quadstyle`, +using floating point types `FT`. +""" +@generated function differentiation_matrix( + ::Type{FT}, + quadstyle::QuadratureStyle, +) where {FT} + differentiation_matrix(quadrature_points(FT, quadstyle())[1]) +end + + +end # module diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl index c04709238e..4ccdbeffbc 100644 --- a/src/Spaces/dss.jl +++ b/src/Spaces/dss.jl @@ -11,6 +11,11 @@ import ..Topologies: load_from_recv_buffer! +perimeter(space::AbstractSpectralElementSpace) = Topologies.Perimeter2D( + Quadratures.degrees_of_freedom(quadrature_style(space)), +) + + """ create_dss_buffer( data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 7cdbe49071..d0e68a12dc 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -8,8 +8,8 @@ struct ExtrudedFiniteDifferenceSpace{ staggering::S end -space(staggering::Staggering, grid::Grids.ExtrudedFiniteDifferenceGrid) = - ExtrudedFiniteDifferenceSpace(staggering, grid) +space(grid::Grids.ExtrudedFiniteDifferenceGrid, staggering::Staggering) = + ExtrudedFiniteDifferenceSpace(grid, staggering) const FaceExtrudedFiniteDifferenceSpace{G} = ExtrudedFiniteDifferenceSpace{G, CellFace} @@ -38,11 +38,16 @@ function ExtrudedFiniteDifferenceSpace( return ExtrudedFiniteDifferenceSpace(grid, vertical_space.staggering) end +FaceExtrudedFiniteDifferenceSpace(space::ExtrudedFiniteDifferenceSpace) = + ExtrudedFiniteDifferenceSpace(space.grid, CellFace()) +CenterExtrudedFiniteDifferenceSpace(space::ExtrudedFiniteDifferenceSpace) = + ExtrudedFiniteDifferenceSpace(space.grid, CellCenter()) + local_dss_weights(space::ExtrudedFiniteDifferenceSpace) = local_dss_weights(grid(space)) staggering(space::ExtrudedFiniteDifferenceSpace) = space.staggering - +grid(space::ExtrudedFiniteDifferenceSpace) = space.grid space(space::ExtrudedFiniteDifferenceSpace, staggering::Staggering) = ExtrudedFiniteDifferenceSpace(grid(space), staggering) @@ -145,7 +150,7 @@ end # TODO: deprecate these column(space::ExtrudedFiniteDifferenceSpace, i, j, h) = - column(space, ColumnIndex((i, j), h)) + column(space, Grids.ColumnIndex((i, j), h)) struct LevelSpace{S, L} <: AbstractSpace @@ -186,90 +191,6 @@ function right_boundary_name(space::ExtrudedFiniteDifferenceSpace) boundaries = Topologies.boundaries(Spaces.vertical_topology(space)) propertynames(boundaries)[2] end -function blockmat( - a::Geometry.Axis2Tensor{ - FT, - Tuple{Geometry.UAxis, Geometry.Covariant1Axis}, - SMatrix{1, 1, FT, 1}, - }, - b::Geometry.Axis2Tensor{ - FT, - Tuple{Geometry.WAxis, Geometry.Covariant3Axis}, - SMatrix{1, 1, FT, 1}, - }, -) where {FT} - A = Geometry.components(a) - B = Geometry.components(b) - Geometry.AxisTensor( - (Geometry.UWAxis(), Geometry.Covariant13Axis()), - SMatrix{2, 2}(A[1, 1], zero(FT), zero(FT), B[1, 1]), - ) -end - -function blockmat( - a::Geometry.Axis2Tensor{ - FT, - Tuple{Geometry.VAxis, Geometry.Covariant2Axis}, - SMatrix{1, 1, FT, 1}, - }, - b::Geometry.Axis2Tensor{ - FT, - Tuple{Geometry.WAxis, Geometry.Covariant3Axis}, - SMatrix{1, 1, FT, 1}, - }, -) where {FT} - A = Geometry.components(a) - B = Geometry.components(b) - Geometry.AxisTensor( - (Geometry.VWAxis(), Geometry.Covariant23Axis()), - SMatrix{2, 2}(A[1, 1], zero(FT), zero(FT), B[1, 1]), - ) -end - -function blockmat( - a::Geometry.Axis2Tensor{ - FT, - Tuple{Geometry.UVAxis, Geometry.Covariant12Axis}, - SMatrix{2, 2, FT, 4}, - }, - b::Geometry.Axis2Tensor{ - FT, - Tuple{Geometry.WAxis, Geometry.Covariant3Axis}, - SMatrix{1, 1, FT, 1}, - }, -) where {FT} - A = Geometry.components(a) - B = Geometry.components(b) - Geometry.AxisTensor( - (Geometry.UVWAxis(), Geometry.Covariant123Axis()), - SMatrix{3, 3}( - A[1, 1], - A[2, 1], - zero(FT), - A[1, 2], - A[2, 2], - zero(FT), - zero(FT), - zero(FT), - B[1, 1], - ), - ) -end - -function product_geometry( - horizontal_local_geometry::Geometry.LocalGeometry, - vertical_local_geometry::Geometry.LocalGeometry, -) - coordinates = Geometry.product_coordinates( - horizontal_local_geometry.coordinates, - vertical_local_geometry.coordinates, - ) - J = horizontal_local_geometry.J * vertical_local_geometry.J - WJ = horizontal_local_geometry.WJ * vertical_local_geometry.WJ - ∂x∂ξ = - blockmat(horizontal_local_geometry.∂x∂ξ, vertical_local_geometry.∂x∂ξ) - return Geometry.LocalGeometry(coordinates, J, WJ, ∂x∂ξ) -end function eachslabindex(cspace::CenterExtrudedFiniteDifferenceSpace) h_iter = eachslabindex(Spaces.horizontal_space(cspace)) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 83e967c07a..a911f5406c 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -51,14 +51,10 @@ FaceFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = CenterFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(topology), CellCenter()) -FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = FiniteDifferenceSpace( - Grids.FiniteDifferenceGrid(Grids.FiniteDifferenceGrid(mesh)), - CellFace(), -) -CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = FiniteDifferenceSpace( - Grids.FiniteDifferenceGrid(Grids.FiniteDifferenceGrid(mesh)), - CellCenter(), -) +FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = + FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(mesh), CellFace()) +CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = + FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(mesh), CellCenter()) diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index fb03fe16f9..b6e8a6f328 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -16,6 +16,8 @@ nlevels(space::AbstractSpectralElementSpace) = 1 eachslabindex(space::AbstractSpectralElementSpace) = 1:Topologies.nlocalelems(Spaces.topology(space)) +staggering(space::AbstractSpectralElementSpace) = nothing + function Base.show(io::IO, space::AbstractSpectralElementSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) From c776d9227bc1717cc53f799d8a2a79e9f18de4de Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 20 Oct 2023 15:07:49 -0700 Subject: [PATCH 17/42] more --- src/Grids/spectralelement.jl | 3 ++- src/Spaces/Spaces.jl | 1 + test/Spaces/spaces.jl | 5 ++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Grids/spectralelement.jl b/src/Grids/spectralelement.jl index 7498047955..f9d6e996ed 100644 --- a/src/Grids/spectralelement.jl +++ b/src/Grids/spectralelement.jl @@ -478,7 +478,8 @@ local_geometry_data(grid::AbstractSpectralElementGrid, ::Nothing) = grid.local_geometry quadrature_style(grid::AbstractSpectralElementGrid) = grid.quadrature_style -local_dss_weights(grid::AbstractSpectralElementGrid) = grid.dss_weights +local_dss_weights(grid::SpectralElementGrid1D) = grid.dss_weights +local_dss_weights(grid::SpectralElementGrid2D) = grid.local_dss_weights diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index caf287097b..19f60ac3ef 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -29,6 +29,7 @@ import ..Grids: CellCenter, topology, local_geometry_data, + local_dss_weights, quadrature_style import ClimaComms diff --git a/test/Spaces/spaces.jl b/test/Spaces/spaces.jl index e58dfe7e95..cdc820026c 100644 --- a/test/Spaces/spaces.jl +++ b/test/Spaces/spaces.jl @@ -2,9 +2,8 @@ using Test using ClimaComms using StaticArrays, IntervalSets, LinearAlgebra -import ClimaCore: slab, Domains, Meshes, Topologies, Spaces, Fields, DataLayouts - -import ClimaCore.Geometry: Geometry +import ClimaCore: + slab, Domains, Meshes, Topologies, Spaces, Fields, DataLayouts, Geometry import ClimaCore.DataLayouts: IJFH, VF @testset "1d domain space" begin From 5bc3b9537c2686b651f568616888e1a28a8e1283 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 20 Oct 2023 16:20:42 -0700 Subject: [PATCH 18/42] more fixes --- src/Grids/finitedifference.jl | 1 + src/Operators/Operators.jl | 1 + src/Operators/finitedifference.jl | 35 ++++++++++++++++--------------- src/Spaces/Spaces.jl | 1 + src/Spaces/dss.jl | 9 ++++++-- src/Spaces/finitedifference.jl | 6 +++--- src/Topologies/dss.jl | 8 +++---- src/Topologies/dss_transform.jl | 5 ----- src/Topologies/topology2d.jl | 19 ++++++++++------- test/Topologies/rectangle.jl | 18 ++++++++++++++++ 10 files changed, 65 insertions(+), 38 deletions(-) diff --git a/src/Grids/finitedifference.jl b/src/Grids/finitedifference.jl index 2f7c346fee..d99897360e 100644 --- a/src/Grids/finitedifference.jl +++ b/src/Grids/finitedifference.jl @@ -141,6 +141,7 @@ FiniteDifferenceGrid(mesh::Meshes.IntervalMesh) = # accessors topology(grid::FiniteDifferenceGrid) = grid.topology +vertical_topology(grid::FiniteDifferenceGrid) = grid.topology local_geometry_data(grid::FiniteDifferenceGrid, ::CellCenter) = grid.center_local_geometry diff --git a/src/Operators/Operators.jl b/src/Operators/Operators.jl index 973d59b842..b6673a3ca0 100644 --- a/src/Operators/Operators.jl +++ b/src/Operators/Operators.jl @@ -13,6 +13,7 @@ import ..Geometry: Geometry, Covariant12Vector, Contravariant12Vector, ⊗ import ..Spaces: Spaces, Quadratures, AbstractSpace import ..Topologies import ..Meshes +import ..Grids import ..Fields: Fields, Field using ..RecursiveApply diff --git a/src/Operators/finitedifference.jl b/src/Operators/finitedifference.jl index a1f11c3272..fa32306a6e 100644 --- a/src/Operators/finitedifference.jl +++ b/src/Operators/finitedifference.jl @@ -22,13 +22,13 @@ right_idx(space::AllFaceFiniteDifferenceSpace) = right_face_boundary_idx(space) left_center_boundary_idx(space::AllFiniteDifferenceSpace) = 1 right_center_boundary_idx(space::AllFiniteDifferenceSpace) = size( - Spaces.local_geometry_data(Spaces.space(space, Spaces.CallCenter())), + Spaces.local_geometry_data(Spaces.space(space, Spaces.CellCenter())), 4, ) left_face_boundary_idx(space::AllFiniteDifferenceSpace) = half right_face_boundary_idx(space::AllFiniteDifferenceSpace) = size( - Spaces.local_geometry_data(Spaces.space(space, Spaces.CallFace())), + Spaces.local_geometry_data(Spaces.space(space, Spaces.CellFace())), 4, ) - half @@ -49,7 +49,8 @@ Base.@propagate_inbounds function Geometry.LocalGeometry( v = mod1(v, length(space)) end i, j, h = hidx - local_geom = Spaces.local_geometry_data(Spaces.CellCenter(), space.grid) + local_geom = + Grids.local_geometry_data(Spaces.grid(space), Grids.CellCenter()) return @inbounds local_geom[CartesianIndex(i, j, 1, v, h)] end Base.@propagate_inbounds function Geometry.LocalGeometry( @@ -62,7 +63,7 @@ Base.@propagate_inbounds function Geometry.LocalGeometry( v = mod1(v, length(space)) end i, j, h = hidx - local_geom = Spaces.local_geometry_data(Spaces.CellFace(), space.grid) + local_geom = Grids.local_geometry_data(Spaces.grid(space), Grids.CellFace()) return @inbounds local_geom[CartesianIndex(i, j, 1, v, h)] end @@ -351,7 +352,7 @@ end InterpolateF2C(; kwargs...) = InterpolateF2C(NamedTuple(kwargs)) return_space(::InterpolateF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallCenter()) + Spaces.space(space, Spaces.CellCenter()) stencil_interior_width(::InterpolateF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -587,7 +588,7 @@ end LeftBiasedF2C(; kwargs...) = LeftBiasedF2C(NamedTuple(kwargs)) return_space(::LeftBiasedF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallCenter()) + Spaces.space(space, Spaces.CellCenter()) stencil_interior_width(::LeftBiasedF2C, arg) = ((-half, -half),) Base.@propagate_inbounds stencil_interior( @@ -647,7 +648,7 @@ end LeftBiased3rdOrderC2F(; kwargs...) = LeftBiased3rdOrderC2F(NamedTuple(kwargs)) return_space(::LeftBiased3rdOrderC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallFace()) + Spaces.space(space, Spaces.CellFace()) stencil_interior_width(::LeftBiased3rdOrderC2F, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -711,7 +712,7 @@ end LeftBiased3rdOrderF2C(; kwargs...) = LeftBiased3rdOrderF2C(NamedTuple(kwargs)) return_space(::LeftBiased3rdOrderF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallCenter()) + Spaces.space(space, Spaces.CellCenter()) stencil_interior_width(::LeftBiased3rdOrderF2C, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -775,7 +776,7 @@ end RightBiasedC2F(; kwargs...) = RightBiasedC2F(NamedTuple(kwargs)) return_space(::RightBiasedC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallFace()) + Spaces.space(space, Spaces.CellFace()) stencil_interior_width(::RightBiasedC2F, arg) = ((half, half),) Base.@propagate_inbounds stencil_interior( @@ -834,7 +835,7 @@ end RightBiasedF2C(; kwargs...) = RightBiasedF2C(NamedTuple(kwargs)) return_space(::RightBiasedF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallCenter()) + Spaces.space(space, Spaces.CellCenter()) stencil_interior_width(::RightBiasedF2C, arg) = ((half, half),) Base.@propagate_inbounds stencil_interior( @@ -896,7 +897,7 @@ end RightBiased3rdOrderC2F(; kwargs...) = RightBiased3rdOrderC2F(NamedTuple(kwargs)) return_space(::RightBiased3rdOrderC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallFace()) + Spaces.space(space, Spaces.CellFace()) stencil_interior_width(::RightBiased3rdOrderC2F, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -948,7 +949,7 @@ end RightBiased3rdOrderF2C(; kwargs...) = RightBiased3rdOrderF2C(NamedTuple(kwargs)) return_space(::RightBiased3rdOrderF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallCenter()) + Spaces.space(space, Spaces.CellCenter()) stencil_interior_width(::RightBiased3rdOrderF2C, arg) = ((-half - 1, half + 1),) Base.@propagate_inbounds stencil_interior( @@ -2254,7 +2255,7 @@ end GradientF2C(; kwargs...) = GradientF2C(NamedTuple(kwargs)) return_space(::GradientF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallCenter()) + Spaces.space(space, Spaces.CellCenter()) stencil_interior_width(::GradientF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2367,7 +2368,7 @@ end GradientC2F(; kwargs...) = GradientC2F(NamedTuple(kwargs)) return_space(::GradientC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallFace()) + Spaces.space(space, Spaces.CellFace()) stencil_interior_width(::GradientC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2491,7 +2492,7 @@ end DivergenceF2C(; kwargs...) = DivergenceF2C(NamedTuple(kwargs)) return_space(::DivergenceF2C, space::AllFaceFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallCenter()) + Spaces.space(space, Spaces.CellCenter()) stencil_interior_width(::DivergenceF2C, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2623,7 +2624,7 @@ end DivergenceC2F(; kwargs...) = DivergenceC2F(NamedTuple(kwargs)) return_space(::DivergenceC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallFace()) + Spaces.space(space, Spaces.CellFace()) stencil_interior_width(::DivergenceC2F, arg) = ((-half, half),) Base.@propagate_inbounds function stencil_interior( @@ -2767,7 +2768,7 @@ end CurlC2F(; kwargs...) = CurlC2F(NamedTuple(kwargs)) return_space(::CurlC2F, space::AllCenterFiniteDifferenceSpace) = - Spaces.space(space, Spaces.CallFace()) + Spaces.space(space, Spaces.CellFace()) fd3_curl(u₊::Geometry.Covariant1Vector, u₋::Geometry.Covariant1Vector, invJ) = Geometry.Contravariant2Vector((u₊.u₁ - u₋.u₁) * invJ) diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index 19f60ac3ef..ce83bbd0b0 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -28,6 +28,7 @@ import ..Grids: CellFace, CellCenter, topology, + vertical_topology, local_geometry_data, local_dss_weights, quadrature_style diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl index 4ccdbeffbc..b41f0de70d 100644 --- a/src/Spaces/dss.jl +++ b/src/Spaces/dss.jl @@ -128,7 +128,7 @@ function weighted_dss_start!( Spaces.SpectralElementSpace2D, Spaces.ExtrudedFiniteDifferenceSpace, }, - hspace::SpectralElementSpace2D{<:Topologies.Topology2D}, + hspace::SpectralElementSpace2D, dss_buffer::DSSBuffer, ) assert_same_eltype(data, dss_buffer) @@ -154,7 +154,12 @@ function weighted_dss_start!( return nothing end -weighted_dss_start!(data, space, hspace, dss_buffer::Nothing) = nothing +weighted_dss_start!( + data, + space, + hspace::SpectralElementSpace1D, + dss_buffer::Nothing, +) = nothing """ weighted_dss_internal!( diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index a911f5406c..e3e21c5fb8 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -19,7 +19,7 @@ grid(space::AbstractFiniteDifferenceSpace) = space.grid staggering(space::FiniteDifferenceSpace) = space.staggering space(grid::Grids.AbstractFiniteDifferenceGrid, staggering::Staggering) = - FiniteDifferenceSpace(staggering, grid) + FiniteDifferenceSpace(grid, staggering) function Base.show(io::IO, space::FiniteDifferenceSpace) indent = get(io, :indent, 0) @@ -42,9 +42,9 @@ CenterFiniteDifferenceSpace(grid::Grids.AbstractFiniteDifferenceGrid) = FiniteDifferenceSpace(grid, CellCenter()) FaceFiniteDifferenceSpace(space::FiniteDifferenceSpace) = - FiniteDifferenceSpace(space, CellFace()) + FiniteDifferenceSpace(space.grid, CellFace()) CenterFiniteDifferenceSpace(space::FiniteDifferenceSpace) = - FiniteDifferenceSpace(space, CellCenter()) + FiniteDifferenceSpace(space.grid, CellCenter()) FaceFiniteDifferenceSpace(topology::Topologies.IntervalTopology) = FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(topology), CellFace()) diff --git a/src/Topologies/dss.jl b/src/Topologies/dss.jl index ba402999a1..87944b6e0a 100644 --- a/src/Topologies/dss.jl +++ b/src/Topologies/dss.jl @@ -45,13 +45,13 @@ function create_dss_buffer( data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, topology::Topology2D, local_geometry::Union{ - DataLayouts.IJFH{S, Nij}, - DataLayouts.VIJFH{S, Nij}, + DataLayouts.IJFH{<:Any, Nij}, + DataLayouts.VIJFH{<:Any, Nij}, Nothing, } = nothing, local_weights::Union{ - DataLayouts.IJFH{S, Nij}, - DataLayouts.VIJFH{S, Nij}, + DataLayouts.IJFH{<:Any, Nij}, + DataLayouts.VIJFH{<:Any, Nij}, Nothing, } = nothing, ) where {S, Nij} diff --git a/src/Topologies/dss_transform.jl b/src/Topologies/dss_transform.jl index b6b0c44d92..3bb89c9623 100644 --- a/src/Topologies/dss_transform.jl +++ b/src/Topologies/dss_transform.jl @@ -230,11 +230,6 @@ end Geometry.transform(ax, targ, local_geometry) end -function weighted_dss! end -function weighted_dss_start! end -function weighted_dss_internal! end -function weighted_dss_ghost! end - # helper functions for DSS2 function _get_idx(sizet::NTuple{5, Int}, loc::NTuple{5, Int}) (n1, n2, n3, n4, n5) = sizet diff --git a/src/Topologies/topology2d.jl b/src/Topologies/topology2d.jl index 06f31fa01d..8607029ba3 100644 --- a/src/Topologies/topology2d.jl +++ b/src/Topologies/topology2d.jl @@ -733,30 +733,35 @@ Construct a perimeter iterator for a 2D spectral element with `Nq` nodes per dimension (i.e. polynomial degree `Nq-1`). """ Perimeter2D(Nq) = Perimeter2D{Nq}() + Adapt.adapt_structure(to, x::Perimeter2D) = x -Base.length(::Perimeter2D{Nq}) where {Nq} = 4 + (Nq - 1) * 4 +Base.IteratorEltype(::Type{Perimeter2D{Nq}}) where {Nq} = Base.HasEltype() +Base.eltype(::Type{Perimeter2D{Nq}}) where {Nq} = Tuple{Int, Int} + +Base.IteratorSize(::Type{Perimeter2D{Nq}}) where {Nq} = Base.HasLength() +Base.length(::Perimeter2D{Nq}) where {Nq} = 4 + (Nq - 2) * 4 function Base.iterate(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} - if loc <= 5 + if loc <= 4 return (vertex_node_index(loc, Nq), loc + 1) elseif loc ≤ length(perimeter) f = cld(loc - 4, Nq - 2) n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) - return (Topologies.face_node_index(f, Nq, 1 + n), loc + 1) + return (face_node_index(f, Nq, 1 + n), loc + 1) else return nothing end end function Base.getindex(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} - if loc < 1 || loc > nperimeter2d(Nq) + if loc < 1 || loc > length(perimeter) return (-1, -1) - elseif loc < 5 - return Topologies.vertex_node_index(loc, Nq) + elseif loc <= 4 + return vertex_node_index(loc, Nq) else f = cld(loc - 4, Nq - 2) n = mod(loc - 4, Nq - 2) == 0 ? (Nq - 2) : mod(loc - 4, Nq - 2) - return Topologies.face_node_index(f, Nq, 1 + n) + return face_node_index(f, Nq, 1 + n) end end diff --git a/test/Topologies/rectangle.jl b/test/Topologies/rectangle.jl index a210951ca4..6e1fb0037d 100644 --- a/test/Topologies/rectangle.jl +++ b/test/Topologies/rectangle.jl @@ -5,6 +5,24 @@ import ClimaCore.Geometry: Geometry using StaticArrays using IntervalSets +@testset "Perimeter2D iterator" begin + @test collect(Topologies.Perimeter2D(4)) == [ + (1, 1) + (4, 1) + (4, 4) + (1, 4) + (2, 1) + (3, 1) + (4, 2) + (4, 3) + (3, 4) + (2, 4) + (1, 3) + (1, 2) + ] +end + + function rectangular_grid( n1, n2, From 12205931cab59fd80c818d48646df6ef4150667d Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 23 Oct 2023 11:28:51 -0700 Subject: [PATCH 19/42] format --- src/Operators/integrals.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Operators/integrals.jl b/src/Operators/integrals.jl index d44586cb2b..3343a9efda 100644 --- a/src/Operators/integrals.jl +++ b/src/Operators/integrals.jl @@ -304,7 +304,5 @@ right_boundary_idx(n, ::Operators.FacePlaceholderSpace) = n - Utilities.half left_boundary_idx(n, ::Spaces.CenterFiniteDifferenceSpace) = 1 right_boundary_idx(n, ::Spaces.CenterFiniteDifferenceSpace) = n -left_boundary_idx(n, ::Spaces.FaceFiniteDifferenceSpace) = - Utilities.half -right_boundary_idx(n, ::Spaces.FaceFiniteDifferenceSpace) = - n - Utilities.half +left_boundary_idx(n, ::Spaces.FaceFiniteDifferenceSpace) = Utilities.half +right_boundary_idx(n, ::Spaces.FaceFiniteDifferenceSpace) = n - Utilities.half From d2bf29119c8816d298b92572564c8ccc9664fcc0 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 23 Oct 2023 15:51:32 -0700 Subject: [PATCH 20/42] upgrade subpackages --- examples/Manifest.toml | 69 ++++++++++++++++-------- lib/ClimaCorePlots/src/ClimaCorePlots.jl | 9 +--- lib/ClimaCoreVTK/src/space.jl | 11 ++-- src/Fields/Fields.jl | 3 +- src/Grids/spectralelement.jl | 15 +++++- src/Spaces/extruded.jl | 27 +++++++--- src/Spaces/spectralelement.jl | 5 ++ 7 files changed, 90 insertions(+), 49 deletions(-) diff --git a/examples/Manifest.toml b/examples/Manifest.toml index 78ac4519bd..26ec797a35 100644 --- a/examples/Manifest.toml +++ b/examples/Manifest.toml @@ -33,9 +33,9 @@ version = "0.4.4" [[deps.Adapt]] deps = ["LinearAlgebra", "Requires"] -git-tree-sha1 = "76289dc51920fdc6e0013c872ba9551d54961c24" +git-tree-sha1 = "68c4c187a232e7abe00ac29e3b03e09af9d77317" uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" -version = "3.6.2" +version = "3.7.0" weakdeps = ["StaticArrays"] [deps.Adapt.extensions] @@ -211,9 +211,9 @@ version = "0.2.2" [[deps.CUDA_Runtime_jll]] deps = ["Artifacts", "CUDA_Driver_jll", "JLLWrappers", "LazyArtifacts", "Libdl", "TOML"] -git-tree-sha1 = "105eb8cf6ec6e8b93493da42ec789c3f65f7d749" +git-tree-sha1 = "a38bc81bd6fdc7334de537aec3ae60e7b098daa2" uuid = "76a88914-d11a-5bdc-97e0-2f5a05c973a2" -version = "0.9.2+3" +version = "0.9.2+4" [[deps.Cairo_jll]] deps = ["Artifacts", "Bzip2_jll", "CompilerSupportLibraries_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"] @@ -238,10 +238,10 @@ uuid = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d" version = "0.5.5" [[deps.ClimaCore]] -deps = ["Adapt", "BandedMatrices", "BlockArrays", "CUDA", "ClimaComms", "CubedSphere", "DataStructures", "DocStringExtensions", "ForwardDiff", "GaussQuadrature", "GilbertCurves", "HDF5", "InteractiveUtils", "IntervalSets", "LinearAlgebra", "PkgVersion", "RecursiveArrayTools", "Requires", "RootSolvers", "SparseArrays", "Static", "StaticArrays", "Statistics", "UnPack"] +deps = ["Adapt", "BandedMatrices", "BlockArrays", "CUDA", "ClimaComms", "CubedSphere", "DataStructures", "DocStringExtensions", "ForwardDiff", "GaussQuadrature", "GilbertCurves", "HDF5", "InteractiveUtils", "IntervalSets", "LinearAlgebra", "Memoize", "PkgVersion", "RecursiveArrayTools", "Requires", "RootSolvers", "SparseArrays", "Static", "StaticArrays", "Statistics", "UnPack", "WeakValueDicts"] path = ".." uuid = "d414da3d-4745-48bb-8d80-42e94e092884" -version = "0.10.53" +version = "0.10.54" [[deps.ClimaCorePlots]] deps = ["ClimaCore", "RecipesBase", "StaticArrays", "TriplotBase"] @@ -841,9 +841,9 @@ uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" [[deps.IntervalSets]] deps = ["Dates", "Random"] -git-tree-sha1 = "8e59ea773deee525c99a8018409f64f19fb719e6" +git-tree-sha1 = "3d8866c029dd6b16e69e0d4a939c4dfcb98fac47" uuid = "8197267c-284f-5f27-9208-e0e47529a953" -version = "0.7.7" +version = "0.7.8" weakdeps = ["Statistics"] [deps.IntervalSets.extensions] @@ -865,10 +865,10 @@ uuid = "82899510-4779-5014-852e-03e436cf321d" version = "1.0.0" [[deps.JLD2]] -deps = ["FileIO", "MacroTools", "Mmap", "OrderedCollections", "Pkg", "Printf", "Reexport", "Requires", "TranscodingStreams", "UUIDs"] -git-tree-sha1 = "572d024660119ee626938419c14db0db5f3f3283" +deps = ["FileIO", "MacroTools", "Mmap", "OrderedCollections", "Pkg", "PrecompileTools", "Printf", "Reexport", "Requires", "TranscodingStreams", "UUIDs"] +git-tree-sha1 = "ebec83429b5dea3857e071d927156207ebd6d617" uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819" -version = "0.4.36" +version = "0.4.37" [[deps.JLFzf]] deps = ["Pipe", "REPL", "Random", "fzf_jll"] @@ -1099,11 +1099,12 @@ version = "2.5.2" [[deps.LinearSolve]] deps = ["ArrayInterface", "ConcreteStructs", "DocStringExtensions", "EnumX", "EnzymeCore", "FastLapackInterface", "GPUArraysCore", "InteractiveUtils", "KLU", "Krylov", "Libdl", "LinearAlgebra", "MKL_jll", "PrecompileTools", "Preferences", "RecursiveFactorization", "Reexport", "Requires", "SciMLBase", "SciMLOperators", "Setfield", "SparseArrays", "Sparspak", "SuiteSparse", "UnPack"] -git-tree-sha1 = "9f27ba34f5821a0495efb09ea3a465c31326495a" +git-tree-sha1 = "158e45dd35cec1ecade0e554c0104ee89e772d82" uuid = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae" -version = "2.10.0" +version = "2.11.1" [deps.LinearSolve.extensions] + LinearSolveBandedMatricesExt = "BandedMatrices" LinearSolveBlockDiagonalsExt = "BlockDiagonals" LinearSolveCUDAExt = "CUDA" LinearSolveEnzymeExt = "Enzyme" @@ -1113,8 +1114,10 @@ version = "2.10.0" LinearSolveKrylovKitExt = "KrylovKit" LinearSolveMetalExt = "Metal" LinearSolvePardisoExt = "Pardiso" + LinearSolveRecursiveArrayToolsExt = "RecursiveArrayTools" [deps.LinearSolve.weakdeps] + BandedMatrices = "aae01518-5342-5314-be14-df237901396f" BlockDiagonals = "0a1fb500-61f7-11e9-3c65-f5ef3456f9f0" CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" @@ -1124,6 +1127,7 @@ version = "2.10.0" KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77" Metal = "dde4c033-4e86-420c-a63e-0dd931031962" Pardiso = "46dd5b70-b6fb-5a00-ae2d-e8fea33afaf2" + RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" [[deps.LogExpFunctions]] deps = ["DocStringExtensions", "IrrationalConstants", "LinearAlgebra"] @@ -1222,6 +1226,12 @@ git-tree-sha1 = "c13304c81eec1ed3af7fc20e75fb6b26092a1102" uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e" version = "0.3.2" +[[deps.Memoize]] +deps = ["MacroTools"] +git-tree-sha1 = "2b1dfcba103de714d31c033b5dacc2e4a12c7caa" +uuid = "c03570c3-d221-55d1-a50c-7939bbd78826" +version = "0.4.4" + [[deps.MicroCollections]] deps = ["BangBang", "InitialValues", "Setfield"] git-tree-sha1 = "629afd7d10dbc6935ec59b32daeb33bc4460a42e" @@ -1300,9 +1310,17 @@ version = "1.2.0" [[deps.NonlinearSolve]] deps = ["ADTypes", "ArrayInterface", "ConcreteStructs", "DiffEqBase", "EnumX", "FiniteDiff", "ForwardDiff", "LineSearches", "LinearAlgebra", "LinearSolve", "PrecompileTools", "RecursiveArrayTools", "Reexport", "SciMLBase", "SimpleNonlinearSolve", "SparseArrays", "SparseDiffTools", "StaticArraysCore", "UnPack"] -git-tree-sha1 = "a5f1f836da05d513c4143576af8f5d8e51b759f5" +git-tree-sha1 = "9203b3333c9610664de2e8cbc23cfd726663df7d" uuid = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" -version = "2.2.1" +version = "2.4.0" + + [deps.NonlinearSolve.extensions] + NonlinearSolveFastLevenbergMarquardtExt = "FastLevenbergMarquardt" + NonlinearSolveLeastSquaresOptimExt = "LeastSquaresOptim" + + [deps.NonlinearSolve.weakdeps] + FastLevenbergMarquardt = "7a0df574-e128-4d35-8cbd-3d84502bf7ce" + LeastSquaresOptim = "0fc2ff8b-aaa3-5acd-a817-1944a5e08891" [[deps.OffsetArrays]] deps = ["Adapt"] @@ -1560,9 +1578,9 @@ version = "0.6.12" [[deps.RecursiveArrayTools]] deps = ["Adapt", "ArrayInterface", "DocStringExtensions", "GPUArraysCore", "IteratorInterfaceExtensions", "LinearAlgebra", "RecipesBase", "Requires", "StaticArraysCore", "Statistics", "SymbolicIndexingInterface", "Tables"] -git-tree-sha1 = "fa453b42ba1623bd2e70260bf44dac850a3430a7" +git-tree-sha1 = "d7087c013e8a496ff396bae843b1e16d9a30ede8" uuid = "731186ca-8d62-57ce-b412-fbd966d074cd" -version = "2.39.0" +version = "2.38.10" [deps.RecursiveArrayTools.extensions] RecursiveArrayToolsMeasurementsExt = "Measurements" @@ -1634,9 +1652,9 @@ version = "0.6.39" [[deps.SciMLBase]] deps = ["ADTypes", "ArrayInterface", "ChainRulesCore", "CommonSolve", "ConstructionBase", "Distributed", "DocStringExtensions", "EnumX", "FillArrays", "FunctionWrappersWrappers", "IteratorInterfaceExtensions", "LinearAlgebra", "Logging", "Markdown", "PrecompileTools", "Preferences", "RecipesBase", "RecursiveArrayTools", "Reexport", "RuntimeGeneratedFunctions", "SciMLOperators", "StaticArraysCore", "Statistics", "SymbolicIndexingInterface", "Tables", "TruncatedStacktraces", "ZygoteRules"] -git-tree-sha1 = "317f77cb31f7a0275cdd045aa7b3526ebc15c817" +git-tree-sha1 = "151c322c309d879d114d1c0bee69c61d5933357f" uuid = "0bca4576-84f4-4d90-8ffe-ffa030f20462" -version = "2.4.0" +version = "2.4.3" [deps.SciMLBase.extensions] SciMLBasePyCallExt = "PyCall" @@ -1700,9 +1718,9 @@ version = "1.1.0" [[deps.SimpleNonlinearSolve]] deps = ["ArrayInterface", "DiffEqBase", "FiniteDiff", "ForwardDiff", "LinearAlgebra", "PackageExtensionCompat", "PrecompileTools", "Reexport", "SciMLBase", "StaticArraysCore"] -git-tree-sha1 = "e308d089f5d0e733a017b61784c5813e672f760d" +git-tree-sha1 = "15ff97fa4881133caa324dacafe28b5ac47ad8a2" uuid = "727e6d20-b764-4bd8-a329-72de5adea6c7" -version = "0.1.22" +version = "0.1.23" [deps.SimpleNonlinearSolve.extensions] SimpleNonlinearSolveNNlibExt = "NNlib" @@ -1867,9 +1885,9 @@ version = "1.0.1" [[deps.Tables]] deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "OrderedCollections", "TableTraits"] -git-tree-sha1 = "a1f34829d5ac0ef499f6d84428bd6b4c71f02ead" +git-tree-sha1 = "cb76cf677714c095e535e3501ac7954732aeea2d" uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" -version = "1.11.0" +version = "1.11.1" [[deps.Tar]] deps = ["ArgTools", "SHA"] @@ -2070,6 +2088,11 @@ git-tree-sha1 = "4528479aa01ee1b3b4cd0e6faef0e04cf16466da" uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91" version = "1.25.0+0" +[[deps.WeakValueDicts]] +git-tree-sha1 = "98528c2610a5479f091d470967a25becfd83edd0" +uuid = "897b6980-f191-5a31-bcb0-bf3c4585e0c1" +version = "0.1.0" + [[deps.WriteVTK]] deps = ["Base64", "CodecZlib", "FillArrays", "LightXML", "TranscodingStreams", "VTKBase"] git-tree-sha1 = "41f0dc2a8f6fd860c266b91fd5cdf4fead65ae69" diff --git a/lib/ClimaCorePlots/src/ClimaCorePlots.jl b/lib/ClimaCorePlots/src/ClimaCorePlots.jl index 00e9e23dba..86f67c893c 100644 --- a/lib/ClimaCorePlots/src/ClimaCorePlots.jl +++ b/lib/ClimaCorePlots/src/ClimaCorePlots.jl @@ -251,12 +251,7 @@ RecipesBase.@recipe function f( field::Fields.Field{<:Any, S}; hinterpolate = 0, ncolors = 256, -) where { - S <: Spaces.ExtrudedFiniteDifferenceSpace{ - <:Any, - <:Spaces.IntervalSpectralElementSpace1D, - }, -} +) where {S <: Spaces.ExtrudedFiniteDifferenceSpace2D} hcoord, vcoord, data = _slice_triplot(field, hinterpolate, ncolors) coord_symbols = propertynames(Fields.coordinate_field(axes(field))) @@ -364,7 +359,7 @@ RecipesBase.@recipe function f( hinterpolate = 0, ncolors = 256, ) where { - S <: Spaces.ExtrudedFiniteDifferenceSpace{ + S <: Spaces.ExtrudedFiniteDifferenceSpace3D{ <:Any, <:Spaces.RectilinearSpectralElementSpace2D, }, diff --git a/lib/ClimaCoreVTK/src/space.jl b/lib/ClimaCoreVTK/src/space.jl index 884b31da72..14e50bb438 100644 --- a/lib/ClimaCoreVTK/src/space.jl +++ b/lib/ClimaCoreVTK/src/space.jl @@ -83,13 +83,10 @@ Construct a vector of `MeshCell` objects representing the elements of `space` as an unstuctured mesh of Lagrange polynomial cells, suitable for passing to `vtk_grid`. """ -function vtk_cells_lagrange( - gspace::Spaces.SpectralElementSpace2D{ - T, - Spaces.Quadratures.ClosedUniform{Nq}, - }, -) where {T, Nq} - # TODO: this should depend on the backing DataLayouts (e.g. IJFH) +function vtk_cells_lagrange(gspace::Spaces.SpectralElementSpace2D) + quad = Spaces.quadrature_style(gspace) + @assert quad isa Quadratures.ClosedUniform + Nq = Quadratures.degrees_of_freedom(quad) # TODO: this should depend on the backing DataLayouts (e.g. IJFH) con_map = vtk_connectivity_map_lagrange(Nq, Nq) [ MeshCell( diff --git a/src/Fields/Fields.jl b/src/Fields/Fields.jl index 311aa28b6e..95231aa4b4 100644 --- a/src/Fields/Fields.jl +++ b/src/Fields/Fields.jl @@ -87,12 +87,11 @@ const CenterExtrudedFiniteDifferenceField{V, S} = Field{ } where {V <: AbstractData, S <: Spaces.CenterExtrudedFiniteDifferenceSpace} # Cubed Sphere Fields -#= + const CubedSphereSpectralElementField2D{V, S} = Field{ V, S, } where {V <: AbstractData, S <: Spaces.CubedSphereSpectralElementSpace2D} -=# Base.propertynames(field::Field) = propertynames(getfield(field, :values)) @inline field_values(field::Field) = getfield(field, :values) diff --git a/src/Grids/spectralelement.jl b/src/Grids/spectralelement.jl index f9d6e996ed..e0544c02c2 100644 --- a/src/Grids/spectralelement.jl +++ b/src/Grids/spectralelement.jl @@ -105,8 +105,6 @@ mutable struct SpectralElementGrid2D{ end - - """ SpectralElementSpace2D(topology, quadrature_style; enable_bubble) @@ -482,6 +480,19 @@ local_dss_weights(grid::SpectralElementGrid1D) = grid.dss_weights local_dss_weights(grid::SpectralElementGrid2D) = grid.local_dss_weights +const RectilinearSpectralElementGrid2D = SpectralElementGrid2D{ + <:Topologies.Topology2D{ + <:ClimaComms.AbstractCommsContext, + <:Meshes.RectilinearMesh, + }, +} + +const CubedSphereSpectralElementGrid2D = SpectralElementGrid2D{ + <:Topologies.Topology2D{ + <:ClimaComms.AbstractCommsContext, + <:Meshes.AbstractCubedSphere, + }, +} ## GPU compatibility diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index d0e68a12dc..2e2405e567 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -81,17 +81,28 @@ end Adapt.adapt_structure(to, space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace(Adapt.adapt(to, space.grid), space.staggering) -#= +const ExtrudedFiniteDifferenceSpace2D = ExtrudedFiniteDifferenceSpace{ + <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid1D}, +} +const ExtrudedFiniteDifferenceSpace3D = ExtrudedFiniteDifferenceSpace{ + <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid1D}, +} const CenterExtrudedFiniteDifferenceSpace2D = - CenterExtrudedFiniteDifferenceSpace{<:SpectralElementSpace1D} + CenterExtrudedFiniteDifferenceSpace{ + <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid1D}, + } const CenterExtrudedFiniteDifferenceSpace3D = - CenterExtrudedFiniteDifferenceSpace{<:SpectralElementSpace2D} -const FaceExtrudedFiniteDifferenceSpace2D = - FaceExtrudedFiniteDifferenceSpace{<:SpectralElementSpace1D} -const FaceExtrudedFiniteDifferenceSpace3D = - FaceExtrudedFiniteDifferenceSpace{<:SpectralElementSpace2D} -=# + CenterExtrudedFiniteDifferenceSpace{ + <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid2D}, + } +const FaceExtrudedFiniteDifferenceSpace2D = FaceExtrudedFiniteDifferenceSpace{ + <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid1D}, +} +const FaceExtrudedFiniteDifferenceSpace3D = FaceExtrudedFiniteDifferenceSpace{ + <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid2D}, +} + function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) indent = get(io, :indent, 0) iio = IOContext(io, :indent => indent + 2) diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index b6e8a6f328..2efe9b7025 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -71,6 +71,11 @@ end +const RectilinearSpectralElementSpace2D = + SpectralElementSpace2D{<:Grids.RectilinearSpectralElementGrid2D} +const CubedSphereSpectralElementSpace2D = + SpectralElementSpace2D{<:Grids.CubedSphereSpectralElementGrid2D} + From c97208d30bfe37a495eece525f54b903b898ab6c Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 23 Oct 2023 16:01:29 -0700 Subject: [PATCH 21/42] update manifests --- .dev/Manifest.toml | 4 +-- docs/Manifest.toml | 71 ++++++++++++++++++++++++++++++---------------- perf/Manifest.toml | 45 ++++++++++++++++++----------- 3 files changed, 77 insertions(+), 43 deletions(-) diff --git a/.dev/Manifest.toml b/.dev/Manifest.toml index ba35806a3a..2e73e1e3ba 100644 --- a/.dev/Manifest.toml +++ b/.dev/Manifest.toml @@ -79,9 +79,9 @@ version = "0.21.4" [[deps.JuliaFormatter]] deps = ["CSTParser", "CommonMark", "DataStructures", "Glob", "Pkg", "PrecompileTools", "Tokenize"] -git-tree-sha1 = "80031f6e58b09b0de4553bf63d9a36ec5db57967" +git-tree-sha1 = "0bac3374ff3aa798148669ecb5559ba20c8b0e73" uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899" -version = "1.0.39" +version = "1.0.40" [[deps.LibCURL]] deps = ["LibCURL_jll", "MozillaCACerts_jll"] diff --git a/docs/Manifest.toml b/docs/Manifest.toml index f0838997b9..a38afa2290 100644 --- a/docs/Manifest.toml +++ b/docs/Manifest.toml @@ -37,9 +37,9 @@ version = "0.4.4" [[deps.Adapt]] deps = ["LinearAlgebra", "Requires"] -git-tree-sha1 = "76289dc51920fdc6e0013c872ba9551d54961c24" +git-tree-sha1 = "68c4c187a232e7abe00ac29e3b03e09af9d77317" uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" -version = "3.6.2" +version = "3.7.0" weakdeps = ["StaticArrays"] [deps.Adapt.extensions] @@ -241,9 +241,9 @@ version = "0.2.2" [[deps.CUDA_Runtime_jll]] deps = ["Artifacts", "CUDA_Driver_jll", "JLLWrappers", "LazyArtifacts", "Libdl", "TOML"] -git-tree-sha1 = "105eb8cf6ec6e8b93493da42ec789c3f65f7d749" +git-tree-sha1 = "a38bc81bd6fdc7334de537aec3ae60e7b098daa2" uuid = "76a88914-d11a-5bdc-97e0-2f5a05c973a2" -version = "0.9.2+3" +version = "0.9.2+4" [[deps.Cairo_jll]] deps = ["Artifacts", "Bzip2_jll", "CompilerSupportLibraries_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"] @@ -274,10 +274,10 @@ uuid = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d" version = "0.5.5" [[deps.ClimaCore]] -deps = ["Adapt", "BandedMatrices", "BlockArrays", "CUDA", "ClimaComms", "CubedSphere", "DataStructures", "DocStringExtensions", "ForwardDiff", "GaussQuadrature", "GilbertCurves", "HDF5", "InteractiveUtils", "IntervalSets", "LinearAlgebra", "PkgVersion", "RecursiveArrayTools", "Requires", "RootSolvers", "SparseArrays", "Static", "StaticArrays", "Statistics", "UnPack"] +deps = ["Adapt", "BandedMatrices", "BlockArrays", "CUDA", "ClimaComms", "CubedSphere", "DataStructures", "DocStringExtensions", "ForwardDiff", "GaussQuadrature", "GilbertCurves", "HDF5", "InteractiveUtils", "IntervalSets", "LinearAlgebra", "Memoize", "PkgVersion", "RecursiveArrayTools", "Requires", "RootSolvers", "SparseArrays", "Static", "StaticArrays", "Statistics", "UnPack", "WeakValueDicts"] path = ".." uuid = "d414da3d-4745-48bb-8d80-42e94e092884" -version = "0.10.53" +version = "0.10.54" [[deps.ClimaCoreMakie]] deps = ["ClimaCore", "Makie"] @@ -549,9 +549,9 @@ version = "0.9.3" [[deps.Documenter]] deps = ["ANSIColoredPrinters", "AbstractTrees", "Base64", "Dates", "DocStringExtensions", "Downloads", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "MarkdownAST", "Pkg", "PrecompileTools", "REPL", "RegistryInstances", "SHA", "Test", "Unicode"] -git-tree-sha1 = "147a3cbb6ddcd9448fe5e6c426b347efc68f9c86" +git-tree-sha1 = "662fb21ae7fad33e044c2b59ece832fdce32c171" uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4" -version = "1.1.1" +version = "1.1.2" [[deps.DocumenterCitations]] deps = ["AbstractTrees", "Bibliography", "Documenter", "Markdown", "MarkdownAST", "OrderedCollections", "Unicode"] @@ -1025,9 +1025,9 @@ version = "0.20.9" [[deps.IntervalSets]] deps = ["Dates", "Random"] -git-tree-sha1 = "8e59ea773deee525c99a8018409f64f19fb719e6" +git-tree-sha1 = "3d8866c029dd6b16e69e0d4a939c4dfcb98fac47" uuid = "8197267c-284f-5f27-9208-e0e47529a953" -version = "0.7.7" +version = "0.7.8" weakdeps = ["Statistics"] [deps.IntervalSets.extensions] @@ -1310,11 +1310,12 @@ version = "0.1.12" [[deps.LinearSolve]] deps = ["ArrayInterface", "ConcreteStructs", "DocStringExtensions", "EnumX", "EnzymeCore", "FastLapackInterface", "GPUArraysCore", "InteractiveUtils", "KLU", "Krylov", "Libdl", "LinearAlgebra", "MKL_jll", "PrecompileTools", "Preferences", "RecursiveFactorization", "Reexport", "Requires", "SciMLBase", "SciMLOperators", "Setfield", "SparseArrays", "Sparspak", "SuiteSparse", "UnPack"] -git-tree-sha1 = "9f27ba34f5821a0495efb09ea3a465c31326495a" +git-tree-sha1 = "158e45dd35cec1ecade0e554c0104ee89e772d82" uuid = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae" -version = "2.10.0" +version = "2.11.1" [deps.LinearSolve.extensions] + LinearSolveBandedMatricesExt = "BandedMatrices" LinearSolveBlockDiagonalsExt = "BlockDiagonals" LinearSolveCUDAExt = "CUDA" LinearSolveEnzymeExt = "Enzyme" @@ -1324,8 +1325,10 @@ version = "2.10.0" LinearSolveKrylovKitExt = "KrylovKit" LinearSolveMetalExt = "Metal" LinearSolvePardisoExt = "Pardiso" + LinearSolveRecursiveArrayToolsExt = "RecursiveArrayTools" [deps.LinearSolve.weakdeps] + BandedMatrices = "aae01518-5342-5314-be14-df237901396f" BlockDiagonals = "0a1fb500-61f7-11e9-3c65-f5ef3456f9f0" CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" @@ -1335,6 +1338,7 @@ version = "2.10.0" KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77" Metal = "dde4c033-4e86-420c-a63e-0dd931031962" Pardiso = "46dd5b70-b6fb-5a00-ae2d-e8fea33afaf2" + RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" [[deps.Literate]] deps = ["Base64", "IOCapture", "JSON", "REPL"] @@ -1473,6 +1477,12 @@ git-tree-sha1 = "c13304c81eec1ed3af7fc20e75fb6b26092a1102" uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e" version = "0.3.2" +[[deps.Memoize]] +deps = ["MacroTools"] +git-tree-sha1 = "2b1dfcba103de714d31c033b5dacc2e4a12c7caa" +uuid = "c03570c3-d221-55d1-a50c-7939bbd78826" +version = "0.4.4" + [[deps.MicrosoftMPI_jll]] deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] git-tree-sha1 = "a7023883872e52bc29bcaac74f19adf39347d2d5" @@ -1567,9 +1577,17 @@ version = "1.2.0" [[deps.NonlinearSolve]] deps = ["ADTypes", "ArrayInterface", "ConcreteStructs", "DiffEqBase", "EnumX", "FiniteDiff", "ForwardDiff", "LineSearches", "LinearAlgebra", "LinearSolve", "PrecompileTools", "RecursiveArrayTools", "Reexport", "SciMLBase", "SimpleNonlinearSolve", "SparseArrays", "SparseDiffTools", "StaticArraysCore", "UnPack"] -git-tree-sha1 = "a5f1f836da05d513c4143576af8f5d8e51b759f5" +git-tree-sha1 = "9203b3333c9610664de2e8cbc23cfd726663df7d" uuid = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" -version = "2.2.1" +version = "2.4.0" + + [deps.NonlinearSolve.extensions] + NonlinearSolveFastLevenbergMarquardtExt = "FastLevenbergMarquardt" + NonlinearSolveLeastSquaresOptimExt = "LeastSquaresOptim" + + [deps.NonlinearSolve.weakdeps] + FastLevenbergMarquardt = "7a0df574-e128-4d35-8cbd-3d84502bf7ce" + LeastSquaresOptim = "0fc2ff8b-aaa3-5acd-a817-1944a5e08891" [[deps.Observables]] git-tree-sha1 = "6862738f9796b3edc1c09d0890afce4eca9e7e93" @@ -1936,9 +1954,9 @@ version = "0.6.12" [[deps.RecursiveArrayTools]] deps = ["Adapt", "ArrayInterface", "DocStringExtensions", "GPUArraysCore", "IteratorInterfaceExtensions", "LinearAlgebra", "RecipesBase", "Requires", "StaticArraysCore", "Statistics", "SymbolicIndexingInterface", "Tables"] -git-tree-sha1 = "fa453b42ba1623bd2e70260bf44dac850a3430a7" +git-tree-sha1 = "d7087c013e8a496ff396bae843b1e16d9a30ede8" uuid = "731186ca-8d62-57ce-b412-fbd966d074cd" -version = "2.39.0" +version = "2.38.10" [deps.RecursiveArrayTools.extensions] RecursiveArrayToolsMeasurementsExt = "Measurements" @@ -2033,9 +2051,9 @@ version = "0.6.39" [[deps.SciMLBase]] deps = ["ADTypes", "ArrayInterface", "ChainRulesCore", "CommonSolve", "ConstructionBase", "Distributed", "DocStringExtensions", "EnumX", "FillArrays", "FunctionWrappersWrappers", "IteratorInterfaceExtensions", "LinearAlgebra", "Logging", "Markdown", "PrecompileTools", "Preferences", "RecipesBase", "RecursiveArrayTools", "Reexport", "RuntimeGeneratedFunctions", "SciMLOperators", "StaticArraysCore", "Statistics", "SymbolicIndexingInterface", "Tables", "TruncatedStacktraces", "ZygoteRules"] -git-tree-sha1 = "317f77cb31f7a0275cdd045aa7b3526ebc15c817" +git-tree-sha1 = "151c322c309d879d114d1c0bee69c61d5933357f" uuid = "0bca4576-84f4-4d90-8ffe-ffa030f20462" -version = "2.4.0" +version = "2.4.3" [deps.SciMLBase.extensions] SciMLBasePyCallExt = "PyCall" @@ -2122,9 +2140,9 @@ version = "0.8.4" [[deps.SimpleNonlinearSolve]] deps = ["ArrayInterface", "DiffEqBase", "FiniteDiff", "ForwardDiff", "LinearAlgebra", "PackageExtensionCompat", "PrecompileTools", "Reexport", "SciMLBase", "StaticArraysCore"] -git-tree-sha1 = "e308d089f5d0e733a017b61784c5813e672f760d" +git-tree-sha1 = "15ff97fa4881133caa324dacafe28b5ac47ad8a2" uuid = "727e6d20-b764-4bd8-a329-72de5adea6c7" -version = "0.1.22" +version = "0.1.23" [deps.SimpleNonlinearSolve.extensions] SimpleNonlinearSolveNNlibExt = "NNlib" @@ -2220,9 +2238,9 @@ weakdeps = ["ChainRulesCore"] [[deps.StableHashTraits]] deps = ["Compat", "SHA", "Tables", "TupleTools"] -git-tree-sha1 = "19df33ca14f24a3ad2df9e89124bd5f5cc8467a2" +git-tree-sha1 = "30edbce1c797dc7d4c74bc07b2b6a57b891bead3" uuid = "c5dd0088-6c3f-4803-b00e-f31a60c170fa" -version = "1.0.1" +version = "1.1.0" [[deps.StackViews]] deps = ["OffsetArrays"] @@ -2351,9 +2369,9 @@ version = "1.0.1" [[deps.Tables]] deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "OrderedCollections", "TableTraits"] -git-tree-sha1 = "a1f34829d5ac0ef499f6d84428bd6b4c71f02ead" +git-tree-sha1 = "cb76cf677714c095e535e3501ac7954732aeea2d" uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" -version = "1.11.0" +version = "1.11.1" [[deps.Tar]] deps = ["ArgTools", "SHA"] @@ -2531,6 +2549,11 @@ git-tree-sha1 = "4528479aa01ee1b3b4cd0e6faef0e04cf16466da" uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91" version = "1.25.0+0" +[[deps.WeakValueDicts]] +git-tree-sha1 = "98528c2610a5479f091d470967a25becfd83edd0" +uuid = "897b6980-f191-5a31-bcb0-bf3c4585e0c1" +version = "0.1.0" + [[deps.WoodburyMatrices]] deps = ["LinearAlgebra", "SparseArrays"] git-tree-sha1 = "de67fa59e33ad156a590055375a30b23c40299d3" diff --git a/perf/Manifest.toml b/perf/Manifest.toml index e65f98ec5e..bba45206c3 100644 --- a/perf/Manifest.toml +++ b/perf/Manifest.toml @@ -33,9 +33,9 @@ version = "0.4.4" [[deps.Adapt]] deps = ["LinearAlgebra", "Requires"] -git-tree-sha1 = "76289dc51920fdc6e0013c872ba9551d54961c24" +git-tree-sha1 = "68c4c187a232e7abe00ac29e3b03e09af9d77317" uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" -version = "3.6.2" +version = "3.7.0" weakdeps = ["StaticArrays"] [deps.Adapt.extensions] @@ -190,9 +190,9 @@ version = "0.2.2" [[deps.CUDA_Runtime_jll]] deps = ["Artifacts", "CUDA_Driver_jll", "JLLWrappers", "LazyArtifacts", "Libdl", "TOML"] -git-tree-sha1 = "105eb8cf6ec6e8b93493da42ec789c3f65f7d749" +git-tree-sha1 = "a38bc81bd6fdc7334de537aec3ae60e7b098daa2" uuid = "76a88914-d11a-5bdc-97e0-2f5a05c973a2" -version = "0.9.2+3" +version = "0.9.2+4" [[deps.Cairo_jll]] deps = ["Artifacts", "Bzip2_jll", "CompilerSupportLibraries_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"] @@ -217,10 +217,10 @@ uuid = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d" version = "0.5.5" [[deps.ClimaCore]] -deps = ["Adapt", "BandedMatrices", "BlockArrays", "CUDA", "ClimaComms", "CubedSphere", "DataStructures", "DocStringExtensions", "ForwardDiff", "GaussQuadrature", "GilbertCurves", "HDF5", "InteractiveUtils", "IntervalSets", "LinearAlgebra", "PkgVersion", "RecursiveArrayTools", "Requires", "RootSolvers", "SparseArrays", "Static", "StaticArrays", "Statistics", "UnPack"] +deps = ["Adapt", "BandedMatrices", "BlockArrays", "CUDA", "ClimaComms", "CubedSphere", "DataStructures", "DocStringExtensions", "ForwardDiff", "GaussQuadrature", "GilbertCurves", "HDF5", "InteractiveUtils", "IntervalSets", "LinearAlgebra", "Memoize", "PkgVersion", "RecursiveArrayTools", "Requires", "RootSolvers", "SparseArrays", "Static", "StaticArrays", "Statistics", "UnPack", "WeakValueDicts"] path = ".." uuid = "d414da3d-4745-48bb-8d80-42e94e092884" -version = "0.10.53" +version = "0.10.54" [[deps.ClimaCorePlots]] deps = ["ClimaCore", "RecipesBase", "StaticArrays", "TriplotBase"] @@ -840,9 +840,9 @@ uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" [[deps.IntervalSets]] deps = ["Dates", "Random"] -git-tree-sha1 = "8e59ea773deee525c99a8018409f64f19fb719e6" +git-tree-sha1 = "3d8866c029dd6b16e69e0d4a939c4dfcb98fac47" uuid = "8197267c-284f-5f27-9208-e0e47529a953" -version = "0.7.7" +version = "0.7.8" weakdeps = ["Statistics"] [deps.IntervalSets.extensions] @@ -876,10 +876,10 @@ uuid = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" version = "0.8.15" [[deps.JLD2]] -deps = ["FileIO", "MacroTools", "Mmap", "OrderedCollections", "Pkg", "Printf", "Reexport", "Requires", "TranscodingStreams", "UUIDs"] -git-tree-sha1 = "572d024660119ee626938419c14db0db5f3f3283" +deps = ["FileIO", "MacroTools", "Mmap", "OrderedCollections", "Pkg", "PrecompileTools", "Printf", "Reexport", "Requires", "TranscodingStreams", "UUIDs"] +git-tree-sha1 = "ebec83429b5dea3857e071d927156207ebd6d617" uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819" -version = "0.4.36" +version = "0.4.37" [[deps.JLFzf]] deps = ["Pipe", "REPL", "Random", "fzf_jll"] @@ -1260,6 +1260,12 @@ git-tree-sha1 = "c13304c81eec1ed3af7fc20e75fb6b26092a1102" uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e" version = "0.3.2" +[[deps.Memoize]] +deps = ["MacroTools"] +git-tree-sha1 = "2b1dfcba103de714d31c033b5dacc2e4a12c7caa" +uuid = "c03570c3-d221-55d1-a50c-7939bbd78826" +version = "0.4.4" + [[deps.MicrosoftMPI_jll]] deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] git-tree-sha1 = "a7023883872e52bc29bcaac74f19adf39347d2d5" @@ -1638,9 +1644,9 @@ version = "0.6.12" [[deps.RecursiveArrayTools]] deps = ["Adapt", "ArrayInterface", "DocStringExtensions", "GPUArraysCore", "IteratorInterfaceExtensions", "LinearAlgebra", "RecipesBase", "Requires", "StaticArraysCore", "Statistics", "SymbolicIndexingInterface", "Tables"] -git-tree-sha1 = "fa453b42ba1623bd2e70260bf44dac850a3430a7" +git-tree-sha1 = "d7087c013e8a496ff396bae843b1e16d9a30ede8" uuid = "731186ca-8d62-57ce-b412-fbd966d074cd" -version = "2.39.0" +version = "2.38.10" [deps.RecursiveArrayTools.extensions] RecursiveArrayToolsMeasurementsExt = "Measurements" @@ -1778,9 +1784,9 @@ version = "1.1.0" [[deps.SimpleNonlinearSolve]] deps = ["ArrayInterface", "DiffEqBase", "FiniteDiff", "ForwardDiff", "LinearAlgebra", "PackageExtensionCompat", "PrecompileTools", "Reexport", "SciMLBase", "StaticArraysCore"] -git-tree-sha1 = "e308d089f5d0e733a017b61784c5813e672f760d" +git-tree-sha1 = "15ff97fa4881133caa324dacafe28b5ac47ad8a2" uuid = "727e6d20-b764-4bd8-a329-72de5adea6c7" -version = "0.1.22" +version = "0.1.23" weakdeps = ["NNlib"] [deps.SimpleNonlinearSolve.extensions] @@ -1945,9 +1951,9 @@ version = "1.0.1" [[deps.Tables]] deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "OrderedCollections", "TableTraits"] -git-tree-sha1 = "a1f34829d5ac0ef499f6d84428bd6b4c71f02ead" +git-tree-sha1 = "cb76cf677714c095e535e3501ac7954732aeea2d" uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" -version = "1.11.0" +version = "1.11.1" [[deps.Tar]] deps = ["ArgTools", "SHA"] @@ -2128,6 +2134,11 @@ git-tree-sha1 = "4528479aa01ee1b3b4cd0e6faef0e04cf16466da" uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91" version = "1.25.0+0" +[[deps.WeakValueDicts]] +git-tree-sha1 = "98528c2610a5479f091d470967a25becfd83edd0" +uuid = "897b6980-f191-5a31-bcb0-bf3c4585e0c1" +version = "0.1.0" + [[deps.WidthLimitedIO]] deps = ["Unicode"] git-tree-sha1 = "71142739e695823729a335e9bc124ef41ec1433a" From dce86837d9194ba1ef79405a36918220be4cc151 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 09:16:24 -0700 Subject: [PATCH 22/42] use accessors --- examples/bickleyjet/bickleyjet_dg.jl | 2 +- examples/hybrid/sphere/remap_pipeline.jl | 2 +- lib/ClimaCorePlots/src/ClimaCorePlots.jl | 3 +- lib/ClimaCoreTempestRemap/src/onlineremap.jl | 8 ++--- lib/ClimaCoreVTK/src/space.jl | 35 +++++++++++--------- src/InputOutput/writers.jl | 8 ++--- src/Operators/numericalflux.jl | 4 +-- src/Spaces/extruded.jl | 7 +++- src/Spaces/spectralelement.jl | 6 ++-- 9 files changed, 43 insertions(+), 32 deletions(-) diff --git a/examples/bickleyjet/bickleyjet_dg.jl b/examples/bickleyjet/bickleyjet_dg.jl index 30ed6ddd35..55f3495397 100644 --- a/examples/bickleyjet/bickleyjet_dg.jl +++ b/examples/bickleyjet/bickleyjet_dg.jl @@ -208,7 +208,7 @@ function rhs!(dydt, y, (parameters, numflux), t) dydt_data .= RecursiveApply.rdiv.(dydt_data, space.local_geometry.WJ) M = Spaces.Quadratures.cutoff_filter_matrix( Float64, - space.quadrature_style, + Spaces.quadrature_rule(space), 3, ) Operators.tensor_product!(dydt_data, M) diff --git a/examples/hybrid/sphere/remap_pipeline.jl b/examples/hybrid/sphere/remap_pipeline.jl index 756e26111a..e0c801d3a0 100644 --- a/examples/hybrid/sphere/remap_pipeline.jl +++ b/examples/hybrid/sphere/remap_pipeline.jl @@ -53,7 +53,7 @@ function remap2latlon(filein, nc_dir, nlat, nlon) # reconstruct space, obtain Nq from space cspace = axes(Y.c) hspace = Spaces.horizontal_space(cspace) - Nq = Spaces.Quadratures.degrees_of_freedom(hspace.quadrature_style) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(hspace)) # create a temporary dir for intermediate data remap_tmpdir = nc_dir * "remaptmp/" diff --git a/lib/ClimaCorePlots/src/ClimaCorePlots.jl b/lib/ClimaCorePlots/src/ClimaCorePlots.jl index 86f67c893c..226f218548 100644 --- a/lib/ClimaCorePlots/src/ClimaCorePlots.jl +++ b/lib/ClimaCorePlots/src/ClimaCorePlots.jl @@ -31,7 +31,8 @@ RecipesBase.@recipe function f( coord_field = Fields.coordinate_field(space) lagrange_quad = Spaces.Quadratures.ClosedUniform{Nu}() - dataspace = Spaces.SpectralElementSpace1D(space.topology, lagrange_quad) + dataspace = + Spaces.SpectralElementSpace1D(Spaces.topology(space), lagrange_quad) interp = Operators.Interpolate(dataspace) ifield = interp.(field) diff --git a/lib/ClimaCoreTempestRemap/src/onlineremap.jl b/lib/ClimaCoreTempestRemap/src/onlineremap.jl index a2aad8748d..f1e0d32900 100644 --- a/lib/ClimaCoreTempestRemap/src/onlineremap.jl +++ b/lib/ClimaCoreTempestRemap/src/onlineremap.jl @@ -170,15 +170,15 @@ function generate_map( out_type = "cgll", ) if (target_space_distr != nothing) - comms_ctx = target_space_distr.topology.context + comms_ctx = ClimaComms.context(target_space_distr) else - comms_ctx = target_space.topology.context + comms_ctx = ClimaComms.context(target_space) end if ClimaComms.iamroot(comms_ctx) # write meshes and generate weights on root process (using global indices) - write_exodus(meshfile_source, source_space.topology) - write_exodus(meshfile_target, target_space.topology) + write_exodus(meshfile_source, Spaces.topology(source_space)) + write_exodus(meshfile_target, Spaces.topology(target_space)) overlap_mesh(meshfile_overlap, meshfile_source, meshfile_target) remap_weights( weightfile, diff --git a/lib/ClimaCoreVTK/src/space.jl b/lib/ClimaCoreVTK/src/space.jl index 14e50bb438..40554ec0e2 100644 --- a/lib/ClimaCoreVTK/src/space.jl +++ b/lib/ClimaCoreVTK/src/space.jl @@ -104,7 +104,8 @@ an unstuctured mesh of linear cells, suitable for passing to `vtk_grid`. """ function vtk_cells_linear(gridspace::Spaces.SpectralElementSpace2D) - Nq = Spaces.Quadratures.degrees_of_freedom(gridspace.quadrature_style) + Nq = + Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(gridspace)) Nh = Topologies.nlocalelems(gridspace) ind = LinearIndices((1:Nq, 1:Nq, 1:Nh)) cells = [ @@ -123,7 +124,7 @@ end function vtk_cells_linear(gridspace::Spaces.FaceExtrudedFiniteDifferenceSpace2D) hspace = Spaces.horizontal_space(gridspace) - Nq = Spaces.Quadratures.degrees_of_freedom(hspace.quadrature_style) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(hspace)) Nh = Topologies.nlocalelems(hspace) Nv = Spaces.nlevels(gridspace) ind = LinearIndices((1:Nv, 1:Nq, 1:Nh)) # assume VIFH @@ -142,7 +143,7 @@ function vtk_cells_linear(gridspace::Spaces.FaceExtrudedFiniteDifferenceSpace2D) end function vtk_cells_linear(gridspace::Spaces.FaceExtrudedFiniteDifferenceSpace3D) hspace = Spaces.horizontal_space(gridspace) - Nq = Spaces.Quadratures.degrees_of_freedom(hspace.quadrature_style) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(hspace)) Nh = Topologies.nlocalelems(hspace) Nv = Spaces.nlevels(gridspace) ind = LinearIndices((1:Nv, 1:Nq, 1:Nq, 1:Nh)) # assumes VIJFH @@ -177,20 +178,20 @@ This generally does two things: - Modifies the vertical space to be on the faces. """ function vtk_grid_space(space::Spaces.SpectralElementSpace1D) - if space.quadrature_style isa Spaces.Quadratures.ClosedUniform + if Spaces.quadrature_rule(space) isa Spaces.Quadratures.ClosedUniform return space end - Nq = Spaces.Quadratures.degrees_of_freedom(space.quadrature_style) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) lagrange_quad = Spaces.Quadratures.ClosedUniform{Nq}() - return Spaces.SpectralElementSpace1D(space.topology, lagrange_quad) + return Spaces.SpectralElementSpace1D(Spaces.topology(space), lagrange_quad) end function vtk_grid_space(space::Spaces.SpectralElementSpace2D) - if space.quadrature_style isa Spaces.Quadratures.ClosedUniform + if Spaces.quadrature_rule(space) isa Spaces.Quadratures.ClosedUniform return space end - Nq = Spaces.Quadratures.degrees_of_freedom(space.quadrature_style) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) lagrange_quad = Spaces.Quadratures.ClosedUniform{Nq}() - return Spaces.SpectralElementSpace2D(space.topology, lagrange_quad) + return Spaces.SpectralElementSpace2D(Spaces.topology(space), lagrange_quad) end function vtk_grid_space(space::Spaces.FaceExtrudedFiniteDifferenceSpace) # this will need to be updated for warped meshes @@ -223,16 +224,20 @@ This generally does two things: - Modifies the vertical space to be on the centers. """ function vtk_cell_space(gridspace::Spaces.SpectralElementSpace1D) - @assert gridspace.quadrature_style isa Spaces.Quadratures.ClosedUniform - Nq = Spaces.Quadratures.degrees_of_freedom(gridspace.quadrature_style) + @assert Spaces.quadrature_rule(gridspace) isa + Spaces.Quadratures.ClosedUniform + Nq = + Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(gridspace)) quad = Spaces.Quadratures.Uniform{Nq - 1}() - return Spaces.SpectralElementSpace1D(gridspace.topology, quad) + return Spaces.SpectralElementSpace1D(Spaces.topology(gridspace), quad) end function vtk_cell_space(gridspace::Spaces.SpectralElementSpace2D) - @assert gridspace.quadrature_style isa Spaces.Quadratures.ClosedUniform - Nq = Spaces.Quadratures.degrees_of_freedom(gridspace.quadrature_style) + @assert Spaces.quadrature_rule(gridspace) isa + Spaces.Quadratures.ClosedUniform + Nq = + Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(gridspace)) quad = Spaces.Quadratures.Uniform{Nq - 1}() - return Spaces.SpectralElementSpace2D(gridspace.topology, quad) + return Spaces.SpectralElementSpace2D(Spaces.topology(gridspace), quad) end function vtk_cell_space(gridspace::Spaces.FaceExtrudedFiniteDifferenceSpace) # this will need to be updated for warped meshes diff --git a/src/InputOutput/writers.jl b/src/InputOutput/writers.jl index af2df79afb..0c236bb195 100644 --- a/src/InputOutput/writers.jl +++ b/src/InputOutput/writers.jl @@ -319,12 +319,12 @@ function write_new!( write_attribute( group, "quadrature_type", - string(nameof(typeof(space.quadrature_style))), + string(nameof(typeof(Spaces.quadrature_rule(space)))), ) write_attribute( group, "quadrature_num_points", - Quadratures.degrees_of_freedom(space.quadrature_style), + Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)), ) write_attribute(group, "topology", write!(writer, space.topology)) return name @@ -340,12 +340,12 @@ function write_new!( write_attribute( group, "quadrature_type", - string(nameof(typeof(space.quadrature_style))), + string(nameof(typeof(Spaces.quadrature_rule(space)))), ) write_attribute( group, "quadrature_num_points", - Quadratures.degrees_of_freedom(space.quadrature_style), + Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)), ) write_attribute(group, "topology", write!(writer, space.topology)) return name diff --git a/src/Operators/numericalflux.jl b/src/Operators/numericalflux.jl index f0d9c0e3ca..57784ee156 100644 --- a/src/Operators/numericalflux.jl +++ b/src/Operators/numericalflux.jl @@ -24,7 +24,7 @@ See also: """ function add_numerical_flux_internal!(fn, dydt, args...) space = axes(dydt) - Nq = Spaces.Quadratures.degrees_of_freedom(space.quadrature_style) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) topology = space.topology for (iface, (elem⁻, face⁻, elem⁺, face⁺, reversed)) in @@ -100,7 +100,7 @@ end function add_numerical_flux_boundary!(fn, dydt, args...) space = axes(dydt) - Nq = Spaces.Quadratures.degrees_of_freedom(space.quadrature_style) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) topology = space.topology for (iboundary, boundarytag) in diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 2e2405e567..57c58720a0 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -119,7 +119,12 @@ function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) println(iio) println(iio, " "^(indent + 2), "horizontal:") println(iio, " "^(indent + 4), "mesh: ", hspace.topology.mesh) - println(iio, " "^(indent + 4), "quadrature: ", hspace.quadrature_style) + println( + iio, + " "^(indent + 4), + "quadrature: ", + Spaces.quadrature_rule(hspace), + ) println(iio, " "^(indent + 2), "vertical:") print(iio, " "^(indent + 4), "mesh: ", space.vertical_topology.mesh) end diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 2efe9b7025..7b5560e52c 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -104,7 +104,7 @@ Base.@propagate_inbounds function slab( h, ) SpectralElementSpaceSlab( - space.quadrature_style, + Spaces.quadrature_rule(space), slab(space.local_geometry, v, h), ) end @@ -154,7 +154,7 @@ Base.eltype(iter::UniqueNodeIterator{<:SpectralElementSpace2D}) = function Base.length(iter::UniqueNodeIterator{<:SpectralElementSpace2D}) space = iter.space topology = space.topology - Nq = Quadratures.degrees_of_freedom(space.quadrature_style) + Nq = Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) nelem = Topologies.nlocalelems(topology) nvert = length(Topologies.local_vertices(topology)) @@ -178,7 +178,7 @@ function Base.iterate( ((i, j), e), ) space = iter.space - Nq = Quadratures.degrees_of_freedom(space.quadrature_style) + Nq = Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) while true # find next node i += 1 From c41d589fafc367a0d3566b4bdfca3c30f2257e13 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 09:39:19 -0700 Subject: [PATCH 23/42] qualify cuda macro --- src/Topologies/dss_cuda.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Topologies/dss_cuda.jl b/src/Topologies/dss_cuda.jl index 058186c344..a7ae60a2a0 100644 --- a/src/Topologies/dss_cuda.jl +++ b/src/Topologies/dss_cuda.jl @@ -21,7 +21,7 @@ function dss_load_perimeter_data!( (nlevels, nperimeter, nfid, nelems) = size(pperimeter_data) nitems = nlevels * nperimeter * nfid * nelems nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_load_perimeter_data_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_load_perimeter_data_kernel!( pperimeter_data, pdata, perimeter, @@ -58,7 +58,7 @@ function dss_unload_perimeter_data!( (nlevels, nperimeter, nfid, nelems) = size(pperimeter_data) nitems = nlevels * nperimeter * nfid * nelems nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_unload_perimeter_data_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_unload_perimeter_data_kernel!( pdata, pperimeter_data, perimeter, @@ -98,7 +98,7 @@ function dss_local!( nitems = nlevels * nfid * (nlocalfaces + nlocalvertices) nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_local_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_local_kernel!( pperimeter_data, topology.local_vertices, topology.local_vertex_offset, @@ -184,7 +184,7 @@ function dss_transform!( (nlevels, nperimeter, _, _) = size(pperimeter_data) nitems = nlevels * nperimeter * nlocalelems nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_transform_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_transform_kernel!( pperimeter_data, pdata, p∂ξ∂x, @@ -290,7 +290,7 @@ function dss_untransform!( (nlevels, nperimeter, _, _) = size(pperimeter_data) nitems = nlevels * nperimeter * nlocalelems nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_untransform_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_untransform_kernel!( pperimeter_data, pdata, p∂ξ∂x, @@ -376,7 +376,7 @@ function dss_local_ghost!( max_threads = 256 nitems = nlevels * nfid * nghostvertices nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_local_ghost_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_local_ghost_kernel!( pperimeter_data, topology.ghost_vertices, topology.ghost_vertex_offset, @@ -427,7 +427,7 @@ function fill_send_buffer!(::ClimaComms.CUDADevice, dss_buffer::DSSBuffer) if nsend > 0 nitems = nsend * nlevels * nfid nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) fill_send_buffer_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) fill_send_buffer_kernel!( send_data, send_buf_idx, pperimeter_data, @@ -467,7 +467,7 @@ function load_from_recv_buffer!(::ClimaComms.CUDADevice, dss_buffer::DSSBuffer) if nrecv > 0 nitems = nrecv * nlevels * nfid nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) load_from_recv_buffer_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) load_from_recv_buffer_kernel!( pperimeter_data, recv_data, recv_buf_idx, @@ -511,7 +511,7 @@ function dss_ghost!( nlevels, _, nfidx, _ = size(pperimeter_data) nitems = nlevels * nfidx * nghostvertices nthreads, nblocks = _configure_threadblock(nitems) - @cuda threads = (nthreads) blocks = (nblocks) dss_ghost_kernel!( + CUDA.@cuda threads = (nthreads) blocks = (nblocks) dss_ghost_kernel!( pperimeter_data, topology.ghost_vertices, topology.ghost_vertex_offset, From 65ff81236df44356b359f6a51cc7717faf336995 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 09:44:22 -0700 Subject: [PATCH 24/42] more fixes --- lib/ClimaCoreTempestRemap/src/netcdf.jl | 2 +- lib/ClimaCoreTempestRemap/src/onlineremap.jl | 8 ++++---- lib/ClimaCoreTempestRemap/test/online_remap.jl | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ClimaCoreTempestRemap/src/netcdf.jl b/lib/ClimaCoreTempestRemap/src/netcdf.jl index d71386a0e2..13c07d87ef 100644 --- a/lib/ClimaCoreTempestRemap/src/netcdf.jl +++ b/lib/ClimaCoreTempestRemap/src/netcdf.jl @@ -53,7 +53,7 @@ def_space_coord( nc::NCDataset, space::Spaces.SpectralElementSpace2D; type = "dgll", -) = def_space_coord(nc, space, space.topology.mesh; type) +) = def_space_coord(nc, space, Spaces.topology(space).mesh; type) function def_space_coord( nc::NCDataset, diff --git a/lib/ClimaCoreTempestRemap/src/onlineremap.jl b/lib/ClimaCoreTempestRemap/src/onlineremap.jl index f1e0d32900..0c76caf637 100644 --- a/lib/ClimaCoreTempestRemap/src/onlineremap.jl +++ b/lib/ClimaCoreTempestRemap/src/onlineremap.jl @@ -72,7 +72,7 @@ function remap!( if R.out_type == "cgll" topology = Spaces.topology(R.target_space) hspace = Spaces.horizontal_space(R.target_space) - quadrature_style = hspace.quadrature_style + quadrature_style = Spaces.quadrature_rule(hspace) Spaces.dss2!(target, topology, quadrature_style) end return target @@ -132,7 +132,7 @@ function remap!(target::Fields.Field, R::LinearMap, source::Fields.Field) if R.out_type == "cgll" topology = Spaces.topology(axes(target)) hspace = Spaces.horizontal_space(axes(target)) - quadrature_style = hspace.quadrature_style + quadrature_style = Spaces.quadrature_rule(hspace) Spaces.dss2!( Fields.field_values(target), topology, @@ -187,11 +187,11 @@ function generate_map( meshfile_overlap; in_type = in_type, in_np = Spaces.Quadratures.degrees_of_freedom( - source_space.quadrature_style, + Spaces.quadrature_rule(source_space), ), out_type = out_type, out_np = Spaces.Quadratures.degrees_of_freedom( - target_space.quadrature_style, + Spaces.quadrature_rule(target_space), ), ) diff --git a/lib/ClimaCoreTempestRemap/test/online_remap.jl b/lib/ClimaCoreTempestRemap/test/online_remap.jl index b91f2a4b18..255528775f 100644 --- a/lib/ClimaCoreTempestRemap/test/online_remap.jl +++ b/lib/ClimaCoreTempestRemap/test/online_remap.jl @@ -33,7 +33,7 @@ function reshape_sparse_to_field!(field::Fields.Field, in_array::Array, R) # broadcast to the redundant nodes using unweighted dss topology = Spaces.topology(axes(field)) hspace = Spaces.horizontal_space(axes(field)) - quadrature_style = hspace.quadrature_style + quadrature_style = Spaces.quadrature_rule(hspace) Spaces.dss2!(Fields.field_values(field), topology, quadrature_style) return field end From 4efb0a63db8b3c59ef42b2ec78aa877a067bd0d7 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 09:49:43 -0700 Subject: [PATCH 25/42] fix function name --- examples/bickleyjet/bickleyjet_dg.jl | 2 +- examples/hybrid/sphere/remap_pipeline.jl | 2 +- lib/ClimaCoreTempestRemap/src/onlineremap.jl | 8 ++--- .../test/online_remap.jl | 2 +- lib/ClimaCoreVTK/src/space.jl | 31 ++++++++++--------- src/InputOutput/writers.jl | 8 ++--- src/Operators/numericalflux.jl | 4 +-- src/Spaces/extruded.jl | 7 +---- src/Spaces/spectralelement.jl | 8 ++--- 9 files changed, 35 insertions(+), 37 deletions(-) diff --git a/examples/bickleyjet/bickleyjet_dg.jl b/examples/bickleyjet/bickleyjet_dg.jl index 55f3495397..8d659aa161 100644 --- a/examples/bickleyjet/bickleyjet_dg.jl +++ b/examples/bickleyjet/bickleyjet_dg.jl @@ -208,7 +208,7 @@ function rhs!(dydt, y, (parameters, numflux), t) dydt_data .= RecursiveApply.rdiv.(dydt_data, space.local_geometry.WJ) M = Spaces.Quadratures.cutoff_filter_matrix( Float64, - Spaces.quadrature_rule(space), + Spaces.quadrature_style(space), 3, ) Operators.tensor_product!(dydt_data, M) diff --git a/examples/hybrid/sphere/remap_pipeline.jl b/examples/hybrid/sphere/remap_pipeline.jl index e0c801d3a0..d08db291cb 100644 --- a/examples/hybrid/sphere/remap_pipeline.jl +++ b/examples/hybrid/sphere/remap_pipeline.jl @@ -53,7 +53,7 @@ function remap2latlon(filein, nc_dir, nlat, nlon) # reconstruct space, obtain Nq from space cspace = axes(Y.c) hspace = Spaces.horizontal_space(cspace) - Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(hspace)) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(hspace)) # create a temporary dir for intermediate data remap_tmpdir = nc_dir * "remaptmp/" diff --git a/lib/ClimaCoreTempestRemap/src/onlineremap.jl b/lib/ClimaCoreTempestRemap/src/onlineremap.jl index 0c76caf637..9ea2cb988e 100644 --- a/lib/ClimaCoreTempestRemap/src/onlineremap.jl +++ b/lib/ClimaCoreTempestRemap/src/onlineremap.jl @@ -72,7 +72,7 @@ function remap!( if R.out_type == "cgll" topology = Spaces.topology(R.target_space) hspace = Spaces.horizontal_space(R.target_space) - quadrature_style = Spaces.quadrature_rule(hspace) + quadrature_style = Spaces.quadrature_style(hspace) Spaces.dss2!(target, topology, quadrature_style) end return target @@ -132,7 +132,7 @@ function remap!(target::Fields.Field, R::LinearMap, source::Fields.Field) if R.out_type == "cgll" topology = Spaces.topology(axes(target)) hspace = Spaces.horizontal_space(axes(target)) - quadrature_style = Spaces.quadrature_rule(hspace) + quadrature_style = Spaces.quadrature_style(hspace) Spaces.dss2!( Fields.field_values(target), topology, @@ -187,11 +187,11 @@ function generate_map( meshfile_overlap; in_type = in_type, in_np = Spaces.Quadratures.degrees_of_freedom( - Spaces.quadrature_rule(source_space), + Spaces.quadrature_style(source_space), ), out_type = out_type, out_np = Spaces.Quadratures.degrees_of_freedom( - Spaces.quadrature_rule(target_space), + Spaces.quadrature_style(target_space), ), ) diff --git a/lib/ClimaCoreTempestRemap/test/online_remap.jl b/lib/ClimaCoreTempestRemap/test/online_remap.jl index 255528775f..3d6233378c 100644 --- a/lib/ClimaCoreTempestRemap/test/online_remap.jl +++ b/lib/ClimaCoreTempestRemap/test/online_remap.jl @@ -33,7 +33,7 @@ function reshape_sparse_to_field!(field::Fields.Field, in_array::Array, R) # broadcast to the redundant nodes using unweighted dss topology = Spaces.topology(axes(field)) hspace = Spaces.horizontal_space(axes(field)) - quadrature_style = Spaces.quadrature_rule(hspace) + quadrature_style = Spaces.quadrature_style(hspace) Spaces.dss2!(Fields.field_values(field), topology, quadrature_style) return field end diff --git a/lib/ClimaCoreVTK/src/space.jl b/lib/ClimaCoreVTK/src/space.jl index 40554ec0e2..7bab99f87e 100644 --- a/lib/ClimaCoreVTK/src/space.jl +++ b/lib/ClimaCoreVTK/src/space.jl @@ -104,8 +104,9 @@ an unstuctured mesh of linear cells, suitable for passing to `vtk_grid`. """ function vtk_cells_linear(gridspace::Spaces.SpectralElementSpace2D) - Nq = - Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(gridspace)) + Nq = Spaces.Quadratures.degrees_of_freedom( + Spaces.quadrature_style(gridspace), + ) Nh = Topologies.nlocalelems(gridspace) ind = LinearIndices((1:Nq, 1:Nq, 1:Nh)) cells = [ @@ -124,7 +125,7 @@ end function vtk_cells_linear(gridspace::Spaces.FaceExtrudedFiniteDifferenceSpace2D) hspace = Spaces.horizontal_space(gridspace) - Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(hspace)) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(hspace)) Nh = Topologies.nlocalelems(hspace) Nv = Spaces.nlevels(gridspace) ind = LinearIndices((1:Nv, 1:Nq, 1:Nh)) # assume VIFH @@ -143,7 +144,7 @@ function vtk_cells_linear(gridspace::Spaces.FaceExtrudedFiniteDifferenceSpace2D) end function vtk_cells_linear(gridspace::Spaces.FaceExtrudedFiniteDifferenceSpace3D) hspace = Spaces.horizontal_space(gridspace) - Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(hspace)) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(hspace)) Nh = Topologies.nlocalelems(hspace) Nv = Spaces.nlevels(gridspace) ind = LinearIndices((1:Nv, 1:Nq, 1:Nq, 1:Nh)) # assumes VIJFH @@ -178,18 +179,18 @@ This generally does two things: - Modifies the vertical space to be on the faces. """ function vtk_grid_space(space::Spaces.SpectralElementSpace1D) - if Spaces.quadrature_rule(space) isa Spaces.Quadratures.ClosedUniform + if Spaces.quadrature_style(space) isa Spaces.Quadratures.ClosedUniform return space end - Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)) lagrange_quad = Spaces.Quadratures.ClosedUniform{Nq}() return Spaces.SpectralElementSpace1D(Spaces.topology(space), lagrange_quad) end function vtk_grid_space(space::Spaces.SpectralElementSpace2D) - if Spaces.quadrature_rule(space) isa Spaces.Quadratures.ClosedUniform + if Spaces.quadrature_style(space) isa Spaces.Quadratures.ClosedUniform return space end - Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)) lagrange_quad = Spaces.Quadratures.ClosedUniform{Nq}() return Spaces.SpectralElementSpace2D(Spaces.topology(space), lagrange_quad) end @@ -224,18 +225,20 @@ This generally does two things: - Modifies the vertical space to be on the centers. """ function vtk_cell_space(gridspace::Spaces.SpectralElementSpace1D) - @assert Spaces.quadrature_rule(gridspace) isa + @assert Spaces.quadrature_style(gridspace) isa Spaces.Quadratures.ClosedUniform - Nq = - Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(gridspace)) + Nq = Spaces.Quadratures.degrees_of_freedom( + Spaces.quadrature_style(gridspace), + ) quad = Spaces.Quadratures.Uniform{Nq - 1}() return Spaces.SpectralElementSpace1D(Spaces.topology(gridspace), quad) end function vtk_cell_space(gridspace::Spaces.SpectralElementSpace2D) - @assert Spaces.quadrature_rule(gridspace) isa + @assert Spaces.quadrature_style(gridspace) isa Spaces.Quadratures.ClosedUniform - Nq = - Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(gridspace)) + Nq = Spaces.Quadratures.degrees_of_freedom( + Spaces.quadrature_style(gridspace), + ) quad = Spaces.Quadratures.Uniform{Nq - 1}() return Spaces.SpectralElementSpace2D(Spaces.topology(gridspace), quad) end diff --git a/src/InputOutput/writers.jl b/src/InputOutput/writers.jl index 0c236bb195..3a3645ba16 100644 --- a/src/InputOutput/writers.jl +++ b/src/InputOutput/writers.jl @@ -319,12 +319,12 @@ function write_new!( write_attribute( group, "quadrature_type", - string(nameof(typeof(Spaces.quadrature_rule(space)))), + string(nameof(typeof(Spaces.quadrature_style(space)))), ) write_attribute( group, "quadrature_num_points", - Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)), + Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)), ) write_attribute(group, "topology", write!(writer, space.topology)) return name @@ -340,12 +340,12 @@ function write_new!( write_attribute( group, "quadrature_type", - string(nameof(typeof(Spaces.quadrature_rule(space)))), + string(nameof(typeof(Spaces.quadrature_style(space)))), ) write_attribute( group, "quadrature_num_points", - Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)), + Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)), ) write_attribute(group, "topology", write!(writer, space.topology)) return name diff --git a/src/Operators/numericalflux.jl b/src/Operators/numericalflux.jl index 57784ee156..4137e03122 100644 --- a/src/Operators/numericalflux.jl +++ b/src/Operators/numericalflux.jl @@ -24,7 +24,7 @@ See also: """ function add_numerical_flux_internal!(fn, dydt, args...) space = axes(dydt) - Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)) topology = space.topology for (iface, (elem⁻, face⁻, elem⁺, face⁺, reversed)) in @@ -100,7 +100,7 @@ end function add_numerical_flux_boundary!(fn, dydt, args...) space = axes(dydt) - Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) + Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)) topology = space.topology for (iboundary, boundarytag) in diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 57c58720a0..68eba562a6 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -119,12 +119,7 @@ function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) println(iio) println(iio, " "^(indent + 2), "horizontal:") println(iio, " "^(indent + 4), "mesh: ", hspace.topology.mesh) - println( - iio, - " "^(indent + 4), - "quadrature: ", - Spaces.quadrature_rule(hspace), - ) + println(iio, " "^(indent + 4), "quadrature: ", quadrature_style(hspace)) println(iio, " "^(indent + 2), "vertical:") print(iio, " "^(indent + 4), "mesh: ", space.vertical_topology.mesh) end diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 7b5560e52c..fb23dabee1 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -104,7 +104,7 @@ Base.@propagate_inbounds function slab( h, ) SpectralElementSpaceSlab( - Spaces.quadrature_rule(space), + quadrature_style(space), slab(space.local_geometry, v, h), ) end @@ -153,8 +153,8 @@ Base.eltype(iter::UniqueNodeIterator{<:SpectralElementSpace2D}) = function Base.length(iter::UniqueNodeIterator{<:SpectralElementSpace2D}) space = iter.space - topology = space.topology - Nq = Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) + topology = Spaces.topology(space) + Nq = Quadratures.degrees_of_freedom(quadrature_style(space)) nelem = Topologies.nlocalelems(topology) nvert = length(Topologies.local_vertices(topology)) @@ -178,7 +178,7 @@ function Base.iterate( ((i, j), e), ) space = iter.space - Nq = Quadratures.degrees_of_freedom(Spaces.quadrature_rule(space)) + Nq = Quadratures.degrees_of_freedom(quadrature_style(space)) while true # find next node i += 1 From 1973dac5f5f921223cc8ad437a12a4e2632dd9d0 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 10:26:36 -0700 Subject: [PATCH 26/42] more fixes --- examples/hybrid/hybrid3dcs_dss.jl | 2 +- examples/hybrid/sphere/hadley_circulation.jl | 2 +- examples/hybrid/sphere/remap_pipeline.jl | 2 +- examples/sphere/limiters_advection.jl | 2 +- examples/sphere/shallow_water_cuda.jl | 4 +-- lib/ClimaCorePlots/src/ClimaCorePlots.jl | 6 ++-- lib/ClimaCoreTempestRemap/src/netcdf.jl | 14 ++++++--- lib/ClimaCoreTempestRemap/src/onlineremap.jl | 12 ++----- .../test/online_remap.jl | 2 +- src/Fields/indices.jl | 2 +- src/Grids/Grids.jl | 2 +- src/InputOutput/writers.jl | 6 ++-- src/Operators/numericalflux.jl | 4 +-- src/Spaces/extruded.jl | 9 ++++-- src/Spaces/finitedifference.jl | 12 ++++++- src/Spaces/spectralelement.jl | 8 ++--- test/Spaces/ddss1.jl | 12 ++++--- test/Spaces/distributed/ddss2.jl | 12 ++++--- test/Spaces/distributed/ddss3.jl | 31 ++++++++++++------- test/Spaces/distributed_cuda/ddss2.jl | 12 ++++--- test/Spaces/distributed_cuda/ddss3.jl | 31 ++++++++++++------- 21 files changed, 114 insertions(+), 73 deletions(-) diff --git a/examples/hybrid/hybrid3dcs_dss.jl b/examples/hybrid/hybrid3dcs_dss.jl index 7cf1605444..ecffa77ada 100644 --- a/examples/hybrid/hybrid3dcs_dss.jl +++ b/examples/hybrid/hybrid3dcs_dss.jl @@ -94,7 +94,7 @@ function hybrid3dcubedsphere_dss_profiler( # precompile relevant functions space = axes(Y.c) - horizontal_topology = space.horizontal_space.topology + horizontal_topology = Spaces.topology(space) Spaces.weighted_dss_internal!(Y.c, ghost_buffer.c) weighted_dss_full!(Y.c, ghost_buffer.c) Spaces.fill_send_buffer!( diff --git a/examples/hybrid/sphere/hadley_circulation.jl b/examples/hybrid/sphere/hadley_circulation.jl index 441bcb326c..2b1db316e9 100644 --- a/examples/hybrid/sphere/hadley_circulation.jl +++ b/examples/hybrid/sphere/hadley_circulation.jl @@ -250,7 +250,7 @@ end # write out our cubed sphere mesh meshfile_cc = remap_tmpdir * "mesh_cubedsphere.g" -write_exodus(meshfile_cc, hv_center_space.horizontal_space.topology) +write_exodus(meshfile_cc, Spaces.topology(hv_center_space)) # write out RLL mesh nlat = 90 diff --git a/examples/hybrid/sphere/remap_pipeline.jl b/examples/hybrid/sphere/remap_pipeline.jl index d08db291cb..1fffecc833 100644 --- a/examples/hybrid/sphere/remap_pipeline.jl +++ b/examples/hybrid/sphere/remap_pipeline.jl @@ -97,7 +97,7 @@ function remap2latlon(filein, nc_dir, nlat, nlon) # write out our cubed sphere mesh meshfile_cc = remap_tmpdir * "mesh_cubedsphere.g" - write_exodus(meshfile_cc, hspace.topology) + write_exodus(meshfile_cc, Spaces.topology(hspace)) meshfile_rll = remap_tmpdir * "mesh_rll.g" rll_mesh(meshfile_rll; nlat = nlat, nlon = nlon) diff --git a/examples/sphere/limiters_advection.jl b/examples/sphere/limiters_advection.jl index db0e5bb23b..49e7e0ea98 100644 --- a/examples/sphere/limiters_advection.jl +++ b/examples/sphere/limiters_advection.jl @@ -107,7 +107,7 @@ for (k, ne) in enumerate(ne_seq) Spaces.SpectralElementSpace2D(grid_topology, quad; enable_bubble = true) # Initialize variables needed for limiters - n_elems = Topologies.nlocalelems(space.topology) + n_elems = Topologies.nlocalelems(Spaces.topology(space)) min_q = zeros(n_elems) max_q = zeros(n_elems) diff --git a/examples/sphere/shallow_water_cuda.jl b/examples/sphere/shallow_water_cuda.jl index 329e1782cb..407402e6d7 100644 --- a/examples/sphere/shallow_water_cuda.jl +++ b/examples/sphere/shallow_water_cuda.jl @@ -473,7 +473,7 @@ function rhs!(dYdt_fv, y_fv, parameters, t) wcurl(Geometry.Covariant3Vector(curl(y.u))), ) - NVTX.@range "dss" Spaces.weighted_dss2!(dYdt, ghost_buffer) + NVTX.@range "dss" Spaces.weighted_dss!(dYdt, ghost_buffer) end NVTX.@range "tendency" begin @@ -489,7 +489,7 @@ function rhs!(dYdt_fv, y_fv, parameters, t) dYdt.u += -grad(g * (y.h + h_s) + norm(y.u)^2 / 2) #+ dYdt.u += y.u × (f + curl(y.u)) end - NVTX.@range "dss" Spaces.weighted_dss2!(dYdt, ghost_buffer) + NVTX.@range "dss" Spaces.weighted_dss!(dYdt, ghost_buffer) end end return dYdt_fv diff --git a/lib/ClimaCorePlots/src/ClimaCorePlots.jl b/lib/ClimaCorePlots/src/ClimaCorePlots.jl index 226f218548..5181862303 100644 --- a/lib/ClimaCorePlots/src/ClimaCorePlots.jl +++ b/lib/ClimaCorePlots/src/ClimaCorePlots.jl @@ -99,7 +99,7 @@ RecipesBase.@recipe function f(space::Spaces.ExtrudedFiniteDifferenceSpace) #TODO: assumes VIFH layout @assert Nj == 1 "plotting only defined for 1D extruded fields" - hspace = space.horizontal_space + hspace = Spaces.horizontal_space(space) quad = Spaces.quadrature_style(hspace) quad_name = Base.typename(typeof(quad)).name @@ -196,7 +196,7 @@ function _slice_triplot(field, hinterpolate, ncolors) Ni, Nj, _, Nv, Nh = size(data) space = axes(field) - htopology = Spaces.topology(space.horizontal_space) + htopology = Spaces.topology(space) hdomain = Topologies.domain(htopology) vdomain = Topologies.domain(space.vertical_topology) @@ -278,7 +278,7 @@ function _slice_along(field, coord) ) end space = axes(field) - hspace = space.horizontal_space + hspace = Spaces.horizontal_space(space) htopo = ClimaCore.Spaces.topology(hspace) hmesh = htopo.mesh linear_idx = LinearIndices(ClimaCore.Meshes.elements(hmesh)) diff --git a/lib/ClimaCoreTempestRemap/src/netcdf.jl b/lib/ClimaCoreTempestRemap/src/netcdf.jl index 13c07d87ef..c6c514aa19 100644 --- a/lib/ClimaCoreTempestRemap/src/netcdf.jl +++ b/lib/ClimaCoreTempestRemap/src/netcdf.jl @@ -224,13 +224,17 @@ end function def_space_coord( nc::NCDataset, - space::Spaces.ExtrudedFiniteDifferenceSpace{S}; + space::Spaces.ExtrudedFiniteDifferenceSpace; type = "dgll", -) where {S <: Spaces.Staggering} - hvar = def_space_coord(nc, space.horizontal_space; type = type) +) + staggering = Spaces.staggering(space) + hvar = def_space_coord(nc, Spaces.horizontal_space(space); type = type) vvar = def_space_coord( nc, - Spaces.FiniteDifferenceSpace{S}(space.vertical_topology), + Spaces.FiniteDifferenceSpace( + Spaces.vertical_topology(space), + staggering, + ), ) (hvar..., vvar...) end @@ -332,7 +336,7 @@ function Base.setindex!( ) nc = NCDataset(var) space = axes(field) - hspace = space.horizontal_space + hspace = Spaces.horizontal_space(space) if nc.attrib["node_type"] == "cgll" nodes = Spaces.unique_nodes(hspace) elseif nc.attrib["node_type"] == "dgll" diff --git a/lib/ClimaCoreTempestRemap/src/onlineremap.jl b/lib/ClimaCoreTempestRemap/src/onlineremap.jl index 9ea2cb988e..14cb831b00 100644 --- a/lib/ClimaCoreTempestRemap/src/onlineremap.jl +++ b/lib/ClimaCoreTempestRemap/src/onlineremap.jl @@ -71,9 +71,7 @@ function remap!( # using out_type == "cgll" if R.out_type == "cgll" topology = Spaces.topology(R.target_space) - hspace = Spaces.horizontal_space(R.target_space) - quadrature_style = Spaces.quadrature_style(hspace) - Spaces.dss2!(target, topology, quadrature_style) + Topologies.dss!(target, topology) end return target end @@ -131,13 +129,7 @@ function remap!(target::Fields.Field, R::LinearMap, source::Fields.Field) # using out_type == "cgll" if R.out_type == "cgll" topology = Spaces.topology(axes(target)) - hspace = Spaces.horizontal_space(axes(target)) - quadrature_style = Spaces.quadrature_style(hspace) - Spaces.dss2!( - Fields.field_values(target), - topology, - quadrature_style, - ) + Topologies.dss!(Fields.field_values(target), topology) end return target end diff --git a/lib/ClimaCoreTempestRemap/test/online_remap.jl b/lib/ClimaCoreTempestRemap/test/online_remap.jl index 3d6233378c..d1aa95303d 100644 --- a/lib/ClimaCoreTempestRemap/test/online_remap.jl +++ b/lib/ClimaCoreTempestRemap/test/online_remap.jl @@ -34,7 +34,7 @@ function reshape_sparse_to_field!(field::Fields.Field, in_array::Array, R) topology = Spaces.topology(axes(field)) hspace = Spaces.horizontal_space(axes(field)) quadrature_style = Spaces.quadrature_style(hspace) - Spaces.dss2!(Fields.field_values(field), topology, quadrature_style) + Topologies.dss!(Fields.field_values(field), topology) return field end diff --git a/src/Fields/indices.jl b/src/Fields/indices.jl index 3491a99962..89cac213b7 100644 --- a/src/Fields/indices.jl +++ b/src/Fields/indices.jl @@ -214,7 +214,7 @@ function byslab( ::ClimaComms.CPUMultiThreaded, space::Spaces.AbstractSpectralElementSpace, ) - Nh = Topologies.nlocalelems(space.topology)::Int + Nh = Topologies.nlocalelems(Spaces.topology(space))::Int @inbounds begin Threads.@threads for h in 1:Nh fn(SlabIndex(nothing, h)) diff --git a/src/Grids/Grids.jl b/src/Grids/Grids.jl index 915cdcdacf..7d594d0d8d 100644 --- a/src/Grids/Grids.jl +++ b/src/Grids/Grids.jl @@ -60,7 +60,7 @@ function vertical_topology end ClimaComms.context(grid::AbstractGrid) = ClimaComms.context(topology(grid)) ClimaComms.device(grid::AbstractGrid) = ClimaComms.device(topology(grid)) -Meshes.domain(grid::AbstractGrid) = Meshes.domain(Topologies.topology(grid)) +Meshes.domain(grid::AbstractGrid) = Meshes.domain(topology(grid)) include("finitedifference.jl") include("spectralelement.jl") diff --git a/src/InputOutput/writers.jl b/src/InputOutput/writers.jl index 3a3645ba16..7b19b1e30e 100644 --- a/src/InputOutput/writers.jl +++ b/src/InputOutput/writers.jl @@ -326,7 +326,7 @@ function write_new!( "quadrature_num_points", Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)), ) - write_attribute(group, "topology", write!(writer, space.topology)) + write_attribute(group, "topology", write!(writer, Spaces.topology(space))) return name end @@ -347,7 +347,7 @@ function write_new!( "quadrature_num_points", Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)), ) - write_attribute(group, "topology", write!(writer, space.topology)) + write_attribute(group, "topology", write!(writer, Spaces.topology(space))) return name end @@ -358,7 +358,7 @@ function write_new!( ) group = create_group(writer.file, "grids/$name") write_attribute(group, "type", "FiniteDifferenceGrid") - write_attribute(group, "topology", write!(writer, space.topology)) + write_attribute(group, "topology", write!(writer, Spaces.topology(space))) return name end diff --git a/src/Operators/numericalflux.jl b/src/Operators/numericalflux.jl index 4137e03122..d767c4cedc 100644 --- a/src/Operators/numericalflux.jl +++ b/src/Operators/numericalflux.jl @@ -25,7 +25,7 @@ See also: function add_numerical_flux_internal!(fn, dydt, args...) space = axes(dydt) Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)) - topology = space.topology + topology = Spaces.topology(space) for (iface, (elem⁻, face⁻, elem⁺, face⁺, reversed)) in enumerate(Topologies.interior_faces(topology)) @@ -101,7 +101,7 @@ end function add_numerical_flux_boundary!(fn, dydt, args...) space = axes(dydt) Nq = Spaces.Quadratures.degrees_of_freedom(Spaces.quadrature_style(space)) - topology = space.topology + topology = Spaces.topology(space) for (iboundary, boundarytag) in enumerate(Topologies.boundary_tags(topology)) diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 68eba562a6..3892989ca4 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -38,11 +38,16 @@ function ExtrudedFiniteDifferenceSpace( return ExtrudedFiniteDifferenceSpace(grid, vertical_space.staggering) end +FaceExtrudedFiniteDifferenceSpace(grid::Grids.ExtrudedFiniteDifferenceGrid) = + ExtrudedFiniteDifferenceSpace(grid, CellFace()) +CenterExtrudedFiniteDifferenceSpace(grid::Grids.ExtrudedFiniteDifferenceGrid) = + ExtrudedFiniteDifferenceSpace(grid, CellCenter()) FaceExtrudedFiniteDifferenceSpace(space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace(space.grid, CellFace()) CenterExtrudedFiniteDifferenceSpace(space::ExtrudedFiniteDifferenceSpace) = ExtrudedFiniteDifferenceSpace(space.grid, CellCenter()) + local_dss_weights(space::ExtrudedFiniteDifferenceSpace) = local_dss_weights(grid(space)) @@ -115,10 +120,10 @@ function Base.show(io::IO, space::ExtrudedFiniteDifferenceSpace) ) print(iio, " "^(indent + 2), "context: ") hspace = Spaces.horizontal_space(space) - Topologies.print_context(iio, hspace.topology.context) + Topologies.print_context(iio, Spaces.topology(hspace).context) println(iio) println(iio, " "^(indent + 2), "horizontal:") - println(iio, " "^(indent + 4), "mesh: ", hspace.topology.mesh) + println(iio, " "^(indent + 4), "mesh: ", Spaces.topology(hspace).mesh) println(iio, " "^(indent + 4), "quadrature: ", quadrature_style(hspace)) println(iio, " "^(indent + 2), "vertical:") print(iio, " "^(indent + 4), "mesh: ", space.vertical_topology.mesh) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index e3e21c5fb8..9b18b1ae3d 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -1,7 +1,12 @@ abstract type AbstractFiniteDifferenceSpace <: AbstractSpace end """ - FiniteDifferenceSpace(staggering::Staggering, grid::Grids.FiniteDifferenceGrid) + FiniteDifferenceSpace( + grid::Grids.FiniteDifferenceGrid, + staggering::Staggering, + ) + + """ struct FiniteDifferenceSpace{ @@ -11,6 +16,11 @@ struct FiniteDifferenceSpace{ grid::G staggering::S end +FiniteDifferenceSpace( + topology::Topologies.IntervalTopology, + staggering::Staggering, +) = FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(topology), staggering) + const FaceFiniteDifferenceSpace{G} = FiniteDifferenceSpace{G, CellFace} const CenterFiniteDifferenceSpace{G} = FiniteDifferenceSpace{G, CellCenter} diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index fb23dabee1..817b52f9b1 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -203,28 +203,28 @@ function Base.iterate( # this also doesn't deal with the case where eo == e if j == 1 # face 1 - eo, _, _ = Topologies.opposing_face(space.topology, e, 1) + eo, _, _ = Topologies.opposing_face(Spaces.topology(space), e, 1) if 0 < eo < e continue end end if i == Nq # face 2 - eo, _, _ = Topologies.opposing_face(space.topology, e, 2) + eo, _, _ = Topologies.opposing_face(Spaces.topology(space), e, 2) if 0 < eo < e continue end end if j == Nq # face 3 - eo, _, _ = Topologies.opposing_face(space.topology, e, 3) + eo, _, _ = Topologies.opposing_face(Spaces.topology(space), e, 3) if 0 < eo < e continue end end if i == 1 # face 4 - eo, _, _ = Topologies.opposing_face(space.topology, e, 4) + eo, _, _ = Topologies.opposing_face(Spaces.topology(space), e, 4) if 0 < eo < e continue end diff --git a/test/Spaces/ddss1.jl b/test/Spaces/ddss1.jl index 908fe35d3e..05b63e678d 100644 --- a/test/Spaces/ddss1.jl +++ b/test/Spaces/ddss1.jl @@ -66,10 +66,14 @@ init_state_vector(local_geometry, p) = Geometry.Covariant12Vector(1.0, -1.0) @test Topologies.nlocalelems(Spaces.topology(space)) == 4 - @test Topologies.local_neighboring_elements(space.topology, 1) == [2, 4] - @test Topologies.local_neighboring_elements(space.topology, 2) == [1, 3] - @test Topologies.local_neighboring_elements(space.topology, 3) == [2, 4] - @test Topologies.local_neighboring_elements(space.topology, 4) == [1, 3] + @test Topologies.local_neighboring_elements(Spaces.topology(space), 1) == + [2, 4] + @test Topologies.local_neighboring_elements(Spaces.topology(space), 2) == + [1, 3] + @test Topologies.local_neighboring_elements(Spaces.topology(space), 3) == + [2, 4] + @test Topologies.local_neighboring_elements(Spaces.topology(space), 4) == + [1, 3] y0 = init_state_scalar.(Fields.local_geometry_field(space), Ref(nothing)) nel = Topologies.nlocalelems(Spaces.topology(space)) diff --git a/test/Spaces/distributed/ddss2.jl b/test/Spaces/distributed/ddss2.jl index f2ce3d7dbe..594b7c5f5a 100644 --- a/test/Spaces/distributed/ddss2.jl +++ b/test/Spaces/distributed/ddss2.jl @@ -17,11 +17,15 @@ include("ddss_setup.jl") @test Topologies.nlocalelems(Spaces.topology(space)) == 2 - @test Topologies.local_neighboring_elements(space.topology, 1) == [2] - @test Topologies.local_neighboring_elements(space.topology, 2) == [1] + @test Topologies.local_neighboring_elements(Spaces.topology(space), 1) == + [2] + @test Topologies.local_neighboring_elements(Spaces.topology(space), 2) == + [1] - @test Topologies.ghost_neighboring_elements(space.topology, 1) == [2] - @test Topologies.ghost_neighboring_elements(space.topology, 2) == [1] + @test Topologies.ghost_neighboring_elements(Spaces.topology(space), 1) == + [2] + @test Topologies.ghost_neighboring_elements(Spaces.topology(space), 2) == + [1] init_state(local_geometry, p) = (ρ = 1.0) y0 = init_state.(Fields.local_geometry_field(space), Ref(nothing)) diff --git a/test/Spaces/distributed/ddss3.jl b/test/Spaces/distributed/ddss3.jl index acdbf994b9..9981a03ab6 100644 --- a/test/Spaces/distributed/ddss3.jl +++ b/test/Spaces/distributed/ddss3.jl @@ -39,22 +39,31 @@ partition numbers @test Topologies.nlocalelems(Spaces.topology(space)) == (pid == 1 ? 6 : 5) if pid == 1 # gidx 1 - @test Topologies.local_neighboring_elements(space.topology, 1) == - [2, 5, 6] - @test Topologies.ghost_neighboring_elements(space.topology, 1) == [] + @test Topologies.local_neighboring_elements( + Spaces.topology(space), + 1, + ) == [2, 5, 6] + @test Topologies.ghost_neighboring_elements( + Spaces.topology(space), + 1, + ) == [] # gidx 6 - @test Topologies.local_neighboring_elements(space.topology, 6) == - [1, 2, 3, 5] - @test space.topology.recv_elem_gidx[Topologies.ghost_neighboring_elements( - space.topology, + @test Topologies.local_neighboring_elements( + Spaces.topology(space), + 6, + ) == [1, 2, 3, 5] + @test Spaces.topology(space).recv_elem_gidx[Topologies.ghost_neighboring_elements( + Spaces.topology(space), 6, )] == [7, 9, 10, 11] elseif pid == 2 # gidx 7 - @test Topologies.local_neighboring_elements(space.topology, 1) == - [2, 4, 5] - @test space.topology.recv_elem_gidx[Topologies.ghost_neighboring_elements( - space.topology, + @test Topologies.local_neighboring_elements( + Spaces.topology(space), + 1, + ) == [2, 4, 5] + @test Spaces.topology(space).recv_elem_gidx[Topologies.ghost_neighboring_elements( + Spaces.topology(space), 1, )] == [2, 3, 4, 6, 12] end diff --git a/test/Spaces/distributed_cuda/ddss2.jl b/test/Spaces/distributed_cuda/ddss2.jl index 8b26bad751..c7e5748c10 100644 --- a/test/Spaces/distributed_cuda/ddss2.jl +++ b/test/Spaces/distributed_cuda/ddss2.jl @@ -59,11 +59,15 @@ pid, nprocs = ClimaComms.init(context) @test Topologies.nlocalelems(Spaces.topology(space)) == 2 - @test Topologies.local_neighboring_elements(space.topology, 1) == [2] - @test Topologies.local_neighboring_elements(space.topology, 2) == [1] + @test Topologies.local_neighboring_elements(Spaces.topology(space), 1) == + [2] + @test Topologies.local_neighboring_elements(Spaces.topology(space), 2) == + [1] - @test Topologies.ghost_neighboring_elements(space.topology, 1) == [2] - @test Topologies.ghost_neighboring_elements(space.topology, 2) == [1] + @test Topologies.ghost_neighboring_elements(Spaces.topology(space), 1) == + [2] + @test Topologies.ghost_neighboring_elements(Spaces.topology(space), 2) == + [1] init_state(local_geometry, p) = (ρ = 1.0) y0 = init_state.(Fields.local_geometry_field(space), Ref(nothing)) diff --git a/test/Spaces/distributed_cuda/ddss3.jl b/test/Spaces/distributed_cuda/ddss3.jl index fcf39a16f1..1bca8d268b 100644 --- a/test/Spaces/distributed_cuda/ddss3.jl +++ b/test/Spaces/distributed_cuda/ddss3.jl @@ -80,22 +80,31 @@ partition numbers @test Topologies.nlocalelems(Spaces.topology(space)) == (pid == 1 ? 6 : 5) if pid == 1 # gidx 1 - @test Topologies.local_neighboring_elements(space.topology, 1) == - [2, 5, 6] - @test Topologies.ghost_neighboring_elements(space.topology, 1) == [] + @test Topologies.local_neighboring_elements( + Spaces.topology(space), + 1, + ) == [2, 5, 6] + @test Topologies.ghost_neighboring_elements( + Spaces.topology(space), + 1, + ) == [] # gidx 6 - @test Topologies.local_neighboring_elements(space.topology, 6) == - [1, 2, 3, 5] - @test space.topology.recv_elem_gidx[Topologies.ghost_neighboring_elements( - space.topology, + @test Topologies.local_neighboring_elements( + Spaces.topology(space), + 6, + ) == [1, 2, 3, 5] + @test Spaces.topology(space).recv_elem_gidx[Topologies.ghost_neighboring_elements( + Spaces.topology(space), 6, )] == [7, 9, 10, 11] elseif pid == 2 # gidx 7 - @test Topologies.local_neighboring_elements(space.topology, 1) == - [2, 4, 5] - @test space.topology.recv_elem_gidx[Topologies.ghost_neighboring_elements( - space.topology, + @test Topologies.local_neighboring_elements( + Spaces.topology(space), + 1, + ) == [2, 4, 5] + @test Spaces.topology(space).recv_elem_gidx[Topologies.ghost_neighboring_elements( + Spaces.topology(space), 1, )] == [2, 3, 4, 6, 12] end From 529d48b60e869ae79db90c142eb354046a5b6bbe Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 10:47:09 -0700 Subject: [PATCH 27/42] more fixes --- examples/hybrid/sphere/deformation_flow.jl | 2 +- examples/sphere/limiters_advection.jl | 2 +- examples/sphere/solidbody.jl | 2 +- lib/ClimaCoreMakie/src/utils.jl | 2 +- lib/ClimaCoreVTK/src/ClimaCoreVTK.jl | 2 +- lib/ClimaCoreVTK/src/space.jl | 5 +++-- src/Fields/broadcast.jl | 2 +- src/Grids/column.jl | 1 + src/Grids/extruded.jl | 2 ++ src/Grids/finitedifference.jl | 1 + src/Grids/level.jl | 1 + src/Grids/spectralelement.jl | 1 + src/Spaces/Spaces.jl | 3 +++ 13 files changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/hybrid/sphere/deformation_flow.jl b/examples/hybrid/sphere/deformation_flow.jl index db995ae2f8..f56071e2ce 100644 --- a/examples/hybrid/sphere/deformation_flow.jl +++ b/examples/hybrid/sphere/deformation_flow.jl @@ -216,7 +216,7 @@ function run_deformation_flow(use_limiter, fct_op) Geometry.LatLongZPoint(ϕ_c, λ_c1, FT(0)), Geometry.LatLongZPoint(ϕ_c, λ_c2, FT(0)), ) - horz_geometry = horz_space.global_geometry + horz_geometry = Spaces.global_geometry(horz_space) rds = map(centers) do center Geometry.great_circle_distance(coord, center, horz_geometry) end diff --git a/examples/sphere/limiters_advection.jl b/examples/sphere/limiters_advection.jl index 49e7e0ea98..d5bc54e0d0 100644 --- a/examples/sphere/limiters_advection.jl +++ b/examples/sphere/limiters_advection.jl @@ -113,7 +113,7 @@ for (k, ne) in enumerate(ne_seq) coords = Fields.coordinate_field(space) Δh[k] = 2 * R / ne - global_geom = space.global_geometry + global_geom = Spaces.global_geometry(space) # Initialize state y0 = map(coords) do coord diff --git a/examples/sphere/solidbody.jl b/examples/sphere/solidbody.jl index 2e1758c187..a7edc694d4 100644 --- a/examples/sphere/solidbody.jl +++ b/examples/sphere/solidbody.jl @@ -87,7 +87,7 @@ for (k, ne) in enumerate(ne_seq) Δh[k] = 2 * R / ne - global_geom = space.global_geometry + global_geom = Spaces.global_geometry(space) h_init = map(coords) do coord rd = Geometry.great_circle_distance(coord, center, global_geom) diff --git a/lib/ClimaCoreMakie/src/utils.jl b/lib/ClimaCoreMakie/src/utils.jl index 223dde88e3..4c183e7236 100644 --- a/lib/ClimaCoreMakie/src/utils.jl +++ b/lib/ClimaCoreMakie/src/utils.jl @@ -22,7 +22,7 @@ function plot_vertices( space, ClimaCore.Geometry.Cartesian123Point.( ClimaCore.Fields.coordinate_field(space), - Ref(space.global_geometry), + Ref(Spaces.global_geometry(space)), ), ) end diff --git a/lib/ClimaCoreVTK/src/ClimaCoreVTK.jl b/lib/ClimaCoreVTK/src/ClimaCoreVTK.jl index 57638b5545..e03b90e7a0 100644 --- a/lib/ClimaCoreVTK/src/ClimaCoreVTK.jl +++ b/lib/ClimaCoreVTK/src/ClimaCoreVTK.jl @@ -52,7 +52,7 @@ function WriteVTK.vtk_grid( coords = Geometry.Cartesian123Point.( Fields.coordinate_field(gridspace), - Ref(gridspace.global_geometry), + Ref(Spaces.global_geometry(gridspace)), ) coord_vecs = ( vec(parent(coords.x1)), diff --git a/lib/ClimaCoreVTK/src/space.jl b/lib/ClimaCoreVTK/src/space.jl index 7bab99f87e..e5ca6f25cb 100644 --- a/lib/ClimaCoreVTK/src/space.jl +++ b/lib/ClimaCoreVTK/src/space.jl @@ -197,7 +197,8 @@ end function vtk_grid_space(space::Spaces.FaceExtrudedFiniteDifferenceSpace) # this will need to be updated for warped meshes horizontal_space = vtk_grid_space(Spaces.horizontal_space(space)) - vertical_space = Spaces.FaceFiniteDifferenceSpace(space.vertical_topology) + vertical_space = + Spaces.FaceFiniteDifferenceSpace(Spaces.vertical_topology(space)) return Spaces.ExtrudedFiniteDifferenceSpace( horizontal_space, vertical_space, @@ -246,7 +247,7 @@ function vtk_cell_space(gridspace::Spaces.FaceExtrudedFiniteDifferenceSpace) # this will need to be updated for warped meshes horizontal_space = vtk_cell_space(Spaces.horizontal_space(gridspace)) vertical_space = - Spaces.CenterFiniteDifferenceSpace(gridspace.vertical_topology) + Spaces.CenterFiniteDifferenceSpace(Spaces.vertical_topology(gridspace)) return Spaces.ExtrudedFiniteDifferenceSpace( horizontal_space, vertical_space, diff --git a/src/Fields/broadcast.jl b/src/Fields/broadcast.jl index a5f5756373..a03e961333 100644 --- a/src/Fields/broadcast.jl +++ b/src/Fields/broadcast.jl @@ -419,7 +419,7 @@ function Base.Broadcast.broadcasted( fs, V, arg, - tuple(space.global_geometry), + tuple(Spaces.global_geometry(space)), local_geometry_field(space), ) end diff --git a/src/Grids/column.jl b/src/Grids/column.jl index b7507460c3..7cc6ab26f6 100644 --- a/src/Grids/column.jl +++ b/src/Grids/column.jl @@ -45,3 +45,4 @@ local_geometry_data(colgrid::ColumnGrid, staggering::Staggering) = column( local_geometry_data(colgrid.full_grid, staggering::Staggering), colgrid.colidx, ) +global_geometry(colgrid::ColumnGrid) = global_geometry(colgrid.full_grid) diff --git a/src/Grids/extruded.jl b/src/Grids/extruded.jl index f1b32a7abe..1a1fe7329a 100644 --- a/src/Grids/extruded.jl +++ b/src/Grids/extruded.jl @@ -77,6 +77,8 @@ local_geometry_data(grid::AbstractExtrudedFiniteDifferenceGrid, ::CellCenter) = grid.center_local_geometry local_geometry_data(grid::AbstractExtrudedFiniteDifferenceGrid, ::CellFace) = grid.face_local_geometry +global_geometry(grid::AbstractExtrudedFiniteDifferenceGrid) = + grid.global_geometry quadrature_style(grid::ExtrudedFiniteDifferenceGrid) = quadrature_style(grid.horizontal_grid) diff --git a/src/Grids/finitedifference.jl b/src/Grids/finitedifference.jl index d99897360e..3bce839536 100644 --- a/src/Grids/finitedifference.jl +++ b/src/Grids/finitedifference.jl @@ -147,3 +147,4 @@ local_geometry_data(grid::FiniteDifferenceGrid, ::CellCenter) = grid.center_local_geometry local_geometry_data(grid::FiniteDifferenceGrid, ::CellFace) = grid.face_local_geometry +global_geometry(grid::FiniteDifferenceGrid) = grid.global_geometry diff --git a/src/Grids/level.jl b/src/Grids/level.jl index 7601bb12d9..87159c9e9c 100644 --- a/src/Grids/level.jl +++ b/src/Grids/level.jl @@ -20,3 +20,4 @@ local_geometry_data(colgrid::LevelGrid{<:Any, PlusHalf{Int}}, ::Nothing) = local_geometry_data(levelgrid.full_grid, CellFace()), levelgrid.level + half, ) +global_geometry(levlgrid::LevelGrid) = global_geometry(levlgrid.full_grid) diff --git a/src/Grids/spectralelement.jl b/src/Grids/spectralelement.jl index e0544c02c2..7917808512 100644 --- a/src/Grids/spectralelement.jl +++ b/src/Grids/spectralelement.jl @@ -474,6 +474,7 @@ topology(grid::AbstractSpectralElementGrid) = grid.topology local_geometry_data(grid::AbstractSpectralElementGrid, ::Nothing) = grid.local_geometry +global_geometry(grid::AbstractSpectralElementGrid) = grid.global_geometry quadrature_style(grid::AbstractSpectralElementGrid) = grid.quadrature_style local_dss_weights(grid::SpectralElementGrid1D) = grid.dss_weights diff --git a/src/Spaces/Spaces.jl b/src/Spaces/Spaces.jl index ce83bbd0b0..99ab5388e7 100644 --- a/src/Spaces/Spaces.jl +++ b/src/Spaces/Spaces.jl @@ -30,6 +30,7 @@ import ..Grids: topology, vertical_topology, local_geometry_data, + global_geometry, local_dss_weights, quadrature_style @@ -63,6 +64,8 @@ vertical_topology(space::AbstractSpace) = vertical_topology(grid(space)) local_geometry_data(space::AbstractSpace) = local_geometry_data(grid(space), staggering(space)) +global_geometry(space::AbstractSpace) = global_geometry(grid(space)) + space(refspace::AbstractSpace, staggering::Staggering) = space(grid(refspace), staggering) From bf5f5fd5e16de76827992f3da3aef38545e6b16d Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 12:11:04 -0700 Subject: [PATCH 28/42] fix plots --- lib/ClimaCorePlots/src/ClimaCorePlots.jl | 35 +++++++---------- src/Fields/Fields.jl | 38 +++++++++++++++++- src/Grids/extruded.jl | 11 ++++++ src/Grids/level.jl | 7 +++- src/Grids/spectralelement.jl | 22 +++-------- src/Spaces/extruded.jl | 49 +++++++++++------------- src/Spaces/spectralelement.jl | 16 +++----- src/Topologies/topology2d.jl | 7 ++++ 8 files changed, 107 insertions(+), 78 deletions(-) diff --git a/lib/ClimaCorePlots/src/ClimaCorePlots.jl b/lib/ClimaCorePlots/src/ClimaCorePlots.jl index 5181862303..989bfab9e6 100644 --- a/lib/ClimaCorePlots/src/ClimaCorePlots.jl +++ b/lib/ClimaCorePlots/src/ClimaCorePlots.jl @@ -61,7 +61,7 @@ RecipesBase.@recipe function f( (xcoords, xdata) end -RecipesBase.@recipe function f(space::Spaces.SpectralElementSpace2D;) +RecipesBase.@recipe function f(space::Spaces.RectilinearSpectralElementSpace2D;) quad = Spaces.quadrature_style(space) quad_name = Base.typename(typeof(quad)).name dof = Spaces.Quadratures.degrees_of_freedom(quad) @@ -152,7 +152,7 @@ RecipesBase.@recipe function f(field::Fields.FiniteDifferenceField) end RecipesBase.@recipe function f( - field::Fields.SpectralElementField2D; + field::Fields.RectilinearSpectralElementField2D; interpolate = 10, ) @assert interpolate ≥ 1 "number of element quadrature points for uniform interpolation must be ≥ 1" @@ -198,7 +198,7 @@ function _slice_triplot(field, hinterpolate, ncolors) space = axes(field) htopology = Spaces.topology(space) hdomain = Topologies.domain(htopology) - vdomain = Topologies.domain(space.vertical_topology) + vdomain = Topologies.domain(Spaces.vertical_topology(space)) @assert Nj == 1 @@ -249,10 +249,10 @@ end # 2D hybrid plot RecipesBase.@recipe function f( - field::Fields.Field{<:Any, S}; + field::Fields.ExtrudedSpectralElementField2D; hinterpolate = 0, ncolors = 256, -) where {S <: Spaces.ExtrudedFiniteDifferenceSpace2D} +) hcoord, vcoord, data = _slice_triplot(field, hinterpolate, ncolors) coord_symbols = propertynames(Fields.coordinate_field(axes(field))) @@ -301,7 +301,7 @@ function _slice_along(field, coord) hidx = axis == 1 ? linear_idx[slice_h, 1] : linear_idx[1, slice_h] # find the node idx we want to slice along the given axis element - hcoord_data = hspace.local_geometry.coordinates + hcoord_data = Spaces.local_geometry_data(hspace).coordinates hdata = ClimaCore.slab(hcoord_data, hidx) hnode_idx = 1 for i in axes(hdata)[axis] @@ -320,8 +320,9 @@ function _slice_along(field, coord) htopo_ortho, ClimaCore.Spaces.quadrature_style(hspace), ) - vspace_ortho = - ClimaCore.Spaces.CenterFiniteDifferenceSpace(space.vertical_topology) + vspace_ortho = ClimaCore.Spaces.CenterFiniteDifferenceSpace( + Spaces.vertical_topology(space), + ) if space.staggering === ClimaCore.Spaces.CellFace vspace_ortho = ClimaCore.Spaces.FaceFiniteDifferenceSpace(slice_vspace) @@ -355,16 +356,11 @@ end # 3D hybrid plot RecipesBase.@recipe function f( - field::Fields.Field{<:Any, S}; + field::Fields.ExtrudedFiniteDifferenceField3D; slice = nothing, hinterpolate = 0, ncolors = 256, -) where { - S <: Spaces.ExtrudedFiniteDifferenceSpace3D{ - <:Any, - <:Spaces.RectilinearSpectralElementSpace2D, - }, -} +) if slice === nothing error("must specify coordinate axis slice for 3D hybrid plots") end @@ -499,15 +495,10 @@ RecipesBase.@recipe function f( end RecipesBase.@recipe function f( - field::Fields.Field{<:Any, S}; + field::Fields.ExtrudedCubedSphereSpectralElementField3D; level = nothing, hinterpolate = 10, -) where { - S <: Spaces.ExtrudedFiniteDifferenceSpace{ - <:Any, - <:Spaces.CubedSphereSpectralElementSpace2D, - }, -} +) @assert hinterpolate ≥ 1 "number of element quadrature points for uniform interpolation must be ≥ 1" space = axes(field) diff --git a/src/Fields/Fields.jl b/src/Fields/Fields.jl index 95231aa4b4..3405f7f4b0 100644 --- a/src/Fields/Fields.jl +++ b/src/Fields/Fields.jl @@ -48,7 +48,7 @@ Adapt.adapt_structure(to, field::Field) = Field( Adapt.adapt(to, axes(field)), ) - +## aliases # Point Field const PointField{V, S} = Field{V, S} where {V <: AbstractData, S <: Spaces.PointSpace} @@ -77,6 +77,14 @@ const ExtrudedFiniteDifferenceField{V, S} = Field{ V, S, } where {V <: AbstractData, S <: Spaces.ExtrudedFiniteDifferenceSpace} +const ExtrudedFiniteDifferenceField2D{V, S} = Field{ + V, + S, +} where {V <: AbstractData, S <: Spaces.ExtrudedFiniteDifferenceSpace2D} +const ExtrudedFiniteDifferenceField3D{V, S} = Field{ + V, + S, +} where {V <: AbstractData, S <: Spaces.ExtrudedFiniteDifferenceSpace3D} const FaceExtrudedFiniteDifferenceField{V, S} = Field{ V, S, @@ -86,12 +94,40 @@ const CenterExtrudedFiniteDifferenceField{V, S} = Field{ S, } where {V <: AbstractData, S <: Spaces.CenterExtrudedFiniteDifferenceSpace} +# +const SpectralElementField1D{V, S} = + Field{V, S} where {V <: AbstractData, S <: Spaces.SpectralElementSpace1D} +const ExtrudedSpectralElementField2D{V, S} = Field{ + V, + S, +} where {V <: AbstractData, S <: Spaces.ExtrudedSpectralElementSpace2D} + +const RectilinearSpectralElementField2D{V, S} = Field{ + V, + S, +} where {V <: AbstractData, S <: Spaces.RectilinearSpectralElementSpace2D} +const ExtrudedRectilinearSpectralElementField3D{V, S} = Field{ + V, + S, +} where { + V <: AbstractData, + S <: Spaces.ExtrudedRectilinearSpectralElementSpace3D, +} + + # Cubed Sphere Fields const CubedSphereSpectralElementField2D{V, S} = Field{ V, S, } where {V <: AbstractData, S <: Spaces.CubedSphereSpectralElementSpace2D} +const ExtrudedCubedSphereSpectralElementField3D{V, S} = Field{ + V, + S, +} where { + V <: AbstractData, + S <: Spaces.ExtrudedCubedSphereSpectralElementSpace3D, +} Base.propertynames(field::Field) = propertynames(getfield(field, :values)) @inline field_values(field::Field) = getfield(field, :values) diff --git a/src/Grids/extruded.jl b/src/Grids/extruded.jl index 1a1fe7329a..4b82f2a3bd 100644 --- a/src/Grids/extruded.jl +++ b/src/Grids/extruded.jl @@ -103,3 +103,14 @@ Adapt.adapt_structure(to, grid::ExtrudedFiniteDifferenceGrid) = quadrature_style(grid::DeviceExtrudedFiniteDifferenceGrid) = grid.quadrature_style + +## aliases + +const ExtrudedSpectralElementGrid2D = + ExtrudedFiniteDifferenceGrid{<:SpectralElementGrid1D} +const ExtrudedSpectralElementGrid3D = + ExtrudedFiniteDifferenceGrid{<:SpectralElementGrid2D} +const ExtrudedRectilinearSpectralElementGrid3D = + ExtrudedFiniteDifferenceGrid{<:RectilinearSpectralElementGrid2D} +const ExtrudedCubedSphereSpectralElementGrid3D = + ExtrudedFiniteDifferenceGrid{<:CubedSphereSpectralElementGrid2D} diff --git a/src/Grids/level.jl b/src/Grids/level.jl index 87159c9e9c..3d3ac391f2 100644 --- a/src/Grids/level.jl +++ b/src/Grids/level.jl @@ -6,16 +6,19 @@ struct LevelGrid{ level::L end +quadrature_style(levelgrid::LevelGrid) = + quadrature_style(levelgrid.full_grid.horizontal_grid) + level(grid::ExtrudedFiniteDifferenceGrid, level::Union{Int, PlusHalf{Int}}) = LevelGrid(grid, level) topology(levelgrid::LevelGrid) = topology(levelgrid.full_grid) -local_geometry_data(colgrid::LevelGrid{<:Any, Int}, ::Nothing) = level( +local_geometry_data(levelgrid::LevelGrid{<:Any, Int}, ::Nothing) = level( local_geometry_data(levelgrid.full_grid, CellCenter()), levelgrid.level, ) -local_geometry_data(colgrid::LevelGrid{<:Any, PlusHalf{Int}}, ::Nothing) = +local_geometry_data(levelgrid::LevelGrid{<:Any, PlusHalf{Int}}, ::Nothing) = level( local_geometry_data(levelgrid.full_grid, CellFace()), levelgrid.level + half, diff --git a/src/Grids/spectralelement.jl b/src/Grids/spectralelement.jl index 7917808512..d46cafe955 100644 --- a/src/Grids/spectralelement.jl +++ b/src/Grids/spectralelement.jl @@ -480,23 +480,7 @@ quadrature_style(grid::AbstractSpectralElementGrid) = grid.quadrature_style local_dss_weights(grid::SpectralElementGrid1D) = grid.dss_weights local_dss_weights(grid::SpectralElementGrid2D) = grid.local_dss_weights - -const RectilinearSpectralElementGrid2D = SpectralElementGrid2D{ - <:Topologies.Topology2D{ - <:ClimaComms.AbstractCommsContext, - <:Meshes.RectilinearMesh, - }, -} - -const CubedSphereSpectralElementGrid2D = SpectralElementGrid2D{ - <:Topologies.Topology2D{ - <:ClimaComms.AbstractCommsContext, - <:Meshes.AbstractCubedSphere, - }, -} - ## GPU compatibility - struct DeviceSpectralElementGrid2D{Q, GG, LG} <: AbstractSpectralElementGrid quadrature_style::Q global_geometry::GG @@ -509,3 +493,9 @@ Adapt.adapt_structure(to, grid::SpectralElementGrid2D) = Adapt.adapt(to, grid.global_geometry), Adapt.adapt(to, grid.local_geometry), ) + +## aliases +const RectilinearSpectralElementGrid2D = + SpectralElementGrid2D{<:Topologies.RectilinearTopology2D} +const CubedSphereSpectralElementGrid2D = + SpectralElementGrid2D{<:Topologies.CubedSphereTopology2D} diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 3892989ca4..fc77020af7 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -90,8 +90,12 @@ const ExtrudedFiniteDifferenceSpace2D = ExtrudedFiniteDifferenceSpace{ <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid1D}, } const ExtrudedFiniteDifferenceSpace3D = ExtrudedFiniteDifferenceSpace{ - <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid1D}, + <:Grids.ExtrudedFiniteDifferenceGrid{<:Grids.SpectralElementGrid2D}, } +const ExtrudedSpectralElementSpace2D = + ExtrudedFiniteDifferenceSpace{<:Grids.ExtrudedSpectralElementGrid2D} +const ExtrudedSpectralElementSpace3D = + ExtrudedFiniteDifferenceSpace{<:Grids.ExtrudedSpectralElementGrid3D} const CenterExtrudedFiniteDifferenceSpace2D = CenterExtrudedFiniteDifferenceSpace{ @@ -168,32 +172,14 @@ end column(space::ExtrudedFiniteDifferenceSpace, i, j, h) = column(space, Grids.ColumnIndex((i, j), h)) - -struct LevelSpace{S, L} <: AbstractSpace - space::S - level::L -end - -level(space::CenterExtrudedFiniteDifferenceSpace, v::Integer) = - LevelSpace(space, v) -level(space::FaceExtrudedFiniteDifferenceSpace, v::PlusHalf) = - LevelSpace(space, v) - -function local_geometry_data( - levelspace::LevelSpace{<:CenterExtrudedFiniteDifferenceSpace, <:Integer}, -) - level(local_geometry_data(levelspace.space), levelspace.level) -end -function local_geometry_data( - levelspace::LevelSpace{<:FaceExtrudedFiniteDifferenceSpace, <:PlusHalf}, -) - level(local_geometry_data(levelspace.space), levelspace.level + half) -end - -function column(levelspace::LevelSpace, args...) - local_geometry = column(local_geometry_data(levelspace), args...) - PointSpace(local_geometry) -end +level(space::CenterExtrudedFiniteDifferenceSpace2D, v::Integer) = + SpectralElementSpace1D(level(grid(space), v)) +level(space::FaceExtrudedFiniteDifferenceSpace2D, v::PlusHalf) = + SpectralElementSpace1D(level(grid(space), v)) +level(space::CenterExtrudedFiniteDifferenceSpace3D, v::Integer) = + SpectralElementSpace2D(level(grid(space), v)) +level(space::FaceExtrudedFiniteDifferenceSpace3D, v::PlusHalf) = + SpectralElementSpace2D(level(grid(space), v)) nlevels(space::ExtrudedFiniteDifferenceSpace) = @@ -218,3 +204,12 @@ function eachslabindex(fspace::FaceExtrudedFiniteDifferenceSpace) Nv = size(fspace.face_local_geometry, 4) return Iterators.product(1:Nv, h_iter) end + + +## aliases +const ExtrudedRectilinearSpectralElementSpace3D = ExtrudedFiniteDifferenceSpace{ + <:Grids.ExtrudedRectilinearSpectralElementGrid3D, +} +const ExtrudedCubedSphereSpectralElementSpace3D = ExtrudedFiniteDifferenceSpace{ + <:Grids.ExtrudedCubedSphereSpectralElementGrid3D, +} diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 817b52f9b1..2439e339e9 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -69,16 +69,6 @@ function SpectralElementSpace2D( SpectralElementSpace2D(grid) end - - -const RectilinearSpectralElementSpace2D = - SpectralElementSpace2D{<:Grids.RectilinearSpectralElementGrid2D} -const CubedSphereSpectralElementSpace2D = - SpectralElementSpace2D{<:Grids.CubedSphereSpectralElementGrid2D} - - - - """ SpectralElementSpaceSlab <: AbstractSpace @@ -232,3 +222,9 @@ function Base.iterate( return ((i, j), e), ((i, j), e) end end + +## aliases +const RectilinearSpectralElementSpace2D = + SpectralElementSpace2D{<:Grids.RectilinearSpectralElementGrid2D} +const CubedSphereSpectralElementSpace2D = + SpectralElementSpace2D{<:Grids.CubedSphereSpectralElementGrid2D} diff --git a/src/Topologies/topology2d.jl b/src/Topologies/topology2d.jl index 8607029ba3..270c199283 100644 --- a/src/Topologies/topology2d.jl +++ b/src/Topologies/topology2d.jl @@ -765,3 +765,10 @@ function Base.getindex(perimeter::Perimeter2D{Nq}, loc = 1) where {Nq} return face_node_index(f, Nq, 1 + n) end end + + +## aliases +const RectilinearTopology2D = + Topology2D{<:ClimaComms.AbstractCommsContext, <:Meshes.RectilinearMesh} +const CubedSphereTopology2D = + Topology2D{<:ClimaComms.AbstractCommsContext, <:Meshes.AbstractCubedSphere} From fa3bcddb118203cc71b35ef6309a472824815f34 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 12:30:30 -0700 Subject: [PATCH 29/42] add create_dss_buffer method --- src/Spaces/dss.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl index b41f0de70d..b65e02a384 100644 --- a/src/Spaces/dss.jl +++ b/src/Spaces/dss.jl @@ -26,7 +26,7 @@ Creates a [`DSSBuffer`](@ref) for the field data corresponding to `data` """ function create_dss_buffer( data::Union{DataLayouts.IJFH{S, Nij}, DataLayouts.VIJFH{S, Nij}}, - hspace::AbstractSpectralElementSpace, + hspace::SpectralElementSpace2D, ) where {S, Nij} create_dss_buffer( data, @@ -36,6 +36,12 @@ function create_dss_buffer( ) end +function create_dss_buffer( + data::Union{DataLayouts.IFH{S, Nij}, DataLayouts.VIFH{S, Nij}}, + hspace::SpectralElementSpace1D, +) where {S, Nij} + nothing +end """ function weighted_dss!( From 51757ea4fb951cde020fa7158d9933b1f64176d9 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 14:24:31 -0700 Subject: [PATCH 30/42] fix docs --- docs/Manifest.toml | 2 +- docs/src/api.md | 49 ++++++++++++++++----------------- src/Spaces/spectralelement.jl | 6 ++++ src/Topologies/Topologies.jl | 1 + src/Topologies/dss.jl | 18 ++++++------ src/Topologies/dss_transform.jl | 4 +-- 6 files changed, 43 insertions(+), 37 deletions(-) diff --git a/docs/Manifest.toml b/docs/Manifest.toml index 1e12f20336..df23b6e734 100644 --- a/docs/Manifest.toml +++ b/docs/Manifest.toml @@ -549,7 +549,7 @@ version = "0.9.3" [[deps.Documenter]] deps = ["ANSIColoredPrinters", "AbstractTrees", "Base64", "Dates", "DocStringExtensions", "Downloads", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "MarkdownAST", "Pkg", "PrecompileTools", "REPL", "RegistryInstances", "SHA", "Test", "Unicode"] -git-tree-sha1 = "662fb21ae7fad33e044c2b59ece832fdce32c171" +path = "/Users/simon/src/Documenter" uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4" version = "1.1.2" diff --git a/docs/src/api.md b/docs/src/api.md index b375df9730..93a75ba254 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -184,7 +184,6 @@ Spaces.Δz_metric_component ```@docs Spaces.SpectralElementSpace1D Spaces.SpectralElementSpace2D -Spaces.SpectralElementSpace2D(topology, quadrature_style; enable_bubble) Spaces.SpectralElementSpaceSlab ``` @@ -192,39 +191,39 @@ Spaces.SpectralElementSpaceSlab ```@docs -Spaces.Quadratures.QuadratureStyle -Spaces.Quadratures.GLL -Spaces.Quadratures.GL -Spaces.Quadratures.Uniform -Spaces.Quadratures.degrees_of_freedom -Spaces.Quadratures.polynomial_degree -Spaces.Quadratures.quadrature_points -Spaces.Quadratures.barycentric_weights -Spaces.Quadratures.interpolation_matrix -Spaces.Quadratures.differentiation_matrix -Spaces.Quadratures.orthonormal_poly +Quadratures.QuadratureStyle +Quadratures.GLL +Quadratures.GL +Quadratures.Uniform +Quadratures.degrees_of_freedom +Quadratures.polynomial_degree +Quadratures.quadrature_points +Quadratures.barycentric_weights +Quadratures.interpolation_matrix +Quadratures.differentiation_matrix +Quadratures.orthonormal_poly ``` #### Internals ```@docs -Spaces.dss_transform -Spaces.dss_transform! -Spaces.dss_untransform! -Spaces.dss_untransform -Spaces.dss_local_vertices! -Spaces.dss_local! -Spaces.dss_local_ghost! -Spaces.dss_ghost! -Spaces.create_dss_buffer -Spaces.fill_send_buffer! -Spaces.DSSBuffer -Spaces.load_from_recv_buffer! +Topologies.dss_transform +Topologies.dss_transform! +Topologies.dss_untransform! +Topologies.dss_untransform +Topologies.dss_local_vertices! +Topologies.dss_local! +Topologies.dss_local_ghost! +Topologies.dss_ghost! +Topologies.create_dss_buffer +Topologies.fill_send_buffer! +Topologies.DSSBuffer +Topologies.load_from_recv_buffer! +Topologies.dss! Spaces.weighted_dss_start! Spaces.weighted_dss_internal! Spaces.weighted_dss_ghost! Spaces.weighted_dss! -Spaces.dss! Spaces.unique_nodes ``` diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 2439e339e9..0202ad806d 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -36,6 +36,9 @@ end # 1D +""" + SpectralElementSpace1D(grid::SpectralElementGrid1D) +""" struct SpectralElementSpace1D{G} <: AbstractSpectralElementSpace grid::G end @@ -54,6 +57,9 @@ end # 2D +""" + SpectralElementSpace2D(grid::SpectralElementGrid1D) +""" struct SpectralElementSpace2D{G} <: AbstractSpectralElementSpace grid::G end diff --git a/src/Topologies/Topologies.jl b/src/Topologies/Topologies.jl index 61bc33685d..8ce97ffe22 100644 --- a/src/Topologies/Topologies.jl +++ b/src/Topologies/Topologies.jl @@ -4,6 +4,7 @@ using DocStringExtensions import ClimaComms, Adapt, CUDA +import ..ClimaCore import ..Geometry import ..Domains: Domains, coordinate_type import ..Meshes: Meshes, domain, coordinates diff --git a/src/Topologies/dss.jl b/src/Topologies/dss.jl index 87944b6e0a..b478084087 100644 --- a/src/Topologies/dss.jl +++ b/src/Topologies/dss.jl @@ -229,7 +229,7 @@ Arguments: - `perimeter`: perimeter iterator - `localelems`: list of local elements to perform transformation operations on -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function dss_transform!( device::ClimaComms.AbstractDevice, @@ -281,7 +281,7 @@ Arguments: - `perimeter`: perimeter iterator - `localelems`: list of local elements to perform transformation operations on -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function dss_untransform!( device::ClimaComms.AbstractDevice, @@ -338,7 +338,7 @@ Arguments: - `covariant12fidx`: field index for Covariant12 vector fields in the data layout - `localelems`: list of local elements to perform transformation operations on -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function dss_transform!( ::ClimaComms.AbstractCPUDevice, @@ -438,7 +438,7 @@ Arguments: - `scalarfidx`: field index for scalar fields in the data layout - `covariant12fidx`: field index for Covariant12 vector fields in the data layout -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function dss_untransform!( @@ -555,7 +555,7 @@ end Performs DSS on local vertices and faces. -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function dss_local!( ::ClimaComms.AbstractCPUDevice, @@ -643,7 +643,7 @@ Computes the "local" part of ghost vertex dss. (i.e. it computes the summation o vertices of a unique ghost vertex and stores the value in each of the local vertex locations in `perimeter_data`) -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function dss_local_ghost!( ::ClimaComms.AbstractCPUDevice, @@ -697,7 +697,7 @@ end Sets the value for all local vertices of each unique ghost vertex, in `perimeter_data`, to that of the representative ghost vertex. -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function dss_ghost!( device::ClimaComms.AbstractCPUDevice, @@ -737,7 +737,7 @@ end Loads the send buffer from `perimeter_data`. For unique ghost vertices, only data from the representative vertices which store result of "ghost local" DSS are loaded. -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function fill_send_buffer!( ::ClimaComms.AbstractCPUDevice, @@ -766,7 +766,7 @@ Adds data from the recv buffer to the corresponding location in `perimeter_data` For ghost vertices, this data is added only to the representative vertices. The values are then scattered to other local vertices corresponding to each unique ghost vertex in `dss_local_ghost`. -Part of [`Spaces.weighted_dss!`](@ref). +Part of [`ClimaCore.Spaces.weighted_dss!`](@ref). """ function load_from_recv_buffer!( ::ClimaComms.AbstractCPUDevice, diff --git a/src/Topologies/dss_transform.jl b/src/Topologies/dss_transform.jl index 3bb89c9623..466a6f641e 100644 --- a/src/Topologies/dss_transform.jl +++ b/src/Topologies/dss_transform.jl @@ -10,7 +10,7 @@ Transformations only apply to vector quantities. - `local_geometry[I...]` is the relevant `LocalGeometry` object. If it is `nothing`, then no transformation is performed - `weight[I...]` is the relevant DSS weights. If `weight` is `nothing`, then the result is simply summation. -See [`Spaces.weighted_dss!`](@ref). +See [`ClimaCore.Spaces.weighted_dss!`](@ref). """ Base.@propagate_inbounds dss_transform(arg, local_geometry, weight, i, j) = dss_transform(arg[i, j], local_geometry[i, j], weight[i, j]) @@ -146,7 +146,7 @@ end Transform `targ[I...]` back to a value of type `T` after performing direct stiffness summation (DSS). -See [`Spaces.weighted_dss!`](@ref). +See [`ClimaCore.Spaces.weighted_dss!`](@ref). """ Base.@propagate_inbounds dss_untransform( ::Type{T}, From e37a0c477d14b0df32845621489ac592af9efee4 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 15:20:14 -0700 Subject: [PATCH 31/42] more fixes --- docs/Manifest.toml | 2 +- src/InputOutput/readers.jl | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/Manifest.toml b/docs/Manifest.toml index df23b6e734..1e12f20336 100644 --- a/docs/Manifest.toml +++ b/docs/Manifest.toml @@ -549,7 +549,7 @@ version = "0.9.3" [[deps.Documenter]] deps = ["ANSIColoredPrinters", "AbstractTrees", "Base64", "Dates", "DocStringExtensions", "Downloads", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "MarkdownAST", "Pkg", "PrecompileTools", "REPL", "RegistryInstances", "SHA", "Test", "Unicode"] -path = "/Users/simon/src/Documenter" +git-tree-sha1 = "662fb21ae7fad33e044c2b59ece832fdce32c171" uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4" version = "1.1.2" diff --git a/src/InputOutput/readers.jl b/src/InputOutput/readers.jl index 6d9b329b04..3c25aa535e 100644 --- a/src/InputOutput/readers.jl +++ b/src/InputOutput/readers.jl @@ -324,19 +324,19 @@ function read_grid_new(reader, name) _scan_quadrature_style(attrs(group)["quadrature_type"], npts) topology = read_topology(reader, attrs(group)["topology"]) if type == "SpectralElementGrid1D" - return Spaces.SpectralElementGrid1D(topology, quadrature_style) + return Grids.SpectralElementGrid1D(topology, quadrature_style) else - return Spaces.SpectralElementGrid2D(topology, quadrature_style) + return Grids.SpectralElementGrid2D(topology, quadrature_style) end elseif type == "FiniteDifferenceGrid" topology = read_topology(reader, attrs(group)["topology"]) - return Spaces.FiniteDifferenceGrid(topology) + return Grids.FiniteDifferenceGrid(topology) elseif type == "ExtrudedFiniteDifferenceGrid" vertical_grid = read_grid(reader, attrs(group)["vertical_grid"]) horizontal_grid = read_grid(reader, attrs(group)["horizontal_grid"]) hypsography_type = get(attrs(group), "hypsography_type", "Flat") if hypsography_type == "Flat" - hypsography = Spaces.Flat() + hypsography = Grids.Flat() elseif hypsography_type == "LinearAdaption" hypsography = Hypsography.LinearAdaption( read_field(reader, attrs(group)["hypsography_surface"]), @@ -344,7 +344,7 @@ function read_grid_new(reader, name) else error("Unsupported hypsography type $hypsography_type") end - return Spaces.ExtrudedFiniteDifferenceGrid( + return Grids.ExtrudedFiniteDifferenceGrid( horizontal_grid, vertical_grid, hypsography, @@ -420,9 +420,9 @@ function read_field(reader::HDF5Reader, name::AbstractString) grid = read_grid(reader, attrs(obj)["grid"]) staggering = get(attrs(obj), "staggering", nothing) if staggering == "CellCenter" - staggering = Spaces.CellCenter() + staggering = Grids.CellCenter() elseif staggering == "CellFace" - staggering = Spaces.CellFace() + staggering = Grids.CellFace() end space = Spaces.space(staggering, grid) else From 918dbe63a7574b35684fbe17b5c13d9e65b47dca Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 16:06:23 -0700 Subject: [PATCH 32/42] fix arg order --- src/InputOutput/readers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/InputOutput/readers.jl b/src/InputOutput/readers.jl index 3c25aa535e..3f1bbce080 100644 --- a/src/InputOutput/readers.jl +++ b/src/InputOutput/readers.jl @@ -424,7 +424,7 @@ function read_field(reader::HDF5Reader, name::AbstractString) elseif staggering == "CellFace" staggering = Grids.CellFace() end - space = Spaces.space(staggering, grid) + space = Spaces.space(grid, staggering) else space = read_space(reader, attrs(obj)["space"]) end From 2ba3e7017b8be1679ee8a1a6fb0a505f35991cf9 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 24 Oct 2023 21:40:05 -0700 Subject: [PATCH 33/42] update benchmarks --- .dev/up_deps.jl | 1 + benchmarks/bickleyjet/Manifest.toml | 13 ++++++++++++- docs/Manifest.toml | 4 ++-- examples/Manifest.toml | 4 ++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/.dev/up_deps.jl b/.dev/up_deps.jl index 338db084d7..7cb2edb256 100644 --- a/.dev/up_deps.jl +++ b/.dev/up_deps.jl @@ -12,6 +12,7 @@ dirs = ( joinpath(root, "perf"), joinpath(root, "docs"), joinpath(root, "test"), + joinpath(root, "benchmarks", "bickleyjet"), joinpath(root, "lib", "ClimaCoreMakie"), joinpath(root, "lib", "ClimaCorePlots"), joinpath(root, "lib", "ClimaCoreTempestRemap"), diff --git a/benchmarks/bickleyjet/Manifest.toml b/benchmarks/bickleyjet/Manifest.toml index fc0a8268ad..610ec216db 100644 --- a/benchmarks/bickleyjet/Manifest.toml +++ b/benchmarks/bickleyjet/Manifest.toml @@ -157,7 +157,7 @@ uuid = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d" version = "0.5.5" [[deps.ClimaCore]] -deps = ["Adapt", "BandedMatrices", "BlockArrays", "CUDA", "ClimaComms", "CubedSphere", "DataStructures", "DocStringExtensions", "ForwardDiff", "GaussQuadrature", "GilbertCurves", "HDF5", "InteractiveUtils", "IntervalSets", "LinearAlgebra", "PkgVersion", "RecursiveArrayTools", "Requires", "RootSolvers", "SparseArrays", "Static", "StaticArrays", "Statistics", "UnPack"] +deps = ["Adapt", "BandedMatrices", "BlockArrays", "CUDA", "ClimaComms", "CubedSphere", "DataStructures", "DocStringExtensions", "ForwardDiff", "GaussQuadrature", "GilbertCurves", "HDF5", "InteractiveUtils", "IntervalSets", "LinearAlgebra", "Memoize", "PkgVersion", "RecursiveArrayTools", "Requires", "RootSolvers", "SparseArrays", "Static", "StaticArrays", "Statistics", "UnPack", "WeakValueDicts"] path = "../.." uuid = "d414da3d-4745-48bb-8d80-42e94e092884" version = "0.10.55" @@ -784,6 +784,12 @@ git-tree-sha1 = "c13304c81eec1ed3af7fc20e75fb6b26092a1102" uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e" version = "0.3.2" +[[deps.Memoize]] +deps = ["MacroTools"] +git-tree-sha1 = "2b1dfcba103de714d31c033b5dacc2e4a12c7caa" +uuid = "c03570c3-d221-55d1-a50c-7939bbd78826" +version = "0.4.4" + [[deps.MicrosoftMPI_jll]] deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] git-tree-sha1 = "a7023883872e52bc29bcaac74f19adf39347d2d5" @@ -1272,6 +1278,11 @@ git-tree-sha1 = "4528479aa01ee1b3b4cd0e6faef0e04cf16466da" uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91" version = "1.25.0+0" +[[deps.WeakValueDicts]] +git-tree-sha1 = "98528c2610a5479f091d470967a25becfd83edd0" +uuid = "897b6980-f191-5a31-bcb0-bf3c4585e0c1" +version = "0.1.0" + [[deps.XML2_jll]] deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Zlib_jll"] git-tree-sha1 = "24b81b59bd35b3c42ab84fa589086e19be919916" diff --git a/docs/Manifest.toml b/docs/Manifest.toml index 1e12f20336..db504d3f47 100644 --- a/docs/Manifest.toml +++ b/docs/Manifest.toml @@ -1310,9 +1310,9 @@ version = "0.1.12" [[deps.LinearSolve]] deps = ["ArrayInterface", "ConcreteStructs", "DocStringExtensions", "EnumX", "EnzymeCore", "FastLapackInterface", "GPUArraysCore", "InteractiveUtils", "KLU", "Krylov", "Libdl", "LinearAlgebra", "MKL_jll", "PrecompileTools", "Preferences", "RecursiveFactorization", "Reexport", "Requires", "SciMLBase", "SciMLOperators", "Setfield", "SparseArrays", "Sparspak", "SuiteSparse", "UnPack"] -git-tree-sha1 = "158e45dd35cec1ecade0e554c0104ee89e772d82" +git-tree-sha1 = "3196f7408f7c6014dc4ed55260e6c5e1e01c58a3" uuid = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae" -version = "2.11.1" +version = "2.12.0" [deps.LinearSolve.extensions] LinearSolveBandedMatricesExt = "BandedMatrices" diff --git a/examples/Manifest.toml b/examples/Manifest.toml index 1001e8a796..066c7fd3c0 100644 --- a/examples/Manifest.toml +++ b/examples/Manifest.toml @@ -1099,9 +1099,9 @@ version = "2.5.2" [[deps.LinearSolve]] deps = ["ArrayInterface", "ConcreteStructs", "DocStringExtensions", "EnumX", "EnzymeCore", "FastLapackInterface", "GPUArraysCore", "InteractiveUtils", "KLU", "Krylov", "Libdl", "LinearAlgebra", "MKL_jll", "PrecompileTools", "Preferences", "RecursiveFactorization", "Reexport", "Requires", "SciMLBase", "SciMLOperators", "Setfield", "SparseArrays", "Sparspak", "SuiteSparse", "UnPack"] -git-tree-sha1 = "158e45dd35cec1ecade0e554c0104ee89e772d82" +git-tree-sha1 = "3196f7408f7c6014dc4ed55260e6c5e1e01c58a3" uuid = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae" -version = "2.11.1" +version = "2.12.0" [deps.LinearSolve.extensions] LinearSolveBandedMatricesExt = "BandedMatrices" From 212dd831412ee5db394108a6b82dc98fb0ca376d Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 07:32:26 -0700 Subject: [PATCH 34/42] pass through keyword args --- src/Spaces/spectralelement.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 0202ad806d..9c2e420c70 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -69,9 +69,10 @@ grid(space::Spaces.SpectralElementSpace2D) = space.grid function SpectralElementSpace2D( topology::Topologies.Topology2D, - quadrature_style::Quadratures.QuadratureStyle, + quadrature_style::Quadratures.QuadratureStyle; + kwargs..., ) - grid = Grids.SpectralElementGrid2D(topology, quadrature_style) + grid = Grids.SpectralElementGrid2D(topology, quadrature_style; kwargs...) SpectralElementSpace2D(grid) end From f807b99df32ec8036d5cb32c44687e2fe8761f01 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 10:37:01 -0700 Subject: [PATCH 35/42] add deprecations for getproperty on space --- src/Spaces/extruded.jl | 53 +++++++++++++++++++ src/Spaces/finitedifference.jl | 29 ++++++++++- src/Spaces/spectralelement.jl | 93 ++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 1 deletion(-) diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index fc77020af7..61bedb19fc 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -38,6 +38,59 @@ function ExtrudedFiniteDifferenceSpace( return ExtrudedFiniteDifferenceSpace(grid, vertical_space.staggering) end +function Base.getproperty(space::ExtrudedFiniteDifferenceSpace, name::Symbol) + if name == :horizontal_space + Base.depwarn( + "`space.horizontal_space` is deprecated, use `Spaces.horizontal_space(space)` instead", + :getproperty, + ) + return horizontal_space(space) + elseif name == :vertical_topology + Base.depwarn( + "`space.vertical_topology` is deprecated, use `Spaces.vertical_topology(space)` instead", + :getproperty, + ) + return vertical_topology(space) + elseif name == :hypsography + Base.depwarn( + "`space.hypsography` is deprecated, use `Spaces.grid(space).hypsography` instead", + :getproperty, + ) + return grid(space).hypsography + elseif name == :global_geometry + Base.depwarn( + "`space.global_geometry` is deprecated, use `Spaces.global_geometry(space)` instead", + :getproperty, + ) + return global_geometry(space) + elseif name == :center_local_geometry + Base.depwarn( + "`space.center_local_geometry` is deprecated, use `Spaces.local_geometry_data(grid(space), Grids.CellCenter())` instead", + :getproperty, + ) + return local_geometry_data(space, Grids.CellCenter()) + elseif name == :face_local_geometry + Base.depwarn( + "`space.face_local_geometry` is deprecated, use `Spaces.local_geometry_data(grid(space), Grids.CellFace())` instead", + :getproperty, + ) + return local_geometry_data(space, Grids.CellFace()) + elseif name == :center_ghost_geometry + Base.depwarn( + "`space.center_ghost_geometry` is deprecated, use `nothing` instead", + :getproperty, + ) + return nothing + elseif name == :face_ghost_geometry + Base.depwarn( + "`space.face_ghost_geometry` is deprecated, use `nothing` instead", + :getproperty, + ) + return nothing + end + return getfield(space, name) +end + FaceExtrudedFiniteDifferenceSpace(grid::Grids.ExtrudedFiniteDifferenceGrid) = ExtrudedFiniteDifferenceSpace(grid, CellFace()) CenterExtrudedFiniteDifferenceSpace(grid::Grids.ExtrudedFiniteDifferenceGrid) = diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 9b18b1ae3d..077dfbb4ec 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -66,7 +66,34 @@ FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(mesh), CellCenter()) - +function Base.getproperty(space::FiniteDifferenceSpace, name::Symbol) + if name == :topology + Base.depwarn( + "`space.topology` is deprecated, use `Spaces.topology(space)` instead", + :getproperty, + ) + return topology(space) + elseif name == :global_geometry + Base.depwarn( + "`space.global_geometry` is deprecated, use `Spaces.global_geometry(space)` instead", + :getproperty, + ) + return global_geometry(space) + elseif name == :center_local_geometry + Base.depwarn( + "`space.center_local_geometry` is deprecated, use `local_geometry_data(grid(space), Grids.CellCenter())` instead", + :getproperty, + ) + return local_geometry_data(space, Grids.CellCenter()) + elseif name == :face_local_geometry + Base.depwarn( + "`space.face_local_geometry` is deprecated, use `local_geometry_data(grid(space), Grids.CellFace())` instead", + :getproperty, + ) + return local_geometry_data(space, Grids.CellFace()) + end + return getfield(space, name) +end Adapt.adapt_structure(to, space::FiniteDifferenceSpace) = FiniteDifferenceSpace(Adapt.adapt(to, space.grid), space.staggering) diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 9c2e420c70..91b4014ad4 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -55,6 +55,40 @@ function SpectralElementSpace1D( end +function Base.getproperty(space::SpectralElementSpace1D, name::Symbol) + if name == :topology + Base.depwarn( + "`space.topology` is deprecated, use `Spaces.topology(space)` instead", + :getproperty, + ) + return topology(space) + elseif name == :quadrature_style + Base.depwarn( + "`space.quadrature_style` is deprecated, use `Spaces.quadrature_style(space)` instead", + :getproperty, + ) + return quadrature_style(space) + elseif name == :global_geometry + Base.depwarn( + "`space.global_geometry` is deprecated, use `Spaces.global_geometry(space)` instead", + :getproperty, + ) + return global_geometry(space) + elseif name == :local_geometry + Base.depwarn( + "`space.local_geometry` is deprecated, use `Spaces.local_geometry_data(space)` instead", + :getproperty, + ) + return local_geometry_data(space) + elseif name == :local_dss_weights + Base.depwarn( + "`space.local_dss_weights` is deprecated, use `Spaces.local_dss_weights(space)` instead", + :getproperty, + ) + return local_dss_weights(space) + end + return getfield(space, name) +end # 2D """ @@ -76,6 +110,65 @@ function SpectralElementSpace2D( SpectralElementSpace2D(grid) end +function Base.getproperty(space::SpectralElementSpace2D, name::Symbol) + if name == :topology + Base.depwarn( + "`space.topology` is deprecated, use `Spaces.topology(space)` instead", + :getproperty, + ) + return topology(space) + elseif name == :quadrature_style + Base.depwarn( + "`space.quadrature_style` is deprecated, use `Spaces.quadrature_style(space)` instead", + :getproperty, + ) + return quadrature_style(space) + elseif name == :global_geometry + Base.depwarn( + "`space.global_geometry` is deprecated, use `Spaces.global_geometry(space)` instead", + :getproperty, + ) + return global_geometry(space) + elseif name == :local_geometry + Base.depwarn( + "`space.local_geometry` is deprecated, use `Spaces.local_geometry_data(space)` instead", + :getproperty, + ) + return local_geometry_data(space) + elseif name == :ghost_geometry + Base.depwarn( + "`space.ghost_geometry` is deprecated, use `nothing` instead", + :getproperty, + ) + return nothing + elseif name == :local_dss_weights + Base.depwarn( + "`space.local_dss_weights` is deprecated, use `Spaces.local_dss_weights(space)` instead", + :getproperty, + ) + return local_dss_weights(space) + elseif name == :ghost_dss_weights + Base.depwarn( + "`space.ghost_dss_weights` is deprecated, use `nothing` instead", + :getproperty, + ) + return nothing + elseif name == :internal_surface_geometry + Base.depwarn( + "`space.internal_surface_geometry` is deprecated, use `space.grid.internal_surface_geometry` instead", + :getproperty, + ) + return space.grid.internal_surface_geometry + elseif name == :boundary_surface_geometries + Base.depwarn( + "`space.boundary_surface_geometries` is deprecated, use `space.grid.boundary_surface_geometries` instead", + :getproperty, + ) + return space.grid.boundary_surface_geometries + end + return getfield(space, name) +end + """ SpectralElementSpaceSlab <: AbstractSpace From 5c7502598cbb722fa8e4bf1ac62dceb92fff0b40 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 10:42:56 -0700 Subject: [PATCH 36/42] qualify _get_idx --- src/Fields/mapreduce_cuda.jl | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Fields/mapreduce_cuda.jl b/src/Fields/mapreduce_cuda.jl index 03ab059be0..61f0f7aaeb 100644 --- a/src/Fields/mapreduce_cuda.jl +++ b/src/Fields/mapreduce_cuda.jl @@ -220,15 +220,6 @@ end return (Nv, Nij, Nf, Nh) end -@inline function _get_idxs(Nv, Nij, Nf, Nh, fidx, gidx) - hidx = cld(gidx, Nv * Nij * Nij * Nf) - offset = ((hidx - 1) * Nf + (fidx - 1)) * Nv * Nij * Nij - jidx = cld(gidx - offset, Nv * Nij) - offset += (jidx - 1) * Nv * Nij - iidx = cld(gidx - offset, Nv) - return (iidx, jidx, hidx) -end - @inline function _cuda_reduce!(op, reduction, tidx, reduction_size, N) if reduction_size > N if tidx ≤ reduction_size - N From a37ad48eed11b5ffb62dfb07b422083fdd99701c Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 10:43:43 -0700 Subject: [PATCH 37/42] using CUDA --- src/Topologies/Topologies.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Topologies/Topologies.jl b/src/Topologies/Topologies.jl index 8ce97ffe22..f0c7a92a3c 100644 --- a/src/Topologies/Topologies.jl +++ b/src/Topologies/Topologies.jl @@ -2,7 +2,8 @@ module Topologies using DocStringExtensions -import ClimaComms, Adapt, CUDA +import ClimaComms, Adapt +using CUDA import ..ClimaCore import ..Geometry From 375c854ec806a51f19e86331a4e6d03f5bb17213 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 11:37:58 -0700 Subject: [PATCH 38/42] inline getproperty --- src/Spaces/extruded.jl | 5 ++++- src/Spaces/spectralelement.jl | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Spaces/extruded.jl b/src/Spaces/extruded.jl index 61bedb19fc..37b691b8da 100644 --- a/src/Spaces/extruded.jl +++ b/src/Spaces/extruded.jl @@ -38,7 +38,10 @@ function ExtrudedFiniteDifferenceSpace( return ExtrudedFiniteDifferenceSpace(grid, vertical_space.staggering) end -function Base.getproperty(space::ExtrudedFiniteDifferenceSpace, name::Symbol) +@inline function Base.getproperty( + space::ExtrudedFiniteDifferenceSpace, + name::Symbol, +) if name == :horizontal_space Base.depwarn( "`space.horizontal_space` is deprecated, use `Spaces.horizontal_space(space)` instead", diff --git a/src/Spaces/spectralelement.jl b/src/Spaces/spectralelement.jl index 91b4014ad4..e7b4255d39 100644 --- a/src/Spaces/spectralelement.jl +++ b/src/Spaces/spectralelement.jl @@ -55,7 +55,7 @@ function SpectralElementSpace1D( end -function Base.getproperty(space::SpectralElementSpace1D, name::Symbol) +@inline function Base.getproperty(space::SpectralElementSpace1D, name::Symbol) if name == :topology Base.depwarn( "`space.topology` is deprecated, use `Spaces.topology(space)` instead", @@ -110,7 +110,7 @@ function SpectralElementSpace2D( SpectralElementSpace2D(grid) end -function Base.getproperty(space::SpectralElementSpace2D, name::Symbol) +@inline function Base.getproperty(space::SpectralElementSpace2D, name::Symbol) if name == :topology Base.depwarn( "`space.topology` is deprecated, use `Spaces.topology(space)` instead", From dede3a20086a208fbd849f166f1bab71c5a3aa30 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 13:47:46 -0700 Subject: [PATCH 39/42] more fixes --- src/MatrixFields/single_field_solver.jl | 2 +- src/Operators/integrals.jl | 6 +++--- src/Operators/thomas_algorithm.jl | 2 +- src/Spaces/dss.jl | 2 +- src/Spaces/finitedifference.jl | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/MatrixFields/single_field_solver.jl b/src/MatrixFields/single_field_solver.jl index bd02d67c2d..94bb0760a7 100644 --- a/src/MatrixFields/single_field_solver.jl +++ b/src/MatrixFields/single_field_solver.jl @@ -63,7 +63,7 @@ function single_field_solve_kernel!(cache, x, A, b) idx = CUDA.threadIdx().x + (CUDA.blockIdx().x - 1) * CUDA.blockDim().x Ni, Nj, _, _, Nh = size(Fields.field_values(A)) if idx <= Ni * Nj * Nh - i, j, h = Spaces._get_idx((Ni, Nj, Nh), idx) + i, j, h = Topologies._get_idx((Ni, Nj, Nh), idx) _single_field_solve!( Spaces.column(cache, i, j, h), Spaces.column(x, i, j, h), diff --git a/src/Operators/integrals.jl b/src/Operators/integrals.jl index e7f2687964..7e49400cee 100644 --- a/src/Operators/integrals.jl +++ b/src/Operators/integrals.jl @@ -36,7 +36,7 @@ function column_integral_definite_kernel!( idx = threadIdx().x + (blockIdx().x - 1) * blockDim().x Ni, Nj, _, _, Nh = size(Fields.field_values(ᶜfield)) if idx <= Ni * Nj * Nh - i, j, h = Spaces._get_idx((Ni, Nj, Nh), idx) + i, j, h = Topologies._get_idx((Ni, Nj, Nh), idx) ∫field_column = Spaces.column(∫field, i, j, h) ᶜfield_column = Spaces.column(ᶜfield, i, j, h) _column_integral_definite!(∫field_column, ᶜfield_column) @@ -127,7 +127,7 @@ function column_integral_indefinite_kernel!( idx = threadIdx().x + (blockIdx().x - 1) * blockDim().x Ni, Nj, _, _, Nh = size(Fields.field_values(ᶜfield)) if idx <= Ni * Nj * Nh - i, j, h = Spaces._get_idx((Ni, Nj, Nh), idx) + i, j, h = Topologies._get_idx((Ni, Nj, Nh), idx) ᶠ∫field_column = Spaces.column(ᶠ∫field, i, j, h) ᶜfield_column = Spaces.column(ᶜfield, i, j, h) _column_integral_indefinite!(ᶠ∫field_column, ᶜfield_column) @@ -314,7 +314,7 @@ function column_mapreduce_kernel_extruded!( idx = threadIdx().x + (blockIdx().x - 1) * blockDim().x Ni, Nj, _, _, Nh = size(Fields.field_values(reduced_field)) if idx <= Ni * Nj * Nh - i, j, h = Spaces._get_idx((Ni, Nj, Nh), idx) + i, j, h = Topologies._get_idx((Ni, Nj, Nh), idx) reduced_field_column = Spaces.column(reduced_field, i, j, h) field_columns = map(field -> Spaces.column(field, i, j, h), fields) _column_mapreduce!(fn, op, reduced_field_column, field_columns...) diff --git a/src/Operators/thomas_algorithm.jl b/src/Operators/thomas_algorithm.jl index 7154a78a6c..c347615020 100644 --- a/src/Operators/thomas_algorithm.jl +++ b/src/Operators/thomas_algorithm.jl @@ -27,7 +27,7 @@ function thomas_algorithm_kernel!( idx = threadIdx().x + (blockIdx().x - 1) * blockDim().x Ni, Nj, _, _, Nh = size(Fields.field_values(A)) if idx <= Ni * Nj * Nh - i, j, h = Spaces._get_idx((Ni, Nj, Nh), idx) + i, j, h = Topologies._get_idx((Ni, Nj, Nh), idx) thomas_algorithm!(Spaces.column(A, i, j, h), Spaces.column(b, i, j, h)) end return nothing diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl index b65e02a384..77c9259581 100644 --- a/src/Spaces/dss.jl +++ b/src/Spaces/dss.jl @@ -293,7 +293,7 @@ weighted_dss_ghost!( function weighted_dss_ghost!( data::Union{DataLayouts.IJFH, DataLayouts.VIJFH}, space::Union{AbstractSpectralElementSpace, ExtrudedFiniteDifferenceSpace}, - hspace::SpectralElementSpace2D{<:Topologies.Topology2D}, + hspace::SpectralElementSpace2D, dss_buffer::DSSBuffer, ) assert_same_eltype(data, dss_buffer) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 077dfbb4ec..10c1a04848 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -66,7 +66,7 @@ FaceFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = CenterFiniteDifferenceSpace(mesh::Meshes.IntervalMesh) = FiniteDifferenceSpace(Grids.FiniteDifferenceGrid(mesh), CellCenter()) -function Base.getproperty(space::FiniteDifferenceSpace, name::Symbol) +@inline function Base.getproperty(space::FiniteDifferenceSpace, name::Symbol) if name == :topology Base.depwarn( "`space.topology` is deprecated, use `Spaces.topology(space)` instead", From 0a2b7932041a5358ec31d3d2431e7a53a197b3aa Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 14:15:45 -0700 Subject: [PATCH 40/42] import dss_ghost --- src/Spaces/dss.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Spaces/dss.jl b/src/Spaces/dss.jl index 77c9259581..0e0abdab94 100644 --- a/src/Spaces/dss.jl +++ b/src/Spaces/dss.jl @@ -7,6 +7,7 @@ import ..Topologies: dss_untransform!, dss_local!, dss_local_ghost!, + dss_ghost!, fill_send_buffer!, load_from_recv_buffer! From 4fe4a8610b1e444fea1b5ffae5f4f6a001f324aa Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 14:25:18 -0700 Subject: [PATCH 41/42] fix level of FD space --- src/Spaces/finitedifference.jl | 4 ++-- src/Spaces/pointspace.jl | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Spaces/finitedifference.jl b/src/Spaces/finitedifference.jl index 10c1a04848..eda4de5feb 100644 --- a/src/Spaces/finitedifference.jl +++ b/src/Spaces/finitedifference.jl @@ -153,12 +153,12 @@ Base.@propagate_inbounds function level( v::PlusHalf, ) @inbounds local_geometry = level(local_geometry_data(space), v.i + 1) - PointSpace(local_geometry) + PointSpace(ClimaComms.device(space), local_geometry) end Base.@propagate_inbounds function level( space::CenterFiniteDifferenceSpace, v::Int, ) local_geometry = level(local_geometry_data(space), v) - PointSpace(local_geometry) + PointSpace(ClimaComms.device(space), local_geometry) end diff --git a/src/Spaces/pointspace.jl b/src/Spaces/pointspace.jl index 4aee1a8ff4..10e0c36dfc 100644 --- a/src/Spaces/pointspace.jl +++ b/src/Spaces/pointspace.jl @@ -17,12 +17,11 @@ ClimaComms.device(space::PointSpace) = ClimaComms.context(space::PointSpace) = ClimaComms.SingletonCommsContext(ClimaComms.CPUSingleThreaded()) -#= + PointSpace(x::Geometry.LocalGeometry) = PointSpace(ClimaComms.CPUSingleThreaded(), x) PointSpace(x::Geometry.AbstractPoint) = PointSpace(ClimaComms.CPUSingleThreaded(), x) -=# function PointSpace(device::ClimaComms.AbstractDevice, x) context = ClimaComms.SingletonCommsContext(device) From c48a3e9dc31bdf610327590494f4bdb6c135323b Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 25 Oct 2023 14:33:18 -0700 Subject: [PATCH 42/42] support for writing level grids --- src/InputOutput/writers.jl | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/InputOutput/writers.jl b/src/InputOutput/writers.jl index 7b19b1e30e..8aca160d32 100644 --- a/src/InputOutput/writers.jl +++ b/src/InputOutput/writers.jl @@ -302,7 +302,8 @@ defaultname(::Grids.SpectralElementGrid2D) = "horizontal_grid" defaultname(::Grids.ExtrudedFiniteDifferenceGrid) = "extruded_finite_difference_grid" defaultname(grid::Grids.FiniteDifferenceGrid) = defaultname(grid.topology) - +defaultname(grid::Grids.LevelGrid) = + "$(defaultname(grid.full_grid)): level $(grid.level)" """ write_new!(writer, space, name) @@ -386,6 +387,23 @@ function write_new!( return name end + +function write_new!( + writer::HDF5Writer, + space::Grids.LevelGrid, + name::AbstractString = defaultname(space), +) + group = create_group(writer.file, "grids/$name") + write_attribute(group, "type", "LevelGrid") + write_attribute(group, "full_grid", write!(writer, space.full_grid)) + if space.level isa PlusHalf + write_attribute(group, "level + half", space.level.value) + else + write_attribute(group, "level", space.level) + end + return name +end + # write fields function write!(writer::HDF5Writer, field::Fields.Field, name::AbstractString) space = axes(field)