ClimateFluidPhysics-ANU / MixedLayerThermoclineDynamics.jl

Julia packages that solves for a series of stacked thermocline layers coupled with a mixed layer on top.
https://climatefluidphysics-anu.github.io/MixedLayerThermoclineDynamics.jl/dev/
MIT License
3 stars 0 forks source link

`fill_halos!` functionality #8

Open navidcy opened 3 years ago

navidcy commented 3 years ago

We want a fill_halos! functionality. E.g., for periodic the process of filling up the halos is unambiguous, e.g.:

function fill_halos!(field::Field1D)
    nx, hx = field.grid.nx, field.grid.hx

    for j in 1:hx
        field.data[nx+j] = field.data[j]
        field.data[-j+1] = field.data[nx-j+1]
    end

    return nothing
end

Now, how do we make sure that the above is only applied for periodic dimensions? We need to think how to do that...

dhruvbhagtani commented 3 years ago

We can just have an if condition for grids. Something like:

function fill_halos!(field::Field1D)
    nx, hx = field.grid.nx, field.grid.hx

    if(typeof(field.grid) == Grid1D(Periodic))
        for j in 1:hx
            field.data[nx+j] = field.data[j]
            field.data[-j+1] = field.data[nx-j+1]
    else if(typeof(field.grid) == Grid1D(Bounded))
        ...........
    end

    return nothing
end
navidcy commented 3 years ago

We don't wanna be calling if statements... We wanna be writing different methods for different types of arguments.

navidcy commented 3 years ago

Also, if topology is Bounded still the way you fill the halo points depends on the actual boundary condition (Dirichet, Neumann, something else). Only for Periodic topologies the way you fill the halo regions is unambiguous.

We don't need to write the most general code at this point. But we just need to do something that it's correct. To support periodic boundary conditions is relatively easy. To support other boundary conditions we need to think a bit.

navidcy commented 3 years ago

We can just have an if condition for grids. Something like:

function fill_halos!(field::Field1D)
    nx, hx = field.grid.nx, field.grid.hx

    if(typeof(field.grid) == Grid1D(Periodic))
        for j in 1:hx
            field.data[nx+j] = field.data[j]
            field.data[-j+1] = field.data[nx-j+1]
    else if(typeof(field.grid) == Grid1D(Bounded))
        ...........
    end

    return nothing
end

What you wrote above could be done, for example, like (take this with a grain of salt... I just wrote the code here, didn't really test it):

function fill_halos!(field::Field1D{Any, G}) where G::Grid1D{Periodic}
    nx, hx = field.grid.nx, field.grid.hx
    for j in 1:hx
        field.data[nx+j] = field.data[j]
        field.data[-j+1] = field.data[nx-j+1]
    end

    return nothing
end

function fill_halos!(field::Field1D{Any, G}) where G::Grid1D{Bounded}
    nx, hx = field.grid.nx, field.grid.hx
    for j in 1:hx
        [do something else]
    end

    return nothing
end
navidcy commented 3 years ago

I'm continuing here some discussion that was going on in #9:

@dhruvbhagtani argues that fill_halos!() requires user data; @navidcy argues that it does not require any user data but simply the type of boundary conditions.

I gave some examples above for Periodic boundary conditions -- no data was required. Probably I'm missing something? @dhruvbhagtani can you elaborate a bit on an example where user-data is required to fill in the halo points?

dhruvbhagtani commented 3 years ago

Didn't we talk about this @navidcy, and I realised I was wrong? But, on second thought, I believe we require user data if we are dealing with anything other than periodic boundary conditions. For Dirichlet or Neumann boundary conditions, we require the user to give us some data for the end points.

navidcy commented 3 years ago

We did talk about this and settled some of the issues. But the issue is still open because we haven't implemented fill_halos! functionality for 2D yet.

Regarding data, if you mean like the 2 values of the field in each side of the domain, or of it's derivative then sure. If you mean anything more than that then I disagree, unless you convince me otherwise with an example. :)