//+
Include "inductive_heat_id.geo";

lc = DefineNumber[ 0.05, Name "Parameters/lc" ];
lc2 = lc;
lc3 = 0.4*lc;

lb = 1.0;

core_rad = 0.08;

rt2 = 0.12;
rt1 = 0.09;
ht = 0.02;

er = 0.3;
eh = 0.1;
eg = 0.01;
eb = -eg;

gap = 4.0e-3;

//
// Box Region
//+
p = newp;
Point(p) = {-lb/2,-lb/2,-lb/2,lc};
Point(p+1) = {lb/2,-lb/2,-lb/2,lc};
Point(p+2) = {lb/2,lb/2,-lb/2,lc};
Point(p+3) = {-lb/2,lb/2,-lb/2,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;
Surface(s) = {ll};
tmp[] = Extrude {0,0,lb} { Surface{s}; };
vol_box = tmp[1];
//+ box
//Surface Loop(1) = {144, 127, 136, 140, 149, 148};
Surface Loop(1) = {s,tmp[{0,2,3,4,5}]};

//
// inductor coil
//+
by = -ht-gap;
p = newp;
Point(p) = {rt1, by, 0, lc2};
Point(p+1) = {rt2, by, 0, lc2};
Point(p+2) = {rt2, by+ht, 0, lc2};
Point(p+3) = {rt1, by+ht, 0, lc2};
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};

coil0[] = Extrude {{0, 1, 0}, {0, 0, 0}, Pi/2} {  Surface{s}; };
coil1[] = Extrude {{0, 1, 0}, {0, 0, 0}, Pi/2} {  Surface{coil0[0]}; };
coil2[] = Extrude {{0, 1, 0}, {0, 0, 0}, Pi/2} {  Surface{coil1[0]}; };
coil3[] = Extrude {{0, 1, 0}, {0, 0, 0}, Pi/2} {  Surface{coil2[0]}; };

vol_coil = {coil0[1],coil1[1],coil2[1],coil3[1]};
coil_crs = s;
//+
//Surface Loop(4) = {15, 19, 23, 27, 81, 85, 89, 93, 59, 63, 67, 71, 37, 41, 45, 49};
Surface Loop(4) = {coil0[{2,3,4,5}],coil1[{2,3,4,5}],coil2[{2,3,4,5}],coil3[{2,3,4,5}] };

//+
// electromagnet core
p = newp;
Point(p) = {0, by, 0, lc};
Point(p+1) = {core_rad, by, 0, lc};
Point(p+2) = {0, by, core_rad, lc};
Point(p+3) = {-core_rad, by, 0, lc};
Point(p+4) = {0, by, -core_rad, lc};
//+
c = newc;
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};
//+
ll = newll;
Line Loop(ll) = {c, c+1, c+2, c+3};
//+
s = news;
Surface(s) = {ll};
//+
tmp[] = Extrude {0, ht, 0.} { Surface{s}; };
vol_core = tmp[1];
//+ core
//Surface Loop(3) = {121, 108, 99, 112, 116, 120};
Surface Loop(3) = {s,tmp[{0,2,3,4,5}]};

//
// pan plate
///////
cc = 0.0;
pb = 0.0;
pr = 0.17;
pt = 5e-3;
ph = 0.2;

p = newp;
Point(p) = {0, pb, 0, lc3};
Point(p+1) = {pr, pb, 0, lc3};
Point(p+2) = {pr, pb+ph, 0, lc};
Point(p+3) = {pr-pt, pb+ph, 0, lc};
Point(p+4) = {pr-pt, pb+pt, 0, lc};
Point(p+5) = {0., pb+pt, 0, 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+4};
Line(l+4) = {p+4, p+5};
Line(l+5) = {p+5, p};
//+
ll = newll;
Line Loop(ll) = {l+4, l+5, l, l+1, l+2, l+3};
//+
reg = newreg;
Plane Surface(reg) = {ll};
//+
//+
tmp0[] = Extrude {{0, 1, 0}, {0, 0, 0}, Pi/2} {  Surface{reg}; };
tmp1[] = Extrude {{0, 1, 0}, {0, 0, 0}, Pi/2} {  Surface{tmp0[0]}; };
tmp2[] = Extrude {{0, 1, 0}, {0, 0, 0}, Pi/2} {  Surface{tmp1[0]}; };
tmp3[] = Extrude {{0, 1, 0}, {0, 0, 0}, Pi/2} {  Surface{tmp2[0]}; };

vol_eddy = {tmp0[1],tmp1[1],tmp2[1],tmp3[1]};
//+ pan
Surface Loop(2) = {tmp0[{2,3,4,5,6}],tmp1[{2,3,4,5,6}], tmp2[{2,3,4,5,6}], tmp3[{2,3,4,5,6}]};
//Surface Loop(2) = {237, 221, 233, 229, 225, 202, 206, 210, 194, 198, 183, 167, 179, 175, 171, 256, 260, 264, 248, 252};
//+
Volume(100) = {1,2,3,4};

Physical Volume(AIR) = {100};
Physical Volume(ICORE) = vol_core[];
Physical Volume(COIL) = vol_coil[];
Physical Volume(ECORE) = vol_eddy[];

Physical Surface(SKINECORE) = CombinedBoundary{Volume{vol_eddy[]};};
Physical Surface(SKINICORE) = CombinedBoundary{Volume{vol_core[]};};
Physical Surface(SKINCOIL) = CombinedBoundary{Volume{vol_coil[]};};
Physical Surface(SURF_AIROUT) = CombinedBoundary{Volume{vol_box[]};};
Physical Surface(SURF_ELEC0) = {coil_crs[]};

