First simulation#

Draft Every code block below is run, unchanged, by documentation/figure_notebooks/guide_first_simulation.ipynb (solver commit 72fb8c6); the outputs shown are its outputs. Awaiting review.

A half-wave strip dipole at 300 MHz, from the geometry to the input impedance and the radiation pattern, in four steps. Run it in a notebook or a script from an empty folder: the solver writes its files in a data/ folder under the working directory (see Data and files).

1. Imports and file paths#

from pathlib import Path

import gmsh
import numpy as np

from harmolab.utils.file_path import setup_save_file_paths
from harmolab.utils.mesh.gmsh_function import (mesh_and_gap_parameters, setup_performance_config,
                                             generate_and_save_mesh, check_simulation_stability)
from harmolab.utils.ports import EnhancedGap
from harmolab.src.radiation_algorithm.radiation_algorithm import radiation_algorithm
from harmolab.efield.efield2 import radiation_intensity_distribution_over_sphere_surface
from harmolab.efield.efield3 import antenna_directivity_pattern

path = setup_save_file_paths("strip_dipole")

setup_save_file_paths(name) creates the folders of data/ if needed and returns a path object holding the name of every file the simulation will read or write: path.msh for the gmsh mesh, path.mat_impedance for the impedance matrix, path.mat_current for the currents, and so on. Every function of the pipeline takes this one object.

2. Geometry and mesh#

frequency = 300e6                 # Hz, wavelength 1 m
length, width = 0.47, 0.01        # m: a strip along y, centred at the origin

# Element size and gap width from the frequency: gap = 0.05 wavelength,
# elements of wavelength/60, so that 3 elements fit in the gap.
mesh_size, gap_width, wavelength = mesh_and_gap_parameters(frequency, resolution=60, gap_fraction=0.05)

gmsh.initialize()
gmsh.model.add(path.name)
setup_performance_config(n_threads=1)          # one thread: the same mesh every run
strip = gmsh.model.occ.addRectangle(-width / 2, -length / 2, 0, width, length)
gap = gmsh.model.occ.addRectangle(-width / 2, -gap_width / 2, 0, width, gap_width)
gmsh.model.occ.fragment([(2, strip)], [(2, gap)])   # the gap edges become mesh lines
gmsh.model.occ.synchronize()
generate_and_save_mesh(path, mesh_size)
gmsh.finalize()
λ = 1.0000 m  |  mesh_size = λ/60 = 0.0167 m  |  gap = 0.05λ = 0.0500 m  |  N_edges_in_gap ≈ 3.0
[PERFORMANCE] Gmsh configured to utilize 1 threads.
Geometry file saved in data/gmsh_files/strip_dipole.brep successfully
--- Starting Mesh Optimization (Dim: 2) ---
--- Optimization Complete ---
Mesh file saved in data/gmsh_files/strip_dipole.msh successfully

The antenna is a surface: a rectangle of zero thickness in the plane \(z = 0\), drawn with gmsh’s OpenCASCADE kernel. mesh_and_gap_parameters chooses the element size and the width of the feed gap together, so that at least three elements fit in the gap (its rules are explained in Excitations and input impedance).

The second rectangle is the feed gap, glued to the strip by fragment: its two edges become lines of the mesh, so the excitation sits exactly where it is drawn. Drawn as a single rectangle, the gap edges cut through triangles; the solver integrates the gap field exactly over the cut triangles, and the same dipole then gives an input resistance within 0.25 Ω of the aligned one (Choosing the mesh size). With one gmsh thread the mesh is the same at every run; with several it is not (Numerical limits and conditioning).

3. Solve#

ports = [EnhancedGap("feed",
                     feed_point=(0.0, 0.0, 0.0),     # centre of the strip
                     axis="y",                       # the gap field points along the strip
                     gap_width=gap_width)]
result = radiation_algorithm(path, frequency, ports, voltages=[1.0])   # 1 V peak across the gap

feed = result.port("feed")
print(f"Input impedance: {feed.impedance:.2f} Ohm")
print(f"Input power:     {feed.power * 1e3:.3f} mW")
check_simulation_stability(feed.impedance, frequency, mesh_size, gap_width)
Number of points: 89
Number of triangles: 116
Number of edges: 144
[calculate_z_matrice] Using GPU (NVIDIA T600 Laptop GPU, CuPy 14.1.1) for the impedance matrix assembly (144x144 edges).
Total input power P_in = 0.5*Re(V·I*) = 6.852393e-03 W
Port 'feed': P_in = 6.8524e-03 W  |  Q_in = 3.8490e-04 VAR  |  Z_in = 72.7377+4.0857j Ohm
Input impedance: 72.74+4.09j Ohm
Input power:     6.852 mW

[Stability Check]
  Port 0: Z_in = 72.74+4.09j Ω  — ✓ OK
  Gap check: gap/mesh = 3.0 ≥ 3 — ✓ OK
  Mesh check: λ/60 — ✓ in recommended range [λ/10, λ/100]
  Overall: ✓ PASS

A port says where and how the antenna is fed; the voltages say how much. EnhancedGap is the enhanced gap source of Ding et al. (2013) (Excitations and input impedance): a uniform field across a gap of width gap_width centred on feed_point, along axis. The two other port models, and how to choose, are on Ports: feeding an antenna. voltages holds one complex voltage per port, in the order of ports.

