I'd like to model a part of the comsol coefficient form for PDEs. First equation in eqn. 16-2. Specifically, I am working on the $\nabla \cdot \gamma$ term. To understand this I have the following script:
from dolfin import *
mesh = UnitIntervalMesh(3)
w = FunctionSpace(mesh, 'P', 1)
#u = TrialFunction(w)
v = TestFunction(w)
def gamma(x): return x
x = MeshCoordinates(mesh)
f = Constant(1.0, cell = mesh.ufl_cell()) * gamma(x)
R = assemble(inner(f, grad(v))*dx(domain = mesh))
print(R.array())
Running this returns
[ 0.83333333 -0.33333333 -0.33333333 -0.16666667]
What I do not understand is that if I change the def of gamma to, say,
def gamma(x) return x**2
I get an error message like:
Shapes do not match: <Product id=139744267574944> and <Grad id=139744267598984>.
Traceback (most recent call last):
File "test_1D_2_grad.py", line 14, in <module>
R = assemble(inner(f, grad(v))*dx(domain = mesh))
which I can not make sense of since both x and x**2 are scalar. What am I missing?
Edit
I found that this also works:
R = assemble(inner(gamma(x), grad(v))*dx(domain = mesh))
which makes me believe that x
gets the entire vector of mesh nodes. How would I operate on that vector to get, say, a x**2
? This does not seem to work:
def gamma(x): return np.array(x) * np.array(x)