C++ newbie qu: grid construction for finite difference

In summary, the C++ newbie is trying to create a grid to solve a finite difference problem. He has come up with a way to construct the grid, but he cannot figure out how to link it to a struct of fluid properties. He is looking for help from others.
  • #1
bzz77
34
0
Hello everyone:

I am a C++ newbie; I am interesting in using C++ in my work on coupled fluid flow-chemical reaction problems. I apologize in advance for what is probably a very simple question. I would very much appreciate any help to get me on the right track!

My goal is to come up with C++ code for solving a finite difference problem. To do this, I need to (1) construct a 2-D grid and (2) link a struct of data (fluid properties, like density) to the grid. At some point, as I become more experienced, I'd like to learn how to use pointers and iterators, etc, but for now I would like to keep things as simple as possible.

Using some examples, I have come up with a way of constructing a grid that mostly makes sense. But I cannot figure out how to link the grid with a struct of fluid properties. Sorry--I'm certain this is a simple thing to do. If anyone has any suggestions, I would love to hear them. Once I have this problem sorted out, I have a couple of further questions about the grid construction. Thanks a lot.

Here is my grid:

template <typename T>
class Grid2D
{
public:
Grid() : xsize_(0), ysize_(0), grid_(0) { }
Grid(double xsize, double ysize) : xsize_(xsize), ysize_(ysize), grid_(new T[ xsize_ * ysize_ ]) { }

~Grid() { delete[ ] grid_; }
Create(double xsize, double ysize)
{
xsize_ = xsize;
ysize_ = ysize;
grid_ = new T[ xsize_ * ysize_ ];
}
Resize(double xsize, double ysize)
{
xsize_ = xsize;
ysize_ = ysize;
delete[ ] grid_;
grid_ = new T[ xsize_ * ysize_ ];
}
Delete()
{
xsize_ = 0;
ysize_ = 0;
delete[ ] grid_;
grid_ = 0;
}
double xsize() const { return xsize_; }
double ysize() const { return ysize_; }
T& operator(double x, double y) { return grid_[ y*xsize_ + x ]; }
const T& operator(double x, double y) const { return grid_[ y*xsize_ + x ]; }
private:
double xsize_, ysize_;
T* grid_;
}
 
Technology news on Phys.org
  • #2
To link the grid with a struct of fluid properties, you could create a class that contains both the Grid2D and the struct. For example: class GridFluid{public:GridFluid() : grid_(0), fluidProperties_(0) {}GridFluid(double xsize, double ysize) : grid_(xsize, ysize), fluidProperties_() {}~GridFluid() { delete[ ] grid_; } Grid& grid() { return grid_; } const Grid& grid() const { return grid_; } FluidProperties& fluidProperties() { return fluidProperties_; } const FluidProperties& fluidProperties() const { return fluidProperties_; } private: Grid grid_;FluidProperties fluidProperties_;};Now when you need to access the fluid properties, you can simply call the GridFluid object and use the .fluidProperties() method. Hope this helps!
 
  • #3


Hello,

First of all, welcome to the world of C++! It's great to see someone interested in using this powerful language in their scientific work.

To answer your question, you can link a struct of fluid properties to your grid by creating a new struct that contains both the grid and the fluid properties. For example, you could create a struct called "FluidGrid" that has two members - a Grid2D object and a struct containing your fluid properties. This way, you can access both the grid and the fluid properties through one object.

As for your further questions about grid construction, I would suggest looking into using multi-dimensional arrays instead of a single-dimensional array for your grid. This can make it easier to access and manipulate individual cells within the grid.

Also, as you mentioned, learning about pointers and iterators will definitely help you in writing more efficient and flexible code. I would recommend practicing with some simple examples to get more comfortable with them.

I hope this helps and good luck with your work! Don't hesitate to reach out for more help if needed.
 

Related to C++ newbie qu: grid construction for finite difference

1. What is grid construction in the context of finite difference in C++?

Grid construction in finite difference involves creating a discrete grid of points to represent a continuous domain in order to solve differential equations numerically. This grid is used to approximate the derivatives needed for the finite difference calculations.

2. How do I create a grid in C++ for finite difference?

To create a grid for finite difference in C++, you need to define the number of points and the spacing between them. Then, you can use loops to assign values to each point on the grid. Alternatively, you can use C++ libraries such as Eigen or Armadillo to create and manipulate grids.

3. Can I use a non-uniform grid for finite difference in C++?

Yes, you can use non-uniform grids for finite difference in C++. This can be useful for solving problems with varying levels of accuracy in different regions of the domain. However, it may require more complex algorithms to handle the non-uniform spacing.

4. How do I handle boundary conditions in grid construction for finite difference in C++?

Boundary conditions can be handled by either explicitly assigning values to the boundary points or by using ghost cells, which are extra points outside the boundary to enforce the boundary conditions. The choice will depend on the specific problem being solved.

5. Are there any C++ libraries specifically for grid construction in finite difference?

Yes, there are several C++ libraries that offer functionality for grid construction in finite difference. Some examples include FEniCS, deal.II, and OpenFOAM. These libraries also offer other useful features for numerical simulations and modeling.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
1
Views
666
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
1
Views
980
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
Back
Top