
mm  = 0.001;
gap = 1*mm;

lc_gap  = 1*mm;
lc_core = 9*mm;
lc_ind  = 8*mm;
lc_air  = 50*mm/2;
lc_plate = 3*mm;

//coil cross section
lc0 = lc_core;
Rin = 30*mm;
Rout = 40*mm;
zoffs = 40*mm;
width = 10*mm;
Function Coil
    p1 = newp; Point(p1) = {0,0,0};
    p2 = newp; Point(p2) = {Rin, 0, 0, lc0};
    p3 = newp; Point(p3) = {0, Rin, 0, lc0};
    p4 = newp; Point(p4) = {-Rin, 0, 0, lc0};
    p5 = newp; Point(p5) = {0, -Rin, 0, lc0};

    p6 = newp; Point(p6) = {Rout, 0, 0, lc0};
    p7 = newp; Point(p7) = {0, Rout, 0, lc0};
    p8 = newp; Point(p8) = {-Rout, 0, 0, lc0};
    p9 = newp; Point(p9) = {0, -Rout, 0, lc0};

    //the r=0.3 circle
    l1 = newl; Circle(l1) = {p2,p1,p3};
    l2 = newl; Circle(l2) = {p3,p1,p4};
    l3 = newl; Circle(l3) = {p4,p1,p5};
    l4 = newl; Circle(l4) = {p5,p1,p2};
    ll10 = newll; Line Loop(ll10) = {l1,l2,l3,l4};

    //the r=0.4 circle
    l5 = newl; Circle(l5) = {p6,p1,p7};
    l6 = newl; Circle(l6) = {p7,p1,p8};
    l7 = newl; Circle(l7) = {p8,p1,p9};
    l8 = newl; Circle(l8) = {p9,p1,p6};
    ll11 = newll; Line Loop(ll11) = {l5,l6,l7,l8};

    p11 = newp; Point(p11) = {0,0,width};
    p12 = newp; Point(p12) = {Rin, 0, width, lc0};
    p13 = newp; Point(p13) = {0, Rin, width, lc0};
    p14 = newp; Point(p14) = {-Rin, 0, width, lc0};
    p15 = newp; Point(p15) = {0, -Rin, width, lc0};

    p16 = newp; Point(p16) = {Rout, 0, width, lc0};
    p17 = newp; Point(p17) = {0, Rout, width, lc0};
    p18 = newp; Point(p18) = {-Rout, 0, width, lc0};
    p19 = newp; Point(p19) = {0, -Rout, width, lc0};

    //the r=0.3 circle
    l11 = newl; Circle(l11) = {p12,p11,p13};
    l12 = newl; Circle(l12) = {p13,p11,p14};
    l13 = newl; Circle(l13) = {p14,p11,p15};
    l14 = newl; Circle(l14) = {p15,p11,p12};
    ll12 = newll; Line Loop(ll12) = {l11,l12,l13,l14};

    //the r=0.4 circle
    l15 = newl; Circle(l15) = {p16,p11,p17};
    l16 = newl; Circle(l16) = {p17,p11,p18};
    l17 = newl; Circle(l17) = {p18,p11,p19};
    l18 = newl; Circle(l18) = {p19,p11,p16};
    ll13 = newll; Line Loop(ll13) = {l15,l16,l17,l18};

    l402 = newl; Line(l402) = {p2,p12};
    l403 = newl; Line(l403) = {p3,p13};
    l404 = newl; Line(l404) = {p4,p14};
    l405 = newl; Line(l405) = {p5,p15};
    l406 = newl; Line(l406) = {p6,p16};
    l407 = newl; Line(l407) = {p7,p17};
    l408 = newl; Line(l408) = {p8,p18};
    l409 = newl; Line(l409) = {p9,p19};

    ll401 = newll; Line Loop(ll401) = {-l1, l402, l11, -l403};
    s401 = news; Ruled Surface(s401) = {ll401};

    ll402 = newll; Line Loop(ll402) = {-l2, l403, l12, -l404};
    s402 = news; Ruled Surface(s402) = {ll402};

    ll403 = newll; Line Loop(ll403) = {-l3, l404, l13, -l405};
    s403 = news; Ruled Surface(s403) = {ll403};

    ll404 = newll; Line Loop(ll404) = {-l4, l405, l14, -l402};
    s404 = news; Ruled Surface(s404) = {ll404};


    ll405 = newll; Line Loop(ll405) = {-l5, l406, l15, -l407};
    s405 = news; Ruled Surface(s405) = {ll405};

    ll406 = newll; Line Loop(ll406) = {-l6, l407, l16, -l408};
    s406 = news; Ruled Surface(s406) = {ll406};

    ll407 = newll; Line Loop(ll407) = {-l7, l408, l17, -l409};
    s407 = news; Ruled Surface(s407) = {ll407};

    ll408 = newll; Line Loop(ll408) = {-l8, l409, l18, -l406};
    s408 = news; Ruled Surface(s408) = {ll408};

    s7 = news; Plane Surface(s7) = {ll11,ll10};
    s8 = news; Plane Surface(s8) = {ll13,ll12};
    //out[] = Extrude{0, 0, 10*mm}{
    //    Surface{7};
    //};
    //Physical Volume(1) = {out[1]};

    resultSurface = newsl; Surface Loop(resultSurface) = {s7, s8, s401, s402, s403, s404, s405, s406, s407, s408};
    resultVolume = newv; Volume(resultVolume) = {resultSurface};
    Translate{coilCenter[0],coilCenter[1],coilCenter[2]-width/2}{Volume{resultVolume};}
    Rotate{ {coilRotAxis[0], coilRotAxis[1], coilRotAxis[1]},
        {coilCenter[0], coilCenter[1], coilCenter[2]}, coilRotAngle}{Volume{resultVolume};}
    Printf("Coil: %d %d",resultVolume, resultSurface);
