Exemple #1
0
def editor(current_dir, input_file_name=False):

    print("\n Entering Editor\n")

    sim_dir = current_dir + '/sim/'

    file_names, param = setup.read_shell_input(current_dir, sim_dir,
                                               input_file_name)
    print("\n Loading restart file {}.npy\n".format(
        sim_dir + file_names['restart_file_name']))
    restart = ut.load_npy(sim_dir + file_names['restart_file_name'])
    pos = restart[0]
    vel = restart[1]
    cell_dim = pos[-1]
    pos = pos[:-1]

    if ('-nrepx' in sys.argv):
        n_rep_x = int(sys.argv[sys.argv.index('-nrepx') + 1]) + 1
    else:
        n_rep_x = 1
    if ('-nrepy' in sys.argv):
        n_rep_y = int(sys.argv[sys.argv.index('-nrepy') + 1]) + 1
    else:
        n_rep_y = 1
    if ('-nrepz' in sys.argv):
        n_rep_z = int(sys.argv[sys.argv.index('-nrepz') + 1]) + 1
    else:
        n_rep_z = 1

    param, keys = check_edit_param(sys.argv, param)

    if (n_rep_x * n_rep_y * n_rep_z) > 1:
        run_temp = True
        keys += [
            'n_fibril_x', 'n_fibril_y', 'n_fibril_z', 'n_fibril', 'n_bead'
        ]
        pos, vel, cell_dim, param = repeat_pos_array(pos, vel, cell_dim, param,
                                                     n_rep_x, n_rep_y, n_rep_z)
    else:
        run_temp = False

    print(" New Simulation Parameters:")
    for key in keys:
        print(" {:<15s} : {}".format(key, param[key]))

    if run_temp:
        from simulation import equilibrate_temperature
        pos, vel = equilibrate_temperature(sim_dir, pos, cell_dim,
                                           param['bond_matrix'],
                                           param['vdw_matrix'], param)

    print("\n Saving parameter file {}".format(file_names['param_file_name']))
    pickle.dump(param,
                open(sim_dir + file_names['param_file_name'] + '.pkl', 'wb'))

    print(" Saving restart file {}\n".format(file_names['restart_file_name']))
    ut.save_npy(sim_dir + file_names['restart_file_name'], (np.vstack(
        (pos, cell_dim)), vel))
