-
Notifications
You must be signed in to change notification settings - Fork 3
Initial coupling to SpeedyWeather via example script #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5550b72
Add initial working version of coupling script
bgroenks96 3ffb8a2
Updates to coupling script
bgroenks96 f6e0820
Add copuled soil-atmosphere model
bgroenks96 d5332e7
Revert back to using single soil layer in Speedy grid
bgroenks96 bbf0f44
Add source for CPU vs GPU benchmark
bgroenks96 84843ea
Remove unnecessary dependencies
bgroenks96 4c34544
Simplifiy FieldInputSource to just allocate input variables
bgroenks96 f0a26f2
Update speedy primitive dry coupling script
bgroenks96 29ca5d7
Remove accidental BenchmarkTools dep
bgroenks96 8ffc3ad
fix fieldtimeseries input
maximilian-gelbrecht eee7e70
actually set the test forcing to one
maximilian-gelbrecht 08a14e6
remove alternative initialize again
maximilian-gelbrecht bd0233a
actually test inputs
maximilian-gelbrecht b513f60
add Unitful to test env
maximilian-gelbrecht 9627e2f
Merge remote-tracking branch 'origin/mg/fix-input-time-series' into b…
bgroenks96 8057044
Minor changes to example notebook
bgroenks96 d2db0f5
Update input test and change initialize API
bgroenks96 ac52466
Fix constructor error
bgroenks96 529c1e1
Revert change to default ctor and fix tests
bgroenks96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| using Terrarium | ||
|
|
||
| using CUDA | ||
| using Dates | ||
| using Rasters, NCDatasets | ||
| using Statistics | ||
|
|
||
| using CairoMakie, GeoMakie | ||
|
|
||
| import RingGrids | ||
| import SpeedyWeather as Speedy | ||
|
|
||
| # Temporary workaround for SpeedyWeather.jl#919 | ||
| Speedy.SpeedyTransforms.FFTW.set_num_threads(1) | ||
|
|
||
| """ | ||
| Naive implementation of a SpeedyWeather "dry" land model that couples to a Terrarium model. | ||
| """ | ||
| struct TerrariumDryLand{NF, TMI<:Terrarium.ModelIntegrator{NF}} <: Speedy.AbstractDryLand | ||
| "Speedy spectral grid" | ||
| spectral_grid::Speedy.SpectralGrid | ||
|
|
||
| "Initialized Terrarium model integrator" | ||
| integrator::TMI | ||
|
|
||
| function TerrariumDryLand(itnegrator::Terrarium.ModelIntegrator{NF, Arch, Grid}; spectral_grid_kwargs...) where {NF, Arch, Grid<:ColumnRingGrid} | ||
| spectral_grid = Speedy.SpectralGrid(integrator.model.grid.rings; NF, nlayers_soil=1, spectral_grid_kwargs...) | ||
| return new{eltype(integrator), typeof(integrator)}(spectral_grid, integrator) | ||
| end | ||
| end | ||
|
|
||
| function Speedy.initialize!( | ||
| progn_land::Speedy.PrognosticVariablesLand, # for dispatch | ||
| progn::Speedy.PrognosticVariables{NF}, | ||
| diagn::Speedy.DiagnosticVariables{NF}, | ||
| land::TerrariumDryLand, | ||
| model::Speedy.PrimitiveEquation, | ||
| ) where {NF} | ||
| Tsoil = interior(land.integrator.state.temperature)[:, 1, end] | ||
| progn_land.soil_temperature .= Tsoil .+ NF(273.15) | ||
| Terrarium.initialize!(land.integrator) | ||
| return nothing | ||
| end | ||
|
|
||
| function Speedy.timestep!( | ||
| progn::Speedy.PrognosticVariables{NF}, | ||
| diagn::Speedy.DiagnosticVariables{NF}, | ||
| land::TerrariumDryLand, | ||
| model::Speedy.PrimitiveEquation, | ||
| ) where {NF} | ||
| # get speedy state variables | ||
| Tair = @view diagn.grid.temp_grid[:, end] | ||
| # terrarium state | ||
| state = land.integrator.state | ||
| set!(state.inputs.air_temperature, Tair) # first set directly to avoid allocating new fields | ||
| set!(state.inputs.air_temperature, state.inputs.air_temperature - 273.15) # then convert to celsius | ||
| # run land forward over speedy timestep interval; | ||
| # we use a smaller actual timestep to ensure stability | ||
| Terrarium.run!(land.integrator, period=progn.clock.Δt, Δt=300.0) | ||
| # Get soil temperatures | ||
| Tsoil = state.temperature | ||
| # Get surface temperature (last z-layer in Oceananigans grids) | ||
| Nx, Nz = Tsoil.grid.Nx, Tsoil.grid.Nz | ||
| Tsurf = @view Tsoil[1:Nx, 1, Nz:Nz] | ||
| # Update speedy soil/skin temperature | ||
| progn.land.soil_temperature .= Tsurf .+ NF(273.15) | ||
| return nothing | ||
| end | ||
|
|
||
| ring_grid = RingGrids.FullGaussianGrid(24) | ||
| Nz = 30 | ||
| Δz_min = 0.05 # currently the coupling is only stable with a large surface layer | ||
| grid = ColumnRingGrid(CPU(), Float32, ExponentialSpacing(; N=Nz, Δz_min), ring_grid) | ||
| # grid = ColumnGrid(CPU(), Float32, ExponentialSpacing(N=30)) | ||
| # Initial conditions | ||
| soil_initializer = FieldInitializers( | ||
| # steady-ish state initial condition for soil temperature | ||
| temperature = (x,z) -> 0 - 0.02f0*z, | ||
| # fully saturated soil | ||
| saturation_water_ice = 1.0f0, | ||
| ) | ||
|
|
||
| # Soil model with prescribed surface temperautre BC | ||
| model = SoilModel(grid, initializer=soil_initializer) | ||
| Tair_input = InputSource(eltype(grid), :air_temperature) | ||
| bcs = PrescribedSurfaceTemperature(:air_temperature) | ||
| integrator = initialize(model, ForwardEuler(eltype(grid)), Tair_input, boundary_conditions=bcs) | ||
|
|
||
| # Initialize Terrarium-Speedy land model | ||
| land = TerrariumDryLand(integrator) | ||
| # Set up coupled model | ||
| land_sea_mask = Speedy.RockyPlanetMask(land.spectral_grid) | ||
| output = Speedy.NetCDFOutput(land.spectral_grid, Speedy.PrimitiveDryModel, path="outputs/") | ||
| time_stepping = Speedy.Leapfrog(land.spectral_grid, Δt_at_T31=Minute(15)) | ||
| primitive_dry_coupled = Speedy.PrimitiveDryModel(land.spectral_grid; land, land_sea_mask, time_stepping, output) | ||
| # add soil temperature as output variable for Speedy simulation | ||
| Speedy.add!(primitive_dry_coupled.output, Speedy.SoilTemperatureOutput()) | ||
| # initialize coupled simulation | ||
| sim_coupled = Speedy.initialize!(primitive_dry_coupled) | ||
| # Speedy.timestep!(sim) | ||
| # run it | ||
| Speedy.run!(sim_coupled, period=Month(1), output=true) | ||
|
|
||
| # Soil temperature in the 5th layer (~0.54 m) | ||
| Tsoil_fig = heatmap(RingGrids.Field(interior(integrator.state.temperature)[:,1,end-4], grid), title="", size=(800,400)) | ||
| # Atmosphere variables | ||
| Tair_fig = heatmap(sim_coupled.diagnostic_variables.grid.temp_grid[:,8] .- 273.15, title="Air temperature", size=(800,400)) | ||
| pres_fig = heatmap(exp.(sim_coupled.diagnostic_variables.grid.pres_grid), title="Surface pressure", size=(800,400)) | ||
| srad_fig = heatmap(exp.(sim_coupled.diagnostic_variables.physics.surface_shortwave_down), title="Surface shortwave down", size=(800,400)) | ||
| # Tskin_fig = heatmap(RingGrids.Field(interior(integrator.state.skin_temperature)[:,1,end], grid), title="", size=(800,400)) | ||
| # save("plots/speedy_primitive_dry_coupled_tair_R48.png", Tair_fig, px_per_unit=1) | ||
| # save("plots/speedy_primitive_dry_coupled_tsoil_R48.png", Tsoil_fig, px_per_unit=1) | ||
|
|
||
| # animate surface air and soil temperatures | ||
| Speedy.animate(sim, variable="temp", coastlines=false, level=spectral_grid.nlayers, output_file="plots/speedy_terrarium_dry_air_temperature.mp4") | ||
| Speedy.animate(sim, variable="st", coastlines=false, level=1, output_file="plots/speedy_terrarium_dry_soil_temperature.mp4") | ||
|
|
||
| # pick a point somewhere in the mid-lattitudes | ||
| T = interior(integrator.state.temperature)[2000,1,:] | ||
| f = interior(integrator.state.liquid_water_fraction)[2000,1,:] | ||
| zs = znodes(integrator.state.temperature) | ||
| # Plot temperature and liquid fraction profiles in upper 15 layers | ||
| Makie.scatterlines(T[end-15:end], zs[end-15:end]) | ||
| Makie.scatterlines(f, zs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.