Subject: [FFC-dev] Error accessing vectors via indices in mixed elements

From: Michael Brandl <[email-masked]>
Date: Wed, 08 Oct 2008 23:58:23 +0200

Hi,

I am new to this list and using ffc for my bachelor thesis.

Hopefully I am not asking something stupid which has already been asked; 
I tried to look through FFC-dev's archieves, but didn't find an answer.

What I want to do is using a mixed element (with a vector element and a 
finite element), but then to access the single components of a test or 
trial function defined on the vector element.

A concrete example would be this modification of the "Mixed formulation 
of Stokes" from the documentation:

P2 = VectorElement("Lagrange", "triangle", 2)
P1 = FiniteElement("Lagrange", "triangle", 1)
TH = MixedElement([P2, P1])
(v, q) = TestFunctions(TH)
(u, p) = TrialFunctions(TH)
f = Function(P2)
a = (dot(grad(v), grad(u)) - div(v)*p + q*div(u))*dx
L = (v[i] * f[i])*dx

where I only changed the last line (from being "L = dot(v, f) * dx" into 
its componentwise computation).

I get the following error:
--------
Preprocessing form file: Test.form --> Test.py

Traceback (most recent call last):
   File "/usr/bin/ffc", line 180, in ?
     sys.exit(main(sys.argv[1:]))
   File "/usr/bin/ffc", line 107, in main
     execfile(outname, ns)
   File "Test.py", line 27, in ?
     L = (v[i] * f[i])*dx
TypeError: list indices must be integers
-----

I am afraid that I am not good enough in Python to find a solution to 
that, therefore I would appreciate any kind of help very much.

Thank you!

By the way, while going through this example (Mixed formulation of 
Stokes)from the documentation I found some minor errors there:
It should be
"P2 = VectorElement("Lagrange", "triangle", 2)"
instead of
"P2 = FiniteElement("Vector Lagrange", "triangle", 2)",
"a = (dot(grad(v), grad(u)) - div(v)*p + q*div(u))*dx"
instead of
"a = (dot(grad(v), grad(u)) - div(v)*P + q*div(u))*dx";

and finally the sentence about the example file with Heat.form seems to 
be misplaced; I didn't manage to find this example there.

Thank you very much,

yours sincerely,

Michael Brandl

---

From: Kristian Oelgaard <[email-masked]>
Date: Thu, 09 Oct 2008 09:32:13 +0200

> TypeError: list indices must be integers

Funny, I get:

  File "/home/oelgaard/fenics/ffc/scripts/ffc", line 218, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/home/oelgaard/fenics/ffc/scripts/ffc", line 107, in main
    execfile(script, {})
  File "Stokes.py", line 24, in <module>
    L = (v[i] * f[i])*dx
TypeError: object cannot be interpreted as an index

which is a little different, but still an error. Apparently the Index() can't
figure out how to handle MixedElements (when the subelements are also mixed
elements), I'm not sure where to look in order to fix this though, and it might
be taken care of in UFL?

For now you can do:

for i in range(2):
    L += (v[i]*f[i])*dx

which produces the same result as:

L = dot(v,f)*dx

or just use fixed indices:

L = (v[0]*f[0])*dx + (v[1]*f[1])*dx

> be misplaced; I didn't manage to find this example there.

I've fixed these issues in the manual + some other similar 'bugs', thanks.

Kristian
 
> Thank you very much,
> 
> yours sincerely,
> 
> Michael Brandl
> _______________________________________________
> FFC-dev mailing list
> [email-masked]
> http://www.fenics.org/mailman/listinfo/ffc-dev
>

---

From: "Martin Sandve Alnæs" <[email-masked]>
Date: Thu, 9 Oct 2008 10:07:41 +0200

> elements), I'm not sure where to look in order to fix this though, and it might

Shouldn't be a problem in UFL.

--
Martin

---

From: Anders Logg <[email-masked]>
Date: Thu, 9 Oct 2008 13:08:16 +0200

> that, therefore I would appreciate any kind of help very much.

This is expected. FFC handles mixed elements like the one above by
first creating a vector-valued element that holds all components of
the system. In this case a vector-valued element with three components
is created.

Then TrialFunctions() etc creates a basis function in this space (with
three components) and returns components of this basis function split
into the parts that make up the element:

  w = TrialFunction(TH)
  u = [w[0], w[1]]
  p = w[2]

So u is just a Python list and it does not know how to handle i which
is an FFC object of class Index.

This will be fixed in UFL as already noted by Martin.

> be misplaced; I didn't manage to find this example there.

Thanks for reporting this. I see that Kristian has already updated the
manual.

-- 
Anders

---

From: Michael Brandl <[email-masked]>
Date: Fri, 10 Oct 2008 01:27:06 +0200

Hi,

thanks to everybody for the help, it works fine with the 
"range"-statement. This will be a little less manageable when treating 
larger expressions with several terms that are indexed in different 
ways, though, so I am looking forward to the mentioned fix in UFL.

However, since I am new to using Dolfin: Is FFC already using UFL? Is it 
reasonable for me to expect a fix to this problem within the next weeks, 
or should I focus on getting my fcc files to work with "range"-statements?

Thank you, and thanks for the corrections in the manual,

/Michael Brandl

Anders Logg wrote:
> ------------------------------------------------------------------------
>

---

From: Anders Logg <[email-masked]>
Date: Fri, 10 Oct 2008 08:03:52 +0200

On Fri, Oct 10, 2008 at 01:27:06AM +0200, Michael Brandl wrote:
> However, since I am new to using Dolfin: Is FFC already using UFL?

No.

> Is it reasonable for me to expect a fix to this problem within the
> next weeks, or should I focus on getting my fcc files to work with
> "range"-statements?

No, that's not reasonable... :-) It will take a while longer.

-- 
Anders