Salome HOME
Merge branch 'occ/new_mg_licnese'
[plugins/ghs3dprlplugin.git] / src / GHS3DPRLPlugin / GHS3DPRLPlugin_GHS3DPRL.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 46f7df4..eb92f30
@@ -1,90 +1,94 @@
-//  GHS3DPRLPlugin : C++ implementation
+// Copyright (C) 2007-2021  CEA/DEN, EDF R&D
 //
-//  Copyright (C) 2006  OPEN CASCADE, CEA/DEN, EDF R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
-//
-//
-// File    : GHS3DPRLPlugin_GHS3DPRL.cxx
-// Author  : Christian VAN WAMBEKE (CEA) (from Hexotic plugin Lioka RAZAFINDRAZAKA)
-// Date    : 2007/02/01
-// Project : SALOME
-//=============================================================================
-using namespace std;
 
+// ---
+// File   : GHS3DPRLPlugin_GHS3DPRL.cxx
+// Author : Christian VAN WAMBEKE (CEA) (from Hexotic plugin Lioka RAZAFINDRAZAKA)
+// ---
+//
 #include "GHS3DPRLPlugin_GHS3DPRL.hxx"
 #include "GHS3DPRLPlugin_Hypothesis.hxx"
-// #include "GHS3DPRLPlugin_Mesher.hxx"
-
-#include "SMDS_MeshElement.hxx"
-#include "SMDS_MeshNode.hxx"
+#include "MG_TetraHPC_API.hxx"
 
-#include <TopExp_Explorer.hxx>
-#include <OSD_File.hxx>
+#include <SMESHDS_Mesh.hxx>
+#include <SMESH_Gen.hxx>
+#include <SMESH_TypeDefs.hxx>
+#include <SMESH_subMesh.hxx>
+#include <SMESH_MesherHelper.hxx>
 
 #include "utilities.h"
 
-#ifndef WIN32
-#include <sys/sysinfo.h>
-#endif
+#include <list>
 
-#ifdef _DEBUG_
-#define DUMP(txt) \
-//  cout << txt
-#else
-#define DUMP(txt)
-#endif
+#include <BRepGProp.hxx>
+#include <GProp_GProps.hxx>
+#include <OSD_File.hxx>
+#include <Standard_ProgramError.hxx>
+#include <TCollection_AsciiString.hxx>
+#include <TopExp_Explorer.hxx>
+#include <TopTools_MapOfShape.hxx>
+#include <TopoDS.hxx>
+#include <TopoDS_Edge.hxx>
+#include <TopoDS_Face.hxx>
 
-#include <SMESH_Gen.hxx>
-#include <SMESHDS_Mesh.hxx>
-#include <SMESH_ControlsDef.hxx>
+#define GMFVERSION GmfDouble
+#define GMFDIMENSION 3
 
-#include <list>
-#include <TCollection_AsciiString.hxx>
+using namespace std;
 
-//=============================================================================
+static void removeFile( const TCollection_AsciiString& fileName )
+{
+  try {
+    OSD_File( fileName ).Remove();
+  }
+  catch ( Standard_ProgramError& ) {
+    MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
+  }
+}
 
-GHS3DPRLPlugin_GHS3DPRL::GHS3DPRLPlugin_GHS3DPRL(int hypId, int studyId, SMESH_Gen* gen)
-  : SMESH_3D_Algo(hypId, studyId, gen)
+//=============================================================================
+GHS3DPRLPlugin_GHS3DPRL::GHS3DPRLPlugin_GHS3DPRL(int hypId, SMESH_Gen* gen)
+  : SMESH_3D_Algo(hypId, gen)
 {
   MESSAGE("GHS3DPRLPlugin_GHS3DPRL::GHS3DPRLPlugin_GHS3DPRL");
-  _name = "GHS3DPRL_3D";
+  _name = "MG-Tetra Parallel";
   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
+  _onlyUnaryInput = false; // Compute() will be called on a compound of solids
   _countSubMesh=0;
   _nodeRefNumber=0;
-  _compatibleHypothesis.push_back("GHS3DPRL_Parameters");
+  _compatibleHypothesis.push_back(GHS3DPRLPlugin_Hypothesis::GetHypType());
+  _requireShape=false;
 }
 
 //=============================================================================
-
 GHS3DPRLPlugin_GHS3DPRL::~GHS3DPRLPlugin_GHS3DPRL()
 {
   MESSAGE("GHS3DPRLPlugin_GHS3DPRL::~GHS3DPRLPlugin_GHS3DPRL");
 }
 
 //=============================================================================
