source: sans/Dev/trunk/NCNR_SANS_Utilities/Single_Model_Template/My_Model_v40.ipf @ 740

Last change on this file since 740 was 589, checked in by srkline, 13 years ago

Updated the SingleModelTemplate? to be compatible with the new release

File size: 6.8 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma IgorVersion=6.0
3
4////////////////////////////////////////////////////
5//
6// A template for writing your own scattering functions
7//
8// FIVE things to do:
9// 1) identify the equation and the free variables before coding
10//
11// --then you may start coding ---
12//
13// 2) change coef_MyModel to have the coefficients (defined in w[])
14//    in the exact order you have specified
15//    - this change is one line, search for "CH#1"
16// 3) change parameters_MyModel to has parameter names, that match
17//    the coefficients - this line follows the coefficients of step 1
18//          - this is "CH#2"
19// 4) change the function MyModel() to calculate what you want
20//    w[N] must contain all the fitting parameters - search for "CH#3"
21// 5) repeat steps 2 and 3 (copy/paste) changing the RHS of the same
22//    lines in the macro for the smeared model calculation
23//    - search for "CH#4", and the following line
24//
25// - then, all you need to do is (re)plot the macro from the Macros menu
26//
27// As-is, the model calculates a form factor for a uniform sphere
28//
29// v 2.00 -- 01 MAY 2006 SRK
30// v 2.1 -- Jul 2007 SRK
31//                              converted to use structures, AAO, like the rest of the package
32// v 2.2 -- Nov 2009 SRK
33//                              updated AddModelToStrings() for the new release
34//
35////////////////////////////////////////////////////
36
37//this macro sets up all the necessary parameters and waves that are
38//needed to calculate the model function.
39//
40Macro PlotMyModel(num,qmin,qmax)
41        Variable num=200, qmin=0.001, qmax=0.7
42        Prompt num "Enter number of data points for model: "
43        Prompt qmin "Enter minimum q-value (^-1) for model: "
44        Prompt qmax "Enter maximum q-value (^-1) for model: "
45//
46        Make/O/D/n=(num) xwave_MyModel, ywave_MyModel
47        xwave_MyModel =  alog(log(qmin) + x*((log(qmax)-log(qmin))/num))
48        Make/O/D coef_MyModel = {1.,60,1e-6,6.3e-6,0.01}                        //CH#1
49        make/o/t parameters_MyModel = {"scale","Radius (A)","SLD sphere (A-2)","SLD solvent (A-2)","bkgd (cm-1)"}       //CH#2
50        Edit parameters_MyModel, coef_MyModel
51       
52        Variable/G root:g_MyModel=0
53        root:g_MyModel := MyModel(coef_MyModel, ywave_MyModel, xwave_MyModel)
54        Display ywave_MyModel vs xwave_MyModel
55        ModifyGraph marker=29, msize=2, mode=4
56        ModifyGraph log=1,grid=1,mirror=2
57        Label bottom "q (\\S-1\\M) "
58        Label left "I(q) (cm\\S-1\\M)"
59        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
60       
61        AddModelToStrings("MyModel","coef_MyModel","parameters_MyModel","MyModel")
62//
63End
64
65//
66//this macro sets up all the necessary parameters and waves that are
67//needed to calculate the  smeared model function.
68//
69//no input parameters are necessary, it MUST use the experimental q-values
70// from the experimental data read in from an AVE/QSIG data file
71////////////////////////////////////////////////////
72// - sets up a dependency to a wrapper, not the actual SmearedModelFunction
73Macro PlotSmearedMyModel(str)                                                           
74        String str
75        Prompt str,"Pick the data folder containing the resolution you want",popup,getAList(4)
76       
77        // if any of the resolution waves are missing => abort
78        if(ResolutionWavesMissingDF(str))               //updated to NOT use global strings (in GaussUtils)
79                Abort
80        endif
81       
82        SetDataFolder $("root:"+str)
83       
84        // Setup parameter table for model function
85        Make/O/D smear_coef_MyModel = {1.,60,1e-6,6.3e-6,0.01}          //CH#4 change this line and the following line
86        make/o/t smear_parameters_MyModel = {"scale","Radius (A)","SLD sphere (A-2)","SLD solvent (A-2)","bkgd (cm-1)"}
87        Edit smear_parameters_MyModel,smear_coef_MyModel                                        //display parameters in a table
88       
89        // output smeared intensity wave, dimensions are identical to experimental QSIG values
90        // make extra copy of experimental q-values for easy plotting
91        Duplicate/O $(str+"_q") smeared_MyModel,smeared_qvals
92        SetScale d,0,0,"1/cm",smeared_MyModel
93                                       
94        Variable/G gs_MyModel=0
95        gs_MyModel := fSmearedMyModel(smear_coef_MyModel,smeared_MyModel,smeared_qvals) //this wrapper fills the STRUCT
96       
97        Display smeared_MyModel vs smeared_qvals
98        ModifyGraph log=1,marker=29,msize=2,mode=4
99        Label bottom "q (\\S-1\\M)"
100        Label left "I(q) (cm\\S-1\\M)"
101        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
102       
103        SetDataFolder root:
104        AddModelToStrings("SmearedMyModel","smear_coef_MyModel","smear_parameters_MyModel","MyModel")
105End
106
107
108// nothing to change here
109//
110//AAO version, uses XOP if available
111// simply calls the original single point calculation with
112// a wave assignment (this will behave nicely if given point ranges)
113Function MyModel(cw,yw,xw) : FitFunc
114        Wave cw,yw,xw
115       
116#if exists("MyModelX")
117        yw = MyModelX(cw,xw)
118#else
119        yw = fMyModel(cw,xw)
120#endif
121        return(0)
122End
123
124
125//CH#3
126// you should write your function to calculate the intensity
127// for a single q-value (that's the input parameter x)
128// based on the wave (array) of parameters that you send it (w)
129//
130// unsmeared model calculation
131//
132Function fMyModel(w,x) : FitFunc
133        Wave w
134        Variable x
135       
136        // variables are:                                                       
137        //[0] scale
138        //[1] radius ()
139        //[2] sld sphere (-2)
140        //[3] sld solv
141        //[4] background (cm-1)
142       
143        Variable scale,radius,delrho,bkg,sldSph,sldSolv,qval           
144        scale = w[0]
145        radius = w[1]
146        sldSph = w[2]
147        sldSolv = w[3]
148        bkg = w[4]
149       
150        delrho = sldSph - sldSolv       
151       
152        qval = x                //rename the input q-value, purely for readability
153       
154        // calculates scale * f^2/Vol where f=Vol*3*delrho*((sin(qr)-qrcos(qr))/qr^3
155        // and is rescaled to give [=] cm^-1
156       
157        Variable bes,f,vol,f2
158        //
159        //handle q==0 separately to avoid divide-by-zero in the Bessel func
160        If(qval==0)
161                f = 4/3*pi*radius^3*delrho*delrho*scale*1e8 + bkg
162                return(f)
163        Endif
164       
165        bes = 3*(sin(qval*radius)-qval*radius*cos(qval*radius))/qval^3/radius^3
166        vol = 4*pi/3*radius^3
167        f = vol*bes*delrho              // [=] 
168        // normalize to single particle volume and convert to 1/cm
169        f2 = f * f / vol * 1e8          // [=] 1/cm
170       
171        return (scale*f2+bkg)   // Scale, then add in the background
172       
173End
174
175//CH#4 
176///////////////////////////////////////////////////////////////
177// smeared model calculation
178//
179// you don't need to do anything with this function, as long as
180// your MyModel works correctly, you get the resolution-smeared
181// version for free.
182//
183// this is all there is to the smeared model calculation!
184Function SmearedMyModel(s) : FitFunc
185        Struct ResSmearAAOStruct &s
186
187//      the name of your unsmeared model (AAO) is the first argument
188        Smear_Model_20(MyModel,s.coefW,s.xW,s.yW,s.resW)
189
190        return(0)
191End
192
193
194///////////////////////////////////////////////////////////////
195
196
197// nothing to change here
198//
199//wrapper to calculate the smeared model as an AAO-Struct
200// fills the struct and calls the ususal function with the STRUCT parameter
201//
202// used only for the dependency, not for fitting
203//
204Function fSmearedMyModel(coefW,yW,xW)
205        Wave coefW,yW,xW
206       
207        String str = getWavesDataFolder(yW,0)
208        String DF="root:"+str+":"
209       
210        WAVE resW = $(DF+str+"_res")
211       
212        STRUCT ResSmearAAOStruct fs
213        WAVE fs.coefW = coefW   
214        WAVE fs.yW = yW
215        WAVE fs.xW = xW
216        WAVE fs.resW = resW
217       
218        Variable err
219        err = SmearedMyModel(fs)
220       
221        return (0)
222End
Note: See TracBrowser for help on using the repository browser.