Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
smiet committed May 23, 2024
1 parent e076915 commit 4983382
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 32 deletions.
25 changes: 13 additions & 12 deletions src/simsopt/field/normal_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, nfp=1, stellsym=True, mpol=1, ntor=0,
vnc = np.zeros((self.mpol + 1, 2 * self.ntor + 1))

if computational_boundary is None:
from simsopt.geo import SurfaceRZFourier
computational_boundary = SurfaceRZFourier(nfp=nfp, stellsym=stellsym, mpol=mpol, ntor=ntor)
computational_boundary.fix_all()
self.computational_boundary = computational_boundary
Expand Down Expand Up @@ -363,12 +364,12 @@ def get_vns_asarray(self, mpol=None, ntor=None):
"""
Return the vns as a single array
"""
if mpol == None:
if mpol is None:
mpol = self.mpol
elif mpol > self.mpol:
raise ValueError('mpol out of bound')

if ntor == None:
if ntor is None:
ntor = self.ntor
elif ntor > self.ntor:
raise ValueError('ntor out of bound')
Expand All @@ -385,12 +386,12 @@ def get_vnc_asarray(self, mpol=None, ntor=None):
"""
Return the vnc as a single array
"""
if mpol == None:
if mpol is None:
mpol = self.mpol
elif mpol > self.mpol:
raise ValueError('mpol out of bound')

if ntor == None:
if ntor is None:
ntor = self.ntor
elif ntor > self.ntor:
raise ValueError('ntor out of bound')
Expand All @@ -407,12 +408,12 @@ def get_vns_vnc_asarray(self, mpol, ntor):
"""
Return the vns and vnc as two arrays single array
"""
if mpol == None:
if mpol is None:
mpol = self.mpol
elif mpol > self.mpol:
raise ValueError('mpol out of bound')

if ntor == None:
if ntor is None:
ntor = self.ntor
elif ntor > self.ntor:
raise ValueError('ntor out of bound')
Expand All @@ -425,12 +426,12 @@ def set_vns_asarray(self, vns, mpol=None, ntor=None):
"""
Set the vns from a single array
"""
if mpol == None:
if mpol is None:
mpol = self.mpol
elif mpol > self.mpol:
raise ValueError('mpol out of bound')

if ntor == None:
if ntor is None:
ntor = self.ntor
elif ntor > self.ntor:
raise ValueError('ntor out of bound')
Expand All @@ -444,12 +445,12 @@ def set_vnc_asarray(self, vnc, mpol=None, ntor=None):
"""
Set the vnc from a single array
"""
if mpol == None:
if mpol is None:
mpol = self.mpol
elif mpol > self.mpol:
raise ValueError('mpol out of bound')

if ntor == None:
if ntor is None:
ntor = self.ntor
elif ntor > self.ntor:
raise ValueError('ntor out of bound')
Expand All @@ -463,12 +464,12 @@ def set_vns_vnc_asarray(self, vns, vnc, mpol=None, ntor=None):
"""
Set the vns and vnc from two single arrays
"""
if mpol == None:
if mpol is None:
mpol = self.mpol
elif mpol > self.mpol:
raise ValueError('mpol out of bound')

