介绍对使用TCAD sentaurus工具最有用的基本Tcl命令
(本章节中用到的所有示例,请参考上一部门《1》的内容)
在Tcl中,变量在使用之前不需要声明,并且它们没有类型。这意味着整数、浮点变量和字符串之间的区别仅在于变量的使用方式:
set i 3
set q 1.6e-19
set W "Hello World"
美元符号($)用于访问变量的值。(put命令写入标准输出、即屏幕)
puts "The value of i is $i"
#-> The value of i 3
puts "The elementary charge is $q C"
#-> The elementary charge is 1.6e-19 C
puts "The string W contains >$W<"
#-> The string W contains >Hello World<
在某些情况下,不清楚变量名称的结尾和字符串的开头。在这种情况下,使用{}指示变量名的结尾:
puts "The ${i}rd value"
#-> The 3rd value
使用Tcl函数expr执行算术表达式:
set j [expr $i+5]
puts "$i + 5 is $j"
#-> 3 +5 is 8
set pi [expr 2.0*asin(1.0)]
puts "pi = $pi"
#-> pi =3.141592
set SIN [expr sin($pi/4.0)]
puts "sin(pi/4.0) is $SIN"
#-> sin(pi/4.0) is 0.70710
与其它编程语言一样,请注意处理数据整数和浮点运算的不同方式:
puts [expr 1/2]
#->0
puts [expr 1/2.0]
#->0.5
因此,在大多数情况下,在进行算术运算时,应该在任何整数上附加一个点(.).
Tcl一个非常基本的类型是列表。列表由用空格分隔的元素组成。列表的第一个元素与索引0相关联。
set ABCList [list a b c d e]
set NUMList [list 1 2 3 4 5 6]
set STRList [list This sentence is a TCL list]
set MIXList [list a 2 3.1415 TCL ?]
set EMPTYList [list]
puts $ABCList
#-> a b c d e
puts $ABCList
#-> 1 2 3 4 5 6
puts $STRList
#-> This sentence is a TCL list
puts $MIXList
#-> a 2 3.1415 TCL ?
set LENGTH [llength $ABCList]
puts "ABCList contains $LENGTH elements."
#-> ABCList contians 5 elements
puts "The first element of ABCList is :[ lindex $ABCList 0]"
#-> The first element of ABCList is : a
puts "The second element of NUMList is : [lindex $NUMList 1'"
#-> The second element of NUMList is : 2
puts "The last element of MIXList is : [lindex $MIXList end]"
#-> The last element of MIXList is ?
puts "The next to last element of ABCList is [lindex $ABCList [expr $LENGTH -2]]"
#-> The next to last element of ABCList is d
set SUBList [lrange $STRList 1 3]
puts "The second to fourth elements of STRList are >$SUBList<"
#-> The second to fourth elements of STRList are >sentence is a<
set cIndex [lsearch $ABCList "c"]
puts "The letter c has the index $cIndex"
#-> The leeter c has the index 2
set NewElement f
lappend ABCList $NewElement
set UnsortedList [list 45.1 78.6 12.6 1.5 89.4 11.6]
set SortedList [lsort -real $UnsortedList]
puts $SortedList
#->1.5 11.6 12.6 45.1 78.6 89.4
最常用的lsort标志有:
…
set UnsortedList [list {a 45.1 1} {g 78.6 5} {r 12.6 8} {c 1.5 2} {q 89.4 3} {n 11.6 4}
set SortedList_0 [lsort -index 0 -ascii $UnsortedList]
puts $SortedList_0
#-> {a 45.1 1} {c 1.5 2} {g 78.6 5} {n 11.6 4} {q 89.4 3} {r 12.6 8}
set SortedList_1 {lsort -index 1 -real $UnsortedList]
puts $SortedList_1
#-> {c 1.5 2} {n 11.6 4} {r 12.6 8} {a 45.1 1} {g 78.6 5} {q 89.4 3}
set SortedList_2 {lsort -index 2 -integer $UnsortedList]
puts $SortedList_2
#-> {a 45.1 1} {c 1.5 2} {q 89.4 3} {n 11.6 4} {g 78.6 5} {r 12.6 8}
本节介绍可以使用Tcl执行的不同循环操作
foreach 循环对列表进行操作:
foreach NUM $NUMList CHAR $ABCList{
puts "The ${NUM} letter of the alphabet is $CHAR"
}
#-> The 1 letter of the alphabet is a
#-> The 2 letter of the alphabet is b
#-> The 3 letter of the alphabet is c
#-> The 4 letter of the alphabet is d
#-> The 5 letter of the alphabet is e
#-> The 6 letter of the alphabet is f
foreach 循环遍历给定的列表,这里是NUMList和ABCList,并执行主体,这里是列表中每个元素的简单输出。
列表的当前元素存储在变量NUM和CHAR中。给foreach循环的所有列表应具有相同数量的元素。否则它们将填充空元素{}
for循环使用在每次循环中递增的计数器。在以下for循环中,变量i取值为0、1、2、…10:
for { set i 0} {$i <= 10} {incr i} {
puts -nonewline " i=$i "
}
#-> i=0 i =1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10
for循环的第一个参数初始化计数器,这里i设置为零。第二个参数定义结束条件,在这里,执行循环直到变量i不在小于或等于10.第三个参数定义传递后计数器的增量,在这里计数器递增1.
for*{ set i 0}*{$i <= 10}*{incr i}*{puts -nonewline " i=$i "}
while循环比for循环更灵活。结束条件可以是任意选择。
set f 1.0
set x 0.0
while { $f >0.0 }{
set x [expr$x + 0.01]
set f [expr 1.0 - $x*$x]
}
puts "Zero crossing is at x=$x"
#-> Zero crossing is at x =1.0
这里使用while循环来查找函数f=1-x2的过零点。在每次传递中,x都会增加,并对函数f求值。当f不在为正时,该过程停止。
使用以下关键字可以影响循环流:
break 立即终止当前循环
continue立即从循环的开始处再次继续
for {set i 0} {$i<=100} { incr i} {
if {[expr fmod($i,2)] == 0}{
continue
} elseif {$i >6 } {
break
}
puts $i
}
#-> 1
#-> 3
#-> 5
数组包含可以使用标识符寻址的元素(从哪里可以看出是数组??model??)
set model(1) Fermi
set model(2) Constant
set model(3) 3Stream
set model(4) 5Stream
for {set i 1} {$i <= 4} { incr i } {
puts "Model #$i is $model($i)"
}
#-> Model #1 is Fermi
#-> Model #2 is Constant
#-> Model #3 is 3Stream
#-> Model #4 is 5Stream
这里,标识符是一个整数索引。然而,在Tcl中,数组标识符不必是整数或数字:
set Identifiers [list first second third fourth]
set model(first) Fermi
set model(second) Constant
set model(third) 3Stream
set model(fourth) 5Stream
foreach i $Identifiers {
puts "The $i model is $model($i)"
}
#-> The first model is Fermi
#-> The second model is Constant
#-> The third model is 3Stream
#-> The fourth model is 5Stream
如果将变量用作数组,则不能再将其用作普通变量,除非使用array unset删除该数组。
if代码块执行条件代码:
set val 1
if {$var == 0} {
puts "val is 0"
} elseif {$val > 2} {
puts "val is larger than 2"
} else {
puts "val is negative or one"
}
#-> val is negative or one
额外,elseif 和else条件语句是可选的。
主要比较器有:
switch语句比If语句更紧凑,但仅限于相等测试:
set MODEL Fermi
switch $MODEL {
Constant {puts "Use contant diffusion model"}
Fermi {puts "Use Fermi diffusion model"}
}
#-> Use Fermi diffusion model
使用逻辑运算符生成更复杂的条件:
set Vd 1.5
set Device "pMOS"
set Simulation "IV"
if { $Vd < 0.1 && Device == "nMOS" && $Simulation != "BV" } {
puts "Simulation nMOS IdVg in linear regime"
} elseif { $Vd > 0.1 && Device == "pMOS" && ! ($Simulation !== "BV") } {
set Vd [expr -1.0*$Vd]
puts "Simulate pMOS IdVg for Vd=$Vd"
} elseif {$Simulation == "BV" || $Device == "BJT"} {
puts "Simulate BJT or MOS breakdonw"
} else {
puts "None of the specified conditions are met..."
}
#-> Simulate pMOS IdVg for Vd=1.5
使用NOT运算符!直接在()之前会混淆sentaurus workbench 预处理器,因为它被误解为Tcl预处理块的开始。要避免这种情况,请在感叹号和左括号之间插入空格: ! ().
以下Tcl列表包含频率点矢量和相应的电流增益(h21).在此任务中:
使用以下数据:
set freqList [list 1.00e5 2.15e5 4.64e5 1.00e6 2.15e6 4.64e6 \
1.00e7 2.15e7 4.64e7 1.00e8 2.15e8 4.64e8 \
1.00e9 2.15e9 4.64e9 1.00e10 2.15e10 4.64e10 \
1.00e11 2.15e11 4.64e11 1e+12 ]
set h21List [list 33.62 33.62 33.62 33.62 33.62 33.62 \
33.62 33.61 33.59 33.46 32.86 30.48 \
23.74 14.12 7.06 3.34 1.57 0.75 \
0.38 0.19 0.20 0.23 ]
点击gtclsh_tcl.cmd文件查找解决方案。