+def _copy_gmsh_param(dim, local_param, global_param):
+ if dim==3:
+ local_param.SetMaxSize(global_param.GetMaxSize())
+ local_param.SetMinSize(global_param.GetMinSize())
+ local_param.Set3DAlgo(global_param.Get3DAlgo())
+ local_param.SetRecombineAll(global_param.GetRecombineAll())
+ local_param.SetSubdivAlgo(global_param.GetSubdivAlgo())
+ local_param.SetRemeshAlgo(global_param.GetRemeshAlgo())
+ local_param.SetRemeshPara(global_param.GetRemeshPara())
+ local_param.SetSmouthSteps(global_param.GetSmouthSteps())
+ local_param.SetSizeFactor(global_param.GetSizeFactor())
+ local_param.SetUseIncomplElem(global_param.GetUseIncomplElem())
+ local_param.SetMeshCurvatureSize(global_param.GetMeshCurvatureSize())
+ local_param.SetSecondOrder(global_param.GetSecondOrder())
+ local_param.SetIs2d(global_param.GetIs2d())
+ elif dim==2:
+ local_param.SetMaxSize(global_param.GetMaxSize())
+ local_param.SetMinSize(global_param.GetMinSize())
+ local_param.Set2DAlgo(global_param.Get2DAlgo())
+ local_param.SetRecomb2DAlgo(global_param.GetRecomb2DAlgo())
+ local_param.SetRecombineAll(global_param.GetRecombineAll())
+ local_param.SetSubdivAlgo(global_param.GetSubdivAlgo())
+ local_param.SetRemeshAlgo(global_param.GetRemeshAlgo())
+ local_param.SetRemeshPara(global_param.GetRemeshPara())
+ local_param.SetSmouthSteps(global_param.GetSmouthSteps())
+ local_param.SetSizeFactor(global_param.GetSizeFactor())
+ local_param.SetUseIncomplElem(global_param.GetUseIncomplElem())
+ local_param.SetMeshCurvatureSize(global_param.GetMeshCurvatureSize())
+ local_param.SetSecondOrder(global_param.GetSecondOrder())
+ local_param.SetIs2d(global_param.GetIs2d())
+
+def _copy_netgen_param(dim, local_param, global_param):
+ """
+ Create 1D/2D/3D netgen parameters from a NETGEN 1D2D3D parameter
+ """
+ if dim==1:
+ #TODO: More global conversion ? or let user define it
+ local_param.NumberOfSegments(int(global_param.GetMaxSize()))
+ elif dim==2:
+ local_param.SetMaxSize(global_param.GetMaxSize())
+ local_param.SetMinSize(global_param.GetMinSize())
+ local_param.SetOptimize(global_param.GetOptimize())
+ local_param.SetFineness(global_param.GetFineness())
+ local_param.SetNbSegPerEdge(global_param.GetNbSegPerEdge())
+ local_param.SetNbSegPerRadius(global_param.GetNbSegPerRadius())
+ #TODO: Why the 0.9 to have same results
+ local_param.SetGrowthRate(global_param.GetGrowthRate()*0.9)
+ local_param.SetChordalError(global_param.GetChordalError())
+ local_param.SetChordalErrorEnabled(global_param.GetChordalErrorEnabled())
+ local_param.SetUseSurfaceCurvature(global_param.GetUseSurfaceCurvature())
+ local_param.SetUseDelauney(global_param.GetUseDelauney())
+ local_param.SetQuadAllowed(global_param.GetQuadAllowed())
+ local_param.SetWorstElemMeasure(global_param.GetWorstElemMeasure())
+ local_param.SetCheckChartBoundary(global_param.GetCheckChartBoundary())
+ local_param.SetNbThreads(global_param.GetNbThreads())
+ else:
+ local_param.SetMaxSize(global_param.GetMaxSize())
+ local_param.SetMinSize(global_param.GetMinSize())
+ local_param.SetOptimize(global_param.GetOptimize())
+ local_param.SetCheckOverlapping(global_param.GetCheckOverlapping())
+ local_param.SetCheckChartBoundary(global_param.GetCheckChartBoundary())
+ local_param.SetFineness(global_param.GetFineness())
+ local_param.SetNbSegPerEdge(global_param.GetNbSegPerEdge())
+ local_param.SetNbSegPerRadius(global_param.GetNbSegPerRadius())
+ local_param.SetGrowthRate(global_param.GetGrowthRate())
+ local_param.SetNbThreads(global_param.GetNbThreads())
+
+
+def _shaperstudy2geom(geompyD, shaper_obj):
+ """
+ Convertion of shaper object to geom object
+
+ Parameters:
+ geompyD: geomBuilder instance
+ shaper_obj: Shaper study object
+
+ Returns:
+ geom object
+
+ """
+ import tempfile
+ #Writing shaperstudy object into a brep file
+ fid, tmp_file = tempfile.mkstemp(suffix='.brep')
+ with open(fid, 'wb') as f:
+ f.write(shaper_obj.GetShapeStream())
+ # Reimporting brep file into geom
+ real_geom = geompyD.ImportBREP(tmp_file)
+ os.remove(tmp_file)
+
+ return real_geom
+
+
+def _split_geom(geompyD, geom):
+ """
+ Splitting geometry into n solids and a 2D/1D compound
+
+ Parameters:
+ geompyD: geomBuilder instance
+ geom: geometrical object for meshing
+
+ Returns:
+ compound containing all the 1D,2D elements
+ list of solids
+ """
+
+ # 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
+ solid_faces = geompyD.ExtractShapes(geom, geompyD.ShapeType["FACE"],
+ True)
+ for face in solid_faces:
+ faces.append(face)
+ iface += 1
+ geompyD.addToStudyInFather(geom, face,
+ 'Face_{}'.format(iface))
+
+ return faces, solids
+
+
+MULTITHREAD, MULTINODE = range(2)
+class ParallelismSettings:
+ """
+ Defines the parameters for the parallelism of ParallelMesh
+ """
+ def __init__(self, mesh):
+ """
+ Construsctor
+
+ Parameters:
+ mesh: Instance of ParallelMesh
+ """
+ if not(isinstance(mesh, ParallelMesh)):
+ raise ValueError("mesh should be a ParallelMesh")
+
+ self._mesh = mesh
+
+
+class MTParallelismSettings(ParallelismSettings):
+ """
+ Defines the parameters for the parallelism of ParallelMesh using MultiThreading
+ """
+ def __init__(self, mesh):
+ ParallelismSettings.__init__(self, mesh)
+
+ # Multithreading methods
+ def SetNbThreads(self, nbThreads):
+ """ Set the number of threads for multithread """
+ if nbThreads < 1:
+ raise ValueError("Number of threads must be stricly greater than 1")
+
+ self._mesh.mesh.SetNbThreads(nbThreads)
+
+ def GetNbThreads(self):
+ """ Get Number of threads """
+ return self._mesh.mesh.GetNbThreads()
+
+ def __str__(self):
+ """ str conversion """
+ string = "\nParameter for MultiThreading parallelism:\n"
+ string += "NbThreads: {}\n".format(self.GetNbThreads())
+
+ return string
+
+
+class MNParallelismSettings(ParallelismSettings):
+ """
+ Defines the parameters for the parallelism of ParallelMesh using MultiNodal
+ """
+ def __init__(self, mesh):
+ ParallelismSettings.__init__(self, mesh)
+
+ def GetResource(self):
+ """ Get the resource on which to run """
+ return self._mesh.mesh.GetResource()
+
+ def SetResource(self, resource):
+ """ Set the resource on which to run """
+ self._mesh.mesh.SetResource(resource)
+
+ def SetNbProc(self, nbProc):
+ """ Set the number of Processor for multinode """
+ if nbProc < 1:
+ raise ValueError("Number of Proc must be stricly greater than 1")
+ self._mesh.mesh.SetNbProc(nbProc)
+
+ def GetNbProc(self):
+ """ Get Number of Processor """
+ return self._mesh.mesh.GetNbProc()
+
+ def SetNbProcPerNode(self, nbProcPerNode):
+ """ Set the number of Processor Per Node for multinode """
+ if nbProcPerNode < 1:
+ raise ValueError("Number of Processor Per Node must be stricly greater than 1")
+
+ self._mesh.mesh.SetNbProcPerNode(nbProcPerNode)
+
+ def GetNbProcPerNode(self):
+ """ Get Number of Processor Per Node """
+ return self._mesh.mesh.GetNbProcPerNode()
+
+ def SetNbNode(self, nbNode):
+ """ Set the number of Node for multinode """
+ if nbNode < 1:
+ raise ValueError("Number of Node must be stricly greater than 1")
+ self._mesh.mesh.SetNbNode(nbNode)
+
+ def GetNbNode(self):
+ """ Get Number of Node """
+ return self._mesh.mesh.GetNbNode()
+
+ def SetWcKey(self, wcKey):
+ """ Set the number of Node for multinode """
+ self._mesh.mesh.SetWcKey(wcKey)
+
+ def GetWcKey(self):
+ """ Get Number of Node """
+ return self._mesh.mesh.GetWcKey()
+
+ def SetWalltime(self, walltime):
+ """ Set the number of Node for multinode """
+ self._mesh.mesh.SetWalltime(walltime)
+
+ def GetWalltime(self):
+ """ Get Number of Node """
+ return self._mesh.mesh.GetWalltime()
+
+ def __str__(self):
+ """ str conversion """
+ string = "\nParameter for MultiNode parallelism:\n"
+ string += "Reource: {}\n".format(self.GetResource())
+ string += "NbProc: {}\n".format(self.GetNbProc())
+ string += "NbProcPerNode: {}\n".format(self.GetNbProcPerNode())
+ string += "NbNode: {}\n".format(self.GetNbNode())
+ string += "WcKey: {}\n".format(self.GetWcKey())
+ string += "Walltime: {}\n".format(self.GetWalltime())
+
+ return string
+
+
+class ParallelMesh(Mesh):
+ """
+ Surcharge on Mesh for parallel computation of a mesh
+ """
+ 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
+ split_geom: If true will divide geometry on solids and 1D/2D
+ coumpound and create the associated submeshes
+ name: the name for the new mesh.
+
+ Returns:
+ an instance of class :class:`ParallelMesh`.
+ """
+
+ if not isinstance(geom, geomBuilder.GEOM._objref_GEOM_Object):
+ raise ValueError("geom argument must be a geometry")
+
+ try:
+ import SHAPERSTUDY
+ shaper_object = SHAPERSTUDY.SHAPERSTUDY_ORB._objref_SHAPER_Object
+ has_shaper = True
+ except ImportError:
+ shaper_object = int
+ has_shaper = False
+
+ # If we have a shaper object converting it into geom (temporary solution)
+ if isinstance(geom, shaper_object):
+ self._geom_obj = _shaperstudy2geom(geompyD, geom)
+ elif isinstance(geom, geomBuilder.GEOM._objref_GEOM_Object):
+ self._geom_obj = geom
+ else:
+ msg= ""
+ if not has_shaper:
+ msg = "\nShaper was not compiled"
+ raise Exception("Could not handle geom format {}.{} ".format(type(geom), msg))
+
+ # Splitting geometry into one geom containing 1D and 2D elements and a
+ # list of 3D elements
+ super(ParallelMesh, self).__init__(smeshpyD, geompyD, self._geom_obj, name, parallel=True)
+
+ if split_geom:
+ self._faces, self._solids = _split_geom(geompyD, self._geom_obj)
+
+ self._param = None
+
+ def _build_submeshes(self, mesher2D, mesher3D):
+ """
+ Contruct the submeshes for a parallel use of smesh
+
+ Parameters:
+ mesher2D: name of 2D mesher for 2D parallel compute (NETGEN)
+ mesher3D: name of 3D mesher for 3D parallel compute (NETGEN or
+ GMSH)
+ """
+
+ # Building global 2D mesher
+ if mesher3D:
+ if mesher3D == "NETGEN":
+ algo2D = "NETGEN_2D"
+ elif mesher3D == "GMSH":
+ algo2D = "GMSH_2D"
+ else:
+ raise ValueError("mesher3D should be either NETGEN or GMSH")
+
+ self._algo2d = self.Triangle(geom=self._geom_obj, algo=algo2D)
+
+ # Parallel 2D
+ if mesher2D:
+ #Means that we want to mesh face of solids in parallel and not
+ #the volume
+ self._algo2d = []
+ #For the moment use AutomaticLength based on finesse
+ # TODO: replace by input hypothesis
+ self._algo1d = self.Segment(geom=self._geom_obj)
+
+ for face_id, face in enumerate(self._faces):
+ name = "face_{}".format(face_id)
+ algo2d = self.Triangle(geom=face, algo="NETGEN_2D_Remote")
+ self._algo2d.append(algo2d)
+
+ if mesher3D:
+ self._algo3d = []
+ for solid_id, solid in enumerate(self._solids):
+ name = "Solid_{}".format(solid_id)
+ if ( mesher3D == "NETGEN" ):
+ algo3d = self.Tetrahedron(geom=solid, algo="NETGEN_3D_Remote")
+ self._algo3d.append(algo3d)
+ elif ( mesher3D == "GMSH" ):
+ algo3d = self.Tetrahedron(geom=solid, algo="GMSH_3D_Remote")
+ self._algo3d.append(algo3d)
+
+ def GetNbSolids(self):
+ """
+ Return the number of 3D solids
+ """
+ return len(self._solids)
+
+ def GetNbFaces(self):
+ """
+ Return the number of 2D faces
+ """
+ return len(self._faces)
+
+ def GetParallelismMethod(self):
+ """ Get the parallelims method """
+ return self.mesh.GetParallelismMethod()
+
+ def SetParallelismMethod(self, method):
+ """ Set the parallelims method """
+ if method not in [MULTITHREAD , MULTINODE]:
+ raise ValueError("Parallelism method can only be 0:MultiThread or 1:MultiNode")
+
+ self.mesh.SetParallelismMethod(method)
+
+ if method == MULTITHREAD:
+ self._param = MTParallelismSettings(self)
+ else:
+ self._param = MNParallelismSettings(self)
+
+ def GetParallelismSettings(self):
+ """
+ Return class to set parameters for the parallelism
+ """
+ if self._param is None:
+ raise Exception("You need to set Parallelism method first (SetParallelismMethod)")
+ return self._param
+
+ def AddGlobalHypothesis(self, hyp):
+ """
+ Split hypothesis to apply it to all the submeshes:
+ - the 1D+2D
+ - each of the 3D solids
+
+ Parameters:
+ hyp: a hypothesis to assign
+
+ """
+ if isinstance(hyp, NETGENPlugin._objref_NETGENPlugin_Hypothesis):
+ copy_param = _copy_netgen_param
+ mesher3D = "NETGEN"
+ elif isinstance(hyp, GMSHPlugin._objref_GMSHPlugin_Hypothesis):
+ copy_param = _copy_gmsh_param
+ mesher3D = "GMSH"
+ else:
+ raise ValueError("param must come from NETGENPlugin or GMSHPlugin")
+
+ self.mesh.SetParallelismDimension(3)
+ self._build_submeshes(None, mesher3D)
+
+ param2d = self._algo2d.Parameters()
+ copy_param(2, param2d, hyp)
+
+ for algo3d in self._algo3d:
+ param3d = algo3d.Parameters()
+ copy_param(3, param3d, hyp)
+
+ def Add2DGlobalHypothesis(self, hyp):
+ """
+ Split hypothesis to apply it to all the submeshes:
+ - the 1D
+ - each of the 2D faces
+
+ Parameters:
+ hyp: a hypothesis to assign
+
+ """
+ if isinstance(hyp, NETGENPlugin._objref_NETGENPlugin_Hypothesis):
+ copy_param = _copy_netgen_param
+ mesher2D = "NETGEN"
+ else:
+ raise ValueError("param must come from NETGENPlugin")
+
+ self.mesh.SetParallelismDimension(2)
+ self._build_submeshes(mesher2D, None)
+
+ param1d = self._algo1d
+ copy_param(1, param1d, hyp)
+
+ for algo2d in self._algo2d:
+ param2d = algo2d.Parameters()
+ copy_param(2, param2d, hyp)
+
+ pass # End of ParallelMesh