source: trunk/gui/scripts/energyLevels.tcl @ 7

Last change on this file since 7 was 7, checked in by mmc, 16 years ago

Created an initial EnergyLevels? viewer and MoleculeViewer?. The
MoleculeViewer? is integrated into the driver, but the EnergyLevels?
is not. The material library file contains some new properties
for the appearance of atoms, and the MoleculeViewer? uses this.
Both energyLevels.tcl and moleculeViewer.tcl still have some
driver code at the bottom of the files, so you can run them as
a single file, by themselves, for testing. This code should be
removed when we get further along.

File size: 5.1 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: EnergyLevels - visualizer for discrete energy levels
3#
4#  This widget is a simple visualizer for a set of quantized energy
5#  levels, as you might find for a molecule or a quantum well.  It
6#  takes the Rappture XML representation for a <table> and extracts
7#  values from the "energy" column, then plots those energies on a
8#  graph.
9# ======================================================================
10#  AUTHOR:  Michael McLennan, Purdue University
11#  Copyright (c) 2004  Purdue Research Foundation, West Lafayette, IN
12# ======================================================================
13package require Itk
14package require BLT
15
16option add *EnergyLevels.width 4i widgetDefault
17option add *EnergyLevels.height 4i widgetDefault
18
19itcl::class Rappture::EnergyLevels {
20    inherit itk::Widget
21
22    constructor {args} { # defined below }
23    destructor { # defined below }
24
25    method load {libobj}
26}
27                                                                               
28itk::usual EnergyLevels {
29}
30
31# ----------------------------------------------------------------------
32# CONSTRUCTOR
33# ----------------------------------------------------------------------
34itcl::body Rappture::EnergyLevels::constructor {args} {
35    itk_option add hull.width hull.height
36    pack propagate $itk_component(hull) no
37
38    itk_component add graph {
39        blt::graph $itk_interior.graph \
40            -highlightthickness 0 -plotpadx 0 -plotpady 0 \
41            -width 3i -height 3i
42    } {
43        keep -background -foreground -cursor -font
44    }
45    pack $itk_component(graph) -expand yes -fill both
46    $itk_component(graph) legend configure -hide yes
47
48    eval itk_initialize $args
49}
50
51# ----------------------------------------------------------------------
52# DESTRUCTOR
53# ----------------------------------------------------------------------
54itcl::body Rappture::EnergyLevels::destructor {} {
55}
56
57# ----------------------------------------------------------------------
58# USAGE: load <libObj>
59#
60# Clients use this to load a list of energy levels from the specified
61# XML libObj, which should be a <table>.  One column in the table
62# should have "labels" and the other "energies".
63# ----------------------------------------------------------------------
64itcl::body Rappture::EnergyLevels::load {libobj} {
65    set clist [$libobj children]
66    set ilabel [lsearch $clist [$libobj element column(labels)]]
67    if {$ilabel < 0} {
68        error "can't find column(labels) in energy table data"
69    }
70    set ienergy [lsearch $clist [$libobj element column(energies)]]
71    if {$ienergy < 0} {
72        error "can't find column(energies) in energy table data"
73    }
74
75    set units [$libobj get column(energies).units]
76    if {$units == ""} {
77        set units "eV"
78    }
79
80    #
81    # Update the graph to show the current set of levels.
82    #
83    set graph $itk_component(graph)
84    eval $graph element delete [$graph element names]
85    eval $graph marker delete [$graph marker names]
86
87    set n 0
88    set emax ""
89    set emin ""
90    set ehomo ""
91    set elumo ""
92    foreach line [split [$libobj get data] "\n"] {
93        set lval [lindex $line $ilabel]
94        set eval [lindex $line $ienergy]
95        if {$lval == "" || $eval == ""} {
96            continue
97        }
98
99        set eval [Rappture::Units::convert $eval \
100            -context $units -to $units -units off]
101
102        if {$lval == "HOMO"} {
103            set ehomo $eval
104        } elseif {$lval == "LUMO"} {
105            set elumo $eval
106        }
107
108        set elem "elem[incr n]"
109        $graph element create $elem \
110            -xdata {0 0.8} -ydata [list $eval $eval] \
111            -color blue -symbol "" -linewidth 2
112
113        $graph marker create text -coords [list 0.8 $eval] \
114            -text $lval -anchor w -background ""
115
116        if {$emax == ""} {
117            set emax $eval
118            set emin $eval
119        } else {
120            if {$eval > $emax} {set emax $eval}
121            if {$eval < $emin} {set emin $eval}
122        }
123    }
124    $graph xaxis configure -min 0 -max 1 -showticks off -linewidth 0
125    $graph yaxis configure -title "Energy ($units)"
126
127    # bump the limits so they are big enough to show labels
128    set fnt $itk_option(-font)
129    set h [expr {0.5*([font metrics $fnt -linespace] + 5)}]
130    set emin [expr {$emin-$h/150.0}]
131    set emax [expr {$emax+$h/150.0}]
132    $graph yaxis configure -min $emin -max $emax
133
134    #
135    # If we found HOMO/LUMO levels, then add the band gap at
136    # that point.
137    #
138    if {$ehomo != "" && $elumo != ""} {
139        $graph marker create line \
140            -coords [list 0.2 $elumo 0.2 $ehomo]
141
142        set egap [expr {$ehomo-$elumo}]
143        set emid [expr {0.5*($ehomo+$elumo)}]
144        $graph marker create text \
145            -coords [list 0.21 $emid] -background "" \
146            -text "Eg = [format %.2g $egap] $units" -anchor w
147    }
148}
149
150package require Rappture
151Rappture::EnergyLevels .e
152pack .e -expand yes -fill both
153
154set lib [Rappture::library {<?xml version="1.0"?>
155<table>
156<column id="labels">Name</column>
157<column id="energies">Energy</column>
158<data>
159  E0    0.1
160  E1    0.2
161  LUMO  0.4
162  HOMO  0.9
163  E4    1.5
164  E5    2.8
165</data>
166</table>}]
167
168.e load $lib
Note: See TracBrowser for help on using the repository browser.