forked from tdalford/FTS-Simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTraceFunctions.py
More file actions
4089 lines (3380 loc) · 152 KB
/
RayTraceFunctions.py
File metadata and controls
4089 lines (3380 loc) · 152 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''This includes the functions of several different simulations of the Meyer
Lab's Compact Fourier Transform Spectrometer. It ONLY includes the functions
that are necessary FOR THE FINAL SIMULATIONS, rather than ones that were used
when building this. It also includes different version of functions regarding
input rays (random initial phase or all zero) and pickling (the ability to
return every single ray generated in the simulation). If you are interested in
more functions showing the build up of this simulation, contact me at
liusarkarm@uchicago.edu Mira Liu'''
import yaml
from tqdm import tqdm
import time
import math
import random
from random import uniform
import numpy
import numpy as np
from numba import jit, njit
# from numba import jot # for speedups
# Create new functions to set each of the origins and angles:
def get_aspect(config, aspect, element, number):
'''Obtain the config for a given aspect, element, and number.
Parameters:
config (yaml file) -- yaml configuration file loaded
aspect (str) -- aspect of the FTS (origins, angles,
coefficients, etc)
element (str) -- element of the FTS for which this aspect
is defined (ellipses, mirror, polarizers)
number (int) -- number of the element that we're specifically
interested in (1-10 for ellipses, 1-4 for
polarizers)'''
def get_item(dic, key, num):
if type(dic[key]) is dict:
return dic[key][num]
else:
return dic[key]
if element is None:
return get_item(config, aspect, number)
else:
return get_item(config[aspect], element, number)
filename = "lab_fts_dims_mcmahon.yml"
# filename = "lab_fts_dims_dicts.yml"
# filename = "lab_fts_dims_dicts_old.yml"
with open(filename, "r") as stream:
config_to_use = yaml.safe_load(stream)
originG = [0., 0., 0.] # the global origin
thetG = [0., 0., 0.] # rotation with respect to itself aka 0,0,0
p1, p2, p3, p4 = [get_aspect(config_to_use, 'polarizer_values', None,
i + 1) for i in range(4)]
origin1, origin2, origin3, origin4, origin5, origin6, origin7, origin8, \
origin9, origin10 = [get_aspect(config_to_use, 'origins', 'ellipses',
i + 1) for i in range(10)]
coeffellipse7 = get_aspect(config_to_use, 'coefficients', 'ellipses', 7)
coeffellipse56 = get_aspect(config_to_use, 'coefficients', 'ellipses', 5)
coeffellipse = get_aspect(config_to_use, 'coefficients', 'ellipses', 1)
thet, thet5, thet6, thet7, thet10 = [get_aspect(
config_to_use, 'angles', 'ellipses', i) for i in [1, 5, 6, 7, 10]]
# Locations and coefficients of the polarizers:
coeffmirr = get_aspect(config_to_use, 'coefficients', 'mirror', None)
coeffpolar = get_aspect(config_to_use, 'coefficients', 'polarizers', None)
originpolar1, originpolar2, originpolar3, originpolar4 = [get_aspect(
config_to_use, 'origins', 'polarizers', i + 1) for i in range(4)]
center1, center2, center3, center4, center5, center6, center7, center8, \
center9, center10 = [get_aspect(
config_to_use, 'centers', None, i + 1) for i in range(10)]
range1, range2, range3, range4, range5, range6, range7, range8, \
range9, range10 = [get_aspect(
config_to_use, 'ranges', None, i + 1) for i in range(10)]
''' Below are functions used in the simulation'''
'''Rotations: Give angle wanted rotated to respective function, returns rotated
point(s).'''
# @njit
def Rx(x):
# Rx = np.matrix([[1, 0, 0], [0, np.cos(x), -np.sin(x)],
# [0, np.sin(x), np.cos(x)]])
Rx = np.array([[1., 0., 0.], [0., np.cos(x), -np.sin(x)],
[0., np.sin(x), np.cos(x)]])
return Rx
# @njit
def Ry(y):
# Ry = np.matrix([[np.cos(y), 0, np.sin(y)], [
# 0, 1, 0], [-np.sin(y), 0, np.cos(y)]])
Ry = np.array([[np.cos(y), 0., np.sin(y)], [
0., 1., 0.], [-np.sin(y), 0., np.cos(y)]])
return Ry
# @njit
def Rz(z):
# Rz = np.matrix([[np.cos(z), - np.sin(z), 0],
# [np.sin(z), np.cos(z), 0], [0, 0, 1]])
Rz = np.array([[np.cos(z), - np.sin(z), 0.],
[np.sin(z), np.cos(z), 0.], [0., 0., 1.]])
return Rz
# @jit
def Rxyz(thet):
Rxyz = Rx(thet[0]).dot(Ry(thet[1])).dot(Rz(thet[2]))
return Rxyz
# @jit
def ELIorganize(p, v, coeffellipse):
return p[0], p[1], p[2], v[0], v[1], v[2], coeffellipse[0], coeffellipse[1]
''' EllipseLineInt(ELI): Give point of the line, vector of the line, and
coefficients of the ellipse, find the intersection(s) of the line and the
ellipsoid (assuming ellipse is rotated about the x-axis. This is where
(x-x_0)/a = (y-y_0)/b) = (z-z_0)/c. And, x^2/d^2 + y^2/e^2 + z^2/e^2 = 1
(ellipse is rotated around x axis to form ellipsoid'''
# @jit
def ELI2(p, v, coeffellipse):
x0, y0, z0, a, b, c, d, e = ELIorganize(p, v, coeffellipse)
A = (e**2)/(d**2) + (b**2)/(a**2) + (c**2)/(a**2)
B = (-2*x0*b**2)/(a**2) + (2*y0*b)/(a) + (-2*x0*c**2)/(a**2) + (2*z0*c)/(a)
C = ((x0**2)*(b**2))/(a**2) + (-2*y0*b*x0)/(a) + y0**2 + \
((x0**2)*(c**2))/(a**2) + (-2*z0*c*x0)/(a) + z0**2 - e**2
xint = [(-B + np.sqrt((B**2) - 4*A*C))/(2*A),
(-B - np.sqrt((B**2) - 4*A*C))/(2*A)]
t = [(xint[0]-x0)/a, (xint[1]-x0)/a]
yint = [y0 + t[0]*b, y0 + t[1]*b]
zint = [z0 + t[0]*c, z0 + t[1]*c]
return xint, yint, zint
'''NormalP: Given a point, vector, and ellipse, finds the point of intersection
and the normal of the corresponding tangent plane.'''
# @jit
def NormalP(pli, v1, coeffellipse):
xint1, yint1, zint1 = ELI2(pli, v1, coeffellipse)
cpos = [(2*xint1[0])/(coeffellipse[0]**2), (2*yint1[0]) /
(coeffellipse[1]**2), (2*zint1[0])/(coeffellipse[1]**2)]
cneg = [(2*xint1[1])/(coeffellipse[0]**2), (2*yint1[1]) /
(coeffellipse[1]**2), (2*zint1[1])/(coeffellipse[1]**2)]
cpos = np.array(cpos)
cneg = np.array(cneg)
return cpos, cneg
''' norm(N): Given a vector, returns the normal of it'''
# @jit
def N(V):
VectL = numpy.array(V)
VNorm = numpy.sqrt(VectL[0]**2 + VectL[1]**2 + VectL[2]**2)
VectLNorm = ([u/VNorm for u in VectL])
VectLNorm = numpy.array(VectLNorm)
return VectLNorm
'''make_line (ML): given a point, vector, and length, makes the corresponding line '''
# this function is bad, don't use it.
# @jit
def ML(p, v, L):
pointL = p
VectL = numpy.array(v)
Lwant = int(L)
VectLNorm = N(v)
t = numpy.linspace(0, Lwant, 50) # make related to wanted length??
x = [pointL[0]]
y = [pointL[1]]
z = [pointL[2]]
for t in range(0, Lwant):
L = numpy.sqrt(
((VectLNorm[0]*t)**2 + (VectLNorm[2]*t)**2 + (VectLNorm[2]*t)**2))
xL = pointL[0] + t*VectLNorm[0]
yL = pointL[1] + t*VectLNorm[1]
zL = pointL[2] + t*VectLNorm[2]
if L <= Lwant:
x.append(xL)
y.append(yL)
z.append(zL)
return x, y, z
'''setrange2d(SR2): Give radius, intersection points, and origin, only keep
points within the circle '''
# @jit
def SR2(xrange, X, Y, Z, origin):
xintG = []
yintG = []
zintG = []
for i in range(0, len(X)):
xinti = X[i]
yinti = Y[i]
zinti = Z[i]
if (xinti-origin[0])**2 + (zinti-origin[2])**2 <= xrange**2:
xintG.append(xinti)
yintG.append(yinti)
zintG.append(zinti)
return xintG, yintG, zintG
'''CreateEllipseBoundShifted(CEBS): creates an ellipse with given coefficients
at origin (0,0). Returns x, positive y, negative y, and z coordinates. (z is
assumed to be 0 as it is 2d)'''
# @jit
def CEBS(coeffellipse, length):
xc = np.linspace(-float(length)/2, float(length) /
2, 100) # centers around angle
# pos side of ellipse
yc1 = np.sqrt((1-(((xc)**2)/(coeffellipse[0]**2)))*coeffellipse[1]**2)
# neg side of ellipse
yc2 = -np.sqrt((1-(((xc)**2)/(coeffellipse[0]**2)))*coeffellipse[1]**2)
# assumes merely in the x,y plane and uses 100 points
zc = np.linspace(0, 0, 100)
return xc, yc1, yc2, zc
'''RotateStrandBoundShiftCORRECTING (RSBSC): Give angle to be rotated about the
x axis, coefficients of ellipse, length to restrict ellipse, origin of
shifting, and sign(pos or neg if it is on the positive or negative side of the
y axis). Creates the ellipse rotated at the specific angle around the x-axis'''
def RSBSC(theta, coeffellipse, length, sign):
Rotated = []
xc, yc1, yc2, zc = CEBS(coeffellipse, length)
if sign == 'pos':
for i in range(0, 100):
# number of original points using POSITIVE side of ellipse
v = [xc[i], yc1[i], zc[i]]
# multiplied by rotation vector
v2 = np.array(np.dot(v, Rx(theta)))
# take away zero-index when converting from mat -> arr
Rotated.append(v2) # rotated vectors
if sign == 'neg':
for i in range(0, 100):
# number of original points on NEGATIVE side of ellipse
v = [xc[i], yc2[i], zc[i]]
# multiplied by rotation vector
v2 = np.array(np.dot(v, Rx(theta)))
# take away zero-index when converting from mat -> arr
Rotated.append(v2) # rotated vectors
xcR1 = []
ycR1 = []
zcR1 = []
for j in range(0, 100): # recombining into arrays of x,y,z to be plotted
xcR1.append(Rotated[j][0])
ycR1.append(Rotated[j][1])
zcR1.append(Rotated[j][2])
return xcR1, ycR1, zcR1
'''CreateZBoundShiftCorrecting (CZBSC): give number of ellipses wanted, half of
the angle (theta) wanted (so if you want half of the ellipsoid, choose
np.pi/2), coefficients of ellipse, restriction length, shift origin, and sign
(pos or neg). Returns the 3d shape of the restricted and shifted ellipsoid
rotated Theta about the x-axis'''
def CZBSC(a, n, coeffellipse, length, sign):
x1 = []
y1 = []
z1 = []
for i in range(0, a):
theta = np.linspace(0, n, a) # range from 0 to n angles in a divisions
x, y, z = RSBSC(theta[i], coeffellipse, length,
sign) # ellipse for specific angle
x1.extend(x) # adding a new ellipse for each angle
y1.extend(y)
z1.extend(z)
return x1, y1, z1 # returns all ellipses
'''FTSCEllipsoidCorrecting(FTSEC): give number of ellipses wanted, half of the
angle covered wanted (so if you want half of the ellipsoid, choose np.pi/2),
coefficients of ellipse, restriction length, shift origin, and sign (pos or
neg). Returns the 3d shape of the restricted and shifted ellipsoid rotated +
Theta and -Theta about the x-axis to create a symmetric ellipsoid on the pos
and neg side of the z plane.'''
def FTSEC(a, n, coeffellipse, length, sign):
# positive side of zplane
X, Y, Z = CZBSC(a, n, coeffellipse, length, sign)
X1, Y1, Z1 = CZBSC(a, -n, coeffellipse, length,
sign) # negative side zplane
if sign != 'pos' and sign != 'neg':
print('Error')
return X, Y, Z, X1, Y1, Z1
'''Separate: given a list of points/vectors (i.e [[x1,y1,z1],[x2,y2,z2], ...] translates into a three arrays of x, y, and z values. (this is the format for the Transform function)'''
def sep(X):
x, y, z = [], [], []
if type(X[0]) is int or type(X[0]) is float or type(X[0]) is numpy.float64:
x = X[0]
y = X[1]
z = X[2]
else:
for i in range(0, len(X)):
x.append(X[i][0])
y.append(X[i][1])
z.append(X[i][2])
return x, y, z
'''The reverse of sep. Translates three arrays of x,y,z values back into series
of [x,y,z] points/vectors. '''
def sepop(x, y, z):
v = []
if type(x) is int or type(x) is float or type(x) is numpy.float64:
v = [x, y, z]
else:
for i in range(0, len(x)):
a = [x[i], y[i], z[i]]
v.append(a)
return v
''' rotate (V, thetaxyz) rotates a vector about a given angle in order of (x,y,z)'''
def rotate(point, thetaxyz):
x = point[0]
y = point[1]
z = point[2]
v = [x, y, z]
lenvect = (x**2 + y**2 + z**2)**.5
V = N(v)
V2 = np.array(np.dot(V, Rxyz(thetaxyz)))
v2f = V2*lenvect # take away zero-index when converting from mat -> arr
return v2f
'''rotate (V, thetaxyz) rotates a vector about a given angle in order of (z,y,x)'''
def rotaterev(point, thetaxyz):
x = point[0]
y = point[1]
z = point[2]
v = [x, y, z]
lenvect = (x**2 + y**2 + z**2)**.5
V = N(v)
VZ = np.array(np.dot(V, Rz(thetaxyz[2])))
VZY = np.array(np.dot(VZ, Ry(thetaxyz[1])))
VZYX = np.array(np.dot(VZY, Rx(thetaxyz[0])))
v2f = VZYX*lenvect # take away zero-index when converting from mat -> arr
return v2f
'''given a point (or vector) and an origin (a local one in global coordinates), shifts to the Local origin in Global coordinates'''
def shift(point, origin):
x = point[0]
y = point[1]
z = point[2]
x2 = x + origin[0]
y2 = y + origin[1]
z2 = z + origin[2]
v2 = [x2, y2, z2]
return v2
'''Given a point (or three arrays of x,y,z for points), the GLOBAL coordinates are transformed to LOCAL coordinates where the LOCAL coordinates are defined in terms of the GLOBAL coordinate system through its GLOBAL origin and GLOBAL rotation. Essentially transforms point(s) from global coordinate system to given local coordinate system. the origin is the LOCAL origin in GLOBAL coordinates'''
def transformGL(x, y, z, origin, thetaxyz):
XTR = []
YTR = []
ZTR = []
if type(x) is int or type(x) is float or type(x) is numpy.float64:
v = [x, y, z]
if x == 0 and y == 0 and z == 0:
vf = shift(v, negvect(origin))
XTR = vf[0]
YTR = vf[1]
ZTR = vf[2]
else:
v2S = shift(v, negvect(origin))
v2RS = rotaterev(v2S, negvect(thetaxyz))
XTR = v2RS[0]
YTR = v2RS[1]
ZTR = v2RS[2]
else:
for i in range(0, len(x)):
v = [x[i], y[i], z[i]]
if x[i] == 0 and y[i] == 0 and z[i] == 0:
vf = shift(v, negvect(origin))
XTR.append(vf[0])
YTR.append(vf[1])
ZTR.append(vf[2])
else:
v2S = shift(v, negvect(origin))
v2RS = rotaterev(v2S, negvect(thetaxyz))
XTR.append(v2RS[0])
YTR.append(v2RS[1])
ZTR.append(v2RS[2])
return XTR, YTR, ZTR
'''transforms point(s) from local coordinate system to corresponding glocal coordinate system. origin is the LOCAL origin in GLOBAL coordinates'''
def transformLG(x, y, z, origin, thetaxyz):
XTR = []
YTR = []
ZTR = []
if type(x) is int or type(x) is float or type(x) is numpy.float64:
v = [x, y, z]
if x == 0 and y == 0 and z == 0:
vf = shift(v, origin)
XTR = vf[0]
YTR = vf[1]
ZTR = vf[2]
else:
v2R = rotate(v, thetaxyz)
v2RS = shift(v2R, origin)
XTR = v2RS[0]
YTR = v2RS[1]
ZTR = v2RS[2]
else:
for i in range(0, len(x)):
v = [x[i], y[i], z[i]]
if x[i] == 0 and y[i] == 0 and z[i] == 0:
vf = shift(v, origin)
XTR.append(vf[0])
YTR.append(vf[1])
ZTR.append(vf[2])
else:
v2R = rotate(v, thetaxyz)
v2RS = shift(v2R, origin)
XTR.append(v2RS[0])
YTR.append(v2RS[1])
ZTR.append(v2RS[2])
return XTR, YTR, ZTR
# given range, one point, origin, if it lies in or not
def SR3B(ranges, xinti, yinti, zinti, origin):
xr = ranges[0]
yr = ranges[1]
zr = ranges[2]
xc = origin[0]
yc = origin[1]
zc = origin[2]
if (((((xinti-xc)**2)/xr**2) + (((yinti-yc)**2)/yr**2) + (((zinti-zc)**2)/zr**2))) <= 1:
return True
else:
return False
'''Negvect: negates a vector. '''
def negvect(vect):
if type(vect[0]) is int or type(vect[0]) is float or type(vect[0]) is numpy.float64:
vectset = [-x for x in vect]
else:
vectset = [[-y for y in x] for x in vect]
return vectset
''' Spec: give the number of rays wanted, returns specular distribution of n vectors. Adapted from Meyer's Specular notebook.'''
def spec(n):
x, y, z = [], [], []
for i in np.arange(n):
theta = np.arccos(uniform(-1, 1))
phi = np.random.uniform(0, 2*np.pi)
xt = np.sin(theta)*np.cos(phi)
yt = np.sin(theta)*np.sin(phi)
zt = np.cos(theta)
if zt < 0.:
zt = -zt
a = uniform(0, 1)
while a > zt:
theta = np.arccos(uniform(-1, 1))
phi = np.random.uniform(0, 2*np.pi)
xt = np.sin(theta)*np.cos(phi)
yt = np.sin(theta)*np.sin(phi)
zt = np.cos(theta)
if zt < 0.:
zt = -zt
a = uniform(0, 1)
x = np.append(x, xt)
y = np.append(y, yt)
z = np.append(z, zt)
V = []
for i in np.arange(n):
v = [x[i], y[i], z[i]]
V.append(v)
return V
'''ReTransform (RT): given points and vectors, transforms from one coordinate system (given by thet and origin with respect to GLOBAL coordinate system) to another coordinate system (given by thet and origin with respect to GLOBAL coordinate system) '''
def RT(sourcepoints, v1, sourcethet1, ellipseorigin1, sourcethet2, ellipseorigin2):
if len(sourcepoints) == 0:
return [], []
spx, spy, spz = sep(sourcepoints)
vx, vy, vz = sep(v1)
vectorigin = [0, 0, 0] # don't shift vectors
# LOCAL to GLOBAL
vGx, vGy, vGz = transformLG(vx, vy, vz, vectorigin, sourcethet1)
spGx, spGy, spGz = transformLG(spx, spy, spz, ellipseorigin1, sourcethet1)
# GLOBAL back to SECOND LOCAL
vfx, vfy, vfz = transformGL(vGx, vGy, vGz, vectorigin, sourcethet2)
spfx, spfy, spfz = transformGL(
spGx, spGy, spGz, ellipseorigin2, sourcethet2)
sp = sepop(spfx, spfy, spfz)
v2 = sepop(vfx, vfy, vfz)
return sp, v2
'''Select Range specifically for ellipse 7 (see page 131 sheets) and 143.'''
def SR10(xrange, X, Y, Z, origin):
xintG = []
yintG = []
zintG = []
for i in range(0, len(X)):
xinti = X[i]
yinti = Y[i]
zinti = Z[i]
if (xinti-origin[0])**2 + (zinti)**2 <= xrange**2 and yinti < 0:
xintG.append(xinti)
yintG.append(yinti)
zintG.append(zinti)
return xintG, yintG, zintG
def SR7(xrange, X, Y, Z, origin):
xintG = []
yintG = []
zintG = []
for i in range(0, len(X)):
xinti = X[i]
yinti = Y[i]
zinti = Z[i]
if (xinti-origin[0])**2 + (zinti)**2 <= xrange**2 and yinti > 0:
xintG.append(xinti)
yintG.append(yinti)
zintG.append(zinti)
return xintG, yintG, zintG
'''Creates a circular source with a given radius'''
def circularsource(r, n): # radius
xpoint = []
ypoint = []
zpoint = []
x = np.asarray([r*random.uniform(-1, 1) for a in np.random.rand(n)])
x = x.astype(float)
y = np.asarray([r*random.uniform(-1, 1) for a in np.random.rand(n)])
y = y.astype(float)
for i in range(n):
d = (x[i]**2) + (y[i]**2)
if d < r**2:
xpoint.append(x[i])
ypoint.append(y[i])
zpoint.append(0.0)
return xpoint, ypoint, zpoint
'''generates random points within a circle '''
def circ2(r):
x = np.asarray([r*random.uniform(-1, 1) for a in np.random.rand(10)])
x = x.astype(float)
y = np.asarray([r*random.uniform(-1, 1) for a in np.random.rand(10)])
y = y.astype(float)
for i in range(10):
d = (x[i]**2) + (y[i]**2)
if d < r**2:
return x[i], y[i]
''' A different function that generates n points within a circle with radius r'''
def circularsource1(r, n): # radius
xpoint = []
ypoint = []
zpoint = []
for i in range(n):
x, y = circ2(r)
xpoint.append(x)
ypoint.append(y)
zpoint.append(0.0)
return xpoint, ypoint, zpoint
'''FormSource: (fixed) give number of rays, potential source points, the GLOBAL angle, and GLOBAL origin (that the source should be made with respect to). Returns random collection of points and vectors. '''
def FS(specnum, sourcepoint, sourcethet, origin):
originG = [0, 0, 0]
if type(sourcepoint[0]) is int or type(sourcepoint[0]) is float or type(sourcepoint[0]) is numpy.float64:
v1 = spec(specnum)
vx, vy, vz = sep(v1)
v1x, v1y, v1z = transformLG(vx, vy, vz, originG, sourcethet)
p1x, p1y, p1z = shift(sourcepoint, origin)
sp = [p1x, p1y, p1z]
v2 = sepop(v1x, v1y, v1z)
else:
v1 = spec(specnum)
vx, vy, vz = sep(v1)
v1x, v1y, v1z = transformLG(vx, vy, vz, originG, sourcethet)
v2 = sepop(v1x, v1y, v1z)
sp = []
for i in range(0, specnum):
j = random.randint(0, len(sourcepoint[0])-1)
spT = [sourcepoint[0][j], sourcepoint[1][j], sourcepoint[2][j]]
sp.append(spT)
return sp, v2
'''creates a list of potential source points within a certain range, tilted at a certain angle, corresponding to a specific origin.
def specsource(r,origin,thet,n):
if r ==0.0:
return origin
x,y,z=circularsource(r,n)
x1,y1,z1 = transformLG(x,y,z,origin,thet)
sourcepoint = [x1,y1,z1]
return sourcepoint '''
'''from the xrange being determined in GLOBAL coordinate system, translates
from Global to Local. Returns center point and the xrange'''
def xrangeGL6(x1, y1, z1, x3, y3, z3, origin, thet):
x, y, z = [], [], []
x2, y2, z2 = transformGL(x1, y1, z1, origin, thet)
x4, y4, z4 = transformGL(x3, y3, z3, origin, thet)
x.extend(x2), x.extend(x4), y.extend(
y2), y.extend(y4), z.extend(z2), z.extend(z4)
xrangeL = np.sqrt((min(x) - max(x))**2)/2
yrangeL = np.sqrt((min(y) - max(y))**2)/2
zrangeL = np.sqrt((min(z) - max(z))**2)/2
xcenter = min(x) + xrangeL
ycenter = min(y) + yrangeL
zcenter = min(z) + zrangeL
xcenterL = [xcenter, ycenter, zcenter]
xrangesL = [xrangeL, yrangeL, zrangeL]
return xcenterL, xrangesL
'''from the xrange being determined in GLOBAL coordinate system, translates
from Global to Local. Returns center point and the xrange. BUT random yrange to
maximize area covered. (see fixing xrangeGL6)'''
def xrangeGL7(x1, y1, z1, x3, y3, z3, origin, thet):
x, y, z = [], [], []
x2, y2, z2 = transformGL(x1, y1, z1, origin, thet)
x4, y4, z4 = transformGL(x3, y3, z3, origin, thet)
x.extend(x2), x.extend(x4), y.extend(
y2), y.extend(y4), z.extend(z2), z.extend(z4)
xrangeL = np.sqrt((min(x) - max(x))**2)/2
yrangeL = np.sqrt((min(y) - max(y))**2)/2
zrangeL = np.sqrt((min(z) - max(z))**2)/2
xcenter = min(x) + xrangeL
ycenter = min(y) + yrangeL
zcenter = min(z) + zrangeL
xcenterL = [xcenter, ycenter, zcenter]
xrangesL = [xrangeL, 200, zrangeL]
return xcenterL, xrangesL
def SR103di(ranges, X, Y, Z, origin): # this corrects SR103d
xintG = []
yintG = []
zintG = []
xr = ranges[0]
yr = ranges[1]
zr = ranges[2]
xc = origin[0]
yc = origin[1]
zc = origin[2]
for i in range(0, len(X)):
xinti = X[i]
yinti = Y[i]
zinti = Z[i]
if (((xinti-xc)**2)/(xr**2) + ((yinti-yc)**2)/(yr**2) <= 1
and ((yinti-yc)**2)/(yr**2) + ((zinti-zc)**2)/(zr**2) <= 1
and ((zinti-zc)**2)/(zr**2) + ((xinti-xc)**2)/(xr**2) <= 1):
xintG.append(xinti)
yintG.append(yinti)
zintG.append(zinti)
return xintG, yintG, zintG
'''given original point and vector from it, figure out properties of intersection wanted returns x2 greater (G) than or less (L) than x1'''
def finddirec(p1, v, intpos, intneg, vectpos, vectneg):
x1 = p1[0]
y1 = p1[1]
z1 = p1[2]
v1 = v[0]
v2 = v[1]
v3 = v[2]
xpos = intpos[0]
ypos = intpos[1]
zpos = intpos[2]
xneg = intneg[0]
yneg = intneg[1]
zneg = intneg[2]
direc = []
direcpos = []
direcneg = []
if v1 >= 0:
direc.append('G')
if v1 < 0:
direc.append('L')
if v2 >= 0:
direc.append('G')
if v2 < 0:
direc.append('L')
if v3 >= 0:
direc.append('G')
if v3 < 0:
direc.append('L')
if xpos >= x1:
direcpos.append('G')
if xpos < x1:
direcpos.append('L')
if ypos >= y1:
direcpos.append('G')
if ypos < y1:
direcpos.append('L')
if zpos >= z1:
direcpos.append('G')
if zpos < z1:
direcpos.append('L')
if xneg >= x1:
direcneg.append('G')
if xneg < x1:
direcneg.append('L')
if yneg >= y1:
direcneg.append('G')
if yneg < y1:
direcneg.append('L')
if zneg >= z1:
direcneg.append('G')
if zneg < z1:
direcneg.append('L')
if direc == direcpos:
return intpos, vectpos
else:
if direc == direcneg:
return intneg, vectneg
'''Reflection of a ray off of an ellipse using ELI2. '''
def REPCNi(coeffellipse, pli, v):
Npos, Nneg = NormalP(pli, v, coeffellipse) # plane coefficients
VectLNorm = N(v) # incident unit vector
Npos = np.array([-x for x in Npos])
Nneg = np.array([-x for x in Nneg])
vectpos = VectLNorm - 2*N(Npos)*(np.dot(VectLNorm, N(Npos)))
vectneg = VectLNorm - 2*N(Nneg)*(np.dot(VectLNorm, N(Nneg)))
xint, yint, zint = ELI2(pli, v, coeffellipse)
intpos = [float(xint[0]), float(yint[0]), float(
zint[0])] # array and points of intersection
intneg = [float(xint[1]), float(yint[1]), float(
zint[1])] # array and points of intersection
GoodInt, GoodVect = finddirec(pli, v, intpos, intneg, vectpos, vectneg)
return GoodInt, GoodVect
'''reflection of a source (mulitple rays). uses 3d ellipses for range, ellipse origin for center of DESIRED range, '''
def RSEPCNi(coeffellipse, pli, vectors, ranges, ellipseorigin):
Vect = []
pointints = []
if len(pli) == 0:
return [], []
if type(pli[0]) is int or type(pli[0]) is float: # assuming it is a source from one point
for i in range(0, len(vectors)):
Gpoint, Gvect = REPCNi(coeffellipse, pli, vectors[i])
if SR3B(ranges, Gpoint[0], Gpoint[1], Gpoint[2], ellipseorigin) == True:
pointints.append(Gpoint)
Vect.append(Gvect)
else:
for i in range(0, len(pli)):
Vi = vectors[i]
Pli = pli[i] # (or pli/original points of lines)
Gpoint, Gvect = REPCNi(coeffellipse, Pli, Vi)
if SR3B(ranges, Gpoint[0], Gpoint[1], Gpoint[2], ellipseorigin) == True:
pointints.append(Gpoint)
Vect.append(Gvect)
return pointints, Vect
''' PlaneLineIntersectionz(PLINT): given a plane z = a number, finds intersection points of all rays'''
def PLINTz(z, p, v):
points = []
for i in range(0, len(p)):
t = (z - p[i][2])/v[i][2]
xi = p[i][0] + t*v[i][0]
yi = p[i][1] + t*v[i][1]
points.append([xi, yi, z])
return points
''' PlaneLineIntersection(PLINT): given a plane y = a number, finds intersection points of all rays'''
def PLINTy(y, p, v):
points = []
for i in range(0, len(p)):
t = (y - p[i][1])/v[i][1]
xi = p[i][0] + t*v[i][0]
zi = p[i][2] + t*v[i][2]
points.append([xi, y, zi])
return points
'''select region mirror. if a point is within the ellipse of a mirror, return true.'''
def SRM(p, coeffmirr, origin):
X = p[0]
Z = p[2]
if ((((X-origin[0])**2)/coeffmirr[1]**2) + ((Z-origin[2])**2)/coeffmirr[0]**2) <= 1:
return True
return False
'''find intersection points of rays and the mirror.'''
def IntM(p, v, coeffmirr, originmirr):
hitints = []
hitvects = []
missints = []
missvects = []
intpoints = PLINTy(originmirr[1], p, v)
for i in range(0, len(intpoints)):
if SRM(intpoints[i], coeffmirr, originmirr) == True:
hitints.append(intpoints[i])
VectLNorm = N(v[i])
PNorm = [0, -1, 0] # from definition of mirror (check sign what)
VectReflect = VectLNorm - 2*N(PNorm)*(np.dot(VectLNorm, N(PNorm)))
hitvects.append(VectReflect) # change to reflected
else:
missints.append(intpoints[i])
missvects.append(v[i])
return hitints, hitvects, missints, missvects
''' for ONE ray'''
def PLINTyS(y, p, v):
t = (y - p[1])/v[1]
xi = p[0] + t*v[0]
zi = p[2] + t*v[2]
return(xi, y, zi)
''' for ONE ray'''
def PLINTzS(z, p, v):
t = (z - p[i][2])/v[i][2]
xi = p[i][0] + t*v[i][0]
yi = p[i][1] + t*v[i][1]
return [xi, yi, z]
'''find intersection points of one ray and the mirror.'''
def IntMS(p, v, coeffmirr, originmirr):
'''test is a test'''
hitints = []
hitvects = []
missints = []
missvects = []
intpoint = PLINTyS(originmirr[1], p, v)
if SRM(intpoint, coeffmirr, originmirr) == True:
hitints = intpoint
VectLNorm = N(v)
PNorm = [0, -1, 0] # from definition of mirror (check sign what)
VectReflect = VectLNorm - 2*N(PNorm)*(np.dot(VectLNorm, N(PNorm)))
hitvects = VectReflect # change to reflected
else:
missints = intpoint
missvects = v
return hitints, hitvects, missints, missvects
'''plotting the mirror '''
def mirror(origin, coeffmirr, y):
px = []
pz = []
py = []
X = np.linspace(-coeffmirr[1], coeffmirr[1], 50)
Z = np.linspace(-coeffmirr[0], coeffmirr[0], 50)
for i in range(50):
x = X[i]
for j in range(50):
z = Z[j]
if ((((x-origin[0])**2)/coeffmirr[1]**2) + ((z-origin[2])**2)/coeffmirr[0]**2) < 1:
px.append(x)
pz.append(z)
py.append(y)
return px, py, pz
'''plotting the polarizers '''
def polarizer(origin, coeffmirr, y):
px = []
pz = []
py = []
X = np.linspace(-coeffmirr[1], coeffmirr[1], 50)
Z = np.linspace(-coeffmirr[0], coeffmirr[0], 50)
for i in range(50):
x = X[i]
for j in range(50):
z = Z[j]
if ((((x)**2)/coeffmirr[1]**2) + ((z)**2)/coeffmirr[0]**2) < 1:
px.append(x)
pz.append(z)
py.append(y)
thet = [0, 0, 0]
px, py, pz = transformLG(px, py, pz, origin, thet)
return px, py, pz
''' EllipseLineInt(ELI): Give point of the line, vector of the line, and coefficients of the ellipse, find the intersection(s) of the line and the ellipsoid (assuming ellipse is rotated about the x-axis.
This is where (x-x_0)/a = (y-y_0)/b) = (z-z_0)/c. And, x^2/d^2 + y^2/e^2 + z^2/e^2 = 1 (ellipse is rotated around x axis to form ellipsoid. Different version of solving equation. '''
def ELI3(pli, v1, coeffellipse):
x0, y0, z0, vx, vy, vz, a, b = ELIorganize(pli, v1, coeffellipse)
A = 1/(a**2) + ((vy**2)/((b**2)*(vx**2))) + ((vz**2)/((b**2)*(vx**2)))
B1 = ((-2*x0*(vy**2))/((b**2)*(vx**2))) + ((2*vy*y0)/((b**2)*(vx)))
B2 = ((-2*x0*(vz**2))/((b**2)*(vx**2))) + ((2*vz*z0)/((b**2)*(vx)))
B = B1 + B2