test_fieldfunctions.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from sympy.core.singleton import S
  2. from sympy.core.symbol import Symbol
  3. from sympy.functions.elementary.trigonometric import (cos, sin)
  4. from sympy.physics.vector import ReferenceFrame, Vector, Point, \
  5. dynamicsymbols
  6. from sympy.physics.vector.fieldfunctions import divergence, \
  7. gradient, curl, is_conservative, is_solenoidal, \
  8. scalar_potential, scalar_potential_difference
  9. from sympy.testing.pytest import raises
  10. R = ReferenceFrame('R')
  11. q = dynamicsymbols('q')
  12. P = R.orientnew('P', 'Axis', [q, R.z])
  13. def test_curl():
  14. assert curl(Vector(0), R) == Vector(0)
  15. assert curl(R.x, R) == Vector(0)
  16. assert curl(2*R[1]**2*R.y, R) == Vector(0)
  17. assert curl(R[0]*R[1]*R.z, R) == R[0]*R.x - R[1]*R.y
  18. assert curl(R[0]*R[1]*R[2] * (R.x+R.y+R.z), R) == \
  19. (-R[0]*R[1] + R[0]*R[2])*R.x + (R[0]*R[1] - R[1]*R[2])*R.y + \
  20. (-R[0]*R[2] + R[1]*R[2])*R.z
  21. assert curl(2*R[0]**2*R.y, R) == 4*R[0]*R.z
  22. assert curl(P[0]**2*R.x + P.y, R) == \
  23. - 2*(R[0]*cos(q) + R[1]*sin(q))*sin(q)*R.z
  24. assert curl(P[0]*R.y, P) == cos(q)*P.z
  25. def test_divergence():
  26. assert divergence(Vector(0), R) is S.Zero
  27. assert divergence(R.x, R) is S.Zero
  28. assert divergence(R[0]**2*R.x, R) == 2*R[0]
  29. assert divergence(R[0]*R[1]*R[2] * (R.x+R.y+R.z), R) == \
  30. R[0]*R[1] + R[0]*R[2] + R[1]*R[2]
  31. assert divergence((1/(R[0]*R[1]*R[2])) * (R.x+R.y+R.z), R) == \
  32. -1/(R[0]*R[1]*R[2]**2) - 1/(R[0]*R[1]**2*R[2]) - \
  33. 1/(R[0]**2*R[1]*R[2])
  34. v = P[0]*P.x + P[1]*P.y + P[2]*P.z
  35. assert divergence(v, P) == 3
  36. assert divergence(v, R).simplify() == 3
  37. assert divergence(P[0]*R.x + R[0]*P.x, R) == 2*cos(q)
  38. def test_gradient():
  39. a = Symbol('a')
  40. assert gradient(0, R) == Vector(0)
  41. assert gradient(R[0], R) == R.x
  42. assert gradient(R[0]*R[1]*R[2], R) == \
  43. R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z
  44. assert gradient(2*R[0]**2, R) == 4*R[0]*R.x
  45. assert gradient(a*sin(R[1])/R[0], R) == \
  46. - a*sin(R[1])/R[0]**2*R.x + a*cos(R[1])/R[0]*R.y
  47. assert gradient(P[0]*P[1], R) == \
  48. ((-R[0]*sin(q) + R[1]*cos(q))*cos(q) - (R[0]*cos(q) + R[1]*sin(q))*sin(q))*R.x + \
  49. ((-R[0]*sin(q) + R[1]*cos(q))*sin(q) + (R[0]*cos(q) + R[1]*sin(q))*cos(q))*R.y
  50. assert gradient(P[0]*R[2], P) == P[2]*P.x + P[0]*P.z
  51. scalar_field = 2*R[0]**2*R[1]*R[2]
  52. grad_field = gradient(scalar_field, R)
  53. vector_field = R[1]**2*R.x + 3*R[0]*R.y + 5*R[1]*R[2]*R.z
  54. curl_field = curl(vector_field, R)
  55. def test_conservative():
  56. assert is_conservative(0) is True
  57. assert is_conservative(R.x) is True
  58. assert is_conservative(2 * R.x + 3 * R.y + 4 * R.z) is True
  59. assert is_conservative(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z) is \
  60. True
  61. assert is_conservative(R[0] * R.y) is False
  62. assert is_conservative(grad_field) is True
  63. assert is_conservative(curl_field) is False
  64. assert is_conservative(4*R[0]*R[1]*R[2]*R.x + 2*R[0]**2*R[2]*R.y) is \
  65. False
  66. assert is_conservative(R[2]*P.x + P[0]*R.z) is True
  67. def test_solenoidal():
  68. assert is_solenoidal(0) is True
  69. assert is_solenoidal(R.x) is True
  70. assert is_solenoidal(2 * R.x + 3 * R.y + 4 * R.z) is True
  71. assert is_solenoidal(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z) is \
  72. True
  73. assert is_solenoidal(R[1] * R.y) is False
  74. assert is_solenoidal(grad_field) is False
  75. assert is_solenoidal(curl_field) is True
  76. assert is_solenoidal((-2*R[1] + 3)*R.z) is True
  77. assert is_solenoidal(cos(q)*R.x + sin(q)*R.y + cos(q)*P.z) is True
  78. assert is_solenoidal(R[2]*P.x + P[0]*R.z) is True
  79. def test_scalar_potential():
  80. assert scalar_potential(0, R) == 0
  81. assert scalar_potential(R.x, R) == R[0]
  82. assert scalar_potential(R.y, R) == R[1]
  83. assert scalar_potential(R.z, R) == R[2]
  84. assert scalar_potential(R[1]*R[2]*R.x + R[0]*R[2]*R.y + \
  85. R[0]*R[1]*R.z, R) == R[0]*R[1]*R[2]
  86. assert scalar_potential(grad_field, R) == scalar_field
  87. assert scalar_potential(R[2]*P.x + P[0]*R.z, R) == \
  88. R[0]*R[2]*cos(q) + R[1]*R[2]*sin(q)
  89. assert scalar_potential(R[2]*P.x + P[0]*R.z, P) == P[0]*P[2]
  90. raises(ValueError, lambda: scalar_potential(R[0] * R.y, R))
  91. def test_scalar_potential_difference():
  92. origin = Point('O')
  93. point1 = origin.locatenew('P1', 1*R.x + 2*R.y + 3*R.z)
  94. point2 = origin.locatenew('P2', 4*R.x + 5*R.y + 6*R.z)
  95. genericpointR = origin.locatenew('RP', R[0]*R.x + R[1]*R.y + R[2]*R.z)
  96. genericpointP = origin.locatenew('PP', P[0]*P.x + P[1]*P.y + P[2]*P.z)
  97. assert scalar_potential_difference(S.Zero, R, point1, point2, \
  98. origin) == 0
  99. assert scalar_potential_difference(scalar_field, R, origin, \
  100. genericpointR, origin) == \
  101. scalar_field
  102. assert scalar_potential_difference(grad_field, R, origin, \
  103. genericpointR, origin) == \
  104. scalar_field
  105. assert scalar_potential_difference(grad_field, R, point1, point2,
  106. origin) == 948
  107. assert scalar_potential_difference(R[1]*R[2]*R.x + R[0]*R[2]*R.y + \
  108. R[0]*R[1]*R.z, R, point1,
  109. genericpointR, origin) == \
  110. R[0]*R[1]*R[2] - 6
  111. potential_diff_P = 2*P[2]*(P[0]*sin(q) + P[1]*cos(q))*\
  112. (P[0]*cos(q) - P[1]*sin(q))**2
  113. assert scalar_potential_difference(grad_field, P, origin, \
  114. genericpointP, \
  115. origin).simplify() == \
  116. potential_diff_P