C/C++ C++ newbie qu: grid construction for finite difference

AI Thread Summary
The discussion focuses on using C++ for solving coupled fluid flow-chemical reaction problems, specifically through finite difference methods. The user seeks guidance on constructing a 2-D grid and linking it to a struct containing fluid properties, such as density. They present a basic implementation of a 2-D grid class, which includes methods for grid creation, resizing, and deletion. To connect the grid with fluid properties, a suggestion is made to create a new class, GridFluid, which encapsulates both the grid and the fluid properties struct. This approach allows for easy access to fluid properties through the GridFluid object. The user expresses a desire to keep the implementation simple while gradually learning more advanced C++ concepts like pointers and iterators.
bzz77
Messages
33
Reaction score
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
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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
3
Views
2K
Replies
11
Views
4K
Replies
23
Views
2K
Replies
5
Views
3K
Replies
17
Views
2K
Replies
23
Views
2K
Replies
31
Views
3K
Back
Top