1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : GHS3DPRLPlugin_GHS3DPRL.cxx
22 // Author : Christian VAN WAMBEKE (CEA) (from Hexotic plugin Lioka RAZAFINDRAZAKA)
25 #include "GHS3DPRLPlugin_GHS3DPRL.hxx"
26 #include "GHS3DPRLPlugin_Hypothesis.hxx"
28 #include <SMDS_MeshElement.hxx>
29 #include <SMDS_MeshNode.hxx>
30 #include <SMESH_subMesh.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <OSD_File.hxx>
35 #include "utilities.h"
38 #include <sys/sysinfo.h>
48 #include <SMESH_Gen.hxx>
49 #include <SMESHDS_Mesh.hxx>
50 #include <SMESH_ControlsDef.hxx>
53 #include <Standard_ProgramError.hxx>
54 #include <TCollection_AsciiString.hxx>
55 #include <TopTools_MapOfShape.hxx>
57 #include <BRepGProp.hxx>
58 #include <GProp_GProps.hxx>
62 static void removeFile( const TCollection_AsciiString& fileName )
65 OSD_File( fileName ).Remove();
67 catch ( Standard_ProgramError ) {
68 MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
72 //=============================================================================
73 GHS3DPRLPlugin_GHS3DPRL::GHS3DPRLPlugin_GHS3DPRL(int hypId, int studyId, SMESH_Gen* gen)
74 : SMESH_3D_Algo(hypId, studyId, gen)
76 MESSAGE("GHS3DPRLPlugin_GHS3DPRL::GHS3DPRLPlugin_GHS3DPRL");
77 _name = "MG-Tetra Parallel";
78 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
81 _compatibleHypothesis.push_back(GHS3DPRLPlugin_Hypothesis::GetHypType());
84 //=============================================================================
85 GHS3DPRLPlugin_GHS3DPRL::~GHS3DPRLPlugin_GHS3DPRL()
87 MESSAGE("GHS3DPRLPlugin_GHS3DPRL::~GHS3DPRLPlugin_GHS3DPRL");
90 //=============================================================================
91 bool GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis
93 const TopoDS_Shape& aShape,
94 SMESH_Hypothesis::Hypothesis_Status& aStatus)
96 //MESSAGE("GHS3DPRLPlugin_GHS3DPRL::CheckHypothesis");
99 list<const SMESHDS_Hypothesis*>::const_iterator itl;
100 const SMESHDS_Hypothesis* theHyp;
102 const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape);
103 int nbHyp = hyps.size();
106 aStatus = SMESH_Hypothesis::HYP_OK;
107 return true; // can work with no hypothesis
111 theHyp = (*itl); // use only the first hypothesis
113 string hypName = theHyp->GetName();
114 if (hypName == GHS3DPRLPlugin_Hypothesis::GetHypType())
116 _hypothesis = static_cast<const GHS3DPRLPlugin_Hypothesis*> (theHyp);
118 aStatus = SMESH_Hypothesis::HYP_OK;
121 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
123 return aStatus == SMESH_Hypothesis::HYP_OK;
126 //=======================================================================
127 // static bool writeGHS3DPRLFiles (const TCollection_AsciiString & GHS3DPRL_In,
128 // SMESHDS_Mesh * theMesh,
129 // map <int,int> & theSmdsToGHS3DPRLIdMap,
130 // map <int,const SMDS_MeshNode*> & theGHS3DPRLIdToNodeMap)
134 // TCollection_AsciiString namefile(GHS3DPRL_In);
135 // namefile+=".points";
136 // removeFile(namefile);
138 // theFile.open(namefile.ToCString(),ios::out);
140 // Ok = theFile.is_open();
142 // Ok=theFile.rdbuf()->is_open();
146 // INFOS("Can't write into "<<namefile.ToCString());
149 // cout<<endl<<"writeGHS3DPRLFiles version tetra_hpc v2.1.11 "<<endl;
150 // cout<<endl<<"Creating GHS3DPRL processed mesh file : "<<namefile<<endl;
152 // int nbVertices=theMesh->NbNodes();
153 // int nbFaces=theMesh->NbFaces(); //triangles or quadrangles
154 // const char* space=" ";
155 // //const int dummyint=1; //nrs,nsd,refnum=1 (for wrap)
157 // // Writing SMESH points into GHS3DPRL File.points
158 // theFile<<nbVertices<<endl;
160 // int aSmdsNodeID=1;
161 // const SMDS_MeshNode* node_2;
162 // SMDS_NodeIteratorPtr itOnNode=theMesh->nodesIterator();
163 // //int ifam=100;//test famille
164 // theFile.precision(15); theFile.setf(ios::scientific,ios::floatfield);
165 // //cout<<"set precision 15 on float\n";
166 // while (itOnNode->more())
168 // node_2 = itOnNode->next();
169 // theSmdsToGHS3DPRLIdMap.insert(map <int,int>::value_type(node_2->GetID(),aSmdsNodeID));
170 // theGHS3DPRLIdToNodeMap.insert(map <int,const SMDS_MeshNode*>::value_type(aSmdsNodeID,node_2));
171 // theFile<<node_2->X()<<space<<node_2->Y()<<space<<node_2->Z()<<space<<ifam<<endl;
173 // //if (aSmdsNodeID==11) ifam++;
175 // //no specified points;
178 // namefile=GHS3DPRL_In+".faces";
179 // removeFile(namefile);
180 // theFile.open(namefile.ToCString(),ios::out);
182 // Ok=theFile.is_open();
184 // Ok=theFile.rdbuf()->is_open();
188 // INFOS("Can't write into "<<namefile.ToCString());
191 // cout<<endl<<"Creating GHS3DPRL processed mesh file : "<<namefile<<endl;
193 // // Writing SMESH faces into GHS3DPRL File.faces
194 // theFile<<nbFaces<<" 0"<<endl; //NB_ELEMS DUMMY_INT
195 // //" 0" is a reserved parameter
197 // const SMDS_MeshElement* aFace;
198 // map<int,int>::const_iterator itOnSmdsNode;
199 // SMDS_ElemIteratorPtr itOnFaceNode;
200 // SMDS_FaceIteratorPtr itOnSmdsFace = theMesh->facesIterator();
201 // long nbNoTriangles=0;
204 // while (itOnSmdsFace->more())
206 // aFace=itOnSmdsFace->next();
207 // itOnFaceNode=aFace->nodesIterator();
208 // const int nbNodes=aFace->NbNodes();
209 // if (nbNodes!=3) nbNoTriangles++;
211 // theFile<<nbNodes<<space; // NB_NODES
212 // while (itOnFaceNode->more())
214 // aSmdsNodeID=itOnFaceNode->next()->GetID();
215 // itOnSmdsNode=theSmdsToGHS3DPRLIdMap.find(aSmdsNodeID);
216 // ASSERT(itOnSmdsNode!=theSmdsToGHS3DPRLIdMap.end());
217 // theFile<<space<<(*itOnSmdsNode).second; //NODE_1 NODE_2 ...
219 // //(NB_NODES+1) times: DUMMY_INT
220 // //if (ifaces==11) ifam++;
221 // theFile<<space<<ifam;
222 // for ( int i=1; i<=nbNodes; i++) theFile<<space<<200+i;
227 // cout<<"Processed mesh files created, they contains :\n";
228 // cout<<" "<<nbVertices<<" vertices\n";
229 // if (nbNoTriangles==0)
230 // cout<<" "<<nbFaces<<" faces\n\n";
232 // cout<<" "<<nbFaces<<" faces with "<<nbNoTriangles<<"faces no triangles\n\n";
237 //=======================================================================
239 #define GHS3DPRLPlugin_BUFLENGTH 256
240 #define GHS3DPRLPlugin_ReadLine(aPtr,aBuf,aFile,aLineNb) \
241 { aPtr = fgets( aBuf, GHS3DPRLPlugin_BUFLENGTH - 2, aFile ); aLineNb++; DUMP(endl); }
244 //=============================================================================
245 // Pass parameters to GHS3DPRL
246 void GHS3DPRLPlugin_GHS3DPRL::SetParameters(const GHS3DPRLPlugin_Hypothesis* hyp)
249 MESSAGE("GHS3DPRLPlugin_GHS3DPRL::SetParameters");
250 _MEDName = hyp->GetMEDName(); //"DOMAIN\0"
251 _NbPart = hyp->GetNbPart();
252 _KeepFiles = hyp->GetKeepFiles();
253 _Background = hyp->GetBackground();
254 _Multithread = hyp->GetMultithread();
255 //_ToMergeSubdomains = hyp->GetToMergeSubdomains();
256 _Gradation = hyp->GetGradation();
257 _MinSize = hyp->GetMinSize();
258 _MaxSize = hyp->GetMaxSize();
262 //=======================================================================
263 //before launching salome
264 //SALOME_TMP_DIR (for keep tepal intermediates files) could be set in user's directories
265 static TCollection_AsciiString getTmpDir()
267 TCollection_AsciiString aTmpDir;
268 char *Tmp_dir = getenv("SALOME_TMP_DIR");
269 if (Tmp_dir == NULL) Tmp_dir = getenv("TMP");
274 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
276 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
282 aTmpDir = TCollection_AsciiString("C:\\");
284 aTmpDir = TCollection_AsciiString("/tmp/");
290 //=============================================================================
291 // Here we are going to use the GHS3DPRL mesher for tetra-hpc (formerly tepal in v3 (2014))
292 bool GHS3DPRLPlugin_GHS3DPRL::Compute(SMESH_Mesh& theMesh,
293 const TopoDS_Shape& theShape)
296 TCollection_AsciiString pluginerror("ghs3dprl: ");
297 SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
298 //cout<<"GetMeshDS done\n";
299 if (_countSubMesh==0){
300 MESSAGE("GHS3DPRLPlugin_GHS3DPRL::Compute for tetra-hpc");
302 TopExp_Explorer expf(meshDS->ShapeToMesh(), TopAbs_SOLID);
303 for ( ; expf.More(); expf.Next() ) _countTotal++;
306 //cout<<"Compute _countSubMesh "<<_countSubMesh<<endl;
307 //no stuff if multiples submesh, multiple call compute
308 //mesh all in one pass tepal (the last)
309 if (_countSubMesh != _countTotal ) return true;
312 if (_hypothesis==NULL){
313 pluginerror += "No existing parameters/hypothesis for GHS3DPRL";
314 cout <<"\n"<<pluginerror<<"\n\n";
315 error(COMPERR_ALGO_FAILED, pluginerror.ToCString() );
318 SetParameters(_hypothesis);
319 cout << "\n" << _name << " parameters:" << endl;
320 cout << " generic path/name of MED Files = " << _MEDName << endl;
321 cout << " number of partitions = " << _NbPart << endl;
322 cout << " gradation = " << _Gradation << endl;
323 cout << " min_size = " << _MinSize << endl;
324 cout << " max_size = " << _MaxSize << endl;
325 cout << " keep intermediates files (from tetra-hpc) = " << _KeepFiles << endl;
326 cout << " background (from tetra-hpc) = " << _Background << "\n";
327 cout << " multithread = " << _Multithread << "\n\n";
329 //string tmpDir=getTmpDir_new();
330 TCollection_AsciiString
335 run_GHS3DPRL("tetrahpc2med "),
345 casenamemed; //_MEDName.c_str());
347 casenamemed += (char *)_MEDName.c_str();
348 int n=casenamemed.SearchFromEnd('/');
350 path=casenamemed.SubString(1,n);
351 casenamemed=casenamemed.SubString(n+1,casenamemed.Length());
356 if (casenamemed.Length()>20){
357 casenamemed=casenamemed.SubString(1,20);
358 cerr<<"MEDName truncated (no more 20 characters) = "<<casenamemed<<endl;
360 //cout<<"path="<<path<<endl;
361 //cout<<"casenamemed="<<casenamemed<<endl;
363 map <int,int> aSmdsToGHS3DPRLIdMap;
364 map <int,const SMDS_MeshNode*> aGHS3DPRLIdToNodeMap;
365 GHS3DPRL_In = path + "GHS3DPRL";
366 GHS3DPRL_Out = path + casenamemed;
367 GHS3DPRL_Outxml = path + casenamemed + ".xml"; //master file
369 Gradation=_Gradation;
374 //tetrahpc2med --casename=/home/whoami/tmp/GHS3DPRL --number=5 --medname=DOMAIN
375 // --gradation=1.05 --min_size=1e-3 --max_size=1e-2
376 // --verbose=0 --menu=no --launchtetra=yes;
378 run_GHS3DPRL = run_GHS3DPRL +
379 " --casename=" + GHS3DPRL_In +
380 " --number=" + NbPart +
381 " --medname=" + GHS3DPRL_Out +
382 " --launchtetra=yes" +
383 " --gradation=" + Gradation +
384 " --min_size=" + MinSize +
385 " --max_size=" + MaxSize +
387 if (_Background) run_GHS3DPRL += " --background=yes"; else run_GHS3DPRL += " --background=no";
388 if (_Multithread) run_GHS3DPRL += " --multithread=yes"; else run_GHS3DPRL += " --multithread=no";
389 run_nokeep_files = rm +GHS3DPRL_In + "* " + path + "tetrahpc.log";
390 system( run_nokeep_files.ToCString() ); //clean files
391 run_nokeep_files = rm + GHS3DPRL_In + "* ";
393 cout<<"GHS3DPRL command :\n "<<run_GHS3DPRL.ToCString()<<endl;
394 fileskinmesh=path + "GHS3DPRL.mesh";
395 cout<<" Write input file for tetra_hpc.exe "<<fileskinmesh<<"...";
396 GHS3DPRL_Out = path + casenamemed;
397 removeFile( GHS3DPRL_Outxml ); //only the master xml file
398 //Ok=writeGHS3DPRLFiles(GHS3DPRL_In, meshDS, aSmdsToGHS3DPRLIdMap, aGHS3DPRLIdToNodeMap);
399 bool toCreateGroups = false;
400 theMesh.ExportGMF(fileskinmesh.ToCString(), meshDS, toCreateGroups );
403 // cout<<" ...NOT done\n";
404 // pluginerror = pluginerror + "problem writing input tepal files " + GHS3DPRL_In + ".mesh";
406 //Ecriture dans un fichier MED ?v2.? meme si not Ok
407 //create empty file -> avoid warning message
408 //med_idt fid=MEDouvrir((const char *)fileskinmed.ToCString(),MED_CREATION);
409 //med_err ret=MEDfermer(fid);
410 //fileskinmed=fileskinmed + "cp /home/wambeke/empty.med "+ path + "GHS3DPRL_skin.med";
411 //system( fileskinmed.ToCString() );
412 fileskinmed=path + "GHS3DPRL_skin.med";
413 cout<<" Write file "<<fileskinmed<<"...";
414 theMesh.ExportMED(fileskinmed.ToCString(),"SKIN_INITIAL",true,1);
415 cout<<" ...done\n\n";
418 //sometimes it is better to wait flushing files on slow filesystem...
420 //launch tetrahpc2med which launch mg-tetra_hpc.py which launch mg-tetra_hpc(_mpi?).exe
421 system( run_GHS3DPRL.ToCString() );
425 pluginerror = pluginerror + "backgrounding... plugin is not waiting for output files "+ casenamemed + "_*.med";
426 cout<<pluginerror<<endl;
427 error(COMPERR_ALGO_FAILED, pluginerror.ToCString());
428 return false; //but it is not a problem but if true my message is overwritten
429 //return true; //but it is not a problem,
432 // read a result, GHS3DPRL_Out is the name of master file (previous xml format)
433 FILE * aResultFile = fopen( GHS3DPRL_Outxml.ToCString(), "r" );
435 //Ok = readResult( aResultFile, meshDS, theShape, aGHS3DPRLIdToNodeMap, GHS3DPRL_Out, _nodeRefNumber );
437 Ok = false; //but it is not a problem but if true my message is overwritten
439 cout<<"GHS3DPRL OK output master file "<<casenamemed<<".xml exist !\n\n";
440 pluginerror = pluginerror + "new tetraedra not in memory, but stored in files "+ casenamemed + "_*.med";
441 cout<<pluginerror<<endl;
442 error(COMPERR_ALGO_FAILED, pluginerror.ToCString());
443 if (!_KeepFiles) system( run_nokeep_files.ToCString() );
446 Ok = false; //it is a problem AND my message is NOT overwritten
447 pluginerror = pluginerror + "output master file " + casenamemed + ".xml do not exist";
448 cout<<pluginerror<<endl;
449 error(COMPERR_ALGO_FAILED, pluginerror.ToCString() );
450 cout<<"GHS3DPRL KO output files "<<GHS3DPRL_Out<<" do not exist ! see intermediates files keeped:\n";
451 TCollection_AsciiString run_list_files("ls -alt ");
452 run_list_files += GHS3DPRL_Out + "* " + GHS3DPRL_In + "* " + path + "tetrahpc.log";
453 system( run_list_files.ToCString() );
464 //=============================================================================
468 //=============================================================================
469 bool GHS3DPRLPlugin_GHS3DPRL::Evaluate(SMESH_Mesh& aMesh,
470 const TopoDS_Shape& aShape,
471 MapShapeNbElems& aResMap)
473 int nbtri = 0, nbqua = 0;
474 double fullArea = 0.0;
475 for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
476 TopoDS_Face F = TopoDS::Face( exp.Current() );
477 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
478 MapShapeNbElemsItr anIt = aResMap.find(sm);
479 if( anIt==aResMap.end() ) {
480 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
481 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,
482 "Submesh can not be evaluated",this));
485 std::vector<int> aVec = (*anIt).second;
486 nbtri += Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
487 nbqua += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
489 BRepGProp::SurfaceProperties(F,G);
490 double anArea = G.Mass();
494 // collect info from edges
495 int nb0d_e = 0, nb1d_e = 0;
496 bool IsQuadratic = false;
498 TopTools_MapOfShape tmpMap;
499 for (TopExp_Explorer exp(aShape, TopAbs_EDGE); exp.More(); exp.Next()) {
500 TopoDS_Edge E = TopoDS::Edge(exp.Current());
501 if( tmpMap.Contains(E) )
504 SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
505 MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
506 std::vector<int> aVec = (*anIt).second;
507 nb0d_e += aVec[SMDSEntity_Node];
508 nb1d_e += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
510 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
516 double ELen = sqrt(2.* ( fullArea/(nbtri+nbqua*2) ) / sqrt(3.0) );
519 BRepGProp::VolumeProperties(aShape,G);
520 double aVolume = G.Mass();
521 double tetrVol = 0.1179*ELen*ELen*ELen;
522 double CoeffQuality = 0.9;
523 int nbVols = (int)aVolume/tetrVol/CoeffQuality;
524 int nb1d_f = (nbtri*3 + nbqua*4 - nb1d_e) / 2;
525 int nb1d_in = (int) ( nbVols*6 - nb1d_e - nb1d_f ) / 5;
526 std::vector<int> aVec(SMDSEntity_Last);
527 for(int i=0; i<SMDSEntity_Last; i++) aVec[i]=0;
529 aVec[SMDSEntity_Node] = nb1d_in/6 + 1 + nb1d_in;
530 aVec[SMDSEntity_Quad_Tetra] = nbVols - nbqua*2;
531 aVec[SMDSEntity_Quad_Pyramid] = nbqua;
534 aVec[SMDSEntity_Node] = nb1d_in/6 + 1;
535 aVec[SMDSEntity_Tetra] = nbVols - nbqua*2;
536 aVec[SMDSEntity_Pyramid] = nbqua;
538 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
539 aResMap.insert(std::make_pair(sm,aVec));