# untimeError: Unable to cast Python instance to C++ type?

**URL:** <https://fenicsproject.discourse.group/t/untimeerror-unable-to-cast-python-instance-to-c-type/10120>\
**Category:** General\
**Created:** [January 10, 2023, 6:10am UTC](https://fenicsproject.discourse.group/t/untimeerror-unable-to-cast-python-instance-to-c-type/10120 "2023-01-10T06:10:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muneef321](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Muneef321](https://fenicsproject.discourse.group/u/Muneef321)\
**Post date:** [January 10, 2023, 6:10am UTC](https://fenicsproject.discourse.group/t/untimeerror-unable-to-cast-python-instance-to-c-type/10120/1 "2023-01-10T06:10:19Z")

</div>

from dolfin import \*

# Create classes for defining parts of the boundaries and the interior

# of the domain

class Left(SubDomain):  
def inside(self, x, on\_boundary):  
tol= 1E-15  
return near(x[0], 0.0,tol)

class Right(SubDomain):  
def inside(self, x, on\_boundary):  
tol= 1E-15  
return near(x[0], 1.0,tol)

class Bottom(SubDomain):  
def inside(self, x, on\_boundary):  
tol= 1E-15  
return near(x[2], 0.0,tol)

class Top(SubDomain):  
def inside(self, x, on\_boundary):  
tol= 1E-15  
return near(x[2], 1.0,tol)

class Outside(SubDomain):  
def inside(self, x, on\_boundary):  
tol= 1E-15  
return near(x[1],1.0,tol)  
class Inside(SubDomain):  
def inside(self, x, on\_boundary):  
tol= 1E-15  
return near(x[1],0.0,tol)

class Subdomain1(SubDomain):  
def inside(self, x, on\_boundary):  
return (between(x[0], (0.2,0.5)) , between(x[1], (0.4, 0.5)) and between(x[2],(0.7,0.9)))

# Initialize sub-domain instances

left = Left()  
top = Top()  
right = Right()  
bottom = Bottom()  
inside = Inside()  
outside = Outside()  
subdomain1 = Subdomain1()

# Define mesh

mesh = UnitCubeMesh(15,15,15)

# Initialize mesh function for interior domains

subdomains = MeshFunction(‘size\_t’,mesh,mesh.topology().dim())  
subdomains.set\_all(0)  
subdomain1.mark(subdomains,1) #IN THIS LINE IT IS SHOWING ERROR

# Initialize mesh function for boundary domains

boundaries = MeshFunction(‘size\_t’,mesh,mesh.topology().dim()-1)

boundaries.set\_all(0)  
left.mark(boundaries, 1)  
top.mark(boundaries, 2)  
right.mark(boundaries, 3)  
bottom.mark(boundaries, 4)  
outside.mark(boundaries, 5)  
inside.mark(boundaries, 6)

# Define input data

a0 = Constant(1.0)  
a1 = Constant(0.01)  
g\_L = Expression(“- 10\*exp(- pow(x[1] - 0.5, 2))”,degree=2)  
g\_R = Constant(“1.0”)  
f = Constant(1.0)  
u\_n = Constant(0.0)  
dt= 0.1  
u\_0 = Constant(0.0)

# Define function space and basis functions

V = FunctionSpace(mesh, “CG”, 2)  
u = TrialFunction(V)  
v = TestFunction(V)

# Define Dirichlet boundary conditions at top and bottom boundaries

bcs = [DirichletBC(V, 5.0, boundaries, 2),DirichletBC(V, 5.0, boundaries, 4),DirichletBC(V, 5.0, boundaries, 5),  
DirichletBC(V, 0.0, boundaries, 6)]

#interpolate  
u\_n = interpolate(u\_0, V)

# Define new measures associated with the interior domains and

# exterior boundaries

ds = Measure(‘ds’, domain=mesh, subdomain\_data=boundaries)  
dx = Measure(‘dx’, domain=mesh,subdomain\_data=domains)

# Define variational form

a = a0_u_v_dx(0) + a1_u_v_dx(1) + a0_dt_dot(grad(u),grad(v))_dx(0) +a1_dt_dot(grad(u),grad(v))dx(1)  
L = a0(u\_n + dt_f)_v_dx(0) + a1\*(u\_n + dt_f)vdx(1) + g\_L_v_ds(1) + g\_R_v\*ds(3)

# Solve problem

u = Function(V)  
file =File(“dtd.pvd”)  
t = 0

T=50\*dt

while (t\<T):  
# Update current time  
t += dt

```
# Compute solution

solve(a == L, u, bcs)

# Update previous solution
u_n.assign(u)

file << (u,t)

```

---

<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:** [January 10, 2023, 6:40am UTC](https://fenicsproject.discourse.group/t/untimeerror-unable-to-cast-python-instance-to-c-type/10120/2 "2023-01-10T06:40:46Z")

</div>

Please encapsulate the code with 3x` as

````python
```python
# insert code here
```

````

Secondly, make sure that the code is properly indented and is possible to execute after copy-pasting it.

Finally, remove all code that should be executed after the error occurs, and make sure to post the full error message.

---

<div class="post-metadata">

**Author:** ![Muneef321](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@Muneef321](https://fenicsproject.discourse.group/u/Muneef321)\
**Post date:** [January 11, 2023, 6:19am UTC](https://fenicsproject.discourse.group/t/untimeerror-unable-to-cast-python-instance-to-c-type/10120/4 "2023-01-11T06:19:19Z")

</div>

Error:  
traceback (most recent call last):  
File “/home/FENICS/Help/dfn\_sbdmns-mltbdrs.py”, line 55, in   
subdomain1.mark(subdomains,1)  
RuntimeError: Unable to cast Python instance to C++ type (#define PYBIND11\_DETAILED\_ERROR\_MESSAGES or compile in debug mode for details)
