예제 #1
0
def run(num_particles,
        box_width,
        box_height,
        box_depth,
        coverage_ratio,
        num_steps=4000):
    """
    The main method that starts the whole process. Initialises box with 
    all phonons and creates matplotlib animation.
    
    :param num_particles: The number of initial phonons
    :param box_width: The width of the box (max x-coordinate)
    :param box_height: The height of the box (max y-coordinate)
    :param box_depth: The depth of the box (max z-coordinate)
    :param coverage_ratio: The amount of surface area covered by detectors
    :param num_steps: The number of steps for the animation 
    :return: None
    """
    print("RUNNING \n \n")
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    particle_dict = {}
    colour_dict = {}

    filename = sys.argv[2]
    outf = open(filename, "a")

    material = Silicon

    # Generating all initial particles.
    for i in range(num_particles):
        random_x = np.random.uniform(0, box_width)
        random_y = np.random.uniform(0, box_height)
        random_z = np.random.uniform(0, box_depth)
        # Map particle indices to colours

        # Initial distribution of phonons governed by the distribution in
        # http://cdms.berkeley.edu/Dissertations/mpyle.pdf page 182.
        # 54.1% Slow Transverse, 36.3% Fast Transverse, 9.6% Longitudinal
        rand_type = (np.random.choice(3, 1, p=[0.541, 0.363, 0.096]) + 1)[0]
        colour_dict[i] = rand_type

        # Ensures that phonons are generated with the appropriate velocity
        # based on type. This velocity magnitude is fixed but the direction is
        # randomised.
        velocity = material.get_particle_velocity(rand_type)

        random_vx, random_vy, random_vz = create_random_spherical_vel(velocity)
        random_freq = np.random.uniform(LOWER_BOUND_FREQ, UPPER_BOUND_FREQ)

        ptcle = Particle(random_x, random_y, random_z, random_vx, random_vy,
                         random_vz, "Particle " + str(i), rand_type,
                         random_freq)

        if np.random.rand() < 1.0:
            ptcle.start_tracking()

        particle_dict[i] = ptcle

    # Box with initial starting configuration. Material parameters defined in UtilityMethods.py
    box = Box(material, box_width, box_height, box_depth, coverage_ratio,
              particle_dict, colour_dict)

    points = ax.scatter(box.get_x_array(),
                        box.get_y_array(),
                        box.get_z_array(),
                        facecolors=get_colour_array(colour_dict.values()))
    ax.set_ylim(0, box_height)
    ax.set_xlim(0, box_width)
    ax.set_zlim(0, box_depth)

    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')

    plt.gca().patch.set_facecolor('white')
    ax.w_xaxis.set_pane_color((0.8, 0.8, 0.8, 1.0))
    ax.w_yaxis.set_pane_color((0.8, 0.8, 0.8, 1.0))
    ax.w_zaxis.set_pane_color((0.8, 0.8, 0.8, 1.0))

    title = ax.set_title('3D Test')
    ani = animation.FuncAnimation(fig,
                                  simulate_step,
                                  frames=np.arange(0, num_steps),
                                  fargs=(box, points, colour_dict, title, outf,
                                         coverage_ratio),
                                  interval=1000)

    plt.grid()
    plt.show()