Skip to content

Commit

Permalink
remove all trace of coils file from MagnetSetFromGeometry
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgar-21 committed Jan 21, 2025
1 parent e3d90e2 commit 70e10da
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 106 deletions.
162 changes: 74 additions & 88 deletions parastell/magnet_coils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,25 @@ class MagnetSet(object):
"""An object representing a set of modular stellarator magnet coils.
Arguments:
coils_file (str): path to coil filament data file.
logger (object): logger object (optional, defaults to None). If no
logger is supplied, a default logger will be instantiated.
Optional attributes:
start_line (int): starting line index for data in filament data file
(defaults to 3).
sample_mod (int): sampling modifier for filament points (defaults to
1). For a user-defined value n, every nth point will be sampled.
scale (float): a scaling factor between the units of the point-locus
data and [cm] (defaults to m2cm = 100).
mat_tag (str): DAGMC material tag to use for magnets in DAGMC
neutronics model (defaults to 'magnets').
"""

def __init__(
self,
coils_file,
logger=None,
**kwargs,
):

self.logger = logger
self.coils_file = coils_file

self.start_line = 3
self.sample_mod = 1
self.scale = m2cm
self.mat_tag = "magnets"

for name in kwargs.keys() & (
"start_line",
"sample_mod",
"scale",
"mat_tag",
):
for name in kwargs.keys() & ("mat_tag",):
self.__setattr__(name, kwargs[name])

@property
Expand All @@ -63,60 +46,6 @@ def logger(self):
def logger(self, logger_object):
self._logger = log.check_init(logger_object)

def _instantiate_filaments(self):
"""Extracts filament coordinate data from input data file and
instantiates Filament class objects.
(Internal function not intended to be called externally)
"""
with open(self.coils_file, "r") as file:
data = file.readlines()[self.start_line :]

coords = []
self.filaments = []

for line in data:
columns = line.strip().split()

if columns[0] == "end":
break

# Coil current
s = float(columns[3])

# s = 0 signals end of filament
if s != 0:
coords.append(
[float(ord) * self.scale for ord in columns[0:3]]
)

else:
coords.append(coords[0])
self.filaments.append(Filament(np.array(coords)))
coords.clear()

def _filter_filaments(self, tol=0):
"""Filters list of Filament objects such that only those within the
toroidal extent of the model are included and filaments are sorted by
center-of-mass toroidal angle.
(Internal function not intended to be called externally)
"""

# Compute lower and upper bounds of toroidal extent within tolerance
lower_bound = 2 * np.pi - tol
upper_bound = self._toroidal_extent + tol

# Create filter determining whether each coil lies within model's
# toroidal extent
filtered_filaments = [
filament
for filament in self.filaments
if filament.in_toroidal_extent(lower_bound, upper_bound)
]
self.filaments = filtered_filaments

# Sort coils by center-of-mass toroidal angle and overwrite stored list
self.filaments = self.sort_filaments_toroidally()

def import_geom_cubit(self):
"""Import geometry file for magnet set into Coreform Cubit."""
first_vol_id = 1
Expand Down Expand Up @@ -209,7 +138,13 @@ def __init__(
logger=None,
**kwargs,
):
super().__init__(coils_file, logger, **kwargs)
super().__init__(logger, **kwargs)

self.coils_file = coils_file

self.start_line = 3
self.sample_mod = 1
self.scale = m2cm

