// Fichier "OneTooth.geo"

Mesh.Algorithm3D = 4; // Frontal
Mesh.Optimize = 1 ;
Mesh.CharacteristicLengthFactor = 1.5 ; // 1. by default

Include "OneTooth-dat.pro";

/// longueurs caractéristiques pour le maillage
scale_lc = 1.0;
lc_core   = 2.5 * mm * scale_lc;
lc_coil   = 5. * mm * scale_lc;
lc_airgap = 0.5 * mm * scale_lc;  //not used
lc_plate  = 2.5 * mm * scale_lc;
lc_air    = 20. * mm * scale_lc;

/*
   p_... = point
   l_... = line
  ll_... = line loop
   s_... = surface
  sl_... = surface loop
   v_... = volume
*/

cut_symmetry[] = {};
s_air[] = {};

/// core

// coordonnées du centre
cx_core = 0.;
cy_core = 0.;
cz_core = 0.;

p_cen =newp; Point(newp) = { cx_core, cy_core, cz_core};

p_co[] = {};

// face "arrière" sur le plan de symétrie (oxy)
p_co[]+=newp; Point(newp) = { cx_core+lx_core/2., cy_core-ly_core/2., cz_core, lc_core}; // 0
p_co[]+=newp; Point(newp) = { cx_core+lx_core/2., cy_core+ly_core/2., cz_core, lc_core}; // 1
p_co[]+=newp; Point(newp) = { cx_core-lx_core/2., cy_core+ly_core/2., cz_core, lc_core}; // 2
p_co[]+=newp; Point(newp) = { cx_core-lx_core/2., cy_core-ly_core/2., cz_core, lc_core}; // 3

l_co[] = {};
l_co[]+=newl; Line(newl) = {p_co[0], p_co[1]};         // 0
l_co[]+=newl; Circle(newl) = {p_co[1], p_cen, p_co[2]}; // 1
l_co[]+=newl; Line(newl) = {p_co[2], p_co[3]};         // 2
l_co[]+=newl; Circle(newl) = {p_co[3], p_cen, p_co[0]}; // 3

ll_co = newll; Line Loop(newll) = {l_co[0], l_co[1], l_co[2], l_co[3]};

s_co=news; Plane Surface(news) = {ll_co};
cut_symmetry[] += s_co;

out[] = Extrude {0, 0, lz_core/2. } { Surface{s_co}; };
v_co = out[1];
skinCore[] = {out[0], out[2], out[3], out[4], out[5]};

s_air[] += {out[0], out[2], out[3], out[4], out[5]};

/// inductor (= coil)

// coordonnées du centre de la bobine
cx_coil = 0.;
cy_coil = 0.;
cz_coil = 0.;

p_in[] = {};

// face droite sur le plan de symétrie (oxy)
p_in[]+=newp; Point(newp) = { cx_coil+re_coil, cy_coil-ly_coil/2., cz_coil, lc_coil}; // 0
p_in[]+=newp; Point(newp) = { cx_coil+re_coil, cy_coil+ly_coil/2., cz_coil, lc_coil}; // 1
p_in[]+=newp; Point(newp) = { cx_coil+ri_coil, cy_coil+ly_coil/2., cz_coil, lc_coil}; // 2
p_in[]+=newp; Point(newp) = { cx_coil+ri_coil, cy_coil-ly_coil/2., cz_coil, lc_coil}; // 3

l_in[] = {};
For k1 In {0:3}
  k2 = (k1<3) ? k1+1 : 0;
  l_in[]+=newl; Line(newl) = {p_in[k1], p_in[k2]}; // 0, 1, 2, 3
EndFor

ll_in[] = {};
ll_in[] += newll; Line Loop(newll) = {l_in[0], l_in[1], l_in[2], l_in[3]}; // 0

s_in=news; Plane Surface(news) = {ll_in[0]};
cut_symmetry[] += s_in;

out[] = Extrude { {0,1,0} , {0., 0., 0.} , -Pi } { 
  Surface{s_in};
};

v_in = out[1];
cut_symmetry[] += out[0];
ll_in[] += newll; Line Loop(newll) = {Boundary{ Surface{ out[0] }; } };

s_air[] += {out[2], out[3], out[4], out[5]};

/// plate

// coordonnées du centre de la plaque
cx_plate = 0.;
cy_plate = 0.;
cz_plate = 0.;

p_cen =newp; Point(newp) = { cx_plate, cy_plate, cz_plate};

p_ple[] = {}; //les points du cercle extérieur
p_pli[] = {}; //les points du cercle intérieur

// face arrière sur le plan de symétrie (oxy)

p_ple[]+=newp; Point(newp) = { cx_plate+re_plate, cy_plate,          cz_plate, lc_plate}; // 0
p_ple[]+=newp; Point(newp) = { cx_plate,          cy_plate+re_plate, cz_plate, lc_plate}; // 1
p_ple[]+=newp; Point(newp) = { cx_plate-re_plate, cy_plate,          cz_plate, lc_plate}; // 2
p_ple[]+=newp; Point(newp) = { cx_plate,          cy_plate-re_plate, cz_plate, lc_plate}; // 3

l_ple[]={}; //les lignes du cercle extérieur
For k1 In {0:3}
   k2 = (k1<3) ? k1+1 : 0;
   l_ple[]+=newl ; Circle(newl) = {p_ple[k1], p_cen, p_ple[k2]};
