def xoppy_calc_xtube_w(VOLTAGE=100.0, RIPPLE=0.0, AL_FILTER=0.0): print("Inside xoppy_calc_xtube_w. ") for file in ["tasmip_tmp.dat"]: try: os.remove(os.path.join(locations.home_bin_run(), file)) except: pass try: with open("xoppy.inp", "wt") as f: f.write("%f\n%f\n%f\n" % (VOLTAGE, RIPPLE, AL_FILTER)) if platform.system() == "Windows": command = "\"" + os.path.join(locations.home_bin(), 'tasmip.exe\" < xoppy.inp') else: command = "'" + os.path.join(locations.home_bin(), 'tasmip') + "' < xoppy.inp" print("Running command '%s' in directory: %s \n" % (command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") return "tasmip_tmp.dat" except Exception as e: raise e
def xoppy_calc_xtube_w(VOLTAGE=100.0,RIPPLE=0.0,AL_FILTER=0.0): print("Inside xoppy_calc_xtube_w. ") for file in ["tasmip_tmp.dat"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass try: with open("xoppy.inp","wt") as f: f.write("%f\n%f\n%f\n"%(VOLTAGE,RIPPLE,AL_FILTER)) if platform.system() == "Windows": command = os.path.join(locations.home_bin(),'tasmip.exe < xoppy.inp') else: command = "'" + os.path.join(locations.home_bin(), 'tasmip') + "' < xoppy.inp" print("Running command '%s' in directory: %s \n"%(command,locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") return "tasmip_tmp.dat" except Exception as e: raise e
def xoppy_calc_xtubes(ITUBE=0, VOLTAGE=30.0): print("Inside xoppy_calc_xtubes. ") for file in ["xtubes_tmp.dat"]: try: os.remove(os.path.join(locations.home_bin_run(), file)) except: pass try: with open("xoppy.inp", "wt") as f: f.write("%d\n%f\n" % (ITUBE + 1, VOLTAGE)) if platform.system() == "Windows": command = os.path.join(locations.home_bin(), 'xtubes.exe < xoppy.inp') else: command = "'" + os.path.join(locations.home_bin(), "xtubes") + "' < xoppy.inp" print("Running command '%s' in directory: %s " % (command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") return os.path.join(locations.home_bin_run(), "xtubes_tmp.dat") except Exception as e: raise e
def xoppy_calc_xtubes(ITUBE=0,VOLTAGE=30.0): print("Inside xoppy_calc_xtubes. ") for file in ["xtubes_tmp.dat"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass try: with open("xoppy.inp","wt") as f: f.write("%d\n%f\n"%(ITUBE+1,VOLTAGE)) if platform.system() == "Windows": command = os.path.join(locations.home_bin(),'xtubes.exe < xoppy.inp') else: command = "'" + os.path.join(locations.home_bin(), "xtubes") + "' < xoppy.inp" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") return os.path.join(locations.home_bin_run(), "xtubes_tmp.dat") except Exception as e: raise e
def xoppy_calc_ws(ENERGY=7.0,CUR=100.0,PERIOD=8.5,N=28.0,KX=0.0,KY=8.739999771118164,\ EMIN=1000.0,EMAX=100000.0,NEE=2000,D=30.0,XPC=0.0,YPC=0.0,XPS=2.0,YPS=2.0,NXP=10,NYP=10): print("Inside xoppy_calc_ws. ") try: with open("ws.inp", "wt") as f: f.write("inputs from xoppy \n") f.write("%f %f\n" % (ENERGY, CUR)) f.write("%f %d %f %f\n" % (PERIOD, N, KX, KY)) f.write("%f %f %d\n" % (EMIN, EMAX, NEE)) f.write("%f %f %f %f %f %d %d\n" % (D, XPC, YPC, XPS, YPS, NXP, NYP)) f.write("%d \n" % (4)) if platform.system() == "Windows": command = os.path.join(locations.home_bin(), 'ws.exe') else: command = "'" + os.path.join(locations.home_bin(), 'ws') + "'" print("Running command '%s' in directory: %s \n" % (command, locations.home_bin_run())) # TODO try to capture the text output of the external code os.system(command) # write spec file txt = open("ws.out").readlines() outFile = os.path.join(locations.home_bin_run(), "ws.spec") f = open(outFile, "w") f.write("#F ws.spec\n") f.write("\n") f.write("#S 1 ws results\n") f.write("#N 4\n") f.write( "#L Energy(eV) Flux(photons/s/0.1%bw) Spectral power(W/eV) Cumulated power(W)\n" ) cum = 0.0 estep = (EMAX - EMIN) / (NEE - 1) for i in txt: tmp = i.strip(" ") if tmp[0].isdigit(): tmp1 = numpy.fromstring(tmp, dtype=float, sep=' ') cum += tmp1[1] * codata.e * 1e3 f.write( "%f %g %f %f \n" % (tmp1[0], tmp1[1], tmp1[1] * codata.e * 1e3, cum * estep)) else: f.write("#UD " + tmp) f.close() print("File written to disk: ws.spec") # print output file # for line in txt: # print(line, end="") print("Results written to file: %s" % os.path.join(locations.home_bin_run(), 'ws.out')) return outFile except Exception as e: raise e
def xoppy_calc_xpowder_fml(self): for file in ["xpowder_fml.par","xpowder_fml.ref","xpowder_fml.out"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass with open("xoppy.inp", "wt") as f: f.write("%s\n"% (self.FILE)) f.write("%s\n"%(self.TITLE)) f.write("%g\n"%(self.LAMBDA)) f.write("%s\n"%(self.JOB)) f.write("%g\n"%(self.U)) f.write("%g\n"%(self.V)) f.write("%g\n"%(self.W)) f.write("%g\n"%(self.X)) f.write("%g\n"%(self.LS)) f.write("%g\n"%(self.THMIN)) f.write("%g\n"%(self.STEP)) f.write("%s\n"%(self.THMAX)) if platform.system() == "Windows": command = "\"" + os.path.join(locations.home_bin(),'xpowder_fml.exe\" < xoppy.inp') else: command = "'" + os.path.join(locations.home_bin(), 'xpowder_fml') + "' < xoppy.inp" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") print("Files written to disk:\n xpowder_fml.par (text output)\n xpowder_fml.ref (reflections)\n xpowder_fml.out (diffractogram)",) # # data = numpy.loadtxt("xpowder_fml.out",skiprows=3).T #send exchange calculated_data = DataExchangeObject("XOPPY", self.get_data_exchange_widget_name()) try: calculated_data.add_content("xoppy_data", numpy.loadtxt("xpowder_fml.out", skiprows=3)) calculated_data.add_content("plot_x_col",0) calculated_data.add_content("plot_y_col",-1) except: pass try: calculated_data.add_content("labels",["TwoTheta[Deg]","Intensity[a.u.]"]) except: pass try: with open("xpowder_fml.par") as f: info = f.readlines() info = [line[:-1] for line in info] # remove "\n" calculated_data.add_content("info",info) except: pass return calculated_data
def xoppy_calc_xpowder_fml(self): for file in ["xpowder_fml.par","xpowder_fml.ref","xpowder_fml.out"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass with open("xoppy.inp", "wt") as f: f.write("%s\n"% (self.FILE)) f.write("%s\n"%(self.TITLE)) f.write("%g\n"%(self.LAMBDA)) f.write("%s\n"%(self.JOB)) f.write("%g\n"%(self.U)) f.write("%g\n"%(self.V)) f.write("%g\n"%(self.W)) f.write("%g\n"%(self.X)) f.write("%g\n"%(self.LS)) f.write("%g\n"%(self.THMIN)) f.write("%g\n"%(self.STEP)) f.write("%s\n"%(self.THMAX)) if platform.system() == "Windows": command = os.path.join(locations.home_bin(),'xpowder_fml.exe < xoppy.inp') else: command = "'" + os.path.join(locations.home_bin(), 'xpowder_fml') + "' < xoppy.inp" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") print("Files written to disk:\n xpowder_fml.par (text output)\n xpowder_fml.ref (reflections)\n xpowder_fml.out (diffractogram)",) # # data = numpy.loadtxt("xpowder_fml.out",skiprows=3).T #send exchange calculated_data = DataExchangeObject("XOPPY", self.get_data_exchange_widget_name()) try: calculated_data.add_content("xoppy_data", numpy.loadtxt("xpowder_fml.out", skiprows=3)) calculated_data.add_content("plot_x_col",0) calculated_data.add_content("plot_y_col",-1) except: pass try: calculated_data.add_content("labels",["TwoTheta[Deg]","Intensity[a.u.]"]) except: pass try: with open("xpowder_fml.par") as f: info = f.readlines() info = [line[:-1] for line in info] # remove "\n" calculated_data.add_content("info",info) except: pass return calculated_data
def xoppy_calc_ws(ENERGY=7.0,CUR=100.0,PERIOD=8.5,N=28.0,KX=0.0,KY=8.739999771118164,\ EMIN=1000.0,EMAX=100000.0,NEE=2000,D=30.0,XPC=0.0,YPC=0.0,XPS=2.0,YPS=2.0,NXP=10,NYP=10): print("Inside xoppy_calc_ws. ") try: with open("ws.inp","wt") as f: f.write("inputs from xoppy \n") f.write("%f %f\n"%(ENERGY,CUR)) f.write("%f %d %f %f\n"%(PERIOD,N,KX,KY)) f.write("%f %f %d\n"%(EMIN,EMAX,NEE)) f.write("%f %f %f %f %f %d %d\n"%(D,XPC,YPC,XPS,YPS,NXP,NYP)) f.write("%d \n"%(4)) command = os.path.join(locations.home_bin(),'ws') print("Running command '%s' in directory: %s \n"%(command, locations.home_bin_run())) # TODO try to capture the text output of the external code print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") # write spec file txt = open("ws.out").readlines() outFile = os.path.join(locations.home_bin_run(), "ws.spec") f = open(outFile,"w") f.write("#F ws.spec\n") f.write("\n") f.write("#S 1 ws results\n") f.write("#N 3\n") f.write("#L Energy(eV) Flux(photons/s/0.1%bw) Spectral power(W/eV)\n") for i in txt: tmp = i.strip(" ") if tmp[0].isdigit(): tmp1 = numpy.fromstring(tmp, dtype=float, sep=' ') f.write("%f %f %f \n"%(tmp1[0],tmp1[1],tmp1[1]*codata.e*1e3)) else: f.write("#UD "+tmp) f.close() print("File written to disk: ws.spec") # print output file for line in txt: print(line, end="") return outFile except Exception as e: raise e
def xoppy_calc_xcrystal(self): CRYSTAL_MATERIAL = self.CRYSTAL_MATERIAL MILLER_INDEX_H = self.MILLER_INDEX_H MILLER_INDEX_K = self.MILLER_INDEX_K MILLER_INDEX_L = self.MILLER_INDEX_L TEMPER = self.TEMPER MOSAIC = self.MOSAIC GEOMETRY = self.GEOMETRY SCAN = self.SCAN UNIT = self.UNIT SCANFROM = self.SCANFROM SCANTO = self.SCANTO SCANPOINTS = self.SCANPOINTS ENERGY = self.ENERGY ASYMMETRY_ANGLE = self.ASYMMETRY_ANGLE THICKNESS = self.THICKNESS MOSAIC_FWHM = self.MOSAIC_FWHM RSAG = self.RSAG RMER = self.RMER ANISOTROPY = self.ANISOTROPY POISSON = self.POISSON CUT = self.CUT FILECOMPLIANCE = self.FILECOMPLIANCE for file in ["diff_pat.dat","diff_pat.gle","diff_pat.par","diff_pat.xop","xcrystal.bra"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass if (GEOMETRY == 1) or (GEOMETRY == 3): if ASYMMETRY_ANGLE == 0.0: print("xoppy_calc_xcrystal: WARNING: In xcrystal the asymmetry angle is the angle between Bragg planes and crystal surface,"+ "in BOTH Bragg and Laue geometries.") descriptor = Crystal_GetCrystalsList()[CRYSTAL_MATERIAL] if SCAN == 3: # energy scan emin = SCANFROM - 1 emax = SCANTO + 1 else: emin = ENERGY - 100.0 emax = ENERGY + 100.0 print("Using crystal descriptor: ",descriptor) bragg_dictionary = bragg_calc(descriptor=descriptor, hh=MILLER_INDEX_H,kk=MILLER_INDEX_K,ll=MILLER_INDEX_L, temper=float(TEMPER), emin=emin,emax=emax,estep=5.0,fileout="xcrystal.bra") with open("xoppy.inp", "wt") as f: f.write("xcrystal.bra\n") f.write("%d\n"%MOSAIC) f.write("%d\n"%GEOMETRY) if MOSAIC == 1: f.write("%g\n"%MOSAIC_FWHM) f.write("%g\n"%THICKNESS) else: f.write("%g\n"%THICKNESS) f.write("%g\n"%ASYMMETRY_ANGLE) scan_flag = 1 + SCAN f.write("%d\n"%scan_flag) f.write("%19.9f\n"%ENERGY) if scan_flag <= 3: f.write("%d\n"%UNIT) f.write("%g\n"%SCANFROM) f.write("%g\n"%SCANTO) f.write("%d\n"%SCANPOINTS) if MOSAIC > 1: # bent f.write("%g\n"%RSAG) f.write("%g\n"%RMER) f.write("0\n") if ( (descriptor == "Si") or (descriptor == "Si2") or (descriptor == "Si_NIST") or (descriptor == "Ge") or descriptor == "Diamond"): pass else: # not Si,Ge,Diamond if ((ANISOTROPY == 1) or (ANISOTROPY == 2)): raise Exception("Anisotropy data not available for this crystal. Either use isotropic or use external compliance file. Please change and run again'") f.write("%d\n"%ANISOTROPY) if ANISOTROPY == 0: f.write("%g\n"%POISSON) elif ANISOTROPY == 1: f.write("%d\n"%CRYSTAL_MATERIAL) f.write("%g\n"%ASYMMETRY_ANGLE) f.write("%d\n"%MILLER_INDEX_H) f.write("%d\n"%MILLER_INDEX_K) f.write("%d\n"%MILLER_INDEX_L) elif ANISOTROPY == 2: f.write("%d\n"%CRYSTAL_MATERIAL) f.write("%g\n"%ASYMMETRY_ANGLE) # TODO: check syntax for CUT: Cut syntax is: valong_X valong_Y valong_Z ; vnorm_X vnorm_Y vnorm_Z ; vperp_x vperp_Y vperp_Z f.write("%s\n"%CUT.split(";")[0]) f.write("%s\n"%CUT.split(";")[1]) f.write("%s\n"%CUT.split(";")[2]) elif ANISOTROPY == 3: f.write("%s\n"%FILECOMPLIANCE) command = "'" + os.path.join(locations.home_bin(), 'diff_pat') + "' < xoppy.inp" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") #show calculated parameters in standard output txt_info = open("diff_pat.par").read() for line in txt_info: print(line,end="") calculated_data = DataExchangeObject("XOPPY", self.get_data_exchange_widget_name()) try: calculated_data.add_content("xoppy_data", numpy.loadtxt("diff_pat.dat", skiprows=5)) calculated_data.add_content("plot_x_col",0) calculated_data.add_content("plot_y_col",-1) calculated_data.add_content("units_to_degrees", self.get_units_to_degrees()) except Exception as e: raise Exception("Error loading diff_pat.dat :" + str(e)) try: calculated_data.add_content("labels", ["Th-ThB{in} [" + self.unit_combo.itemText(self.UNIT) + "]", "Th-ThB{out} [" + self.unit_combo.itemText(self.UNIT) + "]", "phase_p[rad]", "phase_s[rad]","Circ Polariz", "p-polarized reflectivity", "s-polarized reflectivity"]) except: pass try: with open("diff_pat.par") as f: info = f.readlines() calculated_data.add_content("info",info) except: pass return calculated_data
def xoppy_calc_mlayer(self): # copy the variable locally, so no more use of self. MODE = self.MODE SCAN = self.SCAN F12_FLAG = self.F12_FLAG SUBSTRATE = self.SUBSTRATE ODD_MATERIAL = self.ODD_MATERIAL EVEN_MATERIAL = self.EVEN_MATERIAL ENERGY = self.ENERGY THETA = self.THETA SCAN_STEP = self.SCAN_STEP NPOINTS = self.NPOINTS ODD_THICKNESS = self.ODD_THICKNESS EVEN_THICKNESS = self.EVEN_THICKNESS NLAYERS = self.NLAYERS FILE = self.FILE for file in ["mlayer.inp", "mlayer.par", "mlayer.f12"]: try: os.remove(os.path.join(locations.home_bin_run(), file)) except: pass # # write input file for Fortran mlayer: mlayer.inp # f = open('mlayer.inp', 'w') if SCAN == 0 and MODE == 0: a0 = 1 if SCAN == 1 and MODE == 0: a0 = 2 if SCAN == 0 and MODE == 1: a0 = 3 if SCAN == 1 and MODE == 1: a0 = 4 f.write("%d \n" % a0) f.write("N\n") f.write("%g\n" % (codata.h * codata.c / codata.e * 1e10 / ENERGY)) f.write("%g\n" % THETA) # if SCAN == 0: f.write("%g\n" % SCAN_STEP) a2 = codata.h * codata.c / codata.e * 1e10 / ENERGY a3 = codata.h * codata.c / codata.e * 1e10 / (ENERGY + SCAN_STEP) a4 = a3 - a2 if SCAN != 0: f.write("%g\n" % a4) # f.write("%d\n" % NPOINTS) if MODE == 0: f.write("%d\n" % NLAYERS) if MODE == 0: if a0 != 5: f.write("%g %g \n" % (ODD_THICKNESS, EVEN_THICKNESS)) else: for i in range(NLAYERS): f.write("%g %g \n" % (ODD_THICKNESS, EVEN_THICKNESS)) if MODE != 0: f1 = open(FILE, 'r') a5 = f1.read() f1.close() if MODE != 0: print("Number of layers in %s file is %d " % (FILE, NLAYERS)) f.write("%d\n" % NLAYERS) f.write(a5) f.write("mlayer.par\n") f.write("mlayer.dat\n") f.write("6\n") f.close() print('File written to disk: mlayer.inp') # # create f12 file # if F12_FLAG == 0: energy = numpy.arange(0, 500) elefactor = numpy.log10(10000.0 / 30.0) / 300.0 energy = 10.0 * 10**(energy * elefactor) f12_s = f1f2_calc(SUBSTRATE, energy) f12_e = f1f2_calc(EVEN_MATERIAL, energy) f12_o = f1f2_calc(ODD_MATERIAL, energy) f = open("mlayer.f12", 'w') f.write( '; File created by xoppy for materials [substrate=%s,even=%s,odd=%s]: \n' % (SUBSTRATE, EVEN_MATERIAL, ODD_MATERIAL)) f.write('; Atomic masses: \n') f.write( "%g %g %g \n" % (xraylib.AtomicWeight(xraylib.SymbolToAtomicNumber(SUBSTRATE)), xraylib.AtomicWeight( xraylib.SymbolToAtomicNumber(EVEN_MATERIAL)), xraylib.AtomicWeight( xraylib.SymbolToAtomicNumber(ODD_MATERIAL)))) f.write('; Densities: \n') f.write("%g %g %g \n" % (xraylib.ElementDensity( xraylib.SymbolToAtomicNumber(SUBSTRATE)), xraylib.ElementDensity( xraylib.SymbolToAtomicNumber(EVEN_MATERIAL)), xraylib.ElementDensity( xraylib.SymbolToAtomicNumber(ODD_MATERIAL)))) f.write('; Number of energy points: \n') f.write("%d\n" % (energy.size)) f.write( '; For each energy point, energy[eV], f1[substrate], f2[substrate], f1[even], f2[even], f1[odd], f2[odd]: \n' ) for i in range(energy.size): f.write("%g %g %g %g %g %g %g \n" % (energy[i], f12_s[0, i], f12_s[1, i], f12_e[0, i], f12_e[1, i], f12_o[0, i], f12_o[1, i])) f.close() print('File written to disk: mlayer.f12') # # run external program mlayer # if platform.system() == "Windows": command = os.path.join(locations.home_bin(), 'mlayer.exe < mlayer.inp') else: command = "'" + os.path.join(locations.home_bin(), 'mlayer') + "' < mlayer.inp" print("Running command '%s' in directory: %s " % (command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") #send exchange calculated_data = DataExchangeObject( "XOPPY", self.get_data_exchange_widget_name()) try: if SCAN == 0: calculated_data.add_content("xoppy_data", numpy.loadtxt("mlayer.dat")) calculated_data.add_content("plot_x_col", 0) calculated_data.add_content("plot_y_col", 3) elif SCAN == 1: # internal scan is in wavelength. Add a column with energy aa = numpy.loadtxt("mlayer.dat") photon_energy = (codata.h * codata.c / codata.e * 1e10) / aa[:, 0] bb = numpy.zeros((aa.shape[0], 1 + aa.shape[1])) bb[:, 0] = photon_energy bb[:, 1:] = aa calculated_data.add_content("xoppy_data", bb) calculated_data.add_content("plot_x_col", 0) calculated_data.add_content("plot_y_col", 4) calculated_data.add_content("units_to_degrees", 1.0) except: pass try: info = "ML %s(%3.2f A):%s(%3.2f A) %d pairs; E=%5.3f eV" % ( ODD_MATERIAL, ODD_THICKNESS, EVEN_MATERIAL, EVEN_THICKNESS, NLAYERS, ENERGY) calculated_data.add_content("info", info) except: pass return calculated_data
def xoppy_calc_xcrystal(self): CRYSTAL_MATERIAL = self.CRYSTAL_MATERIAL MILLER_INDEX_H = self.MILLER_INDEX_H MILLER_INDEX_K = self.MILLER_INDEX_K MILLER_INDEX_L = self.MILLER_INDEX_L TEMPER = self.TEMPER MOSAIC = self.MOSAIC GEOMETRY = self.GEOMETRY SCAN = self.SCAN UNIT = self.UNIT SCANFROM = self.SCANFROM SCANTO = self.SCANTO SCANPOINTS = self.SCANPOINTS ENERGY = self.ENERGY ASYMMETRY_ANGLE = self.ASYMMETRY_ANGLE THICKNESS = self.THICKNESS MOSAIC_FWHM = self.MOSAIC_FWHM RSAG = self.RSAG RMER = self.RMER ANISOTROPY = self.ANISOTROPY POISSON = self.POISSON CUT = self.CUT FILECOMPLIANCE = self.FILECOMPLIANCE for file in ["diff_pat.dat","diff_pat.gle","diff_pat.par","diff_pat.xop","xcrystal.bra"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass if (GEOMETRY == 1) or (GEOMETRY == 3): if ASYMMETRY_ANGLE == 0.0: print("xoppy_calc_xcrystal: WARNING: In xcrystal the asymmetry angle is the angle between Bragg planes and crystal surface,"+ "in BOTH Bragg and Laue geometries.") descriptor = Crystal_GetCrystalsList()[CRYSTAL_MATERIAL] if SCAN == 3: # energy scan emin = SCANFROM - 1 emax = SCANTO + 1 else: emin = ENERGY - 100.0 emax = ENERGY + 100.0 print("Using crystal descriptor: ",descriptor) bragg_dictionary = bragg_calc(descriptor=descriptor, hh=MILLER_INDEX_H,kk=MILLER_INDEX_K,ll=MILLER_INDEX_L, temper=float(TEMPER), emin=emin,emax=emax,estep=5.0,fileout="xcrystal.bra") with open("xoppy.inp", "wt") as f: f.write("xcrystal.bra\n") f.write("%d\n"%MOSAIC) f.write("%d\n"%GEOMETRY) if MOSAIC == 1: f.write("%g\n"%MOSAIC_FWHM) f.write("%g\n"%THICKNESS) else: f.write("%g\n"%THICKNESS) f.write("%g\n"%ASYMMETRY_ANGLE) scan_flag = 1 + SCAN f.write("%d\n"%scan_flag) f.write("%19.9f\n"%ENERGY) if scan_flag <= 3: f.write("%d\n"%UNIT) f.write("%g\n"%SCANFROM) f.write("%g\n"%SCANTO) f.write("%d\n"%SCANPOINTS) if MOSAIC > 1: # bent f.write("%g\n"%RSAG) f.write("%g\n"%RMER) f.write("0\n") if ( (descriptor == "Si") or (descriptor == "Si2") or (descriptor == "Si_NIST") or (descriptor == "Ge") or descriptor == "Diamond"): pass else: # not Si,Ge,Diamond if ((ANISOTROPY == 1) or (ANISOTROPY == 2)): raise Exception("Anisotropy data not available for this crystal. Either use isotropic or use external compliance file. Please change and run again'") f.write("%d\n"%ANISOTROPY) if ANISOTROPY == 0: f.write("%g\n"%POISSON) elif ANISOTROPY == 1: f.write("%d\n"%CRYSTAL_MATERIAL) f.write("%g\n"%ASYMMETRY_ANGLE) f.write("%d\n"%MILLER_INDEX_H) f.write("%d\n"%MILLER_INDEX_K) f.write("%d\n"%MILLER_INDEX_L) elif ANISOTROPY == 2: f.write("%d\n"%CRYSTAL_MATERIAL) f.write("%g\n"%ASYMMETRY_ANGLE) # TODO: check syntax for CUT: Cut syntax is: valong_X valong_Y valong_Z ; vnorm_X vnorm_Y vnorm_Z ; vperp_x vperp_Y vperp_Z f.write("%s\n"%CUT.split(";")[0]) f.write("%s\n"%CUT.split(";")[1]) f.write("%s\n"%CUT.split(";")[2]) elif ANISOTROPY == 3: f.write("%s\n"%FILECOMPLIANCE) command = os.path.join(locations.home_bin(), 'diff_pat') + " < xoppy.inp" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") #show calculated parameters in standard output txt_info = open("diff_pat.par").read() for line in txt_info: print(line,end="") calculated_data = DataExchangeObject("XOPPY", self.get_data_exchange_widget_name()) try: calculated_data.add_content("xoppy_data", numpy.loadtxt("diff_pat.dat", skiprows=5)) calculated_data.add_content("plot_x_col",0) calculated_data.add_content("plot_y_col",-1) calculated_data.add_content("units_to_degrees", self.get_units_to_degrees()) except Exception as e: raise Exception("Error loading diff_pat.dat :" + str(e)) try: calculated_data.add_content("labels", ["Th-ThB{in} [" + self.unit_combo.itemText(self.UNIT) + "]", "Th-ThB{out} [" + self.unit_combo.itemText(self.UNIT) + "]", "phase_p[rad]", "phase_s[rad]","Circ Polariz", "p-polarized reflectivity", "s-polarized reflectivity"]) except: pass try: with open("diff_pat.par") as f: info = f.readlines() calculated_data.add_content("info",info) except: pass return calculated_data
def xoppy_calc_xtc(self): for file in ["tc.inp", "tc.out"]: try: os.remove(os.path.join(locations.home_bin_run(), file)) except: pass with open("tc.inp", "wt") as f: f.write("TS called from xoppy\n") f.write("%10.3f %10.2f %10.6f %s\n" % (self.ENERGY, self.CURRENT, self.ENERGY_SPREAD, "Ring-Energy(GeV) Current(mA) Beam-Energy-Spread")) f.write("%10.4f %10.4f %10.4f %10.4f %s\n" % (self.SIGX, self.SIGY, self.SIGX1, self.SIGY1, "Sx(mm) Sy(mm) Sx1(mrad) Sy1(mrad)")) f.write("%10.3f %d %s\n" % (self.PERIOD, self.NP, "Period(cm) N")) f.write("%10.1f %10.1f %d %s\n" % (self.EMIN, self.EMAX, self.N, "Emin Emax Ne")) f.write("%d %d %d %s\n" % (self.HARMONIC_FROM, self.HARMONIC_TO, self.HARMONIC_STEP, "Hmin Hmax Hstep")) f.write("%d %d %d %d %s\n" % (self.HELICAL, self.METHOD, 1, self.NEKS, "Helical Method Print_K Neks")) f.write("foreground\n") if platform.system() == "Windows": command = os.path.join(locations.home_bin(), 'tc.exe') else: command = "'" + os.path.join(locations.home_bin(), 'tc') + "'" print("Running command '%s' in directory: %s " % (command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("Output file: %s" % ("tc.out")) print("\n--------------------------------------------------------\n") # # parse result files to exchange object # with open("tc.out", "r") as f: lines = f.readlines() # print output file # for line in lines: # print(line, end="") # remove returns lines = [line[:-1] for line in lines] harmonics_data = [] # separate numerical data from text floatlist = [] harmoniclist = [] txtlist = [] for line in lines: try: tmp = line.strip() if tmp.startswith("Harmonic"): harmonic_number = int(tmp.split("Harmonic")[1].strip()) if harmonic_number != self.HARMONIC_FROM: harmonics_data[-1][1] = harmoniclist harmoniclist = [] harmonics_data.append([harmonic_number, None]) tmp = float(line.strip()[0]) floatlist.append(line) harmoniclist.append(line) except: txtlist.append(line) harmonics_data[-1][1] = harmoniclist data = numpy.loadtxt(floatlist) for index in range(0, len(harmonics_data)): # print (harmonics_data[index][0], harmonics_data[index][1]) harmonics_data[index][1] = numpy.loadtxt(harmonics_data[index][1]) #send exchange calculated_data = DataExchangeObject( "XOPPY", self.get_data_exchange_widget_name()) try: calculated_data.add_content("xoppy_data", data) calculated_data.add_content("xoppy_data_harmonics", harmonics_data) calculated_data.add_content("plot_x_col", 1) calculated_data.add_content("plot_y_col", 2) except: pass try: calculated_data.add_content("labels", [ "Energy (eV) without emittance", "Energy (eV) with emittance", "Brilliance (ph/s/mrad^2/mm^2/0.1%bw)", "Ky", "Total Power (W)", "Power density (W/mr^2)" ]) except: pass try: calculated_data.add_content("info", txtlist) except: pass return calculated_data
def xoppy_calc_xxcom( NAME="Pyrex Glass", SUBSTANCE=3, DESCRIPTION="SiO2:B2O3:Na2O:Al2O3:K2O", FRACTION="0.807:0.129:0.038:0.022:0.004", GRID=1, GRIDINPUT=0, GRIDDATA="0.0804:0.2790:0.6616:1.3685:2.7541", ELEMENTOUTPUT=0, ): print("Inside xoppy_calc_xxcom. ") try: with open("xoppy.inp", "wt") as f: f.write(os.path.join(locations.home_data(), "xcom") + os.sep + "\n") f.write(NAME + "\n") f.write("%d\n" % (1 + SUBSTANCE)) if (1 + SUBSTANCE) != 4: f.write(DESCRIPTION + "\n") if (1 + SUBSTANCE) <= 2: f.write("%d\n" % (1 + ELEMENTOUTPUT)) else: nn = DESCRIPTION.split(":") mm = FRACTION.split(":") f.write("%d\n" % (len(nn))) for i in range(len(nn)): f.write(nn[i] + "\n") f.write(mm[i] + "\n") f.write("1\n") f.write("%d\n" % (1 + GRID)) if (1 + GRID) != 1: f.write("%d\n" % (1 + GRIDINPUT)) if (1 + GRIDINPUT) == 1: nn = GRIDDATA.split(":") f.write("%d\n" % (len(nn))) for i in nn: f.write(i + "\n") if (1 + GRID) != 1: f.write("N\n") f.write("xcom.out\n") f.write("1\n") f.close() command = os.path.join(locations.home_bin(), "xcom") + " < xoppy.inp" print("Running command '%s' in directory: %s " % (command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") # write spec file if (1 + SUBSTANCE) <= 2: if (1 + ELEMENTOUTPUT) == 1: titles = ( "Photon Energy [Mev] Coherent scat [b/atom] " "Incoherent scat [b/atom] Photoel abs [b/atom] " "Pair prod in nucl field [b/atom] Pair prod in elec field [b/atom] " "Tot atten with coh scat [b/atom] Tot atten w/o coh scat [b/atom]" ) elif (1 + ELEMENTOUTPUT) == 2: titles = ( "Photon Energy [Mev] Coherent scat [b/atom] " "Incoherent scat [b/atom] Photoel abs [b/atom] " "Pair prod in nucl field [b/atom] Pair prod in elec field [b/atom] " "Tot atten with coh scat [cm2/g] Tot atten w/o coh scat [cm2/g]" ) elif (1 + ELEMENTOUTPUT) == 3: titles = ( "Photon Energy [Mev] Coherent scat [cm2/g] " "Incoherent scat [cm2/g] Photoel abs [cm2/g] " "Pair prod in nucl field [cm2/g] Pair prod in elec field [cm2/g] " "Tot atten with coh scat [cm2/g] Tot atten w/o coh scat [cm2/g]" ) else: titles = ( "Photon Energy [Mev] Coherent scat [cm2/g] " "Incoherent scat [cm2/g] Photoel abs [cm2/g] " "Pair prod in nucl field [cm2/g] Pair prod in elec field [cm2/g] " "Tot atten with coh scat [cm2/g] Tot atten w/o coh scat [cm2/g]" ) else: titles = ( "Photon Energy [Mev] Coherent scat [cm2/g] " "Incoherent scat [cm2/g] Photoel abs [cm2/g] " "Pair prod in nucl field [cm2/g] Pair prod in elec field [cm2/g] " "Tot atten with coh scat [cm2/g] Tot atten w/o coh scat [cm2/g]" ) txt = open("xcom.out").readlines() # copy to standard output for line in txt: print(line, end="") outFile = "xcom.spec" f = open(outFile, "w") f.write("#F xcom.spec\n") f.write("\n") f.write("#S 1 xcom results\n") f.write("#N 8\n") f.write("#L " + titles + "\n") for i in txt: tmp = i.strip(" ") if tmp[0].isdigit(): f.write(tmp) else: f.write("#UD " + tmp) f.close() print("File written to disk: xcom.spec") return outFile except Exception as e: raise e
def xoppy_calc_xxcom(NAME="Pyrex Glass",SUBSTANCE=3,DESCRIPTION="SiO2:B2O3:Na2O:Al2O3:K2O",\ FRACTION="0.807:0.129:0.038:0.022:0.004",GRID=1,GRIDINPUT=0,\ GRIDDATA="0.0804:0.2790:0.6616:1.3685:2.7541",ELEMENTOUTPUT=0): print("Inside xoppy_calc_xxcom. ") try: with open("xoppy.inp","wt") as f: f.write(os.path.join(locations.home_data(), 'xcom')+ os.sep + "\n" ) f.write( NAME+"\n" ) f.write("%d\n"%(1+SUBSTANCE)) if (1+SUBSTANCE) != 4: f.write( DESCRIPTION+"\n") if (1+SUBSTANCE) <= 2: f.write("%d\n"%(1+ELEMENTOUTPUT)) else: nn = DESCRIPTION.split(":") mm = FRACTION.split(":") f.write("%d\n"%( len(nn))) for i in range(len(nn)): f.write(nn[i]+"\n") f.write(mm[i]+"\n") f.write("1\n") f.write("%d\n"%(1+GRID)) if (1+GRID) != 1: f.write("%d\n"%(1+GRIDINPUT)) if (1+GRIDINPUT) == 1: nn = GRIDDATA.split(":") f.write("%d\n"%( len(nn))) for i in nn: f.write(i+"\n") if (1+GRID) != 1: f.write("N\n") f.write("xcom.out\n") f.write("1\n") f.close() command = "'" + os.path.join(locations.home_bin(),'xcom') + "' < xoppy.inp" print("Running command '%s' in directory: %s "%(command,locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") # write spec file if (1+SUBSTANCE) <= 2: if (1+ELEMENTOUTPUT) == 1: titles = "Photon Energy [Mev] Coherent scat [b/atom] " \ "Incoherent scat [b/atom] Photoel abs [b/atom] " \ "Pair prod in nucl field [b/atom] Pair prod in elec field [b/atom] " \ "Tot atten with coh scat [b/atom] Tot atten w/o coh scat [b/atom]" elif (1+ELEMENTOUTPUT) == 2: titles = "Photon Energy [Mev] Coherent scat [b/atom] " \ "Incoherent scat [b/atom] Photoel abs [b/atom] " \ "Pair prod in nucl field [b/atom] Pair prod in elec field [b/atom] " \ "Tot atten with coh scat [cm2/g] Tot atten w/o coh scat [cm2/g]" elif (1+ELEMENTOUTPUT) == 3: titles = "Photon Energy [Mev] Coherent scat [cm2/g] " \ "Incoherent scat [cm2/g] Photoel abs [cm2/g] " \ "Pair prod in nucl field [cm2/g] Pair prod in elec field [cm2/g] " \ "Tot atten with coh scat [cm2/g] Tot atten w/o coh scat [cm2/g]" else: titles = "Photon Energy [Mev] Coherent scat [cm2/g] " \ "Incoherent scat [cm2/g] Photoel abs [cm2/g] " \ "Pair prod in nucl field [cm2/g] Pair prod in elec field [cm2/g] " \ "Tot atten with coh scat [cm2/g] Tot atten w/o coh scat [cm2/g]" else: titles = "Photon Energy [Mev] Coherent scat [cm2/g] " \ "Incoherent scat [cm2/g] Photoel abs [cm2/g] " \ "Pair prod in nucl field [cm2/g] Pair prod in elec field [cm2/g] " \ "Tot atten with coh scat [cm2/g] Tot atten w/o coh scat [cm2/g]" txt = open("xcom.out").readlines() # copy to standard output for line in txt: print(line,end="") outFile = "xcom.spec" f = open(outFile, "w") f.write("#F xcom.spec\n") f.write("\n") f.write("#S 1 xcom results\n") f.write("#N 8\n") f.write("#L "+titles+"\n") for i in txt: tmp = i.strip(" ") if tmp[0].isdigit(): f.write(tmp) else: f.write("#UD "+tmp) f.close() print("File written to disk: xcom.spec") return outFile except Exception as e: raise e
def xoppy_calc_xtcap(self): for file in ["tcap.inp","tcap.out","tcap.log"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass with open("tcap.inp", "wt") as f: f.write("TCAP called from xoppy\n") f.write("%10.3f %10.2f %10.6f %s\n"%(self.ENERGY,self.CURRENT,self.ENERGY_SPREAD,"Ring-Energy(GeV) Current(mA) Beam-Energy-Spread")) f.write("%10.4f %10.4f %10.4f %10.4f %s\n"%(self.SIGX,self.SIGY,self.SIGX1,self.SIGY1,"Sx(mm) Sy(mm) Sx1(mrad) Sy1(mrad)")) f.write("%10.3f %d %s\n"%(self.PERIOD,self.NP,"Period(cm) N")) f.write("%10.1f %10.1f %d %s\n"%(self.EMIN,self.EMAX,self.N,"Emin Emax Ne")) f.write("%10.3f %10.3f %10.3f %10.3f %10.3f %d %d %s\n"%(self.DISTANCE,self.XPC,self.YPC,self.XPS,self.YPS,10,10,"d xpc ypc xps yps nxp nyp")) f.write("%d %d %d %d %s\n"%(self.HARMONIC_FROM,self.HARMONIC_TO,self.HARMONIC_STEP,self.HRED,"Hmin Hmax Hstep Hreduction")) f.write("%d %d %d %d %d %s\n"%(self.HELICAL,self.METHOD,1,self.NEKS,self.BSL,"Helical Method Print_K Neks Bsl-Subtr ")) f.write("foreground\n") if platform.system() == "Windows": command = "\"" + os.path.join(locations.home_bin(),'tcap.exe') + "\"" else: command = "'" + os.path.join(locations.home_bin(), 'tcap') + "'" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") # os.system(command) # # catch the optut and write the output to a log file as well as print it. # retvalue = os.popen(command).read() print(retvalue) with open("tcap.log", "wt") as f: f.write(retvalue) print("Output file: '%s/tcap.out'"%(os.getcwd()) ) print("\n--------------------------------------------------------\n") # # parse result files to exchange object # with open("tcap.out","r") as f: lines = f.readlines() # print output file # for line in lines: # print(line, end="") # remove returns lines = [line[:-1] for line in lines] harmonics_data = [] # separate numerical data from text floatlist = [] harmoniclist = [] txtlist = [] for line in lines: try: tmp = line.strip() if tmp.startswith("Harmonic"): # harmonic_number = int(tmp.split("Harmonic")[1].strip()) harmonic_number = int(tmp.split()[1]) if harmonic_number != self.HARMONIC_FROM: harmonics_data[-1][1] = harmoniclist harmoniclist = [] harmonics_data.append([harmonic_number, None]) tmp = float(line.strip()[0]) floatlist.append(line) harmoniclist.append(line) except: txtlist.append(line) harmonics_data[-1][1] = harmoniclist data = numpy.loadtxt(floatlist) for index in range(0, len(harmonics_data)): # print (harmonics_data[index][0], harmonics_data[index][1]) harmonics_data[index][1] = numpy.loadtxt(harmonics_data[index][1]) #send exchange calculated_data = DataExchangeObject("XOPPY", self.get_data_exchange_widget_name()) try: calculated_data.add_content("xoppy_data", data) calculated_data.add_content("xoppy_data_harmonics", harmonics_data) calculated_data.add_content("plot_x_col", 0) calculated_data.add_content("plot_y_col", 1) except: pass try: calculated_data.add_content("labels",["Energy(eV)","Flux(ph/s/0.1%bw)","FWHM(eV)","Ky", "Ptot(W)","Pd(W/mm^2)","IntP(W)"]) except: pass try: calculated_data.add_content("info",txtlist) except: pass return calculated_data
def xoppy_calc_xinpro(CRYSTAL_MATERIAL=0,MODE=0,ENERGY=8000.0,MILLER_INDEX_H=1,MILLER_INDEX_K=1,MILLER_INDEX_L=1,\ ASYMMETRY_ANGLE=0.0,THICKNESS=500.0,TEMPERATURE=300.0,NPOINTS=100,SCALE=0,XFROM=-50.0,XTO=50.0): print("Inside xoppy_calc_xinpro. ") try: with open("xoppy.inp", "wt") as f: f.write("%s\n"% (os.path.join(locations.home_data(), "inpro" + os.sep))) if MODE == 0: f.write("+1\n") elif MODE == 1: f.write("-1\n") elif MODE == 2: f.write("+2\n") elif MODE == 3: f.write("-1\n") else: f.write("ERROR!!\n") f.write("%f\n%d\n"%(THICKNESS,CRYSTAL_MATERIAL+1)) f.write("%s\n%f\n"%("EV",ENERGY)) f.write("%d\n%d\n%d\n"%(MILLER_INDEX_H,MILLER_INDEX_K,MILLER_INDEX_L)) f.write("%f\n%f\n%s\n"%(ASYMMETRY_ANGLE,TEMPERATURE, "inpro.dat")) if SCALE == 0: f.write("1\n") else: f.write("%d\n%f\n%f\n"%(2,XFROM,XTO)) f.write("%d\n"%(NPOINTS)) for file in ["inpro.par","inpro.dat","inpro.spec"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass command = os.path.join(locations.home_bin(), 'inpro') + " < xoppy.inp" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") #add SPEC header txt = open("inpro.dat").read() outFile = "inpro.spec" f = open(outFile,"w") f.write("#F inpro.spec\n") f.write("\n") f.write("#S 1 inpro results\n") f.write("#N 3\n") f.write("#L Theta-TetaB s-polarized reflectivity p-polarized reflectivity\n") f.write(txt) f.close() print("File written to disk: inpro.dat, inpro.par, inpro.spec") #show calculated parameters in standard output txt_info = open("inpro.par").read() for line in txt_info: print(line,end="") return outFile except Exception as e: raise e
def xoppy_calc_xinpro(CRYSTAL_MATERIAL=0,MODE=0,ENERGY=8000.0,MILLER_INDEX_H=1,MILLER_INDEX_K=1,MILLER_INDEX_L=1,\ ASYMMETRY_ANGLE=0.0,THICKNESS=500.0,TEMPERATURE=300.0,NPOINTS=100,SCALE=0,XFROM=-50.0,XTO=50.0): print("Inside xoppy_calc_xinpro. ") try: with open("xoppy.inp", "wt") as f: f.write("%s\n" % (os.path.join(locations.home_data(), "inpro" + os.sep))) if MODE == 0: f.write("+1\n") elif MODE == 1: f.write("-1\n") elif MODE == 2: f.write("+2\n") elif MODE == 3: f.write("-1\n") else: f.write("ERROR!!\n") f.write("%f\n%d\n" % (THICKNESS, CRYSTAL_MATERIAL + 1)) f.write("%s\n%f\n" % ("EV", ENERGY)) f.write("%d\n%d\n%d\n" % (MILLER_INDEX_H, MILLER_INDEX_K, MILLER_INDEX_L)) f.write("%f\n%f\n%s\n" % (ASYMMETRY_ANGLE, TEMPERATURE, "inpro.dat")) if SCALE == 0: f.write("1\n") else: f.write("%d\n%f\n%f\n" % (2, XFROM, XTO)) f.write("%d\n" % (NPOINTS)) for file in ["inpro.par", "inpro.dat", "inpro.spec"]: try: os.remove(os.path.join(locations.home_bin_run(), file)) except: pass command = "'" + os.path.join(locations.home_bin(), 'inpro') + "' < xoppy.inp" print("Running command '%s' in directory: %s " % (command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") #add SPEC header txt = open("inpro.dat").read() outFile = "inpro.spec" f = open(outFile, "w") f.write("#F inpro.spec\n") f.write("\n") f.write("#S 1 inpro results\n") f.write("#N 3\n") f.write( "#L Theta-TetaB s-polarized reflectivity p-polarized reflectivity\n" ) f.write(txt) f.close() print("File written to disk: inpro.dat, inpro.par, inpro.spec") #show calculated parameters in standard output txt_info = open("inpro.par").read() for line in txt_info: print(line, end="") return outFile except Exception as e: raise e
def calculate_with_xoppy( bragg_or_laue=0, # diffracted_or_transmitted=0, # crystal_name="Si", # string thickness=1e-2, # meters miller_h=1, # int miller_k=1, # int miller_l=1, # int asymmetry_angle=0.0, # radians energy=8000.0, # eV angle_deviation_min=-100e-6, # radians angle_deviation_max=100e-6, # radians angle_deviation_points=500, ): MILLER_INDEX_H = miller_h MILLER_INDEX_K = miller_k MILLER_INDEX_L = miller_l TEMPER = 1.0 MOSAIC = 0 if bragg_or_laue == 0: if diffracted_or_transmitted == 0: GEOMETRY = 0 elif diffracted_or_transmitted == 1: GEOMETRY = 2 else: raise Exception("Bad geometry type") elif bragg_or_laue == 1: if diffracted_or_transmitted == 0: GEOMETRY = 1 elif diffracted_or_transmitted == 1: GEOMETRY = 3 else: raise Exception("Bad geometry type") else: raise Exception("Bad geometry type") SCAN = 2 UNIT = 0 # rad SCANFROM = angle_deviation_min SCANTO = angle_deviation_max SCANPOINTS = angle_deviation_points ENERGY = energy ASYMMETRY_ANGLE = asymmetry_angle * 180.0 / numpy.pi THICKNESS = thickness * 100 MOSAIC_FWHM = 0.0 RSAG = 0.0 RMER = 0.0 ANISOTROPY = 0 POISSON = 0.0 CUT = 0 FILECOMPLIANCE = "" for file in [ "diff_pat.dat", "diff_pat.gle", "diff_pat.par", "diff_pat.xop", "xcrystal.bra" ]: try: os.remove(os.path.join(locations.home_bin_run(), file)) except: pass if (GEOMETRY == 1) or (GEOMETRY == 3): if ASYMMETRY_ANGLE == 0.0: print( "xoppy_calc_xcrystal: WARNING: In xcrystal the asymmetry angle is the angle between Bragg planes and crystal surface," + "in BOTH Bragg and Laue geometries.") descriptor = crystal_name if SCAN == 3: # energy scan emin = SCANFROM - 1 emax = SCANTO + 1 else: emin = ENERGY - 100.0 emax = ENERGY + 100.0 print("Using crystal descriptor: ", descriptor) bragg_dictionary = bragg_calc(descriptor=descriptor, hh=MILLER_INDEX_H, kk=MILLER_INDEX_K, ll=MILLER_INDEX_L, temper=float(TEMPER), emin=emin, emax=emax, estep=5.0, fileout="xcrystal.bra") with open("xoppy.inp", "wt") as f: f.write("xcrystal.bra\n") f.write("%d\n" % MOSAIC) f.write("%d\n" % GEOMETRY) if MOSAIC == 1: f.write("%g\n" % MOSAIC_FWHM) f.write("%g\n" % THICKNESS) else: f.write("%g\n" % THICKNESS) f.write("%g\n" % ASYMMETRY_ANGLE) scan_flag = 1 + SCAN f.write("%d\n" % scan_flag) f.write("%19.9f\n" % ENERGY) if scan_flag <= 3: f.write("%d\n" % UNIT) f.write("%g\n" % SCANFROM) f.write("%g\n" % SCANTO) f.write("%d\n" % SCANPOINTS) if MOSAIC > 1: # bent f.write("%g\n" % RSAG) f.write("%g\n" % RMER) f.write("0\n") if ((descriptor == "Si") or (descriptor == "Si2") or (descriptor == "Si_NIST") or (descriptor == "Ge") or descriptor == "Diamond"): pass else: # not Si,Ge,Diamond if ((ANISOTROPY == 1) or (ANISOTROPY == 2)): raise Exception( "Anisotropy data not available for this crystal. Either use isotropic or use external compliance file. Please change and run again'" ) f.write("%d\n" % ANISOTROPY) if ANISOTROPY == 0: f.write("%g\n" % POISSON) elif ANISOTROPY == 1: f.write("%d\n" % CRYSTAL_MATERIAL) f.write("%g\n" % ASYMMETRY_ANGLE) f.write("%d\n" % MILLER_INDEX_H) f.write("%d\n" % MILLER_INDEX_K) f.write("%d\n" % MILLER_INDEX_L) elif ANISOTROPY == 2: f.write("%d\n" % CRYSTAL_MATERIAL) f.write("%g\n" % ASYMMETRY_ANGLE) # TODO: check syntax for CUT: Cut syntax is: valong_X valong_Y valong_Z ; vnorm_X vnorm_Y vnorm_Z ; vperp_x vperp_Y vperp_Z f.write("%s\n" % CUT.split(";")[0]) f.write("%s\n" % CUT.split(";")[1]) f.write("%s\n" % CUT.split(";")[2]) elif ANISOTROPY == 3: f.write("%s\n" % FILECOMPLIANCE) command = os.path.join(locations.home_bin(), 'diff_pat') + " < xoppy.inp" print("Running command '%s' in directory: %s " % (command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") #show calculated parameters in standard output txt_info = open("diff_pat.par").read() for line in txt_info: print(line, end="") try: calculated_data = numpy.loadtxt("diff_pat.dat", skiprows=5) except Exception as e: raise Exception("Error loading diff_pat.dat :" + str(e)) print(calculated_data.shape) deviations = calculated_data[:, 0].copy() intensityS = calculated_data[:, 6].copy() intensityP = calculated_data[:, 5].copy() return deviations, intensityS, intensityP
def xoppy_calc_xtc(self): for file in ["tc.inp","tc.out"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass with open("tc.inp", "wt") as f: f.write("TS called from xoppy\n") f.write("%10.3f %10.2f %10.6f %s\n"%(self.ENERGY,self.CURRENT,self.ENERGY_SPREAD,"Ring-Energy(GeV) Current(mA) Beam-Energy-Spread")) f.write("%10.4f %10.4f %10.4f %10.4f %s\n"%(self.SIGX,self.SIGY,self.SIGX1,self.SIGY1,"Sx(mm) Sy(mm) Sx1(mrad) Sy1(mrad)")) f.write("%10.3f %d %s\n"%(self.PERIOD,self.NP,"Period(cm) N")) f.write("%10.1f %10.1f %d %s\n"%(self.EMIN,self.EMAX,self.N,"Emin Emax Ne")) f.write("%d %d %d %s\n"%(self.HARMONIC_FROM,self.HARMONIC_TO,self.HARMONIC_STEP,"Hmin Hmax Hstep")) f.write("%d %d %d %d %s\n"%(self.HELICAL,self.METHOD,1,self.NEKS,"Helical Method Print_K Neks")) f.write("foreground\n") if platform.system() == "Windows": command = os.path.join(locations.home_bin(),'tc.exe') else: command = "'" + os.path.join(locations.home_bin(), 'tc') + "'" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("Output file: %s"%("tc.out")) print("\n--------------------------------------------------------\n") # # parse result files to exchange object # with open("tc.out","r") as f: lines = f.readlines() # print output file # for line in lines: # print(line, end="") # remove returns lines = [line[:-1] for line in lines] harmonics_data = [] # separate numerical data from text floatlist = [] harmoniclist = [] txtlist = [] for line in lines: try: tmp = line.strip() if tmp.startswith("Harmonic"): harmonic_number = int(tmp.split("Harmonic")[1].strip()) if harmonic_number != self.HARMONIC_FROM: harmonics_data[-1][1] = harmoniclist harmoniclist = [] harmonics_data.append([harmonic_number, None]) tmp = float(line.strip()[0]) floatlist.append(line) harmoniclist.append(line) except: txtlist.append(line) harmonics_data[-1][1] = harmoniclist data = numpy.loadtxt(floatlist) for index in range(0, len(harmonics_data)): # print (harmonics_data[index][0], harmonics_data[index][1]) harmonics_data[index][1] = numpy.loadtxt(harmonics_data[index][1]) #send exchange calculated_data = DataExchangeObject("XOPPY", self.get_data_exchange_widget_name()) try: calculated_data.add_content("xoppy_data", data) calculated_data.add_content("xoppy_data_harmonics", harmonics_data) calculated_data.add_content("plot_x_col", 1) calculated_data.add_content("plot_y_col", 2) except: pass try: calculated_data.add_content("labels",["Energy (eV) without emittance", "Energy (eV) with emittance", "Brilliance (ph/s/mrad^2/mm^2/0.1%bw)","Ky","Total Power (W)","Power density (W/mr^2)"]) except: pass try: calculated_data.add_content("info",txtlist) except: pass return calculated_data
def xoppy_calc_mlayer(self): # copy the variable locally, so no more use of self. MODE = self.MODE SCAN = self.SCAN F12_FLAG = self.F12_FLAG SUBSTRATE = self.SUBSTRATE ODD_MATERIAL = self.ODD_MATERIAL EVEN_MATERIAL = self.EVEN_MATERIAL ENERGY = self.ENERGY THETA = self.THETA SCAN_STEP = self.SCAN_STEP NPOINTS = self.NPOINTS ODD_THICKNESS = self.ODD_THICKNESS EVEN_THICKNESS = self.EVEN_THICKNESS NLAYERS = self.NLAYERS FILE=self.FILE for file in ["mlayer.inp","mlayer.par","mlayer.f12"]: try: os.remove(os.path.join(locations.home_bin_run(),file)) except: pass # # write input file for Fortran mlayer: mlayer.inp # f = open('mlayer.inp','w') if SCAN == 0 and MODE == 0: a0 = 1 if SCAN == 1 and MODE == 0: a0 = 5 if SCAN == 0 and MODE == 1: a0 = 3 if SCAN == 1 and MODE == 1: a0 = 5 f.write("%d \n"%a0) f.write("N\n") f.write("%g\n"%( codata.h * codata.c / codata.e * 1e10 / ENERGY)) f.write("%g\n"%THETA) if SCAN == 0: f.write("%g\n"%SCAN_STEP) a2 = codata.h * codata.c / codata.e * 1e10 / ENERGY a3 = codata.h * codata.c / codata.e * 1e10 / (ENERGY + SCAN_STEP) a4 = a3 - a2 if SCAN != 0: f.write("%g\n"%a4) f.write("%d\n"%NPOINTS) if MODE == 0: f.write("%d\n"%NLAYERS) if MODE == 0: if a0 != 5: f.write("%g %g \n"%(ODD_THICKNESS,EVEN_THICKNESS)) else: for i in range(NLAYERS): f.write("%g %g \n"%(ODD_THICKNESS,EVEN_THICKNESS)) if MODE != 0: f1 = open(FILE,'r') a5 = f1.read() f1.close() if MODE != 0: print("Number of layers in %s file is %d "%(FILE,NLAYERS)) f.write("%d\n"%NLAYERS) f.write(a5) f.write("mlayer.par\n") f.write("mlayer.dat\n") f.write("6\n") f.close() print('File written to disk: mlayer.inp') # # create f12 file # if F12_FLAG == 0: energy = numpy.arange(0,500) elefactor = numpy.log10(10000.0 / 30.0) / 300.0 energy = 10.0 * 10**(energy * elefactor) f12_s = f1f2_calc(SUBSTRATE,energy) f12_e = f1f2_calc(EVEN_MATERIAL,energy) f12_o = f1f2_calc(ODD_MATERIAL,energy) f = open("mlayer.f12",'w') f.write('; File created by xoppy for materials [substrate=%s,even=%s,odd=%s]: \n'%(SUBSTRATE,EVEN_MATERIAL,ODD_MATERIAL)) f.write('; Atomic masses: \n') f.write("%g %g %g \n"%(xraylib.AtomicWeight(xraylib.SymbolToAtomicNumber(SUBSTRATE)), xraylib.AtomicWeight(xraylib.SymbolToAtomicNumber(EVEN_MATERIAL)), xraylib.AtomicWeight(xraylib.SymbolToAtomicNumber(ODD_MATERIAL)) )) f.write('; Densities: \n') f.write("%g %g %g \n"%(xraylib.ElementDensity(xraylib.SymbolToAtomicNumber(SUBSTRATE)), xraylib.ElementDensity(xraylib.SymbolToAtomicNumber(EVEN_MATERIAL)), xraylib.ElementDensity(xraylib.SymbolToAtomicNumber(ODD_MATERIAL)) )) f.write('; Number of energy points: \n') f.write("%d\n"%(energy.size)) f.write('; For each energy point, energy[eV], f1[substrate], f2[substrate], f1[even], f2[even], f1[odd], f2[odd]: \n') for i in range(energy.size): f.write("%g %g %g %g %g %g %g \n"%(energy[i],f12_s[0,i],f12_s[1,i],f12_e[0,i],f12_e[1,i],f12_o[0,i],f12_o[1,i])) f.close() print('File written to disk: mlayer.f12') # # run external program mlayer # command = os.path.join(locations.home_bin(), 'mlayer') + " < mlayer.inp" print("Running command '%s' in directory: %s "%(command, locations.home_bin_run())) print("\n--------------------------------------------------------\n") os.system(command) print("\n--------------------------------------------------------\n") #send exchange calculated_data = DataExchangeObject("XOPPY", self.get_data_exchange_widget_name()) try: calculated_data.add_content("xoppy_data", numpy.loadtxt("mlayer.dat")) calculated_data.add_content("plot_x_col", 0) calculated_data.add_content("plot_y_col", 3) calculated_data.add_content("units_to_degrees", 1.0) except: pass try: calculated_data.add_content("labels",["Grazing angle Theta [deg]","s-reflectivity","p-reflectivity","averaged reflectivity","s-phase shift","p-phase shift","(s-electric field)^2","(p-electric field)^2"]) except: pass try: info = "ML %s(%3.2f A):%s(%3.2f A) %d pairs; E=%5.3f eV"%(ODD_MATERIAL,ODD_THICKNESS,EVEN_MATERIAL,EVEN_THICKNESS,NLAYERS,ENERGY) calculated_data.add_content("info",info) except: pass return calculated_data