Skip to content

Commit

Permalink
Convert new workflow to one using CAD-to-DAGMC
Browse files Browse the repository at this point in the history
  • Loading branch information
connoramoreno committed Dec 12, 2024
1 parent 17f93ae commit 4d10131
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 93 deletions.
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies:
- scikit-learn
- cadquery
- moab=5.5.1[build=nompi_tempest_*]
- cad_to_dagmc
- ca-certificates
- certifi
- openssl
Expand All @@ -18,5 +19,4 @@ dependencies:
- netcdf4
- pyyaml
- pytest
- git+https://github.com/aaroncbader/pystell_uw.git
- stellarmesh
- git+https://github.com/aaroncbader/pystell_uw.git
10 changes: 0 additions & 10 deletions parastell/invessel_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
normalize,
expand_list,
read_yaml_config,
filter_kwargs,
m2cm,
)

export_allowed_kwargs = ["export_cad_to_dagmc", "dagmc_filename"]


def orient_spline_surfaces(volume_id):
"""Extracts the inner and outer surface IDs for a given ParaStell in-vessel
Expand Down Expand Up @@ -839,13 +836,6 @@ def generate_invessel_build():

invessel_build.export_step(export_dir=args.export_dir)

if invessel_build_dict["export_cad_to_dagmc"]:

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


if __name__ == "__main__":
generate_invessel_build()
114 changes: 39 additions & 75 deletions parastell/parastell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cubit
import numpy as np
import pystell.read_vmec as read_vmec
import stellarmesh
import cad_to_dagmc

from . import log
from . import invessel_build as ivb
Expand All @@ -15,7 +15,7 @@
from .utils import read_yaml_config, filter_kwargs, m2cm

build_cubit_model_allowed_kwargs = ["skip_imprint", "legacy_faceting"]
export_dagmc_cubit_allowed_kwargs = [
export_cf_dagmc_allowed_kwargs = [
"faceting_tolerance",
"length_tolerance",
"normal_tolerance",
Expand Down Expand Up @@ -170,28 +170,15 @@ def construct_invessel_build(
self.invessel_build.calculate_loci()
self.invessel_build.generate_components()

def export_invessel_build(
self, export_cad_to_dagmc=False, dagmc_filename="dagmc", export_dir=""
):
"""Exports InVesselBuild component STEP files and, optionally, a DAGMC
neutronics H5M file of in-vessel components via CAD-to-DAGMC.
def export_invessel_build(self, export_dir=""):
"""Exports InVesselBuild component STEP files.
Arguments:
export_cad_to_dagmc (bool): export DAGMC neutronics H5M file of
in-vessel components via CAD-to-DAGMC (optional, defaults to
False).
dagmc_filename (str): name of DAGMC output file, excluding '.h5m'
extension (optional, defaults to 'dagmc').
export_dir (str): directory to which to export the output files
(optional, defaults to empty string).
"""
self.invessel_build.export_step(export_dir=export_dir)

if export_cad_to_dagmc:
self.invessel_build.export_cad_to_dagmc(
dagmc_filename=dagmc_filename, export_dir=export_dir
)

def construct_magnets(
self, coils_file, width, thickness, toroidal_extent, **kwargs
):
Expand Down Expand Up @@ -391,7 +378,7 @@ def build_cubit_model(self, skip_imprint=False, legacy_faceting=True):
else:
self._tag_materials_native()

