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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// *****************************************************************************
/*!
  \file      src/IO/ExodusIIMeshWriter.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     ExodusII mesh-based data writer
  \details   ExodusII mesh-based data writer class definition.
*/
// *****************************************************************************

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

#include "NoWarning/exodusII.hpp"<--- Include file: "NoWarning/exodusII.hpp" not found.

#include "ExodusIIMeshWriter.hpp"
#include "Exception.hpp"
#include "UnsMesh.hpp"<--- Include file: "UnsMesh.hpp" not found.

using tk::ExodusIIMeshWriter;

ExodusIIMeshWriter::ExodusIIMeshWriter( const std::string& filename,
                                        ExoWriter mode,
                                        int cpuwordsize,
                                        int iowordsize ) :
  m_filename( filename ), m_outFile( 0 )
// *****************************************************************************
//  Constructor: create/open Exodus II file
//! \param[in] filename File to open as ExodusII file
//! \param[in] mode ExodusII writer constructor mode: ExoWriter::CREATE for
//!   creating a new file, ExoWriter::OPEN for opening an existing file for
//!   appending
//! \param[in] cpuwordsize Set CPU word size, see ExodusII documentation
//! \param[in] iowordsize Set I/O word size, see ExodusII documentation
// *****************************************************************************
{
  // Increase verbosity from ExodusII library in debug mode
  #ifndef NDEBUG
  ex_opts( EX_DEBUG | EX_VERBOSE );
  #endif

  if (mode == ExoWriter::CREATE) {

    m_outFile = ex_create( filename.c_str(),
                           EX_CLOBBER | EX_LARGE_MODEL,
                           &cpuwordsize,
                           &iowordsize );

  } else if (mode == ExoWriter::OPEN) {

    float version;
    m_outFile = ex_open( filename.c_str(),
                         EX_WRITE, 
                         &cpuwordsize,
                         &iowordsize,
                         &version );

  } else Throw( "Unknown ExodusII writer constructor mode" );

  ErrChk( m_outFile > 0, "Failed to create/open ExodusII file: " + filename );
}

ExodusIIMeshWriter::~ExodusIIMeshWriter() noexcept
// *****************************************************************************
//  Destructor
// *****************************************************************************
{
  if ( ex_close(m_outFile) < 0 )
    printf( ">>> WARNING: Failed to close ExodusII file: %s\n",
            m_filename.c_str() );
}

void
ExodusIIMeshWriter::writeMesh( const UnsMesh& mesh ) const
// *****************************************************************************
//  Write ExodusII mesh file taking a tk::UnsMesh object
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
  writeHeader( mesh );
  writeNodes( mesh );
  writeElements( mesh );
  writeSidesets( mesh );
  writeNodesets( mesh );
  writeTimeValues( mesh.vartimes() );
  writeNodeVarNames( mesh.nodevarnames() );
  writeNodeScalars( mesh.nodevars() );
}

void
ExodusIIMeshWriter::writeHeader( const UnsMesh& mesh ) const
// *****************************************************************************
//  Write ExodusII header
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
  ErrChk(
    ex_put_init( m_outFile,
                 "Written by Xyst",
                 3,     // number of dimensions
                 static_cast< int64_t >( mesh.nnode() ),
                 static_cast< int64_t >( mesh.triinpoel().size()/3 +
                                         mesh.tetinpoel().size()/4 ),
                 static_cast< int64_t >( mesh.neblk() ),
                 static_cast< int64_t >( mesh.bnode().size() ),
                 static_cast< int64_t >( mesh.bface().size() )
               ) == 0,
    "Failed to write header to file: " + m_filename );
}

void
ExodusIIMeshWriter::writeNodes( const UnsMesh& mesh ) const
// *****************************************************************************
//  Write node coordinates to ExodusII file
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
  ErrChk( ex_put_coord( m_outFile, mesh.x().data(), mesh.y().data(),
                        mesh.z().data() ) == 0,
          "Failed to write coordinates to ExodusII file: " + m_filename );
}

void
ExodusIIMeshWriter::writeElements( const UnsMesh& mesh ) const
// *****************************************************************************
//  Write element connectivity to ExodusII file
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
  int elclass = 0;

  // For meshes that have no triangle element block (only tetrahedra), keeping
  // the order as tets first followed by triangles allows keeping the tet ids
  // associated to side sets the same when adding the missing triangle elements
  // by meshconv. Hence this order should be kept as tets first triangles next.

  writeElemBlock( elclass, 4, "TETRAHEDRA", mesh.tetinpoel() );
  writeElemBlock( elclass, 3, "TRIANGLES", mesh.triinpoel() );
}

void
ExodusIIMeshWriter::writeElemBlock( int& elclass,
                                    int64_t nnpe,
                                    const std::string& eltype,
                                    const std::vector< std::size_t >& inpoel )
