$ show tmolteno/necpp
This repository provides **NEC2++**, a from-scratch C++ implementation of the Numerical Electromagnetics Code (NEC-2), a well-known Method of Moments (MoM) solver for wire and patch antenna simulation. The code is distributed as a standalone command-line tool (`nec2++`), a shared library (`libnecpp`) with a stable C API, and Python bindings (separate repository `python-necpp`). It reads standard NEC-2 input files (`.nec`) and produces output files with radiation patterns, impedances, near fields, and structure currents. The library is designed for embedding into optimization systems and GUI applications, supporting large models with tens of thousands of segments. Architecturally, the simulation is driven by a `nec_context` class that orchestrates geometry construction (via `c_geometry`), matrix assembly using Eigen's dense linear algebra, network solving, and result extraction. Input parsing uses an ANTLR 4 grammar to handle the free-format NEC card syntax, with a table-driven handler dispatch replacing a large switch statement. The ground model includes Sommerfeld/Norton integrals, implemented in both C (direct Fortran translation) and C++ wrappers. The codebase is well-tested, with a comprehensive test harness comparing outputs against the original Fortran NEC-2 and a C port (`nec2c`), plus unit tests using Catch2. The repository is actively maintained (last commit in 2026, recent PRs fixing bugs and adding features), uses C++17, and bundles a known version of Eigen (v3.4) for linear algebra, avoiding external BLAS/LAPACK dependencies. While the code has some legacy C sections and occasional commented-out dead code, the overall architecture is clean and extensible. Notable reusable components include the ANTLR grammar for parsing structured free-form input, the wire intersection algorithm in `nec_wire.h`, and the ground parameter abstraction in `nec_ground.h`.
The LLM identified 4 files worth highlighting for reuse:
A complete ANTLR 4 grammar for parsing NEC-2 input card files. It defines lexer and parser rules for all supported card types (GW, SP, EX, FR, RP, etc.) with proper error handling via a custom error listener. This grammar is standalone and can be reused in any project that needs to parse NEC-2 geometry and control cards. It demonstrates non-obvious skill in designing a grammar for a free-format, line-oriented input language that originally was fixed-column FORTRAN.
Defines the wire segment geometry and includes `int_solve()`, a closed-form algorithm that computes the intersection point of two line segments in 3D space, returning sub-segments for breaking wires at intersections. This is a self-contained computational geometry utility that could be extracted for any application requiring 3D line intersection (e.g., CAD, mesh generation). The implementation is numerically robust and handles degenerate cases.
Provides a clean C++ class (`nec_ground`) that encapsulates all ground plane parameters (finite/perfect conductivity, Sommerfeld/Norton model, radial wire screen, cliff edge). It initializes from GN/GD cards and manages the transition between different ground models. The class abstracts away the legacy C code in `ground.c` and `c_ggrid.cpp`, making the ground logic reusable and testable independently of the main NEC solver.
The `GeometryBuildVisitor` class implements the ANTLR visitor pattern to build `c_geometry` objects from parsed NEC cards. It translates grammar tree nodes into wire, patch, arc, helix, and transformation operations. This component demonstrates a clean separation between parsing and geometry construction, and the pattern (visitor over parse tree to populate a mutable geometry) is reusable for any system that needs to convert structured input into a geometric model.