Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/IO/MeshDetect.cpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.,
7 : : 2022-2024 J. Bakosi
8 : : All rights reserved. See the LICENSE file for details.
9 : : \brief Unstructured mesh file format detection
10 : : \details Unstructured mesh file format detection functions.
11 : : */
12 : : // *****************************************************************************
13 : :
14 : : #include <string>
15 : : #include <fstream>
16 : : #include <stdexcept>
17 : :
18 : : #include "MeshDetect.hpp"
19 : : #include "Exception.hpp"
20 : : #include "Reader.hpp"
21 : :
22 : : namespace tk {
23 : :
24 : : MeshReaderType
25 : 48 : detectInput( const std::string& filename )
26 : : // *****************************************************************************
27 : : // Detect input mesh file type
28 : : //! \param[in] filename File to open and detect its type
29 : : //! \return enum specifying the mesh reader type
30 : : // *****************************************************************************
31 : : {
32 : 48 : std::ifstream inFile;
33 : :
34 : : // Check if file exists, throw exception if it does not
35 [ + - ]: 48 : inFile.open( filename, std::ifstream::in );
36 [ - + ][ - - ]: 48 : ErrChk( inFile.good(), "Failed to open file: " + filename );
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
37 : :
38 [ + - ]: 48 : inFile.get();
39 [ - + ][ - - ]: 48 : ErrChk( !inFile.fail(), "Failed to read from file: " + filename );
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
40 : :
41 : : // Close it
42 [ + - ]: 48 : inFile.close();
43 [ - + ][ - - ]: 48 : ErrChk( !inFile.fail(), "Failed to close file: " + filename );
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ][ - - ]
44 : :
45 : : // Get first three letters from input file
46 [ + - ][ + - ]: 96 : std::string s( Reader( filename ).firstline().substr(0,4) );
[ + - ]
47 : :
48 [ + + ]: 48 : if ( s.find("$Me") != std::string::npos ) {
49 : : return MeshReaderType::GMSH;
50 [ + + ][ - + ]: 53 : } else if ( s.find("CDF") != std::string::npos ||
51 : : s.find("HDF") != std::string::npos ) {
52 : 33 : return MeshReaderType::EXODUSII;
53 [ + + ]: 10 : } else if ( s.find("*nd") != std::string::npos ) {
54 : : return MeshReaderType::ASC;
55 [ + + ]: 6 : } else if ( s.find(" ") != std::string::npos ) {
56 : : return MeshReaderType::UGRID;
57 [ + + ]: 5 : } else if ( s.find(" npo") != std::string::npos ) {
58 : : return MeshReaderType::RDGFLO;
59 [ + + ]: 4 : } else if ( s.find("Mes") != std::string::npos ) {
60 : : return MeshReaderType::MEDIT;
61 : : } else {
62 : : try {
63 : : // cppcheck-suppress ignoredReturnValue
64 : : std::stoi(s); // try to convert to an integer
65 [ - - ]: 0 : } catch ( const std::invalid_argument& ) {
66 [ - - ][ - - ]: 0 : Throw( "Input mesh file type could not be determined from header: " +
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ]
67 : : filename );
68 : 0 : }
69 : : // could also catch std::out_of_range, the other exception potentially
70 : : // thrown by std::stoi(), but a three-digit integer will always fit into int
71 : :
72 : : // if we got here, the above string-to-integer conversion succeeded
73 : : return MeshReaderType::NETGEN;
74 : : }
75 : 48 : }
76 : :
77 : : MeshWriterType
78 : 19 : pickOutput( const std::string& filename )
79 : : // *****************************************************************************
80 : : // Determine output mesh file type
81 : : //! \param[in] filename Filename to pick its type based on extension given
82 : : //! \return enum specifying the mesh writer type
83 : : // *****************************************************************************
84 : : {
85 : : // Get extension of input file name
86 : : std::string fn = filename;
87 [ + - ]: 19 : std::string ext( fn.substr(fn.find_last_of(".") + 1) );
88 : :
89 [ + + ]: 19 : if ( ext == "msh" ) {
90 : : return MeshWriterType::GMSH;
91 [ + + ][ - + ]: 19 : } else if ( ext == "exo" || ext == "h5" ) {
92 : 11 : return MeshWriterType::EXODUSII;
93 [ - + ]: 4 : } else if ( ext == "mesh" ) {
94 : : return MeshWriterType::NETGEN;
95 : : } else {
96 [ - - ][ - - ]: 0 : Throw( "Output mesh file type could not be determined from extension of "
[ - - ][ - - ]
[ - - ][ - - ]
[ - - ]
97 : : "filename '" + filename + "'; valid extensions are: "
98 : : "'msh' for Gmsh, 'exo' or 'h5' for ExodusII, 'mesh' for Netgen's "
99 : : "neutral" );
100 : : }
101 : : }
102 : :
103 : : } // tk::
|