# Dolfin-adjoint with MultiMesh

**URL:** <https://fenicsproject.discourse.group/t/dolfin-adjoint-with-multimesh/11936>\
**Category:** dolfin-adjoint\
**Created:** [August 10, 2023, 8:36am UTC](https://fenicsproject.discourse.group/t/dolfin-adjoint-with-multimesh/11936 "2023-08-10T08:36:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckrmn](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ckrmn/32/5398_2.png) [@ckrmn](https://fenicsproject.discourse.group/u/ckrmn)\
**Post date:** [August 10, 2023, 8:36am UTC](https://fenicsproject.discourse.group/t/dolfin-adjoint-with-multimesh/11936/1 "2023-08-10T08:36:46Z")

</div>

I’m doing some multimesh shape-optimization and I would like to use/extend `dolfin_adjoint`. The trouble is: it seems that `dolfin`’s `assemble` is overloaded in `dolfin_adjoint`, but `assemble_multimesh` is not. Do you know if there is an example use-case for `dolfin_adjoint` with `MultiMesh`, or is this still not supported as stated [here](https://github.com/dolfin-adjoint/pyadjoint/blob/master/tests/migration/README.md) 5 years ago? Would I have to overload `assemble_multimesh`, along with other multimesh-functions I use, with `pyadjoint` or do you suggest another way?

P.S. [`femorph` seems to not be recommended highly](https://fenicsproject.discourse.group/t/shape-derivatives-in-ufl/3470/10) now, although it was used a bit by Jørgen Dokken in his MultiMeshShapeOpt demo a few years ago.

Helpful thoughts/pointers much appreciated!

---

<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 10, 2023, 8:51am UTC](https://fenicsproject.discourse.group/t/dolfin-adjoint-with-multimesh/11936/2 "2023-08-10T08:51:47Z")

</div>

> [@ckrmn](#):
>
> I’m doing some multimesh shape-optimization and I would like to use/extend `dolfin_adjoint`. The trouble is: it seems that `dolfin`’s `assemble` is overloaded in `dolfin_adjoint`, but `assemble_multimesh` is not. Do you know if there is an example use-case for `dolfin_adjoint` with `MultiMesh`, or is this still not supported as stated [here](https://github.com/dolfin-adjoint/pyadjoint/blob/master/tests/migration/README.md) 5 years ago? Would I have to overload `assemble_multimesh`, along with other multimesh-functions I use, with `pyadjoint` or do you suggest another way?

We never got around to add multimesh to pyadjoint, mostly due to limited time during my PhD.  
Multi-mesh is no longer developed, and since dolfin hasn’t really evolved since 2019 (as development has moved to dolfinx) it is not a priority to work on.

Note that you can use `ufl.derivative` as described in:

> **[Automated shape differentiation in the Unified Form Language - Structural and...](https://link.springer.com/article/10.1007/s00158-019-02281-z)**
>
> We discuss automating the calculation of weak shape derivatives in the Unified Form Language (ACM TOMS 40(2):9:1–9:37 2014) by introducing an appropriate additional step in the pullback from physical to reference space that computes Gâteaux...

ie.

```python
    V = MultiMeshFunctionSpace(multimesh, "Lagrange", 1)
    uh = MultiMeshFunction(V)
    J = uh**2*dX
    Q = MultiMeshVectorFunctionSpace(multimesh, "Lagrange", 1)
    s = MultiMeshFunction(Q)
    x = SpatialCoordinate(multimesh)
    dJdx = derivative(J, x)

```

I think this should give the right result.

---

<div class="post-metadata">

**Author:** ![ckrmn](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ckrmn/32/5398_2.png) [@ckrmn](https://fenicsproject.discourse.group/u/ckrmn)\
**Post date:** [August 10, 2023, 2:19pm UTC](https://fenicsproject.discourse.group/t/dolfin-adjoint-with-multimesh/11936/3 "2023-08-10T14:19:14Z")

</div>

Thanks, Jørgen. The UFL shape-differentiation book seems very helpful so far. Your work on `dolfin`(,`x`), `dolfin_adjoint` and `MultiMesh*` is also very helpful.

Re. using your suggestion for my purpose: it seems that the `derivative` that is imported with

```python
from dolfin import *

```

is a `dolfin.fem.formmanipulations` function. In some cursory tests, this seems to work for me as expected. The `ufl.derivative` seems to be a function from `ufl.formoperators`, and doesn’t work for my present purpose since I use indexed-coefficients. I will have to tinker with both a bit more.

P.S. I reverted to using `dolfin` after starting with `dolfinx` since neither `MultiMesh*` nor `*_adjoint` seems to be (fully) developed in `dolfinx`. I look forward to being able to use both if/when they are developed.

Thanks again!
