Include "pmsm_v2_data.geo";

/////////////////////
// TRACÉ DU STATOR //
/////////////////////


//Mesh.CharacteristicLengthFromCurvature = 1
//Mesh.MinimumCirclePoints = 50;

//PARAMÈTRES DÉDUITS DE LA GÉOMÉTRIE

lc = 0.65*0.03*Ro;
Tt = 2*Pi/(Qs*(at+0.5));
Ts = at*Tt;
To = 2*ao*Ts+Tt;
Rp = Rt+(Ry-Rt)/20;

//DESSIN DU MOTIF ÉLÉMENTAIRE STATOR

Point(1) = {0,0,0,lc};

Point(2) = {Ry*Cos(-0.5*Tt),Rt*Sin(-0.5*Tt),0,lc};
Point(3) = {Rp*Cos(-0.5*Tt),Rt*Sin(-0.5*Tt),0,lc};
Point(4) = {Rt*Cos(-0.5*To),Rt*Sin(-0.5*To),0,0.1*lc};
Point(5) = {Rg*Cos(-0.5*To),Rg*Sin(-0.5*To),0,0.1*lc};

Point(6) = {Rg*Cos(0.5*To),Rg*Sin(0.5*To),0,0.1*lc};
Point(7) = {Rt*Cos(0.5*To),Rt*Sin(0.5*To),0,0.1*lc};
Point(8) = {Rp*Cos(0.5*Tt),Rt*Sin(0.5*Tt),0,lc};
Point(9) = {Ry*Cos(0.5*Tt),Rt*Sin(0.5*Tt),0,lc};

//DUPLICATION ET ROTATION

For i In {1:Qs}
	For j In {1:9}
		Rotate{{0,0,1}, {0,0,0}, i*(0.5*Tt+Ts)} {Duplicata{Point{j};}}
	EndFor
EndFor

//TRACÉ DES LIGNES

For i In {0:Qs-1}
	Line(1+8*i) = {2+8*i, 3+8*i};
	Line(2+8*i) = {3+8*i, 4+8*i};
	Line(3+8*i) = {4+8*i, 5+8*i};
	Circle(4+8*i) = {5+8*i, 1, 6+8*i};
	Line(5+8*i) = {6+8*i, 7+8*i};
	Line(6+8*i) = {7+8*i, 8+8*i};
	Line(7+8*i) = {8+8*i, 9+8*i};
	If (i != Qs-1)
		Circle(8+8*i) = {9+8*i,1, 10+8*i};
	Else
		Circle(8+8*i) = {9+8*i,1, 2};
	EndIf
EndFor

Rotate {{0, 0, 1}, {0, 0, 0}, -0.25*Tt-0.5*Ts} {
  Line{1:8*Qs};
}

//TRACÉ DU PÉRIMÈTRE EXTÉRIEUR

Point(2+8*Qs) = {Ro, 0, 0, lc};
Point(3+8*Qs) = {0, Ro, 0, lc};
Point(4+8*Qs) = {-Ro, 0, 0, lc};
Point(5+8*Qs) = {0, -Ro, 0, lc};
Circle(1+8*Qs) = {2+8*Qs, 1, 3+8*Qs};
Circle(2+8*Qs) = {3+8*Qs, 1, 4+8*Qs};
Circle(3+8*Qs) = {4+8*Qs, 1, 5+8*Qs};
Circle(4+8*Qs) = {5+8*Qs, 1, 2+8*Qs};

//TRACÉ DES LIGNES ENTRE ENTREFER ET ENCOCHES

For i In {0:Qs-2}
	Line(newl) = {12+8*i,7+8*i};
EndFor

Line(newl) = {4,7+8*(Qs-1)};

//DÉFINITION DE LA SURFACE CULASSE STATOR

Line Loop(1) = {1:8*Qs};
Line Loop(2) = {1+8*Qs,2+8*Qs,3+8*Qs,4+8*Qs};

Plane Surface (1) = {2,1};

//DÉFINITION DES SURFACES ENCOCHES

For i In{0:Qs-2}
	Line Loop(3+i) = {6+8*i,7+8*i,8+8*i,9+8*i,10+8*i,5+8*Qs+i};
	Plane Surface (2+i) = {3+i};
EndFor

