|
1 | | -from festim import AverageSurface, AverageSurfaceCylindrical, AverageSurfaceSpherical, x |
| 1 | +from festim import ( |
| 2 | + AverageSurface, |
| 3 | + AverageSurfaceCylindrical, |
| 4 | + AverageSurfaceSpherical, |
| 5 | + x, |
| 6 | + y, |
| 7 | +) |
2 | 8 | import fenics as f |
3 | 9 | import pytest |
4 | 10 | import numpy as np |
@@ -174,3 +180,60 @@ def test_average_surface_spherical_title_no_units_temperature(): |
174 | 180 |
|
175 | 181 | my_export = AverageSurfaceSpherical("T", 9) |
176 | 182 | assert my_export.title == "Average T surface 9 (K)" |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.parametrize("j", [4, 5, 3]) |
| 186 | +def test_average_surface_rhombus(j): |
| 187 | + |
| 188 | + # creating a mesh with FEniCS |
| 189 | + # Define the number of divisions in the x and y directions |
| 190 | + nx, ny = 10, 10 |
| 191 | + mesh = f.RectangleMesh(f.Point(0, 0), f.Point(j, j), nx, ny) |
| 192 | + |
| 193 | + # Access mesh coordinates |
| 194 | + coordinates = mesh.coordinates() |
| 195 | + |
| 196 | + # Rotation matrix for 45 degrees |
| 197 | + theta = np.pi / 4 # 45 degrees in radians |
| 198 | + rotation_matrix = np.array( |
| 199 | + [[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]] |
| 200 | + ) |
| 201 | + |
| 202 | + # Apply rotation to each coordinate |
| 203 | + for i in range(len(coordinates)): |
| 204 | + x_cord, y_cord = coordinates[i] |
| 205 | + # Rotate the point (x, y) using the rotation matrix |
| 206 | + new_coords = np.dot(rotation_matrix, np.array([x_cord, y_cord])) |
| 207 | + coordinates[i] = new_coords |
| 208 | + |
| 209 | + # Create a new mesh with the rotated coordinates |
| 210 | + mesh_rotated = f.Mesh(mesh) |
| 211 | + |
| 212 | + surface_markers = f.MeshFunction( |
| 213 | + "size_t", mesh_rotated, mesh_rotated.topology().dim() - 1 |
| 214 | + ) |
| 215 | + surface_markers.set_all(0) |
| 216 | + |
| 217 | + # find all facets along y = x and mark them |
| 218 | + surf_id = 2 |
| 219 | + for facet in f.facets(mesh): |
| 220 | + midpoint = facet.midpoint() |
| 221 | + if np.isclose(midpoint[0], midpoint[1], atol=1e-10): |
| 222 | + surface_markers[facet.index()] = surf_id |
| 223 | + |
| 224 | + ds = f.Measure("ds", domain=mesh_rotated, subdomain_data=surface_markers) |
| 225 | + my_export = AverageSurface("solute", surf_id) |
| 226 | + V = f.FunctionSpace(mesh_rotated, "P", 1) |
| 227 | + |
| 228 | + c_fun = lambda x, y: 2 * x + y |
| 229 | + expr = f.Expression( |
| 230 | + ccode(c_fun(x, y)), |
| 231 | + degree=1, |
| 232 | + ) |
| 233 | + my_export.function = f.interpolate(expr, V) |
| 234 | + my_export.ds = ds |
| 235 | + |
| 236 | + expected_value = (3 * j) / (2 * (2**0.5)) |
| 237 | + computed_value = my_export.compute() |
| 238 | + |
| 239 | + assert np.isclose(computed_value, expected_value) |
0 commit comments