From 94500c4b9a64ef6022cdcb24bb00e60a81afd388 Mon Sep 17 00:00:00 2001 From: daniel-dudt Date: Mon, 13 May 2024 13:54:48 -0400 Subject: [PATCH] move Dummy coil sets to conftest --- tests/conftest.py | 71 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_coils.py | 23 --------------- 2 files changed, 71 insertions(+), 23 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index cf2b1d020c..a8a9e63818 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,7 +9,17 @@ from netCDF4 import Dataset from desc.__main__ import main +from desc.coils import ( + CoilSet, + FourierPlanarCoil, + FourierRZCoil, + FourierXYZCoil, + MixedCoilSet, +) +from desc.compute import rpz2xyz_vec from desc.equilibrium import EquilibriaFamily, Equilibrium +from desc.examples import get +from desc.grid import LinearGrid from desc.vmec import VMECIO @@ -217,6 +227,67 @@ def DummyStellarator(tmpdir_factory): return DummyStellarator_out +@pytest.fixture(scope="session") +def DummyCoilSet(tmpdir_factory): + """Create and save a dummy coil set for testing.""" + output_dir = tmpdir_factory.mktemp("result") + output_path_sym = output_dir.join("DummyCoilSet_sym.h5") + output_path_asym = output_dir.join("DummyCoilSet_asym.h5") + + eq = get("precise_QH") + minor_radius = eq.compute("a")["a"] + + # CoilSet with symmetry + num_coils = 3 # number of unique coils per half field period + grid = LinearGrid(rho=[0.0], M=0, zeta=2 * num_coils, NFP=eq.NFP * (eq.sym + 1)) + with pytest.warns(UserWarning): # because eq.NFP != grid.NFP + data_center = eq.axis.compute("x", grid=grid, basis="xyz") + data_normal = eq.compute("e^zeta", grid=grid) + centers = data_center["x"] + normals = rpz2xyz_vec(data_normal["e^zeta"], phi=grid.nodes[:, 2]) + coils = [] + for k in range(1, 2 * num_coils + 1, 2): + coil = FourierPlanarCoil( + current=1e6, + center=centers[k, :], + normal=normals[k, :], + r_n=[0, minor_radius + 0.5, 0], + ) + coils.append(coil) + coilset_sym = CoilSet(coils, NFP=eq.NFP, sym=eq.sym) + coilset_sym.save(output_path_sym) + + # equivalent CoilSet without symmetry + coilset_asym = CoilSet.from_symmetry(coilset_sym, NFP=eq.NFP, sym=eq.sym) + coilset_asym.save(output_path_asym) + + DummyCoilSet_out = { + "output_path_sym": output_path_sym, + "output_path_asym": output_path_asym, + } + return DummyCoilSet_out + + +@pytest.fixture(scope="session") +def DummyMixedCoilSet(tmpdir_factory): + """Create and save a dummy mixed coil set for testing.""" + output_dir = tmpdir_factory.mktemp("result") + output_path = output_dir.join("DummyMixedCoilSet.h5") + + tf_coil = FourierPlanarCoil(center=[2, 0, 0], normal=[0, 1, 0], r_n=[1]) + tf_coilset = CoilSet.linspaced_angular(tf_coil, n=4) + vf_coil = FourierRZCoil(R_n=3, Z_n=-1) + vf_coilset = CoilSet.linspaced_linear( + vf_coil, displacement=[0, 0, 2], n=3, endpoint=True + ) + xyz_coil = FourierXYZCoil() + full_coilset = MixedCoilSet((tf_coilset, vf_coilset, xyz_coil)) + + full_coilset.save(output_path) + DummyMixedCoilSet_out = {"output_path": output_path} + return DummyMixedCoilSet_out + + @pytest.fixture(scope="session") def writer_test_file(tmpdir_factory): """Create temporary output directory.""" diff --git a/tests/test_coils.py b/tests/test_coils.py index 1b57fdd730..5e1518bbf8 100644 --- a/tests/test_coils.py +++ b/tests/test_coils.py @@ -17,7 +17,6 @@ from desc.examples import get from desc.geometry import FourierRZCurve, FourierRZToroidalSurface from desc.grid import LinearGrid -from desc.io import load from desc.magnetic_fields import SumMagneticField, VerticalMagneticField @@ -916,28 +915,6 @@ def test_save_makegrid_coils_assert_NFP(tmpdir_factory): coilset.save_in_makegrid_format(str(path), NFP=3) -@pytest.mark.unit -def test_save_h5_for_mixed_coilset(tmpdir_factory): - """Test saving MixedCoilSet containing coilsets and a coil.""" - # tests fix in PR #1016 - output_dir = tmpdir_factory.mktemp("coilset_save") - output_path = output_dir.join("DummyMixedCoilSet.h5") - - tf_coil = FourierPlanarCoil(current=20, center=[2, 0, 0], normal=[0, 1, 0], r_n=[1]) - tf_coilset = CoilSet.linspaced_angular(tf_coil, n=4) - vf_coil = FourierRZCoil(current=100, R_n=3, Z_n=-1) - vf_coilset = CoilSet.linspaced_linear( - vf_coil, displacement=[0, 0, 2], n=3, endpoint=True - ) - xyz_coil = FourierXYZCoil() - full_coilset = MixedCoilSet((tf_coilset, vf_coilset, xyz_coil)) - - full_coilset.save(output_path) - coils = load(output_path) - - assert coils.equiv(full_coilset) - - @pytest.mark.unit def test_load_makegrid_coils_header_asserts(tmpdir_factory): """Test loading in CoilSets from incorrect MAKEGRID format files throws error."""