def xoppy_calc_xf0(self): MAT_FLAG = self.MAT_FLAG DESCRIPTOR = self.DESCRIPTOR GRIDSTART = self.GRIDSTART GRIDEND = self.GRIDEND GRIDN = self.GRIDN qscale = numpy.linspace(GRIDSTART, GRIDEND, GRIDN) f0 = numpy.zeros_like(qscale) if MAT_FLAG == 0: # element descriptor = DESCRIPTOR for i, iqscale in enumerate(qscale): f0[i] = xraylib.FF_Rayl( xraylib.SymbolToAtomicNumber(descriptor), iqscale) elif MAT_FLAG == 1: # formula tmp = parse_formula(DESCRIPTOR) zetas = tmp["Elements"] multiplicity = tmp["n"] for j, jz in enumerate(zetas): for i, iqscale in enumerate(qscale): f0[i] += multiplicity[j] * xraylib.FF_Rayl(jz, iqscale) elif MAT_FLAG == 2: raise Exception("Not implemented") if self.DUMP_TO_FILE: with open(self.FILE_NAME, "w") as file: try: file.write("#F %s\n" % self.FILE_NAME) file.write("\n#S 1 xoppy f0 results\n") file.write("#N 2\n") file.write( "#L q=sin(theta)/lambda [A^-1] f0 [electron units]\n" ) for j in range(qscale.size): # file.write("%19.12e "%energy[j]) file.write("%19.12e %19.12e\n" % (qscale[j], f0[j])) file.close() print("File written to disk: %s \n" % self.FILE_NAME) except: raise Exception( "f0: The data could not be dumped onto the specified file!\n" ) # # return # return { "application": "xoppy", "name": "f0", "data": numpy.vstack((qscale, f0)), "labels": ["q=sin(theta)/lambda [A^-1]", "f0 [electron units]"] }
def dsigma_el(theta, energy, mw, elements, stoic): dsigma = 0 for i in range(0, len(elements)): q = xraylib.MomentTransf(energy, theta) w = xraylib.AtomicWeight(elements[i]) * stoic[i] / mw temp = dsigma_th(theta) * xraylib.FF_Rayl(elements[i], q)**2 temp = temp / xraylib.AtomicWeight(elements[i]) * w dsigma += temp dsigma *= mw return dsigma
def get_scatter_array(scatter_array, numbers, qbin): """ Generate the scattering array, which holds all the Q dependant scatter factors Parameters --------- scatter_array: NxQ array Holds the scatter factors numbers: Nd array Holds the atomic numbers qbin: float The qbin size """ n = len(scatter_array) qmax_bin = scatter_array.shape[1] for kq in range(0, qmax_bin): for tx in range(n): # note xraylib uses q = sin(th/2) # as opposed to our q = 4pi sin(th/2) scatter_array[tx, kq] = xraylib.FF_Rayl(int(numbers[tx]), kq * qbin / 4 / np.pi)
def new_bragg(cls, DESCRIPTOR="Graphite", H_MILLER_INDEX=0, K_MILLER_INDEX=0, L_MILLER_INDEX=2, TEMPERATURE_FACTOR=1.0, E_MIN=5000.0, E_MAX=15000.0, E_STEP=100.0, SHADOW_FILE="bragg.dat"): """ SHADOW preprocessor for crystals - python+xraylib version -""" # retrieve physical constants needed codata = scipy.constants.codata.physical_constants codata_e2_mc2, tmp1, tmp2 = codata["classical electron radius"] # or, hard-code them # In [179]: print("codata_e2_mc2 = %20.11e \n" % codata_e2_mc2 ) # codata_e2_mc2 = 2.81794032500e-15 fileout = SHADOW_FILE descriptor = DESCRIPTOR hh = int(H_MILLER_INDEX) kk = int(K_MILLER_INDEX) ll = int(L_MILLER_INDEX) temper = float(TEMPERATURE_FACTOR) emin = float(E_MIN) emax = float(E_MAX) estep = float(E_STEP) # # end input section, start calculations # f = open(fileout, 'wt') cryst = xraylib.Crystal_GetCrystal(descriptor) volume = cryst['volume'] #test crystal data - not needed itest = 1 if itest: if (cryst == None): sys.exit(1) print(" Unit cell dimensions are %f %f %f" % (cryst['a'], cryst['b'], cryst['c'])) print(" Unit cell angles are %f %f %f" % (cryst['alpha'], cryst['beta'], cryst['gamma'])) print(" Unit cell volume is %f A^3" % volume) print(" Atoms at:") print(" Z fraction X Y Z") for i in range(cryst['n_atom']): atom = cryst['atom'][i] print(" %3i %f %f %f %f" % (atom['Zatom'], atom['fraction'], atom['x'], atom['y'], atom['z'])) print(" ") volume = volume * 1e-8 * 1e-8 * 1e-8 # in cm^3 #flag ZincBlende f.write("%i " % 0) #1/V*electronRadius f.write("%e " % ((1e0 / volume) * (codata_e2_mc2 * 1e2))) #dspacing dspacing = xraylib.Crystal_dSpacing(cryst, hh, kk, ll) f.write("%e " % (dspacing * 1e-8)) f.write("\n") #Z's atom = cryst['atom'] f.write("%i " % atom[0]["Zatom"]) f.write("%i " % atom[-1]["Zatom"]) f.write("%e " % temper) # temperature parameter f.write("\n") ga = (1e0+0j) + cmath.exp(1j*cmath.pi*(hh+kk)) \ + cmath.exp(1j*cmath.pi*(hh+ll)) \ + cmath.exp(1j*cmath.pi*(kk+ll)) gb = ga * cmath.exp(1j * cmath.pi * 0.5 * (hh + kk + ll)) ga_bar = ga.conjugate() gb_bar = gb.conjugate() f.write("(%20.11e,%20.11e ) \n" % (ga.real, ga.imag)) f.write("(%20.11e,%20.11e ) \n" % (ga_bar.real, ga_bar.imag)) f.write("(%20.11e,%20.11e ) \n" % (gb.real, gb.imag)) f.write("(%20.11e,%20.11e ) \n" % (gb_bar.real, gb_bar.imag)) zetas = numpy.array([atom[0]["Zatom"], atom[-1]["Zatom"]]) for zeta in zetas: xx01 = 1e0 / 2e0 / dspacing xx00 = xx01 - 0.1 xx02 = xx01 + 0.1 yy00 = xraylib.FF_Rayl(int(zeta), xx00) yy01 = xraylib.FF_Rayl(int(zeta), xx01) yy02 = xraylib.FF_Rayl(int(zeta), xx02) xx = numpy.array([xx00, xx01, xx02]) yy = numpy.array([yy00, yy01, yy02]) fit = numpy.polyfit(xx, yy, 2) #print "zeta: ",zeta #print "z,xx,YY: ",zeta,xx,yy #print "fit: ",fit[::-1] # reversed coeffs #print "fit-tuple: ",(tuple(fit[::-1].tolist())) # reversed coeffs #print("fit-tuple: %e %e %e \n" % (tuple(fit[::-1].tolist())) ) # reversed coeffs f.write("%e %e %e \n" % (tuple(fit[::-1].tolist()))) # reversed coeffs npoint = int((emax - emin) / estep + 1) f.write(("%i \n") % npoint) for i in range(npoint): energy = (emin + estep * i) f1a = xraylib.Fi(int(zetas[0]), energy * 1e-3) f2a = xraylib.Fii(int(zetas[0]), energy * 1e-3) f1b = xraylib.Fi(int(zetas[1]), energy * 1e-3) f2b = xraylib.Fii(int(zetas[1]), energy * 1e-3) out = numpy.array([energy, f1a, abs(f2a), f1b, abs(f2b)]) f.write(("%20.11e %20.11e %20.11e \n %20.11e %20.11e \n") % (tuple(out.tolist()))) f.close() print("File written to disk: %s" % fileout)
def bragg(): """ SHADOW preprocessor for crystals - python+xraylib version -""" # retrieve physical constants needed codata = scipy.constants.codata.physical_constants codata_e2_mc2, tmp1, tmp2 = codata["classical electron radius"] # or, hard-code them # In [179]: print("codata_e2_mc2 = %20.11e \n" % codata_e2_mc2 ) # codata_e2_mc2 = 2.81794032500e-15 print("bragg: SHADOW preprocessor for crystals - python+xraylib version") fileout = raw_input("Name of output file : ") f = open(fileout, 'wb') print(" bragg (python) only works now for ZincBlende Cubic structures. ") print(" Valid descriptor are: ") print(" Si (alternatiovely Si_NIST, Si2) ") print(" Ge") print(" Diamond") print(" GaAs, GaSb, GaP") print(" InAs, InP, InSb") print(" SiC") descriptor = raw_input("Name of crystal descriptor : ") cryst = xraylib.Crystal_GetCrystal(descriptor) #test crystal data - not needed itest = 1 if itest: if (cryst == None): sys.exit(1) print " Unit cell dimensions are %f %f %f" % (cryst['a'],cryst['b'],cryst['c']) print " Unit cell angles are %f %f %f" % (cryst['alpha'],cryst['beta'],cryst['gamma']) volume = cryst['volume'] print " Unit cell volume is %f" % volume volume = volume*1e-8*1e-8*1e-8 # in cm^3 print " Atoms at:" print " Z fraction X Y Z" for i in range(cryst['n_atom']): atom = cryst['atom'][i] print " %3i %f %f %f %f" % (atom['Zatom'], atom['fraction'], atom['x'], atom['y'], atom['z']) print " " print("Miller indices of crystal plane of reflection.") miller = raw_input("H K L: ") miller = miller.split() hh = int(miller[0]) kk = int(miller[1]) ll = int(miller[2]) #flag ZincBlende f.write( "%i " % 0) #1/V*electronRadius f.write( "%e " % ((1e0/volume)*(codata_e2_mc2*1e2)) ) #dspacing dspacing = xraylib.Crystal_dSpacing(cryst, hh, kk, ll) f.write( "%e " % (dspacing*1e-8) ) f.write( "\n") #Z's atom = cryst['atom'] f.write( "%i " % atom[0]["Zatom"] ) f.write( "%i " % atom[7]["Zatom"] ) temper = raw_input("Temperature (Debye-Waller) factor (set 1 for default): ") temper = float(temper) f.write( "%e " % temper ) # temperature parameter f.write( "\n") ga = (1e0+0j) + cmath.exp(1j*cmath.pi*(hh+kk)) \ + cmath.exp(1j*cmath.pi*(hh+ll)) \ + cmath.exp(1j*cmath.pi*(kk+ll)) gb = ga * cmath.exp(1j*cmath.pi*0.5*(hh+kk+ll)) ga_bar = ga.conjugate() gb_bar = gb.conjugate() f.write( "(%20.11e,%20.11e ) \n" % (ga.real, ga.imag) ) f.write( "(%20.11e,%20.11e ) \n" % (ga_bar.real, ga_bar.imag) ) f.write( "(%20.11e,%20.11e ) \n" % (gb.real, gb.imag) ) f.write( "(%20.11e,%20.11e ) \n" % (gb_bar.real, gb_bar.imag) ) zetas = numpy.array([atom[0]["Zatom"],atom[7]["Zatom"]]) for zeta in zetas: xx01 = 1e0/2e0/dspacing xx00 = xx01-0.1 xx02 = xx01+0.1 yy00= xraylib.FF_Rayl(int(zeta),xx00) yy01= xraylib.FF_Rayl(int(zeta),xx01) yy02= xraylib.FF_Rayl(int(zeta),xx02) xx = numpy.array([xx00,xx01,xx02]) yy = numpy.array([yy00,yy01,yy02]) fit = numpy.polyfit(xx,yy,2) #print "zeta: ",zeta #print "z,xx,YY: ",zeta,xx,yy #print "fit: ",fit[::-1] # reversed coeffs #print "fit-tuple: ",(tuple(fit[::-1].tolist())) # reversed coeffs #print("fit-tuple: %e %e %e \n" % (tuple(fit[::-1].tolist())) ) # reversed coeffs f.write("%e %e %e \n" % (tuple(fit[::-1].tolist())) ) # reversed coeffs emin = raw_input("minimum photon energy (eV): ") emin = float(emin) emax = raw_input("maximum photon energy (eV): ") emax = float(emax) estep = raw_input("energy step (eV): ") estep = float(estep) npoint = int( (emax - emin)/estep + 1 ) f.write( ("%i \n") % npoint) for i in range(npoint): energy = (emin+estep*i) f1a = xraylib.Fi(int(zetas[0]),energy*1e-3) f2a = xraylib.Fii(int(zetas[0]),energy*1e-3) f1b = xraylib.Fi(int(zetas[1]),energy*1e-3) f2b = xraylib.Fii(int(zetas[1]),energy*1e-3) out = numpy.array([energy,f1a,abs(f2a),f1b,abs(f2b)]) f.write( ("%20.11e %20.11e %20.11e \n %20.11e %20.11e \n") % ( tuple(out.tolist()) ) ) f.close() print("File written to disk: %s" % fileout) return None
b.darwinHalfwidth(energy)) print("\n\n====================== Warning =========================") print( "Please note a small difference in FH ratio (preprocessor/xraylib): ", a.FH(energy).real / b.FH(energy).real) print("which corresponds to a difference in f0: ") print( "shadow preprocessor file uses f0_xop() for the coefficients and this is different" ) print("than xraylib.FF_Rayl() by a factor: ") ratio = 0.15946847244512372 import xraylib from dabax.dabax_xraylib import DabaxXraylib print(DabaxXraylib(file_f0='f0_xop.dat').FF_Rayl(14, 0.15946847244512372) / \ xraylib.FF_Rayl(14, 0.15946847244512372) ) print("========================================================\n\n") # print("V0: ", a.vectorK0direction(energy).components()) # print("Bh direction: ", a.vectorHdirection().components()) # print("Bh: ", a.vectorH().components()) # print("K0: ", a.vectorK0(energy).components()) # print("Kh: ", a.vectorKh(energy).components()) # print("Vh: ", a.vectorKhdirection(energy).components()) # # # from crystalpy.util.Photon import Photon # print("Difference to ThetaB uncorrected: ", # a.deviationOfIncomingPhoton(Photon(energy_in_ev=energy, direction_vector=a.vectorK0(energy)))) # # # #