EndFor
ll_ple=newll; Line Loop(newll) = {l_ple[]};

p_pli[]+=newp; Point(newp) = { cx_plate+ri_plate, cy_plate,          cz_plate, lc_plate}; // 0
p_pli[]+=newp; Point(newp) = { cx_plate,          cy_plate+ri_plate, cz_plate, lc_plate}; // 1
p_pli[]+=newp; Point(newp) = { cx_plate-ri_plate, cy_plate,          cz_plate, lc_plate}; // 2
p_pli[]+=newp; Point(newp) = { cx_plate,          cy_plate-ri_plate, cz_plate, lc_plate}; // 3

l_pli[]={}; //les lignes du cercle intérieur
For k1 In {0:3}
   k2 = (k1<3) ? k1+1 : 0;
   l_pli[]+=newl ; Circle(newl) = {p_pli[k1], p_cen, p_pli[k2]};
EndFor
ll_pli=newll; Line Loop(newll) = {l_pli[]};

s_pl=news; Plane Surface(news) = {ll_ple,ll_pli};
cut_symmetry[] += s_pl;

out[] = Extrude {0, 0, lz_plate/2. } { Surface{s_pl}; };
v_pl = out[1];
skinPlate[] = {out[0], out[2], out[3], out[4], out[5], out[6], out[7], out[8], out[9]};

surf = news; Plane Surface(news) = {ll_pli[], ll_co[], ll_in[]}; // face arrière de l'air intérieur
s_air[] += surf;
cut_symmetry[] += surf;
s_air[] += {out[0], out[2], out[3], out[4], out[5], out[6], out[7], out[8], out[9]};

/// la boîte à air

p_air[]+=newp; Point(newp) = {axl, ayb, azb, lc_air}; // 0
p_air[]+=newp; Point(newp) = {axl, ayt, azb, lc_air}; // 1
p_air[]+=newp; Point(newp) = {axr, ayt, azb, lc_air}; // 2
p_air[]+=newp; Point(newp) = {axr, ayb, azb, lc_air}; // 3
p_air[]+=newp; Point(newp) = {axl, ayb, azf, lc_air}; // 4
p_air[]+=newp; Point(newp) = {axl, ayt, azf, lc_air}; // 5
p_air[]+=newp; Point(newp) = {axr, ayt, azf, lc_air}; // 6
p_air[]+=newp; Point(newp) = {axr, ayb, azf, lc_air}; // 7

l_air[]+=newl; Line(newl)   = {p_air[0], p_air[1]}; // 0
l_air[]+=newl; Line(newl)   = {p_air[1], p_air[2]}; // 1
l_air[]+=newl; Line(newl)   = {p_air[2], p_air[3]}; // 2
l_air[]+=newl; Line(newl)   = {p_air[3], p_air[0]}; // 3
l_air[]+=newl; Line(newl)   = {p_air[4], p_air[5]}; // 4
l_air[]+=newl; Line(newl)   = {p_air[5], p_air[6]}; // 5
l_air[]+=newl; Line(newl)   = {p_air[6], p_air[7]}; // 6
l_air[]+=newl; Line(newl)   = {p_air[7], p_air[4]}; // 7
l_air[]+=newl; Line(newl)   = {p_air[1], p_air[5]}; // 8
l_air[]+=newl; Line(newl)   = {p_air[2], p_air[6]}; // 9
l_air[]+=newl; Line(newl)   = {p_air[7], p_air[3]}; // 10
l_air[]+=newl; Line(newl)   = {p_air[4], p_air[0]}; // 11

ll_air[]+=newll; Line Loop(newll) = { l_air[0],   l_air[1],  l_air[2],  l_air[3]};  // 0, back
ll_air[]+=newll; Line Loop(newll) = { l_air[4],   l_air[5],  l_air[6],  l_air[7]};  // 1, front
ll_air[]+=newll; Line Loop(newll) = { l_air[0],   l_air[8], -l_air[4],  l_air[11]}; // 2, right
ll_air[]+=newll; Line Loop(newll) = {-l_air[2],   l_air[9],  l_air[6],  l_air[10]}; // 3, left
ll_air[]+=newll; Line Loop(newll) = { l_air[11], -l_air[3], -l_air[10], l_air[7]};  // 4, bottom
ll_air[]+=newll; Line Loop(newll) = {-l_air[8],   l_air[1],  l_air[9], -l_air[5]};  // 5, top

surf = news; Plane Surface(news) = {ll_air[0], ll_ple[]}; // face arrière de l'air extérieur
s_air[]+= surf;
cut_symmetry[] += surf;

airInfinity[] = {};
For ki In {1:5}
  surf = news; Plane Surface(news) = {ll_air[ki]};
  s_air[] += surf;
  airInfinity[] += surf;
EndFor

sl_air=newsl; Surface Loop(newsl) = { s_air[] };

v_air=newv; Volume(newv) = {sl_air};

/// Les entités physiques

Physical Volume(CORE) = v_co;
Physical Surface(SKIN_CORE) = skinCore[];

Physical Volume(COIL) = v_in;

Physical Volume(PLATE) = v_pl;
Physical Surface(SKIN_PLATE) = skinPlate[];

Physical Volume(AIR) = v_air;
Physical Surface(AIR_INF) = airInfinity[];

Physical Surface(CUT_SYMMETRY) = cut_symmetry[];

// EOF
