Dear FEniCS community,
as far as I understand, one may include user-defined functions in x in the variational form via Expression objects, but regarding functions in u(x), in UFL only predefined functions (as sin, sqrt, conditional ...) are supported.
Is it possible in FEniCS to solve variational problems that include user-defined functions in trial functions? In particular, I have a function representing an imported distribution defined as
# fct simulating the distribution of an image
def g(x):
#calculate y,z depending on x
...
return image.getpixel((math.floor(y),math.floor(z)))
Based on this I want to solve a variational problem like this
u = TrialFunction(V)
v = TestFunction(V)
F = ... + g(nabla_grad(u))*v*dx
F = action(F, u_)
J = derivative(F, u_, u) # Gateaux derivative in dir. of u
problem = NonlinearVariationalProblem(F, u_, None, J)
solver = NonlinearVariationalSolver(problem)
solver.solve()
Using this python-definition of g, the code crashes with
Couldn't map 'v_1' to a float, returning ufl object without evaluation.
Traceback (most recent call last):
File "test/test user defined.py", line 58, in <module>
F = g(nabla_grad(u))*v*dx
File "test/test user defined.py", line 38, in g
pixel = image.getpixel((math.floor(y),math.floor(z)))
File "/usr/lib/python2.6/site-packages/ufl/expr.py", line 133, in __float__
return self(()) # No known x
File "/usr/lib/python2.6/site-packages/ufl/exproperators.py", line 286, in _call
return _eval(self, arg, mapping, component)
File "/usr/lib/python2.6/site-packages/ufl/exproperators.py", line 278, in _eval
return f.evaluate(coord, mapping, component, index_values)
File "/usr/lib/python2.6/site-packages/ufl/algebra.py", line 215, in evaluate
tmp *= o.evaluate(x, mapping, (), index_values)
File "/usr/lib/python2.6/site-packages/ufl/algebra.py", line 303, in evaluate
return float(a) / float(b)
TypeError: __float__ returned non-float (type Sum)