Skip to content

Commit

Permalink
Merge pull request #3 from jonwright/master
Browse files Browse the repository at this point in the history
python3 version
  • Loading branch information
jonwright authored Jan 16, 2018
2 parents 6e715a1 + f23c649 commit 13f0fdd
Show file tree
Hide file tree
Showing 16 changed files with 626 additions and 590 deletions.
67 changes: 35 additions & 32 deletions polyxsim/check_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
# Checking input
#

from string import split
from __future__ import absolute_import
from __future__ import print_function

from copy import copy
import sys, os
import variables
from . import variables
from xfab import tools,sg

import numpy as n

#import logging
#logging.basicConfig(level=logging.DEBUG,format='%(levelname)s %(message)s')

Expand Down Expand Up @@ -105,16 +108,16 @@ def read(self):
try:
f = open(self.filename,'r')
except IOError:
raise IOError, 'No file named %s' %self.filename
raise IOError('No file named %s' %self.filename)

self.input = f.readlines()
f.close()

for lines in self.input:
if lines.find('#') != 0:
if lines.find('#') > 0:
lines = split(lines,'#')[0]
line = split(lines)
lines = lines.split('#')[0]
line = lines.split()
if len(line) != 0:
key = line[0]
val = line[1:]
Expand All @@ -128,7 +131,7 @@ def read(self):
valend = ']'
sepa = ','

if len(val) > 1:
if len(val) > 1 or key == "output":
for i in val:
valtmp = valtmp + i + sepa
# remove last separator
Expand Down Expand Up @@ -260,17 +263,17 @@ def check(self):
for item in self.param:
if '_phase_' in item:
if 'structure' in item:
phase_list_structure.append(eval(split(item,'_phase_')[1]))
phase_list_structure.append(eval(item.split('_phase_')[1]))
elif 'unit_cell' in item:
phase_list_unit_cell.append(eval(split(item,'_phase_')[1]))
phase_list_unit_cell.append(eval(item.split('_phase_')[1]))
elif 'sgno' in item:
phase_list_sgno.append(eval(split(item,'_phase_')[1]))
phase_list_sgno.append(eval(item.split('_phase_')[1]))
elif 'sgname' in item:
phase_list_sgname.append(eval(split(item,'_phase_')[1]))
phase_list_sgname.append(eval(item.split('_phase_')[1]))
elif 'gen_size' in item:
phase_list_gen_size.append(eval(split(item,'_phase_')[1]))
phase_list_gen_size.append(eval(item.split('_phase_')[1]))
elif 'gen_eps' in item:
phase_list_gen_eps.append(eval(split(item,'_phase_')[1]))
phase_list_gen_eps.append(eval(item.split('_phase_')[1]))


phase_list_structure.sort()
Expand Down Expand Up @@ -450,15 +453,15 @@ def check(self):
for item in self.param:
if '_grains_' in item:
if 'U' in item:
grain_list_U.append(eval(split(item,'_grains_')[1]))
grain_list_U.append(eval(item.split('_grains_')[1]))
elif 'pos' in item:
grain_list_pos.append(eval(split(item,'_grains_')[1]))
grain_list_pos.append(eval(item.split('_grains_')[1]))
elif 'eps' in item:
grain_list_eps.append(eval(split(item,'_grains_')[1]))
grain_list_eps.append(eval(item.split('_grains_')[1]))
elif 'size' in item:
grain_list_size.append(eval(split(item,'_grains_')[1]))
grain_list_size.append(eval(item.split('_grains_')[1]))
elif 'phase' in item[:5]:
grain_list_phase.append(eval(split(item,'_grains_')[1]))
grain_list_phase.append(eval(item.split('_grains_')[1]))
self.param['no_grains_phase_%i' %self.param[item]] += 1

#assert that the number of grains in all match
Expand Down Expand Up @@ -593,7 +596,7 @@ def check(self):
' of phase_grains, check for multiple names'
self.param['grain_list'] = grain_list_phase
else:
self.param['grain_list'] = range(no_grains)
self.param['grain_list'] = list(range(no_grains))

# assert that all information needed to generate grains is present
# assert len(grain_list_U) != 0 or self.param['gen_U'] != 0,\
Expand Down Expand Up @@ -769,13 +772,13 @@ def check(self):

def show_errors(self):
if len(self.errors) > 0:
print 'List of errors and/or inconsistencies found in input: '
print '----------------------------------------------------- '
print('List of errors and/or inconsistencies found in input: ')
print('----------------------------------------------------- ')
no = 0
for i in self.errors:
no += 1
print 'Error %3i : ' %no, self.errors[i]
print '----------------------------------------------------- \n'
print('Error %3i : ' %no, self.errors[i])
print('----------------------------------------------------- \n')



