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"]
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