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

Import code among ufl files

0 votes

Hi there!
I have a simple question: is it possible to reuse code from a UFL file in other ufl files? I am thinking of something live an import operation.

One immediate use could be the following

Stokes.ufl:
element = FiniteElement("Lagrange", cell, 1)
...

and then

Stokes2D.ufl:
cell = triangle
import Stokes.ufl

Stokes3D.ufl:
cell = tetrahedron
import Stokes.ufl

so that if I ever want to change the forms [e.g. time stepping scheme] I don't have to change it in all files.

Another possible use is in case I want to define several forms built from the same basic stuff. I can put them all in the same file, but then each time I change one of them, all must be recompiled; conversely, if I put each on them in a separate file I have to copy-paste the common part, and change it in several parts of the code if needed.

asked Nov 1, 2014 by Massimiliano Leoni FEniCS User (1,760 points)

I don't think that the ufl file is really meant to be used like that. With that said, I am sure that you could write your own interface to do that - but it would be a somewhat different thing than what the ufl file was meant to be. Check out the FFC documentation:
http://www.math.chalmers.se/~logg/pub/papers/LoggOelgaard2010a.pdf

(I'm far from an expert, and could easily be way off)

I don't quite get what you want to say. How do you think a ufl file is meant to be used?

1 Answer

0 votes

This is a very ugly hack, I have two files - one containing information similar to your Stokes.ufl file (I used Poisson.ufl) and another that sets the cell to "triangle," named "Poisson2D.ufl"

Poisson.ufl:

element = FiniteElement("Lagrange", cell, 1)

u = TrialFunction(element)
v = TestFunction(element)
f = Coefficient(element)
g = Coefficient(element)

a = inner(grad(u), grad(v))*dx
L = f*v*dx + g*v*ds

Poisson2D.ufl:

cell = triangle
# import Poisson <-- this won't work
with open("Poisson.ufl") as f:
  exec(f.read())

This compiles on my system, dolfin 1.4.0, Mac OS X 10.9, with:

ffc -l dolfin Poisson2D.ufl
answered Nov 5, 2014 by timm FEniCS User (2,100 points)

This is subtle, although also I was taught that exec is evil :)

Thanks for the hint, this will be an option if the problem eventually got too big to take care of manually.

...