Modeling the Solid Angle with Vpython

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
clope023
Messages
990
Reaction score
130
A radiation source at some point through the collimator of a detector.

Modeled as 100 spheres tracing out the image of a circle on a plane some distance above the original position of the source.

What I want to do is collect all the spheres that go through the top of a cylinder and then calculate the area of the circle that they create. Afterwords I would move the source to 5 different positions and trace the area of the circles created by the intersection of the different solid angle planes.

This is my vpython code so far:

from visual import *
from random import uniform


f = frame()
c1 = cylinder(frame = f, pos = (0,5,0), axis = (0,2,0), radius = 7, thickness = 0.2, color = color.cyan)
#c2 = cylinder(frame = f, pos = (0,5,0), axis = (0,0.1,0), radius = 2, thickness = 0.2, color = color.cyan)


ball_list = []
ball_list2 = []
ball_list3 = []
ball_list4 = []
ball_list5 = []

n = 100
ball_rad = 0.01
dt = 0.01
t = 0
maxpos = 2
maxvel = 2

def draw_point():
phi = random.uniform()*2*pi
r = random.uniform()-0.5
ct = 0.9 + 0.2*r
theta = arccos(ct)
rr = sin(theta)
x = rr*cos(phi)
y = ct
z = rr*sin(phi)
return (x,y,z)

for i in range(n):
ball = sphere(color=color.red, radius = ball_rad)
ball.trail = curve(color=color.yellow)
ball.pos = maxpos*vector(0,0,0)
ball.velocity = maxvel*vector(draw_point())
#ball2 = sphere(color=color.red, radius = ball_rad)
#ball2.trail = curve(color=color.orange)
#ball2.pos = vector(2,0,0)
#ball2.velocity = vector(draw_point())
#ball3 = sphere(color=color.red, radius = ball_rad)
#ball3.trail = curve(color=color.blue)
#ball3.pos = vector(-2,0,0)
#ball3.velocity = vector(draw_point())
#ball4 = sphere(color=color.red, radius = ball_rad)
#ball4.trail = curve(color=color.magenta)
#ball4.pos = vector(4,0,0)
#ball4.velocity = vector(draw_point())
#ball5 = sphere(color=color.red, radius = ball_rad)
#ball5.trail = curve(color=color.green)
#ball5.pos = vector(-4,0,0)
#ball5.velocity = vector(draw_point())

rate(100)

for t in range(1000):
ball.pos += ball.velocity*dt
#ball2.pos += ball2.velocity*dt
#ball3.pos += ball3.velocity*dt
#ball4.pos += ball4.velocity*dt
#ball5.pos += ball5.velocity*dt
ball.trail.append(pos=ball.pos)
#ball2.trail.append(pos=ball2.pos)
#ball3.trail.append(pos=ball3.pos)
#ball4.trail.append(pos=ball4.pos)
#ball5.trail.append(pos=ball5.pos)
if ball.y > c1.axis:
ball.velocity = 0*ball.velocity

Are there any suggestions to make this more streamlined? Any help is appreciated.
 
Physics news on Phys.org


Hello, thank you for sharing your vpython code with us. It looks like you are on the right track with your simulation. Here are a few suggestions to make it more streamlined:

1. Instead of creating separate lists for each position of the source, you can create a single list and use a for loop to iterate through the different positions. This will make your code more compact and easier to modify in the future.

2. You can also use a function to calculate the area of the circles created by the intersection of the solid angle planes. This will make your code more modular and easier to modify.

3. Instead of using a for loop to iterate through time, you can use the rate() function to control the speed of your simulation. This will make your code more efficient.

4. You can also use the vector operations provided by vpython to make your code more compact and efficient. For example, instead of using separate variables for x, y and z coordinates, you can use the vector() function to create a vector with the desired coordinates.

5. Lastly, you can consider adding comments to your code to make it more readable and easier to understand for others.

I hope these suggestions help to make your code more streamlined. Keep up the good work!