1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// *****************************************************************************
/*!
  \file      src/IO/MeshFactory.cpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.,
             2022-2025 J. Bakosi
             All rights reserved. See the LICENSE file for details.
  \brief     Unstructured mesh reader and writer factory
  \details   Unstructured mesh reader and writer factory.
*/
// *****************************************************************************

#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "MeshFactory.hpp"
#include "MeshDetect.hpp"
#include "Timer.hpp"
#include "Reader.hpp"
#include "GmshMeshReader.hpp"
#include "NetgenMeshReader.hpp"
#include "ExodusIIMeshReader.hpp"
#include "UGRIDMeshReader.hpp"
#include "RDGFLOMeshReader.hpp"
#include "MeditMeshReader.hpp"
#include "ASCMeshReader.hpp"
#include "NetgenMeshWriter.hpp"
#include "GmshMeshWriter.hpp"
#include "ExodusIIMeshWriter.hpp"
#include "DerivedData.hpp"<--- Include file: "DerivedData.hpp" not found.
#include "Reorder.hpp"<--- Include file: "Reorder.hpp" not found.
#include "XystBuildConfig.hpp"

namespace tk {

UnsMesh
readUnsMesh( const tk::Print& print,
             const std::string& filename,
             std::pair< std::string, tk::real >& timestamp )
// *****************************************************************************
//  Read unstructured mesh from file
//! \param[in] print Pretty printer
//! \param[in] filename Filename to read mesh from
//! \param[out] timestamp A time stamp consisting of a timer label (a string),
//!   and a time state (a tk::real in seconds) measuring the mesh read time
//! \return Unstructured mesh object
// *****************************************************************************
{
  print.diagstart( "Reading mesh from file '" + filename + "' ..." );

  // Read in mesh
  tk::Timer t;
 
  //! Create unstructured mesh to store mesh
  UnsMesh mesh;

  const auto meshtype = detectInput( filename );

  if (meshtype == MeshReaderType::GMSH)
    GmshMeshReader( filename ).readMesh( mesh );
  else if (meshtype == MeshReaderType::NETGEN)
    NetgenMeshReader( filename ).readMesh( mesh );
  else if (meshtype == MeshReaderType::EXODUSII)
    ExodusIIMeshReader( filename ).readMesh( mesh );
  else if (meshtype == MeshReaderType::ASC)
    ASCMeshReader( filename ).readMesh( mesh );
  else if (meshtype == MeshReaderType::UGRID)
    UGRIDMeshReader( filename ).readMesh( mesh );
  else if (meshtype == MeshReaderType::RDGFLO)
    RDGFLOMeshReader( filename ).readMesh( mesh );
  else if (meshtype == MeshReaderType::MEDIT)
    MeditMeshReader( filename ).readMesh( mesh );

  timestamp =
    std::make_pair( "Read mesh from file '" + filename + '\'', t.dsec() );

  print.diagend( "done" );

  // Return (move out) mesh object
  return mesh;
}

std::vector< std::pair< std::string, tk::real > >
writeUnsMesh( const tk::Print& print,
              const std::string& filename,
              UnsMesh& mesh,
              bool reorder )
// *****************************************************************************
//  Write unstructured mesh to file
//! \param[in] print Pretty printer
//! \param[in] filename Filename to write mesh to
//! \param[in] mesh Unstructured mesh object to write from
//! \param[in] reorder Whether to also reorder mesh nodes
//! \return Vector of time stamps consisting of a timer label (a string), and a
//!   time state (a tk::real in seconds) measuring the renumber and the mesh
//!   write time
// *****************************************************************************
{
  std::vector< std::pair< std::string, tk::real > > times;

  tk::Timer t;

  // If mesh has tetrahedra but no triangles, generate triangle connectivity
  if (!mesh.tetinpoel().empty() && mesh.triinpoel().empty()) {
    print.diagstart( "Generating missing surface mesh ..." );

    const auto& inpoel = mesh.tetinpoel();        // get tet connectivity
    auto esup = tk::genEsup( inpoel, 4 );         // elements surrounding points
    auto esuel = tk::genEsuelTet( inpoel, esup ); // elems surrounding elements
    auto& triinpoel = mesh.triinpoel();
    // collect boundary faces
    for (std::size_t e=0; e<esuel.size()/4; ++e) {
      auto mark = e*4;
      for (std::size_t f=0; f<4; ++f) {
        if (esuel[mark+f] == -1) {
          // extract triangle element connectivity from tetrahedron
          triinpoel.push_back( inpoel[ mark+tk::lpofa[f][0] ] );
          triinpoel.push_back( inpoel[ mark+tk::lpofa[f][1] ] );
          triinpoel.push_back( inpoel[ mark+tk::lpofa[f][2] ] );
        }
      }
    }

    print.diagend( "done" );
    times.emplace_back( "Generate surface mesh", t.dsec() );
    t.zero();
  }

  if (reorder) {
    print.diagstart( "Reordering mesh nodes ..." );

    // If mesh has tetrahedra elements, reorder based on those
    if (!mesh.tetinpoel().empty()) {

      auto& inpoel = mesh.tetinpoel();
      const auto psup = tk::genPsup( inpoel, 4, tk::genEsup( inpoel, 4 ) );
      auto map = tk::renumber( psup );
      tk::remap( inpoel, map );
      tk::remap( mesh.triinpoel(), map );
      tk::remap( mesh.x(), map );
      tk::remap( mesh.y(), map );
      tk::remap( mesh.z(), map );

    // If mesh has no tetrahedra elements, reorder based on triangle mesh if any
    } else Throw( "No tetrahedra in input mesh" );

    print.diagend( "done" );
    times.emplace_back( "Reorder mesh", t.dsec() );
    t.zero();
  }

  print.diagstart( "Writing mesh to file '" + filename + "' ..." );

  const auto meshtype = pickOutput( filename );

  if (meshtype == MeshWriterType::GMSH)
    GmshMeshWriter( filename ).writeMesh( mesh );
  else if (meshtype == MeshWriterType::NETGEN)
    NetgenMeshWriter( filename ).writeMesh( mesh );
  else if (meshtype== MeshWriterType::EXODUSII)
    ExodusIIMeshWriter( filename, ExoWriter::CREATE ).writeMesh( mesh );

  print.diagend( "done" );
  times.emplace_back( "Write mesh to file", t.dsec() );

  return times;
}

} // tk::