Skip to content

Commit

Permalink
Merge pull request #6 from PALEOtoolkit/add_airsea
Browse files Browse the repository at this point in the history
Add AirSeaExchange Reactions
  • Loading branch information
sjdaines authored Apr 14, 2023
2 parents f465882 + f22f964 commit 501a8ba
Show file tree
Hide file tree
Showing 9 changed files with 618 additions and 32 deletions.
18 changes: 2 additions & 16 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
name = "PALEOocean"
uuid = "41de04b1-2efd-44ae-92ae-39d71a4fd99b"
authors = ["sd336 "]
version = "0.2.0"
version = "0.3.0"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
MAT = "23992714-dd62-5051-b70f-ba57cb901cac"
PALEOboxes = "804b410e-d900-4b2a-9ecd-f5a06d4c1fd4"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
SIMD = "fdea26ae-647d-5447-a871-4b548cad5224"
SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
TestEnv = "1e6cf692-eddd-4d53-88a5-2d735e33781b"
XLSX = "fdbf4ff8-1666-58a4-91e7-1b58723a45e0"

[compat]
DataFrames = "1.1"
Documenter = "0.27"
Infiltrator = "1.0"
Interpolations = "0.13, 0.14"
MAT = "0.10"
PALEOboxes = "0.20.4, 0.21"
PALEOmodel = "0.15.8"
Plots = "1.0"
Roots = "1.0, 2.0"
SIMD = "3.4"
SnoopPrecompile = "1.0"
TestEnv = "1.0"
XLSX = "0.7, 0.8, 0.9"
julia = "1.6"

[extras]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
NBInclude = "0db19996-df87-5ea3-a455-e3a50d440464"
PALEOmodel = "bf7b4fbe-ccb1-42c5-83c2-e6e9378b660c"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["CSV", "Documenter", "Logging", "NBInclude", "PALEOmodel", "Plots", "Test"]
test = ["Documenter", "Logging", "PALEOmodel", "Test"]
15 changes: 14 additions & 1 deletion docs/src/PALEOocean Reactions.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
# PALEOocean Reactions

## Ocean geometry and transport
```@meta
CurrentModule = PALEOocean.Ocean
```

## Ocean geometry and transport
```@docs
OceanNoTransport.ReactionOceanNoTransport
```

## Ocean surface
```@meta
CurrentModule = PALEOocean.Oceansurface
```

### Air-sea flux
```@docs
AirSeaExchange.ReactionAirSea
AirSeaExchange.ReactionAirSeaO2
AirSeaExchange.ReactionAirSeaCO2
AirSeaExchange.ReactionAirSeaCH4
AirSeaExchange.ReactionAirSeaFixedSolubility
```
3 changes: 2 additions & 1 deletion examples/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import Pkg

Pkg.activate(".") # use the PALEOocean/examples environment
Pkg.develop(path="../") # use the local version of PALEOocean packages to allow local modifications
Pkg.instantiate()
Pkg.instantiate()
Pkg.update()
28 changes: 14 additions & 14 deletions examples/transport_examples/TransportExamples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function PB.register_methods!(rj::ReactionTransportAdvectExample)
rj,
do_setup_advect,
(
PB.VarList_namedtuple(PB.VarDep.(PALEOocean.grid_vars_ocean)),
PB.VarList_namedtuple(PB.VarDep.(PALEOocean.Ocean.grid_vars_ocean)),
),
)

