//SetFactory("OpenCASCADE");
SetFactory("Built-in");
Mesh.Optimize = 1; // optimize quality of tetrahedra
Mesh.VolumeEdges = 0; // hide volume edges
Geometry.ExactExtrusion = 0; // to allow rotation of extruded shapes
Solver.AutoMesh = 2; // always remesh if necessary (don't reuse mesh on disk)

mm = 1.0e-3;
xy = 3 * mm;
box = (xy*1.1);
DefineConstant[
h_cu = {0.018 * mm, Name "Variables/Input/Cooper Height", Min 0.001 * mm, Max 0.01 * mm, Step 0.001 * mm},
h_di = {0.20 * mm, Name "Variables/Input/Dielectric Height", Min 0.001 * mm, Max 0.01 * mm, Step 0.001 * mm},
h_mi = {0.035 * mm, Name "Variables/Input/MicroStrip Height", Min 0.001 * mm, Max 0.01 * mm, Step 0.001 * mm},
w_mi = {0.8 * mm, Name "Variables/Input/MicroStrip Width", Min 0.01 * mm, Max 0.1 * mm, Step 0.001 * mm}
];


//-----------------------
// Cooper
Point(1) = {-xy/2, -xy/2, -h_cu, mm};
Point(2) = {-xy/2, xy/2, -h_cu, mm};
Point(3) = {xy/2, xy/2, -h_cu, mm};
Point(4) = {xy/2, -xy/2, -h_cu, mm};

Line(300) = {1, 2};
Line(301) = {2, 3};
Line(302) = {3, 4};
Line(303) = {4, 1};
Line Loop(304) = {300, 301, 302, 303}; Plane Surface(1) = {304};  // Surface 1. Bottom


Point(5) = {-xy/2, -xy/2, 0, mm};
Point(6) = {-xy/2, xy/2, 0, mm};
Point(7) = {xy/2, xy/2, 0, mm};
Point(8) = {xy/2, -xy/2, 0, mm};

Line(305) = {5,6};
Line(306) = {6,7};
Line(307) = {7,8};
Line(308) = {8,5};
Line Loop(309) = {305,306,307,308}; Plane Surface(2) = {309};  // Surface 2.


Line(400) = {1,5};
Line(401) = {2,6};
Line(402) = {3,7};
Line(403) = {4,8};


//-----------------------
// Dielectric
Point(9) = {-xy/2, -xy/2, h_di, mm};
Point(10) = {-xy/2, xy/2, h_di, mm};
Point(11) = {xy/2, xy/2, h_di, mm};
Point(12) = {xy/2, -xy/2, h_di, mm};

Line(310) = {9,10};
Line(311) = {10,11};
Line(312) = {11,12};
Line(313) = {12,9};
Line Loop(314) = {310,311,312,313}; Plane Surface(3) = {314};  // Surface 3.


Line(410) = {5,9};
Line(411) = {6,10};
Line(412) = {7,11};
Line(413) = {8,12};

//-----------------------
// MicroStrip

Point(13) = {-w_mi/2, -xy/2, h_di, mm};
Point(14) = {-w_mi/2, xy/2, h_di, mm};
Point(15) = {w_mi/2, xy/2, h_di, mm};
Point(16) = {w_mi/2, -xy/2, h_di, mm};

Line(320) = {13,14};
Line(321) = {14,15};
Line(322) = {15,16};
Line(323) = {16,13};
Line Loop(324) = {320,321,322,323}; Plane Surface(4) = {324};  // Surface 4.


Point(17) = {-w_mi/2, -xy/2, h_di+h_mi, mm};
Point(18) = {-w_mi/2, xy/2, h_di+h_mi, mm};
Point(19) = {w_mi/2, xy/2, h_di+h_mi, mm};
Point(20) = {w_mi/2, -xy/2, h_di+h_mi, mm};

Line(325) = {17,18};
Line(326) = {18,19};
Line(327) = {19,20};
Line(328) = {20,17};
Line Loop(329) = {325,326,327,328}; Plane Surface(5) = {329};  // Surface 5.


Line(420) = {13,17};
Line(421) = {14,18};
Line(422) = {15,19};
Line(423) = {16,20};

