/////////////////////
/// GMSH GEOMETRY ///
/////////////////////

/// FILE: axiGeometry2D.geo
/// AUTHOR: Frederic Trillaud <ftrillaudp@gmail.com>
/// UNIVERSITY: UNAM
/// INSTITUTE: IINGEN
/// DATE (english): 11/27/2014
/// UNITS: SI otherwise indicated

/// INTRODUCTION: automatic generation of the geometry to be meshed. Case of the simulation of a solenoid magnet using axial symmetry.

/// The mesh is generated from the graphical interface or the command prompt, taken parameters from the geometry file (see manual):
// > gmsh geometry.geo -2 -o mesh.msh
// -2: 2D mesh
// -o: output to "mesh.msh" file


////////////////
/// GEOMETRY ///
////////////////

/// Superconducting wire dimensions ///
scaling = 1;
bulkHeight = 0.02;
bulkRadius = 0.015;

/// Geometrical parameters (if not given by Salome) ///
/// To be called up in GetDP to set the boundary domain shape ///
/// Inductor:
numberOfTurns = 9000;
inductorInnerRadius = 0.045;
inductorOuterRadius = 0.1;
inductorThickness = inductorOuterRadius-inductorInnerRadius;
inductorHeight = 0.21;
/// Fuid:
fluidRadius = 2*bulkRadius;
fluidHeight = 2*bulkHeight;
/// Boundaries:
infiniteBoundaryInnerRadius = 1.5*Sqrt[inductorOuterRadius^2+(0.5*inductorHeight)^2];
infiniteBoundaryOuterRadius = 1.5*infiniteBoundaryInnerRadius;

/// Surfaces ///
bulkTransverseSurface = bulkRadius*bulkHeight;
inductorTransverseSurface = inductorHeight*inductorThickness;

/// Scaling and unit conversion ///
conversionUnit = 1; // 1: meter to meter
radiantToDegree = 180./Pi;


////////////
/// MESH ///
////////////

/// Coefficient of mesh refinment ///
meshRefinmentCoefficient = 1;

/// quad or triangular mesh ///
quadMesh = 1;

/// Targer element size ///
/// Target element size at the node, smaller numbers indicate higher density as the size is decreasing near the node. The list of target element size is given by the number of values in list x[], y[] and z[]

/// Center:
n_0 = 1e-3; //1e-3;

// Superconductor, protection, substrate and inner side of stabilizer:
For i In {1:6}
 n~{i} = 1e-3;
EndFor
For i In {7:12}
 n~{i} = 2e-3;
EndFor
For i In {13:22}
 n~{i} = 5e-3; //2e-3;
EndFor
For i In {23:26}
 n~{i} = 4e-3;
EndFor
For i In {27:30}
 n~{i} = 1e-2;
EndFor


nQuad_1 = 20;
nQuad_2 = 20;

nQuad_3 = 40;
nQuad_4 = 100;


//////////////////
/// GEOMETRIES ///
//////////////////

/// Geometry options:
Geometry.Color.Lines = Black;
Geometry.LineWidth = 1;
Geometry.PointSize = 1; //4;

/// Smaller than minimum dimension ///
//Geometry.Tolerance = 1e-8;

/// Remove duplicate elementary entities allowing to assemble Line Loop() by merging the points defined separately at a same location. To be used upon labelling philosophy.
//Coherence;

/// To copy all the properties of the geometries during transformation for meshing ///
//Geometry.CopyMeshingMethod = 1;

/// Origin ///
x_0 = 0.; y_0 = 0.; z_0 = 0.;


/////////////////
/// Sub-inductors ///
/////////////////

/// Bulk ///
x_1 = bulkRadius; y_1 = 0.;
x_2 = bulkRadius; y_2 = 0.5*bulkHeight;
x_3 = 0.;         y_3 = 0.5*bulkHeight;

/// fluid ///
x_4 = fluidRadius; y_4 = 0.;
x_5 = fluidRadius; y_5 = 0.5*fluidHeight;
x_6 = 0.;          y_6 = 0.5*fluidHeight;

