source: branches/uq/gui/scripts/analyzer.tcl @ 5169

Last change on this file since 5169 was 5169, checked in by mmh, 10 years ago

fix multiple simulations

File size: 55.4 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: analyzer - output area for Rappture
4#
5#  This widget acts as the output side of a Rappture application.
6#  When the input has changed, it displays a Simulate button that
7#  launches the simulation.  When a simulation is running, this
8#  area shows status.  When it is finished, the results appear
9#  in place of the button, according to the specs in the <analyze>
10#  XML data.
11# ======================================================================
12#  AUTHOR:  Michael McLennan, Purdue University
13#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
14#
15#  See the file "license.terms" for information on usage and
16#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17# ======================================================================
18package require Itk
19
20option add *Analyzer.width 3.5i widgetDefault
21option add *Analyzer.height 4i widgetDefault
22option add *Analyzer.simControl "auto" widgetDefault
23option add *Analyzer.simControlBackground "" widgetDefault
24option add *Analyzer.simControlOutline gray widgetDefault
25option add *Analyzer.simControlActiveBackground #ffffcc widgetDefault
26option add *Analyzer.simControlActiveOutline black widgetDefault
27option add *Analyzer.notebookpage "about" widgetDefault
28
29option add *Analyzer.font \
30    -*-helvetica-medium-r-normal-*-12-* widgetDefault
31option add *Analyzer.codeFont \
32    -*-courier-medium-r-normal-*-12-* widgetDefault
33option add *Analyzer.textFont \
34    -*-helvetica-medium-r-normal-*-12-* widgetDefault
35option add *Analyzer.boldTextFont \
36    -*-helvetica-bold-r-normal-*-12-* widgetDefault
37
38itcl::class Rappture::Analyzer {
39    inherit itk::Widget
40
41    itk_option define -codefont codeFont Font ""
42    itk_option define -textfont textFont Font ""
43    itk_option define -boldtextfont boldTextFont Font ""
44    itk_option define -simcontrol simControl SimControl ""
45    itk_option define -simcontroloutline simControlOutline Background ""
46    itk_option define -simcontrolbackground simControlBackground Background ""
47    itk_option define -simcontrolactiveoutline simControlActiveOutline Background ""
48    itk_option define -simcontrolactivebackground simControlActiveBackground Background ""
49    itk_option define -holdwindow holdWindow HoldWindow ""
50    itk_option define -notebookpage notebookPage NotebookPage ""
51
52    constructor {tool args} {
53        # defined below
54    }
55    destructor {
56        # defined below
57    }
58
59    public method simulate {args}
60    public method reset {{when -eventually}}
61    public method load {xmlobj}
62    public method clear {{xmlobj "all"}}
63    public method download {option args}
64
65    protected method _plot {args}
66    protected method _reorder {comps}
67    protected method _autoLabel {xmlobj path title cntVar}
68    protected method _fixResult {}
69    protected method _fixResultSet {args}
70    protected method _fixSize {}
71    protected method _fixSimControl {}
72    protected method _fixNotebook {}
73    protected method _simState {state args}
74    protected method _simOutput {message}
75    protected method _resultTooltip {}
76    protected method _isPdbTrajectory {data}
77    protected method _isLammpsTrajectory {data}
78    protected method _pdbToSequence {xmlobj path id child data}
79    protected method _lammpsToSequence {xmlobj path id child data}
80    protected method _trajToSequence {xmlobj {path ""}}
81    protected method _pop_uq_dialog {win}
82    protected method _setWaitVariable {state}
83    protected method _adjust_level {win}
84
85    private variable _tool ""          ;# belongs to this tool
86    private variable _appName ""       ;# Name of application
87    private variable _control "manual" ;# start mode
88    private variable _resultset ""     ;# ResultSet object with all results
89    private variable _pages 0          ;# number of pages for result sets
90    private variable _label2page       ;# maps output label => result set
91    private variable _label2desc       ;# maps output label => description
92    private variable _label2item       ;# maps output label => output.xxx item
93    private variable _lastlabel ""     ;# label of last example loaded
94    private variable _plotlist ""      ;# items currently being plotted
95    private variable _lastPlot
96    private common job                 ;# array var used for blt::bgexec jobs
97    private variable _wait_uq 0
98}
99
100itk::usual Analyzer {
101    keep -background -cursor foreground -font
102}
103
104# ----------------------------------------------------------------------
105# CONSTRUCTOR
106# ----------------------------------------------------------------------
107itcl::body Rappture::Analyzer::constructor {tool args} {
108    set _tool $tool
109
110    # use this to store all simulation results
111    set _resultset [Rappture::ResultSet ::\#auto]
112    $_resultset notify add $this [itcl::code $this _fixResultSet]
113
114    # widget settings...
115    itk_option add hull.width hull.height
116    pack propagate $itk_component(hull) no
117
118    frame $itk_interior.simol -borderwidth 1 -relief flat
119    pack $itk_interior.simol -fill x
120
121    itk_component add simbg {
122        frame $itk_interior.simol.simbg -borderwidth 0
123    } {
124        usual
125        rename -background -simcontrolcolor simControlColor Color
126    }
127    pack $itk_component(simbg) -expand yes -fill both
128
129    set simtxt [string trim [$tool xml get tool.action.label]]
130    if {"" == $simtxt} {
131        set simtxt "Simulate"
132    }
133    itk_component add simulate {
134        button $itk_component(simbg).simulate -text $simtxt \
135            -command [itcl::code $this simulate]
136    } {
137        usual
138        rename -highlightbackground -simcontrolcolor simControlColor Color
139    }
140    pack $itk_component(simulate) -side left -padx 4 -pady 4
141
142    # BE CAREFUL: Shift focus when we click on "Simulate".
143    #   This shifts focus away from input widgets and causes them to
144    #   finalize and log any pending changes.  A better way would be
145    #   to have the "sync" operation finalize/sync, but this works
146    #   for now.
147    bind $itk_component(simulate) <ButtonPress> {focus %W}
148
149    # if there's a hub url, then add "About" and "Questions" links
150    set _appName [$_tool xml get tool.id]
151    set url [Rappture::Tool::resources -huburl]
152    if {"" != $url && "" != $_appName} {
153        itk_component add hubcntls {
154            frame $itk_component(simbg).hubcntls
155        } {
156            usual
157            rename -background -simcontrolcolor simControlColor Color
158        }
159        pack $itk_component(hubcntls) -side right -padx 4
160
161        itk_component add icon {
162            label $itk_component(hubcntls).icon -image [Rappture::icon ask] \
163                -highlightthickness 0
164        } {
165            usual
166            ignore -highlightthickness
167            rename -background -simcontrolcolor simControlColor Color
168        }
169        pack $itk_component(icon) -side left
170
171        itk_component add about {
172            button $itk_component(hubcntls).about -text "About this tool" \
173                -command [list Rappture::filexfer::webpage \
174                              "$url/tools/$_appName"]
175        } {
176            usual
177            ignore -font
178            rename -background -simcontrolcolor simControlColor Color
179            rename -highlightbackground -simcontrolcolor simControlColor Color
180        }
181        pack $itk_component(about) -side top -anchor w
182
183        itk_component add questions {
184            button $itk_component(hubcntls).questions -text Questions? \
185                -command [list Rappture::filexfer::webpage \
186                              "$url/resources/$_appName/questions"]
187        } {
188            usual
189            ignore -font
190            rename -background -simcontrolcolor simControlColor Color
191            rename -highlightbackground -simcontrolcolor simControlColor Color
192        }
193        pack $itk_component(questions) -side top -anchor w
194    }
195
196    itk_component add simstatus {
197        text $itk_component(simbg).simstatus -borderwidth 0 \
198            -highlightthickness 0 -height 1 -width 1 -wrap none \
199            -state disabled
200    } {
201        usual
202        ignore -highlightthickness
203        rename -background -simcontrolcolor simControlColor Color
204        rename -font -textfont textFont Font
205    }
206    pack $itk_component(simstatus) -side left -expand yes -fill x
207
208    $itk_component(simstatus) tag configure popup \
209        -underline 1 -foreground blue
210
211    $itk_component(simstatus) tag bind popup \
212        <Enter> {%W configure -cursor center_ptr}
213    $itk_component(simstatus) tag bind popup \
214        <Leave> {%W configure -cursor ""}
215    $itk_component(simstatus) tag bind popup \
216        <ButtonPress> {after idle {Rappture::Tooltip::tooltip show %W}}
217
218
219    itk_component add notebook {
220        Rappture::Notebook $itk_interior.nb
221    }
222    pack $itk_interior.nb -expand yes -fill both
223
224    # ------------------------------------------------------------------
225    # ABOUT PAGE
226    # ------------------------------------------------------------------
227    set w [$itk_component(notebook) insert end about]
228
229    Rappture::Scroller $w.info -xscrollmode off -yscrollmode auto
230    pack $w.info -expand yes -fill both -padx 4 -pady 20
231    itk_component add toolinfo {
232        text $w.info.text -width 1 -height 1 -wrap word \
233            -borderwidth 0 -highlightthickness 0
234    } {
235        usual
236        ignore -borderwidth -relief
237        rename -font -textfont textFont Font
238    }
239    $w.info contents $w.info.text
240
241    # ------------------------------------------------------------------
242    # SIMULATION PAGE
243    # ------------------------------------------------------------------
244    set w [$itk_component(notebook) insert end simulate]
245    frame $w.cntls
246    pack $w.cntls -side bottom -fill x -pady 12
247    frame $w.cntls.sep -background black -height 1
248    pack $w.cntls.sep -side top -fill x
249
250    itk_component add abort {
251        button $w.cntls.abort -text "Abort" \
252            -command [itcl::code $_tool abort]
253    }
254    pack $itk_component(abort) -side left -expand yes -padx 4 -pady 4
255
256    Rappture::Scroller $w.info -xscrollmode auto -yscrollmode auto
257    pack $w.info -expand yes -fill both -padx 4 -pady 4
258    itk_component add runinfo {
259        text $w.info.text -width 1 -height 1 -wrap none \
260            -borderwidth 0 -highlightthickness 0 \
261            -state disabled
262    } {
263        usual
264        ignore -borderwidth -relief
265        rename -font -codefont codeFont Font
266    }
267    $w.info contents $w.info.text
268
269    itk_component add progress {
270        Rappture::Progress $w.progress
271    }
272
273    # ------------------------------------------------------------------
274    # ANALYZE PAGE
275    # ------------------------------------------------------------------
276    set w [$itk_component(notebook) insert end analyze]
277
278    frame $w.top
279    pack $w.top -side top -fill x -pady 8
280    label $w.top.l -text "Result:" -font $itk_option(-font)
281    pack $w.top.l -side left
282
283    itk_component add viewselector {
284        Rappture::Combobox $w.top.sel -width 10 -editable no
285    } {
286        usual
287        rename -font -textfont textFont Font
288    }
289    pack $itk_component(viewselector) -side left -expand yes -fill x
290    bind $itk_component(viewselector) <<Value>> [itcl::code $this _fixResult]
291    bind $itk_component(viewselector) <Enter> \
292        [itcl::code $this download coming]
293
294    Rappture::Tooltip::for $itk_component(viewselector) \
295        "@[itcl::code $this _resultTooltip]"
296
297    $itk_component(viewselector) choices insert end \
298        --- "---"
299
300    itk_component add download {
301        button $w.top.dl -image [Rappture::icon download] -anchor e \
302            -borderwidth 1 -relief flat -overrelief raised \
303            -command [itcl::code $this download start $w.top.dl]
304    }
305    pack $itk_component(download) -side right -padx {4 0}
306    bind $itk_component(download) <Enter> \
307        [itcl::code $this download coming]
308
309    $itk_component(viewselector) choices insert end \
310        @download [Rappture::filexfer::label download]
311
312    if {[Rappture::filexfer::enabled]} {
313        Rappture::Tooltip::for $itk_component(download) "Downloads the current result to a new web browser window on your desktop.  From there, you can easily print or save results.
314
315NOTE:  Your web browser must allow pop-ups from this site.  If your output does not appear, look for a 'pop-up blocked' message and enable pop-ups."
316    } else {
317        Rappture::Tooltip::for $itk_component(download) "Saves the current result to a file on your desktop."
318    }
319
320    itk_component add results {
321        Rappture::Panes $w.pane \
322            -sashwidth 2 -sashrelief solid -sashpadding {2 0}
323    } {
324        usual
325        ignore -sashwidth -sashrelief -sashpadding
326    }
327    pack $itk_component(results) -expand yes -fill both
328    set f [$itk_component(results) pane 0]
329
330    itk_component add resultpages {
331        Rappture::Notebook $f.nb
332    }
333    pack $itk_component(resultpages) -expand yes -fill both
334   set f [$itk_component(results) insert end -fraction 0.1]
335    itk_component add resultselector {
336        Rappture::ResultSelector $f.rsel -resultset $_resultset \
337            -settingscommand [itcl::code $this _plot]
338    }
339    pack $itk_component(resultselector) -expand yes -fill both
340    bind $itk_component(resultselector) <<Layout>> [itcl::code $this _fixSize]
341    bind $itk_component(results) <Configure> [itcl::code $this _fixSize]
342
343    eval itk_initialize $args
344
345    $itk_component(runinfo) tag configure ERROR -foreground red
346    $itk_component(runinfo) tag configure text -font $itk_option(-textfont)
347
348    #
349    # Load up tool info on the first page.
350    #
351    $itk_component(toolinfo) tag configure title \
352        -font $itk_option(-boldtextfont)
353
354    set mesg [string trim [$tool xml get tool.title]]
355    if {"" != $mesg} {
356        $itk_component(toolinfo) insert end $mesg title
357        $itk_component(toolinfo) insert end "\n\n"
358    }
359
360    set mesg [string trim [$tool xml get tool.about]]
361    if {"" != $mesg} {
362        $itk_component(toolinfo) insert end $mesg
363    }
364    $itk_component(toolinfo) configure -state disabled
365    $itk_component(notebook) current about
366
367    # tool can run on "manual" (default) or "auto"
368    set cntl [string trim [$tool xml get tool.control]]
369    if {"" == $cntl} {
370        set cntl [string trim [$tool xml get tool.control.type]]
371    }
372    if {"" != $cntl} {
373        set _control $cntl
374    }
375
376    # reset everything to a clean state
377    reset
378}
379
380# ----------------------------------------------------------------------
381# DESTRUCTOR
382# ----------------------------------------------------------------------
383itcl::body Rappture::Analyzer::destructor {} {
384    after cancel [itcl::code $this simulate]
385    $_resultset notify remove $this
386    itcl::delete object $_resultset
387}
388
389# ----------------------------------------------------------------------
390# USAGE: simulate ?-ifneeded?
391# USAGE: simulate ?<path1> <value1> <path2> <value2> ...?
392#
393# Kicks off the simulator by executing the tool.command associated
394# with the tool.  If any arguments are specified, they are used to
395# set parameters for the simulation.  While the simulation is running,
396# it shows status.  When the simulation is finished, it switches
397# automatically to "analyze" mode and shows the results.
398# ----------------------------------------------------------------------
399itcl::body Rappture::Analyzer::simulate {args} {
400    puts "simulate args='$args'"
401    if {$args == "-ifneeded"} {
402        # check to see if simulation is really needed
403        $_tool sync
404        if {[$_resultset contains [$_tool xml object]]
405              && ![string equal $_control "manual-resim"]} {
406            # not needed -- show results and return
407            $itk_component(notebook) current analyze
408            return
409        }
410        set args ""
411    }
412    puts "simulation needed"
413
414    set uq [$_tool get_uq -uq_type smolyak -uq_args 2]
415    # pop up UQ window
416    if {[$uq num_runs] > 1} {
417        set status [$uq run_dialog $itk_component(simulate)]
418        if {$status == 0} {
419            # cancelled
420            return
421        }
422        lappend args -uq_type [$uq type]
423        lappend args -uq_args [$uq args]
424    }
425
426    puts "simulate args=$args"
427
428    # simulation is needed -- go to simulation page
429    $itk_component(notebook) current simulate
430
431    # no progress messages yet
432    pack forget $itk_component(progress)
433    lappend args -output [itcl::code $this _simOutput]
434
435    _simState off
436    $itk_component(runinfo) configure -state normal
437    $itk_component(runinfo) delete 1.0 end
438    $itk_component(runinfo) insert end "Running simulation...\n\n" text
439    $itk_component(runinfo) configure -state disabled
440
441    # if the hold window is set, then put up a busy cursor
442    if {$itk_option(-holdwindow) != ""} {
443        blt::busy hold $itk_option(-holdwindow)
444        raise $itk_component(hull)
445    }
446
447    # execute the job
448    puts "$_tool run $args"
449
450    foreach {status result} [eval $_tool run $args] break
451
452    # if job was aborted, then allow simulation again
453    if {$result == "ABORT"} {
454        _simState on "Aborted"
455    }
456
457    # load results from run.xml into analyzer
458    if {$status == 0 && $result != "ABORT"} {
459        set status [catch {load $result} result]
460    }
461
462    # back to normal
463    if {$itk_option(-holdwindow) != ""} {
464        blt::busy release $itk_option(-holdwindow)
465    }
466    $itk_component(abort) configure -state disabled
467
468    if {$status != 0} {
469        $itk_component(runinfo) configure -state normal
470        # Don't erase program error messages.
471        # $itk_component(runinfo) delete 1.0 end
472        $itk_component(runinfo) insert end "\n\nProblem launching job:\n\n" text
473        _simOutput $result
474        $itk_component(runinfo) configure -state disabled
475        $itk_component(runinfo) see 1.0
476
477        # Try to create a support ticket for this error.
478        # It may be a real problem.
479        if {[Rappture::bugreport::shouldReport for jobs]} {
480            set ::errorInfo "\n\n== RAPPTURE INPUT ==\n[$_tool xml xml]"
481            Rappture::bugreport::register "Problem launching job:\n$result"
482            Rappture::bugreport::attachment [$_tool xml xml]
483            Rappture::bugreport::send
484        }
485    } else {
486        $itk_component(notebook) current analyze
487    }
488
489    # do this last -- after _simOutput above
490    pack forget $itk_component(progress)
491}
492
493
494# ----------------------------------------------------------------------
495# USAGE: reset ?-eventually|-now?
496#
497# Used to reset the analyzer whenever the input to a simulation has
498# changed.  Sets the mode back to "simulate", so the user has to
499# simulate again to see the output.  If the <start> option is set
500# to "auto", the simulation is invoked immediately.
501# ----------------------------------------------------------------------
502itcl::body Rappture::Analyzer::reset {{when -eventually}} {
503    puts "RESET $when"
504    if {$when == "-eventually"} {
505        after cancel [list catch [itcl::code $this reset -now]]
506        after idle [list catch [itcl::code $this reset -now]]
507        return
508    }
509
510    puts "RESET contains [$_resultset contains [$_tool xml object]]"
511    puts [$_tool xml object]
512
513    # check to see if simulation is really needed
514    $_tool sync
515    if {![$_resultset contains [$_tool xml object]]
516          || [string equal $_control "manual-resim"]} {
517        # if control mode is "auto", then simulate right away
518        if {[string match auto* $_control]} {
519            # auto control -- don't need button
520            pack forget $itk_interior.simol
521
522            after cancel [itcl::code $this simulate]
523            after idle [itcl::code $this simulate]
524        } else {
525            _simState on "new input parameters"
526        }
527    } else {
528        _simState off
529    }
530}
531
532# ----------------------------------------------------------------------
533# USAGE: load <xmlobj>
534#
535# Loads the data from the given <xmlobj> into the appropriate results
536# sets.  If necessary, new results sets are created to store the data.
537# ----------------------------------------------------------------------
538itcl::body Rappture::Analyzer::load {xmlobj} {
539    # only show the last result? then clear first
540    if {[string trim [$_tool xml get tool.analyzer]] == "last"} {
541        clear
542    }
543    $_resultset add $xmlobj
544
545    # NOTE: Adding will trigger a !change event on the ResultSet
546    # object, which will trigger calls to _fixResultSet to add
547    # the results to display.
548}
549
550# ----------------------------------------------------------------------
551# USAGE: clear ?<xmlobj>?
552#
553# Discards one or more results previously loaded into the analyzer.
554# If an <xmlobj> is specified, then that one result is cleared.
555# Otherwise, all results are cleared.
556# ----------------------------------------------------------------------
557itcl::body Rappture::Analyzer::clear {{xmlobj "all"}} {
558    if {$xmlobj eq "" || $xmlobj eq "all"} {
559        $_resultset clear
560    } else {
561        $_resultset clear $xmlobj
562    }
563
564    # NOTE: Clearing will trigger a !change event on the ResultSet
565    # object, which will trigger calls to _fixResultSet to clean up
566    # the results being displayed.
567}
568
569# ----------------------------------------------------------------------
570# USAGE: download coming
571# USAGE: download controls <downloadCommand>
572# USAGE: download start ?widget?
573# USAGE: download now ?widget?
574#
575# Spools the current result so the user can download it.
576# ----------------------------------------------------------------------
577itcl::body Rappture::Analyzer::download {option args} {
578    set title [$itk_component(viewselector) value]
579    set page [$itk_component(viewselector) translate $title]
580
581    switch -- $option {
582        coming {
583            #
584            # Warn result that a download is coming, in case
585            # it needs to take a screen snap.
586            #
587            if {![regexp {^(|@download|---)$} $page]} {
588                set f [$itk_component(resultpages) page $page]
589                $f.rviewer download coming
590            }
591        }
592        controls {
593            # no controls for this download yet
594            return ""
595        }
596        start {
597            set widget $itk_component(download)
598            if {[llength $args] > 0} {
599                set widget [lindex $args 0]
600                if {[catch {winfo class $widget}]} {
601                    set widget $itk_component(download)
602                }
603            }
604            #
605            # See if this download has any controls.  If so, then
606            # post them now and let the user continue the download
607            # after selecting a file format.
608            #
609            if {$page != ""} {
610                set ext ""
611                set f [$itk_component(resultpages) page $page]
612                set arg [itcl::code $this download now $widget]
613                set popup [$f.rviewer download controls $arg]
614                if {"" != $popup} {
615                    $popup activate $widget below
616                } else {
617                    download now $widget
618                }
619            } else {
620                # this shouldn't happen
621                set file error.html
622                set data "<h1>Not Found</h1>There is no result selected."
623            }
624        }
625        now {
626            set widget $itk_component(download)
627            if {[llength $args] > 0} {
628                set widget [lindex $args 0]
629                if {[catch {winfo class $widget}]} {
630                    set widget $itk_component(download)
631                }
632            }
633            #
634            # Perform the actual download.
635            #
636            if {$page != ""} {
637                set ext ""
638                set f [$itk_component(resultpages) page $page]
639                set item [$itk_component(viewselector) value]
640                set result [$f.rviewer download now $widget $_appName $item]
641                if { $result == "" } {
642                    return;                # User cancelled the download.
643                }
644                foreach {ext data} $result break
645                if {"" == $ext} {
646                    if {"" != $widget} {
647                        Rappture::Tooltip::cue $widget \
648                            "Can't download this result."
649                    }
650                    return
651                }
652                regsub -all {[\ -\/\:-\@\{-\~]} $title {} title
653                set file "$title$ext"
654            } else {
655                # this shouldn't happen
656                set file error.html
657                set data "<h1>Not Found</h1>There is no result selected."
658            }
659
660            Rappture::Logger::log download [$itk_component(viewselector) value]
661            set mesg [Rappture::filexfer::download $data $file]
662            if {[string length $mesg] > 0} {
663                Rappture::Tooltip::cue $widget $mesg
664            }
665        }
666        default {
667            error "bad option \"$option\": should be coming, controls, now, start"
668        }
669    }
670}
671
672# ----------------------------------------------------------------------
673# USAGE: _plot ?<index> <options> <index> <options>...?
674#
675# Used internally to update the plot shown in the current result
676# viewer whenever the resultselector settings have changed.  Causes the
677# desired results to show up on screen.
678# ----------------------------------------------------------------------
679itcl::body Rappture::Analyzer::_plot {args} {
680    set _plotlist $args
681
682    set page [$itk_component(viewselector) value]
683    set page [$itk_component(viewselector) translate $page]
684    if {"" != $page} {
685        set f [$itk_component(resultpages) page $page]
686        $f.rviewer plot clear
687        foreach {index opts} $_plotlist {
688            $f.rviewer plot add $index $opts
689        }
690    }
691}
692
693# ----------------------------------------------------------------------
694# USAGE: _reorder <compList>
695#
696# Used internally to change the order of a series of output components
697# found in the <output> section.  Moves the <log> elements to the end
698# and returns the updated list.
699# ----------------------------------------------------------------------
700itcl::body Rappture::Analyzer::_reorder {comps} {
701    set i 0
702    set max [llength $comps]
703    while {$i < $max} {
704        set c [lindex $comps $i]
705        if {[string match log* $c]} {
706            set comps [lreplace $comps $i $i]
707            lappend comps $c
708            incr max -1
709        } else {
710            incr i
711        }
712    }
713    return $comps
714}
715
716# ----------------------------------------------------------------------
717# USAGE: _autoLabel <xmlobj> <path> <title> <cntVar>
718#
719# Used internally to check for an about.label property at the <path>
720# in <xmlobj>.  If this object doesn't have a label, then one is
721# supplied using the given <title>.  The <cntVar> is an array of
722# counters in the calling scopes for titles that have been used
723# in the past.  This is used to create titles like "Plot #2" the
724# second time it is encountered.
725#
726# The <xmlobj> is updated so that the label is inserted directly in
727# the tree.
728# ----------------------------------------------------------------------
729itcl::body Rappture::Analyzer::_autoLabel {xmlobj path title cntVar} {
730    upvar $cntVar counters
731
732    set group [$xmlobj get $path.about.group]
733    set label [$xmlobj get $path.about.label]
734    if {"" == $label} {
735        # no label -- make one up using the title specified
736        if {![info exists counters($group-$title)]} {
737            set counters($group-$title) 1
738            set label $title
739        } else {
740            set label "$title (#[incr counters($group-$title)])"
741        }
742        $xmlobj put $path.about.label $label
743    } else {
744        # handle the case of two identical labels in <output>
745        if {![info exists counters($group-$label)]} {
746            set counters($group-$label) 1
747        } else {
748            set label "$label (#[incr counters($group-$label)])"
749            $xmlobj put $path.about.label $label
750        }
751    }
752    return $label
753}
754
755# ----------------------------------------------------------------------
756# USAGE: _fixResult
757#
758# Used internally to change the result page being displayed whenever
759# the user selects a page from the results combobox.
760# ----------------------------------------------------------------------
761itcl::body Rappture::Analyzer::_fixResult {} {
762    set name [$itk_component(viewselector) value]
763    set page ""
764    if {"" != $name} {
765        set page [$itk_component(viewselector) translate $name]
766    }
767    if {$page == "@download"} {
768        # put the combobox back to its last value
769        $itk_component(viewselector) component entry configure -state normal
770        $itk_component(viewselector) component entry delete 0 end
771        $itk_component(viewselector) component entry insert end $_lastlabel
772        $itk_component(viewselector) component entry configure -state disabled
773        # perform the actual download
774        download start $itk_component(download)
775    } elseif {$page == "---"} {
776        # put the combobox back to its last value
777        $itk_component(viewselector) component entry configure -state normal
778        $itk_component(viewselector) component entry delete 0 end
779        $itk_component(viewselector) component entry insert end $_lastlabel
780        $itk_component(viewselector) component entry configure -state disabled
781    } elseif {$page != ""} {
782        set _lastlabel $name
783        set win [winfo toplevel $itk_component(hull)]
784        $itk_component(resultpages) current $page
785        set f [$itk_component(resultpages) page $page]
786
787        # We don't want to replot if we're using an existing viewer with the
788        # the same list of objects to plot.  So track the viewer and the list.
789        if { ![info exists _lastPlot($f)] || $_plotlist != $_lastPlot($f) } {
790            set _lastPlot($f) $_plotlist
791            blt::busy hold $win
792            $f.rviewer plot clear
793            eval $f.rviewer plot add $_plotlist
794            blt::busy release [winfo toplevel $itk_component(hull)]
795        }
796        Rappture::Logger::log output $_label2item($name)
797        Rappture::Tooltip::for $itk_component(viewselector) \
798            "@[itcl::code $this _resultTooltip]" -log $_label2item($name)
799    }
800}
801
802# ----------------------------------------------------------------------
803# USAGE: _fixResultSet ?<eventData>...?
804#
805# Used internally to react to changes within the ResultSet.  When a
806# result is added, a new result viewer is created for the object.
807# When all results are cleared, the viewers are deleted.
808# ----------------------------------------------------------------------
809itcl::body Rappture::Analyzer::_fixResultSet {args} {
810    array set eventData $args
811    switch -- $eventData(op) {
812        add {
813            set xmlobj $eventData(what)
814
815            # Detect molecule elements that contain trajectory data
816            # and convert to sequences.
817            _trajToSequence $xmlobj output
818
819            # Go through the analysis and find all result sets.
820            set haveresults 0
821            foreach item [_reorder [$xmlobj children output]] {
822                switch -glob -- $item {
823                    log* {
824                        _autoLabel $xmlobj output.$item "Output Log" counters
825                    }
826                    number* {
827                        _autoLabel $xmlobj output.$item "Number" counters
828                    }
829                    integer* {
830                        _autoLabel $xmlobj output.$item "Integer" counters
831                    }
832                    mesh* {
833                        _autoLabel $xmlobj output.$item "Mesh" counters
834                    }
835                    string* {
836                        _autoLabel $xmlobj output.$item "String" counters
837                    }
838                    histogram* - curve* - field* {
839                        _autoLabel $xmlobj output.$item "Plot" counters
840                    }
841                    drawing* {
842                        _autoLabel $xmlobj output.$item "Drawing" counters
843                    }
844                    structure* {
845                        _autoLabel $xmlobj output.$item "Structure" counters
846                    }
847                    table* {
848                        _autoLabel $xmlobj output.$item "Energy Levels" counters
849                    }
850                    sequence* {
851                        _autoLabel $xmlobj output.$item "Sequence" counters
852                    }
853                }
854                set label [$xmlobj get output.$item.about.group]
855                if {"" == $label} {
856                    set label [$xmlobj get output.$item.about.label]
857                }
858
859                set hidden [$xmlobj get output.$item.hide]
860                set hidden [expr {"" != $hidden && $hidden}]
861
862                if {"" != $label && !$hidden} {
863                    set haveresults 1
864                }
865            }
866
867            # if there are any valid results, add them to the resultset
868            if {$haveresults} {
869                set index [$_resultset get simnum $xmlobj]
870
871                # add each result to a result viewer
872                foreach item [_reorder [$xmlobj children output]] {
873                    set label [$xmlobj get output.$item.about.group]
874                    if {"" == $label} {
875                        set label [$xmlobj get output.$item.about.label]
876                    }
877                    set hidden [$xmlobj get output.$item.hide]
878                    if { $hidden == "" } {
879                        set hidden 0
880                    }
881                    if {"" != $label && !$hidden} {
882                        if {![info exists _label2page($label)]} {
883                            set name "page[incr _pages]"
884                            set page [$itk_component(resultpages) \
885                                insert end $name]
886                            set _label2page($label) $page
887                            set _label2item($label) output.$item
888                            set _label2desc($label) \
889                                [$xmlobj get output.$item.about.description]
890                            Rappture::ResultViewer $page.rviewer
891                            pack $page.rviewer -expand yes -fill both -pady 4
892
893                            set end [$itk_component(viewselector) \
894                                choices index -value ---]
895                            if {$end < 0} {
896                                set end "end"
897                            }
898                            $itk_component(viewselector) choices insert $end \
899                                $name $label
900                        }
901
902                        # add/replace the latest result into this viewer
903                        set page $_label2page($label)
904
905                        if {![info exists reset($page)]} {
906                            $page.rviewer clear $index
907                            set reset($page) 1
908                        }
909                        $page.rviewer add $index $xmlobj output.$item
910                    }
911                }
912            }
913
914            # show the first page by default
915            set max [$itk_component(viewselector) choices size]
916            for {set i 0} {$i < $max} {incr i} {
917                set first [$itk_component(viewselector) choices get -label $i]
918                if {$first != ""} {
919                    set page [$itk_component(viewselector) choices get -value $i]
920                    set char [string index $page 0]
921                    if {$char != "@" && $char != "-"} {
922                        $itk_component(resultpages) current $page
923                        $itk_component(viewselector) value $first
924                        set _lastlabel $first
925                        break
926                    }
927                }
928            }
929        }
930        clear {
931            if {$eventData(what) ne "all"} {
932                # delete this result from all viewers
933                array set params $eventData(what)
934                foreach label [array names _label2page] {
935                    set page $_label2page($label)
936                    $page.rviewer clear $params(simnum)
937                }
938            }
939
940            if {[$_resultset size] == 0} {
941                # reset the size of the controls area
942                set ht [winfo height $itk_component(results)]
943                set cntlht [$itk_component(resultselector) size -controlarea]
944                set frac [expr {double($cntlht)/$ht}]
945                $itk_component(results) fraction end $frac
946
947                foreach label [array names _label2page] {
948                    set page $_label2page($label)
949                    destroy $page.rviewer
950                }
951                $itk_component(resultpages) delete -all
952                set _pages 0
953
954                $itk_component(viewselector) value ""
955                $itk_component(viewselector) choices delete 0 end
956                catch {unset _label2page}
957                catch {unset _label2item}
958                catch {unset _label2desc}
959                set _plotlist ""
960
961                $itk_component(viewselector) choices insert end --- "---"
962                $itk_component(viewselector) choices insert end \
963                    @download [Rappture::filexfer::label download]
964                set _lastlabel ""
965            }
966
967            # fix Simulate button state
968            reset
969        }
970        default {
971            error "don't know how to handle op \"$eventData(op)\""
972        }
973    }
974}
975
976# ----------------------------------------------------------------------
977# USAGE: _fixSize
978#
979# Used internally to change the size of the result set area whenever
980# a new control appears.  Adjusts the size available for the result
981# set up to some maximum.
982# ----------------------------------------------------------------------
983itcl::body Rappture::Analyzer::_fixSize {} {
984    set ht [winfo height $itk_component(results)]
985    if {$ht <= 1} { set ht [winfo reqheight $itk_component(results)] }
986    set cntlht [$itk_component(resultselector) size -controlarea]
987    set frac [expr {double($cntlht)/$ht}]
988
989    if {$frac < 0.4} {
990        $itk_component(results) fraction end $frac
991    }
992    _fixSimControl
993}
994
995# ----------------------------------------------------------------------
996# USAGE: _simState <boolean> ?<message>? ?<settings>?
997#
998# Used internally to change the "Simulation" button on or off.
999# If the <boolean> is on, then any <message> and <settings> are
1000# displayed as well.  If the <boolean> is off, then only display
1001# the message. The <message> is a note to the user about
1002# what will be simulated, and the <settings> are a list of
1003# tool parameter settings of the form {path1 val1 path2 val2 ...}.
1004# When these are in place, the next Simulate operation will use
1005# these settings.  This helps fill in missing data values.
1006# ----------------------------------------------------------------------
1007itcl::body Rappture::Analyzer::_simState {state args} {
1008    if {$state} {
1009        $itk_interior.simol configure \
1010            -background $itk_option(-simcontrolactiveoutline)
1011        configure -simcontrolcolor $itk_option(-simcontrolactivebackground)
1012
1013        $itk_component(abort) configure -state disabled
1014        $itk_component(simulate) configure -state normal \
1015            -command [itcl::code $this simulate]
1016
1017        #
1018        # If there's a special message, then put it up next to the button.
1019        #
1020        set mesg [lindex $args 0]
1021        if {"" != $mesg} {
1022            $itk_component(simstatus) configure -state normal
1023            $itk_component(simstatus) delete 1.0 end
1024            $itk_component(simstatus) insert end $mesg
1025
1026            #
1027            # If there are any settings, then install them in the
1028            # "Simulate" button.  Also, pop them up as a tooltip
1029            # for the message.
1030            #
1031            set settings [lindex $args 1]
1032            if {[llength $settings] > 0} {
1033                $itk_component(simulate) configure \
1034                    -command [eval itcl::code $this simulate $settings]
1035
1036                set details ""
1037                foreach {path val} $settings {
1038                    set str [string trim [$_tool xml get $path.about.label]]
1039                    if {"" == $str} {
1040                        set str [$_tool xml element -as id $path]
1041                    }
1042                    append details "$str = $val\n"
1043                }
1044                set details [string trim $details]
1045
1046                Rappture::Tooltip::for $itk_component(simstatus) $details
1047                $itk_component(simstatus) insert end " "
1048                $itk_component(simstatus) insert end "(details...)" popup
1049            }
1050            $itk_component(simstatus) configure -state disabled
1051        }
1052    } else {
1053        if {"" != $itk_option(-simcontrolbackground)} {
1054            set simcbg $itk_option(-simcontrolbackground)
1055        } else {
1056            set simcbg $itk_option(-background)
1057        }
1058        $itk_interior.simol configure \
1059            -background $itk_option(-simcontroloutline)
1060        configure -simcontrolcolor $simcbg
1061
1062        $itk_component(simulate) configure -state disabled
1063        $itk_component(abort) configure -state normal
1064
1065        $itk_component(simstatus) configure -state normal
1066        $itk_component(simstatus) delete 1.0 end
1067        set mesg [lindex $args 0]
1068        if {"" != $mesg} {
1069            $itk_component(simstatus) insert end $mesg
1070        }
1071        $itk_component(simstatus) configure -state disabled
1072    }
1073}
1074
1075# ----------------------------------------------------------------------
1076# USAGE: _simOutput <message>
1077#
1078# Invoked automatically whenever output comes in while running the
1079# tool.  Extracts any =RAPPTURE-???=> messages from the output and
1080# sends the output to the display.  For example, any
1081# =RAPPTURE-PROGRESS=> message pops up the progress meter and updates
1082# it to show the latest progress message.  This is useful for
1083# long-running tools, to let the user know how much longer the
1084# simulation will take.
1085# ----------------------------------------------------------------------
1086itcl::body Rappture::Analyzer::_simOutput {message} {
1087    #
1088    # Scan through and pick out any =RAPPTURE-PROGRESS=> messages first.
1089    #
1090    while {[regexp -indices \
1091               {=RAPPTURE-PROGRESS=> *([-+]?[0-9]+) +([^\n]*)(\n|$)} $message \
1092                match percent mesg]} {
1093
1094        foreach {i0 i1} $percent break
1095        set percent [string range $message $i0 $i1]
1096
1097        foreach {i0 i1} $mesg break
1098        set mesg [string range $message $i0 $i1]
1099
1100        pack $itk_component(progress) -fill x -padx 10 -pady 10
1101        $itk_component(progress) settings -percent $percent -message $mesg
1102
1103        foreach {i0 i1} $match break
1104        set message [string replace $message $i0 $i1]
1105    }
1106
1107    #
1108    # Now handle SUBMIT-PROGRESS
1109    #
1110    while {[regexp -indices {=SUBMIT-PROGRESS=> aborted=([0-9]+) finished=([0-9]+) failed=([0-9]+) executing=([0-9]+)\
1111        waiting=([0-9]+) setting_up=([0-9]+) setup=([0-9]+) %done=([0-9.]+) timestamp=([0-9.]+)} $message \
1112        match aborted finished failed executing waiting setting_up setup percent ts mesg]} {
1113
1114        set mesg ""
1115        foreach {i0 i1} $percent break
1116        set percent [string range $message $i0 $i1]
1117        foreach {i0 i1} $failed break
1118        set failed [string range $message $i0 $i1]
1119        foreach {i0 i1} $match break
1120        set message [string replace $message $i0 $i1]
1121
1122        if {$failed != 0} {set mesg "$failed jobs failed!"}
1123        if {$percent >= 100} { set mesg "Jobs finished.  Analyzing results..."}
1124
1125        pack $itk_component(progress) -fill x -padx 10 -pady 10
1126        $itk_component(progress) settings -percent $percent -message $mesg
1127    }
1128
1129    #
1130    # Break up the remaining lines according to =RAPPTURE-ERROR=> messages.
1131    # Show errors in a special color.
1132    #
1133    $itk_component(runinfo) configure -state normal
1134
1135    while {[regexp -indices \
1136               {=RAPPTURE-([a-zA-Z]+)=>([^\n]*)(\n|$)} $message \
1137                match type mesg]} {
1138
1139        foreach {i0 i1} $match break
1140        set first [string range $message 0 [expr {$i0-1}]]
1141        if {[string length $first] > 0} {
1142            $itk_component(runinfo) insert end $first
1143            $itk_component(runinfo) insert end \n
1144        }
1145
1146        foreach {t0 t1} $type break
1147        set type [string range $message $t0 $t1]
1148        foreach {m0 m1} $mesg break
1149        set mesg [string range $message $m0 $m1]
1150        if {[string length $mesg] > 0 && $type != "RUN"} {
1151            $itk_component(runinfo) insert end $mesg $type
1152            $itk_component(runinfo) insert end \n $type
1153        }
1154
1155        set message [string range $message [expr {$i1+1}] end]
1156    }
1157
1158    if {[string length $message] > 0} {
1159        $itk_component(runinfo) insert end $message
1160        if {[$itk_component(runinfo) get end-2char] != "\n"} {
1161            $itk_component(runinfo) insert end "\n"
1162        }
1163        $itk_component(runinfo) see end
1164    }
1165    $itk_component(runinfo) configure -state disabled
1166}
1167
1168# ----------------------------------------------------------------------
1169# USAGE: _resultTooltip
1170#
1171# Used internally to build the tooltip string displayed for the
1172# result selector.  If the current page has an associated description,
1173# then it is displayed beneath the result.
1174#
1175# Returns the string for the tooltip.
1176# ----------------------------------------------------------------------
1177itcl::body Rappture::Analyzer::_resultTooltip {} {
1178    set tip ""
1179    set name [$itk_component(viewselector) value]
1180    if {[info exists _label2desc($name)] &&
1181         [string length $_label2desc($name)] > 0} {
1182        append tip "$_label2desc($name)\n\n"
1183    }
1184    if {[array size _label2page] > 1} {
1185        append tip "Use this control to display other output results."
1186    }
1187    return $tip
1188}
1189
1190# ----------------------------------------------------------------------
1191# USAGE: _fixSimControl
1192#
1193# Used internally to add or remove the simulation control at the
1194# top of the analysis area.  This is controlled by the -simcontrol
1195# option.
1196# ----------------------------------------------------------------------
1197itcl::body Rappture::Analyzer::_fixSimControl {} {
1198    switch -- $itk_option(-simcontrol) {
1199        on {
1200            pack $itk_interior.simol -fill x -before $itk_interior.nb
1201        }
1202        off {
1203            pack forget $itk_interior.simol
1204        }
1205        auto {
1206            #
1207            # If we have two or more radiodials, then there is a
1208            # chance of encountering a combination of parameters
1209            # with no data, requiring simulation.
1210            #
1211            if {[$itk_component(resultselector) size -controls] >= 2} {
1212                pack $itk_interior.simol -fill x -before $itk_interior.nb
1213            } else {
1214                pack forget $itk_interior.simol
1215            }
1216        }
1217        default {
1218            error "bad value \"$itk_option(-simcontrol)\": should be on, off, auto"
1219        }
1220    }
1221}
1222
1223# ----------------------------------------------------------------------
1224# USAGE: _fixNotebook
1225#
1226# Used internally to switch the active notebook page
1227# ----------------------------------------------------------------------
1228itcl::body Rappture::Analyzer::_fixNotebook {} {
1229    switch -- $itk_option(-notebookpage) {
1230        about {
1231            $itk_component(notebook) current about
1232        }
1233        simulate {
1234            $itk_component(notebook) current simulate
1235        }
1236        analyze {
1237            $itk_component(notebook) current analyze
1238        }
1239        default {
1240            error "bad value \"$itk_option(-notebookpage)\": should be about, simulate, analyze"
1241        }
1242    }
1243}
1244
1245# ----------------------------------------------------------------------
1246# USAGE: _isPdbTrajectory <data>
1247#
1248# Used internally to determine whether pdb or lammps data represents a
1249# trajectory rather than a single frame
1250# ----------------------------------------------------------------------
1251itcl::body Rappture::Analyzer::_isPdbTrajectory {data} {
1252    if { [llength $data] == 0 } {
1253        return 0
1254    }
1255    set nModels 0
1256    foreach line $data {
1257        if { [string match "MODEL*" $line] }  {
1258            incr nModels
1259            if { $nModels > 1 } {
1260                # Stop if more than one model found.  No need to count them
1261                # all.
1262                return 1
1263            }
1264        }
1265    }
1266    return 0
1267}
1268
1269# ----------------------------------------------------------------------
1270# USAGE: _isLammpsTrajectory <data>
1271#
1272# Used internally to determine whether pdb or lammps data represents a
1273# trajectory rather than a single frame
1274# ----------------------------------------------------------------------
1275itcl::body Rappture::Analyzer::_isLammpsTrajectory { data } {
1276    if { [llength $data] == 0 } {
1277        return 0
1278    }
1279    set nModels 0
1280    foreach line $data {
1281        if { [regexp {^[\t ]*ITEM:[ \t]+TIMESTEP} $line] } {
1282            incr nModels
1283            if { $nModels > 1 } {
1284                # Stop if more than one model found.  No need to count them
1285                # all.
1286                return 1
1287            }
1288        }
1289    }
1290    return 0
1291}
1292
1293# ----------------------------------------------------------------------
1294# USAGE: _pdbToSequence <xmlobj> ?<path>?
1295#
1296# If the molecule element is a trajectory, delete the original
1297# and create a sequence of individual molecules.
1298# Used internally to detect any molecule output elements that contain
1299# trajectory data.  Trajectories will be converted into sequences of
1300# individual molecules.  All other elements will be unaffected. Scans
1301# the entire xml tree if a starting path is not specified.
1302# ----------------------------------------------------------------------
1303itcl::body Rappture::Analyzer::_pdbToSequence {xmlobj path id child data} {
1304
1305    set seqLabel [$xmlobj get ${child}.about.label]
1306    set descr    [$xmlobj get ${child}.about.description]
1307    set formula  [$xmlobj get ${child}.components.molecule.formula]
1308    $xmlobj remove $child
1309
1310    set sequence  $path.sequence($id)
1311    $xmlobj put ${sequence}.about.label $seqLabel
1312    $xmlobj put ${sequence}.about.description $descr
1313    $xmlobj put ${sequence}.index.label "Frame"
1314
1315    set frameNum 0
1316    set numLines [llength $data]
1317    for { set i 0 } { $i < $numLines } { incr i } {
1318        set line [lindex $data $i]
1319        set line [string trim $line]
1320        set contents {}
1321        if { [string match "MODEL*" $line] } {
1322            # Save the contents until we get an ENDMDL record.
1323            for {} { $i < $numLines } { incr i } {
1324                set line [lindex $data $i]
1325                set line [string trim $line]
1326                if { $line == "" } {
1327                    continue;           # Skip blank lines.
1328                }
1329                if { [string match "ENDMDL*" $line] } {
1330                    break;
1331                }
1332                append contents $line\n
1333            }
1334            set frame ${sequence}.element($frameNum)
1335            $xmlobj put ${frame}.index $frameNum
1336
1337            set molecule ${frame}.structure.components.molecule
1338            $xmlobj put ${molecule}.pdb $contents
1339            $xmlobj put ${molecule}.formula $formula
1340            incr frameNum
1341        }
1342    }
1343}
1344
1345# ----------------------------------------------------------------------
1346# USAGE: _lammpsToSequence <xmlobj> ?<path>?
1347#
1348# If the molecule element is a trajectory, delete the original
1349# and create a sequence of individual molecules.
1350# Used internally to detect any molecule output elements that contain
1351# trajectory data.  Trajectories will be converted into sequences of
1352# individual molecules.  All other elements will be unaffected. Scans
1353# the entire xml tree if a starting path is not specified.
1354# ----------------------------------------------------------------------
1355itcl::body Rappture::Analyzer::_lammpsToSequence {xmlobj path id child data} {
1356
1357    set seqLabel [$xmlobj get ${child}.about.label]
1358    set descr    [$xmlobj get ${child}.about.description]
1359    set typemap  [$xmlobj get ${child}.components.molecule.lammpstypemap]
1360    $xmlobj remove $child
1361
1362    set sequence ${path}.sequence($id)
1363    $xmlobj put ${sequence}.about.label $seqLabel
1364    $xmlobj put ${sequence}.about.description $descr
1365    $xmlobj put ${sequence}.index.label "Frame"
1366
1367    set frameNum 0
1368    set frameContents ""
1369    set inModel 0
1370    foreach line $data {
1371        set line [string trim $line]
1372        if { $line == "" } {
1373            continue;                   # Skip blank lines
1374        }
1375        if {[regexp {^[\t ]*ITEM:[ \t]+ATOMS} $line] } {
1376            if { $inModel && $frameContents != "" } {
1377                set frame ${sequence}.element($frameNum)
1378                $xmlobj put ${frame}.index $frameNum
1379
1380                set molecule ${frame}.structure.components.molecule
1381                $xmlobj put ${molecule}.lammps $frameContents
1382                $xmlobj put ${molecule}.lammpstypemap $typemap
1383
1384                incr frameNum
1385                set frameContents ""
1386            }
1387            set inModel 1
1388        } elseif { [scan $line "%d %d %f %f %f" a b c d e] == 5 } {
1389            if { !$inModel } {
1390                puts stderr "found \"$line\" without previous \"ITEM: ATOMS\""
1391                set inModel 1
1392            }
1393            append frameContents $line\n
1394        }
1395    }
1396    if { $frameContents != "" } {
1397        set frame ${sequence}.element($frameNum)
1398        $xmlobj put ${frame}.index $frameNum
1399
1400        set molecule ${frame}.structure.components.molecule
1401        $xmlobj put ${molecule}.lammps $frameContents
1402        $xmlobj put ${molecule}.lammpstypemap $typemap
1403    }
1404}
1405
1406# ----------------------------------------------------------------------
1407# USAGE: _trajToSequence <xmlobj> ?<path>?
1408#
1409#       Check for PDB and LAMMPS trajectories in molecule data and rewrite
1410#       the individual models as a sequence of molecules.  Used internally
1411#       to detect any molecule output elements that contain trajectory data.
1412#       Trajectories will be converted into sequences of individual molecules.
1413#       All other elements will be unaffected. Scans the entire xml tree if a
1414#       starting path is not specified.
1415#
1416# ----------------------------------------------------------------------
1417itcl::body Rappture::Analyzer::_trajToSequence {xmlobj {path ""}} {
1418    # Remove leading dot from path, if present.
1419    if { [string index $path 0] == "." } {
1420        set path [string range $path 1 end]
1421    }
1422    # Otherwise check each child.
1423    foreach child [$xmlobj children $path] {
1424        set current ${path}.${child}
1425        if { [string match "structure*" $child] } {
1426            set isTraj [$xmlobj get ${current}.components.molecule.trajectory]
1427            if { $isTraj == "" || !$isTraj } {
1428                continue;               # Not a trajectory.
1429            }
1430            # Look for trajectory if molecule element found.  Check both pdb
1431            # data and lammps data.
1432            set type [$xmlobj element -as type $current]
1433            set id   [$xmlobj element -as id $current]
1434            set pdbdata    [$xmlobj get ${current}.components.molecule.pdb]
1435            set lammpsdata [$xmlobj get ${current}.components.molecule.lammps]
1436            if { $pdbdata != "" && $lammpsdata != "" } {
1437                puts stderr \
1438                    "found both <pdb> and <lammps> elements: picking pdb"
1439            }
1440            set pdbdata [split $pdbdata \n]
1441            set lammpsdata [split $lammpsdata \n]
1442            if { [_isPdbTrajectory $pdbdata] } {
1443                _pdbToSequence $xmlobj $path $id $current $pdbdata
1444            } elseif { [_isLammpsTrajectory $lammpsdata] } {
1445                _lammpsToSequence $xmlobj $path $id $current $lammpsdata
1446            }
1447            continue
1448        }
1449        if 0 {
1450        # Recurse over all child nodes.
1451        _trajToSequence $xmlobj $current
1452        }
1453    }
1454}
1455
1456# ----------------------------------------------------------------------
1457# CONFIGURATION OPTION: -simcontrol
1458#
1459# Controls whether or not the Simulate button is showing.  In some
1460# cases, it is not needed.
1461# ----------------------------------------------------------------------
1462itcl::configbody Rappture::Analyzer::simcontrol {
1463    _fixSimControl
1464}
1465
1466# ----------------------------------------------------------------------
1467# CONFIGURATION OPTION: -notebookpage
1468#
1469# Controls which page of the analyzer notebook is shown. It is
1470# particularly needed when using rerun, when you don't want to
1471# "simulate -ifneeded" because an actual simulation might be
1472# kicked off due to differences between tool.xml and run.xml
1473# ----------------------------------------------------------------------
1474itcl::configbody Rappture::Analyzer::notebookpage {
1475    _fixNotebook
1476}
Note: See TracBrowser for help on using the repository browser.