//-----------------------
// Faces
/// Cooper
Line Loop(501) = {303, 400, -308, -403}; Plane Surface(101) = {501};

Line Loop(502) = {300, 401, -305, -400}; Plane Surface(102) = {502};

Line Loop(503) = {-301, 401, 306, -402}; Plane Surface(103) = {503};

Line Loop(504) = {-302, 402, 307, -403}; Plane Surface(104) = {504};

/// Dielectric
Line Loop(511) = {308, 410, -313, -413}; Plane Surface(111) = {511};

Line Loop(512) = {305, 411, -310, -410}; Plane Surface(112) = {512};

Line Loop(513) = {-306, 411, 311, -412}; Plane Surface(113) = {513};

Line Loop(514) = {-307, 412, 312, -413}; Plane Surface(114) = {514};

/// MicroStrip
Line Loop(521) = {323, 420, -328, -423}; Plane Surface(121) = {521};

Line Loop(522) = {320, 421, -325, -420}; Plane Surface(122) = {522};

Line Loop(523) = {-321, 421, 326, -422}; Plane Surface(123) = {523};

Line Loop(524) = {-322, 422, 327, -423}; Plane Surface(124) = {524};

//-----------------------
// Volumes
/// Cooper
Surface Loop(1) = {1, 102, 103, 104, 101, 2};
Volume(1) = {1};

/// Dielectric
Surface Loop(2) = {2, 112, 113, 114, 111, 3};
Volume(2) = {2};

/// MircroStrip
Surface Loop(3) = {4, 122, 123, 124, 121, 5};
Volume(3) = {3};

//-----------------------
// Board Surface
Line(330) = {13,9};
Line(331) = {10,14};
Line Loop(530) = {330,310,331,-320}; Plane Surface(130) = {530};

Line(332) = {16,12};
Line(333) = {11,15};
Line Loop(531) = {332, -312, 333, 322}; Plane Surface(131) = {531};

Surface Loop(600) = {1,101,102,103,104,111,112,113,114,130,121,122,123,124,131,5};

//-----------------------
// Outter Shell
Point(21) = {0, 0, 0, mm};
Point(22) = {-box, 0, 0, mm};
Point(23) = {0, box, 0, mm};
Point(24) = {box, 0, 0, mm};
Point(25) = {0, -box, 0, mm};
Point(26) = {0, 0, box, mm};
Point(27) = {0, 0, -box, mm};
Circle(424) = {22, 21, 23};
Circle(425) = {23, 21, 24};
Circle(426) = {24, 21, 25};
Circle(427) = {25, 21, 22};
Circle(428) = {22, 21, 26};
Circle(429) = {26, 21, 24};
Circle(430) = {24, 21, 27};
Circle(431) = {27, 21, 22};
Circle(432) = {25, 21, 26};
Circle(433) = {26, 21, 23};
Circle(434) = {23, 21, 27};
Circle(435) = {27, 21, 25};

Line Loop(340) = {432, 429, 426}; Surface(140) = {340};
Line Loop(341) = {429, -425, -433}; Surface(141) = {341};
Line Loop(342) = {433, -424, 428}; Surface(142) = {342};
Line Loop(343) = {-428, -427, 432}; Surface(143) = {343};
Line Loop(344) = {-425, 434, -430}; Surface(144) = {344};
Line Loop(345) = {430, 435, -426}; Surface(145) = {345};
Line Loop(346) = {-435, 431, -427}; Surface(146) = {346};
Line Loop(347) = {-431, -434, -424}; Surface(147) = {347};

Surface Loop(601) = {140, 141, 142, 143, 144, 145, 146, 147}; // External surface

Volume(4) = {601, 600};  // Inner volume


//-----------------------
// Physical entities
Physical Volume(1) = {1};  //  Cooper
Physical Volume(2) = {2};  //  Dielectric
Physical Volume(3) = {3};  //  MicroStrip
Physical Volume(4) = {4};  //  Air
Physical Surface(5) = {601}; // Surf Inf
Physical Surface(6) = {2};  // Bottom face of the dielectric/

Characteristic Length {1, 2, 3, 4, 18, 14, 19, 15, 20, 16, 17, 13} = 0.1*mm;
Characteristic Length {9, 10, 11, 12} = 0.5*mm;

