Skip to content

Commit

Permalink
testing meshing
Browse files Browse the repository at this point in the history
  • Loading branch information
d2f4d131d9eac6cc27e3d6245ab1476b committed Mar 22, 2024
1 parent 144a7b1 commit 47cccf6
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 151 deletions.
49 changes: 27 additions & 22 deletions src/irrevolutions/meshes/test_fuse.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import os
import gmsh
import pytest

# initialise gmsh engine
gmsh.initialize()
# Define the test parameters
TEST_CASES = [
{"name": "minimal_2d", "units": 1, "expected_mesh_size": 2}
# Add more test cases if needed
]

# assign name to geomtry
gmsh.model.add("simple_2d")
@pytest.mark.parametrize("test_case", TEST_CASES)
def test_generate_2d_mesh(test_case):
# Run the script
gmsh.initialize()
gmsh.model.add("simple_2d")
units = test_case["units"]
rectangle_1 = gmsh.model.occ.addRectangle(0.0, 0.0, 0.0, units, 0.5*units, tag=1)
rectangle_2 = gmsh.model.occ.addRectangle(0.0, 0.5*units, 0.0, units, 0.5*units, tag=2)
rectangles = gmsh.model.occ.fuse([(2,1)], [(2,2)], tag=3, removeObject=True, removeTool=True)
gmsh.model.occ.synchronize()
gmsh.model.addPhysicalGroup(2, [3], 1)
gmsh.model.setPhysicalName(2, 1, "rectangles")
gmsh.model.mesh.generate(1)
gmsh.model.mesh.generate(2)

units = 1
rectangle_1 = gmsh.model.occ.addRectangle(0.0, 0.0, 0.0, units, 0.5*units, tag=1)
rectangle_2 = gmsh.model.occ.addRectangle(0.0, 0.5*units, 0.0, units, 0.5*units, tag=2)
# link both domains and remove old rectangles
rectangles = gmsh.model.occ.fuse([(2,1)], [(2,2)], tag=3, removeObject=True, removeTool=True)
# Verify the generated mesh
mesh_file_path = os.path.join(os.path.dirname(__file__), "output", f"{test_case['name']}_mesh.msh")
gmsh.write(mesh_file_path)

# sycrhonise geometry with gmsh
gmsh.model.occ.synchronize()
assert os.path.isfile(mesh_file_path)

# create groups
gmsh.model.addPhysicalGroup(2, [3], 1)
gmsh.model.setPhysicalName(2, 1, "rectangles")

# gnerate 2D mesh, write mesh and convert to xdmf
gmsh.model.mesh.generate(1)
gmsh.model.mesh.generate(2)
gmsh.write("mesh_2d_minimal.msh")

# finalize gmsh engine
gmsh.finalize()
# Clean up
gmsh.finalize()
3 changes: 2 additions & 1 deletion src/irrevolutions/meshes/test_occ_2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gmsh
import os

gmsh.initialize()

Expand Down Expand Up @@ -33,7 +34,7 @@
gmsh.model.mesh.generate(2)

# Write the mesh file
gmsh.write("mesh.msh")
gmsh.write(os.path.join(os.path.dirname(__file__), "mesh.msh"))

# Finalize Gmsh
gmsh.finalize()
71 changes: 42 additions & 29 deletions src/irrevolutions/meshes/test_occ_crack.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,56 @@
import gmsh
import sys
import os
import pytest

gmsh.initialize(sys.argv)
def generate_square_with_cracks():

gmsh.model.add("square with cracks")
gmsh.initialize(sys.argv)

surf1 = 1
gmsh.model.occ.addRectangle(0, 0, 0, 1, 1, surf1)
gmsh.model.add("square with cracks")

pt1 = gmsh.model.occ.addPoint(0.2, 0.2, 0)
pt2 = gmsh.model.occ.addPoint(0.4, 0.4, 0)
line1 = gmsh.model.occ.addLine(pt1, pt2)
pt3 = gmsh.model.occ.addPoint(0.4, 0.4, 0)
pt4 = gmsh.model.occ.addPoint(0.4, 0.9, 0)
line2 = gmsh.model.occ.addLine(pt3, pt4)
surf1 = 1
gmsh.model.occ.addRectangle(0, 0, 0, 1, 1, surf1)

o, m = gmsh.model.occ.fragment([(2, surf1)], [(1, line1), (1, line2)])
gmsh.model.occ.synchronize()
pt1 = gmsh.model.occ.addPoint(0.2, 0.2, 0)
pt2 = gmsh.model.occ.addPoint(0.4, 0.4, 0)
line1 = gmsh.model.occ.addLine(pt1, pt2)
pt3 = gmsh.model.occ.addPoint(0.4, 0.4, 0)
pt4 = gmsh.model.occ.addPoint(0.4, 0.9, 0)
line2 = gmsh.model.occ.addLine(pt3, pt4)

