# Auto adaptive mesh refinement in Dolfin adjoint?

**URL:** <https://fenicsproject.discourse.group/t/auto-adaptive-mesh-refinement-in-dolfin-adjoint/5807>\
**Category:** mesh\
**Created:** [May 17, 2021, 1:14pm UTC](https://fenicsproject.discourse.group/t/auto-adaptive-mesh-refinement-in-dolfin-adjoint/5807 "2021-05-17T13:14:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mary\_h](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mary_h/32/1943_2.png) [@mary\_h](https://fenicsproject.discourse.group/u/mary_h)\
**Post date:** [May 17, 2021, 1:14pm UTC](https://fenicsproject.discourse.group/t/auto-adaptive-mesh-refinement-in-dolfin-adjoint/5807/1 "2021-05-17T13:14:32Z")

</div>

Hello,

I am trying to find a way of using adaptive mesh refinement in Dolfin-adjoint, but I couldn’t find any resource. Can you please help me with this?  
For example, I have attached the below code, and I want to use auto adaptive mesh refinement on this code:

```
import numpy as np
import matplotlib.pyplot as plt
from dolfin import *
from dolfin_adjoint import *
import moola

n = 10
mesh = RectangleMesh(Point(-1,-1),Point(1,1), n, n)

V = FunctionSpace(mesh, "CG", 1)
u = Function(V)
v = TestFunction(V)
S0 = Constant(1)

bc = DirichletBC(V, 1, "on_boundary")

v = project(u, V, bcs=bc)
J = assemble((0.5*inner(grad(v), grad(v)) - v*S0)*dx)
J_hat = ReducedFunctional(J, Control(u))   
u_opt = minimize(J_hat, method = "L-BFGS-B", options = {"gtol": 1e-16, "ftol": 1e-16})
J_hat(u_opt)

u = project (u_opt, V, bcs=bc)
fileU = File("temp.pvd");
fileU << u

```

Thank you so much for your help!

---

<div class="post-metadata">

**Author:** ![mary\_h](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mary_h/32/1943_2.png) [@mary\_h](https://fenicsproject.discourse.group/u/mary_h)\
**Post date:** [May 24, 2021, 2:52pm UTC](https://fenicsproject.discourse.group/t/auto-adaptive-mesh-refinement-in-dolfin-adjoint/5807/2 "2021-05-24T14:52:54Z")

</div>

Hello everyone,

I wanted to follow up on my question. Also, I wanted to ask if it is possible to use auto-adaptive mesh refinement in Dolfin-adjoint?

Thank you very much!
