source: sans/Release/trunk/NCNR_User_Procedures/SANS/Analysis/Models/Packages/SumModel/SumSANSModels_v40.ipf @ 363

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

Merging changes from Dev/trunk up to revision #362 into Release/trunk

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