# Zero-flux boundary condition

**URL:** https://community.freefem.org/t/zero-flux-boundary-condition/2699
**Category:** General Discussion
**Created:** [September 11, 2023, 12:50pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699 "2023-09-11T12:50:52Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [September 11, 2023, 12:50pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/1 "2023-09-11T12:50:52Z")

</div>

Hi all, I have a question regarding a zero flux boundary condition in my model. First, I’ll input the model equations and the relevant boundary conditions, as well as the domain:  
 ![Domain](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/c/cbea76952cd084fe007d0dd41b946115fb822c53.png)

Equations:

\frac{\partial c\_1}{\partial t}+\nabla\cdot\boldsymbol{N}\_1=0

\frac{\partial c\_2}{\partial t}+\nabla\cdot\boldsymbol{N}\_2=0

\nabla\cdot\boldsymbol{j}\_e=0

\boldsymbol{j}\_e=F\left(2\boldsymbol{N}\_1+\boldsymbol{N}\_2-\boldsymbol{N}\_3\right)

\boldsymbol{N}\_1=-D\_1\nabla c\_1-\frac{2D\_1Fc\_1}{RT}\nabla\phi\_e+\frac{6V\_0c\_1}{W^2}x(W-x)\hat{\boldsymbol{e}}\_2

\boldsymbol{N}\_2=-D\_2\nabla c\_2-\frac{D\_2Fc\_2}{RT}\nabla\phi\_e+\frac{6V\_0c\_2}{W^2}x(W-x)\hat{\boldsymbol{e}}\_2

\boldsymbol{N}\_3=-D\_3\nabla (2c\_1+c\_2)+\frac{D\_3F(2c\_1+c\_2)}{RT}\nabla\phi\_e+\frac{6V\_0(2c\_1+c\_2)}{W^2}x(W-x)\hat{\boldsymbol{e}}\_2

Boundary conditions relevant to question:

\boldsymbol{j}\_e|\_{\partial e^-}\cdot\hat{\boldsymbol{e}}\_1=-j\_{Pb}

\boldsymbol{N}\_1|\_{\partial e^-}\cdot\hat{\boldsymbol{e}}\_1=-\frac{j\_{Pb}}{2F}

\boldsymbol{N}\_2|\_{\partial e^-}\cdot\hat{\boldsymbol{e}}\_1=0

As you can see, I have Robin boundary conditions at \partial e^-, which I enter into the weak form after applying the divergence theorem. In particular, I have a zero-flux boundary condition. However, when I evaluate \boldsymbol{N}\_2|\_{\partial e^-}\cdot\hat{\boldsymbol{e}}\_1 along boundary \partial e^-, I do not obtain exactly zero; instead the value of flux oscillates between very small (~1e-9) positive and negative values, which increase in size at later time steps (although they remain very small). Why might this be happening? Is it simply due to numerical error? If so, how can I improve my code such that the zero-flux boundary condition is better obeyed? Would a more refined mesh help? Please let me know if you require more information to answer my question. Many thanks.

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [September 11, 2023, 1:04pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/2 "2023-09-11T13:04:53Z")

</div>

Can you upload ( not paste into message ) your code or a minimal  
working example? I can’t be sure of getting to it but am looking at  
drift-diffusion and electrochemistry somewhat.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [September 11, 2023, 2:31pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/3 "2023-09-11T14:31:32Z")

</div>

Hi Mike, I understand if you’re busy. If you have the time, here’s my code:  
[forForum2.edp](https://community.freefem.org/uploads/short-url/3nCbfzUIW0BZGIIoAPLIQW7Lsei.edp) (21.6 KB)

Note, I have implemented a Newton method to deal with the non-linearity; I put the equations into weak form and then expanded about an approximate solution. So, the code solves for a correction which is then appended to the approximation. Note also that I’ve implemented a varying time step (the solution evolves more rapidly towards the start and so initially requires a very small time step, which is then increased at later times). I started using freefem a few months ago, so my code could no doubt be improved; one thing I’d like to try and do is use the ‘varf’ keyword rather than ‘problem’ as that would allow me to try out some different solvers.

I can upload the rest of the boundary conditions if it would help, just say if you’d like me to.

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [September 11, 2023, 5:01pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/4 "2023-09-11T17:01:38Z")

</div>

I was looking at some etching and used IIRC SNESsolve for the initial  
solution and had some remaining flux but don’t remember where I left it.  
I could upload that but I use several idp files I made and would have to  
look for the code at this point.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [September 11, 2023, 5:45pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/5 "2023-09-11T17:45:13Z")

</div>

Okay, only look for it if you’ve got time. At the very least, any gut instinct ideas that you might have about why the flux isn’t completely zero would be very useful. I can only imagine that it’s simply a result of some kind of numerical error. I should mention that the aspect ratio of my adapted mesh is poor (i.e. close to the boundaries, the triangles are extremely long and thin, which is something that I’m going to ask about correcting in a separate post), which could also have something to do with it. What do you think?

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [September 12, 2023, 2:10pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/6 "2023-09-12T14:10:04Z")

</div>

It is due to numerical error. Your Neumann BC can only ever be as accurate as your discrete approximation of the derivatives. You can use a finer mesh or higher order elements if this is a problem, but I don’t expect errors of the order of 1e-9 to be an issue…

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [September 12, 2023, 2:29pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/7 "2023-09-12T14:29:32Z")

</div>

Is that an absolute number or relative to something? In actual fact their  
will be a flux of reactants getting to the surface and typically  
engaging in something like A+ + e- → A which then creates a concentation  
gradient and net flux away from the boundary.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [September 12, 2023, 4:18pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/8 "2023-09-12T16:18:14Z")

</div>

Hi Mike. Species 2 in the above model is not involved in any reactions at boundary \partial e^-. The flux is absolute and should be equal to zero. But from Chris’ answer, it seems to be due to numerical error, which makes sense.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [September 12, 2023, 4:20pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/9 "2023-09-12T16:20:30Z")

</div>

Hi Chris, this makes a lot of sense. I’ll investigate the effect of increasing the mesh fineness. When you say higher order elements, are you referring to P1, P2 etc.? Apologies, I’m quite rusty with the theory behind FEM.

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [September 12, 2023, 4:36pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/10 "2023-09-12T16:36:01Z")

</div>

Yes, that’s exactly right. In general, you can reduce numerical error by h-refinement (finer mesh) or p-refinement (higher order functional approximation). Note that your estimate of how accurately the Neumann condition is satisfied is also dependent on the functional approximation you make via your discretization. An error of 1e-9 is likely just numerical noise, as your actual errors (the difference between the continuous solution and the discrete one) are probably much higher unless your discretization is extremely resolved.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [September 12, 2023, 5:22pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/11 "2023-09-12T17:22:27Z")

</div>

Okay, this is very useful. One final question: do you think the code’s failure to better meet the zero-flux BC is partly due to the poor aspect ratio of my adapted mesh (as seen in my other question that you answered today)?

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [September 12, 2023, 5:38pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/12 "2023-09-12T17:38:54Z")

</div>

It’s certainly possible, and that would agree with my intuition, but I can’t know for sure based only on this exchange.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [September 12, 2023, 7:45pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/13 "2023-09-12T19:45:58Z")

</div>

Okay, I’ll have a proper investigate tomorrow. Big thanks for the help.

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [September 12, 2023, 9:28pm UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/14 "2023-09-12T21:28:01Z")

</div>

I still haven’t looked at your code but in general there can be “high” fields  
at the interface and the balance between diffusion ( gradient of carriers)  
and drift( gradient of voltage) may be an issue of local mesh parameters.  
It may be interesting to see if the “noise” correlates with any particular  
local mesh features or if a more uniform mesh ( squares with diagonals)  
changes the noise distirbution.

But still there is not really much of an “absolute” nu,mber notion.  
1e-9 could be a nanoamp per something which may be important .  
Probably you do have two large components for a drift term  
and a diffusion term but important current amounts may be  
due to a “small difference” between these two. Does the noise  
effect anything you care about?

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [September 13, 2023, 11:10am UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/15 "2023-09-13T11:10:29Z")

</div>

Hi Mike, I agree, it would certainly be interesting to see if the noise changes with respect to the mesh structure. It is hard to tell if the noise is affecting the solution significantly; I really only have a small number of contour plots in the literature (and a few voltage-time plots) to go by. I’ll look at trying to find a way to reduce the noise, which will then allow me to see if it has any affect upon the solution.

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [September 13, 2023, 11:45am UTC](https://community.freefem.org/t/zero-flux-boundary-condition/2699/16 "2023-09-13T11:45:46Z")

</div>

I just plot drift and diffusion separately. I guess also you can do this just in self consistent  
iterations between charge density and voltage and then think about the issues  
with consistency between the Lagrangian elements and the exponential dependence  
on voltage - this is why I’m looking at other interpolation schemes. If you have a linear  
potential and are near equ you may be better off interpolating the carrier density  
with an exponential. I pushed that too while trying to get datascope out and  
btw I would not discount the utility of 1D models for a starting point.
