14 template <
typename T,
unsigned Dim,
unsigned Size>
17 template <
typename T,
unsigned Dim>
20 template <
typename T,
unsigned Dim>
23 template <
typename T,
typename Func,
unsigned Dim>
34 template <
typename T,
unsigned Dim = 2,
unsigned Size = 2>
103 template <
typename T,
unsigned Dim,
unsigned Size>
110 template <
typename T,
unsigned Size>
120 file.open(file_name);
122 throw std::runtime_error(
"file does not open correctly!");
125 file << field.
nx_ << std::endl;
126 file << field.
ny_ << std::endl;
127 file << field.
time_ << std::endl;
139 file.open(file_name, std::ios::in);
141 throw std::runtime_error(
"file does not open correctly!");
145 file >> nx; file >> ny;
146 if (field.
nx_!=nx || field.
ny_!=ny)
147 throw std::domain_error(
"sizes do not match!");
166 template <
typename T,
unsigned Dim = 2>
167 class Position :
public Field<T, Dim, Dim>
182 void SetAll(
const std::vector<T>& xrange,
const std::vector<T>& yrange)
185 if (xrange.size()!=this->
nx_||yrange.size()!=this->
ny_)
186 throw std::domain_error(
"sizes do not match!");
189 #pragma omp parallel for
190 for (
unsigned i = 0; i < this->
nx_; ++i)
191 for (
unsigned j = 0; j < this->
ny_; ++j)
192 this->
data_(i,j) =
vec(xrange[i], yrange[j]);
194 pos_xrange_ = xrange;
195 pos_yrange_ = yrange;
204 void SetAll(
const T& xmin,
const T& xmax,
const T& ymin,
const T& ymax)
206 std::vector<T> xrange(this->
nx_, 0), yrange(this->
ny_, 0);
209 std::generate(xrange.begin(), xrange.end(),
210 [&]{
return xmin + (i++) * (xmax-xmin)/(this->
nx_-1);});
211 std::generate(yrange.begin(), yrange.end(),
212 [&]{
return ymin + (j++) * (ymax-ymin)/(this->
ny_-1);});
228 inline auto Get(
const unsigned i,
const unsigned j)
const
230 return std::make_tuple(this->
data_(i,j).x, this->
data_(i,j).y);
251 #pragma omp parallel for
252 for (
unsigned i = 0; i < this->
nx_; ++i)
254 for (
unsigned j = 0; j < this->
ny_; ++j)
257 std::tie(vx, vy) = vel.
Get(i,j);
258 this->
data_(i,j).x += vx * delta;
259 this->
data_(i,j).y += vy * delta;
263 if (out_of_bound_ !=
nullptr)
264 if (this->
data_(i,j).x < bound_xmin_ ||
265 this->
data_(i,j).x > bound_xmax_ ||
266 this->
data_(i,j).y < bound_ymin_ ||
267 this->
data_(i,j).y > bound_ymax_)
268 out_of_bound_->SetValue(i, j,
true);
288 if (out_of_bound_ !=
nullptr)
289 return out_of_bound_->GetValue(i,j);
300 inline void SetBound(
const T& xmin,
const T& xmax,
const T& ymin,
const T& ymax)
309 std::unique_ptr<Tensor<bool, Dim>> out_of_bound_;
310 std::vector<T> pos_xrange_;
311 std::vector<T> pos_yrange_;
326 template <
typename T,
unsigned Dim = 2>
327 class Velocity :
public Field<T, Dim, Dim>
345 inline auto Get(
const unsigned i,
const unsigned j)
const
347 return std::make_tuple(this->
data_(i,j).x, this->
data_(i,j).y);
364 auto const ref_pos_x = ref_vel.GetPosition().GetRange(0);
365 auto const ref_pos_y = ref_vel.GetPosition().GetRange(1);
367 #pragma omp parallel for
368 for (
unsigned i = 0; i < this->
nx_; ++i)
373 for (
unsigned j = 0; j < this->
ny_; ++j)
376 if (
pos_.IsOutOfBound(i,j))
continue;
378 std::tie(pos_x, pos_y) =
pos_.Get(i, j);
380 auto iter_x = std::upper_bound(ref_pos_x.begin(), ref_pos_x.end(), pos_x);
381 if (iter_x == ref_pos_x.end()) --iter_x;
382 if (iter_x == ref_pos_x.begin()) ++iter_x;
383 int i_next = std::distance(ref_pos_x.begin(), iter_x);
384 int i_pre = i_next - 1;
386 auto iter_y = std::upper_bound(ref_pos_y.begin(), ref_pos_y.end(), pos_y);
387 if (iter_y == ref_pos_y.end()) --iter_y;
388 if (iter_y == ref_pos_y.begin()) ++iter_y;
389 int j_next = std::distance(ref_pos_y.begin(), iter_y);
390 int j_pre = j_next - 1;
393 std::tie(ref_v(0,0).x, ref_v(0,0).y) = ref_vel.Get(i_pre, j_pre);
394 std::tie(ref_v(0,1).x, ref_v(0,1).y) = ref_vel.Get(i_pre, j_next);
395 std::tie(ref_v(1,0).x, ref_v(1,0).y) = ref_vel.Get(i_next, j_pre);
396 std::tie(ref_v(1,1).x, ref_v(1,1).y) = ref_vel.Get(i_next, j_next);
398 auto vx1 =
interpolate(ref_pos_x[i_pre], ref_pos_x[i_next],
399 ref_v(0,0).x, ref_v(1,0).x, pos_x);
400 auto vx2 =
interpolate(ref_pos_x[i_pre], ref_pos_x[i_next],
401 ref_v(0,1).x, ref_v(1,1).x, pos_x);
402 auto vxm =
interpolate(ref_pos_y[j_pre], ref_pos_y[j_next],
405 auto vy1 =
interpolate(ref_pos_x[i_pre], ref_pos_x[i_next],
406 ref_v(0,0).y, ref_v(1,0).y, pos_x);
407 auto vy2 =
interpolate(ref_pos_x[i_pre], ref_pos_x[i_next],
408 ref_v(0,1).y, ref_v(1,1).y, pos_x);
409 auto vym =
interpolate(ref_pos_y[j_pre], ref_pos_y[j_next],
412 this->
data_(i,j).x = vxm;
413 this->
data_(i,j).y = vym;
429 template <
typename T,
typename Func,
unsigned Dim>
456 std::vector<T>& parameters, T time=0):
Velocity<T, Dim>(nx, ny, pos), f_(parameters)
466 #pragma omp parallel for
467 for (
unsigned i = 0; i < this->
nx_; ++i)
469 for (
unsigned j = 0; j < this->
ny_; ++j)
472 std::tie(x, y) = this->
pos_.Get(i, j);
473 std::tie(vx, vy) = f_(x, y, this->
time_);
void ReadFromFile(const std::string &file_name)
Definition: field.hpp:63
Field(unsigned nx, unsigned ny)
Definition: field.hpp:42
Field of particle velocities.
Definition: field.hpp:21
Position< T, Dim > & pos_
Definition: field.hpp:419
const unsigned nx_
Definition: field.hpp:95
Position(unsigned nx, unsigned ny)
Definition: field.hpp:176
void SetAll(Tensor< vec, Dim > &data)
Definition: field.hpp:218
bool IsOutOfBound(const unsigned i, const unsigned j)
Definition: field.hpp:286
Field of particle positions.
Definition: field.hpp:18
void InterpolateFrom(Velocity< T, Dim > &ref_vel)
Definition: field.hpp:361
Class for tensors.
Definition: basic.hpp:183
auto Get(const unsigned i, const unsigned j) const
Definition: field.hpp:345
T interpolate(T x1, T x2, T y1, T y2, T xm)
Definition: basic.hpp:321
Velocity field with continuous velocity function.
Definition: field.hpp:24
auto & GetAll() const
Definition: field.hpp:47
void SetBound(const T &xmin, const T &xmax, const T &ymin, const T &ymax)
Definition: field.hpp:300
static void WriteToFile(const Field< T, 2, Size > &field, const std::string &file_name)
Definition: field.hpp:117
const unsigned ny_
Definition: field.hpp:96
void Update(Velocity< T, Dim > &vel, T delta)
Definition: field.hpp:249
void SetAll(Tensor< Vector< T, Size >, Dim > &data)
Definition: field.hpp:71
Class for general physical fields.
Definition: field.hpp:35
ContinuousVelocity(unsigned nx, unsigned ny, Position< T, Dim > &pos, T time=0)
Definition: field.hpp:441
void UpdateTime(const T time)
Definition: field.hpp:79
Dimension-dependent implementations of Field class.
Definition: field.hpp:15
Basic classes needed such as Vector and Tensor.
Tensor< Vector< T, Size >, Dim > data_
Definition: field.hpp:97
ContinuousVelocity(unsigned nx, unsigned ny, Position< T, Dim > &pos, std::vector< T > ¶meters, T time=0)
Definition: field.hpp:455
void SetAll()
Definition: field.hpp:464
void WriteToFile(const std::string &file_name) const
Definition: field.hpp:87
Struct for vectors.
Definition: basic.hpp:92
Velocity(unsigned nx, unsigned ny, Position< T, Dim > &pos)
Definition: field.hpp:337
auto & GetPosition()
Definition: field.hpp:353
void SetAll(const T &xmin, const T &xmax, const T &ymin, const T &ymax)
Definition: field.hpp:204
void SetAll(const std::vector< T > &xrange, const std::vector< T > &yrange)
Definition: field.hpp:182
auto GetTime() const
Definition: field.hpp:55
void InitializeOutOfBoundTensor()
Definition: field.hpp:275
auto Get(const unsigned i, const unsigned j) const
Definition: field.hpp:228
auto & GetRange(const unsigned axis)
Definition: field.hpp:236
Func & Function()
Definition: field.hpp:482
static void ReadFromFile(Field< T, 2, Size > &field, const std::string &file_name)
Definition: field.hpp:136
T time_
Definition: field.hpp:98