From 76c1a26f26c63439dfac57dd46c6100691ad2182 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 15 Feb 2024 23:52:32 -0600 Subject: [PATCH] Adding area and volume methods and tests --- dagmc/dagnav.py | 24 ++++++++++++++++++++++++ test/test_basic.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/dagmc/dagnav.py b/dagmc/dagnav.py index 8327600..3963f58 100644 --- a/dagmc/dagnav.py +++ b/dagmc/dagnav.py @@ -370,6 +370,16 @@ def num_triangles(self): def _get_triangle_sets(self): return [self] + @property + def area(self): + """Returns the area of the surface""" + conn, coords = self.get_triangle_conn_and_coords() + sum = 0.0 + for _conn in conn: + tri_coords = coords[_conn] + sum += np.linalg.norm(np.cross(tri_coords[1] - tri_coords[0], tri_coords[2] - tri_coords[0])) + return 0.5 * sum + class Volume(DAGSet): @@ -427,6 +437,20 @@ def num_triangles(self): def _get_triangle_sets(self): return [s.handle for s in self.get_surfaces().values()] + @property + def volume(self): + """Returns the volume of the volume""" + volume = 0.0 + for surface in self.get_surfaces().values(): + conn, coords = surface.get_triangle_conn_and_coords() + sum = 0.0 + for _conn in conn: + tri_coords = coords[_conn] + c = np.cross(tri_coords[1] - tri_coords[0], tri_coords[2] - tri_coords[0]) + sum += np.dot(c, tri_coords[0]) + sign = 1 if surface.forward_volume == self else -1 + volume += sign * sum + return volume / 6.0 class Group(DAGSet): diff --git a/test/test_basic.py b/test/test_basic.py index be68861..e10240d 100644 --- a/test/test_basic.py +++ b/test/test_basic.py @@ -2,6 +2,8 @@ import urllib.request import pytest +import numpy as np + from test import config from pymoab import core @@ -297,3 +299,30 @@ def test_write(request, tmpdir): model = dagmc.DAGModel('fuel_pin_copy.h5m') assert 12345 in model.volumes + + +def test_volume(request): + test_file = str(request.path.parent / 'fuel_pin.h5m') + model = dagmc.DAGModel(test_file) + exp_vols = {1: np.pi * 7**2 * 40, + 2: np.pi * (9**2 - 7**2) * 40, + 3: np.pi * (10**2 - 9**2) * 40,} + pytest.approx(model.volumes[1].volume, exp_vols[1]) + pytest.approx(model.volumes[2].volume, exp_vols[2]) + pytest.approx(model.volumes[3].volume, exp_vols[3]) + + +def test_area(request): + test_file = str(request.path.parent / 'fuel_pin.h5m') + model = dagmc.DAGModel(test_file) + exp_areas = {1: 2 * np.pi * 7 * 40, + 2: np.pi * 7**2, + 3: np.pi * 7**2, + 5: 2 * np.pi * 9 * 40, + 6: np.pi * 9**2, + 7: np.pi * 9**2, + 9: 2 * np.pi * 10 * 40, + 10: np.pi * 10**2, + 11: np.pi * 10**2 } + for surf_id, exp_area in exp_areas.items(): + pytest.approx(model.surfaces[surf_id].area, exp_area) \ No newline at end of file