/// magnet ///
x_7 = inductorInnerRadius;  y_7 = 0.;
x_8 = inductorOuterRadius;  y_8 = 0.;
x_9 = inductorOuterRadius;  y_9 = 0.5*inductorHeight;
x_10 = inductorInnerRadius; y_10 = 0.5*inductorHeight;

/// Surrounding ///
x_11 = infiniteBoundaryInnerRadius;      y_11 = 0.;
x_12 = 0.;                               y_12 = infiniteBoundaryInnerRadius;

/// Infinite space ///
x_13 = infiniteBoundaryOuterRadius;      y_13 = 0.;
x_14 = 0.;                               y_14 = infiniteBoundaryOuterRadius;


For i In {1:14}
 z~{i} = 0.;
EndFor

/// List of coordinates:
For i In {0:14}
 x[] += {x~{i}};
 y[] += {y~{i}};
 z[] += {z~{i}};
 n[] += {n~{i}}; // Local length for meshing
EndFor

/// Points ///
For i In {0:14}
 Point(i) = {x[i], y[i], z[i], n[i]};
EndFor

/// Bulk ///
Line(1) = {0, 1};
Line(2) = {1, 2};
Line(3) = {2, 3};
Line(4) = {3, 0};
Line Loop(1) = {1, 2, 3, 4};
Plane Surface(1) = {1};
Physical Surface(1000) = {1};

/// Fluid ///
Line(5) = {1, 4};
Line(6) = {4, 5};
Line(7) = {5, 6};
Line(8) = {6, 3};
Line Loop(2) = {5, 6, 7, 8, -3, -2};
Plane Surface(2) = {2};
Physical Surface(2000) = {2};

/// Right magnet ///
Line(9) = {7, 8};
Line(10) = {8, 9};
Line(11) = {9, 10};
Line(12) = {10, 7};
Line Loop(3) = {9, 10, 11, 12};
Plane Surface(3) = {3};
Physical Surface(3000) = {3};

/// Surrounding ///
Line(13) = {4, 7};
Line(14) = {8, 11};
Circle(15) = {11, 0, 12};
Line(16) = {12, 6};
Line Loop(4) = {-7, -6, 13, -12, -11, -10, 14, 15, 16};
Plane Surface(4) = {4};
Physical Surface(4000) = {4};

/// Infinite space ///
Line(17) = {11, 13};
Circle(18) = {13, 0, 14};
Line(19) = {14, 12};
Line Loop(5) = {17, 18, 19, -15};
Plane Surface(5) = {5};
Physical Surface(5000) = {5};


//////////////////////
/// Boundary lines ///
//////////////////////

/// Electromagnetic boundary:
Physical Line(10000) = {18, 19, 16, 8, 4};


///////////////
/// MESHING ///
///////////////

/// Quad mesh ///
If (quadMesh == 1)
 Transfinite Line{1, 3} = nQuad_1;
 Transfinite Line{2, 4} = nQuad_2;
 Transfinite Surface{1} = {0, 1, 2, 3}; // Left: surface id , right: list of points at corners of the surface
 Recombine Surface {1}; // just quad, surface id
 Transfinite Line{9, 11} = nQuad_3;
 Transfinite Line{10, 12} = nQuad_4;
 Transfinite Surface{3} = {7, 8, 9, 10}; // Left: surface id , right: list of points at corners of the surface
 Recombine Surface {3}; // just quad, surface id
EndIf


/// Color of meshes:
Color Black {Surface {1};} // bulk
Color Red {Surface {3};} // Inductor
Color Blue {Surface {2};} // fluid
Color Green {Surface {4};} // surroundings
Color DarkGrey {Surface {5};} // Infinite boundary


/// Remove duplicate nodes of the mesh ///
//Coherence Mesh;

/// Meshing parameters ///
//Mesh.Remove4Triangles = 1;
//Mesh.RemeshAlgorithm = 1;
//Mesh.FlexibleTransfinite = 1;
//Mesh.Algorithm = 1;
//Mesh.RandomFactor = 1e-8;
//Mesh.Binary = 1;
