cad-to-dagmc 0.8.1__py3-none-any.whl → 0.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cad-to-dagmc might be problematic. Click here for more details.

_version.py CHANGED
@@ -1,8 +1,13 @@
1
- # file generated by setuptools_scm
1
+ # file generated by setuptools-scm
2
2
  # don't change, don't track in version control
3
+
4
+ __all__ = ["__version__", "__version_tuple__", "version", "version_tuple"]
5
+
3
6
  TYPE_CHECKING = False
4
7
  if TYPE_CHECKING:
5
- from typing import Tuple, Union
8
+ from typing import Tuple
9
+ from typing import Union
10
+
6
11
  VERSION_TUPLE = Tuple[Union[int, str], ...]
7
12
  else:
8
13
  VERSION_TUPLE = object
@@ -12,5 +17,5 @@ __version__: str
12
17
  __version_tuple__: VERSION_TUPLE
13
18
  version_tuple: VERSION_TUPLE
14
19
 
15
- __version__ = version = '0.8.1'
16
- __version_tuple__ = version_tuple = (0, 8, 1)
20
+ __version__ = version = '0.9.0'
21
+ __version_tuple__ = version_tuple = (0, 9, 0)
cad_to_dagmc/core.py CHANGED
@@ -6,9 +6,10 @@ from cadquery import importers
6
6
  from pymoab import core, types
7
7
  import tempfile
8
8
  import warnings
9
+ from cad_to_dagmc import __version__
9
10
 
10
11
 
11
- def _define_moab_core_and_tags() -> tuple[core.Core, dict]:
12
+ def define_moab_core_and_tags() -> tuple[core.Core, dict]:
12
13
  """Creates a MOAB Core instance which can be built up by adding sets of
13
14
  triangles to the instance
14
15
 
@@ -62,7 +63,7 @@ def _define_moab_core_and_tags() -> tuple[core.Core, dict]:
62
63
  return moab_core, tags
63
64
 
64
65
 
65
- def _vertices_to_h5m(
66
+ def vertices_to_h5m(
66
67
  vertices: list[tuple[float, float, float]] | list["cadquery.occ_impl.geom.Vector"],
67
68
  triangles_by_solid_by_face: list[list[tuple[int, int, int]]],
68
69
  material_tags: list[str],
@@ -100,7 +101,7 @@ def _vertices_to_h5m(
100
101
  else:
101
102
  face_ids_with_solid_ids[face_id] = [solid_id]
102
103
 
103
- moab_core, tags = _define_moab_core_and_tags()
104
+ moab_core, tags = define_moab_core_and_tags()
104
105
 
105
106
  volume_sets_by_solid_id = {}
106
107
  for material_tag, (solid_id, triangles_on_each_face) in zip(
@@ -229,11 +230,11 @@ def get_volumes(gmsh, assembly, method="file", scale_factor=1.0):
229
230
  def init_gmsh():
230
231
  gmsh.initialize()
231
232
  gmsh.option.setNumber("General.Terminal", 1)
232
- gmsh.model.add("made_with_cad_to_dagmc_package")
233
+ gmsh.model.add(f"made_with_cad_to_dagmc_package_{__version__}")
233
234
  return gmsh
234
235
 
235
236
 
236
- def _mesh_brep(
237
+ def mesh_brep(
237
238
  gmsh,
238
239
  min_mesh_size: float | None = None,
239
240
  max_mesh_size: float | None = None,
@@ -318,7 +319,9 @@ def mesh_to_vertices_and_triangles(
318
319
  for dim_and_vol in dims_and_vol_ids:
319
320
  # removes all groups so that the following getEntitiesForPhysicalGroup
320
321
  # command only finds surfaces for the volume
321
- gmsh.model.removePhysicalGroups()
322
+ face_groups = gmsh.model.getPhysicalGroups(2)
323
+ if face_groups: # Only remove if 2D groups exist
324
+ gmsh.model.removePhysicalGroups(face_groups)
322
325
 
323
326
  vol_id = dim_and_vol[1]
324
327
  entities_in_volume = gmsh.model.getAdjacencies(3, vol_id)
@@ -355,21 +358,21 @@ def mesh_to_vertices_and_triangles(
355
358
  return vertices, triangles_by_solid_by_face
356
359
 
357
360
 
358
- def _get_ids_from_assembly(assembly: cq.assembly.Assembly):
361
+ def get_ids_from_assembly(assembly: cq.assembly.Assembly):
359
362
  ids = []
360
363
  for obj, name, loc, _ in assembly:
361
364
  ids.append(name)
362
365
  return ids
363
366
 
364
367
 
365
- def _get_ids_from_imprinted_assembly(solid_id_dict):
368
+ def get_ids_from_imprinted_assembly(solid_id_dict):
366
369
  ids = []
367
370
  for id in list(solid_id_dict.values()):
368
371
  ids.append(id[0])
369
372
  return ids
370
373
 
371
374
 
372
- def _check_material_tags(material_tags, iterable_solids):
375
+ def check_material_tags(material_tags, iterable_solids):
373
376
  if material_tags:
374
377
  if len(material_tags) != len(iterable_solids):
375
378
  msg = (
@@ -399,64 +402,122 @@ def order_material_ids_by_brep_order(original_ids, scrambled_id, material_tags):
399
402
  return material_tags_in_brep_order
400
403
 
401
404
 
402
- class MeshToDagmc:
403
- """Convert a GMSH mesh file to a DAGMC h5m file"""
405
+ def export_gmsh_object_to_dagmc_h5m_file(
406
+ material_tags: list[str] | None = None,
407
+ implicit_complement_material_tag: str | None = None,
408
+ filename: str = "dagmc.h5m",
409
+ ) -> str:
410
+ """
411
+ Exports a GMSH object to a DAGMC-compatible h5m file. Note gmsh should
412
+ be initialized by the user prior and the gmsh model should be meshed before
413
+ calling this. Also users should ensure that the gmsh model is finalized.
404
414
 