if ntor == None:
if ntor is None:
ntor = self.ntor
elif ntor > self.ntor:
raise ValueError('ntor out of bound')
Expand Down
9 changes: 4 additions & 5 deletions src/simsopt/geo/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,9 +868,8 @@ def fourier_transform_field(self, field, mpol=None, ntor=None, normalization=Non
- A_mnc: 2D array of shape (mpol+1, 2*ntor+1) containing the cosine coefficients
(these are zero if the surface is stellarator symmetric)
"""
assert (field.shape == [self.quadpoints_phi.size, self.quadpoints_theta.size],
"Field must be evaluated at the quadrature points on the surface. \
the field you passed in has shape {}".format(field.shape))
assert field.shape[0]!= self.quadpoints_phi.size,"Field must be evaluated at the quadrature points on the surface.\n the field you passed in has shape {}".format(field.shape)
assert field.shape[1] != self.quadpoints_theta.size,"Field must be evaluated at the quadrature points on the surface.\n the field you passed in has shape {}".format(field.shape)
stellsym = kwargs.pop('stellsym', self.stellsym)
if mpol is None:
try: mpol = self.mpol
Expand Down Expand Up @@ -908,7 +907,7 @@ def fourier_transform_field(self, field, mpol=None, ntor=None, normalization=Non
if not stellsym:
A_mnc[0, ntor] = np.sum(field) / (ntheta_grid * nphi_grid)
if normalization is not None:
if type(normalization) is not float:
if isinstance(normalization, float):
raise ValueError("normalization must be a float")
A_mns = A_mns / normalization
A_mnc = A_mnc / normalization
Expand Down Expand Up @@ -960,7 +959,7 @@ def inverse_fourier_transform_field(self, A_mns, A_mnc, normalization=None, **kw
if not stellsym:
field = field + A_mnc[0, ntor]
if normalization is not None:
if type(normalization) is not float:
if isinstance(normalization, float):
raise ValueError("normalization must be a float")
field = field * normalization
return field
Expand Down
15 changes: 0 additions & 15 deletions src/simsopt/mhd/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,21 +1205,6 @@ def iota(self):
self.run()
return self.results.transform.fiota[1, 0]

def replace_normal_field_with_coils(self, filename=None, TARGET_LENGTH=3000):
"""
replace the daddy normal_field with a CoilNormalField instance that
inherits its' degrees of freedom from a CoilSet.
A filename can be given in which the coils are specified (standard
JSON format used by calling Optimizable.to_json() on a CoilSet instance)
[NOT IMPLEMENTED YET]
"""
if filename is not None:
raise NotImplementedError('filename not supported yet')

coil_normal_field = CoilNormalField.from_spec_object(self, optimize_coils=True, TARGET_LENGTH=TARGET_LENGTH)
self.normal_field = coil_normal_field

def array_translator(self, array=None, style='spec'):
"""
Returns a SpecFourierArray object to help transforming between
Expand Down
42 changes: 42 additions & 0 deletions tests/geo/test_surface_rzfourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,48 @@ def test_make_rotating_ellipse_iota(self):
np.testing.assert_array_less(0, eq.wout.iotaf)
np.testing.assert_allclose(eq.mean_iota(), 0.4291137962772453, rtol=1e-6)

def test_fourier_transform_field(self):
"""
Test the Fourier transform of a field on a surface.
"""
s = SurfaceRZFourier(mpol=4, ntor=5)
s.rc[0, 0] = 1.3
s.rc[1, 0] = 0.4
s.zs[1, 0] = 0.2

# Create the grid of quadpoints:
phi2d, theta2d = np.meshgrid(2 * np.pi * s.quadpoints_phi,
2 * np.pi * s.quadpoints_theta,
indexing='ij')

# create a test field where only Fourier elements [m=2, n=3]
# and [m=4,n=5] are nonzero:
field = []
for phi, theta in zip(phi2d.flatten(), theta2d.flatten()):
field.append(0.8 * np.sin(2*theta - 3*s.nfp*phi) + 0.2*np.sin(4*theta - 5*s.nfp*phi)
+ 0.7*np.cos(3*theta - 3*s.nfp*phi))
field = np.array(field).reshape(phi2d.shape)

# Transform the field to Fourier space:
ft_sines, ft_cosines = s.fourier_transform_field(field, stellsym=False)
self.assertAlmostEqual(ft_sines[2, 3+s.ntor], 0.8)
self.assertAlmostEqual(ft_sines[4, 5+s.ntor], 0.2)
self.assertAlmostEqual(ft_cosines[3, 3+s.ntor], 0.7)

# Test that all other elements are close to zero
sines_mask = np.ones_like(ft_sines, dtype=bool)
cosines_mask = sines_mask
sines_mask[2, 3 + s.ntor] = False
sines_mask[4, 5 + s.ntor] = False
cosines_mask[3, 3 + s.ntor] = False
self.assertEqual(np.all(np.abs(ft_sines[sines_mask]) < 1e-10), True)
self.assertEqual(np.all(np.abs(ft_cosines[cosines_mask]) < 1e-10), True)

# Transform back to real space:
field2 = s.inverse_fourier_transform_field(ft_sines, ft_cosines, stellsym=False)

# Check that the result is the same as the original field:
np.testing.assert_allclose(field, field2)

class SurfaceRZPseudospectralTests(unittest.TestCase):
def test_names(self):
Expand Down

0 comments on commit 4983382

Please sign in to comment.