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

extract function spaces from an ufl form

+1 vote

hello,

i would like to pass a ufl form to a function and extract the used FunctionSpaces somehow:

def get_function_space(form):
  do something
  return(V)

V=FunctionSpace(mesh, "CG", 1)
u = TestFunction(V)
v = TrialFunction(V)
a = inner(grad(u), grad(v)) * dx

V_extracted = get_function_space(a)

is this possible somehow?
thanks
Florian

asked Aug 26, 2015 by florian_bruckner FEniCS Novice (380 points)

1 Answer

0 votes

Not the most elegant solution, but you can try:

 args = a.arguments()
 x = args[0]    # first argument in form
 X = x.function_space()
answered Aug 26, 2015 by konstantin FEniCS Novice (900 points)

If you want all function spaces, try:

[f.function_space() for f in form.arguments() + form.coefficients()]

(not tested for spelling errors)

...