Expand All @@ -801,7 +804,7 @@ def initialize(self):
modulus = n.abs(omega_end-omega_start)%omega_step
if modulus > 1e-9:
if omega_step-modulus > 1e-9:
raise ValueError, 'The omega range does not match an integer number of omega steps'
raise ValueError('The omega range does not match an integer number of omega steps')

# print omega_start,omega_end,omega_step, (n.abs(omega_end-omega_start)+1e-19)%omega_step
omega_sign = self.param['omega_sign']
Expand Down Expand Up @@ -934,17 +937,17 @@ def init_values(self):
for item in self.param:
if '_phase_' in item:
if 'structure' in item:
phase_list_structure.append(eval(split(item,'_phase_')[1]))
phase_list_structure.append(eval(item.split('_phase_')[1]))
elif 'unit_cell' in item:
phase_list_unit_cell.append(eval(split(item,'_phase_')[1]))
phase_list_unit_cell.append(eval(item.split('_phase_')[1]))
elif 'sgno' in item:
phase_list_sgno.append(eval(split(item,'_phase_')[1]))
phase_list_sgno.append(eval(item.split('_phase_')[1]))
elif 'sgname' in item:
phase_list_sgname.append(eval(split(item,'_phase_')[1]))
phase_list_sgname.append(eval(item.split('_phase_')[1]))
elif 'gen_size' in item:
phase_list_gen_size.append(eval(split(item,'_phase_')[1]))
phase_list_gen_size.append(eval(item.split('_phase_')[1]))
elif 'gen_eps' in item:
phase_list_gen_eps.append(eval(split(item,'_phase_')[1]))
phase_list_gen_eps.append(eval(item.split('_phase_')[1]))


phase_list_structure.sort()
Expand Down Expand Up @@ -1016,13 +1019,13 @@ def init_values(self):
try:
filename = sys.argv[1]
except:
print 'Usage: check_input.py <input.inp>'
print('Usage: check_input.py <input.inp>')
sys.exit()

myinput = parse_input(input_file = filename)
myinput.read()
myinput.check()
if myinput.missing == True:
print 'MISSING ITEMS'
print('MISSING ITEMS')
myinput.evaluate()
print myinput.param
print(myinput.param)
91 changes: 46 additions & 45 deletions polyxsim/file_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import absolute_import, print_function
import numpy as n
from xfab import tools,detector
import variables
from . import variables


A_id = variables.refarray().A_id

Expand All @@ -17,7 +19,7 @@ def write_flt(param,grain):
# out = '# sc fc omega Number_of_pixels avg_intensity s_raw f_raw sigs sigf covsf sigo covso covfo sum_intensity sum_intensity^2 IMax_int IMax_s IMax_f IMax_o Min_s Max_s Min_f Max_f Min_o Max_o dety detz onfirst onlast spot3d_id \n'
out = '# sc fc omega Number_of_pixels avg_intensity s_raw f_raw sigs sigf covsf sigo covso covfo sum_intensity sum_intensity^2 IMax_int IMax_s IMax_f IMax_o Min_s Max_s Min_f Max_f Min_o Max_o dety detz onfirst onlast spot3d_id labels tth_per_grain eta_per_grain h k l\n'
f.write(out)
A = grain[0].refs
for grainno in range(1,param['no_grains']):
A = n.concatenate((A,grain[grainno].refs))
Expand Down Expand Up @@ -84,7 +86,7 @@ def write_flt(param,grain):
)
f.write(out)

f.close()


Expand Down Expand Up @@ -207,17 +209,17 @@ def write_gve(param,grain,hkl):
A = grain[0].refs
for grainno in range(1,param['no_grains']):
A = n.concatenate((A,grain[grainno].refs))
nrefl = A.shape[0]

# from detector.par
(z_center, y_center) = detector.detyz_to_xy([param['dety_center'],param['detz_center']],
param['o11'],
param['o12'],
param['o21'],
param['o22'],
param['dety_size'],
param['detz_size'])
param['o11'],
param['o12'],
param['o21'],
param['o22'],
param['dety_size'],
param['detz_size'])
dout = "# chi 0.0\n"
dout = dout + "# distance %f\n" %(param['distance']*1000.)
dout = dout + "# fit_tolerance 0.5\n"
Expand Down Expand Up @@ -252,19 +254,19 @@ def write_gve(param,grain,hkl):
out = out + "# wavelength = %s\n" %(param['wavelength'])
out = out + "# wedge = %f\n" %(param['wedge'])
out = out + "# axis = 0.000 0.0000 1.0000\n"
# insert detector.par as comment
# insert detector.par as comment
out = out + "# cell__a %s\n" %unit_cell[0]
out = out + "# cell__b %s\n" %unit_cell[1]
out = out + "# cell__c %s\n" %unit_cell[2]
out = out + "# cell_alpha %s\n" %unit_cell[3]
out = out + "# cell_beta %s\n" %unit_cell[4]
out = out + "# cell_gamma %s\n" %unit_cell[5]
out = out + "# cell_gamma %s\n" %unit_cell[5]
out = out + "# cell_lattice_[P,A,B,C,I,F,R] %s\n" %param['sgname_phase_%i' %phase][0]
out = out + dout
# continue with gve format
out = out +"# ds h k l\n"
f.write(out)
thkl = hkl[param['phase_list'].index(phase)].copy()
ds = n.zeros((thkl.shape[0],1))

