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