Fem: Use regex for gmsh groups items

This commit is contained in:
marioalexis
2024-08-25 18:50:52 -03:00
committed by Yorik van Havre
parent b3cc37b2f1
commit 8d622e02ae
2 changed files with 33 additions and 37 deletions

View File

@@ -28,6 +28,7 @@ __url__ = "https://www.freecad.org"
# @{
import os
import re
import subprocess
import FreeCAD
@@ -644,44 +645,39 @@ class GmshTools:
Console.PrintMessage(f" {self.bl_setting_list}\n")
def write_groups(self, geo):
# find shape type and index from group elements and isolate them from possible prefix
# for example: "PartObject.Solid2" -> shape: Solid, index: 2
# we use the element index of FreeCAD which starts with 1 (example: "Face1"),
# same as Gmsh. For unit test we need them to have a fixed order
reg_exp = re.compile(r"(?:.*\.)?(?P<shape>Solid|Face|Edge|Vertex)(?P<index>\d+)$")
if self.group_elements:
# print(" We are going to have to find elements to make mesh groups for.")
geo.write("// group data\n")
# we use the element name of FreeCAD which starts
# with 1 (example: "Face1"), same as Gmsh
# for unit test we need them to have a fixed order
for group in sorted(self.group_elements):
gdata = self.group_elements[group]
# print(gdata)
# geo.write("// " + group + "\n")
ele_nr = ""
if gdata[0].startswith("Solid"):
physical_type = "Volume"
for ele in gdata:
ele_nr += ele.lstrip("Solid") + ", "
elif gdata[0].startswith("Face"):
physical_type = "Surface"
for ele in gdata:
ele_nr += ele.lstrip("Face") + ", "
elif gdata[0].startswith("Edge"):
physical_type = "Line"
for ele in gdata:
ele_nr += ele.lstrip("Edge") + ", "
elif gdata[0].startswith("Vertex"):
physical_type = "Point"
for ele in gdata:
ele_nr += ele.lstrip("Vertex") + ", "
if ele_nr:
ele_nr = ele_nr.rstrip(", ")
# print(ele_nr)
curly_br_s = "{"
curly_br_e = "}"
# explicit use double quotes in geo file
geo.write(
'Physical {}("{}") = {}{}{};\n'.format(
physical_type, group, curly_br_s, ele_nr, curly_br_e
)
)
ele = {"Volume": [], "Surface": [], "Line": [], "Point": []}
for i in gdata:
m = reg_exp.match(i)
if m:
shape = m.group("shape")
index = str(m.group("index"))
if shape == "Solid":
ele["Volume"].append(index)
elif shape == "Face":
ele["Surface"].append(index)
elif shape == "Edge":
ele["Line"].append(index)
elif shape == "Vertex":
ele["Point"].append(index)
for phys in ele:
if ele[phys]:
name = group + "_{}".format(phys)
items = "{" + ", ".join(ele[phys]) + "}"
geo.write('Physical {}("{}") = {};\n'.format(phys, name, items))
geo.write("\n")
def write_boundary_layer(self, geo):

View File

@@ -7,10 +7,10 @@ General.NumThreads = X;
Merge "tmp0TVZbM.brep";
// group data
Physical Surface("Face1") = {1};
Physical Surface("Face2") = {2};
Physical Surface("Face6") = {6};
Physical Volume("Solid1") = {1};
Physical Surface("Face1_Surface") = {1};
Physical Surface("Face2_Surface") = {2};
Physical Surface("Face6_Surface") = {6};
Physical Volume("Solid1_Volume") = {1};
// Characteristic Length
// no boundary layer settings for this mesh