polyfoammesh.core.openfoam_runner

OpenFOAM subprocess runner with retry, error classification, and quality reporting.

This module provides:
  • MeshWorker: interruptible subprocess runner for cartesianMesh via WSL2

  • RetryRunner: orchestrates cartesianMesh with up to 3 automatic retries

  • CheckMeshWorker / PolyDualWorker: quality-check and conversion workers

  • QualityFixWorker: auto-fix loop (mesh -> checkMesh -> fix -> re-mesh)

  • analyze_error / parse_checkmesh_output: error and quality parsers

Module Attributes

GMSH_STALL_MAX_TIMEOUT_S

Upper bound of the size-scaled no-output budget (large jobs).

GMSH_STALL_SMALL_CELLS

Estimated-cell bounds over which the budget ramps from base to the max.

GMSH_ROBUST_OPT_ENV_VAR

set alongside POLYFOAMMESH_GMSH_ROBUST on a signature-triggered retry only (the venturi MeshAdapt runaway class), enabling the 2D optimizer inside robust mode.

GMSH_MANUAL_EQUIV_ENV_VAR

the worker sets this on its last-ditch attempt so the wrapper runs the explicit-size path with sizes derived to match the adaptive request's cell target (adaptive sizing OFF).

Functions

analyze_error(full_output)

Classify cartesianMesh errors using cfMesh-specific patterns.

count_polymesh_prism_cells(case_dir)

Number of prism-shaped cells in constant/polyMesh (best-effort).

generate_fms(case_dir[, angle, timeout])

Run surfaceFeatureEdges to produce an .fms file from the surface STL.

parse_checkmesh_output(text)

Parse checkMesh stdout into a structured report.

poly_bl_patch_selection(case_dir[, ...])

Decide which boundary patches the poly BL step may touch.

poly_geo_wall_patch_names(case_dir)

Geometric wall fallback for raw GMSH cases (every patch type 'patch', surface_N names): infer inlet/outlet from the geometry (case_setup.infer_patch_roles) and treat every other patch as a wall — so the default BL never touches inlet/outlet even when no patch is explicitly named/typed wall.

poly_wall_patch_names(case_dir)

Wall-like patches of a polyMesh boundary file — the default target set for the poly BL step (FASE 1): a patch qualifies if its boundary type is wall (set by the BC editor / set_wall_patch_types) or its name explicitly says wall (*wall*).

polymesh_has_boundary_layer(case_dir)

True only when the polyMesh already carries a REAL boundary layer.

preflight_cell_size(case_dir)

Detect a doomed maxCellSize/geometry combination before running cfMesh.

Classes

CheckMeshWorker(case_dir, of_config[, ...])

Runs checkMesh via WSL2 in a QThread and returns a quality report.

DecomposeParWorker(case_dir, of_config, n_cores)

Runs decomposePar -force in a background QThread to decompose an existing mesh into processorN/ directories for parallel SOLVING.

DualPolyWorker(case_dir[, bl_params, parent])

Runs TetPolyDualConverter (tet -> polyhedral by barycentric dual) in a background QThread.

ErrorInfo([error_type, message, suggestion, ...])

ErrorType(value)

ExportWorker(case_dir, fmt, output_path[, ...])

Export mesh to a format in a background thread.

GmshSignatureStallDetector([clock])

Stateful, content-aware runaway detector for the GMSH volume stream.

GmshSurfaceWorker(geom_path, stl_out, detail)

Runs GMSH surface STL generation + sizing in a subprocess.

GmshVolumeWorker(step_path, msh_path, detail)

Runs GMSH volume mesh generation in a subprocess.

MeshQualityReport([cells, faces, points, ...])

Structured output from checkMesh.

MeshWorker(case_dir, of_config[, parent])

Runs cartesianMesh via WSL2 in a background QThread.

ParallelMeshWorker(case_dir, of_config, ...)

Runs ParallelMeshEngine.run() (synchronous, MPI-based) in a background QThread so it doesn't freeze the GUI's own event loop.

PolyDualWorker(case_dir, of_config[, ...])

Runs polyDualMesh via WSL2 in a background QThread.

PreflightIssue(fatal, message[, suggestion])

A problem detectable before launching cfMesh.

QualityFixWorker(of_config[, parent])

Runs meshing + checkMesh + auto-fix loop with max 3 iterations.

RetryRunner(of_config[, parent])

Orchestrates cartesianMesh with up to 3 automatic retries on known errors.

SolutionAdaptiveWorker(case_dir, bounds, ...)

