Xyst test code coverage report
Current view: top level - IO - DiagWriter.cpp (source / functions) Hit Total Coverage
Commit: 5689ba12dc66a776d3d75f1ee48cc7d78eaa18dc Lines: 29 31 93.5 %
Date: 2024-11-22 19:17:03 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 25 52 48.1 %

           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-2024 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                 :            : 
      19                 :            : #include "DiagWriter.hpp"
      20                 :            : #include "Exception.hpp"
      21                 :            : 
      22                 :            : using tk::DiagWriter;
      23                 :            : 
      24                 :       5026 : DiagWriter::DiagWriter( const std::string& filename,
      25                 :            :                         const std::string& format,
      26                 :            :                         std::streamsize precision,
      27                 :       5026 :                         std::ios_base::openmode mode ) :
      28                 :            :   Writer( filename, mode ),
      29                 :       5026 :   m_precision( static_cast< int >( precision ) ),
      30                 :       5026 :   m_width( std::max( 16, m_precision+8 ) )
      31                 :            : // *****************************************************************************
      32                 :            : //  Constructor
      33                 :            : //! \param[in] filename Output filename to which output the diagnostics
      34                 :            : //! \param[in] format Configure floating-point output format ASCII output
      35                 :            : //! \param[in] precision Configure precision for floating-point ASCII output
      36                 :            : //! \param[in] mode Configure file open mode
      37                 :            : // *****************************************************************************
      38                 :            : {
      39                 :            :   // Set floating-point format for output file stream
      40         [ +  + ]:       5026 :   if (format == "default")
      41                 :            :     {} //m_outFile << std::defaultfloat;   GCC does not yet support this
      42         [ -  + ]:       3080 :   else if (format == "fixed")
      43         [ -  - ]:          0 :     m_outFile << std::fixed;
      44         [ +  - ]:       3080 :   else if (format == "scientific")
      45         [ +  - ]:       3080 :     m_outFile << std::scientific;
      46 [ -  - ][ -  - ]:          0 :   else Throw( "Text floating-point format not recognized." );
                 [ -  - ]
      47                 :            : 
      48                 :            :   // Set numeric precision for output file stream if the input makes sense
      49 [ +  - ][ +  - ]:       5026 :   if (precision > 0 && precision < std::numeric_limits< tk::real >::digits10+2)
      50                 :       5026 :     m_outFile << std::setprecision( static_cast<int>(precision) );
      51                 :       5026 : }
      52                 :            : 
      53                 :            : void
      54                 :        323 : DiagWriter::header( const std::vector< std::string >& name ) const
      55                 :            : // *****************************************************************************
      56                 :            : //  Write out diagnostics file header
      57                 :            : //! \param[in] name Vector of strings with the names of diagnostics
      58                 :            : // *****************************************************************************
      59                 :            : {
      60                 :            :   m_outFile << "#" << std::setw(9) << "1:it"
      61                 :        323 :             << std::setw(m_width) << "2:t"
      62 [ +  - ][ +  - ]:        323 :             << std::setw(m_width) << "3:dt";
         [ +  - ][ +  - ]
      63         [ +  - ]:        323 :   std::stringstream out;
      64                 :            : 
      65                 :            :   // Output names of diagnostics
      66                 :        323 :   std::size_t column = 4;
      67         [ +  + ]:       3981 :   for (const auto& n : name) {
      68 [ +  - ][ +  - ]:       3658 :     out << column++ << ':' << n;
                 [ +  - ]
      69 [ +  - ][ +  - ]:       3658 :     m_outFile << std::setw(m_width) << out.str();
      70 [ +  - ][ +  - ]:       3658 :     out.str("");
      71                 :            :   }
      72                 :            : 
      73         [ +  - ]:        323 :   m_outFile << std::endl;
      74                 :        323 : }
      75                 :            : 
      76                 :            : std::size_t
      77                 :       4703 : DiagWriter::write( uint64_t it,
      78                 :            :                    tk::real t,
      79                 :            :                    tk::real dt,
      80                 :            :                    const std::vector< tk::real >& diagnostics )
      81                 :            : // *****************************************************************************
      82                 :            : //  Write out diagnostics
      83                 :            : //! \param[in] it Iteration counter
      84                 :            : //! \param[in] t Time
      85                 :            : //! \param[in] dt Time step size
      86                 :            : //! \param[in] diagnostics Vector with the diagnostics
      87                 :            : //! \return The total number of diagnostics written to the output file
      88                 :            : // *****************************************************************************
      89                 :            : {
      90                 :       4703 :   m_outFile << std::setw(10) << it
      91                 :       4703 :             << std::setw(m_width) << t
      92                 :       4703 :             << std::setw(m_width) << dt;
      93                 :            : 
      94                 :            :   // Output diagnostics
      95 [ +  - ][ +  + ]:      67732 :   for (const auto& d : diagnostics) m_outFile << std::setw(m_width) << d;
      96                 :            : 
      97                 :       4703 :   m_outFile << std::endl;
      98                 :            : 
      99                 :       4703 :   return diagnostics.size();
     100                 :            : }

Generated by: LCOV version 1.16