Giter Club home page Giter Club logo

Comments (9)

simone-silvestri avatar simone-silvestri commented on June 1, 2024 2

I like the idea adding a method for tuples and named tuples. It seems to be the behavior we want when compute!ing on collection of fields.

from oceananigans.jl.

glwagner avatar glwagner commented on June 1, 2024 1

So basically we could add two methods

compute!(tup::Tuple) = Tuple(compute!(t) for t in tup)
compute!(nt::NamedTuple) = NamedTuple(name => compute!(nt[name]) for name in keys(nt))

as a convenience to auto-compute Fields that are embedded in tuples or named tuples that are arguments to KernelFunctionOperation (which I think is the main application that I can see)

from oceananigans.jl.

glwagner avatar glwagner commented on June 1, 2024

I'm having trouble following your example. Can you come up with a simpler illustration of the issue and avoid constructions with many operations on a line?

from oceananigans.jl.

glwagner avatar glwagner commented on June 1, 2024

Does this illustrate the problem?

using Oceananigans
using Oceananigans.AbstractOperations: KernelFunctionOperation
using Oceananigans.Fields: @compute

grid = RectilinearGrid(size = (1, 1, 1), extent = (1, 1, 1))
model = NonhydrostaticModel(; grid)
set!(model, u=2)
get_u(i, j, k, grid, velocities) = velocities.u[i, j, k]

f_op = KernelFunctionOperation{Face, Center, Center}(get_u, model.grid, velocities)
# f_op[1, 1, 1] works?

u_avg = Field(Average(u))
u_prime = u - u_avg
perturbation_velocities = (; u = u_prime)
f_perturbation_op = KernelFunctionOperation{Face, Center, Center}(get_u, model.grid, perturbation_velocities)
# f_perturbation_op[1, 1, 1] doesn't work?

If so, check that u_avg is actually computed.

from oceananigans.jl.

tomchor avatar tomchor commented on June 1, 2024

Yes, that's the core of it. I modified your example a bit to make it run and I'm posting it here for completeness:

using Oceananigans

grid = RectilinearGrid(size = (1, 1, 1), extent = (1, 1, 1))
model = NonhydrostaticModel(; grid)
set!(model, u=2)
get_u(i, j, k, grid, velocities) = velocities.u[i, j, k]

f_op = KernelFunctionOperation{Face, Center, Center}(get_u, model.grid, model.velocities)

u_avg = Field(Average(model.velocities.u))
u_prime = model.velocities.u - u_avg
perturbation_velocities = (; u = u_prime)
f_perturbation_op = KernelFunctionOperation{Face, Center, Center}(get_u, model.grid, perturbation_velocities)

Then f_op gives the correct answer, but f_perturbation_op does not.

And yes, I agree the core of the issue is that u_avg isn't computed before. If I first compute u_avg, then f_perturbation_op also produces the correct result.

But is that the expected behavior? I'd expect either u_avg to be computed before computing f_perturbation_op or for a warning/error to be thrown.

from oceananigans.jl.

glwagner avatar glwagner commented on June 1, 2024

Hmm, well it looks like we do compute arguments:

compute_at!(Îș::KernelFunctionOperation, time) = Tuple(compute_at!(d, time) for d in Îș.arguments)

But if the arguments are themselves wrapped inside a NamedTuple --- or any other object --- then they won't be computed. In other words compute!(perturbation_velocities) does not compute the elements of perturbation_velocities.

We could add a method compute!(tup::Tuple) = Tuple(compute!(t) for t in tup) and also for NamedTuple. But I'm not sure this is the best API. Maybe it's better to require that arguments that need to be computed should be included directly as arguments. Seems like its up for debate.

PS it does seem to test this correctly then we need to further evaluate

f_perturbation = Field(f_perturbation_op)
compute!(f_perturbation)

from oceananigans.jl.

glwagner avatar glwagner commented on June 1, 2024

This seems reasonable. I'm not sure if there are performance implications. @navidcy @simone-silvestri any thoughts?

from oceananigans.jl.

glwagner avatar glwagner commented on June 1, 2024

Perhaps a more concise and extendable implementation would be

compute!(collection::Union{Tuple, NamedTuple}) = map(compute!, collection)

inspired by Adapt

from oceananigans.jl.

glwagner avatar glwagner commented on June 1, 2024

test

grid = RectilinearGrid(size = (1, 1, 1), extent = (1, 1, 1))
c = CenterField(grid)
set!(c, 1)
at_ijk(i, j, k, grid, a) = a[i, j, k]

one_c = Field(1 * c)
two_c = tuple(Field(2 * c))
ten_c = (; ten_c = Field(10 * c))

one_c_op = KernelFunctionOperation{Center, Center, Center}(at_ijk, grid, one_c)
two_c_op = KernelFunctionOperation{Center, Center, Center}(at_ijk, grid, two_c)
ten_c_op = KernelFunctionOperation{Center, Center, Center}(at_ijk, grid, ten_c)

one_c_field = Field(one_c_op)
two_c_field = Field(two_c_op)
ten_c_field = Field(ten_c_op)

compute!(one_c_field)
compute!(two_c_field)
compute!(ten_c_field)

@test all(interior(one_c) .== 1)
@test all(interior(two_c) .== 2)
@test all(interior(ten_c) .== 10)

from oceananigans.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.