Skip to content

Commit

Permalink
be less careful with kwargs and streamline
Browse files Browse the repository at this point in the history
  • Loading branch information
gonuke committed Apr 27, 2024
1 parent ad749ab commit f0c32f5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 217 deletions.
47 changes: 11 additions & 36 deletions parastell/invessel_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

from . import log
from .utils import (
normalize, expand_ang_list, read_yaml_config, construct_kwargs_from_dict,
set_kwarg_attrs, m2cm
normalize, expand_ang_list, read_yaml_config, filter_kwargs, m2cm
)


def orient_spline_surfaces(volume_id):
"""Extracts the inner and outer surface IDs for a given ParaStell in-vessel
component volume in Coreform Cubit.
Expand Down Expand Up @@ -101,11 +99,9 @@ def __init__(
self.scale = m2cm

allowed_kwargs = ['repeat', 'num_ribs', 'num_rib_pts', 'scale']
set_kwarg_attrs(
self,
kwargs,
allowed_kwargs
)
for name in allowed_kwargs:
if name in kwargs.keys():
self.__setattr__(name,kwargs[name])

self.Surfaces = {}
self.Components = {}
Expand Down Expand Up @@ -575,11 +571,9 @@ def __init__(
self.sol_mat_tag = 'Vacuum'

allowed_kwargs = ['plasma_mat_tag', 'sol_mat_tag']
set_kwarg_attrs(
self,
kwargs,
allowed_kwargs
)
for name in allowed_kwargs:
if name in kwargs.keys():
self.__setattr__(name,kwargs[name])

self._logger.info(
'Constructing radial build...'
Expand Down Expand Up @@ -780,32 +774,20 @@ def generate_invessel_build():

invessel_build_dict = all_data['invessel_build']

rb_allowed_kwargs = ['plasma_mat_tag', 'sol_mat_tag']
rb_kwargs = construct_kwargs_from_dict(
invessel_build_dict,
rb_allowed_kwargs
)

radial_build = RadialBuild(
invessel_build_dict['toroidal_angles'],
invessel_build_dict['poloidal_angles'],
invessel_build_dict['wall_s'],
invessel_build_dict['radial_build'],
logger=logger
**rb_kwargs
)

ivb_allowed_kwargs = ['repeat', 'num_ribs', 'num_rib_pts', 'scale']
ivb_kwargs = construct_kwargs_from_dict(
invessel_build_dict,
ivb_allowed_kwargs
**invessel_build_dict
)

invessel_build = InVesselBuild(
vmec_obj,
radial_build,
logger=logger,
**ivb_kwargs
**invessel_build_dict
)

invessel_build.populate_surfaces()
Expand All @@ -815,17 +797,10 @@ def generate_invessel_build():
invessel_build.export_step(export_dir=args.export_dir)

if invessel_build_dict['export_cad_to_dagmc']:
ivb_dagmc_export_allowed_kwargs = [
'export_cad_to_dagmc', 'dagmc_filename'
]
ivb_dagmc_export_kwargs = construct_kwargs_from_dict(
invessel_build_dict,
ivb_dagmc_export_allowed_kwargs
)


invessel_build.export_cad_to_dagmc(
export_dir=args.export_dir,
**ivb_dagmc_export_kwargs
**(filter_kwargs(invessel_build_dict,['dagmc_filename']))
)


Expand Down
39 changes: 8 additions & 31 deletions parastell/magnet_coils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from . import log
from . import cubit_io as cubit_io
from .utils import (
normalize, read_yaml_config, construct_kwargs_from_dict, set_kwarg_attrs,
m2cm
normalize, read_yaml_config, filter_kwargs, m2cm
)

export_allowed_kwargs = ['step_filename', 'export_mesh', 'mesh_filename']

class MagnetSet(object):
"""An object representing a set of modular stellarator magnet coils.
Expand Down Expand Up @@ -58,11 +57,9 @@ def __init__(
self.mat_tag = 'magnets'

allowed_kwargs = ['start_line', 'sample_mod', 'scale', 'mat_tag']
set_kwarg_attrs(
self,
kwargs,
allowed_kwargs
)
for name in allowed_kwargs:
if name in kwargs.keys():
self.__setattr__(name,kwargs[name])

cubit_io.init_cubit()

Expand Down Expand Up @@ -626,45 +623,25 @@ def generate_magnet_set():

magnet_coils_dict = all_data['magnet_coils']

mc_allowed_kwargs = [
'start_line', 'sample_mod', 'scale', 'mat_tag'
]
mc_kwargs = construct_kwargs_from_dict(
magnet_coils_dict,
mc_allowed_kwargs
)

magnet_set = MagnetSet(
magnet_coils_dict['coils_file'],
magnet_coils_dict['cross_section'],
magnet_coils_dict['toroidal_extent'],
logger=logger
**mc_kwargs
**magnet_coils_dict
)

magnet_set.build_magnet_coils()

mc_step_export_allowed_kwargs = ['step_filename']
mc_step_export_kwargs = construct_kwargs_from_dict(
magnet_coils_dict,
mc_step_export_allowed_kwargs
)

magnet_set.export_step(
export_dir=args.export_dir,
**mc_step_export_kwargs
**(filter_kwargs(magnet_coils_dict,['step_filename']))
)

if magnet_coils_dict['export_mesh']:
mc_mesh_export_allowed_kwargs = ['mesh_filename']
mc_mesh_export_kwargs = construct_kwargs_from_dict(
magnet_coils_dict,
mc_mesh_export_allowed_kwargs
)

magnet_set.export_mesh(
export_dir=args.export_dir,
**mc_mesh_export_kwargs
**(filter_kwargs(magnet_coils_dict, ['mesh_filename']))
)


Expand Down
119 changes: 15 additions & 104 deletions parastell/parastell.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from . import magnet_coils as mc
from . import source_mesh as sm
from . import cubit_io
from .utils import read_yaml_config, construct_kwargs_from_dict, m2cm
from .utils import read_yaml_config, filter_kwargs, m2cm


def make_material_block(mat_tag, block_id, vol_id_str):
Expand Down Expand Up @@ -140,47 +140,20 @@ def construct_invessel_build(
scale (float): a scaling factor between the units of VMEC and [cm]
(defaults to m2cm = 100).
"""
# This block checks the user-supplied keyword arguments; not all
# keyword arguments present in 'kwargs' can be passed to each class
# object below
allowed_kwargs = [
'plasma_mat_tag', 'sol_mat_tag', 'repeat', 'num_ribs',
'num_rib_pts', 'scale'
]
kwargs = construct_kwargs_from_dict(
kwargs,
allowed_kwargs,
all_kwargs=True,
fn_name='construct_invessel_build',
logger=self._logger
)

rb_allowed_kwargs = ['plasma_mat_tag', 'sol_mat_tag']
rb_kwargs = construct_kwargs_from_dict(
kwargs,
rb_allowed_kwargs
)

self.radial_build = ivb.RadialBuild(
toroidal_angles,
poloidal_angles,
wall_s,
radial_build,
logger=self._logger,
**rb_kwargs
)

ivb_allowed_kwargs = ['repeat', 'num_ribs', 'num_rib_pts', 'scale']
ivb_kwargs = construct_kwargs_from_dict(
kwargs,
ivb_allowed_kwargs
**kwargs
)

self.invessel_build = ivb.InVesselBuild(
self._vmec_obj,
self.radial_build,
logger=self._logger,
**ivb_kwargs
**kwargs
)

self.invessel_build.populate_surfaces()
Expand Down Expand Up @@ -235,21 +208,12 @@ def construct_magnets(
mat_tag (str): DAGMC material tag to use for magnets in DAGMC
neutronics model (defaults to 'magnets').
"""
allowed_kwargs = ['start_line', 'sample_mod', 'scale', 'mat_tag']
mc_kwargs = construct_kwargs_from_dict(
kwargs,
allowed_kwargs,
all_kwargs=True,
fn_name='construct_magnets',
logger=self._logger
)

self.magnet_set = mc.MagnetSet(
coils_file,
cross_section,
toroidal_extent,
logger=self._logger,
**mc_kwargs
**kwargs
)

self.magnet_set.build_magnet_coils()
Expand Down Expand Up @@ -302,21 +266,12 @@ def construct_source_mesh(
scale (float): a scaling factor between the units of VMEC and [cm]
(defaults to m2cm = 100).
"""
allowed_kwargs = ['scale']
sm_kwargs = construct_kwargs_from_dict(
kwargs,
allowed_kwargs,
all_kwargs=True,
fn_name='construct_source_mesh',
logger=self._logger
)

self.source_mesh = sm.SourceMesh(
self._vmec_obj,
mesh_size,
toroidal_extent,
logger=self._logger,
**sm_kwargs
**kwargs
)

self.source_mesh.create_vertices()
Expand Down Expand Up @@ -463,93 +418,49 @@ def build_full_model(self, invessel_build, magnet_coils, source_mesh):
"""

# In-Vessel Build

ivb_construct_allowed_kwargs = [
'plasma_mat_tag', 'sol_mat_tag', 'repeat', 'num_ribs', 'num_rib_pts',
'scale'
]
ivb_construct_kwargs = construct_kwargs_from_dict(
invessel_build,
ivb_construct_allowed_kwargs
)

self.construct_invessel_build(
invessel_build['toroidal_angles'],
invessel_build['poloidal_angles'],
invessel_build['wall_s'],
invessel_build['radial_build'],
**ivb_construct_kwargs
**invessel_build
)

# Magnet Coils

mc_construct_allowed_kwargs = [
'start_line', 'sample_mod', 'scale', 'mat_tag'
]
mc_construct_kwargs = construct_kwargs_from_dict(
magnet_coils,
mc_construct_allowed_kwargs
)

self.construct_magnets(
magnet_coils['coils_file'],
magnet_coils['cross_section'],
magnet_coils['toroidal_extent'],
**mc_construct_kwargs
**magnet_coils
)

# Source Mesh

sm_construct_allowed_kwargs = ['scale']
sm_construct_kwargs = construct_kwargs_from_dict(
source_mesh,
sm_construct_allowed_kwargs
)

self.construct_source_mesh(
source_mesh['mesh_size'],
source_mesh['toroidal_extent'],
**sm_construct_kwargs
**source_mesh
)

def export_full_model(self, export_dir, invessel_build,
magnet_coils, source_mesh, dagmc_export):

ivb_export_allowed_kwargs = [
'export_cad_to_dagmc', 'dagmc_filename'
]
ivb_export_kwargs = construct_kwargs_from_dict(
invessel_build,
ivb_export_allowed_kwargs
)
export_cad_to_dagmc = invessel_build.get('export_cad_to_dagmc', False)
dagmc_filename = invessel_build.get('dagmc_filename', 'dagmc')

self.export_invessel_build(
export_dir=export_dir,
**ivb_export_kwargs
export_cad_to_dagmc=export_cad_to_dagmc,
dagmc_filename=dagmc_filename
)

mc_export_allowed_kwargs = [
'step_filename', 'export_mesh', 'mesh_filename'
]
mc_export_kwargs = construct_kwargs_from_dict(
magnet_coils,
mc_export_allowed_kwargs
)


self.export_magnets(
export_dir=export_dir,
**mc_export_kwargs
)

sm_export_allowed_kwargs = ['filename']
sm_export_kwargs = construct_kwargs_from_dict(
source_mesh,
sm_export_allowed_kwargs
**(filter_kwargs(magnet_coils,mc.export_allowed_kwargs))
)

self.export_source_mesh(
export_dir=export_dir,
**sm_export_kwargs
**(filter_kwargs(source_mesh, sm.export_allowed_kwargs))
)

# DAGMC export
Expand Down
Loading

0 comments on commit f0c32f5

Please sign in to comment.