Skip to content

Commit

Permalink
basic python 3.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-zero committed Jul 1, 2016
1 parent c91b4c2 commit 128b1a2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 56 deletions.
98 changes: 49 additions & 49 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
try:
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()

Expand Down Expand Up @@ -594,11 +594,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


Expand All @@ -621,7 +621,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}
Expand Down Expand Up @@ -825,7 +825,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')
Expand Down Expand Up @@ -1367,7 +1367,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:
Expand Down Expand Up @@ -1511,18 +1511,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'):
Expand Down Expand Up @@ -1574,22 +1574,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:
Expand Down Expand Up @@ -1993,7 +1993,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):
Expand Down Expand Up @@ -2048,7 +2048,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')
Expand All @@ -2074,7 +2074,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'}
Expand Down Expand Up @@ -2188,7 +2188,7 @@ 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 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=\'.\','
Expand Down Expand Up @@ -2229,7 +2229,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


Expand Down Expand Up @@ -2468,7 +2468,7 @@ def __get_atomic_projections__(self):
while a.find('<ATM')<0 and a!='':
a = f.readline()
if a=='':
raise RuntimeError, 'no projections found'
raise RuntimeError('no projections found')

while True:
while a.find('<ATM')<0 and a!='':
Expand Down Expand Up @@ -2570,7 +2570,7 @@ def read_3d_grid(self, stream, log):
f.write(x)
x = stream.readline()
if x=='':
raise RuntimeError, 'error reading 3D data grid'
raise RuntimeError('error reading 3D data grid')
f.write(x)
nx, ny, nz = [int(y) for y in stream.readline().split()]
origin = np.array([float(y) for y in stream.readline().split()])
Expand All @@ -2596,7 +2596,7 @@ def read_2d_grid(self, stream, log):
f.write(x)
x = stream.readline()
if x=='':
raise RuntimeError, 'error reading 2D data grid'
raise RuntimeError('error reading 2D data grid')
f.write(x)
nx, ny = [int(y) for y in stream.readline().split()]
origin = np.array([float(y) for y in stream.readline().split()])
Expand Down Expand Up @@ -2627,7 +2627,7 @@ def extract_charge_density(self, spin='both'):
elif spin=='down' or spin==2:
s = 2
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')

p = self.run_ppx('charge.inp',
inputpp=[['plot_num',0],['spin_component',s]],
Expand All @@ -2648,7 +2648,7 @@ def xsf_charge_density(self, xsf, spin='both'):
elif spin=='down' or spin==2:
s = 2
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')

self.run_ppx('charge.inp',
inputpp=[['plot_num',0],['spin_component',s]],
Expand All @@ -2668,7 +2668,7 @@ def extract_total_potential(self, spin='both'):
elif spin=='down' or spin==2:
s = 2
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')

p = self.run_ppx('totalpot.inp',
inputpp=[['plot_num',1],['spin_component',s]],
Expand All @@ -2689,7 +2689,7 @@ def xsf_total_potential(self, xsf, spin='both'):
elif spin=='down' or spin==2:
s = 2
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')

self.run_ppx('totalpot.inp',
inputpp=[['plot_num',1],['spin_component',s]],
Expand Down Expand Up @@ -2831,7 +2831,7 @@ def extract_wavefunction_density(self, band, kpoint=0, spin='up',
elif spin=='z':
s = 3
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')
if self.spinpol:
p = os.popen('grep "number of k points=" '+self.log+'|tail -1|tr \'=\' \' \'', 'r')
nkp = int(p.readline().split()[4])
Expand Down Expand Up @@ -2870,7 +2870,7 @@ def xsf_wavefunction_density(self, xsf, band, kpoint=0, spin='up',
elif spin=='z':
s = 3
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')
if self.spinpol:
p = os.popen('grep "number of k points=" '+self.log+'|tail -1|tr \'=\' \' \'', 'r')
nkp = int(p.readline().split()[4])
Expand Down Expand Up @@ -2947,7 +2947,7 @@ def extract_int_local_dos(self, spin='both', emin=None, emax=None):
elif spin=='down' or spin==2:
s = 2
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')

inputpp=[['plot_num',10],['spin_component',s]]
efermi = self.get_fermi_level()
Expand Down Expand Up @@ -2975,7 +2975,7 @@ def xsf_int_local_dos(self, xsf, spin='both', emin=None, emax=None):
elif spin=='down' or spin==2:
s = 2
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')

inputpp=[['plot_num',10],['spin_component',s]]
efermi = self.get_fermi_level()
Expand Down Expand Up @@ -3050,7 +3050,7 @@ def extract_noncollinear_magnetization(self, spin='all'):
elif spin=='z':
s = 3
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')
p = self.run_ppx('noncollmag.inp',
inputpp=[['plot_num',13],['spin_component',s]],
piperead=True, parallel=False)
Expand All @@ -3072,7 +3072,7 @@ def xsf_noncollinear_magnetization(self, xsf, spin='all'):
elif spin=='z':
s = 3
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')
self.run_ppx('noncollmag.inp',
inputpp=[['plot_num',13],['spin_component',s]],
plot=[['fileout',self.topath(xsf)]],
Expand All @@ -3091,7 +3091,7 @@ def extract_ae_charge_density(self, spin='both'):
elif spin=='down' or spin==2:
s = 2
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')

p = self.run_ppx('aecharge.inp',
inputpp=[['plot_num',17],['spin_component',s]],
Expand All @@ -3112,7 +3112,7 @@ def xsf_ae_charge_density(self, xsf, spin='both'):
elif spin=='down' or spin==2:
s = 2
else:
raise ValueError, 'unknown spin component'
raise ValueError('unknown spin component')

self.run_ppx('aecharge.inp',
inputpp=[['plot_num',17],['spin_component',s]],
Expand Down
6 changes: 3 additions & 3 deletions multiespresso.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def wait_for_total_energies(self):
a = self.calculators[i].cerr.readline()
notdone |= (a!='' and a[:17]!='! total energy')
if a[:13]==' stopping':
raise RuntimeError, 'problem with calculator #%d' % i
raise RuntimeError('problem with calculator #%d' % i)
elif a[:20]==' convergence NOT':
raise RuntimeError, 'calculator #%d did not converge' % i
raise RuntimeError('calculator #%d did not converge' % i)
elif a[1:17]!=' total energy':
stderr.write(a)
else:
Expand All @@ -85,7 +85,7 @@ def wait_for_total_energies(self):

def set_images(self, images):
if len(images)!=self.ncalc:
raise ValueError, 'number of images (%d) doesn\'t match number of calculators (%d)' % (len(images),self.ncalc)
raise ValueError('number of images (%d) doesn\'t match number of calculators (%d)' % (len(images),self.ncalc))
for i in range(self.ncalc):
images[i].set_calculator(self.calculators[i])
self.images = images
Expand Down
8 changes: 4 additions & 4 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [],[]

0 comments on commit 128b1a2

Please sign in to comment.