# Error when implementing Subdomain Tutorial on CoLab

**URL:** <https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389>\
**Category:** dolfinx\
**Tags:** dolfinx\
**Created:** [April 23, 2024, 1:09am UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389 "2024-04-23T01:09:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BoheT](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@BoheT](https://fenicsproject.discourse.group/u/BoheT)\
**Post date:** [April 23, 2024, 1:09am UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/1 "2024-04-23T01:09:33Z")

</div>

Hi,

When I tried to implement the **Subdomain on built-in meshes Tutorial** demo on CoLab following [Defining subdomains for different materials — FEniCSx tutorial](https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html), the following error occurs becasue of the failure of kappa \* grad(u). kappa is a vector and grad(u) is a matrix here. Any idea here how to solve this problem? Thanks!

```auto
from dolfinx import default_scalar_type
from dolfinx.fem import (Constant, dirichletbc, Function, FunctionSpace, assemble_scalar,
                         form, locate_dofs_geometrical, locate_dofs_topological)
from dolfinx.fem.petsc import LinearProblem
from dolfinx.io import XDMFFile, gmshio
from dolfinx.mesh import create_unit_square, locate_entities
from dolfinx.plot import vtk_mesh

from ufl import (SpatialCoordinate, TestFunction, TrialFunction,
                 dx, grad, inner)

from mpi4py import MPI

import meshio
import gmsh
import numpy as np
import pyvista

pyvista.start_xvfb()
mesh = create_unit_square(MPI.COMM_WORLD, 10, 10)
Q = dolfinx.fem.functionspace(mesh, ("DG", 0, (mesh.geometry.dim,)))

def Omega_0(x):
    return x[1] <= 0.5

def Omega_1(x):
    return x[1] >= 0.5

kappa = Function(Q)
cells_0 = locate_entities(mesh, mesh.topology.dim, Omega_0)
cells_1 = locate_entities(mesh, mesh.topology.dim, Omega_1)
kappa.x.array[cells_0] = np.full_like(cells_0, 1, dtype=default_scalar_type)
kappa.x.array[cells_1] = np.full_like(cells_1, 0.1, dtype=default_scalar_type)

V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))
u, v = TrialFunction(V), TestFunction(V)
a = inner(kappa * grad(u), grad(v)) * dx
# x = SpatialCoordinate(mesh)
# L = Constant(mesh, default_scalar_type(1)) * v * dx
# dofs = locate_dofs_geometrical(V, lambda x: np.isclose(x[0], 0))
# bcs = [dirichletbc(default_scalar_type(1), dofs, V)]

```

The error information is

```auto
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-b35bd3cd4c0d> in <cell line: 40>()
     38 V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))
     39 u, v = TrialFunction(V), TestFunction(V)
---> 40 a = inner(kappa * grad(u), grad(v)) * dx
     41 # x = SpatialCoordinate(mesh)
     42 # L = Constant(mesh, default_scalar_type(1)) * v * dx

1 frames
/usr/local/lib/python3.10/dist-packages/ufl/exproperators.py in _mult(a, b)
    157 
    158 else:
--> 159 raise ValueError(f"Invalid ranks {r1} and {r2} in product.")
    160 
    161 # TODO: I think applying as_tensor after index sums results in

ValueError: Invalid ranks 1 and 2 in product.

```

---

<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 23, 2024, 5:51am UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/2 "2024-04-23T05:51:45Z")

</div>