Exemple #2
0
def speed_test(current_dir, comm, input_file_name=False, size=1, rank=0):
    """
	calc_state(pos, cell_dim, bond_matrix, vdw_matrix, param)
	
	Calculate state of simulation using starting configuration and parameters provided

	Parameters
	----------

	pos:  array_like (float); shape=(n_bead, n_dim)
		Positions of n_bead beads in n_dim

	cell_dim: array_like, dtype=float
		Array with simulation cell dimensions

	bond_matrix: array_like (int); shape=(n_bead, n_bead)
		Matrix determining whether a bond is present between two beads

	vdw_matrix: array_like (int); shape=(n_bead, n_bead)
		Matrix determining whether a non-bonded interaction is present between two beads

	param:  dict
		Dictionary of simulation and analysis parameters
	
	Returns
	-------

	frc: array_like, dtype=float
		Forces acting upon each bead in all collagen fibrils

	verlet_list: array_like, dtype=int
		Matrix determining whether two beads are within rc radial distance

	pot_energy:  float
		Total potential energy of system

	virial_tensor:  array_like, (float); shape=(n_dim, n_dim)
		Virial components of pressure tensor of system

	bond_beads:  array_like, (int); shape=(n_angle, 3)
		Array containing indicies in pos array all 3-bead angular interactions

	dist_index:  array_like, (int); shape=(n_bond, 2)
		Array containing indicies in distance arrays of all bonded interactions

	r_index:  array_like, (int); shape=(n_bond, 2)
		Array containing indicies in r array of all bonded interactions
	
	"""

    import time

    sim_dir = current_dir + '/sim/'

    if rank == 0:
        setup_time_start = time.time()
        if not os.path.exists(sim_dir): os.mkdir(sim_dir)
        file_names, param = setup.read_shell_input(current_dir,
                                                   sim_dir,
                                                   input_file_name,
                                                   verbosity=False)
    else:
        file_names = None
        param = None
    file_names = comm.bcast(file_names, root=0)
    param = comm.bcast(param, root=0)

    n_frames = int(param['n_step'] / param['save_step'])
    dig = len(str(param['n_step']))
    sqrt_dt = np.sqrt(param['dt'])

    pos, vel, cell_dim, param = setup.import_files_mpi(sim_dir,
                                                       file_names,
                                                       param,
                                                       comm,
                                                       size,
                                                       rank,
                                                       verbosity=False)

    n_dof = param['n_dim'] * param['n_bead']

    if rank == 0:
        tot_pos = np.zeros((n_frames, param['n_bead'] + 1, param['n_dim']))
        tot_vel = np.zeros((n_frames, param['n_bead'], param['n_dim']))
        tot_frc = np.zeros((n_frames, param['n_bead'], param['n_dim']))

        tot_temp = np.zeros(param['n_step'])
        tot_energy = np.zeros(param['n_step'])
        tot_press = np.zeros(param['n_step'])
        tot_vol = np.zeros(param['n_step'])

    if param['n_dim'] == 2:
        from sim_tools import calc_energy_forces_2D_mpi as calc_energy_forces
    elif param['n_dim'] == 3:
        from sim_tools import calc_energy_forces_3D_mpi as calc_energy_forces

    bond_indices, angle_indices, angle_bond_indices = ut.update_bond_lists_mpi(
        param['bond_matrix'], comm, size, rank)

    pos_indices = np.array_split(np.arange(param['n_bead']), size)[rank]
    frc_indices = (bond_indices[0] + pos_indices[0], bond_indices[1])
    angle_coeff = np.array_split(param['angle_array'], size)[rank]
    vdw_coeff = np.array_split(param['vdw_matrix'], size)[rank]
    virial_indicies = ut.create_index(
        np.argwhere(np.array_split(np.tri(param['n_bead']).T, size)[rank]))

    calc_times = []
    overhead_times = []
    if ('-ntrial' in sys.argv):
        n_trial = int(sys.argv[sys.argv.index('-ntrial') + 1])
    else:
        n_trial = 2000

    for i in range(n_trial):
        start_time = time.time()

        frc, pot_energy, virial_tensor = calc_energy_forces(
            pos, cell_dim, pos_indices, bond_indices, frc_indices,
            angle_indices, angle_bond_indices, angle_coeff, vdw_coeff,
            virial_indicies, param)

        stop_time_1 = time.time()
        calc_times.append(stop_time_1 - start_time)

        pot_energy = np.sum(comm.gather(pot_energy))
        frc = comm.allreduce(frc, op=MPI.SUM)
        virial_tensor = comm.allreduce(virial_tensor, op=MPI.SUM)

        stop_time_2 = time.time()
        overhead_times.append(stop_time_2 - stop_time_1)

    pressure = -1 / (np.prod(cell_dim) * param['n_dim']) * 0.5 * np.sum(
        np.diag(virial_tensor))
    calc_times = np.sum(comm.gather(calc_times, root=0))
    overhead_times = np.sum(comm.gather(overhead_times, root=0))

    if rank == 0:
        calc_times /= (size * n_trial)
        overhead_times /= (size * n_trial)
        print(
            " {:<12s} | {:>10.2f} | {:>10.5f} | {:>15.5f} | {:>21.5f} | {:>10.5f}"
            .format('MPI ' + str(size) + ' proc', pot_energy, pressure,
                    calc_times, overhead_times, calc_times + overhead_times))
