main.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "sparselizardbase.h"
  2. #include "string.h"
  3. using namespace std;
  4. using namespace mathop;
  5. struct Mesher {
  6. mesh model;
  7. vector<string> files;
  8. string tempName;
  9. bool __initialized;
  10. int segmentation_number;
  11. Mesher() {
  12. this->__initialized = false;
  13. this->tempName = "temp.msh";
  14. this->segmentation_number = 1000000;//997;
  15. // 16127
  16. }
  17. int add(const char* filename) {
  18. this->files.push_back(filename);
  19. return this->getfileregion(this->files.size());
  20. }
  21. void load() {
  22. this->model.load(false, this->files, 5, false, true, this->segmentation_number);
  23. }
  24. void addit(const char* filename) {
  25. vector<string> temp;
  26. if(this->__initialized) {
  27. this->write(this->tempName);
  28. temp.push_back(this->tempName);
  29. } else {
  30. this->__initialized = true;
  31. }
  32. temp.push_back(filename);
  33. this->model.load(true, temp, 5, false);
  34. }
  35. void write(string filename) {
  36. this->model.write(filename);
  37. }
  38. void rotate (double x,double y,double z) { this->model.rotate(x,y,z); }
  39. void translate(double x,double y,double z) { this->model.shift(x,y,z); }
  40. void scale (double x,double y,double z) { this->model.scale(x,y,z); }
  41. void rotate (int r, double x,double y,double z) { this->model.rotate(r,x,y,z); }
  42. void translate(int r, double x,double y,double z) { this->model.shift(r,x,y,z); }
  43. void scale (int r, double x,double y,double z) { this->model.scale(r,x,y,z); }
  44. void debug(){ this->model.printphysicalregions(); }
  45. int getphysicalregion(int file, int region, int dimension) {
  46. int result = (this->segmentation_number + 1000 * file)*(dimension + 1) + region;
  47. return result;
  48. }
  49. int getfileregion(int file) {
  50. return 999000000 + file;
  51. }
  52. void printphysicalregions() {
  53. vector<int> regions = this->model.getphysicalregions()->getallnumbers();
  54. for (int i=0;i<regions.size();i++) {
  55. int temp = regions[i];
  56. int region = temp % (this->segmentation_number);
  57. int dim = temp / (this->segmentation_number) -1;
  58. int pd = (1000*(dim+1));
  59. int file = region / pd;
  60. int model = region % pd;
  61. cout << temp << "\tregion: " << region << "\tmodel: " << model << "\tdim: " << dim << "\tfile: " << file << endl;
  62. }
  63. }
  64. };
  65. int resolve(Mesher mesh) {
  66. int Stator = mesh.getfileregion(1);
  67. int Magnet = mesh.getfileregion(2);
  68. int All = regionunion({Stator, Magnet});
  69. int StatorFace = mesh.getphysicalregion(1, 102, 2);
  70. int StatorFaceBack = mesh.getphysicalregion(1, 103, 2);
  71. int MagnetFace = mesh.getphysicalregion(2, 5, 2);
  72. // cout << endl << "[!]Continuitycondition: " << StatorFace << " , " << MagnetFace << endl << endl;
  73. // cout << "a";
  74. // int Intersection = regionintersection({StatorFace, MagnetFace});
  75. // cout << "a";
  76. // Nodal shape functions 'h1' for the z component of the vector potential.
  77. // field a("h1xyz");
  78. spanningtree spantree({Stator, Magnet});
  79. field a("hcurl", spantree);
  80. a.setgauge(All);
  81. a.setorder(All, 2);
  82. field x("x"), y("y"), z("z");
  83. // Vacuum magnetic permeability [H/m]:
  84. double mu0 = 4.0*getpi()*1e-7;
  85. // Define the permeability in all regions.
  86. // Taking into account saturation and measured B-H curves can be easily done
  87. // by defining an expression based on a 'spline' object (see documentation).
  88. parameter mu;
  89. mu|All = 5000.0*mu0;
  90. // Overwrite on non-magnetic regions:
  91. mu|Magnet = 1.05*mu0;
  92. // The remanent induction field in the magnet is 0.5 Tesla perpendicular to the magnet:
  93. expression magnetdirection = array3x1(0,0,1);
  94. expression bremanent = 0.5 * magnetdirection;
  95. formulation magnetostatics;
  96. // The strong form of the magnetostatic formulation is curl( 1/mu * curl(a) ) = j, with b = curl(a):
  97. magnetostatics += integral(All, 1/mu* curl(dof(a)) * curl(tf(a)) );
  98. // Add the remanent magnetization of the rotor magnet:
  99. magnetostatics += integral(Magnet, -1/mu* bremanent * curl(tf(a)));
  100. // Add the current density source js [A/m2] in the z direction in the stator windings.
  101. // A three-phased actuation is used. The currents are dephased by the mechanical angle
  102. // times the number of pole pairs. This gives a stator field rotating at synchronous speed.
  103. // Change the phase (degrees) to tune the electric angle:
  104. // double phase = 0;
  105. // parameter jsz;
  106. // jsz|winda = Imax/windarea * sin( (phase + 4.0*alpha - 0.0) * getpi()/180.0);
  107. // jsz|windb = Imax/windarea * sin( (phase + 4.0*alpha - 60.0) * getpi()/180.0);
  108. // jsz|windc = Imax/windarea * sin( (phase + 4.0*alpha - 120.0) * getpi()/180.0);
  109. // magnetostatics += integral(windings, -array3x1(0,0,jsz) * tf(a));
  110. magnetostatics += continuitycondition(StatorFace, MagnetFace, a, a, 1, false);
  111. // Rotor-stator continuity condition (including antiperiodicity settings with factor '-1'):
  112. // magnetostatics += continuitycondition(gammastat, gammarot, az, az, {0,0,0}, alpha, 45.0, -1.0);
  113. // Rotor and stator antiperiodicity condition:
  114. // magnetostatics += periodicitycondition(gamma1, gamma2, az, {0,0,0}, {0,0,45.0}, -1.0);
  115. solve(magnetostatics);
  116. // int Intersection = regionintersection({Stator, Magnet});
  117. // curl(a).write(all, "results/MagneticField"+std::to_string((int)alpha)+".vtu", 2);
  118. curl(a).write(All, "results/MagneticField.vtu", 2);
  119. return 0;
  120. }
  121. int main(void)
  122. {
  123. SlepcInitialize(0,{},0,0);
  124. Mesher testmesh;
  125. int Stator = testmesh.add("models/Stator.msh");
  126. int Magnet = testmesh.add("models/Magnet45.msh");
  127. testmesh.load();
  128. testmesh.printphysicalregions();
  129. // testmesh.rotate (Magnet, 0, 0, 0);
  130. // testmesh.translate (Magnet, 0, 0, 2);
  131. testmesh.write("models/combined.msh");
  132. resolve(testmesh);
  133. SlepcFinalize();
  134. return 0;
  135. }