Hi everyone,
I’m working with a problem in which a PDE involves the curl of a tensor field. I get the impression that the way I’m computing this curl is relatively slow, and that it could be significantly improved by e.g. vectorizing it in some way. Here is an MWE illustrating how I’ve been doing it:
import dolfin as dlf
import ufl
import numpy as np
# Levi-Civita permutation tensor
Id = np.eye(3)
A = lambda *ind: np.linalg.det(np.array([[Id[p, q] for q in range(3)] for p in ind]))
perm = ufl.as_tensor([[[A(i, j, k) for k in range(3)] for j in range(3)] for i in range(3)])
def tcurl(A):
i, j, k, l = ufl.indices(4)
return ufl.as_tensor(A[i, l].dx(k) * perm[j, k, l], (i,j,))
mesh = dlf.BoxMesh(
dlf.Point(0., 0., 0.),
dlf.Point(2., 2., 2.),
20,
20,
1
)
V = dlf.TensorFunctionSpace(mesh, "CG", 1)
u = dlf.Function(V, name="u")
curl_u = dlf.Function(V, name="curl of u")
u_exp = dlf.Expression("pow(x[0],2) + pow(x[1],2)*x[0] + sin(x[0])", degree=1)
u_init = dlf.Expression(
[
['u11', '0', '0'],
['0', '0', '0'],
['0', '0', '0']
],
u11=u_exp,
degree=1
)
u.assign(dlf.project(u_init, V))
curl_u.assign(dlf.project(tcurl(u), V))
with dlf.XDMFFile("outfile.xdmf") as outfile:
outfile.parameters["flush_output"] = True
outfile.parameters["functions_share_mesh"] = True
outfile.write(u, 0.)
outfile.write(curl_u, 0.)
Does anyone know a faster/better way to compute this?
Thanks!