Synopsys Sentaurus TCAD系列教程之-Tcl《2》

Tool command language(Tcl)

2.基础

介绍对使用TCAD sentaurus工具最有用的基本Tcl命令
(本章节中用到的所有示例,请参考上一部门《1》的内容)

2.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

2.2 算术表达式

使用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

因此,在大多数情况下,在进行算术运算时,应该在任何整数上附加一个点(.).

2.3 列表

Tcl一个非常基本的类型是列表。列表由用空格分隔的元素组成。列表的第一个元素与索引0相关联。

  • 使用list函数定义列表:
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 ?
  • 使用llength函数查找列表中的元素数量:
set LENGTH [llength $ABCList]
puts "ABCList contains $LENGTH elements."
#-> ABCList contians 5 elements
  • 使用lindex函数引用列表中的一个或多个元素:
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<
  • 使用lsearch函数查找列表中给定元素的索引:
set cIndex [lsearch $ABCList "c"]
puts "The letter c has the index $cIndex"
#-> The leeter c has the index 2
  • 使用lappend函数将元素附加到列表:
set NewElement f
lappend ABCList $NewElement
  • 使用lsort 函数对列表进行排序:
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标志有:

    • -ascii (default)
    • -integer
    • -real
    • -increasing
    • -decreasing

  • 用lsort函数对列表进行排序。使用-index选项选择用于排序的条目:
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}

2.4 循环

本节介绍可以使用Tcl执行的不同循环操作

2.4.1 foreach循环

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循环的所有列表应具有相同数量的元素。否则它们将填充空元素{}

2.4.2 for循环

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 "}
2.4.3 while循环

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不在为正时,该过程停止。

2.4.4 break和continue关键字

使用以下关键字可以影响循环流:
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	
2.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删除该数组。

2.6条件分支

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条件语句是可选的。
主要比较器有:

  • EQUAL: ==
  • NOT EQUAL: !=
  • GREATER THAN: >
  • GREATER THAN OR EQUAL TO: >=
  • LESS THAN: <
  • LESS THAN OR EQUAL TO: <=

switch语句比If语句更紧凑,但仅限于相等测试:

set MODEL Fermi
switch $MODEL {
	Constant {puts "Use contant diffusion model"}
	Fermi {puts "Use Fermi diffusion model"}
}
#-> Use Fermi diffusion model
2.7 逻辑运算符

使用逻辑运算符生成更复杂的条件:

  • AND: &&
  • OR: ||
  • NOT: !
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预处理块的开始。要避免这种情况,请在感叹号和左括号之间插入空格: ! ().

2.8 作业

以下Tcl列表包含频率点矢量和相应的电流增益(h21).在此任务中:

  • 将增益转换成dB,即创建一个新列表,其中增益值包含20*log10(h21).
  • 通过扫描第一个增益比低频值低3dB的点的数据,找到3dB点。

使用以下数据:

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文件查找解决方案。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值