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

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

(1) - cursors can now be used to select a subrange of USANS data to fit. This is done by th fit wrapper, assigning a subrange of resW to the struct

(2) all of the smeared model functions are now in the latest form of Smear_Model_N() that is NOT a pointwise calculation anymore, since the USANS matrix smearing in inherently not so.

File size: 11.2 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)
197End
198
199// - sets up a dependency to a wrapper, not the actual SmearedModelFunction
200// the usual macro for plotting the smeared model, updated
201// to take input from the panel
202//
203// - somewhat confusing as the unsmeared coefficients are in root:
204// and all of the newly created smeared waves and coefficients are in the
205// selected data folder
206//
207Proc PlotSmeared_Sum_Model(str)                                                         
208        String str
209        Prompt str,"Pick the data folder containing the resolution you want",popup,getAList(4)
210       
211        // if any of the resolution waves are missing => abort
212        if(ResolutionWavesMissingDF(str))               //updated to NOT use global strings (in GaussUtils)
213                Abort
214        endif
215       
216        SetDataFolder $("root:"+str)
217       
218        //make the coefficients and parameters based on the panel values       
219        Variable nParam,n1,n2
220        n1 = root:myGlobals:SumModel:gNParMod1
221        n2 = root:myGlobals:SumModel:gNParMod2
222        nParam = n1 + n2
223        if(n1==0 || n2==0 || nparam==0)
224                Abort "# of parameters must not be zero"
225        endif
226        // n is ok, keep extra copy so changing panel will not affect functions
227        Variable/G root:myGlobals:SumModel:gN1=n1
228        Variable/G root:myGlobals:SumModel:gN2=n2
229       
230        // these are the function names - make global so the fit function
231        // can find them
232        ControlInfo/W=Sum_Model_Panel popup1_0
233        String/G root:myGlobals:SumModel:gModelStr1=S_Value
234        ControlInfo/W=Sum_Model_Panel popup2_0
235        String/G root:myGlobals:SumModel:gModelStr2=S_Value
236       
237        //these are the coefficent waves - local only, in the current data folder!
238        ControlInfo/W=Sum_Model_Panel popup1_1
239        String/G coefStr1=S_Value
240        ControlInfo/W=Sum_Model_Panel popup2_1
241        String/G coefStr2=S_Value
242       
243        Make/O/D/N=(nParam) smear_coef_sum     
244        smear_coef_sum[0,(n1-1)] = $("root:"+coefStr1)
245        smear_coef_sum[n1,(n1+n2-1)] = $("root:"+coefStr2)[p-n1]
246                                               
247        make/o/t/N=(nParam) smear_parameters_sum
248        String paramStr1 = "parameters"+coefStr1[4,strlen(coefStr1)-1]
249        String paramStr2 = "parameters"+coefStr2[4,strlen(coefStr2)-1]
250        smear_parameters_sum[0,(n1-1)] = $("root:"+paramStr1)
251        smear_parameters_sum[n1,(n1+n2-1)] = $("root:"+paramStr2)[p-n1]
252                               
253        Edit smear_parameters_sum,smear_coef_sum                                       
254       
255        // output smeared intensity wave, dimensions are identical to experimental QSIG values
256        // make extra copy of experimental q-values for easy plotting
257        Duplicate/O $(str+"_q") smeared_sum,smeared_qvals                               
258        SetScale d,0,0,"1/cm",smeared_sum                                                       
259                                       
260        Variable/G gs_sum=0
261        gs_sum := fSmeared_Sum_Model(smear_coef_sum,smeared_sum,smeared_qvals)  //this wrapper fills the STRUCT
262       
263        Display smeared_sum vs $(str+"_q")                                                                     
264        ModifyGraph log=1,marker=29,msize=2,mode=4
265        Label bottom "q (\\S-1\\M)"
266        Label left "Intensity (cm\\S-1\\M)"
267        Legend
268        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
269       
270        SetDataFolder root:
271End
272
273
274// this is the actual fitting function that is the
275// sum of the two selected models.
276//
277// this is an AAO function, there is no XOP version
278// since it should be the sum of two XOPs
279//
280Function Sum_Model(w,yw,xw) : FitFunc
281        Wave w,yw,xw
282       
283        SVAR funcStr1=root:myGlobals:SumModel:gModelStr1                //string names of the functions, set by the macro
284        SVAR funcStr2=root:myGlobals:SumModel:gModelStr2
285        NVAR n1=root:myGlobals:SumModel:gN1                     //number of coefficients, set by the macro
286        NVAR n2=root:myGlobals:SumModel:gN2
287       
288        Variable retVal
289       
290        FUNCREF SANSModelAAO_proto f1 = $funcStr1               //convert str to FCN
291        FUNCREF SANSModelAAO_proto f2 = $funcStr2
292        // make temporary coefficient waves for each model
293        Make/O/D/N=(n1) temp_cw1
294        Make/O/D/N=(n2) temp_cw2
295        temp_cw1 = w[p]
296        temp_cw2 = w[p+n1]
297       
298        // calculate the sum of each of the AAO functions
299        Duplicate/O xw tmp_sum_yw1,tmp_sum_yw2
300       
301        f1(temp_cw1,tmp_sum_yw1,xw)
302        f2(temp_cw2,tmp_sum_yw2,xw)
303        yw = tmp_sum_yw1 + tmp_sum_yw2
304       
305        return(0)
306end
307
308// this is all there is to the smeared calculation!
309Function Smeared_Sum_Model(s) : FitFunc
310        Struct ResSmearAAOStruct &s
311
312//      the name of your unsmeared model (AAO) is the first argument
313        Smear_Model_20(Sum_Model,s.coefW,s.xW,s.yW,s.resW)
314
315        return(0)
316End
317
318//wrapper to calculate the smeared model as an AAO-Struct
319// fills the struct and calls the ususal function with the STRUCT parameter
320//
321// used only for the dependency, not for fitting
322//
323Function fSmeared_Sum_Model(coefW,yW,xW)
324        Wave coefW,yW,xW
325       
326        String str = getWavesDataFolder(yW,0)
327        String DF="root:"+str+":"
328       
329        WAVE resW = $(DF+str+"_res")
330       
331        STRUCT ResSmearAAOStruct fs
332        WAVE fs.coefW = coefW   
333        WAVE fs.yW = yW
334        WAVE fs.xW = xW
335        WAVE fs.resW = resW
336       
337        Variable err
338        err = Smeared_Sum_Model(fs)
339       
340        return (0)
341End
342
343//procedures to clean up after itself
344Function UnloadSumModel()
345        if (WinType("Sum_Model_Panel") == 7)
346                DoWindow/K Sum_Model_Panel
347        endif
348        Execute/P "DELETEINCLUDE \"SumSANSModels\""
349        Execute/P "COMPILEPROCEDURES "
350end
351
352Menu "Macros"
353        Submenu "Packages"
354                "Unload Sum SANS Models", UnloadSumModel()
355        End
356end
Note: See TracBrowser for help on using the repository browser.