# Jos Stam's “Stable Fluids” boundary conditions

In the paper Stable Fluids by Jos Stam, I am confused about the boundaries in the set_bnd function in the code. I don't know what kind of boundaries is this. Is it Neumann boundary condition or Dirichlet boundaries?

Here is the set_bnd function; the full code can be found here.

void set_bnd ( int N, int b, float * x )
{
int i;

for ( i=1 ; i<=N ; i++ ) {
x[IX(0  ,i)] = b==1 ? -x[IX(1,i)] : x[IX(1,i)];
x[IX(N+1,i)] = b==1 ? -x[IX(N,i)] : x[IX(N,i)];
x[IX(i,0  )] = b==2 ? -x[IX(i,1)] : x[IX(i,1)];
x[IX(i,N+1)] = b==2 ? -x[IX(i,N)] : x[IX(i,N)];
}
x[IX(0  ,0  )] = 0.5f*(x[IX(1,0  )]+x[IX(0  ,1)]);
x[IX(0  ,N+1)] = 0.5f*(x[IX(1,N+1)]+x[IX(0  ,N)]);
x[IX(N+1,0  )] = 0.5f*(x[IX(N,0  )]+x[IX(N+1,1)]);
x[IX(N+1,N+1)] = 0.5f*(x[IX(N,N+1)]+x[IX(N+1,N)]);
}

It's Dirichlet boundary condition. The quantities at domain boundaries will take either the same value as its inner neighbor or the negated value, depending on the condition $\mathbf{b}$. Take velocity field for instance, velocity of fluid will either gets reflected or not change at domain boundary. The condition $\mathbf{b}$ is a user option, $\mathbf{b}==1$ is used to set reflected values at the horizonal boundaries, and $\mathbf{b}==2$ is to set reflected values at vertical boundaries. To be more clear:

• $\mathbf{b}==1$: field values at horizonal boundaries get reflected, field values at vertical boundaries do not change.
• $\mathbf{b}==2$: field values at vertical boundaries get reflected, field values at horizonal boundaries do not change.
• $\mathbf{b}== other\ value$: field values at neither vertical nor horizonal boundaries do not change.

set_bnd could be called multiple times to set desired boundary values.

• What about if b is not 1 and 2? Another thing, where is the Neuman boundaries? Those are slip or no-slip? Is it a solid boundaries? If i use the PCG, will something change in the boundaries? Sorry for all of these stupid questions but I want to understand. Thanks cheers! – Anas Alaa May 24 '17 at 11:42
• @AnasAlaa if you have a new question you can post it as a question rather than a comment. – trichoplax May 24 '17 at 20:14
• I am sorry but it's really the same question but in another form. And it's a complete of his answer too. – Anas Alaa May 24 '17 at 22:05
• There's a little mistake in my previous answer, and I've editted the answer. About Neuman boundaries, this demo code didn't implement it. – Fei Zhu May 25 '17 at 1:49
• So, if I just want to solve the pressure poisson equations with the PCG method, can I just use these boundaries in the code? – Anas Alaa May 25 '17 at 23:07