Esempio n. 1
0
def test_get_dxyz():

    dx_check = np.array([[0, 20.3155606 - 20.3657056, 20.3155606 - 19.7335474],
                         [20.3657056 - 20.3155606, 0, 20.3657056 - 19.7335474],
                         [19.7335474 - 20.3155606, 19.7335474 - 20.3657056,
                          0]])
    dy_check = np.array([[0, 29.0287238 - 28.0910350, 29.0287238 - 29.3759130],
                         [28.0910350 - 29.0287238, 0, 28.0910350 - 29.3759130],
                         [29.3759130 - 29.0287238, 29.3759130 - 28.0910350,
                          0]])

    dx, dy = ut.get_distances(pos_2D, cell_dim_2D)

    assert abs(np.sum(dx - dx_check)) <= THRESH
    assert abs(np.sum(dy - dy_check)) <= THRESH

    dx_check = np.array([[0, 20.3155606 - 20.3657056, 20.3155606 - 19.7335474],
                         [20.3657056 - 20.3155606, 0, 20.3657056 - 19.7335474],
                         [19.7335474 - 20.3155606, 19.7335474 - 20.3657056,
                          0]])
    dy_check = np.array([[0, 29.0287238 - 28.0910350, 29.0287238 - 29.3759130],
                         [28.0910350 - 29.0287238, 0, 28.0910350 - 29.3759130],
                         [29.3759130 - 29.0287238, 29.3759130 - 28.0910350,
                          0]])
    dz_check = np.array([[0, 58.6756206 - 58.8612466, 58.6756206 - 59.3516029],
                         [58.8612466 - 58.6756206, 0, 58.8612466 - 59.3516029],
                         [59.3516029 - 58.6756206, 59.3516029 - 58.8612466,
                          0]])

    dx, dy, dz = ut.get_distances(pos_3D, cell_dim_3D)

    assert abs(np.sum(dx - dx_check)) <= THRESH
    assert abs(np.sum(dy - dy_check)) <= THRESH
    assert abs(np.sum(dz - dz_check)) <= THRESH
Esempio n. 2
0
def test_cos_sin_theta():

    dx, dy = ut.get_distances(pos_2D, cell_dim_2D)
    bond_indices, angle_indices, angle_bond_indices = ut.update_bond_lists(
        bond_matrix)

    vector = np.stack((dx[angle_bond_indices], dy[angle_bond_indices]), axis=1)
    n_vector = int(vector.shape[0])

    "Find |rij| values for each vector"
    r_vector = np.sqrt(np.sum(vector**2, axis=1))

    cos_the, sin_the, _ = cos_sin_theta_2D(vector, r_vector)
    check_sin_the = np.array([-0.39291528])

    assert abs(np.sum(cos_the - 0.91957468)) <= THRESH
    assert abs(np.sum(sin_the - check_sin_the)) <= THRESH

    dx, dy, dz = ut.get_distances(pos_3D, cell_dim_3D)
    bond_indices, angle_indices, angle_bond_indices = ut.update_bond_lists(
        bond_matrix)

    vector = np.stack((dx[angle_bond_indices], dy[angle_bond_indices],
                       dz[angle_bond_indices]),
                      axis=1)
    n_vector = int(vector.shape[0])

    "Find |rij| values for each vector"
    r_vector = np.sqrt(np.sum(vector**2, axis=1))

    cos_the, sin_the, _ = cos_sin_theta_3D(vector, r_vector)
    check_sin_the = np.array([[-0.48198494, -0.09796533, -0.36466797]])

    assert abs(np.sum(cos_the - 0.79063936)) <= THRESH
    assert abs(np.sum(sin_the - check_sin_the)) <= THRESH
Esempio n. 3
0
def fibre_vector_analysis(traj, cell_dim, param):
    """
	fibre_vector_analysis(traj, cell_dim, param)

	Parameters
	----------

	traj:  array_like (float); shape=(n_images, n_dim, n_bead)
			Array of sampled configurations from a simulation

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

	param:

	Returns
	-------

	tot_vectors, tot_mag

	"""

    n_image = traj.shape[0]
    bond_indices = np.nonzero(np.triu(param['bond_matrix']))
    n_bond = bond_indices[0].shape[0]
    bond_list = np.zeros((param['n_dim'], n_bond))

    tot_mag = np.zeros((n_image, param['n_fibril']))
    #tot_theta = np.zeros((n_image, param['n_fibril']))

    #tot_mag = np.zeros((n_image, n_bond))
    tot_theta = np.zeros((n_image, n_bond))

    for image, pos in enumerate(traj):

        distances = ut.get_distances(pos.T, cell_dim)
        for i in range(param['n_dim']):
            bond_list[i] = distances[i][bond_indices]

        bead_vectors = bond_list.T
        mag_vectors = np.sqrt(np.sum(bead_vectors**2, axis=1))
        #cos_theta = bead_vectors.T[0] / mag_vectors
        sin_theta = bead_vectors.T[1] / mag_vectors

        norm_vectors = bead_vectors / np.resize(mag_vectors,
                                                bead_vectors.shape)
        fib_vectors = np.sum(norm_vectors.reshape(param['l_fibril'] - 1,
                                                  param['n_fibril'],
                                                  param['n_dim']),
                             axis=0)
        mag_vectors = np.sqrt(np.sum(fib_vectors**2, axis=1))
        #cos_theta = fib_vectors.T[0] / mag_vectors
        #sin_theta = fib_vectors.T[1] / mag_vectors

        tot_theta[image] += np.arcsin(sin_theta) * 360 / np.pi
        #tot_theta[image] += np.arccos(cos_theta) * 360 / np.pi
        tot_mag[image] += mag_vectors / (param['l_fibril'] - 1)

    return tot_theta, tot_mag
