diff --git a/dagmc/dagnav.py b/dagmc/dagnav.py index fc90a9e..ca496f0 100644 --- a/dagmc/dagnav.py +++ b/dagmc/dagnav.py @@ -30,7 +30,7 @@ def _sets_by_category(self, set_type : str): @property def surfaces(self): return [Surface(self, h) for h in self._sets_by_category('Surface')] - + @property def surfaces_by_id(self): return {s.id: s for s in self.surfaces} @@ -359,6 +359,20 @@ def delete(self): self.handle = None self.model = None + @classmethod + def create(cls, model: DAGModel, global_id: Optional[int] = None) -> DAGSet: + """Create new set""" + # Add necessary tags for this meshset to be identified appropriately + ent_set = DAGSet(model, model.mb.create_meshset()) + ent_set.geom_dimension = cls._geom_dimension + ent_set.category = cls._category + # Now that the entity set has proper tags, create derived class and return + out = cls(model, ent_set.handle) + if global_id is not None: + out.id = global_id + return out + + class Surface(DAGSet): _category = 'Surface' @@ -450,12 +464,13 @@ def groups(self) -> list[Group]: """Get list of groups containing this volume.""" return [group for group in self.model.groups if self in group] + @property def _material_group(self): for group in self.groups: if "mat:" in group.name: return group return None - + @property def material(self) -> Optional[str]: """Name of the material assigned to this volume.""" @@ -480,7 +495,7 @@ def material(self, name: str): def surfaces(self): """Returns surface objects for all surfaces making up this vollume""" return [Surface(self.model, h) for h in self.model.mb.get_child_meshsets(self.handle)] - + @property def surfaces_by_id(self): return {s.id: s for s in self.surfaces} @@ -508,6 +523,7 @@ def volume(self): volume += sign * sum return volume / 6.0 + class Group(DAGSet): _category: str = 'Group' @@ -563,7 +579,7 @@ def _get_geom_ent_ids(self, entity_type): def volumes(self): """Returns a list of Volume objects for the volumes contained by the group set.""" return [Volume(self.model, v) for v in self._get_geom_ent_sets('Volume')] - + @property def volumes_by_id(self): return {v.id: v for v in self.volumes} @@ -572,7 +588,7 @@ def volumes_by_id(self): def surfaces(self): """Returns a list of Surface objects for the surfaces contained by the group set.""" return [Surface(self.model, s) for s in self._get_geom_ent_sets('Surface')] - + @property def surfaces_by_id(self): return {s.id: s for s in self.surfaces} @@ -633,7 +649,7 @@ def merge(self, other_group): @classmethod def create(cls, model: DAGModel, name: Optional[str] = None, group_id: Optional[int] = None) -> Group: - """Create a new group instance with the given name, + """Create a new group instance with the given name, or return an existing group if one exists.""" # return existing group if one exists with this name diff --git a/test/test_basic.py b/test/test_basic.py index ce17579..d6efa1f 100644 --- a/test/test_basic.py +++ b/test/test_basic.py @@ -121,10 +121,15 @@ def test_volume(request): v1.material = 'olive oil' assert v1.material == 'olive oil' - assert 'mat:olive oil' in model.groups + assert 'mat:olive oil' in model.groups_by_name assert v1 in model.groups_by_name['mat:olive oil'] assert v1 not in model.groups_by_name['mat:fuel'] + new_vol = dagmc.Volume.create(model, 100) + assert isinstance(new_vol, dagmc.Volume) + assert new_vol.id == 100 + assert model.volumes_by_id[100] == new_vol + def test_surface(request): test_file = str(request.path.parent / 'fuel_pin.h5m') @@ -152,7 +157,7 @@ def test_id_safety(request): used_vol_id = 2 with pytest.raises(ValueError, match="already"): v1.id = used_vol_id - + safe_vol_id = 9876 v1.id = safe_vol_id assert v1.id == safe_vol_id @@ -162,7 +167,7 @@ def test_id_safety(request): used_surf_id = 2 with pytest.raises(ValueError, match="already"): s1.id = used_surf_id - + safe_surf_id = 9876 s1.id = safe_surf_id assert s1.id == safe_surf_id @@ -172,12 +177,17 @@ def test_id_safety(request): used_grp_id = 2 with pytest.raises(ValueError, match="already"): g1.id = used_grp_id - + safe_grp_id = 9876 g1.id = safe_grp_id assert g1.id == safe_grp_id + new_surf = dagmc.Surface.create(model, 100) + assert isinstance(new_surf, dagmc.Surface) + assert new_surf.id == 100 + assert model.surfaces_by_id[100] == new_surf + def test_hash(request): test_file = str(request.path.parent / 'fuel_pin.h5m') @@ -199,7 +209,6 @@ def test_compressed_coords(request, capfd): fuel_group = groups['mat:fuel'] v1 = fuel_group.volumes_by_id[1] - print(v1) conn, coords = v1.get_triangle_conn_and_coords() uconn, ucoords = v1.get_triangle_conn_and_coords(compress=True) @@ -333,7 +342,7 @@ def test_write(request, tmpdir): assert 12345 in model.volumes_by_id -def test_volume(request): +def test_volume_value(request): test_file = str(request.path.parent / 'fuel_pin.h5m') model = dagmc.DAGModel(test_file) exp_vols = {1: np.pi * 7**2 * 40,