Hi,
I am trying to solve time dependent coupled scheme and I want to sum the obtained solutions U_1, U_2 on each time step. However, I am getting error ‘Expected only linear combinations of Functions in the same FunctionSpaces’. And I searched the same error message post on the forum but not able to resolve the issue. So can anyone help me? The minimal code is given as:
from dolfin import *
import matplotlib.pyplot as plt
import numpy as np
set_log_level(LogLevel.ERROR)
T = 1.0 # final time
for j in range(1,4):
nx = 2**(j+1)
T = 1
num_steps = nx*nx
dt = T / (num_steps)
mesh = UnitSquareMesh(nx, nx)
P1 = FiniteElement('P', triangle, 1)
element = MixedElement([P1, P1])
V = FunctionSpace(mesh, element)
W = FunctionSpace(mesh,'CG', 1)
R = Function(W)
u_D = Expression('exp(-t)*sin(pi*x[0])*sin(pi*x[1])', degree = 3, t=0)
def boundary(x, on_boundary):
return on_boundary
bc1 = DirichletBC(V.sub(0), u_D, boundary)
bc2 = DirichletBC(V.sub(1), u_D, boundary)
bc = [bc1, bc2]
#U = Function(V)
U_1, U_2 = TrialFunctions(V)
u_g = Expression(('exp(-t)*sin(pi*x[0])*sin(pi*x[1])', '0'), degree = 3, t=0)
U_n = interpolate(u_g, V)
U_n1, U_n2 = split(U_n)
p, q = TestFunctions(V)
e_time = 0
for i in range(num_steps):
t = (i+1)*dt
u_g.t = t
a = 3/4*U_1*p*dx + 1/4*U_2*p*dx + dt/2*dot(grad(U_1), grad(p))*dx - 9/4*U_1*q*dx + 5/4*U_2*q*dx + dt/2*dot(grad(U_2), grad(q))*dx - dt/2 *(2*pi*pi-1)*U_1*p*dx - dt/2*(2*pi*pi-1)*U_2*q*dx
L = U_n1*p*dx - U_n2*q*dx
U = Function(V)
solve(a==L, U, bc)
U_1, U_2 = U.split(deepcopy=True)
R.assign(U_1+ U_2)
#errorin space using direct command
R_in = interpolate(R, W)
u_ex = interpolate(u_D, W)
error_L2 = errornorm(u_ex, R_in, 'L2')
error_H1 = errornorm(u_ex, R_in, 'H1')
print ('dt=%.4f nx=%.2f error_L2=%8.4E error_H1=%8.4E' % (dt, nx, error_L2, error_H1))
plot(R)
plt.show()
complete error message is R.assign(U_1+ U_2)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/function.py”, line 432, in assign
linear_comb = _check_and_contract_linear_comb(rhs, self, multi_index)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/function.py”, line 161, in _check_and_contract_linear_comb
_assign_error()
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/function.py”, line 34, in _assign_error
raise RuntimeError(“Expected only linear combinations of Functions in the same FunctionSpaces”)
RuntimeError: Expected only linear combinations of Functions in the same FunctionSpaces.