[1892] | 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 |
---|
[3957] | 8 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[1892] | 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 | set p1 [Rappture::Interface::connect "fdfPlot"] |
---|
| 41 | set p2 [Rappture::Interface::connect "fdfPlot2"] |
---|
| 42 | |
---|
| 43 | if {[Rappture::Interface::error] != 0]} { |
---|
| 44 | # there were errors while retrieving input data values |
---|
| 45 | # dump the tracepack |
---|
| 46 | set o [Rappture::Interface::outcome] |
---|
| 47 | puts stderr [$o context] |
---|
| 48 | puts stderr [$o remark] |
---|
| 49 | exit [Rappture::Interface::error] |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | # do science calculations |
---|
| 53 | set nPts 200 |
---|
| 54 | |
---|
| 55 | set kT [expr {8.61734e-5 * $T}] |
---|
| 56 | set Emin [expr {$Ef - 10*$kT}] |
---|
| 57 | set Emax [expr {$Ef + 10*$kT}] |
---|
| 58 | |
---|
| 59 | set dE [expr {(1.0/$nPts)*($Emax-$Emin)}] |
---|
| 60 | |
---|
| 61 | set E $Emin |
---|
| 62 | for {set idx 0} {idx < nPts} {incr idx} { |
---|
| 63 | set E [expr {$E + $dE}] |
---|
| 64 | set f [expr {1.0/(1.0 + exp(($E - $Ef)/$kT))}] |
---|
| 65 | lappend fArr $f |
---|
| 66 | lappend Err $E |
---|
| 67 | set progress [expr {(($E - $Emin)/($Emax - $Emin)*100)}] |
---|
| 68 | Rappture::Utils::progress $progress -mesg "Iterating" |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | # set up the curves for the plot by using the add command |
---|
| 72 | # add <name> <xdata> <ydata> -format <fmt> |
---|
| 73 | # |
---|
| 74 | # to group curves on the same plot, just keep adding curves |
---|
| 75 | # to save space, X array values are compared between curves. |
---|
| 76 | # the the X arrays contain the same values, we only store |
---|
| 77 | # one version in the internal data table, otherwise a new |
---|
| 78 | # column is created for the array. for big arrays this may take |
---|
| 79 | # some time, we should benchmark to see if this can be done |
---|
| 80 | # efficiently. |
---|
| 81 | |
---|
| 82 | $p1 add "fdfCurve1" $fArr $EArr -format "g:o" |
---|
| 83 | |
---|
| 84 | $p2 add "fdfCurve2" $fArr $EArr -format "b-o" |
---|
| 85 | $p2 add "fdfCurve3" $fArr $EArr -format "p--" |
---|
| 86 | |
---|
| 87 | # close the global interface |
---|
| 88 | # signal to the graphical user interface that science |
---|
| 89 | # calculations are complete and to display the data |
---|
| 90 | # as described in the views |
---|
| 91 | Rappture::Interface::close |
---|
| 92 | |
---|
| 93 | exit 0 |
---|