# Invalid value encountered in interpolate

**URL:** <https://fenicsproject.discourse.group/t/invalid-value-encountered-in-interpolate/12054>\
**Category:** dolfinx\
**Created:** [August 28, 2023, 9:15am UTC](https://fenicsproject.discourse.group/t/invalid-value-encountered-in-interpolate/12054 "2023-08-28T09:15:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wwshunan](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wwshunan/32/852_2.png) [@wwshunan](https://fenicsproject.discourse.group/u/wwshunan)\
**Post date:** [August 28, 2023, 9:15am UTC](https://fenicsproject.discourse.group/t/invalid-value-encountered-in-interpolate/12054/1 "2023-08-28T09:15:45Z")

</div>

Hi, I have a dirichlet boundary function in which a divide exists. The division is valid on the boundary but not in the whole domain. This is the warning:

```auto
RuntimeWarning: invalid value encountered in divide
  theta = np.arctan(x[1] / x[0])

```

---

<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:** [August 28, 2023, 10:04am UTC](https://fenicsproject.discourse.group/t/invalid-value-encountered-in-interpolate/12054/2 "2023-08-28T10:04:33Z")

</div>

Use the `cells` input to interpolate to limit the cells you are using interpolation on

---

<div class="post-metadata">

**Author:** ![wwshunan](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wwshunan/32/852_2.png) [@wwshunan](https://fenicsproject.discourse.group/u/wwshunan)\
**Post date:** [August 29, 2023, 4:01am UTC](https://fenicsproject.discourse.group/t/invalid-value-encountered-in-interpolate/12054/3 "2023-08-29T04:01:11Z")

</div>

Dear dokken,  
My code is

```auto
x0 = 23.43211

y0 = 5.19

alpha = np.arctan(y0 / x0)

beta = np.arctan(x0 / y0)

def out_boundary(x):

    theta = np.arctan(x[1] / x[0])

    return 0.13395 - (theta - alpha) / (beta - alpha) * 0.2679

mesh_comm = MPI.COMM_WORLD

gdim = 3

gmsh_model_rank = 0

domain, cell_markers, facet_markers = gmshio.read_from_msh('pole-FEMMeshGmsh.msh', mesh_comm, rank=gmsh_model_rank, gdim=gdim)

#with XDMFFile(MPI.COMM_WORLD, "rfq_out.xdmf", "w") as xdmf:

# xdmf.write_mesh(domain)

# xdmf.write_meshtags(facet_markers)

V = FunctionSpace(domain, ("CG", 2))

u = TrialFunction(V)

v = TestFunction(V)

a = dot(grad(u), grad(v)) * dx

L = Constant(domain, ScalarType(0)) * v * dx

neg_pole_facets = facet_markers.indices[facet_markers.values == 27]

neg_pole_bdofs = locate_dofs_topological(V, 2, neg_pole_facets)

pos_pole_facets = facet_markers.indices[facet_markers.values == 26]

pos_pole_bdofs = locate_dofs_topological(V, 2, pos_pole_facets)

print(neg_pole_bdofs, pos_pole_bdofs)

out_facets = facet_markers.indices[facet_markers.values == 25]

out_bdofs = locate_dofs_topological(V, 2, out_facets)

u_O = Function(V)

u_O.interpolate(out_boundary)

```

u\_O is the function I want to interpolate on the out\_facets. Then how to modify the last line in my code? 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:** [August 29, 2023, 6:41am UTC](https://fenicsproject.discourse.group/t/invalid-value-encountered-in-interpolate/12054/4 "2023-08-29T06:41:31Z")

</div>

Please make a minimal reproducible example next time (using a built in mesh).  
Consider the following:

```python
import dolfinx

from mpi4py import MPI
import numpy as np

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V = dolfinx.fem.FunctionSpace(mesh, ("Lagrange", 2))
u = dolfinx.fem.Function(V)

def f(x):
    return x[1]/x[0]

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

mesh.topology.create_connectivity(mesh.topology.dim-1, mesh.topology.dim)
left_facets = dolfinx.mesh.locate_entities(mesh, mesh.topology.dim-1, left_facet_locator)

cells = dolfinx.mesh.compute_incident_entities(mesh, left_facets, mesh.topology.dim-1, mesh.topology.dim)
u.interpolate(f, cells)

with dolfinx.io.VTXWriter(mesh.comm, "u.bp", [u]) as vtx:
    vtx.write(0.0)

```

returning

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/a/a0a4e2cb4678b29d0ae7c8828e2f2e8ed2e223d2.png)

---

<div class="post-metadata">

**Author:** ![wwshunan](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/wwshunan/32/852_2.png) [@wwshunan](https://fenicsproject.discourse.group/u/wwshunan)\
**Post date:** [August 29, 2023, 6:53am UTC](https://fenicsproject.discourse.group/t/invalid-value-encountered-in-interpolate/12054/5 "2023-08-29T06:53:37Z")

</div>

Thank you very much. 😃