self.width = width
self.thickness = thickness
Expand All @@ -218,6 +153,14 @@ def __init__(
# Define maximum length of coil cross-section
self.max_cs_len = max(self._width, self._thickness)

for name in kwargs.keys() & (
"start_line",
"sample_mod",
"scale",
"mat_tag",
):
self.__setattr__(name, kwargs[name])

@property
def width(self):
return self._width
Expand Down Expand Up @@ -254,6 +197,60 @@ def toroidal_extent(self, angle):
self._logger.error(e.args[0])
raise e

def _instantiate_filaments(self):
"""Extracts filament coordinate data from input data file and
instantiates Filament class objects.
(Internal function not intended to be called externally)
"""
with open(self.coils_file, "r") as file:
data = file.readlines()[self.start_line :]

coords = []
self.filaments = []

for line in data:
columns = line.strip().split()

if columns[0] == "end":
break

# Coil current
s = float(columns[3])

# s = 0 signals end of filament
if s != 0:
coords.append(
[float(ord) * self.scale for ord in columns[0:3]]
)

else:
coords.append(coords[0])
self.filaments.append(Filament(np.array(coords)))
coords.clear()

def _filter_filaments(self, tol=0):
"""Filters list of Filament objects such that only those within the
toroidal extent of the model are included and filaments are sorted by
center-of-mass toroidal angle.
(Internal function not intended to be called externally)
"""

# Compute lower and upper bounds of toroidal extent within tolerance
lower_bound = 2 * np.pi - tol
upper_bound = self._toroidal_extent + tol

# Create filter determining whether each coil lies within model's
# toroidal extent
filtered_filaments = [
filament
for filament in self.filaments
if filament.in_toroidal_extent(lower_bound, upper_bound)
]
self.filaments = filtered_filaments

# Sort coils by center-of-mass toroidal angle and overwrite stored list
self.filaments = self.sort_filaments_toroidally()

def _instantiate_coils(self):
"""Instantiates MagnetCoil class objects using filament data.
(Internal function not intended to be called externally)
Expand Down Expand Up @@ -358,36 +355,25 @@ class MagnetSetFromGeometry(MagnetSet):
with previously defined geometry files.
Arguments:
coils_file (str): path to coil filament data file.
geometry_file (str): filename of the existing coil geometry. Can be of
geometry_file (str): path to the existing coil geometry. Can be of
the types supported by cubit_io.import_geom_to_cubit()
working_dir (str): path to directory in which existing geometry
is saved.
logger (object): logger object (optional, defaults to None). If no
logger is supplied, a default logger will be instantiated.
Optional attributes:
start_line (int): starting line index for data in filament data file
(defaults to 3).
sample_mod (int): sampling modifier for filament points (defaults to
1). For a user-defined value n, every nth point will be sampled.
scale (float): a scaling factor between the units of the point-locus
data and [cm] (defaults to m2cm = 100).
mat_tag (str): DAGMC material tag to use for magnets in DAGMC
neutronics model (defaults to 'magnets').
"""

def __init__(
self,
coils_file,
geometry_file,
working_dir=".",
logger=None,
**kwargs,
):
super().__init__(coils_file, logger, **kwargs)
self.geometry_file = geometry_file
self.working_dir = Path(working_dir)
super().__init__(logger, **kwargs)
self.geometry_file = Path(geometry_file).resolve()
self.working_dir = self.geometry_file.parent

for name in kwargs.keys() & (
"start_line",
Expand Down
17 changes: 2 additions & 15 deletions parastell/parastell.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,33 +247,20 @@ def construct_magnets_from_filaments(
self.magnet_set.populate_magnet_coils()
self.magnet_set.build_magnet_coils()

def add_magnets_from_geometry(
self, coils_file, geometry_file, working_dir=".", **kwargs
):
def add_magnets_from_geometry(self, geometry_file, **kwargs):
"""Adds custom geometry via the MagnetSetFromGeometry class
Arguments:
coils_file (str): path to coil filament data file.
geometry_file (str): filename of the existing coil geometry. Can be of
geometry_file (str): path to the existing coil geometry. Can be of
the types supported by cubit_io.import_geom_to_cubit()
working_dir (str): path to directory in which existing geometry
is saved.
logger (object): logger object (optional, defaults to None). If no
logger is supplied, a default logger will be instantiated.
Optional attributes:
start_line (int): starting line index for data in filament data file
(defaults to 3).
sample_mod (int): sampling modifier for filament points (defaults to
1). For a user-defined value n, every nth point will be sampled.
scale (float): a scaling factor between the units of the point-locus
data and [cm] (defaults to m2cm = 100).
mat_tag (str): DAGMC material tag to use for magnets in DAGMC
neutronics model (defaults to 'magnets').
"""
self.magnet_set = mc.MagnetSetFromGeometry(
coils_file,
geometry_file,
working_dir,
logger=self._logger,
**kwargs,
)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_magnet_coils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ def coil_set_from_filaments():

@pytest.fixture
def coil_set_from_geometry():
coils_file = Path("files_for_tests") / "coils.example"
geom_file = Path("files_for_tests") / "magnet_geom.step"

coil_set_obj = magnet_coils.MagnetSetFromGeometry(coils_file, geom_file)
coil_set_obj = magnet_coils.MagnetSetFromGeometry(geom_file)

return coil_set_obj

Expand Down
2 changes: 1 addition & 1 deletion tests/test_parastell.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_parastell(stellarator):

stellarator.export_invessel_build()

stellarator.add_magnets_from_geometry(coils_file, geometry_file)
stellarator.add_magnets_from_geometry(geometry_file)

stellarator.build_cubit_model()

Expand Down

0 comments on commit 70e10da

Please sign in to comment.