def generate_test_trajectory(structure, supercell=(1, 1, 1), minimum_frequency=0.1, # THz total_time=2, # picoseconds time_step=0.002, # picoseconds temperature=400, # Kelvin silent=False, memmap=False, phase_0=0.0): import random from dynaphopy.power_spectrum import _progress_bar print('Generating ideal harmonic data for testing') kb_boltzmann = 0.831446 # u * A^2 / ( ps^2 * K ) number_of_unit_cells_phonopy = np.prod(np.diag(structure.get_supercell_phonon())) number_of_unit_cells = np.prod(supercell) # atoms_relation = float(number_of_unit_cells)/ number_of_unit_cells_phonopy #Recover dump trajectory from file (test only) import pickle if False: dump_file = open( "trajectory.save", "r" ) trajectory = pickle.load(dump_file) return trajectory number_of_atoms = structure.get_number_of_cell_atoms() number_of_primitive_atoms = structure.get_number_of_primitive_atoms() number_of_dimensions = structure.get_number_of_dimensions() positions = structure.get_positions(supercell=supercell) masses = structure.get_masses(supercell=supercell) number_of_atoms = number_of_atoms*number_of_unit_cells number_of_primitive_cells = number_of_atoms/number_of_primitive_atoms atom_type = structure.get_atom_type_index(supercell=supercell) #Generate additional wave vectors sample # structure.set_supercell_phonon_renormalized(np.diag(supercell)) q_vector_list = pho_interface.get_commensurate_points(structure, np.diag(supercell)) q_vector_list_cart = [ np.dot(q_vector, 2*np.pi*np.linalg.inv(structure.get_primitive_cell()).T) for q_vector in q_vector_list] atoms_relation = float(len(q_vector_list)*number_of_primitive_atoms)/number_of_atoms #Generate frequencies and eigenvectors for the testing wave vector samples print('Wave vectors included in test (commensurate points)') eigenvectors_r = [] frequencies_r = [] for i in range(len(q_vector_list)): print(q_vector_list[i]) eigenvectors, frequencies = pho_interface.obtain_eigenvectors_and_frequencies(structure, q_vector_list[i]) eigenvectors_r.append(eigenvectors) frequencies_r.append(frequencies) number_of_frequencies = len(frequencies_r[0]) #Generating trajectory if not silent: _progress_bar(0, 'generating') #Generating trajectory trajectory = [] for time in np.arange(total_time, step=time_step): coordinates = np.array(positions[:, :], dtype=complex) for i_freq in range(number_of_frequencies): for i_long, q_vector in enumerate(q_vector_list_cart): if abs(frequencies_r[i_long][i_freq]) > minimum_frequency: # Prevent error due to small frequencies amplitude = np.sqrt(number_of_dimensions * kb_boltzmann * temperature / number_of_primitive_cells * atoms_relation)/(frequencies_r[i_long][i_freq] * 2 * np.pi) # + random.uniform(-1,1)*0.05 normal_mode = amplitude * np.exp(np.complex(0, -1) * frequencies_r[i_long][i_freq] * 2.0 * np.pi * time) phase = np.exp(np.complex(0, 1) * np.dot(q_vector, positions.T) + phase_0) coordinates += (1.0 / np.sqrt(masses)[None].T * eigenvectors_r[i_long][i_freq, atom_type] * phase[None].T * normal_mode).real trajectory.append(coordinates) if not silent: _progress_bar(float(time + time_step) / total_time, 'generating', ) trajectory = np.array(trajectory) time = np.array([i * time_step for i in range(trajectory.shape[0])], dtype=float) energy = np.array([number_of_atoms * number_of_dimensions * kb_boltzmann * temperature for i in range(trajectory.shape[0])], dtype=float) #Save a trajectory object to file for later recovery (test only) if False: dump_file = open("trajectory.save", "w") pickle.dump(dyn.Dynamics(structure=structure, trajectory=np.array(trajectory, dtype=complex), energy=np.array(energy), time=time, supercell=np.dot(np.diagflat(supercell), structure.get_cell())), dump_file) dump_file.close() # structure.set_supercell_phonon_renormalized(None) return dyn.Dynamics(structure=structure, trajectory=np.array(trajectory,dtype=complex), energy=np.array(energy), time=time, supercell=np.dot(np.diagflat(supercell), structure.get_cell()), memmap=memmap)
def generate_lammps_trajectory( structure, input_file, total_time=0.1, # picoseconds time_step=0.002, # picoseconds relaxation_time=0, silent=False, supercell=(1, 1, 1), memmap=False, # not fully implemented yet! velocity_only=False, lammps_log=True, sampling_interval=1): # in timesteps cmdargs_lammps = ['-echo', 'none', '-screen', 'none'] if not lammps_log: cmdargs_lammps += ['-log', 'none'] lmp = lammps(cmdargs=cmdargs_lammps) lmp.file(input_file) lmp.command('timestep {}'.format(time_step)) lmp.command('replicate {} {} {}'.format(*supercell)) lmp.command('run 0') # natoms = lmp.extract_global("natoms",0) # mass = lmp.extract_atom("mass",2) # temp = lmp.extract_compute("thermo_temp",0,0) # print("Temperature from compute =",temp) # print("Natoms, mass, x[0][0] coord =", natoms, mass[1], x[0][0]) # print ('thermo', lmp.get_thermo('1')) xlo = lmp.extract_global("boxxlo", 1) xhi = lmp.extract_global("boxxhi", 1) ylo = lmp.extract_global("boxylo", 1) yhi = lmp.extract_global("boxyhi", 1) zlo = lmp.extract_global("boxzlo", 1) zhi = lmp.extract_global("boxzhi", 1) xy = lmp.extract_global("xy", 1) yz = lmp.extract_global("yz", 1) xz = lmp.extract_global("xz", 1) simulation_cell = np.array([[xhi - xlo, xy, xz], [0, yhi - ylo, yz], [0, 0, zhi - zlo]]).T positions = [] velocity = [] energy = [] na = lmp.get_natoms() xc = lmp.gather_atoms("x", 1, 3) reference = np.array([xc[i] for i in range(na * 3)]).reshape((na, 3)) template = get_correct_arrangement(reference, structure) indexing = np.argsort(template) lmp.command('variable energy equal pe'.format( int(relaxation_time / time_step))) lmp.command('run {}'.format(int(relaxation_time / time_step))) if not silent: _progress_bar(0, 'lammps') n_loops = int(total_time / time_step / sampling_interval) for i in range(n_loops): if not silent: _progress_bar( float((i + 1) * time_step * sampling_interval) / total_time, 'lammps', ) lmp.command('run {}'.format(sampling_interval)) xc = lmp.gather_atoms("x", 1, 3) vc = lmp.gather_atoms("v", 1, 3) energy.append(lmp.extract_variable('energy', 'all', 0)) velocity.append( np.array([vc[i] for i in range(na * 3)]).reshape( (na, 3))[indexing, :]) if not velocity_only: positions.append( np.array([xc[i] for i in range(na * 3)]).reshape( (na, 3))[indexing, :]) positions = np.array(positions, dtype=complex) velocity = np.array(velocity, dtype=complex) energy = np.array(energy) if velocity_only: positions = None lmp.close() time = np.array( [i * time_step * sampling_interval for i in range(n_loops)], dtype=float) return dyn.Dynamics(structure=structure, trajectory=positions, velocity=velocity, time=time, energy=energy, supercell=simulation_cell, memmap=memmap)
def generate_lammps_trajectory(structure, input_file, total_time=0.1, # picoseconds time_step=0.002, # picoseconds relaxation_time=0, silent=False, supercell=(1, 1, 1), memmap=False, # not fully implemented yet! velocity_only=False, lammps_log=True, temperature=None, thermostat_mass=0.5, sampling_interval=1): # in timesteps cmdargs_lammps = ['-echo','none', '-screen', 'none'] if not lammps_log: cmdargs_lammps += ['-log', 'none'] lmp = lammps(cmdargs=cmdargs_lammps) lmp.file(input_file) lmp.command('timestep {}'.format(time_step)) lmp.command('replicate {} {} {}'.format(*supercell)) # Force reset temperature (overwrites lammps script) # This forces NVT simulation if temperature is not None: print ('Temperature reset to {} (NVT)'.format(temperature)) lmp.command('fix int all nvt temp {0} {0} {1}'.format(temperature, thermostat_mass)) # Check if correct number of atoms if lmp.extract_global("natoms",0) < 2: print ('Number of atoms in MD should be higher than 1!') exit() # Check if initial velocities all zero if not np.array(lmp.gather_atoms("v", 1, 3)).any(): print('Set lammps initial velocities') t = temperature if temperature is not None else 100 lmp.command('velocity all create {} 3627941 dist gaussian mom yes'.format(t)) lmp.command('velocity all scale {}'.format(t)) lmp.command('run 0') # natoms = lmp.extract_global("natoms",0) # mass = lmp.extract_atom("mass",2) # temp = lmp.extract_compute("thermo_temp",0,0) # print("Temperature from compute =",temp) # print("Natoms, mass, x[0][0] coord =", natoms, mass[1], x[0][0]) # print ('thermo', lmp.get_thermo('1')) xlo =lmp.extract_global("boxxlo", 1) xhi =lmp.extract_global("boxxhi", 1) ylo =lmp.extract_global("boxylo", 1) yhi =lmp.extract_global("boxyhi", 1) zlo =lmp.extract_global("boxzlo", 1) zhi =lmp.extract_global("boxzhi", 1) xy =lmp.extract_global("xy", 1) yz =lmp.extract_global("yz", 1) xz =lmp.extract_global("xz", 1) simulation_cell = np.array([[xhi-xlo, xy, xz], [0, yhi-ylo, yz], [0, 0, zhi-zlo]]).T positions = [] velocity = [] energy = [] na = lmp.get_natoms() xc = lmp.gather_atoms("x", 1, 3) reference = np.array([xc[i] for i in range(na*3)]).reshape((na,3)) template = get_correct_arrangement(reference, structure) indexing = np.argsort(template) lmp.command('variable energy equal pe'.format(int(relaxation_time/time_step))) lmp.command('run {}'.format(int(relaxation_time/time_step))) if not silent: _progress_bar(0, 'lammps') n_loops = int(total_time / time_step / sampling_interval) for i in range(n_loops): if not silent: _progress_bar(float((i+1) * time_step * sampling_interval) / total_time, 'lammps', ) lmp.command('run {}'.format(sampling_interval)) xc = lmp.gather_atoms("x", 1, 3) vc = lmp.gather_atoms("v", 1, 3) energy.append(lmp.extract_variable('energy', 'all', 0)) velocity.append(np.array([vc[i] for i in range(na * 3)]).reshape((na, 3))[indexing, :]) if not velocity_only: positions.append(np.array([xc[i] for i in range(na * 3)]).reshape((na, 3))[indexing, :]) positions = np.array(positions, dtype=complex) velocity = np.array(velocity, dtype=complex) energy = np.array(energy) if velocity_only: positions = None lmp.close() time = np.array([i * time_step * sampling_interval for i in range(n_loops)], dtype=float) return dyn.Dynamics(structure=structure, trajectory=positions, velocity=velocity, time=time, energy=energy, supercell=simulation_cell, memmap=memmap)
def generate_lammps_trajectory( structure, input_file, total_time=0.1, # picoseconds time_step=0.002, # picoseconds relaxation_time=0, silent=False, supercell=(1, 1, 1), memmap=False, # not fully implemented yet! velocity_only=False, lammps_log=True, temperature=None, thermostat_mass=0.5, sampling_interval=1): # in timesteps cmdargs_lammps = ['-echo', 'none', '-screen', 'none'] if not lammps_log: cmdargs_lammps += ['-log', 'none'] lmp = lammps(cmdargs=cmdargs_lammps) lmp.file(input_file) lmp.command('timestep {}'.format(time_step)) lmp.command('replicate {} {} {}'.format(*supercell)) # Force reset temperature (overwrites lammps script) # This forces NVT simulation if temperature is not None: print('Temperature reset to {} (NVT)'.format(temperature)) lmp.command('fix int all nvt temp {0} {0} {1}'.format( temperature, thermostat_mass)) # Check if correct number of atoms if lmp.extract_global("natoms", 0) < 2: print('Number of atoms in MD should be higher than 1!') exit() # Check if initial velocities all zero if not np.array(lmp.gather_atoms("v", 1, 3)).any(): print('Set lammps initial velocities') t = temperature if temperature is not None else 100 lmp.command( 'velocity all create {} 3627941 dist gaussian mom yes'. format(t)) lmp.command('velocity all scale {}'.format(t)) lmp.command('run 0') # natoms = lmp.extract_global("natoms",0) # mass = lmp.extract_atom("mass",2) # temp = lmp.extract_compute("thermo_temp",0,0) # print("Temperature from compute =",temp) # print("Natoms, mass, x[0][0] coord =", natoms, mass[1], x[0][0]) # print ('thermo', lmp.get_thermo('1')) try: xlo = lmp.extract_global("boxxlo") xhi = lmp.extract_global("boxxhi") ylo = lmp.extract_global("boxylo") yhi = lmp.extract_global("boxyhi") zlo = lmp.extract_global("boxzlo") zhi = lmp.extract_global("boxzhi") xy = lmp.extract_global("xy") yz = lmp.extract_global("yz") xz = lmp.extract_global("xz") except TypeError: xlo = lmp.extract_global("boxxlo", 1) xhi = lmp.extract_global("boxxhi", 1) ylo = lmp.extract_global("boxylo", 1) yhi = lmp.extract_global("boxyhi", 1) zlo = lmp.extract_global("boxzlo", 1) zhi = lmp.extract_global("boxzhi", 1) xy = lmp.extract_global("xy", 1) yz = lmp.extract_global("yz", 1) xz = lmp.extract_global("xz", 1) except UnboundLocalError: boxlo, boxhi, xy, yz, xz, periodicity, box_change = lmp.extract_box() xlo, ylo, zlo = boxlo xhi, yhi, zhi = boxhi simulation_cell = np.array([[xhi - xlo, xy, xz], [0, yhi - ylo, yz], [0, 0, zhi - zlo]]).T positions = [] velocity = [] energy = [] na = lmp.get_natoms() id = lmp.extract_atom("id", 0) id = np.array([id[i] - 1 for i in range(na)], dtype=int) xp = lmp.extract_atom("x", 3) reference = np.array([[xp[i][0], xp[i][1], xp[i][2]] for i in range(na)], dtype=float)[id, :] # xc = lmp.gather_atoms("x", 1, 3) # reference = np.array([xc[i] for i in range(na*3)]).reshape((na,3)) template = get_correct_arrangement(reference, structure) indexing = np.argsort(template) lmp.command('variable energy equal pe'.format( int(relaxation_time / time_step))) lmp.command('run {}'.format(int(relaxation_time / time_step))) if not silent: _progress_bar(0, 'lammps') n_loops = int(total_time / time_step / sampling_interval) for i in range(n_loops): if not silent: _progress_bar( float((i + 1) * time_step * sampling_interval) / total_time, 'lammps', ) lmp.command('run {}'.format(sampling_interval)) # xc = lmp.gather_atoms("x", 1, 3) # vc = lmp.gather_atoms("v", 1, 3) energy.append(lmp.extract_variable('energy', 'all', 0)) id = lmp.extract_atom("id", 0) id = np.array([id[i] - 1 for i in range(na)], dtype=int) vc = lmp.extract_atom("v", 3) velocity.append( np.array([[vc[i][0], vc[i][1], vc[i][2]] for i in range(na)], dtype=float)[id, :][indexing, :]) # velocity.append(np.array([vc[i] for i in range(na * 3)]).reshape((na, 3))[indexing, :]) if not velocity_only: xc = lmp.extract_atom("x", 3) positions.append( np.array([[xc[i][0], xc[i][1], xc[i][2]] for i in range(na)], dtype=float)[id, :][indexing, :]) # positions.append(np.array([xc[i] for i in range(na * 3)]).reshape((na, 3))[indexing, :]) positions = np.array(positions, dtype=complex) velocity = np.array(velocity, dtype=complex) energy = np.array(energy) if velocity_only: positions = None lmp.close() time = np.array( [i * time_step * sampling_interval for i in range(n_loops)], dtype=float) return dyn.Dynamics(structure=structure, trajectory=positions, velocity=velocity, time=time, energy=energy, supercell=simulation_cell, memmap=memmap)