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

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

snapshot of uq work

File size: 55.3 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    puts "Analyzer init"
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"
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    }
423
424    puts "args=$args"
425    lappend args -uq_type [$uq type]
426    lappend args -uq_args [$uq args]
427    puts "args=$args"
428
429    # simulation is needed -- go to simulation page
430    $itk_component(notebook) current simulate
431
432    # no progress messages yet
433    pack forget $itk_component(progress)
434    lappend args -output [itcl::code $this _simOutput]
435
436    _simState off
437    $itk_component(runinfo) configure -state normal
438    $itk_component(runinfo) delete 1.0 end
439    $itk_component(runinfo) insert end "Running simulation...\n\n" text
440    $itk_component(runinfo) configure -state disabled
441
442    # if the hold window is set, then put up a busy cursor
443    if {$itk_option(-holdwindow) != ""} {
444        blt::busy hold $itk_option(-holdwindow)
445        raise $itk_component(hull)
446    }
447
448    # execute the job
449    puts "$_tool run $args"
450
451    foreach {status result} [eval $_tool run $args] break
452
453    # if job was aborted, then allow simulation again
454    if {$result == "ABORT"} {
455        _simState on "Aborted"
456    }
457
458    # load results from run.xml into analyzer
459    if {$status == 0 && $result != "ABORT"} {
460        set status [catch {load $result} result]
461    }
462
463    # back to normal
464    if {$itk_option(-holdwindow) != ""} {
465        blt::busy release $itk_option(-holdwindow)
466    }
467    $itk_component(abort) configure -state disabled
468
469    if {$status != 0} {
470        $itk_component(runinfo) configure -state normal
471        # Don't erase program error messages.
472        # $itk_component(runinfo) delete 1.0 end
473        $itk_component(runinfo) insert end "\n\nProblem launching job:\n\n" text
474        _simOutput $result
475        $itk_component(runinfo) configure -state disabled
476        $itk_component(runinfo) see 1.0
477
478        # Try to create a support ticket for this error.
479        # It may be a real problem.
480        if {[Rappture::bugreport::shouldReport for jobs]} {
481            set ::errorInfo "\n\n== RAPPTURE INPUT ==\n[$_tool xml xml]"
482            Rappture::bugreport::register "Problem launching job:\n$result"
483            Rappture::bugreport::attachment [$_tool xml xml]
484            Rappture::bugreport::send
485        }
486    } else {
487        $itk_component(notebook) current analyze
488    }
489
490    # do this last -- after _simOutput above
491    pack forget $itk_component(progress)
492}
493
494
495# ----------------------------------------------------------------------
496# USAGE: reset ?-eventually|-now?
497#
498# Used to reset the analyzer whenever the input to a simulation has
499# changed.  Sets the mode back to "simulate", so the user has to
500# simulate again to see the output.  If the <start> option is set
501# to "auto", the simulation is invoked immediately.
502# ----------------------------------------------------------------------
503itcl::body Rappture::Analyzer::reset {{when -eventually}} {
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    # check to see if simulation is really needed
511    $_tool sync
512    if {![$_resultset contains [$_tool xml object]]
513          || [string equal $_control "manual-resim"]} {
514        # if control mode is "auto", then simulate right away
515        if {[string match auto* $_control]} {
516            # auto control -- don't need button
517            pack forget $itk_interior.simol
518
519            after cancel [itcl::code $this simulate]
520            after idle [itcl::code $this simulate]
521        } else {
522            _simState on "new input parameters"
523        }
524    } else {
525        _simState off
526    }
527}
528
529# ----------------------------------------------------------------------
530# USAGE: load <xmlobj>
531#
532# Loads the data from the given <xmlobj> into the appropriate results
533# sets.  If necessary, new results sets are created to store the data.
534# ----------------------------------------------------------------------
535itcl::body Rappture::Analyzer::load {xmlobj} {
536    # only show the last result? then clear first
537    if {[string trim [$_tool xml get tool.analyzer]] == "last"} {
538        clear
539    }
540    $_resultset add $xmlobj
541
542    # NOTE: Adding will trigger a !change event on the ResultSet
543    # object, which will trigger calls to _fixResultSet to add
544    # the results to display.
545}
546
547# ----------------------------------------------------------------------
548# USAGE: clear ?<xmlobj>?
549#
550# Discards one or more results previously loaded into the analyzer.
551# If an <xmlobj> is specified, then that one result is cleared.
552# Otherwise, all results are cleared.
553# ----------------------------------------------------------------------
554itcl::body Rappture::Analyzer::clear {{xmlobj "all"}} {
555    if {$xmlobj eq "" || $xmlobj eq "all"} {
556        $_resultset clear
557    } else {
558        $_resultset clear $xmlobj
559    }
560
561    # NOTE: Clearing will trigger a !change event on the ResultSet
562    # object, which will trigger calls to _fixResultSet to clean up
563    # the results being displayed.
564}
565
566# ----------------------------------------------------------------------
567# USAGE: download coming
568# USAGE: download controls <downloadCommand>
569# USAGE: download start ?widget?
570# USAGE: download now ?widget?
571#
572# Spools the current result so the user can download it.
573# ----------------------------------------------------------------------
574itcl::body Rappture::Analyzer::download {option args} {
575    set title [$itk_component(viewselector) value]
576    set page [$itk_component(viewselector) translate $title]
577
578    switch -- $option {
579        coming {
580            #
581            # Warn result that a download is coming, in case
582            # it needs to take a screen snap.
583            #
584            if {![regexp {^(|@download|---)$} $page]} {
585                set f [$itk_component(resultpages) page $page]
586                $f.rviewer download coming
587            }
588        }
589        controls {
590            # no controls for this download yet
591            return ""
592        }
593        start {
594            set widget $itk_component(download)
595            if {[llength $args] > 0} {
596                set widget [lindex $args 0]
597                if {[catch {winfo class $widget}]} {
598                    set widget $itk_component(download)
599                }
600            }
601            #
602            # See if this download has any controls.  If so, then
603            # post them now and let the user continue the download
604            # after selecting a file format.
605            #
606            if {$page != ""} {
607                set ext ""
608                set f [$itk_component(resultpages) page $page]
609                set arg [itcl::code $this download now $widget]
610                set popup [$f.rviewer download controls $arg]
611                if {"" != $popup} {
612                    $popup activate $widget below
613                } else {
614                    download now $widget
615                }
616            } else {
617                # this shouldn't happen
618                set file error.html
619                set data "<h1>Not Found</h1>There is no result selected."
620            }
621        }
622        now {
623            set widget $itk_component(download)
624            if {[llength $args] > 0} {
625                set widget [lindex $args 0]
626                if {[catch {winfo class $widget}]} {
627                    set widget $itk_component(download)
628                }
629            }
630            #
631            # Perform the actual download.
632            #
633            if {$page != ""} {
634                set ext ""
635                set f [$itk_component(resultpages) page $page]
636                set item [$itk_component(viewselector) value]
637                set result [$f.rviewer download now $widget $_appName $item]
638                if { $result == "" } {
639                    return;                # User cancelled the download.
640                }
641                foreach {ext data} $result break
642                if {"" == $ext} {
643                    if {"" != $widget} {
644                        Rappture::Tooltip::cue $widget \
645                            "Can't download this result."
646                    }
647                    return
648                }
649                regsub -all {[\ -\/\:-\@\{-\~]} $title {} title
650                set file "$title$ext"
651            } else {
652                # this shouldn't happen
653                set file error.html
654                set data "<h1>Not Found</h1>There is no result selected."
655            }
656
657            Rappture::Logger::log download [$itk_component(viewselector) value]
658            set mesg [Rappture::filexfer::download $data $file]
659            if {[string length $mesg] > 0} {
660                Rappture::Tooltip::cue $widget $mesg
661            }
662        }
663        default {
664            error "bad option \"$option\": should be coming, controls, now, start"
665        }
666    }
667}
668
669# ----------------------------------------------------------------------
670# USAGE: _plot ?<index> <options> <index> <options>...?
671#
672# Used internally to update the plot shown in the current result
673# viewer whenever the resultselector settings have changed.  Causes the
674# desired results to show up on screen.
675# ----------------------------------------------------------------------
676itcl::body Rappture::Analyzer::_plot {args} {
677    set _plotlist $args
678
679    set page [$itk_component(viewselector) value]
680    set page [$itk_component(viewselector) translate $page]
681    if {"" != $page} {
682        set f [$itk_component(resultpages) page $page]
683        $f.rviewer plot clear
684        foreach {index opts} $_plotlist {
685            $f.rviewer plot add $index $opts
686        }
687    }
688}
689
690# ----------------------------------------------------------------------
691# USAGE: _reorder <compList>
692#
693# Used internally to change the order of a series of output components
694# found in the <output> section.  Moves the <log> elements to the end
695# and returns the updated list.
696# ----------------------------------------------------------------------
697itcl::body Rappture::Analyzer::_reorder {comps} {
698    set i 0
699    set max [llength $comps]
700    while {$i < $max} {
701        set c [lindex $comps $i]
702        if {[string match log* $c]} {
703            set comps [lreplace $comps $i $i]
704            lappend comps $c
705            incr max -1
706        } else {
707            incr i
708        }
709    }
710    return $comps
711}
712
713# ----------------------------------------------------------------------
714# USAGE: _autoLabel <xmlobj> <path> <title> <cntVar>
715#
716# Used internally to check for an about.label property at the <path>
717# in <xmlobj>.  If this object doesn't have a label, then one is
718# supplied using the given <title>.  The <cntVar> is an array of
719# counters in the calling scopes for titles that have been used
720# in the past.  This is used to create titles like "Plot #2" the
721# second time it is encountered.
722#
723# The <xmlobj> is updated so that the label is inserted directly in
724# the tree.
725# ----------------------------------------------------------------------
726itcl::body Rappture::Analyzer::_autoLabel {xmlobj path title cntVar} {
727    upvar $cntVar counters
728
729    set group [$xmlobj get $path.about.group]
730    set label [$xmlobj get $path.about.label]
731    if {"" == $label} {
732        # no label -- make one up using the title specified
733        if {![info exists counters($group-$title)]} {
734            set counters($group-$title) 1
735            set label $title
736        } else {
737            set label "$title (#[incr counters($group-$title)])"
738        }
739        $xmlobj put $path.about.label $label
740    } else {
741        # handle the case of two identical labels in <output>
742        if {![info exists counters($group-$label)]} {
743            set counters($group-$label) 1
744        } else {
745            set label "$label (#[incr counters($group-$label)])"
746            $xmlobj put $path.about.label $label
747        }
748    }
749    return $label
750}
751
752# ----------------------------------------------------------------------
753# USAGE: _fixResult
754#
755# Used internally to change the result page being displayed whenever
756# the user selects a page from the results combobox.
757# ----------------------------------------------------------------------
758itcl::body Rappture::Analyzer::_fixResult {} {
759    set name [$itk_component(viewselector) value]
760    set page ""
761    if {"" != $name} {
762        set page [$itk_component(viewselector) translate $name]
763    }
764    if {$page == "@download"} {
765        # put the combobox back to its last value
766        $itk_component(viewselector) component entry configure -state normal
767        $itk_component(viewselector) component entry delete 0 end
768        $itk_component(viewselector) component entry insert end $_lastlabel
769        $itk_component(viewselector) component entry configure -state disabled
770        # perform the actual download
771        download start $itk_component(download)
772    } elseif {$page == "---"} {
773        # put the combobox back to its last value
774        $itk_component(viewselector) component entry configure -state normal
775        $itk_component(viewselector) component entry delete 0 end
776        $itk_component(viewselector) component entry insert end $_lastlabel
777        $itk_component(viewselector) component entry configure -state disabled
778    } elseif {$page != ""} {
779        set _lastlabel $name
780        set win [winfo toplevel $itk_component(hull)]
781        $itk_component(resultpages) current $page
782        set f [$itk_component(resultpages) page $page]
783
784        # We don't want to replot if we're using an existing viewer with the
785        # the same list of objects to plot.  So track the viewer and the list.
786        if { ![info exists _lastPlot($f)] || $_plotlist != $_lastPlot($f) } {
787            set _lastPlot($f) $_plotlist
788            blt::busy hold $win
789            $f.rviewer plot clear
790            eval $f.rviewer plot add $_plotlist
791            blt::busy release [winfo toplevel $itk_component(hull)]
792        }
793        Rappture::Logger::log output $_label2item($name)
794        Rappture::Tooltip::for $itk_component(viewselector) \
795            "@[itcl::code $this _resultTooltip]" -log $_label2item($name)
796    }
797}
798
799# ----------------------------------------------------------------------
800# USAGE: _fixResultSet ?<eventData>...?
801#
802# Used internally to react to changes within the ResultSet.  When a
803# result is added, a new result viewer is created for the object.
804# When all results are cleared, the viewers are deleted.
805# ----------------------------------------------------------------------
806itcl::body Rappture::Analyzer::_fixResultSet {args} {
807    array set eventData $args
808    switch -- $eventData(op) {
809        add {
810            set xmlobj $eventData(what)
811
812            # Detect molecule elements that contain trajectory data
813            # and convert to sequences.
814            _trajToSequence $xmlobj output
815
816            # Go through the analysis and find all result sets.
817            set haveresults 0
818            foreach item [_reorder [$xmlobj children output]] {
819                switch -glob -- $item {
820                    log* {
821                        _autoLabel $xmlobj output.$item "Output Log" counters
822                    }
823                    number* {
824                        _autoLabel $xmlobj output.$item "Number" counters
825                    }
826                    integer* {
827                        _autoLabel $xmlobj output.$item "Integer" counters
828                    }
829                    mesh* {
830                        _autoLabel $xmlobj output.$item "Mesh" counters
831                    }
832                    string* {
833                        _autoLabel $xmlobj output.$item "String" counters
834                    }
835                    histogram* - curve* - field* {
836                        _autoLabel $xmlobj output.$item "Plot" counters
837                    }
838                    drawing* {
839                        _autoLabel $xmlobj output.$item "Drawing" counters
840                    }
841                    structure* {
842                        _autoLabel $xmlobj output.$item "Structure" counters
843                    }
844                    table* {
845                        _autoLabel $xmlobj output.$item "Energy Levels" counters
846                    }
847                    sequence* {
848                        _autoLabel $xmlobj output.$item "Sequence" counters
849                    }
850                }
851                set label [$xmlobj get output.$item.about.group]
852                if {"" == $label} {
853                    set label [$xmlobj get output.$item.about.label]
854                }
855
856                set hidden [$xmlobj get output.$item.hide]
857                set hidden [expr {"" != $hidden && $hidden}]
858
859                if {"" != $label && !$hidden} {
860                    set haveresults 1
861                }
862            }
863
864            # if there are any valid results, add them to the resultset
865            if {$haveresults} {
866                set index [$_resultset get simnum $xmlobj]
867
868                # add each result to a result viewer
869                foreach item [_reorder [$xmlobj children output]] {
870                    set label [$xmlobj get output.$item.about.group]
871                    if {"" == $label} {
872                        set label [$xmlobj get output.$item.about.label]
873                    }
874                    set hidden [$xmlobj get output.$item.hide]
875                    if { $hidden == "" } {
876                        set hidden 0
877                    }
878                    if {"" != $label && !$hidden} {
879                        if {![info exists _label2page($label)]} {
880                            set name "page[incr _pages]"
881                            set page [$itk_component(resultpages) \
882                                insert end $name]
883                            set _label2page($label) $page
884                            set _label2item($label) output.$item
885                            set _label2desc($label) \
886                                [$xmlobj get output.$item.about.description]
887                            Rappture::ResultViewer $page.rviewer
888                            pack $page.rviewer -expand yes -fill both -pady 4
889
890                            set end [$itk_component(viewselector) \
891                                choices index -value ---]
892                            if {$end < 0} {
893                                set end "end"
894                            }
895                            $itk_component(viewselector) choices insert $end \
896                                $name $label
897                        }
898
899                        # add/replace the latest result into this viewer
900                        set page $_label2page($label)
901
902                        if {![info exists reset($page)]} {
903                            $page.rviewer clear $index
904                            set reset($page) 1
905                        }
906                        $page.rviewer add $index $xmlobj output.$item
907                    }
908                }
909            }
910
911            # show the first page by default
912            set max [$itk_component(viewselector) choices size]
913            for {set i 0} {$i < $max} {incr i} {
914                set first [$itk_component(viewselector) choices get -label $i]
915                if {$first != ""} {
916                    set page [$itk_component(viewselector) choices get -value $i]
917                    set char [string index $page 0]
918                    if {$char != "@" && $char != "-"} {
919                        $itk_component(resultpages) current $page
920                        $itk_component(viewselector) value $first
921                        set _lastlabel $first
922                        break
923                    }
924                }
925            }
926        }
927        clear {
928            if {$eventData(what) ne "all"} {
929                # delete this result from all viewers
930                array set params $eventData(what)
931                foreach label [array names _label2page] {
932                    set page $_label2page($label)
933                    $page.rviewer clear $params(simnum)
934                }
935            }
936
937            if {[$_resultset size] == 0} {
938                # reset the size of the controls area
939                set ht [winfo height $itk_component(results)]
940                set cntlht [$itk_component(resultselector) size -controlarea]
941                set frac [expr {double($cntlht)/$ht}]
942                $itk_component(results) fraction end $frac
943
944                foreach label [array names _label2page] {
945                    set page $_label2page($label)
946                    destroy $page.rviewer
947                }
948                $itk_component(resultpages) delete -all
949                set _pages 0
950
951                $itk_component(viewselector) value ""
952                $itk_component(viewselector) choices delete 0 end
953                catch {unset _label2page}
954                catch {unset _label2item}
955                catch {unset _label2desc}
956                set _plotlist ""
957
958                $itk_component(viewselector) choices insert end --- "---"
959                $itk_component(viewselector) choices insert end \
960                    @download [Rappture::filexfer::label download]
961                set _lastlabel ""
962            }
963
964            # fix Simulate button state
965            reset
966        }
967        default {
968            error "don't know how to handle op \"$eventData(op)\""
969        }
970    }
971}
972
973# ----------------------------------------------------------------------
974# USAGE: _fixSize
975#
976# Used internally to change the size of the result set area whenever
977# a new control appears.  Adjusts the size available for the result
978# set up to some maximum.
979# ----------------------------------------------------------------------
980itcl::body Rappture::Analyzer::_fixSize {} {
981    set ht [winfo height $itk_component(results)]
982    if {$ht <= 1} { set ht [winfo reqheight $itk_component(results)] }
983    set cntlht [$itk_component(resultselector) size -controlarea]
984    set frac [expr {double($cntlht)/$ht}]
985
986    if {$frac < 0.4} {
987        $itk_component(results) fraction end $frac
988    }
989    _fixSimControl
990}
991
992# ----------------------------------------------------------------------
993# USAGE: _simState <boolean> ?<message>? ?<settings>?
994#
995# Used internally to change the "Simulation" button on or off.
996# If the <boolean> is on, then any <message> and <settings> are
997# displayed as well.  If the <boolean> is off, then only display
998# the message. The <message> is a note to the user about
999# what will be simulated, and the <settings> are a list of
1000# tool parameter settings of the form {path1 val1 path2 val2 ...}.
1001# When these are in place, the next Simulate operation will use
1002# these settings.  This helps fill in missing data values.
1003# ----------------------------------------------------------------------
1004itcl::body Rappture::Analyzer::_simState {state args} {
1005    if {$state} {
1006        $itk_interior.simol configure \
1007            -background $itk_option(-simcontrolactiveoutline)
1008        configure -simcontrolcolor $itk_option(-simcontrolactivebackground)
1009
1010        $itk_component(abort) configure -state disabled
1011        $itk_component(simulate) configure -state normal \
1012            -command [itcl::code $this simulate]
1013
1014        #
1015        # If there's a special message, then put it up next to the button.
1016        #
1017        set mesg [lindex $args 0]
1018        if {"" != $mesg} {
1019            $itk_component(simstatus) configure -state normal
1020            $itk_component(simstatus) delete 1.0 end
1021            $itk_component(simstatus) insert end $mesg
1022
1023            #
1024            # If there are any settings, then install them in the
1025            # "Simulate" button.  Also, pop them up as a tooltip
1026            # for the message.
1027            #
1028            set settings [lindex $args 1]
1029            if {[llength $settings] > 0} {
1030                $itk_component(simulate) configure \
1031                    -command [eval itcl::code $this simulate $settings]
1032
1033                set details ""
1034                foreach {path val} $settings {
1035                    set str [string trim [$_tool xml get $path.about.label]]
1036                    if {"" == $str} {
1037                        set str [$_tool xml element -as id $path]
1038                    }
1039                    append details "$str = $val\n"
1040                }
1041                set details [string trim $details]
1042
1043                Rappture::Tooltip::for $itk_component(simstatus) $details
1044                $itk_component(simstatus) insert end " "
1045                $itk_component(simstatus) insert end "(details...)" popup
1046            }
1047            $itk_component(simstatus) configure -state disabled
1048        }
1049    } else {
1050        if {"" != $itk_option(-simcontrolbackground)} {
1051            set simcbg $itk_option(-simcontrolbackground)
1052        } else {
1053            set simcbg $itk_option(-background)
1054        }
1055        $itk_interior.simol configure \
1056            -background $itk_option(-simcontroloutline)
1057        configure -simcontrolcolor $simcbg
1058
1059        $itk_component(simulate) configure -state disabled
1060        $itk_component(abort) configure -state normal
1061
1062        $itk_component(simstatus) configure -state normal
1063        $itk_component(simstatus) delete 1.0 end
1064        set mesg [lindex $args 0]
1065        if {"" != $mesg} {
1066            $itk_component(simstatus) insert end $mesg
1067        }
1068        $itk_component(simstatus) configure -state disabled
1069    }
1070}
1071
1072# ----------------------------------------------------------------------
1073# USAGE: _simOutput <message>
1074#
1075# Invoked automatically whenever output comes in while running the
1076# tool.  Extracts any =RAPPTURE-???=> messages from the output and
1077# sends the output to the display.  For example, any
1078# =RAPPTURE-PROGRESS=> message pops up the progress meter and updates
1079# it to show the latest progress message.  This is useful for
1080# long-running tools, to let the user know how much longer the
1081# simulation will take.
1082# ----------------------------------------------------------------------
1083itcl::body Rappture::Analyzer::_simOutput {message} {
1084    #
1085    # Scan through and pick out any =RAPPTURE-PROGRESS=> messages first.
1086    #
1087    while {[regexp -indices \
1088               {=RAPPTURE-PROGRESS=> *([-+]?[0-9]+) +([^\n]*)(\n|$)} $message \
1089                match percent mesg]} {
1090
1091        foreach {i0 i1} $percent break
1092        set percent [string range $message $i0 $i1]
1093
1094        foreach {i0 i1} $mesg break
1095        set mesg [string range $message $i0 $i1]
1096
1097        pack $itk_component(progress) -fill x -padx 10 -pady 10
1098        $itk_component(progress) settings -percent $percent -message $mesg
1099
1100        foreach {i0 i1} $match break
1101        set message [string replace $message $i0 $i1]
1102    }
1103
1104    #
1105    # Now handle SUBMIT-PROGRESS
1106    #
1107    while {[regexp -indices {=SUBMIT-PROGRESS=> aborted=([0-9]+) finished=([0-9]+) failed=([0-9]+) executing=([0-9]+)\
1108        waiting=([0-9]+) setting_up=([0-9]+) setup=([0-9]+) %done=([0-9.]+) timestamp=([0-9.]+)} $message \
1109        match aborted finished failed executing waiting setting_up setup percent ts mesg]} {
1110
1111        set mesg ""
1112        foreach {i0 i1} $percent break
1113        set percent [string range $message $i0 $i1]
1114        foreach {i0 i1} $failed break
1115        set failed [string range $message $i0 $i1]
1116        foreach {i0 i1} $match break
1117        set message [string replace $message $i0 $i1]
1118
1119        if {$failed != 0} {set mesg "$failed jobs failed!"}
1120        if {$percent >= 100} { set mesg "Jobs finished.  Analyzing results..."}
1121
1122        pack $itk_component(progress) -fill x -padx 10 -pady 10
1123        $itk_component(progress) settings -percent $percent -message $mesg
1124    }
1125
1126
1127    #
1128    # Break up the remaining lines according to =RAPPTURE-ERROR=> messages.
1129    # Show errors in a special color.
1130    #
1131    $itk_component(runinfo) configure -state normal
1132
1133    while {[regexp -indices \
1134               {=RAPPTURE-([a-zA-Z]+)=>([^\n]*)(\n|$)} $message \
1135                match type mesg]} {
1136
1137        foreach {i0 i1} $match break
1138        set first [string range $message 0 [expr {$i0-1}]]
1139        if {[string length $first] > 0} {
1140            $itk_component(runinfo) insert end $first
1141            $itk_component(runinfo) insert end \n
1142        }
1143
1144        foreach {t0 t1} $type break
1145        set type [string range $message $t0 $t1]
1146        foreach {m0 m1} $mesg break
1147        set mesg [string range $message $m0 $m1]
1148        if {[string length $mesg] > 0 && $type != "RUN"} {
1149            $itk_component(runinfo) insert end $mesg $type
1150            $itk_component(runinfo) insert end \n $type
1151        }
1152
1153        set message [string range $message [expr {$i1+1}] end]
1154    }
1155
1156    if {[string length $message] > 0} {
1157        $itk_component(runinfo) insert end $message
1158        if {[$itk_component(runinfo) get end-2char] != "\n"} {
1159            $itk_component(runinfo) insert end "\n"
1160        }
1161        $itk_component(runinfo) see end
1162    }
1163    $itk_component(runinfo) configure -state disabled
1164}
1165
1166# ----------------------------------------------------------------------
1167# USAGE: _resultTooltip
1168#
1169# Used internally to build the tooltip string displayed for the
1170# result selector.  If the current page has an associated description,
1171# then it is displayed beneath the result.
1172#
1173# Returns the string for the tooltip.
1174# ----------------------------------------------------------------------
1175itcl::body Rappture::Analyzer::_resultTooltip {} {
1176    set tip ""
1177    set name [$itk_component(viewselector) value]
1178    if {[info exists _label2desc($name)] &&
1179         [string length $_label2desc($name)] > 0} {
1180        append tip "$_label2desc($name)\n\n"
1181    }
1182    if {[array size _label2page] > 1} {
1183        append tip "Use this control to display other output results."
1184    }
1185    return $tip
1186}
1187
1188# ----------------------------------------------------------------------
1189# USAGE: _fixSimControl
1190#
1191# Used internally to add or remove the simulation control at the
1192# top of the analysis area.  This is controlled by the -simcontrol
1193# option.
1194# ----------------------------------------------------------------------
1195itcl::body Rappture::Analyzer::_fixSimControl {} {
1196    switch -- $itk_option(-simcontrol) {
1197        on {
1198            pack $itk_interior.simol -fill x -before $itk_interior.nb
1199        }
1200        off {
1201            pack forget $itk_interior.simol
1202        }
1203        auto {
1204            #
1205            # If we have two or more radiodials, then there is a
1206            # chance of encountering a combination of parameters
1207            # with no data, requiring simulation.
1208            #
1209            if {[$itk_component(resultselector) size -controls] >= 2} {
1210                pack $itk_interior.simol -fill x -before $itk_interior.nb
1211            } else {
1212                pack forget $itk_interior.simol
1213            }
1214        }
1215        default {
1216            error "bad value \"$itk_option(-simcontrol)\": should be on, off, auto"
1217        }
1218    }
1219}
1220
1221# ----------------------------------------------------------------------
1222# USAGE: _fixNotebook
1223#
1224# Used internally to switch the active notebook page
1225# ----------------------------------------------------------------------
1226itcl::body Rappture::Analyzer::_fixNotebook {} {
1227    switch -- $itk_option(-notebookpage) {
1228        about {
1229            $itk_component(notebook) current about
1230        }
1231        simulate {
1232            $itk_component(notebook) current simulate
1233        }
1234        analyze {
1235            $itk_component(notebook) current analyze
1236        }
1237        default {
1238            error "bad value \"$itk_option(-notebookpage)\": should be about, simulate, analyze"
1239        }
1240    }
1241}
1242
1243# ----------------------------------------------------------------------
1244# USAGE: _isPdbTrajectory <data>
1245#
1246# Used internally to determine whether pdb or lammps data represents a
1247# trajectory rather than a single frame
1248# ----------------------------------------------------------------------
1249itcl::body Rappture::Analyzer::_isPdbTrajectory {data} {
1250    if { [llength $data] == 0 } {
1251        return 0
1252    }
1253    set nModels 0
1254    foreach line $data {
1255        if { [string match "MODEL*" $line] }  {
1256            incr nModels
1257            if { $nModels > 1 } {
1258                # Stop if more than one model found.  No need to count them
1259                # all.
1260                return 1
1261            }
1262        }
1263    }
1264    return 0
1265}
1266
1267# ----------------------------------------------------------------------
1268# USAGE: _isLammpsTrajectory <data>
1269#
1270# Used internally to determine whether pdb or lammps data represents a
1271# trajectory rather than a single frame
1272# ----------------------------------------------------------------------
1273itcl::body Rappture::Analyzer::_isLammpsTrajectory { data } {
1274    if { [llength $data] == 0 } {
1275        return 0
1276    }
1277    set nModels 0
1278    foreach line $data {
1279        if { [regexp {^[\t ]*ITEM:[ \t]+TIMESTEP} $line] } {
1280            incr nModels
1281            if { $nModels > 1 } {
1282                # Stop if more than one model found.  No need to count them
1283                # all.
1284                return 1
1285            }
1286        }
1287    }
1288    return 0
1289}
1290
1291# ----------------------------------------------------------------------
1292# USAGE: _pdbToSequence <xmlobj> ?<path>?
1293#
1294# If the molecule element is a trajectory, delete the original
1295# and create a sequence of individual molecules.
1296# Used internally to detect any molecule output elements that contain
1297# trajectory data.  Trajectories will be converted into sequences of
1298# individual molecules.  All other elements will be unaffected. Scans
1299# the entire xml tree if a starting path is not specified.
1300# ----------------------------------------------------------------------
1301itcl::body Rappture::Analyzer::_pdbToSequence {xmlobj path id child data} {
1302
1303    set seqLabel [$xmlobj get ${child}.about.label]
1304    set descr    [$xmlobj get ${child}.about.description]
1305    set formula  [$xmlobj get ${child}.components.molecule.formula]
1306    $xmlobj remove $child
1307
1308    set sequence  $path.sequence($id)
1309    $xmlobj put ${sequence}.about.label $seqLabel
1310    $xmlobj put ${sequence}.about.description $descr
1311    $xmlobj put ${sequence}.index.label "Frame"
1312
1313    set frameNum 0
1314    set numLines [llength $data]
1315    for { set i 0 } { $i < $numLines } { incr i } {
1316        set line [lindex $data $i]
1317        set line [string trim $line]
1318        set contents {}
1319        if { [string match "MODEL*" $line] } {
1320            # Save the contents until we get an ENDMDL record.
1321            for {} { $i < $numLines } { incr i } {
1322                set line [lindex $data $i]
1323                set line [string trim $line]
1324                if { $line == "" } {
1325                    continue;           # Skip blank lines.
1326                }
1327                if { [string match "ENDMDL*" $line] } {
1328                    break;
1329                }
1330                append contents $line\n
1331            }
1332            set frame ${sequence}.element($frameNum)
1333            $xmlobj put ${frame}.index $frameNum
1334
1335            set molecule ${frame}.structure.components.molecule
1336            $xmlobj put ${molecule}.pdb $contents
1337            $xmlobj put ${molecule}.formula $formula
1338            incr frameNum
1339        }
1340    }
1341}
1342
1343# ----------------------------------------------------------------------
1344# USAGE: _lammpsToSequence <xmlobj> ?<path>?
1345#
1346# If the molecule element is a trajectory, delete the original
1347# and create a sequence of individual molecules.
1348# Used internally to detect any molecule output elements that contain
1349# trajectory data.  Trajectories will be converted into sequences of
1350# individual molecules.  All other elements will be unaffected. Scans
1351# the entire xml tree if a starting path is not specified.
1352# ----------------------------------------------------------------------
1353itcl::body Rappture::Analyzer::_lammpsToSequence {xmlobj path id child data} {
1354
1355    set seqLabel [$xmlobj get ${child}.about.label]
1356    set descr    [$xmlobj get ${child}.about.description]
1357    set typemap  [$xmlobj get ${child}.components.molecule.lammpstypemap]
1358    $xmlobj remove $child
1359
1360    set sequence ${path}.sequence($id)
1361    $xmlobj put ${sequence}.about.label $seqLabel
1362    $xmlobj put ${sequence}.about.description $descr
1363    $xmlobj put ${sequence}.index.label "Frame"
1364
1365    set frameNum 0
1366    set frameContents ""
1367    set inModel 0
1368    foreach line $data {
1369        set line [string trim $line]
1370        if { $line == "" } {
1371            continue;                   # Skip blank lines
1372        }
1373        if {[regexp {^[\t ]*ITEM:[ \t]+ATOMS} $line] } {
1374            if { $inModel && $frameContents != "" } {
1375                set frame ${sequence}.element($frameNum)
1376                $xmlobj put ${frame}.index $frameNum
1377
1378                set molecule ${frame}.structure.components.molecule
1379                $xmlobj put ${molecule}.lammps $frameContents
1380                $xmlobj put ${molecule}.lammpstypemap $typemap
1381
1382                incr frameNum
1383                set frameContents ""
1384            }
1385            set inModel 1
1386        } elseif { [scan $line "%d %d %f %f %f" a b c d e] == 5 } {
1387            if { !$inModel } {
1388                puts stderr "found \"$line\" without previous \"ITEM: ATOMS\""
1389                set inModel 1
1390            }
1391            append frameContents $line\n
1392        }
1393    }
1394    if { $frameContents != "" } {
1395        set frame ${sequence}.element($frameNum)
1396        $xmlobj put ${frame}.index $frameNum
1397
1398        set molecule ${frame}.structure.components.molecule
1399        $xmlobj put ${molecule}.lammps $frameContents
1400        $xmlobj put ${molecule}.lammpstypemap $typemap
1401    }
1402}
1403
1404# ----------------------------------------------------------------------
1405# USAGE: _trajToSequence <xmlobj> ?<path>?
1406#
1407#       Check for PDB and LAMMPS trajectories in molecule data and rewrite
1408#       the individual models as a sequence of molecules.  Used internally
1409#       to detect any molecule output elements that contain trajectory data.
1410#       Trajectories will be converted into sequences of individual molecules.
1411#       All other elements will be unaffected. Scans the entire xml tree if a
1412#       starting path is not specified.
1413#
1414# ----------------------------------------------------------------------
1415itcl::body Rappture::Analyzer::_trajToSequence {xmlobj {path ""}} {
1416    # Remove leading dot from path, if present.
1417    if { [string index $path 0] == "." } {
1418        set path [string range $path 1 end]
1419    }
1420    # Otherwise check each child.
1421    foreach child [$xmlobj children $path] {
1422        set current ${path}.${child}
1423        if { [string match "structure*" $child] } {
1424            set isTraj [$xmlobj get ${current}.components.molecule.trajectory]
1425            if { $isTraj == "" || !$isTraj } {
1426                continue;               # Not a trajectory.
1427            }
1428            # Look for trajectory if molecule element found.  Check both pdb
1429            # data and lammps data.
1430            set type [$xmlobj element -as type $current]
1431            set id   [$xmlobj element -as id $current]
1432            set pdbdata    [$xmlobj get ${current}.components.molecule.pdb]
1433            set lammpsdata [$xmlobj get ${current}.components.molecule.lammps]
1434            if { $pdbdata != "" && $lammpsdata != "" } {
1435                puts stderr \
1436                    "found both <pdb> and <lammps> elements: picking pdb"
1437            }
1438            set pdbdata [split $pdbdata \n]
1439            set lammpsdata [split $lammpsdata \n]
1440            if { [_isPdbTrajectory $pdbdata] } {
1441                _pdbToSequence $xmlobj $path $id $current $pdbdata
1442            } elseif { [_isLammpsTrajectory $lammpsdata] } {
1443                _lammpsToSequence $xmlobj $path $id $current $lammpsdata
1444            }
1445            continue
1446        }
1447        if 0 {
1448        # Recurse over all child nodes.
1449        _trajToSequence $xmlobj $current
1450        }
1451    }
1452}
1453
1454# ----------------------------------------------------------------------
1455# CONFIGURATION OPTION: -simcontrol
1456#
1457# Controls whether or not the Simulate button is showing.  In some
1458# cases, it is not needed.
1459# ----------------------------------------------------------------------
1460itcl::configbody Rappture::Analyzer::simcontrol {
1461    _fixSimControl
1462}
1463
1464# ----------------------------------------------------------------------
1465# CONFIGURATION OPTION: -notebookpage
1466#
1467# Controls which page of the analyzer notebook is shown. It is
1468# particularly needed when using rerun, when you don't want to
1469# "simulate -ifneeded" because an actual simulation might be
1470# kicked off due to differences between tool.xml and run.xml
1471# ----------------------------------------------------------------------
1472itcl::configbody Rappture::Analyzer::notebookpage {
1473    _fixNotebook
1474}
Note: See TracBrowser for help on using the repository browser.