source: sans/Analysis/branches/ajj_23APR07/IGOR_Package_Files/Put in User Procedures/SANS_Models_v3.00/Packages/SumModel/SumSANSModels.ipf @ 200

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

Changed 2D model calculations to use a FIXED 25 integration points in an effort to get the number of integration points out of the fitting coefficient wave.

Added "Freeze Model" back to the SANS Models menu

Added the 2D model functions to the list on the model picker

Minor tweaks to the wrapper

File size: 11.4 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2
3///////////////////////////////
4//
5//      17 DEC 04 SRK
6//
7// SumSANSModels.ipf
8//
9//      Procedures to sum two SANS model into a function that can
10// be used to fit SANS data sets. does this by soliciting input
11// from the user through a panel. Needs the function names, the
12// names of the coefficients, and the number of parameters
13// (same information for each model)
14//
15//      Then the "Plot" button on the panel creates the function
16// based on the panel selections. The model is named "Sum_Model",
17// the coef/params are "coef_sum" and "parameters_sum", respectively
18//
19//      The two original coef/param waves are tacked together. No attempt
20// is made to remove duplicate parameters. This is up to the user
21// to keep appropriate values fixed. (especially background)
22//
23//      Changing the popup values will not immediately change the output
24// function. The model must be re-plotted to "pick up" these new
25// selections.
26//
27// JUL 2007
28// - data folder aware
29// - AAO, structure aware
30// (smeared fitting still broken)
31// - created data folders are now buried in myGlobals
32//
33///////////////////////////////
34
35//create the panel as needed
36//
37Proc Init_SumModelPanel()
38        DoWindow/F Sum_Model_Panel
39        if(V_flag==0)
40                if(!DataFolderExists("root:myGlobals"))
41                        NewDataFolder root:myGlobals
42                endif
43                NewDataFolder/O root:myGlobals:SumModel
44                InitSMPGlobals()
45                Sum_Model_Panel()
46        endif
47end
48
49// create the globals that the panel needs
50//
51Function InitSMPGlobals()
52        Variable/G root:myGlobals:SumModel:gNParMod1=0
53        Variable/G root:myGlobals:SumModel:gNParMod2=0
54end
55
56Window Sum_Model_Panel() : Panel
57        PauseUpdate; Silent 1           // building window...
58        NewPanel /W=(670,75,900,450)/K=1
59        DoWindow/C Sum_Model_Panel
60        SetDrawLayer UserBack
61        SetDrawEnv fstyle= 1
62        DrawText 14,17,"Sum Two Model Functions"
63        PopupMenu popup1_0,pos={15,69},size={131,20},title="Model"
64        PopupMenu popup1_0,mode=1,popvalue="Pick model 1",value= #"SumModelPopupList()"
65        PopupMenu popup2_0,pos={15,190},size={131,20},title="Model"
66        PopupMenu popup2_0,mode=1,popvalue="Pick model 2",value= #"SumModelPopupList()"
67        PopupMenu popup1_1,pos={15,98},size={173,20},title="Coef"
68        PopupMenu popup1_1,mode=1,popvalue="Pick coef for model 1",value= #"CoefPopupList()"
69        PopupMenu popup2_1,pos={15,221},size={173,20},title="Coef"
70        PopupMenu popup2_1,mode=1,popvalue="Pick coef for model 2",value= #"CoefPopupList()"
71        GroupBox group1,pos={5,50},size={216,107},title="Function # 1"
72        SetVariable setvar1,pos={14,128},size={140,15},title="# of Parameters"
73        SetVariable setvar1,limits={0,20,1},value= root:myGlobals:SumModel:gNParMod1
74        GroupBox group2,pos={5,171},size={216,107},title="Function # 2"
75        SetVariable setvar2,pos={14,249},size={140,15},title="# of Parameters"
76        SetVariable setvar2,limits={0,20,1},value= root:myGlobals:SumModel:gNParMod2
77        Button button0,pos={36,299},size={150,20},proc=PlotSumButtonProc,title="Plot Summed Model"
78        Button button1,pos={15,330},size={190,20},proc=PlotSmearedSumButtonProc,title="Plot Smeared Summed Model"
79        Button button2,pos={190,23},size={25,20},proc=Sum_HelpButtonProc,title="?"
80EndMacro
81
82// show the available models
83// but not the smeared versions
84// not the f*
85// not the *X XOPS
86//
87// KIND:10 should show only user-defined curve fitting functions
88// - not XOPs
89// - not other user-defined functions
90Function/S SumModelPopupList()
91        String list,tmp
92        list = FunctionList("!Smear*",";","KIND:10")            //don't show smeared models
93       
94        list = RemoveFromList("Sum_Model", list  ,";")
95       
96        tmp = FunctionList("*_proto",";","KIND:10")             //prototypes
97        list = RemoveFromList(tmp, list  ,";")
98
99//      Print list
100        tmp = GrepList(FunctionList("f*",";","KIND:10"),"^f")   
101//      tmp = FunctionList("f*",";","KIND:10")          //point calculations
102       
103//      Print tmp
104       
105        list = RemoveFromList(tmp, list  ,";")
106       
107        // this should be a null string with KIND:10
108        tmp = FunctionList("*X",";","KIND:10")          //XOPs, also point calculations
109        list = RemoveFromList(tmp, list  ,";")
110       
111        return(list)
112End
113
114// show all the appropriate coefficient waves
115Function/S CoefPopupList()
116        String list
117        list = WaveList("coef*",";","")
118        list = RemoveFromList("coef_sum", list  ,";")
119        return(list)
120End
121
122//button procedure
123Function PlotSumButtonProc(ctrlName) : ButtonControl
124        String ctrlName
125
126        Execute "PlotSum_Model()"
127End
128
129//button procedure
130Function PlotSmearedSumButtonProc(ctrlName) : ButtonControl
131        String ctrlName
132
133        Execute "PlotSmeared_Sum_Model()"
134End
135
136Function Sum_HelpButtonProc(ctrlName) : ButtonControl
137        String ctrlName
138        DisplayHelpTopic "Sum SANS Models"
139End
140
141//////////////////////////////
142
143
144// this is the procedure that sets up the plot, just like
145// a normal procedure for a model function
146// - there's just a bit more going on, since it needs to
147// build things up based on the settings of the panel
148Proc PlotSum_Model(num,qmin,qmax)                                               
149        Variable num=128,qmin=0.001,qmax=0.7
150        Prompt num "Enter number of data points for model: "
151        Prompt qmin "Enter minimum q-value (^-1) for model: "
152        Prompt qmax "Enter maximum q-value (^-1) for model: "
153       
154        Make/O/D/n=(num) xwave_sum,ywave_sum                                   
155        xwave_sum = alog(log(qmin) + x*((log(qmax)-log(qmin))/num))
156       
157        //make the coefficients and parameters based on the panel values       
158        Variable nParam,n1,n2
159        n1 = root:myGlobals:SumModel:gNParMod1
160        n2 = root:myGlobals:SumModel:gNParMod2
161        nParam = n1 + n2
162        if(n1==0 || n2==0 || nparam==0)
163                Abort "# of parameters must not be zero"
164        endif
165        // n is ok, keep extra copy so changing panel will not affect functions
166        Variable/G root:myGlobals:SumModel:gN1=n1
167        Variable/G root:myGlobals:SumModel:gN2=n2
168       
169        // these are the function names - make global so the fit function
170        // can find them
171        ControlInfo/W=Sum_Model_Panel popup1_0
172        String/G root:myGlobals:SumModel:gModelStr1=S_Value
173        ControlInfo/W=Sum_Model_Panel popup2_0
174        String/G root:myGlobals:SumModel:gModelStr2=S_Value
175       
176        //these are the coefficent waves - local only
177        ControlInfo/W=Sum_Model_Panel popup1_1
178        String/G root:coefStr1=S_Value
179        ControlInfo/W=Sum_Model_Panel popup2_1
180        String/G root:coefStr2=S_Value
181       
182        Make/O/D/N=(nParam) coef_sum   
183        coef_sum[0,(n1-1)] = $coefStr1
184        coef_sum[n1,(n1+n2-1)] = $coefStr2[p-n1]
185                                               
186        make/o/t/N=(nParam) parameters_sum
187        String paramStr1 = "parameters"+coefStr1[4,strlen(coefStr1)-1]
188        String paramStr2 = "parameters"+coefStr2[4,strlen(coefStr2)-1]
189        parameters_sum[0,(n1-1)] = $paramStr1
190        parameters_sum[n1,(n1+n2-1)] = $paramStr2[p-n1]
191       
192        Edit parameters_sum,coef_sum
193       
194        Variable/G root:g_sum
195        g_sum := Sum_Model(coef_sum,ywave_sum,xwave_sum)                       
196        Display ywave_sum vs xwave_sum                                                 
197        ModifyGraph log=1,marker=29,msize=2,mode=4                     
198        Label bottom "q (\\S-1\\M)"
199        Label left "Intensity (cm\\S-1\\M)"
200        Legend                                 
201        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
202       
203        AddModelToStrings("Sum_Model","coef_sum","sum")
204End
205
206// - sets up a dependency to a wrapper, not the actual SmearedModelFunction
207// the usual macro for plotting the smeared model, updated
208// to take input from the panel
209//
210// - somewhat confusing as the unsmeared coefficients are in root:
211// and all of the newly created smeared waves and coefficients are in the
212// selected data folder
213//
214Proc PlotSmeared_Sum_Model(str)                                                         
215        String str
216        Prompt str,"Pick the data folder containing the resolution you want",popup,getAList(4)
217       
218        // if any of the resolution waves are missing => abort
219        if(ResolutionWavesMissingDF(str))               //updated to NOT use global strings (in GaussUtils)
220                Abort
221        endif
222       
223        SetDataFolder $("root:"+str)
224       
225        //make the coefficients and parameters based on the panel values       
226        Variable nParam,n1,n2
227        n1 = root:myGlobals:SumModel:gNParMod1
228        n2 = root:myGlobals:SumModel:gNParMod2
229        nParam = n1 + n2
230        if(n1==0 || n2==0 || nparam==0)
231                Abort "# of parameters must not be zero"
232        endif
233        // n is ok, keep extra copy so changing panel will not affect functions
234        Variable/G root:myGlobals:SumModel:gN1=n1
235        Variable/G root:myGlobals:SumModel:gN2=n2
236       
237        // these are the function names - make global so the fit function
238        // can find them
239        ControlInfo/W=Sum_Model_Panel popup1_0
240        String/G root:myGlobals:SumModel:gModelStr1=S_Value
241        ControlInfo/W=Sum_Model_Panel popup2_0
242        String/G root:myGlobals:SumModel:gModelStr2=S_Value
243       
244        //these are the coefficent waves - local only, in the current data folder!
245        ControlInfo/W=Sum_Model_Panel popup1_1
246        String/G coefStr1=S_Value
247        ControlInfo/W=Sum_Model_Panel popup2_1
248        String/G coefStr2=S_Value
249       
250        Make/O/D/N=(nParam) smear_coef_sum     
251        smear_coef_sum[0,(n1-1)] = $("root:"+coefStr1)
252        smear_coef_sum[n1,(n1+n2-1)] = $("root:"+coefStr2)[p-n1]
253                                               
254        make/o/t/N=(nParam) smear_parameters_sum
255        String paramStr1 = "parameters"+coefStr1[4,strlen(coefStr1)-1]
256        String paramStr2 = "parameters"+coefStr2[4,strlen(coefStr2)-1]
257        smear_parameters_sum[0,(n1-1)] = $("root:"+paramStr1)
258        smear_parameters_sum[n1,(n1+n2-1)] = $("root:"+paramStr2)[p-n1]
259                               
260        Edit smear_parameters_sum,smear_coef_sum                                       
261       
262        // output smeared intensity wave, dimensions are identical to experimental QSIG values
263        // make extra copy of experimental q-values for easy plotting
264        Duplicate/O $(str+"_q") smeared_sum,smeared_qvals                               
265        SetScale d,0,0,"1/cm",smeared_sum                                                       
266                                       
267        Variable/G gs_sum=0
268        gs_sum := fSmeared_Sum_Model(smear_coef_sum,smeared_sum,smeared_qvals)  //this wrapper fills the STRUCT
269       
270        Display smeared_sum vs $(str+"_q")                                                                     
271        ModifyGraph log=1,marker=29,msize=2,mode=4
272        Label bottom "q (\\S-1\\M)"
273        Label left "Intensity (cm\\S-1\\M)"
274        Legend
275        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
276       
277        SetDataFolder root:
278        AddModelToStrings("Smeared_Sum_Model","smear_coef_sum","sum")
279End
280
281
282// this is the actual fitting function that is the
283// sum of the two selected models.
284//
285// this is an AAO function, there is no XOP version
286// since it should be the sum of two XOPs
287//
288Function Sum_Model(w,yw,xw) : FitFunc
289        Wave w,yw,xw
290       
291        SVAR funcStr1=root:myGlobals:SumModel:gModelStr1                //string names of the functions, set by the macro
292        SVAR funcStr2=root:myGlobals:SumModel:gModelStr2
293        NVAR n1=root:myGlobals:SumModel:gN1                     //number of coefficients, set by the macro
294        NVAR n2=root:myGlobals:SumModel:gN2
295       
296        Variable retVal
297       
298        FUNCREF SANSModelAAO_proto f1 = $funcStr1               //convert str to FCN
299        FUNCREF SANSModelAAO_proto f2 = $funcStr2
300        // make temporary coefficient waves for each model
301        Make/O/D/N=(n1) temp_cw1
302        Make/O/D/N=(n2) temp_cw2
303        temp_cw1 = w[p]
304        temp_cw2 = w[p+n1]
305       
306        // calculate the sum of each of the AAO functions
307        Duplicate/O xw tmp_sum_yw1,tmp_sum_yw2
308       
309        f1(temp_cw1,tmp_sum_yw1,xw)
310        f2(temp_cw2,tmp_sum_yw2,xw)
311        yw = tmp_sum_yw1 + tmp_sum_yw2
312       
313        return(0)
314end
315
316// this is all there is to the smeared calculation!
317Function Smeared_Sum_Model(s) : FitFunc
318        Struct ResSmearAAOStruct &s
319
320//      the name of your unsmeared model (AAO) is the first argument
321        Smear_Model_20(Sum_Model,s.coefW,s.xW,s.yW,s.resW)
322
323        return(0)
324End
325
326//wrapper to calculate the smeared model as an AAO-Struct
327// fills the struct and calls the ususal function with the STRUCT parameter
328//
329// used only for the dependency, not for fitting
330//
331Function fSmeared_Sum_Model(coefW,yW,xW)
332        Wave coefW,yW,xW
333       
334        String str = getWavesDataFolder(yW,0)
335        String DF="root:"+str+":"
336       
337        WAVE resW = $(DF+str+"_res")
338       
339        STRUCT ResSmearAAOStruct fs
340        WAVE fs.coefW = coefW   
341        WAVE fs.yW = yW
342        WAVE fs.xW = xW
343        WAVE fs.resW = resW
344       
345        Variable err
346        err = Smeared_Sum_Model(fs)
347       
348        return (0)
349End
350
351//procedures to clean up after itself
352Function UnloadSumModel()
353        if (WinType("Sum_Model_Panel") == 7)
354                DoWindow/K Sum_Model_Panel
355        endif
356        Execute/P "DELETEINCLUDE \"SumSANSModels\""
357        Execute/P "COMPILEPROCEDURES "
358end
359
360Menu "Macros"
361        Submenu "Packages"
362                "Unload Sum SANS Models", UnloadSumModel()
363        End
364end
Note: See TracBrowser for help on using the repository browser.