Skip to content

Commit

Permalink
Added python script for "Ray relaying" example
Browse files Browse the repository at this point in the history
  • Loading branch information
StasFainer authored Mar 24, 2023
1 parent 0f2e995 commit b6e29dd
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions gallery/ray-relaying.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import numpy as np
import json

f = 40 # lens focal length
d = [80,152] # distance between consecutive lenses in each series, respectively
lensNum = [37,20] # number of lenses in each series, respectively
lensDiameter = 540
beamDisplacement = 40 # the light beam is placed horizontally at "beamDisplacement" to the left of the lens series
beamRadius = 60
x_origin = 0 # simulation origin coordinates
y_origin = 0 #
initial_x = 300 # lenses coordinate displacement
initial_y = 960 #
y_displacement = [0,1.5*lensDiameter] # vertical distance between consecutive series of lenses

# initialize the simulation general properties
Dict = dict()
Dict['version'] = 2
Dict['objs'] = []
Dict['mode'] = "light"
Dict['rayDensity_light'] = 0.25
Dict['rayDensity_images'] = 1
Dict['observer'] = None
Dict['origin'] = {'x':x_origin,'y':y_origin}
Dict['scale'] = 0.4
Dict['colorMode'] = False

# build the series of lenses with the corresponding light beams
for i in range(len(d)):
for k in range(lensNum[i]):
Dict['objs'].append({"type":"lens", "p1": {"type": 1, "x":initial_x+k*d[i], # add the k'th lens of the i'th series into the simulation
"y":initial_y+y_displacement[i], "exist":True}, "p2": {"type": 1, "x":initial_x+k*d[i],
"y":initial_y+lensDiameter+y_displacement[i], "exist":True}, "p": f })
Dict['objs'].append({"type":"parallel", "p1":{"type": 1, "x":initial_x-beamDisplacement, # add the light beam for the i'th series into the simulation
"y":0.5*(2*initial_y+lensDiameter)-beamRadius+y_displacement[i], "exist":True}, "p2":{"type": 1,
"x":initial_x-beamDisplacement, "y":0.5*(2*initial_y+lensDiameter)+beamRadius+y_displacement[i], "exist":True}, "p":0.5})

# int32 type is not serializable, therefore convert it to int type
def convert(o):
if isinstance(o, np.int32): return int(o)
raise TypeError

json_Dict = json.dumps(Dict, default=convert) # converts the dictionary to a json formatted string

# create the json (simulation) file
with open("ray-relaying.json", "w") as f:
f.write(json_Dict)

0 comments on commit b6e29dd

Please sign in to comment.