source: sans/Analysis/branches/ajj_23APR07/IGOR_Package_Files/Put in User Procedures/SANS_Models_v3.00/NewModels_2006/Cylinder_PolyLength.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.8 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 length
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//CORRECTED 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_PolyLength(num,qmin,qmax)
20        Variable num=100,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_cypl,ywave_cypl
26        xwave_cypl = alog(log(qmin) + x*((log(qmax)-log(qmin))/num))
27        make/o/d coef_cypl = {1.,20.,1000,0.2,3.0e-6,0.01}
28        make/o/t parameters_cypl = {"scale","radius (A)","length (A)","polydispersity of Length","SLD diff (A^-2)","incoh. bkg (cm^-1)"}
29        Edit parameters_cypl,coef_cypl
30       
31        Variable/G root:g_cypl
32        g_cypl := Cyl_PolyLength(coef_cypl,ywave_cypl,xwave_cypl)
33        Display ywave_cypl vs xwave_cypl
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_PolyLength","coef_cypl","cypl")
40End
41
42// - sets up a dependency to a wrapper, not the actual SmearedModelFunction
43Proc PlotSmearedCyl_PolyLength(str)                                                             
44        String str
45        Prompt str,"Pick the data folder containing the resolution you want",popup,getAList(4)
46       
47        // if any of the resolution waves are missing => abort
48        if(ResolutionWavesMissingDF(str))               //updated to NOT use global strings (in GaussUtils)
49                Abort
50        endif
51       
52        SetDataFolder $("root:"+str)
53       
54        // Setup parameter table for model function
55        make/o/D smear_coef_cypl = {1.,20.,1000,0.2,3.0e-6,0.01}
56        make/o/t smear_parameters_cypl = {"scale","radius (A)","length (A)","polydispersity of Length","SLD diff (A^-2)","incoh. bkg (cm^-1)"}
57        Edit smear_parameters_cypl,smear_coef_cypl
58       
59        // output smeared intensity wave, dimensions are identical to experimental QSIG values
60        // make extra copy of experimental q-values for easy plotting
61        Duplicate/O $(str+"_q") smeared_cypl,smeared_qvals
62        SetScale d,0,0,"1/cm",smeared_cypl     
63                                       
64        Variable/G gs_cypl=0
65        gs_cypl := fSmearedCyl_PolyLength(smear_coef_cypl,smeared_cypl,smeared_qvals)   //this wrapper fills the STRUCT
66       
67        Display smeared_cypl vs smeared_qvals
68        ModifyGraph log=1,marker=29,msize=2,mode=4
69        Label bottom "q (\\S-1\\M)"
70        Label left "Intensity (cm\\S-1\\M)"
71        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
72       
73        SetDataFolder root:
74        AddModelToStrings("SmearedCyl_PolyLength","smear_coef_cypl","cypl")
75End
76       
77
78
79//AAO version, uses XOP if available
80// simply calls the original single point calculation with
81// a wave assignment (this will behave nicely if given point ranges)
82Function Cyl_PolyLength(cw,yw,xw) : FitFunc
83        Wave cw,yw,xw
84       
85#if exists("Cyl_PolyLengthX")
86        yw = Cyl_PolyLengthX(cw,xw)
87#else
88        yw = fCyl_PolyLength(cw,xw)
89#endif
90        return(0)
91End
92
93//calculate the form factor averaged over the size distribution
94// both integrals are done using quadrature, although both may benefit from an
95// adaptive integration
96Function fCyl_PolyLength(w,x)  : FitFunc
97        Wave w
98        Variable x
99
100        //The input variables are (and output)
101        //[0] scale
102        //[1] avg RADIUS (A)
103        //[2] Length (A)
104        //[3] polydispersity (0<p<1)
105        //[4] contrast (A^-2)
106        //[5] background (cm^-1)
107        Variable scale,radius,pd,delrho,bkg,zz,length
108        scale = w[0]
109        radius = w[1]
110        length = w[2]
111        pd = w[3]
112        delrho = w[4]
113        bkg = w[5]
114       
115        zz = (1/pd)^2-1
116//
117// the OUTPUT form factor is <f^2>/Vavg [cm-1]
118//
119// local variables
120        Variable nord,ii,a,b,va,vb,contr,vcyl,nden,summ,yyy,zi,qq
121        Variable answer,zp1,zp2,zp3,vpoly
122        String weightStr,zStr
123       
124//      nord = 5       
125//      weightStr = "gauss5wt"
126//      zStr = "gauss5z"
127        nord = 20
128        weightStr = "gauss20wt"
129        zStr = "gauss20z"
130
131// 5 Gauss points (not enough for cylinder radius = high q oscillations)
132// use 20 Gauss points
133        if (WaveExists($weightStr) == 0) // wave reference is not valid,
134                Make/D/N=(nord) $weightStr,$zStr
135                Wave wtGau = $weightStr
136                Wave zGau = $zStr               
137                Make20GaussPoints(wtGau,zGau)   
138                //Make5GaussPoints(wtGau,zGau) 
139        else
140                if(exists(weightStr) > 1)
141                         Abort "wave name is already in use"   
142                endif
143                Wave wtGau = $weightStr
144                Wave zGau = $zStr
145        endif
146
147// set up the integration
148// end points and weights
149// limits are technically 0-inf, but wisely choose non-zero region of distribution
150        Variable range=3.4              //multiples of the std. dev. fom the mean
151        a = length*(1-range*pd)
152        if (a<0)
153                a=0             //otherwise numerical error when pd >= 0.3, making a<0
154        endif
155        If(pd>0.3)
156                range = 3.4 + (pd-0.3)*18
157        Endif
158        b = length*(1+range*pd) // is this far enough past avg length?
159//      printf "a,b,len_avg = %g %g %g\r", a,b,length
160        va =a
161        vb =b
162
163        qq = x          //current x point is the q-value for evaluation
164        summ = 0.0              // initialize integral
165   ii=0
166   do
167   //printf "top of nord loop, i = %g\r",i
168        // Using 5 Gauss points         
169                zi = ( zGau[ii]*(vb-va) + vb + va )/2.0         
170                yyy = wtGau[ii] * len_kernel(qq,radius,length,zz,delrho,zi)
171                summ = yyy + summ
172                ii+=1
173        while (ii<nord)                         // end of loop over quadrature points
174//   
175// calculate value of integral to return
176   answer = (vb-va)/2.0*summ
177     
178//  contrast^2 is included in integration rad_kernel
179//      answer *= delrho*delrho
180//normalize by polydisperse volume
181// now volume depends on polydisperse Length - so normalize by the FIRST moment
182// 1st moment = volume!
183        vpoly = Pi*(radius)^2*length
184//Divide by vol, since volume has been "un-normalized" out
185        answer /= vpoly
186//convert to [cm-1]
187        answer *= 1.0e8
188//scale
189        answer *= scale
190// add in the background
191        answer += bkg
192
193        Return (answer)
194End             //End of function PolyRadCylForm()
195
196Function len_kernel(qw,rad,len_avg,zz,delrho,len)
197        Variable qw,rad,len_avg,zz,delrho,len
198       
199        Variable Pq,vcyl,dl
200       
201        //calculate the orientationally averaged P(q) for the input rad
202        //this is correct - see K&C (1983) or Lin &Tsao JACryst (1996)29 170.
203        Make/O/n=5 kernpar
204        Wave kp = kernpar
205        kp[0] = 1               //scale fixed at 1
206        kp[1] = rad
207        kp[2] = len
208        kp[3] = delrho
209        kp[4] = 0               //bkg fixed at 0
210       
211#if exists("CylinderFormX")
212        Pq = CylinderFormX(kp,qw)
213#else
214        Pq = fCylinderForm(kp,qw)
215#endif
216       
217        // undo the normalization that CylinderForm does
218        //CylinderForm returns P(q)/V, we want P(q)
219        vcyl=Pi*rad*rad*len
220        Pq *= vcyl
221        //un-convert from [cm-1]
222        Pq /= 1.0e8
223       
224        // calculate normalized distribution at len value
225        dl = Schulz_Point_pollen(len,len_avg,zz)
226       
227        return (Pq*dl) 
228End
229
230Function Schulz_Point_pollen(x,avg,zz)
231        Variable x,avg,zz
232       
233        Variable dr
234       
235        dr = zz*ln(x) - gammln(zz+1)+(zz+1)*ln((zz+1)/avg)-(x/avg*(zz+1))
236       
237        return (exp(dr))
238End
239
240//wrapper to calculate the smeared model as an AAO-Struct
241// fills the struct and calls the ususal function with the STRUCT parameter
242//
243// used only for the dependency, not for fitting
244//
245Function fSmearedCyl_PolyLength(coefW,yW,xW)
246        Wave coefW,yW,xW
247       
248        String str = getWavesDataFolder(yW,0)
249        String DF="root:"+str+":"
250       
251        WAVE resW = $(DF+str+"_res")
252       
253        STRUCT ResSmearAAOStruct fs
254        WAVE fs.coefW = coefW   
255        WAVE fs.yW = yW
256        WAVE fs.xW = xW
257        WAVE fs.resW = resW
258       
259        Variable err
260        err = SmearedCyl_PolyLength(fs)
261       
262        return (0)
263End
264
265// this is all there is to the smeared calculation!
266Function SmearedCyl_PolyLength(s) :FitFunc
267        Struct ResSmearAAOStruct &s
268
269//      the name of your unsmeared model (AAO) is the first argument
270        Smear_Model_20(Cyl_PolyLength,s.coefW,s.xW,s.yW,s.resW)
271
272        return(0)
273End
274       
Note: See TracBrowser for help on using the repository browser.