Python Monte Carlo Python Plotting Question

AI Thread Summary
The discussion focuses on simulating the projection of a radiation source onto a detector plane to determine if the projection is circular. The user aims to create a list of vector objects representing points moved from the source to the detector, with random distributions in the plane. They plan to check if the x and z components of these vectors satisfy the equation of a circle defined by the collimator's radius. Suggestions include using NumPy for efficient calculations of squared distances and employing Matplotlib for plotting the results. Overall, the user is on the right track with their approach and code implementation.
clope023
Messages
990
Reaction score
130
If this is better served in a section like Nuclear Engineering please let me know.

Homework Statement

Simulate the projection of a radiation source onto the plane of a collimated detector. Check if said projection is circular.

The attempt at a solution

What I want to do is create a list of vector objects that will be 'moved' from their initial position to the final position at the plane of the detector. The source point is at the origin. The collimator entrance is 2mm (in the y-direction) away and the detector plane is 4mm (in the y-direction) away.
I want the final position of the vectors to be randomly distributed in the plane and check if the x and z elements of those vectors satisfy the equation of the circle with the radius of that circle being the radius of the circular collimator and then plot those positions. I want to be using vpython's vector class for some of the vector operations though if simple nested lists are just as good I will use those too. This is the code I have so far.

import numpy as np
import random
import matplotlib.pyplot as plt
from visual import vector
r = 2.5 #radius of collimator in mm
l = 50.8 #length of collimator in mm
n = 1000 #number of test points
lst = []
for i in range(n):
lst.append(vector(0,0,0)) #creates 1000 vectors at the origin
lst1 = []
rr = r*random.random()
for j in lst:
lst1.append(j + vector(rr, l, rr))
def toList(v):
return [v.x, v.y, v.z] #returns the components of the vector and places them in a list
lst2 = []
for k in lst1:
lst2.append(toList(k)) #places those list of vector components into another list
lst3 = []
for u in lst2:
lst3.append(u.pop())
# pop function removes and returns the last element in the list

At this point I have a list filled with a 1000 3 element lists, and I want to organize the x (0th elements) and z (2nd elements) components and put them in a 2d plot and then check if they satisfy the equation of the circle like so:

plt.plot(x,z,'o')
plt.show()
lst4 = []
lst5 = []
for v in lst3:
if v[0]**2 + v[2]**2 <= r**2:
lst4.append(v)
if v[0]**2 + v[2]**2 > r**2:
lst5.append(v)

So right now this where my logic takes me, am I on the right track? Are my commands right to do what I want to do? I appreciate any and all help, thanks very much.
 
Technology news on Phys.org
Yes, you're on the right track. You have the basic steps outlined for what you need to do, so now it's just a matter of implementing them in code. For example, you can use the numpy package to quickly calculate the squared distance between each vector and the origin, which will determine whether or not it falls within the circle. You can also use matplotlib to plot your results.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top