Metadata-Version: 2.4
Name: harmolab
Version: 0.0.1
Summary: HarmoLab — a Python MoM/CMA electromagnetic solver
Author-email: Fahdilou Ouro-Yendou <fahdilou.ouro-yendou@univ-cotedazur.fr>
License-Expression: MIT
Project-URL: Repository, https://github.com/FahdilOuro/harmolab
Keywords: method of moments,MoM,RWG,EFIE,characteristic modes,CMA,antenna,electromagnetics,computational electromagnetics
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.26
Requires-Dist: scipy>=1.11
Requires-Dist: matplotlib>=3.8
Requires-Dist: plotly>=5.0
Requires-Dist: gmsh>=4.8
Requires-Dist: pillow>=10.1
Provides-Extra: export
Requires-Dist: kaleido>=0.2.1; extra == "export"
Provides-Extra: optim
Requires-Dist: scikit-learn>=1.3; extra == "optim"
Requires-Dist: threadpoolctl>=3.1; extra == "optim"
Provides-Extra: gpu
Requires-Dist: cupy-cuda12x>=13.0; extra == "gpu"
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Dynamic: license-file

# HarmoLab

**HarmoLab — a Python MoM/CMA electromagnetic solver.**

HarmoLab computes the currents, input impedance and radiated fields of antennas and scatterers made
of conducting surfaces, with the Method of Moments (MoM), and their characteristic modes (CMA). It
works on triangular surface meshes, with RWG basis functions, in free space.

## Why "HarmoLab"

**HAR**rington + **MO**M + **LAB**. The name is a tribute to Roger F. Harrington, who gave
electromagnetics the method of moments (*Field Computation by Moment Methods*, 1968) and, with
Joseph R. Mautz, the theory of characteristic modes for conducting bodies (*IEEE Trans. Antennas
Propag.*, 1971). Both methods are the core of this solver.

## What it does

Status as in [`feature-status.md`](documentation/source/validation/feature-status.md):
*stable* features are covered by the test suite, *experimental* ones work but are not.

**Method of Moments**
- EFIE with RWG basis functions on arbitrary triangular surface meshes (Gmsh `.msh`), including
  junctions. Singular integrals by Duffy transform and closed-form 1/R integrals. *Stable.*
- Optional GPU assembly of the impedance matrix with CuPy, chosen automatically when a CUDA GPU is
  usable; otherwise NumPy on the CPU. *Stable.*
- Ports: enhanced gap source (Ding et al., 2013), delta gap on a cut, delta gap on one edge; one or
  several ports driven together. *Stable.*
- Radiation (transmitting) and plane-wave scattering (receiving). *Stable.*
- Frequency sweep of the port impedance and reflection coefficient (active values, all ports driven
  together, not the full S-matrix). *Stable.*
- Near and far fields, 3D patterns, polar cuts, RHCP/LHCP, RCS. *Experimental.*
- Lumped R, L, C loads and adaptive mesh refinement. *Experimental.*

**Characteristic Mode Analysis** (`harmolab/cma/`, *stable*)
- Generalised eigenproblem `X·Jₙ = λₙ·R·Jₙ` (with `Z = R + jX`), with a regularised or a rank-truncated
  solver, at one frequency or over a sweep, with mode tracking across frequencies.
- Eigenvalues, modal significance, characteristic angle, modal currents and modal far fields,
  plots and text reports.

**Optimiser** (`harmolab/optim/`, *stable*)
- Exact reduced multiport model for a fixed geometry with changing loads (parasitic elements,
  lumped loads): the MoM system is solved once, then every choice of generators and loads costs a
  small P×P problem.
- Inner loop on the loads (L-BFGS-B), outer Bayesian loop on the geometry, matching networks.
  See [`harmolab/optim/README.md`](harmolab/optim/README.md).

**Graphical interface** (`interface/`, *in progress*)
- A Qt main window that creates and opens projects, imports a Gmsh `.msh` mesh and shows it in a
  3D view, with a light and a dark theme. Simulations cannot be run from it yet.

**Known limitations**: perfect conductors or surface-impedance conductors in free space (no
dielectric, no substrate); the radiation efficiency of lossy conductors is wrong; with several
enhanced-gap ports the radiated power is not yet checked against the input power. Details in
[`feature-status.md`](documentation/source/validation/feature-status.md).

## Installation

HarmoLab needs Python 3.12 or later. It is not on PyPI yet: install it from a clone.

```bash
git clone https://github.com/FahdilOuro/harmolab.git
cd harmolab
conda create --name harmolab python=3.12     # or any Python >= 3.12 environment
conda activate harmolab
python -m pip install -e .
```

The required dependencies (NumPy, SciPy, Matplotlib, Plotly, Gmsh, Pillow) are installed with it.
Optional extras:

