Mesh a cube with a sphere on the side

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

Maybe you could use Gmsh instead.

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.

gmsh -3 cube-sphere.geo -format mesh

Finally, the .edp.

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

1 Like

Ok i will try with Gmsh.

Thank you for your help

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

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.

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?

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

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

Here is a pretty good Gmsh tutorial. You can also look at the numerous Gmsh examples.

I finally done it.

thanks for all