In this section
- Cmd Trench transform · energy-transition-simulator / prototype/mod/engine/cmd-trench.mjs lines 1–121
- Trench Design transform · energy-transition-simulator / prototype/mod/engine/trench-design.mjs lines 1–400
Cmd Trench
A JavaScript module in the engine folder of Energy Transition Simulator that works with cables, solar and 33kV, using BS 7671 and IEC 60287.
Set aside from the main view: 98% of its lines are the same as solar-design-studio/202609271740-site-world/world/cmd-trench.mjs (globalgrid2050), which stays. (near-copy-of, proposed by families.py). 98% of lines shared with Cmd Trench; read that one first. All set-aside files, with reasons. Nothing was deleted.
Evidence
- line-jaccard 0.9802 to globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609271740-site-world/world/cmd-trench.mjs:1-119 76b8793066328ffae3f238213e8e48b35e8cd552
- sha256 4c3fddba2cdde36f5110c7494850a640beadd2ffdb52cf52c294e7e7ea61f3d6
- target sha256 85e1d386116e73b390e120806b12cff75b5333f2f89754efde1d16f85f2653ee
- group of 3 (identity.json near-copy edges >= 0.9)
- canonical rule: reachable, newest stamp, core repo, most lines
transformenergy-transition-simulator / prototype/mod/engine/cmd-trench.mjs · lines 1–121 of 121 · Not reached from any entry point (candidate for review) · pattern designText (shared by 3 files)
- What it does
- It calculates cables, solar and 33kV. Defines 12 functions or classes, including trenchEnv, envFor, f, pct, tight.
- Why it exists in the grid workflow
- Domain stage experimental (path word 'prototype'): work its own path marks as experimental (sandbox, prototype, archive or similar words); by this evidence it is not part of a production workflow. Domain terms in the code: cables, solar, 33kV, 132kV.
- Inputs & outputs
- Voltage levels named: 132kV, 33kV.
- Downstream impact
- Used directly by 1 file: Cmd Model (energy-transition-simulator).
- What it achieves
- It rates text (`rateText`) and is built from the trench env helper (`trenchEnv`) and the env for helper (`envFor`). The command line's trench and cable words, as text. Its code works with 33 kV and 132 kV.
- Why it matters
- Imported by Cmd Model.
- Application
- energy-transition-simulator · prototype/mod/engine · JavaScript module
- Uses
- Cable IEC (import), Cable Rating (import), LV DC Sizing (import), Fault Level (import), Trench Plan (import), Station (import), Block Trenches (import), Block MV (import), and 1 more
- Used by
- imported by Cmd Model (import)
- Produces
- TRENCH_KEYS, TRENCH_DEFAULTS, trenchEnv, specOf, rateText, designText
- Defines here
- trenchEnv, envFor, f, pct, tight, specOf, rateText, basis, blockPlan, designText, line, busiest
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- energy-transition-simulator@a165ce6d56cb22271828874d68d570e8fa5e428a prototype/mod/engine/cmd-trench.mjs:1-121 · blob 80ecc14b62e13916abf09bf5a5e21939c4e5f9ff · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: experimental · users: EPC_contractor · canonical: duplicate_candidate
// COPIED UNCHANGED from the v12 world (web/world/cmd-trench.mjs) at v12 commit 3adcee9 (file last changed 604729d). Modular star family: public #148279 trenchEnv.
// Do not edit here: change the source and re-copy. Everything below this header is byte-identical to the source.
// cmd-trench.mjs: the command line's trench and cable words, as text. Pure: the numbers come from the rating cartridges.
// cable rate 400 al 28 circuits [pitch 0.25] [lv|mv|dc] one group of circuits rated together, the BS 7671 433.1 overload
// check (lv-dc-sizing.mjs) and the trench that carries them
// trench design the solar block's trenches as sized, or the block's two cases
// One rating path: cable rate and trench design both rate through trench-plan.mjs (trench-design.mjs, the IEC 60287 method
// in our own code) with the same cable build, so one case gives one answer. Standards are cited by number and clause only;
// every value is our own computation or a labelled assumption.
import { PRESETS } from './cable-iec.mjs';
import { build as mvBuild } from './cable-rating.mjs';
import { chooseIn, coordinate, faultArea } from './lv-dc-sizing.mjs';
import { transformerFaultKA, TX_FAULT } from './fault-level.mjs';
import { sizeTrench, checkAsDrawn, LOADS, PLAN_DEFAULTS, ILLUSTRATIVE } from './trench-plan.mjs';
import { STATION_DEFAULTS as SD } from './station.mjs';
import { BLOCK_DC_ISC_A, blockTrenches } from './block-trenches.mjs';
import { station } from './station.mjs';
import { mvChain } from './block-mv.mjs';
import { kvForMW } from './sld-connect.mjs';
export const TRENCH_KEYS = Object.freeze(['soil', 'load', 'splitDc', 'splitBranch', 'splitCollector', 'splitMv', 'coverDc', 'coverLv', 'coverMv',
'drumM', 'ring']);
/** The typed trench settings' defaults (cmd-model.mjs DEFAULTS): the same values the block is built with. */
export const TRENCH_DEFAULTS = Object.freeze({ splitDc: 0, splitBranch: 0, splitCollector: 0, splitMv: 0, coverDc: PLAN_DEFAULTS.cover.dc,
coverLv: PLAN_DEFAULTS.cover.lv, coverMv: PLAN_DEFAULTS.cover.mv, drumM: PLAN_DEFAULTS.drumM, ring: 'n-1' });
/** The typed sizing inputs, as trench-plan.mjs and block-build.mjs take them. */
export const trenchEnv = st => ({ soilKmW: st.soil, load: st.load,
split: { dc: st.splitDc ?? 0, branch: st.splitBranch ?? 0, collector: st.splitCollector ?? 0, mv: st.splitMv ?? 0 },
cover: { dc: st.coverDc ?? PLAN_DEFAULTS.cover.dc, lv: st.coverLv ?? PLAN_DEFAULTS.cover.lv, mv: st.coverMv ?? PLAN_DEFAULTS.cover.mv },
drumM: st.drumM ?? PLAN_DEFAULTS.drumM, ring: st.ring ?? 'n-1' });
// The environment for one kind of run: its own typed split (the block's collector for LV, as in trench design).
const envFor = (st, kind) => { const e = trenchEnv(st); return { ...e, split: e.split[kind === 'lv' ? 'collector' : kind] }; };
const f = (v, d = 2) => (Number.isFinite(v) ? v.toLocaleString('en-GB', { minimumFractionDigits: d, maximumFractionDigits: d }) : '–');
const pct = v => `${Math.round(v * 100)} %`;
const BLOCK_LV_A = SD.inverterKVA * 1000 / (Math.sqrt(3) * SD.acVolts); // the block's inverter at full output
const FOOT = `${ILLUSTRATIVE} (IEC 60287 method, own code; not for construction.)`;
const tight = z => (z.tight ? ' (tight: little margin)' : '');
/** One cable, one build: the block's own 400 mm² Al where that is the cable, so cable rate and trench design agree. */
export function specOf(kind, size, metal, loadA) {
if (kind === 'dc') return { loadA, areaMm2: size, odMm: size <= 6 ? 6 : 7.2 };
if (kind === 'mv') return { loadA, areaMm2: size, odMm: mvBuild(size).od, material: metal };
const block = size === SD.acAreaMm2 && metal === 'al';
const od = block ? SD.acDiameter * 1000 : PRESETS.lvAl(size, metal === 'cu' ? { cond: { mat: 'Cu', area: size } } : {}).od;
return { loadA, areaMm2: size, odMm: od, material: metal };
}
/**
* rateText({ size, metal, circuits, pitchM, kind }, st) -> words. kind: 'lv' (default: the block's 800 V inverter
* circuits), 'mv' (33 kV, rating only) or 'dc' (string cables, 12 in a duct, n ducts).
*/
export function rateText({ size, metal = 'al', circuits = 1, pitchM = null, kind = 'lv' }, st) {
const head = `${circuits} × ${size} mm² ${metal === 'cu' ? 'Cu' : 'Al'}`;
if (kind === 'dc') {
const loadA = 1.25 * BLOCK_DC_ISC_A, spec = specOf('dc', size, metal, loadA), env = envFor(st, 'dc'), z = sizeTrench('dc', circuits, spec, env);
const drawn = pitchM ? checkAsDrawn('dc', circuits, spec, pitchM, env) : null;
return [`DC ${head}: ${circuits} duct${circuits > 1 ? 's' : ''} of 12 string cables each, ${f(loadA, 1)} A a cable = 1.25 × Isc ${BLOCK_DC_ISC_A} A `
+ '(assumed; IEC 62548-1 cl. 7.2):',
drawn ? `at ${f(pitchM)} m pitch the hottest cable runs at ${pct(drawn.utilisation)} of its rating (${drawn.hottestC} °C).` : '',
`Needs: ${z.name}, ${f(z.widthM)} m wide, cover ${f(z.cover)} m, ${pct(z.utilisation)} loaded${tight(z)}.`, basis(st), FOOT].filter(Boolean).join('\n');
}
const Ib = kind === 'mv' ? 1 : BLOCK_LV_A, spec = specOf(kind, size, metal, Ib), env = envFor(st, kind), pitch = pitchM ?? (kind === 'mv' ? 0.45 : 0.25);
const one = checkAsDrawn(kind, 1, spec, pitch, env).ratingA, grp = checkAsDrawn(kind, circuits, spec, pitch, env).ratingA;
const lines = [`${kind === 'mv' ? '33 kV' : 'LV'} ${head} trefoils, ${f(pitch)} m apart, ${f(env.cover[kind])} m cover, soil ${f(st.soil, 1)} K.m/W, `
+ `${st.load} load:`,
`one circuit alone carries ${Math.round(one)} A${circuits > 1 ? `; ${circuits} together carry ${Math.round(grp)} A each (${pct(grp / one)} of alone)` : ''}.`];
if (kind === 'lv') {
const In = chooseIn(Ib, { device: 'mccb', adjustable: true, setStep: 5 }), co = coordinate({ Ib, In, Iz: grp, device: 'mccb' });
const kA = transformerFaultKA(), need = faultArea(metal, kA, TX_FAULT.seconds);
lines.push(`Inverter circuit Ib ${Math.round(Ib)} A (${SD.inverterKVA} kVA at ${SD.acVolts} V): ${pct(Ib / grp)} of the grouped rating.`,
`BS 7671 433.1 / IEC 60364-4-43: ${co.checks.map(c => `${c.name} ${c.ok ? 'yes' : 'NO'}`).join(' · ')} (breaker set to ${In} A).`,
`LV board fault level about ${Math.round(kA)} kA (${TX_FAULT.mva} MVA at ${TX_FAULT.zPct} % impedance assumed, plus the inverters; impedance method): `
+ `${size} mm² needs ${Math.round(need)} mm² for ${TX_FAULT.seconds} s, ${size >= need ? 'yes' : 'NO'} (BS 7671 434.5.2).`);
const z = sizeTrench('lv', circuits, spec, env);
lines.push(`Trench that carries them: ${z.name}, ${f(z.widthM)} m wide${z.trenches > 1 ? ` each, ${f(z.corridorM)} m corridor` : ''}, `
+ `${pct(z.utilisation)} loaded${z.ok ? '' : ' (still over: a larger conductor)'}${tight(z)}.`);
}
lines.push(basis(st), FOOT);
return lines.join('\n');
}
const basis = st => `Soil ${f(st.soil, 1)} K.m/W, ground 15 °C, conductor 90 °C; load ${st.load}${st.load === 'solar-day'
? ` (loss-load factor ${f(LOADS['solar-day'])}, no clipping: optimistic where inverters clip; continuous is the BS 7671 compliance basis)` : ''}.`;
// The block's own layout for the typed inputs, as block 10mva builds it (no ground needed for the plan): one source.
let planMemo = { key: '', plan: null };
function blockPlan(st) {
const env = trenchEnv(st), key = JSON.stringify([env, st.pocKv]);
if (planMemo.key !== key) {
// Connection voltage as block 10mva picks it: the typed one, else by plant size (sld-connect.mjs), 132 kV at least.
const s = station(null, { corners: false }), stationMVA = s.inventory.aggregateInverterMVA;
const pocKv = [132, 275, 400].includes(st.pocKv) ? st.pocKv : kvForMW(mvChain({ stations: 1, stationMVA, pocKv: 132 }).exportMW, { minKv: 132 });
const mv = mvChain({ stations: 1, stationMVA, pocKv });
planMemo = { key, plan: blockTrenches(s, mv, 0, 0, a => mvBuild(a).od, env) };
}
return planMemo.plan;
}
/**
* designText(st, rows) -> words: the placed block's sized trenches (rows from block-ui), or, with no block, the same
* block's layout for the typed inputs, and the options for its busiest collector stretch and busiest DC stretch.
*/
export function designText(st, rows = null) {
const line = ([k, v, cls]) => `${k ? k + ': ' : ' '}${cls === 'fault' && !['Design fault', 'Not rated'].includes(k) ? '[OVER] ' : ''}${v}`;
if (rows?.length) return ['Solar block trenches, sized by rating:', ...rows.map(line)].join('\n');
const b = blockPlan(st), out = ['No block placed (type block 10mva). The block\'s trenches as it would be built:',...b.plan.rows.map(line)];
const busiest = g => b.items.filter(t => t.group === g).reduce((a, t) => (t.n > a.n ? t : a));
for (const [kind, t, what, pitch] of [['lv', busiest('collector'), 'inverter circuits on the busiest collector stretch', 0.25],
['dc', busiest('dc'), 'DC ducts on the busiest DC stretch', SD.ductSpacing]]) {
const env = envFor(st, kind), z = sizeTrench(kind, t.n, t.spec, env), d = checkAsDrawn(kind, t.n, t.spec, pitch, env);
out.push(`${t.n} ${what}, cover ${f(z.cover)} m: at the original drawn pitch (${f(pitch)} m) ${pct(d.utilisation)} loaded.`,
...z.options.filter(o => o.layers === 1 || o.trenches === 1).map(o => ` ${o.name === z.name ? '→' : ' '} ${o.name}: ${f(o.widestM)} m wide, `
+ `corridor ${f(o.corridorM)} m, ${f(o.dugM3PerM)} m³ dug per metre, ${pct(o.utilisation)} loaded${o.ok ? '' : ' (over)'}`
+ `${o.name === z.name ? tight(z) : ''}`));
}
out.push('Type trench split collector 2 (or dc, branch, mv) to choose trenches side by side; trench cover dc 0.91, soil 1.5 or load solar-day '
+ 'to change the inputs.', basis(st), FOOT);
return out.join('\n');
}Trench Design
A JavaScript module in the engine folder of Energy Transition Simulator that works with cables, using IEC 60287-1-1 and IEC 60287-2-1. It solves gap (`solveGap`) and checks drawn (`checkDrawn`).
Set aside from the main view: 99% of its lines are the same as solar-design-studio/202609271853-site-world/world/trench-design.mjs (globalgrid2050), which stays. (near-copy-of, proposed by families.py). 99% of lines shared with Trench Design; read that one first. All set-aside files, with reasons. Nothing was deleted.
Evidence
- line-jaccard 0.9937 to globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609271853-site-world/world/trench-design.mjs:1-398 042b70b0236e14be5671144d5fe2d6b81ebdd7f6
- sha256 4f6299e5f48fc282f929582843b10cd7abd29d5cf2c9d05abb56d2c9a3820836
- target sha256 2f324c42cc53449b975ed4aafe5aa77e2dcbcbd51d57a34f5c159eb99990e95b
- group of 3 (identity.json near-copy edges >= 0.9)
- canonical rule: reachable, newest stamp, core repo, most lines
transformenergy-transition-simulator / prototype/mod/engine/trench-design.mjs · lines 1–400 of 400 · Not reached from any entry point (candidate for review) · pattern designOptions (shared by 3 files)
- What it does
- It calculates cables. Defines 22 functions or classes, including cable, ductAirT, footprint, place, rowW.
- Why it exists in the grid workflow
- Domain stage experimental (path word 'prototype'): work its own path marks as experimental (sandbox, prototype, archive or similar words); by this evidence it is not part of a production workflow. Domain terms in the code: cables.
- Downstream impact
- Used directly by 1 file: Trench Plan (energy-transition-simulator).
- What it achieves
- A trench cross-section designed from the cables it has to carry.
- Why it matters
- Imported by Trench Plan.
- Application
- energy-transition-simulator · prototype/mod/engine · JavaScript module
- Uses
- none found
- Used by
- imported by Trench Plan (import)
- Produces
- COVER, DEFAULTS, METAL, R_ALLOWANCE, cable, ductAirT, footprint, place, temperatures, solveGap, section, designOptions
- Defines here
- cable, ductAirT, footprint, place, rowW, hMax, temperatures, cyclicSelf, solveGap, at, section, worstClass, round, r3, designOptions, add, sumPerMetre, r1, checkDrawn, pullPlan, turn, cut
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- energy-transition-simulator@a165ce6d56cb22271828874d68d570e8fa5e428a prototype/mod/engine/trench-design.mjs:1-400 · blob 29927300db7336e4ea040f36bfcc69155196646f · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: experimental · users: EPC_contractor · canonical: duplicate_candidate
// COPIED UNCHANGED from the v12 world (web/world/trench-design.mjs) at v12 commit 3adcee9 (file last changed 9b1ffad). Modular star family: public #148698 cable.
// Do not edit here: change the source and re-copy. Everything below this header is byte-identical to the source.
// trench-design.mjs: a trench cross-section designed from the cables it has to carry.
//
// Given a set of circuits (voltage class, conductor, formation, load), it finds the spacing at which every
// circuit stays within its conductor temperature, then sets the trench width, the depth of cover for the land
// use, the bedding / surround / marker / tile stack, and the spoil and backfill volumes. When one trench would be
// wider than a practical limit it offers the other ways out: several narrower trenches side by side, two layers,
// a larger conductor. Along a route it places draw pits (ducted runs) at changes of direction and wherever the
// pulling tension would pass the limit, and joint bays at drum lengths.
//
// Method (our own code, written from the published method, no tables copied):
// - steady-state conductor temperature by the thermal-circuit method of IEC 60287-1-1 (losses) and
// IEC 60287-2-1 (internal and external thermal resistances);
// - the heating of each cable by every other cable in the ground by superposition with image sources (the
// "unequally loaded groups" approach of IEC 60287-2-1), so a group is rated at its hottest cable;
// - conductor resistance from the resistivity of the metal with a stated allowance, not from a table;
// - the air gap inside a duct from a radiation-plus-convection estimate (our own, conservative), not from the
// standard's duct constants;
// - pulling tension by the capstan relation (straight runs add friction x weight x length, bends multiply by
// e^(friction x angle)), with the tensile limit per mm2 of conductor as an input.
// Cover, clearances and layer thicknesses are typical UK practice or generic project rules, each an input to confirm
// against the operator's or client's spec. A design check, not a rating study: a real design uses the maker's cable
// data and a rating study (BS 7671 Appendix 4 / IEC 60364-5-52 for LV; the operator's method for MV and HV).
// Pure: no imports, no DOM.
const PI = Math.PI, SQ3 = Math.sqrt(3);
// ---------------------------------------------------------------------------------------------------------------
// Inputs that are practice, not physics. Each carries its basis so the world can show it.
/** Depth of cover (m, finished ground to the top of the uppermost cable or duct) by land use and class. */
export const COVER = Object.freeze({
basis: 'typical UK practice; confirm with the network operator or client specification',
farmland: { dc: 0.91, lv: 0.91, mv: 0.91, hv: 0.91, note: 'good agricultural land, cultivated' },
'deep-cultivation': { dc: 1.2, lv: 1.2, mv: 1.2, hv: 1.2, note: 'land that is subsoiled or deep-ploughed' },
'array-field': { dc: 0.6, lv: 0.6, mv: 0.75, hv: 0.9, note: 'inside a fenced array, not cultivated (design choice, to agree)' },
verge: { dc: 0.45, lv: 0.45, mv: 0.75, hv: 0.9, note: 'verge or footway' },
'road-crossing': { dc: 0.6, lv: 0.6, mv: 0.75, hv: 0.9, note: 'carriageway: ducted, 150 mm concrete surround, reinstated to the road spec' },
'track-crossing': { dc: 0.6, lv: 0.6, mv: 0.75, hv: 0.9, note: 'site access track: ducted' }
});
/** Everything below is an input with a stated default; `why` says where it comes from. */
export const DEFAULTS = Object.freeze({
soilKmW: 1.2, groundC: 15, // cleared generic rule: 1.2 K.m/W native, ground at 15 C (IEC 60287 input)
surroundKmW: 1.0, // cleared generic rule: 1.0 K.m/W backfill (used in the sensitivity case only)
bedMm: 75, surroundAboveMm: 75, // typical UK practice: 75 mm bed and 75 mm over the top cable or duct
tapeAboveMm: 200, // cleared project rule: warning tape 200 mm above the service
tileAboveMm: 75, // typical practice: tiles on the surround over MV / HV
sideClearMm: 100, // typical practice: wall to the nearest cable or duct
minGapMm: 75, // typical practice: clear gap between circuits so bedding can be placed
layerGapMm: 150, // two layers: clear vertical gap (a bedding layer between them)
topsoilM: 0.3, // topsoil stripped and kept apart (cleared rule: topsoil and subsoil apart)
bulking: 1.3, // loose spoil over bank volume (cleared generic rule)
maxWidthM: 1.5, // one-pass bucket width that a single trench is allowed (design choice)
betweenTrenchesM: 1.0, // undisturbed ground left between parallel trenches (design choice)
ductWallKmW: 3.5, // polyethylene duct wall (typical material property)
ductBoreRatio: 0.9, // bore over outside diameter of the duct (typical twin-wall / SDR)
emissivity: 0.9, airRiseK: 10, // duct air gap: radiation at this emissivity plus natural convection at this rise
mu: 1, diffusivityM2h: 0.0018 // loss-load factor (1 = continuous; a solar day is lower) and soil diffusivity
});
// Metals: resistivity at 20 C (ohm.m) and temperature coefficient (physical constants), and the allowance that a
// real stranded conductor's resistance carries over the ideal (compaction, lay, the nominal area being a nominal).
export const METAL = Object.freeze({
al: { rho20: 2.8264e-8, alpha: 0.00403, density: 2703 },
cu: { rho20: 1.7241e-8, alpha: 0.00393, density: 8890 }
});
export const R_ALLOWANCE = Object.freeze({ compact: 1.10, flexible: 1.18, why: 'conservative allowance over the ideal resistivity, own estimate; check against the maker\'s data' });
// ---------------------------------------------------------------------------------------------------------------
// One cable: build, losses, internal thermal resistances.
/**
* cable(c) -> the cable's physical build and per-metre electrical figures at its maximum temperature.
* c: { material: 'al'|'cu', areaMm2, odMm, kind: 'ac'|'dc', maxC, insulationMm?, sheathMm?, flexible?, u0Kv?,
* screenMm2? (MV/HV wire screen, copper), bonding? ('both-ends'|'single-point') }
*/
export function cable(c) {
const m = METAL[c.material || 'al'], A = c.areaMm2 * 1e-6;
const dc = Math.sqrt(4 * c.areaMm2 / (PI * (c.flexible ? 0.78 : 0.9))) / 1000; // conductor diameter, m
const od = c.odMm / 1000;
// Insulation and sheath split the radial build unless given; the split only moves T1 against T3 a little.
const radial = (od - dc) / 2;
const scr = c.screenMm2 ? 1.0e-3 : 0;
const ti = c.insulationMm ? c.insulationMm / 1000 : 0.55 * (radial - scr);
const ts = c.sheathMm ? c.sheathMm / 1000 : Math.max(0.0005, radial - scr - ti);
const maxC = c.maxC ?? 90;
const rdc = m.rho20 / A * (c.flexible ? R_ALLOWANCE.flexible : R_ALLOWANCE.compact) * (1 + m.alpha * (maxC - 20));
let R = rdc, lambda1 = 0, Wd = 0;
if (c.kind !== 'dc') {
// Skin and proximity (IEC 60287-1-1 method), round stranded conductor, factors taken as 1 (conservative).
const f = c.freq || 50, x4 = (8 * PI * f / rdc * 1e-7) ** 2, y = x4 / (192 + 0.8 * x4);
const s = c.spacingM || od, r = dc / s;
const yp = y * r * r * (0.312 * r * r + 1.18 / (y + 0.27));
R = rdc * (1 + y + yp);
if (c.screenMm2) {
// Circulating-current screen loss, both ends bonded, trefoil (IEC 60287-1-1 method).
const w = 2 * PI * f, rs = 1.7241e-8 / (c.screenMm2 * 1e-6) * (1 + 0.00393 * (maxC - 30));
const dMean = od - 2 * ts - scr, X = 2 * w * 1e-7 * Math.log(2 * s / dMean);
lambda1 = c.bonding === 'single-point' ? 0 : (rs / R) / (1 + (rs / X) ** 2);
const di = dc + 2 * ti, Cap = (c.epsR || 2.5) / (18 * Math.log(di / dc)) * 1e-9;
Wd = w * Cap * ((c.u0Kv || 19) * 1000) ** 2 * (c.tanDelta || 0.004);
}
}
const T1 = (c.insulationKmW || 3.5) / (2 * PI) * Math.log(1 + 2 * ti / dc);
const T3 = (c.sheathKmW || 5.0) / (2 * PI) * Math.log(1 + 2 * ts / (od - 2 * ts));
// Weight per metre for pulling: metal plus polymer (0.95 t/m3), roughly.
const weightNm = 9.81 * (m.density * A + 950 * (PI / 4) * (od * od - dc * dc));
return { dc, od, R, rdc, lambda1, Wd, T1, T3, maxC, weightNm, areaMm2: c.areaMm2, material: c.material || 'al' };
}
// Air gap between a cable (or a bundle of equivalent diameter De) and the duct bore: 1 / (pi De h), with
// h = radiation (4 e sigma Tm^3) + laminar natural convection of a horizontal cylinder, 1.32 (dT / De)^0.25
// (textbook heat-transfer correlation). Own estimate, not the standard's duct constants; on the high side of them.
export function ductAirT(DeM, p = DEFAULTS, meanC = 60) {
const Tm = meanC + 273.15, hr = 4 * p.emissivity * 5.67e-8 * Tm ** 3, hc = 1.32 * (p.airRiseK / DeM) ** 0.25;
return 1 / (PI * DeM * (hr + hc));
}
// ---------------------------------------------------------------------------------------------------------------
// Layout: every conductor and duct placed in the section (u across, z down positive = depth below ground).
/**
* A circuit: { id, cls: 'dc'|'lv'|'mv'|'hv', cable: {..cable()}, loadA, formation: 'trefoil'|'flat'|'duct-bundle',
* ducted?: true, ductOdMm?, perDuct? (cables in one duct, for a bundle), phases? (3 AC, 2 DC) }
* footprint(c) -> { w, h } of one circuit's group in metres.
*/
export function footprint(ct) {
const od = ct.cable.odMm / 1000, D = ct.ducted || ct.formation === 'duct-bundle' ? ct.ductOdMm / 1000 : od;
if (ct.formation === 'duct-bundle') return { w: D, h: D };
if (ct.formation === 'flat') return { w: 3 * D, h: D };
return { w: 2 * D, h: D * (1 + SQ3 / 2) };
}
/**
* place(circuits, g) -> { sources, width, depthToTop, depthBottom }
* g: { cover, gapM (clear gap between adjacent circuit groups), layers: 1|2, trenches: 1..n, betweenM,
* sideClearM, layerGapM }
* A source is one heat line in the ground: { circuit, u, z, De, W (per unit load^2), ... }.
*/
export function place(circuits, g) {
const p = { ...DEFAULTS, ...g };
const side = (g.sideClearM ?? p.sideClearMm / 1000), gap = g.gapM ?? p.minGapMm / 1000;
const nT = g.trenches || 1, nL = g.layers || 1;
// Split circuits over trenches (round-robin keeps each trench alike), then over layers.
const perTrench = Array.from({ length: nT }, () => []);
circuits.forEach((c, i) => perTrench[Number.isInteger(c.trench) ? Math.min(nT - 1, c.trench) : i % nT].push(c)); // c.trench: fixed
const trenches = [];
let u0 = 0;
const sources = [];
for (const [ti, list] of perTrench.entries()) {
const gapT = g.gaps?.[ti] ?? gap, layers = Array.from({ length: nL }, () => []);
list.forEach((c, i) => layers[Math.floor(i * nL / list.length)].push(c));
const rowW = row => row.reduce((s, c) => s + footprint(c).w, 0) + Math.max(0, row.length - 1) * gapT;
const width = Math.max(...layers.map(rowW)) + 2 * side;
const hMax = row => Math.max(0, ...row.map(c => footprint(c).h));
let zTop = g.covers?.[ti] ?? g.cover;
layers.forEach((row, li) => {
let u = u0 + side + (width - 2 * side - rowW(row)) / 2;
for (const c of row) {
const f = footprint(c), od = c.cable.odMm / 1000;
const D = c.ducted || c.formation === 'duct-bundle' ? c.ductOdMm / 1000 : od;
const zb = zTop + hMax(row); // underside of this layer
const at = [];
if (c.formation === 'duct-bundle') at.push([u + D / 2, zb - D / 2]);
else if (c.formation === 'flat') for (let k = 0; k < 3; k++) at.push([u + D / 2 + k * D, zb - D / 2]);
else at.push([u + D / 2, zb - D / 2], [u + 1.5 * D, zb - D / 2], [u + D, zb - D / 2 - SQ3 / 2 * D]);
at.forEach(([uu, zz], k) => sources.push({ circuit: c.id, phase: k + 1, u: uu, z: zz, D, c, layer: li }));
u += f.w + gapT;
}
zTop += hMax(row) + (p.layerGapMm / 1000);
});
const depthBottom = zTop - p.layerGapMm / 1000;
trenches.push({ u0, width, depthBottom });
u0 += width + (g.betweens?.[ti] ?? g.betweenM ?? p.betweenTrenchesM);
}
return { sources, trenches, corridor: trenches.at(-1).u0 + trenches.at(-1).width };
}
// ---------------------------------------------------------------------------------------------------------------
// Temperatures: every source heats every other through the ground (image method); rating by the hottest cable.
/**
* temperatures(layout, env) -> { hottest, factor, perCircuit }
* factor: how much every load could be multiplied by (all together) before the hottest conductor reaches its
* maximum. factor < 1 means the layout is overloaded; the circuit's rating in this layout is factor x load.
*/
export function temperatures(layout, env = {}) {
const p = { ...DEFAULTS, ...env }, rho = p.soilKmW, S = layout.sources;
// Heat per source at the design load (W/m): all cables in it (a duct bundle holds perDuct cables).
const heat = S.map(s => {
const k = s.c.formation === 'duct-bundle' ? (s.c.perDuct || 1) : 1, cb = s.cb || (s.cb = cable({ ...s.c.cable, spacingM: s.D }));
const Wcond = s.c.loadA ** 2 * cb.R, Wc = Wcond * (1 + cb.lambda1);
return { Wcond, Wc, W: k * Wc, Wd: k * cb.Wd, cb, k };
});
let worst = { factor: Infinity };
const perCircuit = new Map();
S.forEach((s, i) => {
const h = heat[i], cb = h.cb, De = s.D;
// Ground: self (a line source at depth z of diameter De) plus every other source by its image.
const u = 2 * s.z / De, self = rho / (2 * PI) * Math.log(u + Math.sqrt(u * u - 1)), selfL = cyclicSelf(self, s.z, De, p);
let mutualL = 0, mutualD = 0;
S.forEach((o, j) => {
if (j === i) return;
const d = Math.hypot(o.u - s.u, o.z - s.z), d2 = Math.hypot(o.u - s.u, o.z + s.z);
const f = rho / (2 * PI) * Math.log(d2 / Math.max(d, 1e-6));
mutualL += p.mu * heat[j].W * f; mutualD += heat[j].Wd * f;
});
// Duct: air gap and duct wall carry this duct's own heat; a bundle's cable also sits in its neighbours' heat.
let inner = 0, innerD = 0;
if (s.c.ducted || s.c.formation === 'duct-bundle') {
const n = h.k, DeEq = n > 1 ? 1.15 * Math.sqrt(n) * cb.od : cb.od;
const Tair = ductAirT(DeEq, p), Twall = p.ductWallKmW / (2 * PI) * Math.log(1 / p.ductBoreRatio);
inner = h.W * (Tair + Twall); innerD = h.Wd * (Tair + Twall);
}
// Conductor rise at the design load, split into the part that scales with load^2 and the dielectric part.
const load = h.Wcond * cb.T1 + h.Wc * cb.T3 + inner + h.W * selfL + mutualL; // screen loss enters outside T1
const diel = cb.Wd * (0.5 * cb.T1 + cb.T3) + innerD + h.Wd * self + mutualD;
const allow = cb.maxC - p.groundC - diel;
const factor = allow > 0 ? Math.sqrt(allow / load) : 0;
const temp = p.groundC + diel + load;
const cur = perCircuit.get(s.c.id);
if (!cur || factor < cur.factor) perCircuit.set(s.c.id, { factor, temp, amps: factor * s.c.loadA, loadA: s.c.loadA });
if (factor < worst.factor) worst = { factor, temp, circuit: s.c.id, phase: s.phase, u: s.u, z: s.z };
});
return { hottest: worst, factor: worst.factor, perCircuit: [...perCircuit.entries()].map(([id, v]) => ({ id, ...v })) };
}
// Daily cycle (the published 1957 loss-factor approach; IEC 60853-2 is the standard route, cited only): the soil
// within Dx of the cable sees the peak loss, beyond it the mean (mu x peak). mu = 1 gives the steady value back.
function cyclicSelf(self, z, De, p) {
if (!(p.mu < 1)) return self;
const Dx = 1.02 * Math.sqrt(24 * p.diffusivityM2h), rho = p.soilKmW;
return Dx <= De ? p.mu * self : rho / (2 * PI) * (Math.log(Dx / De) + p.mu * Math.log(4 * z / Dx));
}
// ---------------------------------------------------------------------------------------------------------------
// Solve: the smallest gap that meets every rating, for one arrangement.
/**
* solveGap(circuits, g, env) -> { ok, gapM, layout, thermal } : the smallest clear gap between circuit groups
* (at least the practical minimum) at which every conductor is within its limit. ok=false when even a very
* wide gap fails (the circuits in that arrangement are too hot: another arrangement or a larger conductor).
*/
export function solveGap(circuits, g, env = {}, { maxGapM = 3 } = {}) {
const p = { ...DEFAULTS, ...env, ...g }, min = g.minGapM ?? p.minGapMm / 1000;
const at = gap => { const layout = place(circuits, { ...g, gapM: gap }); return { layout, thermal: temperatures(layout, env) }; };
let lo = at(min);
if (lo.thermal.factor >= 1) return { ok: true, gapM: min, ...lo };
let hi = at(maxGapM);
if (hi.thermal.factor < 1) return { ok: false, gapM: maxGapM, ...hi };
let a = min, b = maxGapM;
for (let k = 0; k < 40 && b - a > 0.005; k++) { const m = (a + b) / 2; if (at(m).thermal.factor >= 1) b = m; else a = m; }
// Up to the 25 mm grid, then down while the step below still rates: the smallest grid gap that meets the rating.
let gap = Math.max(min, Math.ceil(b * 40 - 1e-9) / 40), best = at(gap);
for (let q = Math.round(gap * 40 - 1) / 40, t; q >= min - 1e-9 && (t = at(q)).thermal.factor >= 1; q = Math.round(q * 40 - 1) / 40) { gap = q; best = t; }
return { ok: true, gapM: gap, ...best };
}
// ---------------------------------------------------------------------------------------------------------------
// The section: layer stack, markers and volumes per metre for a solved layout.
/** section(solved, env) -> per-trench stack and per-metre volumes (bank m3/m), plus a readout. */
export function section(solved, circuits, env = {}) {
const p = { ...DEFAULTS, ...env }, L = solved.layout, cls = worstClass(circuits);
const bed = p.bedMm / 1000;
return L.trenches.map((t, k) => {
const mine = L.sources.filter(s => s.u >= t.u0 && s.u <= t.u0 + t.width);
const topItem = Math.min(...mine.map(s => s.z - s.D / 2)), bottomItem = Math.max(...mine.map(s => s.z + s.D / 2));
const depth = round(bottomItem + bed, 0.025), w = round(t.width, 0.05);
const surroundTop = Math.max(0.15, topItem - p.surroundAboveMm / 1000);
const itemsArea = mine.reduce((s, x) => s + PI * x.D * x.D / 4, 0);
const dug = w * depth; // vertical sides; batter adds in `ground`
const surround = w * (depth - surroundTop) - itemsArea; // bed + surround material
const topsoil = w * Math.min(p.topsoilM, surroundTop);
const backfill = dug - surround - itemsArea; // includes the topsoil reinstated
const tile = cls === 'mv' || cls === 'hv';
const markers = [
...(tile ? [{ kind: 'tile', depth: round(surroundTop, 0.005), width: round(w - 0.1, 0.05) }] : []),
// Wide trenches carry several tapes side by side so a later dig meets one wherever it opens the ground
// (IEC TS 62738 7.3.4.9.2 asks for this; one per 0.5 m of cable width is our own rule).
{ kind: 'tape', depth: round(Math.max(0.15, topItem - p.tapeAboveMm / 1000), 0.005), count: Math.max(1, Math.ceil((w - 2 * p.sideClearMm / 1000) / 0.5)),
note: 'yellow, service named along it' }
];
// Deeper than 1.2 m the sides are supported or battered (general excavation practice, HSE HSG47; cleared rule).
const support = depth > 1.2 ? 'support (trench box or sheets) or batter the sides' : 'none needed in firm ground; check on site';
return {
trench: k + 1, width: w, depth, coverToTop: round(topItem, 0.005), bed, surroundTop: round(surroundTop, 0.005),
circuits: new Set(mine.map(s => s.circuit)).size, markers, support,
perMetre: { dugBank: r3(dug), spoilLoose: r3(dug * p.bulking), topsoil: r3(topsoil), beddingSurround: r3(surround),
backfillReuse: r3(backfill), ductsOrCables: r3(itemsArea) }
};
});
}
function worstClass(circuits) { const o = ['dc', 'lv', 'mv', 'hv']; return circuits.map(c => c.cls).sort((a, b) => o.indexOf(b) - o.indexOf(a))[0]; }
const round = (x, q) => Math.ceil(x / q - 1e-9) * q;
const r3 = x => Math.round(x * 1000) / 1000;
// ---------------------------------------------------------------------------------------------------------------
// Options for one case: one trench, split, two layers, larger conductor; each solved and sized.
/**
* designOptions({ circuits, landUse, env, upsize }) -> { cover, options: [...] }
* upsize(circuit) -> a circuit with a larger conductor (optional).
*/
export function designOptions({ circuits, landUse = 'farmland', env = {}, upsize = null, maxTrenches = 6, coverM = null }) {
const p = { ...DEFAULTS, ...env }, cls = worstClass(circuits), cover = coverM ?? (COVER[landUse] || COVER.farmland)[cls];
const opts = [];
const add = (name, cs, g, extra = {}) => {
const s = solveGap(cs, { cover, ...g }, env);
const sec = section(s, cs, env);
const widest = Math.max(...sec.map(t => t.width));
opts.push({
name, ok: s.ok, fitsOneBucket: widest <= p.maxWidthM + 1e-9, gapM: s.gapM, trenches: sec.length,
layers: g.layers || 1, corridorM: round(s.layout.corridor, 0.05), widestM: widest, deepestM: Math.max(...sec.map(t => t.depth)),
hottestC: r1(s.thermal.hottest.temp), ratingFactor: r3(s.thermal.factor),
perMetre: sumPerMetre(sec), sections: sec, ...extra
});
};
add('one trench, one layer', circuits, { trenches: 1, layers: 1 });
add('one trench, two layers', circuits, { trenches: 1, layers: 2 });
for (let n = 2; n <= maxTrenches; n++) {
add(`${n} trenches side by side, ${p.betweenTrenchesM} m apart`, circuits, { trenches: n, layers: 1 });
if (opts[opts.length - 1].fitsOneBucket && opts[opts.length - 1].ok) break;
}
if (upsize) {
const big = circuits.map(upsize);
add(`larger conductor (${big[0].cable.areaMm2} mm2 ${big[0].cable.material}), one trench`, big, { trenches: 1, layers: 1 }, { conductor: big[0].cable.areaMm2 });
for (let n = 2; n <= maxTrenches; n++) {
add(`larger conductor, ${n} trenches`, big, { trenches: n, layers: 1 }, { conductor: big[0].cable.areaMm2 });
if (opts[opts.length - 1].fitsOneBucket && opts[opts.length - 1].ok) break;
}
}
return { cover, landUse, coverBasis: COVER.basis, cls, options: opts };
}
function sumPerMetre(sec) {
const o = {};
for (const t of sec) for (const [k, v] of Object.entries(t.perMetre)) o[k] = r3((o[k] || 0) + v);
return o;
}
const r1 = x => Math.round(x * 10) / 10;
/** Check a drawn section as it stands: circuits at a centre pitch in one row at a cover. */
export function checkDrawn(circuits, { cover, pitchM }, env = {}) {
const w = footprint(circuits[0]).w;
const layout = place(circuits, { cover, gapM: Math.max(0, pitchM - w), trenches: 1, layers: 1 });
return { layout, thermal: temperatures(layout, env) };
}
// ---------------------------------------------------------------------------------------------------------------
// Along the route: draw pits, pull sections, joint bays.
export const PULL = Object.freeze({
friction: 0.35, // cable on a lubricated PE duct (typical 0.2-0.5; own conservative choice)
frictionDirect: 0.5, // cable on rollers in an open trench, allowing for roller drag (own choice)
tensileNmm2: { al: 30, cu: 50 },// cleared generic rule: pulling-eye limit per mm2 of conductor
sidewallNm: 3000, // bend sidewall pressure limit, N per m of bend radius (typical for XLPE, to agree)
pitAtTurnDeg: 10, // a change of direction above this gets a draw pit (cleared rule: every change of direction)
drumM: 800, // one cable length on a drum (input: depends on size and drum; joint bays at this)
jointBay: { lengthM: 4.0, extraWidthM: 0.6 }, // typical joint bay for LV/MV single-core (design choice)
pit: { internalM: [1.2, 0.9], covers: 'D400 where vehicles run, C250 elsewhere (cleared rule)' },
basis: 'capstan relation; limits are inputs and the cable maker\'s figures govern'
});
/**
* pullPlan(route, circuit, { ducted }) -> { pits, jointBays, sections }. circuit.maxPullN (or pull.maxPullN): the maker's absolute cap, N.
* route: [[x, y], ...] in metres. A pit at every change of direction over pitAtTurnDeg (ducted runs; the cable
* then pulls straight through each), and more wherever the tension or the drum length would be exceeded. Pulls are
* worked from one end; a bend inside a pull multiplies the tension by e^(mu theta).
*/
export function pullPlan(route, ct, { ducted = true, pull = PULL } = {}) {
const cb = cable(ct.cable), phases = ct.formation === 'duct-bundle' ? (ct.perDuct || 1) : ducted ? 1 : 3;
const mu = ducted ? pull.friction : pull.frictionDirect, wN = cb.weightNm * phases;
const stressN = pull.tensileNmm2[cb.material] * ct.cable.areaMm2 * phases, capN = ct.maxPullN ?? pull.maxPullN;
const Tmax = capN > 0 ? Math.min(stressN, capN) : stressN; // the conductor stress limit, capped by the maker's maximum
// Walk the polyline: segment lengths and turn angles.
const segs = [];
for (let i = 1; i < route.length; i++) {
const [ax, ay] = route[i - 1], [bx, by] = route[i];
segs.push({ len: Math.hypot(bx - ax, by - ay), dir: Math.atan2(by - ay, bx - ax) });
}
const turn = i => { let d = Math.abs(segs[i].dir - segs[i - 1].dir) % (2 * PI); return d > PI ? 2 * PI - d : d; };
const pits = [], jointBays = [], sections = [];
let T = 0, run = 0, drum = 0, chain = 0, start = 0;
const cut = (at, why) => { sections.push({ from: r1(start), to: r1(at), lengthM: r1(at - start), tensionN: Math.round(T), limitN: Math.round(Tmax) }); start = at; T = 0; run = 0; };
segs.forEach((s, i) => {
if (i > 0) {
const th = turn(i);
if (ducted && th * 180 / PI > pull.pitAtTurnDeg) { pits.push({ at: r1(chain), why: 'change of direction' }); cut(chain, 'turn'); }
else T *= Math.exp(mu * th);
}
// Along the segment in 1 m steps so the pit or joint lands where the limit is reached.
for (let x = 0; x < s.len; x += 1) {
const dl = Math.min(1, s.len - x);
if (T + mu * wN * dl > Tmax) { if (ducted) pits.push({ at: r1(chain), why: 'pulling tension' }); else jointBays.push({ at: r1(chain), why: 'pulling tension' }); cut(chain); }
if (drum + dl > pull.drumM) { jointBays.push({ at: r1(chain), why: 'drum length' }); drum = 0; if (!ducted) cut(chain); }
T += mu * wN * dl; chain += dl; run += dl; drum += dl;
}
});
cut(chain);
// A joint must sit in a pit or joint bay, never direct-buried (cleared rule); in a ducted run it goes in a pit.
return { lengthM: r1(chain), pits, jointBays, sections, limitN: Math.round(Tmax), weightNm: r3(wN), friction: mu };
}