Double Array with 2 row and 2 column varibles

Pages: 12
Dear Friends
I want to make a double array in which I want two variables for row (say I and i) and two variables for column (say J and j) changing one after the other.
Please recommend me the type of loop which I should use?





This is valid syntax.
for ( a = 0, b = 0 ; a < 10 ; a++, b++ )
Can you please write the full loop?
I could, but it would be more instructive for you to attempt it first.
Firstly why do you want this? Can you tell us what you're trying to do?

Not entirely sure what you want but if you wanted to 'have two iterating variables each for row and column which iterate alternatively'

then this is the idea:

1
2
3
4
5
for ( int i = 0, I = 0; i < sizeof(array)/sizeof(array[0]) && 
/* or array.size() if STL container*/I < sizeof(array)/sizeof(array[0]); i%2==0? i++ : I++ ) 
   for ( int j = 0, J = 0; j < sizeof(array[0])/sizeof(array[0][0]) && 
   /* or array.size() if STL container*/J < sizeof(array[0])/sizeof(array[0][0]); j%2==0? j++ : J++ )
      cout << i << ' ' << I << ' ' << j << ' ' << J;


Not very elegant so if you can tell us why you want this then we can find you a better way.
@Grime
Can I send you some picture for the problem I am trying to code/?
Sure paste the problem and tell us your approach.
How can I paste or attach a picture on the forum? I am trying but not happening.
No not the picture. Copy and paste the text I meant. If it's on your book then type it out.
By the way you can use image hosting sites like imgur.com if you really had to share an image.

But we won't solve the problem for you. I want to know your approach because I think it's wrong or at least is really weird.
The picture is available at the following link.
https://imgur.com/sZuCfJp
As you see it has two kinds of rows J and j, further it has two kinds of column I and i. I am trying to write code for this problem.
It's a staggered mesh for CFD, @usmannazir. It looks like it comes from Versteeg and Malalasekera's book. What exactly are you planning to do with it and what information are you storing in arrays?

Note that you usually give the velocity components the same index as the scalar nodes to which they point - not make a distinction between i and I, or j and J.

This is manifestly not a novice problem in any computing language.
Last edited on
I am trying to write cpp code for lid driven cavity, yes you are right the book is from Versteeg and Malalasekera. Can you help in writing source term for u_star velocity?
If it's 2-d laminar flow, with a cartesian grid, then your source term for a momentum equation will just come from the net pressure force. So, integrated over a volume:
(pL-pR).dy
where pL and pR are the pressure nodes half a cell left and half a cell right (i.e the faces of the u-velocity cell, marked W and P in your picture) and dy is the grid spacing in the y direction.

In code this would probably look something like
bu[i][j] = ( p[i-1][j] - p[i][j] ) * dy;
where bu[][] is the explicit part of the source term for u-momentum, and I've assumed the indexing convention given in my previous post.

Similarly in the v-momentum equation you would have
bv[i][j] = ( p[i][j-1] - p[i][j] ) * dx;

It should be given in V and M's book (my copy of which is currently at work, which is not where I am presently.)
Last edited on
But in Appendix C of the same book, the term SuV is elaborated in detailed, where it has terms other than pressure.
And those terms are what?
https://imgur.com/a/LKEg2iW
They are shown here
Last edited on
If it's laminar flow then those viscous v terms aren't necessary in the u equation. In conjunction with part of the u terms they should sum overall to zero, since du/dx+dv/dy=0 for an incompressible flow.

The viscous u-terms are diffusive (gradient) terms. They will ultimately form your matrix coefficients (probably denoted aw, ae, as, an) NOT the source term.

You need to read further through the book to see how each term is discretised.
Last edited on
@usmannazir,
OK. I've finally had a look at Versteeg and Malalasekara's book. I have no idea why they dressed up the source term like in Appendix C - nor where in the book this Appendix is referenced from. You would only need to put viscous or other stress terms in the source if you had a variable viscosity (for example, if you were using an eddy-viscosity turbulence model); I don't think that is what you are doing.

If you look in Chapter 6.3 or the worked examples in Chapter 6.10 (I'm looking at the second edition) and they only have pressure in the source term. There may also be additional "deferred correction" terms from advection if you use anything other than first-order upwinding to discretise the advective fluxes.

For what it's worth, the following is my take on the matter:
https://imgur.com/EMHb4c1
@Lastchance
Thanks for the details. How should I handle the gradient of pressure =0 at the top moving lid of the cavity.
Should I add equation for the moving lid in p_prime calculations?
Last edited on
Presumably your nearest pressure node is half a cell inside the domain. If you have a zero-gradient boundary condition then take the boundary value of pressure equal to that.

There is no flow through the moving lid - it is shearing along the boundary of a cell. The normal velocity component is simply zero on the boundary. Whether or not the lid is moving shouldn't affect the mass-conservation (p-prime) equation: it will only provide a viscous stress in the momentum equations.
Last edited on
Pages: 12