Skip to content

Commit

Permalink
feat: Add volume of grid cells
Browse files Browse the repository at this point in the history
Add a function dV that calculates the volume of the individual grid
cells.
  • Loading branch information
musoke committed Apr 29, 2022
1 parent 1e8ad49 commit 3c848e1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UltraDark"
uuid = "1c8d022d-dfc0-4b41-80ab-3fc7e88cdfea"
authors = ["Nathan Musoke <[email protected]>"]
version = "0.4.0"
version = "0.4.1"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
1 change: 1 addition & 0 deletions src/UltraDark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions src/grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down

2 comments on commit 3c848e1

@musoke
Copy link
Owner Author

@musoke musoke commented on 3c848e1 Apr 29, 2022

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/59377

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.1 -m "<description of version>" 3c848e1ae4ba0665ed9f214845dffe8f93713a9d
git push origin v0.4.1

Please sign in to comment.