From 72f73c164a44251f0b38bb04b4a5e787f3d02579 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 12 Sep 2022 17:25:27 +0100 Subject: [PATCH 1/7] refactor exporting to dagmc --- paramak/reactor.py | 2 -- paramak/utils.py | 27 +++++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/paramak/reactor.py b/paramak/reactor.py index 69b4b0502..fbe64f4dd 100644 --- a/paramak/reactor.py +++ b/paramak/reactor.py @@ -265,8 +265,6 @@ def export_dagmc_h5m( for shape in shapes_to_convert: tags.append(shape.name) - print(tags) - output_filename = export_solids_to_dagmc_h5m( solids=[shape.solid for shape in shapes_to_convert], filename=filename, diff --git a/paramak/utils.py b/paramak/utils.py index 6f335618e..dfc872e8d 100644 --- a/paramak/utils.py +++ b/paramak/utils.py @@ -105,7 +105,7 @@ def export_solids_to_dagmc_h5m( raise ValueError(msg) # a local import is used here as these packages need Moab to work - from brep_to_h5m import brep_to_h5m + from brep_to_h5m import mesh_brep, mesh_to_h5m_in_memory_method import brep_part_finder as bpf # saves the reactor as a Brep file with merged surfaces @@ -163,19 +163,30 @@ def export_solids_to_dagmc_h5m( tmp_brep_filename = mkstemp(suffix=".brep", prefix="paramak_")[1] brep_shape.exportBrep(tmp_brep_filename) - brep_to_h5m( + gmsh, volumes = mesh_brep( brep_filename=tmp_brep_filename, - volumes_with_tags=key_and_part_id, - h5m_filename=filename, min_mesh_size=min_mesh_size, max_mesh_size=max_mesh_size, - delete_intermediate_stl_files=True, + volumes_with_tags=key_and_part_id, + ) + + if verbose: + gmsh_filename = mkstemp(suffix=".msh", prefix="paramak_")[1] + print(f"written gmsh file to {gmsh_filename}") + gmsh.write(gmsh_filename) + + h5m_filename = mesh_to_h5m_in_memory_method( + volumes=volumes, + volumes_with_tags=key_and_part_id, + h5m_filename=filename, ) - # temporary brep is deleted using os.remove - remove(tmp_brep_filename) + if not verbose: + print(f"written brep file to {tmp_brep_filename}") + # temporary brep is deleted using os.remove + remove(tmp_brep_filename) - return filename + return h5m_filename def get_bounding_box(solid) -> Tuple[Tuple[float, float, float], Tuple[float, float, float]]: From 28098b6ab287beaf08f2160b22505c117da09495 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Tue, 20 Sep 2022 17:52:27 +0100 Subject: [PATCH 2/7] corrected function name --- paramak/utils.py | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/paramak/utils.py b/paramak/utils.py index dfc872e8d..4803ef077 100644 --- a/paramak/utils.py +++ b/paramak/utils.py @@ -111,35 +111,15 @@ def export_solids_to_dagmc_h5m( # saves the reactor as a Brep file with merged surfaces brep_shape = export_solids_to_brep_object(solids=solids) - brep_file_part_properties = bpf.get_brep_part_properties_from_shape(brep_shape) + brep_file_part_properties = bpf.get_part_properties_from_shapes(brep_shape) if verbose: print("brep_file_part_properties", brep_file_part_properties) - shape_properties = {} - for counter, solid in enumerate(solids): - sub_solid_descriptions = [] + shape_properties = bpf.get_part_properties_from_shapes(solids) + # for counter, solid in enumerate(solids): - # checks if the solid is a cq.Compound or not - if isinstance(solid, cq.occ_impl.shapes.Compound): - iterable_solids = solid.Solids() - else: - iterable_solids = solid.val().Solids() - - for sub_solid in iterable_solids: - part_bb = sub_solid.BoundingBox() - part_center = sub_solid.Center() - sub_solid_description = { - "volume": sub_solid.Volume(), - "center": (part_center.x, part_center.y, part_center.z), - "bounding_box": ( - (part_bb.xmin, part_bb.ymin, part_bb.zmin), - (part_bb.xmax, part_bb.ymax, part_bb.zmax), - ), - } - sub_solid_descriptions.append(sub_solid_description) - - shape_properties[tags[counter]] = sub_solid_descriptions + # shape_properties[counter] = bpf.get_part_properties_from_shape(solid) if verbose: print("shape_properties", shape_properties) @@ -148,16 +128,22 @@ def export_solids_to_dagmc_h5m( # using the volume, center, bounding box that we know about when creating the # CAD geometry in the first place - key_and_part_id = bpf.get_dict_of_part_ids( + brep_and_shape_part_ids = bpf.get_matching_part_ids( brep_part_properties=brep_file_part_properties, shape_properties=shape_properties, volume_atol=volume_atol, center_atol=center_atol, bounding_box_atol=bounding_box_atol, ) + if verbose: + print(f"brep_and_shape_part_ids={brep_and_shape_part_ids}") + + material_tags_in_brep_order = [] + for (brep_id, shape_id) in brep_and_shape_part_ids: + material_tags_in_brep_order.append(tag[shape_id - 1]) if verbose: - print(f"key_and_part_id={key_and_part_id}") + print(f"material_tags_in_brep_order={material_tags_in_brep_order}") # gmsh requires an actual brep file to load tmp_brep_filename = mkstemp(suffix=".brep", prefix="paramak_")[1] @@ -167,7 +153,6 @@ def export_solids_to_dagmc_h5m( brep_filename=tmp_brep_filename, min_mesh_size=min_mesh_size, max_mesh_size=max_mesh_size, - volumes_with_tags=key_and_part_id, ) if verbose: @@ -177,7 +162,7 @@ def export_solids_to_dagmc_h5m( h5m_filename = mesh_to_h5m_in_memory_method( volumes=volumes, - volumes_with_tags=key_and_part_id, + material_tags=material_tags_in_brep_order, h5m_filename=filename, ) From 0251ca327132ff39602a674835d80ec9252b3e14 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 21 Sep 2022 09:46:42 +0100 Subject: [PATCH 3/7] building up material tags correctly --- paramak/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramak/utils.py b/paramak/utils.py index 4803ef077..ee20d75cb 100644 --- a/paramak/utils.py +++ b/paramak/utils.py @@ -140,7 +140,7 @@ def export_solids_to_dagmc_h5m( material_tags_in_brep_order = [] for (brep_id, shape_id) in brep_and_shape_part_ids: - material_tags_in_brep_order.append(tag[shape_id - 1]) + material_tags_in_brep_order.append(tags[shape_id - 1]) if verbose: print(f"material_tags_in_brep_order={material_tags_in_brep_order}") From e79aac5e8495d59f95fe266d417b66a99de56dcd Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 22 Sep 2022 09:29:00 +0100 Subject: [PATCH 4/7] bumped brep h5m packages --- conda/meta.yaml | 4 ++-- setup.cfg | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/conda/meta.yaml b/conda/meta.yaml index 49fa83b90..1fb53ea24 100644 --- a/conda/meta.yaml +++ b/conda/meta.yaml @@ -22,8 +22,8 @@ requirements: - mpmath - plasmaboundaries >=0.1.8 - plotly - - brep_part_finder >=0.4.4 # [not win] - - brep_to_h5m >=0.3.1 # [not win] + - brep_part_finder >=0.5.0 # [not win] + - brep_to_h5m >=0.4.0 # [not win] - moab * nompi_tempest_* # - jupyter-cadquery not available on conda diff --git a/setup.cfg b/setup.cfg index c7e0d6e02..7ef783c5a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,8 +35,8 @@ install_requires= plasmaboundaries >= 0.1.8 jupyter-client < 7 jupyter-cadquery >= 3.2.0 - brep_part_finder >= 0.4.4 - brep_to_h5m >= 0.3.1 + brep_part_finder >= 0.5.0 + brep_to_h5m >= 0.4.0 setuptools_scm [options.extras_require] From ce285acb8be8486d2c6a15a956c72c5880ad8199 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 22 Sep 2022 15:26:21 +0100 Subject: [PATCH 5/7] expanding tags for compounds --- paramak/utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/paramak/utils.py b/paramak/utils.py index ee20d75cb..015387f58 100644 --- a/paramak/utils.py +++ b/paramak/utils.py @@ -104,6 +104,17 @@ def export_solids_to_dagmc_h5m( ) raise ValueError(msg) + compound_expanded_tags = [] + # solids could contain compounds + for tag, solid in zip(tags, solids): + # before accessing the .val() check it exists + if hasattr(solid, "val"): + # if it is a compound then we need more material tags + if isinstance(solid.val(), cq.occ_impl.shapes.Compound): + compound_expanded_tags = compound_expanded_tags + [tag] * len(solid.val().Solids()) + else: + compound_expanded_tags.append(tag) + # a local import is used here as these packages need Moab to work from brep_to_h5m import mesh_brep, mesh_to_h5m_in_memory_method import brep_part_finder as bpf @@ -140,7 +151,7 @@ def export_solids_to_dagmc_h5m( material_tags_in_brep_order = [] for (brep_id, shape_id) in brep_and_shape_part_ids: - material_tags_in_brep_order.append(tags[shape_id - 1]) + material_tags_in_brep_order.append(compound_expanded_tags[shape_id - 1]) if verbose: print(f"material_tags_in_brep_order={material_tags_in_brep_order}") From 9f544129fc0f832c995bdc9710ff4c1a00a5665a Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 22 Sep 2022 17:41:21 +0100 Subject: [PATCH 6/7] pep8 --- paramak/utils.py | 17 +++++++++++++++-- tests/tests_h5m/test_reactor_export_h5m.py | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/paramak/utils.py b/paramak/utils.py index 015387f58..8f469ec2d 100644 --- a/paramak/utils.py +++ b/paramak/utils.py @@ -96,6 +96,9 @@ def export_solids_to_dagmc_h5m( bounding_box_atol: float = 0.000001, tags: List[str] = None, ): + if verbose: + print("solids", solids, "\n") + print("tags", tags, "\n") if len(tags) != len(solids): msg = ( "When specifying tags then there must be one tag for " @@ -111,9 +114,19 @@ def export_solids_to_dagmc_h5m( if hasattr(solid, "val"): # if it is a compound then we need more material tags if isinstance(solid.val(), cq.occ_impl.shapes.Compound): - compound_expanded_tags = compound_expanded_tags + [tag] * len(solid.val().Solids()) + additional_tags = [tag] * len(solid.val().Solids()) + compound_expanded_tags = compound_expanded_tags + additional_tags else: compound_expanded_tags.append(tag) + # if it is a compound then we need more material tags + elif isinstance(solid, cq.occ_impl.shapes.Compound): + additional_tags = [tag] * len(solid.Solids()) + compound_expanded_tags = compound_expanded_tags + additional_tags + else: + compound_expanded_tags.append(tag) + + if verbose: + print("compound_expanded_tags", compound_expanded_tags, "\n") # a local import is used here as these packages need Moab to work from brep_to_h5m import mesh_brep, mesh_to_h5m_in_memory_method @@ -125,7 +138,7 @@ def export_solids_to_dagmc_h5m( brep_file_part_properties = bpf.get_part_properties_from_shapes(brep_shape) if verbose: - print("brep_file_part_properties", brep_file_part_properties) + print("brep_file_part_properties", brep_file_part_properties, "\n") shape_properties = bpf.get_part_properties_from_shapes(solids) # for counter, solid in enumerate(solids): diff --git a/tests/tests_h5m/test_reactor_export_h5m.py b/tests/tests_h5m/test_reactor_export_h5m.py index 60d5fb240..8407acf7e 100644 --- a/tests/tests_h5m/test_reactor_export_h5m.py +++ b/tests/tests_h5m/test_reactor_export_h5m.py @@ -97,7 +97,7 @@ def test_dagmc_h5m_export_with_graveyard(self): named in the resulting h5m file, includes the optional graveyard""" self.test_reactor_3.rotation_angle = 180 - self.test_reactor_3.export_dagmc_h5m("dagmc_reactor.h5m", include_graveyard={"size": 250}) + self.test_reactor_3.export_dagmc_h5m("dagmc_reactor.h5m", include_graveyard={"size": 250}, verbose=True) vols = di.get_volumes_from_h5m("dagmc_reactor.h5m") assert vols == [1, 2, 3, 4] From 77652392b82e29265ece52c81673826c6b650ac3 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 23 Sep 2022 20:46:32 +0100 Subject: [PATCH 7/7] allowing temp brep file to be found --- paramak/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/paramak/utils.py b/paramak/utils.py index 8f469ec2d..fd8412f15 100644 --- a/paramak/utils.py +++ b/paramak/utils.py @@ -190,8 +190,9 @@ def export_solids_to_dagmc_h5m( h5m_filename=filename, ) - if not verbose: + if verbose: print(f"written brep file to {tmp_brep_filename}") + else: # temporary brep is deleted using os.remove remove(tmp_brep_filename)