I'm working on a coupled 'kinetics' kind of problem which appears fairly simple, but I'm having trouble implementing it into FEniCS. I can also imagine stability issues but I'm not at that point yet. I could go into what's what, but since all parameters are dimensionless it does not seem relevant. Do note that $t$ is time and $x$ is a spatial position.
The mathematical formulation of the problem is as follows:
$$ \frac{\partial i(x,t)}{\partial x} = - i(x,t) c(x,t) $$
$$ \frac{\partial c(x,t)}{\partial t} = -i(x,t) c(x,t) $$
s.t. $c(z,0) =1$, $\frac{\partial c}{\partial x} = 0$ at $x=0$ and $x=L$, with $L$ e.g. 15, and $i(0,t) = 1$.
The implementation should IMO be something like below, but I'm experiencing problems with the variational formulation (for starters):
from dolfin import *
T = 5.0 # final time
num_steps = 500 # number of time steps
dt = T / num_steps # time step size
# Create mesh
mesh = RectangleMesh(Point(0.0, 0.0), Point(15.0, 5.0), 30, 10)
# Define function space
P1 = FiniteElement('P', triangle, 1)
element = MixedElement([P1, P1])
V = FunctionSpace(mesh, element)
# Define test functions
v_1, v_2= TestFunctions(V)
# Define functions
u = Function(V)
u_n = Function(V)
# Split system functions to access components
u_1, u_2 = split(u)
u_n1, u_n2 = split(u_n)
# Define Dirichlet boundary (x = 0)
def boundary(x):
return x[0] < DOLFIN_EPS
# Define boundary condition
u_01 = Constant(1.0)
bc = DirichletBC(V.sub(0), u_01, boundary)
# Define expressions used in variational forms
k = Constant(dt)
# Define variational problem
F = div(u_1)*v_1*dx + u_1*u_2*v_1*dx \
+ ((u_2 - u_n2) / k) * u_1*u_2*v_2*dx
# Time-stepping
t = 0
for n in range(num_steps):
# Update current time
t += dt
# Solve variational problem for time step
solve(F == 0, u, bc)
# Update previous solution
u_n.assign(u)
To narrow down the initial problem, how would the function
$$ \frac{\partial i(x,t)}{\partial x} = - i(x,t) c(x,t) $$
be written in 'FEniCS python? Considering that
div(u_1)*v_1*dx + u_1*u_2*v_1*dx
gives errors w.r.t. shape differences.