# How to create mesh from levet set function

**URL:** https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680
**Category:** General Discussion
**Created:** [September 3, 2023, 7:18pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680 "2023-09-03T19:18:34Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![gurkanceren](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/gurkanceren/32/1893_2.png) [@gurkanceren](https://community.freefem.org/u/gurkanceren)
#### Post date: [September 3, 2023, 7:18pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/1 "2023-09-03T19:18:34Z")

</div>

Hello,  
I am a newbee in the platform and I want to create a square domain with a 3 petal flower-like shape in it (I have the level set definition of this flower geometry, shared below). Then I want to create a mesh in the area between square and the flower geometry. Anyone can help me with this? Thanks in advance.

LS definition:  
A=(x.^2 + y.^2 );  
LS = (sqrt(A) - 0.27 -0.27._2_cos(3\*atan2(x,y)));

---

<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: [September 4, 2023, 3:38am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/2 "2023-09-04T03:38:13Z")

</div>

You can use Mmg, an interface to `mmg2d` was recently introduced.

---

<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: [September 4, 2023, 3:44am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/3 "2023-09-04T03:44:38Z")

</div>

Here is a sample code.

```auto
load "mmg"
mesh Th = square(400, 400, [-0.5 + x, -0.5 + y]);
fespace Vh(Th, P1);
Vh A = x^2 + y^2;
Vh ls = sqrt(A) - 0.27 - 0.27 * 2 * cos(3 * atan2(x, y));
plot(ls, wait = 1);
Th = mmg2d(Th, iso = 1, ls = 0.0, metric = ls[], hmax = 0.05);
A = x^2 + y^2;
ls = sqrt(A) - 0.27 - 0.27 * 2 * cos(3 * atan2(x, y));
plot(ls, wait = 1);
plot(Th);

```

 ![Screenshot 2023-09-04 at 5.44.04 AM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/8/82ddfaeaede954f7dbb7b3b1da275bf5a8d9681c.png)

---

<div class="post-metadata">

### Author: ![gurkanceren](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/gurkanceren/32/1893_2.png) [@gurkanceren](https://community.freefem.org/u/gurkanceren)
#### Post date: [September 4, 2023, 6:53am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/4 "2023-09-04T06:53:48Z")

</div>

Thank you very much for the answer, exactly what I was looking for. One more thing though do you know how to procced if one just wants to mesh the outer-flower area i.e. only the red elements in the figure and make inside flower “empty” ?

---

<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: [September 4, 2023, 8:28am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/5 "2023-09-04T08:28:41Z")

</div>

The mmg2d thing did not work for me and looking at examples all I can find  
is mmg3d,

```auto
FreeFem++ petals.edp 
-- FreeFem++ v4.12 (Sat 20 May 2023 07:29:03 PM EDT - git no git)
   file : petals.edp
 Load: lg_fem lg_mesh lg_mesh3 eigenvalue 
    1 : load "mmg" load: mmg 

    2 : mesh Th = square(400, 400, [-0.5 + x, -0.5 + y]);
    3 : fespace Vh(Th, P1);
    4 : Vh A = x^2 + y^2;
    5 : Vh ls = sqrt(A) - 0.27 - 0.27 * 2 * cos(3 * atan2(x, y));
    6 : plot(ls, wait = 1);
    7 : Th = mmg2d The Identifier mmg2d does not exist 

 Error line number 7, in file petals.edp, before token mmg2d

  current line = 7
Compile error : 
	line number :7, mmg2d
error Compile error : 
	line number :7, mmg2d
 code = 1 mpirank: 0

```

---

<div class="post-metadata">

### Author: ![gurkanceren](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/gurkanceren/32/1893_2.png) [@gurkanceren](https://community.freefem.org/u/gurkanceren)
#### Post date: [September 4, 2023, 8:42am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/6 "2023-09-04T08:42:59Z")

</div>

Just tried to run the code and I see the same problem

---

<div class="post-metadata">

### Author: ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)
#### Post date: [September 4, 2023, 9:26am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/7 "2023-09-04T09:26:14Z")

</div>

You need to rebuild with the latest version:

> <https://github.com/FreeFem/FreeFem-sources/commit/2b624111b3dc1d0c331409a4b5d7a6d50df48633>

---

<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: [September 4, 2023, 9:34am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/8 "2023-09-04T09:34:03Z")

</div>

There are probably ways to do this more efficiently, but you can just add

```auto
Th = trunc(Th, region == 2);
plot(Th);

```

to the file, and you’ll get only the outside domain, cf. screenshot.

 ![Screenshot 2023-09-04 at 11.33.32 AM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/f/fc1388c979a1247751c5db0558f865c393753be2.png)

---

<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: [September 4, 2023, 9:52am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/9 "2023-09-04T09:52:18Z")

</div>

Thanks. Is the region number predictable or should you extract itby getting  
the region from some point in the region of interest? In the past my assumptions  
about region numbers did not work out.

---

<div class="post-metadata">

### Author: ![gurkanceren](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/gurkanceren/32/1893_2.png) [@gurkanceren](https://community.freefem.org/u/gurkanceren)
#### Post date: [September 4, 2023, 10:32am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/10 "2023-09-04T10:32:03Z")

</div>

Thank you @prj . Another question now poped up as well. Can we distort the mesh? I mean say if the “flower” rotates towards right —numbers in level set function changes slightly say it becomes _Vh ls = sqrt(A) - 0.27 - 0.27 \* 2 \* cos((3 \* atan2(x, y))-15)_ — can we strech out the existing mesh to adapt this configuration without remeshing?

---

<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: [September 4, 2023, 10:50am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/11 "2023-09-04T10:50:03Z")

</div>

It’s predictable, see the Mmg documentation.

---

<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: [September 4, 2023, 10:50am UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/12 "2023-09-04T10:50:52Z")

</div>

You can use `movemesh`, see [Mesh Generation](https://doc.freefem.org/documentation/mesh-generation.html#the-command-movemesh).

---

<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: [September 4, 2023, 7:07pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/13 "2023-09-04T19:07:00Z")

</div>

I went and got the file , renamed it to something like “mmgned2d”  
and compiled it with “ff-c++” which I use for my custom datascope plugin  
successfully. It built a shared object which ldd says links ok but  
it refused to open. What do I need to do to get this to work?  
I’m pretty sure it is finding the so but can’t load it due to some other issue;  
I guess if it had a line number I could set a bp in gdb or if it threw but  
otherwise it will take a little time to diagnose.  
Thanks.

```auto
ls -al *.so
-rwxrwxr-x 1 marchywka marchywka 1689808 Sep 4 14:46 mmgnew2d.so
marchywka@happy:/home/documents/cpp/proj/freefem/forum$ FreeFem++ petals.edp 
-- FreeFem++ v4.12 (Sat 20 May 2023 07:29:03 PM EDT - git no git)
   file : petals.edp
 Load: lg_fem lg_mesh lg_mesh3 eigenvalue 
    1 : verbosity=100;
    2 : load "mmgnew2d"
Load error: mmgnew2d
	 fail: 
 dlerror : /home/ubuntu/dev//freefem/install/lib/ff++/4.12/lib/mmgnew2d.so: cannot open shared object file: No such file or directory
list prefix: './' '/home/ubuntu/dev//freefem/install/lib/ff++/4.12/lib/' list suffix: '' , '.so' 
  current line = 2
Load error : mmgnew2d
	line number :2, mmgnew2d
error Load error : mmgnew2d
	line number :2, mmgnew2d
 code = 2 mpirank: 0

```

---

<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: [September 4, 2023, 7:19pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/14 "2023-09-04T19:19:37Z")

</div>

Run with `-v 10000` to get more information.

---

<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: [September 4, 2023, 7:37pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/15 "2023-09-04T19:37:36Z")

</div>

Thanks, its missing a symbol apparently but not sure if this should be fixed at  
compile time or with a path change… When I make my plugin I just added  
“-l readline -l pthread” can I do that here?  
Also, fwiw, in my plugin most of the info goes through a macro MM\_ERR that attaches  
file and line to the output so you can hunt it down easily.

```auto
    2 : load "mmgnew2d" test dlopen(./mmgnew2d) = 0
load error was: ./mmgnew2d: cannot open shared object file: No such file or directory
 test dlopen(./mmgnew2d.so) = 0
load error was: ./mmgnew2d.so: undefined symbol: MMGS_mmgslib
 test dlopen(/home/ubuntu/dev//freefem/install/lib/ff++/4.12/lib/mmgnew2d) = 0
load error was: /home/ubuntu/dev//freefem/install/lib/ff++/4.12/lib/mmgnew2d: cannot open shared object file: No such file or directory
 test dlopen(/home/ubuntu/dev//freefem/install/lib/ff++/4.12/lib/mmgnew2d.so) = 0
load error was: /home/ubuntu/dev//freefem/install/lib/ff++/4.12/lib/mmgnew2d.so: cannot open shared object file: No such file or directory

Load error: mmgnew2d

```

---

<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: [September 4, 2023, 7:48pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/16 "2023-09-04T19:48:45Z")

</div>

You need to switch to the `develop` branch.

---

<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: [September 4, 2023, 8:08pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/17 "2023-09-04T20:08:27Z")

</div>

I guess this is defined in a 3rdparty mmg libtary but it may not be built  
in 4.12 or can I just make if from an archive lib in the build dir?  
Or do I really need to get the dev branch lol?

---

<div class="post-metadata">

### Author: ![gurkanceren](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/gurkanceren/32/1893_2.png) [@gurkanceren](https://community.freefem.org/u/gurkanceren)
#### Post date: [September 6, 2023, 4:02pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/19 "2023-09-06T16:02:07Z")

</div>

Hi,  
To be able to run the minimal example given above I have installed a VM (Ubuntu system) in my Windows machine and followed the installation guide on [Installation guide](https://doc.freefem.org/introduction/installation.html#compilation-on-ubuntu) . When I run the command

make -j  
make -j check

I see the error in the picture

 ![Screenshot (115)](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/f/faa9d14cbaa9061234bfc930138964eb57e39c8c.jpeg)

can anyone guide me on this?

---

<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: [September 6, 2023, 4:05pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/20 "2023-09-06T16:05:37Z")

</div>

copy/paste the text the screenshot is difficult or impossible to read and  
can’t easily be quoted anyway. This is linus, not windohs 🙂

Meanwhile, I was debating about just doing a separate build of mmg  
and see if my plugin will link to that. I guess I need to look at mmg  
anyway and its easier than getting dev build of ff for now.

---

<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: [September 6, 2023, 4:10pm UTC](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680/21 "2023-09-06T16:10:54Z")

</div>

You are not using the proper installation guide, please look for “Standard Compilation Process” in [https://joliv.et/FreeFem-tutorial/main.pdf](https://joliv.et/FreeFem-tutorial/main.pdf).

[Next page](https://community.freefem.org/t/how-to-create-mesh-from-levet-set-function/2680.md?page=2)
