I want to solve two diffusion equations time dependently. c1 is solved for both subdomains, and c2 is solved for one subdomain. My problem is how to define initial condition for c2 just for its subdomain, and solve c2 for that subdomain?
this is the code I tried to do it:
from fenics import *
mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, 'P', 1)
subdomains = MeshFunction("size_t", mesh, mesh.topology().dim())
subdomain_1 = CompiledSubDomain("x[0] < 0.5")
subdomain_2 = CompiledSubDomain("x[0] >= 0.5")
subdomains.set_all(0)
subdomain_1.mark(subdomains, 1)
subdomain_2.mark(subdomains, 2)
c1_initial = Expression("1", degree=1)
c2_initial = Expression("0", degree=1)
c1 = interpolate(c1_initial, V)
c2 = interpolate(c2_initial, V)
D1 = Constant(1.0)
D2 = Constant(0.5)
c1_trial = TrialFunction(V)
c1_test = TestFunction(V)
c2_trial = TrialFunction(V)
c2_test = TestFunction(V)
c1_diffusion = D1 * dot(grad(c1_trial), grad(c1_test)) * dx
c1_rhs = Constant(0) * c1_test * dx + c2_trial * c1_test * dx(2)
c1_problem = LinearVariationalProblem(c1_diffusion, c1_rhs, c1)
c2_diffusion = D2 * dot(grad(c2_trial), grad(c2_test)) * dx(2)
c2_rhs = Constant(0) * c2_test * dx(2)
c2_problem = LinearVariationalProblem(c2_diffusion, c2_rhs, c2)
t = 0.0
dt = 0.01
num_steps = 100
c1_solver = LinearVariationalSolver(c1_problem)
c2_solver = LinearVariationalSolver(c2_problem)
for step in range(num_steps):
subdomain_1.mark(subdomains, 1)
c1_solver.bcs = [DirichletBC(V, c1_initial, subdomains, 1)]
c1_solver.solve()
subdomain_2.mark(subdomains, 2)
c2_solver.bcs = [DirichletBC(V, c2_initial, subdomains, 2)]
c2_solver.solve()
t += dt
This is the error:
Blockquote
raise ArityMismatch(“Adding expressions with non-matching form arguments {0} vs {1}.”.format(_afmt(a), _afmt(b)))
ArityMismatch: Adding expressions with non-matching form arguments (‘v_0’, ‘v_1’) vs (‘v_0’,).