def check_system(wfklist): if len(wfklist) == 0: error_message = 'The list does not contain any wavefunction data' basic_utils.error_exit(error_message) elif len(wfklist) == 1: return constants.NOERROR else: ref_number_of_atoms = wfklist[0].number_of_atoms ref_number_of_atom_species = wfklist[0].number_of_atom_species ref_atom_species = wfklist[0].atom_species ref_atomic_numbers = wfklist[0].atomic_numbers ref_number_of_spins = wfklist[0].number_of_spins ref_number_of_spinor_components = wfklist[ 0].number_of_spinor_components ref_number_of_components = wfklist[0].number_of_components for iwfk in range(1, len(wfklist)): if (wfklist[iwfk].number_of_atoms != ref_number_of_atoms) or \ (wfklist[iwfk].number_of_atom_species != ref_number_of_atom_species) or \ (wfklist[iwfk].number_of_spins != ref_number_of_spins) or \ (wfklist[iwfk].number_of_spinor_components != ref_number_of_spinor_components) or \ (wfklist[iwfk].number_of_components != ref_number_of_components) or \ not (N.array_equal(wfklist[iwfk].atom_species,ref_atom_species)) or \ not (N.array_equal(wfklist[iwfk].atomic_numbers,ref_atomic_numbers)): error_message = 'The list contains wavefunctions of different systems : the atoms or atom species differ' basic_utils.error_exit(error_message) return constants.NOERROR
def get_type(variable): if N.isscalar(variable): typ = type(variable) if typ is N.bool or typ is bool: return N.bool elif typ is N.int or typ is N.int8 or typ is N.int16 or typ is N.int32 or typ is N.int64: return N.int elif typ is N.uint or typ is N.uint8 or typ is N.uint16 or typ is N.uint32 or typ is N.uint64: return N.uint elif typ is N.float or typ is N.float32 or typ is N.float64: return N.float elif typ is N.complex or typ is N.complex64 or typ is N.complex128: return N.complex elif typ is str or typ is unicode: return str else: error_message = 'Type "%s" not found in get_type(variable)' % type( variable) basic_utils.error_exit(error_message) else: if N.issubdtype(N.bool, variable.dtype): return N.bool elif N.issubdtype(variable.dtype, N.int): return N.int elif N.issubdtype(variable.dtype, N.uint): return N.uint elif N.issubdtype(N.float, variable.dtype): return N.float elif N.issubdtype(variable.dtype, N.complex): return N.complex elif N.issubdtype(variable.dtype, str): return str else: error_message = 'Type "%s" not found in get_type(variable)' % variable.dtype basic_utils.error_exit(error_message)
def get_units(self): for ivar in self.variables_attributes: if 'units' in self.variables_attributes[ivar]: if self.variables_attributes[ivar][ 'units'] != constants.ATOMIC_UNITS: error_message = 'This wavefunction file contains variables in units differing from "%s"' % constants.ATOMIC_UNITS basic_utils.error_exit(error_message) return constants.ATOMIC_UNITS
def get_indices_kinetic_energy(wfklist): if len(wfklist) == 0: error_message = 'The list does not contain any wavefunction data' basic_utils.error_exit(error_message) elif len(wfklist) == 1: return N.array([0], N.int) else: ecuts = N.zeros(len(wfklist), N.float) for iwfk in range(len(wfklist)): ecuts[iwfk] = wfklist[iwfk].kinetic_energy_cutoff indices = N.argsort(ecuts) return indices, ecuts[indices]
def realspace_wfk_from_pw_coeff(self): if 'realspace_wfk_comp' in self.__dict__: return self.realspace_wfk_comp elif 'coefficients_of_wavefunctions' in self.__dict__: self.realspace_wfk_comp = N.fft.ifftn( self.coefficients_of_wavefunctions) return self.realspace_wfk_comp else: error_message = 'Wavefunction does not contain plane wave coefficients,\ the real space wavefunction cannnot be computed' basic_utils.error_exit(error_message)
def check_kinetic_energy_cutoff(wfklist): if len(wfklist) == 0: error_message = 'The list does not contain any wavefunction data' basic_utils.error_exit(error_message) elif len(wfklist) == 1: return constants.CONSTANT else: ref_kinetic_energy_cutoff = wfklist[0].kinetic_energy_cutoff for iwfk in range(1, len(wfklist)): if (wfklist[iwfk].kinetic_energy_cutoff != ref_kinetic_energy_cutoff): return constants.VARIABLE return constants.CONSTANT
def check_red_coord(wfklist): if len(wfklist) == 0: error_message = 'The list does not contain any wavefunction data' basic_utils.error_exit(error_message) elif len(wfklist) == 1: return constants.CONSTANT else: ref_reduced_atom_positions = wfklist[0].reduced_atom_positions for iwfk in range(1, len(wfklist)): if not N.array_equal(ref_reduced_atom_positions, wfklist[iwfk].reduced_atom_positions): return constants.VARIABLE return constants.CONSTANT
def check_primitive_vectors(wfklist): if len(wfklist) == 0: error_message = 'The list does not contain any wavefunction data' basic_utils.error_exit(error_message) elif len(wfklist) == 1: return constants.CONSTANT else: ref_primitive_vectors = wfklist[0].primitive_vectors for iwfk in range(1, len(wfklist)): if not N.array_equal(ref_primitive_vectors, wfklist[iwfk].primitive_vectors): return constants.VARIABLE return constants.CONSTANT
def get_units(wfklist): if len(wfklist) == 0: error_message = 'The list does not contain any wavefunction data' basic_utils.error_exit(error_message) elif len(wfklist) == 1: wfklist[0].get_units() else: ref_units = wfklist[0].get_units() for iwfk in range(1, len(wfklist)): wfk_units = wfklist[iwfk].get_units() if wfk_units != ref_units: error_message = 'The wavefunction file number %s contains variables defined in other units than "atomic units"' basic_utils.error_exit(error_message) return 'atomic units'
def check_kpoints(wfklist): if len(wfklist) == 0: error_message = 'The list does not contain any wavefunction data' basic_utils.error_exit(error_message) elif len(wfklist) == 1: return constants.CONSTANT else: ref_number_of_kpoints = wfklist[0].number_of_kpoints ref_reduced_coordinates_of_kpoints = wfklist[ 0].reduced_coordinates_of_kpoints for iwfk in range(1, len(wfklist)): if (wfklist[iwfk].number_of_kpoints != ref_number_of_kpoints) or \ not N.array_equal(wfklist[iwfk].reduced_coordinates_of_kpoints,ref_reduced_coordinates_of_kpoints): return constants.VARIABLE return constants.CONSTANT
def get_eig_align(self, align_option=constants.ALIGN_OPTION_MEAN_OCCEIG, number_of_bands=1, bands=N.array([1], N.int)): if 'eigenvalues' in self.__dict__: if align_option == constants.ALIGN_OPTION_MEAN_OCCEIG: if 'alignment_mean_occeig' in self.__dict__: return self.alignment_mean_occeig elif 'occupations' in self.__dict__: occtimeseig = N.multiply(self.occupations, self.eigenvalues) wtk = N.repeat(N.repeat(N.reshape(self.kpoint_weights,[1,len(self.kpoint_weights),1]),\ N.shape(occtimeseig)[2],axis=2),N.shape(occtimeseig)[0],axis=0) wtktimesocctimeseig = N.multiply(wtk, occtimeseig) occtimeswtk = N.multiply(wtk, self.occupations) self.alignment_mean_occeig = N.sum( wtktimesocctimeseig) / N.sum(occtimeswtk) return self.alignment_mean_occeig else: error_message = 'The wavefunction data that has been read does not contain occupations, it is not possible to get the alignment' basic_utils.error_exit(error_message) else: error_message = 'Alignment option is invalid' basic_utils.error_exit(error_message) else: error_message = 'The wavefunction data that has been read does not contain eigenvalues, it is not possible to get the alignment' basic_utils.error_exit(error_message)
def put_var_in_wfkdata(wfkdata, varname, varobj, varndim, varshape, vartype): if varndim == 0: if vartype == N.bool: wfkdata.put(varname, N.bool(varobj.getValue())) elif vartype == N.int: wfkdata.put(varname, N.int(varobj.getValue())) elif vartype == N.uint: wfkdata.put(varname, N.uint(varobj.getValue())) elif vartype == N.float: wfkdata.put(varname, N.float(varobj.getValue())) elif vartype == N.complex: wfkdata.put(varname, N.complex(varobj.getValue())) elif vartype == str: wfkdata.put(varname, str(varobj.tostring())) else: error_message = 'Variable "%s" with type "%s" cannot be put in a wavefunction data' % ( varname, vartype) basic_utils.error_exit(error_message) else: if vartype == N.bool or vartype == N.int or vartype == N.uint or vartype == N.float or vartype == N.complex: newarray = N.reshape(N.array(varobj, vartype), varshape) # newarray = N.array(varobj,vartype) wfkdata.put(varname, newarray) elif vartype == str: newstring = str(varobj[:].tostring()) wfkdata.put(varname, newstring) else: error_message = 'Variable "%s" with type "%s" and shape "%s" cannot be put in a wavefunction data' % ( varname, vartype, varshape) basic_utils.error_exit(error_message) if variables_flags[varname] and varname in variables_attributes_flags: for attrname in varobj.ncattrs(): if attrname in variables_attributes_flags[varname]: if variables_attributes_flags[varname][attrname]: attr = varobj.getncattr(attrname) attrtype = get_type(attr) put_attr_in_wfkdata(wfkdata, attrname, attr, attrtype, varname)
def put_attr_in_wfkdata(wfkdata, attrname, attr, attrtype, varname=None): if varname is None: if attrtype is N.bool: wfkdata.put(attrname, N.bool(attr)) elif attrtype is N.int: wfkdata.put(attrname, N.int(attr)) elif attrtype is N.uint: wfkdata.put(attrname, N.uint(attr)) elif attrtype is N.float: wfkdata.put(attrname, N.float(attr)) elif attrtype is N.complex: wfkdata.put(attrname, N.complex(attr)) elif attrtype is str: wfkdata.put(attrname, str(attr)) else: error_message = 'Global attribute "%s" of type "%s" cannot be put in a wavefunction data' % ( attrname, attrtype) basic_utils.error_exit(error_message) else: if attrtype is N.bool: newattr = N.bool(attr) elif attrtype is N.int: newattr = N.int(attr) elif attrtype is N.uint: newattr = N.uint(attr) elif attrtype is N.float: newattr = N.float(attr) elif attrtype is N.complex: newattr = N.complex(attr) elif attrtype is str: newattr = str(attr) else: error_message = 'Attribute "%s" of variable "%s" of type "%s" cannot be put in a wavefunction data' % ( attrname, varname, attrtype) basic_utils.error_exit(error_message) wfkdata.set_attribute(varname, attrname, newattr)
def check_file_etsf_wavefunction(filename): if not (os.path.isfile(filename)): error_message = 'File "%s" does not exists' % filename basic_utils.error_exit(error_message) else: try: test = netCDF4.Dataset(filename) attributes_dict = test.__dict__ if not ('file_format' in attributes_dict): error_message = 'File "%s" does not contain the "file_format" attribute' % filename test.close() basic_utils.error_exit(error_message) elif not (attributes_dict['file_format'] == 'ETSF Nanoquanta'): error_message = 'Attribute "file_format" of file "%s" is %s \ \nwhile it should be "ETSF Nanoquanta"' % ( filename, attributes_dict['file_format']) test.close() basic_utils.error_exit(error_message) except RuntimeError: error_message = 'File "%s" cannot be opened as a netCDF file' % filename basic_utils.error_exit(error_message)
#List containing all the wavefunction containers wavefunctions_list = list() constants = etsfIO.constants # Beginning of script print '+------------------------------------+' print '| Reading of ETSF wavefunction files |' print '+------------------------------------+\n' #Get filenames and check they exist and that they are in ETSF file format ctrl.number_of_etsf_wfk_files_to_read = basic_utils.raw_input_int('\nEnter the number of ETSF wavefunction files to read : \n') print ctrl.number_of_etsf_wfk_files_to_read if ctrl.number_of_etsf_wfk_files_to_read <= 0: basic_utils.error_exit('The number of ETSF wavefunction files to read is lower than or equal to 0') else: for iread in range(ctrl.number_of_etsf_wfk_files_to_read): current_file = raw_input('\nEnter the name of file number %s : ' %(iread+1)) print current_file etsfIO.check_file_etsf_wavefunction(current_file) if (current_file in ctrl.etsf_wfk_files_to_read): error_message = 'File "%s" is already in the list of files to be read' %current_file basic_utils.error_exit(error_message) else: ctrl.etsf_wfk_files_to_read.append(current_file) #Get reading/checking choice print '\nReading option' print ' "1" => Read geometry, k-points and basic input/output variables' print ' "2" => "1" + Read eigenvalues and occupation numbers'
constants.ALIGN_OPTION_MEAN_OCCEIG = N.int(1) constants.HA_TO_EV = 27.211 #Flags for the attributes (0 : do not read, 1 : read, 2 : read and check that it is the same for all files) attributes_flags = dict() dimensions_flags = dict() variables_flags = dict() variables_attributes_flags = dict() # Get all the flags in the dictionnaries and set them to DO_NOT_READ flagsfile = str(find_module('etsfIO')[1][:-9] + 'flags.list') try: flagsreader = open(flagsfile, 'r') except IOError: error_message = 'The flags.list file has not been found' basic_utils.error_exit(error_message) flagslines = flagsreader.readlines() basic_utils.clean(flagslines) flagsreader.close() attrread = False dimread = False varread = False for iline in range(len(flagslines)): line = flagslines[iline] if len(line.split()) == 0: continue elif line.split()[0] == 'ATTRBEGIN': attrread = True elif line.split()[0] == 'ATTREND': attrread = False elif line.split()[0] == 'DIMBEGIN': dimread = True elif line.split()[0] == 'DIMEND': dimread = False elif line.split()[0] == 'VARBEGIN': varread = True elif line.split()[0] == 'VAREND': varread = False