Expand All @@ -65,7 +65,7 @@ function do_setup_advect(m::PB.ReactionMethod, pars, (grid_vars, ), cellrange::P
# advection in a loop down column 1, up column 2
loopindices = vcat(rj.domain.grid.Icolumns[1], reverse(rj.domain.grid.Icolumns[2]), [first(rj.domain.grid.Icolumns[1])])
@info "do_setup_advect: $(PB.fullname(rj)) adding advective flux $(pars.T[]) (m^3 s-1) around loop $loopindices"
PALEOocean.add_loop!(A, grid_vars.volume, pars.T[], loopindices)
PALEOocean.Ocean.add_loop!(A, grid_vars.volume, pars.T[], loopindices)

# store the transpose as a sparse matrix for computational efficiency
# yr-1 s yr-1 s-1
Expand All @@ -77,18 +77,18 @@ end
function PB.register_dynamic_methods!(rj::ReactionTransportAdvectExample)

(transport_conc_vars, transport_sms_vars, transport_input_vars) =
PALEOocean.find_transport_vars(rj.domain, add_transport_input_vars=true)
PALEOocean.Ocean.find_transport_vars(rj.domain, add_transport_input_vars=true)

PB.add_method_do!(
rj,
do_advect,
(
PB.VarList_namedtuple(PB.VarDep.(PALEOocean.grid_vars_ocean)),
PB.VarList_namedtuple(PB.VarDep.(PALEOocean.Ocean.grid_vars_ocean)),
PB.VarList_components(transport_conc_vars),
PB.VarList_components(transport_sms_vars),
PB.VarList_components(transport_input_vars),
),
preparefn=PALEOocean.prepare_transport
preparefn=PALEOocean.Ocean.prepare_transport
)

return nothing
Expand All @@ -103,7 +103,7 @@ function do_advect(
)
rj = m.reaction

PALEOocean.do_transport_tr(
PALEOocean.Ocean.do_transport_tr(
grid_vars, transport_conc_components, transport_sms_components, transport_input_components, buffer,
rj.trspt_dtm_tr,
cellrange
Expand Down Expand Up @@ -144,7 +144,7 @@ function PB.register_methods!(rj::ReactionTransportDiffuseExample)
rj,
do_setup_diffuse,
(
PB.VarList_namedtuple(PB.VarDep.(PALEOocean.grid_vars_ocean)),
PB.VarList_namedtuple(PB.VarDep.(PALEOocean.Ocean.grid_vars_ocean)),
),
)

Expand Down Expand Up @@ -186,7 +186,7 @@ function do_setup_diffuse(m::PB.ReactionMethod, pars, (grid_vars, ), cellrange::
# m^3 s-1 = m^2 / m * m^2 s-1
F = Aintf/(grid_vars.zmid[icell_top] - grid_vars.zmid[icell_bot])*pars.Kz[]
loopindices = (icell_top, icell_bot, icell_top)
PALEOocean.add_loop!(A, grid_vars.volume, F, loopindices)
PALEOocean.Ocean.add_loop!(A, grid_vars.volume, F, loopindices)
end
end

Expand All @@ -200,18 +200,18 @@ end
function PB.register_dynamic_methods!(rj::ReactionTransportDiffuseExample)

(transport_conc_vars, transport_sms_vars, transport_input_vars) =
PALEOocean.find_transport_vars(rj.domain, add_transport_input_vars=true)
PALEOocean.Ocean.find_transport_vars(rj.domain, add_transport_input_vars=true)

PB.add_method_do!(
rj,
do_diffuse,
(
PB.VarList_namedtuple(PB.VarDep.(PALEOocean.grid_vars_ocean)),
PB.VarList_namedtuple(PB.VarDep.(PALEOocean.Ocean.grid_vars_ocean)),
PB.VarList_components(transport_conc_vars),
PB.VarList_components(transport_sms_vars),
PB.VarList_components(transport_input_vars),
),
preparefn=PALEOocean.prepare_transport
preparefn=PALEOocean.Ocean.prepare_transport
)

return nothing
Expand All @@ -226,7 +226,7 @@ function do_diffuse(
)
rj = m.reaction

PALEOocean.do_transport_tr(
PALEOocean.Ocean.do_transport_tr(
grid_vars, transport_conc_components, transport_sms_components, transport_input_components, buffer,
rj.trspt_dtm_tr,
cellrange
Expand Down Expand Up @@ -313,7 +313,7 @@ function PB.set_model_geometry(rj::ReactionOceanColumnGrid, model::PB.Model)
cellnames=Dict(name=>i for (i, name) in enumerate(rj.grid_ocean.columnnames)))
PB.Grids.set_subdomain!(rj.grid_oceanfloor, "ocean", PB.Grids.InteriorSubdomain(ocean_cells, ifloor), true)

PALEOocean.set_model_domains(model, rj.grid_ocean, rj.grid_oceansurface, rj.grid_oceanfloor)
PALEOocean.Ocean.set_model_domains(model, rj.grid_ocean, rj.grid_oceansurface, rj.grid_oceanfloor)

return nothing
end
Expand All @@ -326,7 +326,7 @@ function PB.register_methods!(rj::ReactionOceanColumnGrid)
rj,
do_setup_grid,
(
PB.VarList_namedtuple(PALEOocean.grid_vars_all),
PB.VarList_namedtuple(PALEOocean.Ocean.grid_vars_all),
),
)

Expand Down
98 changes: 98 additions & 0 deletions examples/transport_examples/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Logging
using Test

import PALEOboxes as PB
import PALEOmodel
import PALEOocean

include("TransportExamples.jl")

@testset "transport_examples" begin

skipped_testsets = [
# "transport_advect",
# "transport_diffuse",
]

!("transport_advect" in skipped_testsets) && @testset "transport_advect" begin

model = PB.create_model_from_config(
joinpath(@__DIR__, "TransportExamples_cfg.yaml"), "example_advect"
)

initial_state, modeldata = PALEOmodel.initialize!(model)

# bodge an updated initial_state for testing
ocean_T = PB.get_data(PB.get_variable(model, "ocean.T"), modeldata) # model data array for tracer T
ocean_T[1] = 2e14*200.0*1.0 # ~ 1 mol m-3 in first cell (top of first column)
initial_state = PALEOmodel.get_statevar(modeldata.solver_view_all)

tspan=(0.0, 5e3)

run = PALEOmodel.Run(model=model, output = PALEOmodel.OutputWriters.OutputMemory())

PALEOmodel.ODE.integrateForwardDiff(
run, initial_state, modeldata, tspan,
solvekwargs=(reltol=1e-5,),
)

println("conservation checks:")
conschecks = [
("ocean", "T_total", 1e-14),
]
for (domname, varname, rtol) in conschecks
startval, endval = PB.get_data(run.output, domname*"."*varname)[[1, end]]
println(" check $domname.$varname $startval $endval $rtol")
@test isapprox(startval, endval, rtol=rtol)
end

end

!("transport_diffuse" in skipped_testsets) && @testset "transport_diffuse" begin

model = PB.create_model_from_config(
joinpath(@__DIR__, "TransportExamples_cfg.yaml"), "example_diffuse"
)

initial_state, modeldata = PALEOmodel.initialize!(model)

# bodge an updated initial_state for testing
ocean_T = PB.get_data(PB.get_variable(model, "ocean.T"), modeldata) # model data array for tracer T
ocean_T[50] = 2e14*20.0*1.0 # ~ 1 mol m-3 in middle cell of first column
ocean_T[100+5] = 2e13*200.0*1.0 # ~ 1 mol m-3 in middle cell of second column
initial_state = PALEOmodel.get_statevar(modeldata.solver_view_all)

tspan=(0.0, 1e6)

run = PALEOmodel.Run(model=model, output = PALEOmodel.OutputWriters.OutputMemory())

PALEOmodel.ODE.integrateForwardDiff(
run, initial_state, modeldata, tspan,
solvekwargs=(reltol=1e-5,),
)

println("conservation checks:")
conschecks = [
("ocean", "T_total", 1e-14),
]
for (domname, varname, rtol) in conschecks
startval, endval = PB.get_data(run.output, domname*"."*varname)[[1, end]]
println(" check $domname.$varname $startval $endval $rtol")
@test isapprox(startval, endval, rtol=rtol)
end

println("check values at end of run:")
checkvals = [
("ocean", "T_conc", 0.01*ones(110), 1e-5 ), # check uniform concentration in 110 ocean cells
]
for (domname, varname, checkval, rtol) in checkvals
outputval = PB.get_data(run.output, domname*"."*varname)[end]
println(" check $domname.$varname $outputval $checkval $rtol")
@test isapprox(outputval, checkval, rtol=rtol)
end

end



end
2 changes: 2 additions & 0 deletions src/PALEOocean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ end

include("ocean/Ocean.jl")

include("oceansurface/Oceansurface.jl")


end # module PALEOocean
Loading

2 comments on commit 501a8ba

@sjdaines
Copy link
Member Author

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/81594

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.3.0 -m "<description of version>" 501a8ba5012fab5a82cbfe731f1277ec91eb34c2
git push origin v0.3.0

Please sign in to comment.