const
// *****************************************************************************
//  Write element block to ExodusII file
//! \param[inout] elclass Count element class ids in file
//! \param[in] nnpe Number of nodes per element for block
//! \param[in] eltype String describing element type
//! \param[in] inpoel Element connectivity
// *****************************************************************************
{
  if (inpoel.empty()) return;

  // increase number of element classes in file
  ++elclass;

  // Compute number of edges and number of faces for triangles and tetrahedra
  int nedge = 0, nface = 0;
  if (nnpe == 4) {
    nedge = 6;
    nface = 4;
  } else if (nnpe == 3) {
    nedge = 3;
    nface = 1;
  } else Throw( "Write ExodusII element block does not support elements with "
                + std::to_string(nnpe) + " nodes" );

  // Write element block information
  ErrChk(
    ex_put_block( m_outFile,            // exo file handle
                  EX_ELEM_BLOCK,        // block type: elem block
                  elclass,              // element class id
                  eltype.c_str(),       // element block description
                  static_cast< int64_t >( inpoel.size() ) / nnpe, // nof cells
                  nnpe, // number of nodes per element
                  nedge,// number of edges per element
                  nface,// number of faces per element
                  0     // number of attributes per element
                ) == 0,
    "Failed to write " + eltype + " element block to ExodusII file: " +
    m_filename );

  // Write element connectivity with 1-based node ids
  std::vector< int > inp( inpoel.size() );
  std::size_t i = 0;
  for (auto p : inpoel) inp[ i++ ] = static_cast< int >( p+1 );
  ErrChk( ex_put_conn( m_outFile, EX_ELEM_BLOCK, elclass, inp.data(),
                       nullptr, nullptr ) == 0,
          "Failed to write " + eltype + " element connectivity to ExodusII "
          "file: " + m_filename );
}

void
ExodusIIMeshWriter::writeSidesets( const UnsMesh& mesh ) const
// *****************************************************************************
//  Write side sets and their face connectivity to ExodusII file
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
  // Write all side sets face list and connectivity in mesh
  for (const auto& s : mesh.bface()) {
    // Write side set parameters
    ErrChk( ex_put_set_param( m_outFile, EX_SIDE_SET, s.first,
                              static_cast<int64_t>(s.second.size()), 0 ) == 0,
      "Failed to write side set parameters to ExodusII file: " + m_filename );

    // ExodusII wants 32-bit integers as IDs of element ids
    std::vector< int > bface( s.second.size() );
    std::size_t i = 0;
    for (auto f : s.second) bface[ i++ ] = static_cast<int>(f)+1;
    // ExodusII wants 32-bit integers as element-relative face IDs
    const auto& fi = tk::cref_find( mesh.faceid(), s.first );
    std::vector< int > faceid( fi.size() );
    i = 0;
    for (auto f : fi) faceid[ i++ ] = static_cast<int>(f)+1;

    // Write side set data: ExodusII-file internal element ids adjacent to side
    // set and face id relative to element indicating which face is aligned with
    // the side set.
    ErrChk( ex_put_set( m_outFile, EX_SIDE_SET, s.first, bface.data(),
                        faceid.data() ) == 0,
      "Failed to write side set face list to ExodusII file: " + m_filename );
  }
}

void
ExodusIIMeshWriter::writeNodesets( const UnsMesh& mesh ) const
// *****************************************************************************
//  Write side sets and their node list to ExodusII file
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
  // Write all side set node lists in mesh
  for (const auto& s : mesh.bnode()) {
    // Write side set parameters
    ErrChk( ex_put_set_param( m_outFile, EX_NODE_SET, s.first,
                              static_cast<int64_t>(s.second.size()), 0 ) == 0,
      "Failed to write side set parameters to ExodusII file: " + m_filename );

    // ExodusII wants 32-bit integers as IDs of node ids
    std::vector< int > bnode( s.second.size() );
    std::size_t i = 0;
    for (auto n : s.second) bnode[ i++ ] = static_cast<int>(n)+1;

    // Write side set data
    ErrChk( ex_put_set( m_outFile, EX_NODE_SET, s.first, bnode.data(),
                        nullptr ) == 0,
      "Failed to write side set node list to ExodusII file: " + m_filename );
  }
}

void
ExodusIIMeshWriter::writeTimeStamp( uint64_t it, tk::real time ) const
// *****************************************************************************
//  Write time stamp to ExodusII file
//! \param[in] it Iteration number
//! \param[in] time Time
// *****************************************************************************
{
  ErrChk( ex_put_time( m_outFile, static_cast<int>(it), &time ) == 0,
          "Failed to write time stamp to ExodusII file: " + m_filename );
}

void
ExodusIIMeshWriter::writeTimeValues( const std::vector< tk::real >& tv ) const
// *****************************************************************************
//  Write time values to ExodusII file
//! \param[in] tv Time values for all time steps
// *****************************************************************************
{
   int i = 0;
   for (const auto& v : tv) {
     ErrChk( ex_put_time( m_outFile, ++i, &v ) == 0,
             "Failed to write time value for a time step to ExodusII file: " +
             m_filename );
   }
}

