1
from sympy import S, Symbol, sin, cos
2
from sympy.physics.vector import ReferenceFrame, Vector, Point, \
4
from sympy.physics.vector.fieldfunctions import divergence, \
5
gradient, curl, is_conservative, is_solenoidal, \
6
scalar_potential, scalar_potential_difference
7
from sympy.utilities.pytest import raises
9
R = ReferenceFrame('R')
10
q = dynamicsymbols('q')
11
P = R.orientnew('P', 'Axis', [q, R.z])
15
assert curl(Vector(0), R) == Vector(0)
16
assert curl(R.x, R) == Vector(0)
17
assert curl(2*R[1]**2*R.y, R) == Vector(0)
18
assert curl(R[0]*R[1]*R.z, R) == R[0]*R.x - R[1]*R.y
19
assert curl(R[0]*R[1]*R[2] * (R.x+R.y+R.z), R) == \
20
(-R[0]*R[1] + R[0]*R[2])*R.x + (R[0]*R[1] - R[1]*R[2])*R.y + \
21
(-R[0]*R[2] + R[1]*R[2])*R.z
22
assert curl(2*R[0]**2*R.y, R) == 4*R[0]*R.z
23
assert curl(P[0]**2*R.x + P.y, R) == \
24
- 2*(R[0]*cos(q) + R[1]*sin(q))*sin(q)*R.z
25
assert curl(P[0]*R.y, P) == cos(q)*P.z
28
def test_divergence():
29
assert divergence(Vector(0), R) == S(0)
30
assert divergence(R.x, R) == S(0)
31
assert divergence(R[0]**2*R.x, R) == 2*R[0]
32
assert divergence(R[0]*R[1]*R[2] * (R.x+R.y+R.z), R) == \
33
R[0]*R[1] + R[0]*R[2] + R[1]*R[2]
34
assert divergence((1/(R[0]*R[1]*R[2])) * (R.x+R.y+R.z), R) == \
35
-1/(R[0]*R[1]*R[2]**2) - 1/(R[0]*R[1]**2*R[2]) - \
37
v = P[0]*P.x + P[1]*P.y + P[2]*P.z
38
assert divergence(v, P) == 3
39
assert divergence(v, R).simplify() == 3
40
assert divergence(P[0]*R.x + R[0]*P.x, R) == 2*cos(q)
45
assert gradient(0, R) == Vector(0)
46
assert gradient(R[0], R) == R.x
47
assert gradient(R[0]*R[1]*R[2], R) == \
48
R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z
49
assert gradient(2*R[0]**2, R) == 4*R[0]*R.x
50
assert gradient(a*sin(R[1])/R[0], R) == \
51
- a*sin(R[1])/R[0]**2*R.x + a*cos(R[1])/R[0]*R.y
52
assert gradient(P[0]*P[1], R) == \
53
(-R[0]*sin(2*q) + R[1]*cos(2*q))*R.x + \
54
(R[0]*cos(2*q) + R[1]*sin(2*q))*R.y
55
assert gradient(P[0]*R[2], P) == P[2]*P.x + P[0]*P.z
58
scalar_field = 2*R[0]**2*R[1]*R[2]
59
grad_field = gradient(scalar_field, R)
60
vector_field = R[1]**2*R.x + 3*R[0]*R.y + 5*R[1]*R[2]*R.z
61
curl_field = curl(vector_field, R)
64
def test_conservative():
65
assert is_conservative(0) is True
66
assert is_conservative(R.x) is True
67
assert is_conservative(2 * R.x + 3 * R.y + 4 * R.z) is True
68
assert is_conservative(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z) is \
70
assert is_conservative(R[0] * R.y) is False
71
assert is_conservative(grad_field) is True
72
assert is_conservative(curl_field) is False
73
assert is_conservative(4*R[0]*R[1]*R[2]*R.x + 2*R[0]**2*R[2]*R.y) is \
75
assert is_conservative(R[2]*P.x + P[0]*R.z) is True
78
def test_solenoidal():
79
assert is_solenoidal(0) is True
80
assert is_solenoidal(R.x) is True
81
assert is_solenoidal(2 * R.x + 3 * R.y + 4 * R.z) is True
82
assert is_solenoidal(R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z) is \
84
assert is_solenoidal(R[1] * R.y) is False
85
assert is_solenoidal(grad_field) is False
86
assert is_solenoidal(curl_field) is True
87
assert is_solenoidal((-2*R[1] + 3)*R.z) is True
88
assert is_solenoidal(cos(q)*R.x + sin(q)*R.y + cos(q)*P.z) is True
89
assert is_solenoidal(R[2]*P.x + P[0]*R.z) is True
92
def test_scalar_potential():
93
assert scalar_potential(0, R) == 0
94
assert scalar_potential(R.x, R) == R[0]
95
assert scalar_potential(R.y, R) == R[1]
96
assert scalar_potential(R.z, R) == R[2]
97
assert scalar_potential(R[1]*R[2]*R.x + R[0]*R[2]*R.y + \
98
R[0]*R[1]*R.z, R) == R[0]*R[1]*R[2]
99
assert scalar_potential(grad_field, R) == scalar_field
100
assert scalar_potential(R[2]*P.x + P[0]*R.z, R) == \
101
R[0]*R[2]*cos(q) + R[1]*R[2]*sin(q)
102
assert scalar_potential(R[2]*P.x + P[0]*R.z, P) == P[0]*P[2]
103
raises(ValueError, lambda: scalar_potential(R[0] * R.y, R))
106
def test_scalar_potential_difference():
108
point1 = origin.locatenew('P1', 1*R.x + 2*R.y + 3*R.z)
109
point2 = origin.locatenew('P2', 4*R.x + 5*R.y + 6*R.z)
110
genericpointR = origin.locatenew('RP', R[0]*R.x + R[1]*R.y + R[2]*R.z)
111
genericpointP = origin.locatenew('PP', P[0]*P.x + P[1]*P.y + P[2]*P.z)
112
assert scalar_potential_difference(S(0), R, point1, point2, \
114
assert scalar_potential_difference(scalar_field, R, origin, \
115
genericpointR, origin) == \
117
assert scalar_potential_difference(grad_field, R, origin, \
118
genericpointR, origin) == \
120
assert scalar_potential_difference(grad_field, R, point1, point2,
122
assert scalar_potential_difference(R[1]*R[2]*R.x + R[0]*R[2]*R.y + \
123
R[0]*R[1]*R.z, R, point1,
124
genericpointR, origin) == \
126
potential_diff_P = 2*P[2]*(P[0]*sin(q) + P[1]*cos(q))*\
127
(P[0]*cos(q) - P[1]*sin(q))**2
128
assert scalar_potential_difference(grad_field, P, origin, \
130
origin).simplify() == \