基于LLRFLibsPy库的连续和离散超导腔模型示例

example_sim_cavity_basic.py 代码解读

该代码展示了如何模拟一个射频(RF)腔体的响应。假设腔体的半带宽和失谐是恒定的。这种假设通常适用于常导腔体,但在示例中使用了 TESLA 腔体的参数。实际情况中,超导腔体的失谐和半带宽由于洛伦兹力失谐和微音现象通常是时变的。

代码结构

  1. 导入必要的库和模块
  2. 定义腔体参数
  3. 生成输入信号
  4. 调用连续和离散模型进行模拟
  5. 绘制结果

导入必要的库和模块

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

from set_path import *
from rf_sim import *
from rf_control import *
from rf_calib import *

定义腔体参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pi      = np.pi       # 圆周率
fs = 1e6 # 采样频率,Hz
Ts = 1 / fs # 采样时间,s
N = 2048 # 脉冲中的点数

f0 = 1.3e9 # 射频工作频率,Hz
roQ = 1036 # 腔体的 r/Q 值,Ohm
QL = 3e6 # 负载质量因数
RL = 0.5 * roQ * QL # 负载电阻,Ohm
ig = 0.016 # 射频驱动功率等效电流,A
ib = 0.008 # 平均束流电流,A
t_fill = 510 / 1.3e9 # 腔体填充时间,s
detuning = -100 # 腔体失谐,Hz
half_bw = 650 # 半带宽,Hz

beta = QL / (roQ * RL) # 耦合系数

生成输入信号

1
2
3
4
5
6
7
t = np.arange(0, N) * Ts  # 时间数组
T = t # 时间数组
u = np.ones(N) # 输入信号

# 生成前向和束流电压信号
vf = u * ig * RL * np.exp(1j * 2 * pi * f0 * t)
vb = u * ib * RL * np.exp(1j * 2 * pi * f0 * t)

连续模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
state_rf = np.zeros((2, 1), dtype = np.complex)  # 初始化射频状态
state_bm = np.zeros((2, 1), dtype = np.complex) # 初始化束流状态
vc = np.zeros(N, dtype = np.complex) # 初始化腔体电压数组
vr = np.zeros(N, dtype = np.complex) # 初始化反射电压数组

for i in range(N):
result = sim_ncav_cout(vf_step = vf[i], vb_step = vb[i], state_rf0 = state_rf,
state_bm0 = state_bm, f0 = f0, QL = QL, roQ = roQ,
detuning = detuning, half_bw = half_bw, beta = beta)
vc[i] = result[1]
vr[i] = result[2]
state_rf = result[3]
state_bm = result[4]

离散模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
state_rf = np.zeros((2, 1), dtype = np.complex)
state_bm = np.zeros((2, 1), dtype = np.complex)
state_vc = 0 + 0j
vc2 = np.zeros(N, dtype = np.complex)
vr2 = np.zeros(N, dtype = np.complex)
vc3 = np.zeros(N, dtype = np.complex)
vr3 = np.zeros(N, dtype = np.complex)

for i in range(N):
result = sim_ncav_step_ss(half_bw, detuning, vf_step = vf[i], vb_step = vb[i],
state_rf0 = state_rf, state_bm0 = state_bm)
vc2[i] = result[1]
vr2[i] = result[2]
state_rf = result[3]
state_bm = result[4]

status, vc3[i], vr3[i] = sim_ncav_step_simple(half_bw, detuning, vf[i], vb[i],
state_vc, Ts, beta = beta)
state_vc = vc3[i]

绘制结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
plt.figure()
plt.subplot(2, 1, 1)
plt.plot(T, np.abs(vc), label = 'Cavity probe continous sim')
plt.plot(T, np.abs(vc2), '--', label = 'Cavity probe discrete sim SS')
plt.plot(T, np.abs(vc3), '-.', label = 'Cavity probe discrete sim Euler')
plt.plot(T, np.abs(vr), label = 'Reflected continous sim')
plt.plot(T, np.abs(vr2), '--', label = 'Reflected discrete sim SS')
plt.plot(T, np.abs(vr3), '-.', label = 'Reflected discrete sim Euler')
plt.legend()
plt.grid()
plt.xlabel('Time (s)')
plt.ylabel('Amplitude (V)')
plt.subplot(2, 1, 2)
plt.plot(T, np.angle(vc, deg = True), label = 'Cavity probe continous sim')
plt.plot(T, np.angle(vc2, deg = True), '--', label = 'Cavity probe discrete sim SS')
plt.plot(T, np.angle(vc3, deg = True), '-.', label = 'Cavity probe discrete sim Euler')
plt.plot(T, np.angle(vr, deg = True), label = 'Reflected continous sim')
plt.plot(T, np.angle(vr2, deg = True), '--', label = 'Reflected discrete sim SS')
plt.plot(T, np.angle(vr3, deg = True), '-.', label = 'Reflected discrete sim Euler')
plt.legend()
plt.grid()
plt.xlabel('Time (s)')
plt.ylabel('Phase (deg)')
plt.suptitle('Cavity Response Simulated with Discrete Models')
plt.show(block = False)

# 将前向和反射电压转换为功率
status, for_power, ref_power, C = for_ref_volt2power(roQ, QL, vf, vr)
plt.figure()
plt.plot(T, for_power / 1000)
plt.plot(T, ref_power / 1000)
plt.grid()
plt.xlabel('Time (s)')
plt.ylabel('Power (kW)')
plt.show(block = False)

结论

这个示例代码展示了如何使用连续和离散模型模拟 RF 腔体的响应,并将结果可视化。通过比较连续模拟和两种不同离散模拟方法的结果,可以了解这些模型的性能和差异。

source code:example_sim_cavity_basic.py

graph TD
    A[导入必要的库和模块] --> B[定义腔体参数]
    B --> C[生成输入信号]
    C --> D[连续模拟]
    C --> E[离散模拟]
    D --> F[绘制连续模拟结果]
    E --> F[绘制离散模拟结果]
    F --> G[将电压转换为功率并绘制]
    
    subgraph 连续模拟
        D1[初始化状态变量]
        D2[循环计算腔体和反射电压]
        D --> D1
        D1 --> D2
    end
    
    subgraph 离散模拟
        E1[初始化状态变量]
        E2[循环计算腔体和反射电压(SS方法)]
        E3[循环计算腔体和反射电压(Euler方法)]
        E --> E1
        E1 --> E2
        E1 --> E3
    end