This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How do I add a gradient and a user defined vector that contains the unknown?

0 votes

In the middle of my code I have something like

F = inner(grad(u)+(w1, w2), grad(v))*dx

where (w1, w2) is a vector field where w1 and and w2 are scalar functions of x, y, and u, with x and y defined as

x = Expression("x[0]")
y = Expression("x[1]").

I get the error "can't multiply sequence by non-int of type 'Sum' "

I've seen people define vector fields as

vf = Expression(('x[0]**2', 'x[1]**2'))

but how do I do that when I need to have something like

vf = Expression(('u*x[0]', 'u*x[1]'))?

asked Apr 24, 2017 by srody FEniCS Novice (180 points)
edited Apr 24, 2017 by srody

1 Answer

0 votes

Hi, consider

from dolfin import *

mesh = UnitSquareMesh(10, 10)

V = FunctionSpace(mesh, 'CG', 1)
u = TrialFunction(V)
v = TestFunction(V)

x = Expression('x[0]', degree=1)
y = Expression('x[1]', degree=1)

eq = inner(grad(u)+as_vector((x, y)), grad(v))*dx
a, L = system(eq)
print assemble(a)
print assemble(L)

a = inner(grad(u) + u*as_vector((x, y)), grad(v))*dx
print assemble(a) 
answered Apr 25, 2017 by MiroK FEniCS Expert (80,920 points)
...