UQTk: Uncertainty Quantification Toolkit  3.1.1
Array3D.h
Go to the documentation of this file.
1 /* =====================================================================================
2 
3  The UQ Toolkit (UQTk) version 3.1.1
4  Copyright (2021) NTESS
5  https://www.sandia.gov/UQToolkit/
6  https://github.com/sandialabs/UQTk
7 
8  Copyright 2021 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
9  Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10  retains certain rights in this software.
11 
12  This file is part of The UQ Toolkit (UQTk)
13 
14  UQTk is open source software: you can redistribute it and/or modify
15  it under the terms of BSD 3-Clause License
16 
17  UQTk is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  BSD 3 Clause License for more details.
21 
22  You should have received a copy of the BSD 3 Clause License
23  along with UQTk. If not, see https://choosealicense.com/licenses/bsd-3-clause/.
24 
25  Questions? Contact the UQTk Developers at <uqtk-developers@software.sandia.gov>
26  Sandia National Laboratories, Livermore, CA, USA
27 ===================================================================================== */
30 
31 
32 #ifndef ARRAY3D_H_SEEN
33 #define ARRAY3D_H_SEEN
34 
35 #include <stddef.h>
36 #include <vector>
37 #include <iostream>
38 #include <fstream>
39 #include <iterator>
40 #include <algorithm>
41 
42 using namespace std;
43 
55 template <typename T>
56 class Array3D {
57  public:
59  Array3D(): xsize_(0), ysize_(0), zsize_(0) {};
60 
62  Array3D(const size_t& nx, const size_t& ny, const size_t& nz):
63  xsize_(nx), ysize_(ny), zsize_(nz) {
64  data_.resize(xsize_*ysize_*zsize_);
65  }
66 
68  Array3D(const size_t& nx, const size_t& ny, const size_t& nz, const T& t):
69  xsize_(nx), ysize_(ny), zsize_(nz) {
70  data_.resize(xsize_*ysize_*zsize_ , t);
71  }
72 
74  ~Array3D() {data_.clear();}
75 
77  void Clear() {
78  xsize_ = 0;
79  ysize_ = 0;
80  zsize_ = 0;
81  data_.clear();
82  }
83 
85  size_t XSize() const {return xsize_;}
87  size_t YSize() const {return ysize_;}
89  size_t ZSize() const {return zsize_;}
90 
98  void Resize(const size_t& nx, const size_t& ny, const size_t& nz) {
99  xsize_ = nx;
100  ysize_ = ny;
101  zsize_ = nz;
102  data_.resize(xsize_*ysize_*zsize_);
103  }
104 
110  void Resize(const size_t& nx, const size_t& ny, const size_t& nz, const T& t) {
111  data_.clear();
112  xsize_ = nx;
113  ysize_ = ny;
114  zsize_ = nz;
115  data_.resize(xsize_*ysize_*zsize_ , t);
116  }
117 
119  void SetValue(const T& t){
120  for(size_t i=0; i < data_.size(); i++){
121  data_[i] = t;
122  }
123  }
124 
129  return &(data_[0]);
130  }
131 
135  const T* GetConstArrayPointer() const {
136  return &(data_[0]);
137  }
138 
144  T& operator()(size_t ix, size_t iy, size_t iz) {return data_[ix+xsize_*(iy+ysize_*iz)];}
145 
151  const T& operator()(size_t ix, size_t iy, size_t iz) const {return data_[ix+xsize_*(iy+ysize_*iz)];}
152 
154  void DumpBinary(FILE* f_out) const {
155  fwrite(&xsize_,sizeof(xsize_),1,f_out);
156  fwrite(&ysize_,sizeof(ysize_),1,f_out);
157  fwrite(&zsize_,sizeof(zsize_),1,f_out);
158  fwrite(this->GetConstArrayPointer(),sizeof(T),xsize_*ysize_*zsize_,f_out);
159  }
160 
165  void DumpText(std::ofstream& f_out) const {
166  vector<double>::const_iterator it1;
167  vector<double>::const_iterator it2;
168  it2=data_.begin();
169 
170  for (int iz=0;iz<zsize_;iz++) {
171  for (int iy=0;iy<ysize_;iy++) {
172  it1=it2;
173  advance(it2,xsize_);
174  std::copy(it1,it2,std::ostream_iterator<T>(f_out," "));
175  f_out << endl;
176  }
177  }
178 
179  }
180 
182  void ReadText(FILE* f_in){
183  fread(&xsize_,sizeof(xsize_),1,f_in);
184  fread(&ysize_,sizeof(ysize_),1,f_in);
185  fread(&zsize_,sizeof(zsize_),1,f_in);
186  data_.resize(xsize_*ysize_*zsize_);
187  fread(this->GetArrayPointer(),sizeof(T),xsize_*ysize_*zsize_,f_in);
188  }
189 
192  void ReadBinary(std::ifstream& f_in){
193 
194  typedef std::istream_iterator<T> istream_iterator;
195  std::copy(istream_iterator(f_in),istream_iterator(),data_.begin());
196  }
197 
198 
199  private:
200 
203  Array3D(const Array3D &obj) {};
204 
206  size_t xsize_;
208  size_t ysize_;
210  size_t zsize_;
211 
217  vector<T> data_;
218 };
219 
220 #endif /* ARRAY3D_H_SEEN */
Array1D< double > copy(Array1D< double > &in_array)
Returns a copy of 1D array.
Definition: arraytools.cpp:1604
Stores data of any type T in a 3D array.
Definition: Array3D.h:56
void SetValue(const T &t)
Set all values in the array to the given value.
Definition: Array3D.h:119
size_t XSize() const
Returns size in the x-direction.
Definition: Array3D.h:85
void DumpBinary(FILE *f_out) const
Dump contents of the array to a file in binary format.
Definition: Array3D.h:154
vector< T > data_
Data in the array with size = xsize_ * ysize_ * zsize_.
Definition: Array3D.h:217
Array3D()
Default constructor, which does not allocate any memory.
Definition: Array3D.h:59
void ReadBinary(std::ifstream &f_in)
Read contents of the array from a file in text format Added by Maher Salloum.
Definition: Array3D.h:192
~Array3D()
Destructor that frees up the memory.
Definition: Array3D.h:74
Array3D(const size_t &nx, const size_t &ny, const size_t &nz, const T &t)
Constructor that allocates and initializes the data.
Definition: Array3D.h:68
T * GetArrayPointer()
Return a pointer to the first element of the data in the vector so we can use it access the data in a...
Definition: Array3D.h:128
void Clear()
Function to clear the memory.
Definition: Array3D.h:77
void DumpText(std::ofstream &f_out) const
Dump contents of the array to a file in text format Added by Maher Salloum When post-processing (in m...
Definition: Array3D.h:165
size_t ysize_
Number of elements in the y-dimension.
Definition: Array3D.h:208
size_t zsize_
Number of elements in the z-dimension.
Definition: Array3D.h:210
Array3D(const size_t &nx, const size_t &ny, const size_t &nz)
Constructor that allocates the memory.
Definition: Array3D.h:62
void ReadText(FILE *f_in)
Read contents of the array from a file in binary format.
Definition: Array3D.h:182
void Resize(const size_t &nx, const size_t &ny, const size_t &nz, const T &t)
Resizes the array and sets ALL entries to the specified value.
Definition: Array3D.h:110
size_t ZSize() const
Returns size in the z-direction.
Definition: Array3D.h:89
const T * GetConstArrayPointer() const
Return a const pointer to the first element of the data in the vector so we can use it access the dat...
Definition: Array3D.h:135
void Resize(const size_t &nx, const size_t &ny, const size_t &nz)
Resizes the array.
Definition: Array3D.h:98
size_t YSize() const
Returns size in the y-direction.
Definition: Array3D.h:87
Array3D(const Array3D &obj)
Copy constructor, which is made private so it would not be used inadvertently (until we define a prop...
Definition: Array3D.h:203
const T & operator()(size_t ix, size_t iy, size_t iz) const
Fortran-like () const operator to access values in the 3D data array.
Definition: Array3D.h:151
size_t xsize_
Number of elements in the x-dimension.
Definition: Array3D.h:203
T & operator()(size_t ix, size_t iy, size_t iz)
Fortran-like () operator to access values in the 3D data array.
Definition: Array3D.h:144