Esempio n. 4
0
def equilibrate_temperature_mpi(sim_dir,
                                pos,
                                cell_dim,
                                param,
                                comm,
                                size=1,
                                rank=0,
                                inc=0.1,
                                thresh=5E-2):
    """
	equilibrate_temperature(pos, vel, cell_dim, bond_matrix, vdw_matrix, param, thresh=2E-2)

	Equilibrate temperature of system

	Parameters
	----------
	
	pos:  array_like (float); shape=(n_bead, n_dim)
		Updated positions of n_bead beads in n_dim

	vel: array_like, dtype=float
		Updated velocity of each bead in all collagen fibrils

	cell_dim:  array_like (float); shape=(n_dim)
		Simulation cell dimensions in n_dim 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

	thresh:  float (optional)
		Threshold difference between average system and reference temperature

		
	Returns
	-------

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

	vel: array_like, dtype=float
		Updated velocity of each bead in all collagen fibrils

	"""

    if rank == 0:
        print("\n" + " " * 15 + "----Equilibrating Temperature----\n")

    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

    dt = param['dt'] / 2
    sqrt_dt = np.sqrt(dt)
    n_dof = param['n_dim'] * param['n_bead']
    vel = np.zeros(pos.shape)

    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]))

    kBT = 2 * ut.kin_energy(vel, param['mass'], param['n_dim']) / n_dof
    step = 1
    kBT_array = [kBT]
    optimising = True
    ref_kBT = param['kBT']
    param['kBT'] = inc
    param['sigma'] = np.sqrt(param['gamma'] * (2 - param['gamma']) *
                             (param['kBT'] / param['mass']))

    if rank == 0:
        print(" Starting kBT:    {:>10.4f}\n Reference kBT:   {:>10.4f}\n".
              format(kBT, param['kBT']))
        print(" {:^18s} | {:^18s} | {:^18s}".format('Step', 'Ref kBT', 'kBT'))
        print(" " + "-" * 60)

    distances = ut.get_distances(pos, cell_dim)
    r2 = np.sum(distances**2, axis=0)
    verlet_list_rc = ut.check_cutoff(r2, param['rc']**2)

    while optimising:
        """	
		sim_state = velocity_verlet_alg(pos, vel, frc, virial_tensor, param, bond_matrix, vdw_matrix, 
			verlet_list_rc, angle_indices, angle_bond_indices, r_indices, param['dt']/2, sqrt_dt, cell_dim)
		(pos, vel, frc, cell_dim, pot_energy, virial_tensor, r2) = sim_state
		verlet_list_rc = ut.check_cutoff(r2, param['rc']**2)

		#"""
        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, dt, 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(bond_matrix)
		"""
        kBT = 2 * ut.kin_energy(vel, param['mass'], param['n_dim']) / n_dof
        kBT_array.append(kBT)

        if step % 250 == 0:
            av_kBT = np.mean(kBT_array)
            kBT_array = [kBT]
            if rank == 0:
                print(" {:18d} | {:>18.4f} | {:>18.4f}".format(
                    step, param['kBT'], av_kBT))
            if abs(av_kBT - param['kBT']) <= thresh:
                param['kBT'] += inc
                param['sigma'] = np.sqrt(param['gamma'] *
                                         (2 - param['gamma']) *
                                         (param['kBT'] / param['mass']))
                if ref_kBT <= param['kBT']: optimising = False

        step += 1

    param['kBT'] = ref_kBT
    param['sigma'] = np.sqrt(param['gamma'] * (2 - param['gamma']) *
                             (param['kBT'] / param['mass']))

    if rank == 0:
        print("\n No. iterations:   {:>10d}".format(step))
        print(" Final kBT:   {:>10.4f}".format(kBT))

    return pos, vel
Esempio n. 5
0
                bestj = j
                bestv = distances[current][j]

        results += str(bestj) + ': ' + str(bestv) + '\n'

        # We set the current point to the closest one
        current = bestj

    results += '0: ' + str(distances[current][0]) + '\n'

    # We print our path
    print(results)


# We ask for the number of cities
print('Number of cities ?')
number = eval(input())

# We generated random distances matrix (we prefer it to coordinates because it is faster, but less realistic)
distances = utilities.get_distances(number, 0, 1000)

t1 = time.time_ns()

# We execute the function
greedy(distances)

t2 = time.time_ns()

# We print the elapsed time of the function
print('Time: ' + str((t2 - t1) / 1000000) + 'ms')