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

HOW Apply boundary conditions to a Function ?

0 votes

Hello Fenics users,

My problem is as follow :
assume I got the element u created as u = Function(FE_space)
and the list of boundary conditions [bc1, bc2, ...]

My question is
How to apply these boundary conditions to u, such that if I plot it (plot(u)) I have to see the field only on the boundary ?

any idea that could bring a path is appreciated
thanks in advance

asked May 30, 2016 by Amexsa FEniCS Novice (350 points)

1 Answer

0 votes

Is this sufficient?

for bc in [bc1, bc2, ...]:
    bc.apply(u.vector())
plot(u)
interactive()
answered May 31, 2016 by Øyvind Evju FEniCS Expert (17,700 points)

I've already tried it, but it results in the following error:

*** Error: Unable to successfully call PETSc function 'VecSetValuesLocal'.
*** Reason: PETSc error code is: 63.
*** Where: This error was encountered inside /build/dolfin-k_QrtL/dolfin-1.6.0/dolfin/la/PETScVector.cpp.
*** Process: 0


*** DOLFIN version: 1.6.0
*** Git changeset: unknown

any idea ?

Not immediately, no. Can you post a minimal working example?

This is a description of what I do exactly :

# Load mesh
meshfilename = "square"
mesh = Mesh("%s.xml" %(meshfilename))

# define boundary conditions, measure, and the outward normal
boundaries=MeshFunction("size_t",mesh,"%s_facet_region.xml" %(meshfilename))
ds = Measure("ds")[boundaries]  
outnormal = FacetNormal(mesh) # outward normals to cells
#**************************************************************************************************************
# Define function spaces
velocity_FE_space = VectorFunctionSpace(mesh, "CG", 2)
pressure_FE_space = FunctionSpace(mesh, "CG", 1)
W = velocity_FE_space * pressure_FE_space
#******************************************************************************************************************

u_top_bc = Constant((cos(theta*t),0))
phys_tags_and_bdc_values = [[7,u_top_bc],[8,zero],[9,zero],[10,zero]];
bcs = set_bdc(W.sub(0), boundaries, phys_tags_and_bdc_values);

uo = Function(velocity_FE_space)

for bd in bcs:
    bd.apply(uo.vector())

plot(u), interactive

The list of boundary conditions are returned by the function 'set_bcs' :

def set_bdc(FE_space, boundaries, phys_tags_and_bdc_values):
    bcs = [];
    for i in range(len(phys_tags_and_bdc_values)):
        bcs.append(DirichletBC(FE_space, phys_tags_and_bdc_values[i][1], boundaries, phys_tags_and_bdc_values[i][0]))
    return bcs

Note that the mesh is generated by Gmsh and converted to xml by the command line dolfin-convert file.msh file.xml

try change bc.apply(u.vector()) to bc.apply(u)

this did not work as well !
I got this error :

TypeError: in method 'DirichletBC_apply', argument 2 of type 'dolfin::GenericVector &'

Anybody for a suggested idea !!

...