@yakovlev-i-a/dsh-openfoam-workbench 0.0.0-stage → 0.9.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +683 -0
- package/CORE.md +50 -0
- package/LICENSE +202 -0
- package/NOTICE +29 -0
- package/README.md +373 -2
- package/client.js +48 -0
- package/cordis.patch.yml +18 -0
- package/icon.png +0 -0
- package/lib/buildlib.js +42 -0
- package/lib/caselib.js +212 -0
- package/lib/core.js +47 -0
- package/lib/discover.js +139 -0
- package/lib/escalations.js +92 -0
- package/lib/explib.js +149 -0
- package/lib/impact.js +55 -0
- package/lib/index.js +109 -0
- package/lib/knowledge.js +108 -0
- package/lib/meshlib.js +375 -0
- package/lib/needs-input.js +32 -0
- package/lib/of-case.js +485 -0
- package/lib/of-engine.js +1061 -0
- package/lib/paperlib.js +200 -0
- package/lib/presentation.js +365 -0
- package/lib/registry.js +142 -0
- package/lib/routing-guard.js +126 -0
- package/lib/runtime.js +580 -0
- package/lib/scaffold-cht.js +318 -0
- package/lib/scaffold-compressible.js +171 -0
- package/lib/scaffold.js +355 -0
- package/lib/settings-schema.js +86 -0
- package/lib/skills.js +53 -0
- package/lib/tools/build.js +90 -0
- package/lib/tools/case.js +1082 -0
- package/lib/tools/env.js +592 -0
- package/lib/tools/experience.js +165 -0
- package/lib/tools/mesh.js +408 -0
- package/lib/tools/meta.js +95 -0
- package/lib/tools/papers.js +105 -0
- package/lib/tools/render.js +94 -0
- package/lib/tools/run.js +331 -0
- package/lib/versions.js +133 -0
- package/licenses/dsh-openfoam-doctor-MIT.txt +21 -0
- package/locale/en.json +6 -0
- package/locale/ru.json +6 -0
- package/package.json +44 -4
- package/skills/openfoam-analyze/SKILL.md +27 -0
- package/skills/openfoam-case-builder/SKILL.md +61 -0
- package/skills/openfoam-cht/SKILL.md +83 -0
- package/skills/openfoam-code-modify/SKILL.md +32 -0
- package/skills/openfoam-mesh-independence/SKILL.md +55 -0
- package/skills/openfoam-meshing/SKILL.md +30 -0
- package/skills/openfoam-paper-reproduction/SKILL.md +36 -0
- package/skills/openfoam-self-evolution/SKILL.md +21 -0
- package/skills/openfoam-solver-development/SKILL.md +61 -0
- package/skills/openfoam-stats/SKILL.md +46 -0
- package/skills/openfoam-viz/SKILL.md +35 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { textOut } from '../runtime.js'
|
|
2
|
+
import { paperExtract, paperFetch, paperSearch, readDocument, readPaper } from '../paperlib.js'
|
|
3
|
+
|
|
4
|
+
export default function papersTools({ cfg }) {
|
|
5
|
+
return [
|
|
6
|
+
// --- tool: paper parameter verify ---
|
|
7
|
+
{
|
|
8
|
+
name: 'openfoam_paper_param_verify',
|
|
9
|
+
description: 'Sanity-check extracted paper parameters: non-empty finite values, and (when expected_units is given) that every expected parameter is present and its unit is a known dimension.',
|
|
10
|
+
parameters: {
|
|
11
|
+
type: 'object',
|
|
12
|
+
properties: { params: { type: 'object' }, expected_units: { type: 'object' } },
|
|
13
|
+
required: ['params'],
|
|
14
|
+
},
|
|
15
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
16
|
+
timeoutMs: 30000,
|
|
17
|
+
isConcurrencySafe: () => true,
|
|
18
|
+
execute: async (args) => {
|
|
19
|
+
const params = args.params || {}
|
|
20
|
+
const expectedUnits = args.expected_units || {}
|
|
21
|
+
const issues = []
|
|
22
|
+
for (const [k, v] of Object.entries(params)) {
|
|
23
|
+
if (v == null || v === '') issues.push({ param: k, issue: 'empty value' })
|
|
24
|
+
else if (typeof v === 'number' && !Number.isFinite(v)) issues.push({ param: k, issue: 'non-finite value' })
|
|
25
|
+
}
|
|
26
|
+
const KNOWN = new Set(['m', 's', 'm/s', 'm2/s', '1/s', 'kg/m3', 'kg/s', 'pa', 'k', 'j/kg', 'w/m2', '-', ''])
|
|
27
|
+
for (const [k, u] of Object.entries(expectedUnits)) {
|
|
28
|
+
if (!(k in params)) issues.push({ param: k, issue: `expected unit ${u}, but the parameter is missing` })
|
|
29
|
+
else if (!KNOWN.has(String(u).toLowerCase())) issues.push({ param: k, issue: `unknown unit ${u}` })
|
|
30
|
+
}
|
|
31
|
+
return { ok: true, pass: issues.length === 0, issues }
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
// --- tool: paper search ---
|
|
35
|
+
{
|
|
36
|
+
name: 'openfoam_paper_search',
|
|
37
|
+
description: 'Search academic papers via Semantic Scholar + arXiv (merged, deduped by title). Returns title/authors/year/venue/citations/DOI/arXiv/PDF. Better than web_search for finding the original paper of an algorithm.',
|
|
38
|
+
parameters: {
|
|
39
|
+
type: 'object',
|
|
40
|
+
properties: { query: { type: 'string' }, top_k: { type: 'number' } },
|
|
41
|
+
required: ['query'],
|
|
42
|
+
},
|
|
43
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
44
|
+
timeoutMs: 60000,
|
|
45
|
+
isConcurrencySafe: () => true,
|
|
46
|
+
execute: async (args) => paperSearch(cfg, args),
|
|
47
|
+
},
|
|
48
|
+
// --- tool: paper fetch ---
|
|
49
|
+
{
|
|
50
|
+
name: 'openfoam_paper_fetch',
|
|
51
|
+
description: 'Fetch paper details by DOI / ARXIV:<id> / S2 paperId (abstract, tldr, references, OA PDF link). download=true saves the OA PDF to <caseRoots[0]>/downloads/papers/.',
|
|
52
|
+
parameters: {
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: { id: { type: 'string' }, download: { type: 'boolean' }, max_refs: { type: 'number' } },
|
|
55
|
+
required: ['id'],
|
|
56
|
+
},
|
|
57
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
58
|
+
timeoutMs: 120000,
|
|
59
|
+
isConcurrencySafe: () => true,
|
|
60
|
+
execute: async (args) => paperFetch(cfg, args),
|
|
61
|
+
},
|
|
62
|
+
// --- tool: read a paper ---
|
|
63
|
+
{
|
|
64
|
+
name: 'openfoam_read_paper',
|
|
65
|
+
description: 'Read a PDF and split it into sections (abstract/introduction/methods/results/conclusion/references); optional focus returns matching passages.',
|
|
66
|
+
parameters: {
|
|
67
|
+
type: 'object',
|
|
68
|
+
properties: { path: { type: 'string' }, focus: { type: 'string' } },
|
|
69
|
+
required: ['path'],
|
|
70
|
+
},
|
|
71
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
72
|
+
timeoutMs: 120000,
|
|
73
|
+
isConcurrencySafe: () => true,
|
|
74
|
+
execute: async (args) => readPaper(cfg, args),
|
|
75
|
+
},
|
|
76
|
+
// --- tool: extract from a paper ---
|
|
77
|
+
{
|
|
78
|
+
name: 'openfoam_paper_extract',
|
|
79
|
+
description: 'Heuristically extract numbered equations, tables and numeric parameters from a PDF (grounded — nothing invented).',
|
|
80
|
+
parameters: {
|
|
81
|
+
type: 'object',
|
|
82
|
+
properties: { path: { type: 'string' }, focus: { type: 'string' } },
|
|
83
|
+
required: ['path'],
|
|
84
|
+
},
|
|
85
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
86
|
+
timeoutMs: 120000,
|
|
87
|
+
isConcurrencySafe: () => true,
|
|
88
|
+
execute: async (args) => paperExtract(cfg, args),
|
|
89
|
+
},
|
|
90
|
+
// --- tool: read a document ---
|
|
91
|
+
{
|
|
92
|
+
name: 'openfoam_read_document',
|
|
93
|
+
description: 'Read a local document: PDF (text via pdftotext/pymupdf) or plain text formats (txt/md/csv/log/dat/json/yaml).',
|
|
94
|
+
parameters: {
|
|
95
|
+
type: 'object',
|
|
96
|
+
properties: { path: { type: 'string' } },
|
|
97
|
+
required: ['path'],
|
|
98
|
+
},
|
|
99
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
100
|
+
timeoutMs: 120000,
|
|
101
|
+
isConcurrencySafe: () => true,
|
|
102
|
+
execute: async (args) => readDocument(cfg, args),
|
|
103
|
+
},
|
|
104
|
+
]
|
|
105
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { resolveCase, textOut } from '../runtime.js'
|
|
3
|
+
import { doRender, renderBlocks, renderCompare } from '../presentation.js'
|
|
4
|
+
|
|
5
|
+
export default function renderTools({ cfg, attachImage }) {
|
|
6
|
+
return [
|
|
7
|
+
// --- tool: render a case (pvpython) ---
|
|
8
|
+
{
|
|
9
|
+
name: 'openfoam_render',
|
|
10
|
+
description: 'Off-screen render one frame of a case with pvpython to PNG (ParaView 6.x renders headless without a display; legacy 5.x GLX builds fall back to xvfb). Optional field colors it, time_step picks a step; multi-region cases render every region (or pass region="fluid"). Fails loudly on an empty scene or an unknown field instead of returning a blank PNG.',
|
|
11
|
+
parameters: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
case_path: { type: 'string' },
|
|
15
|
+
field: { type: 'string', description: 'Cell/point field to color by (e.g. T / U / p). Unknown fields return the list of available ones.' },
|
|
16
|
+
region: { type: 'string', description: 'Render one mesh region of a multi-region case (e.g. fluid); by default all regions are shown.' },
|
|
17
|
+
time_step: { type: 'number', description: '0-based step index, or -1 for the last.' },
|
|
18
|
+
azimuth: { type: 'number' },
|
|
19
|
+
elevation: { type: 'number' },
|
|
20
|
+
zoom: { type: 'number' },
|
|
21
|
+
},
|
|
22
|
+
required: ['case_path'],
|
|
23
|
+
},
|
|
24
|
+
output: { schema: { type: 'object' }, render: (_a, v) => renderBlocks(v) },
|
|
25
|
+
timeoutMs: 360000,
|
|
26
|
+
isConcurrencySafe: () => false,
|
|
27
|
+
execute: async (args) => attachImage(await doRender(cfg, args)),
|
|
28
|
+
},
|
|
29
|
+
// --- tool: compare two renders ---
|
|
30
|
+
{
|
|
31
|
+
name: 'openfoam_compare_render',
|
|
32
|
+
description: 'Render the same field at the same step from two cases, returning both image paths for a side-by-side comparison.',
|
|
33
|
+
parameters: {
|
|
34
|
+
type: 'object',
|
|
35
|
+
properties: {
|
|
36
|
+
case_a: { type: 'string' },
|
|
37
|
+
case_b: { type: 'string' },
|
|
38
|
+
field: { type: 'string' },
|
|
39
|
+
region: { type: 'string', description: 'Mesh region to render from both cases (multi-region).' },
|
|
40
|
+
time_step: { type: 'number' },
|
|
41
|
+
azimuth: { type: 'number' },
|
|
42
|
+
elevation: { type: 'number' },
|
|
43
|
+
},
|
|
44
|
+
required: ['case_a', 'case_b'],
|
|
45
|
+
},
|
|
46
|
+
output: { schema: { type: 'object' }, render: (_a, v) => renderCompare(v) },
|
|
47
|
+
timeoutMs: 600000,
|
|
48
|
+
isConcurrencySafe: () => false,
|
|
49
|
+
execute: async (args) => {
|
|
50
|
+
const a = await attachImage(await doRender(cfg, { ...args, case_path: args.case_a }))
|
|
51
|
+
const b = await attachImage(await doRender(cfg, { ...args, case_path: args.case_b }))
|
|
52
|
+
return { ok: a.ok && b.ok, a, b }
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
// --- tool: post verify ---
|
|
56
|
+
{
|
|
57
|
+
name: 'openfoam_post_verify',
|
|
58
|
+
description: 'Checklist for post-processing review: confirm renders exist and an expectation was stated; the physical judgement is done by the agent with read_image.',
|
|
59
|
+
parameters: {
|
|
60
|
+
type: 'object',
|
|
61
|
+
properties: { images: { type: 'array', items: { type: 'string' } }, expected: { type: 'string' } },
|
|
62
|
+
},
|
|
63
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
64
|
+
timeoutMs: 30000,
|
|
65
|
+
isConcurrencySafe: () => true,
|
|
66
|
+
execute: async (args) => {
|
|
67
|
+
const images = Array.isArray(args.images) ? args.images : []
|
|
68
|
+
const checks = []
|
|
69
|
+
checks.push({ name: 'renders provided', pass: images.length > 0, detail: `${images.length} image(s)` })
|
|
70
|
+
checks.push({ name: 'expectation stated', pass: !!args.expected, detail: args.expected || '(none)' })
|
|
71
|
+
const missing = []
|
|
72
|
+
const invalid = []
|
|
73
|
+
const tooSmall = []
|
|
74
|
+
for (const raw of images) {
|
|
75
|
+
const p = resolveCase(cfg, raw)
|
|
76
|
+
if (!p || !existsSync(p)) { missing.push(raw); continue }
|
|
77
|
+
try {
|
|
78
|
+
const buf = readFileSync(p)
|
|
79
|
+
const isPng = buf.length >= 8 && buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47
|
|
80
|
+
const isJpg = buf.length >= 3 && buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff
|
|
81
|
+
if (!isPng && !isJpg) invalid.push(raw)
|
|
82
|
+
else if (buf.length < 20 * 1024) tooSmall.push(`${raw} (${buf.length} B)`)
|
|
83
|
+
} catch (e) {
|
|
84
|
+
invalid.push(raw)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
checks.push({ name: 'all images exist', pass: missing.length === 0, detail: missing.join(', ') || 'ok' })
|
|
88
|
+
checks.push({ name: 'valid image format', pass: invalid.length === 0, detail: invalid.join(', ') || 'ok' })
|
|
89
|
+
checks.push({ name: 'non-trivial image size (>=20 KB)', pass: tooSmall.length === 0, detail: tooSmall.join(', ') || 'ok' })
|
|
90
|
+
return { ok: true, pass: checks.every((c) => c.pass), checks, note: 'open the images with read_image and judge divergence, symmetry and units' }
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
]
|
|
94
|
+
}
|
package/lib/tools/run.js
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { bashrcFor, caseFromRunRecord, caseLookupHint, countTimeSteps, dictEntry, inferCaseDir, listRunIds, pickLogPath, readLogTail, readText, resolveCase, runFoam, runSnapshot, startSolverRun, stopRun, textOut } from '../runtime.js'
|
|
4
|
+
import { parseCheckMesh, parseRunLog } from '../of-engine.js'
|
|
5
|
+
import { commandAllowed, ensureFoamFile, mtimeOf, progressFrom, restoreZeroDir } from '../of-case.js'
|
|
6
|
+
|
|
7
|
+
function restoreBuiltin(dir, chk, cmd) {
|
|
8
|
+
if (chk.app !== 'restore0Dir') return null
|
|
9
|
+
const processor = (chk.args || []).includes('-processor')
|
|
10
|
+
const r = restoreZeroDir(dir, { processor })
|
|
11
|
+
return { ok: r.ok, command: cmd, builtin: 'restore0Dir', ...(r.ok ? { restored: r.restored, processor } : { error: r.error }) }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function scanInfo(scan) {
|
|
15
|
+
return scan.truncated
|
|
16
|
+
? { truncated: true, scannedBytes: scan.scannedBytes, totalBytes: scan.bytes, note: 'only the log tail was read; earlier errors are not visible' }
|
|
17
|
+
: { truncated: false, totalBytes: scan.bytes }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default function runTools({ cfg }) {
|
|
21
|
+
return [
|
|
22
|
+
// --- tool: run a whitelisted OpenFOAM command ---
|
|
23
|
+
{
|
|
24
|
+
name: 'openfoam_run',
|
|
25
|
+
description: 'Run a whitelisted OpenFOAM command inside a case directory with the OpenFOAM environment sourced: blockMesh, checkMesh, snappyHexMesh, setFields, topoSet, decomposePar, foamRun, foamMultiRun, or a solver, optionally wrapped as `mpirun -np N <app>`. Use this INSTEAD of bash for any OpenFOAM executable. Synchronous with a timeout and refuses `mpirun` — use openfoam_run_solver_async for parallel/production runs. Returns the log tail.',
|
|
26
|
+
parameters: {
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: {
|
|
29
|
+
casePath: { type: 'string' },
|
|
30
|
+
command: { type: 'string' },
|
|
31
|
+
timeout_ms: { type: 'number' },
|
|
32
|
+
distribution: { type: 'string', description: 'org | com' },
|
|
33
|
+
},
|
|
34
|
+
required: ['casePath', 'command'],
|
|
35
|
+
},
|
|
36
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
37
|
+
timeoutMs: 700000,
|
|
38
|
+
isConcurrencySafe: () => false,
|
|
39
|
+
execute: async (args, exec) => {
|
|
40
|
+
const dir = resolveCase(cfg, args.casePath)
|
|
41
|
+
if (!dir || !existsSync(dir)) return { ok: false, error: 'case not found: ' + dir + caseLookupHint(cfg) }
|
|
42
|
+
const cmd = String(args.command || '').trim()
|
|
43
|
+
const chk = commandAllowed(cmd, cfg.extraCommands)
|
|
44
|
+
if (!chk.ok) return { ok: false, error: chk.reason, hint: chk.hint, command: cmd, allowed: chk.allowed }
|
|
45
|
+
const builtin = restoreBuiltin(dir, chk, cmd)
|
|
46
|
+
if (builtin) return builtin
|
|
47
|
+
const bash = bashrcFor(cfg, args.distribution)
|
|
48
|
+
if (!bash || !existsSync(bash)) return { ok: false, error: 'bashrc/foamRoot not configured' }
|
|
49
|
+
// A synchronous tool must never start a production run: it would be killed
|
|
50
|
+
// by the timeout halfway through a time step, or block the whole turn.
|
|
51
|
+
if (chk.launcher) {
|
|
52
|
+
return {
|
|
53
|
+
ok: false,
|
|
54
|
+
error: 'refusing to start an MPI (production) run from the synchronous tool',
|
|
55
|
+
hint: 'use openfoam_run_solver_async for `mpirun ...` runs — it returns a runId and you can track it with openfoam_run_progress / openfoam_solver_status',
|
|
56
|
+
command: cmd,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const foam = ensureFoamFile(dir)
|
|
60
|
+
const t0 = Date.now()
|
|
61
|
+
const r = await runFoam(chk.argv, {
|
|
62
|
+
bashrc: bash,
|
|
63
|
+
dir,
|
|
64
|
+
signal: exec && exec.signal,
|
|
65
|
+
timeoutMs: Math.min(Number(args.timeout_ms) || 600000, 3600000),
|
|
66
|
+
})
|
|
67
|
+
const log = (r.stdout || '') + (r.stderr ? '\n[stderr]\n' + r.stderr : '')
|
|
68
|
+
return { ok: r.ok, command: cmd, exitCode: r.code, killed: r.killed, durationMs: Date.now() - t0, logTail: log.slice(-4000) }
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
// --- tool: parse residuals from a log ---
|
|
72
|
+
{
|
|
73
|
+
name: 'openfoam_residual_series',
|
|
74
|
+
description: 'Per-time-step initial residuals from a saved solver log (the series for plots and convergence trends). For the run verdict use openfoam_solve_verify; for state and ETA of a live run use openfoam_run_progress. Accepts path (alias: log_path).',
|
|
75
|
+
parameters: {
|
|
76
|
+
type: 'object',
|
|
77
|
+
properties: {
|
|
78
|
+
path: { type: 'string' },
|
|
79
|
+
log_path: { type: 'string', description: 'Alias of path.' },
|
|
80
|
+
max_points: { type: 'number' },
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
84
|
+
timeoutMs: 60000,
|
|
85
|
+
isConcurrencySafe: () => true,
|
|
86
|
+
execute: async (args) => {
|
|
87
|
+
const picked = pickLogPath(args)
|
|
88
|
+
if (picked.error) return { ok: false, error: picked.error }
|
|
89
|
+
const p = resolveCase(cfg, picked.path)
|
|
90
|
+
const scan = readLogTail(p, Number(cfg.logTailBytes) || undefined)
|
|
91
|
+
const text = scan.text
|
|
92
|
+
if (!text) return { ok: false, error: `log not found or unreadable: ${p} (accepted names: path, log_path)` }
|
|
93
|
+
const byTime = new Map()
|
|
94
|
+
const order = []
|
|
95
|
+
let t = null
|
|
96
|
+
for (const line of text.split('\n')) {
|
|
97
|
+
const tm = line.match(/Time\s*=\s*([0-9.eE+-]+)/)
|
|
98
|
+
if (tm) { t = Number(tm[1]); continue }
|
|
99
|
+
const rm = line.match(/Solving for (\w+), Initial residual = ([0-9.eE+-]+)/)
|
|
100
|
+
if (rm && t !== null) {
|
|
101
|
+
if (!byTime.has(t)) { byTime.set(t, {}); order.push(t) }
|
|
102
|
+
byTime.get(t)[rm[1]] = Number(rm[2])
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const max = Math.min(Number(args.max_points) || 60, 500)
|
|
106
|
+
const take = order.slice(-max)
|
|
107
|
+
return { ok: true, path: p, timeSteps: take.length, logScan: scanInfo(scan), series: take.map((x) => ({ time: x, residuals: byTime.get(x) })) }
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
// --- tool: parse a log ---
|
|
111
|
+
{
|
|
112
|
+
name: 'openfoam_log',
|
|
113
|
+
description: 'Parse one saved log and return its metrics: checkMesh metrics with an out-of-range judgment, or the outcome of a solver/snappy run (residual floor, fatal errors, divergence). For a live run use openfoam_run_progress (state + ETA); for the final verdict openfoam_solve_verify; for residual curves openfoam_residual_series.',
|
|
114
|
+
parameters: {
|
|
115
|
+
type: 'object',
|
|
116
|
+
properties: { path: { type: 'string' } },
|
|
117
|
+
required: ['path'],
|
|
118
|
+
},
|
|
119
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
120
|
+
timeoutMs: 60000,
|
|
121
|
+
isConcurrencySafe: () => true,
|
|
122
|
+
execute: async (args) => {
|
|
123
|
+
const p = resolveCase(cfg, args.path)
|
|
124
|
+
const scan = readLogTail(p, Number(cfg.logTailBytes) || undefined)
|
|
125
|
+
const text = scan.text
|
|
126
|
+
if (!text) return { ok: false, error: 'cannot read ' + p }
|
|
127
|
+
const isMesh = /checkMesh|Mesh stats|non-orthogonality|aspect ratio/i.test(text)
|
|
128
|
+
return { ok: true, kind: isMesh ? 'checkMesh' : 'run', logScan: scanInfo(scan), summary: isMesh ? parseCheckMesh(text) : parseRunLog(text) }
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
// --- tool: start a background solver run ---
|
|
132
|
+
{
|
|
133
|
+
name: 'openfoam_run_solver_async',
|
|
134
|
+
description: 'Start a whitelisted OpenFOAM command in the background with the OpenFOAM environment sourced — including multi-region runs as `mpirun -np N foamMultiRun`, which openfoam_run cannot hold synchronously. Use this INSTEAD of bash for production runs. Returns a runId for openfoam_solver_status / openfoam_solver_stop; track progress with openfoam_run_progress.',
|
|
135
|
+
parameters: {
|
|
136
|
+
type: 'object',
|
|
137
|
+
properties: {
|
|
138
|
+
casePath: { type: 'string' },
|
|
139
|
+
command: { type: 'string' },
|
|
140
|
+
distribution: { type: 'string', description: 'org | com' },
|
|
141
|
+
},
|
|
142
|
+
required: ['casePath', 'command'],
|
|
143
|
+
},
|
|
144
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
145
|
+
timeoutMs: 60000,
|
|
146
|
+
isConcurrencySafe: () => false,
|
|
147
|
+
execute: async (args) => {
|
|
148
|
+
const dir = resolveCase(cfg, args.casePath)
|
|
149
|
+
if (!dir || !existsSync(dir)) return { ok: false, error: 'case not found: ' + dir + caseLookupHint(cfg) }
|
|
150
|
+
const cmd = String(args.command || '').trim()
|
|
151
|
+
const chk = commandAllowed(cmd, cfg.extraCommands)
|
|
152
|
+
if (!chk.ok) return { ok: false, error: chk.reason, hint: chk.hint, command: cmd, allowed: chk.allowed }
|
|
153
|
+
const builtin = restoreBuiltin(dir, chk, cmd)
|
|
154
|
+
if (builtin) return builtin
|
|
155
|
+
const bash = bashrcFor(cfg, args.distribution)
|
|
156
|
+
if (!bash || !existsSync(bash)) return { ok: false, error: 'bashrc/foamRoot not configured' }
|
|
157
|
+
const foam = ensureFoamFile(dir)
|
|
158
|
+
const run = startSolverRun(cfg, dir, chk.argv, bash)
|
|
159
|
+
return { ok: true, runId: run.id, casePath: dir, command: cmd, foamFile: foam.ok ? foam.name : null, log: run.log }
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
// --- tool: background run status ---
|
|
163
|
+
{
|
|
164
|
+
name: 'openfoam_solver_status',
|
|
165
|
+
description: 'Check a background run started by openfoam_run_solver_async: running state, exit code, last Time, duration and log tail.',
|
|
166
|
+
parameters: {
|
|
167
|
+
type: 'object',
|
|
168
|
+
properties: { run_id: { type: 'string' } },
|
|
169
|
+
required: ['run_id'],
|
|
170
|
+
},
|
|
171
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
172
|
+
timeoutMs: 60000,
|
|
173
|
+
isConcurrencySafe: () => true,
|
|
174
|
+
execute: async (args) => {
|
|
175
|
+
const s = runSnapshot(cfg, args.run_id)
|
|
176
|
+
if (!s) return { ok: false, error: 'no such run: ' + args.run_id }
|
|
177
|
+
return { ok: true, ...s }
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
// --- tool: stop a background run ---
|
|
181
|
+
{
|
|
182
|
+
name: 'openfoam_solver_stop',
|
|
183
|
+
description: 'Stop a background run started by openfoam_run_solver_async (SIGTERM, then SIGKILL after 3 s).',
|
|
184
|
+
parameters: {
|
|
185
|
+
type: 'object',
|
|
186
|
+
properties: { run_id: { type: 'string' } },
|
|
187
|
+
required: ['run_id'],
|
|
188
|
+
},
|
|
189
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
190
|
+
timeoutMs: 60000,
|
|
191
|
+
isConcurrencySafe: () => true,
|
|
192
|
+
execute: async (args) => stopRun(cfg, args.run_id),
|
|
193
|
+
},
|
|
194
|
+
// --- tool: solve verify ---
|
|
195
|
+
{
|
|
196
|
+
name: 'openfoam_solve_verify',
|
|
197
|
+
description: 'Judge a solver run from its log: RUNNING (t, steps, s/step, ETA), finished/End, divergence, residual floor, with reasons. Use at the end of a run (during a run openfoam_run_progress is lighter); the series itself is openfoam_residual_series. Understands multi-region and parallel logs and ignores the startup sigFpe banner. Accepts log_path (alias: path).',
|
|
198
|
+
parameters: {
|
|
199
|
+
type: 'object',
|
|
200
|
+
properties: {
|
|
201
|
+
log_path: { type: 'string' },
|
|
202
|
+
path: { type: 'string', description: 'Alias of log_path.' },
|
|
203
|
+
casePath: { type: 'string', description: 'Case directory; when omitted it is inferred from the log path (used for endTime/ETA).' },
|
|
204
|
+
residual_floor_tol: { type: 'number' },
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
208
|
+
timeoutMs: 60000,
|
|
209
|
+
isConcurrencySafe: () => true,
|
|
210
|
+
execute: async (args) => {
|
|
211
|
+
const picked = pickLogPath(args)
|
|
212
|
+
if (picked.error) return { ok: false, error: picked.error }
|
|
213
|
+
const p = resolveCase(cfg, picked.path)
|
|
214
|
+
const scan = readLogTail(p, Number(cfg.logTailBytes) || undefined)
|
|
215
|
+
const text = scan.text
|
|
216
|
+
if (!text) return { ok: false, error: 'log not found: ' + p }
|
|
217
|
+
const tol = Number(args.residual_floor_tol) || 1e-3
|
|
218
|
+
const caseDir = inferCaseDir(cfg, p, args.casePath)
|
|
219
|
+
let endTime = null
|
|
220
|
+
if (caseDir) {
|
|
221
|
+
const cd = readText(join(caseDir, 'system', 'controlDict'))
|
|
222
|
+
if (cd) endTime = Number(dictEntry(cd, 'endTime')) || null
|
|
223
|
+
}
|
|
224
|
+
const total = scan.truncated ? countTimeSteps(p) : null
|
|
225
|
+
const prog = progressFrom(text, {
|
|
226
|
+
endTime, mtimeMs: mtimeOf(p), nowMs: Date.now(), tolerance: tol,
|
|
227
|
+
stepsTotal: total && !total.budgetExceeded ? total.count : null,
|
|
228
|
+
logWindow: scan.truncated ? { truncated: true, bytes: scan.bytes, scannedBytes: scan.scannedBytes ?? null } : null,
|
|
229
|
+
})
|
|
230
|
+
const reasons = []
|
|
231
|
+
if (prog.state === 'running') {
|
|
232
|
+
reasons.push(`run is in progress: t = ${prog.time}${endTime ? ' / ' + endTime : ''}, ${prog.steps} time steps, ${prog.secondsPerStep ? prog.secondsPerStep.toFixed(2) + ' s/step' : 's/step unknown'}${prog.eta ? ', ETA ' + prog.eta : ''}`)
|
|
233
|
+
}
|
|
234
|
+
if (prog.state === 'stalled') reasons.push(`log has not grown for ${prog.staleSeconds}s while the run has no End`)
|
|
235
|
+
if (prog.state === 'finished' && !prog.pass) reasons.push(prog.residualFloor != null ? `residual floor ${prog.residualFloor} >= ${tol}` : 'no residual floor parsed')
|
|
236
|
+
if (prog.diverged) reasons.push('divergence signals found in the log')
|
|
237
|
+
if (prog.errors.length) reasons.push('errors in the log: ' + prog.errors.slice(0, 2).join(' | '))
|
|
238
|
+
return {
|
|
239
|
+
ok: true,
|
|
240
|
+
state: prog.state,
|
|
241
|
+
pass: prog.pass,
|
|
242
|
+
decision: prog.decision,
|
|
243
|
+
steps: prog.steps,
|
|
244
|
+
stepsInWindow: prog.stepsInWindow,
|
|
245
|
+
...(prog.stepsTotal != null ? { stepsTotal: prog.stepsTotal } : {}),
|
|
246
|
+
...(prog.logWindow ? { logWindow: prog.logWindow } : {}),
|
|
247
|
+
metrics: {
|
|
248
|
+
finished: prog.finished,
|
|
249
|
+
diverged: prog.diverged,
|
|
250
|
+
residualFloor: prog.residualFloor,
|
|
251
|
+
lastTime: prog.time,
|
|
252
|
+
timeSteps: prog.steps,
|
|
253
|
+
secondsPerStep: prog.secondsPerStep,
|
|
254
|
+
eta: prog.eta,
|
|
255
|
+
elapsed: prog.elapsed,
|
|
256
|
+
endTime,
|
|
257
|
+
errors: prog.errors,
|
|
258
|
+
warnings: prog.warnings,
|
|
259
|
+
},
|
|
260
|
+
reasons,
|
|
261
|
+
casePath: caseDir,
|
|
262
|
+
logScan: scanInfo(scan),
|
|
263
|
+
visual: 'render the final state and judge with read_image',
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
// --- tool: list durable background runs ---
|
|
268
|
+
{
|
|
269
|
+
name: 'openfoam_run_list',
|
|
270
|
+
description: 'List background runs from the durable registry (id, case, command, status, last Time, duration). Survives restarts; statuses: running | stalled | success | interrupted | stopped | stopping.',
|
|
271
|
+
parameters: {
|
|
272
|
+
type: 'object',
|
|
273
|
+
properties: {
|
|
274
|
+
status: { type: 'string', description: 'Optional filter: running | stalled | success | interrupted | stopped | stopping.' },
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
278
|
+
timeoutMs: 60000,
|
|
279
|
+
isConcurrencySafe: () => true,
|
|
280
|
+
execute: async (args) => {
|
|
281
|
+
const runs = []
|
|
282
|
+
for (const id of listRunIds(cfg)) {
|
|
283
|
+
const s = runSnapshot(cfg, id)
|
|
284
|
+
if (!s) continue
|
|
285
|
+
if (args.status && s.status !== args.status) continue
|
|
286
|
+
runs.push(s)
|
|
287
|
+
}
|
|
288
|
+
runs.sort((a, b) => (b.startedAt || 0) - (a.startedAt || 0))
|
|
289
|
+
return { ok: true, count: runs.length, runs }
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
// --- tool: run progress (t, steps, s/step, ETA) ---
|
|
293
|
+
{
|
|
294
|
+
name: 'openfoam_run_progress',
|
|
295
|
+
description: 'Report progress of a running (or finished) solver log: current time, time steps, seconds per step, ETA, residual floor and state (running | stalled | finished | failed). Understands multi-region and parallel logs. Use it while a run is in flight; openfoam_solve_verify gives the final verdict and openfoam_residual_series the per-step residuals. Accepts log_path (alias: path).',
|
|
296
|
+
parameters: {
|
|
297
|
+
type: 'object',
|
|
298
|
+
properties: {
|
|
299
|
+
log_path: { type: 'string' },
|
|
300
|
+
path: { type: 'string', description: 'Alias of log_path.' },
|
|
301
|
+
casePath: { type: 'string', description: 'Optional case dir (inferred from the log path); used for endTime/ETA.' },
|
|
302
|
+
endTime: { type: 'number', description: 'Override the case endTime.' },
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
output: { schema: { type: 'object' }, render: (_a, v) => textOut(v) },
|
|
306
|
+
timeoutMs: 60000,
|
|
307
|
+
isConcurrencySafe: () => true,
|
|
308
|
+
execute: async (args) => {
|
|
309
|
+
const picked = pickLogPath(args)
|
|
310
|
+
if (picked.error) return { ok: false, error: picked.error }
|
|
311
|
+
const p = resolveCase(cfg, picked.path)
|
|
312
|
+
const scan = readLogTail(p, Number(cfg.logTailBytes) || undefined)
|
|
313
|
+
const text = scan.text
|
|
314
|
+
if (text == null) return { ok: false, error: 'log not found: ' + p }
|
|
315
|
+
const caseDir = inferCaseDir(cfg, p, args.casePath) || caseFromRunRecord(p)
|
|
316
|
+
let endTime = Number(args.endTime) || null
|
|
317
|
+
if (!endTime && caseDir) {
|
|
318
|
+
const cd = readText(join(caseDir, 'system', 'controlDict'))
|
|
319
|
+
if (cd) endTime = Number(dictEntry(cd, 'endTime')) || null
|
|
320
|
+
}
|
|
321
|
+
const total = scan.truncated ? countTimeSteps(p) : null
|
|
322
|
+
const prog = progressFrom(text, {
|
|
323
|
+
endTime, mtimeMs: mtimeOf(p),
|
|
324
|
+
stepsTotal: total && !total.budgetExceeded ? total.count : null,
|
|
325
|
+
logWindow: scan.truncated ? { truncated: true, bytes: scan.bytes, scannedBytes: scan.scannedBytes ?? null, stepsInWindow: null } : null,
|
|
326
|
+
})
|
|
327
|
+
return { ok: true, log: p, casePath: caseDir, endTime, logScan: scanInfo(scan), ...prog }
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
]
|
|
331
|
+
}
|
package/lib/versions.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// OpenFOAM flavour/version capabilities.
|
|
2
|
+
//
|
|
3
|
+
// The workbench targets two families (org modular, com legacy) across many
|
|
4
|
+
// releases. Instead of branching on strings inside every tool, one capability
|
|
5
|
+
// object per installation describes what it can do and which dictionary
|
|
6
|
+
// conventions it reads. Feature detection (which utilities exist) wins over the
|
|
7
|
+
// version number; the number only feeds the support policy and the message.
|
|
8
|
+
import { existsSync } from 'node:fs'
|
|
9
|
+
import { probeBashrc } from './runtime.js'
|
|
10
|
+
|
|
11
|
+
const ORG = {
|
|
12
|
+
family: 'org',
|
|
13
|
+
modular: true,
|
|
14
|
+
support: 'partial',
|
|
15
|
+
snappyQualityKeys: true,
|
|
16
|
+
blockMeshScale: 'scale',
|
|
17
|
+
featuresUtility: 'surfaceFeatures',
|
|
18
|
+
featuresDict: 'surfaceFeaturesDict',
|
|
19
|
+
splitRegions: ['-cellZones', 'all', '-defaultRegion', 'fluid'],
|
|
20
|
+
featuresDictStyle: 'surfaceFeatures',
|
|
21
|
+
postProcess: 'foamPostProcess',
|
|
22
|
+
fieldStats: { min: 'cellMin', max: 'cellMax', mag: 'cellMaxMag' },
|
|
23
|
+
thermoFile: 'physicalProperties',
|
|
24
|
+
transportFile: 'physicalProperties',
|
|
25
|
+
turbulenceFile: 'momentumTransport',
|
|
26
|
+
regionModel: 'regionSolvers',
|
|
27
|
+
zeroDir: '0',
|
|
28
|
+
namedDimensions: true,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const COM = {
|
|
32
|
+
family: 'com',
|
|
33
|
+
modular: false,
|
|
34
|
+
support: 'partial',
|
|
35
|
+
snappyQualityKeys: true,
|
|
36
|
+
blockMeshScale: 'scale',
|
|
37
|
+
featuresUtility: 'surfaceFeatureExtract',
|
|
38
|
+
featuresDict: 'surfaceFeatureExtractDict',
|
|
39
|
+
splitRegions: ['-cellZones', '-overwrite'],
|
|
40
|
+
featuresDictStyle: 'extract',
|
|
41
|
+
postProcess: 'postProcess',
|
|
42
|
+
fieldStats: { min: 'cellMin', max: 'cellMax', mag: 'cellMaxMag' },
|
|
43
|
+
thermoFile: 'thermophysicalProperties',
|
|
44
|
+
transportFile: 'transportProperties',
|
|
45
|
+
turbulenceFile: 'turbulenceProperties',
|
|
46
|
+
regionModel: 'regionProperties',
|
|
47
|
+
zeroDir: '0.orig',
|
|
48
|
+
namedDimensions: true,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Parse "14", "v2606" into a comparable number (0 when unknown). */
|
|
52
|
+
export function versionNumber(version) {
|
|
53
|
+
const text = String(version || '').trim()
|
|
54
|
+
// com: v2606, v2512, and custom suffixes such as v2606wang
|
|
55
|
+
const com = /^v(\d{4})/i.exec(text)
|
|
56
|
+
if (com) return Number(com[1])
|
|
57
|
+
const org = /^v?(\d{1,2})$/.exec(text)
|
|
58
|
+
return org ? Number(org[1]) : 0
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** How well this release is covered: full = tested end to end, unknown = best effort. */
|
|
62
|
+
export function supportLevel(family, number) {
|
|
63
|
+
if (!number) return 'unknown'
|
|
64
|
+
if (family === 'org') return number >= 14 ? 'full' : number >= 10 ? 'partial' : 'unknown'
|
|
65
|
+
return number >= 2606 ? 'full' : number >= 2312 ? 'partial' : 'unknown'
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Capabilities for a family + version, without probing an installation. */
|
|
69
|
+
export function versionCaps(distribution, version) {
|
|
70
|
+
const text = String(distribution || '')
|
|
71
|
+
const family = /^(v\d|com)/i.test(text) ? 'com' : 'org'
|
|
72
|
+
const n = versionNumber(version) || versionNumber(text)
|
|
73
|
+
const caps = { ...(family === 'com' ? COM : ORG), version: String(version || ''), number: n }
|
|
74
|
+
caps.support = supportLevel(family, n)
|
|
75
|
+
if (family === 'org' && n && n < 10) {
|
|
76
|
+
caps.modular = false
|
|
77
|
+
caps.blockMeshScale = 'convertToMeters'
|
|
78
|
+
caps.featuresUtility = 'surfaceFeatureExtract'
|
|
79
|
+
caps.featuresDict = 'surfaceFeatureExtractDict'
|
|
80
|
+
caps.featuresDictStyle = 'extract'
|
|
81
|
+
caps.postProcess = 'postProcess'
|
|
82
|
+
}
|
|
83
|
+
return caps
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const cache = new Map()
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Capabilities of the installation behind `bashrc`, detected once per process.
|
|
90
|
+
* The probe asks the installation itself: its version and which utilities exist.
|
|
91
|
+
*/
|
|
92
|
+
export async function probeCaps(bashrc, distribution = '') {
|
|
93
|
+
const key = String(bashrc || '') + '|' + String(distribution || '')
|
|
94
|
+
if (cache.has(key)) return cache.get(key)
|
|
95
|
+
let caps = versionCaps(distribution, '')
|
|
96
|
+
if (bashrc && existsSync(bashrc)) {
|
|
97
|
+
const script = [
|
|
98
|
+
'echo "$WM_PROJECT_VERSION"',
|
|
99
|
+
'echo "$WM_PROJECT"',
|
|
100
|
+
'for u in postProcess foamPostProcess surfaceFeatureExtract surfaceFeatures foamRun foamMultiRun createZones topoSet setsToZones; do command -v "$u" >/dev/null && echo "yes $u" || echo "no $u"; done',
|
|
101
|
+
].join('; ')
|
|
102
|
+
try {
|
|
103
|
+
const r = await probeBashrc(bashrc, script, { timeoutMs: 20000 })
|
|
104
|
+
const lines = String(r.stdout || '').split('\n').map((s) => s.trim())
|
|
105
|
+
const version = lines[0] || ''
|
|
106
|
+
const project = lines[1] || ''
|
|
107
|
+
caps = versionCaps(/^(v\d|com)/i.test(project) || /^v\d/i.test(version) ? 'com' : 'org', version)
|
|
108
|
+
const has = (u) => lines.includes('yes ' + u)
|
|
109
|
+
if (has('foamPostProcess')) caps.postProcess = 'foamPostProcess'
|
|
110
|
+
else if (has('postProcess')) caps.postProcess = 'postProcess'
|
|
111
|
+
if (has('surfaceFeatures')) caps.featuresUtility = 'surfaceFeatures'
|
|
112
|
+
else if (has('surfaceFeatureExtract')) caps.featuresUtility = 'surfaceFeatureExtract'
|
|
113
|
+
caps.detected = { version, project, modular: has('foamRun') }
|
|
114
|
+
caps.modular = Boolean(caps.detected.modular)
|
|
115
|
+
} catch {
|
|
116
|
+
caps.probeError = 'could not probe the installation; family defaults used'
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
cache.set(key, caps)
|
|
120
|
+
return caps
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Drop the probe cache (tests, or after switching installations). */
|
|
124
|
+
export function resetCapsCache() {
|
|
125
|
+
cache.clear()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** One-line human summary, used by env_check/version_info/doctor. */
|
|
129
|
+
export function capsSummary(caps) {
|
|
130
|
+
if (!caps) return ''
|
|
131
|
+
const v = caps.version ? String(caps.version) : '?'
|
|
132
|
+
return `${caps.family} ${v} (${caps.modular ? 'modular' : 'legacy'}, support: ${caps.support})`
|
|
133
|
+
}
|