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)