18 #include <type_traits>
38 std::vector<T>
operator+(
const std::vector<T>& a,
const std::vector<T>& b)
40 assert(a.size() == b.size());
42 std::vector<T> result;
43 result.reserve(a.size());
45 std::transform(a.begin(), a.end(), b.begin(),
46 std::back_inserter(result), std::plus<T>());
57 std::vector<T>
operator-(
const std::vector<T>& a,
const std::vector<T>& b)
59 assert(a.size() == b.size());
61 std::vector<T> result;
62 result.reserve(a.size());
64 std::transform(a.begin(), a.end(), b.begin(),
65 std::back_inserter(result), std::minus<T>());
76 template <
class T,
class T2>
77 std::vector<T>
operator* (
const T2 c, std::vector<T> a)
79 for (
unsigned i = 0; i < a.size(); ++i)
91 template <
typename T,
unsigned Size = 2>
114 template <
typename T,
unsigned Size>
128 template <
typename T,
unsigned Size>
142 template <
typename T,
unsigned Size>
153 template <
typename T>
162 template <
typename T>
182 template <
typename T,
unsigned Dim = 2>
191 data_(nx * ny, T()), nx_(nx), ny_(ny) {}
210 return data_[i*ny_ + j];
220 return data_[i*ny_ + j];
228 inline T&
Get(
unsigned i,
unsigned j)
230 return data_[i*ny_ + j];
238 inline T
Get(
unsigned i,
unsigned j)
const
240 return data_[i*ny_ + j];
250 return data_[i*ny_ + j];
266 inline auto GetNearby(
const unsigned i,
const unsigned j)
const
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);
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);
274 return std::make_tuple(x_pre, x_next, y_pre, y_next);
283 inline void SetValue(
unsigned i,
unsigned j, T value)
285 data_[i*ny_ + j] = value;
301 return std::make_tuple(nx_, ny_);
305 std::vector<T> data_;
320 template <
typename T>
323 return y1 + (xm - x1) * (y2 - y1) / (x2 - x1);
335 template <
typename Field,
typename T>
338 auto tensor_result = f1.
GetAll();
341 auto vec1 = f1.
GetAll().GetAll();
342 auto vec2 = f2.
GetAll().GetAll();
343 auto vec_result = vec1 + ((xm - x1)/(x2 - x1)) * (vec2 - vec1);
345 tensor_result.SetAll(vec_result);
347 result.
SetAll(tensor_result);
360 total_elapsed_time_(0), begin_time_(), end_time_(), started_(false) {}
368 begin_time_ = std::chrono::system_clock::now();
379 end_time_ = std::chrono::system_clock::now();
383 elapsed_time_ = std::chrono::duration_cast<std::chrono::nanoseconds>
384 (end_time_ - begin_time_).count() / 1e9;
385 total_elapsed_time_ += elapsed_time_;
394 return elapsed_time_;
402 return total_elapsed_time_;
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_;
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