Skip to content

Commit

Permalink
Merge pull request #112 from svalinn/export_cub5
Browse files Browse the repository at this point in the history
add option to export to cub5
  • Loading branch information
connoramoreno authored Jun 4, 2024
2 parents 63998e6 + 1d45418 commit 1e26b75
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 45 deletions.
3 changes: 3 additions & 0 deletions Examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ invessel_build:
mat_tag: vac_vessel
plasma_mat_tag: Vacuum
sol_mat_tag: Vacuum
split_chamber: True
export_cad_to_dagmc: False

magnet_coils:
Expand All @@ -83,3 +84,5 @@ dagmc_export:
skip_imprint: False
legacy_faceting: True
filename: dagmc

cub5_export: False
7 changes: 5 additions & 2 deletions Examples/parastell_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@
export_dir=export_dir
)

# Build Cubit model of Parastell Components
stellarator.build_cubit_model(
skip_imprint=False,
legacy_faceting=True)

# Export DAGMC neutronics H5M file
stellarator.export_dagmc(
skip_imprint=False,
legacy_faceting=True,
filename='dagmc',
export_dir=export_dir
)
12 changes: 12 additions & 0 deletions parastell/cubit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ def export_step_cubit(filename, export_dir=''):
export_path = Path(export_dir) / Path(filename).with_suffix('.step')
cubit.cmd(f'export step "{export_path}" overwrite')

def export_cub5(filename, export_dir=''):
"""Export cub5 representation of model (native Cubit format).
Arguments:
filename (str): name of cub5 output file, excluding '.cub5' extension.
export_dir (str): directory to which to export the cub5 output file
(defaults to empty string).
"""
init_cubit()

export_path = Path(export_dir) / Path(filename).with_suffix('.cub5')
cubit.cmd(f'save cub5 "{export_path}" overwrite')

def export_mesh_cubit(filename, export_dir=''):
"""Exports Cubit mesh to H5M file format, first exporting to Exodus format
Expand Down
127 changes: 84 additions & 43 deletions parastell/parastell.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
from . import cubit_io
from .utils import read_yaml_config, filter_kwargs, m2cm

build_cubit_model_allowed_kwargs = ['skip_imprint', 'legacy_faceting']
export_dagmc_allowed_kwargs = ['faceting_tolerance', 'length_tolerance',
'normal_tolerance', 'anisotropic_ratio',
'deviation_angle']


def make_material_block(mat_tag, block_id, vol_id_str):
"""Issue commands to make a material block using Cubit's
native capabilities.
Arguments:
mat_tag (str) : name of material block
block_id (int) : block number
Expand Down Expand Up @@ -58,7 +63,7 @@ def __init__(
vmec_file,
logger=None
):

self.logger = logger
self.vmec_file = vmec_file