Runs the solution-adaptive refinement loop in a background QThread.

WatertightWorker(stl_paths[, parent])

Runs watertight check + optional pymeshfix repair in a subprocess.

WslCheckWorker(of_config[, parent])

Runs OFConfig.validate() (blocking wsl.exe call) in a background QThread.

class polyfoammesh.core.openfoam_runner.ErrorType(value)[source]

Bases: Enum

NONE = 'none'
NON_WATERTIGHT = 'non_watertight'
SURFACE_READ = 'surface_read'
PATCH_NOT_FOUND = 'patch_not_found'
FOAM_FATAL = 'foam_fatal'
CRASH = 'crash'
NON_MAPPABLE = 'non_mappable'
BOUNDARY_NOT_FOUND = 'boundary_not_found'
BL_FAILURE = 'boundary_layer_failure'
class polyfoammesh.core.openfoam_runner.ErrorInfo(error_type=ErrorType.NONE, message='', suggestion='', detail='')[source]

Bases: object

Parameters:
error_type: ErrorType = 'none'
message: str = ''
suggestion: str = ''
detail: str = ''
polyfoammesh.core.openfoam_runner.analyze_error(full_output)[source]

Classify cartesianMesh errors using cfMesh-specific patterns.

Return type:

ErrorInfo

Parameters:

full_output (str)

class polyfoammesh.core.openfoam_runner.MeshWorker(case_dir, of_config, parent=None)[source]

Bases: QObject

Runs cartesianMesh via WSL2 in a background QThread.

Reads stdout from a dedicated reader thread (NOT select.select(), which on Windows only works on sockets, never on pipes) so that interruption requests are still honoured while the subprocess is silent for a long time.

Contract: run() ALWAYS emits finished exactly once, on every path including unexpected errors. Callers block on that signal, so a missing emit is not a lost error message — it is a permanent hang.

