13.07.2015 Views

UFL Specification and User Manual 0.3 - FEniCS Project

UFL Specification and User Manual 0.3 - FEniCS Project

UFL Specification and User Manual 0.3 - FEniCS Project

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>UFL</strong> <strong>Specification</strong> <strong>and</strong> <strong>User</strong> <strong>Manual</strong> <strong>0.3</strong>Martin S. Alnæs, Anders Loggclass FunctionReplacer(Transformer):def __init__(self):Transformer.__init__(self)expr = Transformer.reuse_if_possibleterminal = Transformer.always_reusedef function(self, o):return FloatValue(3.14)element = FiniteElement("CG", triangle, 1)v = TestFunction(element)f = Function(element)a = f*vr = FunctionReplacer()b = r.visit(a)print bThe output of this code is the transformed expression b == 3.14*v. Thiscodealsodemonstrateshowtoreuseexistingh<strong>and</strong>lers. Theh<strong>and</strong>lerTransformer.reuse if poswill return the input object if the oper<strong>and</strong>s have not changed, <strong>and</strong> otherwisereconstruct a new instance of the same type but with the new transformedoper<strong>and</strong>s. The h<strong>and</strong>ler Transformer.always reuse always reusesthe instance without recursing into its children, usually applied to terminals.To set these defaults with less code, inherit ReuseTransformer instead ofTransformer. This ensures that the parts of the expression tree that are notchanged by the transformation algorithms always reuse the same instances.We have already mentioned the difference between pre–traversal <strong>and</strong> post–traversal, <strong>and</strong> some times you need to combine the two. Transformer makesthis easy by checking the number of arguments to your h<strong>and</strong>ler functions tosee if they take transformed oper<strong>and</strong>s as input or not. If a h<strong>and</strong>ler functiondoes not take more than a single argument in addition to self, its childrenare not visited automatically, <strong>and</strong> the h<strong>and</strong>ler function must call visit onits oper<strong>and</strong>s itself.86

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!