Return

// coil 1
coilCenter[] = {0*mm, 70*mm, 100*mm};
coilRotAxis[] = {1,0,0};
coilRotAngle = Pi/2;
width = 10*mm;
Rin = 40*mm;
Rout = 50*mm;
Call Coil;
Printf("AfterCoil: %d %d",resultVolume, resultSurface);
surfInd = resultSurface;
volumeInd = resultVolume;

// coil 2
coilCenter[] = {0*mm, 0*mm, 0*mm};
coilRotAxis[] = {1,0,0};
coilRotAngle = 0;
width = 10*mm;
Rin = 75*mm;
Rout = 85*mm;
Call Coil;
Printf("AfterCoil: %d %d",resultVolume, resultSurface);
surfInd2 = resultSurface;
volumeInd2 = resultVolume;

// metal ring
coilCenter[] = {0,0,-102.5*mm};
coilRotAxis[] = {1,0,0};
coilRotAngle = 0;
width = 55*mm;
Rin = 75*mm;
Rout = 90*mm;
Call Coil;
Printf("AfterCoil: %d %d",resultVolume, resultSurface);
surfCore = resultSurface;
volCore = resultVolume;

// metal ring2
coilCenter[] = {0,0,-175*mm};
coilRotAxis[] = {1,0,0};
coilRotAngle = 0;
width = 80*mm;
Rin = 75*mm;
Rout = 90*mm;
Call Coil;
Printf("AfterCoil: %d %d",resultVolume, resultSurface);
surfCore2 = resultSurface;
volCore2 = resultVolume;


//Inductor 


scale = 4 ;
//air
axl=-150*mm;  //left of airbox 
axr=150*mm;  //right of airbox 
ayb=-150*mm; //bottom of airbox
ayt=150*mm;  //top of airbox
azb=-300*mm; //back of airbox
azf=200*mm;  //front of airbos
 
Point(80) = {axl, ayb, azf, lc_air};
Point(81) = {axr, ayb, azf, lc_air};
Point(82) = {axr, ayt, azf, lc_air};
Point(83) = {axl, ayt, azf, lc_air};

Point(84) = {axl, ayb, azb, lc_air};
Point(85) = {axr, ayb, azb, lc_air};
Point(86) = {axr, ayt, azb, lc_air};
Point(87) = {axl, ayt, azb, lc_air};

Line(80) = {80,81};
Line(81) = {81,82};
Line(82) = {82,83};
Line(83) = {83,80};

Line(84) = {84,85};
Line(85) = {85,86};
Line(86) = {86,87};
Line(87) = {87,84};

Line(180) = {80,84};
Line(181) = {81,85};
Line(182) = {82,86};
Line(183) = {83,87};


Line Loop(280) = {80, 81, 82, 83};
Plane Surface(281)={280};
Line Loop(282) = {81, 182, -85, -181};
Plane Surface(283)={282};
Line Loop(284) = {84, 85, 86, 87};
Plane Surface(285)={284};
Line Loop(286) = {83, 180, -87, -183};
Plane Surface(287)={286};
Line Loop(288) = {80, 181, -84, -180};
Plane Surface(289)={288};
Line Loop(290) = {82, 183, -86, -182};
Plane Surface(291)={290};

Surface Loop(292) = {281, 283, 285, 287, 289, 291};
//Surface Loop
Volume(293)={292, surfInd, surfInd2, surfCore, surfCore2};


//physical stuff
Physical Volume(1000) = {volCore, volCore2}; //Left half core
Physical Volume(1001) = {volumeInd}; //Inductor 
Physical Volume(1007) = {volumeInd2}; //Inductor 
Physical Volume(1003) = {293}; //Air

//Skin of Air to set boundary for magnetic potential
Physical Surface(1004) = {281, 283, 285, 287, 289, 291}; //outside edge of air (collection of surfaces)


Physical Surface(1005) = {surfCore, surfCore2}; //skin core
Printf("surfCore %d %d\n", surfCore, surfCore2);



