source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/SANS/MC_SimulationScripting.ipf @ 903

Last change on this file since 903 was 903, checked in by srkline, 10 years ago

minor changes to the MC scripting, more examples of looping to generate long "runs"

Modified the NCNR loader (Macros menu) to include different facility packages, hopefully exclusive. Will need final tweaking once we settle on file names.

For the USANS procedures from KIST - changed the names to have "KIST_" prefix, and changed NCNR-specific to have "NCNR_" prefix, and I'll do the same with the SANS reduction procedures. Then all will be consistent, and each set of facility-specific files will be grouped in a directory listing.

File size: 29.2 KB
Line 
1#pragma rtGlobals=1             // Use modern global access method.
2
3// this is a file with all of the functions that can be used to
4// script the MC simulation of data
5// 1D will also be supported, unless I can find a reason not to support this.
6//
7// there will be a lot of utility functions to "move" the parts of an instrument that
8// are "moveable" into different configurations. The aim is to have this appear as
9// much like running an experiment as possible.
10//
11//
12//  The basic cycle:
13//
14// 1) plot the model function, and set the coefficients to what you want
15//
16// 2) set (and save) as many configurations as you like -picking ones that are most appropriate for the sample scattering
17//
18//              Sim_SaveConfiguration(waveStr)
19//
20// 3) simulate the sample scattering at your configurations for specified times. The 1D count rate
21//               should match fairly well to the 2D count rate. (Check this for low/high incoherent levels)
22//-or-
23// 4) run everything for a short time (say 1s to 10s) to get the count rates
24//  - this will actually save the files - just like a real experiment. This is like step 6, but with short count times.
25//
26// 5) with the sample count rates, find the optimal count times
27//
28//              OptimalCount(samCR,empCR)
29//
30// 6) Write a function to "run" the samples at configurations and count times. Don't forget to simulate
31//               the transmissions. (See the examples below). Copy the function to a new procedure window, rename it,
32//              and edit it there as you wish.
33//
34//              ListSASCALCConfigs() will print out the list of configurations
35//
36// 7) Then with the optimal count times, do a dry run to see how long the whole simulation would take.
37//
38//              Sim_DryRun(funcStr)
39//
40// 8) Then if that's OK, run the full simulation
41//
42//              Your function here()
43//
44// 9) If it's 1D data, use NSORT to combine. If it's 2D data, reduce as usual. Be sure to skip the detector
45//              efficiency correction, and set the dead time to a tiny value so that it won't have any
46//    effect (since the detector CR is fictionally high, and the detector is perfect):
47//
48//              Sim_SetDeadTimeTiny()
49//
50// When you have a lot of 1D waves to combine, and they are not numbered like in a real reduction
51// experiment, see:
52//
53//  MakeCombineTable_byName()
54//  DoCombineFiles_byName(lowQfile,medQfile,hiQfile,saveName) (in NSORT.ipf)
55//
56// this works with 1, 2, or 3 data files
57//
58
59
60// In general, when setting up a simulation, it's easier to set up sample conditions for a particular
61// sample, and loop through the configurations. If you want your 2D data to "look" like a typical
62// experiment, then you'll need to simulate each different sample at one configuration, then "move"
63// to a different configuration and loop through the samples again. Somewhat more cumbersome, all to
64// get the file catalog to be "grouped" like a real SANS experiment.
65
66
67//
68// Important - in the reduction process, set the  dead time correction to something
69// really small, say 10^-15
70//
71// see Sim_SetDeadTimeTiny()
72
73//
74// TODO:
75// x- need to update the 1D writer to allow a name to be passed in, rather than
76// always using the dialog
77// x- fill in the "RunSample" functions
78//
79//  -- I need a little panel to control all of this, and get the information, just
80//     like setting up a real experiment. Or maybe not. Maybe better to keep some of this
81//     hidden.
82//  -- It would be nice to be able to automatically do the sum_model to add the 2D empty cell contribution
83//     For some experiments it's quite useful to see the level of this contribution, rather than
84//     the completely clean simulation.
85//  -- in this file, clearly separate the examples from the utility functions
86//  -- get a function list of everything, and document what each does (if not obvious)
87//  -- step by step comments of one of the examples (1D and 2D) are probably the most useful
88//  -- "dry run" function to estimate the total simulation time for 2D. (? set all monitor counts to 1000
89//      or whatever I use to get other estimates, and then run through everything to get estimated times)
90//
91// set the global:
92//      NVAR g_estimateOnly = root:Packages:NIST:SAS:g_estimateOnly             // == 1 for just a time estimate, == 0 (default) to do the simulation
93// and then do the dry run
94//
95//
96// ----- transmission really doesn't work in the simulation...
97// -- add function to simulate transmission in 2D by saving a "Trans" configuration
98//     and automatically using that. Currently the trans is already written to the file
99// x- would also need a function to simulate an empty beam measurement to rescale the
100//     transmissions. (use the model given, set scale (w[0] to something really small)
101//  -- or better, use the empty cell function that is guaranteed to have coefficients that are
102//     well behaved - and I can set/reset them to get a proper "empty beam"
103//
104//
105// -- scattering from an empty cell is NOT provided in 2D
106
107Menu "Macros"
108        Submenu "Simulation Scripting - Beta"
109                "Save Configuration",Sim_saveConfProc()
110                "Move to Configuration",Sim_moveConfProc()
111                "List Configurations",ListSASCALCConfigs()
112                "Setup Sim Example",Setup_Sim_Example()
113                "1D Count Rates",DryRunProc_1D()
114                "2D Dry Run",DryRunProc_2D()
115                "Optimal Count Times",OptimalCountProc()
116                "Make Table to Combine By Name",MakeCombineTable_byName()
117                "Combine by Name",DoCombineFiles_byName(lowQfile,medQfile,hiQfile,saveName)
118                "Turn Off Dead Time Correction",Sim_SetDeadTimeTiny()
119        End
120End
121
122// run this before the examples to make sure that the proper named configurations exist.
123// this function will overwrite any same-named waves
124Function Setup_Sim_Example()
125
126        SetDataFolder root:Packages:NIST:SAS:
127       
128        Make/O/T/N=20 Config_1m,Config_4m,Config_13m
129        Config_1m = {"checkNG7","8","5.08 cm","6","0.115","checkChamber","1/2\"","0","100","25","","","","","","","","","",""}
130        Config_4m = {"checkNG7","5","5.08 cm","6","0.115","checkChamber","1/2\"","0","400","0","","","","","","","","","",""}
131        Config_13m = {"checkNG7","1","5.08 cm","6","0.115","checkChamber","1/2\"","0","1300","0","","","","","","","","","",""}
132 
133        SetDataFolder root:
134
135End
136
137Function Example_1DSim()
138
139        String confList,ctTimeList,saveNameList,funcStr,titleStr
140       
141
142        Sim_SetSimulationType(0)                //kill the simulation panel
143        Sim_SetSimulationType(1)                //open the 1D simulation panel
144       
145//(1)   enter the (unsmeared) function name
146        funcStr = "SchulzSpheres_SC"
147        Wave cw = $("root:"+getFunctionCoef(funcStr))
148        Sim_SetModelFunction(funcStr)                                           // model function name
149               
150//(2) model coefficients here, if needed. Wave name is "cw"
151//   then set the sample thickness. Be sure to use an appropriate incoherent background in the model
152        Sim_Set1D_Transmission(0.75)                                            // For 1D, I need to supply the transmission
153        Sim_SetThickness(0.2)                                                           // thickness
154        Sim_Set1D_ABS(1)                                                                                // absolute scaling (1== yes)
155        Sim_Set1D_Noise(1)                                                                      // noise (1== yes, add statistical noise)
156
157        cw[0] = 0.1
158        // as needed - look at the parameter list for the model
159       
160       
161//(3) set the configuration list, times, and saved names
162// -- the mumber of listed configurations must match the number of discrete count times and save names
163// titleStr is the label and is the same for each run of the same sample
164        confList = "Config_1m;Config_4m;Config_13m;"
165        ctTimeList = "100;300;900;"
166        saveNameList = "sim_1m.abs;sim_4m.abs;sim_13m.abs;"
167        titleStr = "MySample 1"
168
169        // then this runs the samples as listed
170        Sim_RunSample_1D(confList,ctTimeList,titleStr,saveNameList)
171
172        // no transmissions or empty beam measurements to make for 1D simulation
173
174        return(0)       
175End
176
177//
178Function Example_2DSim()
179
180        String confList,ctTimeList,titleStr,transConfList,transCtTimeList
181        Variable runIndex,val,totalTime
182        String funcStr
183
184tic()
185
186        Sim_SetSimulationType(0)                //kill the simulation panel
187        Sim_SetSimulationType(2)                //open the 2D simulation panel
188        Sim_SetSimTimeWarning(36000)                    //sets the threshold for the warning dialog to 10 hours
189        totalTime = 0
190
191
192//(1)   enter the (unsmeared) function name
193        funcStr = "SchulzSpheres_SC"
194        Wave cw = $("root:"+getFunctionCoef(funcStr))
195        Sim_SetModelFunction(funcStr)                                           // model function name
196
197//(2) set the standard sample cell size (1" diam banjo cell)
198// and set the conditions for beam stop in, and raw counts
199        Sim_SetSampleRadius(1.27)                                                       // sam radius (cm)
200        Sim_SetRawCountsCheck(1)                                                        // raw cts? 1== yes
201        Sim_SetBeamStopInOut(1)                                                         // BS in? 1==yes
202
203//(3) model coefficients here, if needed. Wave name is "cw"
204//   then set the sample thickness and incoherent cross section
205
206        cw[0] = 0.1
207        // as needed - look at the parameter list for the model
208
209        Sim_SetThickness(0.2)                                                           // thickness (cm)
210        Sim_SetIncohXS(1.3)                                                                     // incoh XS
211       
212//(4) starting run index for the saved raw data files. this will automatically increment
213//    as the sample is "Run"
214        runIndex = 400
215       
216//(5) set the configuration list, times, a single sample label, and the starting run index
217// -- the mumber of listed configurations must match the number of discrete count times
218        confList = "Config_1m;Config_4m;Config_13m;"
219        ctTimeList = "100;300;900;"
220        transConfList = "Config_13m;"           // trans only @ 13m
221        transCtTimeList = "1;"                          // trans count time = 1s
222        titleStr = "MySample 1"
223
224        // runIndex is PBR and updates as the number of files are written
225       
226        // this runs the sample
227        totalTime += Sim_RunSample_2D(confList,ctTimeList,titleStr,runIndex)
228        // this runs the transmissions
229        totalTime += Sim_RunTrans_2D(transConfList,transCtTimeList,titleStr,runIndex)
230       
231        // run the empty beam at all configurations
232        // This will automatically change the function to "EC_Empirical" and "empty beam" conditions
233        transConfList = "Config_1m;Config_4m;Config_13m;"
234        transCtTimeList = "1;1;1;"     
235        titleStr = "Empty Beam"
236        totalTime += Sim_RunEmptyBeamTrans_2D(transConfList,transCtTimeList,titleStr,runIndex)
237       
238        Print "runIndex = ",runIndex
239
240        Sim_SetSimTimeWarning(10)
241
242toc()
243       
244        return(totalTime)
245End
246
247// this example was really used, run repeatedly with different count times to get
248// replicate data sets to test the overlap
249Function Example_Loop_1DSim()
250
251        String confList,ctTimeList,saveNameList,funcStr,titleStr
252       
253
254        Sim_SetSimulationType(0)                //kill the simulation panel
255        Sim_SetSimulationType(1)                //open the 1D simulation panel
256       
257//(1)   enter the (unsmeared) function name
258        funcStr = "DAB_model"
259        Wave cw = $("root:"+getFunctionCoef(funcStr))
260        Sim_SetModelFunction(funcStr)                                           // model function name
261               
262//(2) model coefficients here, if needed. Wave name is "cw"
263//   then set the sample thickness. Be sure to use an appropriate incoherent background in the model
264        Sim_Set1D_Transmission(0.8)                                             // For 1D, I need to supply the transmission
265        Sim_SetThickness(0.1)                                                           // thickness
266        Sim_Set1D_ABS(1)                                                                                // absolute scaling (1== yes)
267        Sim_Set1D_Noise(1)                                                                      // noise (1== yes, add statistical noise)
268
269
270        cw = {1e-05,200,0}
271       
272       
273//(3) set the configuration list, times, and saved names
274// -- the mumber of listed configurations must match the number of discrete count times and save names
275// titleStr is the label and is the same for each run of the same sample
276        confList = "Config_4m;"
277        ctTimeList = ""         //filled in the loop
278        saveNameList = ""
279        titleStr = "DAB versus count time t = "
280
281        Make/O/D ctTime = {5,11,16,21,27,32,37,43,48,53,107,160,214,267,321,374,428,481,535,1604,5348,21390,53476}
282
283        Variable ii=0,nTrials=10,jj
284       
285        for(jj=0;jj<numpnts(ctTime);jj+=1)     
286                for(ii=0;ii<nTrials;ii+=1)
287                        titleStr = "DAB versus count time t = "+num2str(ctTime[jj])
288                        saveNameList = "DAB_4m_t"+num2str(ctTime[jj])+"_"+num2str(ii)+".abs;"
289                        ctTimeList = num2str(ctTime[jj])+";"
290                        // then this runs the samples as listed
291                        Sim_RunSample_1D(confList,ctTimeList,titleStr,saveNameList)
292                endfor
293        endfor
294
295        // no transmissions or empty beam measurements to make for 1D simulation
296
297        return(0)       
298End
299
300
301
302
303
304// pass in a semicolon delimited list of configurations + corresponding count times + saved names
305Function Sim_RunSample_1D(confList,ctTimeList,titleStr,saveNameList)
306        String confList,ctTimeList,titleStr,saveNameList
307       
308        Variable ii,num,ct,cr,numPt
309        String twStr,fname,type
310        NVAR g_estimateOnly = root:Packages:NIST:SAS:g_estimateOnly             // == 1 for just count rate, == 0 (default) to do the simulation and save
311        WAVE/Z crWave = root:CR_1D
312        WAVE/Z/T fileWave = root:Files_1D
313
314        type = "SAS"            // since this is a simulation
315        num=ItemsInList(confList)
316       
317        for(ii=0;ii<num;ii+=1)
318                twStr = StringFromList(ii, confList,";")
319                Wave/T tw=$("root:Packages:NIST:SAS:"+twStr)
320                ct = str2num(StringFromList(ii, ctTimeList,";"))
321                fname = StringFromList(ii, saveNameList,";")
322               
323                Sim_MoveToConfiguration(tw)
324                Sim_SetCountTime(ct)
325                cr = Sim_Do1DSimulation()
326               
327                // either save it out, or return a table of the count rates
328                if(g_estimateOnly)
329                        numPt = numpnts(crWave)
330                        InsertPoints numPt, 1, crWave,fileWave
331                        crWave[numPt] = cr
332                        fileWave[numPt] = fname
333                else
334                        Sim_Save1D_wName(type,titleStr,fname)           //this function will increment the runIndex
335                endif
336       
337        endfor
338
339        return(0)
340End
341
342// fakes an empty beam transmission by setting the conditions of the
343// empty cell temporarily to something that doesn't scatter
344Function Sim_RunEmptyBeamTrans_2D(confList,ctTimeList,titleStr,runIndex)
345        String confList,ctTimeList,titleStr
346        Variable &runIndex
347       
348        Wave cw = root:Packages:NIST:SAS:coef_ECEmp             //EC coefs are in the SAS folder, not root:
349        Variable totalTime
350       
351        // change the function and coefficients
352        Sim_SetModelFunction("EC_Empirical")
353        cw = {1e-18,3.346,0,9,0}
354        Sim_SetIncohXS(0)                                                                       // incoh XS = 0 for empty beam
355        Sim_SetThickness(0.1)                                                   // thickness (cm) to give proper level of scattering
356
357               
358        totalTime = Sim_RunTrans_2D(confList,ctTimeList,titleStr,runIndex)
359               
360        // change them back
361        cw = {2.2e-08,3.346,0.0065,9,0.016}
362       
363        return(totalTime)
364End
365
366 
367// should be called with each list having only one item
368// and a count time of 1 s
369//
370// -- for an empty beam, before calling, set the incoh XS = 0 and set the scale
371// of the model to something tiny so that there is no coherent scattering
372//
373Function Sim_RunTrans_2D(confList,ctTimeList,titleStr,runIndex)
374        String confList,ctTimeList,titleStr
375        Variable &runIndex
376       
377        Variable ii,num,ct,index,totalTime
378        String twStr,type
379
380        NVAR g_estimateOnly = root:Packages:NIST:SAS:g_estimateOnly             // == 1 for just a time estimate, == 0 (default) to do the simulation
381        NVAR g_estimatedMCTime = root:Packages:NIST:SAS:g_estimatedMCTime               // estimated MC sim time
382        totalTime = 0
383               
384        type = "SAS"            // since this is a simulation
385        num=ItemsInList(confList)
386       
387        for(ii=0;ii<num;ii+=1)
388                twStr = StringFromList(ii, confList,";")
389                Wave/T tw=$("root:Packages:NIST:SAS:"+twStr)
390                ct = str2num(StringFromList(ii, ctTimeList,";"))
391               
392                Sim_MoveToConfiguration(tw)
393                Sim_SetBeamStopInOut(0)                                                         // BS out for Trans
394                Sim_SetCountTime(ct)
395                Sim_DoMCSimulation()
396               
397                if(g_estimateOnly)
398                        totalTime += g_estimatedMCTime          //      don't save, don't increment. time passed back as global after each MC simulation
399                else
400                        SaveAsVAXButtonProc("",runIndex=runIndex,simLabel=(titleStr+" at "+twStr))
401                        runIndex += 1
402                endif
403
404        endfor
405       
406        Sim_SetBeamStopInOut(1)                                                         // put the BS back in
407
408        return(totalTime)
409End
410
411
412
413// if just an estimate, I can also get the count rate too
414// WAVE results = root:Packages:NIST:SAS:results
415//      Print "Sample Simulation (2D) CR = ",results[9]/ctTime
416// -- for estimates, iMon is set to 1000, so time=1000/(root:Packages:NIST:SAS:gImon)
417Function Sim_RunSample_2D(confList,ctTimeList,titleStr,runIndex)
418        String confList,ctTimeList,titleStr
419        Variable &runIndex
420
421        Variable ii,num,ct,index,totalTime
422        String twStr,type
423
424        NVAR g_estimateOnly = root:Packages:NIST:SAS:g_estimateOnly             // == 1 for just a time estimate, == 0 (default) to do the simulation
425        NVAR g_estimatedMCTime = root:Packages:NIST:SAS:g_estimatedMCTime               // estimated MC sim time
426        totalTime = 0
427       
428        type = "SAS"            // since this is a simulation
429        num=ItemsInList(confList)
430       
431       
432        for(ii=0;ii<num;ii+=1)
433                twStr = StringFromList(ii, confList,";")
434                Wave/T tw=$("root:Packages:NIST:SAS:"+twStr)
435                ct = str2num(StringFromList(ii, ctTimeList,";"))
436               
437                Sim_MoveToConfiguration(tw)
438                Sim_SetBeamStopInOut(1)                                                         // BS in?
439                Sim_SetCountTime(ct)
440                Sim_DoMCSimulation()
441               
442                if(g_estimateOnly)
443                        totalTime += g_estimatedMCTime          //      don't save, don't increment. time passed back as global after each MC simulation
444                else
445                        SaveAsVAXButtonProc("",runIndex=runIndex,simLabel=(titleStr+" at "+twStr))
446                        runIndex += 1
447                endif
448               
449        endfor
450
451        return(totalTime)
452End
453
454// a prototype function with no parameters
455// for a real experiment - it must return the total time, if you want a proper estimate
456Function Sim_Expt_Proto()
457        Print "In the Sim_Expt_Proto -- error"
458        return(0)
459End
460
461Proc DryRunProc_2D(funcStr)
462        String funcStr
463        Sim_2DDryRun(funcStr)
464end
465
466// pass the function string, no parameters
467Function Sim_2DDryRun(funcStr)
468        String funcStr
469       
470        FUNCREF Sim_Expt_Proto func=$funcStr
471       
472        NVAR g_estimateOnly = root:Packages:NIST:SAS:g_estimateOnly
473        g_estimateOnly = 1
474       
475        Variable totalTime
476//      totalTime = Sim_CountTime_2D()  // your "exeriment" here, returning the totalTime
477       
478        totalTime = func()
479        g_estimateOnly = 0
480       
481        Printf "Total Estimated Time = %g s or %g h\r",totalTime,totalTime/3600
482end
483
484Proc DryRunProc_1D(funcStr)
485        String funcStr
486        Sim_1DDryRun(funcStr)
487end
488
489// pass the function string, no parameters
490// makes a (new) table of the files and CR
491Function Sim_1DDryRun(funcStr)
492        String funcStr
493       
494        FUNCREF Sim_Expt_Proto func=$funcStr
495       
496        NVAR g_estimateOnly = root:Packages:NIST:SAS:g_estimateOnly
497        g_estimateOnly = 1
498       
499        Variable totalTime
500       
501        Make/O/D/N=0 root:CR_1D
502        Make/O/T/N=0 root:Files_1D
503       
504        totalTime = func()
505        g_estimateOnly = 0
506       
507        Edit Files_1D,CR_1D
508       
509//      Printf "Total Estimated Time = %g s or %g h\r",totalTime,totalTime/3600
510        return(0)
511end
512
513
514
515
516
517Proc OptimalCountProc(samCountRate,emptyCountRate)
518        Variable samCountRate,emptyCountRate
519        OptimalCount(samCountRate,emptyCountRate)
520End
521
522Function OptimalCount(samCR,empCR)
523        Variable samCR,empCR
524       
525        String str
526        Variable ratio,target
527        ratio = sqrt(samCR/empCR)
528       
529        str = "For %g Sample counts, t sam = %g s and t emp = %g s\r"
530       
531        target = 1e6
532        printf str,target,round(target/samCR),round((target/samCR)/ratio)
533       
534        target = 1e5
535        printf str,target,round(target/samCR),round((target/samCR)/ratio)       
536
537        target = 1e4
538        printf str,target,round(target/samCR),round((target/samCR)/ratio)       
539        return(0)
540end
541
542
543//
544// Important - in the reduction process, set the  dead time correction to something
545// really small, say 10^-15 so that  it won't have any effect (since the detector CR is fictionally high)
546//
547Function Sim_SetDeadTimeTiny()
548
549//      NVAR DeadtimeNG3_ILL = root:myGlobals:DeadtimeNG3_ILL           //pixel resolution in cm
550//      NVAR DeadtimeNG5_ILL = root:myGlobals:DeadtimeNG5_ILL
551//      NVAR DeadtimeNG7_ILL = root:myGlobals:DeadtimeNG7_ILL
552//      NVAR DeadtimeNGB_ILL = root:myGlobals:DeadtimeNGB_ILL
553        NVAR DeadtimeNG3_ORNL_VAX = root:myGlobals:DeadtimeNG3_ORNL_VAX
554//      NVAR DeadtimeNG3_ORNL_ICE = root:myGlobals:DeadtimeNG3_ORNL_ICE
555//      NVAR DeadtimeNG5_ORNL = root:myGlobals:DeadtimeNG5_ORNL
556        NVAR DeadtimeNG7_ORNL_VAX = root:myGlobals:DeadtimeNG7_ORNL_VAX
557//      NVAR DeadtimeNG7_ORNL_ICE = root:myGlobals:DeadtimeNG7_ORNL_ICE
558        NVAR DeadtimeNGB_ORNL_ICE = root:myGlobals:DeadtimeNGB_ORNL_ICE
559        NVAR DeadtimeDefault = root:myGlobals:DeadtimeDefault
560
561        DeadtimeNG3_ORNL_VAX = 1e-15
562        DeadtimeNG7_ORNL_VAX = 1e-15
563        DeadtimeNGB_ORNL_ICE = 1e-15
564        DeadtimeDefault = 1e-15
565       
566        return(0)
567End
568
569
570
571/////////////////////////////////////////////////////////////////////
572///////////// All of the utility functions to make things "move"
573
574
575
576// set the wavelength
577Function Sim_SetLambda(val)
578        Variable val
579
580        NVAR lam = root:Packages:NIST:SAS:gLambda
581        lam=val
582        LambdaSetVarProc("",val,num2str(val),"")
583        return(0)
584end
585
586// set the wavelength spread
587// ?? what are the allowable choices
588// there are 3 choices for each instrument
589//
590// TO DO ?? do this in a better way
591// currently there is no error checking...
592//
593Function Sim_SetDeltaLambda(strVal)
594        String strVal
595       
596       
597        DeltaLambdaPopMenuProc("",1,strVal)             // recalculates intensity if 2nd param==1, skip if == 0
598        PopupMenu popup0_2 win=SASCALC,popmatch=strVal
599//      ControlUpdate/W=SASCALC popup0_2
600       
601        return(0)
602End
603
604// if state == 0, lenses out (box un-checked, no parameters change)
605// if state == 1, lenses in, changes guides, aperture, SDD and wavelength appropriately
606// if prisms desired, follow this call with a set wavelength=17.2
607Function Sim_SetLenses(state)
608        Variable state
609       
610        LensCheckProc("",state)
611        CheckBox checkLens,win=SASCALC,value=state
612       
613        return(0)
614End
615
616// instrName = "checkNG3" or "checkNG7" or "checkNGB"
617// these are the only allowable choices
618Function Sim_SetInstrument(instrName)
619        String instrName
620       
621        SelectInstrumentCheckProc(instrName,0)
622        return(0)
623End
624
625
626//two steps to change the number of guides
627// - first the number of guides, then the source aperture (if 0g, 7g where there are choices)
628//
629// strVal is "5 cm" or "1.43 cm" (MUST be identical to the popup string, MUST have the units)
630///
631Function Sim_SetNumGuides_SrcAp(ng,strVal)
632        Variable ng
633        String strVal
634
635        NVAR gNg=root:Packages:NIST:SAS:gNg
636        gNg = ng
637       
638        Slider SC_Slider,win=SASCALC,variable=root:Packages:NIST:SAS:gNg                                //guides
639
640        // with the new number of guides, update the allowable source aperture popups
641        DoWindow/F SASCALC
642        UpdateControls()
643       
644        // TO DO -- this needs some logic added, or an additional parameter to properly set the
645        // source aperture
646       
647        popupmenu popup0 win=SASCALC,popmatch=strVal
648        Variable dum=sourceApertureDiam()               //sets the proper value in the header wave
649        return(0)
650End
651
652// strVal is "1/2\"" or similar. note the extra backslash so that " is part of the string
653//
654Function Sim_SetSampleApertureDiam(strVal)
655        String strVal
656       
657        popupmenu popup0_1 win=SASCALC,popmatch=strVal
658        Variable dum=sampleApertureDiam()               //sets the proper value in the header wave
659        return(0)
660End     
661
662Function Sim_SetOffset(offset)
663        Variable offset
664       
665        NVAR detOffset = root:Packages:NIST:SAS:gOffset
666        detOffset = offset      //in cm
667        detectorOffset()                //changes header and estimates (x,y) beamcenter
668        return(0)
669end
670
671Function Sim_SetSDD(sdd)
672        Variable sdd
673       
674        NVAR detDist = root:Packages:NIST:SAS:gDetDist
675
676        detDist = sdd
677        sampleToDetectorDist()          //changes the SDD and wave (DetDist is the global)
678        return(0)
679end
680
681
682// change the huber/sample chamber position
683// if str="checkHuber", the pos is set to huber
684// otherwise, it's set to the sample chamber position
685//
686Function Sim_SetTablePos(strVal)
687        String strVal
688       
689        TableCheckProc(strVal,0)
690        return(0)
691end
692
693
694//
695// ------------ specifc for the simulation
696//
697//
698// type = 0 = none, kills panel
699// type = 1 = 1D
700// type = 2 = 2D
701//
702// -- DON'T leave the do2D global set to 1 - be sure to set back to 0 before exiting
703//
704Function Sim_SetSimulationType(type)
705        Variable type
706
707        STRUCT WMCheckboxAction CB_Struct
708        Variable checkState
709        if(type == 1 || type == 2)
710                CB_Struct.checked = 1
711                checkState = 1
712        else
713                CB_Struct.checked = 0
714                checkState = 0
715        endif
716
717        NVAR do2D = root:Packages:NIST:SAS:gDoMonteCarlo
718
719        if(type == 1)
720                do2D = 0
721        else
722                do2D = 1
723        endif
724       
725        SimCheckProc(CB_Struct)
726
727        // Very important
728        do2D = 0
729       
730        CheckBox checkSim win=SASCALC,value=checkState
731        return(0)
732End
733
734Function Sim_DoMCSimulation()
735
736        STRUCT WMButtonAction ba
737        ba.eventCode = 2                        //fake mouse click on button
738
739        MC_DoItButtonProc(ba)
740        return(0)
741End
742
743Function Sim_Do1DSimulation()
744
745        ReCalculateInten(1)
746        NVAR estCR = root:Packages:NIST:SAS:g_1DEstDetCR
747        return(estCR)
748End
749
750// counting time (set this last - after all of the instrument moves are done AND the
751// intensity has been updated)
752//
753// ctTime is in seconds
754// sets the global variable, and fakes an "entry" in that field
755Function Sim_SetCountTime(ctTime)
756        Variable ctTime
757       
758        STRUCT WMSetVariableAction sva
759        NVAR ct = root:Packages:NIST:SAS:gCntTime
760        ct=ctTime
761        sva.dval = ctTime                       //seconds
762        sva.eventCode = 3               //live update code
763        CountTimeSetVarProc(sva)
764        return(0)
765End
766
767
768Function Sim_SetIncohXS(val)
769        Variable val
770       
771        NVAR xs = root:Packages:NIST:SAS:gSig_incoh
772        xs = val
773        return(0)
774End
775
776Function Sim_SetThickness(val)
777        Variable val
778       
779        NVAR th = root:Packages:NIST:SAS:gThick
780        th = val
781        return(0)
782End
783
784Function Sim_SetSampleRadius(val)
785        Variable val
786       
787        NVAR r2 = root:Packages:NIST:SAS:gR2
788        r2 = val
789       
790        return(0)
791End
792
793Function Sim_SetNumberOfNeutrons(val)
794        Variable val
795       
796        NVAR num = root:Packages:NIST:SAS:gImon
797        num = val
798       
799        return(0)
800End
801
802// 1 == beam stop in
803// 0 == beam stop out == transmission
804//
805Function Sim_SetBeamStopInOut(val)
806        Variable val
807       
808        NVAR BS = root:Packages:NIST:SAS:gBeamStopIn                    //set to zero for beamstop out (transmission)
809        WAVE rw=root:Packages:NIST:SAS:realsRead
810       
811        BS = val                        //set the global
812       
813        // fake the header
814        if(BS == 1)
815                rw[37] = 0                              // make sure BS X = 0 if BS is in
816        else
817                rw[37] = -10                    // fake BS out as X = -10 cm
818        endif
819       
820        return(0)
821End
822
823Function Sim_SetRawCountsCheck(val)
824        Variable val
825       
826        NVAR rawCt = root:Packages:NIST:SAS:gRawCounts
827        rawCt = val
828        return(0)
829End
830
831Function Sim_Set1D_ABS(val)
832        Variable val
833       
834        NVAR gAbs = root:Packages:NIST:SAS:g_1D_DoABS
835        gAbs = val
836       
837        return(0)
838End
839
840Function Sim_Set1D_Noise(val)
841        Variable val
842       
843        NVAR doNoise = root:Packages:NIST:SAS:g_1D_AddNoise
844        doNoise = val
845        return(0)
846End
847
848Function Sim_Set1D_Transmission(val)
849        Variable val
850       
851        NVAR trans = root:Packages:NIST:SAS:gSamTrans
852        trans = val
853        return(0)
854End
855
856Function Sim_SetModelFunction(strVal)
857        String strVal
858       
859        DoWindow MC_SASCALC
860        if(V_flag==1)
861                //then 2D
862                PopupMenu MC_popup0 win=MC_SASCALC,popmatch=strVal
863        endif
864       
865        DoWindow        Sim_1D_Panel
866        if(V_flag==1)
867                //then 1D
868                PopupMenu MC_popup0 win=Sim_1D_Panel,popmatch=strVal
869        endif
870       
871        SVAR gStr = root:Packages:NIST:SAS:gFuncStr
872        gStr = strVal
873
874        return(0)
875End
876
877
878// ------------
879
880//
881// set the threshold for the time warning dialog to some really large value (say 36000 s)
882// while doing the simulation to keep the dialog from popping up and stopping the simulation.
883// then be sure to set it back to 10 s for "normal" simulations that are atttended
884//
885// input val is in seconds
886Function Sim_SetSimTimeWarning(val)
887        Variable val
888
889        NVAR SimTimeWarn = root:Packages:NIST:SAS:g_SimTimeWarn
890        SimTimeWarn = val                       //sets the threshold for the warning dialog     
891
892        return(0)
893end
894
895// if you need to eliminate the high count data to save the rest of the set
896Function Sim_TrimOverflowCounts()
897        Wave w = root:Packages:NIST:SAS:linear_data
898        w = (w > 2767000) ? 0 : w
899        return(0)
900End
901
902// if home path exists, save there, otherwise present a dialog
903// (no sense to use catPathName, since this is simulation, not real data
904//
905Function Sim_Save1D_wName(type,titleStr,fname)
906        String type,titleStr,fname
907       
908        String fullPath
909        NVAR autoSaveIndex = root:Packages:NIST:SAS:gAutoSaveIndex
910
911        //now save the data     
912        PathInfo home
913        fullPath = S_path + fname
914       
915        Save_1DSimData("",runIndex=autoSaveIndex,simLabel=titleStr,saveName=fullPath)
916       
917        autoSaveIndex += 1
918        return(0)
919End
920
921Proc Sim_saveConfProc(waveStr)
922        String waveStr
923       
924        Sim_SaveConfiguration(waveStr)
925End
926//
927// just make a wave and fill it
928// I'll keep track of what's in each element
929// -- not very user friendly, but nobody needs to see this
930//
931// poll the panel to fill it in
932//
933// 0 = instrument
934// 1 = number of guides
935// 2 = source aperture
936// 3 = lambda
937// 4 = delta lambda
938// 5 = huber/chamber
939// 6 = sample aperture
940// 7 = lenses/prism/none
941// 8 = SDD
942// 9 = offset
943//
944Function Sim_SaveConfiguration(waveStr)
945        String waveStr
946       
947        String str=""
948       
949        SetDataFolder root:Packages:NIST:SAS
950       
951        Make/O/T/N=20 $("Config_"+waveStr)
952        Wave/T tw=$("Config_"+waveStr)
953        tw = ""
954       
955        // 0 = instrument
956        SVAR instStr = root:Packages:NIST:SAS:gInstStr
957        tw[0] = "check"+instStr
958       
959        // 1 = number of guides
960        NVAR ng = root:Packages:NIST:SAS:gNg
961        tw[1] = num2str(ng)
962       
963        // 2 = source aperture
964        ControlInfo/W=SASCALC popup0
965        tw[2] = S_Value
966       
967        // 3 = lambda
968        NVAR lam = root:Packages:NIST:SAS:gLambda
969        tw[3] = num2str(lam)
970       
971        // 4 = delta lambda
972//      NVAR dlam = root:Packages:NIST:SAS:gDeltaLambda
973//      tw[4] = num2str(dlam)
974       
975        //or
976        ControlInfo/W=SASCALC popup0_2
977        tw[4] = S_Value
978       
979       
980        // 5 = huber/chamber
981        NVAR sampleTable = root:Packages:NIST:SAS:gTable                //2=chamber, 1=table
982        if(sampleTable==1)
983                str = "checkHuber"
984        else
985                str = "checkChamber"
986        endif
987        tw[5] = str
988       
989        // 6 = sample aperture
990        ControlInfo/W=SASCALC popup0_1
991        tw[6] = S_Value
992       
993        // 7 = lenses/prism/none
994        NVAR lens = root:Packages:NIST:SAS:gUsingLenses         //==0 for no lenses, 1 for lenses(or prisms)
995        tw[7] = num2str(lens)
996       
997        // 8 = SDD
998        NVAR sdd = root:Packages:NIST:SAS:gDetDist
999        tw[8] = num2str(sdd)
1000       
1001        // 9 = offset
1002        NVAR offset = root:Packages:NIST:SAS:gOffset
1003        tw[9] = num2str(offset)
1004       
1005        //
1006       
1007        SetDataFolder root:
1008        return(0)
1009End
1010
1011Proc Sim_moveConfProc(waveStr)
1012        String waveStr
1013        Prompt waveStr,"Select Configuration",popup,ListSASCALCConfigs()
1014       
1015        Sim_MoveToConfiguration($("root:Packages:NIST:SAS:"+waveStr))
1016End
1017// restore the configuration given a wave of information
1018//
1019// be sure to recalculate the intensity after all is set
1020// -- some changes recalculate, some do not...
1021//
1022Function Sim_MoveToConfiguration(tw)
1023        WAVE/T tw
1024
1025
1026// 0 = instrument
1027        Sim_SetInstrument(tw[0])
1028         
1029// 1 = number of guides
1030// 2 = source aperture
1031        Sim_SetNumGuides_SrcAp(str2num(tw[1]),tw[2])
1032       
1033// 3 = lambda
1034        Sim_SetLambda(str2num(tw[3]))
1035       
1036// 4 = delta lambda
1037        Sim_SetDeltaLambda(tw[4])
1038
1039// 5 = huber/chamber
1040        Sim_SetTablePos(tw[5])
1041
1042// 6 = sample aperture
1043        Sim_SetSampleApertureDiam(tw[6])
1044
1045// 7 = lenses/prism/none
1046        Sim_SetLenses(str2num(tw[7]))
1047       
1048// 8 = SDD
1049        Sim_SetSDD(str2num(tw[8]))
1050
1051// 9 = offset
1052        Sim_SetOffset(str2num(tw[9]))
1053
1054
1055        ReCalculateInten(1)
1056        return(0)
1057end
1058
1059
1060///////////// a panel to (maybe) write to speed the setup of the runs
1061//
1062Function/S ListSASCALCConfigs()
1063        String str
1064        SetDataFolder root:Packages:NIST:SAS
1065        str = WaveList("Conf*",";","")
1066        SetDataFolder root:
1067        print str
1068        return(str)
1069End
1070
1071//Proc Sim_SetupRunPanel() : Panel
1072//      PauseUpdate; Silent 1           // building window...
1073//      NewPanel /W=(399,44,764,386)
1074//      ModifyPanel cbRGB=(56639,28443,117)
1075//      ShowTools/A
1076//EndMacro
Note: See TracBrowser for help on using the repository browser.