tomchor / Oceanostics.jl

Diagnostics for Oceananigans
https://tomchor.github.io/Oceanostics.jl/
MIT License
24 stars 8 forks source link

Add `TracerVarianceBudgetTerms` module #118

Closed tomchor closed 1 year ago

tomchor commented 1 year ago

This does two things:

  1. Adds TracerVarianceBudgetTerms module (along with TracerVarianceTendency and TracerVarianceDiffusiveTerm)
  2. Simplifies organization of tests, while making them broader

Point 1 creates a module dedicated for tracer variance budgets and uses Oceananigans kernels directly to calculate the terms. I'll add more terms slowly one by one, but using Oceananigans kernels should make this process easy and (since there are no interpolations to get tracer variance in (Center, Center, Center), we can guarantee that the terms are conserve tracer variance up to machine precision.

In fact, one advantage is that this gives a robust point of comparison for our tracer variance dissipation rate, whose average is now tested against TracerVarianceTendency and TracerVarianceDiffusiveTerm up to machine precision to ensure that it's formulation is conservative.

Point 2 makes the testing code much simpler, and now we're also going to test everything for every combination between several closures, model types, and grid types. Something like this:

@testset "Oceanostics" begin
    for grid in (regular_grid, stretched_grid)
        for model_type in (NonhydrostaticModel, HydrostaticFreeSurfaceModel)
            for closure in closures
                #Test things
...

This is likely overkill, but it does make the code much more readable and the code case for now is small enough that tests still run pretty fast. (Something like 20 minutes on my laptop, not counting the budget tests, which have to evolve a simulation.)

tomchor commented 1 year ago

For future reference and clarity:

In this PR I'm also making the requirements for the budget test (where we evolve a simulation, time integrate the tracer variance dissipation and compare it with the tracer variance) more relaxed. This follows a discussion on slack about making "top-down" estimates (in this case the tracer variance computed as c^2) match with "bottom-up" estimates (i.e. the time-integrated tracer variance dissipation rate).

The main takeaway from that discussion for me was that, unless we get the calculation of (i) the tracer variance dissipation rate and (ii) the time-integration scheme to exactly match what Oceananigans does, the results will depend on grid resolution and time-step.

The previous budget test was trying to get both (i) and (ii) right, and failing to do so exactly (particularly is was failing on (ii), which is far from trivial). However, (ii) is not a goal of this package (instead that is up to the users) but (i) is. So we are now testing (i) up to machine precision by intercomparing results from one snapshot directly with Oceananigans kernels, which is a much better (and faster) way to do things imo.

I'm not 100% sure what to do with budget evolution, but for now I'm increasing the tolerance of the test to 2% and adjusting the time-step with a TimeStepWizard which makes it run faster and still makes sure that the top-down estimates approximately match the bottom-up ones. I think that's good compromise, although I'm not not totally against removing that test altogether (especially if tests start to become slow in the future).

glwagner commented 1 year ago

This paper might be relevant to calculations of numerical + explicit dissipation:

https://www.sciencedirect.com/science/article/pii/S1463500314000754?casa_token=WpWBDmsmS-EAAAAA:BMPNh2q4glKqfTbmcs_1qnR1EB_rmv7e96cxWIwiTVNyHVOeQpTB7aznm2wKOouidG_a6YFLYw

I believe this is essentially an unsolved problem though a number of people are working on it.