-
 bool GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis
                          (SMESH_Mesh& aMesh,
                           const TopoDS_Shape& aShape,
                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
 {
-  MESSAGE("GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis");
-
+  //MESSAGE("GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis");
   _hypothesis = NULL;
 
   list<const SMESHDS_Hypothesis*>::const_iterator itl;
@@ -94,15 +98,15 @@ bool GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis
   int nbHyp = hyps.size();
   if (!nbHyp)
   {
-    aStatus = SMESH_Hypothesis::HYP_OK;
-    return true;  // can work with no hypothesis
+    aStatus = SMESH_Hypothesis::HYP_MISSING;
+    return false;  // can't work with no hypothesis
   }
 
   itl = hyps.begin();
   theHyp = (*itl); // use only the first hypothesis
 
   string hypName = theHyp->GetName();
-  if (hypName == "GHS3DPRL_Parameters")
+  if (hypName == GHS3DPRLPlugin_Hypothesis::GetHypType())
   {
     _hypothesis = static_cast<const GHS3DPRLPlugin_Hypothesis*> (theHyp);
     ASSERT(_hypothesis);
@@ -114,285 +118,32 @@ bool GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis
   return aStatus == SMESH_Hypothesis::HYP_OK;
 }
 
-//=======================================================================
-
-static bool writeGHS3DPRLFiles (const TCollection_AsciiString &  GHS3DPRL_In,
-                                SMESHDS_Mesh *                   theMesh,
-                                map <int,int> &                  theSmdsToGHS3DPRLIdMap,
-                                map <int,const SMDS_MeshNode*> & theGHS3DPRLIdToNodeMap)
-{
-   bool Ok;
-   TCollection_AsciiString namefile(GHS3DPRL_In);
-   namefile+=".points";
-   OSD_File(namefile).Remove();
-   ofstream theFile;
-   theFile.open(namefile.ToCString(),ios::out);
-#ifdef WIN32
-   Ok=theFile->is_open();
-#else
-   Ok=theFile.rdbuf()->is_open();
-#endif
-   if (!Ok)
-   {
-      INFOS("Can't write into "<<namefile.ToCString());
-      return false;
-   }
-   cout<<endl<<"Creating GHS3DPRL processed mesh file : "<<namefile<<endl;
-
-   int nbVertices=theMesh->NbNodes();
-   int nbFaces=theMesh->NbFaces();        //triangles or quadrangles
-   const char* space="  ";
-   const int dummyint=1;                  //nrs,nsd,refnum=1 (for wrap)
-
-   // Writing SMESH points into GHS3DPRL File.points
-   theFile<<nbVertices<<endl;
-
-   int aSmdsNodeID = 1;
-   const SMDS_MeshNode* node_2;
-   SMDS_NodeIteratorPtr itOnNode = theMesh->nodesIterator();
-   while (itOnNode->more())
-   {
-      node_2 = itOnNode->next();
-      theSmdsToGHS3DPRLIdMap.insert(map <int,int>::value_type(node_2->GetID(),aSmdsNodeID));
-      theGHS3DPRLIdToNodeMap.insert(map <int,const SMDS_MeshNode*>::value_type(aSmdsNodeID,node_2));
-      aSmdsNodeID++;
-      theFile<<node_2->X()<<space<<node_2->Y()<<space<<node_2->Z()<<space<<dummyint<<endl;
-   }
-   //no specified points;
-   theFile.close();
-
-   namefile=GHS3DPRL_In+".faces";
-   OSD_File(namefile).Remove();
-   theFile.open(namefile.ToCString(),ios::out);
-#ifdef WIN32
-   Ok=theFile->is_open();
-#else
-   Ok=theFile.rdbuf()->is_open();
-#endif
-   if (!Ok)
-   {
-      INFOS("Can't write into "<<namefile.ToCString());
-      return false;
-   }
-   cout<<endl<<"Creating GHS3DPRL processed mesh file : "<<namefile<<endl;
-
-   // Writing SMESH faces into GHS3DPRL File.faces
-   theFile<<nbFaces<<" 0"<<endl;   //NB_ELEMS DUMMY_INT
-                                   //" 0" is a reserved parameter
-
-   const SMDS_MeshElement* aFace;
-   map<int,int>::const_iterator itOnSmdsNode;
-   SMDS_ElemIteratorPtr itOnFaceNode;
-   SMDS_FaceIteratorPtr itOnSmdsFace = theMesh->facesIterator();
-   long nbNoTriangles=0;
-   while (itOnSmdsFace->more())
-   {
-      aFace=itOnSmdsFace->next();
-      itOnFaceNode=aFace->nodesIterator();
-      const int nbNodes=aFace->NbNodes();
-      if (nbNodes!=3) nbNoTriangles++;
-      theFile<<nbNodes<<space;        // NB_NODES
-      while (itOnFaceNode->more())
-      {
-          aSmdsNodeID=itOnFaceNode->next()->GetID();
-          itOnSmdsNode=theSmdsToGHS3DPRLIdMap.find(aSmdsNodeID);
-          ASSERT(itOnSmdsNode!=theSmdsToGHS3DPRLIdMap.end());
-          theFile<<space<<(*itOnSmdsNode).second; //NODE_1 NODE_2 ...
-      }
-      //(NB_NODES+1) times: DUMMY_INT
-      for ( int i=0; i<=nbNodes; i++) theFile<<space<<dummyint;
-      theFile<<endl;
-   }
-   theFile.close();
-
-   cout<<"Processed mesh files created, they contains :\n";
-   cout<<"    "<<nbVertices<<" vertices\n";
-   if (nbNoTriangles==0)
-      cout<<"    "<<nbFaces<<" faces\n\n";
-   else
-      cout<<"    "<<nbFaces<<" faces with "<<nbNoTriangles<<"faces no triangles\n\n";
-   return true;
-}
-
-//=======================================================================
-
-static bool getInt( int & theValue, char * & theLine )
-{
-  char *ptr;
-  theValue = strtol( theLine, &ptr, 10 );
-  if ( ptr == theLine ||
-      // there must not be neither '.' nor ',' nor 'E' ...
-      (*ptr != ' ' && *ptr != '\n' && *ptr != '\0'))
-    return false;
-
-  DUMP( "  " << theValue );
-  theLine = ptr;
-  return true;
-}
-
-//=======================================================================
-
-static bool getDouble( double & theValue, char * & theLine )
-{
-  char *ptr;
-  theValue = strtod( theLine, &ptr );
-  if ( ptr == theLine )
-    return false;
-
-  DUMP( "   " << theValue );
-  theLine = ptr;
-  return true;
-}
-
-//=======================================================================
-
-#define GHS3DPRLPlugin_BUFLENGTH 256
-#define GHS3DPRLPlugin_ReadLine(aPtr,aBuf,aFile,aLineNb) \
-{  aPtr = fgets( aBuf, GHS3DPRLPlugin_BUFLENGTH - 2, aFile ); aLineNb++; DUMP(endl); }
-
-//=======================================================================
-
-static bool readResult(FILE *                           theFile,
-                       SMESHDS_Mesh *                   theMesh,
-                       const TopoDS_Shape &             theShape,
-                       map <int,const SMDS_MeshNode*> & theGHS3DPRLIdToNodeMap,
-                       const TCollection_AsciiString &  GHS3DPRL_Out,
-                       int &                            nodeRefNumber)
-{
-  // ---------------------------------
-  // Read generated elements and nodes
-  // ---------------------------------
-
-  cout << "Reading GHS3DPRL output file : " << GHS3DPRL_Out << endl;
-  cout << endl;
-
-  char aBuffer[ GHS3DPRLPlugin_BUFLENGTH ];
-  char * aPtr;
-  int aLineNb = 0;
-  int shapeID = theMesh->ShapeToIndex( theShape );
-
-  int line = 1, EndOfFile = 0, nbElem = 0, nField = 10, nbRef = 0, aGHS3DPRLNodeID = 0;
-  char * theField;
-
-  char * tabField [nField];
-  int    tabRef [nField];
-
-  tabField[0] = "MeshVersionFormatted";    tabRef[0] = 0;
-  tabField[1] = "Dimension";               tabRef[1] = 0;
-  tabField[2] = "Vertices";                tabRef[2] = 3;
-  tabField[3] = "Edges";                   tabRef[3] = 2;
-  tabField[4] = "Triangles";               tabRef[4] = 3;
-  tabField[5] = "Quadrilaterals";          tabRef[5] = 4;
-  tabField[6] = "Hexahedra";               tabRef[6] = 8;
-  tabField[7] = "Corners";                 tabRef[7] = 1;
-  tabField[8] = "Ridges";                  tabRef[0] = 1;
-  tabField[9] = "End";                     tabRef[0] = 0;
-
-  nodeRefNumber += theMesh->NbNodes();
-
-  SMDS_NodeIteratorPtr itOnGHS3DPRLInputNode = theMesh->nodesIterator();
-  while ( itOnGHS3DPRLInputNode->more() )
-      theMesh->RemoveNode( itOnGHS3DPRLInputNode->next() );
-
-  while ( EndOfFile == 0  ) {
-      GHS3DPRLPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb );
-      for ( int iField = 0; iField < nField; iField++ ) {
-          stringstream theMessage;
-          theField = tabField[iField];
-          if ( strncmp(aPtr, theField, strlen(theField)) == 0 ) {
-              if ( strcmp(theField, "End") == 0 ) {
-                  EndOfFile = 1;
-                  theMessage << "End of GHS3DPRL output file has been reached";
-              }
-              else {
-                  GHS3DPRLPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb );
-                  line++;
-                  getInt( nbElem, aPtr );
-
-                  if ( strcmp(theField, "MeshVersionFormatted") == 0 )
-                      theMessage << "GHS3DPRL mesh descriptor : " << theField << " " << nbElem;
-                  else if ( strcmp(theField, "Dimension") == 0 )
-                      theMessage << "GHS3DPRL mesh of " << nbElem << "D dimension";
-                  else if ( strcmp(theField, "Vertices")       == 0 ||
-                            strcmp(theField, "Edges")          == 0 ||
-                            strcmp(theField, "Quadrilaterals") == 0 ||
-                            strcmp(theField, "Hexahedra")      == 0 ) {
-                      nbRef = tabRef[iField];
-                      GHS3DPRLPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb );           // read blank line
-
-                      if ( strcmp(theField, "Vertices") == 0 ) {
-                          int aGHS3DPRLID;
-                          double coord[nbRef];
-                          SMDS_MeshNode * aGHS3DPRLNode;
-
-                          for ( int iElem = 0; iElem < nbElem; iElem++ ) {
-                              aGHS3DPRLID = iElem + 1 + nodeRefNumber;
-                              GHS3DPRLPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb );   // read file lines
-                              for ( int iCoord = 0; iCoord < 3; iCoord++ )
-                                  getDouble ( coord[ iCoord ], aPtr );
-                              aGHS3DPRLNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
-                              theMesh->SetNodeInVolume( aGHS3DPRLNode, shapeID );
-                              theGHS3DPRLIdToNodeMap[ aGHS3DPRLID ] = aGHS3DPRLNode;
-                          }
-                      }
-                      else {
-                          const SMDS_MeshNode * node[nbRef];
-                          SMDS_MeshElement* aGHS3DPRLElement;
-                          map <int,const SMDS_MeshNode*>::iterator itOnGHS3DPRLNode;
-
-                          for ( int iElem = 0; iElem < nbElem; iElem++ ) {
-                              GHS3DPRLPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb );   // read file lines
-                              for ( int iRef = 0; iRef < nbRef; iRef++ ) {
-                                  getInt ( aGHS3DPRLNodeID, aPtr );                         // read nbRef aGHS3DPRLNodeID
-                                  aGHS3DPRLNodeID += nodeRefNumber;
-                                  itOnGHS3DPRLNode = theGHS3DPRLIdToNodeMap.find( aGHS3DPRLNodeID );
-                                  node[ iRef ] = itOnGHS3DPRLNode->second;
-                              }
-
-                              if ( strcmp(theField, "Edges") == 0 )                        // create an element
-                                  aGHS3DPRLElement = theMesh->AddEdge( node[0], node[1] );
-                              else if ( strcmp(theField, "Quadrilaterals") == 0 )
-                                  aGHS3DPRLElement = theMesh->AddFace( node[0], node[1], node[2], node[3] );
-                              else if ( strcmp(theField, "Hexahedra") == 0 )
-                                  aGHS3DPRLElement = theMesh->AddVolume( node[0], node[1], node[2], node[3], node[4], node[5], node[6], node[7] );
-
-                              theMesh->SetMeshElementOnShape( aGHS3DPRLElement, shapeID );
-                          }
-                      }
-                      theMessage << nbElem << " " << theField << " created";
-                  }
-              }
-              if ( theMessage.str().size() != 0 ) {
-                  cout << theMessage.str() << endl;
-                  break;
-              }
-          }
-      }
-  }
-  cout << endl;
-  return true;
-}
-
 //=============================================================================
 // Pass parameters to GHS3DPRL
