UFL compatability with FunctionSpace and as_tensor()

Hi everyone

I am having some issues with using UFL in my Fenics code (I am fairly new to Fenics btw). Initially, I wished to use the as_tensor function, which requires me to import UFL. But when I did this, my FunctionSpace function gave me this error:

Traceback (most recent call last):
  File "target2.py", line 49, in <module>
    Space = FunctionSpace((mesh), 'P', 1)
TypeError: __init__() takes 3 positional arguments but 4 were given

I don’t think the wrong number of positional arguments have been given, as the mesh is defined by the following code:

mesh = BoxMesh(Point(x0,y0,z0), Point(x1,y1,z1), 80,80,80)
Space = FunctionSpace((mesh), 'P', 1)

Is there some fundamental reason why importing UFL causes this error to appear for my FunctionSpace line? Is it possible to use UFL, with FunctionSpace, such that I can use FunctionSpace and as_tensor() within my code? My full code is shown below:

from __future__ import print_function
from fenics import *
import numpy as np
import matplotlib.pyplot as plt
from dolfin import *
from mshr import *
from ufl import *
import math

parameters["allow_extrapolation"] = True
parameters["form_compiler"]["cpp_optimize"] = True


#--Dimensions in mm----
x0 = 0 
y0 = 0
z0 = 0
x1 = 8
y1 = 8
z1 = 8
t_start = 0.0
t_end = 900
nstep = 450
dtime = (t_start - t_end)/ nstep
#Configure for dimensions of system

pi = 3.141592653589793
T_am = 0.0 #ambient vacuum temperature
T_a4 = T_am**4
epsilon = 0.05  # material emissivity
sigma = 5.67E-14 # W/(m2.K4)
es = epsilon*sigma
Laser = Expression('2*A*Pmax/(pi*w*w)*exp(-2*pow((x[0] - 4), 2)/(w*w)-2*pow((x[1]-4), 2)/(w*w)-2*pow((x[2]-z1), 2)/(w2*w2) )',degree=3, A=0.1, Pmax=500,w=1, w2= 0.1,z1=z1) #power (w2 localises the z-coordinates)
kappa = 0.174         #conductivity [W/mm K] 
c = 0.132         #Specific Heat Capacity [J/gK]
rho = 0.019            #Density [g/mm^3]
const = kappa /(c * rho)
tau_T = 0.0 #Temperature Gradient Lag
tau_q = 0.0 #Heat Flux Lag

mesh = BoxMesh(Point(x0,y0,z0), Point(x1,y1,z1), 80,80,80)
Space = FunctionSpace((mesh), 'P', 1)
VectorSpace = VectorFunctionSpace(mesh, 'P', 1)
cells = MeshFunction('size_t',mesh,mesh.topology().dim())
facets = MeshFunction('size_t',mesh,mesh.topology().dim()-1)
da = Measure('ds', domain=mesh, subdomain_data = facets)
dv = Measure('dx', domain=mesh, subdomain_data = cells)

initial_T = Expression("Tini", Tini=T_am, degree=3)
T0 = interpolate(initial_T, Space)
T = Function(Space)
V = TestFunction(Space)
dT = TrialFunction(Space)
q0 = Function(VectorSpace)
G = as_tensor(T.dx(i), (i))
G0 = as_tensor(T0.dx(i), (i))
q = as_tensor(dt/(dt + tau_q) * (tau_q/dt*q0[i] - kappa*(1+tau_T/dt)*G[i] + kappa*tau_T/dt*G0[i]),(i))

F = (rho*c/dt*(T-T0)*V - q[i]*V.dx(i) - rho*Laser*V ) * dv + es*(T**4 - T_a4)*V*da
Gain = derivative(Form, T, dT)

file_T = File('target3D/solution.pvd')
for t in np.arange(t_start,t_end,dtime):
	print( "Time", t)
	solve(F==0, T, [], J = Gain, solver_parameters={"newton_solver":{"linear_solver": "mumps", "relative_tolerance": 1e-3} }, form_compiler_parameters={"cpp_optimize": True, "representation": "quadrature","quadrature_degree": 2} )
	file_T << (T,t)
	q0 = project(q, VectorSpace)

Thanks in advance!!

You could consider importing only the elements from the ufl module, that you require, i.e.

from ufl import as_tensor


import ufl


Hi there,

Thank you, this solves my issue with FunctionSpace, but throws up an error with as_tensor, specifically:

Traceback (most recent call last):
  File "target2.py", line 61, in <module>
    G = as_tensor(T.dx(i), (i))
NameError: name 'i' is not defined

Do you have any ideas how this can be rectified?

Following the intended use of as_tensor, you have to define i.
In your case

from ufl import indices
i = indices(3)