# Errors in determine circumference through levelset function

**URL:** <https://community.freefem.org/t/errors-in-determine-circumference-through-levelset-function/781>\
**Category:** General Discussion\
**Created:** [February 1, 2021, 3:26pm UTC](https://community.freefem.org/t/errors-in-determine-circumference-through-levelset-function/781 "2021-02-01T15:26:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hezl09](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@hezl09](https://community.freefem.org/u/hezl09)\
**Post date:** [February 1, 2021, 3:26pm UTC](https://community.freefem.org/t/errors-in-determine-circumference-through-levelset-function/781/1 "2021-02-01T15:26:37Z")

</div>

Hi, can anyone help with my following question.

I’m trying to get the circumference of an ellipse. I have the following mesh which have two domains. The correct circumference for my ellipse (a = 1.5, b = 1) should be c ~= 7.9, this could be obtained through my following code if I set mesh density factor n = 1. However, if I change the mesh density factor n to 2 (OR 3,4,…), this c will change to c ~= 3.9. Strangely, if I change it to a circle with a = 1 and b = 1, then regardless of the value of n, it always gives me right answer with c ~= 6.28.

I realize that by using a different mesh, say only define the 4 sides and directly mesh the whole domain, I can get the correct ellipse circumference with n \>= 2. But how to explain such difference by using different mesh discretization algorithms?

Codes are as follows. Thanks! [circumference.edp](https://community.freefem.org/uploads/short-url/eIl9DTFu3vzqcTPgq3gq3KksHMm.edp) (648 Bytes)

/\* circumference of an ellipse or circle \*/

/\* mesh and FE space _/  
border bottom(t=-2.0,2.0){x=t;y=-2.0;}  
border right(t=-2.0,2.0){x=2.0;y=t;}  
border top(t=-2.0,2.0){x=-t;y=2.0;}  
border left(t=-2.0,2.0){x=-2.0;y=-t;}  
real a = 1.5;  
real b = 1.;  
real n = 2.;  
border ellipse(t=0.,2.pi){x=acos(t);y=b_sin(t);}  
mesh Th1 = buildmesh(bottom(n_20)+right(n_20)+top(n_20)+left(n_20)+ellipse(-n_40));  
mesh Th2 = buildmesh(ellipse(n_40));  
mesh Th = Th1 + Th2;  
fespace Vh(Th,P1);

/\* levelset \*/  
func phio = (x^2./a^2.+y^2./b^2.-1.);

/\* circumference \*/  
real c = int1d(Th,levelset=phio)(1.);  
cout \<\< "n: " \<\< n \<\< ", c: " \<\< c \<\< endl;

---

<div class="post-metadata">

**Author:** ![hezl09](https://avatars.discourse-cdn.com/v4/letter/h/f07891/32.png) [@hezl09](https://community.freefem.org/u/hezl09)\
**Post date:** [February 11, 2021, 2:26am UTC](https://community.freefem.org/t/errors-in-determine-circumference-through-levelset-function/781/2 "2021-02-11T02:26:22Z")

</div>

Using the above code, I further did a parametric study with parameter “a” changed from 0.6 to 1.7, while all other parameters kept. I compared the circumference provided by FreeFEM++ and theoretic formula, and below are the results.

 ![1](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/fa8bdd6fdd63d8c3176570e7d1020001fcc864c9.jpeg)

It shows that when the ellipse is close to a circle, then the results by FreeFEM++ in this case is reliable. However, if it is too elliptic, or the difference between a and b is large, then the calculations by FreeFEM++ in this case are not reliable. Is there any idea on this phenomenon? Thanks!

---

<div class="post-metadata">

**Author:** ![WeiQLiu](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/weiqliu/32/1043_2.png) [@WeiQLiu](https://community.freefem.org/u/WeiQLiu)\
**Post date:** [July 28, 2022, 2:25pm UTC](https://community.freefem.org/t/errors-in-determine-circumference-through-levelset-function/781/3 "2022-07-28T14:25:43Z")

</div>

Have you solved this problem? I have a similar problem recently, and I hope you can give me some help. My current solution is to give him a small offset, for example, func phio = (x^2./a^2.+y^2./b^2.-1.0001); however, it introduce some errors.

---

<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:** [August 3, 2022, 1:43pm UTC](https://community.freefem.org/t/errors-in-determine-circumference-through-levelset-function/781/4 "2022-08-03T13:43:44Z")

</div>

Thank to all, you find a bug, I will try correct this, but it is difficult because it is du to round-off.
