Extracting components of conditional expressions


106
views
0
4 months ago by
Is there a way to access one component of a vector function defined using "conditional"? Trying to do so using the "sub" method throws an AttributeError:

To illustrate:
from dolfin import *

mesh = UnitSquareMesh(20,20)

Ze = VectorElement("CG", mesh.ufl_cell(), 2)
Z = FunctionSpace(mesh, Ze)
(x,y) = SpatialCoordinate(Z.mesh)
f = Expression(("1.0", "0.0"), element = Z.ufl_element())
g = Expression(("-1.0", "0.0"), element = Z.ufl_element())
zf = interpolate(f, Z)
zg = interpolate(g, Z)
z = conditional(ge(y,0.5), zf, zg)
z.sub(0) #AttributeError​
I am using dolfin 2017.2.0 installed using apt-get on Linux Mint 18.3. I suspect my problem arises from not understanding what kind of object is produced by the "conditional" statement. Thanks in advance!

P.S. What's the markup for monospace fonts/code snippets on this forum?
Community: FEniCS Project

1 Answer


4
4 months ago by
You can access components of UFL objects with nonzero rank using the square brackets, e.g.,

from dolfin import *

mesh = UnitSquareMesh(20,20)

Ze = VectorElement("CG", mesh.ufl_cell(), 2)
Z = FunctionSpace(mesh, Ze)

# This line also gave me an error, so I changed it:
#(x,y) = SpatialCoordinate(Z.mesh)
(x,y) = SpatialCoordinate(mesh)

f = Expression(("1.0", "0.0"), element = Z.ufl_element())
g = Expression(("-1.0", "0.0"), element = Z.ufl_element())
zf = interpolate(f, Z)
zg = interpolate(g, Z)
z = conditional(ge(y,0.5), zf, zg)

#z.sub(0) #AttributeError
z0 = z[0]
​
Thank you for your answer, I thought it would be something simple! Could you help me understand a bit more about how the "Conditional" class differs from "Function"? The UFL documentation is pretty scant and I've noticed that things like function assignment don't work either.

To illustrate, if we append the following to the code you posted in your answer:
w = interpolate(Constant(("1.0","0.0")), Z)

u = Function(Z)
u.assign(w) #This is fine
u.assign(z) #RuntimeError​
written 4 months ago by Hamza Alawiye  
2
Objects of the type Function represent linear combinations of finite element basis functions.  The vector of coefficients for a Function u can be retrieved as a GenericVector by u.vector().  The assign() method copies the coefficients from one Function's vector into the coefficient vector of another Function in the same space.  It can also take a linear combination of Functions in the same space as an argument.  However, a Conditional (or many other UFL objects, e.g., the product or ratio of two Functions) cannot necessarily be represented as a linear combination of finite element basis functions, so there is not a vector of coefficients to copy with the assign() method.
written 4 months ago by David Kamensky  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »