r1 = 1;

cubeL = r1/Sqrt(3); //half of the actual cube length
cubeP = r1/Sqrt(2); //half of the actual cube length

N = 6+1; //Needs to be an odd number!
N2 = Floor(N/2)+1; //Needs to be an even number!
N1 = 6; //sphere to box connecting points
N3 = 6; //outerbox points


//Progression values
p1=1.15; //angular direction progression inside the cube
p2=1.28; //progression in verticle direction
p3=1.28; //progression in right direction(outlet)
p4=1.28; //progression in left direction(inlet)


Point(1) = {0,0,0}; //centre?
Point(2) = {cubeP,cubeP,0}; //front right-top
Point(3) = {cubeP,-cubeP,0}; //front right-bottom
Point(4) = {-cubeP,-cubeP,0}; //front left-bottom
Point(5) = {-cubeP,cubeP,0}; //front left-top

Circle(1) = {2,1,3}; //front-left loop
Circle(2) = {3,1,4}; //front-bottom loop
Circle(3) = {4,1,5}; //front-right loop
Circle(4) = {5,1,2}; //front-top loop

Point(6) = {-cubeL,-cubeL,cubeL}; //back  left-bottom
Point(7) = {-cubeL,cubeL,cubeL}; //back left -top

Circle(6) = {7,1,6}; //back-left loop
Circle(7) = {4,1,6}; //bottom -left loop
Circle(5) = {7,1,5}; //top -left loop

Point(8) = {cubeL,cubeL,cubeL}; //back right-top
Point(9) = {cubeL,-cubeL,cubeL}; //back right-bottom

Circle(8) = {2,1,8}; //top right loop
Circle(9) = {9,1,8}; //back right loop
Circle(10) = {3,1,9}; //bottom right loop

Circle(11) = {6,1,9}; //bottom back loop
Circle(12) = {7,1,8}; //top back loop

Transfinite Line {1,2,3,4,6,9,11,12} = N Using Progression 1; //TANGENT
Transfinite Line {5,7,8,10} = N2 Using Progression 1; //TANGENT

//front face
Line Loop(1) = {1,2,3,4};
//Ruled Surface(100) = {-1};
//Transfinite Surface {100};
//Recombine Surface {100};

//left face - inlet
Line Loop(2) = {3,-5,6,-7};
Ruled Surface(200) = {-2};
Transfinite Surface {200};
Recombine Surface {200} ;

//bottom face
Line Loop(3) = {2,7,11,-10};
Ruled Surface(300) = {-3};
Transfinite Surface {300};
Recombine Surface {300} ;

//top
Line Loop(4) = {4,8,-12,5};
Ruled Surface(400) = {-4};
Transfinite Surface {400};
Recombine Surface {400} ;

//right-outlet
Line Loop(5) = {1,10,9,-8};
Ruled Surface(500) = {-5};
Transfinite Surface {500};
Recombine Surface {500} ;

//back
Line Loop(6) = {11,9,-12,6};
Ruled Surface(600) = {-6};
Transfinite Surface {600};
Recombine Surface {600} ;
/////////////////////

//small cube size
t=3; //small cube size as a multiplication factor
d=t*r1;

//Creating squared front face - 10series
Point(1010) = {d,d,0};
Point(1011) = {d,-d,0};
Point(1012) = {-d,-d,0};
Point(1013) = {-d,d,0};
Line(10100) = {1010,1011};
Line(10101) = {1011,1012};
Line(10102) = {1012,1013};
Line(10103) = {1013,1010};

Line(10104) = {2,1010};
Line(10105) = {3,1011};
Line(10106) = {4,1012};
Line(10107) = {5,1013};

Transfinite Line {10100,10101,10102,10103} = N Using Progression 1; //OUTER SQUARE LINE (FRONT)
Transfinite Line {10104,10105,10106,10107} = N1 Using Progression p1; //SPHERE AND CUBE CONNECTING LINES (FRONT)


//Creating squared back face - 20series
Point(2010) = {d,d,d}; //back-right corner
Point(2011) = {d,-d,d}; // back-left corner
Point(2012) = {-d,-d,d}; //bottom-left corner
Point(2013) = {-d,d,d}; //bottom-right corner
Line(20100) = {2010,2011};
Line(20101) = {2011,2012};
Line(20102) = {2012,2013};
Line(20103) = {2013,2010};

Line(20104) = {8,2010};
Line(20105) = {9,2011};
Line(20106) = {6,2012};
Line(20107) = {7,2013};

Transfinite Line {20100,20101,20102,20103} = N Using Progression 1; //OUTER SQUARE LINES (BACK)
Transfinite Line {20104,20105,20106,20107} = N1 Using Progression p1; //SPHERE AND CUBE CONNECTING LINE (BACK)


//Creating squared inlet face - 30series

Line(30101) = {1012,2012};
Line(30103) = {2013,1013};

Transfinite Line {30101,30103} = N2 Using Progression 1; // OUTER SQUARE LINE (INLET)

//Creating squared outlet face - 40series

Line(40101) = {2011,1011};
Line(40103) = {2010,1010};

Transfinite Line {40101,40103} = N2 Using Progression 1; //OUTER SQUARE LINE (OUTLET)

Line Loop(107) = {1,10105,-10100,-10104}; //front-right side trap
Line Loop(108) = {2,10106,-10101,-10105}; //front-bottom  trap
Line Loop(109) = {3,10107,-10102,-10106}; //front -left side trap
Line Loop(1010) = {4,10104,-10103,-10107}; //front - top trap

Plane Surface (10700) = {-107}; //front-right side trap
Transfinite Surface {10700};
Recombine Surface {10700} ;

Plane Surface (10701) = {-108}; //front-bottom  trap
Transfinite Surface {10701};
Recombine Surface {10701} ;

Plane Surface (10702) = {109}; //front -left side trap
Transfinite Surface {10702};
Recombine Surface {10702} ;

Plane Surface (10703) = {1010}; //front - top trap
Transfinite Surface {10703};
Recombine Surface {10703} ;


//Back surface loop
Line Loop(207) = {-9,20105,-20100,-20104}; //back-right side trap
Line Loop(208) = {-11,20106,-20101,-20105}; //back - bottom trap
Line Loop(209) = {-6,20107,-20102,-20106}; // back - left side trap
Line Loop(2010) = {12,20104,-20103,-20107}; //back- top trap
Line Loop(2011) = {20100,20101,20102,20103}; //back - squarei

Plane Surface (20700) = {-207}; //back-right side trap
Transfinite Surface {20700};
Recombine Surface {20700} ;

Plane Surface (20701) = {-208};//back - bottom trap
Transfinite Surface {20701};
Recombine Surface {20701} ;

Plane Surface (20702) = {209};// back - left side trap
Transfinite Surface {20702};
Recombine Surface {20702} ;

Plane Surface (20703) = {2010};//back- top trap
Transfinite Surface {20703};
Recombine Surface {20703} ;

Plane Surface (20705) = {2011};//back - square
Transfinite Surface {20705};
Recombine Surface {20705} ;


//inlet loops
Line Loop(309) = {5,10107,-30103,-20107}; //topinlet loop
Line Loop(3010) = {7,20106,-30101,-10106};//bottom inlet loop
Line Loop(3011) = {-10102,30101,20102,30103};//inletloop


Plane Surface (30700) = {309};
Transfinite Surface {30700};
Recombine Surface {30700} ;

Plane Surface (30701) = {-3010};
Transfinite Surface {30701};
Recombine Surface {30701} ;

Plane Surface (30702) = {-3011};
Transfinite Surface {30702};
Recombine Surface {30702} ;

//outletloops
Line Loop(409) = {-8,10104,-40103,-20104}; //top-outlet loop
Line Loop(4010) = {-10,10105,-40101,-20105};//bottom-outlet loop
Line Loop(4011) = {10100,-40101,-20100,40103};//outletloop

Plane Surface (40700) = {409};
Transfinite Surface {40700};
Recombine Surface {40700};

Plane Surface (40701) = {-4010};
Transfinite Surface {40701};
Recombine Surface {40701};

Plane Surface (40702) = {4011};
Transfinite Surface {40702};
Recombine Surface {40702};



//toploops - 50 series
Line Loop(5011) = {10103,-40103,-20103,30103};//outletloop

Plane Surface (50700) = {5011};
Transfinite Surface {50700};
Recombine Surface {50700};



//bottom loops - 60 series
Line Loop(6011) = {10101,30101,-20101,40101};//outletloop

Plane Surface (60700) = {6011};
Transfinite Surface {60700};
Recombine Surface {60700};



//Creating volumes

//back
Surface Loop (2000) = {600,20700,20701,20702,20703,20705};
Volume (20000) = {2000};

//inlet
Surface Loop (3000) = {200,30700,30701,30702,10702,20702};
Volume (30000) = {3000};

//outlet
Surface Loop (4000) = {500,40700,40701,40702,10700,-20700};
Volume (40000) = {4000};

//top
Surface Loop (5000) = {400,50700,10703,20703,30700,40700};
Volume (50000) = {5000};

//bottom
Surface Loop (6000) = {300,60700,10701,-20701,30701,40701};
Volume (60000) = {6000};

Transfinite Volume {20000,30000,40000,50000,60000};

Physical Surface("<inlet>") = {30702};
Physical Surface("<outlet>") = {40702};
Physical Surface("<walls>") = {20705};
Physical Volume("<flow_domain>") = {20000,30000,40000,50000,60000};
Physical Surface("<symmetry_plane>") = {10703, 10700, 10702, 10701};
Physical Surface("<top>") = {50700};
Physical Surface("<bottom>") = {60700};
Physical Surface("<particle_wall>") = {400, 300, 200, 500, 600};


//Mesh.RecombinationAlgorithm = 0 ;
//Mesh.ElementOrder = 1;
//Mesh.SecondOrderLinear = 1;
//Mesh.Algorithm = 8;
//Mesh.Algorithm3D = 1;
//Mesh.ElementOrder = 1;
