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

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

Quoting Michael Brandl <[email-masked]>:

> 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
> -----

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

> 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.

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