ValueError: UFL conditions cannot be evaluated as bool in a Python context

def set_values_inside_cylinder(x, A, B, r, height):
b = B - A
a = A - x
ab = np.dot(a, b)
b_square = np.dot(b, b)
ab_b = ab * b
projection = ab_b / b_square
distance_to_cylinder_axis = np.dot(projection,projection)
distance_to_cylinder_center = (x[0]-projection[0])**2
+(x[1]-projection[1])**2
+ (x[2]-projection[2])**2

# Define the condition for being inside the cylinder
condition0 = ufl.lt(np.dot (projection,a),0)
condition1 = ufl.lt(distance_to_cylinder_axis , height**2)
condition2 = ufl.lt (distance_to_cylinder_center , r**2)
conditions = condition0 and condition1 and condition2
# Use ufl.conditional to set the value based on the condition
value = ufl.conditional(conditions, 1, 0)
return value[quote="mYO, post:1, topic:12583, full:true"]

Please note that post do not meet the requirements of being:

  1. Complete with information making the problem reproducible
  2. Any context of your error, except the error message as the title, with no traceback
  3. No formatting of the code (use 3x` encapsulation), ie.
# Add code here

If further posts are made without complying to these rules, they will be locked and removed.

Please modify your post to conform to the guidelines above.
See for instance:

for a list of examples.

1 Like

sorry i want to add a code but i don’t know to how to paste in like you said

You add in:

```python
# Add code in here
import dolfin
# Add reproducible example
```
```bash
Add traceback/error message here
```
random_code : 0

some text

random_code = 1

visit this site

Python code

from dolfinx import mesh, fem, io
from dolfinx.mesh import MeshTags
import dolfinx
import numpy as np
import pyvista
from dolfinx.fem import (Constant, Function, FunctionSpace, 
                         assemble_scalar, dirichletbc, form, locate_dofs_geometrical)
from dolfinx.fem.petsc import LinearProblem
from dolfinx.mesh import create_unit_cube
from mpi4py import MPI
from ufl import SpatialCoordinate, TestFunction, TrialFunction, dot, dx, ds, grad, Measure
from petsc4py.PETSc import ScalarType
import ufl
domain = create_unit_cube(MPI.COMM_WORLD,3,3,3)
height=0.7
r=0.5
p = 1
V=FunctionSpace(domain, ("Lagrange", p))
A=np.array([0.5,0.85,0.5])
B=np.array([0.5,0.15,0.5])
x = ufl.SpatialCoordinate(domain)
b= B-A
a= x-A
ab = np.dot(a, b)
b_square = np.dot(b, b)
ab_b = ab * b
projection = ab_b /b_square
marker = lambda x : np.dot(projection,a) >0  and np.norm( projection) < height  and\
                    (x[0, :]-projection[0])**2 \
                    +(x[1, :]-projection[1])**2 \
                    + (x[2, :]-projection[2])**2 < r**2
voltage = 62
sphere_dofs = fem.locate_dofs_geometrical (V=V, marker=marker)

i got this i want to mark inside the subdomain

ValueError: UFL conditions cannot be evaluated as bool in a Python context.

You are here mixing ufl and numpy objects.

The function fem.locate_dofs_geometrical takes in a function (in your case marker that takes in a single parameter, x, which is a numpy.ndarray of shape (3, num_points), such that x[0] is a set of x-coordinates, x[1] are the y-coordinates and x[2] is the z-coordinates.

To me it does not seem like you need to do anything with ufl to set up your conditional marker statement, i.e. something along the lines of

from dolfinx import fem
import numpy as np
from dolfinx.fem import (FunctionSpace)

from dolfinx.mesh import create_unit_cube
from mpi4py import MPI
domain = create_unit_cube(MPI.COMM_WORLD,3,3,3)
height=0.7
r=0.5
p = 1
A=np.array([[0.5,0.85,0.5]])
B=np.array([[0.5,0.15,0.5]])
b= B-A
b_square = (B @ B.T) [0, 0]

def marker(x):

    x_T = x.T
    a = x_T-A
    ab = (a @ b.T).reshape(-1, 1)
    ab_b = ab @ b
    projection = ab_b / b_square
    dot_projection = np.einsum("ij,ij->i", projection, a)
    norm_projection = np.einsum("ij,ij->i", projection, projection)
    diff = projection-x.T
    conditional = (dot_projection > 0) & (norm_projection<height) & (np.einsum("ij,ij->i", diff, diff) < r**2)
    return conditional


voltage = 62
V=FunctionSpace(domain, ("Lagrange", p))
sphere_dofs = fem.locate_dofs_geometrical (V=V, marker=marker)
print(sphere_dofs)

Note that I did my best to try to vectorize your code, but it might have introduced some bugs