Skip to content

Commit

Permalink
Update versions and dependencies (#98)
Browse files Browse the repository at this point in the history
* Add missing compat entries

* Remove unneccesary packages

* Move plotting into an extension

* Rename extension

* Add export

* Fix extension name

* Remove unneeded dep

* Updating import order

* Function stubs

* Adding different exports

* Add JLD2 dependency

* Cleanup plotting and extension code

* Add CairoMakie dependency
  • Loading branch information
skygering authored Jul 31, 2024
1 parent aca4f19 commit 177a3bd
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 213 deletions.
19 changes: 10 additions & 9 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ authors = ["Skylar Gering"]
version = "0.1.0"

[deps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Extents = "411431e0-e8b7-467b-b5e0-f676ba4f2910"
GeometryOps = "3251bfac-6a57-4b6d-aa61-ac1fef2975ab"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
Expand All @@ -27,23 +23,28 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
VoronoiCells = "e3e34ffb-84e9-5012-9490-92c94d0c60a4"

[weakdeps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"

[extensions]
SubzeroMakieExt = "CairoMakie"

[compat]
CairoMakie = "0.12"
DataStructures = "0.18"
CoordinateTransformations = "0.6"
Extents = "0.1"
GeometryOps = "0.1.10"
Interpolations = "0.14, 0.15"
JLD2 = "0.4"
Makie = "0.19, 0.20, 0.21"
Measures = "0.3"
NCDatasets = "0.12, 0.13, 0.14"
NetCDF = "0.11, 0.12"
NCDatasets = "0.14"
Rotations = "1"
SplitApplyCombine = "1"
StaticArrays = "1"
Statistics = "1"
StructArrays = "0.6"
VoronoiCells = "0.4"
julia = "1.6"
julia = "1.9"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
2 changes: 1 addition & 1 deletion examples/converge_diverge_flow.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JLD2, Random, Statistics, Subzero
using JLD2, Random, Statistics, Subzero, CairoMakie

# User Inputs
const FT = Float64
Expand Down
2 changes: 1 addition & 1 deletion examples/forcing_contained_floes.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JLD2, Random, Statistics, Subzero
using JLD2, Random, Statistics, Subzero, CairoMakie

# User Inputs
const FT = Float64
Expand Down
2 changes: 1 addition & 1 deletion examples/moving_bounds.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JLD2, Random, Statistics, Subzero
using JLD2, Random, Statistics, Subzero, CairoMakie

# User Inputs
const FT = Float64
Expand Down
2 changes: 1 addition & 1 deletion examples/restart_sim.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JLD2, Subzero, Random, Statistics
using JLD2, Subzero, Random, Statistics, CairoMakie

const FT = Float64
const Δt = 10
Expand Down
2 changes: 1 addition & 1 deletion examples/shear_flow.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JLD2, Random, Statistics, Subzero
using JLD2, Random, Statistics, Subzero, CairoMakie

# User Inputs
const FT = Float64
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_strait.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JLD2, Random, Statistics, Subzero
using JLD2, Random, Statistics, Subzero, CairoMakie

# User Inputs
const FT = Float64
Expand Down
2 changes: 1 addition & 1 deletion examples/uniform_flow.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JLD2, Random, Statistics, Subzero
using JLD2, Random, Statistics, Subzero, CairoMakie

# User Inputs
const FT = Float64
Expand Down
170 changes: 170 additions & 0 deletions ext/SubzeroMakieExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
module SubzeroMakieExt

using CairoMakie
using Subzero, JLD2
import Subzero: prettytime, plot_sim, plot_sim_with_ocean_field

"""
CoordPlot
Recipe for plotting list of `PolyVec`s that creates two new functions: `coordplot` and
`coordplot!`.
These functions each take in a vector of `PolyVec`s to plot the floe field at a given
timestep.
Note: I would like to remove this recipe and it's corresponding `plot!` function eventually.
Now that all of the floes' carry around a `GeoInterface` polygon, we can simply use
`GeoInterfaceMakie` to plot them and not deal with plotting from coordiantes.
"""
CairoMakie.@recipe(CoordPlot, coord_list) do scene
Attributes(
color = :lightblue, # floe fill color (could give transparent color)
strokecolor = :black, # outline color of floes
strokewidth = 1, # width of floe outline
)
end

"""
Makie.plot!(coordplot)
Defines coordplot and coordplot! for plotting vectors of PolyVecs, representing the floe
field at a given timestep.
Note: I would like to remove this function and it's corresponding `CoordPlot` recipe
eventually. Now that all of the floes' carry around a `GeoInterface` polygon, we can simply
use `GeoInterfaceMakie` to plot them and not deal with plotting from coordiantes.
"""
function CairoMakie.plot!(coordplot::CoordPlot{<:Tuple{<:Vector{<:PolyVec}}})
coord_list = coordplot[1]
poly_list = @lift([[Point2f(verts) for verts in c[1]] for c in $coord_list])
Makie.poly!(
coordplot,
poly_list,
color = coordplot[:color],
strokecolor = coordplot[:strokecolor],
strokewidth = coordplot[:strokewidth],
)
coordplot
end

"""
plot_sim(floe_fn, initial_state_fn, title, Δt, output_fn)
Basic plotting of a simulation using the simulation's floe and initial state files. This
function is meant for basic plotting and as an example of how to create video.
Does not have underlying ocean.
## Arguments:
- `floe_fn::String`: $(Subzero.FLOE_FN_DEF)
- `initial_state_fn::String`: $(Subzero.INITIAL_STATE_FN_DEF)
- `title::String`: plot title
- `Δt::Int`: $(Subzero.ΔT_DEF)
- `output_fn::String`: $(Subzero.MP4_OUTPUT_FN)
"""
function plot_sim(
floe_fn,
initial_state_fn,
Δt,
output_fn;
)
# Open files
file = jldopen(floe_fn)
domain = load(initial_state_fn)["sim"].model.domain
timesteps = keys(file["centroid"])
# Set up observables
floes = Observable(file["coords"][timesteps[1]])
# Plot floes
fig, ax, _ = coordplot(floes)
# Set axis limits and names
xlims!(domain.west.val, domain.east.val)
ylims!(domain.south.val, domain.north.val)
ax.xlabel = "Meters"
ax.ylabel = "Meters"
# Plot topography
if !isempty(domain.topography)
coordplot!(domain.topography.coords, color = :lightgrey)
end
# Create movie
record(fig, output_fn, timesteps; framerate = 20) do time
ax.title = Subzero.prettytime(parse(Float64, time) * Δt)
new_coords = file["coords"][time]
floes[] = new_coords
end
close(file)
end

"""
plot_sim_with_ocean_field(floe_fn, initial_state_fn, Δt, ocean_fn, ocean_func, output_fn)
Basic plotting of sim as an example of how to create video. Does not have
underlying ocean.
Basic plotting of a simulation using the simulation's floe and initial state files, as well
as an surface data file output by `Oceananigans` that can be run through an `ocean_func` to
get a gridded output. This function is meant for basic plotting and as an example of how to
create video.
## Arguments:
- `floe_fn::String`: $(Subzero.FLOE_FN_DEF)
- `initial_state_fn::String`: $(Subzero.INITIAL_STATE_FN_DEF)
- `Δt::Int`: $(Subzero.ΔT_DEF)
- `ocean_fn::String`: $(Subzero.OCEAN_FN_DEF)
- `ocean_func::Function`: function that takes in `ocean_fn` and returns an Nx by Ny by timesteps surface field for plotting as well as xc and yc fields (see `calc_ro_field` for example)
- `colorbar_title::String`: name for colorbar associated with ocean_func vals
- `output_fn::String`: $(Subzero.MP4_OUTPUT_FN)
"""
function plot_sim_with_ocean_field(
floe_fn,
initial_state_fn,
Δt,
ocean_fn,
ocean_func,
colorbar_title,
output_fn,
)
# Open files
file = jldopen(floe_fn)
domain = load(initial_state_fn)["sim"].model.domain
timesteps = keys(file["centroid"])
ocean_data, xc, yc = ocean_func(ocean_fn)
# Set up observables needed for plotting
floes = Observable(file["coords"][timesteps[1]])
ocean_vals = Observable(@view ocean_data[:, :, 1])
min_ocn_val, max_ocn_val = extrema(ocean_data)
fig = Figure()
# Plot ocean
ax, hm = heatmap(
fig[1, 1],
xc,
yc,
ocean_vals,
colormap = :RdBu_9,
colorrange = (min_ocn_val, max_ocn_val)
)
# Add axis limits and titles
xlims!(domain.west.val, domain.east.val)
ylims!(domain.south.val, domain.north.val)
ax.xlabel = "Meters"
ax.ylabel = "Meters"
# Add colorbar
Colorbar(fig[1, 2], hm, label = colorbar_title)
# Plot floes
coordplot!(fig[1, 1], floes)
# Plot topography
if !isempty(domain.topography)
coordplot!(fig[1, 1], domain.topography.coords, color = :lightgrey)
end

# Create movie
record(fig, output_fn, 1:length(timesteps), framerate = 20) do i
time = timesteps[i]
ax.title = prettytime(parse(Float64, time) * Δt)
new_coords = file["coords"][time]
ocean_vals[] = @view ocean_data[:, :, i]
floes[] = new_coords
end
close(file)
end

end # module
8 changes: 4 additions & 4 deletions src/Subzero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ export
IceStressCell,
CellFloes,
MonteCarloPointsGenerator,
SubGridPointsGenerator,
plot_sim
SubGridPointsGenerator

import Base.@kwdef # this is being exported as of version 1.9
import GeometryOps as GO
import GeometryOps.GeoInterface as GI
import GeometryOps.GeometryBasics as GB
import StaticArrays as SA
using CairoMakie, CoordinateTransformations, DataStructures, Dates, Extents,
Interpolations, JLD2, LinearAlgebra, Logging, Makie, Measures, NCDatasets, NetCDF,
using CoordinateTransformations, Dates, Extents,
Interpolations, JLD2, LinearAlgebra, Logging, Measures, NCDatasets,
Printf, Random, Rotations, SplitApplyCombine, Statistics, StructArrays,
VoronoiCells

Expand Down
2 changes: 1 addition & 1 deletion src/simulation_components/floe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ function generate_voronoi_coords(
tess_cells = voronoicells(
xpoints,
ypoints,
Rectangle(Point2(0.0, 0.0), Point2(1.0, 1.0)),
Rectangle(GB.Point2((0.0, 0.0)), GB.Point2((1.0, 1.0))),
rng = rng
).Cells
# Scale and translate voronoi coordinates
Expand Down
Loading

0 comments on commit 177a3bd

Please sign in to comment.