Parameters:
log_line
finished
cell_count_found
progress_update
eta_update
MAX_RUNTIME_SECONDS = 14400
run()[source]
staticMetaObject = PySide6.QtCore.QMetaObject("MeshWorker" inherits "QObject": Methods:   #4 type=Signal, signature=log_line(QString), parameters=QString   #5 type=Signal, signature=finished(int,QString), parameters=int, QString   #6 type=Signal, signature=cell_count_found(int), parameters=int   #7 type=Signal, signature=progress_update(int), parameters=int   #8 type=Signal, signature=eta_update(QString), parameters=QString   #9 type=Slot, signature=run() )
class polyfoammesh.core.openfoam_runner.RetryRunner(of_config, parent=None)[source]

Bases: QObject

Orchestrates cartesianMesh with up to 3 automatic retries on known errors.

V1.1 additions:
  • F2: cell_count_relay for real cell count.

  • F6: automatic fallback without boundary layers if BL causes failure.

  • F7: exit-code 15 treated as non-fatal for unconnected regions.

Parameters:

of_config (OFConfig)

MAX_RETRIES = 3
cell_count_found
log_emitted
cell_count_relay
progress_update
eta_update
property is_running: bool
thread()[source]
Return type:

QThread | None

request_stop()[source]
run(case_dir, on_log=None, on_finished=None, fix_action=None, bl_params=None, max_cell=0.05, min_cell=0.01, patch_cell_size=None, patch_names=None)[source]
Parameters:
terminate()[source]

Kill subprocess and cleanup. Call before starting a new run.

staticMetaObject = PySide6.QtCore.QMetaObject("RetryRunner" inherits "QObject": Methods:   #4 type=Signal, signature=cell_count_found(int), parameters=int   #5 type=Signal, signature=log_emitted(QString), parameters=QString   #6 type=Signal, signature=cell_count_relay(int), parameters=int   #7 type=Signal, signature=progress_update(int), parameters=int   #8 type=Signal, signature=eta_update(QString), parameters=QString   #9 type=Slot, signature=_on_attempt_finished(int,QString), parameters=int, QString )
class polyfoammesh.core.openfoam_runner.MeshQualityReport(cells=0, faces=0, points=0, max_non_ortho=0.0, avg_non_ortho=0.0, max_skewness=0.0, avg_skewness=0.0, max_aspect_ratio=0.0, neg_cells=0, min_volume=0.0, has_fatal=False, raw_output='')[source]

Bases: object

Structured output from checkMesh.

Parameters:
cells: int = 0
faces: int = 0
points: int = 0
max_non_ortho: float = 0.0
avg_non_ortho: float = 0.0
max_skewness: float = 0.0
avg_skewness: float = 0.0
max_aspect_ratio: float = 0.0
neg_cells: int = 0
min_volume: float = 0.0
has_fatal: bool = False
raw_output: str = ''
property passed: bool
property status: str
summary()[source]

One-line summary for logs/status bars.

Return type:

str

to_dict()[source]
Return type:

dict

polyfoammesh.core.openfoam_runner.parse_checkmesh_output(text)[source]

Parse checkMesh stdout into a structured report.

Return type:

MeshQualityReport

Parameters:

text (str)

class polyfoammesh.core.openfoam_runner.CheckMeshWorker(case_dir, of_config, parent=None, pipeline_staged=False)[source]

Bases: QObject

Runs checkMesh via WSL2 in a QThread and returns a quality report.

Parameters:
log_line
finished
failed
cancel()[source]

Kill the live subprocess (wsl.exe), if any is running right now. See GmshSurfaceWorker.cancel for why this exists — same gap here even though _on_cancel_meshing’s _kill_wsl_processes() gives this one partial coverage already, killing wsl.exe directly by PID is faster and doesn’t depend on process-name matching.

run()[source]
staticMetaObject = PySide6.QtCore.QMetaObject("CheckMeshWorker" inherits "QObject": Methods:   #4 type=Signal, signature=log_line(QString), parameters=QString   #5 type=Signal, signature=finished(PyObject), parameters=PyObject   #6 type=Signal, signature=failed(QString), parameters=QString   #7 type=Slot, signature=cancel()   #8 type=Slot, signature=run() )
class polyfoammesh.core.openfoam_runner.PolyDualWorker(case_dir, of_config, feature_angle=90, parent=None)[source]

Bases: QObject

Runs polyDualMesh via WSL2 in a background QThread.

polyDualMesh converts the hexahedral mesh from cartesianMesh into an arbitrary polyhedral mesh (reduces cell count while preserving surface quality). It writes output to the same case directory.

Parameters:
log_line
finished
failed
run()[source]
staticMetaObject = PySide6.QtCore.QMetaObject("PolyDualWorker" inherits "QObject": Methods:   #4 type=Signal, signature=log_line(QString), parameters=QString   #5 type=Signal, signature=finished(PyObject), parameters=PyObject   #6 type=Signal, signature=failed(QString), parameters=QString   #7 type=Slot, signature=run() )
class polyfoammesh.core.openfoam_runner.QualityFixWorker(of_config, parent=None)[source]

Bases: QObject

Runs meshing + checkMesh + auto-fix loop with max 3 iterations.

Uses RetryRunner internally for the meshing step, then runs checkMesh. If quality checks fail, applies meshDict fixes and re-runs.

Parameters:

of_config (OFConfig)

MAX_FIX_ATTEMPTS = 3
log_line
finished
failed
run(case_dir, **kwargs)[source]
Parameters:

case_dir (Path)

terminate()[source]
staticMetaObject = PySide6.QtCore.QMetaObject("QualityFixWorker" inherits "QObject": Methods:   #4 type=Signal, signature=log_line(QString), parameters=QString   #5 type=Signal, signature=finished(int), parameters=int   #6 type=Signal, signature=failed(QString), parameters=QString   #7 type=Slot, signature=run() )
class polyfoammesh.core.openfoam_runner.ParallelMeshWorker(case_dir, of_config, max_cell, min_cell, n_cores, patch_names=None, method='scotch', parent=None, bl_params=None)[source]

Bases: QObject

Runs ParallelMeshEngine.run() (synchronous, MPI-based) in a background QThread so it doesn’t freeze the GUI’s own event loop.

Supports cancellation via cancel(): sets an event that the engine’s polling subprocess runner checks every 0.5s and kills the MPI ranks.

Parameters:
log_line
finished
failed
cancelled
cancel()[source]

Request cancellation of the running parallel meshing. Safe to call from any thread; triggers a process-tree kill inside WSL2 on the next 0.5s poll cycle.

Return type:

None

run()[source]
staticMetaObject = PySide6.QtCore.QMetaObject("ParallelMeshWorker" inherits "QObject": Methods:   #4 type=Signal, signature=log_line(QString), parameters=QString   #5 type=Signal, signature=finished(PyObject), parameters=PyObject   #6 type=Signal, signature=failed(QString), parameters=QString   #7 type=Signal, signature=cancelled()   #8 type=Slot, signature=run() )
class polyfoammesh.core.openfoam_runner.DecomposeParWorker(case_dir, of_config, n_cores, method='scotch', parent=None)[source]

Bases: QObject

Runs decomposePar -force in a background QThread to decompose an existing mesh into processorN/ directories for parallel SOLVING.

Parameters:
log_line
finished
failed
run()[source]
staticMetaObject = PySide6.QtCore.QMetaObject("DecomposeParWorker" inherits "QObject": Methods:   #4 type=Signal, signature=log_line(QString), parameters=QString   #5 type=Signal, signature=finished(PyObject), parameters=PyObject   #6 type=Signal, signature=failed(QString), parameters=QString   #7 type=Slot, signature=run() )
class polyfoammesh.core.openfoam_runner.WslCheckWorker(of_config, parent=None)[source]

Bases: QObject

Runs OFConfig.validate() (blocking wsl.exe call) in a background QThread. WSL2 auto-shuts-down its VM after inactivity, so this can take well over a minute on a cold boot — running it on the GUI thread freezes the whole window with no feedback, indistinguishable from a crash.

Parameters:

of_config (OFConfig)

finished
run()[source]
staticMetaObject = PySide6.QtCore.QMetaObject("WslCheckWorker" inherits "QObject": Methods:   #4 type=Signal, signature=finished(bool), parameters=bool   #5 type=Slot, signature=run() )
polyfoammesh.core.openfoam_runner.generate_fms(case_dir, angle=60.0, timeout=60)[source]

Run surfaceFeatureEdges to produce an .fms file from the surface STL.

Wraps the WSL2 call to surfaceFeatureEdges -angle <angle>, run from within the case directory so that output paths are relative and OpenFOAM can find its config files.

The FMS file is written to constant/triSurface/surface.fms.

Parameters:
  • case_dir (Path | str) – OpenFOAM case directory (must contain constant/triSurface/surface.stl).

  • angle (float) – Feature angle threshold in degrees (default 30).

  • timeout (int) – Max wall-clock seconds for the WSL call.

Return type:

Path | None

Returns:

Path to the generated .fms file, or None on failure.

polyfoammesh.core.openfoam_runner.poly_wall_patch_names(case_dir)[source]

Wall-like patches of a polyMesh boundary file — the default target set for the poly BL step (FASE 1): a patch qualifies if its boundary type is wall (set by the BC editor / set_wall_patch_types) or its name explicitly says wall (*wall*). GMSH surface_N patches and role-named patches (inlet/outlet/opening) do NOT qualify — a raw GMSH case returns an empty list and the worker falls back to the geometric inference (poly_geo_wall_patch_names), so the default BL never touches inlet/outlet even without explicit wall naming.

Return type:

list[str]

Parameters:

case_dir (Path | str)

polyfoammesh.core.openfoam_runner.poly_geo_wall_patch_names(case_dir)[source]

Geometric wall fallback for raw GMSH cases (every patch type ‘patch’, surface_N names): infer inlet/outlet from the geometry (case_setup.infer_patch_roles) and treat every other patch as a wall — so the default BL never touches inlet/outlet even when no patch is explicitly named/typed wall. Returns [] when the roles cannot be inferred (e.g. a fully closed domain) — the caller then falls back to ALL with a warning.

Return type:

list[str]

Parameters:

case_dir (Path | str)

polyfoammesh.core.openfoam_runner.poly_bl_patch_selection(case_dir, apply_to_all_override=False)[source]

Decide which boundary patches the poly BL step may touch.

Returns (patch_names, apply_to_all, notes):

  • patch_names is None -> skip the BL entirely (nothing safe to extrude from). The caller must report this rather than extruding.

Three tiers, cheapest and most authoritative first:

  1. An explicit user override means “all patches” and is honoured literally — that is the user’s call to make.

  2. Boundary type / name, then the geometric inference (poly_wall_patch_names / poly_geo_wall_patch_names).

  3. When both fail (every patch unnamed GMSH surface_N and no inlet/outlet could be placed — closed domains, external aero, multi-inlet manifolds), exclude only what is positively known to be a non-wall and extrude from the rest. If that leaves nothing, skip.

Tier 3 is the one that used to be “apply to ALL”. It fired on exactly the geometries where the least is known, and it put prism stacks in the inlets and outlets: checkMesh accepts that, so the failure only surfaces when the solver diverges. A mesh without layers is a recoverable warning; a mesh with layers in the inlet is a wrong answer.

Return type:

tuple[list[str] | None, bool, list[str]]

Parameters: