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

Fix some plot issues when NFP differs from one for objects, or when passed-in phi exceeds 2pi/nfp #1204

Merged
merged 15 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions desc/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,10 +1352,9 @@
phi = np.atleast_1d(phi)
nphi = len(phi)
if grid is None:
nfp = eq.NFP
grid_kwargs = {
"L": 25,
"NFP": nfp,
"NFP": 1,
"axis": False,
"theta": np.linspace(0, 2 * np.pi, 91, endpoint=True),
"zeta": phi,
Expand Down Expand Up @@ -1610,9 +1609,14 @@
phi = np.atleast_1d(phi)
nphi = len(phi)

# do not need NFP supplied to these grids as
# the above logic takes care of the correct phi range
# if defaults are requested. Setting NFP here instead
# can create reshaping issues when phi is supplied and gets
# truncated by 2pi/NFP. See PR #1204
grid_kwargs = {
"rho": rho,
"NFP": nfp,
"NFP": 1,
"theta": np.linspace(0, 2 * np.pi, NT, endpoint=True),
"zeta": phi,
}
Expand All @@ -1631,7 +1635,7 @@
)
grid_kwargs = {
"rho": np.linspace(0, 1, NR),
"NFP": nfp,
"NFP": 1,
"theta": theta,
"zeta": phi,
}
Expand Down Expand Up @@ -1960,7 +1964,7 @@
plot_axis = plot_axis and eq.L > 0
rho = np.array([0.0, 1.0]) if plot_axis else np.array([1.0])

grid_kwargs = {"NFP": eq.NFP, "rho": rho, "theta": 100, "zeta": phi}
grid_kwargs = {"NFP": 1, "rho": rho, "theta": 100, "zeta": phi}
grid = _get_grid(**grid_kwargs)
nr, nt, nz = grid.num_rho, grid.num_theta, grid.num_zeta
grid = Grid(
Expand Down Expand Up @@ -2030,6 +2034,9 @@
):
"""Plot stellarator boundaries at multiple toroidal coordinates.

NOTE: If attempting to plot objects with differing NFP, `phi` must
be given explicitly.

Parameters
----------
eqs : array-like of Equilibrium, Surface or EquilibriaFamily
Expand Down Expand Up @@ -2085,7 +2092,21 @@
fig, ax = plot_boundaries((eq1, eq2, eq3))

"""
# if NFPs are not all equal, means there are
# objects with differing NFPs, which it is not clear
# how to choose the phis for by default, so we will throw an error
# unless phi was given.
phi = parse_argname_change(phi, kwargs, "zeta", "phi")
errorif(

Check warning on line 2100 in desc/plotting.py

View check run for this annotation

Codecov / codecov/patch

desc/plotting.py#L2100

Added line #L2100 was not covered by tests
not np.allclose([thing.NFP for thing in eqs], eqs[0].NFP) and phi is None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

@dpanici Maybe we should change this to

not np.allclose([thing.NFP for thing in eqs], eqs[0].NFP) and (phi is None or isinstance(phi, numbers.Integral)),

Because integer phi is still ambigious.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, what does it do rn if phi integral is passed in? btw 0->2pi or 0->NFP of the first thing?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It uses 0 to 2pi/nfp of the last thing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, doesn't throw any error or warning.

ValueError,
"supplied objects must have the same number of field periods, "
"or if there are differing field periods, `phi` must be given explicitly."
f" Instead, supplied objects have NFPs {[t.NFP for t in eqs]}."
" If attempting to plot an axisymmetric object with non-axisymmetric objects,"
" you must use the `change_resolution` method to make the axisymmetric "
"object have the same NFP as the non-axisymmetric objects.",
)

figsize = kwargs.pop("figsize", None)
cmap = kwargs.pop("cmap", "rainbow")
Expand Down Expand Up @@ -2129,7 +2150,7 @@
plot_axis_i = plot_axis and eqs[i].L > 0
rho = np.array([0.0, 1.0]) if plot_axis_i else np.array([1.0])

grid_kwargs = {"NFP": eqs[i].NFP, "theta": 100, "zeta": phi, "rho": rho}
grid_kwargs = {"NFP": 1, "theta": 100, "zeta": phi, "rho": rho}

Check warning on line 2153 in desc/plotting.py

View check run for this annotation

Codecov / codecov/patch

desc/plotting.py#L2153

Added line #L2153 was not covered by tests
grid = _get_grid(**grid_kwargs)
nr, nt, nz = grid.num_rho, grid.num_theta, grid.num_zeta
grid = Grid(
Expand Down Expand Up @@ -2198,6 +2219,9 @@
):
"""Plot comparison between flux surfaces of multiple equilibria.

NOTE: If attempting to plot objects with differing NFP, `phi` must
be given explicitly.

Parameters
----------
eqs : array-like of Equilibrium or EquilibriaFamily
Expand Down Expand Up @@ -2266,7 +2290,21 @@
)

"""
# if NFPs are not all equal, means there are
# objects with differing NFPs, which it is not clear
# how to choose the phis for by default, so we will throw an error
# unless phi was given.
phi = parse_argname_change(phi, kwargs, "zeta", "phi")
errorif(
not np.allclose([thing.NFP for thing in eqs], eqs[0].NFP) and phi is None,
ValueError,
"supplied objects must have the same number of field periods, "
"or if there are differing field periods, `phi` must be given explicitly."
f" Instead, supplied objects have NFPs {[t.NFP for t in eqs]}."
" If attempting to plot an axisymmetric object with non-axisymmetric objects,"
" you must use the `change_resolution` method to make the axisymmetric "
"object have the same NFP as the non-axisymmetric objects.",
)
color = parse_argname_change(color, kwargs, "colors", "color")
ls = parse_argname_change(ls, kwargs, "linestyles", "ls")
lw = parse_argname_change(lw, kwargs, "lws", "lw")
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 24 additions & 1 deletion tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,14 @@ def test_plot_boundaries(self):
eq1 = get("SOLOVEV")
eq2 = get("DSHAPE")
eq3 = get("W7-X")
fig, ax, data = plot_boundaries((eq1, eq2, eq3), return_data=True)
eq4 = get("ESTELL")
with pytest.raises(ValueError, match="differing field periods"):
fig, ax = plot_boundaries([eq3, eq4], theta=0)
fig, ax, data = plot_boundaries(
(eq1, eq2, eq3),
phi=np.linspace(0, 2 * np.pi / eq3.NFP, 4, endpoint=False),
return_data=True,
)
assert "R" in data.keys()
assert "Z" in data.keys()
assert len(data["R"]) == 3
Expand Down Expand Up @@ -550,6 +557,22 @@ def test_plot_comparison_no_theta(self):
fig, ax = plot_comparison(eqf, theta=0)
return fig

@pytest.mark.unit
@pytest.mark.mpl_image_compare(remove_text=True, tolerance=tol_2d)
def test_plot_comparison_different_NFPs(self):
"""Test plotting comparison of flux surfaces with differing NFPs."""
eq = get("SOLOVEV")
eq_nonax = get("HELIOTRON")
eq_nonax2 = get("ESTELL")
with pytest.raises(ValueError, match="differing field periods"):
fig, ax = plot_comparison([eq_nonax, eq_nonax2], theta=0)
fig, ax = plot_comparison(
[eq, eq_nonax],
phi=np.linspace(0, 2 * np.pi / eq_nonax.NFP, 6, endpoint=False),
theta=0,
)
return fig


class TestPlotGrid:
"""Tests for the plot_grid function."""
Expand Down
Loading