// eddy current testing

// constants
hC = 0.001; // height of excitation coil above coordinate system
rC = 0.005; // radius of excitation coil
tC = 0.001; // thickness of excitation coil
R = 0.02; // radius of air
nTurns = 1; // number of coil windings
hc = 0; //0.001*nTurns; // height of coil
tS = 0.002; // thickness of metal sheet
xS = 0.01; // length of metal sheet
yS = 0.005; // width of metal sheet

lC = 0.0003;
lr = 0.007;

COIL = 1001;
SHEET = 1002;
SKINSHEET = 1003;
AIR = 1004;
AIRINF = 1005;
DIRPHI = 1006;

// excitation coil 
p = newp; Point(p) = {-rC, 0, hC, lC};
Point(p+1) = {-rC-tC/2, 0, hC, lC};
Point(p+2) = {-rC, 0, hC-tC/2, lC};
Point(p+3) = {-rC+tC/2, 0, hC, lC};
Point(p+4) = {-rC, 0, hC+tC/2, lC};
c = newl;
Circle(c) = {p+1,p,p+2};
Circle(c+1) = {p+2,p,p+3};
Circle(c+2) = {p+3,p,p+4};
Circle(c+3) = {p+4,p,p+1};
LL1 = newll; Line Loop(LL1) = {c,c+1,c+2,c+3};
s = news; Plane Surface(s) = {LL1};
tmp[] = {s};
vol_coil[] = {};
For j In {1:4*nTurns}
	tmp[] = Extrude{{0,0,hc/4/nTurns},{0,0,1},{0,0,0},Pi/2}{Surface{tmp[0]};};
	vol_coil[] += tmp[1];
EndFor
skin_coil[] = CombinedBoundary{ Volume{vol_coil[]}; };

// sheet
p = newp; Point(p) = {-xS, -yS, -tS, lC};
Point(p+1) = {xS, -yS, -tS, lC};
Point(p+2) = {xS, yS, -tS, lC};
Point(p+3) = {-xS, yS, -tS, lC};
l = newl; Line(l) = {p, p+1};
Line(l+1) = {p+1,p+2};
Line(l+2) = {p+2,p+3};
Line(l+3) = {p+3,p};
ll = newll; Line Loop(ll) = {l,l+1,l+2,l+3};
s = news; Plane Surface(s) = {ll};
vol_sheet[] = Extrude{0,0,tS}{Surface{s};};
skin_sheet[] = Boundary{Volume{vol_sheet[1]};};
slS = news; Surface Loop(slS) = {skin_sheet[]};

// Air
p = newp; Point(p) = {  0 ,    0, 0,  lr/2};
Point(p+1) = {  R,    0,  0,  lr};
Point(p+2) = {  0 ,   R,  0,  lr};
c = newc; Circle(c) = {p+1,p,p+2};
Point(p+3) = { -R,    0,  0,  lr};
Point(p+4) = {   0,  -R,  0,  lr};
Circle(c+1) = {p+2,p,p+3};
Circle(c+2) = {p+3,p,p+4};
Circle(c+3) = {p+4,p,p+1};
Point(p+5) = {   0,    0,  R, lr};
Point(p+6) = {   0,    0,  -R, lr};
Circle(c+4) = {p+2,p,p+5};
Circle(c+5) = {p+5,p,p+4};
Circle(c+6) = {p+4,p,p+6};
Circle(c+7) = {p+6,p,p+2};
Circle(c+8) = {p+1,p,p+6};
Circle(c+9) = {p+6,p,p+3};
Circle(c+10) = {p+3,p,p+5};
Circle(c+11) = {p+5,p,p+1};
LLA1 = newll; Line Loop(LLA1) = {c+1,c+7,-(c+9)};
SA1 = news; Ruled Surface(SA1) = {LLA1};
LLA2 = newll; Line Loop(LLA2) = {c+9,c+2,c+6};
SA2 = news; Ruled Surface(SA2) = {LLA2};
LLA3 = newll; Line Loop(LLA3) = {-(c+7),-(c+8),c};
SA3 = news; Ruled Surface(SA3) = {LLA3};
LLA4 = newll; Line Loop(LLA4) = {-(c+10),-(c+1),c+4};
SA4 = news; Ruled Surface(SA4) = {LLA4};
LLA5 = newll; Line Loop(LLA5) = {-(c+4),-(c+11),-c};
SA5 = news; Ruled Surface(SA5) = {LLA5};
LLA6 = newll; Line Loop(LLA6) = {-(c+2),c+10,c+5};
SA6 = news; Ruled Surface(SA6) = {LLA6};
LLA7 = newll; Line Loop(LLA7) = {-(c+6),c+3,c+8};
SA7 = news; Ruled Surface(SA7) = {LLA7};
LLA8 = newll; Line Loop(LLA8) = {-(c+3),c+11,-(c+5)};
SA8 = news; Ruled Surface(SA8) = {LLA8};
SL3 = newsl; Surface Loop(SL3) = {SA1,SA2,SA3,SA4,SA5,SA6,SA7,SA8};
slC = news; Surface Loop(slC) = {skin_coil[]};
VA = newv; Volume(VA) = {SL3,slC,slS};

Reverse Surface {s,SA1,SA2,SA3,SA4,SA5,SA6,SA7,SA8};

// physical entities
Physical Volume(COIL) = {vol_coil[]};
Physical Volume(SHEET) = {vol_sheet[1]};
Physical Surface(SKINSHEET) = {skin_sheet[]};
Physical Volume(AIR) = {VA};
Physical Surface(AIRINF) = {SL3};
Physical Point(DIRPHI) = {280};

View[1].Sampling = 20;
View[1].ArrowSizeMin = 25;
View[1].ArrowSizeMax = 35;
