source: sans/Analysis/branches/ajj_23APR07/IGOR_Package_Files/Put in User Procedures/SANS_Models_v3.00/Sphere.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: 6.5 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma IgorVersion = 6.0
3
4////////////////////////////////////////////////
5// this procedure calculates the form factor of a sphere
6//
7// 06 NOV 98 SRK
8//
9// modified June 2007 to calculate all-at-once
10// and to use a structure for resolution information
11//
12////////////////////////////////////////////////
13
14Proc PlotSphereForm(num,qmin,qmax)                                             
15        Variable num=128,qmin=0.001,qmax=0.7
16        Prompt num "Enter number of data points for model: "
17        Prompt qmin "Enter minimum q-value (^-1) for model: "
18        Prompt qmax "Enter maximum q-value (^-1) for model: "
19       
20        Make/O/D/n=(num) xwave_sf,ywave_sf                                     
21        xwave_sf = alog(log(qmin) + x*((log(qmax)-log(qmin))/num))                                     
22        Make/O/D coef_sf = {1.,60,1e-6,0.01}                                           
23        make/o/t parameters_sf = {"scale","Radius (A)","contrast (-2)","bkgd (cm-1)"}         
24        Edit parameters_sf,coef_sf     
25        Variable/G root:g_sf=0                 
26        root:g_sf := SphereForm(coef_sf,ywave_sf,xwave_sf)      // AAO calculation, "fake" dependency
27//      ywave_sf := SphereForm(coef_sf,xwave_sf)                //point calculation
28        Display ywave_sf vs xwave_sf                                                   
29        ModifyGraph log=1,marker=29,msize=2,mode=4                     
30        Label bottom "q (\\S-1\\M)"
31        Label left "Intensity (cm\\S-1\\M)"                                     
32        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
33       
34        AddModelToStrings("SphereForm","coef_sf","sf")
35End
36
37// - sets up a dependency to a wrapper, not the actual SmearedModelFunction
38Proc PlotSmearedSphereForm(str)                                                         
39        String str
40        Prompt str,"Pick the data folder containing the resolution you want",popup,getAList(4)
41       
42        // if any of the resolution waves are missing => abort
43        if(ResolutionWavesMissingDF(str))               //updated to NOT use global strings (in GaussUtils)
44                Abort
45        endif
46       
47        SetDataFolder $("root:"+str)
48       
49        // Setup parameter table for model function
50        make/o/t smear_parameters_sf = {"scale","Radius (A)","contrast (-2)","bkgd (cm-1)"}
51        Make/O/D smear_coef_sf = {1.,60,1e-6,0.0}                                       
52        Edit smear_parameters_sf,smear_coef_sf                                 
53       
54        Duplicate/O $(str+"_q") smeared_sf,smeared_qvals
55        SetScale d,0,0,"1/cm",smeared_sf                                       
56               
57        Variable/G gs_sf=0
58        gs_sf := fSmearedSphereForm(smear_coef_sf,smeared_sf,smeared_qvals)     //this wrapper fills the STRUCT
59        Display smeared_sf vs smeared_qvals                                                             
60       
61        ModifyGraph log=1,marker=29,msize=2,mode=4
62        Label bottom "q (\\S-1\\M)"
63        Label left "Intensity (cm\\S-1\\M)"
64        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
65       
66        SetDataFolder root:
67        AddModelToStrings("SmearedSphereForm","smear_coef_sf","sf")
68End
69
70//AAO version, uses XOP if available
71// simply calls the original single point calculation with
72// a wave assignment (this will behave nicely if given point ranges)
73Function SphereForm(cw,yw,xw) : FitFunc
74        Wave cw,yw,xw
75       
76#if exists("SphereFormX")
77        yw = SphereFormX(cw,xw)
78#else
79        yw = fSphereForm(cw,xw)
80#endif
81        return(0)
82End
83
84///////////////////////////////////////////////////////////////
85// unsmeared model calculation
86// this is a "regular" calculation at a single q-value
87///////////////////////////
88Function fSphereForm(w,x)                                       
89        Wave w
90        Variable x
91       
92        // variables are:                                                       
93        //[0] scale
94        //[1] radius ()
95        //[2] delrho (-2)
96        //[3] background (cm-1)
97       
98        Variable scale,radius,delrho,bkg                               
99        scale = w[0]
100        radius = w[1]
101        delrho = w[2]
102        bkg = w[3]
103       
104        // calculates scale * f^2/Vol where f=Vol*3*delrho*((sin(qr)-qrcos(qr))/qr^3
105        // and is rescaled to give [=] cm^-1
106       
107        Variable bes,f,vol,f2
108        //
109        //handle q==0 separately
110        If(x==0)
111                f = 4/3*pi*radius^3*delrho*delrho*scale*1e8 + bkg
112                return(f)
113        Endif
114       
115        bes = 3*(sin(x*radius)-x*radius*cos(x*radius))/x^3/radius^3
116        vol = 4*pi/3*radius^3
117        f = vol*bes*delrho              // [=] 
118        // normalize to single particle volume, convert to 1/cm
119        f2 = f * f / vol * 1.0e8                // [=] 1/cm
120       
121        return (scale*f2+bkg)   // Scale, then add in the background
122End
123
124
125//wrapper to calculate the smeared model as an AAO-Struct
126// fills the struct
127// calls the ususal function with the STRUCT parameter
128//
129// -- problem- this assumes USANS w/no matrix
130// -- needs experimental q-values to properly calculate the fitted curve?? is this true?
131// -- how to fill the struct if the type of data is unknown?
132// -- where do I find the matrix?
133//
134Function fSmearedSphereForm(coefW,yW,xW)
135        Wave coefW,yW,xW
136       
137        String str = getWavesDataFolder(yW,0)
138        String DF="root:"+str+":"
139       
140        WAVE resW = $(DF+str+"_res")
141       
142        STRUCT ResSmearAAOStruct fs
143        WAVE fs.coefW = coefW           //is this the proper way to populate? seems redundant...
144        WAVE fs.yW = yW
145        WAVE fs.xW = xW
146        WAVE fs.resW = resW
147       
148        Variable err
149        err = SmearedSphereForm(fs)
150       
151        return (0)
152End
153
154// smeared calculation, AAO and using a structure...
155// defined as as STRUCT, there can never be a dependency linked directly to this function
156// - so set a dependency to the wrapper
157//
158// like the unsmeared function, AAO is equivalent to a wave assignment to the point calculation
159// - but now the function passed is an AAO function
160//
161// Smear_Model_20() takes care of what calculation is done, depending on the resolution information
162//
163//
164Function SmearedSphereForm(s) : FitFunc
165        Struct ResSmearAAOStruct &s
166
167////the name of your unsmeared model is the first argument
168        Smear_Model_20(SphereForm,s.coefW,s.xW,s.yW,s.resW)
169
170        return(0)
171End
172 
173
174// wrapper to do the desired fit
175//
176// str is the data folder for the desired data set
177//
178// -- this looks like something that can be made rather generic rather easily
179//
180//Function SphereFitWrapper(str)
181//      String str
182//             
183//      SetDataFolder root:
184//      String DF="root:"+str+":"
185//     
186//      Struct ResSmearAAOStruct fs
187//      WAVE resW = $(DF+str+"_res")           
188//      WAVE fs.resW =  resW
189//     
190//      WAVE cw=$(DF+"smear_coef_sf")
191//      WAVE yw=$(DF+str+"_i")
192//      WAVE xw=$(DF+str+"_q")
193//      WAVE sw=$(DF+str+"_s")
194//     
195//      Duplicate/O yw $(DF+"FitYw")
196//     
197//      //can't use execute if /STRC is needed since structure instance is not a global!
198//      //don't use the auto-destination with no flag, it doesn't appear to work correctly
199//      //force it to use a wave of identical length, at least - no guarantee that the q-values
200//      // will be the same? be sure that the smearing iterpolates
201//      //
202//      // ?? how can I get the hold string in correctly?? - from a global? - no, as string function
203//      //
204//      FuncFit/H=getHoldStr() /NTHR=0 SmearedSphereForm, cw, yw /X=xw /W=sw /I=1 /STRC=fs
205////    FuncFit/H=getHoldStr() /NTHR=0 SphereForm cw, yw /X=xw /W=sw /I=1 /D=$(DF+"FitYw")
206//
207////    FuncFit/H="0010"/NTHR=0 SmearedSphereForm cw, yw /X=xw /W=sw /I=1 /D=$(DF+"FitYw") /STRC=fs
208//      Wave fityw =  $(DF+"FitYw")
209//      fs.yW = fityw
210//      SmearedSphereForm(fs)
211//      AppendToGraph fityw vs xw
212//     
213//      print "V_chisq = ",V_chisq
214//      print cw
215//      WAVE w_sigma
216//      print w_sigma
217//     
218//      return(0)
219//End
220//
221//Function/S getHoldStr()
222//
223//      String str="0010"
224//      return str
225//End
Note: See TracBrowser for help on using the repository browser.