main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // This example shows the time-simulation of a 3D high-temperature superconductor (HTS):
  2. // a superconducting tube is used to shield its inside from an externally applied magnetic field
  3. // that increases linearly over time. For the simulation the so called a-v magnetodynamic formulation is used.
  4. //
  5. // The tube height is 210 mm, its thickness is 1 mm and its inner radius is 10 mm.
  6. //
  7. // This example is based on and validated with the experimental and simulation work done by K. Hogan in his thesis
  8. // "Passive magnetic shielding with bulk high temperature superconductors under a non-uniform magnetic field".
  9. //
  10. // Details on the equations for HTS can also be found in "Numerical simulation of the magnetization of
  11. // high-temperature superconductors: a 3D finite element method using a single time-step iteration" (G. Lousberg)
  12. #include "sparselizardbase.h"
  13. using namespace mathop;
  14. // Arguments are:
  15. //
  16. // Tube thickness, height and inner radius, size of the air domain around as well as integers defining the mesh size.
  17. // The last argument gives the mesh progression in the air around the tube (increase for a finer mesh at the tube).
  18. //
  19. mesh createmesh(double thtube, double htube, double rintube, double linf, int nhtube, int ncircumtube, int nthtube, int nair, int prog);
  20. void sparselizard(void)
  21. {
  22. wallclock clk;
  23. // The domain regions as defined in 'createmesh':
  24. int wholedomain = 1, tube = 2, insidetube = 3, tubeskin = 4, domainskin = 5, ground = 6, boxcut = 7;
  25. // Tube thickness, height, inner radius and domain size [m]:
  26. double thtube = 1e-3, htube = 210e-3, rintube = 10e-3, linf = 100e-3;
  27. // Create the geometry and the mesh:
  28. mesh mymesh = createmesh(thtube, htube, rintube, linf, 7, 4, 10, 7, 20);
  29. // Define a region to visualize the solution:
  30. boxcut = regionexclusion(wholedomain, boxcut);
  31. // Write the mesh for display:
  32. mymesh.write("tubeinair.msh");
  33. // Magnetic permeability of the air:
  34. double mu = 4*getpi()*1e-7;
  35. // Define a spanning tree to gauge the magnetic vector potential (otherwise the matrix to invert is singular).
  36. // Start growing the tree from the regions with constrained potential vector (here the domain boundary):
  37. spanningtree spantree({domainskin});
  38. // Use nodal shape functions 'h1' for the electric scalar potential 'v'.
  39. // Use edge shape functions 'hcurl' for the magnetic vector potential 'a'.
  40. // A spanning tree has to be provided to field 'a' for gauging.
  41. field a("hcurl", spantree), v("h1"), x("x"), y("y"), z("z");
  42. // Use interpolation order 1:
  43. a.setorder(wholedomain, 1);
  44. v.setorder(wholedomain, 1);
  45. // Gauge the vector potential field on the whole volume:
  46. a.setgauge(wholedomain);
  47. // Put a magnetic wall (i.e. set field a to 0) all around the domain (no magnetic flux can cross it):
  48. a.setconstraint(domainskin);
  49. // Ground v on a node to have a well-defined problem:
  50. v.setconstraint(ground);
  51. // The magnetic vector potential a is rewritten as the sum of a known
  52. // source term and an unknown part to be determined: atotal = a + asource.
  53. // We want to apply an external induction field b of 1 mT in
  54. // the z direction that linearly increases over time:
  55. expression bsource = array3x1(0,0,1e-3)*t();
  56. // Since b = curl(a) a valid choice for a is:
  57. expression asource = 1e-3*0.5*array3x1(-y,x,0)*t();
  58. // And dt(a)/dt is:
  59. expression dtasource = 1e-3*0.5*array3x1(-y,x,0);
  60. // The strong form of the magnetodynamic a-v formulation is
  61. //
  62. // curl( 1/mu * curl(a) ) + sigma * (dt(a) + grad(v)) = js, with b = curl(a) and e = -dt(a) - grad(v)
  63. //
  64. expression e = -dt(a)-dtasource - grad(v);
  65. // The conductivity of the high temperature superconductor is modeled using
  66. // a power law relation between the electric field and the current density:
  67. //
  68. // J = Jc/Ec^(1/n) * norm(E)^((1-n)/n) * E
  69. // |_______________________________|
  70. // sigma(E)
  71. //
  72. // where the critical current density Jc [A/m^2] and critical electric field Ec [V/m] are supposed
  73. // constant here and the J(E) relation is isotropic. An extra 1e-11 is added to never divide by 0.
  74. //
  75. double n = 30.0, Ec = 1e-4, Jc = 1e8;
  76. expression sigma = Jc/( pow(Ec,1.0/n) ) * pow( norm(e) + 1e-11, (1.0-n)/n );
  77. // Define the weak magnetodynamic formulation:
  78. formulation magdyn;
  79. // Magnetic equation (add an extra odd integration degree for convergence):
  80. magdyn += integral(wholedomain, 1/mu*( curl(dof(a)) + bsource ) * curl(tf(a)) );
  81. magdyn += integral(tube, sigma*( dt(dof(a)) + dtasource )*tf(a), +1 );
  82. magdyn += integral(tube, sigma*grad(dof(v))*tf(a) );
  83. // Electric equation:
  84. magdyn += integral(tube, sigma*grad(dof(v))*grad(tf(v)) );
  85. magdyn += integral(tube, sigma*( dt(dof(a)) + dtasource )*grad(tf(v)), +1 );
  86. // Start the implicit Euler time resolution with an all zero solution and time derivative:
  87. vec initsol(magdyn), initdtsol(magdyn);
  88. impliciteuler eul(magdyn, initsol, initdtsol);
  89. // Tolerance on the nonlinear iteration:
  90. eul.settolerance(1e-3);
  91. // Relaxation factor for the nonlinear iteration (decrease to improve convergence):
  92. eul.setrelaxationfactor(0.5);
  93. // Use a 5 seconds timestep:
  94. double timestep = 5.0;
  95. // Run from 0 to 150 sec with a fixed-point nonlinear iteration at every timestep.
  96. // Set the maximum number of nonlinear iterations to 25.
  97. std::vector<std::vector<vec>> sols = eul.runnonlinear(0.0, timestep, 150.0, 25);
  98. // Field a is available at every timestep in sols[0] and its time derivative in sols[1]:
  99. std::vector<vec> asol = sols[0];
  100. std::vector<vec> dtasol = sols[1];
  101. // Loop over all time solutions:
  102. std::vector<double> bcenter(asol.size());
  103. for (int i = 0; i < asol.size(); i++)
  104. {
  105. // Set variable t() to the time of the current timestep:
  106. settime(timestep*i);
  107. // Make the data of vector asol[i] available in field a:
  108. a.setdata(wholedomain, asol[i]);
  109. // Write b = curl(a) to disk:
  110. norm(curl(a) + bsource).write(boxcut, "normbtotal" + std::to_string(i+1000) + ".pos");
  111. // Output the b induction field [T] at the tube center to assess the shielding effectiveness.
  112. bcenter[i] = norm(curl(a) + bsource).interpolate(wholedomain, {1e-10,0,0})[0];
  113. std::cout << "b source " << timestep*i << " mT --> b tube center " << bcenter[i]*1e3 << " mT" << std::endl;
  114. }
  115. // Write to file the field at the tube center for all timesteps:
  116. writevector("bcenter.csv", bcenter);
  117. clk.print("Total computation time:");
  118. // Code validation line. Can be removed.
  119. std::cout << 1;
  120. }
  121. // THE MESH BELOW IS FULLY STRUCTURED AND IS CREATED USING THE (BASIC) SPARSELIZARD GEOMETRY CREATION TOOL.
  122. // THE ADVANTAGE OF IT IS THAT THE CODE ABOVE CAN BE CALLED FOR ANY TUBE DIMENSION WITHOUT NEEDING CALLS TO EXTERNAL MESHING SOFTWARE.
  123. // GMSH COULD HAVE BEEN USED AS AN ALTERNATIVE TO DEFINE THE GEOMETRY AND THE MESH.
  124. mesh createmesh(double thtube, double htube, double rintube, double linf, int nhtube, int ncircumtube, int nthtube, int nair, int prog)
  125. {
  126. int wholedomain = 1, tube = 2, insidetube = 3, tubeskin = 4, domainskin = 5, ground = 6, boxcut = 7;
  127. // To be sure to have nodes at z = 0:
  128. if (nhtube%2 == 0)
  129. nhtube++;
  130. field x("x"), y("y");
  131. // Create the footprint face for the air inside the tube:
  132. shape disk("disk", -1, {0,0,0}, rintube, ncircumtube*4);
  133. // Create the footprint face for the tube:
  134. shape arcin = disk.getsons()[0];
  135. shape arcout("arc", -1, {rintube+thtube,0,0, 0,rintube+thtube,0, 0,0,0}, ncircumtube+1);
  136. shape line1("line", -1, {rintube,0,0, rintube+thtube,0,0}, nthtube);
  137. shape line2 = line1.duplicate(); line2.rotate(0,0,90);
  138. std::vector<shape> quads(4);
  139. for (int i = 0; i < 4; i++)
  140. {
  141. shape curquad("quadrangle", -1, {arcin,line1,arcout,line2});
  142. curquad = curquad.duplicate();
  143. curquad.rotate(0,0,90.0*i);
  144. quads[i] = curquad;
  145. }
  146. shape tubefootprint("union", -1, quads);
  147. // Create the footprint for the remaining domain with a given progression 'prog':
  148. double rout = std::pow(linf*std::pow(rintube+thtube,prog-1), 1.0/prog);
  149. shape arcinf("arc", -1, {rout,0,0, 0,rout,0, 0,0,0}, ncircumtube+1);
  150. shape lineinf1("line", -1, {rintube+thtube,0,0, rout,0,0}, nair);
  151. shape lineinf2 = lineinf1.duplicate(); lineinf2.rotate(0,0,90);
  152. std::vector<shape> airquads(4);
  153. for (int i = 0; i < 4; i++)
  154. {
  155. shape curquad("quadrangle", -1, {arcout,lineinf1,arcinf,lineinf2});
  156. curquad = curquad.duplicate();
  157. curquad.rotate(0,0,90.0*i);
  158. airquads[i] = curquad;
  159. }
  160. shape airfootprint("union", -1, airquads);
  161. // Deform to have a mesh progression in the air:
  162. expression radius = sqrt(x*x+y*y);
  163. airfootprint.deform(x*pow(radius/(rintube+thtube),prog-1)-x,y*pow(radius/(rintube+thtube),prog-1)-y,0);
  164. // Create the cylinder region inside the tube:
  165. shape voltubeinside = disk.duplicate().extrude(insidetube, htube, nhtube);
  166. voltubeinside.shift(0,0,-htube/2);
  167. // Create the tube region:
  168. shape voltube = tubefootprint.duplicate().extrude(tube, htube, nhtube);
  169. voltube.shift(0,0,-htube/2);
  170. // Create the air cylinder around the tube:
  171. shape volairaroundtube = airfootprint.duplicate().extrude(-1, htube, nhtube);
  172. volairaroundtube.shift(0,0,-htube/2);
  173. // Create the air above and below the tube:
  174. shape quadall("union", -1, {disk,tubefootprint,airfootprint});
  175. shape volabove = quadall.duplicate().extrude(-1, linf, nair);
  176. volabove.shift(0,0,htube/2);
  177. shape volbelow = volabove.duplicate();
  178. volbelow.scale(1,1,-1);
  179. // Define the whole volume:
  180. shape wholevol("union", wholedomain, {voltube.duplicate(), voltubeinside.duplicate(), volairaroundtube.duplicate(), volabove.duplicate(), volbelow.duplicate()});
  181. mesh mymesh;
  182. mymesh.regionskin(tubeskin, tube);
  183. mymesh.regionskin(domainskin, wholedomain);
  184. mymesh.boxselection(ground, wholedomain, 0, {rintube+thtube-1e-10,rintube+thtube+1e-10, -1e-10,1e-10, -1e-10,1e-10});
  185. mymesh.boxselection(boxcut, wholedomain, 3, {-1e-10,linf+1e-10, -1e-10,linf+1e-10, -htube-linf,htube+linf});
  186. mymesh.load({voltube, voltubeinside, wholevol});
  187. return mymesh;
  188. }
  189. int main(void)
  190. {
  191. SlepcInitialize(0,{},0,0);
  192. sparselizard();
  193. SlepcFinalize();
  194. return 0;
  195. }