Jim's
Tutorials

Spring 2019
course
site

So I didn't get too much done with the simulations as I have been very focused on my experiment, as that is the one component of my plan I am unable to work on after I leave. Honestly, I know things always go wrong in an experiment, but this one has been ridiculous.

Anyway, coding. I worked on cleaning up and documenting things much more clearly for one of the programs. I changed a few small things around to make them run better, though simulating more than a few hundred particles still takes an excessive amount of time. I have implemented tests on the three most fundamental functions of my code, though I still need to add tests to my gamma calcuating function, as well as my autocorrelation and power spectrum functions. My test for the time step is actually its own function and not a docstring test, because the time step actually moves things randomly. I still need to implement more tests on the phasemaker function, but I have encountered a small bug, though I don not know how much of an issue it will be. Essentially, math.sin(math.pi) != 0. where of course in reality \( \sin(\pi) = 0 \). It is still a small number, so I don't expect it to effect much (and I don't really have much intention of changing the angle anyway) but it is worth knowing about.

I am a little unsure how to approach making tests for the autocorrelation and power spectrum. I will probably end up doing the same thing I did for the time step and write a sort of second function purely for testing it out with known data.

Further there is a problem with my calculate gamma function, not even a bug really, but if I give it funny data (the type of stuff I would only simulate just to try different things out), it often spits out a domain error in one of the calculations. I would like for the program to continue with the other calculations as many are independent, and while it is useful to know about the domain error, I would have liked to see what the other calculations returned.


Jim says : the np.random.normal() also takes a shape ; google "numpy random normal"

>>> r = np.zeros((3, 10))
>>> r
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> r.shape
(3, 10)
>>> r[0]
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> r[0][4]
0.0
>>> r += np.random.normal(0.0, 1.0, r.shape)
>>> r
array([[ 0.86549219, -0.30952217,  0.7550444 , -0.28534548,  0.34299055,
         0.51720932,  0.05886278, -0.38516911,  1.28670218,  0.40743662],
       [-0.02715535,  0.55164946, -0.41770748,  1.07924797,  1.09859553,
         1.05311502, -0.28881042,  0.69651133,  0.75428301,  0.48562593],
       [-0.48109334, -1.12786699, -0.033737  ,  0.25195133, -0.31897119,
         0.08913677,  0.60184171, -1.08931517,  0.29819102, -0.53473455]])

in phasemaker, it's a dot product ... just get the order correct to have the sizes match.

>>> k = np.array([1,2,3])
>>> k
array([1, 2, 3])
>>> k.shape
(3,)
>>> k.transpose
<built-in method transpose of numpy.ndarray object at 0x10c0acd50>
>>> k.transpose()
array([1, 2, 3])
>>> k.transpose().shape()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable
>>> k.transpose().shape
(3,)
>>> np.dot(k, r)
array([-0.63209853, -2.58982422, -0.18158156,  2.62900445,  1.58326802,
        2.89084967,  1.28676707, -2.26009196,  3.68984127, -0.22551519])
>>> np.dot(r,k)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shapes (3,10) and (3,) not aligned: 10 (dim 1) != 3 (dim 0)
>>> 

Here's how to debug with try...catch :

def f(x, y):
    try:
        a = x/y
    except ZeroDivisionError:
        print("Oops: division by zero, x={}, y={}".format(x,y))
        a = 0
    return "oops"

print("1/0 is ", f(1,0))
https://cs.marlboro.college /cours /spring2019 /jims_tutorials /jtuttle /apr23
last modified Fri May 3 2024 9:52 pm

attachments [paper clip]

  last modified size
TXT brownianlab_3.html Fri May 03 2024 09:52 pm 360K
TXT brownianlab_3.ipynb Fri May 03 2024 09:52 pm 70K