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

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

Added a procedure file to allow "scripting" of the MonteCarlo? simulation: MC_SimulationScripting.ipf. See the beginning of the file for instructions, and then the examples for numbered instructions of what needs to be edited to write your own script. Not casual-user-friendly, but makes it easire for more advanced users to simulate an entire experiment.

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