Salome HOME
Removin argument from ParallelMesh init
authorYoann Audouin <yoann.audouin@edf.fr>
Wed, 11 Jan 2023 15:47:11 +0000 (16:47 +0100)
committerYoann Audouin <yoann.audouin@edf.fr>
Wed, 11 Jan 2023 15:47:11 +0000 (16:47 +0100)
src/SMESH_SWIG/smeshBuilder.py
test/SMESH_ParallelCompute.py

index 1b84f446216787a73d3ad3aea86d8bdfcaa0de98..f00c620839d463ff921f282f99bbe06edc04d4b7 100644 (file)
@@ -462,20 +462,21 @@ class smeshBuilder( SMESH._objref_SMESH_Gen, object ):
             obj,name = name,obj
         return Mesh(self, self.geompyD, obj, name)
 
-    def ParallelMesh(self, obj, param, nbThreads, name=0):
+    def ParallelMesh(self, obj, name=0, split_geom=True):
         """
         Create a parallel mesh.
 
         Parameters:
             obj: geometrical object for meshing
             name: the name for the new mesh.
-            param: full mesh parameters
-            nbThreads: Number of threads for parallelisation.
+            split_geom: If True split the geometry and create the assoicated
+            sub meshes
 
         Returns:
             an instance of class :class:`ParallelMesh`.
         """
-        return ParallelMesh(self, self.geompyD, obj, param, nbThreads, name)
+        return ParallelMesh(self, self.geompyD, obj,
+                            split_geom=split_geom, name=name)
 
     def RemoveMesh( self, mesh ):
         """
@@ -7504,6 +7505,9 @@ class Mesh(metaclass = MeshMeta):
 
 
 def _copy_netgen_param(dim, local_param, global_param):
+    """
+    Create 1D/2D/3D netgen parameters from a NETGEN 1D2D3D parameter
+    """
     if dim==1:
         #TODO: Try to identify why we need to substract 1
         local_param.NumberOfSegments(int(global_param.GetNbSegPerEdge())-1)
@@ -7535,62 +7539,66 @@ def _copy_netgen_param(dim, local_param, global_param):
         local_param.SetGrowthRate(global_param.GetGrowthRate())
         local_param.SetNbThreads(global_param.GetNbThreads())
 
-class ParallelMesh(Mesh):
-    """
-    Surcharge on Mesh for parallel computation of a mesh
+def _split_geom(geompyD, geom):
     """
+    Splitting geometry into n solids and a 2D/1D compound
 
-    def __split_geom(self, geompyD, geom):
-        """
-        Splitting geometry into n solids and a 2D/1D compound
-
-        Parameters:
-            geom: geometrical object for meshing
-
-        """
-        # Splitting geometry into 3D elements and all the 2D/1D into one compound
-        object_solids = geompyD.ExtractShapes(geom, geompyD.ShapeType["SOLID"],
-                                              True)
-
-        solids = []
-        isolid = 0
-        for solid in object_solids:
-            isolid += 1
-            geompyD.addToStudyInFather( geom, solid, 'Solid_{}'.format(isolid) )
-            solids.append(solid)
-        # If geom is a solid ExtractShapes will return nothin in that case geom is the solids
-        if isolid == 0:
-           solids = [geom]
-
-        faces = []
-        iface = 0
-        for isolid, solid in enumerate(solids):
-            solid_faces = geompyD.ExtractShapes(solid, geompyD.ShapeType["FACE"],
-                                                True)
-            for face in solid_faces:
-                faces.append(face)
-                iface += 1
-                geompyD.addToStudyInFather(solid, face,
-                                           'Face_{}'.format(iface))
+    Parameters:
+        geompyD: geomBuilder instance
+        geom: geometrical object for meshing
 
-        # Creating submesh for edges 1D/2D part
+    """
+    # Splitting geometry into 3D elements and all the 2D/1D into one compound
+    object_solids = geompyD.ExtractShapes(geom, geompyD.ShapeType["SOLID"],
+                                          True)
+
+    solids = []
+    isolid = 0
+    for solid in object_solids:
+        isolid += 1
+        geompyD.addToStudyInFather( geom, solid, 'Solid_{}'.format(isolid) )
+        solids.append(solid)
+    # If geom is a solid ExtractShapes will return nothin in that case geom is the solids
+    if isolid == 0:
+       solids = [geom]
+
+    faces = []
+    iface = 0
+    for isolid, solid in enumerate(solids):
+        solid_faces = geompyD.ExtractShapes(solid, geompyD.ShapeType["FACE"],
+                                            True)
+        for face in solid_faces:
+            faces.append(face)
+            iface += 1
+            geompyD.addToStudyInFather(solid, face,
+                                       'Face_{}'.format(iface))
+
+    # Creating submesh for edges 1D/2D part
+
+    all_faces = geompyD.MakeCompound(faces)
+    geompyD.addToStudy(all_faces, 'Compound_1')
+    all_faces = geompyD.MakeGlueEdges(all_faces, 1e-07)
+    all_faces = geompyD.MakeGlueFaces(all_faces, 1e-07)
+    geompyD.addToStudy(all_faces, 'global2D')
+
+    return all_faces, solids
 