Expand All @@ -69,7 +74,7 @@ def __init__(
@property
def vmec_file(self):
return self._vmec_file

@vmec_file.setter
def vmec_file(self, file):
self._vmec_file = file
Expand All @@ -82,7 +87,7 @@ def vmec_file(self, file):
@property
def logger(self):
return self._logger

@logger.setter
def logger(self, logger_object):
self._logger = log.check_init(logger_object)
Expand Down Expand Up @@ -340,7 +345,7 @@ def _tag_materials_native(self):
block_id = min(vol_list)
vol_id_str = " ".join(str(i) for i in vol_list)
make_material_block(self.magnet_set.mat_tag, block_id, vol_id_str)

if self.invessel_build:
for data in (
self.invessel_build.radial_build.radial_build.values()
Expand All @@ -349,19 +354,42 @@ def _tag_materials_native(self):
vol_id_str = str(block_id)
make_material_block(data['mat_tag'], block_id, vol_id_str)

def export_dagmc(
self, skip_imprint=False, legacy_faceting=True, filename='dagmc',
export_dir='', **kwargs
):
"""Exports DAGMC neutronics H5M file of ParaStell components via
Coreform Cubit.
def build_cubit_model(self, skip_imprint=False, legacy_faceting=True):
"""Build model for DAGMC neutronics H5M file of Parastell components via
Coreform Cubit
Arguments:
skip_imprint (bool): choose whether to imprint and merge all in
Coreform Cubit or to merge surfaces based on import order and
geometry information (optional, defaults to False).
legacy_faceting (bool): choose legacy or native faceting for DAGMC
export (optional, defaults to True).
"""
self.legacy_faceting = legacy_faceting

self._logger.info(
'Building DAGMC neutronics model via Coreform Cubit...'
)

if self.invessel_build:
self._import_ivb_step()

if skip_imprint:
self.invessel_build.merge_layer_surfaces()
else:
cubit.cmd('imprint volume all')
cubit.cmd('merge volume all')

if legacy_faceting:
self._tag_materials_legacy()
else:
self._tag_materials_native()

def export_dagmc(self, filename='dagmc', export_dir='', **kwargs):
"""Exports DAGMC neutronics H5M file of ParaStell components via
Coreform Cubit.
Arguments:
filename (str): name of DAGMC output file, excluding '.h5m'
extension (optional, defaults to 'dagmc').
export_dir (str): directory to which to export DAGMC output file
Expand All @@ -387,35 +415,42 @@ def export_dagmc(
attribute is used only for the native faceting method.
"""
cubit_io.init_cubit()

self._logger.info(
'Exporting DAGMC neutronics model...'
)

if self.invessel_build:
self._import_ivb_step()

if skip_imprint:
self.invessel_build.merge_layer_surfaces()
else:
cubit.cmd('imprint volume all')
cubit.cmd('merge volume all')

if legacy_faceting:
self._tag_materials_legacy()
if self.legacy_faceting:
cubit_io.export_dagmc_cubit_legacy(
filename=filename,
export_dir=export_dir,
**kwargs
)
else:
self._tag_materials_native()
cubit_io.export_dagmc_cubit_native(
filename=filename,
export_dir=export_dir,
**kwargs
)

def export_cub5(self, filename='stellarator', export_dir=''):
"""Export native Coreform Cubit format (cub5) of Parastell model.
Arguments:
filename (str): name of DAGMC output file, excluding '.h5m'
extension (optional, defaults to 'dagmc').
export_dir (str): directory to which to export DAGMC output file
(optional, defaults to empty string).
"""
cubit_io.init_cubit()

self._logger.info(
'Exporting cub5 model...'
)

cubit_io.export_cub5(filename=filename,
export_dir=export_dir)


def parse_args():
"""Parser for running as a script.
Expand Down Expand Up @@ -445,7 +480,7 @@ def parse_args():
)

parser.add_argument(
'-i', '--ivb',
'-i', '--ivb',
action='store_true',
help=(
'flag to indicate the creation of in-vessel component geometry '
Expand All @@ -454,15 +489,15 @@ def parse_args():
)

parser.add_argument(
'-m', '--magnets',
'-m', '--magnets',
action='store_true',
help=(
'flag to indicate the creation of magnet geometry (default: False)'
)
)

parser.add_argument(
'-s', '--source',
'-s', '--source',
action='store_true',
help=(
'flag to indicate the creation of a tetrahedral source mesh '
Expand All @@ -471,7 +506,7 @@ def parse_args():
)

parser.add_argument(
'-n', '--nwl',
'-n', '--nwl',
action='store_true',
help=(
'flag to indicate the creation of a geometry for neutron wall '
Expand Down Expand Up @@ -503,30 +538,30 @@ def check_inputs(
ivb_tor_ext = (repeat + 1) * invessel_build['toroidal_angles'][-1]
mag_tor_ext = magnet_coils['toroidal_extent']
src_tor_ext = source_mesh['toroidal_extent']

if ivb_tor_ext != mag_tor_ext:
w = Warning(
f'The total toroidal extent of the in-vessel build, {ivb_tor_ext} '
'degrees, does not match the toroidal extent of the magnet coils, '
f'{mag_tor_ext} degrees.'
)
logger.warning(w.args[0])

if ivb_tor_ext != src_tor_ext:
w = Warning(
f'The total toroidal extent of the in-vessel build, {ivb_tor_ext} '
'degrees, does not match the toroidal extent of the source mesh, '
f'{src_tor_ext} degrees.'
)
logger.warning(w.args[0])

if mag_tor_ext != src_tor_ext:
w = Warning(
f'The toroidal extent of the magnet coils, {mag_tor_ext} degrees, '
f'does not match that of the source mesh, {src_tor_ext} degrees.'
)
logger.warning(w.args[0])

if 'scale' in invessel_build:
ivb_scale = invessel_build['scale']
else:
Expand All @@ -544,7 +579,7 @@ def check_inputs(
)
logger.error(e.args[0])
raise e

if (
'export_cad_to_dagmc' in invessel_build and
invessel_build['export_cad_to_dagmc']
Expand All @@ -553,7 +588,7 @@ def check_inputs(
ivb_dagmc_filename = invessel_build['dagmc_filename']
else:
ivb_dagmc_filename = 'dagmc'

if 'filename' in dagmc_export:
ps_dagmc_filename = dagmc_export['filename']
else:
Expand Down Expand Up @@ -610,7 +645,7 @@ def parastell():
stellarator.export_magnets(
export_dir=args.export_dir,
**(filter_kwargs(magnet_coils, mc.export_allowed_kwargs))
)
)

if args.source:
source_mesh = all_data['source_mesh']
Expand All @@ -619,20 +654,26 @@ def parastell():
export_dir=args.export_dir,
**(filter_kwargs(source_mesh, sm.export_allowed_kwargs))
)

if args.ivb or args.magnets:
dagmc_export = all_data['dagmc_export']
stellarator.build_cubit_model(
**(filter_kwargs(dagmc_export, build_cubit_model_allowed_kwargs))
)
stellarator.export_dagmc(
export_dir=args.export_dir,
**dagmc_export
**(filter_kwargs(dagmc_export, export_dagmc_allowed_kwargs))
)

if all_data['cub5_export']:
stellarator.export_cub5(export_dir=args.export_dir)

if args.nwl:
if not args.ivb:
invessel_build = all_data['invessel_build']
if not args.magnets:
dagmc_export = all_data['dagmc_export']

if cubit_io.initialized:
cubit.cmd('new')

Expand All @@ -644,22 +685,22 @@ def parastell():
nwl_required_keys = [
'toroidal_angles', 'poloidal_angles', 'wall_s'
]

nwl_build = {}
for key in nwl_keys:
nwl_build[key] = invessel_build[key]
nwl_build['radial_build'] = {}

nwl_optional_keys = [
'num_ribs', 'num_rib_pts', 'repeat', 'scale'
]

for key in invessel_build.keys() & nwl_optional_keys:
nwl_build[key] = invessel_build[key]

nwl_geom.construct_invessel_build(**nwl_build)
nwl_geom.export_invessel_build(export_dir=args.export_dir)

nwl_geom.export_dagmc(
skip_imprint=True,
filename='nwl_geom',
Expand Down
5 changes: 5 additions & 0 deletions tests/test_parastell.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def remove_files():
Path.unlink('magnet_mesh.h5m')
if Path('dagmc.h5m').exists():
Path.unlink('dagmc.h5m')
if Path('dagmc.cub5').exists():
Path.unlink('dagmc.cub5')
if Path('source_mesh.h5m').exists():
Path.unlink('source_mesh.h5m')
if Path('stellarator.log').exists():
Expand Down Expand Up @@ -118,8 +120,11 @@ def test_parastell(stellarator):

filename_exp = 'dagmc'

stellarator.build_cubit_model()
stellarator.export_dagmc(filename=filename_exp)
stellarator.export_cub5(filename=filename_exp)

assert Path(filename_exp).with_suffix('.h5m').exists()
assert Path(filename_exp).with_suffix('.cub5').exists()

remove_files()

0 comments on commit 1e26b75

Please sign in to comment.