Skip to content

Commit 621a161

Browse files
authored
Merge pull request #156 from mpigou/fix-mappings
Fix xI_to_Y and Y_to_xI mappings
2 parents 3256b68 + c84603a commit 621a161

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/few/utils/mappings/pn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ def _PN_L(q, p, e, Y):
175175
rhs_first_term = -_g2(q, p, e, Y) * E / h2
176176
rhs_sqrt = rhs_first_term**2 + (_f2(q, p, e, Y) * E * E - _d2(q, p, e, Y)) / h2
177177

178-
return rhs_first_term + sqrt(rhs_sqrt)
178+
sgnY = 1 if Y > 0 else -1
179+
return rhs_first_term + sgnY * sqrt(rhs_sqrt)
179180

180181

181182
@njit(fastmath=False)

tests/test_utils/test_mappings.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
3+
from few.tests.base import FewTest
4+
from few.utils.mappings.pn import Y_to_xI, xI_to_Y
5+
6+
7+
class PnMappingsTest(FewTest):
8+
@classmethod
9+
def name(self) -> str:
10+
return "PN Mappings"
11+
12+
def test_back_and_forth_scalar_xI(self):
13+
# When a, p, and e are scalar values
14+
a = 0.5
15+
p = 10.0
16+
e = 0.0
17+
# Given a xI value in the range [-1, 1]
18+
xI_inputs = np.linspace(-1.0, 1.0, 10, endpoint=True)
19+
self.logger.info(f"{xI_inputs=}")
20+
# Let's map xI values to corresponding Y
21+
Y_inputs = np.array([xI_to_Y(a, p, e, xI) for xI in xI_inputs])
22+
self.logger.info(f"{Y_inputs=}")
23+
24+
# Then map result Y back to xI
25+
xI_outputs = np.array([Y_to_xI(a, p, e, Y) for Y in Y_inputs])
26+
self.logger.info(f"{xI_outputs=}")
27+
# And check that resulting values are close to input ones
28+
np.testing.assert_allclose(xI_outputs, xI_inputs, rtol=1e-4)
29+
30+
# And map once again to Y
31+
Y_outputs = np.array([xI_to_Y(a, p, e, xI) for xI in xI_outputs])
32+
# And check values match
33+
np.testing.assert_allclose(Y_outputs, Y_inputs, rtol=1e-4)

0 commit comments

Comments
 (0)