From 3c848e1ae4ba0665ed9f214845dffe8f93713a9d Mon Sep 17 00:00:00 2001 From: Nathan Musoke Date: Fri, 29 Apr 2022 12:10:44 -0400 Subject: [PATCH] feat: Add volume of grid cells Add a function dV that calculates the volume of the individual grid cells. --- Project.toml | 2 +- src/UltraDark.jl | 1 + src/grids.jl | 26 ++++++++++++++++++++++++++ test/grids.jl | 6 ++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 8d528ae..4460d34 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "UltraDark" uuid = "1c8d022d-dfc0-4b41-80ab-3fc7e88cdfea" authors = ["Nathan Musoke "] -version = "0.4.0" +version = "0.4.1" [deps] AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c" diff --git a/src/UltraDark.jl b/src/UltraDark.jl index 87d531f..59438f9 100644 --- a/src/UltraDark.jl +++ b/src/UltraDark.jl @@ -9,6 +9,7 @@ using FFTW export simulate export Grids, PencilGrids +export dV export Config, SimulationConfig, constant_scale_factor, TimeStepOptions export OutputConfig export SummaryStatistics, SummaryStatisticsMeanMaxRms diff --git a/src/grids.jl b/src/grids.jl index 5b1c75d..4455371 100644 --- a/src/grids.jl +++ b/src/grids.jl @@ -271,3 +271,29 @@ function rk_norm(lengths, resols) (kx.^2 .+ ky.^2 .+ kz.^2).^0.5 end + +""" + dV(grids) + +Calculate the volume of each grid cell + +# Examples + +```jldoctest +julia> using UltraDark + +julia> box_length = 1.0; + +julia> resol = 16; + +julia> g = Grids(box_length, resol); + +julia> dV(g) * resol^3 == box_length^3 +true + +``` + +""" +function dV(grids) + diff(grids.x, dims=1)[1] * diff(grids.y, dims=2)[1] * diff(grids.z, dims=3)[1] +end diff --git a/test/grids.jl b/test/grids.jl index 6f1b59a..095eaba 100644 --- a/test/grids.jl +++ b/test/grids.jl @@ -14,12 +14,16 @@ for grids_type in [Grids, PencilGrids] @test reshape(grids.x, resol) == reshape(grids.y, resol) @test reshape(grids.x, resol) == reshape(grids.z, resol) + @test dV(grids) * resol^3 == 1.0^3 + grids = grids_type((1.0, 1.0, 1.0), (resol, resol, resol)) @test typeof(grids) <: grids_type @test all(grids.ψx .== 0) @test reshape(grids.x, resol) == reshape(grids.y, resol) @test reshape(grids.x, resol) == reshape(grids.z, resol) + @test dV(grids) * resol^3 == 1.0^3 + grids = grids_type((4.0, 2.0, 1.0), (4resol, 2resol, 1resol)) @test typeof(grids) <: grids_type @test all(grids.ψx .== 0) @@ -28,6 +32,8 @@ for grids_type in [Grids, PencilGrids] @test size(grids.z) == (1, 1, 1resol) @test reshape(grids.x, 4resol)[1 + 4resol÷2 - 4resol÷8:4resol÷2 + 4resol÷8] == reshape(grids.z, resol) @test reshape(grids.y, 2resol)[1 + 2resol÷2 - 2resol÷4:2resol÷2 + 2resol÷4] == reshape(grids.z, resol) + + @test dV(grids) * 4resol * 2resol * 1resol == 4.0 * 2.0 * 1.0 end end