// 
//	gyrotron mesh for Michelle code
//	author: Danny Holstein
// 	started 2/9/2015
// 

Include "keri.dat";
// Include "calcs.dat";
Include "functions.dat";

Printf(Str("0", "SECTION", "2", "ENTITIES")) > dxf_file;
// 
// spreadsheet calculations and initialization complete, build geometry
// 

// cathode
Point(1) = {-ls/2*Cos(k_slope) * m_per_mm, k_r + 0.5*ls*Sin(k_slope) * m_per_mm, 0, mesh_sz};
Point(2) = { ls/2*Cos(k_slope) * m_per_mm, k_r - 0.5*ls*Sin(k_slope) * m_per_mm, 0, mesh_sz};

Line(1) = {1, 2};
cL[] = {1, 2}; Call line_dxf;

// cathode nose
cp[] = Point{2};
c = k_nose * Sin(Pi/2 - k_slope);
d = k_nose * Cos(Pi/2 - k_slope);
e = (cp[1] - c)
   / Tan(k_slope);
Point(3) = {cp[0] + e - d, 		0, 0, mesh_sz};
Point(4) = {cp[0] + e - d + k_nose, 	0, 0, mesh_sz};
Point(5) = {cp[0] + e, 			c, 0, mesh_sz};

Circle(2) = {5, 3, 4};
arc_pts[] = {4, 3, 5}; Call arc_dxf;
Line(3) = {2, 5};
cL[] = {2, 5}; Call line_dxf;

// cathode stalk
cp[] = Point{1};

c = k_stalk_r * (1 - Cos(k_slope));
d = k_stalk_r * Sin(k_slope);
e = k_stalk - k_stalk_r;
f = cp[0] - (k_stalk - c - cp[1]) / Tan(k_slope);

Point(6) = {f, 	k_stalk - c, 0, mesh_sz};
Point(7) = {f - d, 	  e, 0, mesh_sz};
Point(8) = {f - d,  k_stalk, 0, mesh_sz};
cp[] = Point{8};

Point(9)  = {cp[0] - 2 * k_2_a * m_per_mm,  k_stalk, 		     0, mesh_sz};
Point(10) = {cp[0] - 2 * k_2_a * m_per_mm,  k_stalk + and_s1, 0, mesh_sz};
Line(4) = {1, 6};
cL[] = {1, 6}; Call line_dxf;
Circle(5) = {6, 7, 8};
arc_pts[] = {6, 7, 8}; Call arc_dxf;
Line(6) = {8, 9};
cL[] = {8, 9}; Call line_dxf;

// neumann, stalk
Line(7) = {9, 10};
cL[] = {9, 10}; Call line_dxf;

// anode
cx[] = Point{2};
cx[0] = cx[0] + and_k * Cos(Pi/2 - k_slope);	//  define point in front of cathode (on cone)
cx[1] = cx[1] + and_k * Sin(Pi/2 - k_slope);

cp[] = Point{10};	//  define first anode radius
rc_arry[] = {0, cp[1], -Tan(k_slope), cx[1] - (-Tan(k_slope) * cx[0]), and_r1};	// Function radius_corner(aL, bL, aR, bR, r)
Call radius_corner;
arc_pts[] = {PR, PC, PL}; Call arc_dxf;

Line(9) = {10, 12};
cL[] = {10, 12}; Call line_dxf;

cp[] = Point{PInt};	//  define anode cone to second radius
rc_arry[] = {- Tan(k_slope), cp[1] + Tan(k_slope) * cp[0], 0, and_d2/2, and_r2};	// Function radius_corner(aL, bL, aR, bR, r)
Call radius_corner;
arc_pts[] = {(sign > 0 ? PR: PL), PC, (sign > 0 ? PL: PR)}; Call arc_dxf;
Line(11) = {13, 16};
cL[] = {13, 16}; Call line_dxf;

cp[] = Point{PInt};	//  define anode diameter in front of cathode
aL = 0;
bL = and_d2/2;
aR = (and_d3/2 - cp[1])/and_l3;
bR = cp[1] - aR * (cp[0] + and_l2);
rc_arry[] = {aL, bL, aR, bR, and_r3};	// Function radius_corner(aL, bL, aR, bR, r)
Call radius_corner;
arc_pts[] = {(sign > 0 ? PR: PL), PC, (sign > 0 ? PL: PR)}; Call arc_dxf;
Line(13) = {17, 20};
cL[] = {17, 20}; Call line_dxf;

cp[] = Point{PInt};
aL = aR;
bL = bR;
aR = (and_d4 - and_d3)/2/and_l4;
bR = and_d3/2 - aR * (cp[0] + and_l3);
rc_arry[] = {aL, bL, aR, bR, and_r3};	// Function radius_corner(aL, bL, aR, bR, r)
Call radius_corner;
arc_pts[] = {(sign > 0 ? PR: PL), PC, (sign > 0 ? PL: PR)}; Call arc_dxf;
Line(15) = {21, 24};
cL[] = {21, 24}; Call line_dxf;

cp[] = Point{PInt};
aL = aR;
bL = bR;
aR = (tun_d - and_d4)/2/tun_l;
bR = and_d4/2 - aR * (cp[0] + and_l4);
// Printf("aL=%f, bL=%f, aR=%f, bR=%f, and_r3=%f", aL, bL, aR, bR, and_r3);
rc_arry[] = {aL, bL, aR, bR, and_r3};	// Function radius_corner(aL, bL, aR, bR, r)
Call radius_corner;
// Printf("angleL=%f, angleR=%f", angleL, angleR);
// Printf("PInt=%.0f", PInt);
arc_pts[] = {(sign > 0 ? PR: PL), PC, (sign > 0 ? PL: PR)}; Call arc_dxf;
Line(17) = {25, 28};
cL[] = {25, 28}; Call line_dxf;

cp[] = Point{PInt};
Point(newp) = {cp[0] + and_l4, tun_d/2, 0, tun_d/10};
Point(newp) = {cp[0] + and_l4,	     0, 0, tun_d/10};
Line(18) = {29, 31};
cL[] = {29, 31}; Call line_dxf;
Line(19) = {31, 32};
cL[] = {31, 32}; Call line_dxf;
Line(20) = {4, 32};
cL[] = {4, 32}; Call line_dxf;

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

Printf(Str("0", "ENDSEC", "0", "EOF")) >> dxf_file;

Characteristic Length {1, 2} = 0.0001;
Characteristic Length {13, 16} = 0.0005;
Characteristic Length {16, 17} = 0.001;
Characteristic Length {20, 17} = 0.001;
Characteristic Length {2, 5} = 0.0005;
Characteristic Length {4, 32} = 0.002;
Characteristic Length {21, 24} = 0.001;

