# Zero-flux boundary condition

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: 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.

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.

Hi Mike, I understand if you’re busy. If you have the time, here’s my code:
forForum2.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.

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.

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?

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…

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.

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.

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.

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.

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)?

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

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

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