"""使用串联馈电贴片阵列天线的雷达阵列三天线的 CST 自动化建模与仿真 Demo========================================工作频率: 8 GHz介质基板: RO4350:εr = 3.66,厚度 = 2.2 mm求解器: 时域求解器"""
from py4cst.cst import Interface, Projectfrom py4cst.cst.wrappers import Brick, DiscretePort, Material, Units, Solver, Boundary, MeshSettings, Monitor, Transformfrom py4cst import cst, material_utils, material_libraryimport os
print("=" * 60)print("步骤 1: 初始化 CST 接口并创建项目")print("=" * 60)
ifc = Interface(start_mode=Interface.StartMode.ExistingOrNew)ifc.set_quiet_mode(False)
try: proj = ifc.get_active_project() print("使用当前活动项目")
except RuntimeError: proj = ifc.new_microwave_studio_project() print("创建新的 Microwave Studio 项目")
print(f" 应用名称: {proj.get_application_name()}")print(f" 应用版本: {proj.get_application_version()}")
print("\n" + "=" * 60)print("步骤 2: 设置项目单位 (mm, GHz)")print("=" * 60)
units = Units(proj)units.set_geometry_unit(cst.units.GEOMETRY_MILLIMETER)units.set_frequency_unit(cst.units.FREQUENCY_GIGAHERTZ)print(" 几何单位: mm")print(" 频率单位: GHz")
print("\n" + "=" * 60)print("步骤 3: 设置频率范围")print("=" * 60)f_min = 0 f_max = 10.0
print("\n" + "=" * 60)print("步骤 4: 定义天线参数")print("=" * 60)
freq_ghz = 8 freq_farfield1 = 7.95 freq_farfield2 = freq_ghz freq_farfield3 = 8.05
c = 3e8f = freq_ghz * 1e9
er = 3.66 h = 2.2 copper_t = 0.035
N = 6feedline_w1 = 1 feedline_l1 = 11.9 patch_l1 = 9.2
patch_w1 = 5.0 patch_w2 = 7.2 patch_w3 = 9.0 patch_w4 = patch_w3 patch_w5 = patch_w2 patch_w6 = patch_w1
patch_w_list = [ patch_w1, patch_w2, patch_w3, patch_w4, patch_w5, patch_w6]
cell_distance = feedline_l1 + patch_l1
half_lamda = c / (2 * f) * 1000translate_x_x1 = -half_lamda
gnd_feedline_gap = 0.1 gnd_wadd = 10.0 gnd_lmain = 10 gnd_ladd_end = 3 gnd_w = 2 * gnd_wadd + max(patch_w_list) + 2 * abs(translate_x_x1) array_l = N * cell_distance gnd_l = gnd_lmain + gnd_feedline_gap + array_l + gnd_ladd_end
print(f" 阵元数量 N: {N}")print(f" 阵元间距: {cell_distance:.2f} mm")print(f" 各贴片宽度列表: {patch_w_list}")print(f" 第一组RX平移X = {translate_x_x1} mm,复制2次")
params = proj.get_parameters()params.store('freq', freq_ghz)params.store('er', er)params.store('h', h)params.store('copper_t', copper_t)params.store('N', N)params.store('feedline_w1', feedline_w1)params.store('feedline_l1', feedline_l1)params.store('patch_l1', patch_l1)params.store('patch_w1', patch_w1)params.store('patch_w2', patch_w2)params.store('patch_w3', patch_w3)params.store('patch_w4', patch_w4)params.store('patch_w5', patch_w5)params.store('patch_w6', patch_w6)params.store('cell_distance', cell_distance)params.store('half_lamda', half_lamda)params.store('translate_x_x1', translate_x_x1)params.store('gnd_feedline_gap', gnd_feedline_gap)params.store('gnd_wadd', gnd_wadd)params.store('gnd_lmain', gnd_lmain)params.store('gnd_ladd_end', gnd_ladd_end)params.store('gnd_w', gnd_w)params.store('array_l', array_l)params.store('gnd_l', gnd_l)print(" 参数已存储到 CST 参数系统")
print("\n" + "=" * 60)print("步骤 5: 创建材料")print("=" * 60)
material = Material(proj)
mat_copper = material_library.Material()copper_mtd_path = material_library.get_material_path(proj, "Copper (annealed)")mat_copper.load_from_file(copper_mtd_path)mat_copper.import_to_project(proj)
material_utils.prepare_simple_material(material, rel_permitivity=er, rel_permeability=1.0)material.set_name('Substrate')material.create()print(f" 创建介质材料 'Substrate': εr={er}, μr=1.0")
print("\n" + "=" * 60)print("步骤 6: 创建 3D 几何结构 - 原始TX阵列")print("=" * 60)
brick = Brick(proj)
brick.reset()brick.set_name('substrate')brick.set_component('antenna')brick.set_material('Substrate')brick.set_x_range(-2 * gnd_wadd - 2 * abs(translate_x_x1), 2 * gnd_wadd)brick.set_y_range(-gnd_lmain, gnd_l - gnd_lmain)brick.set_z_range(0, h)brick.create()print(f" 创建介质基板: {gnd_w:.2f} x {gnd_l:.2f} x {h} mm")
brick.reset()brick.set_name('ground')
brick.set_component('antenna')brick.set_material('Copper (annealed)')brick.set_x_range(-2 * gnd_wadd - 2 * abs(translate_x_x1), 2 * gnd_wadd)brick.set_y_range(-gnd_lmain, gnd_l - gnd_lmain)brick.set_z_range(-copper_t, 0)brick.create()print(f" 创建接地板")
brick.reset()brick.set_name('mainboard')brick.set_component('antenna')brick.set_material('Copper (annealed)')brick.set_x_range(-2 * gnd_wadd - 2 * abs(translate_x_x1), 2 * gnd_wadd)brick.set_y_range(-gnd_lmain, 0)brick.set_z_range(-copper_t, h + copper_t)brick.create()print(f" 创建主板区域")
print(f" 开始循环生成 {N} 组馈线与辐射贴片(原始阵列)")src_shape_full_names = []for i in range(1, N + 1): idx = i - 1 y_base = gnd_feedline_gap + idx * cell_distance current_patch_w = patch_w_list[idx]
brick.reset() brick.set_name(f'feedline{i}') brick.set_component('antenna') brick.set_material('Copper (annealed)') brick.set_x_range(-feedline_w1 / 2, feedline_w1 / 2) brick.set_y_range(y_base, y_base + feedline_l1) brick.set_z_range(h, h + copper_t) brick.create() src_shape_full_names.append(f"antenna:feedline{i}") print(f" 创建馈线 {i}")
brick.reset() brick.set_name(f'patch{i}') brick.set_component('antenna') brick.set_material('Copper (annealed)') brick.set_x_range(-current_patch_w / 2, current_patch_w / 2) brick.set_y_range(y_base + feedline_l1, y_base + cell_distance) brick.set_z_range(h, h + copper_t) brick.create() src_shape_full_names.append(f"antenna:patch{i}") print(f" 创建辐射贴片 {i} 宽度={current_patch_w} mm")
print("\n" + "=" * 60)print("步骤 7: 批量创建3组50Ω离散端口,永久保存不丢失")print("=" * 60)
port_x_offset_list = [ 0.0, translate_x_x1, 2 * translate_x_x1 ]
for port_idx, x_off in enumerate(port_x_offset_list): port_num = port_idx + 1 discrete_port = DiscretePort(proj) discrete_port.reset() discrete_port.port_number(port_num) discrete_port.type(DiscretePort.PortType.SPARAMETER) discrete_port.impedance(50) discrete_port.monitor(True) discrete_port.radius(0) discrete_port.set_p1(False, x_off, 0, h + copper_t) discrete_port.set_p2(False, x_off, gnd_feedline_gap, h + copper_t) discrete_port.create() print(f" 端口
{port_num}: 50Ω 离散端口,X偏移={x_off:.3f} mm")