# Mesh a cube with a sphere on the side

**URL:** <https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424>\
**Category:** General Discussion\
**Created:** [May 21, 2020, 6:12pm UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424 "2020-05-21T18:12:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![abou](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abou](https://community.freefem.org/u/abou)\
**Post date:** [May 21, 2020, 6:12pm UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/1 "2020-05-21T18:12:47Z")

</div>

Dear All

I’d like to mesh a cube with a sphere on the side.

i use" tetgen".

There is the code FreeFem++

load “msh3”  
load “TetGen”  
load “medit”  
include “MeshSurface.idp”

// Parameters  
real hs = 0.1; //mesh size on sphere  
int r= 0.5;  
int[int] M = [20, 20, 20];  
real [int,int] B = [[-1, 1], [-1, 1], [-1, 1]];  
int [int,int] L = [[1, 2], [3, 4], [5, 6]];

// Meshes  
meshS ThH = SurfaceHex(M, B, L, 1);  
meshS ThS = Sphere(0.5, hs, 7, 1);

meshS Th1S= movemesh(ThS, [x+1, y+1, z+1]);

meshS ThHS = Th1S + ThH ;  
medit(“Hex-Sphere”, ThHS);

real voltet = (hs^3)/6.;  
cout \<\< "voltet = " \<\< voltet \<\< endl;  
real[int] domain = [0, 0, 0, 1, voltet, 1, 1, 1, 2, voltet];  
mesh3 Th = tetg(ThHS, switch=“pqaAAYYQ”, nbofregions=2, regionlist=domain);

medit(“Cube with ball”, Th);

plot(Th, wait=true,fill=true);

I have this error bellow:

Loading data file(s)  
End of mesh  
Input seconds: 0.00

medit1()

Building scene(s)  
Creating scene 1  
Loading default options  
Scene seconds: 0.36

Rendering scene(s)

Total running seconds: 10.16  
Thank you for using Medit.  
voltet = 0.000166667  
3D RemplissageSurf3D:: Vertex triangle2 border 3208 6408 240  
tetgenio: vertex  
tetgenio: facet  
tetgen: before tetrahedralize( , &in, &out);  
numberof regions 10  
numberof hole 0  
PLC Error: A segment and a facet intersect at point (0.562503,1,0.762503).  
Segment: [2021,2041] #-1 (4)  
Facet: [287,282,312] #7  
Strange catch exception ???  
at exec line 94 , mpirank 0

Thank you in advance to the help

---

<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:** [May 22, 2020, 8:10am UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/2 "2020-05-22T08:10:12Z")

</div>

Maybe you could use [Gmsh](http://gmsh.info/) instead.

```auto
SetFactory('OpenCASCADE');
Box(1) = {-1.0,-1.0,-1.0, 2, 2, 2};
Sphere(2) = {0.9999,1,1, 0.5};
BooleanFragments{ Volume{1,2}; Delete; }{}
Characteristic Length{ PointsOf{ Volume{1}; } } = 0.1;
Characteristic Length{ PointsOf{ Volume{3}; } } = 0.1;
Characteristic Length{ PointsOf{ Volume{2}; } } = 0.1;

```

Then, to do the actual meshing.

```auto
gmsh -3 cube-sphere.geo -format mesh

```

Finally, the .edp.

```auto
// gmsh -3 cube-sphere.geo -format mesh

load "msh3"
load "medit"

mesh3 Th = readmesh3("cube-sphere.mesh");
fespace Vh(Th, P1);
Vh u = x + y + z;

medit("Th", Th, u);

```

 ![Screenshot 2020-05-22 at 10.09.41 AM](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/ddadcadef808fcffb8624293dcc6d0f770756e80.png)

---

<div class="post-metadata">

**Author:** ![abou](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abou](https://community.freefem.org/u/abou)\
**Post date:** [May 24, 2020, 12:13am UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/3 "2020-05-24T00:13:12Z")

</div>

Ok i will try with Gmsh.

Thank you for your help

---

<div class="post-metadata">

**Author:** ![abou](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abou](https://community.freefem.org/u/abou)\
**Post date:** [May 27, 2020, 11:18pm UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/4 "2020-05-27T23:18:38Z")

</div>

i try with Gmsh and i got this error:

read mesh ok 1Mesh3, num Tetra:= 0, num Vertice:= 0 num boundary Triangles:= 0  
– Mesh3::Mesh3 Erreur opening cube.mesh  
current line = 6  
Assertion fail : (0)  
line :161, in file …/femlib/Mesh3dn.cpp  
Assertion fail : (0)  
line :161, in file …/femlib/Mesh3dn.cpp  
err code 6 , mpirank 0  
try getConsole C:\Users\aboubakiri.diaw\Desktop\tr\test.edp  
save log in : ‘C:\Users\aboubakiri.diaw\Desktop\tr\test.log’  
wait enter ?

can you help me to fix it, i never use Gmsh before  
thank you in advance

---

<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:** [May 28, 2020, 8:59am UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/5 "2020-05-28T08:59:37Z")

</div>

What `.geo` did you mesh, how did you mesh it, how do you load it? It seems there is no `cube.mesh` in your folder.

---

<div class="post-metadata">

**Author:** ![abou](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abou](https://community.freefem.org/u/abou)\
**Post date:** [May 28, 2020, 1:18pm UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/6 "2020-05-28T13:18:37Z")

</div>

I tried to mesh with the code you wrote above. I first wrote the code on a notepad and then saved it as a .geo file. Then I tried to open it with FreeFem++. is this correct?

---

<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:** [May 28, 2020, 2:53pm UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/7 "2020-05-28T14:53:57Z")

</div>

No. You need to actually mesh the `.geo` using Gmsh.

---

<div class="post-metadata">

**Author:** ![abou](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abou](https://community.freefem.org/u/abou)\
**Post date:** [May 28, 2020, 4:49pm UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/8 "2020-05-28T16:49:18Z")

</div>

okay, i was able to mesh it using Gmsh.  
How can i add a sphere in the middle and a quarter sphere on the side?  
what is the syntax in this case with gmsh?

Thank you in advance for you response

---

<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:** [May 28, 2020, 5:49pm UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/9 "2020-05-28T17:49:10Z")

</div>

Here is a pretty good Gmsh [tutorial](https://bthierry.pages.math.cnrs.fr/tutorial/gmsh/occ_overview/). You can also look at the numerous Gmsh [examples](https://gmsh.info/doc/texinfo/gmsh.html#Tutorial).

---

<div class="post-metadata">

**Author:** ![abou](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abou](https://community.freefem.org/u/abou)\
**Post date:** [May 28, 2020, 10:32pm UTC](https://community.freefem.org/t/mesh-a-cube-with-a-sphere-on-the-side/424/10 "2020-05-28T22:32:57Z")

</div>

I finally done it.

thanks for all
