# Code fails when mesh adaptation is incorperated into a code formulated with the 'varf' keyword

**URL:** https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000
**Category:** General Discussion
**Created:** [February 26, 2024, 6:10pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000 "2024-02-26T18:10:22Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [February 26, 2024, 6:10pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/1 "2024-02-26T18:10:22Z")

</div>

Hi all, I’m currently trying to solve a toy problem using the `varf` keyword. So far, I have been able to solve the problem without mesh adaptation using both the `problem` keyword and `varf` keyword, and I have also been able to solve the problem with mesh adaptation using the `problem` keyword. However, issues arise when I try to introduce the same mesh adaptation to the code formulated using the `varf` keyword; the exact error message that I recieve is dependent upon which solver/preconditioner I am using. If I use `UMFPACK64` with `sparsesolver`, I recieve no error message at all and the code simply terminates itself upon reaching the first mesh adaptation. Alternatively, if I use `PETSc` with `"-pc_type gamg" `, the code teminates in the same place with the following error code in the terminal:

```auto
[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see https://petsc.org/release/faq/#valgrind and https://petsc.org/release/faq/
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run
[0]PETSC ERROR: to get more information on the crash.
[0]PETSC ERROR: Run with -malloc_debug to check if memory corruption is causing the crash.

```

I don’t understand how this could be a memory related issue for such a simple PDE system. Can anyone help me with this problem? Perhaps I’ve just written my code incorrectly. Many thanks!

My code formulated using the `problem` keyword (with mesh adaptation):  
[SimplerProblemLinearCoupled\_problem.edp](https://community.freefem.org/uploads/short-url/paU8SeTW7pDDI7Y2jhC2h2mSJlA.edp) (2.2 KB)

My code formulated using the `varf` keyword (without mesh adaptation):  
[SimplerProblemLinearCoupled\_varf.edp](https://community.freefem.org/uploads/short-url/7AydkqvRqpLkNWd3o4sziRc2HUt.edp) (2.6 KB)

My code formulated using the `varf` keyword (with mesh adaptation):  
[SimplerProblemLinearCoupled\_varf\_adaptive.edp](https://community.freefem.org/uploads/short-url/2O5nHwwyw4dfDeejdJJ8wKyRAWw.edp) (2.9 KB)

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [February 26, 2024, 7:47pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/2 "2024-02-26T19:47:49Z")

</div>

If you don’t know how to do mesh adaptation with PETSc, you can have a look at this example: [FreeFem-sources/examples/hpddm/laplace-adapt-3d-PETSc.edp at develop · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/laplace-adapt-3d-PETSc.edp).

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [February 26, 2024, 7:50pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/3 "2024-02-26T19:50:28Z")

</div>

My guess is that you are not properly resizing the `Mat` after mesh adaptation, thus the error. See the above example for how to fix this.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [February 26, 2024, 8:54pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/4 "2024-02-26T20:54:43Z")

</div>

Hi, thanks for the reply. The example you’ve provided looks quite different to my own code, I’m not particularly famililar with the methods presented but I will take a look. Can you see any obvious mistakes in my code or is it just not possible to use the `adaptmesh( )` function in a code formulated with the `varf` keyword? Also, I use `matrix`, not `Mat`; is this a problem?

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [February 26, 2024, 8:58pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/5 "2024-02-26T20:58:25Z")

</div>

One additional point, for some reason it occasionally gives the following error: `Assertion fail : (mu>0 && y.N()%mu==0)`. Not sure if this is a clue at all.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [February 26, 2024, 9:31pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/6 "2024-02-26T21:31:58Z")

</div>

Sorry, I thought you had a question specific to PETSc, but it appears you have the error even in sequential. Have you run the code with debugging turned on?

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [February 26, 2024, 11:56pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/7 "2024-02-26T23:56:29Z")

</div>

The immediate problem is a size problem.  
If you don’t set the size of aux and just define it on first assignment  
the code will run, see the changes here, but seg faults later.  
IIRC I had some issue earlier running out of memory using regions or  
something but there are probably quirks in the mesh adaptation  
that may not work indefinitely.

[SimplerProblemLinearCoupled\_varf\_adaptive.edp](https://community.freefem.org/uploads/short-url/qHAyFMIvRfNMR5rNU2TyhDjPOSy.edp) (3.1 KB)

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [February 27, 2024, 10:02am UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/8 "2024-02-27T10:02:58Z")

</div>

Hi prj, yes exactly, it doesn’t even work in sequential unforrtunately. I’m using VScode with a FreeFem extension, which sadly does not feature a debugger. Do you know of a way to debug FreeFem code?

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [February 27, 2024, 10:09am UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/9 "2024-02-27T10:09:00Z")

</div>

Hi Mike, thanks for the reply! Your adjustment almost does the trick, however it appears that it is also necessary to resize `sol` and `solold`. When I print out the sizes of `sol` and `solold`, they each remain at 6200 while the `aux`, `b` and `Xh` all change with the mesh adaptation. Any ideas of how to resize `sol` and `solold`? It doesn’t seem to work when I try to resize them in the same in which `aux`, `b` and `Xh` are resized.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [February 27, 2024, 10:39am UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/10 "2024-02-27T10:39:11Z")

</div>

Just `sol = 0; solold = 0;` will resize both functions.

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [February 27, 2024, 12:05pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/11 "2024-02-27T12:05:36Z")

</div>

IIRC when I ran into a problem it was because adapting a mesh with regions  
had a memory leak but i just used indicator variable instead.  
That probably is not relevant here.

I hesitated to go further with size issues since I couldn’t remember what does what …  
I think there may have also been an issue with number of dofs and array asize  
depending on the problem. Some values of tgv are supposed to eliminate  
fixed equations so I would imagine the A and rhs shrink.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [February 27, 2024, 4:45pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/12 "2024-02-27T16:45:37Z")

</div>

Hi Mike, you might’ve given me enough info to work with. I’ll update this thread if I get anywhere. Many thanks to you and prj for your input! And if anything occurs to you, do let me know.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [February 28, 2024, 11:50am UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/13 "2024-02-28T11:50:30Z")

</div>

Hi, I seem to have sorted the issue; now the code works with mesh adaptation while using the `varf` keyword. Here’s the code that seems to work:  
[SimplerProblemLinearCoupled\_varf\_adaptive\_fixed.edp](https://community.freefem.org/uploads/short-url/8XvouSTCoaHd4BOMbQsM68J7u3z.edp) (3.1 KB)

However, I’ve unearthed an unexpected problem and that is the question of how to resize the value of the unknowns at the previous timestep (`uu1` and `uu2`) to the newly adapted mesh. I’ve never given this much thought and always assumed that the proper technique was to simply include the following lines of code just after calling the `adaptmesh( )` function:

```auto
  uu1=uu1;     
  uu2=uu2;

```

But, what I’ve noticed is that the inclusion of this line of code causes signifcant changes to `uu1` and `uu2` in subsequent mesh adaptations during a given timestep, which doesn’t seem right. Also, the solution obtained when these lines of codes are included is quite different to the solution obtained when they are absent. My question simply is, what is the correct procedure here? I’ve attached the code formulated with the `problem` keyword below. On lines 111-120 I’ve inlcuded several different possible lines of code (options A to D), one of which may be the correct procedure. Maybe someone can highlight which option is correct or maybe someone can suggest a different option entirely?  
[SimplerProblemLinearCoupled\_problem\_adaptive.edp](https://community.freefem.org/uploads/short-url/enEF4QLpTAZ2D8hS36NFtpOjcMH.edp) (2.9 KB)

Many thanks for the help.

---

<div class="post-metadata">

### Author: ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)
#### Post date: [February 28, 2024, 2:42pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/14 "2024-02-28T14:42:07Z")

</div>

I remember originally I tried to do all of that manually with interpolate but it looks  
like assigning on fespace to the other does all of that. if you add the brackets I think  
that refers to the underlying array which does not know about the mesh.  
The semantics take a little getting used to. I’ve tried to write some small specialized “almost” languages  
and I understand from the author’s POV how these things happen but it can be confusing  
to learn as a user lol.

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [February 28, 2024, 2:54pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/15 "2024-02-28T14:54:23Z")

</div>

Hi Mike, yes it’s certainly a bit confusing! So, which of the methods (options A-D) do you think is most appropriate? Are you suggesting that I use option A?

---

<div class="post-metadata">

### Author: ![jna1g18](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@jna1g18](https://community.freefem.org/u/jna1g18)
#### Post date: [March 5, 2024, 4:39pm UTC](https://community.freefem.org/t/code-fails-when-mesh-adaptation-is-incorperated-into-a-code-formulated-with-the-varf-keyword/3000/16 "2024-03-05T16:39:29Z")

</div>

Hi Mike, did you have any thoughts on my question?
