// Gmsh project created on Wed Mar 20 17:41:38 2019
SetFactory("OpenCASCADE");

//lc = 50;
//Mesh.CharacteristicLengthMin = lc/100;
//Mesh.CharacteristicLengthMax = lc;

Mesh.CharacteristicLengthExtendFromBoundary = 0;    // large influence!!
Mesh.CharacteristicLengthFromPoints = 1;
Mesh.CharacteristicLengthFromCurvature = 0;


// MODEL
Geometry.OCCTargetUnit = 'M';	           	// import step in "M"eters
Merge "wing_fuselage.step";
Reverse Surface { 1:8 };
wingfuelageSL               = newsl;
Surface Loop(wingfuelageSL) = {1:7,9:15};  	// 8 and 16 are surfaces on the symmetry plane; command generates surfaces 17, 18... and curves 39-44
Delete { Volume{1,2}; }    					// delete the volume
Delete { Surface{8,16, 17,18}; }  			// delete the symmetry surfaces
//delete the symmetry curves, PROBLEM: when deleting here, GMSH crashes when generating the outer volume (voloutside)!
//Delete { Curve{39:44}; }



// BOUNDING BOX
lcbox      = 1;
Point(101) = {-5, -5, -5, lcbox};
Point(102) = {-5, -5,  5, lcbox};
Point(103) = {-5,  5, -5, lcbox};
Point(104) = {-5,  5,  5, lcbox};

Line(111) = {101,103};
Line(112) = {103,104};
Line(113) = {104,102};
Line(114) = {102,101};
Line Loop(115) = {111,112,113,114};
Plane Surface(1001) = {115};
farfieldEXT[] = Extrude {10,0,0} { Surface{1001};};
farfieldVOL   = farfieldEXT[1];
farfieldBOX[] = Boundary{Volume{farfieldVOL};};
farfieldSL    = newsl;
Surface Loop(farfieldSL) = farfieldBOX[];	// command generates surfaces 1007-1010...
Delete { Volume{farfieldVOL}; } 			// delete the volume
Delete { Surface{1007:1010}; }  			// delete the surfaces

// FLOW VOLUME
// create the flow volume by outer and innser surface loop
voloutside         = newv; 
Volume(voloutside) = {farfieldSL,wingfuelageSL}; 	// outside volume; command generates surfaces 1009...
Delete { Surface{1009}; }  							// delete the surfaces: does not work here, surface still there..
//Delete { Curve{39:44}; }  // delete the symmetry curves, PROBLEM: when deleting here, GMSH crashes



// PHYSICAL ENTITITES
Physical Volume("Air out")  = voloutside;
Physical Surface("wingfuselage") = {1:7,9:15};
Physical Surface("farfield")     = farfieldBOX[];



Printf("extruded surfaces '%g', '%g', '%g', '%g', '%g', '%g'", farfieldEXT[0],farfieldEXT[1],farfieldEXT[2],farfieldEXT[3],farfieldEXT[4],farfieldEXT[5]);
Printf("farfield surfaces '%g', '%g', '%g', '%g', '%g', '%g'", farfieldBOX[]);
Printf("surface Loop '%g'", farfieldSL[]);




// MESHING
// distance on leading edge and fairing
Field[1] = Distance;
Field[1].EdgesList = {9:12,20:23};   // wing upper skin edges
Field[1].NNodesByEdge = 100;

Field[2] = Threshold;
Field[2].DistMax = 50/1000;
Field[2].DistMin = 15/1000;
Field[2].IField = 1;
Field[2].LcMax = 20/1000;
Field[2].LcMin = 2/1000;

Field[3]           = Restrict;
Field[3].FacesList = {1:16};
Field[3].EdgesList = {1:38};
Field[3].IField    = 2;



// distance on fuselage
Field[11] = Distance;
Field[11].EdgesList = {1,22};
Field[11].NNodesByEdge = 100;

Field[12] = Threshold;
Field[12].DistMax = 15/1000;
Field[12].DistMin = 5/1000;
Field[12].IField = 3;
Field[12].LcMax = 5/1000;
Field[12].LcMin = 2/1000;


Field[13]           = Restrict;
Field[13].FacesList = {1:16};
Field[13].EdgesList = {1:38};
Field[13].IField    = 12;



// fields on farfield
Field[21] = Distance;
Field[21].EdgesList = {101:112};
Field[21].FacesList = {1001:1006};
Field[21].NNodesByEdge = 500;

Field[22] = Threshold;
Field[22].DistMax = 8000/1000;
Field[22].DistMin = 5000/1000;
Field[22].IField = 21;
Field[22].LcMax = 500/1000;
Field[22].LcMin = 400/1000;

Field[23]           = Restrict;
Field[23].EdgesList = {101:112};
Field[23].FacesList = {1001:1006};
Field[23].IField    = 22;

// gather fields
Field[100] = Min;
Field[100].FieldsList = {3,13,23};

Background Field = 100;