How to create mesh from levet set function

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.2cos(3*atan2(x,y)));

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

Here is a sample code.

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);

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” ?

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

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

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

You need to rebuild with the latest version:

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

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

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

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.

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?

It’s predictable, see the Mmg documentation.

You can use movemesh, see Mesh Generation.

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.

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

Run with -v 10000 to get more information.

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.

    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

You need to switch to the develop branch.

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?

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 . When I run the command

make -j
make -j check

I see the error in the picture

can anyone guide me on this?

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

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.

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