Subject: [FFC-dev] including a file in .form

From: "Garth N. Wells" <[email-masked]>
Date: Fri, 30 Dec 2005 10:57:59 +0100

Is it to "include" a .form (or .py) file in a .form file? If not, could
this be done easily in the FFC parser?

I'm setting up the convection-diffusion solver in DOLFIN for both 2D and
3D, and the only difference in the FFC .form files is the definition of
the finite elements at the top (3 lines in the case of
convection-diffusion). I'd like to have something like

ConvectionDiffusion.form:
  u = BasisFunction(scalar)
  .
  .
  a = . . .
  L = ...

ConvectionDiffusion_3D.form:
  scalar = FiniteElement("Lagrange", "tetrahedron", 1)
  .
  include ConvectionDiffusion.form

ConvectionDiffusion_2D.form:
  scalar = FiniteElement("Lagrange", "triangle", 1)
  .
  include ConvectionDiffusion.form

Since "ConvectionDiffusion.form" is incomplete, a better name might be
"ConvectionDiffusion.pde".

This would be useful for developing 2D and 3D versions of a particular
PDE, as well as different order elements. 

Garth

---

From: Anders Logg <[email-masked]>
Date: Fri, 30 Dec 2005 09:34:10 -0600

There are two ways to do this:

1. Create three files as you suggest and import the file
ConvectionDiffusion.form in both ConvectionDiffusion_2D.form and
ConvectionDiffusion_3D.form as follows:

    ConvectionDiffusion.form
    ------------------------
    ...
    a = ...
    L = ...

    ConvectionDiffusion_2D.form
    ---------------------------
    scalar = FiniteElement("Lagrange", "triangle", 1)
    ...
    import "ConvectionDiffusion.form"

    ConvectionDiffusion_3D.form
    ---------------------------
    scalar = FiniteElement("Lagrange", "tetrahedron", 1)
    ...
    import "ConvectionDiffusion.form"

2. Use the Python interface to FFC and create one file named
ConvectionDiffusion.py and then do

    python ConvectionDiffusion.py

to compile. The contents could be something like

    from ffc import *

    def forms(scalar, vector):
        u = BasisFunction(scalar)
        ...
        a = ...
        L = ...
        return (a, L)

    scalar = FiniteElement("Lagrange", "triangle", 1)
    vector = FiniteElement("Vector Lagrange", "triangle", 1)
    (a, L) = forms(scalar, vector)
    compile([a, L], "ConvectionDiffusion_2D")

    scalar = FiniteElement("Lagrange", "tetrahedron", 1)
    vector = FiniteElement("Vector Lagrange", "tetrahedron", 1)
    (a, L) = forms(scalar, vector)
    compile([a, L], "ConvectionDiffusion_3D")

We could add some built-in support to the parser/command-line
interface to handle both 2D and 3D problems, but one could also argue
that we should keep it as simple as possible, since all the
flexibility you need is available anyway through the Python interface.

/Anders

On Fri, Dec 30, 2005 at 10:57:59AM +0100, Garth N. Wells wrote:
> _______________________________________________
> FFC-dev mailing list
> [email-masked]
> http://www.fenics.org/cgi-bin/mailman/listinfo/ffc-dev
> 

-- 
Anders Logg
Research Assistant Professor
Toyota Technological Institute at Chicago
http://www.tti-c.org/logg/

---

From: "Garth N. Wells" <[email-masked]>
Date: Fri, 30 Dec 2005 16:43:27 +0100

> flexibility you need is available anyway through the Python interface.

The procedure to include files seems really simple, so I think that
there is no need to build in any extra functionality for 2D/3D.

Garth

-- 
Dr. Garth N. Wells
Faculty of Civil Engineering and Geosciences
Delft University of Technology
Stevinweg 1
2628 CN Delft
The Netherlands

tel.     +31 15 278 7922
fax.     +31 15 278 6383
e-mail   [email-masked]
url      http://www.mechanics.citg.tudelft.nl/~garth

---

From: Anders Logg <[email-masked]>
Date: Fri, 30 Dec 2005 09:44:41 -0600

ok, sounds good.

I just realized that you need to name the file you include
"ConvectionDiffusion.py" instead of "ConvectionDiffusion.form", since
it is not a valid form file and the script scripts/compileforms in
DOLFIN would otherwise try to compile it.

/Anders

---

From: Johan Jansson <[email-masked]>
Date: Fri, 30 Dec 2005 18:46:19 +0100

> there is no need to build in any extra functionality for 2D/3D.

I think there could be a problem with the Python search path (at least
when using the FFC executable). I remember testing something similar
to this, but then the Python search path was not set to the directory
containing the form file. So perhaps the DOLFIN build system needs to
be exploited to handle this, or some other convention adopted, like
adding the current directory to the search path.

  Johan

---

From: Anders Logg <[email-masked]>
Date: Fri, 30 Dec 2005 16:15:47 -0600

> adding the current directory to the search path.

You're right, there is (was) a problem. I've fixed it so now it should
work to use import from .form files.

Note that if you put stuff in foo.py, you need to import it using

    import foo

and then access the functions etc you put in there by foo.bar.

The easiest is probably to just declare a and L etc in foo.py and then
do

    from foo import *

I tested this for a simple example and it works now, so tell me if you
run into difficulties.

/Anders