-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavefront2.py
More file actions
206 lines (155 loc) · 4.87 KB
/
Copy pathwavefront2.py
File metadata and controls
206 lines (155 loc) · 4.87 KB
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
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 9 18:53:21 2022
@author: bmetcalf
"""
import math
import numpy as np
import matplotlib.pyplot as plt
class Vector2D:
"""A two-dimensional vector with Cartesian coordinates."""
def __init__(self, x, y):
self.x, self.y = x, y
def __str__(self):
"""Human-readable string representation of the vector."""
return '{:g}i + {:g}j'.format(self.x, self.y)
def __repr__(self):
"""Unambiguous string representation of the vector."""
return repr((self.x, self.y))
def dot(self, other):
"""The scalar (dot) product of self and other. Both must be vectors."""
if not isinstance(other, Vector2D):
raise TypeError('Can only take dot product of two Vector2D objects')
return self.x * other.x + self.y * other.y
# Alias the __matmul__ method to dot so we can use a @ b as well as a.dot(b).
__matmul__ = dot
def __sub__(self, other):
"""Vector subtraction."""
return Vector2D(self.x - other.x, self.y - other.y)
def __add__(self, other):
"""Vector addition."""
return Vector2D(self.x + other.x, self.y + other.y)
def __mul__(self, scalar):
"""Multiplication of a vector by a scalar."""
if isinstance(scalar, int) or isinstance(scalar, float):
return Vector2D(self.x*scalar, self.y*scalar)
raise NotImplementedError('Can only multiply Vector2D by a scalar')
def __rmul__(self, scalar):
"""Reflected multiplication so vector * scalar also works."""
return self.__mul__(scalar)
def __neg__(self):
"""Negation of the vector (invert through origin.)"""
return Vector2D(-self.x, -self.y)
def __truediv__(self, scalar):
"""True division of the vector by a scalar."""
return Vector2D(self.x / scalar, self.y / scalar)
def __mod__(self, scalar):
"""One way to implement modulus operation: for each component."""
return Vector2D(self.x % scalar, self.y % scalar)
def __abs__(self):
"""Absolute value (magnitude) of the vector."""
return math.sqrt(self.x**2 + self.y**2)
def distance_to(self, other):
"""The distance between vectors self and other."""
return abs(self - other)
def to_polar(self):
"""Return the vector's components in polar coordinates."""
return self.__abs__(), math.atan2(self.y, self.x)
class Potential :
def __init__(self, m, a , rmax):
self.m=m
self.a = a
self.rmax = rmax
def __call__(self,r) :
if r>self.rmax :
return 0,0
return -self.m*self.a/(r+self.a),self.m*self.a/(r+self.a)**2
class Potential2 :
def __init__(self, m, a , rmax):
self.m=m
self.a = a
self.rmax = rmax
def __call__(self,r) :
p=-self.m*self.a*np.exp(-r/rmax)/(r+self.a)
return p,-p/(r+self.a) - p/rmax
class Stepper :
def __init__(self,c,xm,potential) :
self.c = c
self.xm = xm
self.pot = potential
self.vec_f = np.vectorize(self.f)
def __call__(self,x,v) :
return self.vec_f(x,v)
def f(self,x,v) :
rv = (x-self.xm)
r = abs(rv)
phi,dphi = self.pot(r)
x = x + (1 + 2*phi)*v*self.c
#x = x + v*self.c
v = v - 2*dphi*( rv - v.dot(rv)*v )/r*self.c
v = v / abs(v)
return x,v
# def step(x,v,xm,pot) :
# rv = (x-xm)
# r = abs(rv)
# phi,dphi = pot(r)
# x = x + 2*phi*v
# v = v + 2*dphi*( rv - v.dot(rv)*rv )/r
# return x,v
# step_v = np.vectorize(step)
def x_comp(v):
return v.x
genx = np.vectorize(x_comp)
def y_comp(v):
return v.y
geny = np.vectorize(y_comp)
def plotx(x,style='-') :
#plt.plot(genx(x),geny(x),color='red')
plt.plot(genx(x),geny(x),linestyle=style)
#### set initial conditions
x = []
v = []
for t in np.arange(np.pi*(0.5-0.05),np.pi*(0.5+0.05),np.pi/8000.) :
y=Vector2D(np.cos(t),np.sin(t))
x.append(y)
v.append( y )
plotx(x)
m=0.3 # mass of lens
a=0.02 # softening length for lens
rmax=0.05
x_lens = Vector2D(0,1.3) # position of lens
c = 0.002 # sleed of light
pot = Potential2(m,a,rmax)
step = Stepper(c,x_lens,pot)
N=350
index1 = 325
ray1 = []
index2 = 240
ray2 = []
#index3 = int(3*99.9)
index3 = 399
ray3 = []
index4 = 100
ray4 = []
while( x[300].y < 1.15) :
x,v = step(x,v)
for i in range(1,N) :
x,v = step(x,v)
ray1.append(x[index1])
ray2.append(x[index2])
ray3.append(x[index3])
ray4.append(x[index4])
if( (i % 30) == 0) :
plotx(x)
#plt.plot(xray,yray,linestyle=':')
plotx(x)
plotx(ray1,style='--')
plotx(ray2,style='--')
plotx(ray3,style='--')
plotx(ray4,style='--')
plt.plot(x_lens.x,x_lens.y,'o')
plt.xlim(-0.2,0.2)
plt.ylim(1.15,1.6)
plt.savefig('wavefronts.png')
plt.show()