<p>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:</p>

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

<p>set_bnd could be called multiple times to set desired boundary values.</p>

CC BY-SA 3.0
2017-05-24T11:42:32.357Z
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
CC BY-SA 3.0
2017-05-24T20:14:15.433Z
@AnasAlaa if you have a new question you can post it as a question rather than a comment.
trichoplax is on Codidact now
CC BY-SA 3.0
2017-05-24T22:05:10.613Z
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
CC BY-SA 3.0
2017-05-25T01:49:01.350Z
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
CC BY-SA 3.0
2017-05-25T23:07:02.193Z
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
CC BY-SA 3.0
2017-05-26T01:50:29.490Z
Yes, I think so.
Fei Zhu
CC BY-SA 3.0
2017-05-26T19:18:48.873Z
I think the answer is not correct. The two options are either homogenous Neumann boundary conditions (if the value is copied) or homogenous dirichlet boundary condition as negating the value represents a zero value in between the interface. Furthermore I doubt that you can use this function for a cg solver. To which variables do you want apply it? For a cg solver I would recommend to check Robert bridsons book fluid simulation for computer graphics, although he uses a slightly modified (superior) discretization (staggered grid).
dweber
CC BY-SA 3.0
2017-05-27T03:05:56.473Z
I don't understand how the boundaries will be dirchlet and neuman at the same time. Bridson book is good but he has no code to implement what he said about boundaries and PCG... etc
Anas Alaa
CC BY-SA 3.0
2017-05-27T13:15:16.483Z
Boundary conditions are either dirichlet or Neumann, I don't know from which part of the code you see this. Neumann  conditions are realized by modifying the matrix. You might want to check a finite difference book for details. This function from stam emulates a modified matrix for a gauss seidel iteration.  I fear you have to do the transition from text / equations to code. You can't just copy and paste everything from different sources...
dweber
CC BY-SA 3.0
2017-05-27T23:55:11.180Z
I understand you, in the code, the boundaries are dirichlet or neuman in the code. But I doubt if I can just use the same boundaries for cg solver you can see the full code here
Anas Alaa
CC BY-SA 3.0
2017-05-28T02:42:20.940Z
https://www.autodeskresearch.com/sites/default/files/TheArtOfFluidAnimationCode.zip


Here is the code and you just use the set_bnd for div in the project function to whom I want just to replace the gauss seidel with PCG.
Anas Alaa
CC BY-SA 3.0
2017-05-30T17:47:36.870Z
Where are you? I need help
Anas Alaa
CC BY-SA 3.0
2017-05-24T09:08:53.453Z
2017-05-25T02:16:26.863Z
2017-05-25T02:16:26.863Z
Fei Zhu
<p>In the paper <a href="http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/ns.pdf" rel="nofollow noreferrer">Stable Fluids</a> 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? </p>

<p>Here is the <code>set_bnd</code> function; the full code can be found <a href="https://www.autodeskresearch.com/sites/default/files/TheArtOfFluidAnimationCode.zip" rel="nofollow noreferrer">here</a>.</p>

<pre><code>void set_bnd ( int N, int b, float * x )
{
    int i;

    for ( i=1 ; i&lt;=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)]);
}
</code></pre>

CC BY-SA 3.0
2017-05-23T14:35:16.307Z
Can you please add a link to the paper or book you're referring to, and a relevant excerpt from the code you're talking about?
Nathan Reed
CC BY-SA 3.0
2017-05-23T14:46:40.623Z
http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/ns.pdf this is it I thought the links are banned or something
Anas Alaa
CC BY-SA 3.0
2017-05-27T23:57:41.090Z
https://www.autodeskresearch.com/sites/default/files/TheArtOfFluidAnimationCode.zip Here is the code and you just use the set_bnd for div in the project function to whom I want just to replace the gauss seidel with PCG.
Anas Alaa
<c++><fluid-sim>
Jos Stam's "Stable Fluids" boundary conditions
Anas Alaa
# Jos Stam's "Stable Fluids" boundary conditions

(Asked by: username_0 on 2017-05-23)

In the paper [Stable Fluids](http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/ns.pdf) 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](https://www.autodeskresearch.com/sites/default/files/TheArtOfFluidAnimationCode.zip).

```
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)]);
}

```

**Tags:** c++, fluid-sim

> **Comments:**
> **username_1**: Can you please add a link to the paper or book you're referring to, and a relevant excerpt from the code you're talking about?  
> **username_0**: http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/ns.pdf this is it I thought the links are banned or something  
> **username_0**: https://www.autodeskresearch.com/sites/default/files/TheArtOfFluidAnimationCode.zip Here is the code and you just use the set_bnd for div in the project function to whom I want just to replace the gauss seidel with PCG.  

---

## 1 answers

### Answer by username_2 on 2017-05-24. Score: 3 | Accepted answer

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.

> **Comments:**
> **username_0**: 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!  
> **username_3**: @username_0 if you have a new question you can post it as a question rather than a comment.  
> **username_0**: I am sorry but it's really the same question but in another form. And it's a complete of his answer too.  
> **username_2**: 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.  
> **username_0**: So, if I just want to solve the pressure poisson equations with the PCG method, can I just use these boundaries in the code?  
> **username_2**: Yes, I think so.  
> **username_4**: I think the answer is not correct. The two options are either homogenous Neumann boundary conditions (if the value is copied) or homogenous dirichlet boundary condition as negating the value represents a zero value in between the interface. Furthermore I doubt that you can use this function for a cg solver. To which variables do you want apply it? For a cg solver I would recommend to check Robert bridsons book fluid simulation for computer graphics, although he uses a slightly modified (superior) discretization (staggered grid).  
> **username_0**: I don't understand how the boundaries will be dirchlet and neuman at the same time. Bridson book is good but he has no code to implement what he said about boundaries and PCG... etc  
> **username_4**: Boundary conditions are either dirichlet or Neumann, I don't know from which part of the code you see this. Neumann  conditions are realized by modifying the matrix. You might want to check a finite difference book for details. This function from stam emulates a modified matrix for a gauss seidel iteration.  I fear you have to do the transition from text / equations to code. You can't just copy and paste everything from different sources...  
> **username_0**: I understand you, in the code, the boundaries are dirichlet or neuman in the code. But I doubt if I can just use the same boundaries for cg solver you can see the full code here  
> **username_0**: https://www.autodeskresearch.com/sites/default/files/TheArtOfFluidAnimationCode.zip   Here is the code and you just use the set_bnd for div in the project function to whom I want just to replace the gauss seidel with PCG.  
> **username_0**: Where are you? I need help  

---