Exemple #3
0
def simulation_mpi(current_dir, comm, input_file_name=False, size=1, rank=0):

    sim_dir = current_dir + '/sim/'

    if rank == 0:
        print("\n " + " " * 15 + "----Entering Setup----\n")
        setup_time_start = time.time()
        if not os.path.exists(sim_dir): os.mkdir(sim_dir)
        file_names, param = setup.read_shell_input(current_dir, sim_dir,
                                                   input_file_name)
    else:
        file_names = None
        param = None
    file_names = comm.bcast(file_names, root=0)
    param = comm.bcast(param, root=0)

    if param['n_dim'] == 2:
        from sim_tools import calc_energy_forces_2D_mpi as calc_energy_forces
    elif param['n_dim'] == 3:
        from sim_tools import calc_energy_forces_3D_mpi as calc_energy_forces

    n_frames = int(param['n_step'] / param['save_step'])
    dig = len(str(param['n_step']))
    sqrt_dt = np.sqrt(param['dt'])

    pos, vel, cell_dim, param = setup.import_files_mpi(sim_dir, file_names,
                                                       param, comm, size, rank)

    n_dof = param['n_dim'] * param['n_bead']

    if rank == 0:
        tot_pos = np.zeros((n_frames, param['n_bead'] + 1, param['n_dim']))
        tot_vel = np.zeros((n_frames, param['n_bead'], param['n_dim']))
        tot_frc = np.zeros((n_frames, param['n_bead'], param['n_dim']))

        tot_temp = np.zeros(param['n_step'])
        tot_energy = np.zeros(param['n_step'])
        tot_press = np.zeros(param['n_step'])
        tot_vol = np.zeros(param['n_step'])

    sim_state = setup.calc_state_mpi(pos, cell_dim, param, comm, size, rank)
    frc, pot_energy, virial_tensor, bond_indices, angle_indices, angle_bond_indices = sim_state

    pos_indices = np.array_split(np.arange(param['n_bead']), size)[rank]
    frc_indices = (bond_indices[0] + pos_indices[0], bond_indices[1])
    angle_coeff = np.array_split(param['angle_array'], size)[rank]
    vdw_coeff = np.array_split(param['vdw_matrix'], size)[rank]
    virial_indicies = ut.create_index(
        np.argwhere(np.array_split(np.tri(param['n_bead']).T, size)[rank]))

    kin_energy = ut.kin_energy(vel, param['mass'], param['n_dim'])
    pressure = 1 / (np.prod(cell_dim) * param['n_dim']) * (
        kin_energy - 0.5 * np.sum(np.diag(virial_tensor)))
    temperature = 2 * kin_energy / n_dof

    if rank == 0:
        tot_pos[0] += np.vstack((pos, cell_dim))
        tot_vel[0] += vel
        tot_frc[0] += frc

        tot_energy[0] = pot_energy + kin_energy
        tot_temp[0] = temperature
        tot_press[0] = pressure
        tot_vol[0] = np.prod(cell_dim)

        setup_time_stop = time.time()
        setup_time = setup_time_stop - setup_time_start
        time_hour = int(setup_time / 60**2)
        time_min = int((setup_time / 60) % 60)
        time_sec = int(setup_time) % 60

        print("\n " + " " * 15 + "----Setup Complete----\n")
        print(" {:5d} hr {:2d} min {:2d} sec ({:8.3f} sec)".format(
            time_hour, time_min, time_sec, setup_time))
        print(" Fibre diameter = {} um\n Simulation cell dimensions = {} um".
              format(param['l_conv'], cell_dim * param['l_conv']))
        print(" Cell density:     {:>10.4f} bead mass um-3".format(
            param['n_bead'] * param['mass'] /
            np.prod(cell_dim * param['l_conv'])))
        print(" Number of simulation steps = {}".format(param['n_step']))
        print(" Number of steps between saved traj = {}".format(
            param['save_step']))

        sim_time_start = time.time()

        print("\n" + " " * 15 + "----Running Simulation----")

    for step in range(1, param['n_step']):

        sim_state = velocity_verlet_alg_mpi(
            pos, vel, frc, virial_tensor, param, pos_indices, bond_indices,
            frc_indices, angle_indices, angle_bond_indices, angle_coeff,
            vdw_coeff, virial_indicies, param['dt'] / 2, sqrt_dt, cell_dim,
            calc_energy_forces, comm, size, rank)

        (pos, vel, frc, cell_dim, pot_energy, virial_tensor) = sim_state
        """
		"DYNAMIC BONDS - not yet implemented fully"
		if step % 1 == 0: 
			param['bond_matrix'], update = ut.bond_check(param['bond_matrix'], fib_end, r2, param['rc'], param['bond_rb'], param['vdw_sigma'])
			if update:
				bond_beads, dist_index, r_index, fib_end = ut.update_bond_lists(param['bond_matrix'])
				ut.update_param_file(sim_dir + file_names['param_file_name'], 'bond_matrix', param['bond_matrix'])
		#"""

        kin_energy = ut.kin_energy(vel, param['mass'], param['n_dim'])
        pressure = 1 / (np.prod(cell_dim) * param['n_dim']) * (
            kin_energy - 0.5 * np.sum(np.diag(virial_tensor)))
        temperature = 2 * kin_energy / n_dof

        if rank == 0:

            tot_energy[step] += pot_energy + kin_energy
            tot_temp[step] += temperature
            tot_press[step] += pressure
            tot_vol[step] = np.prod(cell_dim)

            if step % param['save_step'] == 0:
                i = int(step / param['save_step'])
                tot_pos[i] += np.vstack((pos, cell_dim))
                tot_vel[i] += vel
                tot_frc[i] += frc

                ut.save_npy(sim_dir + file_names['restart_file_name'],
                            (tot_pos[i], tot_vel[i]))

                sim_time = (time.time() -
                            sim_time_start) * (param['n_step'] / step - 1)
                time_hour = int(sim_time / 60**2)
                time_min = int((sim_time / 60) % 60)
                time_sec = int(sim_time) % 60

                print(" " + "-" * 56)
                print(" " + "| Step: {:{dig}d} {}   |".format(
                    step, " " * (44 - dig), dig=dig))
                print(" " +
                      "| Temp: {:>10.4f} kBT    Energy: {:>10.3f} per fibril |"
                      .format(tot_temp[step], tot_energy[step] /
                              param['n_fibril']))
                print(" " +
                      "| Pressure: {:>10.4f}    Volume: {:>10.4f}            |"
                      .format(tot_press[step], tot_vol[step]))
                print(" " + "|" + " " * 55 + "|")
                print(
                    " " +
                    "| Estimated time remaining: {:5d} hr {:2d} min {:2d} sec      |"
                    .format(time_hour, time_min, time_sec))
                print(" " + "-" * 56)

        if temperature >= param['kBT'] * 1E3:
            if rank == 0:
                print("Max temperature exceeded: {}  ({}), step ={}".format(
                    param['kBT'] * 1E3, temperature, step))
            n_step = step
            sys.exit()

    if rank == 0:
        sim_time_stop = time.time()

        sim_time = sim_time_stop - sim_time_start
        time_hour = int(sim_time / 60**2)
        time_min = int((sim_time / 60) % 60)
        time_sec = int(sim_time) % 60

        print("\n " + " " * 15 + "----Simulation Complete----\n")
        print(" {:5d} hr {:2d} min {:2d} sec ({:8.3f} sec)".format(
            time_hour, time_min, time_sec, sim_time))
        print("\n Averages:")
        print(" Average Temperature: {:>10.4f} kBT".format(np.mean(tot_temp)))
        print(" Average Energy:      {:>10.4f} per fibril".format(
            np.mean(tot_energy) / param['n_fibril']))
        print(" Average Pressure:    {:>10.4f}".format(np.mean(tot_press)))
        print(" Average Volume:      {:>10.4f}".format(np.mean(tot_vol)))
        print("\n RMS:")
        print(" RMS Temperature: {:>10.4f} kBT".format(np.std(tot_temp)))
        print(" RMS Energy:      {:>10.4f} per fibril".format(
            np.std(tot_energy / param['n_fibril'])))
        print(" RMS Pressure:    {:>10.4f}".format(np.std(tot_press)))
        print(" RMS Volume:      {:>10.4f}\n".format(np.std(tot_vol)))

        print(" Saving restart file {}".format(
            file_names['restart_file_name']))
        ut.save_npy(sim_dir + file_names['restart_file_name'],
                    (tot_pos[-1], tot_vel[-1]))

        print(" Saving trajectory file {}".format(
            file_names['traj_file_name']))
        ut.save_npy(sim_dir + file_names['traj_file_name'], tot_pos)

        print(" Saving output file {}".format(file_names['output_file_name']))
        ut.save_npy(sim_dir + file_names['output_file_name'],
                    (tot_energy, tot_temp, tot_press))
