Salome HOME
98974c1c909fbb48d1667403037236fccb1a30d0
[modules/homard.git] / src / FrontTrack / FrontTrack.cxx
1 // File      : FrontTrack.cxx
2 // Created   : Tue Apr 25 17:20:28 2017
3 // Author    : Edward AGAPOV (eap)
4
5 #include "FrontTrack.hxx"
6 #include "FrontTrack_NodeGroups.hxx"
7 #include "FrontTrack_Utils.hxx"
8
9 #include <MCAuto.hxx>
10 #include <MEDCouplingMemArray.hxx>
11 #include <MEDFileMesh.hxx>
12
13 #include <XAO_Xao.hxx>
14 #include <XAO_BrepGeometry.hxx>
15
16 #include <stdexcept>
17
18 #include <OSD_Parallel.hxx>
19
20 /*!
21  * \brief Relocate nodes to lie on geometry
22  *  \param [in] theInputMedFile - a MED file holding a mesh including nodes that will be
23  *         moved onto the geometry
24  *  \param [in] theOutputMedFile - a MED file to create, that will hold a modified mesh
25  *  \param [in] theNodeFiles - an array of names of files describing groups of nodes that
26  *         will be moved onto the geometry
27  *  \param [in] theXaoFileName - a path to a file in XAO format holding the geometry and
28  *         the geometrical groups.
29  *  \param [in] theIsParallel - if \c true, all processors are used to treat boundary shapes
30  *          in parallel.
31  */
32 void FrontTrack::track( const std::string&                 theInputMedFile,
33                         const std::string&                 theOutputMedFile,
34                         const std::vector< std::string > & theNodeFiles,
35                         const std::string&                 theXaoFileName,
36                         bool                               theIsParallel )
37 {
38   // check arguments
39
40   if ( theNodeFiles.empty() )
41     return;
42
43 #ifdef _DEBUG_
44   std::cout << "Input MED file:" << theInputMedFile << std::endl;
45 #endif
46   if ( !FT_Utils::fileExists( theInputMedFile ))
47     throw std::invalid_argument( "Input MED file does not exist: " + theInputMedFile );
48
49 #ifdef _DEBUG_
50   std::cout << "Output MED file:" << theOutputMedFile << std::endl;
51 #endif
52   if ( !FT_Utils::canWrite( theOutputMedFile ))
53     throw std::invalid_argument( "Can't create the output MED file: " + theOutputMedFile );
54
55   for ( size_t i = 0; i < theNodeFiles.size(); ++i )
56   {
57 #ifdef _DEBUG_
58     std::cout << "Fichier" << theNodeFiles[i] << std::endl;
59 #endif
60     if ( !FT_Utils::fileExists( theNodeFiles[i] ))
61       throw std::invalid_argument( "Input node file does not exist: " + theNodeFiles[i] );
62   }
63
64 #ifdef _DEBUG_
65   std::cout << "XAO file:" << theXaoFileName << std::endl;
66 #endif
67   if ( !FT_Utils::fileExists( theXaoFileName ))
68     throw std::invalid_argument( "Input XAO file does not exist: " + theXaoFileName );
69
70
71   // read a mesh
72
73 #ifdef _DEBUG_
74   std::cout << "Lecture du maillage" << std::endl;
75 #endif
76   MEDCoupling::MCAuto< MEDCoupling::MEDFileUMesh >
77     mfMesh( MEDCoupling::MEDFileUMesh::New( theInputMedFile ));
78   if ( mfMesh.isNull() )
79     throw std::invalid_argument( "Failed to read the input MED file: " + theInputMedFile );
80
81   MEDCoupling::DataArrayDouble * nodeCoords = mfMesh->getCoords();
82   if ( !nodeCoords || nodeCoords->empty() )
83     throw std::invalid_argument( "No nodes in the input mesh" );
84
85
86   // read a geometry
87
88 #ifdef _DEBUG_
89   std::cout << "Lecture de la geometrie" << std::endl;
90 #endif
91   XAO::Xao xao;
92   if ( !xao.importXAO( theXaoFileName ) || !xao.getGeometry() )
93     throw std::invalid_argument( "Failed to read the XAO input file: " + theXaoFileName );
94
95   XAO::BrepGeometry* xaoGeom = dynamic_cast<XAO::BrepGeometry*>( xao.getGeometry() );
96   if ( !xaoGeom || xaoGeom->getTopoDS_Shape().IsNull() )
97     throw std::invalid_argument( "Failed to get a BREP shape from the XAO input file" );
98
99
100   // read groups of nodes and associate them with boundary shapes using names (no projection so far)
101
102 #ifdef _DEBUG_
103   std::cout << "Lecture des groupes" << std::endl;
104 #endif
105   FT_NodeGroups nodeGroups;
106   nodeGroups.read( theNodeFiles, &xao, nodeCoords );
107
108
109   // project nodes to the boundary shapes and change their coordinates
110
111 #ifdef _DEBUG_
112   std::cout << "Projection des noeuds" << std::endl;
113 #endif
114   OSD_Parallel::For( 0, nodeGroups.nbOfGroups(), nodeGroups, !theIsParallel );
115
116   // save the modified mesh
117
118   const int erase = 2;
119   mfMesh->write( theOutputMedFile, /*mode=*/erase );
120
121   if ( !nodeGroups.isOK() )
122     throw std::runtime_error("Unable to project some nodes");
123 }