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

How to traverse UFL vector expressions?

0 votes

Dear all,

I am trying to manipulate UFL forms, following the traverser example from the UFL user manual to be found here (page 87)

http://fenicsproject.org/pub/documents/ufl/ufl-user-manual/ufl-user-manual.pdf

The traverser example from there unfortunately fails, if the input form contains a vector. In particular, my traverser is

from dolfin import *
from ufl import algorithms

class Traverser(algorithms.transformer.ReuseTransformer):
    def __init__(self):
        algorithms.transformer.ReuseTransformer.__init__(self)

    def sum(self, o):
        operands = o.ufl_operands
        print "SUM", map(str,operands)
        newoperands = []
        for e in operands:
            newoperands.append( self.visit(e) )
        return sum(newoperands)

and I use it on

FS = VectorFunctionSpace(mesh, "CG", 1)
e = Function(FS)
e.rename("e", "")
f = Function(FS)
f.rename("f", "")
g = Function(FS)
g.rename("g", "")
h = Function(FS)
h.rename("h", "")
a = inner(e, f+g+h)

r = Traverser()

print "Traverser Original:"
b = r.visit(a)
print "Done:", b

Now, if FS is declared a FunctionSpace, everything works. But if FS is declared a VectorFunctionSpace, the traverser crashes with

Can't add expressions with different shapes.

How do I make it dimension/vector/scalar independent? Any ideas are much appreciated...

asked May 11, 2016 by sschmidt FEniCS Novice (490 points)
edited May 11, 2016 by sschmidt

I tried running your code and it worked fine for me, for FS declared FunctionSpace, VectorFunctionSpace and TensorFunctionSpace.
This was with an up-to-date version of ufl.

Thanks!!

That seems odd... what UFL version have you been using? The latest stable release? I am running UFL version 1.7.0dev built from sources on OS X 10.11.4...

It's version 1.7.0dev, up-to-date with ufl master. You may have an older dev version, it seems to work with 1.7.0dev as of February or more recent.

...