user2616047
user2616047

Reputation: 9

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(), but I'm not test wohle arrays for truth

I get this error message but I'm not testing whole arrays. My minimum running code:

from dolfin import *
import time, numpy, sys

# parameters
eps = 10e-6

# dimensions in meter
l = 100.0
b = 6.0
h = 3.0

# create mesh
mesh = BoxMesh(0.0, 0.0, 0.0, l, h, b, int(l), int(h), int(b))
print mesh
# functionspace
V = VectorFunctionSpace(mesh, "CG", 2)
boundary_parts = FacetFunction("uint", mesh)
boundary_parts.set_all(0)

# boundary conditions
class BCLeftRight (SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and ((x[0] <= eps) or (x[0] >= l-eps))

n = 41 
steps = numpy.linspace(10,90,n)
i = 0
while i<n:
    class BCTop (SubDomain):
        def inside(self, x, on_boundary):
            return on_boundary and x[1] >= h-eps and x[2] >= b-3-eps and ((
            x[0] >= steps[i]-9.0-15-eps and x[0] <= steps[i]+9.0-15+eps) or (
            x[0] >= steps[i]-9.0+15-eps and x[0] <= steps[i]+9.0+15+eps))#this line produces the error message
    boundary_parts.set_all(0)

    bcLeftRight = BCLeftRight()
    bcTop = BCTop()
    bcLeftRight.mark(boundary_parts, 1)
    bcTop.mark(boundary_parts, 2)
    u0 = Constant((0.0, 0.0, 0.0))
    bc0 = DirichletBC(V, u0, boundary_parts, 1)
    ds = Measure("ds")[boundary_parts]

    # define variational problem
    u = TrialFunction(V)
    v = TestFunction(V)
    f = Constant((0.0, -10000, 0.0))
    a = inner(grad(u), grad(v))*dx
    L = inner(f, v)*dx + inner(f, v)*ds(2)
    #L = inner(load, v)*ds(2)

    # Compute solution
    b = None 
    A = assemble(a)
    u = Function(V)
    b = assemble(L, tensor=b, exterior_facet_domains=boundary_parts) 
    bc0.apply(A, b) 
    solve(A, u.vector(), b)
    i = i+1

This code reproduces the error. Strange is: If I leave the while-loop away I don't get the error message. I already tried "&" and "|" and "logical_and" and "logical_or" and brackets"()". I would be happy for quick advice.

Upvotes: 0

Views: 443

Answers (2)

user2616047
user2616047

Reputation: 9

I could find the fault. It was the

x[2] >= b-3-eps

which I had to modify to

x[2] >= 2.99

Now it works but I don't know why this piece of code was a problem. I had brackets arround the comparisons before but the error still ocurred. thanks for your help.

Upvotes: 0

hpaulj
hpaulj

Reputation: 231615

I think operator order is giving you problems.

x>1 and x<3 produces an error because it is evaluated as (x>1 and x)<3. (x>1) & (x<3) is the correct expression.

This error message comes up frequently on SO, though this () order cause isn't quite so common.

I nearly missed the comment that flagged the error line. The comment is way off screen:

x[0] >= steps[i]-9.0-15-eps and x[0] <= steps[i]+9.0-15+eps) or (
        x[0] >= steps[i]-9.0+15-eps and x[0] <= steps[i]+9.0+15+eps))

try:

(x[0] >= steps[i]-9.0-15-eps) & (x[0] <= steps[i]+9.0-15+eps)) | (
        (x[0] >= steps[i]-9.0+15-eps) & (x[0] <= steps[i]+9.0+15+eps)))

You had the right () in

return on_boundary and ((x[0] <= eps) or (x[0] >= l-eps))

The key is to make sure the logical comparisons are performed first. You may still have to change and/or to ^/|.

Upvotes: 2

Related Questions