405
- def __init__(self, filename: str):
406
- self.filename = filename
415
+ Args:
416
+ material_tags: A list of material tags corresponding to the volumes in the GMSH object.
417
+ implicit_complement_material_tag: The material tag for the implicit complement (void space).
418
+ filename: The name of the output h5m file. Defaults to "dagmc.h5m".
407
419
 
408
- # TODO add export_unstructured_mesh_file
409
- # TODO add add_gmsh_msh_file
410
- # TODO test for exports result in files
420
+ Returns:
421
+ str: The filename of the generated DAGMC h5m file.
411
422
 
412
- def export_dagmc_h5m_file(
413
- self,
414
- material_tags: list[str],
415
- implicit_complement_material_tag: str | None = None,
416
- filename: str = "dagmc.h5m",
417
- ):
418
- """Saves a DAGMC h5m file of the geometry
423
+ Raises:
424
+ ValueError: If the number of material tags does not match the number of volumes in the GMSH object.
425
+ """
419
426
 
420
- Args:
421
- material_tags (list[str]): the names of the DAGMC
422
- material tags to assign. These will need to be in the same
423
- order as the volumes in the GMESH mesh and match the
424
- material tags used in the neutronics code (e.g. OpenMC).
425
- implicit_complement_material_tag (str | None, optional):
426
- the name of the material tag to use for the implicit
427
- complement (void space). Defaults to None which is a vacuum.
428
- filename (str, optional): _description_. Defaults to "dagmc.h5m".
427
+ if material_tags is None:
428
+ material_tags = _get_material_tags_from_gmsh()
429
429
 
430
- Raises:
431
- ValueError: _description_
430
+ dims_and_vol_ids = gmsh.model.getEntities(3)
432
431
 
