Skip to content

Conversation

@simone-silvestri
Copy link
Collaborator

@simone-silvestri simone-silvestri commented Sep 4, 2025

this PR should allow the pattern described in #4758, for example:

julia> grid = RectilinearGrid(topology=(Bounded, Bounded, Bounded), size=(4, 4, 4), extent=(1, 1, 1));

julia> boundary_conditions = FieldBoundaryConditions(top = GradientBoundaryCondition(1));

julia> CenterField(grid; boundary_conditions).boundary_conditions
Oceananigans.FieldBoundaryConditions, with boundary conditions
├── west: FluxBoundaryCondition: Nothing
├── east: FluxBoundaryCondition: Nothing
├── south: FluxBoundaryCondition: Nothing
├── north: FluxBoundaryCondition: Nothing
├── bottom: FluxBoundaryCondition: Nothing
├── top: GradientBoundaryCondition: 1.0
└── immersed: Nothing

julia> XFaceField(grid; boundary_conditions).boundary_conditions
Oceananigans.FieldBoundaryConditions, with boundary conditions
├── west: OpenBoundaryCondition{Nothing}: Nothing
├── east: OpenBoundaryCondition{Nothing}: Nothing
├── south: FluxBoundaryCondition: Nothing
├── north: FluxBoundaryCondition: Nothing
├── bottom: FluxBoundaryCondition: Nothing
├── top: GradientBoundaryCondition: 1.0
└── immersed: Nothing
julia> grid = ImmersedBoundaryGrid(grid, GridFittedBottom(-1));

julia> YFaceField(grid; boundary_conditions).boundary_conditions
Oceananigans.FieldBoundaryConditions, with boundary conditions
├── west: FluxBoundaryCondition: Nothing
├── east: FluxBoundaryCondition: Nothing
├── south: OpenBoundaryCondition{Nothing}: Nothing
├── north: OpenBoundaryCondition{Nothing}: Nothing
├── bottom: FluxBoundaryCondition: Nothing
├── top: GradientBoundaryCondition: 1.0
└── immersed: FluxBoundaryCondition: Nothing

closes #4758

@simone-silvestri
Copy link
Collaborator Author

I guess we cannot really regularize_boundary_conditions because fields do not have the information of dependencies, but we can add another method that correctly replaces the DefaultBoundaryCondition

@simone-silvestri simone-silvestri changed the title Regularize FieldBoundaryConditions in Field constructor materialize DefaultBoundaryCondition from bcs in Field constructor Sep 4, 2025
@simone-silvestri
Copy link
Collaborator Author

As part of this PR, I have removed the boundary_condition inside the DefaultBoundaryCondition.
In my opinion, the default is uniquely defined and should depend on location and grid; user-defined BCs should be explicitly specified rather than changing a default globally (which is not even possible because Face and Center locations do not accept the same bcs so this approach could very well lead to error)

end

DefaultBoundaryCondition() = DefaultBoundaryCondition(NoFluxBoundaryCondition())
struct DefaultBoundaryCondition end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you change this design?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of this PR, I have removed the boundary_condition inside the DefaultBoundaryCondition.
In my opinion, the default is uniquely defined and should depend on location and grid; user-defined BCs should be explicitly specified rather than changing a default globally (which is not even possible because Face and Center locations do not accept the same bcs so this approach could very well lead to error)

Copy link
Collaborator Author

@simone-silvestri simone-silvestri Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought to remove it for this reason and in particular because validate_boundary_conditions is only grid and location dependent (not model dependent), so it basically forces a limited default choice. But of course, I am open to discuss about it and keep this feature if this is the consensus 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, what do you think about models / situations in which "no flux" is irrelevant. For example a model that has no derivatives.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have the default be a nothing like on Faces, this would then be general.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That isn't general because models with gradients require NoFluxBoundaryCondition() as a default.

@glwagner
Copy link
Member

glwagner commented Sep 4, 2025

I guess we cannot really regularize_boundary_conditions because fields do not have the information of dependencies, but we can add another method that correctly replaces the DefaultBoundaryCondition

No, this is ok --- only prognostic fields can have field dependencies, and those are built separately inside the model constructor. This PR applies to auxiliary fields only.

north = DefaultBoundaryCondition(),
bottom = DefaultBoundaryCondition(),
top = DefaultBoundaryCondition(),
immersed = DefaultBoundaryCondition()) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this PR removes a nice feature that I designed for changing the default boundary condition. Note that this code is meant to be applicable to a wide class of models --- not just NonhydrostaticModel and HydrostaticFreeSurfaceModel.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this PR makes the previous code more useful...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can keep the default in the DefaultBoundaryCondition. I guess having it or not is not really the scope of this PR

@simone-silvestri
Copy link
Collaborator Author

This last commit should have allowed the possibility of choosing a general default without needing to use a field internal to DefaultBoundaryCondition.
The UI remains the same, i.e. on main and on this PR

bcs = FieldBoundaryConditions(FluxBoundaryCondition(10))

will have the same effect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no method matching fill_halo_kernel! error when building Fields with explicit boundary conditions

3 participants