Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/IO/DiagWriter.cpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.,
7 : : 2022-2025 J. Bakosi
8 : : All rights reserved. See the LICENSE file for details.
9 : : \brief Text diagnostics writer declaration
10 : : \details This file declares the ASCII diagnostics writer class that
11 : : facilitates outputing diagnostics to text files.
12 : : */
13 : : // *****************************************************************************
14 : :
15 : : #include <iostream>
16 : : #include <iomanip>
17 : : #include <limits>
18 : : #include <cstdint>
19 : :
20 : : #include "DiagWriter.hpp"
21 : : #include "Exception.hpp"
22 : :
23 : : using tk::DiagWriter;
24 : :
25 : 5254 : DiagWriter::DiagWriter( const std::string& filename,
26 : : const std::string& format,
27 : : std::streamsize precision,
28 : 5254 : std::ios_base::openmode mode ) :
29 : : Writer( filename, mode ),
30 : 5254 : m_precision( static_cast< int >( precision ) ),
31 [ + + ]: 5254 : m_width( std::max( 20, m_precision+8 ) )
32 : : // *****************************************************************************
33 : : // Constructor
34 : : //! \param[in] filename Output filename to which output the diagnostics
35 : : //! \param[in] format Configure floating-point output format ASCII output
36 : : //! \param[in] precision Configure precision for floating-point ASCII output
37 : : //! \param[in] mode Configure file open mode
38 : : // *****************************************************************************
39 : : {
40 : : // Set floating-point format for output file stream
41 [ + + ]: 5254 : if (format == "default")
42 : : {} //m_outFile << std::defaultfloat; GCC does not yet support this
43 [ - + ]: 3281 : else if (format == "fixed")
44 : 0 : m_outFile << std::fixed;
45 [ + - ]: 3281 : else if (format == "scientific")
46 : 3281 : m_outFile << std::scientific;
47 [ - - ][ - - ]: 0 : else Throw( "Text floating-point format not recognized." );
[ - - ]
48 : :
49 : : // Set numeric precision for output file stream if the input makes sense
50 [ + - ]: 5254 : if (precision > 0 && precision < std::numeric_limits< tk::real >::digits10+2)
51 : 5254 : m_outFile << std::setprecision( static_cast<int>(precision) );
52 : 5254 : }
53 : :
54 : : void
55 : 339 : DiagWriter::header( const std::vector< std::string >& name ) const
56 : : // *****************************************************************************
57 : : // Write out diagnostics file header
58 : : //! \param[in] name Vector of strings with the names of diagnostics
59 : : // *****************************************************************************
60 : : {
61 : : m_outFile << "#" << std::setw(9) << "1:it"
62 : 339 : << std::setw(m_width) << "2:t"
63 : 678 : << std::setw(m_width) << "3:dt";
64 : 339 : std::stringstream out;
65 : :
66 : : // Output names of diagnostics
67 : : std::size_t column = 4;
68 [ + + ]: 4208 : for (const auto& n : name) {
69 [ + - ][ + - ]: 3869 : out << column++ << ':' << n;
70 [ + - ]: 3869 : m_outFile << std::setw(m_width) << out.str();
71 [ + - ]: 7738 : out.str("");
72 : : }
73 : :
74 : : m_outFile << std::endl;
75 : 339 : }
76 : :
77 : : std::size_t
78 : 4915 : DiagWriter::write( uint64_t it,
79 : : tk::real t,
80 : : tk::real dt,
81 : : const std::vector< tk::real >& diagnostics )
82 : : // *****************************************************************************
83 : : // Write out diagnostics
84 : : //! \param[in] it Iteration counter
85 : : //! \param[in] t Time
86 : : //! \param[in] dt Time step size
87 : : //! \param[in] diagnostics Vector with the diagnostics
88 : : //! \return The total number of diagnostics written to the output file
89 : : // *****************************************************************************
90 : : {
91 : 4915 : m_outFile << std::setw(10) << it
92 : 4915 : << std::setw(m_width) << t
93 : 4915 : << std::setw(m_width) << dt;
94 : :
95 : : // Output diagnostics
96 [ + + ]: 71388 : for (const auto& d : diagnostics) m_outFile << std::setw(m_width) << d;
97 : :
98 : : m_outFile << std::endl;
99 : :
100 : 4915 : return diagnostics.size();
101 : : }
|