62using pfc::world::get_total_size;
64template <
typename T>
struct Field {
65 std::vector<T> m_data;
68 Field(
const World &world) : m_data(get_total_size(world)), m_world(world) {
78 T &operator[](
size_t i) {
return m_data[
i]; }
80 const T &operator[](
size_t i)
const {
return m_data[
i]; }
83template <
typename T>
inline Field<T> create(
const World &world) {
87template <
typename T>
inline const auto &get_data(
const Field<T> &field) {
91template <
typename T>
inline auto &get_data(Field<T> &field) {
return field.m_data; }
93template <
typename T>
inline const auto &
get_world(
const Field<T> &field) {
97template <
typename T> Field<T>
create(
const World &world, std::vector<T> &&data) {
99 if (data.size() != get_total_size(world)) {
100 throw std::runtime_error(
"Moved-in data size mismatch.");
102 f.m_data = std::move(data);
107Field<T>
create(
const World &world,
const std::vector<T> &data) {
109 if (data.size() != get_total_size(world)) {
110 throw std::runtime_error(
"Copied-in data size mismatch.");
112 std::copy(data.begin(), data.end(), f.m_data.begin());
116template <
typename T,
typename Func,
117 typename = std::enable_if_t<std::is_invocable_v<Func, Real3>>>
118Field<T>
create(
const World &world, Func &&func) {
120 apply(f, std::forward<Func>(func));
124template <
typename T,
typename Func>
void apply(Field<T> &f, Func &&func) {
127 static_assert(std::is_invocable_v<Func, Real3>,
128 "Func must be callable with (Real3 = std::array<double, 3>)");
130 auto &data = get_data(f);
138 for (
int k = low[2]; k <= high[2]; ++k) {
139 for (
int j = low[1]; j <= high[1]; ++j) {
140 for (
int i = low[0]; i <= high[0]; ++i) {
141 data[idx++] = std::invoke(func,
to_coords(cs, {i, j, k}));
147template <
typename T>
auto indices(
const Field<T> &f) {
152 bool operator!=(
const Iterator &other)
const {
return i != other.i; }
153 size_t operator*()
const {
return i; }
154 Iterator &operator++() {
159 Iterator begin()
const {
return {0}; }
160 Iterator end()
const {
return {size}; }
162 return IndexRange{get_data(f).size()};
Core type definitions for World parameters.
Extensible coordinate system framework.
const Real3 to_coords(const CartesianCS &cs, const Int3 &idx) noexcept
Convert grid indices to physical coordinates.
Definition csys.hpp:291
const auto & get_world(const Decomposition &decomposition) noexcept
Alias for get_global_world()
Definition decomposition.hpp:240
const auto & get_upper(const CartesianWorld &world) noexcept
Get the upper bounds of the world in a specific dimension.
Definition world_queries.hpp:168
auto create(const World< T > &world, const heffte::box3d< int > &box)
Construct a new World object from an existing one and a box.
Definition decomposition.hpp:62
const auto & get_lower(const CartesianWorld &world) noexcept
Get the lower bounds of the world.
Definition world_queries.hpp:149
const auto & get_coordinate_system(const World< T > &world) noexcept
Get the coordinate system of the world.
Definition world_queries.hpp:192
Represents the global simulation domain (the "world").
Definition world.hpp:91
World class definition and unified interface.