// parametres par defaut
box=50;gap=6;rc=1;lelec=50;
helectrode=15;hsubs=50;hdielsup=2;hdielinf=2;hair=50;
small=3;large=20;ratio=3;xc=0;yc=0;zc=0;

Include "par.dat";

hdiel=hdielsup+hdielinf+(2*rc);
boxbis= box+gap/2;
lc=100;arc=rc/Sqrt[3];
// sphere
Point(1) = {0, 0, 0, lc};
Point(2) = {arc, arc, arc, lc};
Point(3) = {-arc, arc, arc, lc};
Point(4) = {arc, -arc, arc, lc};
Point(5) = {-arc, -arc, arc, lc};
Point(6) = {arc, arc, -arc, lc};
Point(7) = {-arc, arc, -arc, lc};
Point(8) = {arc, -arc, -arc, lc};
Point(9) = {-arc, -arc, -arc, lc};

Translate {xc, yc, zc} {Point{1,8, 6, 9, 7, 5, 3, 2, 4};}

Circle(1) = {8, 1, 9};
Circle(2) = {8, 1, 6};
Circle(3) = {9, 1, 7};
Circle(4) = {7, 1, 6};
Circle(5) = {4, 1, 8};
Circle(6) = {6, 1, 2};
Circle(7) = {2, 1, 4};
Circle(8) = {9, 1, 5};
Circle(9) = {5, 1, 3};
Circle(10) = {3, 1, 2};
Circle(11) = {3, 1, 7};
Circle(12) = {5, 1, 4};
Line Loop(13) = {3, 4, -2, 1};
Ruled Surface(14) = {13};
Line Loop(15) = {2, 6, 7, 5};
Ruled Surface(16) = {15};
Line Loop(17) = {11, 4, 6, -10};
Ruled Surface(18) = {17};
Line Loop(19) = {10, 7, -12, 9};
Ruled Surface(20) = {19};
Line Loop(21) = {9, 11, -3, 8};
Ruled Surface(22) = {21};
Line Loop(23) = {1, 8, 12, 5};
Ruled Surface(24) = {23};
Surface Loop(41) = {16, 14, 22, 20, 18, 24};
Volume(42) = {41};

// couche englobant la sphere
//bas
Point(10) = {-boxbis, -boxbis, 0, lc};
Point(11) = { boxbis, -boxbis, 0, lc};
Point(12) = {-boxbis,  boxbis, 0, lc};
Point(13) = { boxbis,  boxbis, 0, lc};
Line(43) = {11, 13};
Line(44) = {13, 12};
Line(45) = {12, 10};
Line(46) = {10, 11};
//haut
Point(14) = {-boxbis, -boxbis,  hdiel, lc};
Point(15) = { boxbis, -boxbis,  hdiel, lc};
Point(16) = {-boxbis,  boxbis,  hdiel, lc};
Point(17) = { boxbis,  boxbis,  hdiel, lc};
Line(47) = {11, 15};
Line(48) = {13, 17};
Line(49) = {12, 16};
Line(50) = {10, 14};
Line Loop(65) = {45, 46, 43, 44};
Plane Surface(66) = {65};

//Electrode Ferro
Point(18) = {-gap/2,        boxbis,  hdiel, lc};
Point(19) = {-gap/2-lelec,  boxbis,  hdiel, lc};
Point(20) = {-gap/2,       -boxbis,  hdiel, lc};
Point(21) = {-gap/2-lelec, -boxbis,  hdiel, lc};

//Electrode de droite
Point(22) = {gap/2, -lelec/2,  hdiel, lc};
Point(23) = {gap/2,  lelec/2,  hdiel, lc};
Point(24) = {boxbis,   -lelec/2,  hdiel, lc};
Point(25) = {boxbis,    lelec/2,  hdiel, lc};