Line Loop(3+Qs-1) = {6+8*(Qs-1),7+8*(Qs-1),8+8*(Qs-1),1,2,5+8*Qs+(Qs-1)};
Plane Surface (2+Qs-1) = {3+Qs-1};


////////////////////
// TRACÉ DU ROTOR //
////////////////////


//PARAMÈTRES DÉDUITS DE LA GÉOMÉTRIE

Tp = ap*Pi/p;

//DESSIN DU MOTIF ÉLÉMENTAIRE ROTOR

np = newp;

Point(np)   = {Rr*Cos(-0.5*Tp+Tr*Pi/180),Rr*Sin(-0.5*Tp+Tr*Pi/180),0,lc};
Point(np+1) = {Rr*Cos(0.5*Tp+Tr*Pi/180),Rr*Sin(0.5*Tp+Tr*Pi/180),0,lc};
Point(np+2) = {Rm*Cos(0.5*Tp+Tr*Pi/180),Rm*Sin(0.5*Tp+Tr*Pi/180),0,0.1*lc};
Point(np+3) = {Rm*Cos(-0.5*Tp+Tr*Pi/180),Rm*Sin(-0.5*Tp+Tr*Pi/180),0,0.1*lc};

//DUPLICATION ET ROTATION

For i In {1:2*p-1}
	Rotate {{0, 0, 1}, {0, 0, 0}, i*Pi/p} {
	Duplicata { Point{np, np+1, np+2, np+3}; }
	}
EndFor

//TRACÉ DES LIGNES

nl = 5+9*Qs;

Circle(nl) = {np,1,np+1};
Line(nl+1) = {np+1,np+2};
Circle(nl+2) = {np+2,1,np+3};
Line(nl+3) = {np+3,np};

For i In {1:2*p-1}
	Rotate {{0, 0, 1}, {0, 0, 0}, i*Pi/p} {
	Duplicata { Line{nl, nl+1, nl+2, nl+3}; }
	}
EndFor

Circle(nl+8*p) = {np+1,1,np+4};

For i In {1:2*p-1}
	Rotate {{0, 0, 1}, {0, 0, 0}, i*Pi/p} {
	Duplicata { Line{nl+8*p}; }
	}
EndFor

//DÉFINITION DE LA SURFACE CULASSE ROTOR

rotor_int() = {nl:nl+8*p-1:4};
rotor_int() += {nl+8*p:nl+10*p-1};

Line Loop(3+Qs) = rotor_int();
Plane Surface(2+Qs) = {3+Qs};

//DÉFINITION DES SURFACES AIMANTS

For i In {0:2*p-1}
	Line Loop(3+Qs+1+i) = {nl+4*i,nl+1+4*i,nl+2+4*i,nl+3+4*i};
	Plane Surface(2+Qs+1+i) = {-(3+Qs+1+i)};
EndFor

//DÉFINITION DE LA SURFACE ENTREFER

rotor_ext() = {nl+1,nl+2,nl+3,-(nl+8*p)};

For i In {1:2*p-1}
	rotor_ext() += {nl+1+4*i,nl+2+4*i,nl+3+4*i,-(nl+8*p+i)};
EndFor

stator_int() = {-5-9*Qs+1:-5-8*Qs};
For i In {0:Qs-1}
	stator_int += {3+8*i,4+8*i,5+8*i};
EndFor

Line Loop(3+Qs+2*p+1) = rotor_ext();
Line Loop(3+Qs+2*p+2) = stator_int();

Plane Surface(2+Qs+2*p+1) = {-(3+Qs+2*p+1),-(3+Qs+2*p+2)};


/////////////////////////////////////
//DÉFINITION DES SURFACES PHYSIQUES//
/////////////////////////////////////


Physical Surface(1000) = {1};

For i In {1:Qs}
	Physical Surface(1000+i) = {i+1};
EndFor

Physical Surface(2000) = {Qs+2};

For i In {1:2*p}
	Physical Surface(2000+i) = Qs+2+i;
EndFor

Physical Surface(3000) = {Qs+2*p+3};

Physical Line(10000) = {1+8*Qs,2+8*Qs,3+8*Qs,4+8*Qs};
Physical Line(10001) = {1:(8*Qs+4+8*2*p)};