Exemple #4
0
def speed_test(current_dir, input_file_name=False):
    """
	calc_state(pos, cell_dim, bond_matrix, vdw_matrix, param)
	
	Calculate state of simulation using starting configuration and parameters provided

	Parameters
	----------

	pos:  array_like (float); shape=(n_bead, n_dim)
		Positions of n_bead beads in n_dim

	cell_dim: array_like, dtype=float
		Array with simulation cell dimensions

	bond_matrix: array_like (int); shape=(n_bead, n_bead)
		Matrix determining whether a bond is present between two beads

	vdw_matrix: array_like (int); shape=(n_bead, n_bead)
		Matrix determining whether a non-bonded interaction is present between two beads

	param:  dict
		Dictionary of simulation and analysis parameters
	
	Returns
	-------

	frc: array_like, dtype=float
		Forces acting upon each bead in all collagen fibrils

	verlet_list: array_like, dtype=int
		Matrix determining whether two beads are within rc radial distance

	pot_energy:  float
		Total potential energy of system

	virial_tensor:  array_like, (float); shape=(n_dim, n_dim)
		Virial components of pressure tensor of system

	bond_beads:  array_like, (int); shape=(n_angle, 3)
		Array containing indicies in pos array all 3-bead angular interactions

	dist_index:  array_like, (int); shape=(n_bond, 2)
		Array containing indicies in distance arrays of all bonded interactions

	r_index:  array_like, (int); shape=(n_bond, 2)
		Array containing indicies in r array of all bonded interactions
	
	"""

    import time, multiprocessing

    sim_dir = current_dir + '/sim/'

    setup_time_start = time.time()
    if not os.path.exists(sim_dir): os.mkdir(sim_dir)
    file_names, param = setup.read_shell_input(current_dir, sim_dir,
                                               input_file_name)

    n_frames = int(param['n_step'] / param['save_step'])
    dig = len(str(param['n_step']))
    sqrt_dt = np.sqrt(param['dt'])

    pos, vel, cell_dim, param = setup.import_files(sim_dir, file_names, param)

    n_dof = param['n_dim'] * param['n_bead']

    if param['n_dim'] == 2:
        from sim_tools import calc_energy_forces_2D as calc_energy_forces
    elif param['n_dim'] == 3:
        from sim_tools import calc_energy_forces_3D as calc_energy_forces

    bond_indices, angle_indices, angle_bond_indices = ut.update_bond_lists(
        param['bond_matrix'])

    calc_times = []
    overhead_times = []
    if ('-ntrial' in sys.argv):
        n_trial = int(sys.argv[sys.argv.index('-ntrial') + 1])
    else:
        n_trial = 2000

    print('\n Speed test in serial and MPI using < {} processors\n'.format(
        multiprocessing.cpu_count()))

    for i in range(n_trial):
        start_time = time.time()

        frc, pot_energy, virial_tensor = calc_energy_forces(
            pos, cell_dim, bond_indices, angle_indices, angle_bond_indices,
            param)

        stop_time_1 = time.time()
        calc_times.append(stop_time_1 - start_time)

    pressure = -1 / (np.prod(cell_dim) * param['n_dim']) * 0.5 * np.sum(
        np.diag(virial_tensor))

    calc_times = np.mean(calc_times)

    print(" {:^12s} | {:^10s} | {:^10s} | {:^15s} | {:^21s} | {:^10s} ".format(
        '', 'energy', 'pressure', 'calc time (s)', 'mpi overhead time (s)',
        'tot time (s)'))
    print(" " + "-" * 95)
    print(" {:<12s} | {:>10.2f} | {:>10.5f} | {:>15.5f} | {:>21s} | {:>10.5f}".
          format('Serial', pot_energy, pressure, calc_times, 'N/A',
                 calc_times))