# m contains, for each input entity (surf1, line1 and line2), the child entities
# (if any) after the fragmentation, as lists of tuples. To apply the crack
# plugin we group all the intersecting lines in a physical group
o, m = gmsh.model.occ.fragment([(2, surf1)], [(1, line1), (1, line2)])
gmsh.model.occ.synchronize()

new_surf = m[0][0][1]
new_lines = [item[1] for sublist in m[1:] for item in sublist]
# m contains, for each input entity (surf1, line1 and line2), the child entities
# (if any) after the fragmentation, as lists of tuples. To apply the crack
# plugin we group all the intersecting lines in a physical group

gmsh.model.addPhysicalGroup(2, [new_surf], 100)
gmsh.model.addPhysicalGroup(1, new_lines, 101)
new_surf = m[0][0][1]
new_lines = [item[1] for sublist in m[1:] for item in sublist]

gmsh.model.mesh.generate(2)
gmsh.model.addPhysicalGroup(2, [new_surf], 100)
gmsh.model.addPhysicalGroup(1, new_lines, 101)

gmsh.plugin.setNumber("Crack", "Dimension", 1)
gmsh.plugin.setNumber("Crack", "PhysicalGroup", 101)
gmsh.plugin.setNumber("Crack", "DebugView", 1)
gmsh.plugin.run("Crack")
gmsh.model.mesh.generate(2)

# save all the elements in the mesh (even those that do not belong to any
# physical group):
gmsh.option.setNumber("Mesh.SaveAll", 1)
gmsh.write("crack.msh")
gmsh.plugin.setNumber("Crack", "Dimension", 1)
gmsh.plugin.setNumber("Crack", "PhysicalGroup", 101)
gmsh.plugin.setNumber("Crack", "DebugView", 1)
gmsh.plugin.run("Crack")

gmsh.finalize()
# save all the elements in the mesh (even those that do not belong to any
# physical group):
gmsh.option.setNumber("Mesh.SaveAll", 1)
output_file = os.path.join(os.path.dirname(__file__), "output", "crack.msh")

gmsh.finalize()

return output_file

def test_generate_square_with_cracks():
output_file = generate_square_with_cracks()
assert os.path.isfile(output_file)

if __name__ == "__main__":
pytest.main(args=[__file__])
39 changes: 24 additions & 15 deletions src/irrevolutions/meshes/test_occ_pacman.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import gmsh
import numpy as np

import os
import pytest
import sys

def create_triangle_with_angle(opening_deg, rotation = 0):

gmsh.initialize(sys.argv)

# Create a new model
gmsh.model.add("triangle_with_angle")

Expand Down Expand Up @@ -35,6 +39,12 @@ def create_triangle_with_angle(opening_deg, rotation = 0):

domain = gmsh.model.occ.rotate([(2, triangle_surface)], *p0, *_rotation_axis, angle_radians)

gmsh.model.mesh.generate(2)
output_file = os.path.join(os.path.dirname(__file__), "output", "triangle_with_angle.msh")
gmsh.write(output_file)

gmsh.finalize()
return output_file
# gmsh.model.occ.rotate([(2, triangle_surface)], base_point1, 0, 0, 1, angle_radians)

# Generate mesh
Expand Down Expand Up @@ -64,7 +74,6 @@ def create_pacman(opening_deg, rotation = 0):
base_point2 = gmsh.model.occ.addPoint(adjacent_length, 0, 0)
base_line = gmsh.model.occ.addLine(base_point1, base_point2)


# Define the vertex of the triangle
vertex_point = gmsh.model.occ.addPoint(adjacent_length * np.cos(angle_radians),
adjacent_length * np.sin(angle_radians), 0)
Expand Down Expand Up @@ -116,20 +125,20 @@ def create_pacman(opening_deg, rotation = 0):
occ.synchronize()

gmsh.model.mesh.generate(2)
# Write the mesh file
gmsh.write("test_occ.msh")
output_file = os.path.join(os.path.dirname(__file__), "output", "pacman.msh")
gmsh.write(output_file)

gmsh.finalize()
return output_file

# Finalize Gmsh
return True

# Example: Create a triangle with a 45-degree angle
def test_create_triangle_with_angle():
output_file = create_triangle_with_angle(opening_deg=30, rotation=0)
assert os.path.isfile(output_file)

def test_create_pacman():
output_file = create_pacman(opening_deg=30, rotation=180-30/2)
assert os.path.isfile(output_file)

if __name__ == "__main__":
gmsh.initialize()

# domain = create_triangle_with_angle(opening_deg = 30, rotation = 0)
domain = create_pacman(opening_deg = 30, rotation = 180-30/2)

gmsh.finalize()

pass
pytest.main(args=[__file__])
Loading

0 comments on commit 47cccf6

Please sign in to comment.