Resistor example 1D - Define constants explanation

From Flooxs
Jump to navigation Jump to search

This page explains this code snippet from the 1D Resistor Example

Define constants

set T 300.0               ;# K - Temperature
set k 1.38066e-23         ;# J/K - Boltzmann's constant
set q 1.619e-19           ;# C - elementary charge
set Vt [expr {$k*$T/$q}]  ;# V - thermal voltage
set ni 1.1e10             ;# /cm^3 - intrinsic concentration of Silicon
set esi [expr 11.8 * 8.85418e-14];# F/cm - permittivity of Silicon
set eps [expr $esi / $q]  ;# divide by q for Poisson's equation
set Emob 350.0            ;# cm^2/V/s - constant electron mobility in Silicon
set Hmob 150.0            ;# cm^2/V/s - constant hole mobility in Silicon
set small 1.0e-10         ;# a small number or "small epsilon," instead of "zero"

the tcl "set" command

The code snippet above uses tcl to set global variables. Use "set" for numbers that are NOT dependent on grid or location (i.e. not dependent on x, y, or z), and that are NOT dependent on solution variables. This is why this section is labeled "Define constants," because quantities such as "q," the elementary charge on on electron 1) is not spatially varying 2) is not dependent on outside variables (such as applied voltage, or DevPsi), and 3) will not change with FLOOXS iterations.

tcl variables are ALL strings

By default, all tcl variables are strings. If you're concerned about spaces or other funny business, enclose your expression in quotes:

set mytclvar "Here is a good example of a tcl string. Also, my favorite constant is $ni"

the tcl "expr" command

Why do math yourself? Use the tcl "expr" command (which stands for mathematical "expression") to do simple calculations. This command tells tcl to parse the strings you've defined with the "set" command as numbers and operators. The brackets "[]" mean "evaluate this now and put the answer in this spot." The braces "{}" are good tcl book keeping and should always surround the expression you want tcl to evaluate. You can read outside sources about tcl to learn more about why you should do this. You can also find a reference online for which built-in math functions are defined in tcl and how to use them (such as exp(s), pow(x,y), abs(x), and so on).

Don't forget the "$" !!!!

If you want to access the contents of your tcl variables, be sure to remember to put a "$" in front of it!! Try this in flooxs:

set ni 1.0e10
puts "ni is $ni"

Advanced Concepts

Tcl variables are distinct from Alagator variables declared using the "solution" or "sel z=" commands. Alagator variables do NOT need the "$". In fact, you can declare "ni" as both a tcl string variable - these are distinct variables in FLOOXS, so be careful how you call them:

set ni 1.0e10
sel z=1.1e10 name=ni

which means that the resulting expressions below will give slightly different values:

ni*exp(DevPsi/$Vt)
$ni*exp(DevPsi/$Vt)

Either way is "allowed" or "okay," just be careful how you have defined all your variables and how you call them.