# How to apply a load on a single point or a node

**URL:** <https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799>\
**Category:** Uncategorized\
**Created:** [December 25, 2020, 1:09pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799 "2020-12-25T13:09:50Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [June 3, 2021, 3:44pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/21 "2021-06-03T15:44:39Z")

</div>

> [@Owodou](#):
>
> `E.vector()[:] = E_VALUES`

```auto
E.vector()[:] = 2
E.vector()[pos]=5

```

would assign the value 5 the cell with index `pos` and 2 to all other cells

---

<div class="post-metadata">

**Author:** ![Owodou](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@Owodou](https://fenicsproject.discourse.group/u/Owodou)\
**Post date:** [June 4, 2021, 10:06am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/22 "2021-06-04T10:06:16Z")

</div>

Hi Dokken,  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![Owodou](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@Owodou](https://fenicsproject.discourse.group/u/Owodou)\
**Post date:** [July 2, 2021, 4:49pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/23 "2021-07-02T16:49:28Z")

</div>

Hi,  
I am getting an error in a readable file that I am not understanding.  
Code

```auto
import meshio
msh = meshio.read("/home/pierre/Desktop/GMSH1/carre.msh")
for cell in msh.cells:
    triangle_cells = cell.data
for key in msh.cell_data_dict["gmsh:physical"].keys():
    if key == "triangle":
        triangle_data = msh.cell_data_dict["gmsh:physical"][key]
mesh =meshio.Mesh(points=msh.points,
                           cells=[("triangle", triangle_cells)],
                           cell_data={"name_to_read":[triangle_data]})
meshio.write("test.xdmf",mesh)

E = 2.1e11 #p[0]
nu = 0.3 #p[1]
beta = (E)/(1-(nu*nu))
C = as_matrix([[beta, beta*nu, 0],[beta*nu, beta, 0],[0, 0, beta*(1-nu)/2]])
# ******************************************************************** #
def eps(v):                                                           
    return sym(grad(v))                                               
def strain2voigt(e):                                                  
    """e is a 2nd-order tensor, returns its Voigt vectorial representation"""
    return as_vector([e[0,0],e[1,1],2*e[0,1]])                        
def voigt2stress(s):                                                  
    """
    s is a stress-like vector (no 2 factor on last component)
    returns its tensorial representation
    """
    return as_tensor([[s[0],s[2]],[s[2],s[1]]])                   
def sigma(v):                                                                                    
    return voigt2stress(dot(C,strain2voigt(eps(v))))              
# ******************************************************************** #
class Top(SubDomain):                          
    def inside(self, x, on_boundary):           
        return near(x[1],10) and on_boundary    
class Left(SubDomain):                         
    def inside(self, x, on_boundary):           
        return near(x[0],0) and on_boundary    
class Bottom(SubDomain):                        
      def inside(self, x, on_boundary):
          return near(x[1],0) and on_boundary    
# ******************************************************************** #
# exterior facets MeshFunction
facets = MeshFunction("size_t", mesh, 1) 

```

Error message

```auto

  File "/home/pierre/Desktop/MesEssais/essai28_jun/essai28_jun22.py", line 59, in <module>
    facets = MeshFunction("size_t", mesh, 1)

  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/mesh/meshfunction.py", line 18, in __new__
    return fn(mesh, dim)

TypeError: __init__ (): incompatible constructor arguments. The following argument types are supported:
    1. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: int)
    2. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: int, arg2: int)
    3. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: str)
    4. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: int, arg2: dolfin.cpp.mesh.MeshDomains)
    5. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: dolfin::MeshValueCollection<unsigned long>)

Invoked with: <meshio mesh object>
  Number of points: 146
  Number of cells:
    triangle: 250
  Cell data: name_to_read, 1

```

I tried to correct it based on the error message but unsuccessfully.

Please need your help again.  
Thanks

---

<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:** [July 5, 2021, 8:20pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/24 "2021-07-05T20:20:27Z")

</div>

You need to load the mesh from `XDMF`. i.e.

```python
from dolfin import * 
# First write mesh to xdmf
# ...
# Then define functions and classes
# ...

# Then load mesh
mesh = Mesh()
with XDMFFile("test.xdmf") as infile:
    infile.read(mesh)
# Then create mesh function
facets = MeshFunction("size_t", mesh, 1) 

```

---

<div class="post-metadata">

**Author:** ![Owodou](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@Owodou](https://fenicsproject.discourse.group/u/Owodou)\
**Post date:** [July 6, 2021, 3:03pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/25 "2021-07-06T15:03:40Z")

</div>

Thank you dokken,  
I have followed your instructions, but I have another error message:

```auto
Shapes do not match: <ListTensor id=140219301787456> and <Sym id=140219301787200>.
Traceback (most recent call last):

  File "/home/pierre/Desktop/MesEssais/essai28_jun/essai28_jun22.py", line 86, in <module>
    a = inner(sigma(du), eps(u_))*dx

  File "/usr/lib/python3/dist-packages/ufl/operators.py", line 158, in inner
    return Inner(a, b)

  File "/usr/lib/python3/dist-packages/ufl/tensoralgebra.py", line 147, in __new__
    error("Shapes do not match: %s and %s." % (ufl_err_str(a), ufl_err_str(b)))

  File "/usr/lib/python3/dist-packages/ufl/log.py", line 158, in error
    raise self._exception_type(self._format_raw(*message))

UFLException: Shapes do not match: <ListTensor id=140219301787456> and <Sym id=140219301787200>.

```

Please, how to solve this?

---

<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:** [July 6, 2021, 3:11pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/26 "2021-07-06T15:11:48Z")

</div>

You need to provide a minimal code example that reproduces the error, as this is related to the variational form

---

<div class="post-metadata">

**Author:** ![Owodou](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@Owodou](https://fenicsproject.discourse.group/u/Owodou)\
**Post date:** [July 6, 2021, 4:55pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/27 "2021-07-06T16:55:48Z")

</div>

Ok, based on your previous advice, I have developed the code bellow:

```auto
from dolfin import * 
# First write mesh to xdmf
# ...
# Then define functions and classes
# ...

# Then load mesh
mesh = Mesh()
with XDMFFile("test.xdmf") as infile:
    infile.read(mesh)

# exterior facets MeshFunction
facets = MeshFunction("size_t", mesh, 1)  
facets.set_all(0)                           
Top().mark(facets, 1)                       
Left().mark(facets, 2)                      
Bottom().mark(facets, 3)                    
ds = Measure("ds", subdomain_data = facets)

# Define function space
V = VectorFunctionSpace(mesh, 'CG', 1)           

# Define variational problem
du = TrialFunction(V)                  
u_ = TestFunction(V)                   
u = Function(V, name='Displacement')  
a = inner(sigma(du), eps(u_))*dx      

# uniform traction on top boundary
Ttrac = Constant((0., 1e7))
l = dot(Ttrac, u_)*ds(1)  

```

It is the code that gives the previous error I sent you.

---

<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:** [July 6, 2021, 7:41pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/28 "2021-07-06T19:41:32Z")

</div>

> [@Owodou](#):
>
> ```auto
> mesh =meshio.Mesh(points=msh.points,
> cells=[("triangle", triangle_cells)],
> cell_data={"name_to_read":[triangle_data]})
> 
> ```

If your mesh is 2 dimensional, you Need to prune the z-coordinates of points before writing to file (you can use ` mesh.prune_z_0()` for the mesh above.  
You can verify this by printing `mesh.geometry().dim()` after loading the mesh into dolfin; it should be 2

---

<div class="post-metadata">

**Author:** ![Owodou](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@Owodou](https://fenicsproject.discourse.group/u/Owodou)\
**Post date:** [July 7, 2021, 12:09pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/29 "2021-07-07T12:09:40Z")

</div>

Sorry Dokken, another error occurs when I was trying to prune the z-coordinates.

```auto
from __future__ import print_function
from fenics import *   
from dolfin import *    
from mshr import *                        
import numpy as np 
from numpy import linalg as LA 
import meshio

msh = meshio.read("/home/pierre/Desktop/GMSH1/carre.msh")
for cell in msh.cells:
    triangle_cells = cell.data
for key in msh.cell_data_dict["gmsh:physical"].keys():
    if key == "triangle":
        triangle_data = msh.cell_data_dict["gmsh:physical"][key]
mesh =meshio.Mesh(points=msh.points,
                           cells=[("triangle", triangle_cells)],
                           cell_data={"name_to_read":[triangle_data]})
mesh.prune_z_0()
meshio.write("test.xdmf",mesh)

```

Error message:

```auto

  File "/home/pierre/Desktop/MesEssais/essai28_jun/essai28_jun22.py", line 18, in <module>
    mesh.prune_z_0()

AttributeError: 'Mesh' object has no attribute 'prune_z_0'

```

Thank you for spending your time with me.

---

<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:** [July 7, 2021, 1:18pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/30 "2021-07-07T13:18:35Z")

</div>

What version of meshio are you using? This function is part of: [meshio/\_mesh.py at 20ef6275f4fdd07c2a85082761dd38fccf393476 · nschloe/meshio · GitHub](https://github.com/nschloe/meshio/blob/20ef6275f4fdd07c2a85082761dd38fccf393476/src/meshio/_mesh.py#L206)  
and has been in meshio since version 4.3.0

---

<div class="post-metadata">

**Author:** ![Owodou](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@Owodou](https://fenicsproject.discourse.group/u/Owodou)\
**Post date:** [July 7, 2021, 4:07pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/31 "2021-07-07T16:07:26Z")

</div>

meshio version : 2.3.10 on ubuntu 20.04 environment.  
I am trying to update it to the latest version.  
Come back soon  
thanks

---

<div class="post-metadata">

**Author:** ![hkhodavirdi](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hkhodavirdi/32/3955_2.png) [@hkhodavirdi](https://fenicsproject.discourse.group/u/hkhodavirdi)\
**Post date:** [June 30, 2022, 7:04pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/32 "2022-06-30T19:04:07Z")

</div>

Hi,  
My meshio version is 5.3.4 but I get the same error. Can you please help me with this?  
Thanks

---

<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:** [June 30, 2022, 8:04pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/33 "2022-06-30T20:04:47Z")

</div>

See [What is wrong with my mesh? - #8 by dokken](https://fenicsproject.discourse.group/t/what-is-wrong-with-my-mesh/7504/8)

---

<div class="post-metadata">

**Author:** ![hkhodavirdi](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/hkhodavirdi/32/3955_2.png) [@hkhodavirdi](https://fenicsproject.discourse.group/u/hkhodavirdi)\
**Post date:** [July 1, 2022, 1:28am UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/34 "2022-07-01T01:28:16Z")

</div>

It worked. Thank you so much.

---

<div class="post-metadata">

**Author:** ![Eric](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Eric](https://fenicsproject.discourse.group/u/Eric)\
**Post date:** [February 26, 2023, 1:29pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/35 "2023-02-26T13:29:27Z")

</div>

Hi Dokken, thank you very much for your help! I have a question to ask you. When I run this code, it returns this error. Is this a problem with my “fenics\_adjoint”, or is it something else?

 ![fig1](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/b/b38552978446795d824e2f174052e15ff571c293.png)

---

<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:** [February 26, 2023, 1:31pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/36 "2023-02-26T13:31:52Z")

</div>

See reply in: [Implement point source in the variational formulation - #11 by dokken](https://fenicsproject.discourse.group/t/implement-point-source-in-the-variational-formulation/229/11)

Pleaee avoid making duplicate questions.

---

<div class="post-metadata">

**Author:** ![Eric](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Eric](https://fenicsproject.discourse.group/u/Eric)\
**Post date:** [February 26, 2023, 1:36pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/37 "2023-02-26T13:36:31Z")

</div>

I apologize for this and thank you for the reminder.

---

<div class="post-metadata">

**Author:** ![Eric](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Eric](https://fenicsproject.discourse.group/u/Eric)\
**Post date:** [February 28, 2023, 1:21pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/38 "2023-02-28T13:21:20Z")

</div>

Hi Dokken, thank you very much for your help. Am I to understand that the position (1, 1) where delta is loaded is its function space and not its coordinates in the actual space.

---

<div class="post-metadata">

**Author:** ![RR\_rr](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/rr_rr/32/6571_2.png) [@RR\_rr](https://fenicsproject.discourse.group/u/RR_rr)\
**Post date:** [November 17, 2024, 3:32pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/39 "2024-11-17T15:32:32Z")

</div>

> [@dokken](#):
>
> ```auto
> from dolfin import *
> import numpy as np
> mesh = UnitSquareMesh(20, 20)
> V = VectorFunctionSpace(mesh, "CG", 1)
> 
> class Delta(UserExpression):
> def __init__ (self, eps, x0, **kwargs):
> self.eps = eps
> self.x0 = x0
> UserExpression. __init__ (self, **kwargs)
> def eval(self, values, x):
> eps = self.eps
> values[0] = eps/pi/(np.linalg.norm(x-self.x0) **2 + eps** 2)
> values[1] = 0
> 
> def value_shape(self): return (2, )
> 
> delta = Delta(eps=1E-4, x0=np.array([1, 1]), degree=5)
> 
> u = project(delta, V)
> File("u.pvd") << u
> 
> ```

Hello, Dokken. I understand that the `delta` in your code is a 2D vector. Its x-component is a delta function, and its y-component is 0. I want to define a scalar delta function, which has one component. I changed your code a little bit, as shown below:

```auto
from dolfin import *
import numpy as np
mesh = UnitSquareMesh(20, 20)
V = VectorFunctionSpace(mesh, "CG", 1)
V_new = FunctionSpace(mesh, "CG", 1)

class Delta(UserExpression):
    def __init__ (self, eps, x0, **kwargs):
        self.eps = eps
        self.x0 = x0
        UserExpression. __init__ (self, **kwargs)

    def eval(self, values, x):
        eps = self.eps
        values[0] = eps/pi/(np.linalg.norm(x-self.x0) **2 + eps** 2)

    def value_shape(self): return (1, )

delta = Delta(eps=1E-4, x0=np.array([1, 1]), degree=5)

u_new = project(delta, V_new)
File("u_new.pvd") << u_new

```

As shown in the above code, I defined a scalar delta function, and projected it into a scalar function. Then I open it using Paraview to see its value. However, when I run this code, I got this error:  
`ufl_legacy.log.UFLException: Shapes do not match: <Argument id=133605434534752> and <Coefficient id=133604840997376>.`  
Could you please give me some advice? 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:** [November 17, 2024, 5:18pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799/40 "2024-11-17T17:18:00Z")

</div>

> [@RR\_rr](#):
>
> ` def value_shape(self): return (1, )`

Try  
` def value_shape(self): return ()`

[Previous page](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799.md?page=1)

[Next page](https://fenicsproject.discourse.group/t/how-to-apply-a-load-on-a-single-point-or-a-node/4799.md?page=3)
