PSCF v1.5.0
GridArray.h
1#ifndef UTIL_GRID_ARRAY_H
2#define UTIL_GRID_ARRAY_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/space/IntVector.h>
12#include <util/misc/Memory.h>
13#include <util/global.h>
14
15namespace Util
16{
17
28 template <typename Data>
30 {
31
32 public:
33
37 GridArray();
38
42 GridArray(GridArray<Data> const & other);
43
49 ~GridArray();
50
55
56 // Initialization
57
63 void allocate(IntVector const & dimensions);
64
71 template <class Archive>
72 void serialize(Archive& ar, const unsigned int version);
73
77 bool isAllocated() const;
78
79 // Grid Interface
80
86 IntVector const & dimensions();
87
93 int dimension(int i) const;
94
98 int size() const;
99
106 IntVector position(int rank) const;
107
114 int rank(IntVector const & position) const;
115
123 bool isInGrid(int coordinate, int i) const;
124
132 bool isInGrid(IntVector& position) const;
133
146 int shift(int& coordinate, int i) const;
147
160
161 // Array Interface
162
168 Data const & operator[] (int rank) const;
169
175 Data& operator[] (int rank);
176
182 Data const & operator() (IntVector const & position) const;
183
189 Data& operator() (IntVector const & position);
190
191 /*
192 * Return pointer to underlying 1D C-array.
193 */
194 Data* data();
195
196 private:
197
199 Data* data_;
200
202 IntVector offsets_;
203
205 IntVector dimensions_;
206
208 int size_;
209
210 };
211
212 // Method definitions
213
217 template <typename Data>
219 : data_(nullptr),
220 offsets_(),
221 dimensions_(),
222 size_(0)
223 {}
224
225 /*
226 * Destructor.
227 *
228 * Delete dynamically allocated C array.
229 */
230 template <typename Data>
232 {
233 if (data_) {
234 Memory::deallocate<Data>(data_, size_);
235 size_ = 0;
236 }
237 }
238
239 /*
240 * Copy constructor.
241 */
242 template <typename Data>
244 : data_(nullptr),
245 offsets_(),
246 dimensions_(),
247 size_(0)
248 {
249 // Precondition
250 if (!other.data_) {
251 UTIL_THROW("Other GridArray must be allocated");
252 }
253 if (isAllocated()) {
254 UTIL_THROW("GridArray already allocated in copy constructor");
255 }
256
257 allocate(other.dimensions_);
258 if (offsets_ != other.offsets_ ) {
259 UTIL_THROW("Unequal offsets");
260 }
261 if (size_ != other.size_ ) {
262 UTIL_THROW("Unequal sizes");
263 }
264 for (int i = 0; i < size_; ++i) {
265 data_[i] = other.data_[i];
266 }
267 }
268
269 /*
270 * Assignment.
271 */
272 template <typename Data>
275 {
276 // Check for self assignment.
277 if (this == &other) {
278 return *this;
279 }
280
281 // Precondition
282 if (!other.data_) {
283 UTIL_THROW("Other GridArray must be allocated before assignment");
284 }
285
286 // If this GridArray if not allocated, allocate now.
287 // If it is allocated, check that dimensions are equal.
288 if (!isAllocated()) {
289 allocate(other.dimensions_);
290 } else {
291 if (dimensions_ != other.dimensions_ ) {
292 UTIL_THROW("Unequal dimensions");
293 }
294 }
295 if (offsets_ != other.offsets_ ) {
296 UTIL_THROW("Unequal offsets");
297 }
298 if (size_ != other.size_ ) {
299 UTIL_THROW("Unequal sizes");
300 }
301
302 // Copy elements
303 for (int i = 0; i < size_; ++i) {
304 data_[i] = other.data_[i];
305 }
306
307 return *this;
308 }
309
310 /*
311 * Set dimensions and allocate memory.
312 */
313 template <typename Data>
315 {
316 if (isAllocated()) {
317 UTIL_THROW("Attempt to re-allocate a GridArray");
318 }
319 for (int i = 0; i < Dimension; ++i) {
320 if (dimensions[i] <= 0) {
321 UTIL_THROW("Dimension not positive");
322 }
323 }
324 dimensions_ = dimensions;
325 offsets_[Dimension -1] = 1;
326 for (int i = Dimension - 1; i > 0; --i) {
327 offsets_[i-1] = offsets_[i]*dimensions_[i];
328 }
329 size_ = offsets_[0]*dimensions_[0];
330 Memory::allocate<Data>(data_, size_);
331 }
332
333 /*
334 * Serialize a GridArray to/from an Archive.
335 */
336 template <typename Data>
337 template <class Archive>
338 void GridArray<Data>::serialize(Archive& ar, const unsigned int version)
339 {
341 if (Archive::is_saving()) {
342 dimensions = dimensions_;
343 }
344 ar & dimensions;
345 if (Archive::is_loading()) {
346 if (!isAllocated()) {
348 }
349 }
350 for (int i = 0; i < size_; ++i) {
351 ar & data_[i];
352 }
353 }
354
355 /*
356 * Get IntVector of dimensions.
357 */
358 template <typename Data>
360 { return dimensions_; }
361
362 /*
363 * Get dimension in direction i.
364 */
365 template <typename Data>
366 inline int GridArray<Data>::dimension(int i) const
367 { return dimensions_[i]; }
368
369 /*
370 * Get total number of grid points.
371 */
372 template <typename Data>
373 inline int GridArray<Data>::size() const
374 { return size_; }
375
376 /*
377 * Calculate 1D rank from grid coordinates.
378 */
379 template <typename Data>
380 #ifdef UTIL_DEBUG
381 int GridArray<Data>::rank(IntVector const & position) const
382 {
383 int result = 0;
384 int i;
385 for (i = 0; i < Dimension - 1; ++i) {
386 assert(position[i] >= 0);
387 assert(position[i] < dimensions_[i]);
388 result += position[i]*offsets_[i];
389 }
390 assert(position[i] >= 0);
391 assert(position[i] < dimensions_[i]);
392 result += position[i];
393 return result;
394 }
395 #else
396 inline int GridArray<Data>::rank(IntVector const & position) const
397 {
398 return (position[0]*offsets_[0] + position[1]*offsets_[1] + position[2]);
399 }
400 #endif
401
402 /*
403 * Calculate coordinates from 1D rank.
404 */
405 template <typename Data>
407 {
409 int remainder = rank;
410
411 int i;
412 for (i = 0; i < Dimension - 1; ++i) {
413 position[i] = remainder/offsets_[i];
414 remainder -= position[i]*offsets_[i];
415 }
416 position[i] = remainder;
417 return position;
418 }
419
420 /*
421 * Test if a single coordinate is within range of grid.
422 */
423 template <typename Data>
424 bool GridArray<Data>::isInGrid(int coordinate, int i) const
425 {
426 bool result = true;
427 if (coordinate < 0)
428 result = false;
429 if (coordinate >= dimensions_[i])
430 result = false;
431 return result;
432 }
433
434 /*
435 * Test if a IntVector position is within primary grid domain.
436 */
437 template <typename Data>
439 {
440 bool result = true;
441 for (int i = 0; i < Dimension; ++i) {
442 if (position[i] < 0)
443 result = false;
444 if (position[i] >= dimensions_[i])
445 result = false;
446 }
447 return result;
448 }
449
450 /*
451 * Shift a 1D coordinate to primary domain.
452 */
453 template <typename Data>
454 int GridArray<Data>::shift(int& coordinate, int i) const
455 {
456 int shift;
457 if (coordinate >= 0) {
458 shift = coordinate/dimensions_[i];
459 } else {
460 shift = -1 + ((coordinate+1)/dimensions_[i]);
461 }
462 coordinate -= shift*dimensions_[i];
463 return shift;
464 }
465
466 /*
467 * Shift a IntVector position to primary domain.
468 */
469 template <typename Data>
471 {
472 IntVector shifts;
473 for (int i = 0; i < Dimension; ++i) {
474 shifts[i] = shift(position[i], i);
475 }
476 return shifts;
477 }
478
479 /*
480 * Return element by const reference, indexed by 1D rank.
481 */
482 template <typename Data>
483 inline Data const & GridArray<Data>::operator[] (int rank) const
484 { return *(data_ + rank); }
485
486 /*
487 * Return element by reference, indexed by 1D rank.
488 */
489 template <typename Data>
491 { return *(data_ + rank); }
492
493 /*
494 * Return element by const reference, indexed by IntVector of coordinates
495 */
496 template <typename Data>
497 inline
499 const
500 { return *(data_ + rank(position)); }
501
502 /*
503 * Return element by reference, indexed by IntVector of coordinates
504 */
505 template <typename Data>
507 { return *(data_ + rank(position)); }
508
509 /*
510 * Return pointer to underlying 1D C-array.
511 */
512 template <typename Data>
513 inline Data * GridArray<Data>::data()
514 { return data_;}
515
516 /*
517 * Return true if the GridArray has been allocated, false otherwise.
518 */
519 template <typename Data>
521 { return (bool)data_; }
522
523}
524#endif
Multi-dimensional array with the dimensionality of space.
Definition GridArray.h:30
int rank(IntVector const &position) const
Get the rank of a grid point with specified position.
Definition GridArray.h:396
~GridArray()
Destructor.
Definition GridArray.h:231
GridArray()
Constructor.
Definition GridArray.h:218
Data const & operator[](int rank) const
Return element by const reference, indexed by 1D rank.
Definition GridArray.h:483
bool isAllocated() const
Return true if the GridArray has been allocated, false otherwise.
Definition GridArray.h:520
Data const & operator()(IntVector const &position) const
Return element by const reference, indexed by IntVector position.
Definition GridArray.h:498
IntVector position(int rank) const
Get the position IntVector of a grid point with a specified rank.
Definition GridArray.h:406
bool isInGrid(int coordinate, int i) const
Is this 1D coordinate in range?
Definition GridArray.h:424
GridArray< Data > & operator=(GridArray< Data > const &other)
Assignment.
Definition GridArray.h:274
void serialize(Archive &ar, const unsigned int version)
Serialize a GridArray to/from an Archive.
Definition GridArray.h:338
void allocate(IntVector const &dimensions)
Allocate memory for a matrix.
Definition GridArray.h:314
int dimension(int i) const
Get number of grid points along direction i.
Definition GridArray.h:366
int shift(int &coordinate, int i) const
Shift a periodic 1D coordinate into primary range.
Definition GridArray.h:454
IntVector const & dimensions()
Get all dimensions of array as an IntVector.
Definition GridArray.h:359
int size() const
Get total number of grid points.
Definition GridArray.h:373
An IntVector is an integer Cartesian vector.
Definition IntVector.h:74
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
Definition Memory.h:202
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
Definition Memory.h:172
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
const int Dimension
Dimensionality of space.
Definition Dimension.h:19
Utility classes for scientific computation.