Skip to content

Commit

Permalink
Document use of _set_up function and fix test to raise ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
unalmis committed Jul 19, 2023
1 parent 3ff4ebf commit 15f816a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
36 changes: 19 additions & 17 deletions desc/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def __init__(
self._method = method
# assign according to logic in setter function
self.method = method
# assign according to logic in property function
self._matrices = None
self._matrices = self.matrices
self._matrices = self._get_matrices()
if build:
self.build()
if build_pinv:
Expand Down Expand Up @@ -130,6 +128,18 @@ def _sort_derivatives(self):
)
self._derivatives = self.derivatives[sort_idx]

def _get_matrices(self):
"""Get matrices to compute all derivatives."""
n = np.amax(self.derivatives) + 1
matrices = {
"direct1": {
i: {j: {k: {} for k in range(n)} for j in range(n)} for i in range(n)
},
"fft": {i: {j: {} for j in range(n)} for i in range(n)},
"direct2": {i: {} for i in range(n)},
}
return matrices

def _check_inputs_fft(self, grid, basis):
"""Check that inputs are formatted correctly for fft method."""
if grid.num_nodes == 0 or basis.num_modes == 0:
Expand Down Expand Up @@ -716,32 +726,24 @@ def change_derivatives(self, derivs, build=True):
self._sort_derivatives()

if len(derivs_to_add):
# if we actually added derivatives and didn't build them, then its not built
# if we actually added derivatives and didn't build them, then it's not built
self._built = False
if build:
# we don't update self._built here because it is still built from before,
# but it still might have unbuilt matrices from new derivatives
self.build()

def _set_up(self):
# fixme? dummy method needed for _matrices attribute to persist?
self.method = self.method
"""Recreate attributes that were not saved to output files."""
# fixme: Make api for reloading attributes that are not saved better.
# See equilibrium_io.py for the logic that calls this function.
self._matrices = self.matrices

@property
def matrices(self):
"""dict: transform matrices such that x=A*c."""
if getattr(self, "_matrices", None) is None:
# to allow computing of highest order derivative
n = np.amax(self.derivatives) + 1
self._matrices = {
"direct1": {
i: {j: {k: {} for k in range(n)} for j in range(n)}
for i in range(n)
},
"fft": {i: {j: {} for j in range(n)} for i in range(n)},
"direct2": {i: {} for i in range(n)},
}
if not hasattr(self, "_matrices"):
self._matrices = self._get_matrices()
return self._matrices

@property
Expand Down
2 changes: 1 addition & 1 deletion tests/test_curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def test_misc(self):
def test_asserts(self):
"""Test error checking when creating FourierXYZCurve."""
c = FourierXYZCurve()
with pytest.raises(KeyError):
with pytest.raises(ValueError):
c.compute_coordinates(dt=4)
with pytest.raises(TypeError):
c.grid = [1, 2, 3]
Expand Down

0 comments on commit 15f816a

Please sign in to comment.