ARTS 2.5.9 (git: 825fa5f2)
field.h
Go to the documentation of this file.
1/* Copyright (C) 2019 Richard Larsson <ric.larsson@gmail.com>
2
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 USA. */
17
26#ifndef FIELD_HEADER
27#define FIELD_HEADER
28#include "interpolation.h"
29#include <vector>
30
32template <class base>
33class Field3D {
34 private:
35 size_t mpages, mrows, mcols;
36 std::vector<base> data;
37
38 public:
44 Field3D(const Field3D& g) = default;
45
52 Field3D& operator=(const Field3D& g) = default;
53
60 Field3D& operator=(Field3D&& g) noexcept = default;
61
67 Field3D(Field3D&& g) noexcept
68 : mpages(std::move(g.mpages)),
69 mrows(std::move(g.mrows)),
70 mcols(std::move(g.mcols)),
71 data(std::move(g.data)) {}
72
81 Field3D(size_t pages = 0,
82 size_t rows = 0,
83 size_t cols = 0,
84 const base& init = base())
85 : mpages(pages),
86 mrows(rows),
87 mcols(cols),
88 data(cols * rows * pages, init) {}
89
100 base& operator()(size_t page = 0, size_t row = 0, size_t col = 0) {
101 return data[col + row * mcols + page * mrows * mcols];
102 }
103
114 const base& operator()(size_t col = 0,
115 size_t row = 0,
116 size_t page = 0) const {
117 return data[col + row * mcols + page * mrows * mcols];
118 }
119
120
133 base operator()(const GridPos& page = {0, {0, 1}},
134 const GridPos& row = {0, {0, 1}},
135 const GridPos& col = {0, {0, 1}}) const {
136 const std::array pos{
137 col.idx + 0 + (row.idx + 0) * mcols + (page.idx + 0) * mrows * mcols,
138 col.idx + 1 + (row.idx + 0) * mcols + (page.idx + 0) * mrows * mcols,
139 col.idx + 0 + (row.idx + 1) * mcols + (page.idx + 0) * mrows * mcols,
140 col.idx + 1 + (row.idx + 1) * mcols + (page.idx + 0) * mrows * mcols,
141 col.idx + 0 + (row.idx + 0) * mcols + (page.idx + 1) * mrows * mcols,
142 col.idx + 1 + (row.idx + 0) * mcols + (page.idx + 1) * mrows * mcols,
143 col.idx + 0 + (row.idx + 1) * mcols + (page.idx + 1) * mrows * mcols,
144 col.idx + 1 + (row.idx + 1) * mcols + (page.idx + 1) * mrows * mcols};
145
146 const std::array w{page.fd[1] * row.fd[1] * col.fd[1],
147 page.fd[1] * row.fd[1] * col.fd[0],
148 page.fd[1] * row.fd[0] * col.fd[1],
149 page.fd[1] * row.fd[0] * col.fd[0],
150 page.fd[0] * row.fd[1] * col.fd[1],
151 page.fd[0] * row.fd[1] * col.fd[0],
152 page.fd[0] * row.fd[0] * col.fd[1],
153 page.fd[0] * row.fd[0] * col.fd[0]};
154
155 bool any_base = false;
156 base out;
157 for (size_t i = 0; i < 8; i++) {
158 if (w[i] not_eq 0) {
159 if (any_base)
160 out += w[i] * data[pos[i]];
161 else
162 out = w[i] * data[pos[i]];
163 any_base = true;
164 }
165 }
166 return out;
167 }
168
170 [[nodiscard]] size_t npages() const { return mpages; }
171
173 [[nodiscard]] size_t nrows() const { return mrows; }
174
176 [[nodiscard]] size_t ncols() const { return mcols; }
177
178 friend std::ostream& operator<<(std::ostream& os, const Field3D& v) {
179 for (size_t i = 0; i < v.npages(); i++)
180 for (size_t j = 0; j < v.nrows(); j++)
181 for (size_t k = 0; k < v.ncols(); k++) os << v(i, j, k) << '\n';
182 return os;
183}
184};
185
186#endif // FIELD_HEADER
Creates a 3D field of a base unit.
Definition: field.h:33
base operator()(const GridPos &page={0, {0, 1}}, const GridPos &row={0, {0, 1}}, const GridPos &col={0, {0, 1}}) const
Weighted access operator by GridPos.
Definition: field.h:133
size_t ncols() const
Number of columns.
Definition: field.h:176
std::vector< base > data
Definition: field.h:36
Field3D(const Field3D &g)=default
Construct a new Field3D object.
friend std::ostream & operator<<(std::ostream &os, const Field3D &v)
Definition: field.h:178
size_t mpages
Definition: field.h:35
size_t npages() const
Number of pages.
Definition: field.h:170
Field3D(Field3D &&g) noexcept
Construct a new Field3D object.
Definition: field.h:67
Field3D & operator=(Field3D &&g) noexcept=default
Default move operator.
base & operator()(size_t page=0, size_t row=0, size_t col=0)
Access operator.
Definition: field.h:100
size_t mcols
Definition: field.h:35
Field3D(size_t pages=0, size_t rows=0, size_t cols=0, const base &init=base())
Construct a new Field 3 D object.
Definition: field.h:81
size_t mrows
Definition: field.h:35
size_t nrows() const
Number of rows.
Definition: field.h:173
Field3D & operator=(const Field3D &g)=default
Default assignment operator.
const base & operator()(size_t col=0, size_t row=0, size_t page=0) const
Access operator.
Definition: field.h:114
Header file for interpolation.cc.
Structure to store a grid position.
Definition: interpolation.h:73
std::array< Numeric, 2 > fd
Definition: interpolation.h:75
Index idx
Definition: interpolation.h:74
#define v
#define w