def export_dagmc_cubit(self, filename="dagmc", export_dir="", **kwargs):
def export_cf_dagmc(self, filename="dagmc", export_dir="", **kwargs):
"""Exports DAGMC neutronics H5M file of ParaStell components via
Coreform Cubit.
Expand Down Expand Up @@ -448,24 +435,14 @@ def export_cub5(self, filename="stellarator", export_dir=""):

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

def build_stellarmesh_model(
self, min_mesh_size=20, max_mesh_size=50, **kwargs
):
def build_cad_to_dagmc_model(self):
"""Build model for DAGMC neutronics H5M file of Parastell components via
Stellarmesh.
Arguments:
min_mesh_size (float): minimum size of mesh elements (defaults to
20).
max_mesh_size (float): maximum size of mesh elements (defaults to
50).
Optional arguments:
num_threads (int): maximum number of threads to use for geometry
meshing (defaults to None). Only valid when Gmsh is compiled
with OpenMP support. An input value of 0 equates to the system
default (i.e. OMP_NUM_THREADS).
CAD-to-DAGMC.
"""
self._logger.info(
"Building DAGMC neutronics model via CAD-to-DAGMC..."
)

solids = []
material_names = []

Expand All @@ -483,28 +460,41 @@ def build_stellarmesh_model(
)
)

geometry = stellarmesh.Geometry(solids, material_names)
self.full_mesh = stellarmesh.Mesh.from_geometry(
geometry,
min_mesh_size=min_mesh_size,
max_mesh_size=max_mesh_size,
**kwargs,
)
self.dagmc_model = cad_to_dagmc.CadToDagmc()

def export_dagmc_stellarmesh(self, filename="dagmc", export_dir=""):
for solid, mat_tag in zip(solids, material_names):
self.dagmc_model.add_cadquery_object(
solid, material_tags=[mat_tag]
)

def export_cad_to_dagmc(
self,
filename="dagmc",
export_dir="",
min_mesh_size=20,
max_mesh_size=50,
):
"""Exports DAGMC neutronics H5M file of ParaStell components via
Stellarmesh.
CAD-to-DAGMC.
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).
min_mesh_size (float): minimum size of mesh elements (defaults to
20).
max_mesh_size (float): maximum size of mesh elements (defaults to
50).
"""
export_path = Path(export_dir) / Path(filename).with_suffix(".h5m")
self._logger.info("Exporting DAGMC neutronics model...")

self.dagmc_model = stellarmesh.DAGMCModel.from_mesh(self.full_mesh)
self.dagmc_model.write(export_path)
export_path = Path(export_dir) / Path(filename).with_suffix(".h5m")
self.dagmc_model.export_dagmc_h5m_file(
filename=str(export_path),
min_mesh_size=min_mesh_size,
max_mesh_size=max_mesh_size,
)


def parse_args():
Expand Down Expand Up @@ -640,29 +630,6 @@ 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"]
):
if "dagmc_filename" in invessel_build:
ivb_dagmc_filename = invessel_build["dagmc_filename"]
else:
ivb_dagmc_filename = "dagmc"

if "filename" in dagmc_export:
ps_dagmc_filename = dagmc_export["filename"]
else:
ps_dagmc_filename = "dagmc"

if ivb_dagmc_filename == ps_dagmc_filename:
e = ValueError(
"The DAGMC H5M filename for the CAD-to-DAGMC export matches "
"that of the Coreform Cubit DAGMC export. Please change one to "
"prevent overwriting files."
)
logger.error(e.args[0])
raise e


def parastell():
"""Main method when run as a command line script."""
Expand Down Expand Up @@ -690,10 +657,7 @@ def parastell():
if args.ivb:
invessel_build = all_data["invessel_build"]
stellarator.construct_invessel_build(**invessel_build)
stellarator.export_invessel_build(
export_dir=args.export_dir,
**(filter_kwargs(invessel_build, ivb.export_allowed_kwargs)),
)
stellarator.export_invessel_build(export_dir=args.export_dir)

if args.magnets:
magnet_coils = all_data["magnet_coils"]
Expand All @@ -718,7 +682,7 @@ def parastell():
)
stellarator.export_dagmc_cubit(
export_dir=args.export_dir,
**(filter_kwargs(dagmc_export, export_dagmc_cubit_allowed_kwargs)),
**(filter_kwargs(dagmc_export, export_cf_dagmc_allowed_kwargs)),
)

if all_data["cub5_export"]:
Expand Down Expand Up @@ -750,7 +714,7 @@ def parastell():
nwl_geom.construct_invessel_build(**nwl_build)
nwl_geom.export_invessel_build(export_dir=args.export_dir)

nwl_geom.export_dagmc_cubit(
nwl_geom.export_cf_dagmc(
skip_imprint=True, filename="nwl_geom", export_dir=args.export_dir
)

Expand Down
11 changes: 5 additions & 6 deletions tests/test_parastell.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,17 @@ def test_parastell(stellarator):
)
assert stellarator.magnet_set.volume_ids == magnet_volume_ids_exp

stellarator.export_dagmc_cubit(filename=filename_exp)
stellarator.export_cf_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()

stellarator.build_stellarmesh_model(
min_mesh_size=50, max_mesh_size=100, num_threads=0
)
stellarator.export_dagmc_stellarmesh()
print([volume.global_id for volume in stellarator.dagmc_model.volumes])
stellarator.build_cad_to_dagmc_model()
stellarator.export_cad_to_dagmc(min_mesh_size=50, max_mesh_size=100)

assert Path(filename_exp).with_suffix(".h5m").exists()

remove_files()

0 comments on commit 4d10131

Please sign in to comment.