FaceCenteredGrid3.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: FaceCenteredGrid3.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 3-D face-centered (a.k.a MAC or staggered) grid.
6 > Created Time: 2017/08/03
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_FACE_CENTERED_GRID3_H
10 #define CUBBYFLOW_FACE_CENTERED_GRID3_H
11 
12 #include <Core/Array/Array3.h>
14 #include <Core/Grid/VectorGrid3.h>
15 
16 namespace CubbyFlow
17 {
25  class FaceCenteredGrid3 final : public VectorGrid3
26  {
27  public:
29 
30  class Builder;
31 
34 
37 
40 
43  size_t resolutionX, size_t resolutionY, size_t resolutionZ,
44  double gridSpacingX = 1.0, double gridSpacingY = 1.0, double gridSpacingZ = 1.0,
45  double originX = 0.0, double originY = 0.0, double originZ = 0.0,
46  double initialValueU = 0.0, double initialValueV = 0.0, double initialValueW = 0.0);
47 
50  const Size3& resolution,
51  const Vector3D& gridSpacing = Vector3D(1.0, 1.0, 1.0),
52  const Vector3D& origin = Vector3D(),
53  const Vector3D& initialValue = Vector3D());
54 
57 
64  void Swap(Grid3* other) override;
65 
67  void Set(const FaceCenteredGrid3& other);
68 
71 
73  double& GetU(size_t i, size_t j, size_t k);
74 
76  const double& GetU(size_t i, size_t j, size_t k) const;
77 
79  double& GetV(size_t i, size_t j, size_t k);
80 
82  const double& GetV(size_t i, size_t j, size_t k) const;
83 
85  double& GetW(size_t i, size_t j, size_t k);
86 
88  const double& GetW(size_t i, size_t j, size_t k) const;
89 
91  Vector3D ValueAtCellCenter(size_t i, size_t j, size_t k) const;
92 
94  double DivergenceAtCellCenter(size_t i, size_t j, size_t k) const;
95 
97  Vector3D CurlAtCellCenter(size_t i, size_t j, size_t k) const;
98 
101 
104 
107 
110 
113 
116 
119 
122 
125 
127  Size3 GetUSize() const;
128 
130  Size3 GetVSize() const;
131 
133  Size3 GetWSize() const;
134 
141  Vector3D GetUOrigin() const;
142 
149  Vector3D GetVOrigin() const;
150 
157  Vector3D GetWOrigin() const;
158 
160  void Fill(const Vector3D& value, ExecutionPolicy policy = ExecutionPolicy::Parallel) override;
161 
163  void Fill(const std::function<Vector3D(const Vector3D&)>& func, ExecutionPolicy policy = ExecutionPolicy::Parallel) override;
164 
166  std::shared_ptr<VectorGrid3> Clone() const override;
167 
175  void ForEachUIndex(const std::function<void(size_t, size_t, size_t)>& func) const;
176 
185  void ParallelForEachUIndex(const std::function<void(size_t, size_t, size_t)>& func) const;
186 
194  void ForEachVIndex(const std::function<void(size_t, size_t, size_t)>& func) const;
195 
204  void ParallelForEachVIndex(const std::function<void(size_t, size_t, size_t)>& func) const;
205 
213  void ForEachWIndex(const std::function<void(size_t, size_t, size_t)>& func) const;
214 
223  void ParallelForEachWIndex(const std::function<void(size_t, size_t, size_t)>& func) const;
224 
225  // VectorField3 implementations
227  Vector3D Sample(const Vector3D& x) const override;
228 
230  double Divergence(const Vector3D& x) const override;
231 
233  Vector3D Curl(const Vector3D& x) const override;
234 
241  std::function<Vector3D(const Vector3D&)> Sampler() const override;
242 
244  static Builder GetBuilder();
245 
246  protected:
247  // VectorGrid3 implementations
248  void OnResize(
249  const Size3& resolution,
250  const Vector3D& gridSpacing,
251  const Vector3D& origin,
252  const Vector3D& initialValue) final;
253 
255  void GetData(std::vector<double>* data) const override;
256 
258  void SetData(const std::vector<double>& data) override;
259 
260  private:
261  Array3<double> m_dataU;
262  Array3<double> m_dataV;
263  Array3<double> m_dataW;
264  Vector3D m_dataOriginU;
265  Vector3D m_dataOriginV;
266  Vector3D m_dataOriginW;
267  LinearArraySampler3<double, double> m_uLinearSampler;
268  LinearArraySampler3<double, double> m_vLinearSampler;
269  LinearArraySampler3<double, double> m_wLinearSampler;
270  std::function<Vector3D(const Vector3D&)> m_sampler;
271 
272  void ResetSampler();
273  };
274 
276  using FaceCenteredGrid3Ptr = std::shared_ptr<FaceCenteredGrid3>;
277 
282  {
283  public:
285  Builder& WithResolution(const Size3& resolution);
286 
288  Builder& WithResolution(size_t resolutionX, size_t resolutionY, size_t resolutionZ);
289 
291  Builder& WithGridSpacing(const Vector3D& gridSpacing);
292 
294  Builder& WithGridSpacing(double gridSpacingX, double gridSpacingY, double gridSpacingZ);
295 
297  Builder& WithOrigin(const Vector3D& gridOrigin);
298 
300  Builder& WithOrigin(double gridOriginX, double gridOriginY, double gridOriginZ);
301 
303  Builder& WithInitialValue(const Vector3D& initialVal);
304 
306  Builder& WithInitialValue(double initialValX, double initialValY, double initialValZ);
307 
309  FaceCenteredGrid3 Build() const;
310 
313 
320  const Size3& resolution,
321  const Vector3D& gridSpacing,
322  const Vector3D& gridOrigin,
323  const Vector3D& initialVal) const override;
324 
325  private:
326  Size3 m_resolution{ 1, 1, 1 };
327  Vector3D m_gridSpacing{ 1, 1, 1 };
328  Vector3D m_gridOrigin{ 0, 0, 0 };
329  Vector3D m_initialVal{ 0, 0, 0 };
330  };
331 }
332 
333 #endif
ScalarDataAccessor GetUAccessor()
Returns u data accessor.
3-D vector class.
Definition: Vector3.h:26
Builder & WithInitialValue(const Vector3D &initialVal)
Returns builder with initial value.
Abstract base class for 3-D cartesian grid structure.
Definition: Grid3.h:30
3-D array accessor class.
Definition: ArrayAccessor3.h:31
void ForEachVIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Invokes the given function func for each v-data point.
ScalarDataAccessor GetWAccessor()
Returns w data accessor.
FaceCenteredGrid3()
Constructs empty grid.
FaceCenteredGrid3 & operator=(const FaceCenteredGrid3 &other)
Sets the contents with the given other grid.
std::function< Vector3D(size_t, size_t, size_t)> DataPositionFunc
Function type for mapping data index to actual position.
Definition: Grid3.h:34
void ParallelForEachVIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Invokes the given function func for each v-data point in parallel.
3-D read-only array accessor class.
Definition: ArrayAccessor3.h:269
Abstract base class for 3-D vector grid builder.
Definition: VectorGrid3.h:93
Size3 GetWSize() const
Returns data size of the w component.
Vector3D GetWOrigin() const
Returns w-data position for the grid point at (0, 0, 0).
Vector3D GetVOrigin() const
Returns v-data position for the grid point at (0, 0, 0).
double & GetU(size_t i, size_t j, size_t k)
Returns u-value at given data point.
double & GetV(size_t i, size_t j, size_t k)
Returns v-value at given data point.
double Divergence(const Vector3D &x) const override
Returns divergence at given position x.
void ForEachUIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Invokes the given function func for each u-data point.
FaceCenteredGrid3 Build() const
Builds CellCenteredScalarGrid3 instance.
Front-end to create CellCenteredScalarGrid3 objects step by step.
Definition: FaceCenteredGrid3.h:281
ConstScalarDataAccessor GetWConstAccessor() const
Returns read-only w data accessor.
#define CUBBYFLOW_GRID3_TYPE_NAME(DerivedClassName)
Definition: Grid3.h:116
Builder & WithGridSpacing(const Vector3D &gridSpacing)
Returns builder with grid spacing.
void ParallelForEachWIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Invokes the given function func for each w-data point in parallel.
3-D face-centered (a.k.a MAC or staggered) grid.
Definition: FaceCenteredGrid3.h:25
3-D point class.
Definition: Point3.h:26
std::shared_ptr< FaceCenteredGrid3 > FaceCenteredGrid3Ptr
Shared pointer type for the FaceCenteredGrid3.
Definition: FaceCenteredGrid3.h:276
DataPositionFunc GetWPosition() const
Returns function object that maps w data point to its actual position.
ScalarDataAccessor GetVAccessor()
Returns v data accessor.
void ParallelForEachUIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Invokes the given function func for each u-data point in parallel.
Vector3D Sample(const Vector3D &x) const override
Returns sampled value at given position x.
void Set(const FaceCenteredGrid3 &other)
Sets the contents with the given other grid.
void OnResize(const Size3 &resolution, const Vector3D &gridSpacing, const Vector3D &origin, const Vector3D &initialValue) final
Invoked when the resizing happens.
Vector3D ValueAtCellCenter(size_t i, size_t j, size_t k) const
Returns interpolated value at cell center.
Definition: pybind11Utils.h:24
double & GetW(size_t i, size_t j, size_t k)
Returns w-value at given data point.
void Fill(const Vector3D &value, ExecutionPolicy policy=ExecutionPolicy::Parallel) override
Fills the grid with given value.
ConstScalarDataAccessor GetVConstAccessor() const
Returns read-only v data accessor.
FaceCenteredGrid3Ptr MakeShared() const
Builds shared pointer of FaceCenteredGrid3 instance.
void Swap(Grid3 *other) override
Swaps the contents with the given other grid.
void SetData(const std::vector< double > &data) override
Sets the data from a continuous linear array.
Vector3D CurlAtCellCenter(size_t i, size_t j, size_t k) const
Returns curl at cell-center location.
Vector3D Curl(const Vector3D &x) const override
Returns curl at given position x.
Size3 GetUSize() const
Returns data size of the u component.
Builder & WithResolution(const Size3 &resolution)
Returns builder with resolution.
DataPositionFunc GetUPosition() const
Returns function object that maps u data point to its actual position.
DataPositionFunc GetVPosition() const
Returns function object that maps v data point to its actual position.
void ForEachWIndex(const std::function< void(size_t, size_t, size_t)> &func) const
Invokes the given function func for each w-data point.
3-D array class.
Definition: Array3.h:45
std::function< Vector3D(const Vector3D &)> Sampler() const override
Returns the sampler function.
Builder & WithOrigin(const Vector3D &gridOrigin)
Returns builder with grid origin.
Vector3D GetUOrigin() const
Returns u-data position for the grid point at (0, 0, 0).
ExecutionPolicy
Execution policy tag.
Definition: Parallel.h:15
Size3 GetVSize() const
Returns data size of the v component.
std::shared_ptr< VectorGrid3 > VectorGrid3Ptr
Shared pointer for the VectorGrid3 type.
Definition: VectorGrid3.h:90
ConstScalarDataAccessor GetUConstAccessor() const
Returns read-only u data accessor.
void GetData(std::vector< double > *data) const override
Fetches the data into a continuous linear array.
Abstract base class for 3-D vector grid structure.
Definition: VectorGrid3.h:19
static Builder GetBuilder()
Returns builder fox FaceCenteredGrid3.
std::shared_ptr< VectorGrid3 > Clone() const override
Returns the copy of the grid instance.
Vector3< double > Vector3D
Double-type 3D vector.
Definition: Vector3.h:353
3-D linear array sampler class.
Definition: ArraySamplers3.h:78
double DivergenceAtCellCenter(size_t i, size_t j, size_t k) const
Returns divergence at cell-center location.