# Define the inflow and outflow on interior boundaries

**URL:** <https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145>\
**Category:** General Discussion\
**Created:** [December 5, 2025, 9:31am UTC](https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145 "2025-12-05T09:31:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LXX](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@LXX](https://community.freefem.org/u/LXX)\
**Post date:** [December 5, 2025, 9:31am UTC](https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145/1 "2025-12-05T09:31:21Z")

</div>

Hello everyone, I’m trying to implement an upwind DG scheme, but this requires defining the inflow and outflow on the interior boundaries, and I don’t know how to represent them. This has been bothering me for a long time.

Define `mesh Th = square(128, 128, [2*x-1, 2*y-1]);` Assume that the velocity \mathbf{u}\_{h}^n has already been obtained. `uh=[u1old,u2old]` with `fespace Vh(Th, [P2,P2]);` We have \sigma\_{h}^{n+1},w\_{h}\in W\_{h} with `fespace Wh(Th, P2dc);`

Now I want to reproduce an upwind DG scheme in W\_{h}.  
\left( \frac{\sigma\_{h}^{n+1}-\sigma\_{h}^n}{\delta t},w\_{h} \right) + \left( \sum\_{e}\left(\hat{\sigma}\_{h}^{n+1}\mathbf{u}\_{h}^n,[[w\_{h}]]\_{e}\right) - (\sigma\_{h}^{n+1}\mathbf{u}\_{h}^n,\nabla w\_{h})\right) = 0.

For any function w continuous on each K\in \sum, we define  
[[w]] = (w^{-} - w^{+})\cdot \mathbf{n}\_e  
to be the jump of the function w on an edge e=\partial K^{-}\cap \partial K^{+}, with w^{\pm} denoting the trace of w from K^{\pm} onto e, and \mathbf{n}\_{e} denoting the normal vector on e pointing towards K^{+}.

The \hat{\sigma}\_{h}^{n+1} is the numerical flux, which is a single-valued function defined at the cell interfaces and in general depending on the values of the numerical solution \sigma\_{h}^{n+1} from both sides of the interface. Here, the numerical flux on an edge e can be chosen according to the upwind principle, namely,

\hat{\sigma}\_{h}^{n+1} = (\sigma\_{h}^{n+1})^{-}\quad \text{on }e\_{+}^n,\qquad \hat{\sigma}\_{h}^{n+1} = (\sigma\_{h}^{n+1})^{+}\quad \text{on }e\_{-}^n

where e\_{-}^n and e\_{+}^n denote the numerical inflow and outflow parts of an edge e at time t=t\_{n}, defined by  
e\_{-}^n = \{\mathbf{x}\in e:(\mathbf{u}\_{h}^n\cdot \mathbf{n}\_{e})(\mathbf{x})\leq 0\},\quad\quad e\_{+}^n = \{\mathbf{x}\in e:(\mathbf{u}\_{h}^n\cdot \mathbf{n}\_{e})(\mathbf{x})\> 0\}.

I want to know how to implement the crucial feature in FreeFem++.  
\sum\_{e}\left(\hat{\sigma}\_{h}^{n+1}\mathbf{u}\_{h}^n,[[w\_{h}]]\_{e}\right)

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [December 6, 2025, 3:16pm UTC](https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145/2 "2025-12-06T15:16:42Z")

</div>

It seems to me that this sum can be written (at least for the interior edges) as  
\displaystyle\sum\_T\sum\_{e \subset T} \int\_e\sigma\_h^{n+1} \max(\mathbf{u}\_h^n\cdot \mathbf{n}\_e,0)(-1)[w\_h]\_e  
where [w]\_e=w^+ - w^- and “-” means the side of T, “+” the other side, \mathbf{n}\_e goes from “-” to “+” ie points outside T, and \sigma\_h^{n+1} is evaluated from the side of T.  
This can be coded as

```auto
load "Element_PkEdge"
verbosity=0;

border bb(t=0.,1.){x=cos(t*2.*pi);y=sin(t*2.*pi);}
mesh Th=buildmesh(bb(20));

fespace Vh(Th,[P2,P2]);
Vh [u1old,u2old];
[u1old,u2old]=[-y,x];

fespace Wh(Th,P2dc);
Wh sig,wh;

fespace Wedgeh(Th,P2edge);// functions P2 on each edge (continuous through edge)
//this space could be useful

varf inout(sig,wh)=
  intalledges(Th)(sig*max(u1old*N.x+u2old*N.y,0.)*(-1.)*jump(wh))
  ;

```

---

<div class="post-metadata">

**Author:** ![LXX](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@LXX](https://community.freefem.org/u/LXX)\
**Post date:** [December 10, 2025, 8:55am UTC](https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145/4 "2025-12-10T08:55:48Z")

</div>

Thank you very much, Professor, I understand now. Also, what if I choose the boundary condition  
\sigma(\mathbf{x},t)|\_{\Gamma\_{\mathbf{u}}}=g(\mathbf{x},t) with \Gamma\_{\mathbf{u}}=\{\mathbf{x}\in\partial \Omega: \mathbf{u}(\mathbf{x})\cdot \mathbf{n}\<0\}

```auto
macro h(t)( ...) //

Th = change(Th, flabel = ( (label < 5) && (u1old*N.x + u2old*N.y < 0) ) ? 10 : label);

solve inout(sig,wh)=
  int2d(Th)(sig*wh/dt - (sig * u1old * dx(wh) + sig * u2old * dy(wh)) )
- int2d(Th)(sigold*wh/dt)
+ intalledges(Th)(sig*max(u1old*N.x+u2old*N.y,0.)*(-1.)*jump(wh))
- int1d(Th,10)( h(t) * (u1old*N.x+u2old*N.y) * (-1) * wh );

```

Is this correct, Professor?

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [December 10, 2025, 9:31am UTC](https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145/5 "2025-12-10T09:31:39Z")

</div>

I would put  
`+int1d(Th)( h(t) * max(-(u1old*N.x+u2old*N.y),0.) * (-1) * wh )`  
in order to represent an edge integral which is the boundary of a “ghost triangle” outside of the domain, but I do not guarantee that this is correct…  
(and do not use the line `Th=change()`)

---

<div class="post-metadata">

**Author:** ![LXX](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@LXX](https://community.freefem.org/u/LXX)\
**Post date:** [December 10, 2025, 10:40am UTC](https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145/6 "2025-12-10T10:40:14Z")

</div>

I understand what you mean, Professor. But why can’t I use `Th=change()`?

I’ve provided the exact solutions for \mathbf{u} and \sigma, so I think I should use \Gamma\_{\mathbf{u}}=\{\mathbf{x}\in\partial \Omega: \mathbf{u}(\mathbf{x})\cdot \mathbf{n}\<0\} with the exact solutions \mathbf{u} instead of `max(-(u1old*N.x+u2old*N.y),0.)`.

Another point, `intalledges(Th)(sig*max(u1old*N.x+u2old*N.y,0.)*(-1.)*jump(wh))` is it unnecessary to distinguish between internal edges and boundary edges in this command?

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [December 10, 2025, 11:51am UTC](https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145/7 "2025-12-10T11:51:29Z")

</div>

You can use the change() and/or the \Gamma\_u, but I think it is more in the spirit of the method to write the boundary term similarly as the interior terms.  
I think that the intalledges has to include both internal and boundary edges.

---

<div class="post-metadata">

**Author:** ![LXX](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@LXX](https://community.freefem.org/u/LXX)\
**Post date:** [December 11, 2025, 1:19pm UTC](https://community.freefem.org/t/define-the-inflow-and-outflow-on-interior-boundaries/4145/8 "2025-12-11T13:19:10Z")

</div>

Thank you, Professor. I think I understand how FreeFem++ handles the DG scheme.
