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 some 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) 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.

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_;
}

Dani AI

Generated

As described, the goal is to attach a small struct of fluid properties to each grid cell and iterate that 2‑D array for finite‑difference updates. The posted Grid2D has a few concrete issues that should be corrected: constructor name should match the class, sizes should be integer counts (not double), the call operator must be operator() (not operator with two parameters), and manual new[]/delete[] is brittle. Good catch from about using the C++ forum; the thread was later moved by .

A simple, safer design: keep storage contiguous for cache locality, use std::vector<T> so no manual new/delete or custom destructor is needed, use size_t for width/height, provide T& operator()(size_t x, size_t y) and a const overload, and document whether indexing is row‑major (index = y*width + x). Add optional bounds checks with assert or a separate at() accessor. Store the fluid properties by value in the container (not raw pointers) unless there is a compelling polymorphic reason.

Example (minimal) — grid type and usage:

struct Cell { double p; double conc; /* ... */ };

template<typename T>
class Grid2D {
public:
  Grid2D(size_t w=0, size_t h=0) : w_(w), h_(h), data_(w*h) {}
  void resize(size_t w, size_t h) { w_=w; h_=h; data_.assign(w*h, T()); }
  T& operator()(size_t x, size_t y)            { return data_[y*w_ + x]; }
  const T& operator()(size_t x, size_t y) const { return data_[y*w_ + x]; }
  size_t width() const { return w_; } size_t height() const { return h_; }
private:
  size_t w_, h_;
  std::vector<T> data_;
};

// usage:
Grid2D<Cell> G(100,100);
G(10,5).p = 101325.0;

Finite‑difference notes: add a halo (ghost) layer if implementing boundary conditions, iterate inner loop over x and outer over y to follow the y*w + x layout, and initialize cells explicitly when T has nontrivial state. This keeps the code simple, safe, and fast while leaving pointers/iterators for later optimization.

Recommended Answers

All 3 Replies

That is a very good question Anettest. But, you get more chances in getting it solved if you pose it in the C++ forum, instead of the C# forum.
Also: put code between code tags.

That is a very good question Anettest. But, you get more chances in getting it solved if you pose it in the C++ forum, instead of the C# forum.
Also: put code between code tags.

Sorry about that! Will do.

Moved & closed due to double thread. Discussion continued here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.