-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
144a7b1
commit 47cccf6
Showing
5 changed files
with
190 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.