Hello,
I am performing FEA on human long bones. The mesh is imported to dolfinx from a .xdmf file. I have to apply a displacement on the femur’s head however I cannot define a geometric function to find the right dofs or facets with dolfinx.fem.locate_dofs_geometrical or dolfinx.fem.locate_dofs_topological.
To solve this I opened the mesh in Paraview and selected the nodes. I used this in dolfinx to find the closest nodes and their corresponding dof number. However I have to set the BCs for the three components of the displacement and I need only for u_x and u_z. I am trying to build the equivalent to dolfinx.fem.locate_dofs_geometrical((V_u.sub(0), V_u.sub(0).collapse()), top) manually - without geometric function, as I did imposing boundary conditions to the three displacement components (shown in the code below).
I would appreciate any help, thank you
## Example for illustration
# Mesh construction
filename = "meshes/"+boneID+"_LinMsh_hRef_prox1.xdmf"
with XDMFFile(MPI.COMM_WORLD, filename, "r") as file:
mesh = file.read_mesh(name="Grid")
element_u = ufl.VectorElement("Lagrange", mesh.ufl_cell(), degree=1, dim=3)
V_u = dolfinx.FunctionSpace(mesh, element_u)
u_ = dolfinx.Function(V_u, name="Boundary Displacement") # Full vector BC
def find_ClosestPts(coor_E,coor):
# Create KD-Tree from the E-coordinates
tree = KDTree(coor)
# Initialize an array to store DOF values associated with mesh points
nearest_values = np.full(coor_E.shape[0], 0) # Initialize with NaN
for i in range(len(coor_E)):
# Query the KD-Tree to find the closest point for each mesh node
dist, index = tree.query(coor_E[i])
# Check if the distance is within the maximum threshold
max_distance = 2.0 # Set the maximum allowed distance (optional)
if dist <= max_distance:
nearest_values[i] = int(index)
return nearest_values
def top(coor):
file_dofs_topBC = "meshes/top_BCs_dofs_" + boneID + "_LinMsh_selRef_prox1.csv"
# Load the coordinates from the CSV file into a numpy array (x, y, z)
coor_csv = np.genfromtxt(file_dofs_topBC, delimiter=",", skip_header=1, usecols=(1, 2, 3))
return find_ClosestPts(coor_csv,coor)
coords = mesh.geometry.x # Get all coordinates of the mesh
dofs_uImp_top = top(coords)
dolfinx.fem.DirichletBC(u_, dofs_uImp_top.astype(np.int32))