def initialize_from_hdf5_file(file_name, structure, read_trajectory=True, initial_cut=1, final_cut=None, memmap=False): import h5py print(("Reading data from hdf5 file: " + file_name)) trajectory = None velocity = None vc = None reduced_q_vector = None #Check file exists if not os.path.isfile(file_name): print((file_name + ' file does not exist!')) exit() hdf5_file = h5py.File(file_name, "r") if "trajectory" in hdf5_file and read_trajectory is True: trajectory = hdf5_file['trajectory'][:] if final_cut is not None: trajectory = trajectory[initial_cut-1:final_cut] else: trajectory = trajectory[initial_cut-1:] if "velocity" in hdf5_file: velocity = hdf5_file['velocity'][:] if final_cut is not None: velocity = velocity[initial_cut-1:final_cut] else: velocity = velocity[initial_cut-1:] if "vc" in hdf5_file: vc = hdf5_file['vc'][:] if final_cut is not None: vc = vc[initial_cut-1:final_cut] else: vc = vc[initial_cut-1:] if "reduced_q_vector" in hdf5_file: reduced_q_vector = hdf5_file['reduced_q_vector'][:] print(("Load trajectory projected onto {0}".format(reduced_q_vector))) time = hdf5_file['time'][:] super_cell = hdf5_file['super_cell'][:] hdf5_file.close() if vc is None: return dyn.Dynamics(structure=structure, trajectory=trajectory, velocity=velocity, time=time, supercell=np.dot(np.diagflat(super_cell), structure.get_cell()), memmap=memmap) else: return vc, reduced_q_vector, dyn.Dynamics(structure=structure, time=time, supercell=np.dot(np.diagflat(super_cell), structure.get_cell()), memmap=memmap)
def read_vasp_trajectory( file_name, structure=None, time_step=None, limit_number_steps=10000000, # Maximum number of steps read (for security) last_steps=None, initial_cut=1, end_cut=None, memmap=False): # warning warnings.warn( 'This parser will be deprecated, you can use XDATCAR instead', DeprecationWarning) # Check file exists if not os.path.isfile(file_name): print('Trajectory file does not exist!') exit() # Check time step if time_step is not None: print( 'Warning! Time step flag has no effect reading from VASP OUTCAR file (time step will be read from file)' ) if memmap: print( 'Warning! Memory mapping is not implemented in VASP OUTCAR parser') # Starting reading print("Reading VASP trajectory") print("This could take long, please wait..") # Dimensionality of VASP calculation number_of_dimensions = 3 with open(file_name, "r+") as f: #Memory-map the file file_map = mmap.mmap(f.fileno(), 0) position_number = file_map.find('NIONS =') file_map.seek(position_number + 7) number_of_atoms = int(file_map.readline()) #Read time step position_number = file_map.find('POTIM =') file_map.seek(position_number + 8) time_step = float( file_map.readline().split()[0]) * 1E-3 # in picoseconds #Reading super cell position_number = file_map.find('direct lattice vectors') file_map.seek(position_number) file_map.readline() super_cell = [] for i in range(number_of_dimensions): super_cell.append( file_map.readline().split()[0:number_of_dimensions]) super_cell = np.array(super_cell, dtype='double').T file_map.seek(position_number) file_map.readline() # Check if number of atoms is multiple of cell atoms if structure: if number_of_atoms % structure.get_number_of_cell_atoms() != 0: print( 'Warning: Number of atoms not matching, check VASP output files' ) # structure.set_number_of_atoms(number_of_atoms) # Read coordinates and energy trajectory = [] energy = [] counter = 0 while True: counter += 1 #Initial cut control if initial_cut > counter: continue position_number = file_map.find('POSITION') if position_number < 0: break file_map.seek(position_number) file_map.readline() file_map.readline() read_coordinates = [] for i in range(number_of_atoms): read_coordinates.append( file_map.readline().split()[0:number_of_dimensions]) position_number = file_map.find('energy(') file_map.seek(position_number) read_energy = file_map.readline().split()[2] trajectory.append( np.array(read_coordinates, dtype=float).flatten()) #in angstrom energy.append(np.array(read_energy, dtype=float)) #security routine to limit maximum of steps to read and put in memory if limit_number_steps + initial_cut < counter: print( "Warning! maximum number of steps reached! No more steps will be read" ) break if end_cut is not None and end_cut <= counter: break file_map.close() trajectory = np.array([[[ trajectory[i][j * number_of_dimensions + k] for k in range(number_of_dimensions) ] for j in range(number_of_atoms)] for i in range(len(trajectory))]) if last_steps is not None: trajectory = trajectory[-last_steps:, :, :] energy = energy[-last_steps:] print(('Number of total steps read: {0}'.format(trajectory.shape[0]))) time = np.array([i * time_step for i in range(trajectory.shape[0])], dtype=float) print('Trajectory file read') return dyn.Dynamics(structure=structure, trajectory=np.array(trajectory, dtype=complex), energy=np.array(energy), time=time, supercell=super_cell, memmap=memmap)
def read_VASP_XDATCAR(file_name, structure=None, time_step=None, limit_number_steps=10000000, last_steps=None, initial_cut=1, end_cut=None, memmap=False): # Time in picoseconds # Coordinates in Angstroms #Read environtment variables try: temp_directory = os.environ["DYNAPHOPY_TEMPDIR"] if os.path.isdir(temp_directory): print(('Set temporal directory: {0}'.format(temp_directory))) temp_directory += '/' else: temp_directory = '' except KeyError: temp_directory = '' number_of_atoms = None bounds = None #Check file exists if not os.path.isfile(file_name): print('Trajectory file does not exist!') exit() #Check time step if time_step is None: print('Warning! XDATCAR file does not contain time step information') print('Using default: 0.001 ps') time_step = 0.001 #Starting reading print("Reading XDATCAR file") print("This could take long, please wait..") #Dimensionality of VASP calculation number_of_dimensions = 3 time = [] data = [] counter = 0 with open(file_name, "r+") as f: file_map = mmap.mmap(f.fileno(), 0) #Read cell for i in range(2): file_map.readline() a = file_map.readline().split() b = file_map.readline().split() c = file_map.readline().split() super_cell = np.array([a, b, c], dtype='double').T for i in range(1): file_map.readline() number_of_atoms = np.array(file_map.readline().split(), dtype=int).sum() while True: counter += 1 #Read time steps position_number = file_map.find('Direct configuration') if position_number < 0: break file_map.seek(position_number) time.append(float(file_map.readline().split('=')[1])) if memmap: if end_cut: data = np.memmap( temp_directory + 'trajectory.{0}'.format(os.getpid()), dtype='complex', mode='w+', shape=(end_cut - initial_cut + 1, number_of_atoms, number_of_dimensions)) else: print( 'Memory mapping requires to define reading range (use read_from/read_to option)' ) exit() #Initial cut control if initial_cut > counter: continue #Reading coordinates read_coordinates = [] for i in range(number_of_atoms): read_coordinates.append( file_map.readline().split()[0:number_of_dimensions]) try: if memmap: data[counter - initial_cut, :, :] = np.array( read_coordinates, dtype=float) #in angstroms else: data.append(np.array(read_coordinates, dtype=float)) #in angstroms except ValueError: print(("Error reading step {0}".format(counter))) break # print(read_coordinates) #security routine to limit maximum of steps to read and put in memory if limit_number_steps + initial_cut < counter: print( "Warning! maximum number of steps reached! No more steps will be read" ) break if end_cut is not None and end_cut <= counter: break file_map.close() time = np.array(time) * time_step if not memmap: data = np.array(data, dtype=complex) if last_steps is not None: data = data[-last_steps:, :, :] time = time[-last_steps:] return dyn.Dynamics(structure=structure, scaled_trajectory=data, time=time, supercell=super_cell, memmap=memmap)
def read_lammps_trajectory(file_name, structure=None, time_step=None, limit_number_steps=10000000, last_steps=None, initial_cut=1, end_cut=None, memmap=False): # Time in picoseconds # Coordinates in Angstroms # Read environtment variables try: temp_directory = os.environ["DYNAPHOPY_TEMPDIR"] if os.path.isdir(temp_directory): print(('Set temporal directory: {0}'.format(temp_directory))) temp_directory += '/' else: temp_directory = '' except KeyError: temp_directory = '' number_of_atoms = None bounds = None #Check file exists if not os.path.isfile(file_name): print('Trajectory file does not exist!') exit() # Check time step if time_step is None: print( 'Warning! LAMMPS trajectory file does not contain time step information' ) print('Using default: 0.001 ps') time_step = 0.001 # Starting reading print("Reading LAMMPS trajectory") print("This could take long, please wait..") # Dimension of LAMMP calculation if structure: number_of_dimensions = structure.get_number_of_dimensions() else: number_of_dimensions = 3 time = [] data = [] counter = 0 lammps_labels = False with open(file_name, "r+") as f: file_map = mmap.mmap(f.fileno(), 0) while True: counter += 1 #Read time steps position_number = file_map.find('TIMESTEP') if position_number < 0: break file_map.seek(position_number) file_map.readline() time.append(float(file_map.readline())) if number_of_atoms is None: #Read number of atoms file_map = mmap.mmap(f.fileno(), 0) position_number = file_map.find('NUMBER OF ATOMS') file_map.seek(position_number) file_map.readline() number_of_atoms = int(file_map.readline()) # Check if number of atoms is multiple of cell atoms if structure: if number_of_atoms % structure.get_number_of_cell_atoms( ) != 0: print( 'Warning: Number of atoms not matching, check LAMMPS output file' ) if bounds is None: #Read cell file_map = mmap.mmap(f.fileno(), 0) position_number = file_map.find('BOX BOUNDS') file_map.seek(position_number) file_map.readline() bounds = [] for i in range(3): bounds.append(file_map.readline().split()) bounds = np.array(bounds, dtype=float) if bounds.shape[1] == 2: bounds = np.append(bounds, np.array([0, 0, 0])[None].T, axis=1) xy = bounds[0, 2] xz = bounds[1, 2] yz = bounds[2, 2] xlo = bounds[0, 0] - np.min([0.0, xy, xz, xy + xz]) xhi = bounds[0, 1] - np.max([0.0, xy, xz, xy + xz]) ylo = bounds[1, 0] - np.min([0.0, yz]) yhi = bounds[1, 1] - np.max([0.0, yz]) zlo = bounds[2, 0] zhi = bounds[2, 1] supercell = np.array([[xhi - xlo, xy, xz], [0, yhi - ylo, yz], [0, 0, zhi - zlo]]) #for 2D supercell = supercell[:number_of_dimensions, : number_of_dimensions] # Testing cell lx = xhi - xlo ly = yhi - ylo lz = zhi - zlo a = lx b = np.sqrt(pow(ly, 2) + pow(xy, 2)) c = np.sqrt(pow(lz, 2) + pow(xz, 2) + pow(yz, 2)) alpha = np.arccos((xy * xz + ly * yz) / (b * c)) beta = np.arccos(xz / c) gamma = np.arccos(xy / b) # End testing cell if memmap: if end_cut: data = np.memmap(temp_directory + 'trajectory.{0}'.format(os.getpid()), dtype='complex', mode='w+', shape=(end_cut - initial_cut + 1, number_of_atoms, number_of_dimensions)) else: print( 'Memory mapping requires to define reading range (use read_from/read_to option)' ) exit() position_number = file_map.find('ITEM: ATOMS') file_map.seek(position_number) lammps_labels = file_map.readline() #Initial cut control if initial_cut > counter: continue #Reading coordinates read_coordinates = [] for i in range(number_of_atoms): read_coordinates.append( file_map.readline().split()[0:number_of_dimensions]) try: if memmap: data[counter - initial_cut, :, :] = np.array( read_coordinates, dtype=float) #in angstroms else: data.append(np.array(read_coordinates, dtype=float)) #in angstroms except ValueError: print(("Error reading step {0}".format(counter))) break # print(read_coordinates) #security routine to limit maximum of steps to read and put in memory if limit_number_steps + initial_cut < counter: print( "Warning! maximum number of steps reached! No more steps will be read" ) break if end_cut is not None and end_cut <= counter: break file_map.close() time = np.array(time) * time_step if not memmap: data = np.array(data, dtype=complex) if last_steps is not None: data = data[-last_steps:, :, :] time = time[-last_steps:] # Check position/velocity dump if 'vx vy' in lammps_labels: return dyn.Dynamics(structure=structure, velocity=data, time=time, supercell=supercell, memmap=memmap) if 'x y' in lammps_labels: return dyn.Dynamics(structure=structure, trajectory=data, time=time, supercell=supercell, memmap=memmap) print(( 'LAMMPS parsing error. Data not recognized: {}'.format(lammps_labels))) exit()
def read_from_file_test(): print('Reading structure from test file') #Condicions del test number_of_dimensions = 2 f_coordinates = open('Data Files/test.out', 'r') f_velocity = open('Data Files/test2.out', 'r') f_trajectory = open('Data Files/test3.out', 'r') #Coordinates reading positions = [] while True: row = f_coordinates.readline().split() if not row: break for i in range(len(row)): row[i] = float(row[i]) positions.append(row) atom_type = np.array(positions,dtype=int)[:, 2] positions = np.array(positions)[:,:number_of_dimensions] print('Coordinates reading complete') structure = atomtest.Structure(positions=positions, atomic_numbers=atom_type, cell=[[2,0],[0,1]], masses=[1 for i in range(positions.shape[0])]) #all 1 number_of_atoms = structure.get_number_of_atoms() structure.set_number_of_primitive_atoms(2) print('number of atoms in primitive cell') print((structure.get_number_of_primitive_atoms())) print('number of total atoms in structure (super cell)') print(number_of_atoms) #Velocity reading section velocity = [] while True: row = f_velocity.readline().replace('I','j').replace('*','').replace('^','E').split() if not row: break for i in range(len(row)): row[i] = complex('('+row[i]+')') velocity.append(row) # velocity = velocity[:4000][:] #Limitate the number of points (just for testing) time = np.array([velocity[i][0] for i in range(len(velocity))]).real velocity = np.array([[[velocity[i][j*number_of_dimensions+k+1] for k in range(number_of_dimensions)] for j in range(number_of_atoms)] for i in range (len(velocity))]) print('Velocity reading complete') #Trajectory reading trajectory = [] while True: row = f_trajectory.readline().replace('I','j').replace('*','').replace('^','E').split() if not row: break for i in range(len(row)): row[i] = complex('('+row[i]+')') trajectory.append(row) trajectory = np.array([[[trajectory[i][j*number_of_dimensions+k+1] for k in range(number_of_dimensions)] for j in range(number_of_atoms)] for i in range (len(trajectory))]) print('Trajectory reading complete') return dyn.Dynamics(trajectory=trajectory, #velocity=velocity, time=time, structure=structure)
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): 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, custom_supercell=np.diag(supercell)) q_vector_list_cart = [ np.dot(q_vector, 2*np.pi*np.linalg.inv(structure.get_primitive_cell())) 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)) 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) print((trajectory.shape[0])) 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().T).T), 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().T).T, memmap=memmap)