+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 to have same results
+ local_param.NumberOfSegments(int(global_param.GetNbSegPerEdge())-1)
+ 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
+ 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
+
+
+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")
+
+ import SHAPERSTUDY
+ import shaperBuilder
+ # If we have a shaper object converting it into geom (temporary solution)
+ if isinstance(geom, SHAPERSTUDY.SHAPERSTUDY_ORB._objref_SHAPER_Object):
+ geom_obj = _shaperstudy2geom(geompyD, geom)
+ else:
+ geom_obj = geom
+
+ # Splitting geometry into one geom containing 1D and 2D elements and a
+ # list of 3D elements
+ super(ParallelMesh, self).__init__(smeshpyD, geompyD, geom_obj, name, parallel=True)
+
+ if split_geom:
+ self._all_faces, self._solids = _split_geom(geompyD, geom_obj)
+
+ order = []
+ self._algo2d = self.Triangle(geom=geom_obj, algo="NETGEN_2D")
+ self._algo3d = []
+
+ for solid_id, solid in enumerate(self._solids):
+ name = "Solid_{}".format(solid_id)
+ algo3d = self.Tetrahedron(geom=solid, algo="NETGEN_3D_Remote")
+ self._algo3d.append(algo3d)
+
+ self._param = None
+
+
+ def GetNbSolids(self):
+ """
+ Return the number of 3D solids
+ """
+ return len(self._solids)
+
+ 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 not isinstance(hyp, NETGENPlugin._objref_NETGENPlugin_Hypothesis):
+ raise ValueError("param must come from NETGENPlugin")
+
+ param2d = self._algo2d.Parameters()
+ _copy_netgen_param(2, param2d, hyp)
+
+ for algo3d in self._algo3d:
+
+ param3d = algo3d.Parameters()
+ _copy_netgen_param(3, param3d, hyp)
+
+
+ pass # End of ParallelMesh
+