-
-void GHS3DPRLPlugin_GHS3DPRL::SetParameters(const GHS3DPRLPlugin_Hypothesis* hyp) {
+void GHS3DPRLPlugin_GHS3DPRL::SetParameters(const GHS3DPRLPlugin_Hypothesis* hyp)
+{
   if (hyp) {
     MESSAGE("GHS3DPRLPlugin_GHS3DPRL::SetParameters");
-    _MEDName = hyp->GetMEDName();  //"DOMAIN\0"
-    _NbPart = hyp->GetNbPart();
-    _KeepFiles = hyp->GetKeepFiles();
+    _MEDName     = hyp->GetMEDName();  //"DOMAIN\0"
+    _NbPart      = hyp->GetNbPart();
+    _KeepFiles   = hyp->GetKeepFiles();
+    _Background  = hyp->GetBackground();
+    _Multithread = hyp->GetMultithread();
+    _Gradation   = hyp->GetGradation();
+    _MinSize     = hyp->GetMinSize();
+    _MaxSize     = hyp->GetMaxSize();
+    _AdvOptions  = hyp->GetAdvancedOption();
   }
 }
 
 //=======================================================================
-
+//before launching salome
+//SALOME_TMP_DIR (for keep tepal intermediates files) could be set in user's directories
 static TCollection_AsciiString getTmpDir()
 {
   TCollection_AsciiString aTmpDir;
-
   char *Tmp_dir = getenv("SALOME_TMP_DIR");
-  if (Tmp_dir == NULL) Tmp_dir = getenv("TMPDIR");
+  if (Tmp_dir == NULL) Tmp_dir = getenv("TMP");
   if(Tmp_dir != NULL)
   {
     aTmpDir = Tmp_dir;
@@ -407,148 +158,336 @@ static TCollection_AsciiString getTmpDir()
 #ifdef WIN32
       aTmpDir = TCollection_AsciiString("C:\\");
 #else
-      aTmpDir = TCollection_AsciiString("/export/home/");
+      aTmpDir = TCollection_AsciiString("/tmp/");
 #endif
   }
-  //cout<<"getTmpDir()="<<aTmpDir<<endl;
   return aTmpDir;
 }
 
 //=============================================================================
-// Here we are going to use the GHS3DPRL mesher
-
-bool GHS3DPRLPlugin_GHS3DPRL::Compute(SMESH_Mesh&          theMesh,
-                                     const TopoDS_Shape& theShape) {
-
-
-   bool Ok;
-   MESSAGE("GHS3DPRLPlugin_GHS3DPRL::Compute");
+// Write a skin mesh into a GMF file or pass it to MG-TetraHPC API
+static void exportGMF(MG_TetraHPC_API*    theTetraInput,
+                      const char*         theFile,
+                      const SMESHDS_Mesh* theMeshDS)
+{
+  int meshID = theTetraInput->GmfOpenMesh( theFile, GmfWrite, GMFVERSION, GMFDIMENSION);
+  
+  // nodes
+  int iN = 0, nbNodes = theMeshDS->NbNodes();
+  theTetraInput->GmfSetKwd( meshID, GmfVertices, nbNodes );
+  std::map< const SMDS_MeshNode*, int, TIDCompare > node2IdMap;
+  SMDS_NodeIteratorPtr nodeIt = theMeshDS->nodesIterator();
+  SMESH_TNodeXYZ n;
+  while ( nodeIt->more() )
+  {
+    n.Set( nodeIt->next() );
+    theTetraInput->GmfSetLin( meshID, GmfVertices, n.X(), n.Y(), n.Z(), n._node->getshapeId() );
+    node2IdMap.insert( node2IdMap.end(), std::make_pair( n._node, ++iN ));
+  }
 
-   if (_hypothesis==NULL)
-   {
-      Ok = false;
-      cout <<"\nNo existing parameters/hypothesis for GHS3DPRL!\n\n";
-      return Ok;
-   }
-   SetParameters(_hypothesis);
+  // triangles
+  SMDS_ElemIteratorPtr elemIt = theMeshDS->elementGeomIterator( SMDSGeom_TRIANGLE );
+  if ( elemIt->more() )
+  {
+    int nbTria = theMeshDS->GetMeshInfo().NbElements( SMDSGeom_TRIANGLE );
+    theTetraInput->GmfSetKwd(meshID, GmfTriangles, nbTria );
+    for ( int gmfID = 1; elemIt->more(); ++gmfID )
+    {
+      const SMDS_MeshElement* tria = elemIt->next();
+      theTetraInput->GmfSetLin(meshID, GmfTriangles, 
+                              node2IdMap[ tria->GetNode( 0 )],
+                              node2IdMap[ tria->GetNode( 1 )],
+                              node2IdMap[ tria->GetNode( 2 )],
+                              tria->getshapeId() );
+    }
+  }
+  theTetraInput->GmfCloseMesh( meshID );
+}
 
-  cout << endl;
-  cout << _name << " parameters :" << endl;
-  cout << "     generic path/name of MED Files = " << _MEDName << endl;
-  cout << "     number of partitions = " << _NbPart << endl;
-  cout << "     keep intermediates files (from tepal) = " << _KeepFiles << endl;
-  cout << endl;
+//=============================================================================
+// Here we are going to use the GHS3DPRL mesher for tetra-hpc (formerly tepal in v3 (2014))
+bool GHS3DPRLPlugin_GHS3DPRL::Compute(SMESH_Mesh&         theMesh,
+                                      const TopoDS_Shape& /*theShape*/)
+{
+  SMESH_MesherHelper helper( theMesh );
+  bool ok = Compute( theMesh, &helper );
+  return ok;
+}
 
+//=============================================================================
+// Here we are going to use the GHS3DPRL mesher for tetra-hpc (formerly tepal in v3 (2014))
+bool GHS3DPRLPlugin_GHS3DPRL::Compute(SMESH_Mesh&         theMesh,
+                                      SMESH_MesherHelper* /*theHelper*/)
+{
+  bool Ok=false;
+  TCollection_AsciiString pluginerror("ghs3dprl: ");
   SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
+  if ( theMesh.NbTriangles() == 0 )
+    return error( COMPERR_BAD_INPUT_MESH, "No triangles in the mesh" );
 
-  if (_countSubMesh==0)
-  {
-      _countTotal=0;
-      TopExp_Explorer expf(meshDS->ShapeToMesh(), TopAbs_SOLID);
-      for ( ; expf.More(); expf.Next() )
-          _countTotal++;
+  if (_hypothesis==NULL){
+    pluginerror += "No existing parameters/hypothesis for GHS3DPRL";
+    cout <<"\n"<<pluginerror<<"\n\n";
+    error(COMPERR_ALGO_FAILED, pluginerror.ToCString() );
+    return false;
+  }
+  SetParameters(_hypothesis);
+  cout << "\n" << _name << " parameters:" << endl;
+  cout << "   generic path/name of MED Files = " << _MEDName << endl;
+  cout << "   number of partitions = " << _NbPart << endl;
+  cout << "   gradation = " << _Gradation << endl;
+  cout << "   min_size = " << _MinSize << endl;
+  cout << "   max_size = " << _MaxSize << endl;
+  cout << "   keep intermediates files (from tetra-hpc) = " << _KeepFiles << endl;
+  cout << "   background (from tetra-hpc) = " << _Background << "\n";
+  cout << "   multithread = " << _Multithread << "\n\n";
+  cout << "   adv. options = '" << _AdvOptions << "'\n\n";
+
+  TCollection_AsciiString
+    tmpDir=getTmpDir(),
+    GHS3DPRL_In,
+    GHS3DPRL_Out,
+    GHS3DPRL_Out_Mesh,
+    GHS3DPRL_Outxml,
+    logFileName,
+    run_GHS3DPRL("tetrahpc2med "),
+    rm("rm -f "),
+    run_nokeep_files,
+    NbPart,
+    Gradation,
+    MinSize,
+    MaxSize,
+    fileskinmed(""),
+    fileskinmesh(""),
+    path,
+    casenamemed;  //_MEDName.c_str());
+  int res = 0;    // 0 is OK 
+
+  casenamemed += (char *)_MEDName.c_str();
+  int n=casenamemed.SearchFromEnd('/');
+  if (n>0) {
+    path=casenamemed.SubString(1,n);
+    casenamemed=casenamemed.SubString(n+1,casenamemed.Length());
   }
+  else
+    path=tmpDir;
 
-  _countSubMesh++;
+  if (casenamemed.Length()>20){
+    casenamemed=casenamemed.SubString(1,20);
+    cerr<<"MEDName truncated (no more 20 characters) = "<<casenamemed<<endl;
+  }
 
-  if (_countSubMesh == _countTotal )
+  map <int,int> aSmdsToGHS3DPRLIdMap;
+  map <int,const SMDS_MeshNode*> aGHS3DPRLIdToNodeMap;
+  GHS3DPRL_In = path + "GHS3DPRL";
+  GHS3DPRL_Out = path + casenamemed;
+  GHS3DPRL_Out_Mesh = path + casenamemed + "_out.mesh";
+  GHS3DPRL_Outxml = path + casenamemed + ".xml"; //master file
+  logFileName = path + casenamemed + ".log"; // MG library output
+  NbPart=_NbPart;
+  Gradation=_Gradation;
+  MinSize=_MinSize;
+  MaxSize=_MaxSize;
+
+  //an example:
+  //tetrahpc2med --casename=/home/whoami/tmp/GHS3DPRL --number=5 --medname=DOMAIN
+  //             --gradation=1.05 --min_size=1e-3 --max_size=1e-2
+  //             --verbose=0 --menu=no --launchtetra=yes;
+
+  run_GHS3DPRL = run_GHS3DPRL +
+    " --casename=" + GHS3DPRL_In +
+    " --number=" + NbPart +
+    " --medname=" + GHS3DPRL_Out +
+    " --launchtetra=yes" +
+    " --gradation=" + Gradation +
+    " --min_size=" + MinSize +
+    " --max_size=" + MaxSize +
+    " --verbose=3" +
+    " " + _AdvOptions.c_str();
+  if (_Background) run_GHS3DPRL += " --background=yes"; else run_GHS3DPRL += " --background=no";
+  if (_Multithread) run_GHS3DPRL += " --multithread=yes"; else run_GHS3DPRL += " --multithread=no";
+  run_nokeep_files = rm +GHS3DPRL_In + "* " + path + "tetrahpc.log";
+  system( run_nokeep_files.ToCString() ); //clean files
+  run_nokeep_files = rm + GHS3DPRL_In + "* ";
+
+  fileskinmesh=path + "GHS3DPRL.mesh";
+  GHS3DPRL_Out = path + casenamemed;
+  removeFile( GHS3DPRL_Outxml ); //only the master xml file
+
+  MG_TetraHPC_API mgTetraHPC( _computeCanceled, _progress );
+  bool useLib = ( mgTetraHPC.IsLibrary() && !_Background && _Multithread );
+  if ( !useLib )
+    mgTetraHPC.SetUseExecutable();
+
+  exportGMF( &mgTetraHPC, fileskinmesh.ToCString(), meshDS );
+
+  if ( true /*useLib*/ )
   {
-      TCollection_AsciiString
-         tmpDir=getTmpDir(),
-         GHS3DPRL_In,
-         GHS3DPRL_Out,
-         NbPart,
-         run_GHS3DPRL("tepal2med "),
-         run_keep_files("rm ");
-
-      //example: tepal2med GHS3DPRL 4 12500000 4 Test noMemu LaunchTepal
-
-      TCollection_AsciiString path;
-      TCollection_AsciiString casenamemed;//_MEDName.c_str());
-      casenamemed += (char *)_MEDName.c_str();
-      int n=casenamemed.SearchFromEnd('/');
-      if (n>0)
-      {
-         path=casenamemed.SubString(1,n);
-         casenamemed=casenamemed.SubString(n+1,casenamemed.Length());
-      }
-      else
-         path=tmpDir;
-
-      if (casenamemed.Length()>20)
-      {
-         casenamemed=casenamemed.SubString(1,20);
-         cerr<<"MEDName truncated (no more 20 characters) = "<<casenamemed<<endl;
-      }
-      cout<<"path="<<path<<endl;
-      cout<<"casenamemed="<<casenamemed<<endl;
-
-      map <int,int> aSmdsToGHS3DPRLIdMap;
-      map <int,const SMDS_MeshNode*> aGHS3DPRLIdToNodeMap;
-      GHS3DPRL_In=path + "GHS3DPRL";
-      GHS3DPRL_Out=path + casenamemed;
-      NbPart=_NbPart;
-      run_GHS3DPRL += GHS3DPRL_In + " " + NbPart + " 12500000 1 Test noMemu LaunchTepal " + GHS3DPRL_Out;
-      run_keep_files += GHS3DPRL_In + ".*.*.* " + path + "tepal.out";
-      cout<<"GHS3DPRL command : "<<run_GHS3DPRL<<endl;
-
-      OSD_File( GHS3DPRL_Out ).Remove(); //only the master med file
-      Ok=writeGHS3DPRLFiles(GHS3DPRL_In, meshDS, aSmdsToGHS3DPRLIdMap, aGHS3DPRLIdToNodeMap);
-
-      if (Ok) system( run_GHS3DPRL.ToCString() );
-
-      // read a result, GHS3DPRL_Out is the name of master file (previous xml format)
-      FILE * aResultFile = fopen( GHS3DPRL_Out.ToCString(), "r" );
-      if (aResultFile)
-      {
-          //Ok = readResult( aResultFile, meshDS, theShape, aGHS3DPRLIdToNodeMap, GHS3DPRL_Out, _nodeRefNumber );
-          Ok = true;
-          fclose(aResultFile);
-          cout << "GHS3DPRL OK output file "<<GHS3DPRL_Out<<" exist !\n\n";
-          if (!_KeepFiles)
-          {
-             system( run_keep_files.ToCString() );
-          }
-      }
-      else
-      {
-          Ok = false;
-          cout << "GHS3DPRL KO output files "<<GHS3DPRL_Out<<" do not exist ! see intermediates files keeped:\n";
-          TCollection_AsciiString run_keep_files("ls -alt ");
-          run_keep_files += GHS3DPRL_Out + "* " + GHS3DPRL_In + "* " + path + "tepal.out";
-          system( run_keep_files.ToCString() );
-          cout<<endl;
-      }
-      _countSubMesh=0;
+    TCollection_AsciiString cmd = TCollection_AsciiString("mg-tetra_hpc.exe") + 
+      " --number_of_subdomains=" + NbPart +
+      " --gradation=" + Gradation +
+      " --min_size=" + MinSize +
+      " --max_size=" + MaxSize +
+      " --verbose=3" +
+      " --out=" + GHS3DPRL_Out_Mesh +
+      " " + _AdvOptions.c_str();
+
+    cout << endl
+         << "  Run mg-tetra_hpc as library. Creating a mesh file " << GHS3DPRL_Out_Mesh << endl
+         << "  Creating a log file : " << logFileName << endl << endl;
+    mgTetraHPC.SetLogFile( logFileName.ToCString() );
+
+    std::string log;
+    mgTetraHPC.Compute( cmd.ToCString(), log );
+
+    if ( log.empty() )
+      log = mgTetraHPC.GetLog();
+    if ( log.find(" Dlim "   ) != std::string::npos ||
+         log.find(" license ") != std::string::npos )
+      return error("License problem");
+
+    // set --launchtetra=no
+    int yesPos = run_GHS3DPRL.Search("launchtetra") + sizeof("launchtetra");
+    run_GHS3DPRL.SetValue( yesPos+0, 'n' );
+    run_GHS3DPRL.SetValue( yesPos+1, 'o' );
+    run_GHS3DPRL.SetValue( yesPos+2, ' ' );
+  }
+  else
+  {
+    cout<<"  Write input file for mg-tetra_hpc "<<fileskinmesh<<"...";
+    cout<<" ...done\n";
+  }
+  fileskinmed=path + "GHS3DPRL_skin.med";
+  cout<<"  Write file "<<fileskinmed<<"...";
+  theMesh.ExportMED(fileskinmed.ToCString(),"SKIN_INITIAL",true);
+  cout<<" ...done\n\n";
+
+  cout<<"GHS3DPRL command :\n  "<<run_GHS3DPRL.ToCString()<<endl;
+  //sometimes it is better to wait flushing files on slow filesystem...
+  system( "sleep 3" );
+  //launch tetrahpc2med which launch mg-tetra_hpc.py which launch mg-tetra_hpc(_mpi?).exe
+  res = system( run_GHS3DPRL.ToCString() );
+  if (res > 0) 
+  {
+    pluginerror = pluginerror + "PROBLEM tetrahpc2med command";
+    cout<<pluginerror<<endl;
+    error(COMPERR_ALGO_FAILED, pluginerror.ToCString());
+    return false; //but it is not a problem but if true my message is overwritten
+  }
+  system( "sleep 3" );
+
+  if (_Background) {
+    pluginerror = pluginerror + "backgrounding... plugin is not waiting for output files "+ casenamemed + "_*.med";
+    cout<<pluginerror<<endl;
+    error(COMPERR_NO_MESH_ON_SHAPE, pluginerror.ToCString());
+    return false; //but it is not a problem but if true my message is overwritten
+    //return true; //but it is not a problem,
+  }
 
+  // read a result, GHS3DPRL_Out is the name of master file (previous xml format)
+  FILE * aResultFile = fopen( GHS3DPRL_Outxml.ToCString(), "r" );
+  if (aResultFile){
+    Ok = true;
+    fclose(aResultFile);
+    cout<<"GHS3DPRL OK output master file "<<casenamemed<<".xml exist !\n\n";
+    pluginerror = pluginerror + "MG-tetra_hpc mesh(es) not loaded in memory, are stored in files "+ casenamemed + "_*.med";
+    cout<<pluginerror<<endl;
+    error(COMPERR_WARNING, pluginerror.ToCString() );
+    if (!_KeepFiles) system( run_nokeep_files.ToCString() );
+  }
+  else{
+    Ok = false; //it is a problem AND my message is NOT overwritten
+    pluginerror = pluginerror + "output master file " + casenamemed + ".xml do not exist";
+    cout<<pluginerror<<endl;
+    error(COMPERR_ALGO_FAILED, pluginerror.ToCString() );
+    cout<<"GHS3DPRL KO output files "<<GHS3DPRL_Out<<" do not exist ! see intermediates files keeped:\n";
+    TCollection_AsciiString run_list_files("ls -alt ");
+    run_list_files +=  GHS3DPRL_Out + "* " + GHS3DPRL_In + "* " + path + "tetrahpc.log";
+    system( run_list_files.ToCString() );
+    cout<<endl;
   }
+
   return Ok;
 }
 
-//=============================================================================
 
-ostream & GHS3DPRLPlugin_GHS3DPRL::SaveTo(ostream & save)
-{
-  return save;
-}
 
 //=============================================================================
-
-istream & GHS3DPRLPlugin_GHS3DPRL::LoadFrom(istream & load)
-{
-  return load;
-}
-
+/*!
+ *
+ */
 //=============================================================================
-
-ostream & operator << (ostream & save, GHS3DPRLPlugin_GHS3DPRL & hyp)
+bool GHS3DPRLPlugin_GHS3DPRL::Evaluate(SMESH_Mesh& aMesh,
+                                       const TopoDS_Shape& aShape,
+                                       MapShapeNbElems& aResMap)
 {
-  return hyp.SaveTo( save );
-}
+  smIdType nbtri = 0, nbqua = 0;
+  double fullArea = 0.0;
+  for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
+    TopoDS_Face F = TopoDS::Face( exp.Current() );
+    SMESH_subMesh *sm = aMesh.GetSubMesh(F);
+    MapShapeNbElemsItr anIt = aResMap.find(sm);
+    if( anIt==aResMap.end() ) {
+      SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
+      smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,
+                                            "Submesh can not be evaluated",this));
+      return false;
+    }
+    std::vector<smIdType> aVec = (*anIt).second;
+    nbtri += std::max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
+    nbqua += std::max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
+    GProp_GProps G;
+    BRepGProp::SurfaceProperties(F,G);
+    double anArea = G.Mass();
+    fullArea += anArea;
+  }
 
