Storing data for Scientific Programs

Hi All,
Most scientific programs involve solving multiple equations to evolve fields (3D xyz matrices). This requires us to maintain multiple matrices of same size for different variables. A simple alternative C/C++ offers is to have structs/classes with all the variable and have an array of instances of those. Now we would be maintaining a matrix of these structs/class objects. Is this the best way to do it, or are there better alternatives.

For Example, For a simple program for fluid flow, we uniformly discretize the domain (represented by a matrix where every element points to correspoinding point on the domain). Variables at each point would be Temperature, Velocity, Pressure, Previous_Temperature, Previous_Velocity, Previous_Pressure. Now these 6 variable can be stored in an object array obj.

1
2
3
4
5
class All_Vars{
  public :
    var1,var2,var3,var4
}
All_Vars obj=new All_Vars[rows*columns];
Last edited on
it depends on what you need to do really. I did a lot of matrix math, and I kept each matrix separate as its own thing, which could then easily be used in computations and so on.

that would look like

vector<Matrix> (6); // store 6 variables in 1 vector, each variable is a matrix

do you just need raw storage? I would use vector instead of a pointer, for sure.
what you have will work just fine, if it solves the problem you have.

-- its common to use struct instead of class for this type of container, as classes have private members by default and usually use the getter-setter paradigm, while structs are public by default and simple structs sometimes avoid the getter-setter nonsense for simple use cases. This is just a common convention.

I would probably wrap this thing one more time. Final thing (as you did it) would look like

1
2
3
4
5
6
7
8
9
10
struct All_Vars
{ type  var1,var2,var3,var4;}; 

class Meta
{
    int rows, columns; //tie the rows and cols to the group for design reasons
   vector<All_Vars> All_Vars;
   //assignment operator? file read/write method? other stuff maybe? constructor?
   //getters, setters...  etc
};


if you don't want the suggested methods, you can just do struct meta and leave it as a pure data blob object. Those have merits, when they solve the problem at hand.
Last edited on
Topic archived. No new replies allowed.