/
EF_of_dipole.py
195 lines (159 loc) · 7.2 KB
/
EF_of_dipole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from mpmath import mp
from math import cos, radians
# * Initializing accuracy level and value of const_k
mp.dps = 100
const_k = mp.fmul(8.9875518, 10**9)
def dipole_moment(charge, a):
'''
Purpose: to calculate the value of dipole moment
Return: returns dipole moment calculated
'''
# Editing Value of charge as per the unit
if charge[1] in ('Coulomb', 'C'):
charge = charge[0]
elif charge[1] in ('microCoulomb', 'uC'):
charge = mp.fmul(charge[0], 10**(-6) )
elif charge[1] in ('milliCoulomb', 'mC'):
charge = mp.fmul(charge[0], 10**(-3) )
elif charge[1] in ('electronCharge', 'eC'):
charge = mp.fmul(charge[0], mp.fmul(1.60217646, mp.fmul(10, -19) ) ) # 1.60217646⋅10-19
elif charge[1] in ('nanoCoulomb', 'nC'):
charge = mp.fmul(charge[0], mp.power(10, -10) )
elif charge[1] in ('picoCoulomb', 'pC'):
charge = mp.fmul( charge[0], mp.power(10, -12) )
# Editing value of a as per the unit
if a[1].lower() in ('meters', 'm'):
a = a[0]
elif a[1].lower() in ('centimeters', 'cm'):
a = mp.fmul(a[0], 10**(-2) )
elif a[1].lower() in ('millimeters', 'mm'):
a = mp.fmul(a[0], 10**(-3) )
elif a[1].lower() in ('angstroms', 'a', 'A'):
a = mp.fmul(a[0], 10**(-10) )
return mp.fmul(charge, mp.fmul(2, a))
def dipole(r, theta, charge, a):
'''
Purpose : To calculate Electric Potential due to Dipole considering very small values
Formula Used :
(q*const_k)*(1/r_1 - 1/r_2)
where,
r_1 = distance between negative charge and point of observation
= (r^2 + a^2 - 2*a*Cos(theta))**0.5
r_2 = distance between positive charge and point of observation
= (r^2 + a^2 + 2*a*Cos(theta))**0.5
const_k = 4*pi*epsilon_not
= 8.9875518 x 10^9
Parameters :
a) r - distance between center of the dipole and point of observation
b) theta - Angle between positive charge and point of observation
c) charge - either charge irrespective of sign
d) a - distance between either charge and center of dipole
Return: returns exact value of electric field calculated
'''
# Editing value of r as per the unit
if r[1].lower() in ('meters', 'm'):
r = r[0]
elif r[1].lower() in ('centimeters', 'cm'):
r = mp.fmul(r[0], 10**(-2) )
elif r[1].lower() in ('millimeters', 'mm'):
r = mp.fmul(r[0], 10**(-3) )
elif r[1].lower() in ('angstroms', 'a', 'A'):
r = mp.fmul(r[0], 10**(-10) )
# Editing value of a as per the unit
if a[1].lower() in ('meters', 'm'):
a = a[0]
elif a[1].lower() in ('centimeters', 'cm'):
a = mp.fmul(a[0], 10**(-2) )
elif a[1].lower() in ('millimeters', 'mm'):
a = mp.fmul(a[0], 10**(-3) )
elif a[1].lower() in ('angstroms', 'a', 'A'):
a = mp.fmul(a[0], 10**(-10) )
# Calculating Value of Cos(theta)
if theta[1].lower() == 'radians':
cos_theta = round( mp.cos(theta[0]), 5 )
elif theta[1].lower() == 'degrees':
cos_theta = round( mp.cos(mp.radians(theta[0])), 5 )
# Editing Value of charge as per the unit
if charge[1] in ('Coulomb', 'C'):
charge = charge[0]
elif charge[1] in ('microCoulomb', 'uC'):
charge = mp.fmul(charge[0], 10**(-6) )
elif charge[1] in ('milliCoulomb', 'mC'):
charge = mp.fmul(charge[0], 10**(-3) )
elif charge[1] in ('electronCharge', 'eC'):
charge = mp.fmul(charge[0], mp.fmul(1.60217646, mp.fmul(10, -19) ) ) # 1.60217646⋅10-19
elif charge[1] in ('nanoCoulomb', 'nC'):
charge = mp.fmul(charge[0], mp.power(10, -10) )
elif charge[1] in ('picoCharge', 'pC'):
charge = mp.fmul( charge[0], mp.power(10, -12) )
# Calculating value of r_1 and r_2
r_1 = mp.sqrt(mp.fsub(mp.fadd(mp.power(r, 2), mp.power(a, 2)), mp.fmul(2, mp.fmul(a, mp.fmul(r, cos_theta)))))
r_2 = mp.sqrt(mp.fadd(mp.fadd(mp.power(r, 2), mp.power(a, 2)), mp.fmul(2, mp.fmul(a, mp.fmul(r, cos_theta)))))
# Calculating final result
result = mp.fmul(mp.fmul(charge, const_k), mp.fsub(mp.fdiv(1, r_1), mp.fdiv(1, r_2)))
# returning final result
return result
def dipole_approx(r, theta, charge, a):
'''
Purpose : To calculate Electric Potential due to Dipole neglecting very small value of a^2
Formula Used :
q*2*a*cos(theta)*const_k/(r**2)
where,
const_k = 4*pi*epsilon_not
= 8.9875518 x 10^9
Parameters :
a) r - distance between center of the dipole and point of observation
b) theta - Angle between positive charge and point of observation
c) charge - either charge irrespective of sign
d) a - distance between either charge and center of dipole
Return : returns approx electric field calculated
'''
'''
Editing Values of the arguments as per the values received by this function
'''
# Editing value of r as per the unit
if r[1].lower() in ('meters', 'm'):
r = r[0]
elif r[1].lower() in ('centimeters', 'cm'):
r = mp.fmul(r[0], 10**(-2) )
elif r[1].lower() in ('millimeters', 'mm'):
r = mp.fmul(r[0], 10**(-3) )
elif r[1].lower() in ('angstroms', 'a', 'A'):
r = mp.fmul(r[0], 10**(-10) )
# Editing value of a as per the unit
if a[1].lower() in ('meters', 'm'):
a = a[0]
elif a[1].lower() in ('centimeters', 'cm'):
a = mp.fmul(a[0], 10**(-2) )
elif a[1].lower() in ('millimeters', 'mm'):
a = mp.fmul(a[0], 10**(-3) )
elif a[1].lower() in ('angstroms', 'a', 'A'):
a = mp.fmul(a[0], 10**(-10) )
# Calculating Value of Cos(theta)
if theta[1].lower() == 'radians':
cos_theta = round( cos(theta[0]), 5 )
elif theta[1].lower() == 'degrees':
cos_theta = round( cos(mp.radians(theta[0])), 5 )
# Editing Value of charge as per the unit
if charge[1] in ('Coulomb', 'C'):
charge = charge[0]
elif charge[1] in ('microCoulomb', 'uC'):
charge = mp.fmul(charge[0], 10**(-6) )
elif charge[1] in ('milliCoulomb', 'mC'):
charge = mp.fmul(charge[0], 10**(-3) )
elif charge[1] in ('electronCharge', 'eC'):
charge = mp.fmul(charge[0], mp.fmul(1.60217646, mp.fmul(10, -19) ) ) # 1.60217646⋅10-19
elif charge[1] in ('nanoCoulomb', 'nC'):
charge = mp.fmul(charge[0], mp.power(10, -10) )
elif charge[1] in ('picoCharge', 'pC'):
charge = mp.fmul( charge[0], mp.power(10, -12) )
# Applying Formula to given parameters
result = mp.fdiv(mp.fmul(const_k, mp.fmul(charge, mp.fmul(2, mp.fmul(a, cos_theta)))), mp.power(r,2))
# returning final result
return result
def diff(EP:float, AP:float):
'''
Purpose: to calculate the difference between the exact and approx value
Return: returns the error
'''
return mp.fsub(EP, AP)