FaceCenteredGrid2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: FaceCenteredGrid2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 2-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_GRID2_H
10 #define CUBBYFLOW_FACE_CENTERED_GRID2_H
11 
12 #include <Core/Array/Array2.h>
14 #include <Core/Grid/VectorGrid2.h>
15 
16 namespace CubbyFlow
17 {
25  class FaceCenteredGrid2 final : public VectorGrid2
26  {
27  public:
29 
30  class Builder;
31 
34 
37 
40 
43  size_t resolutionX, size_t resolutionY,
44  double gridSpacingX = 1.0, double gridSpacingY = 1.0,
45  double originX = 0.0, double originY = 0.0,
46  double initialValueU = 0.0, double initialValueV = 0.0);
47 
50  const Size2& resolution,
51  const Vector2D& gridSpacing = Vector2D(1.0, 1.0),
52  const Vector2D& origin = Vector2D(),
53  const Vector2D& initialValue = Vector2D());
54 
57 
64  void Swap(Grid2* other) override;
65 
67  void Set(const FaceCenteredGrid2& other);
68 
71 
73  double& GetU(size_t i, size_t j);
74 
76  const double& GetU(size_t i, size_t j) const;
77 
79  double& GetV(size_t i, size_t j);
80 
82  const double& GetV(size_t i, size_t j) const;
83 
85  Vector2D ValueAtCellCenter(size_t i, size_t j) const;
86 
88  double DivergenceAtCellCenter(size_t i, size_t j) const;
89 
91  double CurlAtCellCenter(size_t i, size_t j) const;
92 
95 
98 
101 
104 
107 
110 
112  Size2 GetUSize() const;
113 
115  Size2 GetVSize() const;
116 
123  Vector2D GetUOrigin() const;
124 
131  Vector2D GetVOrigin() const;
132 
134  void Fill(const Vector2D& value, ExecutionPolicy policy = ExecutionPolicy::Parallel) override;
135 
137  void Fill(const std::function<Vector2D(const Vector2D&)>& func, ExecutionPolicy policy = ExecutionPolicy::Parallel) override;
138 
140  std::shared_ptr<VectorGrid2> Clone() const override;
141 
149  void ForEachUIndex(const std::function<void(size_t, size_t)>& func) const;
150 
159  void ParallelForEachUIndex(const std::function<void(size_t, size_t)>& func) const;
160 
168  void ForEachVIndex(const std::function<void(size_t, size_t)>& func) const;
169 
178  void ParallelForEachVIndex(const std::function<void(size_t, size_t)>& func) const;
179 
180  // VectorField2 implementations
182  Vector2D Sample(const Vector2D& x) const override;
183 
185  double Divergence(const Vector2D& x) const override;
186 
188  double Curl(const Vector2D& x) const override;
189 
196  std::function<Vector2D(const Vector2D&)> Sampler() const override;
197 
199  static Builder GetBuilder();
200 
201  protected:
202  // VectorGrid2 implementations
203  void OnResize(
204  const Size2& resolution,
205  const Vector2D& gridSpacing,
206  const Vector2D& origin,
207  const Vector2D& initialValue) final;
208 
210  void GetData(std::vector<double>* data) const override;
211 
213  void SetData(const std::vector<double>& data) override;
214 
215  private:
216  Array2<double> m_dataU;
217  Array2<double> m_dataV;
218  Vector2D m_dataOriginU;
219  Vector2D m_dataOriginV;
220  LinearArraySampler2<double, double> m_uLinearSampler;
221  LinearArraySampler2<double, double> m_vLinearSampler;
222  std::function<Vector2D(const Vector2D&)> m_sampler;
223 
224  void ResetSampler();
225  };
226 
228  using FaceCenteredGrid2Ptr = std::shared_ptr<FaceCenteredGrid2>;
229 
234  {
235  public:
237  Builder& WithResolution(const Size2& resolution);
238 
240  Builder& WithResolution(size_t resolutionX, size_t resolutionY);
241 
243  Builder& WithGridSpacing(const Vector2D& gridSpacing);
244 
246  Builder& WithGridSpacing(double gridSpacingX, double gridSpacingY);
247 
249  Builder& WithOrigin(const Vector2D& gridOrigin);
250 
252  Builder& WithOrigin(double gridOriginX, double gridOriginY);
253 
255  Builder& WithInitialValue(const Vector2D& initialVal);
256 
258  Builder& WithInitialValue(double initialValX, double initialValY);
259 
261  FaceCenteredGrid2 Build() const;
262 
265 
272  const Size2& resolution,
273  const Vector2D& gridSpacing,
274  const Vector2D& gridOrigin,
275  const Vector2D& initialVal) const override;
276 
277  private:
278  Size2 m_resolution{ 1, 1 };
279  Vector2D m_gridSpacing{ 1, 1 };
280  Vector2D m_gridOrigin{ 0, 0 };
281  Vector2D m_initialVal{ 0, 0 };
282  };
283 }
284 
285 #endif
Builder & WithResolution(const Size2 &resolution)
Returns builder with resolution.
double CurlAtCellCenter(size_t i, size_t j) const
Returns curl at cell-center location.
2-D read-only array accessor class.
Definition: ArrayAccessor2.h:261
ConstScalarDataAccessor GetVConstAccessor() const
Returns read-only v data accessor.
static Builder GetBuilder()
Returns builder fox FaceCenteredGrid2.
void ParallelForEachUIndex(const std::function< void(size_t, size_t)> &func) const
Invokes the given function func for each u-data point in parallel.
Builder & WithInitialValue(const Vector2D &initialVal)
Returns builder with initial value.
Abstract base class for 2-D vector grid builder.
Definition: VectorGrid2.h:93
Abstract base class for 2-D cartesian grid structure.
Definition: Grid2.h:30
Abstract base class for 2-D vector grid structure.
Definition: VectorGrid2.h:19
void SetData(const std::vector< double > &data) override
Sets the data from a continuous linear array.
DataPositionFunc GetVPosition() const
Returns function object that maps v data point to its actual position.
double Curl(const Vector2D &x) const override
Returns curl at given position x.
void Swap(Grid2 *other) override
Swaps the contents with the given other grid.
std::function< Vector2D(const Vector2D &)> Sampler() const override
Returns the sampler function.
FaceCenteredGrid2 & operator=(const FaceCenteredGrid2 &other)
Sets the contents with the given other grid.
std::shared_ptr< VectorGrid2 > Clone() const override
Returns the copy of the grid instance.
Vector2D GetVOrigin() const
Returns v-data position for the grid point at (0, 0).
void OnResize(const Size2 &resolution, const Vector2D &gridSpacing, const Vector2D &origin, const Vector2D &initialValue) final
Invoked when the resizing happens.
2-D point class.
Definition: Point2.h:25
double & GetU(size_t i, size_t j)
Returns u-value at given data point.
void ParallelForEachVIndex(const std::function< void(size_t, size_t)> &func) const
Invokes the given function func for each v-data point in parallel.
2-D array accessor class.
Definition: ArrayAccessor2.h:31
Size2 GetUSize() const
Returns data size of the u component.
2-D face-centered (a.k.a MAC or staggered) grid.
Definition: FaceCenteredGrid2.h:25
FaceCenteredGrid2Ptr MakeShared() const
Builds shared pointer of FaceCenteredGrid2 instance.
Vector2D Sample(const Vector2D &x) const override
Returns sampled value at given position x.
Vector2D ValueAtCellCenter(size_t i, size_t j) const
Returns interpolated value at cell center.
Builder & WithOrigin(const Vector2D &gridOrigin)
Returns builder with grid origin.
Size2 GetVSize() const
Returns data size of the v component.
double & GetV(size_t i, size_t j)
Returns v-value at given data point.
Definition: pybind11Utils.h:24
FaceCenteredGrid2()
Constructs empty grid.
std::shared_ptr< VectorGrid2 > VectorGrid2Ptr
Shared pointer for the VectorGrid2 type.
Definition: VectorGrid2.h:90
Vector2< double > Vector2D
Double-type 2D vector.
Definition: Vector2.h:341
void ForEachVIndex(const std::function< void(size_t, size_t)> &func) const
Invokes the given function func for each v-data point.
void Fill(const Vector2D &value, ExecutionPolicy policy=ExecutionPolicy::Parallel) override
Fills the grid with given value.
DataPositionFunc GetUPosition() const
Returns function object that maps u data point to its actual position.
double Divergence(const Vector2D &x) const override
Returns divergence at given position x.
ScalarDataAccessor GetUAccessor()
Returns u data accessor.
void ForEachUIndex(const std::function< void(size_t, size_t)> &func) const
Invokes the given function func for each u-data point.
Front-end to create FaceCenteredGrid2 objects step by step.
Definition: FaceCenteredGrid2.h:233
std::shared_ptr< FaceCenteredGrid2 > FaceCenteredGrid2Ptr
Shared pointer type for the FaceCenteredGrid2.
Definition: FaceCenteredGrid2.h:228
void GetData(std::vector< double > *data) const override
Fetches the data into a continuous linear array.
Vector2D GetUOrigin() const
Returns u-data position for the grid point at (0, 0).
#define CUBBYFLOW_GRID2_TYPE_NAME(DerivedClassName)
Definition: Grid2.h:110
double DivergenceAtCellCenter(size_t i, size_t j) const
Returns divergence at cell-center location.
2-D vector class.
Definition: Vector2.h:26
ScalarDataAccessor GetVAccessor()
Returns v data accessor.
2-D array class.
Definition: Array2.h:42
ExecutionPolicy
Execution policy tag.
Definition: Parallel.h:15
2-D linear array sampler class.
Definition: ArraySamplers2.h:78
ConstScalarDataAccessor GetUConstAccessor() const
Returns read-only u data accessor.
std::function< Vector2D(size_t, size_t)> DataPositionFunc
Function type for mapping data index to actual position.
Definition: Grid2.h:34
void Set(const FaceCenteredGrid2 &other)
Sets the contents with the given other grid.
FaceCenteredGrid2 Build() const
Builds FaceCenteredGrid2 instance.
Builder & WithGridSpacing(const Vector2D &gridSpacing)
Returns builder with grid spacing.