diff --git a/__init__.py b/__init__.py index 439a037..b181e54 100644 --- a/__init__.py +++ b/__init__.py @@ -10,10 +10,10 @@ import os try: - import espsite + from . import espsite except ImportError: - print '*** ase-espresso requires a site-specific espsite.py in PYTHONPATH.' - print '*** You may use the espsite.py.example.* in the git checkout as templates.' + print('*** ase-espresso requires a site-specific espsite.py in PYTHONPATH.') + print('*** You may use the espsite.py.example.* in the git checkout as templates.') raise ImportError site = espsite.config() @@ -21,10 +21,9 @@ import atexit import sys, string import numpy as np -from types import FileType, StringType -from constants import * -from utils import * -from subdirs import * +from .constants import * +from .utils import * +from .subdirs import * # ase controlled pw.x's register themselves here, so they can be # stopped automatically @@ -394,7 +393,7 @@ def __init__(self, if type(kpts)==float or type(kpts)==int: from ase.calculators.calculator import kptdensity2monkhorstpack kpts = kptdensity2monkhorstpack(atoms, kpts) - elif isinstance(kpts, StringType): + elif isinstance(kpts, str): assert kpts == 'gamma' else: assert len(kpts) == 3 @@ -594,11 +593,11 @@ def __init__(self, atoms.set_calculator(self) if hasattr(site, 'mpi_not_setup') and self.onlycreatepwinp is None: - print '*** Without cluster-adjusted espsite.py, ase-espresso can only be used' - print '*** to create input files for pw.x via the option onlycreatepwinp.' - print '*** Otherwise, ase-espresso requires a site-specific espsite.py' - print '*** in PYTHONPATH.' - print '*** You may use the espsite.py.example.* in the git checkout as templates.' + print('*** Without cluster-adjusted espsite.py, ase-espresso can only be used') + print('*** to create input files for pw.x via the option onlycreatepwinp.') + print('*** Otherwise, ase-espresso requires a site-specific espsite.py') + print('*** in PYTHONPATH.') + print('*** You may use the espsite.py.example.* in the git checkout as templates.') raise ImportError @@ -621,7 +620,7 @@ def input_update(self): try: self.psppath = os.environ['ESP_PSP_PATH'] except: - print 'Unable to find pseudopotential path. Consider setting ESP_PSP_PATH environment variable' + print('Unable to find pseudopotential path. Consider setting ESP_PSP_PATH environment variable') raise if self.dipole is None: self.dipole = {'status':False} @@ -631,7 +630,7 @@ def input_update(self): if self.convergence is None: self.conv_thr = 1e-6/rydberg else: - if self.convergence.has_key('energy'): + if 'energy' in self.convergence: self.conv_thr = self.convergence['energy']/rydberg else: self.conv_thr = 1e-6/rydberg @@ -654,11 +653,11 @@ def create_outdir(self): self.log = self.txt self.scratch = mkscratch(self.localtmp, site) if self.output is not None: - if self.output.has_key('removewf'): + if 'removewf' in self.output: removewf = self.output['removewf'] else: removewf = True - if self.output.has_key('removesave'): + if 'removesave' in self.output: removesave = self.output['removesave'] else: removesave = False @@ -741,7 +740,7 @@ def atoms2species(self): if type(self.U)==dict: Ulist = np.zeros(len(symbols), np.float) for i,s in enumerate(symbols): - if self.U.has_key(s): + if s in self.U: Ulist[i] = self.U[s] else: Ulist = list(self.U) @@ -754,7 +753,7 @@ def atoms2species(self): if type(self.J)==dict: Jlist = np.zeros(len(symbols), np.float) for i,s in enumerate(symbols): - if self.J.has_key(s): + if s in self.J: Jlist[i] = self.J[s] else: Jlist = list(self.J) @@ -767,7 +766,7 @@ def atoms2species(self): if type(self.U_alpha)==dict: U_alphalist = np.zeros(len(symbols), np.float) for i,s in enumerate(symbols): - if self.U_alpha.has_key(s): + if s in self.U_alpha: U_alphalist[i] = self.U_alpha[s] else: U_alphalist = list(self.U_alpha) @@ -784,7 +783,7 @@ def atoms2species(self): symcounter[s] = 0 for i in range(len(symbols)): key = symbols[i]+'_m%.14eU%.14eJ%.14eUa%.14e' % (magmoms[i],Ulist[i],Jlist[i],U_alphalist[i]) - if dic.has_key(key): + if key in dic: self.specprops.append((dic[key][1],pos[i])) else: symcounter[symbols[i]] += 1 @@ -825,7 +824,7 @@ def writeinputfile(self, filename='pw.inp', mode=None, overridekpts=None, overridekptshift=None, overridenbands=None, suppressforcecalc=False, usetetrahedra=False): if self.atoms is None: - raise ValueError, 'no atoms defined' + raise ValueError('no atoms defined') if self.cancalc: fname = self.localtmp+'/'+filename #f = open(self.localtmp+'/pw.inp', 'w') @@ -866,14 +865,14 @@ def writeinputfile(self, filename='pw.inp', mode=None, if self.calcstress: print >>f, ' tstress=.true.,' if self.output is not None: - if self.output.has_key('avoidio'): + if 'avoidio' in self.output: if self.output['avoidio']: self.output['disk_io'] = 'none' - if self.output.has_key('disk_io'): + if 'disk_io' in self.output: if self.output['disk_io'] in ['high', 'low', 'none']: print >>f, ' disk_io=\''+self.output['disk_io']+'\',' - if self.output.has_key('wf_collect'): + if 'wf_collect' in self.output: if self.output['wf_collect']: print >>f, ' wf_collect=.true.,' if self.opt_algorithm!='ase3' or not self.cancalc: @@ -941,7 +940,7 @@ def writeinputfile(self, filename='pw.inp', mode=None, if self.tot_magnetization != 'hund': self.totmag = self.tot_magnetization else: - from atomic_configs import hundmag + from .atomic_configs import hundmag self.totmag = sum([hundmag(x) for x in self.atoms.get_chemical_symbols()]) print >>f, ' tot_magnetization='+num2str(self.totmag*inimagscale)+',' print >>f, ' ecutwfc='+num2str(self.pw/rydberg)+',' @@ -1030,15 +1029,15 @@ def writeinputfile(self, filename='pw.inp', mode=None, if dipfield or efield: print >>f, ' edir='+str(edir)+',' if dipfield: - if self.dipole.has_key('emaxpos'): + if 'emaxpos' in self.dipole: emaxpos = self.dipole['emaxpos'] else: emaxpos = self.find_max_empty_space(edir) - if self.dipole.has_key('eopreg'): + if 'eopreg' in self.dipole: eopreg = self.dipole['eopreg'] else: eopreg = 0.025 - if self.dipole.has_key('eamp'): + if 'eamp' in self.dipole: eamp = self.dipole['eamp'] else: eamp = 0.0 @@ -1046,15 +1045,15 @@ def writeinputfile(self, filename='pw.inp', mode=None, print >>f, ' eopreg='+num2str(eopreg)+',' print >>f, ' eamp='+num2str(eamp)+',' if efield: - if self.field.has_key('emaxpos'): + if 'emaxpos' in self.field: emaxpos = self.field['emaxpos'] else: emaxpos = 0.0 - if self.field.has_key('eopreg'): + if 'eopreg' in self.field: eopreg = self.field['eopreg'] else: eopreg = 0.0 - if self.field.has_key('eamp'): + if 'eamp' in self.field: eamp = self.field['eamp'] else: eamp = 0.0 @@ -1367,7 +1366,7 @@ def writeinputfile(self, filename='pw.inp', mode=None, ### closing PWscf input file ### f.close() if self.verbose == 'high': - print '\nPWscf input file %s written\n' % fname + print('\nPWscf input file %s written\n' % fname) def set_atoms(self, atoms): if self.atoms is None or not self.started: @@ -1511,18 +1510,18 @@ def read(self, atoms): break if a[:20]==' convergence NOT': self.stop() - raise RuntimeError, 'scf cycles did not converge\nincrease maximum number of steps and/or decreasing mixing' + raise RuntimeError('scf cycles did not converge\nincrease maximum number of steps and/or decreasing mixing') elif a[:13]==' stopping': self.stop() self.checkerror() #if checkerror shouldn't find an error here, #throw this generic error - raise RuntimeError, 'SCF calculation failed' + raise RuntimeError('SCF calculation failed') elif a=='' and self.calcmode in ('ase3','relax','scf','vc-relax','vc-md','md'): self.checkerror() #if checkerror shouldn't find an error here, #throw this generic error - raise RuntimeError, 'SCF calculation failed' + raise RuntimeError('SCF calculation failed') self.atom_occ = atom_occ self.results['magmoms'] = magmoms if self.calcmode in ('ase3','relax','scf','vc-relax','vc-md','md','hund'): @@ -1574,22 +1573,22 @@ def read(self, atoms): a = self.cout.readline() s.write(a) if not self.dontcalcforces: - while a[:11]!=' Forces': - a = self.cout.readline() - s.write(a) - s.flush() - a = self.cout.readline() - s.write(a) - self.forces = np.empty((self.natoms,3), np.float) - for i in range(self.natoms): - a = self.cout.readline() - while a.find('force')<0: - s.write(a) - a = self.cout.readline() - s.write(a) - forceinp = a.split() - self.forces[i][:] = [float(x) for x in forceinp[len(forceinp)-3:]] - self.forces *= rydberg_over_bohr + while a[:11]!=' Forces': + a = self.cout.readline() + s.write(a) + s.flush() + a = self.cout.readline() + s.write(a) + self.forces = np.empty((self.natoms,3), np.float) + for i in range(self.natoms): + a = self.cout.readline() + while a.find('force')<0: + s.write(a) + a = self.cout.readline() + s.write(a) + forceinp = a.split() + self.forces[i][:] = [float(x) for x in forceinp[len(forceinp)-3:]] + self.forces *= rydberg_over_bohr else: self.forces = None else: @@ -1993,7 +1992,7 @@ def get_final_stress(self): p.close() if len(s)!=3: - raise RuntimeError, 'stress was not calculated\nconsider specifying calcstress or running a unit cell relaxation' + raise RuntimeError('stress was not calculated\nconsider specifying calcstress or running a unit cell relaxation') stress = np.empty((3,3), np.float) for i in range(3): @@ -2048,7 +2047,7 @@ def checkerror(self): try: n = int(p.readline().split()[0].strip(':')) except: - raise RuntimeError, 'Espresso executable doesn\'t seem to have been started.' + raise RuntimeError('Espresso executable doesn\'t seem to have been started.') p.close() p = os.popen(('tail -n +%d ' % n)+self.log+' | grep -n %%%%%%%%%%%%%%%% |tail -2','r') @@ -2074,7 +2073,7 @@ def checkerror(self): msg = '' for e in err: msg += e - raise RuntimeError, msg[:len(msg)-1] + raise RuntimeError(msg[:len(msg)-1]) def relax_cell_and_atoms(self, cell_dynamics='bfgs', # {'none', 'sd', 'damp-pr', 'damp-w', 'bfgs'} @@ -2186,9 +2185,9 @@ def run_espressox(self, binary, inp, log=None, piperead=False, def run_ppx(self, inp, log=None, inputpp=[], plot=[], output_format=5, iflag=3, piperead=False, parallel=True): - if self.output.has_key('disk_io'): + if 'disk_io' in self.output: if self.output['disk_io'] == 'none': - print "run_ppx requires output['disk_io'] to be at least 'low' and avoidio=False" + print("run_ppx requires output['disk_io'] to be at least 'low' and avoidio=False") self.stop() f = open(self.localtmp+'/'+inp, 'w') print >>f, '&INPUTPP\n prefix=\'calc\',\n outdir=\'.\',' @@ -2229,7 +2228,7 @@ def get_fermi_level(self): efermi = float(p.readline().split()[-2]) p.close() except: - raise RuntimeError, 'get_fermi_level called before DFT calculation was run' + raise RuntimeError('get_fermi_level called before DFT calculation was run') return efermi @@ -2349,7 +2348,7 @@ def calc_pdos(self, else: #ncomponents = 2*j+1 +1 (latter for m summed up) ncomponents = int(2.*float(channel[jpos+2:]))+2 - if not self.pdos[iatom].has_key(channel): + if channel not in self.pdos[iatom]: self.pdos[iatom][channel] = np.zeros((ncomponents,npoints), np.float) first = True else: @@ -2468,7 +2467,7 @@ def __get_atomic_projections__(self): while a.find('/dev/null') os.chdir(cdir) - if hasattr(site, 'mpdshutdown') and not os.environ.has_key('QEASE_MPD_ISSHUTDOWN'): + if hasattr(site, 'mpdshutdown') and 'QEASE_MPD_ISSHUTDOWN' not in os.environ: os.environ['QEASE_MPD_ISSHUTDOWN'] = 'yes' os.system(site.mpdshutdown) diff --git a/utils.py b/utils.py index d26c3d1..bbbf6c0 100644 --- a/utils.py +++ b/utils.py @@ -72,11 +72,11 @@ def convert_constraints(atoms): elif isinstance(d, constraints.FixInternals.FixDihedral): otherconstr.append("'torsional_angle' %d %d %d %d %s" % (d.indices[0]+1,d.indices[1]+1,d.indices[2]+1,d.indices[3]+1,num2str(np.arccos(d.angle)*180./np.pi))) else: - raise NotImplementedError, 'constraint '+d.__name__+' from FixInternals not implemented\n' \ - 'consider ase-based relaxation with this constraint instead' + raise NotImplementedError('constraint '+d.__name__+' from FixInternals not implemented\n' \ + 'consider ase-based relaxation with this constraint instead') else: - raise NotImplementedError, 'constraint '+c.__name__+' not implemented\n' \ - 'consider ase-based relaxation with this constraint instead' + raise NotImplementedError('constraint '+c.__name__+' not implemented\n' \ + 'consider ase-based relaxation with this constraint instead') return forcefilter,otherconstr else: return [],[]