433
- Returns:
434
- _type_: _description_
435
- """
432
+ if len(dims_and_vol_ids) != len(material_tags):
433
+ msg = f"Number of volumes {len(dims_and_vol_ids)} is not equal to number of material tags {len(material_tags)}"
434
+ raise ValueError(msg)
436
435
 
437
- gmsh.initialize()
438
- self.mesh_file = gmsh.open(self.filename)
439
- dims_and_vol_ids = gmsh.model.getEntities(3)
436
+ vertices, triangles_by_solid_by_face = mesh_to_vertices_and_triangles(
437
+ dims_and_vol_ids=dims_and_vol_ids
438
+ )
440
439
 
441
- if len(dims_and_vol_ids) != len(material_tags):
442
- msg = f"Number of volumes {len(dims_and_vol_ids)} is not equal to number of material tags {len(material_tags)}"
443
- raise ValueError(msg)
440
+ h5m_filename = vertices_to_h5m(
441
+ vertices=vertices,
442
+ triangles_by_solid_by_face=triangles_by_solid_by_face,
443
+ material_tags=material_tags,
444
+ h5m_filename=filename,
445
+ implicit_complement_material_tag=implicit_complement_material_tag,
446
+ )
444
447
 
445
- vertices, triangles_by_solid_by_face = mesh_to_vertices_and_triangles(
446
- dims_and_vol_ids=dims_and_vol_ids
447
- )
448
+ return h5m_filename
448
449
 
449
- gmsh.finalize()
450
450
 
451
- h5m_filename = _vertices_to_h5m(
452
- vertices=vertices,
453
- triangles_by_solid_by_face=triangles_by_solid_by_face,
454
- material_tags=material_tags,
455
- h5m_filename=filename,
456
- implicit_complement_material_tag=implicit_complement_material_tag,
457
- )
451
+ def _get_material_tags_from_gmsh() -> list[str]:
452
+ """Gets the Physical groups of 3D groups from the GMSH object and returns
453
+ their names."""
454
+
455
+ # Get all 3D physical groups (volumes)
456
+ volume_groups = gmsh.model.getPhysicalGroups(3)
457
+
458
+ material_tags = []
459
+ # Get the name for each physical group
460
+ for dim, tag in volume_groups:
461
+ name = gmsh.model.getPhysicalName(dim, tag)
462
+ material_tags.append(name)
463
+ print(f"Material tag: {name}")
464
+ print(f"Material tags: {material_tags}")
465
+ return material_tags
466
+
467
+
468
+ def export_gmsh_file_to_dagmc_h5m_file(
469
+ gmsh_filename: str,
470
+ material_tags: list[str] | None = None,
471
+ implicit_complement_material_tag: str | None = None,
472
+ dagmc_filename: str = "dagmc.h5m",
473
+ ) -> str:
474
+ """Saves a DAGMC h5m file of the geometry GMsh file. This function
475
+ initializes and finalizes Gmsh.
476
+
477
+ Args:
478
+ material_tags (list[str]): the names of the DAGMC
479
+ material tags to assign. These will need to be in the same
480
+ order as the volumes in the GMESH mesh and match the
481
+ material tags used in the neutronics code (e.g. OpenMC).
482
+ implicit_complement_material_tag (str | None, optional):
483
+ the name of the material tag to use for the implicit
484
+ complement (void space). Defaults to None which is a vacuum.
485
+ dagmc_filename (str, optional): _description_. Defaults to "dagmc.h5m".
486
+
487
+ Returns:
488
+ str: The filename of the generated DAGMC h5m file.
489
+
490
+ Raises:
491
+ ValueError: If the number of material tags does not match the number of volumes in the GMSH object.
492
+ """
493
+
494
+ gmsh.initialize()
495
+ gmsh.open(gmsh_filename)
496
+
497
+ if material_tags is None:
498
+ material_tags = _get_material_tags_from_gmsh()
499
+
500
+ dims_and_vol_ids = gmsh.model.getEntities(3)
501
+
502
+ if len(dims_and_vol_ids) != len(material_tags):
503
+ msg = f"Number of volumes {len(dims_and_vol_ids)} is not equal to number of material tags {len(material_tags)}"
504
+ raise ValueError(msg)
458
505
 
459
- return h5m_filename
506
+ vertices, triangles_by_solid_by_face = mesh_to_vertices_and_triangles(
507
+ dims_and_vol_ids=dims_and_vol_ids
508
+ )
509
+
510
+ gmsh.finalize()
511
+
512
+ h5m_filename = vertices_to_h5m(
513
+ vertices=vertices,
514
+ triangles_by_solid_by_face=triangles_by_solid_by_face,
515
+ material_tags=material_tags,
516
+ h5m_filename=dagmc_filename,
517
+ implicit_complement_material_tag=implicit_complement_material_tag,
518
+ )
519
+
520
+ return h5m_filename
460
521
 
461
522
 
462
523
  class CadToDagmc:
@@ -537,7 +598,7 @@ class CadToDagmc:
537
598
  else:
538
599
  scaled_iterable_solids = [part.scale(scale_factor) for part in iterable_solids]
539
600
 
540
- _check_material_tags(material_tags, scaled_iterable_solids)
601
+ check_material_tags(material_tags, scaled_iterable_solids)
541
602
  if material_tags:
542
603
  self.material_tags = self.material_tags + material_tags
543
604
  self.parts = self.parts + scaled_iterable_solids
@@ -613,7 +674,7 @@ class CadToDagmc:
613
674
 
614
675
  gmsh, _ = get_volumes(gmsh, imprinted_assembly, method=method, scale_factor=scale_factor)
615
676
 
616
- gmsh = _mesh_brep(
677
+ gmsh = mesh_brep(
617
678
  gmsh=gmsh,
618
679
  min_mesh_size=min_mesh_size,
619
680
  max_mesh_size=max_mesh_size,
@@ -690,7 +751,7 @@ class CadToDagmc:
690
751
 
691
752
  gmsh, _ = get_volumes(gmsh, imprinted_assembly, method=method, scale_factor=scale_factor)
692
753
 
693
- gmsh = _mesh_brep(
754
+ gmsh = mesh_brep(
694
755
  gmsh=gmsh,
695
756
  min_mesh_size=min_mesh_size,
696
757
  max_mesh_size=max_mesh_size,
@@ -761,7 +822,7 @@ class CadToDagmc:
761
822
  for part in self.parts:
762
823
  assembly.add(part)
763
824
 
764
- original_ids = _get_ids_from_assembly(assembly)
825
+ original_ids = get_ids_from_assembly(assembly)
765
826
 
766
827
  # both id lists should be the same length as each other and the same
767
828
  # length as the self.material_tags
@@ -774,7 +835,7 @@ class CadToDagmc:
774
835
  assembly
775
836
  )
776
837
 
777
- scrambled_ids = _get_ids_from_imprinted_assembly(imprinted_solids_with_org_id)
838
+ scrambled_ids = get_ids_from_imprinted_assembly(imprinted_solids_with_org_id)
778
839
 
779
840
  material_tags_in_brep_order = order_material_ids_by_brep_order(
780
841
  original_ids, scrambled_ids, self.material_tags
@@ -783,7 +844,7 @@ class CadToDagmc:
783
844
  material_tags_in_brep_order = self.material_tags
784
845
  imprinted_assembly = assembly
785
846
 
786
- _check_material_tags(material_tags_in_brep_order, self.parts)
847
+ check_material_tags(material_tags_in_brep_order, self.parts)
787
848
 
788
849
  gmsh = init_gmsh()
789
850
 
@@ -791,7 +852,7 @@ class CadToDagmc:
791
852
  gmsh, imprinted_assembly, method=method, scale_factor=scale_factor
792
853
  )
793
854
 
794
- gmsh = _mesh_brep(
855
+ gmsh = mesh_brep(
795
856
  gmsh=gmsh,
796
857
  min_mesh_size=min_mesh_size,
797
858
  max_mesh_size=max_mesh_size,
@@ -808,7 +869,7 @@ class CadToDagmc:
808
869
  gmsh.finalize()
809
870
 
810
871
  # checks and fixes triangle fix_normals within vertices_to_h5m
811
- return _vertices_to_h5m(
872
+ return vertices_to_h5m(
812
873
  vertices=vertices,
813
874
  triangles_by_solid_by_face=triangles_by_solid_by_face,
814
875
  material_tags=material_tags_in_brep_order,
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: cad_to_dagmc
3
- Version: 0.8.1
3
+ Version: 0.9.0
4
4
  Summary: Converts CAD files to a DAGMC h5m file
5
5
  Author-email: Jonathan Shimwell <mail@jshimwell.com>
6
6
  Project-URL: Homepage, https://github.com/fusion-energy/cad_to_dagmc
@@ -20,6 +20,8 @@ Requires-Dist: gmsh
20
20
  Provides-Extra: tests
21
21
  Requires-Dist: pytest; extra == "tests"
22
22
  Requires-Dist: vtk; extra == "tests"
23
+ Requires-Dist: assembly-mesh-plugin; extra == "tests"
24
+ Dynamic: license-file
23
25
 
24
26
 
25
27
  [![N|Python](https://www.python.org/static/community_logos/python-powered-w-100x40.png)](https://www.python.org)
@@ -43,10 +45,13 @@ cad-to-dagmc can create DAGMC compatible:
43
45
 
44
46
  cad-to-dagmc can convert the following in to DAGMC compatible meshes:
45
47
  - STEP files
46
- - CadQuery objects (in memory)
47
- - Gmsh meshes
48
+ - CadQuery objects (optionally use names as material tags)
49
+ - Gmsh meshes (optionally use physical groups as material tags)
48
50
 
49
51
  Cad-to-dagmc offers a wide range of features including.
52
+ - Compatibly with [assembly-mesh-plugin](https://github.com/CadQuery/assembly-mesh-plugin) (see examples)
53
+ - Access to the Gmsh mesh to allow user to define full set of mesh parameters
54
+ - Option to use Gmsh physical groups as material tags
50
55
  - Geometry scaling with ```scale_factor``` argument
51
56
  - Model wide mesh size parameters with ```min_mesh_size``` and ```max_mesh_size``` arguments
52
57
  - Volume specific mesh sizing parameters with the ```set_size``` argument
@@ -0,0 +1,8 @@
1
+ _version.py,sha256=fqtOm8q4HIqAgc-mXbjsKoz34kMK1y3t9LDhc4497WE,511
2
+ cad_to_dagmc/__init__.py,sha256=fskHUTyCunSpnpJUvBfAYjx4uwDKXHTTiMP6GqnFRf0,494
3
+ cad_to_dagmc/core.py,sha256=C3NEy5z4bjbg0SQjodGcZSOVqMAc8g5e0zLoF2gs0Og,34140
4
+ cad_to_dagmc-0.9.0.dist-info/licenses/LICENSE,sha256=B8kznH_777JVNZ3HOKDc4Tj24F7wJ68ledaNYeL9sCw,1070
5
+ cad_to_dagmc-0.9.0.dist-info/METADATA,sha256=J3Q4BXSU_ApF86K8sjDXA_hYT_k5GRRAKXaYc-ksbuw,9092
6
+ cad_to_dagmc-0.9.0.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
7
+ cad_to_dagmc-0.9.0.dist-info/top_level.txt,sha256=zTi8C64SEBsE5WOtPovnxhOzt-E6Oc5nC3RW6M_5aEA,22
8
+ cad_to_dagmc-0.9.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (78.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,8 +0,0 @@
1
- _version.py,sha256=Z8fNqmfsoA07nbVEewR90wZCgaN6eyDwPwgtgc2KIak,411
2
- cad_to_dagmc/__init__.py,sha256=fskHUTyCunSpnpJUvBfAYjx4uwDKXHTTiMP6GqnFRf0,494
3
- cad_to_dagmc/core.py,sha256=DbxXCaVEIiJxulRJlatoQg-yce37SS9Dx914Gt91qKY,31883
4
- cad_to_dagmc-0.8.1.dist-info/LICENSE,sha256=B8kznH_777JVNZ3HOKDc4Tj24F7wJ68ledaNYeL9sCw,1070
5
- cad_to_dagmc-0.8.1.dist-info/METADATA,sha256=AQ41vcWt_gHdZFL_rh7n28O5CheNftqa-FWdaVWvhjI,8700
6
- cad_to_dagmc-0.8.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
- cad_to_dagmc-0.8.1.dist-info/top_level.txt,sha256=zTi8C64SEBsE5WOtPovnxhOzt-E6Oc5nC3RW6M_5aEA,22
8
- cad_to_dagmc-0.8.1.dist-info/RECORD,,