Salome HOME
cb348a508a5e565a6b79720c7e2634874c85c85a
[plugins/ghs3dprlplugin.git] / src / GHS3DPRLPlugin / MG_TetraHPC_API.cxx
1 // Copyright (C) 2004-2022  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "MG_TetraHPC_API.hxx"
21
22 #include <SMESH_Comment.hxx>
23 #include <SMESH_File.hxx>
24 #include <SMESH_MGLicenseKeyGen.hxx>
25 #include <Utils_SALOME_Exception.hxx>
26
27 #include <cstring>
28 #include <iostream>
29 #include <iterator>
30 #include <vector>
31
32 #ifdef USE_MG_LIBS
33
34 extern "C"{
35 #include <meshgems/meshgems.h>
36 #include <meshgems/tetra_hpc.h>
37 }
38
39 struct MG_TetraHPC_API::LibData
40 {
41   // MG objects
42   context_t *           _context;
43   tetra_hpc_session_t * _session;
44   mesh_t *              _tria_mesh;
45   sizemap_t *           _sizemap;
46   mesh_t *              _tetra_mesh;
47
48   // data to pass to MG
49   std::vector<double> _xyz;
50   std::vector<double> _nodeSize; // required nodes
51   std::vector<int>    _edgeNodes;
52   int                 _nbRequiredEdges;
53   std::vector<int>    _triaNodes;
54   int                 _nbRequiredTria;
55
56   int                 _count;
57   volatile bool&      _cancelled_flag;
58   std::string         _errorStr;
59   double&             _progress;
60
61   LibData( volatile bool & cancelled_flag, double& progress )
62     : _context(0), _session(0), _tria_mesh(0), _sizemap(0), _tetra_mesh(0),
63       _nbRequiredEdges(0), _nbRequiredTria(0),
64       _cancelled_flag( cancelled_flag ), _progress( progress )
65   {
66   }
67   // methods setting callbacks implemented after callback definitions
68   void Init();
69   bool Compute( const std::string& outFile );
70
71   ~LibData()
72   {
73     if ( _tetra_mesh )
74       tetra_hpc_regain_mesh( _session, _tetra_mesh );
75     if ( _session )
76       tetra_hpc_session_delete( _session );
77     if ( _tria_mesh )
78       mesh_delete( _tria_mesh );
79     if ( _sizemap )
80       sizemap_delete( _sizemap );
81     if ( _context )
82       context_delete( _context );
83
84     _tetra_mesh = 0;
85     _session = 0;
86     _tria_mesh = 0;
87     _sizemap = 0;
88     _context = 0;
89   }
90
91   void AddError( const char *txt )
92   {
93     if ( txt )
94       _errorStr += txt;
95   }
96
97   const std::string& GetErrors()
98   {
99     return _errorStr;
100   }
101
102   void MG_Error(const char* txt="")
103   {
104     SMESH_Comment msg("\nMeshGems error. ");
105     msg << txt << "\n";
106     AddError( msg.c_str() );
107   }
108
109   bool SetParam( const std::string& param, const std::string& value )
110   {
111     status_t ret = tetra_hpc_set_param( _session, param.c_str(), value.c_str() );
112 #ifdef _DEBUG_
113     //std::cout << param << " = " << value << std::endl;
114 #endif
115     return ( ret == STATUS_OK );
116   }
117
118   bool Cancelled()
119   {
120     return _cancelled_flag;
121   }
122
123   int ReadNbSubDomains()
124   {
125     integer nb = 0;
126     status_t ret = mesh_get_subdomain_count( _tetra_mesh, & nb );
127
128     if ( ret != STATUS_OK ) MG_Error("mesh_get_subdomain_count problem");
129     return nb;
130   }
131
132   int ReadNbNodes()
133   {
134     integer nb = 0;
135     status_t ret = mesh_get_vertex_count( _tetra_mesh, & nb );
136
137     if ( ret != STATUS_OK ) MG_Error("mesh_get_vertex_count problem");
138     return nb;
139   }
140
141   int ReadNbEdges()
142   {
143     integer nb = 0;
144     status_t ret = mesh_get_edge_count( _tetra_mesh, & nb );
145
146     if ( ret != STATUS_OK ) MG_Error("mesh_get_edge_count problem");
147     return nb;
148   }
149
150   int ReadNbTria()
151   {
152     integer nb = 0;
153     status_t ret = mesh_get_triangle_count( _tetra_mesh, & nb );
154
155     if ( ret != STATUS_OK ) MG_Error("mesh_get_triangle_count problem");
156     return nb;
157   }
158
159   int ReadNbQuads()
160   {
161     integer nb = 0;
162     status_t ret = mesh_get_quadrangle_count( _tetra_mesh, & nb );
163
164     if ( ret != STATUS_OK ) MG_Error("mesh_get_quadrangle_count problem");
165     return nb;
166   }
167
168   int ReadNbTetra()
169   {
170     integer nb = 0;
171     status_t ret = mesh_get_tetrahedron_count( _tetra_mesh, & nb );
172
173     if ( ret != STATUS_OK ) MG_Error("mesh_get_tetrahedron_count problem");
174     return nb;
175   }
176
177   int ReadNbHexa()
178   {
179     integer nb = 0;
180     status_t ret = mesh_get_hexahedron_count( _tetra_mesh, & nb );
181
182     if ( ret != STATUS_OK ) MG_Error("mesh_get_hexahedron_count problem");
183     return nb;
184   }
185
186   void ResetCounter()
187   {
188     _count = 1;
189   }
190
191   void ReadSubDomain( int* nbNodes, int* faceInd, int* ori, int* domain )
192   {
193     integer tag, seed_type, seed_idx, seed_orientation;
194     status_t ret = mesh_get_subdomain_description( _tetra_mesh, _count,
195                                                    &tag, &seed_type, &seed_idx, &seed_orientation);
196
197     if ( ret != STATUS_OK ) MG_Error( "unable to get a sub-domain description");
198
199     *nbNodes = 3;
200     *faceInd = seed_idx;
201     *domain  = tag;
202     *ori     = seed_orientation;
203
204     ++_count;
205   }
206   void ReadNodeXYZ( double* x, double* y, double *z, int* /*domain*/  )
207   {
208     real coo[3];
209     status_t ret = mesh_get_vertex_coordinates( _tetra_mesh, _count, coo );
210     if ( ret != STATUS_OK ) MG_Error( "unable to get resulting vertices" );
211
212     *x = coo[0];
213     *y = coo[1];
214     *z = coo[2];
215
216     ++_count;
217   }
218
219   void ReadEdgeNodes( int* node1, int* node2, int* domain )
220   {
221     integer vtx[2], tag;
222     status_t ret = mesh_get_edge_vertices( _tetra_mesh, _count, vtx);
223     if (ret != STATUS_OK) MG_Error( "unable to get resulting edge" );
224
225     *node1 = vtx[0];
226     *node2 = vtx[1];
227
228     ret = mesh_get_edge_tag( _tetra_mesh, _count, &tag );
229     if (ret != STATUS_OK) MG_Error( "unable to get resulting edge tag" );
230
231     *domain = tag;
232
233     ++_count;
234   }
235
236   void ReadTriaNodes( int* node1, int* node2, int* node3, int* domain )
237   {
238     integer vtx[3], tag;
239     status_t ret = mesh_get_triangle_vertices( _tetra_mesh, _count, vtx);
240     if (ret != STATUS_OK) MG_Error( "unable to get resulting triangle" );
241
242     *node1 = vtx[0];
243     *node2 = vtx[1];
244     *node3 = vtx[2];
245
246     ret = mesh_get_triangle_tag( _tetra_mesh, _count, &tag );
247     if (ret != STATUS_OK) MG_Error( "unable to get resulting triangle tag" );
248
249     *domain = tag;
250
251     ++_count;
252   }
253
254   void ReadQuadNodes( int* node1, int* node2, int* node3, int* node4, int* domain )
255   {
256     integer vtx[4], tag;
257     status_t ret = mesh_get_quadrangle_vertices( _tetra_mesh, _count, vtx);
258     if (ret != STATUS_OK) MG_Error( "unable to get resulting quadrangle" );
259
260     *node1 = vtx[0];
261     *node2 = vtx[1];
262     *node3 = vtx[2];
263     *node4 = vtx[3];
264
265     ret = mesh_get_quadrangle_tag( _tetra_mesh, _count, &tag );
266     if (ret != STATUS_OK) MG_Error( "unable to get resulting quadrangle tag" );
267
268     *domain = tag;
269
270     ++_count;
271   }
272
273   void ReadTetraNodes( int* node1, int* node2, int* node3, int* node4, int* domain )
274   {
275     integer vtx[4], tag;
276     status_t ret = mesh_get_tetrahedron_vertices( _tetra_mesh, _count, vtx);
277     if (ret != STATUS_OK) MG_Error( "unable to get resulting tetrahedron" );
278
279     *node1 = vtx[0];
280     *node2 = vtx[1];
281     *node3 = vtx[2];
282     *node4 = vtx[3];
283
284     ret = mesh_get_tetrahedron_tag( _tetra_mesh, _count, &tag );
285     if (ret != STATUS_OK) MG_Error( "unable to get resulting tetrahedron tag" );
286
287     *domain = tag;
288
289     ++_count;
290   }
291
292   void ReadHexaNodes( int* node1, int* node2, int* node3, int* node4,
293                      int* node5, int* node6, int* node7, int* node8, int* domain )
294   {
295     integer vtx[8], tag;
296     status_t ret = mesh_get_hexahedron_vertices( _tetra_mesh, _count, vtx);
297     if (ret != STATUS_OK) MG_Error( "unable to get resulting hexahedron" );
298
299     *node1 = vtx[0];
300     *node2 = vtx[1];
301     *node3 = vtx[2];
302     *node4 = vtx[3];
303     *node5 = vtx[4];
304     *node6 = vtx[5];
305     *node7 = vtx[6];
306     *node8 = vtx[7];
307
308     ret = mesh_get_hexahedron_tag( _tetra_mesh, _count, &tag );
309     if (ret != STATUS_OK) MG_Error( "unable to get resulting hexahedron tag" );
310
311     *domain = tag;
312
313     ++_count;
314   }
315
316   void SetNbVertices( int nb )
317   {
318     _xyz.reserve( _xyz.capacity() + nb );
319   }
320
321   void SetNbEdges( int nb )
322   {
323     _edgeNodes.reserve( nb * 2 );
324   }
325
326   void SetNbTria( int nb )
327   {
328     _triaNodes.reserve( nb * 3 );
329   }
330
331   void SetNbReqVertices( int nb )
332   {
333     _nodeSize.reserve( nb );
334   }
335
336   void SetNbReqEdges( int nb )
337   {
338     _nbRequiredEdges = nb;
339   }
340
341   void SetNbReqTria( int nb )
342   {
343     _nbRequiredTria = nb;
344   }
345
346   void AddNode( double x, double y, double z, int /*domain*/ )
347   {
348     _xyz.push_back( x );
349     _xyz.push_back( y );
350     _xyz.push_back( z );
351   }
352
353   void AddSizeAtNode( double size )
354   {
355     _nodeSize.push_back( size );
356   }
357   
358   void AddEdgeNodes( int node1, int node2, int /*domain*/ )
359   {
360     _edgeNodes.push_back( node1 );
361     _edgeNodes.push_back( node2 );
362   }
363   
364   void AddTriaNodes( int node1, int node2, int node3, int /*domain*/ )
365   {
366     _triaNodes.push_back( node1 );
367     _triaNodes.push_back( node2 );
368     _triaNodes.push_back( node3 );
369   }
370
371   int NbNodes()
372   {
373     return _xyz.size() / 3;
374   }
375
376   double* NodeCoord( int iNode )
377   {
378     return & _xyz[ iNode * 3 ];
379   }
380
381   int NbEdges()
382   {
383     return _edgeNodes.size() / 2;
384   }
385
386   int* GetEdgeNodes( int iEdge )
387   {
388     return & _edgeNodes[ iEdge * 2 ];
389   }
390
391   int NbTriangles()
392   {
393     return _triaNodes.size() / 3;
394   }
395
396   int * GetTriaNodes( int iTria )
397   {
398     return & _triaNodes[ iTria * 3 ];
399   }
400
401   int IsVertexRequired( int iNode )
402   {
403     return ! ( iNode < int( NbNodes() - _nodeSize.size() ));
404   }
405
406   double GetSizeAtVertex( int iNode )
407   {
408     return IsVertexRequired( iNode ) ? _nodeSize[ iNode - NbNodes() + _nodeSize.size() ] : 0.;
409   }
410 };
411
412 namespace // functions called by MG library to exchange with the application
413 {
414   status_t get_vertex_count(integer * nbvtx, void *user_data)
415   {
416     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
417     *nbvtx = data->NbNodes();
418
419     return STATUS_OK;
420   }
421
422   status_t get_vertex_coordinates(integer ivtx, real * xyz, void *user_data)
423   {
424     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
425     double* coord = data->NodeCoord( ivtx-1 );
426     for (int j = 0; j < 3; j++)
427       xyz[j] = coord[j];
428
429     return STATUS_OK;
430   }
431   status_t get_edge_count(integer * nbedge, void *user_data)
432   {
433     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
434     *nbedge = data->NbEdges();
435
436     return STATUS_OK;
437   }
438
439   status_t get_edge_vertices(integer iedge, integer * vedge, void *user_data)
440   {
441     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
442     int* nodes = data->GetEdgeNodes( iedge-1 );
443     vedge[0] = nodes[0];
444     vedge[1] = nodes[1];
445
446     return STATUS_OK;
447   }
448
449   status_t get_triangle_count(integer * nbtri, void *user_data)
450   {
451     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
452     *nbtri = data->NbTriangles();
453
454     return STATUS_OK;
455   }
456
457   status_t get_triangle_vertices(integer itri, integer * vtri, void *user_data)
458   {
459     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
460     int* nodes = data->GetTriaNodes( itri-1 );
461     vtri[0] = nodes[0];
462     vtri[1] = nodes[1];
463     vtri[2] = nodes[2];
464
465     return STATUS_OK;
466   }
467
468   // status_t get_triangle_extra_vertices(integer itri, integer * typetri,
469   //                                      integer * evtri, void *user_data)
470   // {
471   //   int j;
472   //   MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
473
474   //   if (1) {
475   //     /* We want to describe a linear "3 nodes" triangle */
476   //     *typetri = MESHGEMS_MESH_ELEMENT_TYPE_TRIA3;
477   //   } else {
478   //     /* We want to describe a quadratic "6 nodes" triangle */
479   //     *typetri = MESHGEMS_MESH_ELEMENT_TYPE_TRIA6;
480   //     for (j = 0; j < 3; j++)
481   //       evtri[j] = 0;             /* the j'th quadratic vertex index of the itri'th triangle */
482   //   }
483
484   //   return STATUS_OK;
485   // }
486
487   // status_t get_tetrahedron_count(integer * nbtetra, void *user_data)
488   // {
489   //   MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
490
491   //   *nbtetra = 0;                 /* the number of tetra in your input mesh (0 if you describe a surface mesh) */
492
493   //   return STATUS_OK;
494   // }
495
496   // status_t get_tetrahedron_vertices(integer itetra, integer * vtetra,
497   //                                   void *user_data)
498   // {
499   //   int j;
500   //   MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
501
502   //   for (j = 0; j < 4; j++)
503   //     vtetra[j] = 0;              /* the j'th vertex index of the itetra'th tetrahedron */
504
505   //   return STATUS_OK;
506   // }
507
508   status_t get_vertex_required_property(integer ivtx, integer * rvtx, void *user_data)
509   {
510     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
511     *rvtx = data->IsVertexRequired( ivtx - 1 );
512
513     return STATUS_OK;
514   }
515
516   // status_t get_vertex_weight(integer ivtx, real * wvtx, void *user_data)
517   // {
518   //   MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
519   //   *wvtx = data->GetSizeAtVertex( ivtx - 1 );
520
521   //   return STATUS_OK;
522   // }
523
524   status_t my_message_cb(message_t * msg, void *user_data)
525   {
526     char *desc;
527     message_get_description(msg, &desc);
528
529     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
530     data->AddError( desc );
531
532 #ifdef _DEBUG_
533     //std::cout << desc << std::endl;
534 #endif
535
536     return STATUS_OK;
537   }
538
539   status_t my_interrupt_callback(integer *interrupt_status, void *user_data)
540   {
541     MG_TetraHPC_API::LibData* data = (MG_TetraHPC_API::LibData *) user_data;
542     *interrupt_status = ( data->Cancelled() ? INTERRUPT_STOP : INTERRUPT_CONTINUE );
543
544     //std::cout << "INTERRUPT_STOP" << std::endl;
545
546
547     return STATUS_OK;
548   }
549
550 } // end namespace
551
552
553 void MG_TetraHPC_API::LibData::Init()
554 {
555   status_t ret;
556
557   // Create the meshgems working context
558   _context = context_new();
559   if ( !_context ) MG_Error( "unable to create a new context" );
560
561   // Set the message callback for the _context.
562   ret = context_set_message_callback( _context, my_message_cb, this );
563   if ( ret != STATUS_OK ) MG_Error("in context_set_message_callback");
564
565   // Create the structure holding the callbacks giving access to triangle mesh
566   _tria_mesh = mesh_new( _context );
567   if ( !_tria_mesh ) MG_Error("unable to create a new mesh");
568
569   // Set callbacks to provide triangle mesh data
570   mesh_set_get_vertex_count( _tria_mesh, get_vertex_count, this );
571   mesh_set_get_vertex_coordinates( _tria_mesh, get_vertex_coordinates, this );
572   mesh_set_get_vertex_required_property( _tria_mesh, get_vertex_required_property, this );
573   mesh_set_get_edge_count( _tria_mesh, get_edge_count, this);
574   mesh_set_get_edge_vertices( _tria_mesh, get_edge_vertices, this );
575   mesh_set_get_triangle_count( _tria_mesh, get_triangle_count, this );
576   mesh_set_get_triangle_vertices( _tria_mesh, get_triangle_vertices, this );
577
578   // Create a tetra session
579   _session = tetra_hpc_session_new( _context );
580   if ( !_session ) MG_Error( "unable to create a new tetra_hpc session");
581
582   ret = tetra_hpc_set_interrupt_callback( _session, my_interrupt_callback, this );
583   if ( ret != STATUS_OK ) MG_Error("in tetra_set_interrupt_callback");
584
585 }
586
587 bool MG_TetraHPC_API::LibData::Compute( const std::string& outFile )
588 {
589   // MG license
590   std::string errorTxt;
591   status_t ret;
592 #if MESHGEMS_VERSION_HEX > MESHGEMS_215
593   // need to unlock Tetra license only once
594   std::string SPATIAL_LICENSE = SMESHUtils_MGLicenseKeyGen::GetKey(errorTxt);
595   ret = meshgems_tetra_hpc_unlock_product(SPATIAL_LICENSE.c_str());
596   if STATUS_IS_ERROR( ret )
597     {
598     AddError( SMESH_Comment( "Problem with SPATIAL_LICENSE to unlock Tetra_HPC: ") << errorTxt );
599     return false;
600     }
601   else
602     MESSAGE("SPATIAL_LICENSE unlock Tetra_HPC: " << ret);
603 #else
604   if ( !SMESHUtils_MGLicenseKeyGen::SignMesh( _tria_mesh, errorTxt ))
605   {
606     AddError( SMESH_Comment( "Problem with library SalomeMeshGemsKeyGenerator: ") << errorTxt );
607     return false;
608   }
609 #endif
610
611   // Set surface mesh
612   ret = tetra_hpc_set_input_mesh( _session, _tria_mesh );
613   if ( ret != STATUS_OK ) MG_Error( "unable to set surface mesh");
614
615   // // Set a sizemap
616   // if ( !_nodeSize.empty() )
617   // {
618   //   _sizemap = meshgems_sizemap_new( _tria_mesh, meshgems_sizemap_type_iso_mesh_vertex,
619   //                                    (void*) &get_vertex_weight, this );
620   //   if ( !_sizemap ) MG_Error("unable to create a new sizemap");
621
622   //   ret = tetra_hpc_set_sizemap( _session, _sizemap );
623   //   if ( ret != STATUS_OK ) MG_Error( "unable to set sizemap");
624   // }
625
626   //////////////////////////////////////////////////////////////////////////////////////////
627   // const char* file  =  "/tmp/ghs3d_IN.mesh";
628   // mesh_write_mesh( _tria_mesh,file);
629   // std::cout << std::endl << std::endl << "Write " << file << std::endl << std::endl << std::endl;
630
631   ret = tetra_hpc_mesh( _session );
632   if ( ret != STATUS_OK ) return false;
633
634   ret = tetra_hpc_get_mesh( _session, &_tetra_mesh);
635   if (ret != STATUS_OK) MG_Error( "unable to get resulting mesh");
636
637   mesh_write_mesh( _tetra_mesh, outFile.c_str() );
638
639   return true;
640 }
641
642 #else // ifdef USE_MG_LIBS
643
644 struct MG_TetraHPC_API::LibData // to avoid compiler warnings
645 {
646   volatile bool& _cancelled_flag;
647   double& _progress;
648   LibData(volatile bool& cancelled_flag, double& progress)
649     : _cancelled_flag{cancelled_flag}, _progress{progress}
650   {}
651 };
652
653 #endif // ifdef USE_MG_LIBS
654
655
656 //================================================================================
657 /*!
658  * \brief Constructor
659  */
660 //================================================================================
661
662 MG_TetraHPC_API::MG_TetraHPC_API(volatile bool& cancelled_flag, double& progress):
663   _nbNodes(0), _nbEdges(0), _nbFaces(0), _nbVolumes(0)
664 {
665   _useLib = false;
666   _libData = new LibData( cancelled_flag, progress );
667 #ifdef USE_MG_LIBS
668   _useLib = true;
669   _libData->Init();
670   if ( getenv("MG_TetraHPC_USE_EXE"))
671     _useLib = false;
672 #endif
673 }
674
675 //================================================================================
676 /*!
677  * \brief Destructor
678  */
679 //================================================================================
680
681 MG_TetraHPC_API::~MG_TetraHPC_API()
682 {
683 #ifdef USE_MG_LIBS
684   delete _libData;
685   _libData = 0;
686 #endif
687   std::set<int>::iterator id = _openFiles.begin();
688   for ( ; id != _openFiles.end(); ++id )
689     ::GmfCloseMesh( *id );
690   _openFiles.clear();
691 }
692
693 //================================================================================
694 /*!
695  * \brief Return the way of MG usage
696  */
697 //================================================================================
698
699 bool MG_TetraHPC_API::IsLibrary()
700 {
701   return _useLib;
702 }
703
704 //================================================================================
705 /*!
706  * \brief Switch to usage of MG-Tetra executable
707  */
708 //================================================================================
709
710 void MG_TetraHPC_API::SetUseExecutable()
711 {
712   _useLib = false;
713 }
714
715 //================================================================================
716 /*!
717  * \brief Compute the tetra mesh
718  *  \param [in] cmdLine - a command to run mg_tetra.exe
719  *  \return bool - Ok or not
720  */
721 //================================================================================
722
723 bool MG_TetraHPC_API::Compute( const std::string& cmdLine, std::string& errStr )
724 {
725   if ( _useLib ) {
726 #ifdef USE_MG_LIBS
727
728     // split cmdLine
729     std::istringstream strm( cmdLine );
730     std::istream_iterator<std::string> sIt( strm ), sEnd;
731     std::vector< std::string > args( sIt, sEnd );
732
733     std::string outFile;
734
735     // set parameters
736     std::string arg, param, value;
737     for ( size_t i = 1; i < args.size(); ++i )
738     {
739       // look for a param name; it starts from "-"
740       arg = args[i];
741       if ( arg.size() < 2 || arg[0] != '-')
742         continue;
743       while ( arg[0] == '-')
744         arg = arg.substr( 1 );
745
746       size_t eqPos = arg.find('=');
747       if ( eqPos != std::string::npos )
748       {
749         param = arg.substr( 0, eqPos );
750         value = arg.substr( eqPos+1 );
751       }
752       else
753       {
754         param = arg;
755         value = "";
756       }
757       if ( param == "out" )
758       {
759         outFile = value;
760         continue;
761       }
762       while ( i+1 < args.size() && args[i+1][0] != '-' )
763       {
764         if ( !value.empty() ) value += " ";
765         value += args[++i];
766       }
767       if ( !_libData->SetParam( param, value ))
768         std::cout << "Warning: wrong param: '" << param <<"' = '" << value << "'" << std::endl;
769     }
770
771     // compute
772     bool ok = _libData->Compute( outFile );
773
774     GetLog(); // write a log file
775     _logFile = ""; // not to write it again
776
777     return ok;
778 #endif
779   }
780
781   // add MG license key
782   {
783     std::string errorTxt, meshIn;
784     std::string key = SMESHUtils_MGLicenseKeyGen::GetKey( meshIn,
785                                                           _nbNodes, _nbEdges, _nbFaces, _nbVolumes,
786                                                           errorTxt );
787     if ( key.empty() )
788     {
789       errStr = "Problem with library SalomeMeshGemsKeyGenerator: " + errorTxt;
790       return false;
791     }
792
793     const_cast< std::string& >( cmdLine ) += " --key " + key;
794   }
795
796   std::cout << "Launching: " << cmdLine << std::endl;
797
798   int err = system( cmdLine.c_str() ); // run
799
800   if ( err )
801   {
802     errStr = SMESH_Comment("system command failed with error: ")
803       << strerror( errno );
804     return false;
805   }
806   return true;
807
808 }
809
810 //================================================================================
811 /*!
812  * \brief Prepare for reading a mesh data
813  */
814 //================================================================================
815
816 int  MG_TetraHPC_API::GmfOpenMesh(const char* theFile, int rdOrWr, int * ver, int * dim)
817 {
818   if ( _useLib ) {
819 #ifdef USE_MG_LIBS
820     return 1;
821 #endif
822   }
823   int id = ::GmfOpenMesh(theFile, rdOrWr, ver, dim );
824   _openFiles.insert( id );
825   return id;
826 }
827
828 //================================================================================
829 /*!
830  * \brief Return nb of entities
831  */
832 //================================================================================
833
834 int MG_TetraHPC_API::GmfStatKwd( int iMesh, GmfKwdCod what )
835 {
836   if ( _useLib ) {
837 #ifdef USE_MG_LIBS
838     switch ( what )
839     {
840     case GmfSubDomainFromGeom: return _libData->ReadNbSubDomains();
841     case GmfVertices:          return _libData->ReadNbNodes();
842     case GmfEdges:             return _libData->ReadNbEdges();
843     case GmfTriangles:         return _libData->ReadNbTria();
844     case GmfQuadrilaterals:    return _libData->ReadNbQuads();
845     case GmfTetrahedra:        return _libData->ReadNbTetra();
846     case GmfHexahedra:         return _libData->ReadNbHexa();
847     default:                   return 0;
848     }
849     return 0;
850 #endif
851   }
852   return ::GmfStatKwd( iMesh, what );
853 }
854
855 //================================================================================
856 /*!
857  * \brief Prepare for reading some data
858  */
859 //================================================================================
860
861 void MG_TetraHPC_API::GmfGotoKwd( int iMesh, GmfKwdCod what )
862 {
863   if ( _useLib ) {
864 #ifdef USE_MG_LIBS
865     _libData->ResetCounter();
866     return;
867 #endif
868   }
869   ::GmfGotoKwd( iMesh, what );
870 }
871
872 //================================================================================
873 /*!
874  * \brief Return index of a domain identified by a triangle normal
875  *  \param [in] iMesh - mesh file index
876  *  \param [in] what - must be GmfSubDomainFromGeom
877  *  \param [out] nbNodes - nb nodes in a face
878  *  \param [out] faceInd - face index
879  *  \param [out] ori - face orientation
880  *  \param [out] domain - domain index
881  *  \param [in] dummy - an artificial param used to discriminate from GmfGetLin() reading
882  *              a triangle
883  */
884 //================================================================================
885
886 void MG_TetraHPC_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* faceInd, int* ori, int* domain, int /*dummy*/ )
887 {
888   if ( _useLib ) {
889 #ifdef USE_MG_LIBS
890     _libData->ReadSubDomain( nbNodes, faceInd, ori, domain );
891     return;
892 #endif
893   }
894   ::GmfGetLin( iMesh, what, nbNodes, faceInd, ori, domain );
895 }
896
897 //================================================================================
898 /*!
899  * \brief Return coordinates of a next node
900  */
901 //================================================================================
902
903 void MG_TetraHPC_API::GmfGetLin(int iMesh, GmfKwdCod what,
904                              double* x, double* y, double *z, int* domain )
905 {
906   if ( _useLib ) {
907 #ifdef USE_MG_LIBS
908     _libData->ReadNodeXYZ( x, y, z, domain );
909     return;
910 #endif
911   }
912   ::GmfGetLin(iMesh, what, x, y, z, domain );
913 }
914
915 //================================================================================
916 /*!
917  * \brief Return coordinates of a next node
918  */
919 //================================================================================
920
921 void MG_TetraHPC_API::GmfGetLin(int iMesh, GmfKwdCod what,
922                              float* x, float* y, float *z, int* domain )
923 {
924   if ( _useLib ) {
925 #ifdef USE_MG_LIBS
926     double X,Y,Z;
927     _libData->ReadNodeXYZ( &X, &Y, &Z, domain );
928     *x = X;
929     *y = Y;
930     *z = Z;
931     return;
932 #endif
933   }
934   ::GmfGetLin(iMesh, what, x, y, z, domain );
935 }
936
937 //================================================================================
938 /*!
939  * \brief Return node index of a next corner
940  */
941 //================================================================================
942
943 void MG_TetraHPC_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node )
944 {
945   if ( _useLib ) {
946 #ifdef USE_MG_LIBS
947     node = 0;
948     return;
949 #endif
950   }
951   ::GmfGetLin(iMesh, what, node );
952 }
953
954 //================================================================================
955 /*!
956  * \brief Return node indices of a next edge
957  */
958 //================================================================================
959
960 void MG_TetraHPC_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node1, int* node2, int* domain )
961 {
962   if ( _useLib ) {
963 #ifdef USE_MG_LIBS
964     _libData->ReadEdgeNodes( node1, node2, domain );
965     return;
966 #endif
967   }
968   ::GmfGetLin( iMesh, what, node1, node2, domain );
969 }
970
971 //================================================================================
972 /*!
973  * \brief Return node indices of a next triangle
974  */
975 //================================================================================
976
977 void MG_TetraHPC_API::GmfGetLin(int iMesh, GmfKwdCod what,
978                              int* node1, int* node2, int* node3, int* domain )
979 {
980   if ( _useLib ) {
981 #ifdef USE_MG_LIBS
982     _libData->ReadTriaNodes( node1, node2, node3, domain );
983     return;
984 #endif
985   }
986   ::GmfGetLin(iMesh, what, node1, node2, node3, domain );
987 }
988
989 //================================================================================
990 /*!
991  * \brief Return node indices of a next tetrahedron or quadrangle
992  */
993 //================================================================================
994
995 void MG_TetraHPC_API::GmfGetLin(int iMesh, GmfKwdCod what,
996                              int* node1, int* node2, int* node3, int* node4, int* domain )
997 {
998   if ( _useLib ) {
999 #ifdef USE_MG_LIBS
1000     if ( what == GmfQuadrilaterals )
1001       _libData->ReadQuadNodes( node1, node2, node3, node4, domain );
1002     else
1003       _libData->ReadTetraNodes( node1, node2, node3, node4, domain );
1004     return;
1005 #endif
1006   }
1007   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, domain );
1008 }
1009
1010 //================================================================================
1011 /*!
1012  * \brief Return node indices of a next hexahedron
1013  */
1014 //================================================================================
1015
1016 void MG_TetraHPC_API::GmfGetLin(int iMesh, GmfKwdCod what,
1017                              int* node1, int* node2, int* node3, int* node4,
1018                              int* node5, int* node6, int* node7, int* node8,
1019                              int* domain )
1020 {
1021   if ( _useLib ) {
1022 #ifdef USE_MG_LIBS
1023     _libData->ReadHexaNodes( node1, node2, node3, node4,
1024                              node5, node6, node7, node8, domain );
1025     return;
1026 #endif
1027   }
1028   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, node5, node6, node7, node8, domain );
1029 }
1030
1031 //================================================================================
1032 /*!
1033  * \brief Prepare for passing data to MeshGems
1034  */
1035 //================================================================================
1036
1037 int  MG_TetraHPC_API::GmfOpenMesh(const char* theFile, int rdOrWr, int ver, int dim)
1038 {
1039   if ( _useLib ) {
1040 #ifdef USE_MG_LIBS
1041     return 1;
1042 #endif
1043   }
1044   int id = ::GmfOpenMesh(theFile, rdOrWr, ver, dim);
1045   _openFiles.insert( id );
1046   return id;
1047 }
1048
1049 //================================================================================
1050 /*!
1051  * \brief Set number of entities
1052  */
1053 //================================================================================
1054
1055 void MG_TetraHPC_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nb )
1056 {
1057   if ( iMesh == 1 )
1058   {
1059     switch ( what ) {
1060     case GmfVertices:  _nbNodes = nb; break;
1061     case GmfEdges:     _nbEdges = nb; break;
1062     case GmfTriangles: _nbFaces = nb; break;
1063     default:;
1064     }
1065   }
1066
1067   if ( _useLib ) {
1068 #ifdef USE_MG_LIBS
1069     switch ( what ) {
1070     case GmfVertices:          _libData->SetNbVertices( nb ); break;
1071     case GmfEdges:             _libData->SetNbEdges   ( nb ); break;
1072     case GmfRequiredEdges:     _libData->SetNbReqEdges( nb ); break;
1073     case GmfTriangles:         _libData->SetNbTria    ( nb ); break;
1074     case GmfRequiredTriangles: _libData->SetNbReqTria ( nb ); break;
1075     default:;
1076     }
1077     return;
1078 #endif
1079   }
1080   ::GmfSetKwd(iMesh, what, nb );
1081 }
1082
1083 //================================================================================
1084 /*!
1085  * \brief Add coordinates of a node
1086  */
1087 //================================================================================
1088
1089 void MG_TetraHPC_API::GmfSetLin(int iMesh, GmfKwdCod what, double x, double y, double z, int domain)
1090 {
1091   if ( _useLib ) {
1092 #ifdef USE_MG_LIBS
1093     _libData->AddNode( x, y, z, domain );
1094     return;
1095 #endif
1096   }
1097   ::GmfSetLin(iMesh, what, x, y, z, domain);
1098 }
1099
1100 //================================================================================
1101 /*!
1102  * \brief Set number of field entities
1103  *  \param [in] iMesh - solution file index
1104  *  \param [in] what - solution type
1105  *  \param [in] nbNodes - nb of entities
1106  *  \param [in] nbTypes - nb of data entries in each entity
1107  *  \param [in] type - types of the data entries
1108  *
1109  * Used to prepare to storing size at nodes
1110  */
1111 //================================================================================
1112
1113 void MG_TetraHPC_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nbNodes, int dummy, int type[] )
1114 {
1115   if ( _useLib ) {
1116 #ifdef USE_MG_LIBS
1117     if ( what == GmfSolAtVertices ) _libData->SetNbReqVertices( nbNodes );
1118     return;
1119 #endif
1120   }
1121   ::GmfSetKwd(iMesh, what, nbNodes, dummy, type );
1122 }
1123
1124 //================================================================================
1125 /*!
1126  * \brief Add solution data
1127  */
1128 //================================================================================
1129
1130 void MG_TetraHPC_API::GmfSetLin(int iMesh, GmfKwdCod what, double vals[])
1131 {
1132   if ( _useLib ) {
1133 #ifdef USE_MG_LIBS
1134     _libData->AddSizeAtNode( vals[0] );
1135     return;
1136 #endif
1137   }
1138   ::GmfSetLin(iMesh, what, vals);
1139 }
1140
1141 //================================================================================
1142 /*!
1143  * \brief Add edge nodes
1144  */
1145 //================================================================================
1146
1147 void MG_TetraHPC_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int domain )
1148 {
1149   if ( _useLib ) {
1150 #ifdef USE_MG_LIBS
1151     _libData->AddEdgeNodes( node1, node2, domain );
1152     return;
1153 #endif
1154   }
1155   ::GmfSetLin(iMesh, what, node1, node2, domain );
1156 }
1157
1158 //================================================================================
1159 /*!
1160  * \brief Add a 'required' flag
1161  */
1162 //================================================================================
1163
1164 void MG_TetraHPC_API::GmfSetLin(int iMesh, GmfKwdCod what, int id )
1165 {
1166   if ( _useLib ) {
1167 #ifdef USE_MG_LIBS
1168     return;
1169 #endif
1170   }
1171   ::GmfSetLin(iMesh, what, id );
1172 }
1173
1174 //================================================================================
1175 /*!
1176  * \brief Add triangle nodes
1177  */
1178 //================================================================================
1179
1180 void MG_TetraHPC_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int node3, int domain )
1181 {
1182   if ( _useLib ) {
1183 #ifdef USE_MG_LIBS
1184     _libData->AddTriaNodes( node1, node2, node3, domain );
1185     return;
1186 #endif
1187   }
1188   ::GmfSetLin(iMesh, what, node1, node2, node3, domain );
1189 }
1190
1191 //================================================================================
1192 /*!
1193  * \brief Close a file
1194  */
1195 //================================================================================
1196
1197 void MG_TetraHPC_API::GmfCloseMesh( int iMesh )
1198 {
1199   if ( _useLib ) {
1200 #ifdef USE_MG_LIBS
1201     return;
1202 #endif
1203   }
1204   ::GmfCloseMesh( iMesh );
1205   _openFiles.erase( iMesh );
1206 }
1207
1208 //================================================================================
1209 /*!
1210  * \brief Return true if the log is not empty
1211  */
1212 //================================================================================
1213
1214 bool MG_TetraHPC_API::HasLog()
1215 {
1216   if ( _useLib ) {
1217 #ifdef USE_MG_LIBS
1218     return !_libData->GetErrors().empty();
1219 #endif
1220   }
1221   SMESH_File file( _logFile );
1222   return file.size() > 0;
1223 }
1224
1225 //================================================================================
1226 /*!
1227  * \brief Return log contents
1228  */
1229 //================================================================================
1230
1231 std::string MG_TetraHPC_API::GetLog()
1232 {
1233   if ( _useLib ) {
1234 #ifdef USE_MG_LIBS
1235     const std::string& err = _libData->GetErrors();
1236     if ( !_logFile.empty() && !err.empty() )
1237     {
1238       SMESH_File file( _logFile, /*openForReading=*/false );
1239       file.openForWriting();
1240       file.write( err.c_str(), err.size() );
1241     }
1242     return err;
1243 #endif
1244   }
1245   std::cout << "_logFile: " << _logFile << std::endl;
1246
1247   SMESH_File file( _logFile );
1248   if (file.exists() && file.size() > 0)
1249     return file.getPos();
1250   else
1251     return "";
1252 }