Skip to content

Commit

Permalink
move Dummy coil sets to conftest
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-dudt committed May 13, 2024
1 parent f06f8ae commit 94500c4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 23 deletions.
71 changes: 71 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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."""
Expand Down
23 changes: 0 additions & 23 deletions tests/test_coils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 94500c4

Please sign in to comment.