Hello,
I looked up online for topics similar to this, but nothing helped.
I have a two-dimensional mesh, on which I can successfully define scalar, vectors and tensors. This mesh represents a surface in three-dimensional space, on which a 3d vector field is defined. I would like to define and plot this vector field.
Here is a minimal (non) working example
from __future__ import print_function
from fenics import *
from mshr import *
import numpy as np
import meshio
import ufl as ufl
R = 1.0
r = 0.25
c_R = [0.0, 0.0]
c_r = [0.0, -0.1]
xdmffile_geometry = XDMFFile("geometry.xdmf")
mesh = UnitSquareMesh(1, 1)
V = VectorFunctionSpace(mesh, 'P', 2)
Q = FunctionSpace(mesh, 'P', 2)
def norm(x):
return (np.sqrt(np.dot(x, x)))
class MyVectorFunctionExpression(UserExpression):
def eval(self, values, x):
values[0] = x[0]
values[1] = -x[1]
def value_shape(self):
return (2,)
class MyScalarFunctionExpression(UserExpression):
def eval(self, values, x):
values[0] = 4*x[0]*x[1]*sin(8*(norm(np.subtract(x, c_r)) - r))*sin(8*(norm(np.subtract(x, c_R)) - R))
def value_shape(self):
return (1,)
def X(z):
return as_tensor([x[0], x[1], z(x)])
z_ = Function(Q)
u_ = interpolate(MyVectorFunctionExpression(element=V.ufl_element()) ,V)
z_ = interpolate(MyScalarFunctionExpression(element=Q.ufl_element()), Q)
xdmffile_geometry.parameters.update(
{
"functions_share_mesh": True,
"rewrite_function_mesh": False
})
xdmffile_geometry.write(project(z_, Q), 0)
# xdmffile_geometry.write(project(X(z_), V), 0)
If I comment out the last line, this code runs with no errors. However, if I uncomment it, I get
$ python3 mwe.py
Traceback (most recent call last):
File "mwe.py", line 58, in <module>
xdmffile_geometry.write(project(X(z_), V), 0)
File "mwe.py", line 45, in X
return as_tensor([x[0], x[1], z(x)])
NameError: name 'x' is not defined
Do you know what I am doing wrong ? I would like to define a 3D vectorial field X
that depends on the 2d planar coordinates x,y
and on a function z(x,y)
, where X = [x[0], x[1], z(x)]
, and plot this vector field just like I did for the scalar field z_
in xdmffile_geometry.write(project(z_, Q), 0)
. Thanks