2

I have a follow-up question for this thread Coupled non-linear equations in FyPi. I managed to set up the system and get the reasonable results when using the Neumann boundary conditions for all variables (i.e. keeping the constant concentrations of the electrons and holes and the potentials at the boundaries).

The system of equations

Now, I would like to compare it with the situation when the fluxes of electrons (n) and holes (p) are related by the relations:

Boundary conditions

where n0(0), p0(0) are the equilibrium concentrations at the left boundary, similar equations would be for the right-hand side bounary. s is the recombination velocity.

I'm aware of this description in the documentation (https://www.ctcms.nist.gov/fipy/documentation/USAGE.html#applying-fixed-flux-boundary-conditions) but don't know how to apply it.

Since the boundary conditions are considering the concentrations at the edges, should be the variables n, p, phi defined as FaceVariables? Could you please help me with defining these boundary conditions?

josefT
  • 21
  • 2

1 Answers1

1

Original Question

Let's consider just the equation for electrons. In FiPy it will look something like this,

eqn = TransientTerm(var=var_n) == G - R - DiffusionTerm(coeff_phi, var=phi) + DiffusionTerm(coeff_n, var=var_n) + boundary_condition

In order to solve this with the correct boundary condition we need to zero out the flux in and out of the domain in the diffusion terms on the left hand side and add in the flux for the boundary condition. coeff_phi and coeff_n are the diffusion coefficients that depend on the various parameters. coeff_phi also depends on n_var as well. To zero out the flux use,

coeff_phi.constrain(0., mesh.facesLeft)
coeff_n.constrain(0., mesh.facesLeft)

To construct the boundary condition use,

boundary_condition = (mesh.facesLeft * s * (n_var.faceValue - n0) / charge).divergence

I'm assuming n0 changes with time so that should be a Variable object which is updated in the time steps based on its relationship to time. I'm assuming s is some sort of value that's constant.


Defining the coefficents to be 0 (question from comment)

To clarify, there are two ways to maintain a zero coefficient on the boundary.

Using constraints

Given a coefficient defined as an operation on FaceVariables, then the following works as expected (keeping the left side to zero).

from fipy import Grid1D, FaceVariable

mesh = Grid1D(nx=10, dx=0.1)
var0 = FaceVariable(mesh=mesh, value=1 - mesh.x.faceValue)
var1 = var0 * var0
print('before constraint:', var1)
var1.constrain(0, where=mesh.facesLeft)
print('after constraint:', var1)
var0[0] = 10.0
print('after var0 reset:', var1)

Multiplying by ~mesh.facesLeft

This is an alternative approach to using constrains and might be easier to control and understand. Use this if you don't trust constraints to do the right thing. ~mesh.facesLeft is a boolean array so can be used like a constraint.

from fipy import Grid1D, FaceVariable

mesh = Grid1D(nx=10, dx=0.1)
var0 = FaceVariable(mesh=mesh, value=1 - mesh.x.faceValue)
var1 = var0 * var0 * ~mesh.facesLeft
print('using ~mesh.facesLeft:', var1)
var0[0] = 10.0
print('after var0 reset:', var1)
wd15
  • 1,068
  • 5
  • 8
  • Thank you very much for your response. I understand the `boundary_condition` term but I'm having problems with constraining the coefficients to zero. Also, I defined the equation a bit differently so it looks something like this: `eq1 = TransientTerm(var=var_n) == G - R - ConvectionTerm(coeff=mu_n*var_phi.faceGrad, var=var_n) + DiffusionTerm(coeff=D_n, var=self.n)` Both `mu_n` and `D_n` are constats. So I tried defining the coefficients for the covection and diffusion term as `Variable()` but was getting various errors (value, index or VectorCoeffError). – josefT Aug 15 '20 at 15:24
  • So, how can I define these coefficients, please? – josefT Aug 15 '20 at 15:27
  • I've amended the answer to hopefully address your comment query. Of `D_n` is just a float then you can construct a FaceVariable with `D_n * ~mesh.facesLeft` and is probably what you want. – wd15 Aug 17 '20 at 19:16