Skip to content

New Basic Operations#454

Open
CharlesKrop wants to merge 12 commits into
NOAA-GFDL:developfrom
CharlesKrop:more_basic_operations
Open

New Basic Operations#454
CharlesKrop wants to merge 12 commits into
NOAA-GFDL:developfrom
CharlesKrop:more_basic_operations

Conversation

@CharlesKrop

@CharlesKrop CharlesKrop commented May 5, 2026

Copy link
Copy Markdown
Collaborator

Added 2D and 3D addition, subtraction, multiplication, division and a 2D copy (3D already exists) to basic_operations.py.

There are two sets of the mathematics stencils, one which modify inputs (a = a + b) and one which do not (a + b = c).

New stencils are covered by tests in test_basic_operations.py.

…d variants. also added 2d copy

all operations follow the pattern a + b = c, rather than modifying input fields
@CharlesKrop CharlesKrop requested review from romanc and twicki May 6, 2026 14:19

@romanc romanc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me. Thanks for adding those.

We could talk about consistency, e.g.

  • add_2d vs set_value_2D
  • adjustmentfactor_stencil vs adjust_divide_stencil - imo we could even loose the _stencil suffix
  • set_IJ_mask_value also looks out of sync

However, those were all previously there and would need - to be nice with our users - properly deprecated for a release before being removed.

Comment thread tests/test_basic_operations.py Outdated
Comment thread tests/test_basic_operations.py Outdated
Comment thread ndsl/stencils/basic_operations.py Outdated


def adjustmentfactor_stencil(adjustment: FloatFieldIJ, q_out: FloatField) -> None:
def copy_2d(input: FloatFieldIJ, output: FloatFieldIJ) -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

what is the use-case for this?
If you call this stencil with two 2D-Fields you can call it on interval(...) so it matches the 3d stencil if that is done forward as well.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure I follow you here. Are you just saying that I should swap interval(0, 1) to interval(...)? That's fair. it's done.

I don't have a use for the stencil at the moment, but I want to make sure I include 2d variants of everything so people have a full set available.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

reverted interval(...) back to interval(0, 1) to avoid over computing

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we only need this stencil specifically because input and output are typehinted as 2D?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If that's the case why not extend to IntField and IntField2d`?

Comment thread ndsl/stencils/basic_operations.py Outdated
Comment thread ndsl/stencils/basic_operations.py Outdated


def set_value(q_out: FloatField, value: Float) -> None:
def add_2d(input_1: FloatFieldIJ, input_2: FloatFieldIJ, output: FloatFieldIJ) -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same question as above

Comment thread ndsl/stencils/basic_operations.py Outdated
Comment thread ndsl/stencils/basic_operations.py Outdated
Comment thread ndsl/stencils/basic_operations.py Outdated
Comment thread ndsl/stencils/basic_operations.py Outdated
Comment thread ndsl/stencils/basic_operations.py Outdated
@CharlesKrop CharlesKrop marked this pull request as draft May 9, 2026 01:46
@CharlesKrop

Copy link
Copy Markdown
Collaborator Author

Back to draft for the weekend - I am adding more stencils to the batch

@CharlesKrop CharlesKrop marked this pull request as ready for review May 14, 2026 18:05
@CharlesKrop CharlesKrop requested review from romanc and twicki May 14, 2026 18:06

@oelbert oelbert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This adds a lot of stencils and I guess I'm curious how much we need to specify each of them here, especially 2D and 3D versions? How often are we only adding or only dividing in our numerics?

Comment thread ndsl/stencils/basic_operations.py Outdated


def adjustmentfactor_stencil(adjustment: FloatFieldIJ, q_out: FloatField) -> None:
def copy_2d(input: FloatFieldIJ, output: FloatFieldIJ) -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we only need this stencil specifically because input and output are typehinted as 2D?

Comment thread ndsl/stencils/basic_operations.py Outdated


def adjustmentfactor_stencil(adjustment: FloatFieldIJ, q_out: FloatField) -> None:
def copy_2d(input: FloatFieldIJ, output: FloatFieldIJ) -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If that's the case why not extend to IntField and IntField2d`?

@CharlesKrop

Copy link
Copy Markdown
Collaborator Author

This adds a lot of stencils and I guess I'm curious how much we need to specify each of them here, especially 2D and 3D versions? How often are we only adding or only dividing in our numerics?

Surprisingly often. I found that, when I don't have these stencils, I just end up wrapping the simple one-liner adds into other stencils, even if it doesn't make a lot of sense scientifically for them to be there, but once I had these stencils I started was more free to write discrete scientific blocks full of smaller, more narrowly tuned stencils, which has the added effect of allowing stencils names to more accurately act as (in my opinion) the best documentation.

@CharlesKrop

Copy link
Copy Markdown
Collaborator Author

To you other point - yes and no. 3d vs 2d need to be their own entity, because 3d can use computation/interval PARALLEL/..., while 2d must use (at least for now) FORWARD/0, 1.

It would be nice to say "this stencil can use any type/shape so long as all fields are of the same type/shape" and have one stencil work everything, but I think we are quite a way from having something like that.

@CharlesKrop

Copy link
Copy Markdown
Collaborator Author

A good example of their use can be found here: Fortran v NDSL (WIP)

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.

4 participants