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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
| """ ============================================================= Antenna Shape Optimization: Maximizing Directivity & Radiation Efficiency with Differential Evolution ============================================================= """
import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from mpl_toolkits.mplot3d import Axes3D from scipy.optimize import differential_evolution from scipy.integrate import trapezoid import warnings, time warnings.filterwarnings('ignore')
C = 3e8 FREQ = 300e6 LAM = C / FREQ K = 2 * np.pi / LAM N_ELEM = 5
N_THETA = 181 N_PHI = 361 THETA = np.linspace(1e-4, np.pi - 1e-4, N_THETA) PHI = np.linspace(0, 2 * np.pi, N_PHI)
def element_pattern(length_lam, theta): hl = length_lam * np.pi return (np.cos(hl * np.cos(theta)) - np.cos(hl)) / (np.sin(theta) + 1e-14)
def far_field_1D(params, theta): d_dir, l_ref, l_drv, l_dir = params d_ref = 0.25 z = np.array([-d_ref, 0.0] + [i * d_dir for i in range(1, N_ELEM - 1)]) lens = np.array([l_ref, l_drv] + [l_dir] * (N_ELEM - 2)) I = np.ones(N_ELEM, dtype=complex) I[0] = 0.95 * np.exp( 1j * np.pi * 0.15) for n in range(2, N_ELEM): I[n] = (0.92 ** n) * np.exp(-1j * np.pi * 0.12 * n) E = np.zeros_like(theta, dtype=complex) for n in range(N_ELEM): E += I[n] * element_pattern(lens[n], theta) * np.exp(1j * K * LAM * z[n] * np.cos(theta)) return np.abs(E) ** 2
def radiated_power(params): P1D = far_field_1D(params, THETA) return max(2 * np.pi * trapezoid(P1D * np.sin(THETA), THETA), 1e-14)
def directivity_dB(params): P1D = far_field_1D(params, THETA) return 10 * np.log10(4 * np.pi * P1D.max() / radiated_power(params) + 1e-14)
def front_to_back_ratio(params): P1D = far_field_1D(params, THETA) return 10 * np.log10( P1D[np.argmin(np.abs(THETA - 0.05))] / (P1D[np.argmin(np.abs(THETA - np.pi + 0.05))] + 1e-14) + 1e-14 )
def objective(params): return -directivity_dB(params) + 0.5 * max(0, 15 - front_to_back_ratio(params))
BOUNDS = [(0.15, 0.45), (0.45, 0.55), (0.44, 0.50), (0.38, 0.47)] X_BASELINE = np.array([0.310, 0.490, 0.470, 0.430])
print("=" * 55) print(" Yagi-Uda Antenna Optimization | 300 MHz | N=5") print("=" * 55) print(f"\n[Baseline] D = {directivity_dB(X_BASELINE):.2f} dBi | FBR = {front_to_back_ratio(X_BASELINE):.2f} dB") print("\nRunning Differential Evolution …", flush=True)
t0 = time.time() result = differential_evolution( objective, bounds=BOUNDS, strategy='best1bin', maxiter=300, popsize=18, tol=1e-6, mutation=(0.5, 1.2), recombination=0.85, seed=42, polish=True, updating='deferred', workers=1, ) X_OPT = result.x print(f"Done in {time.time()-t0:.1f}s | Converged: {result.success}") print(f"\n[Optimized] D = {directivity_dB(X_OPT):.2f} dBi | FBR = {front_to_back_ratio(X_OPT):.2f} dB")
plt.rcParams.update({ 'figure.facecolor':'#0d0d0d','axes.facecolor':'#111111', 'axes.edgecolor':'#444','axes.labelcolor':'#ddd', 'text.color':'#ddd','xtick.color':'#aaa','ytick.color':'#aaa', 'grid.color':'#2a2a2a','grid.linestyle':'--', 'font.size':10, }) CYAN='#00e5ff'; ORANGE='#ff7043'; GREEN='#69ff47'; YELLOW='#ffd740'
P_base = far_field_1D(X_BASELINE, THETA) P_opt = far_field_1D(X_OPT, THETA)
fig = plt.figure(figsize=(20, 21), facecolor='#0d0d0d') gs = gridspec.GridSpec(3, 3, figure=fig, hspace=0.45, wspace=0.38, left=0.06, right=0.97, top=0.94, bottom=0.04) fig.suptitle("Yagi-Uda Antenna Shape Optimization — 300 MHz, 5 Elements", fontsize=16, fontweight='bold', color='white', y=0.975)
ax1 = fig.add_subplot(gs[0, 0], projection='polar') ax1.set_facecolor('#111111') ax1.plot(THETA, P_base/P_base.max(), color=ORANGE, lw=1.8, label='Baseline', alpha=0.85) ax1.plot(THETA, P_opt/P_opt.max(), color=CYAN, lw=2.2, label='Optimized') ax1.set_theta_zero_location('N'); ax1.set_theta_direction(-1) ax1.set_rlabel_position(135); ax1.tick_params(colors='#aaa', labelsize=8) ax1.set_title("E-plane (Linear)", color='white', pad=14, fontsize=11) ax1.legend(loc='lower right', fontsize=8, facecolor='#1a1a1a', edgecolor='#444', labelcolor='white')
ax2 = fig.add_subplot(gs[0, 1], projection='polar') ax2.set_facecolor('#111111') def todB(P): return np.clip(10*np.log10(P/(P.max()+1e-14)+1e-14), -30, 0) floor=-30 ax2.plot(THETA, todB(P_base)-floor, color=ORANGE, lw=1.8, label='Baseline', alpha=0.85) ax2.plot(THETA, todB(P_opt) -floor, color=CYAN, lw=2.2, label='Optimized') ax2.set_theta_zero_location('N'); ax2.set_theta_direction(-1) ax2.set_rlabel_position(135) ax2.set_rticks([10, 20, 30]) ax2.set_yticklabels(['-20 dB','-10 dB','0 dB'], fontsize=7, color='#aaa') ax2.tick_params(colors='#aaa', labelsize=8) ax2.set_title("E-plane (dB)", color='white', pad=14, fontsize=11) ax2.legend(loc='lower right', fontsize=8, facecolor='#1a1a1a', edgecolor='#444', labelcolor='white')
ax3 = fig.add_subplot(gs[0, 2]) metrics = ['Directivity\n(dBi)', 'FBR\n(dB)'] bv = [directivity_dB(X_BASELINE), front_to_back_ratio(X_BASELINE)] ov = [directivity_dB(X_OPT), front_to_back_ratio(X_OPT)] xp = np.arange(2); bw = 0.32 b1 = ax3.bar(xp-bw/2, bv, bw, color=ORANGE, alpha=0.85, label='Baseline', edgecolor='white', lw=0.4) b2 = ax3.bar(xp+bw/2, ov, bw, color=CYAN, alpha=0.9, label='Optimized', edgecolor='white', lw=0.4) for bar,v in zip(b1,bv): ax3.text(bar.get_x()+bar.get_width()/2, v+0.15, f'{v:.1f}', ha='center', fontsize=9, color=ORANGE) for bar,v in zip(b2,ov): ax3.text(bar.get_x()+bar.get_width()/2, v+0.15, f'{v:.1f}', ha='center', fontsize=9, color=CYAN) ax3.set_xticks(xp); ax3.set_xticklabels(metrics, fontsize=10) ax3.set_title("Performance Metrics", color='white', fontsize=11) ax3.legend(fontsize=8, facecolor='#1a1a1a', edgecolor='#444', labelcolor='white') ax3.set_facecolor('#111111'); ax3.grid(axis='y', alpha=0.35)
def make3D(P1D): Pn = P1D / P1D.max() TH, PH = np.meshgrid(THETA, PHI, indexing='ij') R = np.outer(Pn, np.ones(N_PHI)) return (R*np.sin(TH)*np.cos(PH), R*np.sin(TH)*np.sin(PH), R*np.cos(TH), R)
def style3d(ax, title): ax.set_facecolor('#0d0d0d') ax.set_title(title, color='white', fontsize=10, pad=2) for lbl,fn in [('X',ax.set_xlabel),('Y',ax.set_ylabel),('Z',ax.set_zlabel)]: fn(lbl, color='#aaa', fontsize=7, labelpad=0) ax.tick_params(colors='#777', labelsize=6, pad=0) for pane in [ax.xaxis.pane, ax.yaxis.pane, ax.zaxis.pane]: pane.fill = False; pane.set_edgecolor('#222') ax.view_init(elev=25, azim=45)
ax4 = fig.add_subplot(gs[1, 0], projection='3d') X4,Y4,Z4,R4 = make3D(P_base) ax4.plot_surface(X4,Y4,Z4, facecolors=plt.cm.plasma(R4), rstride=4, cstride=6, alpha=0.88, linewidth=0, antialiased=True) style3d(ax4, "3D Pattern — Baseline") m4=plt.cm.ScalarMappable(cmap='plasma'); m4.set_array(R4) cb4=fig.colorbar(m4,ax=ax4,shrink=0.5,pad=0.08,aspect=12) cb4.set_label('Norm. Power', color='#aaa', fontsize=7) cb4.ax.yaxis.set_tick_params(color='#aaa', labelsize=6) plt.setp(cb4.ax.yaxis.get_ticklabels(), color='#aaa')
ax5 = fig.add_subplot(gs[1, 1], projection='3d') X5,Y5,Z5,R5 = make3D(P_opt) ax5.plot_surface(X5,Y5,Z5, facecolors=plt.cm.cool(R5), rstride=4, cstride=6, alpha=0.88, linewidth=0, antialiased=True) style3d(ax5, "3D Pattern — Optimized") m5=plt.cm.ScalarMappable(cmap='cool'); m5.set_array(R5) cb5=fig.colorbar(m5,ax=ax5,shrink=0.5,pad=0.08,aspect=12) cb5.set_label('Norm. Power', color='#aaa', fontsize=7) cb5.ax.yaxis.set_tick_params(color='#aaa', labelsize=6) plt.setp(cb5.ax.yaxis.get_ticklabels(), color='#aaa')
ax6 = fig.add_subplot(gs[1, 2], projection='3d') diff = P_opt - P_base diff_n = (diff - diff.min()) / (diff.max() - diff.min() + 1e-14) X6,Y6,Z6,R6 = make3D(np.abs(diff)) DN = np.outer(diff_n, np.ones(N_PHI)) ax6.plot_surface(X6,Y6,Z6, facecolors=plt.cm.RdYlGn(DN), rstride=4, cstride=6, alpha=0.88, linewidth=0, antialiased=True) style3d(ax6, "3D Gain Δ (Opt − Base)") m6=plt.cm.ScalarMappable(cmap='RdYlGn'); m6.set_array(diff_n) cb6=fig.colorbar(m6,ax=ax6,shrink=0.5,pad=0.08,aspect=12) cb6.set_label('Δ (norm)', color='#aaa', fontsize=7) cb6.ax.yaxis.set_tick_params(color='#aaa', labelsize=6) plt.setp(cb6.ax.yaxis.get_ticklabels(), color='#aaa')
ax7 = fig.add_subplot(gs[2, 0:2]) def draw_yagi(params, ax, color, label, yo=0, alpha=1.0): d_dir,l_ref,l_drv,l_dir = params z = np.array([-0.25,0.0]+[i*d_dir for i in range(1,N_ELEM-1)]) lens = np.array([l_ref,l_drv]+[l_dir]*(N_ELEM-2)) names= ['Reflector','Driven','Dir 1','Dir 2','Dir 3'] mks = ['s','D','o','o','o'] for n in range(N_ELEM): ax.plot([z[n],z[n]], [-lens[n]+yo, lens[n]+yo], color=color, lw=2.8, alpha=alpha, solid_capstyle='round') ax.scatter(z[n], yo, color=color, s=50, zorder=5, marker=mks[n], alpha=alpha) if yo >= 0: ax.text(z[n], lens[n]+yo+0.025, names[n], ha='center', va='bottom', fontsize=8, color=color) ax.plot([z[0],z[-1]], [yo,yo], color=color, lw=1, alpha=0.35*alpha, ls='--') ax.text(z[-1]+0.04, yo, label, va='center', fontsize=9, color=color, fontweight='bold')
draw_yagi(X_BASELINE, ax7, ORANGE, 'Baseline', yo= 0.18, alpha=0.85) draw_yagi(X_OPT, ax7, CYAN, 'Optimized', yo=-0.18) ax7.set_xlabel('Position along boom (λ)', color='#ddd', fontsize=10) ax7.set_ylabel('Element half-length (λ)', color='#ddd', fontsize=10) ax7.set_title("Antenna Geometry: Baseline vs Optimized", color='white', fontsize=11) ax7.set_facecolor('#111111'); ax7.grid(True, alpha=0.22) ax7.axhline(0, color='#333', lw=0.5); ax7.set_xlim(-0.42, 1.35)
ax8 = fig.add_subplot(gs[2, 2]) d_sweep = np.linspace(0.15, 0.45, 60) Ds, Fs = [], [] for d in d_sweep: p = X_OPT.copy(); p[0] = d Ds.append(directivity_dB(p)); Fs.append(front_to_back_ratio(p)) ax8.plot(d_sweep, Ds, color=CYAN, lw=2.2, label='Directivity (dBi)') ax8.plot(d_sweep, Fs, color=ORANGE, lw=2.0, label='FBR (dB)', ls='--') ax8.axvline(X_OPT[0], color=GREEN, lw=1.5, ls=':', label=f'Opt d={X_OPT[0]:.3f}λ') ax8.axvline(X_BASELINE[0], color=YELLOW, lw=1.5, ls=':', label=f'Base d={X_BASELINE[0]:.3f}λ') ax8.set_xlabel('Director spacing d_dir (λ)', color='#ddd', fontsize=9) ax8.set_ylabel('dBi / dB', color='#ddd', fontsize=9) ax8.set_title("Sensitivity: Director Spacing", color='white', fontsize=11) ax8.legend(fontsize=7.5, facecolor='#1a1a1a', edgecolor='#444', labelcolor='white') ax8.set_facecolor('#111111'); ax8.grid(True, alpha=0.3)
plt.savefig('antenna_optimization.png', dpi=130, bbox_inches='tight', facecolor='#0d0d0d') plt.show() print("\n✓ Saved: antenna_optimization.png")
print("\n" + "="*55) labels = ["d_dir","l_ref","l_drv","l_dir"] print(f" {'Metric':<22} {'Baseline':>10} {'Optimized':>10} {'Δ':>8}") print(" "+"-"*52) print(f" {'Directivity (dBi)':<22} " f"{directivity_dB(X_BASELINE):>10.3f} " f"{directivity_dB(X_OPT):>10.3f} " f"{directivity_dB(X_OPT)-directivity_dB(X_BASELINE):>+8.3f}") print(f" {'FBR (dB)':<22} " f"{front_to_back_ratio(X_BASELINE):>10.3f} " f"{front_to_back_ratio(X_OPT):>10.3f} " f"{front_to_back_ratio(X_OPT)-front_to_back_ratio(X_BASELINE):>+8.3f}") print(f"\n Optimized parameters:") for lbl,xo in zip(labels,X_OPT): print(f" {lbl:<8} = {xo:.5f} λ = {xo*LAM*100:.2f} cm") print("="*55)
|