-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bugs in ESMF interpolation method #544
Fix bugs in ESMF interpolation method #544
Conversation
This bug yielded garbage results when interpolating between two MPAS meshes. When interpolating from a regular grid to an MPAS grid, it would cause a shift of one cell on the regular grid.
For interpolating from 4km AIS (385379 cells) to 8km AIS (98341 cells) this reduced interpolation time for one variable from 13 seconds to 0.04 seconds (~300x speedup).
This was never attempted before and needed additional handling.
See section 12.5 of ESMF_RegridWeightGen documentation: https://earthsystemmodeling.org/docs/release/ESMF_8_0_1/ESMF_refdoc/node3.html#SECTION03025000000000000000
@trhille , I decided to leave this branch as is and ask for review now (no rush). The issue about the conserve method filling 0s in regions of extrapolation of the source mesh is a larger problem that it is not obvious how to resolve, so I left it as a known issue for now. But if you have ideas about appropriate functionality for that situation, I'm happy to do further work in this PR. |
@xylar , there is a commit in this PR that makes an update to
Can you explain to me how this is supposed to be handled? I've never fully understood how the conda package interacts (or doesn't) with the legacy scripts. |
@matthewhoffman, when you do a local install of the
it makes a copy of the mesh_tools, ocean, land_ice and other directories that contain scripts because it can't include anything in the python package that lives outside of Ideally, if we want to have |
@xylar , thanks for clarifying that. I had run |
For large grids, this script was very slow. For the 500m BedMachine-AIS dataset, it's been running for over half an hour and is not complete. After refactoring, it took about 3 minutes.
5b9e298
to
f758259
Compare
TestingThese updated scripts were used for the 2km AIS mesh generation in MPAS-Dev/compass#750 and it worked as expected end to end. The refactoring sped up those individual operations dramatically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matthewhoffman, this is fantastic! Thanks for making these changes, which collectively reduce the pain of mesh generation by a huge margin. I have one measly complaint about an inscrutable variable name, but otherwise this is ready to go.
wfile.close() | ||
#---------------------------- | ||
|
||
# convert to sparse matrix format | ||
wt_csr = scipy.sparse.coo_array((S, (row - 1, col - 1)), shape=(n_b, n_a)).tocsr() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dislike the variable name wt_csr
, because it requires googling to figure out what it means. Can we use a longer, more descriptive name? Also, is wt
short for weight
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in b0e6147.
sourceFieldFlat = sourceField.flatten() # Flatten source field | ||
for i in range(len(row)): | ||
destinationField[row[i]-1] = destinationField[row[i]-1] + S[i] * sourceFieldFlat[col[i]] | ||
source_csr = scipy.sparse.csr_matrix(sourceField.flatten()[:, np.newaxis]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same complaint as above about the csr
in the variable name, although here it's adjacent to the scipy method that helps explain it some.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in b0e6147.
@@ -328,7 +337,7 @@ def interpolate_field_with_layers(MPASfieldName): | |||
if filetype=='cism': | |||
print(' Input layer {}, layer {} min/max: {} {}'.format(z, InputFieldName, InputField[z,:,:].min(), InputField[z,:,:].max())) | |||
elif filetype=='mpas': | |||
print(' Input layer {}, layer {} min/max: {} {}'.format(z, InputFieldName, InputField[:,z].min(), InputField[z,:].max())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this just a typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this was a typo that has been present from whenever this print statement was introduced. It wasn't obviously incorrect from the output because the typo version still yielded reasonable values.
I'm about to make a new release to fix #547. If this can be merged today, I'll wait for it. Otherwise, it will be in the next release. |
On second thought, I'll be testing in E3SM-Unified for a few more days most likely so if this gets merged in the next 2 or 3 days, that should be soon enough. |
@trhille , thanks for the feedback on these changes. I've added a little more detail to the comments and improved the new variable name. If you prefer more detail, let me know. |
@matthewhoffman, thanks! That looks great. This is ready to merge, as far as I'm concerned, although I see that two of the CI checks were not successful. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SCRIP changes look good to me!
I need to make an MPAS-Tools release ASAP so let me know if this can get merged today. |
This merge fixes a number of issues with the 'esmf' method for interpolation in the MALI interpolation script:
Two issues remain for interpolating between two MPAS meshes with the 'esmf' method:
This PR also includes a refactoring of create_SCRIP_file_from_planar_rectangular_grid.py that speeds it up by orders of magnitude for large meshes, as well as a minor update to define_cullMask.py