Salome HOME
Adding nnthreads as parameter for netgen
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_Mesher.cxx
index ed6021089369bdb10565e735407cb2bb8d4492e6..615c970fea997620e02ea70b6410ee34289539e3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -380,7 +380,7 @@ namespace
 
   //================================================================================
   /*!
-   * \brief Restrict size of elements on the given edge 
+   * \brief Restrict size of elements on the given edge
    */
   //================================================================================
 
@@ -599,6 +599,17 @@ void NETGENPlugin_Mesher::SetDefaultParameters()
   _fineness               = NETGENPlugin_Hypothesis::GetDefaultFineness();
   mparams.uselocalh       = NETGENPlugin_Hypothesis::GetDefaultSurfaceCurvature();
   netgen::merge_solids    = NETGENPlugin_Hypothesis::GetDefaultFuseEdges();
+
+#ifdef NETGEN_V6
+
+  mparams.nthreads = NETGENPlugin_Hypothesis::GetDefaultNbThreads();
+
+  if ( getenv( "SALOME_NETGEN_DISABLE_MULTITHREADING" ))
+  {
+    mparams.nthreads = 1;
+    mparams.parallel_meshing = false;
+  }
+#endif
 }
 
 //=============================================================================
@@ -643,6 +654,7 @@ void NETGENPlugin_Mesher::SetParameters(const NETGENPlugin_Hypothesis* hyp)
 #ifdef NETGEN_V6
     // std::string
     mparams.meshsizefilename = hyp->GetMeshSizeFile();
+    mparams.nthreads = hyp->GetNbThreads();
 #else
     // const char*
     mparams.meshsizefilename= hyp->GetMeshSizeFile().empty() ? 0 : hyp->GetMeshSizeFile().c_str();
@@ -669,6 +681,12 @@ void NETGENPlugin_Mesher::SetParameters(const NETGENPlugin_Hypothesis* hyp)
       }
     }
   }
+
+#ifdef NETGEN_V6
+
+  netgen::mparam.closeedgefac = 2;
+
+#endif
 }
 
 //=============================================================================
