I am new to FEniCS. I found a code in a paper I am attempting to update. I have worked on it for a while. I have spent time in the tutorials. I am running Python 3.7.3 with FEniCS installed through Conda.
The code is getting stuck on the lines beneath #initialize solution according to exact solution.
I would also welcome direction to specific resources to help update this code.
Thank you and I apologize for what I am sure is a very simple problem.
from dolfin import *
import numpy
from ufl import nabla_grad
from ufl import nabla_div
def gravity(u):
'''
right hand side of Eq. 7
'''
Ra = 1.0
val = as_vector([0.0, Ra*u])
return val
nx = 10
order = 1
t = 0
dt = 0.01
T = 0.1
mesh = UnitSquareMesh(nx, nx)
DGO = FiniteElement("DG", mesh.ufl_cell(), order)
BDM = FiniteElement("BDM", mesh.ufl_cell(), order+1)
DG1 = FiniteElement("DG", mesh.ufl_cell(), order+1)
W = FunctionSpace(mesh, MixedElement([DGO, BDM, DG1]))
#source terms
f_1 = Expression('(sin(x[0]) + cos(x[1]))*cos(t)', t=t, element = DGO)
f_2 = Expression(('0.0','(-sin(x[0]) - sin(x[1]))*cos(t)'), t=t, element = BDM)
f_3 = Expression('-(sin(x[0]) +sin(x[1]))*sin(t) + (-cos(x[0])*cos(x[0]) + sin(x[1])*cos(x[1]))*cos(t)*cos(t) + (sin(x[0])+sin(x[1]))*cos(t)', t=t, element = DG1)
# exact solutions
p_exact = Expression('(sin(x[0]) + cos(x[1]))*cos(t)', t=t, element = DGO) # pressure
uu_exact = Expression(('-cos(x[0])*cos(t)','sin(x[1])*cos(t)'), t=t, element = BDM) # velocity
u_exact = Expression('(sin(x[0]) + sin(x[1]))*cos(t)', t=t, element = DG1) # tempreture
#(p, uu, u) = TrialFunctions(W) # store the solution for current step
#(p0, uu0, u0) = TrialFunctions(W) # store the solution for previous step
w = Function(W)
w0 =Function(W)
#initialize solution according to exact solution
assign(w.sub(0), interpolate(p_exact, DGO))
assign(w.sub(1), interpolate(uu_exact, BDM))
assign(w.sub(2), interpolate(u_exact, DG1))
assign(w0.sub(0), interpolate(p_exact, DGO))
assign(w0.sub(1), interpolate(uu_exact, BDM))
assign(w0.sub(2), interpolate(u_exact, DG1))
# split w into p (pressure), uu (velocity) and u (temperature)
(p, uu, u) = w.split()
(p0, uu0, u0) = w0.split()
# define test functions
(q, vv, v) = TestFunctions(W)
n = FacetNormal(mesh)
# penalty weights
alpha = Constant(5000.0)
gamma = Constant(10000.0)
# cell size
h = CellDiameter(mesh)
# Eq. 20, flow
F_flo = nabla_div(uu)*q*dx - f_1*q*dx
#F_flo = inner(nabla_div(uu),q)*dx - inner(f_o91,q)*dx
# Eq. 21, velocity
F_vel = (dot(uu,vv) - div(vv)*p - inner(gravity(u),vv) - inner(f_2,vv))*dx + dot(n,vv)*p_exact*ds
# un = un on the outflow facet, otherwise 0
un = (dot(uu,n) +abs(dot(uu,n))) / 2.0
# Eq. 26 , uu is not divergence-free here
a_a = dot(grad(v), -uu*u)*dx - u*v*nabla_div(uu)*dx - f_3*v*dx + dot(jump(v), un('+')*u('+') - un('-')*u('-'))*dS + dot(v,un*u)*ds
# Eq. 28
a_d = dot(grad(v),grad(u))*dx - dot(avg(grad(v)),jump(u,n))*dS - dot(jump(v,n), avg(grad(u)))*dS\
- dot(grad(v),n*u)*ds - dot(v*n, grad(u))*ds\
+ (alpha('+')/h('+'))*dot(jump(v,n),jump(u,n))*dS + (gamma/h)*v*u*ds\
+ u_exact*dot(grad(v),n)*ds - (gamma/h)*u_exact*v*ds
a_tim = (u-u0)/dt*v*dx
#Eq. 29
F_a_d = a_tim +a_a +a_d
#Eq. 32
F = F_flo + F_vel + F_a_d
# Time-stepping loop
while t<T:
t += dt
print ("nx=%d order=%d t=%f" % (nx, order, t))
# update time in time-dependent expressions
f_1.t = t; f_2.t = t; f_3.t = t
u_exact.t = t; p_exact.t = t; uu_exact.t = t
# solve the system and store solution in w
solve(F==0, w)
# update w0 with w
w0.vector()[:] = w.vector()
# save solution in .xml file
File('Solution.xml') << w
ERROR Output
TypeError Traceback (most recent call last)
<ipython-input-51-abd99c760f99> in <module>()
44
45 #intitialize solution according to exact solution
---> 46 assign(w.sub(0), interpolate(p_exact, DGO))
47 assign(w.sub(1), interpolate(uu_exact, BDM))
48 assign(w.sub(2), interpolate(u_exact, DG1))
/usr/lib/python3/dist-packages/dolfin/fem/interpolation.py in interpolate(v, V)
65 Pv = MultiMeshFunction(V)
66 else:
---> 67 Pv = Function(V)
68
69 # Compute interpolation
/usr/lib/python3/dist-packages/dolfin/function/function.py in __init__(self, *args, **kwargs)
232
233 else:
--> 234 raise TypeError("Expected a FunctionSpace or a Function as argument 1")
235
236 # Set name as given or automatic
TypeError: Expected a FunctionSpace or a Function as argument 1