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

/// FILE: permanentMagnet.geo
/// AUTHOR: Frederic Trillaud <ftrillaudp@gmail.com>
/// UNIVERSITY: UNAM
/// INSTITUTE: IINGEN
/// DATE (english): 02/23/2013
/// UNITS: SI otherwise indicated

/// INTRODUCTION: automatic generation of the geometry to be meshed. Case of a brushless motor with permanent magnets. The user can use saliency id required.

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


//////////////////////
/// INCLUDED FILES ///
//////////////////////

/// Include parameter file to define parameters of the geometry ///
Include "./permanentMagnet.par";


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

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

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

/// list of target element size (see parameter file) ///
n[] = {n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16, n17, n18, n19, n20, n21, n22, n23, n24, n25, n26};


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

/// Origin ///
x000 = 0; y000 = 0; z000 = 0;

/// Coordinates ///
/// Rotor
x1 = radiusRotor;                      y1 = 0.;
x2 = radiusRotor*Cos[anglePM/2.];      y2 = radiusRotor*Sin[anglePM/2.];
x3 = 0.;                               y3 = radiusRotor;
x4 = (-1)*radiusRotor*Cos[anglePM/2.]; y4 = radiusRotor*Sin[anglePM/2.];
x5 = (-1)*radiusRotor;                 y5 = 0.;
x6 = (-1)*radiusRotor*Cos[anglePM/2.]; y6 = (-1)*radiusRotor*Sin[anglePM/2.];
x7 = 0.;                               y7 = (-1)*radiusRotor;
x8 = radiusRotor*Cos[anglePM/2.];      y8 = (-1)*radiusRotor*Sin[anglePM/2.];
/// Permanent magnet 1:
x9 = (radiusRotor+thicknessPM)*Cos[anglePM/2.];  y9 = (-1)*(radiusRotor+thicknessPM)*Sin[anglePM/2.];
x10 = radiusRotor+thicknessPM;                   y10 = 0.;
x11 = (radiusRotor+thicknessPM)*Cos[anglePM/2.]; y11 = (radiusRotor+thicknessPM)*Sin[anglePM/2.];
/// Permanent magnet 2:
x12 = (-1)*(radiusRotor+thicknessPM)*Cos[anglePM/2.]; y12 = (radiusRotor+thicknessPM)*Sin[anglePM/2.];
x13 = (-1)*(radiusRotor+thicknessPM);                 y13 = 0.;
x14 = (-1)*(radiusRotor+thicknessPM)*Cos[anglePM/2.]; y14 = (-1)*(radiusRotor+thicknessPM)*Sin[anglePM/2.];
// Stator:
x15 = innerRadiusStator;      y15 = 0.;
x16 = 0.;                     y16 = innerRadiusStator;
x17 = (-1)*innerRadiusStator; y17 = 0.;
x18 = 0.;                     y18 = (-1)*innerRadiusStator;
x19 = outerRadiusStator;      y19 = 0.;
x20 = 0.;                     y20 = outerRadiusStator;
x21 = (-1)*outerRadiusStator; y21 = 0.;
x22 = 0.;                     y22 = (-1)*outerRadiusStator;
// Infinite boundary space:
x23 = radiusInfiniteBoundarySpace;      y23 = 0.;
x24 = 0.;                               y24 = radiusInfiniteBoundarySpace;
x25 = (-1)*radiusInfiniteBoundarySpace; y25 = 0.;
x26 = 0.;                               y26 = (-1)*radiusInfiniteBoundarySpace;
/// List of coordinates
x[] = {x000, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26};
y[] = {y000, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18, y19, y20, y21, y22, y23, y24, y25, y26};
z = 0.; // 2D geometry

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

/// Rotor ///
For i In {1:7}
 Circle(i) = {i, 0, i+1};
EndFor
Circle(8) = {8, 0, 1};
For i In {1:8}
 listRotor[] += {i};
EndFor
Line Loop(1) = listRotor[];
Plane Surface(1) = {1};

/// Permanent magnets ///
/// PM1:
Line(9) = {8, 9};
Circle(10) = {9, 0, 10};
Circle(11) = {10, 0, 11};
Line(12) = {11, 2};
Line Loop(2) = {-1, -8, 9, 10, 11, 12};
Plane Surface(2) = {2};
/// PM2:
Line(13) = {4, 12};
Circle(14) = {12, 0, 13};
Circle(15) = {13, 0, 14};
Line(16) = {14, 6};
Line Loop(3) = {-5, -4, 13, 14, 15, 16};
Plane Surface(3) = {3};

/// Air gap ///
/// On rotor and permanent magnets:
Line Loop(4) = {9, 10, 11, 12, 2, 3, 13, 14, 15, 16, 6, 7};
/// On stator:
Circle(17) = {15, 0, 16};
Circle(18) = {16, 0, 17};
Circle(19) = {17, 0, 18};
Circle(20) = {18, 0, 15};
Line Loop(5) = {17, 18, 19, 20};
Plane Surface(4) = {5, 4};

/// Stator ///
Circle(21) = {19, 0, 20};
Circle(22) = {20, 0, 21};
Circle(23) = {21, 0, 22};
Circle(24) = {22, 0, 19};
Line Loop(6) = {21, 22, 23, 24};
Plane Surface(5) = {6, 5};

/// Infinite boundary space ///
Circle(25) = {23, 0, 24};
Circle(26) = {24, 0, 25};
Circle(27) = {25, 0, 26};
Circle(28) = {26, 0, 23};
Line Loop(lineLoopBoundary) = {25, 26, 27, 28};
Plane Surface(6) = {lineLoopBoundary, 6};


///////////////////////
/// PHYSICAL GROUPS ///
///////////////////////
/// NB: define groups of similar properties or to be considered as one by the solver

/// Rotor ///
Physical Surface(rotorID) = {1};
/// Permanent magnets ///
/// PM1:
Physical Surface(PMID) = {2};
/// PM2:
Physical Surface(PMID+1) = {3};
/// Air gap ///
Physical Surface(airGapID) = {4};
/// Stator ///
Physical Surface(statorID) = {5};
/// Infinite boundary space ///
Physical Surface(infiniteBoundarySpaceID) = {6};

/// Boundary line ///
Physical Line(boundaryLineID) = {25, 26, 27, 28};
