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

Create a vector with prescribed dofs on the Dirichlet boundary

+1 vote

Hi, all,

I want to create a vector with prescribed dofs on the Dirichlet boundary, just like the following:

from dolfin import *

# Functionspace and Dirichlet Bcs
V = VectorFunctionSpace(MyMesh, "Lagrange", 1)
bcs = DirichletBC(V, imposed_displacement, definedSubDomain) 

# The solution vector
v = Function(V)
# The vector I need: 
v_p = Function(V) # except those dofs prescribed in bcs, all the other are zero.

I found a very similar question: enter link description here

But it is very complex and hard to understand. I think there should be some simpler routines to this task. Could anyone give any hint? Thanks!

asked Mar 22, 2017 by jywu FEniCS Novice (510 points)

1 Answer

+1 vote
 
Best answer

Please consider using DirichletBC::apply, as follows

from dolfin import *

MyMesh = UnitSquareMesh(3,3)
imposed_displacement = Constant(1)

# Functionspace and Dirichlet Bcs
V = FunctionSpace(MyMesh, "Lagrange", 1)
bcs = DirichletBC(V, imposed_displacement, "on_boundary") 

# The solution vector
v = Function(V)
# The vector I need: 
v_p = Function(V) # except those dofs prescribed in bcs, all the other are zero.

bcs.apply(v_p.vector())
print v.vector().array()
print v_p.vector().array()
plot(v_p, interactive=True)
answered Mar 22, 2017 by francesco.ballarin FEniCS User (4,070 points)
selected Mar 22, 2017 by jywu

It is exactly what I need. Thank you so much, Ballarin!

...