ug4
matrix_io.h
Go to the documentation of this file.
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"
46 
47 namespace ug
48 {
49 
66 {
67  public:
69  enum Type {
71  UNDEFINED = 0,
75  HARWELL_BOING = 2 // TODO: HarwellBoing format not yet implemented
76  };
77 
91  MatrixFileType( int type ) : m_type() {
92  UG_ASSERT( type >= 0 && type <= 2, "Unknown MatrixFileType code: " << type );
93  m_type = type;
94  }
99  bool is_mm() const {
100  return m_type == MATRIX_MARKET;
101  }
106  bool is_hb() const {
107  return m_type == HARWELL_BOING;
108  }
109 
110  private:
112  int m_type;
113 };
114 
125 class MatrixIO
126 {
127  public:
129  enum OpenMode {
131  EXISTING = 1,
133  NEW = 2
134  };
135 
136  private:
138  std::string *m_pMatFileName;
140  std::fstream m_matFileStream;
144  //int m_rows;
146  //int m_cols;
148  //int m_lines;
149 
150  public:
154  MatrixIO();
162  MatrixIO( std::string mFile, int openMode=EXISTING );
170  ~MatrixIO();
171 
182  void set_mat_file_name( std::string mFile, int openMode=EXISTING );
189  std::string get_mat_file_name() const;
190 
191  private:
202  void open_file( std::ios_base::openmode mode=std::ios_base::in );
206  void close_file();
207 };
208 
209 // end group matrixio
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
Representation of a matrix exchange file format.
Definition: matrix_io.h:66
bool is_hb() const
Tells whether this is a Harwell-Boing type.
Definition: matrix_io.h:106
bool is_mm() const
Tells whether this is a MatrixMarket type.
Definition: matrix_io.h:99
int m_type
Holds the file type value (which is one of MatrixFileType::Type)
Definition: matrix_io.h:112
MatrixFileType(int type)
Constructor specifying file type.
Definition: matrix_io.h:91
Type
Supported matrix exchange file formats.
Definition: matrix_io.h:69
@ MATRIX_MARKET
MatrixMarket format (by NIS)
Definition: matrix_io.h:73
@ HARWELL_BOING
Harwell-Boing format (by NIS)
Definition: matrix_io.h:75
@ UNDEFINED
undefined format
Definition: matrix_io.h:71
MatrixFileType()
Default Constructor.
Definition: matrix_io.h:83
Generic matrix I/O functionality.
Definition: matrix_io.h:126
void set_mat_file_name(std::string mFile, int openMode=EXISTING)
Sets associated file to another one.
Definition: matrix_io.cpp:60
std::fstream m_matFileStream
Internal file stream for reading from and writing into the matrix exchange file.
Definition: matrix_io.h:140
void open_file(std::ios_base::openmode mode=std::ios_base::in)
Opens the associated file in m_matFileStream.
Definition: matrix_io.cpp:85
std::string * m_pMatFileName
Full path name of the matrix exchange file.
Definition: matrix_io.h:138
void close_file()
Closes file stream.
Definition: matrix_io.cpp:95
OpenMode
Specifies how file names pointing to non-existing files should be handeld.
Definition: matrix_io.h:129
@ EXISTING
Only existing files are valid files (i.e. non-existing file result in error)
Definition: matrix_io.h:131
@ NEW
Non-existing files are created with the specified file name.
Definition: matrix_io.h:133
std::string get_mat_file_name() const
Retreive associated exchange file path and name.
Definition: matrix_io.cpp:78
MatrixIO()
Number of rows as specified in the matrix exchange file.
Definition: matrix_io.cpp:40
MatrixFileType m_matFileType
Matrix exchange file type.
Definition: matrix_io.h:142
~MatrixIO()
Destructor.
Definition: matrix_io.cpp:52
File utility functions.
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
the ug namespace