> [@BoheT](#):
>
> ```auto
> Q = dolfinx.fem.functionspace(mesh, ("DG", 0, (mesh.geometry.dim,)))
> 
> def Omega_0(x):
> 
> ```

Here you define Q as a vector space, then

> [@BoheT](#):
>
> `kappa = Function(Q)`

and

> [@BoheT](#):
>
> `a = inner(kappa * grad(u), grad(v))`

Does not make sende as the first product is a vector, while the second is matrix

---

<div class="post-metadata">

**Author:** ![BoheT](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@BoheT](https://fenicsproject.discourse.group/u/BoheT)\
**Post date:** [April 23, 2024, 8:12pm UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/3 "2024-04-23T20:12:26Z")

</div>

Thank you for replying!

I am trying to learn how to set up subdomains with different kappa, so I followed the Subdomain Demo [Defining subdomains for different materials — FEniCSx tutorial](https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html), and want to set up different kappa for different domains. Every code is the same with the Demo, except for

```auto
Q = dolfinx.fem.functionspace(mesh, ("DG", 0, (mesh.geometry.dim,)))

```

and

```auto
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))

```

In the Demo, they are

```auto
Q = FunctionSpace(mesh, ("DG", 0))

```

and

```auto
V = FunctionSpace(mesh, ("Lagrange", 1))

```

But another error would occur if I used the code shown in the Demo. I thought it was becasue of the update of FuncationSpace. Anything else is the same. Then I was not sure how to set up different kappa for different domains.

I saw another way [https://fenicsproject.org/pub/tutorial/sphinx1/.\_ftut1005.html](https://fenicsproject.org/pub/tutorial/sphinx1/._ftut1005.html) in FeniCs instead of FeniCSx is through Expression.

```auto
class K(Expression):
    def __init__ (self, materials, k_0, k_1, **kwargs):
        self.materials = materials
        self.k_0 = k_0
        self.k_1 = k_1

def eval_cell(self, values, x, cell):
        if self.materials[cell.index] == 0:
            values[0] = self.k_0
        else:
            values[0] = self.k_1

kappa = K(materials, k_0, k_1, degree=0)

```

I can try this to see if this will work. But it would be great if you may have other suggestions.

Thank 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:** [April 23, 2024, 8:33pm UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/4 "2024-04-23T20:33:47Z")

</div>

> [@BoheT](#):
>
> [Defining subdomains for different materials — FEniCSx tutorial](https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html), and want to set up different kappa for different domains. Every code is the same with the Demo, except for
> 
> ```auto
> Q = dolfinx.fem.functionspace(mesh, ("DG", 0, (mesh.geometry.dim,)))
> 
> ```
> 
> and
> 
> ```auto
> V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))
> 
> ```
> 
> In the Demo, they are
> 
> ```auto
> Q = FunctionSpace(mesh, ("DG", 0))
> 
> ```

Should kappa still be a constant per cell (But unique per cell) Or what does kappa signify in your code?

---

<div class="post-metadata">

**Author:** ![BoheT](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@BoheT](https://fenicsproject.discourse.group/u/BoheT)\
**Post date:** [April 23, 2024, 9:07pm UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/5 "2024-04-23T21:07:45Z")

</div>

Thank you for your quick reply! kappa is a constant per cell, i.e. for one domain kappa is value\_1 and for another domain kappa is value\_2.

Let’s say the conductivity is different within different domains. So for one domain, the governing equation is  
\partial T / \partial t = kappa\_1 \* diffusion term,  
For another subdomain, the governing equation is  
\partial T / \partial t = kappa\_2 \* diffusion term.

And FYI, the interfaces between two subdomains will have a Neumann boundary condition. But this will be implemented later, it is not related to the question here.

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:** [April 23, 2024, 9:36pm UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/6 "2024-04-23T21:36:19Z")

</div>

> [@BoheT](#):
>
> Thank you for your quick reply! kappa is a constant per cell, i.e. for one domain kappa is value\_1 and for another domain kappa is value\_2.

Then just use kappa as done in the tutorial.

---

<div class="post-metadata">

**Author:** ![BoheT](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@BoheT](https://fenicsproject.discourse.group/u/BoheT)\
**Post date:** [April 23, 2024, 11:43pm UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/7 "2024-04-23T23:43:04Z")

</div>

That was what I did before.

If I copied and pasted the tutorial code, then I will get the error shown below,

```auto
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-1195952df25b> in <cell line: 22>()
     20 
     21 mesh = create_unit_square(MPI.COMM_WORLD, 10, 10)
---> 22 Q = FunctionSpace(mesh, ("DG", 0))
     23 
     24 def Omega_0(x):

TypeError: FunctionSpace. __init__ () missing 1 required positional argument: 'cppV'

```

If I copied and pasted the tutorial code but used “Q = dolfinx.fem.functionspace(mesh, (“DG”, 0, (mesh.geometry.dim,)))” instead, then I will get the error shown in my first message,

```auto
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-6c2490af87d4> in <cell line: 46>()
     44 V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))
     45 u, v = TrialFunction(V), TestFunction(V)
---> 46 kappa * grad(u)
     47 a = inner(kappa * grad(u), grad(v)) * dx
     48 # x = SpatialCoordinate(mesh)

1 frames
/usr/local/lib/python3.10/dist-packages/ufl/exproperators.py in _mult(a, b)
    157 
    158 else:
--> 159 raise ValueError(f"Invalid ranks {r1} and {r2} in product.")
    160 
    161 # TODO: I think applying as_tensor after index sums results in

ValueError: Invalid ranks 1 and 2 in product.

```

I guess my question is I cannot run the tutorial demo, but I thought it should work because I copied and pasted the tutorial demo. Is it possible it is because I am using FeniCSx on Colab?

I copied and pasted the demo in this one [Defining subdomains for different materials — FEniCSx tutorial](https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html).

---

<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 24, 2024, 4:57am UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/8 "2024-04-24T04:57:04Z")

</div>

You are using the main branch of DOLFINx, that has some API changes. The tutorial reflects the latest stable release (0.7.x).

You should use

```auto
import dolfinx.fem
Q = fem.functionspace(mesh, ("DG", 0))

```

Most changes relating to API changes are listed at

> <https://github.com/jorgensd/dolfinx-tutorial/blob/a36ae80c0d9c4e6b30462132686d8c2e90b6dbc5/Changelog.md>

---

<div class="post-metadata">

**Author:** ![BoheT](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@BoheT](https://fenicsproject.discourse.group/u/BoheT)\
**Post date:** [April 24, 2024, 11:22pm UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/9 "2024-04-24T23:22:33Z")

</div>

I see. Thank you so much for this!

---

<div class="post-metadata">

**Author:** ![BoheT](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@BoheT](https://fenicsproject.discourse.group/u/BoheT)\
**Post date:** [April 24, 2024, 11:37pm UTC](https://fenicsproject.discourse.group/t/error-when-implementing-subdomain-tutorial-on-colab/14389/10 "2024-04-24T23:37:13Z")

</div>

It works now on Colab! Thank you!