@@ -732,7 +750,7 @@ void NETGENPlugin_Mesher::SetLocalSize( netgen::OCCGeometry& occgeo,
     if ( faceNgID >= 1 )
     {
 #ifdef NETGEN_V6
-      occgeo.SetFaceMaxH(faceNgID-1, val);
+      occgeo.SetFaceMaxH(faceNgID-1, val, netgen::mparam);
 #else
       occgeo.SetFaceMaxH(faceNgID, val);
 #endif
@@ -801,7 +819,7 @@ void NETGENPlugin_Mesher::SetLocalSizeForChordalError( netgen::OCCGeometry& occg
       double maxCurv = Max( Abs( surfProp.MaxCurvature()), Abs( surfProp.MinCurvature() ));
       double    size = elemSizeForChordalError( _chordalError, 1 / maxCurv );
 #ifdef NETGEN_V6
-      occgeo.SetFaceMaxH( i-1, size * sizeCoef );
+      occgeo.SetFaceMaxH( i-1, size * sizeCoef, netgen::mparam );
 #else
       occgeo.SetFaceMaxH( i, size * sizeCoef );
 #endif
@@ -1297,11 +1315,11 @@ bool NETGENPlugin_Mesher::FillNgMesh(netgen::OCCGeometry&           occgeom,
           if ( id ) solidSMDSIDs[ bool( solidSMDSIDs[0] )] = meshDS->ShapeToIndex( *solid );
         }
       }
+      bool isShrunk = true;
       if ( proxyMesh && proxyMesh->GetProxySubMesh( geomFace ))
       {
         // if a proxy sub-mesh contains temporary faces, then these faces
         // should be used to mesh only one SOLID
-        bool hasTmp = false;
         smDS = proxyMesh->GetSubMesh( geomFace );
         SMDS_ElemIteratorPtr faces = smDS->GetElements();
         while ( faces->more() )
@@ -1309,7 +1327,7 @@ bool NETGENPlugin_Mesher::FillNgMesh(netgen::OCCGeometry&           occgeom,
           const SMDS_MeshElement* f = faces->next();
           if ( proxyMesh->IsTemporary( f ))
           {
-            hasTmp = true;
+            isShrunk = false;
             if ( solidSMDSIDs[1] && proxyMesh->HasPrismsOnTwoSides( meshDS->MeshElements( geomFace )))
               break;
             else
@@ -1328,7 +1346,7 @@ bool NETGENPlugin_Mesher::FillNgMesh(netgen::OCCGeometry&           occgeom,
           }
         }
         const int fID = occgeom.fmap.FindIndex( geomFace );
-        if ( !hasTmp ) // shrunk mesh
+        if ( isShrunk ) // shrunk mesh
         {
           // move netgen points according to moved nodes
           SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true);
@@ -1380,7 +1398,7 @@ bool NETGENPlugin_Mesher::FillNgMesh(netgen::OCCGeometry&           occgeom,
       {
         solidSMDSIDs[1] = 0;
       }
-      const bool hasVLOn2Sides = ( solidSMDSIDs[1] > 0 );
+      const bool hasVLOn2Sides = ( solidSMDSIDs[1] > 0 && !isShrunk );
 
       // Add ng face descriptors of meshed faces
       faceNgID++;
@@ -1440,7 +1458,7 @@ bool NETGENPlugin_Mesher::FillNgMesh(netgen::OCCGeometry&           occgeom,
         const SMDS_MeshElement* f = faces->next();
         if ( f->NbNodes() % 3 != 0 ) // not triangle
         {
-          PShapeIteratorPtr solidIt=helper.GetAncestors(geomFace,*sm->GetFather(),TopAbs_SOLID);
+          PShapeIteratorPtr solidIt = helper.GetAncestors( geomFace,*sm->GetFather(),TopAbs_SOLID);
           if ( const TopoDS_Shape * solid = solidIt->next() )
             sm = _mesh->GetSubMesh( *solid );
           SMESH_BadInputElements* badElems =
@@ -1557,7 +1575,7 @@ void NETGENPlugin_Mesher::FixIntFaces(const netgen::OCCGeometry& occgeom,
                                       NETGENPlugin_Internals&    internalShapes)
 {
   SMESHDS_Mesh* meshDS = internalShapes.getMesh().GetMeshDS();
-  
+
   // find ng indices of internal faces
   set<int> ngFaceIds;
   for ( int ngFaceID = 1; ngFaceID <= occgeom.fmap.Extent(); ++ngFaceID )
@@ -1717,7 +1735,7 @@ namespace
     double dist3D = surf->Value( uv1.X(), uv1.Y() ).Distance( surf->Value( uv2.X(), uv2.Y() ));
     if ( stopHandler == 0 ) // stop recursion
       return dist3D;
-    
+
     // start recursion if necessary
     double dist2D = SMESH_MesherHelper::ApplyIn2D(surf, uv1, uv2, gp_XY_Subtracted, 0).Modulus();
     if ( fabs( dist3D - dist2D ) < dist2D * 1e-10 )
@@ -2187,7 +2205,7 @@ void NETGENPlugin_Mesher::AddIntVerticesInSolids(const netgen::OCCGeometry&
  *  \param wires - data of nodes on FACE boundary
  *  \param helper - mesher helper holding the FACE
  *  \param nodeVec - vector of nodes in which node index == netgen ID
- *  \retval SMESH_ComputeErrorPtr - error description 
+ *  \retval SMESH_ComputeErrorPtr - error description
  */
 //================================================================================
 
@@ -2694,7 +2712,7 @@ int NETGENPlugin_Mesher::FillSMesh(const netgen::OCCGeometry&          occgeo,
 
   for ( int i = 1; i <= nbVol; ++i )
   {
-    const netgen::Element& elem = ngMesh.VolumeElement(i);      
+    const netgen::Element& elem = ngMesh.VolumeElement(i);
     int aSolidInd = elem.GetIndex();
     TopoDS_Solid aSolid;
     if ( aSolidInd > 0 && aSolidInd <= occgeo.somap.Extent() )
@@ -2887,7 +2905,7 @@ bool NETGENPlugin_Mesher::Compute()
 
   // vector of nodes in which node index == netgen ID
   vector< const SMDS_MeshNode* > nodeVec;
-  
+
   {
     // ----------------
     // compute 1D mesh
@@ -3228,7 +3246,7 @@ bool NETGENPlugin_Mesher::Compute()
       // fill _ngMesh with faces of sub-meshes
       err = ! ( FillNgMesh(occgeo, *_ngMesh, nodeVec, meshedSM[ MeshDim_2D ], &quadHelper));
       initState = NETGENPlugin_ngMeshInfo(_ngMesh, /*checkRemovedElems=*/true);
-      // toPython( _ngMesh );
+      // toPython( _ngMesh )
     }
     if (!err && _isVolume)
     {
@@ -3493,7 +3511,7 @@ bool NETGENPlugin_Mesher::Evaluate(MapShapeNbElems& aResMap)
   const int hugeNb = std::numeric_limits<int>::max() / 100;
 
   // ----------------
-  // evaluate 1D 
+  // evaluate 1D
   // ----------------
   // pass 1D simple parameters to NETGEN
   if ( _simpleHyp )
@@ -3600,7 +3618,7 @@ bool NETGENPlugin_Mesher::Evaluate(MapShapeNbElems& aResMap)
     return false;
 
   // ----------------
-  // evaluate 2D 
+  // evaluate 2D
   // ----------------
   if ( _simpleHyp ) {
     if ( double area = _simpleHyp->GetMaxElementArea() ) {
@@ -3802,9 +3820,9 @@ NETGENPlugin_Mesher::ReadErrors(const vector<const SMDS_MeshNode* >& nodeVec)
     }
     else if ( strncmp( file, "Intersecting: ", 14 ) == 0 )
     {
-// Intersecting: 
+// Intersecting:
 // openelement 18 with open element 126
-// 41  36  38  
+// 41  36  38
 // 69  70  72
       file.getLine();
       const char* pos = file;
@@ -3814,7 +3832,7 @@ NETGENPlugin_Mesher::ReadErrors(const vector<const SMDS_MeshNode* >& nodeVec)
       ok = ok && file.getInts( three2 );
       for ( int i = 0; ok && i < 3; ++i )
         ok = ( three1[i] < nbNodes && nodeVec[ three1[i]]);
-      for ( int i = 0; ok && i < 3; ++i ) 
+      for ( int i = 0; ok && i < 3; ++i )
         ok = ( three2[i] < nbNodes && nodeVec[ three2[i]]);
       if ( ok )
       {
@@ -3865,8 +3883,63 @@ void NETGENPlugin_Mesher::toPython( const netgen::Mesh* ngMesh )
           << "mesh = smesh.Mesh()" << std::endl << std::endl;
 
   using namespace netgen;
+
+#ifdef NETGEN_V6
+
+  for ( int i = 1; i <= ngMesh->GetNP(); i++)
+  {
+    const Point3d & p = ngMesh->Point(i);
+    outfile << "mesh.AddNode( ";
+    outfile << p.X() << ", ";
+    outfile << p.Y() << ", ";
+    outfile << p.Z() << ") ## "<< i << std::endl;
+  }
+
+  int nbDom = ngMesh->GetNDomains();
+  for ( int i = 0; i < nbDom; ++i )
+    outfile<< "grp" << i+1 << " = mesh.CreateEmptyGroup( SMESH.FACE, 'domain"<< i+1 << "')"<< std::endl;
+
+  int nbDel = 0;
+  for (int i = 1; i <= ngMesh->GetNSE(); i++)
+  {
+    outfile << "mesh.AddFace([ ";
+    Element2d sel = ngMesh->SurfaceElement(i);
+    for (int j = 1; j <= sel.GetNP(); j++)
+      outfile << sel.PNum(j) << ( j < sel.GetNP() ? ", " : " ])");
+    if ( sel.IsDeleted() ) outfile << " ## IsDeleted ";
+    outfile << std::endl;
+    nbDel += sel.IsDeleted();
+
+    if (sel.GetIndex())
+    {
+      if ( int dom1 = ngMesh->GetFaceDescriptor(sel.GetIndex ()).DomainIn())
+        outfile << "grp"<< dom1 <<".Add([ " << i - nbDel << " ])" << std::endl;
+      if ( int dom2 = ngMesh->GetFaceDescriptor(sel.GetIndex ()).DomainOut())
+        outfile << "grp"<< dom2 <<".Add([ " << i - nbDel << " ])" << std::endl;
+    }
+  }
+
+  for (int i = 1; i <= ngMesh->GetNE(); i++)
+  {
+    Element el = ngMesh->VolumeElement(i);
+    outfile << "mesh.AddVolume([ ";
+    for (int j = 1; j <= el.GetNP(); j++)
+      outfile << el.PNum(j) << ( j < el.GetNP() ? ", " : " ])");
+    outfile << std::endl;
+  }
+
+  for (int i = 1; i <= ngMesh->GetNSeg(); i++)
+  {
+    const Segment & seg = ngMesh->LineSegment (i);
+    outfile << "mesh.AddEdge([ "
+            << seg[0]+1 << ", "
+            << seg[1]+1 << " ])" << std::endl;
+  }
+
+#else  //////// V 5
+
   PointIndex pi;
-  for (pi = PointIndex::BASE; 
+  for (pi = PointIndex::BASE;
        pi < ngMesh->GetNP()+PointIndex::BASE; pi++)
   {
     outfile << "mesh.AddNode( ";
@@ -3879,6 +3952,7 @@ void NETGENPlugin_Mesher::toPython( const netgen::Mesh* ngMesh )
   for ( int i = 0; i < nbDom; ++i )
     outfile<< "grp" << i+1 << " = mesh.CreateEmptyGroup( SMESH.FACE, 'domain"<< i+1 << "')"<< std::endl;
 
+  int nbDel = 0;
   SurfaceElementIndex sei;
   for (sei = 0; sei < ngMesh->GetNSE(); sei++)
   {
@@ -3888,13 +3962,14 @@ void NETGENPlugin_Mesher::toPython( const netgen::Mesh* ngMesh )
       outfile << sel[j] << ( j+1 < sel.GetNP() ? ", " : " ])");
     if ( sel.IsDeleted() ) outfile << " ## IsDeleted ";
     outfile << std::endl;
+    nbDel += sel.IsDeleted();
 
     if ((*ngMesh)[sei].GetIndex())
     {
       if ( int dom1 = ngMesh->GetFaceDescriptor((*ngMesh)[sei].GetIndex ()).DomainIn())
-        outfile << "grp"<< dom1 <<".Add([ " << (int)sei+1 << " ])" << std::endl;
+        outfile << "grp"<< dom1 <<".Add([ " << (int)sei+1 - nbDel << " ])" << std::endl;
       if ( int dom2 = ngMesh->GetFaceDescriptor((*ngMesh)[sei].GetIndex ()).DomainOut())
-        outfile << "grp"<< dom2 <<".Add([ " << (int)sei+1 << " ])" << std::endl;
+        outfile << "grp"<< dom2 <<".Add([ " << (int)sei+1 - nbDel  << " ])" << std::endl;
     }
   }
 
@@ -3914,6 +3989,9 @@ void NETGENPlugin_Mesher::toPython( const netgen::Mesh* ngMesh )
             << seg[0] << ", "
             << seg[1] << " ])" << std::endl;
   }
+
+#endif
+
   std::cout << "Write " << pyFile << std::endl;
 }
 
@@ -4426,10 +4504,11 @@ int NETGENPlugin_NetgenLibWrapper::GenerateMesh( netgen::OCCGeometry& occgeo,
                                                  netgen::Mesh* & ngMesh )
 {
   int err = 0;
-#ifdef NETGEN_V6
-
   if ( !ngMesh )
     ngMesh = new netgen::Mesh;
+
+#ifdef NETGEN_V6
+
   ngMesh->SetGeometry( shared_ptr<netgen::NetgenGeometry>( &occgeo, &NOOP_Deleter ));
 
   netgen::mparam.perfstepsstart = startWith;