source: sans/Analysis/branches/ajj_23APR07/IGOR_Package_Files/Put in User Procedures/SANS_Models_v3.00/NewModels_2006/Cylinder_PolyRadius.ipf @ 166

Last change on this file since 166 was 166, checked in by srkline, 15 years ago

Modified all procedure files to add to the keyword=value strings to identify the function, coefficients, and suffix once the model is plotted (and the objects will exist)

a one-liner in the Plot and PlotSmeared? macros.

  • necessary for smoother functioning of the wrapper panel.
File size: 7.9 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma IgorVersion = 6.0
3
4#include "CylinderForm"
5
6// calculates the form factor of a cylinder with polydispersity of radius
7// the length distribution is a Schulz distribution, and any normalized distribution
8// could be used, as the average is performed numerically
9//
10// since the cylinder form factor is already a numerical integration, the size average is a
11// second integral, and significantly slows the calculation, and smearing adds a third integration.
12//
13//CORRECT! 12/5/2000 - Invariant is now correct vs. monodisperse cylinders
14// + upper limit of integration has been changed to account for skew of
15//Schulz distribution at high (>0.5) polydispersity
16//Requires 20 gauss points for integration of the radius (5 is not enough)
17//Requires either CylinderFit XOP (MacOSX only) or the normal CylinderForm Function
18//
19Proc PlotCyl_PolyRadius(num,qmin,qmax)
20        Variable num=128,qmin=0.001,qmax=0.7
21        Prompt num "Enter number of data points for model: "
22        Prompt qmin "Enter minimum q-value (^-1) for model: "
23        Prompt qmax "Enter maximum q-value (^-1) for model: "
24       
25        make/o/d/n=(num) xwave_cypr,ywave_cypr
26        xwave_cypr = alog(log(qmin) + x*((log(qmax)-log(qmin))/num))
27        make/o/d coef_cypr = {1.,20.,400,0.2,3.0e-6,0.01}
28        make/o/t parameters_cypr = {"scale","radius (A)","length (A)","polydispersity of Radius","SLD diff (A^-2)","incoh. bkg (cm^-1)"}
29        Edit parameters_cypr,coef_cypr
30       
31        Variable/G root:g_cypr
32        g_cypr := Cyl_PolyRadius(coef_cypr,ywave_cypr,xwave_cypr)
33        Display ywave_cypr vs xwave_cypr
34        ModifyGraph log=1,marker=29,msize=2,mode=4
35        Label bottom "q (\\S-1\\M)"
36        Label left "Intensity (cm\\S-1\\M)"
37        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
38       
39        AddModelToStrings("Cyl_PolyRadius","coef_cypr","cypr")
40End
41
42///////////////////////////////////////////////////////////
43// - sets up a dependency to a wrapper, not the actual SmearedModelFunction
44Proc PlotSmearedCyl_PolyRadius(str)                                                             
45        String str
46        Prompt str,"Pick the data folder containing the resolution you want",popup,getAList(4)
47       
48        // if any of the resolution waves are missing => abort
49        if(ResolutionWavesMissingDF(str))               //updated to NOT use global strings (in GaussUtils)
50                Abort
51        endif
52       
53        SetDataFolder $("root:"+str)
54       
55        // Setup parameter table for model function
56        make/o/D smear_coef_cypr = {1.,20.,400,0.2,3.0e-6,0.01}
57        make/o/t smear_parameters_cypr = {"scale","radius (A)","length (A)","polydispersity of Radius","SLD diff (A^-2)","incoh. bkg (cm^-1)"}
58        Edit smear_parameters_cypr,smear_coef_cypr
59       
60        // output smeared intensity wave, dimensions are identical to experimental QSIG values
61        // make extra copy of experimental q-values for easy plotting
62        Duplicate/O $(str+"_q") smeared_cypr,smeared_qvals
63        SetScale d,0,0,"1/cm",smeared_cypr     
64                                       
65        Variable/G gs_cypr=0
66        gs_cypr := fSmearedCyl_PolyRadius(smear_coef_cypr,smeared_cypr,smeared_qvals)   //this wrapper fills the STRUCT
67       
68        Display smeared_cypr vs smeared_qvals
69        ModifyGraph log=1,marker=29,msize=2,mode=4
70        Label bottom "q (\\S-1\\M)"
71        Label left "Intensity (cm\\S-1\\M)"
72        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
73       
74        SetDataFolder root:
75        AddModelToStrings("SmearedCyl_PolyRadius","smear_coef_cypr","cypr")
76End
77       
78
79
80//AAO version, uses XOP if available
81// simply calls the original single point calculation with
82// a wave assignment (this will behave nicely if given point ranges)
83Function Cyl_PolyRadius(cw,yw,xw) : FitFunc
84        Wave cw,yw,xw
85       
86#if exists("Cyl_PolyRadiusX")
87        yw = Cyl_PolyRadiusX(cw,xw)
88#else
89        yw = fCyl_PolyRadius(cw,xw)
90#endif
91        return(0)
92End
93
94Function fCyl_PolyRadius(w,x) :FitFunc
95        Wave w
96        Variable x
97
98        //The input variables are (and output)
99        //[0] scale
100        //[1] avg RADIUS (A)
101        //[2] Length (A)
102        //[3] polydispersity (0<p<1)
103        //[4] contrast (A^-2)
104        //[5] background (cm^-1)
105        Variable scale,radius,pd,delrho,bkg,zz,length
106        scale = w[0]
107        radius = w[1]
108        length = w[2]
109        pd = w[3]
110        delrho = w[4]
111        bkg = w[5]
112       
113        zz = (1/pd)^2-1
114//
115// the OUTPUT form factor is <f^2>/Vavg [cm-1]
116//
117// local variables
118        Variable nord,ii,a,b,va,vb,contr,vcyl,nden,summ,yyy,zi,qq
119        Variable answer,zp1,zp2,zp3,vpoly
120        String weightStr,zStr
121       
122//      nord = 5       
123//      weightStr = "gauss5wt"
124//      zStr = "gauss5z"
125        nord = 20
126        weightStr = "gauss20wt"
127        zStr = "gauss20z"
128
129//      if wt,z waves don't exist, create them
130// 5 Gauss points (not enough for cylinder radius = high q oscillations)
131// use 20 Gauss points
132        if (WaveExists($weightStr) == 0) // wave reference is not valid,
133                Make/D/N=(nord) $weightStr,$zStr
134                Wave wtGau = $weightStr
135                Wave zGau = $zStr               // wave references to pass
136                Make20GaussPoints(wtGau,zGau)   
137                //Make5GaussPoints(wtGau,zGau) 
138//      //                  printf "w[0],z[0] = %g %g\r", wtGau[0],zGau[0]
139        else
140                if(exists(weightStr) > 1)
141                         Abort "wave name is already in use"    // execute if condition is false
142                endif
143                Wave wtGau = $weightStr
144                Wave zGau = $zStr
145//      //          printf "w[0],z[0] = %g %g\r", wtGau[0],zGau[0]     
146        endif
147
148// set up the integration
149// end points and weights
150// limits are technically 0-inf, but wisely choose non-zero region of distribution
151        Variable range=3.4              //multiples of the std. dev. fom the mean
152        a = radius*(1-range*pd)
153        if (a<0)
154                a=0             //otherwise numerical error when pd >= 0.3, making a<0
155        endif
156        If(pd>0.3)
157                range = 3.4 + (pd-0.3)*18
158        Endif
159        b = radius*(1+range*pd) // is this far enough past avg radius?
160//      printf "a,b,ravg = %g %g %g\r", a,b,radius
161        va =a
162        vb =b
163
164// evaluate at Gauss points
165        // remember to index from 0,size-1     
166        qq = x          //current x point is the q-value for evaluation
167        summ = 0.0              // initialize integral
168   ii=0
169   do
170   //printf "top of nord loop, i = %g\r",i
171        // Using 5 Gauss points         
172                zi = ( zGau[ii]*(vb-va) + vb + va )/2.0         
173                yyy = wtGau[ii] * rad_kernel(qq,radius,length,zz,delrho,zi)
174                summ = yyy + summ
175                ii+=1
176        while (ii<nord)                         // end of loop over quadrature points
177//   
178// calculate value of integral to return
179   answer = (vb-va)/2.0*summ
180     
181//  contrast^2 is included in integration rad_kernel
182//      answer *= delrho*delrho
183//normalize by polydisperse volume
184// now volume depends on polydisperse RADIUS - so normalize by the second moment
185// 2nd moment = (zz+2)/(zz+1)
186        vpoly = Pi*(radius)^2*length*(zz+2)/(zz+1)
187//Divide by vol, since volume has been "un-normalized" out
188        answer /= vpoly
189//convert to [cm-1]
190        answer *= 1.0e8
191//scale
192        answer *= scale
193// add in the background
194        answer += bkg
195
196        Return (answer)
197End             //End of function PolyRadCylForm()
198
199Function rad_kernel(qw,ravg,len,zz,delrho,rad)
200        Variable qw,ravg,len,zz,delrho,rad
201       
202        Variable Pq,vcyl,dr
203       
204        //calculate the orientationally averaged P(q) for the input rad
205        //this is correct - see K&C (1983) or Lin &Tsao JACryst (1996)29 170.
206        Make/O/D/n=5 kernpar
207        Wave kp = kernpar
208        kp[0] = 1               //scale fixed at 1
209        kp[1] = rad
210        kp[2] = len
211        kp[3] = delrho
212        kp[4] = 0               //bkg fixed at 0
213       
214#if exists("CylinderFormX")
215        Pq = CylinderFormX(kp,qw)
216#else
217        Pq = fCylinderForm(kp,qw)
218#endif
219       
220        // undo the normalization that CylinderForm does
221        vcyl=Pi*rad*rad*len
222        Pq *= vcyl
223        //un-convert from [cm-1]
224        Pq /= 1.0e8
225       
226        // calculate normalized distribution at len value
227        dr = Schulz_Point_pr(rad,ravg,zz)
228       
229        return (Pq*dr) 
230End
231
232Function Schulz_Point_pr(x,avg,zz)
233        Variable x,avg,zz
234       
235        Variable dr
236       
237        dr = zz*ln(x) - gammln(zz+1)+(zz+1)*ln((zz+1)/avg)-(x/avg*(zz+1))
238       
239        return (exp(dr))
240End
241
242//wrapper to calculate the smeared model as an AAO-Struct
243// fills the struct and calls the ususal function with the STRUCT parameter
244//
245// used only for the dependency, not for fitting
246//
247Function fSmearedCyl_PolyRadius(coefW,yW,xW)
248        Wave coefW,yW,xW
249       
250        String str = getWavesDataFolder(yW,0)
251        String DF="root:"+str+":"
252       
253        WAVE resW = $(DF+str+"_res")
254       
255        STRUCT ResSmearAAOStruct fs
256        WAVE fs.coefW = coefW   
257        WAVE fs.yW = yW
258        WAVE fs.xW = xW
259        WAVE fs.resW = resW
260       
261        Variable err
262        err = SmearedCyl_PolyRadius(fs)
263       
264        return (0)
265End
266
267// this is all there is to the smeared calculation!
268Function SmearedCyl_PolyRadius(s) :FitFunc
269        Struct ResSmearAAOStruct &s
270
271//      the name of your unsmeared model (AAO) is the first argument
272        Smear_Model_20(Cyl_PolyRadius,s.coefW,s.xW,s.yW,s.resW)
273
274        return(0)
275End
Note: See TracBrowser for help on using the repository browser.