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 | // TO FIX: |
---|
28 | // - add a help button to call the help file |
---|
29 | // ...(and write the documentation) |
---|
30 | // - must be thoroughly tested... |
---|
31 | // |
---|
32 | /////////////////////////////// |
---|
33 | |
---|
34 | //create the panel as needed |
---|
35 | // |
---|
36 | Proc Init_SumModelPanel() |
---|
37 | DoWindow/F Sum_Model_Panel |
---|
38 | if(V_flag==0) |
---|
39 | NewDataFolder/O root:SumModel |
---|
40 | InitSMPGlobals() |
---|
41 | Sum_Model_Panel() |
---|
42 | endif |
---|
43 | end |
---|
44 | |
---|
45 | // create the globals that the panel needs |
---|
46 | // |
---|
47 | Function InitSMPGlobals() |
---|
48 | Variable/G root:SumModel:gNParMod1=0 |
---|
49 | Variable/G root:SumModel:gNParMod2=0 |
---|
50 | end |
---|
51 | |
---|
52 | Window Sum_Model_Panel() : Panel |
---|
53 | PauseUpdate; Silent 1 // building window... |
---|
54 | NewPanel /W=(670,75,900,450)/K=1 |
---|
55 | DoWindow/C Sum_Model_Panel |
---|
56 | SetDrawLayer UserBack |
---|
57 | SetDrawEnv fstyle= 1 |
---|
58 | DrawText 14,17,"Sum Two Model Functions" |
---|
59 | PopupMenu popup1_0,pos={15,69},size={131,20},title="Model" |
---|
60 | PopupMenu popup1_0,mode=1,popvalue="Pick model 1",value= #"SumModelPopupList()" |
---|
61 | PopupMenu popup2_0,pos={15,190},size={131,20},title="Model" |
---|
62 | PopupMenu popup2_0,mode=1,popvalue="Pick model 2",value= #"SumModelPopupList()" |
---|
63 | PopupMenu popup1_1,pos={15,98},size={173,20},title="Coef" |
---|
64 | PopupMenu popup1_1,mode=1,popvalue="Pick coef for model 1",value= #"CoefPopupList()" |
---|
65 | PopupMenu popup2_1,pos={15,221},size={173,20},title="Coef" |
---|
66 | PopupMenu popup2_1,mode=1,popvalue="Pick coef for model 2",value= #"CoefPopupList()" |
---|
67 | GroupBox group1,pos={5,50},size={216,107},title="Function # 1" |
---|
68 | SetVariable setvar1,pos={14,128},size={140,15},title="# of Parameters" |
---|
69 | SetVariable setvar1,limits={0,20,1},value= root:SumModel:gNParMod1 |
---|
70 | GroupBox group2,pos={5,171},size={216,107},title="Function # 2" |
---|
71 | SetVariable setvar2,pos={14,249},size={140,15},title="# of Parameters" |
---|
72 | SetVariable setvar2,limits={0,20,1},value= root:SumModel:gNParMod2 |
---|
73 | Button button0,pos={36,299},size={150,20},proc=PlotSumButtonProc,title="Plot Summed Model" |
---|
74 | Button button1,pos={15,330},size={190,20},proc=PlotSmearedSumButtonProc,title="Plot Smeared Summed Model" |
---|
75 | Button button2,pos={190,23},size={25,20},proc=Sum_HelpButtonProc,title="?" |
---|
76 | EndMacro |
---|
77 | |
---|
78 | // show the availabel models |
---|
79 | // but not the smeared versions |
---|
80 | Function/S SumModelPopupList() |
---|
81 | String list |
---|
82 | list = FunctionList("!Smear*",";","KIND:14,NINDVARS:1") //don't show smeared models |
---|
83 | list = RemoveFromList("Sum_Model", list ,";") |
---|
84 | list = RemoveFromList("SANSModel_proto", list ,";") |
---|
85 | return(list) |
---|
86 | End |
---|
87 | |
---|
88 | // show all the appropriate coefficient waves |
---|
89 | Function/S CoefPopupList() |
---|
90 | String list |
---|
91 | list = WaveList("coef*",";","") |
---|
92 | list = RemoveFromList("coef_sum", list ,";") |
---|
93 | return(list) |
---|
94 | End |
---|
95 | |
---|
96 | //button procedure |
---|
97 | Function PlotSumButtonProc(ctrlName) : ButtonControl |
---|
98 | String ctrlName |
---|
99 | |
---|
100 | Execute "PlotSum_Model()" |
---|
101 | End |
---|
102 | |
---|
103 | //button procedure |
---|
104 | Function PlotSmearedSumButtonProc(ctrlName) : ButtonControl |
---|
105 | String ctrlName |
---|
106 | |
---|
107 | Execute "PlotSmeared_Sum_Model()" |
---|
108 | End |
---|
109 | |
---|
110 | Function Sum_HelpButtonProc(ctrlName) : ButtonControl |
---|
111 | String ctrlName |
---|
112 | DisplayHelpTopic "Sum SANS Models" |
---|
113 | End |
---|
114 | |
---|
115 | ////////////////////////////// |
---|
116 | |
---|
117 | |
---|
118 | // this is the procedure that sets up the plot, just like |
---|
119 | // a normal procedure for a model function |
---|
120 | // - there's just a bit more going on, since it needs to |
---|
121 | // build things up based on the settings of the panel |
---|
122 | Proc PlotSum_Model(num,qmin,qmax) |
---|
123 | Variable num=128,qmin=0.001,qmax=0.7 |
---|
124 | Prompt num "Enter number of data points for model: " |
---|
125 | Prompt qmin "Enter minimum q-value (^-1) for model: " |
---|
126 | Prompt qmax "Enter maximum q-value (^-1) for model: " |
---|
127 | |
---|
128 | Make/O/D/n=(num) xwave_sum,ywave_sum |
---|
129 | xwave_sum = alog(log(qmin) + x*((log(qmax)-log(qmin))/num)) |
---|
130 | |
---|
131 | //make the coefficients and parameters based on the panel values |
---|
132 | Variable nParam,n1,n2 |
---|
133 | n1 = root:SumModel:gNParMod1 |
---|
134 | n2 = root:SumModel:gNParMod2 |
---|
135 | nParam = n1 + n2 |
---|
136 | if(n1==0 || n2==0 || nparam==0) |
---|
137 | Abort "# of parameters must not be zero" |
---|
138 | endif |
---|
139 | // n is ok, keep extra copy so changing panel will not affect functions |
---|
140 | Variable/G root:SumModel:gN1=n1 |
---|
141 | Variable/G root:SumModel:gN2=n2 |
---|
142 | |
---|
143 | // these are the function names - make global so the fit function |
---|
144 | // can find them |
---|
145 | ControlInfo/W=Sum_Model_Panel popup1_0 |
---|
146 | String/G root:SumModel:gModelStr1=S_Value |
---|
147 | ControlInfo/W=Sum_Model_Panel popup2_0 |
---|
148 | String/G root:SumModel:gModelStr2=S_Value |
---|
149 | |
---|
150 | //these are the coefficent waves - local only |
---|
151 | ControlInfo/W=Sum_Model_Panel popup1_1 |
---|
152 | String/G root:coefStr1=S_Value |
---|
153 | ControlInfo/W=Sum_Model_Panel popup2_1 |
---|
154 | String/G root:coefStr2=S_Value |
---|
155 | |
---|
156 | Make/O/D/N=(nParam) coef_sum |
---|
157 | coef_sum[0,(n1-1)] = $coefStr1 |
---|
158 | coef_sum[n1,(n1+n2-1)] = $coefStr2[p-n1] |
---|
159 | |
---|
160 | make/o/t/N=(nParam) parameters_sum |
---|
161 | String paramStr1 = "parameters"+coefStr1[4,strlen(coefStr1)-1] |
---|
162 | String paramStr2 = "parameters"+coefStr2[4,strlen(coefStr2)-1] |
---|
163 | parameters_sum[0,(n1-1)] = $paramStr1 |
---|
164 | parameters_sum[n1,(n1+n2-1)] = $paramStr2[p-n1] |
---|
165 | |
---|
166 | Edit parameters_sum,coef_sum |
---|
167 | ywave_sum := Sum_Model(coef_sum,xwave_sum) |
---|
168 | Display ywave_sum vs xwave_sum |
---|
169 | ModifyGraph log=1,marker=29,msize=2,mode=4 |
---|
170 | Label bottom "q (\\S-1\\M)" |
---|
171 | Label left "Intensity (cm\\S-1\\M)" |
---|
172 | Legend |
---|
173 | AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2) |
---|
174 | End |
---|
175 | |
---|
176 | // the usual macro for plotting the smeared model, updated |
---|
177 | // to take input from the panel |
---|
178 | Proc PlotSmeared_Sum_Model() |
---|
179 | |
---|
180 | // if no gQvals wave, data must not have been loaded => abort |
---|
181 | if(ResolutionWavesMissing()) |
---|
182 | Abort |
---|
183 | endif |
---|
184 | |
---|
185 | //make the coefficients and parameters based on the panel values |
---|
186 | Variable nParam,n1,n2 |
---|
187 | n1 = root:SumModel:gNParMod1 |
---|
188 | n2 = root:SumModel:gNParMod2 |
---|
189 | nParam = n1 + n2 |
---|
190 | if(n1==0 || n2==0 || nparam==0) |
---|
191 | Abort "# of parameters must not be zero" |
---|
192 | endif |
---|
193 | // n is ok, keep extra copy so changing panel will not affect functions |
---|
194 | Variable/G root:SumModel:gN1=n1 |
---|
195 | Variable/G root:SumModel:gN2=n2 |
---|
196 | |
---|
197 | // these are the function names - make global so the fit function |
---|
198 | // can find them |
---|
199 | ControlInfo/W=Sum_Model_Panel popup1_0 |
---|
200 | String/G root:SumModel:gModelStr1=S_Value |
---|
201 | ControlInfo/W=Sum_Model_Panel popup2_0 |
---|
202 | String/G root:SumModel:gModelStr2=S_Value |
---|
203 | |
---|
204 | //these are the coefficent waves - local only |
---|
205 | ControlInfo/W=Sum_Model_Panel popup1_1 |
---|
206 | String/G root:coefStr1=S_Value |
---|
207 | ControlInfo/W=Sum_Model_Panel popup2_1 |
---|
208 | String/G root:coefStr2=S_Value |
---|
209 | |
---|
210 | Make/O/D/N=(nParam) smear_coef_sum |
---|
211 | smear_coef_sum[0,(n1-1)] = $coefStr1 |
---|
212 | smear_coef_sum[n1,(n1+n2-1)] = $coefStr2[p-n1] |
---|
213 | |
---|
214 | make/o/t/N=(nParam) smear_parameters_sum |
---|
215 | String paramStr1 = "parameters"+coefStr1[4,strlen(coefStr1)-1] |
---|
216 | String paramStr2 = "parameters"+coefStr2[4,strlen(coefStr2)-1] |
---|
217 | smear_parameters_sum[0,(n1-1)] = $paramStr1 |
---|
218 | smear_parameters_sum[n1,(n1+n2-1)] = $paramStr2[p-n1] |
---|
219 | |
---|
220 | Edit smear_parameters_sum,smear_coef_sum |
---|
221 | |
---|
222 | // output smeared intensity wave, dimensions are identical to experimental QSIG values |
---|
223 | // make extra copy of experimental q-values for easy plotting |
---|
224 | Duplicate/O $gQvals smeared_sum,smeared_qvals |
---|
225 | SetScale d,0,0,"1/cm",smeared_sum |
---|
226 | |
---|
227 | smeared_sum := Smeared_Sum_Model(smear_coef_sum,$gQvals) |
---|
228 | Display smeared_sum vs $gQvals |
---|
229 | ModifyGraph log=1,marker=29,msize=2,mode=4 |
---|
230 | Label bottom "q (\\S-1\\M)" |
---|
231 | Label left "Intensity (cm\\S-1\\M)" |
---|
232 | Legend |
---|
233 | AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2) |
---|
234 | End |
---|
235 | |
---|
236 | |
---|
237 | // this is the actual fitting function that is the |
---|
238 | // sum of the two selected models. |
---|
239 | // |
---|
240 | Function Sum_Model(w,x) :FitFunc |
---|
241 | Wave w;Variable x |
---|
242 | |
---|
243 | SVAR funcStr1=root:SumModel:gModelStr1 //string names of the functions, set by the macro |
---|
244 | SVAR funcStr2=root:SumModel:gModelStr2 |
---|
245 | NVAR n1=root:SumModel:gN1 //number of coefficients, set by the macro |
---|
246 | NVAR n2=root:SumModel:gN2 |
---|
247 | |
---|
248 | Variable retVal |
---|
249 | |
---|
250 | FUNCREF SANSModel_proto f1 = $funcStr1 //convert str to FCN |
---|
251 | FUNCREF SANSModel_proto f2 = $funcStr2 |
---|
252 | // make temporary coefficient waves for each model |
---|
253 | Make/O/D/N=(n1) temp_cw1 |
---|
254 | Make/O/D/N=(n2) temp_cw2 |
---|
255 | temp_cw1 = w[p] |
---|
256 | temp_cw2 = w[p+n1] |
---|
257 | |
---|
258 | // calculate the sum |
---|
259 | retVal = f1(temp_cw1,x) + f2(temp_cw2,x) |
---|
260 | return(retVal) |
---|
261 | end |
---|
262 | |
---|
263 | // this is all there is to the smeared calculation! |
---|
264 | Function Smeared_Sum_Model(w,x) :FitFunc |
---|
265 | Wave w |
---|
266 | Variable x |
---|
267 | |
---|
268 | Variable ans |
---|
269 | SVAR sq = gSig_Q |
---|
270 | SVAR qb = gQ_bar |
---|
271 | SVAR sh = gShadow |
---|
272 | SVAR gQ = gQVals |
---|
273 | |
---|
274 | //the name of your unsmeared model is the first argument |
---|
275 | ans = Smear_Model_20(Sum_Model,$sq,$qb,$sh,$gQ,w,x) |
---|
276 | |
---|
277 | return(ans) |
---|
278 | End |
---|
279 | |
---|
280 | //procedures to clean up after itself |
---|
281 | Function UnloadSumModel() |
---|
282 | if (WinType("Sum_Model_Panel") == 7) |
---|
283 | DoWindow/K Sum_Model_Panel |
---|
284 | endif |
---|
285 | Execute/P "DELETEINCLUDE \"SumSANSModels\"" |
---|
286 | Execute/P "COMPILEPROCEDURES " |
---|
287 | end |
---|
288 | |
---|
289 | Menu "Macros" |
---|
290 | Submenu "Packages" |
---|
291 | "Unload Sum SANS Models", UnloadSumModel() |
---|
292 | End |
---|
293 | end |
---|