Skip to content

Commit

Permalink
Merge branch 'master' into dp/poincare
Browse files Browse the repository at this point in the history
  • Loading branch information
YigitElma authored Sep 27, 2024
2 parents f64ecd7 + b64da96 commit 702faa7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
20 changes: 11 additions & 9 deletions desc/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,25 +1648,27 @@ def most_rational(a, b, itol=1e-14):
"""
a = float(_round(a, itol))
b = float(_round(b, itol))
# handle empty range

# Handle empty range
if a == b:
return a
# ensure a < b
elif a > b:
c = a
a = b
b = c
# return 0 if in range

# Return 0 if in range
if np.sign(a * b) <= 0:
return 0
# handle negative ranges
elif np.sign(a) < 0:

# Handle negative ranges
if np.sign(a) < 0:
s = -1
a *= -1
b *= -1
else:
s = 1

# Ensure a < b
if a > b:
a, b = b, a

a_cf = dec_to_cf(a)
b_cf = dec_to_cf(b)
idx = 0 # first index of dissimilar digits
Expand Down
28 changes: 28 additions & 0 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
dec_to_cf,
find_least_rational_surfaces,
find_most_rational_surfaces,
most_rational,
)
from desc.profiles import PowerSeriesProfile

Expand Down Expand Up @@ -823,6 +824,33 @@ def test_find_most_rational_surfaces():
np.testing.assert_allclose(rho, np.linspace(0, 1, 5), atol=1e-14, rtol=0)
np.testing.assert_allclose(io, np.linspace(1, 3, 5), atol=1e-14, rtol=0)

# simple test, linear iota going from -1 to -3
iota = PowerSeriesProfile([-1, -2])
rho, io = find_most_rational_surfaces(iota, 5)
np.testing.assert_allclose(rho, np.linspace(0, 1, 5), atol=1e-14, rtol=0)
np.testing.assert_allclose(io, np.linspace(-1, -3, 5), atol=1e-14, rtol=0)

# invalid (a > b) ranges and negative ranges are swapped and made positive
all_same = [
most_rational(1, 2),
most_rational(2, 1),
most_rational(-1, -2),
most_rational(-2, -1),
]

assert len(set(map(abs, all_same))) == 1

# if 0 in range, return 0
has_zero = [
most_rational(0, 1),
most_rational(0, -1),
most_rational(0, 0),
most_rational(-1, 0),
most_rational(1, 0),
]

assert all(result == 0 for result in has_zero)


@pytest.mark.unit
def test_find_least_rational_surfaces():
Expand Down

0 comments on commit 702faa7

Please sign in to comment.