Changeset dbc01f2 in sasview for sanscalculator
- Timestamp:
- Jan 10, 2013 5:16:51 PM (12 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- b1174ec
- Parents:
- 2d9c7266
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sanscalculator/src/sans/calculator/sans_gen.py
rd7b6913 rdbc01f2 5 5 import sans.models.sans_extension.sld2i as mod 6 6 from periodictable import formula 7 from periodictable import nsf 7 8 import numpy 8 9 import os … … 11 12 MFactor_mT = 2.3164E-9 12 13 METER2ANG = 1.0E+10 14 #Avogadro constant [1/mol] 15 NA = 6.02214129e+23 13 16 14 17 def mag2sld(mag, v_unit=None): … … 48 51 self.data_my = None 49 52 self.data_mz = None 50 self. volume = 1000#[A^3]53 self.data_vol = None #[A^3] 51 54 ## Name of the model 52 55 self.name = "GenSAS" … … 69 72 self.fixed=[] 70 73 71 def set_pixel_volume (self, volume):74 def set_pixel_volumes(self, volume): 72 75 """ 73 76 Set the volume of a pixel in (A^3) unit 74 """ 75 self.volume = volume 77 :Param volume: pixel volume [float] 78 """ 79 if self.data_vol == None: 80 raise 81 self.data_vol = volume 76 82 77 83 def _gen(self, x, y, i): … … 87 93 model = mod.new_GenI(len_x, self.data_x, self.data_y, self.data_z, 88 94 self.data_sldn, self.data_mx, self.data_my, 89 self.data_mz, self.params['Up_frac_in'], 95 self.data_mz, self.data_vol, 96 self.params['Up_frac_in'], 90 97 self.params['Up_frac_out'], 91 98 self.params['Up_theta']) 99 92 100 mod.genicom(model, len_q, x, y, i) 93 return self.params['scale'] * i * self.volume \ 94 + self.params['background'] 101 return self.params['scale'] * i + self.params['background'] 95 102 96 103 def set_sld_data(self, sld_data=None): … … 107 114 self.data_my = sld_data.sld_my 108 115 self.data_mz = sld_data.sld_mz 116 self.data_vol = sld_data.vol_pix 109 117 110 118 def getProfile(self): … … 126 134 y_in = numpy.zero_like(x[0]) 127 135 # 1D I is found at y =0 in the 2D pattern 128 return self._gen(x[0], y_in, i_out )136 return self._gen(x[0], y_in, i_out ) 129 137 else: 130 138 msg = "Q must be given as list of qx's and qy's" … … 183 191 self.mz = None 184 192 self.sld_n = None 193 self.vol_pix = None 185 194 self.output = None 186 195 self.omfdata = None … … 443 452 type = ["pdb files (*.PDB, *.pdb)|*.pdb"] 444 453 ## List of allowed extensions 445 ext = ['.pdb', '.PDB'] 454 ext = ['.pdb', '.PDB'] 446 455 447 456 def read(self, path): … … 462 471 sld_my = numpy.zeros(0) 463 472 sld_mz = numpy.zeros(0) 473 vol_pix = numpy.zeros(0) 464 474 pix_symbol = numpy.zeros(0) 465 475 try: … … 498 508 pos_z = numpy.append(pos_z, _pos_z) 499 509 try: 500 #from periodictable import nsf501 #val = nsf.neutron_sld(atom_name)502 val = formula(atom_name).atoms.keys()[0].neutron.b_c503 510 # sld in Ang unit (from fm) 504 val = float(val) 505 val *= 1.0e-5 511 #val = formula(atom_name).atoms.keys()[0].neutron.b_c 512 #val *= 1.0e-5 513 val = nsf.neutron_sld(atom_name)[0] 514 # sld in Ang^-2 unit 515 val *= 1.0e-6 506 516 sld_n = numpy.append(sld_n, val) 517 atom = formula(atom_name) 518 # cm to A units 519 vol = 1.0e+24 * atom.mass / atom.density / NA 520 vol_pix = numpy.append(vol_pix, vol) 507 521 except: 508 522 print "Error: set the sld of %s to zero"% atom_name … … 525 539 output.set_pixel_symbols(pix_symbol) 526 540 output.set_nodes() 527 output.sld_unit = 'A' 541 output.set_pixel_volumes(vol_pix) 542 output.sld_unit = '1/A^(2)' 528 543 return output 529 544 except: … … 561 576 :raise ValueError: when the length of the data vectors are inconsistent 562 577 """ 563 pos_x = numpy.zeros(0) 564 pos_y = numpy.zeros(0) 565 pos_z = numpy.zeros(0) 566 sld_n = numpy.zeros(0) 567 sld_mx = numpy.zeros(0) 568 sld_my = numpy.zeros(0) 569 sld_mz = numpy.zeros(0) 578 579 570 580 try: 571 input_f = open(path, 'rb') 572 buff = input_f.read() 573 lines = buff.split('\n') 574 input_f.close() 575 for line in lines: 576 toks = line.split() 577 try: 578 _pos_x = float(toks[0]) 579 _pos_y = float(toks[1]) 580 _pos_z = float(toks[2]) 581 _sld_n = float(toks[3]) 582 _sld_mx = float(toks[4]) 583 _sld_my = float(toks[5]) 584 _sld_mz = float(toks[6]) 585 586 pos_x = numpy.append(pos_x, _pos_x) 587 pos_y = numpy.append(pos_y, _pos_y) 588 pos_z = numpy.append(pos_z, _pos_z) 589 sld_n = numpy.append(sld_n, _sld_n) 590 sld_mx = numpy.append(sld_mx, _sld_mx) 591 sld_my = numpy.append(sld_my, _sld_my) 592 sld_mz = numpy.append(sld_mz, _sld_mz) 593 except: 594 # Skip non-data lines 595 pass 581 input_f = numpy.loadtxt(path, dtype='float', skiprows=1, 582 ndmin=1, unpack=True) 583 pos_x = numpy.array(input_f[0]) 584 pos_y = numpy.array(input_f[1]) 585 pos_z = numpy.array(input_f[2]) 586 sld_n = numpy.array(input_f[3]) 587 sld_mx = numpy.array(input_f[4]) 588 sld_my = numpy.array(input_f[5]) 589 sld_mz = numpy.array(input_f[6]) 590 ncols = len(input_f) 591 if ncols == 8: 592 vol_pix = numpy.array(input_f[7]) 593 elif ncols == 7: 594 vol_pix = None 595 else: 596 raise 596 597 pos_x -= (min(pos_x) + max(pos_x)) / 2.0 597 598 pos_y -= (min(pos_y) + max(pos_y)) / 2.0 598 599 pos_z -= (min(pos_z) + max(pos_z)) / 2.0 599 600 600 output = MagSLD(pos_x, pos_y, pos_z, sld_n, sld_mx, sld_my, sld_mz) 601 output = MagSLD(pos_x, pos_y, pos_z, sld_n, 602 sld_mx, sld_my, sld_mz) 603 601 604 output.filename = os.path.basename(path) 602 605 output.set_pix_type('pixel') 603 606 output.set_pixel_symbols('pixel') 607 if vol_pix != None: 608 output.set_pixel_volumes(vol_pix) 604 609 return output 605 610 except: … … 621 626 y_val = data.pos_y 622 627 z_val = data.pos_z 628 vol_pix = data.vol_pix 623 629 624 630 length = len(x_val) … … 639 645 out = open(path, 'w') 640 646 # First Line: Column names 641 out.write("X Y Z SLDN SLDMx SLDMy SLDMz")647 out.write("X Y Z SLDN SLDMx SLDMy SLDMz VOLUMEpix") 642 648 for ind in range(length): 643 out.write("\n%g %g %g %g %g %g %g " % (x_val[ind], y_val[ind],649 out.write("\n%g %g %g %g %g %g %g %g" % (x_val[ind], y_val[ind], 644 650 z_val[ind], sld_n[ind], 645 651 sld_mx[ind], sld_my[ind], 646 sld_mz[ind] ))652 sld_mz[ind], vol_pix[ind])) 647 653 out.close() 648 654 … … 749 755 750 756 def __init__(self, pos_x, pos_y, pos_z, sld_n=None, 751 sld_mx=None, sld_my=None, sld_mz=None ):757 sld_mx=None, sld_my=None, sld_mz=None, vol_pix=None): 752 758 """ 753 759 Init for mag SLD … … 773 779 self.sld_my = sld_my 774 780 self.sld_mz = sld_mz 781 self.vol_pix = vol_pix 775 782 self.sld_m = None 776 783 self.sld_phi = None … … 851 858 else: 852 859 self.pix_symbol = symbol 860 861 def set_pixel_volumes(self, vol): 862 """ 863 Set pixel volumes 864 :Params pixel: str; pixel or atomic symbol, or array of strings 865 """ 866 if self.sld_n == None: 867 return 868 if vol.__class__.__name__ == 'ndarray': 869 self.vol_pix = vol 870 elif vol.__class__.__name__.count('float') > 0: 871 self.vol_pix = numpy.repeat(vol, len(self.sld_n)) 872 else: 873 self.vol_pix = None 853 874 854 875 def get_sldn(self): … … 913 934 self.zstepsize = numpy.fabs(z_pos - zpos_pre) 914 935 break 936 #default pix volume 937 self.vol_pix = numpy.ones(len(self.pos_x)) 938 vol = self.xstepsize * self.ystepsize * self.zstepsize 939 self.set_pixel_volumes(vol) 915 940 self.has_stepsize = True 916 941 except: … … 918 943 self.ystepsize = None 919 944 self.zstepsize = None 945 self.vol_pix = None 920 946 self.has_stepsize = False 921 947 else: … … 1006 1032 1007 1033 if __name__ == "__main__": 1008 #test()1034 test() 1009 1035 test_load()
Note: See TracChangeset
for help on using the changeset viewer.