-//=============================================================================
+  // collect info from edges
+  smIdType nb0d_e = 0, nb1d_e = 0;
+  bool IsQuadratic = false;
+  bool IsFirst = true;
+  TopTools_MapOfShape tmpMap;
+  for (TopExp_Explorer exp(aShape, TopAbs_EDGE); exp.More(); exp.Next()) {
+    const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
+    if( tmpMap.Contains(E) )
+      continue;
+    tmpMap.Add(E);
+    SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
+    MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
+    std::vector<smIdType> aVec = (*anIt).second;
+    nb0d_e += aVec[SMDSEntity_Node];
+    nb1d_e += std::max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
+    if(IsFirst) {
+      IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
+      IsFirst = false;
+    }
+  }
+  tmpMap.Clear();
+
+  double ELen = sqrt(2.* ( fullArea/(nbtri+nbqua*2) ) / sqrt(3.0) );
+
+  GProp_GProps G;
+  BRepGProp::VolumeProperties(aShape,G);
+  double aVolume = G.Mass();
+  double tetrVol = 0.1179*ELen*ELen*ELen;
+  double CoeffQuality = 0.9;
+  smIdType nbVols = (smIdType)aVolume/tetrVol/CoeffQuality;
+  smIdType nb1d_f = (nbtri*3 + nbqua*4 - nb1d_e) / 2;
+  smIdType nb1d_in = (smIdType) ( nbVols*6 - nb1d_e - nb1d_f ) / 5;
+  std::vector<smIdType> aVec(SMDSEntity_Last);
+  for(smIdType i=0; i<SMDSEntity_Last; i++) aVec[i]=0;
+  if( IsQuadratic ) {
+    aVec[SMDSEntity_Node] = nb1d_in/6 + 1 + nb1d_in;
+    aVec[SMDSEntity_Quad_Tetra] = nbVols - nbqua*2;
+    aVec[SMDSEntity_Quad_Pyramid] = nbqua;
+  }
+  else {
+    aVec[SMDSEntity_Node] = nb1d_in/6 + 1;
+    aVec[SMDSEntity_Tetra] = nbVols - nbqua*2;
+    aVec[SMDSEntity_Pyramid] = nbqua;
+  }
+  SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
+  aResMap.insert(std::make_pair(sm,aVec));
 
-istream & operator >> (istream & load, GHS3DPRLPlugin_GHS3DPRL & hyp)
-{
-  return hyp.LoadFrom( load );
+  return true;
 }