def main(): """Command line executable""" if not argv[1].endswith('.cube'): raise ValueError(argv[1]) rho_e, atoms = read_cube_data(open(argv[1], 'r')) if not argv[2].endswith('.cube'): raise ValueError(argv[2]) rho_h, atoms = read_cube_data(open(argv[2], 'r')) drho = rho_h + rho_e name = argv[1].split('.cube')[0]+'drho.cube' write_cube(open(name, 'w'), atoms, data=drho)
def ParsePpPlot(pp_calc_out): import io outfile = pp_calc_out.open('aiida.out') outfile.seek(0) while True: l = outfile.readline() if 'Reading data from file aiida.filplot' in l: break if l == '': raise RuntimeError("Invalid output file") f = io.StringIO() try: while True: l = outfile.readline() if 'Output format:' in l: break f.write(l) if l == '': raise RuntimeError("Invalid output file") except: raise RuntimeError("Unexpected error") finally: f.seek(0) from ase.io.cube import read_cube_data data, atoms = read_cube_data(f) return orm.Float(data[0, 0, 0])
def aa2ua_cube(infile_pdb, infile_top, infile_cube, outfile_cube, implicitHbondingPartners): ase_struct = ase.io.read(infile_pdb) pmd_struct = pmd.load_file(infile_pdb) with warnings.catch_warnings(): warnings.simplefilter('ignore') # throws some warnings on angle types, does not matter for bonding info pmd_top = gromacs.GromacsTopologyFile(infile_top, parametrize=False) pmd_top.strip(':SOL,CL') # strip water and electrolyte from system pmd_top.box = pmd_struct.box # Needed because .prmtop contains box info pmd_top.positions = pmd_struct.positions new_ase_struct, new_pmd_struct, names, residues = insertHbyList( ase_struct, pmd_top, implicitHbondingPartners, 1.0) surplus_atoms = len(new_ase_struct) - len(ase_struct) # print("{} atoms are going to be truncated from file " # "{} ...".format(surplus_atoms,infile_cube)) # hdf5 = h5py.File(infile_h5,'r') cube_data, cube_atoms = read_cube_data(infile_cube) # print("Truncated atoms: {}".format(cube_atoms[len(ase_struct):])) ase.io.write(outfile_cube, cube_atoms[0:len(ase_struct)], data=cube_data)
def cube2histo(cube_filename, hist_obj): """ Convert cube file to histogram, .cube file maybe generated using Prof. Cory Simon's code in Julia. :type cube_filename: str :param cube_filename: path to the cube file generated by Prof. Cory Simon's code :type hist_obj: instance of the histo class :param hist_obj: contains all the info regarding the energy histogram of sites for a given material :raises: One has to initialize the histogram object before calling this function. pandas could throw up if the file is not formatted correctly. Refer documentation page :rtype: instance of the histo class. """ import pandas as pd import numpy as np from ase.io import read, write from ase.io.cube import read_cube_data data, atoms = read_cube_data(cube_filename) e_vals = np.reshape(data, (data.size, 1), order='C') e_vals = e_vals/grid_obj.Temperature # Reduced units for energy e_vals = e_vals[~np.isnan(e_vals)] bins1 = np.linspace(min(e_vals)-0.5, hist_obj.E_max/grid_obj.Temperature , hist_obj.nbins+1) hist_obj.RhoE, binedges1 = np.histogram(e_vals, bins=bins1, density=hist_obj.normed_flag) bincenters = 0.5 * (binedges1[1:] + binedges1[:-1]) # Bincenters hist_obj.E = bincenters return hist_obj
def __init__(self, atoms, data): """ Paramaters: =========== atoms: ase Atoms instance data: grid data (in the cell of atoms, 3-dimensional array) or cube file containing the data """ if type(data) == type(''): self.data, aux = read_cube_data(data) else: self.data = data self.atoms = atoms self.cell = atoms.get_cell() assert not self.cell[2, :2].any() and not self.cell[:2, 2].any() self.ldos = None self.heights = None self.axes = vec([self.cell[i, i] for i in range(3)]) self.dg = None self.ng = self.data.shape self.grids = [ np.linspace(0, self.axes[i], self.ng[i]) for i in range(3) ] self.ztol = 0.001 self.dataip = TrilinearInterpolation(self.data, grids=self.grids)
def plot(name): assert name in ("ZnVO", "CoVO") data, atoms = read_cube_data(cube_file(name)) lattice = atoms.cell na, nb, nc = data.shape layer = data[:, :, nc // 2].T print(layer.max(), layer.min()) x_ = numpy.linspace(0, 1, na) y_ = numpy.linspace(0, 1, nb) xx_s, yy_s = numpy.meshgrid(x_, y_) xx_fine, yy_fine = numpy.meshgrid(numpy.linspace(0, 1, 256), numpy.linspace(0, 1, 256)) xy_flat = numpy.vstack([xx_s.flat, yy_s.flat]).T print(xy_flat.shape) c_fine = griddata(xy_flat, layer.flat, (xx_fine, yy_fine), method="cubic") zz_fine = numpy.ones_like(yy_fine) * 0.5 xx, yy, zz = numpy.tensordot(lattice.T, numpy.array([xx_fine, yy_fine, zz_fine]), axes=1) # print(xx, yy) fig = plt.figure(figsize=(3.5, 3.0)) ax = fig.add_subplot(111) # layer = layer.T ax.pcolor( xx, yy, c_fine, cmap="rainbow_r", vmin=-10, # antialiased=True, # interpolate="bicubic", rasterized=True) sc = make_supercell(atoms, numpy.diag([2, 2, 1])) idx = [p.index for p in sc if (p.z > atoms.cell[-1, -1]*0.43) \ and (p.z < atoms.cell[-1, -1] * 0.58)] # and (p.x < atoms.cell[0, 0] * 1.1) \ # and (p.y < atoms.cell[1, 1] * 1.2)] new_atoms = sc[idx] rot = {"ZnVO": (-0.4, 1.30, 0.02), "CoVO": (0.00, 1.89, 0.00)} off = {"ZnVO": (-1.40, -1.15), "CoVO": (-0.9, -1.2)} plot_atoms(new_atoms, ax=ax, rotation="{0}x,{1}y,{2}z".format(*rot[name]), radii=0.6, offset=off[name]) # show_unit_cell=True) # offset=(-1.1, -0.8), # show_unit_cell=True) # plt.show() ax.set_aspect("equal") ax.set_xlim(xx.min(), xx.max()) ax.set_ylim(yy.min(), yy.max()) ax.set_axis_off() # ax.set_axis_off() fig.savefig(join(img_path, "{}_half.svg".format(name)))
def main(args=None): parser = optparse.OptionParser(usage='%prog [options] filename', description=description) add = parser.add_option add('-n', '--band-index', type=int, metavar='INDEX', help='Band index counting from zero.') add('-s', '--spin-index', type=int, metavar='SPIN', help='Spin index: zero or one.') add('-c', '--contours', default='4', help='Use "-c 3" for 3 contours or "-c -0.5,0.5" for specific ' + 'values. Default is four contours.') add('-r', '--repeat', help='Example: "-r 2,2,2".') add('-C', '--calculator-name', metavar='NAME', help='Name of calculator.') opts, args = parser.parse_args(args) if len(args) != 1: parser.error('Incorrect number of arguments') arg = args[0] if arg.endswith('.cube'): data, atoms = read_cube_data(arg) else: calc = get_calculator(opts.calculator_name)(arg, txt=None) atoms = calc.get_atoms() if opts.band_index is None: data = calc.get_pseudo_density(opts.spin_index) else: data = calc.get_pseudo_wave_function(opts.band_index, opts.spin_index or 0) if data.dtype == complex: data = abs(data) mn = data.min() mx = data.max() print('Min: %16.6f' % mn) print('Max: %16.6f' % mx) if opts.contours.isdigit(): n = int(opts.contours) d = (mx - mn) / n contours = np.linspace(mn + d / 2, mx - d / 2, n).tolist() else: contours = [float(x) for x in opts.contours.split(',')] if len(contours) == 1: print('1 contour:', contours[0]) else: print('%d contours: %.6f, ..., %.6f' % (len(contours), contours[0], contours[-1])) if opts.repeat: repeat = [int(r) for r in opts.repeat.split(',')] data = np.tile(data, repeat) atoms *= repeat plot(atoms, data, contours)
def from_cube(cls, filename): """ Returns System object with box data in system.info['box'], read from cube file :param filename str: name of the file :return: instance of the :class:system """ data, atoms = read_cube_data(filename) atoms = cls(atoms) atoms.info['box'] = data[None, :, :, :] return atoms
def get_com(f_cube, vec): # Calculation of COM # COM ... center of mass (COM), center of gravity (COG), centroid # Determine the origin of the cube file #f = open(f_cube,'r') #ll = f.readlines() #f.close() #vec_tmp = ll[2].split() #vec_a = -1*float(vec_tmp[1])*Bohr #vec_b = -1*float(vec_tmp[2])*Bohr #vec_c = -1*float(vec_tmp[3])*Bohr #vec = [vec_a,vec_b,vec_c] # cube structure in [Ang] orb = cube.read(f_cube) # cuba data in [Bohr**3] data = cube.read_cube_data(f_cube) # cell of cube in [Ang] cell = orb.get_cell() shape = np.array(data[0]).shape spacing_vec = cell / shape[0] / Bohr values = data[0] idx = 0 unit = 1 / Bohr #**3 X = [] Y = [] Z = [] V = [] for i in range(0, shape[0]): for j in range(0, shape[0]): for k in range(0, shape[0]): idx += 1 x, y, z = i * float(spacing_vec[0, 0]), j * float( spacing_vec[1, 1]), k * float(spacing_vec[2, 2]) # approximate fermi hole h = 2*abs(phi_i)**2 # Electron Pairing and Chemical Bonds: # see Bonding in Hypervalent Molecules from Analysis of Fermi Holes Eq(11) x, y, z, v = x / unit, y / unit, z / unit, 2. * np.abs( values[i, j, k] )**2. #2*np.abs(values[i,j,k])**2 # fermi hole approximation values[i,j,k] X.append(x) Y.append(y) Z.append(z) V.append(v) X = np.array(X) Y = np.array(Y) Z = np.array(Z) V = np.array(V) x = sum(X * V) y = sum(Y * V) z = sum(Z * V) # Shifting to the origin of the cube file. com = (np.array([x / sum(V), y / sum(V), z / sum(V)]) - vec).tolist() return com
def load(self, iorbs, sdm=None): """Overload WavefunctionHandler.load() due to ASE MPI problem.""" from ase.io.cube import read_cube_data wfc = self.wfc counter = 0 for iorb in range(self.wfc.norbs): # Iterate over all orbitals, because ASE use global communicator fname = wfc.iorb_fname_map[iorb] if self.density: rho = read_cube_data(fname)[0] psird = np.sign(rho) * np.sqrt(np.abs(rho)) psir = self.dft.interp(psird, wfc.ft.n1, wfc.ft.n2, wfc.ft.n3) else: psir = read_cube_data(fname)[0] self.wfc.set_psir(iorb, psir) counter += 1 if counter >= wfc.norbs // 10: if mpiroot: print("........") counter = 0
def get_com(f_cube): '''' Calculation of COM ''' orb = cube.read(f_cube) # cuba data in [Bohr**3] data = cube.read_cube_data(f_cube) # cell of cube in [Ang] cell = orb.get_cell() shape = numpy.array(data[0]).shape spacing_vec = cell / shape[0] / Bohr values = data[0] idx = 0 unit = 1 / Bohr #**3 X = [] Y = [] Z = [] V = [] fv = open(f_cube, 'r') ll = fv.readlines() fv.close() vec_tmp = ll[2].split() vec_a = -1 * float(vec_tmp[1]) * Bohr vec_b = -1 * float(vec_tmp[2]) * Bohr vec_c = -1 * float(vec_tmp[3]) * Bohr vec = [vec_a, vec_b, vec_c] for i in range(0, shape[0]): for j in range(0, shape[0]): for k in range(0, shape[0]): idx += 1 x, y, z = i * float(spacing_vec[0, 0]), j * float( spacing_vec[1, 1]), k * float(spacing_vec[2, 2]) # approximate fermi hole h = 2*abs(phi_i)**2 # see Bonding in Hypervalent Molecules from Analysis of Fermi Holes Eq(11) x, y, z, v = x / unit, y / unit, z / unit, 2. * numpy.abs( values[i, j, k])**2. X.append(x) Y.append(y) Z.append(z) V.append(v) X = numpy.array(X) Y = numpy.array(Y) Z = numpy.array(Z) V = numpy.array(V) x = sum(X * V) y = sum(Y * V) z = sum(Z * V) # Shifting to the origin of the cube file. com = (numpy.array([x / sum(V), y / sum(V), z / sum(V)]) - vec).tolist() return com
def get_cube(self, prefix = None, roll = [0, 0, 0], repeat = [1, 1, 1], box = True, output = None): """ """ if prefix: self.prefix = prefix file = '{0}.cube'.format(self.prefix) data, atoms = read_cube_data(file) # repeat if repeat: data = np.tile(data, repeat) atoms *= repeat t = np.sum(roll*atoms.cell/data.shape, axis = 0) atoms.translate(t) data = np.roll(data, roll, axis=[0, 1, 2]) self.cube = [atoms, data]
def import_blase( inputfile, model_type='0', camera='True', light='True', world='False', show_unit_cell=False, ball_type='Ball-and-stick', scale=1.0, bond_cutoff=1.0, search_pbc_atoms=False, search_molecule=False, make_real=False, name=None, ): # print('=' * 30) print('Import structure') print('=' * 30) print(model_type) # kwargs = { 'show_unit_cell': show_unit_cell, 'scale': scale, 'bond_cutoff': bond_cutoff, 'world': world, 'camera': camera, 'light': light, 'search_pbc_atoms': search_pbc_atoms, 'search_molecule': search_molecule, 'make_real': make_real, } if isinstance(inputfile, str): if inputfile.split('.')[-1] == 'cube': images, data = read_cube_data(inputfile) else: images = read(inputfile) else: images = inputfile print(images) bobj = Batoms(images, name=name, model_type=model_type) #, **kwargs) bobj.draw()
def get_work_function(self, ax=None, inpfile='potential.cube', output=None, shift=False): import matplotlib.pyplot as plt from ase.io.cube import read_cube_data from ase.units import create_units if ax is None: import matplotlib.pyplot as plt ax = plt.gca() units = create_units('2006') # filename = os.path.join(self.directory, inpfile) data, atoms = read_cube_data(filename) data = data * units['Ry'] ef = self.get_fermi_level() # x, y, z, lp = calc.get_local_potential() nx, ny, nz = data.shape axy = np.array([np.average(data[:, :, z]) for z in range(nz)]) # setup the x-axis in realspace uc = atoms.get_cell() xaxis = np.linspace(0, uc[2][2], nz) if shift: ax.plot(xaxis, axy - ef) ef = 0 else: ax.plot(xaxis, axy) ax.plot([min(xaxis), max(xaxis)], [ef, ef], 'k:') ax.set_xlabel('Position along z-axis ($\AA$)') ax.set_ylabel('Potential (eV)') if output: plt.savefig('%s' % output) # plt.show() atoms = self.results['atoms'] pos = max(atoms.positions[:, 2] + 3) ind = (xaxis > pos) & (xaxis < pos + 3) wf = np.average(axy[ind]) - ef print('min: %s, max: %s' % (pos, pos + 3)) print('The workfunction is {0:1.2f} eV'.format(wf))
def scan(self): from ...common.misc import parse_one_value from ase.io.cube import read_cube_data ufnames = sorted(glob("*up*.cube")) dfnames = sorted(glob("*down*.cube")) nuorbs = len(ufnames) ndorbs = len(dfnames) iorb_fname_map = ufnames + dfnames idxsbmap = map( lambda fname: ("up" if "up" in fname else "down", parse_one_value(int, fname, -1)), iorb_fname_map ) # Define cell and Fourier transform grid from first cube file fname0 = iorb_fname_map[0] psir, ase_cell = read_cube_data(fname0) cell = Cell(ase_cell) if self.density: self.dft = FourierTransform(*psir.shape) if self.fftgrid == "wave": ft = FourierTransform(*map(lambda x: x // 2, psir.shape)) if mpiroot: print("Charge density grid {} will be interpolated to wavefunction grid {}.\n".format( map(lambda x: x // 2, psir.shape), psir.shape )) else: if mpiroot: print("Charge density grid will be used.\n") ft = FourierTransform(*psir.shape) else: ft = FourierTransform(*psir.shape) self.wfc = Wavefunction(cell=cell, ft=ft, nuorbs=nuorbs, ndorbs=ndorbs, iorb_sb_map=idxsbmap, iorb_fname_map=iorb_fname_map) self.info()
def get_rho_r(self): """ Implement abstract fetch_rhor method for Qbox. Send plot charge density commands to Qbox, then parse charge density. """ vspin = self.sample.vspin n1, n2, n3 = self.sample.n1, self.sample.n2, self.sample.n3 self.sample.rho_r = np.zeros([vspin, n1, n2, n3]) for ispin in range(vspin): # Qbox generates charge density self.run_cmd(cmd="plot -density {} {}".format( "-spin {}".format(ispin + 1) if vspin == 2 else "", self.rhor_file)) rhor_raw = read_cube_data(self.rhor_file)[0] assert rhor_raw.shape == (n1, n2, n3) # rhor1 = np.roll(rhor_raw, n1 // 2, axis=0) rhor2 = np.roll(rhor1, n2 // 2, axis=1) rhor3 = np.roll(rhor2, n3 // 2, axis=2) self.sample.rho_r[ispin] = rhor3
def aa2ua_cube(infile_pdb, infile_top, infile_cube, outfile_cube, implicitHbondingPartners={ 'CD4': 1, 'CD3': 1, 'CA2': 2, 'CA3': 2, 'CA4': 2, 'CB2': 2, 'CB3': 2 }): #infile_pdb = args.infile_pdb #infile_top = args.infile_top #infile_cube = args.infile_cube #outfile_cube = args.outfile_cube ase_struct = ase.io.read(infile_pdb) pmd_struct = pmd.load_file(infile_pdb) pmd_top = gromacs.GromacsTopologyFile(infile_top, parametrize=False) # throws some warnings on angle types, does not matter for bonding info pmd_top.strip(':SOL,CL') # strip water and electrolyte from system pmd_top.box = pmd_struct.box # Needed because .prmtop contains box info pmd_top.positions = pmd_struct.positions new_ase_struct, new_pmd_struct, names, residues = insertHbyList( ase_struct, pmd_top, implicitHbondingPartners, 1.0) surplus_atoms = len(new_ase_struct) - len(ase_struct) print("{} atoms are going to be truncated from file" "{}...".format(surplus_atoms, infile_cube)) # hdf5 = h5py.File(infile_h5,'r') cube_data, cube_atoms = read_cube_data(infile_cube) ase.io.write(outfile_cube, cube_atoms[0:len(ase_struct)], data=cube_data)
def __init__(self, atoms, data): """ Paramaters: =========== atoms: ase Atoms instance data: grid data (in the cell of atoms, 3-dimensional array) or cube file containing the data """ if type(data)==type(''): self.data, aux=read_cube_data(data) else: self.data=data self.atoms=atoms self.cell = atoms.get_cell() assert not self.cell[2, :2].any() and not self.cell[:2, 2].any() self.ldos = None self.heights = None self.axes=vec([self.cell[i,i] for i in range(3)]) self.dg=None self.ng=self.data.shape self.grids=[np.linspace(0,self.axes[i],self.ng[i]) for i in range(3)] self.ztol=0.001 self.dataip=TrilinearInterpolation(self.data,grids=self.grids)
import numpy as np from ase.io import read from ase.io.cube import read_cube_data from timeit import default_timer as timer start = timer() h**o = read_cube_data('h**o.cube')[0] lumo = read_cube_data('lumo.cube')[0] epsilon = 3.28 with open('h**o.cube','r') as f: f.readline() f.readline() f.readline() line1 = f.readline().split() line2 = f.readline().split() line3 = f.readline().split() n1,n2,n3 = int(line1[0]),int(line2[0]),int(line3[0]) n = n1*n2*n3 x1,y1,z1 = [float(s) for s in line1[1:]] x2,y2,z2 = [float(s) for s in line2[1:]] x3,y3,z3 = [float(s) for s in line3[1:]] vec = np.array([[x1,y1,z1],[x2,y2,z2],[x3,y3,z3]]) dx = x1+x2+x3 dy = y1+y2+y3 dz = z1+z2+z3 da = np.linalg.norm(vec[0]) db = np.linalg.norm(vec[1])
def get_pseudo_wave_function(self, band=0, kpt=0, spin=None): """Return pseudo-wave-function array. The method is limited to the gamma point, and is implemented as a wrapper to denchar (a tool shipped with siesta); denchar must be available in the command path. When retrieving a p_w_f from a non-spin-polarized calculation, spin must be None (default), and for spin-polarized calculations, spin must be set to either 0 (up) or 1 (down). As long as the necessary files are present and named correctly, old p_w_fs can be read as long as the calculator label is set. E.g. >>> c = Siesta(label='name_of_old_calculation') >>> pwf = c.get_pseudo_wave_function() The broadcast and pad options are not implemented. """ # Not implemented: kpt=0, broadcast=True, pad=True # kpoint must be Gamma assert kpt == 0, ( "siesta.get_pseudo_wave_function is unfortunately limited " "to the gamma point only. kpt must be 0." ) # In denchar, band numbering starts from 1 assert type(band) is int and band >= 0 band = band + 1 if spin is None: spin_name = "" elif spin == 0: spin_name = ".UP" elif spin == 1: spin_name = ".DOWN" label = self.label # If <label>.WF<band>.cube already exist and is newer than <label>.fdf, # just return it fn_wf = label + (".WF%i%s.cube" % (band, spin_name)) fn_fdf = label + ".fdf" if isfile(fn_wf) and isfile(fn_fdf) and (getmtime(fn_wf) > getmtime(fn_fdf)): x, _ = read_cube_data(fn_wf) return x if not isfile(fn_fdf): raise RuntimeError("Could not find the fdf-file. It is required as " "part of the input for denchar.") fdf_mtime = getmtime(fn_fdf) for suf in [".WFS", ".PLD", ".DM", ".DIM"]: if not isfile(label + suf): raise RuntimeError( 'Could not find file "%s%s" which is required ' "when extracting wave functions " '(make sure the fdf options "WriteDenchar" is ' 'True, and WaveFuncKpoints is [0.0 0.0 0.0]")' % (label, suf) ) if not getmtime(label + suf) > fdf_mtime: # This should be handled in a better way, e.g. by implementing # a "calculation_required() and calculate()" raise RuntimeError("The calculation is not up to date.") # Simply read the old fdf-file and pick some meta info from there. # However, strictly it's not always neccesary fdf = read_fdf(fn_fdf) if "latticeconstant" in fdf: const = float(fdf["latticeconstant"][0]) unit = fdf["latticeconstant"][1] else: const = 1.0 unit = "Ang" if "latticevectors" in fdf: cell = np.array(fdf["latticevectors"], dtype="d") else: raise RuntimeError("Failed to find the lattice vectors in the fdf-file.") if "spinpolarized" in fdf and fdf["spinpolarized"][0].lower() in ["yes", "true", ".true.", "T", ""]: if spin is None: raise RuntimeError("The calculation was spin polarized, pick either " "spin=0 or 1.") else: if not spin is None: raise RuntimeError("The calculation was not spin polarized, " "spin argument must be None.") denc_fdf = open(fn_fdf).readlines() denc_fdf.append("Denchar.TypeOfRun 3D\n") denc_fdf.append("Denchar.PlotWaveFunctions T\n") for dim, dir in zip(cell.transpose(), ["X", "Y", "Z"]): # Naive square box limits to denchar denc_fdf.append("Denchar.Min%s %f %s\n" % (dir, const * dim.min(), unit)) denc_fdf.append("Denchar.Max%s %f %s\n" % (dir, const * dim.max(), unit)) # denchar rewinds stdin and fails if stdin is a pipe denc_fdf_file = open(label + ".denchar.fdf", "w") denc_fdf_file.write("".join(denc_fdf)) denc_fdf_file.close() try: from subprocess import Popen, PIPE p = Popen( "denchar", shell=True, stdin=open(label + ".denchar.fdf"), stdout=PIPE, stderr=PIPE, close_fds=True ) exitcode = p.wait() except ImportError: raise RuntimeError("get_pseudo_wave_function implemented only with subprocess.") if exitcode == 0: if not isfile(fn_wf): raise RuntimeError("Could not find the requested file (%s)" % fn_wf) x, _ = read_cube_data(fn_wf) return x elif exitcode == 127: raise RuntimeError("No denchar executable found. Make sure it is in the path.") else: import sys print >> sys.stderr, "".join(p.stderr.readlines()) raise RuntimeError("Execution of denchar failed!")
# Creates: h2o-bader.png import os import pickle import numpy as np import matplotlib.pyplot as plt from ase.io.cube import read_cube_data if os.path.isfile('h2o.pckl'): with open('h2o.pckl', 'rb') as fd: dens, bader, atoms = pickle.load(fd) else: dens, atoms = read_cube_data('density.cube') bader, atoms = read_cube_data('AtIndex.cube') x = len(dens) // 2 dens = dens[x] bader = bader[x] with open('h2o.pckl', 'wb') as fd: pickle.dump((dens, bader, atoms), fd) x0, y0, z0 = atoms.positions[0] y = np.linspace(0, atoms.cell[1, 1], len(dens), endpoint=False) - y0 z = np.linspace(0, atoms.cell[2, 2], len(dens[0]), endpoint=False) - z0 print(y.shape, z.shape, dens.shape, bader.shape) print(atoms.positions) print(dens.min(), dens.mean(), dens.max()) plt.figure(figsize=(5, 5)) plt.contourf(z, y, dens, np.linspace(0.01, 0.9, 15)) plt.contour(z, y, bader, [1.5], colors='k') plt.axis(xmin=-2, xmax=2, ymin=-2, ymax=2) plt.savefig('h2o-bader.png')
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('inputfile', type=str, help="the input json file, includes coordinates of a \ set of points, threshold and a list of pairs of points " ) parser.add_argument('--display', action='store_true', default=True, help="render") parser.add_argument('--run_render', action='store_false', default=False, help="render") parser.add_argument('--model', '-m', type=int, default=0, help="structure model") parser.add_argument('--outfile', '-o', type=str, default='output', help="write output to specified file ") parser.add_argument('--camera', action='store_false', default=False, help="camera") parser.add_argument('--light', action='store_false', default=False, help="light") parser.add_argument('--search_pbc_atoms', action='store_false', default=False, help="search_pbc_atoms") parser.add_argument('--search_molecule', action='store_false', default=False, help="search_molecule") args = parser.parse_args() # if args.inputfile.split('.')[-1] == 'cube': atoms, data = read_cube_data(inputfile) else: atoms = read(args.inputfile) if atoms.pbc.any(): kwargs['show_unit_cell'] = True kwargs['display'] = args.display kwargs['camera'] = args.camera kwargs['light'] = args.light kwargs['outfile'] = args.outfile + '.png' kwargs['run_render'] = args.run_render kwargs['search_pbc_atoms'] = args.search_pbc_atoms kwargs['search_molecule'] = args.search_molecule if args.model == 0: kwargs['radii'] = 0.6 kwargs['bond_cutoff'] = 1.0 elif args.model == 1: kwargs['bond_cutoff'] = None elif args.model == 2: kwargs['radii'] = 0.6 kwargs['bond_cutoff'] = 1.0 kwargs['polyhedral'] = {} print('=' * 30) print('Import structure') print('=' * 30) write_blender(atoms, **kwargs) print('\n Finished!')
from ase.data.colors import cpk_colors from ase.calculators.siesta.import_functions import xv_to_atoms from matplotlib import cm import matplotlib.pyplot as plt import numpy as np from matplotlib import animation selectedTimeSteps = range(5,1000,20) vmax = 0.01 vmin = -vmax slide = 27 dataAndAtoms = [read_cube_data(str(i)+'/siesta.DRHO.cube') for i in selectedTimeSteps] fig, axs = plt.subplots(1,2,figsize=(12,6)) pho0,atoms = dataAndAtoms[0] #atoms = xv_to_atoms('siesta.XV') X = np.arange(pho0.shape[1])*atoms.cell[1,1]/pho0.shape[1] Y = np.arange(pho0.shape[2])*atoms.cell[2,2]/pho0.shape[2] x, y = np.meshgrid(X, Y) import pyramids.io.result as dP time, light = dP.getEField() step = len(time)/len(dataAndAtoms) #print light.shape line, = axs[1].plot(time,light[:,2])
def __init__(self, ase_cell: Atoms, vspin: int, n1: int, n2: int, n3: int, atomic_density_files: dict = None): # define cell self.R = ase_cell.get_cell() * angstrom_to_bohr self.G = 2 * np.pi * np.linalg.inv(self.R).T assert np.all( np.isclose(np.dot(self.R, self.G.T), 2 * np.pi * np.eye(3))) self.omega = np.linalg.det(self.R) self.atoms = list( Atom(sample=self, ase_atom=atom) for atom in ase_cell) self.natoms = len(self.atoms) self.species = sorted(set([atom.symbol for atom in self.atoms])) self.nspecies = len(self.species) # define fragments and constraints list self.fragments = [] self.constraints = [] # define vspin and FFT grid self.vspin = vspin self.n1, self.n2, self.n3 = n1, n2, n3 self.n = n1 * n2 * n3 # define energies, forces and wavefunction self.Ed = None self.Ec = None self.W = None self.Fd = None self.Fc = None self.Fw = None self.wfc = None # define charge density and promolecule charge densities self.rho_r = None self.rhopro_tot_r = None self.rhoatom_g = {} self.rhoatom_rd = {} # compute the norm of all G vectors on the [n1, n2, n3] grid G1, G2, G3 = self.G # all Gx,Gy,Gz values G1s = np.outer(G1, fftfreq(n1, d=1. / n1)) G2s = np.outer(G2, fftfreq(n2, d=1. / n2)) G3s = np.outer(G3, fftfreq(n3, d=1. / n3)) # make grid from G1s, G2s,G3s using Python built-in broadcasting self.Gx_g = (G1s[0, :, np.newaxis, np.newaxis] + G2s[0, np.newaxis, :, np.newaxis] + G3s[0, np.newaxis, np.newaxis, :]) self.Gy_g = (G1s[1, :, np.newaxis, np.newaxis] + G2s[1, np.newaxis, :, np.newaxis] + G3s[1, np.newaxis, np.newaxis, :]) self.Gz_g = (G1s[2, :, np.newaxis, np.newaxis] + G2s[2, np.newaxis, :, np.newaxis] + G3s[2, np.newaxis, np.newaxis, :]) self.G2_g = self.Gx_g**2 + self.Gy_g**2 + self.Gz_g**2 # compute atomic density for all species if atomic_density_files is not None: # read atomic density from file for s in self.species: rho_r, ase_cell = read_cube_data(atomic_density_files[s]) omega = ase_cell.get_volume() * angstrom_to_bohr**3 assert rho_r.shape == (n1, n2, n3) # in order to have rhoatom_g on commensurate grid # as generated by fftfreq, roll around rho_r1 = np.roll(rho_r, n1 // 2, axis=0) rho_r2 = np.roll(rho_r1, n2 // 2, axis=1) rho_r3 = np.roll(rho_r2, n3 // 2, axis=2) # self.rhoatom_g[s] = omega / self.n * fftn(rho_r3) self.rhoatom_g[s] = omega / self.n * fftn(rho_r3) else: # calculate atomic density from pre-computed spherically-averaged atomic density # located in atomic/rho # define mapping from all G vectors to G vectors with unique norm # i.e., repeated |G| are excluded # tocheck: is 5A cutoff for atomic densities sufficient # tocheck: is 0.02 integration step sufficient # rd_grid size set to [251,]; 5 Ang cutoff with 0.02 Ang step # Gmapping, rho_g: n1 x n2 x n3 # sinrG: rd_grid x unique |G| G2_d = np.sort(np.array(list(set(self.G2_g.flatten())))) self.Gmapping = np.searchsorted(G2_d, self.G2_g) self.G_d = np.sqrt(G2_d) self.sinrG = np.sin(np.outer(rd_grid, self.G_d)) # rho(G) = 4 pi int( rho(r) r sinGr / G ) # rho(G=0) = 4 pi int( rho(r) r^2 ) = nel / omega for s in self.species: rho_rd = np.loadtxt("{}/{}.spavr".format(rho_path, s), dtype=float)[:, 1] rho_rd[rho_rd < 0] = 0 with warnings.catch_warnings(): warnings.simplefilter("ignore") rho_d = 4 * np.pi * drd * np.einsum( "r,rg->g", rho_rd * rd_grid, self.sinrG / self.G_d) rho_g = rho_d[self.Gmapping] rho_g[0, 0, 0] = 4 * np.pi * drd * np.sum(rho_rd * rd_grid**2) fac = SG15PP[s]["nel"] / rho_g[0, 0, 0] rho_rd *= fac rho_g *= fac # normalize charge density self.rhoatom_g[s] = rho_g self.rhoatom_rd[s] = rho_rd
cubefile = open(file, "r") lines = cubefile.readlines() nside[0] = int(lines[3].split()[0]) nside[1] = int(lines[4].split()[0]) nside[2] = int(lines[5].split()[0]) npoints = 1 for i in range(3): npoints *= nside[i] dx = float(lines[3].split()[1]) dy = float(lines[4].split()[2]) dz = float(lines[5].split()[3]) origin = np.asarray(lines[2].split(), dtype=float)[1:4] cubefile.close() cubefile = aio.read(file) #density_regular=cube.read_cube_data(file)[0] density_regular = np.concatenate(np.concatenate(cube.read_cube_data(file)[0])) #construct the grid grid_regular = np.transpose( np.asarray( np.meshgrid(dx * np.asarray(range(nside[0])), dy * np.asarray(range(nside[1])), dz * np.asarray(range(nside[2])))), (2, 1, 3, 0)) grid_regular = grid_regular.reshape((npoints, 3)) grid_regular += origin # find the indices of the field at 3.25 A over the molecule layer = 3.25 / bohr2ang mask_1 = grid_regular[:, 2] >= layer - dz mask_2 = grid_regular[:, 2] <= layer + dz
from ase.io.cube import read_cube_data from x3dase.x3d import X3D data, atoms = read_cube_data('datas/test.cube') atoms.pbc = [True, True, True] X3D(atoms, bond=1.0, label=True, isosurface=[data, -0.001, 0.001]).write('cube.html')
[p1[1], p2[1]], [p1[2], p2[2]], tube_radius=0.1) cp = mlab.contour3d(data, contours=contours, transparent=True, opacity=1, colormap='hot') # Do some tvtk magic in order to allow for non-orthogonal unit cells: polydata = cp.actor.actors[0].mapper.input pts = np.array(polydata.points) - 1 # Transform the points to the unit cell: polydata.points = np.dot(pts, A / np.array(data.shape)[:, np.newaxis]) # Apparently we need this to redraw the figure, maybe it can be done in # another way? mlab.view(azimuth=155, elevation=70, distance='auto') # Show the 3d plot: mlab.show() dataAndAtoms = [read_cube_data(str(i)+'/siesta.RHO.cube') for i in range(5,155,30)] for index, image in enumerate(dataAndAtoms): #if index == 0: # pho0, atoms0 = dataAndAtoms[0] #else: pho, atoms = image data = (pho) write(str(index)+'.cube',atoms,data=data) lowBound = data.min() upBound = data.max() plot(atoms,data,[-0.007],index)
def get_pseudo_wave_function(self, band=0, kpt=0, spin=None): """Return pseudo-wave-function array. The method is limited to the gamma point, and is implemented as a wrapper to denchar (a tool shipped with siesta); denchar must be available in the command path. When retrieving a p_w_f from a non-spin-polarized calculation, spin must be None (default), and for spin-polarized calculations, spin must be set to either 0 (up) or 1 (down). As long as the necessary files are present and named correctly, old p_w_fs can be read as long as the calculator label is set. E.g. >>> c = Siesta(label='name_of_old_calculation') >>> pwf = c.get_pseudo_wave_function() The broadcast and pad options are not implemented. """ # Not implemented: kpt=0, broadcast=True, pad=True # kpoint must be Gamma assert kpt == 0, \ "siesta.get_pseudo_wave_function is unfortunately limited " \ "to the gamma point only. kpt must be 0." # In denchar, band numbering starts from 1 assert type(band) is int and band >= 0 band = band + 1 if spin is None: spin_name = "" elif spin == 0: spin_name = ".UP" elif spin == 1: spin_name = ".DOWN" label = self.label # If <label>.WF<band>.cube already exist and is newer than <label>.fdf, # just return it fn_wf = label + ('.WF%i%s.cube' % (band, spin_name)) fn_fdf = label + '.fdf' if isfile(fn_wf) and isfile(fn_fdf) and (getmtime(fn_wf) > getmtime(fn_fdf)): x, _ = read_cube_data(fn_wf) return x if not isfile(fn_fdf): raise RuntimeError( 'Could not find the fdf-file. It is required as ' 'part of the input for denchar.') fdf_mtime = getmtime(fn_fdf) for suf in ['.WFS', '.PLD', '.DM', '.DIM']: if not isfile(label + suf): raise RuntimeError( 'Could not find file "%s%s" which is required ' 'when extracting wave functions ' '(make sure the fdf options "WriteDenchar" is ' 'True, and WaveFuncKpoints is [0.0 0.0 0.0]")' % (label, suf)) if not getmtime(label + suf) > fdf_mtime: # This should be handled in a better way, e.g. by implementing # a "calculation_required() and calculate()" raise RuntimeError('The calculation is not up to date.') # Simply read the old fdf-file and pick some meta info from there. # However, strictly it's not always neccesary fdf = read_fdf(fn_fdf) if 'latticeconstant' in fdf: const = float(fdf['latticeconstant'][0]) unit = fdf['latticeconstant'][1] else: const = 1.0 unit = 'Ang' if 'latticevectors' in fdf: cell = np.array(fdf['latticevectors'], dtype='d') else: raise RuntimeError( 'Failed to find the lattice vectors in the fdf-file.') if 'spinpolarized' in fdf and \ fdf['spinpolarized'][0].lower() in ['yes', 'true', '.true.', 'T', '']: if spin is None: raise RuntimeError( 'The calculation was spin polarized, pick either ' 'spin=0 or 1.') else: if not spin is None: raise RuntimeError('The calculation was not spin polarized, ' 'spin argument must be None.') denc_fdf = open(fn_fdf).readlines() denc_fdf.append('Denchar.TypeOfRun 3D\n') denc_fdf.append('Denchar.PlotWaveFunctions T\n') for dim, dir in zip(cell.transpose(), ['X', 'Y', 'Z']): # Naive square box limits to denchar denc_fdf.append('Denchar.Min%s %f %s\n' % (dir, const * dim.min(), unit)) denc_fdf.append('Denchar.Max%s %f %s\n' % (dir, const * dim.max(), unit)) # denchar rewinds stdin and fails if stdin is a pipe denc_fdf_file = open(label + '.denchar.fdf', 'w') denc_fdf_file.write(''.join(denc_fdf)) denc_fdf_file.close() p = Popen('denchar', shell=True, stdin=open(label + '.denchar.fdf'), stdout=PIPE, stderr=PIPE, close_fds=True) exitcode = p.wait() if exitcode == 0: if not isfile(fn_wf): raise RuntimeError('Could not find the requested file (%s)' % fn_wf) x, _ = read_cube_data(fn_wf) return x elif exitcode == 127: raise RuntimeError( 'No denchar executable found. Make sure it is in the path.') else: import sys print >> sys.stderr, ''.join(p.stderr.readlines()) raise RuntimeError('Execution of denchar failed!')
def cube2gesp(infile_cube, outfile_gesp, N=-1): # cube file measures in Bohr, but ASE converts input to Angstrom # however, field is not converted while distances are cube_data, cube_atoms = read_cube_data(infile_cube) # nX = cube_data.shape # X = cube_atoms.cell.diagonal() # measures of cell in spatial dimensions # x_grid = np.linspace(0,X[0],nX[0]) # y_grid = np.linspace(0,X[1],nX[1]) # z_grid = np.linspace(0,X[2],nX[2]) # x_grid3,y_grid3,z_grid3=np.meshgrid(x_grid,y_grid,z_grid) # general approach for creating a grid of coordinates # in 3 dimensions and N points in each spatial dimension, # X.shape == (3, N, N, N) dim = cube_data.ndim # usually 3 print("Read .cube file of shape {} in box {}.".format( cube_data.shape, cube_atoms.cell.diagonal())) X_lin = [] X = np.empty((dim, ) + cube_data.shape) for i in range(0, dim): X_lin.append(np.linspace(0, cube_atoms.cell[i, i], cube_data.shape[i])) X_list = np.meshgrid(*X_lin, indexing='ij') X = np.asarray(X_list) Unit_Cell = X[:, 1, 1, 1].prod() # for uniform grid Integral = cube_data.flatten().sum() * Unit_Cell print("Reconstructed uniform grid of shape {}.".format(X.shape)) if N > 0: print( "Interpolate .cube data of shape {} and {} points in total onto grid" " of less than {} points.".format(cube_data.shape, np.prod(cube_data.shape), N)) gridInterpolator = RegularGridInterpolator(tuple(X_lin), cube_data, method="linear", bounds_error=True) NX = np.asarray(cube_data.shape) r = (N / np.prod(cube_data.shape))**(1 / dim) Nx = np.floor(r * NX) x_lin = [] for i in range(0, dim): x_lin.append(np.linspace(0, cube_atoms.cell[i, i], Nx[i])) x_list = np.meshgrid(*x_lin, indexing='ij') # X1d = n X = np.asarray(x_list) print( " Original grid: {:32,d} points, {} by spatial dimensions".format( int(NX.prod()), NX.astype(int))) cube_data = gridInterpolator(X.T) print( "Coarsened grid: {:32,d} points, {} by spatial dimensions".format( int(Nx.prod()), Nx.astype(int))) print("Coarsened grid by factor {:.3f}.".format(r)) unit_cell = X[:, 1, 1, 1].prod() # for uniform grid integral = cube_data.flatten().sum() * unit_cell print("on original grid: data integral {:.3e} with {:.3e} unit cell". format(Integral, Unit_Cell)) print("on coarsened grid: data integral {:.3e} with {:.3e} unit cell". format(integral, unit_cell)) # atom positions in N x 3 array (rows x cols) pos = cube_atoms.get_positions() # potential on grid in M x 4 array (rows x cols) # dat = np.vstack( ( cube_data.flatten(), x_grid3.flatten()/Bohr, y_grid3.flatten()/Bohr, z_grid3.flatten()/Bohr ) ).T dat = np.vstack((cube_data.flatten(), X[0].flatten() / Bohr, X[1].flatten() / Bohr, X[2].flatten() / Bohr)).T n_atoms = len(cube_atoms) n_dat = dat.shape[0] l1 = "{:5d}{:6d}{:5d}".format(n_atoms, n_dat, 0) # first line ghd = io.BytesIO() # .gesp header buffer np.savetxt(ghd, pos, fmt='%16.7E', delimiter='', header=l1, comments='') ghd = ('\n' + ' ' * 16).join(ghd.getvalue().decode().splitlines()) np.savetxt(outfile_gesp, dat, fmt='%16.7E', delimiter='', header=ghd, comments='')
from ase.io.cube import read_cube_data from blase.tools import write_blender data, atoms = read_cube_data('test.cube') kwargs = { 'show_unit_cell': 1, 'engine': 'BLENDER_EEVEE', #'BLENDER_EEVEE' #'BLENDER_WORKBENCH' 'radii': 0.4, # 'bond_cutoff': 1.0, 'isosurface': [data, -0.002, 0.002], 'rotations': [[90, '-x'], [30, 'y']], 'display': True, 'outfile': 'figs/testcube' } write_blender(atoms, **kwargs)
from ase.calculators.siesta.import_functions import xv_to_atoms from matplotlib import cm import matplotlib.pyplot as plt import numpy as np from matplotlib import animation selectedTimeSteps = range(5, 1000, 20) vmax = 0.01 vmin = -vmax slide = 27 dataAndAtoms = [ read_cube_data(str(i) + '/siesta.DRHO.cube') for i in selectedTimeSteps ] fig, axs = plt.subplots(1, 2, figsize=(12, 6)) pho0, atoms = dataAndAtoms[0] #atoms = xv_to_atoms('siesta.XV') X = np.arange(pho0.shape[1]) * atoms.cell[1, 1] / pho0.shape[1] Y = np.arange(pho0.shape[2]) * atoms.cell[2, 2] / pho0.shape[2] x, y = np.meshgrid(X, Y) import pyramids.io.result as dP time, light = dP.getEField() step = len(time) / len(dataAndAtoms) #print light.shape line, = axs[1].plot(time, light[:, 2])
# top_file = 'system100.top' # hdf5_file = 'smamp_charges.h5' # top_outfile = 'smamp_esp_charges.top' infile_pdb = args.infile_pdb infile_top = args.infile_top infile_cube = args.infile_cube outfile_cube = args.outfile_cube ase_struct = ase.io.read(infile_pdb) pmd_struct = pmd.load_file(infile_pdb) pmd_top = gromacs.GromacsTopologyFile(infile_top, parametrize=False) # throws some warnings on angle types, does not matter for bonding info pmd_top.strip(':SOL,CL') # strip water and electrolyte from system pmd_top.box = pmd_struct.box # Needed because .prmtop contains box info pmd_top.positions = pmd_struct.positions new_ase_struct, new_pmd_struct, names = insertHbyList( ase_struct, pmd_top, implicitHbondingPartners, 1.0) surplus_atoms = len(new_ase_struct) - len(ase_struct) print("{} atoms are going to be truncated from file {}...".format( surplus_atoms, infile_cube)) # hdf5 = h5py.File(infile_h5,'r') cube_data, cube_atoms = read_cube_data(infile_cube) ase.io.write(outfile_cube, cube_atoms[0:len(ase_struct)], data=cube_data) # ATTENTION: this script just truncates atoms based on total count difference # in UA and AA representations
calc.read_results() e = calc.results['energy'] fermi = calc.get_fermi_level() print('Energy: {0:1.3f}'.format(e)) #=============================================================== # post calculation calc.post(package='pp', plot_num=5, sample_bias=0.0735, iflag=3, output_format=6, fileout='stm1_1eV.cube') # ======================== # Creates: 2d.png, 2d_I.png, line.png, dIdV.png from ase.dft.stm import STM from ase.io.cube import read_cube_data import pickle ldos, atoms = read_cube_data('scf/al/stm1_1eV.cube') with open('scf/al/datas.pickle', 'wb') as f: pickle.dump([ldos, 1.0, atoms.cell], f) stm = STM('scf/al/datas.pickle') z = 8.0 bias = 1.0 c = stm.get_averaged_current(bias, z) x, y, h = stm.scan(bias, c, repeat=(3, 5)) # print(x, y, h) import matplotlib.pyplot as plt plt.gca(aspect='equal') plt.contourf(x, y, h, 40) plt.colorbar() plt.savefig('2d.png')
def plot_density(ax): from ase.io.cube import read_cube_data data, atom = read_cube_data("data/licoo2/monolayer.cube") lat = atom.get_cell_lengths_and_angles() x = np.linspace(0, lat[0], data.mean(axis=0).T[::-1].shape[1]) z = np.linspace(0, lat[2], data.mean(axis=0).T[::-1].shape[0]) # r=ax.contourf(x,z,data.mean(axis=0).T,90,cmap="Greys",vmin=1e-10,vmax=.92e-2) r = ax.contourf(z, x, data.mean(axis=0), 200, cmap="Greys", vmin=1e-10, vmax=.92e-2) for c in r.collections: c.set_rasterized(True) # cb=plt.colorbar(r,orientation='horizontal') color = {} color["O"] = ["#003049", 9] color["Co"] = ["#2d6a4f", 20] color["Li"] = ["#ff7b00", 30] for cnt, k in enumerate([ -atom.get_cell_lengths_and_angles()[0], 0, atom.get_cell_lengths_and_angles()[0] ]): for j, i in enumerate(atom.get_positions()): c = color[atom.get_chemical_symbols()[j]] ax.scatter(i[2], i[1] + k, edgecolor=c[0], facecolor="none", s=c[1] * .5e1, linewidth=2, alpha=1) ax.scatter(i[2], i[1] + k, edgecolor="none", facecolor=c[0], s=c[1] * .5e1, linewidth=0, alpha=.6) # if atom.get_chemical_symbols()[j]=="Li" and (cnt==2 or cnt==1): if -.1 < i[1] + k < atom.get_cell_lengths_and_angles()[0] + .1: ax.text(i[2] - .65, i[1] + k - .3, atom.get_chemical_symbols()[j], bbox=dict(facecolor='w', edgecolor='black', boxstyle='round,pad=.2', alpha=0.3)) for j, i in enumerate(atom.get_positions()): c = color[atom.get_chemical_symbols()[j]] ax.scatter(i[2], i[1] + k, edgecolor=c[0], facecolor="none", s=c[1] * .5e1, linewidth=2, alpha=1, label=atom.get_chemical_symbols()[j]) ax.legend() ax.set_ylim(0, atom.get_cell_lengths_and_angles()[0]) ax.yaxis.set_visible(False) ax.xaxis.set_visible(False)