| Extra | Installs | Needed for |
|---|---|---|
| `export` | kaleido | saving figures as PDF or PNG |
| `optim` | scikit-learn, threadpoolctl | the Bayesian outer loop of the optimiser |
| `gpu` | cupy-cuda12x | GPU assembly (CUDA 12.x) |
| `test` | pytest | the test suite |

```bash
python -m pip install -e ".[export,optim,test]"
```

The graphical interface is not part of the package. From the clone:

```bash
python -m pip install PySide6 pyvista pyvistaqt
python -m interface.gui
```

## A first simulation

A half-wave strip dipole at 300 MHz: mesh it with Gmsh, feed it at its centre, get its input
impedance. Run it from an empty folder: HarmoLab writes every intermediate result (mesh, impedance
matrix, currents) in a `data/` folder under the working directory.

```python
import gmsh

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)
from harmolab.utils.ports import EnhancedGap
from harmolab.src.radiation_algorithm.radiation_algorithm import radiation_algorithm

path = setup_save_file_paths("strip_dipole")   # file names under ./data/
frequency = 300e6                              # Hz, wavelength 1 m
length, width = 0.47, 0.01                     # m, a strip along y centred at the origin

# Element size lambda/60 and a feed gap of 0.05 lambda (3 elements across it).
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 at 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()

ports = [EnhancedGap("feed", feed_point=(0.0, 0.0, 0.0), axis="y", gap_width=gap_width)]
result = radiation_algorithm(path, frequency, ports, voltages=[1.0], show=False)

feed = result.port("feed")
print(f"Input impedance: {feed.impedance:.2f} Ohm")   # 72.74+4.09j Ohm
```

`radiation_algorithm` assembles the impedance matrix, builds the excitation, solves `Z·I = V` and
returns the currents and, for each port, its voltage, current, impedance and power. With
`show=True` (the default) it also opens a 3D view of the surface current.

More examples, one notebook per antenna or topic (radiation, scattering, characteristic modes,
ports, frequency sweeps, mesh refinement, optimisation), are in
[`harmolab/antenna_simulation/`](harmolab/antenna_simulation/).

## Gallery

Figures from the example notebooks.

4-port circularly polarised CubeSat antenna
([`Nice_Cube_radiation.ipynb`](harmolab/antenna_simulation/Nice_Cube/Nice_Cube_radiation.ipynb)):
surface current and RHCP gain pattern.

<p align="center">
<img src="https://raw.githubusercontent.com/FahdilOuro/harmolab/main/harmolab/antenna_simulation/plot_figures/nice_cube_surface_current.png" width="48%">
<img src="https://raw.githubusercontent.com/FahdilOuro/harmolab/main/harmolab/antenna_simulation/plot_figures/nice_cube_rhcp_gain_3d.png" width="48%">
</p>

Characteristic modes of a 480 × 240 mm rectangular loop
([`loop_antenna.ipynb`](harmolab/antenna_simulation/cma_implementation/loop_antenna.ipynb)):
current and far field of mode 1.

<p align="center">
<img src="https://raw.githubusercontent.com/FahdilOuro/harmolab/main/harmolab/antenna_simulation/plot_figures/cma_loop_mode1_current.png" width="48%">
<img src="https://raw.githubusercontent.com/FahdilOuro/harmolab/main/harmolab/antenna_simulation/plot_figures/cma_loop_mode1_farfield.png" width="48%">
</p>

Some notebooks contain interactive Plotly figures that GitHub does not display; open them with
[nbviewer](https://nbviewer.org/).

## Repository layout

- `harmolab/` — the solver package (`harmolab.rwg`, `harmolab.efield`, `harmolab.cma`,
  `harmolab.optim`, `harmolab.utils`, `harmolab.src`), and the example notebooks in
  `harmolab/antenna_simulation/` (not installed with the package).
- `interface/` — the graphical interface, in progress; see [`interface/README.md`](interface/README.md).
- `test/` — the test suite: `python -m pytest test/ harmolab/utils/quadrature/tests/`.
- `documentation/` — sources of the documentation website (theory, user guide, API), in progress.

## Citing HarmoLab

If you use HarmoLab in your work, please cite it with the metadata in [`CITATION.cff`](CITATION.cff)
(GitHub shows it under "Cite this repository").

## Origins and acknowledgments

HarmoLab started as a Python translation of the MATLAB code of Sergey N. Makarov's book
*Antenna and EM Modeling with MATLAB* (Wiley, 2002), and has been extended well beyond it (exact
port models, singular quadrature, GPU assembly, characteristic modes, optimisation).

Thanks to:

- Sergey N. Makarov, for the foundational work in antenna modelling with MATLAB;
- Nguyen Tran Quang Khai (@Khainguyen1349), whose Python MoM implementation inspired this project.

## License

MIT License, © 2025 Fahdilou Ouro-Yendou. See [`LICENSE`](LICENSE).
