SPICE
この記事はまだ始まっちゃいません。
回路を記述子たネットリストを作る
ネットリストを読み込む(source)
解析する(run, op, dc, ac, trac)
表示する(print, plot)
解析法によってはできたりできなんだり。
例1
V1 1 0 DC 10
R1 1 0 1000
DC10ボルトの電源V1をノード1とノード0につなぐ。
ノード0はグランド。
1000$${\Omega}$$の抵抗R1をノード1とノード0につなぐ。
V1 1 0
の
1 0
がノード。
この単純な例に適した解析はop(Operating Point)こと動作点解析。
ngspiceインタプリタが起動した状態で
op
これにより解析が完了するので見たいノードを見る。
print v(1)
すると以下の出力を得る。
v(1) = 1.000000e+01
`v(1) = 1.000000e+01` という表記は、ノード 1 の電圧が 10 ボルトであることを示しています。この表記は科学的記数法で表されており、`1.000000e+01` は `1.000000 × 10^1` を意味します。科学的記数法では、`e` または `E` は 10 のべき乗を表し、その後の数値はべき数を示します。
したがって、`1.000000e+01` は次のように読みます:
`1.000000` は基数です。
`e` は指数(10のべき乗)を意味します。
`01` はべき数(10の1乗)を示します。
これを通常の数値に変換すると `1.000000 × 10^1 = 10.0` となり、これは 10 ボルトを意味します。科学的記数法は特に大きな数値や小さな数値を簡潔に表すためによく使われます。
しかしspiceは単純なオームの法則
print i(R1)
のようなことには向いてない。
つまりspiceむずかしいからまずは簡単な例からとオームの法則をやりだすと逆にむずかしくなる。
v()は引数にノードをとり、素子を取れない。
v(1)は良し。v(R1)は駄目。
i()は引数に素子をとり、ノードを取れない。かつ素子は能動素子であること。
i(1)はそもそも駄目。i(R1)は素子が駄目。
よって強引に電圧0の電圧源を用いて
* Simple DC Circuit
V1 1 0 DC 10
R1 1 2 1000
Vtest 2 0 DC 0
.end
のように記述する。
これはR1に電圧0の電圧源をへばりつけたと考える。
これでもよい。
V1 1 0 DC 10
Vtest 1 2 DC 0
R1 2 0 1000
例2
V1 1 0 DC 10
R1 1 0 1000
R2 1 0 1000
あるいは
V1 1 0 DC 10
R1 0 1 1000
R2 0 1 1000
ただし以下は電源の極性が逆になる。
V1 0 1 DC 10
R1 1 0 1000
R2 1 0 1000
例3分圧
ようやく分圧。
V1 1 0 DC 10
R1 1 2 1000
R2 2 0 1000
R1で電圧降下した、ととれる。
例4.control
.controlで始めて.endcで閉じると
そのブロックで各種命令を実行できるようになる。
つまりインタプリタと逐次やり取りしなくてもコマンドを実行できる。
できないこともある。
例えばprintは失敗する。
* Transient analysis plot example
V1 1 0 DC 10
R1 1 2 1k
C1 2 0 10u
.tran 1m 100m uic
.control
run
plot v(1) v(2)
.endc
.end
uicオプションを付けないとヘタる。
10μFのコンデンサが1kΩの抵抗を介して充電されます。
この回路の時定数(τ = R*C)は τ = 1kΩ × 10μF = 10ms(ミリ秒)です。時定数は、回路が安定状態の約63%に到達するまでの時間を意味します。
理論的にこの回路のノード2(実質コンデンサの両端)は0Vから10Vに充電されるべきと思われます。
パラメータをいじりましょう。
* Transient analysis plot example
V1 1 0 DC 10
R1 1 2 1k
C1 2 0 100u IC=0
.tran 1m 20m uic
.control
run
plot v(1) v(2)
.endc
.end
* Transient analysis plot example
V1 1 0 DC 10
R1 1 2 1k
C1 2 0 100u IC=0
.tran 1m 30m
.control
run
plot v(1) v(2)
.endc
.end
理論的には、以下の指数関数を使用してコンデンサの電圧を時間関数として記述することができます:
$$
V(t) = V_{\text{max}} \left(1 - e^{-\frac{t}{RC}}\right)
$$
ここで、
$${ V(t) }$$ は時間 $${ t }$$ におけるコンデンサの電圧です。
$${ V_{\text{max}} }$$ は電源の電圧(この場合は10V)です。
$${ R }$$ は抵抗の値$${1kΩ}$$です。
$${ C }$$ はコンデンサの値$${100μF}$$です。
$${ e }$$ は自然対数の底です。
この式により、電圧が時間とともにどのように増加するかを計算することができます。5τ(5時定数)後には、電圧は元の値の約99.3%に達します。この場合、5τ = 500ms後にはノード2の電圧はほぼ10Vになるはずです。しかし、あなたが設定した過渡解析の時間は30msなので、この短い時間ではコンデンサが完全に充電される前に解析が終了します。
したがって、もしコンデンサが完全に充電される過程を観察したい場合は、シミュレーションの総時間を少なくとも5時定数、つまり500ms以上に設定するとよいでしょう。
* Transient analysis plot example
V1 1 0 DC 10
R1 1 2 1k
C1 2 0 100u IC=0
.tran 1m 500m uic
.control
run
plot v(2)
.endc
.end
式がわかってるのでGoogle colabで試すこともできる。
ただしおそらくspiceの内部処理はこの式を直接使ったりはしていない。
import numpy as np
import matplotlib.pyplot as plt
# 定数の定義
V_max = 10 # 最大電圧 (単位: ボルト)
R = 1000 # 抵抗 (単位: オーム)
C = 0.0001 # キャパシタンス (単位: ファラド)
# 時間の範囲を設定 (0秒から10秒まで、0.1秒ごと)
t = np.linspace(0, 0.5, 1000)
# 電圧 V(t) の計算
V_t = V_max * (1 - np.exp(-t / (R * C)))
# グラフの描画
plt.plot(t, V_t)
plt.title('RC V(t)')
plt.xlabel('time (s)')
plt.ylabel('voltage (v)')
plt.grid(True)
plt.show()
UbuntuでNgspiceをインストール
sudo apt-get update
sudo apt-get install ngspice
回路ファイル
Ngspiceを使うには、まずSPICE回路記述ファイル(通常は.cir、.sp、または.netという拡張子)を作成する必要があります。このファイルには、回路のコンポーネント、接続、およびシミュレーションを行うための指示が含まれます。
* Simple Resistor Circuit
V1 1 0 DC 10
R1 1 0 1000
.end
シミュレーションを実行するには、ターミナルで次のコマンドを使用します:
ngspice
これでngspiceインタプリタが起動してプロンプトが出ます。
ngspice 1 ->
次にすべきことは、シミュレーションを実行するために、ネットリストが記述されたSPICE回路記述ファイルを読み込むことです。あなたがすでに回路ファイル(例えばsimple_resistor_circuit.cirという名前のファイル)を作成している場合、以下のコマンドを使用してそのファイルを読み込むことができます。
source simple_resistor_circuit.cir
このコマンドを実行すると、指定した回路ファイルが読み込まれNgspiceでシミュレーションを行う準備が整います。
その後、シミュレーションを実行するためのコマンドを入力します。例えば、トランジエント解析(過渡解析)を行いたい場合は以下のようなコマンドを使用します。
tran 1ms 100ms
これにより、100ミリ秒の間にわたって、1ミリ秒ごとにシミュレーションが行われます。
シミュレーションが完了したら、結果を分析するためのコマンドを使用できます。例えば、特定のノードの電圧波形をプロットするには以下のようにします。
plot v(1)
解析種
ngspiceで利用可能な主要な解析方法を以下に列挙します。これらは、回路シミュレーションにおいて異なる側面や動作特性を調査するために使用されます。
1. **DC解析** (`dc`):
- 直流解析は、回路の直流動作点を決定します。
- 電圧源や電流源をスイープして、回路の応答を調べます。
2. **DCオペレーティングポイント解析** (`op`):
- DCオペレーティングポイント解析は、回路の定常状態の直流動作点を決定します。
- 主にバイアスポイントや定常状態の解析に使用されます。
3. **AC解析** (`ac`):
- 交流解析は、周波数領域での回路の動作を解析します。
- 交流信号の振幅や位相の応答を調べます。
4. **トランジェント解析** (`tran`):
- 時間領域解析(トランジェント解析)は、時間経過に伴う回路の動作をシミュレートします。
- 一時的な応答や動的な挙動を調査します。
5. **パラメータスイープ解析**:
- 特定のパラメータを変更しながら、上記の解析(DC、AC、トランジェント)を実行します。
- 回路特性の変化をパラメータの範囲で調べます。
6. **感度解析**:
- 回路要素の値に対する回路の応答の感度を調べます。
- 回路の動作が特定のパラメータにどれだけ敏感かを分析します。
7. **ノイズ解析**:
- 回路内のノイズ源から生じるノイズの影響を解析します。
- 通常、低周波数でのノイズ特性を調べるために使用されます。
8. **温度解析**:
- 温度が変化するときの回路の動作を調べます。
- 温度による回路特性の変化を分析します。
これらの解析方法は、回路設計や性能評価において重要な役割を果たします。ngspiceでは、これらの解析を適切に設定し、ネットリストファイルにコマンドとして記述することで、様々なシミュレーションが可能です。
Ngspice起動時のhelp allコマンド
->でプロンプトが出てる時に使います。
ngspice 1 -> help all
ac [.ac line args] : Do an ac analysis.
alias [[word] alias] : Define an alias.
alter devspecs : parmname value : Alter device parameters.
altermod devspecs : parmname value : Alter model parameters.
alterparam devspecs : parmname value : Alter .param parameters.
asciiplot plotargs : Produce ascii plots.
aspice file [outfile] : Run a spice job asynchronously.
bug : Report a ngspice bug.
cd [directory] : Change working directory.
circbyline line : Enter a circuit line.
codemodel library library ... : Loads the code model libraries.
compose var parm=val ... : Compose a vector.
cross vecname number [ vector ... ] : Make a vector in a strange way.
dc [.dc line args] : Do a dc analysis.
define [[func (args)] stuff] : Define a user-definable function.
deftype spec name pat ... : Redefine vector and plot types.
delete [all] [break number ...] : Delete breakpoints and traces.
destroy [plotname] ... : Throw away all the data in the plot.
diff plotname plotname [vec ...] : 'diff' two plots.
display : Display vector status.
disto [.disto line args] : Do an distortion analysis.
dump : Print a dump of the current circuit.
echo [stuff ...] : Print stuff.
edisplay node node ... : Print all event nodes.
edit [filename] : Edit a spice deck and then load it in.
eprint node node ... : Print event values.
eprvcd node node ... : Print event values into vcd file.
fft vector ... : Create a frequency domain plot with FFT.
fourier fund_freq vector ... : Do a fourier analysis of some data.
getcwd [directory] : Print current working directory.
gnuplot file plotargs : Send plot to gnuplot.
hardcopy file plotargs : Produce hardcopy plots.
help [subject] ... : Hierarchical documentation browser.
history [-r] [number] : Print command history.
iplot [all] [node ...] : Incrementally plot a node.
jobs : Report on asynchronous spice jobs.
let varname = expr : Assign vector variables.
listing [logical] [physical] [deck] : Print the current circuit.
load file ... : Load in data.
mc_source : Re-source the actual circuit deck for MC simulation.
meas various ... : User defined signal evaluation.
newhelp [command name] ... : help.
noise [.noise line args] : Do a noise analysis.
oldhelp [command name] ... : Print help.
op [.op line args] : Determine the operating point of the circuit.
option [option] [option = value] ... : Set a simulator option.
options [option] [option = value] ... : Set a simulator option.
plot expr ... [vs expr] [xl xlo xhi] [yl ylo yhi] : Plot things.
print [col] expr ... : Print vector values.
psd vector ... : Create a power spetral density plot with FFT.
pss [.pss line args] : Do a periodic state analysis.
pz [.pz line args] : Do a pole / zero analysis.
quit : Quit ngspice.
rehash : Rebuild the unix command database.
remcirc : Remove current citcuit.
removecirc [circuit name] : Remove the current circuit from memory.
remzerovec remove zero length vectors.
reset : Terminate a simulation after a breakpoint (formerly 'end').
reshape vector ... [ shape ] : change the dimensions of a vector.
resume : Continue after a stop.
rspice [input file] : Run a spice job remotely.
run [rawfile] : Run the simulation as specified in the input file.
rusage [resource ...] : Print current resource usage.
save [all] [node ...] : Save a spice output.
sens [.sens line args] : Do a sensitivity analysis.
set [option] [option = value] ... : Set a variable.
setcirc [circuit name] : Change the current circuit.
setcs [option] [option = value] ... : Set a variable, case remains as given.
setplot [plotname] : Change the current working plot.
setscale [vecname] : Change default scale of current working plot.
setseed [seed value] : Reset the random number generator with new seed value.
shell [args] : Fork a shell, or execute the command.
shift [var] [number] : Shift argv or the named list var to the left.
show devices ... : parameters ... : Print out device summary.
showmod models ... : parameters ... : Print out model summary.
snload file : Load a snapshot.
snsave file : Save a snapshot.
source file : Source a ngspice file.
spec start_freq stop_freq step_freq vector ... : Create a frequency domain plot.
state (unimplemented) : Print the state of the circuit.
status : Print the current breakpoints and traces.
step [number] : Iterate number times, or one.
stop [stop args] : Set a breakpoint.
sysinfo Print out system info summary.
tf [.tran line args] : Do a transient analysis.
trace [all] [node ...] : Trace a node.
tran [.tran line args] : Do a transient analysis.
transpose varname ... : Perform matrix transposition on multi-D vectors.
tutorial [subject] ... : Hierarchical documentation browser.
unalias word ... : Undefine an alias.
undefine [func ...] : Undefine a user-definable function.
unlet varname ... : Undefine vectors.
unset varname ... : Unset a variable.
version [number] : Print the version number.
where : Print last non-converging node or device
wrdata file plotargs : Send plot data to file.
write file expr ... : Write data to a file.
wrs2p file : Send s-param data to file.
For further details please see the latest official ngspice manual in PDF format at
http://ngspice.sourceforge.net/docs/ngspice-manual.pdf
or in HTML format at
http://ngspice.sourceforge.net/docs/ngspice-html-manual/manual.html
この記事が気に入ったらサポートをしてみませんか?