Line(51) = {20, 21};
Line(52) = {21, 14};
Line(53) = {14, 16};
Line(54) = {16, 19};
Line(55) = {19, 18};
Line(56) = {18, 17};
Line(57) = {17, 25};
Line(58) = {25, 24};
Line(59) = {24, 15};
Line(60) = {15, 20};
Line(61) = {24, 22};
Line(62) = {22, 23};
Line(63) = {23, 25};
Line(64) = {20, 18};
Line(65) = {19, 21};



Line Loop(67) = {64, 56, 57, -63, -62, -61, 59, 60};//Surface autour electrode+gap
Plane Surface(68) = {67};
Line Loop(69) = {63, 58, 61, 62};//Electrode Droite (non ferro)
Plane Surface(70) = {69};
Line Loop(71) = {51, -65, 55, -64};//Electrode Ferro
Plane Surface(72) = {71};
Line Loop(73) = {52, 53, 54, 65};//Gauche electrode Ferro
Plane Surface(74) = {73};
//Cotés
Line Loop(75) = {53, -49, 45, 50};
Plane Surface(76) = {75};
Line Loop(77) = {54, 55, 56, -48, 44, 49};
Plane Surface(78) = {77};
Line Loop(79) = {50, -52, -51, -60, -47, -46};
Plane Surface(80) = {79};
Line Loop(81) = {47, -59, -58, -57, -48, -43};
Plane Surface(82) = {81};
// substrat par extrusion
Extrude {0, 0, -hsubs} {
  Surface{66};
}

//Electrodes par extrusion
Extrude {0, 0, helectrode} {
  Surface{72};
}
Extrude {0, 0, helectrode} {
  Surface{70};
}

//Air par extrusion des points + line + surface 
Extrude {0, 0, hair} {
  Point{14};
}
Extrude {0, 0, hair} {
  Point{15};
}
Extrude {0, 0, hair} {
  Point{17};
}
Extrude {0, 0, hair} {
  Point{16};
}
Line(153) = {56, 59};
Line(154) = {59, 58};
Line(155) = {58, 57};
Line(156) = {57, 56};
Line Loop(157) = {156, 153, 154, 155};//Dessus
Plane Surface(158) = {157};
Line Loop(159) = {151, 155, -150, -59, 138, -129, -134, -57};//Coté droit avec contour electrode
Plane Surface(160) = {159};
Line Loop(161) = {56, 151, -154, -152, 54, 116, 108, -120};//Fond
Plane Surface(162) = {161};
Line Loop(163) = {152, -153, -149, 53};//Coté gauche
Plane Surface(164) = {163};
Line Loop(165) = {149, -156, -150, 60, 111, 106, -112, 52};//devant
Plane Surface(166) = {165};

//Volumes
//Air
Surface Loop(167) = {164, 162, 160, 158, 166, 74, 117, 126, 125, 68, 143, 147, 135, 148};
Volume(168) = {167};
//dielectrique
Surface Loop(169) = {80, 76, 78, 82, 66, 68, 70, 72, 74, 16, 14, 22, 20, 18, 24};
Volume(170) = {169};
//Volume electrodes et substrat fait lors des extrusions
//Volume agragat fait plus haut

//Physical quantities
Physical Volume(3001) = {170};//dielec
Physical Volume(3002) = {43};//substrat
Physical Volume(3003) = {168};//air

Physical Surface(2001) = {16, 24, 22, 18, 20, 14};//Cluster
Physical Surface(2002) = {126, 125, 72, 117};//Lplus electrode ferro
Physical Surface(2003) = {143, 148, 135, 70, 147};//Lmoins Electrode droite
Physical Surface(2004) = {104};//Gate

// Attractor pour definir la densite de maillage
Field[1] = Attractor;
//Field[1].EdgesList = {53,54,65,85};   // bords electrode
Field[1].FacesList = {16,14,22,20,18,24};   // cluster
Field[2] = Threshold;
Field[2].IField = 1;
Field[2].LcMin = small;
Field[2].LcMax = large;
Field[2].DistMin = small*ratio;
Field[2].DistMax = large*ratio;
Background Field = 2;





