source: sans/Dev/trunk/NCNR_User_Procedures/SANS/Analysis/Models/NewModels_2006/LamellarPS_v40.ipf @ 391

Last change on this file since 391 was 391, checked in by srkline, 14 years ago

Lamellar_PS models now set the epsilon wave properly, and it doesn't get overwritten

Small fic to the wrapper to have better behavior with cursors. This issue is not done yet.

File size: 8.3 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2#pragma IgorVersion = 6.0
3
4////////////////////////////////////////////////////
5//
6// model function that calculates the scattering from
7// lamellar surfactant structures. contrast is two-phase,
8// from the solvent and uniform bilayer. Integer numbers of
9// repeating bilayers (at a repeat spacing) leads to the
10// familiar lamellar peaks. Bending constant information
11// can be extracted from the Caille parameter with moderate
12// success. A number of the parameters should be held
13// fixed during the fitting procedure, as they should be well
14// known:
15//              repeat spacing D = 2*pi/Qo
16//              contrast = calculated value
17//              polydispersity should be close to 0.1-0.3
18//   Caille parameter <0.8 or 1.0
19//
20// NOTES for Curve Fitting:
21// the epsilon wave "epsilon_Lamellar" should be used to force a
22// larger derivative step for the # of repeat units, which is an integer.
23// a singular matix error will always result if you don't follow this.
24// Also, the # of repeats should be constrained to ~3<N<200, otherwise
25// the optimization can pick a VERY large N, and waste lots of time
26// in the summation loop
27//
28// instrumental resolution is taken into account in the REGULAR
29// model calculation. resolution of ONLY the S(Q) peaks are
30// included. performing the typical smearing calculation would
31// be "double smearing", so is not done.
32//
33// the delta Q parameter "gDelQ" or "dQ" is taken from the q-dependent
34// instrument resolution "abssq" column as this is the identical definition
35// as in the original reference. If the real resolution function cannot be
36// found, a default value, typical of a "medium" q-range on the NG3 SANS is
37// used, although the real values are highly preferred
38//
39// REFERENCE:   Nallet, Laversanne, and Roux, J. Phys. II France, 3, (1993) 487-502.
40//              also in J. Phys. Chem. B, 105, (2001) 11081-11088.
41//
42// 14 JULY 2003 SRK
43//
44////////////////////////////////////////////////////
45
46//this macro sets up all the necessary parameters and waves that are
47//needed to calculate the model function.
48//
49Proc PlotLamellarPS(num,qmin,qmax)
50        Variable num=128, qmin=.001, qmax=.5
51        Prompt num "Enter number of data points for model: "
52        Prompt qmin "Enter minimum q-value (A^1) for model: "
53        Prompt qmax "Enter maximum q-value (A^1) for model: "
54//
55        // constants
56//      Variable/G root:gEuler = 0.5772156649           // Euler's constant
57//      Variable/G root:gDelQ = 0.0025          //[=] 1/A, q-resolution, default value
58       
59        Make/O/D/n=(num) xwave_LamellarPS, ywave_LamellarPS
60        xwave_LamellarPS =  alog(log(qmin) + x*((log(qmax)-log(qmin))/num))
61        Make/O/D coef_LamellarPS = {1,400,30,0.15,1e-6,6.3e-6,20,0.1,0}                 //CH#2
62        make/o/t parameters_LamellarPS = {"Scale","Lamellar spacing, D (A)","Bilayer Thick (delta) (A)","polydisp of Bilayer Thickness","SLD bilayer (A^-2)","SLD solvent (A^-2)","# of Lamellar plates","Caille parameter","Incoherent Bgd (cm-1)"}    //CH#3
63        Edit parameters_LamellarPS, coef_LamellarPS
64        ModifyTable width(parameters_LamellarPS)=160
65       
66        Variable/G root:g_LamellarPS
67        g_LamellarPS := LamellarPS(coef_LamellarPS, ywave_LamellarPS, xwave_LamellarPS)
68        Display ywave_LamellarPS vs xwave_LamellarPS
69        ModifyGraph marker=29, msize=2, mode=4
70        ModifyGraph log=1
71        Label bottom "q (A\\S-1\\M) "
72        Label left "I(q) (cm\\S-1\\M)"
73        //
74        // make epsilon wave appropriate for integer number of lamellar repeats
75        Duplicate/O coef_LamellarPS epsilon_LamellarPS
76        epsilon_LamellarPS = 1e-4*coef_LamellarPS
77        epsilon_LamellarPS[6] = 1               //to make the derivative useful
78        AutoPositionWindow/M=1/R=$(WinName(0,1)) $WinName(0,2)
79       
80        AddModelToStrings("LamellarPS","coef_LamellarPS","LamellarPS")
81End
82
83// - sets up a dependency to a wrapper, not the actual SmearedModelFunction
84Proc PlotSmearedLamellarPS(str)                                                         
85        String str
86        Prompt str,"Pick the data folder containing the resolution you want",popup,getAList(4)
87       
88        // if any of the resolution waves are missing => abort
89        if(ResolutionWavesMissingDF(str))               //updated to NOT use global strings (in GaussUtils)
90                Abort
91        endif
92       
93        SetDataFolder $("root:"+str)
94       
95        // constants
96//      Variable/G root:gEuler = 0.5772156649           // Euler's constant
97//      Variable/G root:gDelQ = 0.0025          //[=] 1/A, q-resolution, default value
98        // Setup parameter table for model function
99        Make/O/D smear_coef_LamellarPS = {1,400,30,0.15,1e-6,6.3e-6,20,0.1,0}           //CH#4
100        make/o/t smear_parameters_LamellarPS = {"Scale","Lamellar spacing, D (A)","Bilayer Thick (delta) (A)","polydisp of Bilayer Thickness","SLD bilayer (A^-2)","SLD solvent (A^-2)","# of Lamellar plates","Caille parameter","Incoherent Bgd (cm-1)"}
101        Edit smear_parameters_LamellarPS,smear_coef_LamellarPS                                  //display parameters in a table
102       
103        // output smeared intensity wave, dimensions are identical to experimental QSIG values
104        // make extra copy of experimental q-values for easy plotting
105        Duplicate/O $(str+"_q") smeared_LamellarPS,smeared_qvals                                //
106        SetScale d,0,0,"1/cm",smeared_LamellarPS                                                        //
107                                       
108        Variable/G gs_LamellarPS=0
109        gs_LamellarPS := fSmearedLamellarPS(smear_coef_LamellarPS,smeared_LamellarPS,smeared_qvals)     //this wrapper fills the STRUCT
110       
111        Display smeared_LamellarPS vs smeared_qvals                                                                     //
112        ModifyGraph log=1,marker=29,msize=2,mode=4
113        Label bottom "q (A\\S-1\\M)"
114        Label left "I(q) (cm\\S-1\\M)"
115       
116        // make epsilon wave appropriate for integer number of lamellar repeats
117        Duplicate/O smear_coef_LamellarPS epsilon_LamellarPS
118        epsilon_LamellarPS = 1e-4*smear_coef_LamellarPS
119        epsilon_LamellarPS[6] = 1               //to make the derivative useful
120       
121        SetDataFolder root:
122        AddModelToStrings("SmearedLamellarPS","smear_coef_LamellarPS","LamellarPS")
123End
124
125
126//AAO version, uses XOP if available
127// simply calls the original single point calculation with
128// a wave assignment (this will behave nicely if given point ranges)
129Function LamellarPS(cw,yw,xw) : FitFunc
130        Wave cw,yw,xw
131       
132#if exists("LamellarPSX")
133        yw = LamellarPSX(cw,xw)
134#else
135        yw = fLamellarPS(cw,xw)
136#endif
137        return(0)
138End
139
140// instrument resolution IS included here in S(Q)
141Function fLamellarPS(w,x) : FitFunc
142        Wave w
143        Variable x
144//       Input (fitting) variables are:
145//[0]Scale
146//[1]Lam spacing, D
147//[2]Bilay Thick (delta)
148//[3]polydisp of the bilayer thickness
149//[4]contrast
150//[5]# of Lam plates
151//[6]Caille parameter
152//[7]Incoherent Bgd (cm-1)
153       
154//      give them nice names
155        Variable scale,dd,del,sig,contr,NN,Cp,bkg,sldb,slds
156        scale = w[0]
157        dd = w[1]
158        del = w[2]
159        sig = w[3]*del
160        sldb = w[4]
161        slds = w[5]
162        contr = sldb-slds
163        NN = trunc(w[6])                //be sure that NN is an integer
164        Cp = w[7]
165        bkg = w[8]
166       
167//      local variables
168        Variable inten, qval,Pq,Sq,ii,alpha,temp,t1,t2,t3,dQ
169        Variable Euler = 0.5772156649
170        Variable dQDefault = 0
171        dQ = dqDefault
172       
173//      NVAR Euler = root:gEuler
174//      NVAR dQDefault = root:gDelQ
175        //      x is the q-value for the calculation
176        qval = x
177        //get the instrument resolution
178//      SVAR/Z sigQ = gSig_Q
179//      SVAR/Z qStr = gQVals
180//     
181//      if(SVAR_Exists(sigQ) && SVAR_Exists(qStr))
182//              Wave/Z sigWave=$sigQ
183//              Wave/Z sig_Qwave = $qStr
184//              if(waveexists(sigWave)&&waveexists(sig_qwave))
185//                      dQ = interp(qval, sig_Qwave, sigWave )
186//              else
187////                    if(qval>0.01 && qval<0.012)
188////                            print "using default resolution"
189////                    endif
190//                      dQ = dQDefault
191//              endif
192//      else
193//              dQ = dQDefault
194//      endif
195       
196        Pq = 2*contr^2/qval/qval*(1-cos(qval*del)*exp(-0.5*qval^2*sig^2))
197       
198        ii=0
199        Sq = 0
200        for(ii=1;ii<(NN-1);ii+=1)
201                temp = 0
202                alpha = Cp/4/pi/pi*(ln(pi*ii) + Euler)
203                t1 = 2*dQ*dQ*dd*dd*alpha
204                t2 = 2*qval*qval*dd*dd*alpha
205                t3 = dQ*dQ*dd*dd*ii*ii
206               
207                temp = 1-ii/NN
208                temp *= cos(dd*qval*ii/(1+t1))
209                temp *= exp(-1*(t2 + t3)/(2*(1+t1)) )
210                temp /= sqrt(1+t1)
211               
212                Sq += temp
213        endfor
214        Sq *= 2
215        Sq += 1
216       
217        inten = 2*Pi*scale*Pq*Sq/(dd*Qval^2)
218       
219        inten *= 1e8            // 1/A to 1/cm
220        //inten = Sq
221        Return (inten+bkg)
222End
223
224//wrapper to calculate the smeared model as an AAO-Struct
225// fills the struct and calls the ususal function with the STRUCT parameter
226//
227// used only for the dependency, not for fitting
228//
229Function fSmearedLamellarPS(coefW,yW,xW)
230        Wave coefW,yW,xW
231       
232        String str = getWavesDataFolder(yW,0)
233        String DF="root:"+str+":"
234       
235        WAVE resW = $(DF+str+"_res")
236       
237        STRUCT ResSmearAAOStruct fs
238        WAVE fs.coefW = coefW   
239        WAVE fs.yW = yW
240        WAVE fs.xW = xW
241        WAVE fs.resW = resW
242       
243        Variable err
244        err = SmearedLamellarPS(fs)
245       
246        return (0)
247End
248
249//the smeared model calculation
250Function SmearedLamellarPS(s) :FitFunc
251        Struct ResSmearAAOStruct &s
252
253//      the name of your unsmeared model (AAO) is the first argument
254        Smear_Model_76(LamellarPS,s.coefW,s.xW,s.yW,s.resW)
255
256        return(0)
257End
258////
Note: See TracBrowser for help on using the repository browser.