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

Projection on subspace of mixed function space

+3 votes

I defined a mixed function space M and now I want to project some function ( expression ) on the sub space of M that is M.sub(0). I received an error message which is at the end of the following code. Is there any way to project an analytical expression on the subpace of a mixed function space?

from dolfin import *
import numpy, sys 

# create mesh and function spaces
degree = int(sys.argv[1])
divisions =[int(arg) for arg in (sys.argv[2:])]
mesh=UnitSquareMesh(*divisions)
V = FunctionSpace(mesh,"Lagrange",degree) # Function space for u ( Temperature)
Q = FunctionSpace(mesh,"Lagrange",degree)  # Function space for \mu ( materi
#property
L = FunctionSpace(mesh,"Lagrange",degree)  # Function space for \lembda 
#Mixed function space
M = MixedFunctionSpace([V,Q,L]) # M is mixed function space

# Define the test function space
TestFunction = TestFunction(M)
(v,q,w) = split(TestFunction)

# split the mixed function space into components
# Define the trial function space
solution = Function(M)
(u,mu,lmd) = split(solution)
PrescribedU=Expression("0.25*(pow(x[0],2)+pow(x[1],2))-x[0]*x[1]*0.5-x[0]+0.5*x[1]")

# projecting analytical solution on the function space
u_m = project(PrescribedU, M.sub(0))

Error message:

Traceback (most recent call last):
  File "objmin_var.py", line 32, in <module>
u_m = project(PrescribedU, M.sub(0))
  File "/usr/lib/python2.7/dist-packages/dolfin/fem/projection.py", line 104, in project
form_compiler_parameters=form_compiler_parameters)
  File "/usr/lib/python2.7/dist-packages/dolfin/fem/assembling.py", line 317, in assemble_system
x0)
RuntimeError: 
*** -------------------------------------------------------------------------
*** Error:   Unable to access ownership range of degree of freedom mapping.
*** Reason:  Cannot determine ownership range for sub-dofmaps.
*** Where:   This error was encountered inside DofMap.cpp.
*** Process: 0
*** -------------------------------------------------------------------------
asked Jan 3, 2014 by mt.fan FEniCS Novice (260 points)
retagged Jan 3, 2014 by johanhake

1 Answer

+3 votes

With the development version you can interpolate the Expressions to a scalar Function and then use the newly implemented FunctionAssigner:

s = Function(V)
s.interpolate(PrescribedU)

# With uncached dofmaps
assign(solution.sub(0), s)

# Or with cached dofmaps
assigner = FunctionAssigner(M.sub(0), V)
assigner.assign(solution.sub(0), s)
answered Jan 3, 2014 by johanhake FEniCS Expert (22,480 points)

Thanks, that solves my problem . But now I am getting the following error( posted after the code) .

PrescribedU=Expression("0.25*(pow(x[0],2)+pow(x[1],2))-x[0]*x[1]*0.5-x[0]+0.5*x[1]")

# projecting analytical solution on the function space
s = Function(V)
u_m = s.interpolate(PrescribedU)


F=inner(nabla_grad(lmd),nabla_grad(v))*mu*dx \
        +inner(nabla_grad(lmd),nabla_grad(u))*q*dx\
        +inner(nabla_grad(w),nabla_grad(u))*mu*dx+inner(u-u_m,v)*dx

Error message is :
Traceback (most recent call last):
File "objmin_var.py", line 38, in
+inner(nabla_grad(w),nabla_grad(u))mudx+inner(u-u_m,v)*dx
TypeError: unsupported operand type(s) for -: 'Indexed' and 'NoneType'

why this type error when u and u_m live in the same space that is V .

...