Skip to content

Support equivalent and identical units in capgen and ccpp-prebuild#656

Merged
mkavulich merged 17 commits into
NCAR:developfrom
climbfuji:feature/equivalent_units
May 1, 2025
Merged

Support equivalent and identical units in capgen and ccpp-prebuild#656
mkavulich merged 17 commits into
NCAR:developfrom
climbfuji:feature/equivalent_units

Conversation

@climbfuji
Copy link
Copy Markdown
Collaborator

@climbfuji climbfuji commented Apr 14, 2025

This PR adds logic to support equivalent and identical units in both capgen and ccpp-prebuild.

Note. This PR builds on top of #571 (credits @dustinswales). It contains all its commits, therefore we can either keep #571 until this PR is merged, or close it as superseded by #656.

  1. Support for identical units (m2 s-2 is identical to m+2 s-2). Internally, both capgen and ccpp-prebuild convert the former to the latter. For capgen, this happens deep down in the unit conversion logic. If capgen finds that two units are identical after parsing them, it returns a (None, None) unit conversion that tells the VarCompatibilityObject that the variables are equivalent w.r.t. units. For ccpp-prebuild, this happens when translating capgen metadata to ccpp-prebuild metadata. The outcome is the same.
  2. Support for equivalent units (m2 s-2 is equivalent to J kg-1). This is handled differently for the two code generators. While capgen skips the unit conversion step (VarCompatObj says variables are equivalent, hence no conversion required), ccpp-prebuild uses an identity transformation {var} = {var}.

As a result, the units metadata can now be written as m2 s-2 or m+2 s-2. Both are acceptable and dealt with by the two code generators.

The ccpp-prebuild implementation is simpler than the capgen implementation, because of the interface between the capgen-provided metadata from the parser and the metadata used by ccpp-prebuild, and also because the ccpp-prebuild implementation is more of a workaround (yes, prebuild needs to go).

User interface changes?: No

Fixes #570

Testing:
test removed: none
unit tests: all pass, including doc tests; added several doc tests for the new capabilities
system tests: all pass; extended capgen and ccpp-prebuild tests to cover the new features
manual testing: ran all unit and system tests on my laptop

@climbfuji climbfuji changed the base branch from main to develop April 14, 2025 19:02
Copy link
Copy Markdown
Collaborator Author

@climbfuji climbfuji left a comment

Choose a reason for hiding this comment

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

self-review

Comment thread scripts/var_props.py Outdated
Comment thread test/var_compatibility_test/test_host_mod.F90
@climbfuji climbfuji marked this pull request as ready for review April 14, 2025 20:38
@climbfuji climbfuji self-assigned this Apr 14, 2025
@climbfuji climbfuji added capgen bugs, requests, etc. that involve ccpp_capgen ccpp_prebuild bugs, requests, etc. that involve ccpp_prebuild labels Apr 14, 2025
@mkavulich
Copy link
Copy Markdown
Collaborator

Previous comment from @peverwhee #571 (review)

Copy link
Copy Markdown
Collaborator

@mkavulich mkavulich left a comment

Choose a reason for hiding this comment

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

Just some minor comments/suggestions

Comment thread scripts/common.py Outdated
Comment thread scripts/metadata_parser.py Outdated
end if
! Check values in data array2
write(error_unit,'(a,e12.4)') 'In unit_conv_scheme_1_run: checking min/max values of data array 2 to be approximately ', target_value2
if (minval(data_array2)<0.99*target_value2 .or. maxval(data_array2)>1.01*target_value2) then
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 probably doesn't matter, but do you know why we chose 1% deviation as the "acceptable" level of difference? That seems very large for a unit conversion.

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.

If the unit conversion isn't working as expected, the result will be off by a factor of 1E3 / 1E-3. Therefore, any arbitrary value that is smaller than that works.

climbfuji and others added 2 commits April 29, 2025 07:38
Co-authored-by: Michael Kavulich <kavulich@ucar.edu>
Co-authored-by: Michael Kavulich <kavulich@ucar.edu>
@climbfuji climbfuji requested a review from mkavulich April 29, 2025 13:40
@climbfuji
Copy link
Copy Markdown
Collaborator Author

@mkavulich I resolved the conflicts and tests all pass. Can we merge so that I don't have to do this again?

@mkavulich mkavulich merged commit 654b3c5 into NCAR:develop May 1, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

capgen bugs, requests, etc. that involve ccpp_capgen ccpp_prebuild bugs, requests, etc. that involve ccpp_prebuild

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants