source: sans/Release/trunk/NCNR_SANS_Utilities/Single_Model_Template/My_Model.ipf @ 339

Last change on this file since 339 was 323, checked in by ajj, 15 years ago

Add Single Model Template to new Dev tree

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