
// author: Danny Holstein

conv = 0.0254 ; 		// units are inches

//  compute anode position for magnetics, h = 0.0200
//  anode_dis=0.0630

h = 0.0200*conv ;			// 
width = 0.100*conv ;			// 
height = h	 ;		// beam tunnel height
crad =1.0300*3*h ;			// cathode radius
cthick = 1.0200*2*h ;			// cathode thickness
cthick2 = cthick/2 ;

anode_dis = 1.0500*3*h ;		// anode-cathode gap
anode_cntr = 1.0100*1.4*cthick2 ;	// anode center diameter
anode_od =  0.9800*anode_cntr*2 ;	// anode od
anode_rad = 1*h/10 ;		// anode radius of curvature
anode_prot = 0.9600*.7*h ;		// anode protrusion from plane

bfe_gap = .002*conv ;		// BFE gap
bfe_off = 1*.006*conv ;		// BFE axial offset from cathode corner
bfe_rad = 0.9900*1.5*h + cthick2 ;	// BFE radius
bfe_ang = 58*Pi/180 ;		// BFE angle
bfe_len = 1.0200*h/3 ;			// BFE length, projected from intersection point
bfe_crv = 0.9800*h/3 ;			// BFE radius of curvature

length = 2*anode_dis;			// beam tunnel length
mesh_len = height/6;


/* gun */

Point(1) = {length, height/2, 0.0, mesh_len/2} ;	//  beam out top
Point(2) = {length, 0.0, 0.0, mesh_len/2} ;		//  beam out on sym-plane
Point(3) = {-length/2, 0.0, 0.0, mesh_len/4} ;		//  origin

x = -length/2 ; y = height/2 ;
Point(4) = {x, cthick2, 0.0, mesh_len/2} ;
x = x + crad ;
Point(5) = {x, 0.0, 0.0, 0.0} ;							//  cathode radius center (not part of mesh)
y = cthick/2 ;
Point(6) = {x - Sqrt(crad*crad - cthick2*cthick2), y, 0.0, mesh_len/2} ;	//  cathode edge/corner
x = -length/2 ; y = y + bfe_gap ;
Point(7) = {x, y, 0.0, mesh_len/2} ;						//  cathode stalk
x = -length/2 + bfe_off ;
Point(8) = {x, y, 0.0, mesh_len/2} ;
x = x + (bfe_rad - cthick/2)/Tan(bfe_ang) ; y = bfe_rad ;
Point(9) = {x, y, 0.0, mesh_len} ;						//  BFE outer radius
x = x + (Fabs(bfe_len)+.5)*bfe_crv ;
Point(10) = {x, y, 0.0, mesh_len} ;
y = y + bfe_crv ;
Point(11) = {x, y, 0.0, mesh_len} ;
x = x + bfe_crv ;
Point(12) = {x, y, 0.0, mesh_len} ;
x = -length/2 + anode_dis ;
Point(13) = {x, y, 0.0, mesh_len} ;
y = anode_od/2 ;
Point(14) = {x, y, 0.0, mesh_len} ;
x = x - anode_prot ; y = anode_cntr/2 ;

tmp = anode_od/2 - anode_cntr/2 ;
c = Sqrt(anode_prot*anode_prot + tmp*tmp) ; 
t1 = Atan(anode_prot/tmp) ; t2 = Asin(anode_rad/c) ; t3 = Pi/2 - t1 - t2 ;

Point(15) = {x, y, 0.0, mesh_len} ;						//  anode center of radius
Point(16) = {x - anode_rad*Sin(t3), y + anode_rad*Cos(t3), 0.0, mesh_len} ;	//  anode top tangent
Point(17) = {x - anode_rad, y, 0.0, mesh_len} ;					//  anode tip
Point(18) = {-length/2 + anode_dis, height/2, 0.0, mesh_len/2} ;		//  anode/beam hole interface
tmp = anode_cntr/2 - height/2 ;
c = Sqrt(anode_prot*anode_prot + tmp*tmp) ; 
t1 = Atan(anode_prot/tmp) ; t2 = Asin(anode_rad/c) ; t3 = Pi/2 - t1 - t2 ;
Point(19) = {x - anode_rad*Sin(t3), y - anode_rad*Cos(t3), 0.0, mesh_len/2} ;	//  anode bottom tangent


Line(1) = {1,2};
Line(2) = {2,3};
Circle (3) = {3, 5, 6};
Line(4) = {18,1};
Line(5) = {6,4};
Line(6) = {4,7};
Line(7) = {7,8};
Line(8) = {8,9};
Line(9) = {9,10};
Circle (10) = {10,11,12};
Line(11) = {12,13};
Line(12) = {13,14};
Line(13) = {14,16};
Circle (14) = {16,15,17};
Circle (15) = {17,15,19};
Line(16) = {19,18};

Physical Line("cathode") = {3};
Physical Line("cathode-stalk") = {5};
Physical Line("neumann") = {6,11};
Physical Line("bfe") = {7,8,9,10};
Physical Line("anode") = {12,13,14,15,16,4};
Physical Line("beam-out") = {1};
Physical Line("mirror") = {2};

Translate {0, width/2, 0} {
  Line{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
}
Line(17) = {6, 8};
Line Loop(18) = {1, 2, 3, 17, 8, 9, 10, 11, 12, 13, 14, 15, 16, 4};
Plane Surface(19) = {18};
Line Loop(20) = {5, 6, 7, -17};
Plane Surface(21) = {20};
Mesh.Algorithm3D = 4;
Geometry.Tolerance = 1.e-3;
Coherence;

Extrude {{1, width/4, 0}, {-1, width/4, 0}, Pi/2} {
  Surface{21, 19}; Layers{1};
}

Extrude {0, -width/2, 0} {
  Line {2};
}

Extrude {{1, 0, 0}, {-1, 0, 0}, Pi} {
  Surface{114, 19, 21};
}

Delete {
  Volume{5, 4, 3};
}
Delete {
  Surface{130, 150, 202, 182, 198, 186, 194, 190, 178, 174, 170, 166, 162, 158, 126, 123, 212, 216, 220};
}
Delete {
  Line{173, 157, 215, 211, 122, 169, 165, 189, 193, 185, 148, 121, 177, 181, 197, 161};
}
Extrude {{1, -width/4, 0}, {-1, -width/4, 0}, -Pi/2} {
  Surface{203, 225};
}
// Extrude {0, -width, 0} {
//   Surface{110, 43};
// }
