def symbolicForCurrentLoop(): import sympy as sp from sympy.functions.special.elliptic_integrals import elliptic_k, elliptic_e from sympy import sympify, lambdify, Eq, sqrt, diff z = sp.symbols('z', real=True) rho, R, m = sp.symbols('rho R m', positive=True) Bz = 1 / sqrt((R + rho)**2 + z**2) * (elliptic_k(m) + (R**2 - rho**2 - z**2) / ((R - rho)**2 + z**2) * elliptic_e(m)) Brho = z / (rho * sqrt( (R + rho)**2 + z**2)) * (-elliptic_k(m) + (R**2 + rho**2 + z**2) / ((R - rho)**2 + z**2) * elliptic_e(m)) mExpr = (4 * R * rho) / ((R + rho)**2 + z**2) Bz = Bz.subs(m, mExpr) Brho = Brho.subs(m, mExpr) def _doLambdify(expr): return lambdify((z, rho, R), expr, modules=[{ "elliptic_k": special.ellipk, "elliptic_e": special.ellipe }, "numpy"]) return _doLambdify(Bz), _doLambdify(Brho), _doLambdify( diff(Brho, rho)), _doLambdify(diff(Brho, z)), _doLambdify(diff( Bz, rho)), _doLambdify(diff(Bz, z))
def test_circumference(): M = Symbol('M') m = Symbol('m') assert Ellipse(Point(0, 0), M, m).circumference == 4 * M * elliptic_e((M ** 2 - m ** 2) / M**2) assert Ellipse(Point(0, 0), 5, 4).circumference == 20 * elliptic_e(S(9) / 25) # circle assert Ellipse(None, 1, None, 0).circumference == 2*pi # test numerically assert abs(Ellipse(None, hradius=5, vradius=3).circumference.evalf(16) - 25.52699886339813) < 1e-10
def test_circumference(): M = Symbol('M') m = Symbol('m') assert Ellipse(Point(0, 0), M, m).circumference == 4 * M * elliptic_e((M ** 2 - m ** 2) / M**2) assert Ellipse(Point(0, 0), 5, 4).circumference == 20 * elliptic_e(S(9) / 25) # degenerate ellipse assert Ellipse(None, 1, None, 1).length == 2 # circle assert Ellipse(None, 1, None, 0).circumference == 2*pi # test numerically assert abs(Ellipse(None, hradius=5, vradius=3).circumference.evalf(16) - 25.52699886339813) < 1e-10
def test_manualintegrate_special(): f, F = 4*exp(-x**2/3), 2*sqrt(3)*sqrt(pi)*erf(sqrt(3)*x/3) assert_is_integral_of(f, F) f, F = 3*exp(4*x**2), 3*sqrt(pi)*erfi(2*x)/4 assert_is_integral_of(f, F) f, F = x**Rational(1, 3)*exp(-x/8), -16*uppergamma(Rational(4, 3), x/8) assert_is_integral_of(f, F) f, F = exp(2*x)/x, Ei(2*x) assert_is_integral_of(f, F) f, F = exp(1 + 2*x - x**2), sqrt(pi)*exp(2)*erf(x - 1)/2 assert_is_integral_of(f, F) f = sin(x**2 + 4*x + 1) F = (sqrt(2)*sqrt(pi)*(-sin(3)*fresnelc(sqrt(2)*(2*x + 4)/(2*sqrt(pi))) + cos(3)*fresnels(sqrt(2)*(2*x + 4)/(2*sqrt(pi))))/2) assert_is_integral_of(f, F) f, F = cos(4*x**2), sqrt(2)*sqrt(pi)*fresnelc(2*sqrt(2)*x/sqrt(pi))/4 assert_is_integral_of(f, F) f, F = sin(3*x + 2)/x, sin(2)*Ci(3*x) + cos(2)*Si(3*x) assert_is_integral_of(f, F) f, F = sinh(3*x - 2)/x, -sinh(2)*Chi(3*x) + cosh(2)*Shi(3*x) assert_is_integral_of(f, F) f, F = 5*cos(2*x - 3)/x, 5*cos(3)*Ci(2*x) + 5*sin(3)*Si(2*x) assert_is_integral_of(f, F) f, F = cosh(x/2)/x, Chi(x/2) assert_is_integral_of(f, F) f, F = cos(x**2)/x, Ci(x**2)/2 assert_is_integral_of(f, F) f, F = 1/log(2*x + 1), li(2*x + 1)/2 assert_is_integral_of(f, F) f, F = polylog(2, 5*x)/x, polylog(3, 5*x) assert_is_integral_of(f, F) f, F = 5/sqrt(3 - 2*sin(x)**2), 5*sqrt(3)*elliptic_f(x, Rational(2, 3))/3 assert_is_integral_of(f, F) f, F = sqrt(4 + 9*sin(x)**2), 2*elliptic_e(x, Rational(-9, 4)) assert_is_integral_of(f, F)
def circumference(self): """The circumference of the ellipse. Examples ======== >>> from sympy import Point, Ellipse >>> p1 = Point(0, 0) >>> e1 = Ellipse(p1, 3, 1) >>> e1.circumference 12*elliptic_e(8/9) """ if self.eccentricity == 1: # degenerate return 4 * self.major elif self.eccentricity == 0: # circle return 2 * pi * self.hradius else: return 4 * self.major * elliptic_e(self.eccentricity**2)
def magneticFunctionsFromCoils(self): """ Calculate the poloidal magnetic flux function psi = -R*A_phi, where A_phi is the toroidal (anti-clockwise) component of magnetic vector potential due to coils. See for example http://physics.usask.ca/~hirose/p812/notes/Ch3.pdf The currents in the coils are taken to be positive in the anti-clockwise direction here. Note e_R x e_phi = e_Z A radially increasing psi results in Bp going clockwise in the poloidal plane. """ import sympy from sympy.functions.special.elliptic_integrals import elliptic_k, elliptic_e import scipy.special R, Z = sympy.symbols("R Z") mu0 = 4.0e-7 * sympy.pi A_phi = 0 * R for coil in self.coils: # little-r is the vector position from the centre of the coil to (R,Z) # sinTheta is the angle between r and the axis through the centre of the coil rSquared = R**2 + (Z - coil.Z)**2 r = sympy.sqrt(rSquared) sinTheta = R / r kSquared = (4 * coil.R * r * sinTheta / (rSquared + coil.R**2 + 2 * coil.R * r * sinTheta)) A_phi += ( coil.I * coil.R / sympy.sqrt(r**2 + coil.R**2 + 2 * coil.R * r * sinTheta) / kSquared * ((2 - kSquared) * elliptic_k(kSquared) - 2 * elliptic_e(kSquared))) # multiply by costant pre-factor A_phi *= mu0 / sympy.pi psi = -R * A_phi dpsidR = sympy.diff(psi, R) dpsidZ = sympy.diff(psi, Z) modGradpsiSquared = dpsidR**2 + dpsidZ**2 B_R = dpsidZ / R B_Z = -dpsidR / R d2psidR2 = sympy.diff(psi, R, R) d2psidZ2 = sympy.diff(psi, Z, Z) d2psidRdZ = sympy.diff(psi, R, Z) self.psi = sympy.lambdify( [R, Z], psi, modules=[ "numpy", { "elliptic_k": scipy.special.ellipk, "elliptic_e": scipy.special.ellipe, }, ], ) self.f_R = sympy.lambdify( [R, Z], dpsidR / modGradpsiSquared, modules=[ "numpy", { "elliptic_k": scipy.special.ellipk, "elliptic_e": scipy.special.ellipe, }, ], ) self.f_Z = sympy.lambdify( [R, Z], dpsidZ / modGradpsiSquared, modules=[ "numpy", { "elliptic_k": scipy.special.ellipk, "elliptic_e": scipy.special.ellipe, }, ], ) self.Bp_R = sympy.lambdify( [R, Z], B_R, modules=[ "numpy", { "elliptic_k": scipy.special.ellipk, "elliptic_e": scipy.special.ellipe, }, ], ) self.Bp_Z = sympy.lambdify( [R, Z], B_Z, modules=[ "numpy", { "elliptic_k": scipy.special.ellipk, "elliptic_e": scipy.special.ellipe, }, ], ) self.d2psidR2 = sympy.lambdify( [R, Z], d2psidR2, modules=[ "numpy", { "elliptic_k": scipy.special.ellipk, "elliptic_e": scipy.special.ellipe, }, ], ) self.d2psidZ2 = sympy.lambdify( [R, Z], d2psidZ2, modules=[ "numpy", { "elliptic_k": scipy.special.ellipk, "elliptic_e": scipy.special.ellipe, }, ], ) self.d2psidRdZ = sympy.lambdify( [R, Z], d2psidRdZ, modules=[ "numpy", { "elliptic_k": scipy.special.ellipk, "elliptic_e": scipy.special.ellipe, }, ], )