From 8d622e02ae9bced5ae0974bb8237ae5b1a78fee2 Mon Sep 17 00:00:00 2001 From: marioalexis Date: Sun, 25 Aug 2024 18:50:52 -0300 Subject: [PATCH] Fem: Use regex for gmsh groups items --- src/Mod/Fem/femmesh/gmshtools.py | 62 +++++++++---------- src/Mod/Fem/femtest/data/elmer/group_mesh.geo | 8 +-- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/Mod/Fem/femmesh/gmshtools.py b/src/Mod/Fem/femmesh/gmshtools.py index 523746b5bf..c03250cd5a 100644 --- a/src/Mod/Fem/femmesh/gmshtools.py +++ b/src/Mod/Fem/femmesh/gmshtools.py @@ -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"(?:.*\.)?(?PSolid|Face|Edge|Vertex)(?P\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): diff --git a/src/Mod/Fem/femtest/data/elmer/group_mesh.geo b/src/Mod/Fem/femtest/data/elmer/group_mesh.geo index 809ee15158..7e744dcc45 100644 --- a/src/Mod/Fem/femtest/data/elmer/group_mesh.geo +++ b/src/Mod/Fem/femtest/data/elmer/group_mesh.geo @@ -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