basic.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <vector>
10 #include <algorithm>
11 #include <tuple>
12 #include <cassert>
13 #include <stdexcept>
14 #include <memory>
15 #include <string>
16 #include <fstream>
17 #include <functional>
18 #include <type_traits>
19 #include <chrono>
20 #include <iomanip>
21 #include <omp.h>
22 
29 namespace LCS {
30 
37 template <typename T>
38 std::vector<T> operator+(const std::vector<T>& a, const std::vector<T>& b)
39 {
40  assert(a.size() == b.size());
41 
42  std::vector<T> result;
43  result.reserve(a.size());
44 
45  std::transform(a.begin(), a.end(), b.begin(),
46  std::back_inserter(result), std::plus<T>());
47  return result;
48 }
49 
56 template <typename T>
57 std::vector<T> operator-(const std::vector<T>& a, const std::vector<T>& b)
58 {
59  assert(a.size() == b.size());
60 
61  std::vector<T> result;
62  result.reserve(a.size());
63 
64  std::transform(a.begin(), a.end(), b.begin(),
65  std::back_inserter(result), std::minus<T>());
66  return result;
67 }
68 
76 template <class T, class T2>
77 std::vector<T> operator* (const T2 c, std::vector<T> a)
78 {
79  for (unsigned i = 0; i < a.size(); ++i)
80  a[i] = c * a[i];
81 
82  return a;
83 }
84 
91 template <typename T, unsigned Size = 2>
92 struct Vector
93 {
95  Vector(): x(), y() {}
96 
101  Vector(const T& x, const T& y): x(x), y(y) {}
102 
103  T x;
104  T y;
105 };
106 
114 template <typename T, unsigned Size>
116 {
117  Vector<T,Size> result(a.x+b.x, a.y+b.y);
118  return result;
119 }
120 
128 template <typename T, unsigned Size>
130 {
131  Vector<T,Size> result(a.x-b.x, a.y-b.y);
132  return result;
133 }
134 
142 template <typename T, unsigned Size>
144 {
145  a.x *= c;
146  a.y *= c;
147  return a;
148 }
149 
153 template <typename T>
155 
162 template <typename T>
163 struct Vector<T, 1>
164 {
166  Vector(): value() {}
167 
171  Vector(const T& x): value(x) {}
172 
173  T value;
174 };
175 
182 template <typename T, unsigned Dim = 2>
183 class Tensor
184 {
185  public:
190  Tensor(unsigned nx, unsigned ny):
191  data_(nx * ny, T()), nx_(nx), ny_(ny) {}
192 
196  inline void operator= (const Tensor<T, Dim>& t)
197  {
198  // need to implement size check
199  data_ = t.GetAll();
200  }
201 
207  inline T& operator() (unsigned i, unsigned j)
208  {
209  // need to implement size check
210  return data_[i*ny_ + j];
211  }
212 
218  inline T operator() (unsigned i, unsigned j) const
219  {
220  return data_[i*ny_ + j];
221  }
222 
228  inline T& Get(unsigned i, unsigned j)
229  {
230  return data_[i*ny_ + j];
231  }
232 
238  inline T Get(unsigned i, unsigned j) const
239  {
240  return data_[i*ny_ + j];
241  }
242 
248  inline T GetValue(unsigned i, unsigned j) const
249  {
250  return data_[i*ny_ + j];
251  }
252 
256  inline auto& GetAll() const
257  {
258  return data_;
259  }
260 
266  inline auto GetNearby(const unsigned i, const unsigned j) const
267  {
268  auto x_pre = (i != 0) ? Get(i-1,j) : Get(i,j);
269  auto x_next = (i != nx_-1) ? Get(i+1,j) : Get(i,j);
270 
271  auto y_pre = (j != 0) ? Get(i,j-1) : Get(i,j);
272  auto y_next = (j != ny_-1) ? Get(i,j+1) : Get(i,j);
273 
274  return std::make_tuple(x_pre, x_next, y_pre, y_next);
275 
276  }
277 
283  inline void SetValue(unsigned i, unsigned j, T value)
284  {
285  data_[i*ny_ + j] = value;
286  }
287 
291  inline void SetAll(std::vector<T>& data)
292  {
293  data_ = data;
294  }
295 
299  inline auto Size() const
300  {
301  return std::make_tuple(nx_, ny_);
302  }
303 
304  private:
305  std::vector<T> data_;
306  const unsigned nx_;
307  const unsigned ny_;
308 };
309 
310 
320 template <typename T>
321 inline T interpolate(T x1, T x2, T y1, T y2, T xm)
322 {
323  return y1 + (xm - x1) * (y2 - y1) / (x2 - x1);
324 }
325 
335 template <typename Field, typename T>
336 void interpolate(T x1, T x2, Field& f1, Field& f2, T xm, Field& result)
337 {
338  auto tensor_result = f1.GetAll();
339  if (x1 != x2)
340  {
341  auto vec1 = f1.GetAll().GetAll();
342  auto vec2 = f2.GetAll().GetAll();
343  auto vec_result = vec1 + ((xm - x1)/(x2 - x1)) * (vec2 - vec1);
344 
345  tensor_result.SetAll(vec_result);
346  }
347  result.SetAll(tensor_result);
348 }
349 
354 class Clock
355 {
356  public:
359  Clock():
360  total_elapsed_time_(0), begin_time_(), end_time_(), started_(false) {}
361 
363  inline void Begin()
364  {
365  // TODO: make sure clock is not started
366  if (!started_)
367  {
368  begin_time_ = std::chrono::system_clock::now();
369  started_ = true;
370  }
371  }
372 
374  inline void End()
375  {
376  // TODO: make sure clock is started
377  if (started_)
378  {
379  end_time_ = std::chrono::system_clock::now();
380  started_ = false;
381 
382  // calculate elapsed time (in seconds) and add it to total_time_
383  elapsed_time_ = std::chrono::duration_cast<std::chrono::nanoseconds>
384  (end_time_ - begin_time_).count() / 1e9;
385  total_elapsed_time_ += elapsed_time_;
386  }
387  }
388 
392  inline double GetElapsedTime() const
393  {
394  return elapsed_time_;
395  }
396 
400  inline double GetTotalElapsedTime() const
401  {
402  return total_elapsed_time_;
403  }
404 
405  private:
406  double total_elapsed_time_;
407  double elapsed_time_;
408  std::chrono::time_point<std::chrono::system_clock> begin_time_;
409  std::chrono::time_point<std::chrono::system_clock> end_time_;
410  bool started_;
411 };
412 
413 
414 }
void SetValue(unsigned i, unsigned j, T value)
Definition: basic.hpp:283
double GetElapsedTime() const
Definition: basic.hpp:392
std::vector< T > operator-(const std::vector< T > &a, const std::vector< T > &b)
Definition: basic.hpp:57
Tensor(unsigned nx, unsigned ny)
Definition: basic.hpp:190
void End()
Definition: basic.hpp:374
T y
Definition: basic.hpp:104
Vector(const T &x, const T &y)
Definition: basic.hpp:101
void SetAll(std::vector< T > &data)
Definition: basic.hpp:291
T x
Definition: basic.hpp:103
T value
Definition: basic.hpp:173
auto GetNearby(const unsigned i, const unsigned j) const
Definition: basic.hpp:266
Vector()
Definition: basic.hpp:166
Class for tensors.
Definition: basic.hpp:183
T interpolate(T x1, T x2, T y1, T y2, T xm)
Definition: basic.hpp:321
auto & GetAll() const
Definition: field.hpp:47
Class for time recording.
Definition: basic.hpp:354
T GetValue(unsigned i, unsigned j) const
Definition: basic.hpp:248
Vector()
Definition: basic.hpp:95
void SetAll(Tensor< Vector< T, Size >, Dim > &data)
Definition: field.hpp:71
Class for general physical fields.
Definition: field.hpp:35
T Get(unsigned i, unsigned j) const
Definition: basic.hpp:238
T & operator()(unsigned i, unsigned j)
Definition: basic.hpp:207
T & Get(unsigned i, unsigned j)
Definition: basic.hpp:228
Vector with one element.
Definition: basic.hpp:163
void operator=(const Tensor< T, Dim > &t)
Definition: basic.hpp:196
Clock()
Definition: basic.hpp:359
std::vector< T > operator+(const std::vector< T > &a, const std::vector< T > &b)
Definition: basic.hpp:38
auto & GetAll() const
Definition: basic.hpp:256
Vector(const T &x)
Definition: basic.hpp:171
auto Size() const
Definition: basic.hpp:299
std::vector< T > operator*(const T2 c, std::vector< T > a)
Definition: basic.hpp:77
Struct for vectors.
Definition: basic.hpp:92
double GetTotalElapsedTime() const
Definition: basic.hpp:400
void Begin()
Definition: basic.hpp:363