LCOV - code coverage report
Current view: top level - ugbase/lib_algebra/common/matrixio - matrix_io.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 2 2
Test Date: 2025-09-21 23:31:46 Functions: - 0 0

            Line data    Source code
       1              : /*
       2              :  * Copyright (c) 2012-2015:  G-CSC, Goethe University Frankfurt
       3              :  * Author: Torbjörn Klatt
       4              :  * 
       5              :  * This file is part of UG4.
       6              :  * 
       7              :  * UG4 is free software: you can redistribute it and/or modify it under the
       8              :  * terms of the GNU Lesser General Public License version 3 (as published by the
       9              :  * Free Software Foundation) with the following additional attribution
      10              :  * requirements (according to LGPL/GPL v3 §7):
      11              :  * 
      12              :  * (1) The following notice must be displayed in the Appropriate Legal Notices
      13              :  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
      14              :  * 
      15              :  * (2) The following notice must be displayed at a prominent place in the
      16              :  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
      17              :  * 
      18              :  * (3) The following bibliography is recommended for citation and must be
      19              :  * preserved in all covered files:
      20              :  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
      21              :  *   parallel geometric multigrid solver on hierarchically distributed grids.
      22              :  *   Computing and visualization in science 16, 4 (2013), 151-164"
      23              :  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
      24              :  *   flexible software system for simulating pde based models on high performance
      25              :  *   computers. Computing and visualization in science 16, 4 (2013), 165-179"
      26              :  * 
      27              :  * This program is distributed in the hope that it will be useful,
      28              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      29              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      30              :  * GNU Lesser General Public License for more details.
      31              :  */
      32              : 
      33              : #ifndef __H__UG__LIB_ALGEBRA__MATRIX_IO_H
      34              : #define __H__UG__LIB_ALGEBRA__MATRIX_IO_H
      35              : 
      36              : #include <string>
      37              : #include <fstream>
      38              : 
      39              : #include "common/util/file_util.h"
      40              : #include "common/assert.h"
      41              : #include "common/error.h"
      42              : #include "common/log.h"
      43              : #include "common/profiler/profiler.h"
      44              : #include "lib_algebra/lib_algebra.h"
      45              : #include "lib_algebra/cpu_algebra_types.h"
      46              : 
      47              : namespace ug
      48              : {
      49              : 
      50              : /**
      51              :  * \defgroup matrixio MatrixIO
      52              :  * \ingroup lib_algebra_common
      53              :  * \{
      54              :  */
      55              : 
      56              : /**
      57              :  * \brief Representation of a matrix exchange file format
      58              :  *
      59              :  * Supported matrix exchange file formats are:
      60              :  * - MatrixMarket (http://math.nist.gov/MatrixMarket/formats.html#MMformat)
      61              :  *
      62              :  * \note The Harwell-Boing (http://math.nist.gov/MatrixMarket/formats.html#hb)
      63              :  * format is not yet implemented.
      64              :  */
      65              : class MatrixFileType
      66              : {
      67              :   public:
      68              :     /// Supported matrix exchange file formats
      69              :     enum Type {
      70              :       /// undefined format
      71              :       UNDEFINED = 0,
      72              :       /// MatrixMarket format (by NIS)
      73              :       MATRIX_MARKET = 1,
      74              :       /// Harwell-Boing format (by NIS)
      75              :       HARWELL_BOING = 2   // TODO: HarwellBoing format not yet implemented
      76              :     };
      77              : 
      78              :     /**
      79              :      * \brief Default Constructor
      80              :      *
      81              :      * Initializes file format type to \c 0 (thus undefined).
      82              :      */
      83              :     MatrixFileType() : m_type( UNDEFINED ) {}
      84              :     /**
      85              :      * \brief Constructor specifying file type
      86              :      *
      87              :      * Initializes file format type to the given type.
      88              :      *
      89              :      * \param[in] type  a valid value of MatrixFileType::Type
      90              :      */
      91           27 :     MatrixFileType( int type ) : m_type() {
      92              :       UG_ASSERT( type >= 0 && type <= 2, "Unknown MatrixFileType code: " << type );
      93           13 :       m_type = type;
      94              :     }
      95              :     /**
      96              :      * \brief Tells whether this is a MatrixMarket type
      97              :      * \return \c true if it is a MatrixMarket file type, \c false otherwise.
      98              :      */
      99              :     bool is_mm() const {
     100              :       return m_type == MATRIX_MARKET;
     101              :     }
     102              :     /**
     103              :      * \brief Tells whether this is a Harwell-Boing type
     104              :      * \return \c true if it is a Harwell-Boing file type, \c false otherwise.
     105              :      */
     106              :     bool is_hb() const {
     107              :       return m_type == HARWELL_BOING;
     108              :     }
     109              : 
     110              :   private:
     111              :     /// Holds the file type value (which is one of MatrixFileType::Type)
     112              :     int m_type;
     113              : };
     114              : 
     115              : /**
     116              :  * \brief Generic matrix I/O functionality
     117              :  *
     118              :  * This is the generic class for matrix I/O functionality.
     119              :  * For actual matrix I/O operations use one of it's subclasses specifying the
     120              :  * specific behaviour for one of the supported matrix exchange file formats.
     121              :  *
     122              :  * \note Please include this header file, when you want to use the I/O functions
     123              :  * for a specific exchange file format.
     124              :  */
     125              : class MatrixIO
     126              : {
     127              :   public:
     128              :     /// Specifies how file names pointing to non-existing files should be handeld
     129              :     enum OpenMode {
     130              :       /// Only existing files are valid files (i.e. non-existing file result in error)
     131              :       EXISTING = 1,
     132              :       /// Non-existing files are created with the specified file name
     133              :       NEW = 2
     134              :     };
     135              :     
     136              :   private:
     137              :     /// Full path name of the matrix exchange file
     138              :     std::string *m_pMatFileName;
     139              :     /// Internal file stream for reading from and writing into the matrix exchange file
     140              :     std::fstream m_matFileStream;
     141              :     /// Matrix exchange file type
     142              :     MatrixFileType m_matFileType;
     143              :     /// Number of rows as specified in the matrix exchange file
     144              :     //int m_rows;
     145              :     /// Number of columns as specified in the matrix exchange file
     146              :     //int m_cols;
     147              :     /// Number of data lines as specified in the matrix exchange file
     148              :     //int m_lines;
     149              : 
     150              :   public:
     151              :     /**
     152              :      * \brief Default constructor
     153              :      */
     154              :     MatrixIO();
     155              :     /**
     156              :      * \brief Constructor specifying matrix exchange file path
     157              :      *
     158              :      * \param[in] mFile     path and name of matrix exchange file.
     159              :      * \param[in] openMode  how to deal with non-existing files (a value of
     160              :      *                      MatrixIO::OpenMode)
     161              :      */
     162              :     MatrixIO( std::string mFile, int openMode=EXISTING );
     163              :     /**
     164              :      * \brief Destructor
     165              :      *
     166              :      * The destructor calls MatrixIO::close_file to make sure, that write
     167              :      * operations to the associated exchange file are flushed and terminated
     168              :      * correctly.
     169              :      */
     170              :     ~MatrixIO();
     171              : 
     172              :     /**
     173              :      * \brief Sets associated file to another one
     174              :      * 
     175              :      * \param[in] mFile Full path and name of matrix exchange file.
     176              :      * \param[in] openMode  how to deal with non-existing files
     177              :      *
     178              :      * \throws std::runtime_error if exchange file is not found and
     179              :      *                            <tt>openMode=OpenMode::Existing</tt>.
     180              :      * \throws UGError            if \c openMode is non of OpenMode
     181              :      */
     182              :     void set_mat_file_name( std::string mFile, int openMode=EXISTING );
     183              :     /**
     184              :      * \brief Retreive associated exchange file path and name
     185              :      *
     186              :      * \return String representation of exchange file with full path as set by
     187              :      * constructor or MatrixIO::set_mat_file_name.
     188              :      */
     189              :     std::string get_mat_file_name() const;
     190              : 
     191              :   private:
     192              :     /**
     193              :      * \brief Opens the associated file in \c m_matFileStream
     194              :      *
     195              :      * \param[in] mode  Modus of the resulting file stream (a value of
     196              :      *                  std::ios_base::openmode). Defaults to std::ios_base::in.
     197              :      *
     198              :      * \throws std::runtime_error if \c m_pMatFileName is empty
     199              :      * \throws std::runtime_error if file stream can not be opened (i.e.
     200              :      *                            \c failbit or \c badbit are set)
     201              :      */
     202              :     void open_file( std::ios_base::openmode mode=std::ios_base::in );
     203              :     /**
     204              :      * \brief Closes file stream
     205              :      */
     206              :     void close_file();
     207              : };
     208              : 
     209              : // end group matrixio
     210              : /// \}
     211              : 
     212              : } // namespace ug
     213              : 
     214              : // include specializations
     215              : #include "matrix_io_mtx.h"
     216              : 
     217              : #endif // __H__UG__LIB_ALGEBRA__MATRIXIO_H
     218              : 
     219              : // EOF
        

Generated by: LCOV version 2.0-1