
paramaille= 5.000000000000000e+00;
lambda_m  = 6.000000000000000e+03;
nm        = 1.000000000000000e+00;

period_x  = 2.000000000000000e+03 ;
period_y  = 2.000000000000000e+03 ;
PML_top   = 6.000000000000000e+03 ;
PML_bot   = 6.000000000000000e+03;

rx        = 4.500000000000000e+02;
ry        = 4.500000000000000e+02;
rz        = 5.000000000000000e+01;
Deph      = 0.000000000000000e+00;
a_latz    = 2.000000000000000e+02;

eps_re_L_1 = 1.000000000000000e+00;
eps_im_L_1 = 0.000000000000000e+00;
thick_L_1  = 1.000000000000000e+03;
hh_L_1     = 9.600000000000000e+03;

eps_re_L_2 = 1.000000000000000e+00;
eps_im_L_2 = 0.000000000000000e+00;
thick_L_2  = 1.000000000000000e+03;
hh_L_2     = 8.600000000000000e+03;

eps_re_L_3 = 1.000000000000000e+00;
eps_im_L_3 = 0.000000000000000e+00;
thick_L_3  = 1.000000000000000e+03;
hh_L_3     = 7.600000000000000e+03;

eps_re_L_4 = 7.182400000000001e+00;
eps_im_L_4 = 0.000000000000000e+00;
thick_L_4  = 2.600000000000000e+03;
hh_L_4     = 5.000000000000000e+03;

eps_re_L_5 = 5.856400000000000e+00;
eps_im_L_5 = 0.000000000000000e+00;
thick_L_5  = 5.000000000000000e+03;
hh_L_5     = 0.000000000000000e+00;

eps_re_L_6 = 1.000000000000000e+00;
eps_im_L_6 = 0.000000000000000e+00;
thick_L_6  = 1.000000000000000e+03;
hh_L_6     = -1.000000000000000e+03;

eps_re_InSin1 = -1.167886969526316e+03;
eps_im_InSin1 = -2.060171273684212e+02;
thick_deposit = 1.000000000000000e+02;
eps_re_InSin2 = -1.167886969526316e+03;
eps_im_InSin2 = -2.060171273684212e+02;
PML_bot_hh = hh_L_6-PML_bot;
PML_top_hh = hh_L_1+thick_L_1;

SetFactory("OpenCASCADE");

L1_n     = Sqrt(eps_re_L_1);
L2_n     = Sqrt(eps_re_L_2);
L3_n     = Sqrt(eps_re_L_3);
L4_n     = Sqrt(eps_re_L_4);
L5_n     = Sqrt(eps_re_L_5);
L6_n     = Sqrt(eps_re_L_6);
InSin1_n = 3;//Sqrt(Fabs(eps_re_InSin1));
InSin2_n = 3;//Sqrt(Fabs(eps_re_InSin2));

PML_lc     = lambda_m/(paramaille*.5);
L1_lc      = lambda_m/(paramaille*L1_n);
L2_lc      = lambda_m/(paramaille*L2_n);
L3_lc      = lambda_m/(paramaille*L3_n);
L4_lc      = lambda_m/(paramaille*L4_n);
L5_lc      = lambda_m/(paramaille*L5_n);
L6_lc      = lambda_m/(paramaille*L6_n);
InSin1_lc  = lambda_m/(paramaille*InSin1_n);
InSin2_lc  = lambda_m/(paramaille*InSin2_n);

Box(1) = {-period_x/2,-period_y/2, PML_bot_hh,period_x,period_y, PML_bot  };
Box(2) = {-period_x/2,-period_y/2,     hh_L_6,period_x,period_y, thick_L_6};
Box(3) = {-period_x/2,-period_y/2,     hh_L_5,period_x,period_y, thick_L_5};
Box(4) = {-period_x/2,-period_y/2,     hh_L_4,period_x,period_y, thick_L_4};
Box(5) = {-period_x/2,-period_y/2,     hh_L_3,period_x,period_y, thick_L_3};
Box(6) = {-period_x/2,-period_y/2,     hh_L_2,period_x,period_y, thick_L_2};
Box(7) = {-period_x/2,-period_y/2,     hh_L_1,period_x,period_y, thick_L_1};
Box(8) = {-period_x/2,-period_y/2, PML_top_hh,period_x,period_y, PML_top  };
// Box(9) = {-rx,-ry,hh_L_3               ,2*rx,2*ry, thick_deposit};
// Box(10)= {-rx,-ry,hh_L_3+thick_deposit ,2*rx,2*ry, 2*rz};
R_fillet_lat   = period_x/50;
R_fillet_top   = period_x/100;
rotate_rod = Pi/3;
// Rotate {{0, 0, 1}, {0, 0, 0}, rotate_rod} { Volume{9,10}; }

