ElectrodesRes = 0.03;
OxidRes = 0.03;
AirBoxRes = 0.03;
res = 0.005;

ElectrodeHeight = 0.03; AirBoxHeight = 0.1; OxidLayerHeight = 0.05;
XLeft=-0.27; XRight=0.22; YUpper=0.4; YLower=-0.4;

Point(1)={XLeft, 0.135, 0, ElectrodesRes};
Point(2)={XRight, 0.135, 0, ElectrodesRes};
Point(3)={XLeft, YUpper, 0, ElectrodesRes};
Point(4)={XRight, YUpper, 0, ElectrodesRes};
Point(5)={XLeft, 0.135, ElectrodeHeight, ElectrodesRes};
Point(6)={XRight, 0.135, ElectrodeHeight, ElectrodesRes};
Point(7)={XLeft, YUpper, ElectrodeHeight, ElectrodesRes};
Point(8)={XRight, YUpper, ElectrodeHeight, ElectrodesRes};

Line(1) = {5,6};
Line(2) = {6,8};
Line(3) = {8,7};
Line(4) = {7,5};
Line(5) = {5,1};
Line(6) = {7,3};
Line(7) = {8,4};
Line(8) = {6,2};
Line(9) = {2,1};
Line(10) = {1,3};
Line(11) = {3,4};
Line(12) = {4,2};

Line Loop(1) = {1,2,3,4};
Plane Surface(1) = {1};
Line Loop(2) = {4,5,10,-6};
Plane Surface(2) = {2};
Line Loop(3) = {3,6,11,-7};
Plane Surface(3) = {3};
Line Loop(4) = {2,7,12,-8};
Plane Surface(4) = {4};
Line Loop(5) = {1,8,9,-5};
Plane Surface(5) = {5};
Line Loop(6) = {9,10,11,12};
Plane Surface(6) = {6};

Physical Surface(100005)={1};
Physical Surface(100006)={5};

Surface Loop(1) = {3,2,4,1,5,6};
Volume(100001) = {1};

Physical Volume(100001)={100001};


Point(9)={XLeft, YLower, 0, ElectrodesRes};
Point(10)={XRight, YLower, 0, ElectrodesRes};
Point(11)={XRight, -0.15, 0, ElectrodesRes};
Point(12)={XLeft, -0.15, 0, ElectrodesRes};
Point(13)={XLeft, YLower, ElectrodeHeight, ElectrodesRes};
Point(14)={XRight, YLower, ElectrodeHeight, ElectrodesRes};
Point(15)={XRight, -0.15, ElectrodeHeight, ElectrodesRes};
Point(16)={XLeft, -0.15, ElectrodeHeight, ElectrodesRes};

Line(13) = {16,15};
Line(14) = {15,14};
Line(15) = {14,13};
Line(16) = {13,16};
Line(17) = {16,12};
Line(18) = {15,11};
Line(19) = {14,10};
Line(20) = {13,9};
Line(21) = {9,12};
Line(22) = {12,11};
Line(23) = {11,10};
Line(24) = {10,9};

Line Loop(7) = {13,14,15,16};
Plane Surface(7) = {7};
Line Loop(8) = {13,18,-22,-17};
Plane Surface(8) = {8};
Line Loop(9) = {16,17,-21,-20};
Plane Surface(9) = {9};
Line Loop(10) = {14,19,-23,-18};
Plane Surface(10) = {10};
Line Loop(11) = {15,20,-24,-19};
Plane Surface(11) = {11};
Line Loop(12) = {24,21,22,23};
Plane Surface(12) = {12};

Surface Loop(2) = {7,8,10,9,11,12};
Volume(100002) = {2};

Physical Volume(100002)={100002};


Point(17)={XLeft, YLower, -OxidLayerHeight, OxidRes};
Point(18)={XRight, YLower, -OxidLayerHeight, OxidRes};
Point(19)={XRight, YUpper, -OxidLayerHeight, OxidRes};
Point(20)={XLeft, YUpper, -OxidLayerHeight, OxidRes};

Line(25) = {3,20};
Line(26) = {20,17};
Line(27) = {17,9};
Line(28) = {12,1};
Line(29) = {17,18};
Line(30) = {18,10};
Line(31) = {18,19};
Line(32) = {19,4};
Line(33) = {2,11};
Line(34) = {19,20};

Line Loop(13) = {9,-28,22,-33};
Plane Surface(13) = {13};
Line Loop(14) = {28,10,25,26,27,21};
Plane Surface(14) = {14};
Line Loop(15) = {27,-24,-30,-29};
Plane Surface(15) = {15};
Line Loop(16) = {30,-23,-33,-12,-32,-31};
Plane Surface(16) = {16};
Line Loop(17) = {32,-11,25,-34};
Plane Surface(17) = {17};
Line Loop(18) = {31,34,26,29};
Plane Surface(18) = {18};

Surface Loop(100003) = {14,17,16,15,18,13,12,6};
Volume(100003) = {100003};


