1 | # ---------------------------------------------------------------------- |
---|
2 | # EXAMPLE: Fermi-Dirac function in Tcl. |
---|
3 | # |
---|
4 | # This simple example shows how to use Rappture within a simulator |
---|
5 | # written in Tcl. |
---|
6 | # ====================================================================== |
---|
7 | # AUTHOR: Derrick Kearney, Purdue University |
---|
8 | # Copyright (c) 2005-2009 Purdue Research Foundation |
---|
9 | # |
---|
10 | # See the file "license.terms" for information on usage and |
---|
11 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
12 | # ====================================================================== |
---|
13 | package require Rappture |
---|
14 | |
---|
15 | # initialize the global interface |
---|
16 | Rappture::Interface $argv fermi_io |
---|
17 | |
---|
18 | # check the global interface for errors |
---|
19 | if {[Rappture::Interface::error] != 0} { |
---|
20 | # there were errors while setting up the inteface |
---|
21 | # dump the traceback |
---|
22 | set o [Rappture::Interface::outcome] |
---|
23 | puts stderr [$o context] |
---|
24 | puts stderr [$o remark] |
---|
25 | exit [Rappture::Interface::error] |
---|
26 | } |
---|
27 | |
---|
28 | # connect variables to the interface |
---|
29 | # look in the global interface for an object named |
---|
30 | # "temperature, convert its value to Kelvin, and |
---|
31 | # store the value into the address of T. |
---|
32 | # look in the global interface for an object named |
---|
33 | # "Ef", convert its value to electron Volts and store |
---|
34 | # the value into the address of Ef |
---|
35 | # look in the global interface for an object named |
---|
36 | # factorsTable and set the variable result to |
---|
37 | # point to it. |
---|
38 | set T [Rappture::Interface::connect "temperature" -hints {"units=K"}] |
---|
39 | set Ef [Rappture::Interface::connect "Ef" -hints {"units=eV"}] |
---|
40 | |
---|
41 | set x1 [Rappture::Interface::connect "Fermi-Dirac Factor"] |
---|
42 | set y1 [Rappture::Interface::connect "Energy"] |
---|
43 | set x2 [Rappture::Interface::connect "Fermi-Dirac Factor * 2"] |
---|
44 | set y2 [Rappture::Interface::connect "Energy * 2"] |
---|
45 | |
---|
46 | if {[Rappture::Interface::error] != 0]} { |
---|
47 | # there were errors while retrieving input data values |
---|
48 | # dump the tracepack |
---|
49 | set o [Rappture::Interface::outcome] |
---|
50 | puts stderr [$o context] |
---|
51 | puts stderr [$o remark] |
---|
52 | exit [Rappture::Interface::error] |
---|
53 | } |
---|
54 | |
---|
55 | # do science calculations |
---|
56 | set nPts 200 |
---|
57 | |
---|
58 | set kT [expr {8.61734e-5 * $T}] |
---|
59 | set Emin [expr {$Ef - 10*$kT}] |
---|
60 | set Emax [expr {$Ef + 10*$kT}] |
---|
61 | |
---|
62 | set dE [expr {(1.0/$nPts)*($Emax-$Emin)}] |
---|
63 | |
---|
64 | set E $Emin |
---|
65 | for {set idx 0} {idx < nPts} {incr idx} { |
---|
66 | set E [expr {$E + $dE}] |
---|
67 | set f [expr {1.0/(1.0 + exp(($E - $Ef)/$kT))}] |
---|
68 | lappend fArr $f |
---|
69 | lappend fArr2 [expr {$f*2}] |
---|
70 | lappend EArr $E |
---|
71 | lappend EArr2 [expr {$E*2}] |
---|
72 | set progress [expr {(($E - $Emin)/($Emax - $Emin)*100)}] |
---|
73 | Rappture::Utils::progress $progress -mesg "Iterating" |
---|
74 | } |
---|
75 | |
---|
76 | # store results in the results table |
---|
77 | # add data to the table pointed to by the variable result. |
---|
78 | # put the fArr data in the column named "Fermi-Dirac Factor" |
---|
79 | # put the EArr data in the column named "Energy" |
---|
80 | # |
---|
81 | # Rappture::Table Commands: |
---|
82 | # the store command overwrites data that already exists in |
---|
83 | # the column with the new array of data. |
---|
84 | # the append command will append the new array of data onto |
---|
85 | # any previously existing array of data. |
---|
86 | |
---|
87 | $x1 store $fArr |
---|
88 | $y1 store $fArr2 |
---|
89 | $x2 store $EArr |
---|
90 | $y2 store $EArr2 |
---|
91 | |
---|
92 | Rappture::Interface::close |
---|
93 | |
---|
94 | exit 0 |
---|