ug4
connection_viewer_input.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
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 CONNECTION_VIEWER_IO_H
34 #define CONNECTION_VIEWER_IO_H
35 
36 #include <fstream>
37 #include <vector>
38 #include <string>
39 #include <cassert>
40 #include "common/progress.h"
42 
43 
44 namespace ug{
45 
46 namespace ConnectionViewer{
47 
48 template<typename matrix_type>
49 bool ReadMatrix(std::string filename, matrix_type &matrix, std::vector<MathVector<3> > &grid, int &dimension)
50 {
51  std::cout << " Reading matrix from " << filename << "... ";
52  std::fstream matfile(filename.c_str(), std::ios::in);
53  if(matfile.is_open() == false) { std::cout << "failed.\n"; return false; }
54 
55  int version=-1, gridsize=-1;
56  dimension=-1;
57 
58  matfile >> version;
59  matfile >> dimension;
60  matfile >> gridsize;
61 
62  assert(version == 1);
63  assert(dimension == 2 || dimension == 3);
64  assert(gridsize != -1);
65 
66  std::cout << "v=" << version << " dim=" << dimension << " size=" << gridsize;
67  PROGRESS_START(prog, gridsize*2, "ReadMatrix " << dimension << "d from " << filename << " , " << gridsize << " x " << gridsize);
68  grid.resize(gridsize);
69  for(int i=0; i<gridsize; i++)
70  {
71  if(i%100) { PROGRESS_UPDATE(prog, i); }
72  if(matfile.eof())
73  {
74  assert(0);
75  std::cout << " failed.\n";
76  return false;
77  }
78 
79  matfile >> grid[i].x() >> grid[i].y();
80  if(dimension==3) matfile >> grid[i].z();
81  else grid[i].z() = 0;
82  }
83 
84  int printStringsInWindow;
85  matfile >> printStringsInWindow;
86 
87  matrix.resize_and_clear(gridsize, gridsize);
88  bool bEOF = matfile.eof();
89 
90  while(!bEOF)
91  {
92  int from, to; double value;
93  char c = matfile.peek();
94  if(c == -1 || c == 'c' || c == 'v' || matfile.eof())
95  break;
96 
97  matfile >> from >> to >> value;
98  if(value != 0.0)
99  matrix(from, to) = value;
100 
101  if(from%100) { PROGRESS_UPDATE(prog, gridsize+from); }
102  bEOF = matfile.eof();
103  }
104  matrix.defragment();
105 
106  return true;
107 }
108 
109 template<typename vector_type>
110 bool ReadVector(std::string filename, vector_type &vec)
111 {
112  Progress p;
113  std::cout << " Reading std::vector from " << filename << "... ";
114  std::fstream matfile(filename.c_str(), std::ios::in);
115  if(matfile.is_open() == false) { std::cout << "failed.\n"; return false; }
116 
117  int version=-1, gridsize=-1, dimension=-1;
118 
119  matfile >> version;
120  matfile >> dimension;
121  matfile >> gridsize;
122 
123  assert(version == 1);
124  assert(dimension == 2 || dimension == 3);
125  assert(gridsize != -1);
126 
127  PROGRESS_START(prog, gridsize*2, "ReadVector " << dimension << "d from " << filename << " , " << gridsize << " x " << gridsize);
128  for(int i=0; i<gridsize; i++)
129  {
130  if(i%100) { PROGRESS_UPDATE(prog, i); }
131  if(matfile.eof())
132  {
133  std::cout << " failed.\n";
134  assert(0);
135  return false;
136  }
137  double x, y, z;
138  matfile >> x >> y;
139  if(dimension==3) matfile >> z;
140  }
141 
142  int printStringsInWindow;
143  matfile >> printStringsInWindow;
144 
145  vec.resize(gridsize);
146  bool bEOF = matfile.eof();
147  while(!bEOF)
148  {
149  int from, to; double value;
150  char c = matfile.peek();
151  if(c == -1 || c == 'c' || c == 'v' || matfile.eof())
152  break;
153 
154  matfile >> from >> to >> value;
155  assert(from == to);
156  vec[from] = value;
157  if(from%100) { PROGRESS_UPDATE(prog, from); }
158  bEOF = matfile.eof();
159  }
160  return true;
161 }
162 
163 /*
164 template<typename vector_type>
165 bool WriteVector(std::string filename, const vector_type &vec, const std::vector<MathVector<3> > &grid, int dimension)
166 {
167  std::cout << " Writing std::vector (size " << vec.size() << " to " << filename << "\n";
168  assert(vec.size() == grid.size());
169  std::fstream f(filename.c_str(), std::ios::out);
170  WriteGridHeader(f, grid, dimension);
171  for(size_t i=0; i<vec.size(); i++)
172  f << i << " " << i << " " << vec[i] << "\n";
173  return true;
174 }
175 
176 
177 template<typename matrix_type>
178 bool WriteMatrix(std::string filename, const matrix_type &mat, const std::vector<MathVector<3> > &grid, int dimension)
179 {
180  std::cout << " Writing matrix (size " << mat.num_rows() << " x " << mat.num_cols() << ") to " << filename << "\n";
181  assert(mat.num_rows() == mat.num_cols() && mat.num_cols() == grid.size());
182  std::fstream f(filename.c_str(), std::ios::out);
183  WriteGridHeader(f, grid, dimension);
184  for(size_t i=0; i<mat.num_rows(); i++)
185  {
186  for(typename matrix_type::const_row_iterator it = mat.begin_row(i); it != mat.end_row(i); ++it)
187  f << i << " " << index(it) << " " << value(it) << "\n";
188  }
189  return true;
190 }
191 
192 template<typename matrix_type>
193 bool WriteProlongation(std::string filename, const matrix_type &mat, const std::vector<MathVector<3> > &finegrid, int dimension, std::vector<int> newIndex, int nCoarse)
194 {
195  std::cout << " Writing prolongation matrix (size " << mat.num_rows() << " x " << mat.num_cols() << ") to " << filename << "\n";
196  assert(mat.num_rows() == finegrid.size() && mat.num_cols() == nCoarse);
197  std::fstream f(filename.c_str(), std::ios::out);
198  WriteGridHeader(f, finegrid, dimension);
199 
200  std::vector<int> parentIndex(nCoarse, -1);
201  for(size_t i=0; i<newIndex.size(); i++)
202  if(newIndex[i] != -1)
203  parentIndex[newIndex[i]] = i;
204 
205  for(size_t i=0; i<mat.num_rows(); i++)
206  {
207  for(typename matrix_type::const_row_iterator it = mat.begin_row(i); it != mat.end_row(i); ++it)
208  f << i << " " << parentIndex[index(it)] << " " << value(it) << "\n";
209  }
210  return true;
211 }
212 
213 template<typename matrix_type>
214 bool WriteRestriction(std::string filename, const matrix_type &mat, const std::vector<MathVector<3> > &finegrid, int dimension, std::vector<int> newIndex, int nCoarse)
215 {
216  std::cout << " Writing restriction matrix (size " << mat.num_rows() << " x " << mat.num_cols() << ") to " << filename << "\n";
217  assert(mat.num_rows() == nCoarse);
218  assert(mat.num_cols() == finegrid.size());
219  std::fstream f(filename.c_str(), std::ios::out);
220  WriteGridHeader(f, finegrid, dimension);
221 
222  std::vector<int> parentIndex(nCoarse, -1);
223  for(size_t i=0; i<newIndex.size(); i++)
224  if(newIndex[i] != -1)
225  parentIndex[newIndex[i]] = i;
226 
227  for(size_t i=0; i<mat.num_rows(); i++)
228  {
229  for(typename matrix_type::const_row_iterator it = mat.begin_row(i); it != mat.end_row(i); ++it)
230  f << parentIndex[i] << " " << index(it) << " " << value(it) << "\n";
231  }
232  return true;
233 }
234 
235 template<typename matrix_type>
236 bool WriteGraph(std::string filename, const matrix_type &g, const std::vector<MathVector<3> > &grid, int dimension)
237 {
238  std::cout << " Writing graph (size " << g.size() << ") to " << filename << "\n";
239  assert(g.size() == grid.size());
240  std::fstream f(filename.c_str(), std::ios::out);
241  WriteGridHeader(f, grid, dimension);
242  for(size_t i=0; i<g.size(); i++)
243  {
244  for(typename matrix_type::const_row_iterator it = g.begin_row(i); it != g.end_row(i); ++it)
245  f << i << " " << index(it) << " 1\n";
246  }
247  return true;
248 }
249 */
250 
251 }
252 } // namespace ug
253 
254 #endif // CONNECTION_VIEWER_IO_H
parameterString p
a mathematical Vector with N entries.
Definition: math_vector.h:97
Wrapper for sequential matrices to handle them in parallel.
Definition: parallel_matrix.h:65
Definition: progress.h:50
bool ReadVector(std::string filename, vector_type &vec)
Definition: connection_viewer_input.h:110
bool ReadMatrix(std::string filename, matrix_type &matrix, std::vector< MathVector< 3 > > &grid, int &dimension)
Definition: connection_viewer_input.h:49
CPUAlgebra::vector_type vector_type
the ug namespace
#define PROGRESS_START(progVarName, dSize, msg)
Definition: progress.h:111
#define PROGRESS_UPDATE(progVarName, d)
Definition: progress.h:117