# Adaptmesh with a posteriori error

**URL:** https://community.freefem.org/t/adaptmesh-with-a-posteriori-error/2833
**Category:** General Discussion
**Created:** [December 12, 2023, 10:43pm UTC](https://community.freefem.org/t/adaptmesh-with-a-posteriori-error/2833 "2023-12-12T22:43:26Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)
#### Post date: [December 12, 2023, 10:43pm UTC](https://community.freefem.org/t/adaptmesh-with-a-posteriori-error/2833/1 "2023-12-12T22:43:26Z")

</div>

Hey everyone, I’m trying to adapt a mesh regarding the a posteriori error but it seems the mesh is no refined. Could someone tell me what I did wrong in this code

```auto
//RHS 
func f = 8*pi*pi*sin(2*pi*x)*sin(2*pi*y);

//exact solution
func uex = sin(2*pi*x)*sin(2*pi*y);
func dxu = 2*pi*cos(2*pi*x)*sin(2*pi*y);
func dyu = 2*pi*sin(2*pi*x)*cos(2*pi*y);

//f integration order
int forder = 25;

// Mesh
include "getARGV.idp"
int coarseMesh = getARGV("-cM", 0);
mesh Th = square(2^coarseMesh, 2^coarseMesh);

// Macro
macro grad(A) [dx(A), dy(A)] //

// Fespace
func Pk = P1;
real eta = 50;
while(eta > 10){
    fespace Uh(Th, Pk);
    Uh u;

    // Problem
    varf vPoisson (u, uh)
     = int2d(Th)(grad(u)' * grad(uh)) + int2d(Th)(f * uh) + on(1, 2, 3, 4, u=0);
    
    matrix<real> Poisson = vPoisson(Uh, Uh, solver=sparsesolver);
    real[int] PoissonBoundary = vPoisson(0, Uh);
    u[] = Poisson^-1 * PoissonBoundary;

    // Plot
    // plot(u, nbiso=30, fill=true, value=true);

    real L2error = int2d(Th)((uex - u)^2);
    real H1error = L2error + int2d(Th)((dxu - dx(u))^2 + (dyu - dy(u))^2);

    fespace Nh(Th, P0); // the space function constant / triangle
    Nh etak;
    Nh hT = hTriangle;
    varf vetaK (unused, chiK)
      = intalledges(Th)(chiK*lenEdge*square(jump(N.x*dx(u) + N.y*dy(u)))) + int2d(Th)(chiK*square(hTriangle*(f)));
    
    etak[] = vetaK(0, Nh);

    eta = sqrt(etak[].sum);
    real hmesh = hT[].max;
    
    cout << "h - eta - ||u-uh||_1" << "\n";
    cout << hmesh << " " << eta << " " << H1error << "\n";
    
    plot(Th, u, wait=1);

    Th = adaptmesh(Th, eta);
    u = u;

    cout << "h - eta - ||u-uh||_1" << "\n";
    cout << hmesh << " " << eta << " " << H1error << "\n";
    // cout << L2error << "\n";
}

```

---

<div class="post-metadata">

### Author: ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)
#### Post date: [December 13, 2023, 2:41pm UTC](https://community.freefem.org/t/adaptmesh-with-a-posteriori-error/2833/2 "2023-12-13T14:41:10Z")

</div>

See exemple 5.1.9 Adaptation using residual error indicator  
in the doc.
