Skip to content

Commit

Permalink
Merge pull request #10 from svalinn/areas-vols
Browse files Browse the repository at this point in the history
Adding area and volume methods and tests
  • Loading branch information
gonuke authored Feb 16, 2024
2 parents e596565 + 76c1a26 commit 43122c1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions dagmc/dagnav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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):

Expand Down
29 changes: 29 additions & 0 deletions test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import urllib.request

import pytest
import numpy as np

from test import config

from pymoab import core
Expand Down Expand Up @@ -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)

0 comments on commit 43122c1

Please sign in to comment.