Expand Down Expand Up @@ -322,7 +324,7 @@ def write_gve(param,grain,hkl):
zl*1000.
)
f.write(out)
f.close()


Expand Down Expand Up @@ -406,13 +408,13 @@ def write_par(param):
#Prepare detector part of p.par output
#Calc beam center in ImageD11 coordinate system
(z_center, y_center) = detector.detyz_to_xy([param['dety_center'],param['detz_center']],
param['o11'],
param['o12'],
param['o21'],
param['o22'],
param['dety_size'],
param['detz_size'])
param['o11'],
param['o12'],
param['o21'],
param['o22'],
param['dety_size'],
param['detz_size'])
dout = "chi 0.0\n"
dout = dout + "distance %f\n" %(param['distance']*1000.)
dout = dout + "fit_tolerance 0.5\n"
Expand All @@ -435,24 +437,24 @@ def write_par(param):
dout = dout + "z_size %f\n" %(param['z_size']*1000.)

for phase in param['phase_list']:
if param['no_phases'] > 1:
if param['no_phases'] > 1:
filename = '%s/%s_phase_%i.par' %(param['direc'],param['stem'],phase)
else:
else:
filename = '%s/%s.par' %(param['direc'],param['stem'])
f = open(filename,'w')
unit_cell = param['unit_cell_phase_%i' %phase]
out = "cell__a %s\n" %unit_cell[0]
out = out + "cell__b %s\n" %unit_cell[1]
out = out + "cell__c %s\n" %unit_cell[2]
out = out + "cell_alpha %s\n" %unit_cell[3]
out = out + "cell_beta %s\n" %unit_cell[4]
out = out + "cell_gamma %s\n" %unit_cell[5]
out = out + "cell_lattice_[P,A,B,C,I,F,R] %s\n" %param['sgname_phase_%i' %phase][0]
out = out + dout
f = open(filename,'w')
unit_cell = param['unit_cell_phase_%i' %phase]
out = "cell__a %s\n" %unit_cell[0]
out = out + "cell__b %s\n" %unit_cell[1]
out = out + "cell__c %s\n" %unit_cell[2]
out = out + "cell_alpha %s\n" %unit_cell[3]
out = out + "cell_beta %s\n" %unit_cell[4]
out = out + "cell_gamma %s\n" %unit_cell[5]
out = out + "cell_lattice_[P,A,B,C,I,F,R] %s\n" %param['sgname_phase_%i' %phase][0]
out = out + dout

f.write(out)
f.close()
f.write(out)
f.close()



Expand All @@ -462,7 +464,7 @@ def write_ref(param,grain,grainno=None):
"""

if grainno == None:
savegrains = range(len(grain))
savegrains = list(range(len(grain)))
else:
savegrains = grainno

Expand Down Expand Up @@ -524,15 +526,15 @@ def write_res(param,filename=None):
filename = '%s/%s.res' %(param['direc'],param['stem'])

f = open(filename,'w')
#initialise and sort keys alphabetically
out = ""
keys = param.keys()
keys = list(param.keys())
keys.sort()

for item in keys:
# rule out None entries
if param[item] != None:
if param[item] is not None:
# treat all strings, remember quotation marks
if type(param[item]) == str:
out += "%s '%s'\n" %(item,param[item])
Expand Down Expand Up @@ -577,19 +579,18 @@ def write_ubi(param):
for i in range(param['no_grains']):
U = param['U_grains_%s' %(param['grain_list'][i])]
gr_eps = n.array(param['eps_grains_%s' %(param['grain_list'][i])])
if param['no_phases'] == 1:
if param['no_phases'] == 1:
phase = param['phase_list'][0]
else:
else:
phase = param['phase_grains_%s' %(param['grain_list'][i])]
# Calculate the B-matrix based on the strain tensor for each grain
# Calculate the B-matrix based on the strain tensor for each grain
B = tools.epsilon_to_b(gr_eps,param['unit_cell_phase_%i' %phase])/(2*n.pi)
UBI = n.linalg.inv(n.dot(U,B))
for j in range(3):
out = format %(UBI[j,0],UBI[j,1],UBI[j,2])
f.write(out)

out = "\n"
f.write(out)
f.close()

Loading

0 comments on commit 13f0fdd

Please sign in to comment.