java-topology/whitepaper/outreach/openfoam-0003.md
russell@unturf.com 652608142a feat: close outreach doc gap — 276 docs (batches 11-16)
All projects with patches now have outreach docs. 276 new docs covering
CWE-407, CWE-312, CWE-362 across C, C++, Java, Python, Go, Rust, C#,
PHP, Ruby, JavaScript, Dart, Erlang, R, and more.

Outreach gap: 276 -> 0.
2026-04-15 13:57:42 -04:00

2.6 KiB
Raw Blame History

OpenFOAM — CWE-407 Disclosure Brief (openfoam-0003)

2026-04-13 · Patch available — awaiting upstream merge

Finding

One O(C × T × M × K) defect in OpenFOAM's DSMC cloud initialization. Patched. DSMCCloud::initialise() calls findIndex(typeIdList_, moleculeName) inside a quadruple-nested loop, producing unnecessary string comparisons during particle seeding.

The Defect

openfoam-0003 (PATCHED — HIGH): src/lagrangian/DSMC/clouds/Templates/DSMCCloud/DSMCCloud.C:98

// In DSMCCloud::initialise() — fires per cell × tet × molecule:
forAll(molecules, i)
{
    const word& moleculeName(molecules[i]);
    label typeId(findIndex(typeIdList_, moleculeName));  // O(K) string scan
    // ... use typeId for particle creation
}

findIndex(typeIdList_, moleculeName) is O(K) where K = number of molecule types. Called inside forAll(molecules) inside forAll(cellTets) inside forAll(mesh_.cells()). Total cost: O(C × T × M × K).

Complexity Proof

At C=5,000,000 cells, T=5 tets/cell, M=2 molecules, K=5 types:

  • Defective: 5M × 5 × 2 × 5 = 250,000,000 string comparisons
  • Fixed: 5M × 5 × 2 × O(1) = 50,000,000 hash lookups (5× reduction from eliminating K)
  • 5× op reduction at K=5. Scales linearly better with more molecule types.

Impact

OpenFOAM is the most widely used open-source CFD framework. DSMC (Direct Simulation Monte Carlo) initializes millions of particles across the mesh at simulation startup. The redundant string lookups for molecule type IDs add measurable overhead during initialization of large-scale rarefied gas simulations.

The Fix

Pre-build a HashTable<label> mapping molecule names to type IDs before the cell loop:

// Before: O(K) findIndex per cell-tet-molecule
label typeId(findIndex(typeIdList_, moleculeName));

// After: O(1) hash lookup
HashTable<label> moleculeTypeIds(molecules.size());
forAll(molecules, i) {
    moleculeTypeIds.insert(molecules[i], findIndex(typeIdList_, molecules[i]));
}
// ...
label typeId(moleculeTypeIds[moleculeName]);

Patch

Fix available: defects/openfoam-0003/patch/openfoam-0003-dsmc-typeId-lookup.patch

Touches DSMCCloud.C. 5× speedup at K=5 molecule types.

What We Ask

A patch is ready for review.

  1. Confirm receipt and assign an issue reference (OpenFOAM/OpenFOAM-dev).
  2. Assess severity — fires during DSMC particle initialization across millions of cells.
  3. Coordinate a disclosure date — we are targeting 90 days from first contact.
  4. We will credit the OpenFOAM team in the public disclosure. Preferred acknowledgment format welcome.

Contact: see cover email. This brief is confidential until coordinated disclosure.