Tatooine
discretize_field.h
Go to the documentation of this file.
1#ifndef TATOOINE_DISCRETIZE_FIELD_H
2#define TATOOINE_DISCRETIZE_FIELD_H
3//==============================================================================
4#include <tatooine/field.h>
5#include <tatooine/tags.h>
7//==============================================================================
8namespace tatooine {
9//==============================================================================
10template <
11 typename V, arithmetic VReal, std::size_t NumDimensions, typename Tensor,
12 floating_point_range... SpatialDimensions, arithmetic T>
15 rectilinear_grid<SpatialDimensions...> const& discretized_domain, T const t,
16 std::size_t padding = 0, VReal padval = 0) {
17 auto const nan = VReal(0) / VReal(0);
18 std::vector<VReal> raw_data;
19 auto vs = discretized_domain.vertices();
20 raw_data.reserve(vs.size() * (f.num_tensor_components() + padding));
21 for (auto v : vs) {
22 auto const x = vs[v];
23 auto sample = f(x, t);
24 if constexpr (f.is_scalarfield()) {
25 raw_data.push_back(static_cast<VReal>(sample));
26 } else {
27 for (std::size_t i = 0; i < f.num_tensor_components(); ++i) {
28 raw_data.push_back(static_cast<VReal>(sample[i]));
29 }
30 }
31 for (std::size_t i = 0; i < padding; ++i) {
32 raw_data.push_back(padval);
33 }
34 }
35 return raw_data;
36}
37//------------------------------------------------------------------------------
38template <typename V, arithmetic VReal, std::size_t NumDimensions,
39 typename Tensor,
40 floating_point_range... SpatialDimensions,
41 floating_point_range TemporalDimension>
44 rectilinear_grid<SpatialDimensions...> const& discretized_domain,
45 TemporalDimension const& temporal_domain, std::size_t padding = 0,
46 VReal padval = 0) {
47 auto const nan = VReal(0) / VReal(0);
48 std::vector<VReal> raw_data;
49 auto vs = discretized_domain.vertices();
50 raw_data.reserve(vs.size() * temporal_domain.size() *
51 (f.num_tensor_components() + padding));
52 for (auto t : temporal_domain) {
53 for (auto v : vs) {
54 auto const x = v.position();
55 auto sample = f(x, t);
56 if constexpr (f.is_scalarfield()) {
57 raw_data.push_back(static_cast<VReal>(sample));
58 } else {
59 for (std::size_t i = 0; i < f.num_tensor_components(); ++i) {
60 raw_data.push_back(static_cast<VReal>(sample[i]));
61 }
62 }
63 for (std::size_t i = 0; i < padding; ++i) {
64 raw_data.push_back(padval);
65 }
66 }
67 }
68 return raw_data;
69}
70//------------------------------------------------------------------------------
71template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
72 floating_point_range... SpatialDimensions>
75 rectilinear_grid<SpatialDimensions...> const& discretized_domain,
76 arithmetic auto const t) {
78 using tensor_type = typename V::tensor_type;
79 auto const nan = VReal(0) / VReal(0);
80 std::vector<tensor_type> data;
81 auto vs = discretized_domain.vertices();
82 data.reserve(vs.size());
83 for (auto v : vs) {
84 auto const x = vs[v];
85 data.push_back(f(x, t));
86 }
87 return data;
88}
89//------------------------------------------------------------------------------
90template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
91 floating_point_range... SpatialDimensions>
94 rectilinear_grid<SpatialDimensions...> const& discretized_domain,
95 arithmetic auto const t, execution_policy_tag auto const pol) -> auto {
96 auto data = dynamic_multidim_array<Tensor>{discretized_domain.size()};
97 discretized_domain.vertices().iterate_indices(
98 [&](auto const... is) {
99 data(is...) = f(discretized_domain.vertex_at(is...), t);
100 },
101 pol);
102 return data;
103}
104// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
106 floating_point_range... SpatialDimensions>
109 rectilinear_grid<SpatialDimensions...> const& discretized_domain,
110 arithmetic auto const t) -> auto {
111 return discretize(f, discretized_domain, t, execution_policy::sequential);
112}
113// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
115 floating_point_range... SpatialDimensions>
118 rectilinear_grid<SpatialDimensions...> const& discretized_domain) -> auto& {
119 return discretize(f, discretized_domain, 0, execution_policy::sequential);
120}
121// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
122template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
123 floating_point_range... SpatialDimensions>
126 rectilinear_grid<SpatialDimensions...> const& discretized_domain,
127 execution_policy_tag auto const pol) -> auto& {
128 return discretize(f, discretized_domain, 0, pol);
129}
130//------------------------------------------------------------------------------
131template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
132 floating_point_range... SpatialDimensions>
134 rectilinear_grid<SpatialDimensions...>& discretized_domain,
135 std::string const& property_name, arithmetic auto const t,
136 execution_policy_tag auto const pol) -> auto& {
137 auto& discretized_field = [&]() -> decltype(auto) {
138 if constexpr (is_arithmetic<Tensor>) {
139 return discretized_domain.template insert_vertex_property<VReal>(
140 property_name);
141 } else if constexpr (static_vec<Tensor>) {
142 return discretized_domain
143 .template vertex_property<vec<VReal, Tensor::dimension(0)>>(
144 property_name);
145 } else if constexpr (static_mat<Tensor>) {
146 return discretized_domain.template vertex_property<
148 property_name);
149 } else {
150 return discretized_domain.template vertex_property<Tensor>(property_name);
151 }
152 }();
153 discretized_domain.vertices().iterate_indices(
154 [&](auto const... is) {
155 auto const x = discretized_domain.vertex_at(is...);
156 discretized_field(is...) = f(x, t);
157 },
158 pol);
159 return discretized_field;
160}
161// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
162template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
163 floating_point_range... SpatialDimensions>
165 rectilinear_grid<SpatialDimensions...>& discretized_domain,
166 std::string const& property_name, arithmetic auto const t)
167 -> auto& {
168 return discretize(f, discretized_domain, property_name, t,
170}
171// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
172template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
173 floating_point_range... SpatialDimensions>
175 rectilinear_grid<SpatialDimensions...>& discretized_domain,
176 std::string const& property_name) -> auto& {
177 return discretize(f, discretized_domain, property_name, 0,
179}
180// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
181template <arithmetic VReal, std::size_t NumDimensions, typename Tensor,
182 floating_point_range... SpatialDimensions>
184 rectilinear_grid<SpatialDimensions...>& discretized_domain,
185 std::string const& property_name,
186 execution_policy_tag auto const pol) -> auto& {
187 return discretize(f, discretized_domain, property_name, 0, pol);
188}
189//------------------------------------------------------------------------------
192template <typename V, arithmetic VReal, std::size_t NumDimensions,
193 typename Tensor, typename BasisReal, typename X0Real, typename X1Real>
197 vec<X1Real, 2> const& spatial_size, std::size_t const res0,
198 std::size_t const res1, std::string const& property_name,
199 arithmetic auto const t) {
200 auto const cell_extent =
201 vec<VReal, 2>{spatial_size(0) / (res0 - 1), spatial_size(1) / (res1 - 1)};
202 std::cerr << x0 << '\n';
203 std::cerr << spatial_size << '\n';
204 std::cerr << cell_extent << '\n';
205 auto discretized_domain = rectilinear_grid{
206 linspace<VReal>{0, length(basis * vec{spatial_size(0), 0}), res0},
207 linspace<VReal>{0, length(basis * vec{0, spatial_size(1)}), res1}};
208
209 auto& discretized_field = [&]() -> decltype(auto) {
210 if constexpr (is_scalarfield<V>()) {
211 return discretized_domain.template insert_chunked_vertex_property<VReal>(
212 property_name);
213 } else if constexpr (is_vectorfield<V>()) {
214 return discretized_domain
215 .template vertex_property<vec<VReal, V::tensor_type::dimension(0)>>(
216 property_name);
217 } else if constexpr (is_matrixfield<V>()) {
218 return discretized_domain.template vertex_property<mat<
219 VReal, V::tensor_type::dimension(0), V::tensor_type::dimension(1)>>(
220 property_name);
221 } else {
222 return discretized_domain.template vertex_property<Tensor>(property_name);
223 }
224 }();
225 for (std::size_t i1 = 0; i1 < res1; ++i1) {
226 for (std::size_t i0 = 0; i0 < res0; ++i0) {
227 auto const x = x0 + basis * vec{cell_extent(0) * i0, cell_extent(1) * i1};
228 discretized_field(i0, i1) = f(x, t);
229 }
230 }
231 return discretized_domain;
232}
233//------------------------------------------------------------------------------
236template <typename V, arithmetic VReal, std::size_t NumDimensions,
237 typename Tensor, typename BasisReal, typename X0Real>
240 vec<X0Real, NumDimensions> const& x0, std::size_t const res0,
241 std::size_t const res1, std::string const& property_name,
242 arithmetic auto const t) {
243 return discretize(f, x0, basis, vec<BasisReal, 2>::ones(), res0, res1,
244 property_name, t);
245}
246//------------------------------------------------------------------------------
249template <typename V, arithmetic VReal, std::size_t NumDimensions,
250 typename Tensor, typename BasisReal, typename X0Real>
252 vec<BasisReal, NumDimensions> const& extent0,
253 vec<BasisReal, NumDimensions> const& extent1,
254 vec<X0Real, NumDimensions> const& /*x0*/,
255 std::size_t const res0, std::size_t const res1,
256 std::string const& property_name, arithmetic auto const t) {
258 basis.col(0) = extent0;
259 basis.col(1) = extent1;
260 return discretize(f, basis, res0, res1, property_name, t);
261}
262//==============================================================================
263} // namespace tatooine
264//==============================================================================
265#endif
Definition: dynamic_multidim_array.h:18
auto size() const -> auto const &
Definition: dynamic_multidim_size.h:107
Definition: rectilinear_grid.h:38
auto vertices() const
Definition: rectilinear_grid.h:637
Definition: concepts.h:33
Definition: tags.h:72
Definition: tensor_concepts.h:26
Definition: tensor_concepts.h:23
static constexpr sequential_t sequential
Definition: tags.h:63
Definition: algorithm.h:6
auto discretize(polymorphic::field< VReal, NumDimensions, Tensor > const &f, rectilinear_grid< SpatialDimensions... > const &discretized_domain, arithmetic auto const t, execution_policy_tag auto const pol) -> auto
Definition: discretize_field.h:92
tensor< real_number, Dimensions... > Tensor
Definition: tensor.h:184
auto sample_to_raw(field< V, VReal, NumDimensions, Tensor > const &f, rectilinear_grid< SpatialDimensions... > const &discretized_domain, T const t, std::size_t padding=0, VReal padval=0)
Definition: discretize_field.h:13
auto nan(const char *arg="")
Definition: nan.h:26
auto sample_to_vector(polymorphic::field< VReal, NumDimensions, Tensor > const &f, rectilinear_grid< SpatialDimensions... > const &discretized_domain, arithmetic auto const t)
Definition: discretize_field.h:73
auto constexpr dimension() const
Definition: contracted_dynamic_tensor.h:36
Definition: field.h:134
Definition: linspace.h:26
Definition: mat.h:14
auto constexpr col(std::size_t i)
Definition: mat.h:175
Definition: field.h:13
static constexpr auto is_scalarfield()
Definition: field.h:35
static constexpr auto num_tensor_components()
Definition: field.h:40
Definition: vec.h:12