Skip to content
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

Interpolate to new grid #231

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

Interpolate to new grid #231

wants to merge 14 commits into from

Conversation

johnomotani
Copy link
Collaborator

@johnomotani johnomotani commented Dec 24, 2021

Add methods BoutDataset.interpolate_to_new_grid()/BoutDataArray.interpolate_to_new_grid() to interpolate a Dataset or DataArray to a new grid. The new grid should be defined by a new grid file created using the same equilibrium as the original grid. Interpolation is done first radially (using psi as the coordinate) and then in the parallel direction (using poloidal distance along a psi-contour as the coordinate).

Also includes a new method BoutData*.interpolate_radial(), for radial interpolation, which is used inside interpolate_to_new_grid().

Requires grid files generated using a version of hypnotoad including the changes in this PR, which saves poloidal distances to the grid files: boutproject/hypnotoad#116. hypnotoad>=0.4.3.

Similar functionality to boutproject/boutdata#56, but interpolates in radial/parallel directions rather than R/Z. Using radial/parallel may be an advantage when interpolating 3d restart files.

Todo:

  • Unit test all functionality of utility functions _update_metadata_increased_x_resolution() and _update_metadata_increased_y_resolution().
  • Unit test _make_1d_xcoord().
  • Tests for interpolate_radial(). These could probably follow the pattern of those already existing for interpolate_parallel.
  • Tests for interpolate_to_new_grid().
  • Option to do radial interpolation in field-aligned coordinates. Probably only suitable if most of the turbulence is around the outboard midplane, but may result in a smaller initial transient when restarting on the new grid due to better field-alignment of the perturbations.
  • Interpolation in the toroidal direction.

The new grid file should represent the same equilibrium as the original
grid.
When using poloidal_distance for parallel interpolation, calculation was
previously very slow due to explicit loop over x. Using apply_ufunc()
instead helps.
@pep8speaks
Copy link

pep8speaks commented Dec 24, 2021

Hello @johnomotani! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-12-27 18:43:54 UTC

@johnomotani johnomotani added the enhancement New feature or request label Dec 24, 2021
@codecov-commenter
Copy link

codecov-commenter commented Dec 25, 2021

Codecov Report

Merging #231 (7beece4) into master (212b407) will decrease coverage by 4.20%.
The diff coverage is 23.67%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master     #231      +/-   ##
==========================================
- Coverage   68.94%   64.75%   -4.20%     
==========================================
  Files          15       16       +1     
  Lines        3214     3447     +233     
  Branches      792      856      +64     
==========================================
+ Hits         2216     2232      +16     
- Misses        734      944     +210     
- Partials      264      271       +7     
Impacted Files Coverage Δ
xbout/boutdataset.py 66.66% <3.79%> (-10.07%) ⬇️
xbout/boutdataarray.py 59.79% <25.00%> (-18.57%) ⬇️
xbout/utils.py 77.54% <43.47%> (-4.66%) ⬇️
xbout/bout_info.py 100.00% <100.00%> (ø)
xbout/geometries.py 69.63% <100.00%> (-0.32%) ⬇️
xbout/region.py 83.59% <100.00%> (+0.03%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Need to set values in metadata, not directly in da.
MXG and MYG are now set correctly in _open_grid(), so can apply geometry
just by passing the argument to open_boutdataset() now.
When zShift is saved by BOUT++ it may have NaN in some corner
boundary/guard cells. When doing FFTs of arrays, these NaNs can make the
whole output NaN, even in entries that should not depend on the corner
cells of zShift. To avoid this, replace the NaNs with 0 - nothing
important should depend on the corner cell values, so this should not
cause problems.
@johnomotani johnomotani added the help wanted Extra attention is needed label Dec 27, 2021
Avoids circular dependencies when importing these lists into different
files.
The attributes "cell_location", "direction_y", "direction_z",
"bout_type" are written by BOUT++ for each variable, so should be
preserved.
if not isinstance(new_gridfile, xr.Dataset):
new_gridfile = open_boutdataset(
new_gridfile,
keep_xboundaries=da.metadata["keep_xboundaries"],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi @johnomotani. This is throwing an error on my end because da is not defined. Seems like a simple typo and it should be self.da.metadata instead of da.metadata. Is that correct or did I miss something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's correct. Maybe cleanest solution is just to move the

da = self.data

from L.724 to the top of the method, and (for consistency) replace all of the self.data with da.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants