1 // Copyright (C) 2017-2021 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
19 // File : FrontTrack.cxx
20 // Created : Tue Apr 25 17:20:28 2017
21 // Author : Edward AGAPOV (eap)
23 #include "FrontTrack.hxx"
24 #include "FrontTrack_NodeGroups.hxx"
25 #include "FrontTrack_Utils.hxx"
28 #include <MEDCouplingMemArray.hxx>
29 #include <MEDFileMesh.hxx>
31 #include <XAO_Xao.hxx>
32 #include <XAO_BrepGeometry.hxx>
36 #include <OSD_Parallel.hxx>
39 * \brief Relocate nodes to lie on geometry
40 * \param [in] theInputMedFile - a MED file holding a mesh including nodes that will be
41 * moved onto the geometry
42 * \param [in] theOutputMedFile - a MED file to create, that will hold a modified mesh
43 * \param [in] theInputNodeFiles - an array of names of files describing groups of nodes that
44 * will be moved onto the geometry
45 * \param [in] theXaoFileName - a path to a file in XAO format holding the geometry and
46 * the geometrical groups.
47 * \param [in] theIsParallel - if \c true, all processors are used to treat boundary shapes
50 void FrontTrack::track( const std::string& theInputMedFile,
51 const std::string& theOutputMedFile,
52 const std::vector< std::string > & theInputNodeFiles,
53 const std::string& theXaoFileName,
58 std::cout << "FrontTrack::track" << std::endl;
61 if ( theInputNodeFiles.empty() )
65 std::cout << "Input MED file: " << theInputMedFile << std::endl;
67 if ( !FT_Utils::fileExists( theInputMedFile ))
68 throw std::invalid_argument( "Input MED file does not exist: " + theInputMedFile );
71 std::cout << "Output MED file: " << theOutputMedFile << std::endl;
73 if ( !FT_Utils::canWrite( theOutputMedFile ))
74 throw std::invalid_argument( "Can't create the output MED file: " + theOutputMedFile );
76 std::vector< std::string > theNodeFiles ;
77 for ( size_t i = 0; i < theInputNodeFiles.size(); ++i )
80 std::cout << "Initial input node file #"<<i<<": " << theInputNodeFiles[i] << std::endl;
82 if ( !FT_Utils::fileExists( theInputNodeFiles[i] ))
83 throw std::invalid_argument( "Input node file does not exist: " + theInputNodeFiles[i] );
84 // the name of the groupe on line #1, then the numbers of nodes on line #>1
85 // keep only files with more than 1 line:
86 std::ifstream fichier(theInputNodeFiles[i].c_str());
88 unsigned int nb_lines = 0;
89 while(std::getline(fichier,s)) ++nb_lines;
90 // std::cout << ". nb_lines: " << nb_lines << std::endl;
91 if ( nb_lines >= 2 ) { theNodeFiles.push_back( theInputNodeFiles[i] ); }
94 for ( size_t i = 0; i < theNodeFiles.size(); ++i )
95 { std::cout << "Valid input node file #"<<i<<": " << theNodeFiles[i] << std::endl; }
99 std::cout << "XAO file: " << theXaoFileName << std::endl;
101 if ( !FT_Utils::fileExists( theXaoFileName ))
102 throw std::invalid_argument( "Input XAO file does not exist: " + theXaoFileName );
107 std::cout << "Lecture du maillage" << std::endl;
109 MEDCoupling::MCAuto< MEDCoupling::MEDFileUMesh >
110 mfMesh( MEDCoupling::MEDFileUMesh::New( theInputMedFile ));
111 if ( mfMesh.isNull() )
112 throw std::invalid_argument( "Failed to read the input MED file: " + theInputMedFile );
114 MEDCoupling::DataArrayDouble * nodeCoords = mfMesh->getCoords();
115 if ( !nodeCoords || nodeCoords->empty() )
116 throw std::invalid_argument( "No nodes in the input mesh" );
122 std::cout << "Lecture de la geometrie" << std::endl;
125 if ( !xao.importXAO( theXaoFileName ) || !xao.getGeometry() )
126 throw std::invalid_argument( "Failed to read the XAO input file: " + theXaoFileName );
129 std::cout << "Conversion en BREP" << std::endl;
131 XAO::BrepGeometry* xaoGeom = dynamic_cast<XAO::BrepGeometry*>( xao.getGeometry() );
132 if ( !xaoGeom || xaoGeom->getTopoDS_Shape().IsNull() )
133 throw std::invalid_argument( "Failed to get a BREP shape from the XAO input file" );
136 // read groups of nodes and associate them with boundary shapes using names (no projection so far)
139 std::cout << "Lecture des groupes" << std::endl;
141 FT_NodeGroups nodeGroups;
142 nodeGroups.read( theNodeFiles, &xao, nodeCoords );
144 std::cout << "Nombre de groupes : " << nodeGroups.nbOfGroups() << std::endl;
147 // project nodes to the boundary shapes and change their coordinates
150 std::cout << "Projection des noeuds, theIsParallel=" << theIsParallel << std::endl;
152 OSD_Parallel::For( 0, nodeGroups.nbOfGroups(), nodeGroups, !theIsParallel );
154 // save the modified mesh
157 std::cout << "Ecriture du maillage" << std::endl;
160 mfMesh->write( theOutputMedFile, /*mode=*/erase );
162 if ( !nodeGroups.isOK() )
163 throw std::runtime_error("Unable to project some nodes");