Tatooine
rbc.h
Go to the documentation of this file.
1#ifndef TATOOINE_RBC_H
2#define TATOOINE_RBC_H
3
4#include "field.h"
5#include "grid_sampler.h"
6
7//==============================================================================
8namespace tatooine {
9//==============================================================================
10
11struct rbc : field<rbc, double, 2, 2> {
12 using this_type = rbc;
17 using grid_t = grid_sampler<real_type, 2, vec<real_type, 2>, interpolation::linear,
19 static constexpr std::array dim{512ul, 128ul, 201ul};
20 static constexpr grid domain{linspace{0.00390625, 3.99609375, dim[0]},
21 linspace{0.00390625, 0.99609375, dim[1]},
22 linspace{2000.0, 2020.0, dim[2]}};
23 //============================================================================
24 private:
25 std::vector<grid_t> grids;
26
27 //============================================================================
28 public:
29 rbc(const std::string& rbc_binary_dir) { read_from_binary(rbc_binary_dir); }
30 //----------------------------------------------------------------------------
31 void read_from_binary(const std::string& rbc_binary_dir) {
32 grids.reserve(dim[2]);
33 for (size_t ti = 0; ti < dim[2]; ++ti) {
34 std::stringstream ss;
35 ss << domain.dimension(2)[ti];
36 const std::string filename = rbc_binary_dir + "/rbc_" + ss.str() + ".bin";
37 grids.emplace_back(domain.dimension(0), domain.dimension(1));
38
39 std::ifstream file(filename, std::ifstream::binary);
40 if (file.is_open()) {
41 std::vector<vec<double, 2>> data(dim[0] * dim[1]);
42 // std::cout << "reading: " << filename <<'\n';
43 constexpr auto num_bytes = sizeof(double) * dim[0] * dim[1] * 2;
44 file.read((char*)(data.data()), num_bytes);
45 file.close();
46
47 grids.back().data() = data;
48 } else {
49 throw std::runtime_error{"could not open " + filename};
50 }
51 }
52 }
53 //----------------------------------------------------------------------------
54 tensor_type evaluate(const pos_type& pos, real_type t) const {
55 const auto& times = domain.dimension(2);
56 for (size_t i = 0; i < grids.size() - 1; ++i)
57 if (times[i] <= t && t <= times[i + 1]) {
58 real_type f = (t - times[i]) / (times[i + 1] - times[i]);
59 return (1 - f) * grids[i](pos(0), pos(1)) +
60 f * grids[i + 1](pos(0), pos(1));
61 }
62 return {0, 0};
63 }
64 //----------------------------------------------------------------------------
65 bool in_domain(const pos_type& p, real_type t) const {
66 auto& times = domain.dimension(2);
67 return times.front() <= t && t <= times.back() &&
68 grids.front().in_domain(p(0), p(1));
69 }
70};
71
72//==============================================================================
73} // namespace tatooine
74//==============================================================================
75#endif
Definition: grid_edge.h:16
Definition: algorithm.h:6
Definition: field.h:134
Real real_type
Definition: field.h:17
2 tensor_type
Definition: field.h:18
Definition: interpolation.h:16
Definition: linspace.h:26
Definition: rbc.h:11
bool in_domain(const pos_type &p, real_type t) const
Definition: rbc.h:65
grid_sampler< real_type, 2, vec< real_type, 2 >, interpolation::linear, interpolation::linear > grid_t
Definition: rbc.h:18
static constexpr grid domain
Definition: rbc.h:20
std::vector< grid_t > grids
Definition: rbc.h:25
static constexpr std::array dim
Definition: rbc.h:19
tensor_type evaluate(const pos_type &pos, real_type t) const
Definition: rbc.h:54
rbc(const std::string &rbc_binary_dir)
Definition: rbc.h:29
void read_from_binary(const std::string &rbc_binary_dir)
Definition: rbc.h:31
Definition: vec.h:12