Exemple #5
0
def analysis(current_dir, input_file_name=False):

    sim_dir = current_dir + '/sim/'
    gif_dir = current_dir + '/gif/'
    fig_dir = current_dir + '/fig/'
    data_dir = current_dir + '/data/'

    ow_shg = ('-ow_shg' in sys.argv)
    ow_data = ('-ow_data' in sys.argv)
    mk_gif = ('-mk_gif' in sys.argv)

    print("\n " + " " * 15 + "----Beginning Image Analysis----\n")
    if not os.path.exists(gif_dir): os.mkdir(gif_dir)
    if not os.path.exists(fig_dir): os.mkdir(fig_dir)
    if not os.path.exists(data_dir): os.mkdir(data_dir)

    file_names, param = setup.read_shell_input(current_dir,
                                               sim_dir,
                                               input_file_name,
                                               verbosity=False)
    fig_name = file_names['gif_file_name'].split('/')[-1]

    keys = ['l_conv', 'res', 'sharp', 'skip', 'l_sample', 'min_sample']
    print("\n Analysis Parameters found:")
    for key in keys:
        print(" {:<15s} : {}".format(key, param[key]))

    print("\n Loading output file {}{}".format(sim_dir,
                                               file_names['output_file_name']))
    tot_energy, tot_temp, tot_press = ut.load_npy(
        sim_dir + file_names['output_file_name'])

    tot_energy *= param['l_fibril'] / param['n_bead']
    print_thermo_results(fig_dir, fig_name, tot_energy, tot_temp, tot_press)

    print("\n Loading trajectory file {}{}.npy".format(
        sim_dir, file_names['traj_file_name']))
    tot_pos = ut.load_npy(sim_dir + file_names['traj_file_name'])

    n_frame = tot_pos.shape[0]
    n_image = int(n_frame / param['skip'])
    cell_dim = tot_pos[0][-1]
    n_xyz = tuple(
        np.array(cell_dim * param['l_conv'] * param['res'], dtype=int))
    conv = param['l_conv'] / param['sharp'] * param['res']

    image_md = np.moveaxis([tot_pos[n][:-1] for n in range(0, n_frame)], 2, 1)

    "Perform Fibre Vector analysis"
    tot_theta, tot_mag = fibre_vector_analysis(image_md, cell_dim, param)
    print_vector_results(fig_dir, fig_name, param, tot_mag, tot_theta)

    "Generate SHG Images"
    image_file_name = ut.check_file_name(file_names['output_file_name'], 'out',
                                         'npy') + '_{}_{}_{}_image_shg'.format(
                                             n_frame, param['res'],
                                             param['sharp'])

    if not ow_shg:
        try:
            image_shg = ut.load_npy(sim_dir + image_file_name,
                                    range(0, n_frame, param['skip']))
        except:
            ow_shg = True

    if ow_shg:
        "Generate Gaussian convoluted images and intensity derivatives"
        image_shg = shg_images(image_md, param['vdw_sigma'] * conv, n_xyz,
                               2 * param['rc'] * conv)

        print("\n Saving image files {}".format(
            file_names['output_file_name']))
        ut.save_npy(sim_dir + image_file_name, image_shg)

        image_shg = np.array(
            [image_shg[i] for i in range(0, n_frame, param['skip'])])

    fig_name += '_{}_{}'.format(param['res'], param['sharp'])

    "Make Gif of SHG Images"
    if ow_shg or mk_gif:
        print('\n Making Simulation SHG Gif {}/{}.gif'.format(
            fig_dir, fig_name))
        make_gif(fig_name + '_SHG', fig_dir, gif_dir, n_image, image_shg,
                 param, cell_dim * param['l_conv'], 'SHG')