R_belly  = period_x*10;
p=newp;
Point(p+0)  = {-rx , -ry , hh_L_2, 1.0};
Point(p+1)  = { rx , -ry , hh_L_2, 1.0};
Point(p+2)  = { rx ,  ry , hh_L_2, 1.0};
Point(p+3)  = {-rx ,  ry , hh_L_2, 1.0};
Point(p+4)  = { 0        , R_belly , hh_L_2, 1.0};
Point(p+5)  = { -R_belly , 0       , hh_L_2, 1.0};
Point(p+6)  = { 0        ,-R_belly , hh_L_2, 1.0};
Point(p+7)  = { R_belly  , 0       , hh_L_2, 1.0};

l=newl;
Circle(l+0) = {p+1, p+5, p+2};
Circle(l+1) = {p+0, p+4, p+1};
Circle(l+2) = {p+3, p+7, p+0};
Circle(l+3) = {p+2, p+6, p+3};
ll=newll;
Line Loop(ll) = {l+2, l+1, l+0, l+3};
s=news;
Plane Surface(s) = {ll};
Rotate {{0, 0, 1}, {0, 0, 0}, rotate_rod} { Surface{s}; }

out1()=Extrude{0,0,thick_deposit}{ Surface{s};};
out2()=Extrude{0,0,rz}{ Surface{out1[0]};};
tag_vol_deposit = out1[1];
tag_vol_rod = out2[1];
// For k In {0:#out1()-1}
//   Printf("extrusion 1 tag%g = %g",k,out1[k]);
// EndFor
// For k In {0:#out2()-1}
//   Printf("extrusion 2 tag%g = %g",k,out2[k]);
// EndFor
Fillet {tag_vol_rod} {109,110,112,114} { R_fillet_lat }
Fillet {tag_vol_deposit} {101,102,104,106} { R_fillet_lat }
Fillet {tag_vol_rod} {123,125,127,128,126,124,121,112} { R_fillet_top }

Coherence;

Periodic Surface {33,39,44,49,54,6,60,65} = {32,38,43,48,53,1,59,64} Translate {period_x,0,0} ;
Periodic Surface {35,41,46,51,56,4,62,67} = {34,40,45,50,55,2,61,66} Translate {0,period_y,0} ;


Physical Surface("BLOCHXM",750) = {32,38,43,48,53,1,59,64};
Physical Surface("BLOCHXP",751) = {33,39,44,49,54,6,60,65};

// Bloch Y - puis +
Physical Surface("BLOCHYM",760) = {34,40,45,50,55,2,61,66};
Physical Surface("BLOCHYP",761) = {35,41,46,51,56,4,62,67};

// Diri
Physical Surface("ENDPML",770) = {36,68};

Physical Volume("PMLBOT"  ,1000)  = {1};
Physical Volume("LAYER_L6",2000)  = {2};
Physical Volume("LAYER_L5",3000)  = {3};
Physical Volume("LAYER_L4",4000)  = {4};
Physical Volume("LAYER_L3",5000)  = {5};
Physical Volume("LAYER_L2",6000)  = {13};
Physical Volume("LAYER_L1",7000)  = {7};
Physical Volume("PMLTOP"  ,8000)  = {8};
Physical Volume("ROD"     ,9000)  = {12};
Physical Volume("DEPOSIT" ,10000) = {11};

pts_PMLBOT()   = PointsOf{ Physical Volume{1000}; };
pts_LAYER_L1() = PointsOf{ Physical Volume{2000}; };
pts_LAYER_L2() = PointsOf{ Physical Volume{3000}; };
pts_LAYER_L5() = PointsOf{ Physical Volume{4000}; };
pts_LAYER_L4() = PointsOf{ Physical Volume{5000}; };
pts_LAYER_L3() = PointsOf{ Physical Volume{6000}; };
pts_LAYER_L6() = PointsOf{ Physical Volume{7000}; };
pts_PMLTOP()   = PointsOf{ Physical Volume{8000}; };
pts_ROD()      = PointsOf{ Physical Volume{9000}; };
pts_DEPOSIT()  = PointsOf{ Physical Volume{10000}; };

Characteristic Length{:}              = PML_lc   ;
Characteristic Length{pts_LAYER_L1()} = L1_lc    ;
Characteristic Length{pts_LAYER_L2()} = L2_lc    ;
Characteristic Length{pts_LAYER_L5()} = L3_lc    ;
Characteristic Length{pts_LAYER_L4()} = L4_lc    ;
Characteristic Length{pts_LAYER_L3()} = L5_lc    ;
Characteristic Length{pts_LAYER_L6()} = L6_lc    ;
Characteristic Length{pts_DEPOSIT()}  = InSin1_lc;
Characteristic Length{pts_ROD()}      = InSin2_lc;
