KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Util.cpp
Go to the documentation of this file.
1 /*
2  * KernelExpansion.cpp
3  *
4  * Created on: 22.07.2013
5  * Author: CreaByte
6  */
7 
8 #include "kermorpp.h"
9 #include <iostream>
10 #include <fstream>
11 
12 using namespace std;
13 using namespace Eigen;
14 
15 namespace kermorpp {
16 
17 VectorXd Util.loadVector(string file) {
18 
19 #if DEBUG
20  cout << "Loading vector from file " << file << endl;
21 #endif
22 
23  ifstream fs;
24  fs.open(file.c_str(), ios.in | ios.binary);
25  if (!fs) {
26  cerr << "Not able to read file " << file << endl;
27  fs.close();
28  exit(-1);
29  } else {
30  char buf[DOUBLE_BYTES];
31  bool le = little_endian();
32  fs.seekg(0, ios.beg);
33 
34  // Faster would be: fs.read(reinterpret_cast<char*>(&res.n), INT_BYTES);
35  // But have to take care of endianness
36  fs.read(buf, INT_BYTES);
37  if (le)
38  reverse(buf, buf + INT_BYTES);
39  int n;
40  copy(buf, buf + INT_BYTES, reinterpret_cast<char*>(&n));
41 
42  VectorXd res(n);
43  for (int l = 0; l < res.rows(); l++) {
44  fs.read(buf, DOUBLE_BYTES);
45  if (le)
46  reverse(buf, buf + DOUBLE_BYTES);
47  double tmp;
48  copy(buf, buf + DOUBLE_BYTES, reinterpret_cast<char*>(&tmp));
49  res(l) = tmp;
50  }
51  fs.close();
52  return res;
53  }
54 }
55 
56 MatrixXd Util.loadMatrix(string file) {
57 
58  ifstream fs;
59  fs.open(file.c_str(), ios.in | ios.binary);
60  if (!fs) {
61  cerr << "Not able to read file " << file << endl;
62  fs.close();
63  exit(-1);
64  } else {
65  char buf[DOUBLE_BYTES];
66  bool le = little_endian();
67  fs.seekg(0, ios.beg);
68 
69  int n, m;
70  // Faster would be: fs.read(reinterpret_cast<char*>(&res.n), INT_BYTES);
71  // But have to take care of endianness
72  fs.read(buf, INT_BYTES);
73  if (le)
74  reverse(buf, buf + INT_BYTES);
75  copy(buf, buf + INT_BYTES, reinterpret_cast<char*>(&n));
76 
77  fs.read(buf, INT_BYTES);
78  if (le)
79  reverse(buf, buf + INT_BYTES);
80  copy(buf, buf + INT_BYTES, reinterpret_cast<char*>(&m));
81 
82  MatrixXd res(n, m);
83 
84 #if DEBUG
85  cout << "Reading " << res.rows() << " x " << res.cols() << " matrix" << endl;
86 #endif
87 
88  for (int i = 0; i < res.rows(); i++) {
89  for (int j = 0; j < res.cols(); j++) {
90  //fs.read(reinterpret_cast<char*>(&res.values[l]), DOUBLE_BYTES);
91  fs.read(buf, DOUBLE_BYTES);
92  if (le)
93  reverse(buf, buf + DOUBLE_BYTES);
94  double tmp;
95  copy(buf, buf + DOUBLE_BYTES, reinterpret_cast<char*>(&tmp));
96  res(i, j) = tmp;
97  }
98  }
99  fs.close();
100  return res;
101  }
102 
103 }
104 
105 inline bool Util.little_endian(void) {
106  typedef unsigned char* pbyte;
107  static unsigned long word = 0x1;
108  static bool is_little_endian_machine = (*pbyte(&word) == 0x1);
109  return is_little_endian_machine;
110 }
111 
112 }
113 
const int INT_BYTES
Definition: kermorpp.h:26
const int DOUBLE_BYTES
Definition: kermorpp.h:27