# Problems implementing the linear elasticity tutorial on Google Colab

**URL:** <https://fenicsproject.discourse.group/t/problems-implementing-the-linear-elasticity-tutorial-on-google-colab/12843>\
**Category:** General\
**Created:** [November 23, 2023, 6:29pm UTC](https://fenicsproject.discourse.group/t/problems-implementing-the-linear-elasticity-tutorial-on-google-colab/12843 "2023-11-23T18:29:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jpalma](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@Jpalma](https://fenicsproject.discourse.group/u/Jpalma)\
**Post date:** [November 23, 2023, 6:29pm UTC](https://fenicsproject.discourse.group/t/problems-implementing-the-linear-elasticity-tutorial-on-google-colab/12843/1 "2023-11-23T18:29:18Z")

</div>

I’ve been trying to implement the [linear elasticity tutorial](https://jsdokken.com/dolfinx-tutorial/chapter2/linearelasticity_code.html) on Google Colab using a [FEM on Colab](https://fem-on-colab.github.io/packages.html) installation of dolfinx 0.8.0.0. So far I have been unable to properly set a constant vector for the Dirichlet boundary condition of the problem.  
So far I’ve been using the exact code of the implementation, except for the use of `fem.functionspace` instead of `fem.VectorFunctionSpace`

This is the code I’ve been using so far

```auto
from dolfinx import mesh, fem, default_scalar_type
from mpi4py import MPI
import ufl
import numpy as np
L = 1
W = 0.2

domain = mesh.create_box(MPI.COMM_WORLD, [np.array([0, 0, 0]), np.array([L, W, W])],
                         [20, 6, 6], cell_type=mesh.CellType.hexahedron)
V = fem.functionspace(domain, ("Lagrange", 1))

def clamped_boundary(x):
    return np.isclose(x[0], 0)

fdim = domain.topology.dim - 1
boundary_facets = mesh.locate_entities_boundary(domain, fdim, clamped_boundary)

u_D = np.array([0, 0, 0], dtype=default_scalar_type)

bc = fem.dirichletbc(u_D, fem.locate_dofs_topological(V, fdim, boundary_facets), V)

```

However, I always get the following error

```auto
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/dolfinx/fem/bcs.py in dirichletbc(value, dofs, V)
    174 try:
--> 175 bc = bctype(_value, dofs, V)
    176 except TypeError:

TypeError: __init__ (): incompatible function arguments. The following argument types are supported:
    1. __init__ (self, g: ndarray[dtype=float64, writable=False, order='C'], dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C'], V: dolfinx.cpp.fem.FunctionSpace_float64) -> None
    2. __init__ (self, g: dolfinx.cpp.fem.Constant_float64, dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C'], V: dolfinx.cpp.fem.FunctionSpace_float64) -> None
    3. __init__ (self, g: dolfinx.cpp.fem.Function_float64, dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C']) -> None
    4. __init__ (self, g: dolfinx.cpp.fem.Function_float64, dofs: list[ndarray[dtype=int32, writable=False, shape=(*), order='C']], V: dolfinx.cpp.fem.FunctionSpace_float64) -> None

Invoked with types: dolfinx.cpp.fem.DirichletBC_float64, ndarray, ndarray, dolfinx.fem.function.FunctionSpace

During handling of the above exception, another exception occurred:

RuntimeError Traceback (most recent call last)
1 frames
/usr/local/lib/python3.10/dist-packages/dolfinx/fem/bcs.py in dirichletbc(value, dofs, V)
    175 bc = bctype(_value, dofs, V)
    176 except TypeError:
--> 177 bc = bctype(_value, dofs, V._cpp_object)
    178 else:
    179 bc = bctype(_value, dofs)

RuntimeError: Rank mis-match between Constant and function space in DirichletBC

```

This error can be bypassed by using `u_D = default_scalar_type(0)`. However, this generates issues with the dimension of the boundary condition latter on.  
Can anybody help me understand what is going wrong with this impementation?

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [November 23, 2023, 6:36pm UTC](https://fenicsproject.discourse.group/t/problems-implementing-the-linear-elasticity-tutorial-on-google-colab/12843/2 "2023-11-23T18:36:00Z")

</div>

You likely need to provide shape to your function space. Something like

```python
V = fem.functionspace(domain, ("Lagrange", 1, (mesh.geometry.dim,)))

```

---

<div class="post-metadata">

**Author:** ![Jpalma](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@Jpalma](https://fenicsproject.discourse.group/u/Jpalma)\
**Post date:** [November 23, 2023, 7:26pm UTC](https://fenicsproject.discourse.group/t/problems-implementing-the-linear-elasticity-tutorial-on-google-colab/12843/3 "2023-11-23T19:26:04Z")

</div>

Thanks for the answer.  
I’ve tried the suggested code and it solved my issue.  
However I do not quite understand why the shape is provided in the form `(shape, )`  
Anyways, 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 23, 2023, 9:39pm UTC](https://fenicsproject.discourse.group/t/problems-implementing-the-linear-elasticity-tutorial-on-google-colab/12843/4 "2023-11-23T21:39:15Z")

</div>

> [@Jpalma](#):
>
> However I do not quite understand why the shape is provided in the form `(shape, )`

In the old version of dolfinx, you had the Notion of a vector Finite element space. That is a space where a single basis function is repeated over multiple dimensions (such as a vector or tensor), with unique coefficients per dimension).

In the 0.8.0 code, UFL has removed the Notion of such elements, and we Therefore provide a triplet to define the Finite element:

1. The family, here Lagrange
2. The degree of the element, here 1
3. how it should be repeated over multiple dimensions. Default is a single dimension (a scalar), and otherwise you send in a tensor of the appropriate shape, in your case a (3,) vector, which can be generalized by the geometrical dimension of your mesh.

---

<div class="post-metadata">

**Author:** ![Jpalma](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@Jpalma](https://fenicsproject.discourse.group/u/Jpalma)\
**Post date:** [November 24, 2023, 3:03pm UTC](https://fenicsproject.discourse.group/t/problems-implementing-the-linear-elasticity-tutorial-on-google-colab/12843/5 "2023-11-24T15:03:47Z")

</div>

Thanks, that makes it a lot clearer to me!
