# How to apply a pointwise force using dolfinx

**URL:** <https://fenicsproject.discourse.group/t/how-to-apply-a-pointwise-force-using-dolfinx/14298>\
**Category:** dolfinx\
**Created:** [April 17, 2024, 7:59am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-pointwise-force-using-dolfinx/14298 "2024-04-17T07:59:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyunfei1211](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/lyunfei1211/32/6600_2.png) [@lyunfei1211](https://fenicsproject.discourse.group/u/lyunfei1211)\
**Post date:** [April 17, 2024, 7:59am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-pointwise-force-using-dolfinx/14298/1 "2024-04-17T07:59:07Z")

</div>

Hi, everyone! I want to apply a force on a specify node of the mesh in only one direction (e.g x-direction) in linear elasticity problem. How should I do this?

---

<div class="post-metadata">

**Author:** ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)\
**Post date:** [April 17, 2024, 8:03am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-pointwise-force-using-dolfinx/14298/2 "2024-04-17T08:03:50Z")

</div>

Please use the search button before posting. The question has been asked several times in this forum, and one possible solution is [Point Sources (Redux) - #6 by dokken](https://fenicsproject.discourse.group/t/point-sources-redux/13496/6)

---

<div class="post-metadata">

**Author:** ![lyunfei1211](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/lyunfei1211/32/6600_2.png) [@lyunfei1211](https://fenicsproject.discourse.group/u/lyunfei1211)\
**Post date:** [April 17, 2024, 8:15am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-pointwise-force-using-dolfinx/14298/3 "2024-04-17T08:15:23Z")

</div>

Thank you Mr. ballarin. for your suggestion. I apologize for the post question. I’ll search the forum to make sure no the same posting before posting. 🙏

---

<div class="post-metadata">

**Author:** ![lyunfei1211](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/lyunfei1211/32/6600_2.png) [@lyunfei1211](https://fenicsproject.discourse.group/u/lyunfei1211)\
**Post date:** [April 17, 2024, 8:59am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-pointwise-force-using-dolfinx/14298/4 "2024-04-17T08:59:17Z")

</div>

```auto
rd = dolfinx.fem.locate_dofs_geometrical(V=V, marker=lambda x: np.isclose(x[0], 2.0) & np.isclose(x[1], 0.0))
# create compiled form
a_compiled = dolfinx.fem.form(a)
L_compiled = dolfinx.fem.form(L)
# Define a linear variational solver
ksp_type = getattr(petsc4py.PETSc.KSP.Type, "PREONLY")
pc_type = getattr(petsc4py.PETSc.PC.Type, "LU")
solver = petsc4py.PETSc.KSP().create(mesh.comm)
solver.setType(ksp_type)
solver.getPC().setType(pc_type)
# assemble_matrix with Dirichlet conditions
A = dolfinx.fem.petsc.assemble_matrix(a_compiled)
A.assemble()
dofs, _ = bc._cpp_object.dof_indices()
A.zeroRowsColumnsLocal(dofs, diag=1)
solver.setOperators(A)
# assemble_vector without dirichletbc
b = dolfinx.fem.petsc.assemble_vector(L_compiled)
b.ghostUpdate(addv=petsc4py.PETSc.InsertMode.ADD_VALUES, mode=petsc4py.PETSc.ScatterMode.REVERSE)
# apply_lifting, i.e subtract Ag from the vector
dolfinx.fem.petsc.apply_lifting(b, [a_compiled], bcs=[[bc]])
b.ghostUpdate(addv=petsc4py.PETSc.InsertMode.INSERT_VALUES, mode=petsc4py.PETSc.ScatterMode.FORWARD)
# set bc, i.e set the degrees of freedom for known dofs
dolfinx.fem.petsc.set_bc(b, [bc])
b.setValue(rd, 1)

```

When I use ‘’‘b.setValue(rd, 1)’‘’ to apply point force using the above code, it will apply the force to all directions of the point “rd” (which is the dof of the point). But I just want to appy the force in the x-direction. Do you have any suggestions for this? Thank you very much.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 17, 2024, 11:42am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-pointwise-force-using-dolfinx/14298/5 "2024-04-17T11:42:31Z")

</div>

Please add a minimal reproducible example, as it is hard to Give any guidance from the snippet you have produced

---

<div class="post-metadata">

**Author:** ![lyunfei1211](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/lyunfei1211/32/6600_2.png) [@lyunfei1211](https://fenicsproject.discourse.group/u/lyunfei1211)\
**Post date:** [April 17, 2024, 11:57am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-pointwise-force-using-dolfinx/14298/6 "2024-04-17T11:57:49Z")

</div>

I figured out what the mistake was. The degrees-of-freedom of the point I want to find through “rd = dolfinx.fem.locate\_dofs\_geometrical(V=V, marker=lambda x: np.isclose(x[0], 2.0) & np.isclose(x[1], 0.0))” is actually The global index of the node. So if I want to apply a x-direction force on the node, I should use “b.setValue(2\*rd, 1)” instead of “b.setValue(rd, 1)”. Still thanks for your reply.
