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

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

Fixing keyWord string pairs for model functions as errors are found.

File size: 11.3 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        tmp = FunctionList("f*",";","KIND:10")          //point calculations
100        list = RemoveFromList(tmp, list  ,";")
101       
102        // this should be a null string with KIND:10
103        tmp = FunctionList("*X",";","KIND:10")          //XOPs, also point calculations
104        list = RemoveFromList(tmp, list  ,";")
105       
106        return(list)
107End
108
109// show all the appropriate coefficient waves
110Function/S CoefPopupList()
111        String list
112        list = WaveList("coef*",";","")
113        list = RemoveFromList("coef_sum", list  ,";")
114        return(list)
115End
116
117//button procedure
118Function PlotSumButtonProc(ctrlName) : ButtonControl
119        String ctrlName
120
121        Execute "PlotSum_Model()"
122End
123
124//button procedure
125Function PlotSmearedSumButtonProc(ctrlName) : ButtonControl
126        String ctrlName
127
128        Execute "PlotSmeared_Sum_Model()"
129End
130
131Function Sum_HelpButtonProc(ctrlName) : ButtonControl
132        String ctrlName
133        DisplayHelpTopic "Sum SANS Models"
134End
135
136//////////////////////////////
137
138
139// this is the procedure that sets up the plot, just like
140// a normal procedure for a model function
141// - there's just a bit more going on, since it needs to
142// build things up based on the settings of the panel
143Proc PlotSum_Model(num,qmin,qmax)                                               
144        Variable num=128,qmin=0.001,qmax=0.7
145        Prompt num "Enter number of data points for model: "
146        Prompt qmin "Enter minimum q-value (^-1) for model: "
147        Prompt qmax "Enter maximum q-value (^-1) for model: "
148       
149        Make/O/D/n=(num) xwave_sum,ywave_sum                                   
150        xwave_sum = alog(log(qmin) + x*((log(qmax)-log(qmin))/num))
151       
152        //make the coefficients and parameters based on the panel values       
153        Variable nParam,n1,n2
154        n1 = root:myGlobals:SumModel:gNParMod1
155        n2 = root:myGlobals:SumModel:gNParMod2
156        nParam = n1 + n2
157        if(n1==0 || n2==0 || nparam==0)
158                Abort "# of parameters must not be zero"
159        endif
160        // n is ok, keep extra copy so changing panel will not affect functions
161        Variable/G root:myGlobals:SumModel:gN1=n1
162        Variable/G root:myGlobals:SumModel:gN2=n2
163       
164        // these are the function names - make global so the fit function
165        // can find them
166        ControlInfo/W=Sum_Model_Panel popup1_0
167        String/G root:myGlobals:SumModel:gModelStr1=S_Value
168        ControlInfo/W=Sum_Model_Panel popup2_0
169        String/G root:myGlobals:SumModel:gModelStr2=S_Value
170       
171        //these are the coefficent waves - local only
172        ControlInfo/W=Sum_Model_Panel popup1_1
173        String/G root:coefStr1=S_Value
174        ControlInfo/W=Sum_Model_Panel popup2_1
175        String/G root:coefStr2=S_Value
176       
177        Make/O/D/N=(nParam) coef_sum   
178        coef_sum[0,(n1-1)] = $coefStr1
179        coef_sum[n1,(n1+n2-1)] = $coefStr2[p-n1]
180                                               
181        make/o/t/N=(nParam) parameters_sum
182        String paramStr1 = "parameters"+coefStr1[4,strlen(coefStr1)-1]
183        String paramStr2 = "parameters"+coefStr2[4,strlen(coefStr2)-1]
184        parameters_sum[0,(n1-1)] = $paramStr1
185        parameters_sum[n1,(n1+n2-1)] = $paramStr2[p-n1]
186       
187        Edit parameters_sum,coef_sum
188       
189        Variable/G root:g_sum
190        g_sum := Sum_Model(coef_sum,ywave_sum,xwave_sum)                       
191        Display ywave_sum vs xwave_sum                                                 
192        ModifyGraph log=1,marker=29,msize=2,mode=4                     
193        Label bottom "q (\\S-1\\M)"
194        Label left "Intensity (cm\\S-1\\M)"
195        Legend                                 
196        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
197       
198        AddModelToStrings("Sum_Model","coef_sum","sum")
199End
200
201// - sets up a dependency to a wrapper, not the actual SmearedModelFunction
202// the usual macro for plotting the smeared model, updated
203// to take input from the panel
204//
205// - somewhat confusing as the unsmeared coefficients are in root:
206// and all of the newly created smeared waves and coefficients are in the
207// selected data folder
208//
209Proc PlotSmeared_Sum_Model(str)                                                         
210        String str
211        Prompt str,"Pick the data folder containing the resolution you want",popup,getAList(4)
212       
213        // if any of the resolution waves are missing => abort
214        if(ResolutionWavesMissingDF(str))               //updated to NOT use global strings (in GaussUtils)
215                Abort
216        endif
217       
218        SetDataFolder $("root:"+str)
219       
220        //make the coefficients and parameters based on the panel values       
221        Variable nParam,n1,n2
222        n1 = root:myGlobals:SumModel:gNParMod1
223        n2 = root:myGlobals:SumModel:gNParMod2
224        nParam = n1 + n2
225        if(n1==0 || n2==0 || nparam==0)
226                Abort "# of parameters must not be zero"
227        endif
228        // n is ok, keep extra copy so changing panel will not affect functions
229        Variable/G root:myGlobals:SumModel:gN1=n1
230        Variable/G root:myGlobals:SumModel:gN2=n2
231       
232        // these are the function names - make global so the fit function
233        // can find them
234        ControlInfo/W=Sum_Model_Panel popup1_0
235        String/G root:myGlobals:SumModel:gModelStr1=S_Value
236        ControlInfo/W=Sum_Model_Panel popup2_0
237        String/G root:myGlobals:SumModel:gModelStr2=S_Value
238       
239        //these are the coefficent waves - local only, in the current data folder!
240        ControlInfo/W=Sum_Model_Panel popup1_1
241        String/G coefStr1=S_Value
242        ControlInfo/W=Sum_Model_Panel popup2_1
243        String/G coefStr2=S_Value
244       
245        Make/O/D/N=(nParam) smear_coef_sum     
246        smear_coef_sum[0,(n1-1)] = $("root:"+coefStr1)
247        smear_coef_sum[n1,(n1+n2-1)] = $("root:"+coefStr2)[p-n1]
248                                               
249        make/o/t/N=(nParam) smear_parameters_sum
250        String paramStr1 = "parameters"+coefStr1[4,strlen(coefStr1)-1]
251        String paramStr2 = "parameters"+coefStr2[4,strlen(coefStr2)-1]
252        smear_parameters_sum[0,(n1-1)] = $("root:"+paramStr1)
253        smear_parameters_sum[n1,(n1+n2-1)] = $("root:"+paramStr2)[p-n1]
254                               
255        Edit smear_parameters_sum,smear_coef_sum                                       
256       
257        // output smeared intensity wave, dimensions are identical to experimental QSIG values
258        // make extra copy of experimental q-values for easy plotting
259        Duplicate/O $(str+"_q") smeared_sum,smeared_qvals                               
260        SetScale d,0,0,"1/cm",smeared_sum                                                       
261                                       
262        Variable/G gs_sum=0
263        gs_sum := fSmeared_Sum_Model(smear_coef_sum,smeared_sum,smeared_qvals)  //this wrapper fills the STRUCT
264       
265        Display smeared_sum vs $(str+"_q")                                                                     
266        ModifyGraph log=1,marker=29,msize=2,mode=4
267        Label bottom "q (\\S-1\\M)"
268        Label left "Intensity (cm\\S-1\\M)"
269        Legend
270        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
271       
272        SetDataFolder root:
273        AddModelToStrings("Smeared_Sum_Model","smear_coef_sum","sum")
274End
275
276
277// this is the actual fitting function that is the
278// sum of the two selected models.
279//
280// this is an AAO function, there is no XOP version
281// since it should be the sum of two XOPs
282//
283Function Sum_Model(w,yw,xw) : FitFunc
284        Wave w,yw,xw
285       
286        SVAR funcStr1=root:myGlobals:SumModel:gModelStr1                //string names of the functions, set by the macro
287        SVAR funcStr2=root:myGlobals:SumModel:gModelStr2
288        NVAR n1=root:myGlobals:SumModel:gN1                     //number of coefficients, set by the macro
289        NVAR n2=root:myGlobals:SumModel:gN2
290       
291        Variable retVal
292       
293        FUNCREF SANSModelAAO_proto f1 = $funcStr1               //convert str to FCN
294        FUNCREF SANSModelAAO_proto f2 = $funcStr2
295        // make temporary coefficient waves for each model
296        Make/O/D/N=(n1) temp_cw1
297        Make/O/D/N=(n2) temp_cw2
298        temp_cw1 = w[p]
299        temp_cw2 = w[p+n1]
300       
301        // calculate the sum of each of the AAO functions
302        Duplicate/O xw tmp_sum_yw1,tmp_sum_yw2
303       
304        f1(temp_cw1,tmp_sum_yw1,xw)
305        f2(temp_cw2,tmp_sum_yw2,xw)
306        yw = tmp_sum_yw1 + tmp_sum_yw2
307       
308        return(0)
309end
310
311// this is all there is to the smeared calculation!
312Function Smeared_Sum_Model(s) : FitFunc
313        Struct ResSmearAAOStruct &s
314
315//      the name of your unsmeared model (AAO) is the first argument
316        Smear_Model_20(Sum_Model,s.coefW,s.xW,s.yW,s.resW)
317
318        return(0)
319End
320
321//wrapper to calculate the smeared model as an AAO-Struct
322// fills the struct and calls the ususal function with the STRUCT parameter
323//
324// used only for the dependency, not for fitting
325//
326Function fSmeared_Sum_Model(coefW,yW,xW)
327        Wave coefW,yW,xW
328       
329        String str = getWavesDataFolder(yW,0)
330        String DF="root:"+str+":"
331       
332        WAVE resW = $(DF+str+"_res")
333       
334        STRUCT ResSmearAAOStruct fs
335        WAVE fs.coefW = coefW   
336        WAVE fs.yW = yW
337        WAVE fs.xW = xW
338        WAVE fs.resW = resW
339       
340        Variable err
341        err = Smeared_Sum_Model(fs)
342       
343        return (0)
344End
345
346//procedures to clean up after itself
347Function UnloadSumModel()
348        if (WinType("Sum_Model_Panel") == 7)
349                DoWindow/K Sum_Model_Panel
350        endif
351        Execute/P "DELETEINCLUDE \"SumSANSModels\""
352        Execute/P "COMPILEPROCEDURES "
353end
354
355Menu "Macros"
356        Submenu "Packages"
357                "Unload Sum SANS Models", UnloadSumModel()
358        End
359end
Note: See TracBrowser for help on using the repository browser.