Point(21)={XLeft, YLower, AirBoxHeight, AirBoxRes};
Point(22)={XRight, YLower, AirBoxHeight, AirBoxRes};
Point(23)={XRight, YUpper, AirBoxHeight, AirBoxRes};
Point(24)={XLeft, YUpper, AirBoxHeight, AirBoxRes};

Line(35) = {24,7};
Line(36) = {24,21};
Line(37) = {21,13};
Line(38) = {21,22};
Line(39) = {22,14};
Line(40) = {22,23};
Line(41) = {23,8};
Line(42) = {23,24};

Line Loop(19) = {35,4,5,-28,-17,-16,-37,-36};
Plane Surface(19) = {19};
Line Loop(20) = {38,39,15,-37};
Plane Surface(20) = {20};
Line Loop(21) = {39,-14,18,-33,-8,2,-41,-40};
Plane Surface(21) = {21};
Line Loop(22) = {41,3,-35,-42};
Plane Surface(22) = {22};
Line Loop(23) = {40,42,36,38};
Plane Surface(23) = {23};


Point(25)={XLeft+0.001, 0, 0.001, AirBoxRes};
Point(26)={XRight+0.001, 0, 0.001, AirBoxRes};
Point(27)={XLeft+0.001, 0, AirBoxHeight-0.001, AirBoxRes};
Point(28)={XRight+0.001, 0, AirBoxHeight-0.001, AirBoxRes};


Function Particle

p1 = newp; Point(p1) = {x,  y,  z,  res} ;
p2 = newp; Point(p2) = {x+r,y,  z,  res} ;
p3 = newp; Point(p3) = {x,  y+r,z,  res} ;
p4 = newp; Point(p4) = {x,  y,  z+r,res} ;
p5 = newp; Point(p5) = {x-r,y,  z,  res} ;
p6 = newp; Point(p6) = {x,  y-r,z,  res} ;
p7 = newp; Point(p7) = {x,  y,  z-r,res} ;

c1 = newreg; Circle(c1) = {p2,p1,p7};
c2 = newreg; Circle(c2) = {p7,p1,p5};
c3 = newreg; Circle(c3) = {p5,p1,p4};
c4 = newreg; Circle(c4) = {p4,p1,p2};
c5 = newreg; Circle(c5) = {p2,p1,p3};
c6 = newreg; Circle(c6) = {p3,p1,p5};
c7 = newreg; Circle(c7) = {p5,p1,p6};
c8 = newreg; Circle(c8) = {p6,p1,p2};
c9 = newreg; Circle(c9) = {p7,p1,p3};
c10 = newreg; Circle(c10) = {p3,p1,p4};
c11 = newreg; Circle(c11) = {p4,p1,p6};
c12 = newreg; Circle(c12) = {p6,p1,p7};

l1 = newreg; Line Loop(l1) = {c5,c10,c4};   Ruled Surface(newreg) = {l1};
l2 = newreg; Line Loop(l2) = {c9,-c5,c1};   Ruled Surface(newreg) = {l2};
l3 = newreg; Line Loop(l3) = {c12,-c8,-c1}; Ruled Surface(newreg) = {l3};
l4 = newreg; Line Loop(l4) = {c8,-c4,c11};  Ruled Surface(newreg) = {l4};
l5 = newreg; Line Loop(l5) = {-c10,c6,c3};  Ruled Surface(newreg) = {l5};
l6 = newreg; Line Loop(l6) = {-c11,-c3,c7}; Ruled Surface(newreg) = {l6};
l7 = newreg; Line Loop(l7) = {-c2,-c7,-c12};Ruled Surface(newreg) = {l7};
l8 = newreg; Line Loop(l8) = {-c6,-c9,c2};  Ruled Surface(newreg) = {l8};


theloops[u] = newreg ;
Surface Loop(theloops[u]) = {l8+1,l5+1,l1+1,l2+1,l3+1,l7+1,l6+1,l4+1};
thehole = newreg ;
Volume(thehole) = theloops[u] ;

Return // end of function

Ymax = 14; Xmax = 50 ;
FixR = 0.005;
d = 0.0015;

For s In {1:Xmax}
For t In {1:Ymax}
  RandNbr = 0.001 ;
  r = 0.005;
  z = r + d;
  a = (-1)^(s);
  If (a == -1)
  x = -0.12 + 0.5*((s-1)*(2*FixR + d));
  y = -0.15 + FixR + d + 2*((t-1)*Sqrt((2*FixR + d)^(2) - (FixR + 0.5*d)^(2)));
  EndIf
  If (a == 1)
  x = -0.12 - 0.5*d + 0.5*((s-1)*(2*FixR + d));
  y = -0.15 + FixR + d + Sqrt((2*FixR+d)^(2) - (FixR + 0.5*d)^(2)) + 2*((t-1)*Sqrt((2*FixR+d)^(2) - (FixR + 0.5*d)^(2)));
  EndIf
  u = t + (s-1)*Ymax;
  Call Particle;
  Physical Volume (u) = thehole ;
EndFor

EndFor

theloops[0] = newreg ;

Surface Loop(theloops[0]) = {13,5,1,8,7,20,23,21,22,19};

Volume(100004) = {theloops[]};

Physical Volume(100004) = {100004};