void
ExodusIIMeshWriter::writeNodeVarNames( const std::vector< std::string >& nv )
const
// *****************************************************************************
//  Write the names of nodal output variables to ExodusII file
//! \param[in] nv Nodal variable names
// *****************************************************************************
{
  if (!nv.empty()) {

    #if defined(__clang__)
      #pragma clang diagnostic push
      #pragma clang diagnostic ignored "-Wvla"
      #pragma clang diagnostic ignored "-Wvla-extension"
    #elif defined(STRICT_GNUC)
      #pragma GCC diagnostic push
      #pragma GCC diagnostic ignored "-Wvla"
    #endif

    ErrChk(
      ex_put_variable_param( m_outFile, EX_NODE_BLOCK,
                             static_cast< int >( nv.size() ) ) == 0,
      "Failed to write nodal output variable parameters to ExodusII file: " +
      m_filename );

    char* names[ nv.size() ];
    std::size_t i = 0;
    for (const auto& n : nv) names[ i++ ] = const_cast< char* >( n.c_str() );

    ErrChk( ex_put_variable_names( m_outFile,
                                   EX_NODE_BLOCK,
                                   static_cast< int >( nv.size() ),
                                   names ) == 0,
            "Failed to write nodal output variable names to ExodusII file: " +
            m_filename );

    #if defined(__clang__)
      #pragma clang diagnostic pop
    #elif defined(STRICT_GNUC)
      #pragma GCC diagnostic pop
    #endif
  }
}

void
ExodusIIMeshWriter::writeElemVarNames( const std::vector< std::string >& ev )
const
// *****************************************************************************
//  Write the names of element output variables to ExodusII file
//! \param[in] ev Elem variable names
// *****************************************************************************
{
  if (!ev.empty()) {

    #if defined(__clang__)
      #pragma clang diagnostic push
      #pragma clang diagnostic ignored "-Wvla"
      #pragma clang diagnostic ignored "-Wvla-extension"
    #elif defined(STRICT_GNUC)
      #pragma GCC diagnostic push
      #pragma GCC diagnostic ignored "-Wvla"
    #endif

    ErrChk(
      ex_put_variable_param( m_outFile, EX_ELEM_BLOCK,
                             static_cast< int >( ev.size() ) ) == 0,
      "Failed to write element output variable parameters to ExodusII file: " +
      m_filename );

    char* names[ ev.size() ];
    std::size_t i = 0;
    for (const auto& n : ev) names[ i++ ] = const_cast< char* >( n.c_str() );

    ErrChk( ex_put_variable_names( m_outFile,
                                   EX_ELEM_BLOCK,
                                   static_cast< int >( ev.size() ),
                                   names ) == 0,
            "Failed to write element output variable names to ExodusII file: " +
            m_filename );

    #if defined(__clang__)
      #pragma clang diagnostic pop
    #elif defined(STRICT_GNUC)
      #pragma GCC diagnostic pop
    #endif
  }
}

void
ExodusIIMeshWriter::writeNodeScalars(
  const std::vector< std::vector< std::vector< tk::real > > >& var ) const
// *****************************************************************************
//  Write multiple node scalar fields to ExodusII file at multiple time steps
//! \param[in] var Vector of nodal variables to write: inner vector: nodes,
//!   middle vector: (physics) variable, outer vector: time step
// *****************************************************************************
{
  uint64_t time = 0;
  int varid = 0;

  for (const auto& t : var) {    // for all times
    ++time;
    for (const auto& v : t) {    // for all variables
      writeNodeScalar( time, ++varid, v );
    }
    varid = 0;
  }
}

void
ExodusIIMeshWriter::writeNodeScalar( uint64_t it,
                                     int varid,
                                     const std::vector< tk::real >& var ) const
// *****************************************************************************
//  Write node scalar field to ExodusII file
//! \param[in] it Iteration number
//! \param[in] varid Variable id
//! \param[in] var Vector of variable to output
// *****************************************************************************
{
  if (!var.empty()) {
    ErrChk( ex_put_var( m_outFile,
                        static_cast< int >( it ),
                        EX_NODE_BLOCK,
                        varid,
                        1,
                        static_cast< int64_t >( var.size() ),
                        var.data() ) == 0,
            "Failed to write node scalar to ExodusII file: " + m_filename );
  }
}

void
ExodusIIMeshWriter::writeElemScalar( uint64_t it,
                                     int varid,
                                     const std::vector< tk::real >& var ) const
// *****************************************************************************
//  Write elem scalar field to ExodusII file
//! \param[in] it Iteration number
//! \param[in] varid Variable id
//! \param[in] var Vector of variable to output
// *****************************************************************************
{
  if (!var.empty()) {
    ErrChk( ex_put_var( m_outFile,
                        static_cast< int >( it ),
                        EX_ELEM_BLOCK,
                        varid,
                        1,
                        static_cast< int64_t >( var.size() ),
                        var.data() ) == 0,
            "Failed to write elem scalar to ExodusII file: " + m_filename );
  }
}