# How to reset a metric to cleared state?

**URL:** <https://forum.mmgtools.org/t/how-to-reset-a-metric-to-cleared-state/442>\
**Category:** Developers Help\
**Created:** [May 26, 2020, 10:51am UTC](https://forum.mmgtools.org/t/how-to-reset-a-metric-to-cleared-state/442 "2020-05-26T10:51:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathias-Fuchs](https://forum.mmgtools.org/letter_avatar_proxy/v4/letter/m/c77e96/32.png) [@Mathias-Fuchs](https://forum.mmgtools.org/u/Mathias-Fuchs)\
**Post date:** [May 26, 2020, 10:51am UTC](https://forum.mmgtools.org/t/how-to-reset-a-metric-to-cleared-state/442/1 "2020-05-26T10:51:02Z")

</div>

Dear forum, in general, the function

> MMG2D\_mmg2dlib(mesh, met)

is not idempotent: calling it repeatedly gives different results than calling it once. Regardless of whether it makes sens to call it repeatedly, I wondered how _not_ to make use of the metric computed in the first call.  
If met-\>np was zero before the first call, it is !=0 after the first call. So, I wondered how to reset it to a state with met-\>np == 0. Just setting met-\>np = 0 works but it is not an API function (and maybe it leads to a memory leak somewhere). On the other hand, freeing the solution with the API function

> MMG2D\_Free\_solutions(mesh, met)

between the calls does not work: it gives the message ##ERROR: WRONG DATA TYPE in the second call.  
I feel there should be an API function to clear a solution/metric since I don’t want to touch the internals of mmg because they can change in the future. What API function should I use to reset a metric/solution to the state it was left after a call to

> MMG2D\_Init\_mesh(MMG5\_ARG\_start, MMG5\_ARG\_ppMesh, &mesh, MMG5\_ARG\_ppMet, &met, MG5\_ARG\_end

?  
Thank you.

---

<div class="post-metadata">

**Author:** ![Algiane](https://forum.mmgtools.org/user_avatar/forum.mmgtools.org/algiane/32/30_2.png) [@Algiane](https://forum.mmgtools.org/u/Algiane)\
**Post date:** [May 28, 2020, 9:47am UTC](https://forum.mmgtools.org/t/how-to-reset-a-metric-to-cleared-state/442/2 "2020-05-28T09:47:56Z")

</div>

Hi Mathias,

Most of time, people uses iterative call of `MMG2D_mmg2dlib` in the context of the mesh adaptation to a solution. In this case they provide a metric computed from the solution between each call of mmg so it works without any issue.

In your case, I totally agree that touching the internal structure of Mmg is not a good idea and that I should provide APIs to reset it. I have tried to patch the error in a such way that after the call to `MMG2D_Free_solutions` :

- you can call `MMG2D_mmg2dlib` without error (the structure is in a state compatible with the state it had after the `MMG2D_Init_mesh` call);
- you can use `MMG2D_Set_solSize` and `MMG2D_Set_.*Sol` to set a new metric;
- I hope that the function is still compatible with your PR #70.

For now you can test the correction in the branch `feature/cleaned-free-solutions-API`. I let you confirm if it works on your test case and I will merge it into the `develop` branch.

By the way, I will also add the equivalent `MMG*_Free_solutions` functions in mmgs and mmg3d.

Regards,

Algiane

---

<div class="post-metadata">

**Author:** ![Mathias-Fuchs](https://forum.mmgtools.org/letter_avatar_proxy/v4/letter/m/c77e96/32.png) [@Mathias-Fuchs](https://forum.mmgtools.org/u/Mathias-Fuchs)\
**Post date:** [May 31, 2020, 1:49pm UTC](https://forum.mmgtools.org/t/how-to-reset-a-metric-to-cleared-state/442/3 "2020-05-31T13:49:04Z")

</div>

Thank you for the answer. Yes, that looks as if it’ll do the trick!

---

<div class="post-metadata">

**Author:** ![Algiane](https://forum.mmgtools.org/user_avatar/forum.mmgtools.org/algiane/32/30_2.png) [@Algiane](https://forum.mmgtools.org/u/Algiane)\
**Post date:** [June 2, 2020, 2:19pm UTC](https://forum.mmgtools.org/t/how-to-reset-a-metric-to-cleared-state/442/4 "2020-06-02T14:19:55Z")

</div>

Hello Mathias,

Thank you for your feedback. It is merged in the develop now.

Have a nice day

---

<div class="post-metadata">

**Author:** ![Mathias-Fuchs](https://forum.mmgtools.org/letter_avatar_proxy/v4/letter/m/c77e96/32.png) [@Mathias-Fuchs](https://forum.mmgtools.org/u/Mathias-Fuchs)\
**Post date:** [June 2, 2020, 2:21pm UTC](https://forum.mmgtools.org/t/how-to-reset-a-metric-to-cleared-state/442/5 "2020-06-02T14:21:07Z")

</div>

Ok, thank you!  
Mathias
