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 | # open the XML file containing the run parameters |
---|
16 | set lib [Rappture::Library] |
---|
17 | |
---|
18 | $lib loadFile [lindex $argv 0] |
---|
19 | |
---|
20 | if {[$lib error] != 0} { |
---|
21 | # cannot open file or out of memory |
---|
22 | set o [$lib outcome] |
---|
23 | puts stderr [$o context] |
---|
24 | puts stderr [$o remark] |
---|
25 | exit [$lib error] |
---|
26 | } |
---|
27 | |
---|
28 | set T [Rappture::Connect $lib "temperature"] |
---|
29 | set Ef [$lib value "Ef" "units eV"] |
---|
30 | |
---|
31 | if {[$lib error != 0]} { |
---|
32 | # there were errors while retrieving input data values |
---|
33 | # dump the tracepack |
---|
34 | set o [$lib outcome] |
---|
35 | puts stderr [$o context] |
---|
36 | puts stderr [$o remark] |
---|
37 | exit [$lib error] |
---|
38 | } |
---|
39 | |
---|
40 | set nPts 200 |
---|
41 | |
---|
42 | set kT [expr {8.61734e-5 * [$T value "K"]}] |
---|
43 | set Emin [expr {$Ef - 10*$kT}] |
---|
44 | set Emax [expr {$Ef + 10*$kT}] |
---|
45 | |
---|
46 | set dE [expr {(1.0/$nPts)*($Emax-$Emin)}] |
---|
47 | |
---|
48 | set E $Emin |
---|
49 | for {set idx 0} {idx < nPts} {incr idx} { |
---|
50 | set E [expr {$E + $dE}] |
---|
51 | set f [expr {1.0/(1.0 + exp(($E - $Ef)/$kT))}] |
---|
52 | lappend fArr $f |
---|
53 | lappend Err $E |
---|
54 | set progress [expr {(($E - $Emin)/($Emax - $Emin)*100)}] |
---|
55 | Rappture::Utils::progress $progress -mesg "Iterating" |
---|
56 | } |
---|
57 | |
---|
58 | # do it the easy way, |
---|
59 | # create a plot to add to the library |
---|
60 | # plot is registered with lib upon object creation |
---|
61 | # p1->add(nPts,xArr,yArr,format,curveLabel,curveDesc); |
---|
62 | |
---|
63 | set p1 [Rappture::Plot $lib] |
---|
64 | $p1 add $fArr $EArr -name "fdfactor" |
---|
65 | $p1 propstr "label" "Fermi-Dirac Curve" |
---|
66 | $p1 propstr "desc" "Plot of Fermi-Dirac Calculation" |
---|
67 | $p1 propstr "xlabel" "Fermi-Dirac Factor" |
---|
68 | $p1 propstr "ylabel" "Energy" |
---|
69 | $p1 propstr "yunits" "eV" |
---|
70 | |
---|
71 | $lib result |
---|
72 | |
---|
73 | exit 0 |
---|