Skip to content

Commit

Permalink
[Bug] Fix area calculations for STLs (#205)
Browse files Browse the repository at this point in the history
* fix the area calculations

* Increase tolerance
  • Loading branch information
ktangsali authored Nov 15, 2024
1 parent 42d8afb commit e27baef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix the area calculated for STL meshes.

### Security

### Dependencies
Expand Down
40 changes: 13 additions & 27 deletions modulus/sym/geometry/tessellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,17 @@ def sample(
invar["normal_z"].append(
np.full(x.shape, mesh.normals[index, 2]) / normal_scale
)
invar["area"].append(
np.full(x.shape, triangle_areas[index] / x.shape[0])
)

invar["x"] = np.concatenate(invar["x"], axis=0)
invar["y"] = np.concatenate(invar["y"], axis=0)
invar["z"] = np.concatenate(invar["z"], axis=0)
invar["normal_x"] = np.concatenate(invar["normal_x"], axis=0)
invar["normal_y"] = np.concatenate(invar["normal_y"], axis=0)
invar["normal_z"] = np.concatenate(invar["normal_z"], axis=0)
invar["area"] = np.concatenate(invar["area"], axis=0)
# Compute area from the original mesh
invar["area"] = np.ones_like(invar["x"]) * (
np.sum(triangle_areas) / nr_points
)

# sample from the param ranges
params = parameterization.sample(nr_points, quasirandom=quasirandom)
Expand Down Expand Up @@ -234,29 +235,14 @@ def _sample_triangle(


# area of array of triangles
def _area_of_triangles(
v0, v1, v2
): # ref https://math.stackexchange.com/questions/128991/how-to-calculate-the-area-of-a-3d-triangle
a = np.sqrt(
(v0[:, 0] - v1[:, 0]) ** 2
+ (v0[:, 1] - v1[:, 1]) ** 2
+ (v0[:, 2] - v1[:, 2]) ** 2
+ 1e-10
)
b = np.sqrt(
(v1[:, 0] - v2[:, 0]) ** 2
+ (v1[:, 1] - v2[:, 1]) ** 2
+ (v1[:, 2] - v2[:, 2]) ** 2
+ 1e-10
)
c = np.sqrt(
(v0[:, 0] - v2[:, 0]) ** 2
+ (v0[:, 1] - v2[:, 1]) ** 2
+ (v0[:, 2] - v2[:, 2]) ** 2
+ 1e-10
)
s = (a + b + c) / 2
area = np.sqrt(s * (s - a) * (s - b) * (s - c) + 1e-10)
def _area_of_triangles(v0, v1, v2):
edge1 = v1 - v0
edge2 = v2 - v0

# use cross product to find the area of the triangle
cross_product = np.cross(edge1, edge2)
area = np.linalg.norm(cross_product, axis=1) / 2

return area


Expand Down
12 changes: 9 additions & 3 deletions test/test_pdes/test_linear_elasticity.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,15 @@ def test_linear_elasticity_equations():
assert np.allclose(traction_x_eval_pred, traction_x_true), "Test Failed!"
assert np.allclose(traction_y_eval_pred, traction_y_true), "Test Failed!"
assert np.allclose(traction_z_eval_pred, traction_z_true), "Test Failed!"
assert np.allclose(navier_x_eval_pred, navier_x_true, rtol=1e-3), "Test Failed!"
assert np.allclose(navier_y_eval_pred, navier_y_true, rtol=1e-3), "Test Failed!"
assert np.allclose(navier_z_eval_pred, navier_z_true, rtol=1e-3), "Test Failed!"
assert np.allclose(
navier_x_eval_pred, navier_x_true, atol=1e-3, rtol=1e-2
), "Test Failed!"
assert np.allclose(
navier_y_eval_pred, navier_y_true, atol=1e-3, rtol=1e-2
), "Test Failed!"
assert np.allclose(
navier_z_eval_pred, navier_z_true, atol=1e-3, rtol=1e-2
), "Test Failed!"


def test_linear_elasticity_plane_stress_equations():
Expand Down

0 comments on commit e27baef

Please sign in to comment.