# How to create a ds only on the upper surface in fenicsx

**URL:** <https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021>\
**Category:** General\
**Created:** [February 18, 2025, 2:14pm UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021 "2025-02-18T14:14:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@Ian](https://fenicsproject.discourse.group/u/Ian)\
**Post date:** [February 18, 2025, 2:14pm UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021/1 "2025-02-18T14:14:09Z")

</div>

Hi, I have a physical model in a 3D domain(cube for temporarily).  
I am using fenicsx now, and in the model, there is a laser shining on the upper surface. So the weak form has a term like:

```auto
F=....*dx+lasersource*ds

```

Due to the physical model, I only want the integrate term `lasersource*ds` established on the upper surface. What should I do?🙂

---

<div class="post-metadata">

**Author:** ![Stein](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/stein/32/8751_2.png) [@Stein](https://fenicsproject.discourse.group/u/Stein)\
**Post date:** [February 18, 2025, 2:19pm UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021/2 "2025-02-18T14:19:34Z")

</div>

You’ll have to create a `meshtags` object where you tag that surface. Then you create a surface measure with that meshtags object attached:

```auto
ds = ufl.Measure('ds', domain=mesh, subdomain_data=surface_meshtags)

```

In your weak form, you’d use:

```auto
lasersource*testfunction*ds(1)

```

Assuming you’ve tagged that surface with value `1`.

See this post from two hours ago 😉

> [@Eadg integral on part of the boundary](https://fenicsproject.discourse.group/t/eadg-integral-on-part-of-the-boundary/17020/6):
>
> It should be like marker\_value = 33 inner\_facets = mesh.locate\_entities\_boundary(msh, 1, boundary\_marker) facet\_tags = mesh.meshtags(msh, 1, inner\_facets, np.full(len(inner\_facets), marker\_value, dtype=np.int32)) ds = ufl.Measure("ds", domain=msh, subdomain\_data=facet\_tags, subdomain\_id=marker\_value)

---

<div class="post-metadata">

**Author:** ![Ian](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@Ian](https://fenicsproject.discourse.group/u/Ian)\
**Post date:** [February 19, 2025, 11:56am UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021/3 "2025-02-19T11:56:58Z")

</div>

Thanks for your advice!  
Did you mean this?

```auto
def top_surface(x):
    return np.isclose(x[2], 1, atol=1e-8)
top_facets = mesh.locate_entities_boundary(domain, domain.topology.dim - 1, top_surface)
facet_tags = mesh.meshtags(domain, 1, top_facets, np.full(len(top_facets), marker_value, dtype=np.int32))
ds = ufl.Measure("ds", domain=domain, subdomain_data=facet_tags)

```

And for the integrate term

```auto
F_heat=×××*dx-(nu * e **2*100 / (2 * m_e * omega_p** 2)) * laser_source * v_T * ds(1))

```

However, there is a value called `marker_value`, I don’t know the meaning of this . Besides, do you know how to tag the upper surface with value `1`? 😖

---

<div class="post-metadata">

**Author:** ![Ian](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@Ian](https://fenicsproject.discourse.group/u/Ian)\
**Post date:** [February 19, 2025, 11:59am UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021/4 "2025-02-19T11:59:51Z")

</div>

oh,should I let marker\_value=1?

---

<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 19, 2025, 12:02pm UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021/5 "2025-02-19T12:02:09Z")

</div>

A mesh-tag can contain multiple entities, with different values.  
In the integration, by choosing a specific value, say `1`, you integrate over all entities in the mesh-tag whose corresponding value is `1`.

---

<div class="post-metadata">

**Author:** ![Ian](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@Ian](https://fenicsproject.discourse.group/u/Ian)\
**Post date:** [February 19, 2025, 12:15pm UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021/6 "2025-02-19T12:15:14Z")

</div>

Thank you, I think I get it.  
So I think maybe I can integrate in the upper surface by using this?

```auto
def top_surface(x):
    return np.isclose(x[2], 1, atol=1e-8)
marker_value=1
top_facets = mesh.locate_entities_boundary(domain, domain.topology.dim - 1, top_surface)
facet_tags = mesh.meshtags(domain, 2, top_facets, np.full(len(top_facets), marker_value, dtype=np.int32))
ds = ufl.Measure("ds", domain=domain, subdomain_data=facet_tags)
F=lasersource*ds(1)

```

---

<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 19, 2025, 12:30pm UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021/7 "2025-02-19T12:30:56Z")

</div>

> [@Ian](#):
>
> ```auto
> def top_surface(x):
> return np.isclose(x[2], 1, atol=1e-8)
> marker_value=1
> top_facets = mesh.locate_entities_boundary(domain, domain.topology.dim - 1, top_surface)
> facet_tags = mesh.meshtags(domain, 2, top_facets, np.full(len(top_facets), marker_value, dtype=np.int32))
> ds = ufl.Measure("ds", domain=domain, subdomain_data=facet_tags)
> F=lasersource*ds(1)
> 
> ```

Yes. But i would be consistent, use marker\_value throughout, i.e. including `ds(marker_value)`, in case you change it to something else than 1 at a later instance.

---

<div class="post-metadata">

**Author:** ![Ian](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@Ian](https://fenicsproject.discourse.group/u/Ian)\
**Post date:** [February 19, 2025, 12:34pm UTC](https://fenicsproject.discourse.group/t/how-to-create-a-ds-only-on-the-upper-surface-in-fenicsx/17021/8 "2025-02-19T12:34:16Z")

</div>

I get it, thank you for your advice! 😃
