Skip to content
Development documentation — This site follows the main branch and may differ from an installed release. View the changelog.

Treecode

Treecode groups sources in an octree and evaluates a distant group as one monopole. Near nodes and nodes with cancelling positive and negative charge are expanded to a Direct sum, so the fraction of accepted far nodes determines both accuracy and speed. It targets fewer source evaluations than Direct for medium free-boundary problems.

PropertyDescription
Element kerneltriangle P0 (fixed)
Field boundaryfree only
Far approximationNode total charge at its center of charge
Near evaluationDirect leaf sum of the analytic triangle-P0 panel kernel
PotentialUses the same tree traversal and near/far classification as the field
[sim]
field_solver = "treecode"
field_bc_mode = "free"

At initialization, triangle centroids are used as source positions and an octree is built as follows:

  1. Find the axis-aligned bounding box of the source centroids in a node.
  2. Split at the box center into eight octants.
  3. Recurse until a node contains at most tree_leaf_max sources.

If the centroids are numerically coincident, or splitting leaves every source in one octant, the node remains a leaf even when it exceeds the nominal limit. Tree topology is reused while the mesh geometry remains fixed.

Partitioning uses centroids, but expands each MAC node radius to contain every triangle vertex in the node. This prevents a panel that reaches near a target from being misclassified as a far monopole merely because its centroid is distant.

Refresh node charge moments for each batch

Section titled “Refresh node charge moments for each batch”

Changing surface charge does not rebuild the tree. Instead, node charge moments are accumulated from leaves to the root. For node nn, BEACH updates

Qn=inqi,An=inqi,Q_n=\sum_{i\in n}q_i, \qquad A_n=\sum_{i\in n}|q_i|, cQ,n={Qn1inqici,Qn>tiny,cn,otherwise,\mathbf{c}_{Q,n}= \begin{cases} Q_n^{-1}\sum_{i\in n}q_i\mathbf{c}_i, & |Q_n|>\mathrm{tiny},\\ \mathbf{c}_{n}, & \text{otherwise}, \end{cases}

where cn\mathbf{c}_n is the geometric node center. This refresh forms the field snapshot at the start of a batch. Element charge committed at the end of a batch is used by the next refresh.

All sources in a leaf are evaluated directly. For an internal node, the node radius RR, distance dd from its center to the target, and tree_theta define the far candidate condition

R<θ(dR).R < \theta(d-R).

A target inside the node’s bounding sphere always causes traversal into the children. A smaller θ\theta opens more nodes and is slower and more accurate; a larger θ\theta groups more interactions and is faster and coarser.

An accepted far node is evaluated as a monopole QnQ_n at cQ,n\mathbf{c}_{Q,n} for both field and potential. Leaves use analytic triangle-P0 panel sums, preserving near fields, on-surface jumps, and principal-value self potentials. Treecode traverses the source tree for every target; unlike FMM, it does not construct target-side local expansions.

In a node containing positive and negative charge, cancellation can make QnQ_n small and move cQ,n\mathbf{c}_{Q,n} far outside the node. A single-monopole approximation is then unstable. In addition to the geometric condition, BEACH accepts a node only if

AnQn64ϵmachmax(An,Qn).|A_n-|Q_n|| \le 64\,\epsilon_{\mathrm{mach}}\max(A_n,|Q_n|).

This tolerance permits only accumulation roundoff for same-sign charge. An effectively mixed-sign node is opened even when it is geometrically distant, eventually reaching leaf Direct sums.

The guard preserves accuracy under cancellation but can reduce acceleration when signs are mixed at fine scales. For such a distribution, measure runtime as well as error against Direct, and compare FMM when appropriate.

KeyRoleConstraint
tree_thetaGeometric acceptance of a far node0<θ10 < \theta \le 1
tree_leaf_maxNominal number of sources in a leafat least 1
tree_min_nelemSwitching threshold for field_solver="auto"at least 1

When tree_theta and tree_leaf_max are not explicitly present in the input, element-count values are selected even for an explicit treecode mode.

nelemtree_thetatree_leaf_max
< 15000.4012
150099990.5016
10000499990.5820
>= 500000.6524

These are starting values for speed and accuracy, not case-specific error bounds. If only one of the two keys is explicitly set, that value overrides the table while the other still comes from the table.

Accelerate field and potential with the same tree

Section titled “Accelerate field and potential with the same tree”

The Treecode path evaluates electric fields at particle positions, eval_potential at arbitrary points, and potential_history.csv at all element centers with the same source tree. Mesh potential traverses the tree once per element center and reduces source evaluations relative to the O(N2)O(N^2) Direct sum when enough far nodes are accepted. It preserves the analytic triangle-P0 centroid self potential.

The tree is built during initialization, node moments are refreshed after charge changes, and the tree is traversed for every target. Well-behaved distributions require far fewer interactions per target than Direct, but the exact cost depends on tree imbalance, θ\theta, leaf size, and the fraction of mixed-sign nodes.

Measure approximation error against Direct

Section titled “Measure approximation error against Direct”

Compare against Direct with the same normalization and mesh.

  1. Sample strong-field regions, near-surface points, far points, and charge-cancellation regions.
  2. Reduce tree_theta and verify convergence toward Direct.
  3. Vary tree_leaf_max and measure both result and runtime sensitivity.
  4. Compare particle impact elements and post-batch q_elem, not only fields at isolated points.
  5. Measure performance in a release build with representative particle and step counts.

Regression tests separately verify that same-sign nodes retain the field and potential monopole paths, strongly cancelling mixed-sign nodes preserve Direct accuracy, triangle-P0 near and self terms match analytic panel sums, and length normalization returns consistent SI values.