// Gmsh project created on Mon Nov 26 14:54:40 2018
SetFactory("OpenCASCADE");

r_max = 0.03; 
r_base = 0.025;
h_base = 0.005;
h_tronco_cono=0.003;
h_motore=0.05;
eps = 0.0005;
h_ceramica=0.0015;
r_ceramica=0.023;
r_int=0.01051;
r_ext=0.0175;
h_catodo = 0.001;
h_central_cone = 0.003;
r_catodo = 0.0001;

Cylinder(1) = {0, 0, 0, 0, 0, h_motore, r_max, 2*Pi};
Cone(2) = {0, 0, 0, 0, 0, -h_tronco_cono, r_max, r_base, 2*Pi};
Cylinder(3) = {0, 0, -h_tronco_cono+2*eps, 0, 0, -h_base, r_base, 2*Pi};

BooleanUnion{ Volume{1}; Delete; }{ Volume{2}; Delete; }
BooleanUnion{ Volume{4}; Delete; }{ Volume{3}; Delete; }

Cylinder(2) = {0, 0, h_motore-0.01, 0, 0, h_ceramica+0.01, r_ceramica, 2*Pi};

BooleanUnion{ Volume{1}; Delete; }{ Volume{2}; Delete; }

// Next is the central cone

Cone(2) = {0, 0, h_motore+h_ceramica-0.0001, 0, 0, h_central_cone, r_int, 0.004, 2*Pi};

BooleanUnion{ Volume{1}; Delete; }{ Volume{2}; Delete; }//+
Translate {0, 0, -0.0515} {
  Volume{1}; 
}

Delete {
  Volume{1}; 
}

// Catodo

dx = 0.0;
z_catodo = 0.0095;
l_catodo = 0.0257;
l_base=0.005;
r_base = 0.0115;
r_keeper=0.0065;

Cylinder(1) = {-r_ceramica-dx, -0, z_catodo, -l_catodo, 0, 0, r_keeper, 2*Pi};
Cylinder(2) = {-r_ceramica-dx-l_catodo, 0, z_catodo, -l_base, 0, 0, r_base, 2*Pi};

BooleanUnion{ Volume{1}; Delete; }{ Volume{2}; Delete; }

Delete {
  Volume{1}; 
}

// Add Far field and background field

Mesh.CharacteristicLengthExtendFromBoundary = 1;


//+
Delete {
  Surface{7}; Surface{14}; 
}

np = newp;

Point(np) = {0, 0, 0, 1.0};
Point(np+1) = {     0, r_ext, 0, 1.0};
Point(np+2) = { r_ext,     0, 0, 1.0};
Point(np+3) = {     0,-r_ext, 0, 1.0};
Point(np+4) = {-r_ext,     0, 0, 1.0};

nl = newl;

Circle(nl)   = {np+1, np, np+2};
Circle(nl+1) = {np+2, np, np+3};
Circle(nl+2) = {np+3, np, np+4};
Circle(nl+3) = {np+4, np, np+1};
//+


// Foro keeper

r_keeper = 0.0005;

np = newp;

Point(np)   = {-r_ceramica-dx,        0, z_catodo, 1.0};
Point(np+1) = {-r_ceramica-dx, r_keeper, z_catodo, 1.0};
Point(np+2) = {-r_ceramica-dx,        0, z_catodo+r_keeper, 1.0};
Point(np+3) = {-r_ceramica-dx,-r_keeper, z_catodo, 1.0};
Point(np+4) = {-r_ceramica-dx,        0, z_catodo-r_keeper, 1.0};

nl = newl;

Circle(nl)   = {np+1, np, np+2};
Circle(nl+1) = {np+2, np, np+3};
Circle(nl+2) = {np+3, np, np+4};
Circle(nl+3) = {np+4, np, np+1};


Curve Loop(21) = {11};
Curve Loop(22) = {21, 22, 23, 24};
Plane Surface(17) = {21, 22};

Curve Loop(23) = {21, 22, 23, 24};
Curve Loop(24) = {12};
Plane Surface(18) = {23, 24};

Curve Loop(25) = {20};
Curve Loop(26) = {25, 26, 27, 28};
Plane Surface(19) = {25, 26};

Curve Loop(27) = {25, 26, 27, 28};
Plane Surface(20) = {27};

/////////////////////////////////////////////

Surface Loop(3) = {1, 3, 5, 17, 18, 8, 9, 2, 4, 6};
Volume(2) = {3};
Surface Loop(4) = {12, 10, 11, 13, 19, 20};
Volume(3) = {4};




