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

Multiplying an Expression by -1 gives PETSc error when generating a DirichletBC

+1 vote

I would like to switch the sign on a Dirichlet boundary deep in the code after the BC is specified, after I have lost access to the original arguments of the Expression which specified the boundary value. When I do this I get a PETSc error

Error: Unable to successfully call PETSc function 'MatSetSizes'.

My FEniCS is the development version from mid September, so I am wondering if anyone
can reproduce this with other FEniCS versions. Also any suggestions to workarounds are welcome.

from dolfin import *

mesh = UnitCubeMesh(2, 2, 2)
V = VectorFunctionSpace(mesh, "CG", 2)
Q = FunctionSpace(mesh, "CG", 1)
W = V*Q

foo = Expression(["0.0", "t", "0.0"], t = 1.0)
foo2 = Expression(["0.0", "t", "0.0"], t = 1.0)
foo2 *= -1

#This one works
bc1 = DirichletBC(*[W.sub(0), foo, "near(x[0], 1.0)"])
#This one does not
bc2 = DirichletBC(*[W.sub(0), foo2, "near(x[0], 1.0)"])
asked Oct 28, 2014 by Gabriel Balaban FEniCS User (1,210 points)

Hi, could this be due to types of foo and foo2 being respectively

print type(foo)
#<class 'dolfin.functions.expression.CompiledExpression'> 
print type(foo2)
#<class 'ufl.tensors.ComponentTensor'>

Anyway. How about this as a workaround

foo2 = Expression(["sign*0.0", "sign*t", "sign*0.0"], t = 1.0, sign=1)
foo2.sign = -1

Hi Mirok,
I think the differing types play a role as well. Your workaround is a good idea! The sign can be easily changed later on in the code.

1 Answer

0 votes

Yes, it is reproducible (with later fenics-dev) and failing is the current intended behaviour. Here is a different workaround at the cost of an interpolation, but no assumptions on the original expression 'foo':

foo2 = interpolate(foo, V)
a = foo2.vector()
a *= - 1.0
bc2 = DirichletBC(*[W.sub(0), foo2, "near(x[0], 1.0)"])
answered Oct 30, 2014 by Marie E. Rognes FEniCS User (5,380 points)
...