PlasmaControl / DESC

Stellarator Equilibrium and Optimization Suite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Attribute types are not always correct

ddudt opened this issue · comments

Some class attributes are not always the intended data type. For example, Equilibrium.NFP is supposed to be an int but in the past it was sometimes a float. More recently, I discovered that Equilibrium.sym was of type numpy.bool_ instead of the python bool, which is treated differently by the pytree utility functions we use in the coil objectives. I recommend we cast to the intended data type in class getter methods to ensure the returned type is always correct.

MWE to reproduce this type of error:

from desc.coils import *
from desc.io import load
from desc.objectives import *

# this is the DummyMixedCoilSet
tf_coil = FourierPlanarCoil(current=3, center=[2, 0, 0], normal=[0, 1, 0], r_n=[1])
tf_coilset = CoilSet.linspaced_angular(tf_coil, n=4)
vf_coil = FourierRZCoil(current=-1, R_n=3, Z_n=-1)
vf_coilset = CoilSet.linspaced_linear(
    vf_coil, displacement=[0, 0, 2], n=3, endpoint=True
)
xyz_coil = FourierXYZCoil(current=2)
coilset_1 = MixedCoilSet((tf_coilset, vf_coilset, xyz_coil))
coilset_1.save("dummy_mixed_coilset.h5")
coilset_2 = load("dummy_mixed_coilset.h5")

objective = ObjectiveFunction(
    (CoilLength(coils=coilset_2), CoilCurvature(coil=coilset_2))
)
objective.build()  # raises an AttributeError

The objective build works for coilset_1 but not coilset_2, so the data type is somehow changing in the save/load process.