-        all_faces = geompyD.MakeCompound(faces)
-        geompyD.addToStudy(all_faces, 'Compound_1')
-        all_faces = geompyD.MakeGlueEdges(all_faces, 1e-07)
-        all_faces = geompyD.MakeGlueFaces(all_faces, 1e-07)
-        geompyD.addToStudy(all_faces, 'global2D')
+class ParallelMesh(Mesh):
+    """
+    Surcharge on Mesh for parallel computation of a mesh
+    """
 
-        return all_faces, solids
 
-    def __init__(self, smeshpyD, geompyD, geom, param, nbThreads, name=0):
+    def __init__(self, smeshpyD, geompyD, geom, split_geom=True, name=0):
         """
         Create a parallel mesh.
 
         Parameters:
+            smeshpyD: instance of smeshBuilder
+            geompyD: instance of geomBuilder
             geom: geometrical object for meshing
-            param: full mesh parameters
-            nbThreads: Number of threads for parallelisation.
+            split_geom: If true will divide geometry on solids and 1D/2D
+            coumpund and create the associated submeshes
             name: the name for the new mesh.
 
         Returns:
@@ -7600,35 +7608,58 @@ class ParallelMesh(Mesh):
         if not isinstance(geom, geomBuilder.GEOM._objref_GEOM_Object):
             raise ValueError("geom argument must be a geometry")
 
-        if not isinstance(param, NETGENPlugin._objref_NETGENPlugin_Hypothesis):
-            raise ValueError("param must come from NETGENPlugin")
+        # Splitting geometry into one geom containing 1D and 2D elements and a
+        # list of 3D elements
+        super(ParallelMesh, self).__init__(smeshpyD, geompyD, geom, name, parallel=True)
 
-        if nbThreads < 1:
-            raise ValueError("Number of threads must be stricly greater than 1")
+        if split_geom:
+            self._all_faces, self._solids = _split_geom(geompyD, geom)
 
-        all_faces, solids = self.__split_geom(geompyD, geom)
+            self.UseExistingSegments()
+            self.UseExistingFaces()
 
-        super(ParallelMesh, self).__init__(smeshpyD, geompyD, geom, name, parallel=True)
+            self._algo2d = self.Triangle(geom=self._all_faces, algo="NETGEN_2D")
+            self._algo3d = []
 
-        self.mesh.SetNbThreads(nbThreads)
+            for solid_id, solid in enumerate(self._solids):
+                name = "Solid_{}".format(solid_id)
+                self.UseExistingSegments(geom=solid)
+                self.UseExistingFaces(geom=solid)
+                algo3d = self.Tetrahedron(geom=solid, algo="NETGEN_3D_Remote")
+                self._algo3d.append(algo3d)
 
-        self.UseExistingSegments()
-        self.UseExistingFaces()
 
-        algo2d = self.Triangle(geom=all_faces, algo="NETGEN_2D")
-        param2d = algo2d.Parameters()
+    def AddGlobalHypothesis(self, hyp):
+        """
+        Assign a hypothesis
 
-        _copy_netgen_param(2, param2d, param)
+        Parameters:
+                hyp: a hypothesis to assign
+
+        """
+        if not isinstance(hyp, NETGENPlugin._objref_NETGENPlugin_Hypothesis):
+            raise ValueError("param must come from NETGENPlugin")
 
-        for solid_id, solid in enumerate(solids):
-            name = "Solid_{}".format(solid_id)
-            self.UseExistingSegments(geom=solid)
-            self.UseExistingFaces(geom=solid)
-            algo3d = self.Tetrahedron(geom=solid, algo="NETGEN_3D_Remote")
+        param2d = self._algo2d.Parameters()
+        _copy_netgen_param(2, param2d, hyp)
+
+        for algo3d in self._algo3d:
 
             param3d = algo3d.Parameters()
+            _copy_netgen_param(3, param3d, hyp)
+
+    def SetNbThreads(self, nbThreads):
+        """
+        Define the number of threads for meshing
+
+        Parameters:
+            nbThreads: Number of threads
+        """
 
-            _copy_netgen_param(3, param3d, param)
+        if nbThreads < 1:
+            raise ValueError("Number of threads must be stricly greater than 1")
+
+        self.mesh.SetNbThreads(nbThreads)
 
     pass # End of ParallelMesh
 
index fccec13004cd413103483135123ac6fd80086c21..9cfc0ee8000c872906f251625ccaccfb7bc5622a 100644 (file)
@@ -98,7 +98,9 @@ def run_test(nbox=2, boxsize=100):
     """
     geom, seq_mesh, netgen_parameters = build_seq_mesh(nbox, boxsize, 0)
 
-    par_mesh = smesh.ParallelMesh(geom, netgen_parameters, 6, name="par_mesh")
+    par_mesh = smesh.ParallelMesh(geom, name="par_mesh")
+    par_mesh.AddGlobalHypothesis(netgen_parameters)
+    par_mesh.SetNbThreads(6)
 
     start = time.monotonic()
     is_done = seq_mesh.Compute()