radiation_algorithm runs the whole pipeline of Method of Moments and the impedance matrix: it converts the mesh, builds the RWG functions, places the ports on the mesh (a port that finds no edge fails here, before the expensive part), assembles \(Z\) (on the GPU here, because one was found and the mesh has more than 100 edges; without one it runs on the CPU), builds the excitation, solves \(ZI = V\) and computes the input impedance and power of each port. Every stage is saved in data/. It returns a RadiationResult: result.z_matrix, result.voltage (\(V\)), result.current (\(I\)), result.surface_current_density and result.ports, one PortResult per port (voltage, current, impedance, power, reactive_power); result.port("feed") finds one by name.

The result: \(Z_\mathrm{in}\) = 72.7 + 4.1j Ω: a small positive reactance, so this strip resonates a little below 300 MHz. How fine the mesh must be for this number to be trustworthy is the subject of Choosing the mesh size. The line about the GPU depends on the machine. The function also opens an interactive Plotly view of the surface current, reproduced in Figure 1.

A thin strip along y, coloured by the magnitude of the surface current, red at the centre where the feed point is marked, blue at both ends.

Figure 1 Magnitude of the surface current density (A/m) on the strip, as drawn by radiation_algorithm: maximum at the feed, zero at the ends, the half-sine of a half-wave dipole. Drag to rotate, scroll to zoom. Generated by documentation/figure_notebooks/guide_first_simulation.ipynb on solver commit 72fb8c6.#

4. Radiation pattern#

radiation_intensity_distribution_over_sphere_surface(path)   # 3D pattern, "Gain" (a directivity), efficiency
antenna_directivity_pattern(path)                            # two polar cuts
Hollow sphere loaded: 5150 points, 10296 triangles.

[Antenna Results]
  Total Radiated Power : 0.0068561612963706445 W
  Gain : 1.6341 (2.13 dBi)

[Power Budget]
  P_in  (0.5*Re(V·I*)) = 6.852393e-03 W
  P_rad (sphere integ.) = 6.856161e-03 W
  Raw efficiency       = 100.0550 %
  Port 0: Z_in = 72.7377+4.0857j Ω  |  R_in = 72.7377 Ω
  Total Efficiency     = 100.00 %

The first call creates, on its first use, the observation sphere in data/ (a few seconds), computes the radiation intensity on it and draws the 3D pattern (Figure 2). The value it prints as “Gain” is the directivity, 2.13 dBi here (the exact value for an infinitely thin half-wave dipole is 2.15 dBi). The efficiency compares the radiated power, integrated over the sphere, with the input power; it reads slightly above 100 % before being capped, because of the small error of that integration (Radiated fields, directivity and gain explains both points). The second call draws two cuts of the pattern, in the \(yz\) and \(xy\) planes.

A doughnut-shaped radiation pattern whose axis is the y axis, red over most of its surface, with a hole along y where the colour falls to green.

Figure 2 3D radiation pattern drawn by radiation_intensity_distribution_over_sphere_surface, in dBi over 20 dB of dynamic range: the doughnut of a dipole, with its null along the strip (\(y\) axis). Drag to rotate, scroll to zoom. Generated by documentation/figure_notebooks/guide_first_simulation.ipynb on solver commit 72fb8c6.#

Polar plot with two figure-of-eight curves over 40 dB: the yz cut, maximum at 90 and 270 degrees, and the xy cut, maximum at 0 and 180 degrees, both with nulls along the y axis.

Figure 3 The two cuts drawn by antenna_directivity_pattern, in dBi over 40 dB below the maximum (its dynamic_range argument). Solid: the \(yz\) plane, against the angle from \(+y\), so 90° is \(+z\). Dashed: the \(xy\) plane, against \(\phi\). Both have their nulls along the strip (\(\pm y\)). Generated by documentation/figure_notebooks/guide_first_simulation.ipynb on solver commit 72fb8c6. Click to enlarge.#

What was written#

for folder in sorted(Path("data").iterdir()):
    print(folder.as_posix() + "/", [f.name for f in sorted(folder.iterdir())])
data/antennas_current/ ['strip_dipole_current.mat']
data/antennas_gain_power/ ['strip_dipole_gain_power.mat']
data/antennas_impedance/ ['strip_dipole_impedance.mat']
data/antennas_mesh/ ['hollow_sphere.mat', 'strip_dipole.mat']
data/antennas_mesh1/ ['strip_dipole_mesh1.mat']
data/antennas_mesh2/ ['strip_dipole_mesh2.mat']
data/antennas_sweep/ []
data/gmsh_files/ ['hollow_sphere.msh', 'strip_dipole.brep', 'strip_dipole.msh']

(This last block needs from pathlib import Path.) What each file contains, and how to reload it without recomputing, is on Data and files.

Next#

Data and files

What data/ holds, file by file

Data and files
Radiation, in detail

Every parameter of radiation_algorithm

Radiation: feeding an antenna
Frequency sweep

Input impedance and \(S_{11}\) over a band

Frequency sweeps and port parameters