From 800060fa4577daf32b39f8c97b59b2f8463df12f Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 Jul 2024 14:19:17 +0200 Subject: [PATCH] test normal field setter too --- src/simsopt/mhd/spec.py | 6 +++--- tests/mhd/test_spec.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/simsopt/mhd/spec.py b/src/simsopt/mhd/spec.py index b42cabeb1..8a9655efc 100644 --- a/src/simsopt/mhd/spec.py +++ b/src/simsopt/mhd/spec.py @@ -399,12 +399,12 @@ def normal_field(self, normal_field): """ if not self.freebound: raise ValueError('Normal field can only be set in freeboundary case') - if not isinstance(normal_field, NormalField) or not isinstance(normal_field): + if not isinstance(normal_field, NormalField): raise ValueError('Input should be a NormalField or CoilNormalField') if self._normal_field is not normal_field: self.remove_parent(self._normal_field) - if self._computational_boundary is not normal_field.computational_boundary: - normal_field.computational_boundary = self._computational_boundary + if self._computational_boundary is not normal_field.surface: + normal_field.surface = self._computational_boundary self._normal_field = normal_field self.append_parent(normal_field) return diff --git a/tests/mhd/test_spec.py b/tests/mhd/test_spec.py index cea494812..179a7e2af 100755 --- a/tests/mhd/test_spec.py +++ b/tests/mhd/test_spec.py @@ -25,6 +25,7 @@ from simsopt.geo import SurfaceGarabedian from simsopt.mhd import ProfileSpec +from simsopt.field import NormalField from simsopt.objectives import LeastSquaresProblem from simsopt.solve import least_squares_serial_solve @@ -94,6 +95,27 @@ def test_init_freeboundary_nonstellsym(self): self.assertAlmostEqual(s.normal_field.get_vns(3, -1), -1.269776831212886e-04, places) self.assertAlmostEqual(s.normal_field.get_vnc(1, 0), 1.924871538367248e-04, places) self.assertAlmostEqual(s.normal_field.get_vnc(1, -2), 4.070523669489626e-04, places) + + def test_normal_field_setter(self): + """ + Try creating a Spec instance from a freeboundary file that is also + non-stellarator symmetric. + Check value of normal field + """ + + filename = os.path.join(TEST_DIR, 'M16N08.sp') + + with ScratchDir("."): + s = Spec(filename) + surface = s.boundary + old_normal = s.normal_field + new_normal = NormalField(s.nfp, stellsym=s.stellsym, mpol=s.mpol, ntor=s.ntor, surface=surface) + s.normal_field = new_normal + self.assertAlmostEqual(s.normal_field.get_vns(0,1), 0) # set to zeros + self.assertIs(s.normal_field.surface, s._computational_boundary) # normal field surface is set to spec computational boundary. + self.assertIsNot(old_normal, new_normal) + + def test_init_freeboundary(self): """