Package reflectometry :: Package model1d :: Package adaptor :: Module calcMagReflTheory

Source Code for Module reflectometry.model1d.adaptor.calcMagReflTheory

  1  import os 
  2  import sys 
  3  import numpy 
  4   
  5  from reflectometry.model1d.model.layer       import Layer 
  6  from reflectometry.model1d.model.bspline     import BSpline 
  7  from reflectometry.model1d.profileview.reflutils import decodeP 
  8  from reflectometry.model1d.model.calcProfile import build_profile 
  9  from reflectometry.model1d.model.calcRefl    import convolve, reflectivity, \ 
 10                                                      magnetic_reflectivity, \ 
 11                                                      unpolarized_magnetic 
 12  from reflectometry.model1d.model.auxs        import combine 
 13   
14 -def getLayers4Profile(profile):
15 DataArrayList = profile.getDataArrayList() 16 17 z = DataArrayList[0] 18 _rhoVal = DataArrayList[1] 19 _muVal = DataArrayList[2] 20 _phiVal = DataArrayList[3] 21 _thetaVal = DataArrayList[4] 22 23 return (z, _rhoVal, _muVal, _phiVal, _thetaVal )
24 25 26
27 -def getMeta4MetaObj( meta ):
28 angularDiv = meta.angularDiv 29 background = meta.background 30 wavelength = meta.wavelength 31 wavelengthDiv = meta.wavelengthDiv 32 33 return (angularDiv, background, wavelength, wavelengthDiv)
34 35 36
37 -def calcMagProfie4Params( params, maxQ ):
38 39 _depth = []; _rho = []; _mu = []; _phi = []; _theta = []; _rough = [] 40 for pm in params: 41 _depth.append( pm.depth ) 42 _rough.append( pm.rough ) 43 _rho.append( decodeP(pm.rho ) ) 44 _mu.append( decodeP(pm.mu ) ) 45 _phi.append( decodeP(pm.phi ) ) 46 _theta.append( decodeP(pm.theta ) ) 47 48 if len(_depth) <= 2: 49 return ( None, None, None, None, None ) 50 51 _Lrho = []; _Lmu = [] ; _Lphi = []; _Ltheta = [] 52 for x in xrange(len(_depth)): 53 _Lrho.append( Layer(_rho[x] ) ) 54 _Lmu.append( Layer(_mu[x] ) ) 55 _Lphi.append( Layer(_phi[x] ) ) 56 _Ltheta.append( Layer(_theta[x]) ) 57 58 if 3*_rough[1] >= 100: _depth[0] = 3*_rough[1] 59 else: _depth[0] = 100 60 61 if 3*_rough[-1] >= 100: _depth[-1] = 3*_rough[-1] 62 else: _depth[-1] = 100 63 64 offset=numpy.concatenate(((0,),numpy.cumsum( _depth )) ) - _depth[0] 65 66 # 10 is just a reasonable constant, we can change it to other value 67 #n = int( 10.0*(2.0*3.1415926/maxQ) ) 68 #z = numpy.linspace( offset[0], offset[-1], n) 69 70 step = 1 71 z = numpy.arange( offset[0], offset[-1]+step-1e-8, step) 72 (_rhoVal, _muVal, _phiVal, _thetaVal) = [build_profile(_depth, 73 _rough, 74 c, 75 z, 76 max_rough= 3) 77 for c in (_Lrho, _Lmu, _Lphi, _Ltheta) ] 78 79 dz = step*numpy.ones(z.shape) 80 dz[0] = dz[-1] = 0 81 82 return (dz, _rhoVal, _muVal, _phiVal, _thetaVal )
83 84
85 -def getMinQ(Qs):
86 minQ = Qs[0].min() 87 for i in xrange(len(Qs)-1): 88 _minQ = Qs[i+1].min() 89 if _minQ < minQ: minQ = _minQ 90 return minQ
91 92
93 -def getMaxQ(Qs):
94 maxQ = Qs[0].max() 95 for i in xrange(len(Qs)-1): 96 _maxQ = Qs[i+1].max() 97 if _maxQ > maxQ: maxQ = _maxQ 98 99 return maxQ
100 101 102
103 -def calcMagReflTheory( Qs, 104 params, 105 meta, 106 profile, 107 dQs, 108 ):
109 110 maxQ = getMaxQ( Qs ) 111 112 # get profile 113 if profile == None: 114 (z,_rhoVal,_muVal,_phiVal,_thetaVal ) = \ 115 calcMagProfie4Params(params, maxQ) 116 else: 117 (z,_rhoVal,_muVal,_phiVal,_thetaVal ) = getLayers4Profile(profile) 118 119 if z==None or _rhoVal==None or _muVal==None or \ 120 _phiVal==None or _thetaVal==None: 121 return 122 123 (angularDiv, background, wavelength, wavelengthDiv) = \ 124 getMeta4MetaObj( meta ) 125 126 # Combine Qs and sort it. 127 baseQs = combine( Qs ) 128 baseQs.sort() 129 130 #calculate it 131 baseRs = magnetic_reflectivity(baseQs, 132 z, 133 _rhoVal, 134 mu=_muVal, 135 wavelength=wavelength, 136 rho_m = _phiVal, 137 theta_m = _thetaVal 138 ) 139 # Add background 140 baseRs = numpy.array(baseRs) + background 141 142 # Do convolution 143 retval = [] 144 for x in range(4): 145 currdQ = dQs[x] 146 if currdQ == None: 147 currdQ = numpy.zeros( len(Qs[x]) ) 148 newitem = convolve(baseQs, baseRs[x], Qs[x], currdQ) 149 150 retval += [newitem] 151 152 return retval
153 154 155
156 -def calcUnpolarizedMagTheory( Q, 157 params, 158 meta, 159 profile, 160 dQ=None, 161 ):
162 163 # get the max Q 164 maxQ = Q.max() 165 166 # get profile 167 if profile == None: 168 (z,_rhoVal,_muVal,_phiVal,_thetaVal ) = \ 169 calcMagProfie4Params(params, maxQ) 170 else: 171 (z,_rhoVal,_muVal,_phiVal,_thetaVal ) = getLayers4Profile(profile) 172 173 if z==None or _rhoVal==None or _muVal==None or \ 174 _phiVal==None or _thetaVal==None: 175 return 176 177 # meta data 178 (angularDiv, background, wavelength, wavelengthDiv) = \ 179 getMeta4MetaObj( meta ) 180 181 # Do calc reflectivity 182 R = ( unpolarized_magnetic(Q, 183 z, 184 _rhoVal, 185 mu = _muVal, 186 rho_m = _phiVal, 187 theta_m = _thetaVal, 188 wavelength=float(wavelength) )+float(background) 189 ) 190 191 # Do convolution 192 Rconv = convolve(Q, R, Q, dQ) 193 194 return R #Rconv
195