updates
This commit is contained in:
57
test/basic/hydraulic-model.basic.test.js
Normal file
57
test/basic/hydraulic-model.basic.test.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { ValveHydraulicModel } = require('../../src/hydraulicModel');
|
||||
|
||||
test('hydraulic model gas branch keeps existing formula when not choked', () => {
|
||||
const model = new ValveHydraulicModel({ serviceType: 'gas', gasChokedRatioLimit: 0.9 });
|
||||
const result = model.calculateDeltaPMbar({
|
||||
qM3h: 36,
|
||||
kv: 10,
|
||||
downstreamGaugeMbar: 500,
|
||||
rho: 1.204,
|
||||
tempK: 293.15,
|
||||
});
|
||||
|
||||
const p2AbsBar = (500 / 1000) + 1.01325;
|
||||
const expectedDeltaPMbar = ((36 ** 2 * 1.204 * 293.15) / (514 ** 2 * 10 ** 2 * p2AbsBar)) * 1000;
|
||||
|
||||
assert.ok(result);
|
||||
assert.ok(Math.abs(result.deltaPMbar - expectedDeltaPMbar) < 0.05, `expected ${expectedDeltaPMbar}, got ${result.deltaPMbar}`);
|
||||
assert.equal(result.details.isChoked, false);
|
||||
});
|
||||
|
||||
test('hydraulic model gas branch applies choked-flow cap', () => {
|
||||
const model = new ValveHydraulicModel({ serviceType: 'gas', gasChokedRatioLimit: 0.2 });
|
||||
const result = model.calculateDeltaPMbar({
|
||||
qM3h: 1000,
|
||||
kv: 1,
|
||||
downstreamGaugeMbar: 500,
|
||||
rho: 1.204,
|
||||
tempK: 293.15,
|
||||
});
|
||||
|
||||
const p2AbsBar = (500 / 1000) + 1.01325;
|
||||
const expectedCappedDeltaPMbar = p2AbsBar * 0.2 * 1000;
|
||||
|
||||
assert.ok(result);
|
||||
assert.equal(result.details.isChoked, true);
|
||||
assert.ok(Math.abs(result.deltaPMbar - expectedCappedDeltaPMbar) < 0.0001, `expected ${expectedCappedDeltaPMbar}, got ${result.deltaPMbar}`);
|
||||
});
|
||||
|
||||
test('hydraulic model liquid branch uses liquid Kv equation', () => {
|
||||
const model = new ValveHydraulicModel({ serviceType: 'liquid' });
|
||||
const result = model.calculateDeltaPMbar({
|
||||
qM3h: 100,
|
||||
kv: 50,
|
||||
downstreamGaugeMbar: 500,
|
||||
rho: 998,
|
||||
tempK: 293.15,
|
||||
});
|
||||
|
||||
const expectedDeltaPMbar = (((100 / 50) ** 2) * (998 / 1000)) * 1000;
|
||||
|
||||
assert.ok(result);
|
||||
assert.equal(result.details.isChoked, false);
|
||||
assert.ok(Math.abs(result.deltaPMbar - expectedDeltaPMbar) < 0.0001, `expected ${expectedDeltaPMbar}, got ${result.deltaPMbar}`);
|
||||
});
|
||||
124
test/integration/fluid-compatibility.integration.test.js
Normal file
124
test/integration/fluid-compatibility.integration.test.js
Normal file
@@ -0,0 +1,124 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const EventEmitter = require('events');
|
||||
|
||||
const Valve = require('../../src/specificClass');
|
||||
|
||||
function buildValve({ runtimeOptions = {} } = {}) {
|
||||
return new Valve(
|
||||
{
|
||||
general: {
|
||||
name: 'valve-fluid-test',
|
||||
logging: { enabled: false, logLevel: 'error' },
|
||||
},
|
||||
asset: {
|
||||
supplier: 'binder',
|
||||
category: 'valve',
|
||||
type: 'control',
|
||||
model: 'ECDV',
|
||||
unit: 'm3/h',
|
||||
},
|
||||
functionality: {
|
||||
positionVsParent: 'atEquipment',
|
||||
},
|
||||
},
|
||||
{
|
||||
general: {
|
||||
logging: { enabled: false, logLevel: 'error' },
|
||||
},
|
||||
movement: { speed: 1 },
|
||||
time: { starting: 0, warmingup: 0, stopping: 0, coolingdown: 0 },
|
||||
},
|
||||
runtimeOptions
|
||||
);
|
||||
}
|
||||
|
||||
function buildFluidSource({
|
||||
id,
|
||||
softwareType,
|
||||
serviceType = null,
|
||||
status = 'resolved',
|
||||
}) {
|
||||
const emitter = new EventEmitter();
|
||||
let contract = { status, serviceType };
|
||||
return {
|
||||
emitter,
|
||||
config: {
|
||||
general: { id, name: id },
|
||||
functionality: { softwareType },
|
||||
asset: {
|
||||
serviceType: serviceType || undefined,
|
||||
},
|
||||
},
|
||||
getFluidContract() {
|
||||
return { ...contract };
|
||||
},
|
||||
setFluidContract(next) {
|
||||
contract = { ...contract, ...next };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('valve flags mismatch for direct machine source with incompatible fluid', () => {
|
||||
const valve = buildValve({ runtimeOptions: { serviceType: 'gas' } });
|
||||
const source = buildFluidSource({
|
||||
id: 'machine-1',
|
||||
softwareType: 'machine',
|
||||
serviceType: 'liquid',
|
||||
});
|
||||
|
||||
assert.equal(valve.registerChild(source, 'machine'), true);
|
||||
const compatibility = valve.getFluidCompatibility();
|
||||
assert.equal(compatibility.status, 'mismatch');
|
||||
assert.equal(compatibility.expectedServiceType, 'gas');
|
||||
assert.equal(compatibility.receivedServiceType, 'liquid');
|
||||
|
||||
valve.destroy();
|
||||
});
|
||||
|
||||
test('valve flags conflict when grouped upstream sources expose mixed fluids', () => {
|
||||
const valve = buildValve();
|
||||
const machine = buildFluidSource({
|
||||
id: 'machine-1',
|
||||
softwareType: 'machine',
|
||||
serviceType: 'liquid',
|
||||
});
|
||||
const group = buildFluidSource({
|
||||
id: 'vgc-1',
|
||||
softwareType: 'valvegroupcontrol',
|
||||
serviceType: 'gas',
|
||||
});
|
||||
|
||||
assert.equal(valve.registerChild(machine, 'machine'), true);
|
||||
assert.equal(valve.registerChild(group, 'valvegroupcontrol'), true);
|
||||
|
||||
const compatibility = valve.getFluidCompatibility();
|
||||
assert.equal(compatibility.status, 'conflict');
|
||||
assert.deepEqual(new Set(compatibility.upstreamServiceTypes), new Set(['liquid', 'gas']));
|
||||
|
||||
valve.destroy();
|
||||
});
|
||||
|
||||
test('valve updates compatibility when upstream group fluid contract changes', async () => {
|
||||
const valve = buildValve({ runtimeOptions: { serviceType: 'gas' } });
|
||||
const group = buildFluidSource({
|
||||
id: 'vgc-1',
|
||||
softwareType: 'valvegroupcontrol',
|
||||
serviceType: 'gas',
|
||||
});
|
||||
|
||||
assert.equal(valve.registerChild(group, 'valvegroupcontrol'), true);
|
||||
assert.equal(valve.getFluidCompatibility().status, 'match');
|
||||
|
||||
group.setFluidContract({ serviceType: 'liquid' });
|
||||
group.emitter.emit('fluidContractChange');
|
||||
|
||||
// Event handlers run synchronously; await microtask for deterministic test sequencing.
|
||||
await Promise.resolve();
|
||||
|
||||
const compatibility = valve.getFluidCompatibility();
|
||||
assert.equal(compatibility.status, 'mismatch');
|
||||
assert.equal(compatibility.receivedServiceType, 'liquid');
|
||||
|
||||
valve.destroy();
|
||||
});
|
||||
117
test/integration/valve-physics-and-curve.integration.test.js
Normal file
117
test/integration/valve-physics-and-curve.integration.test.js
Normal file
@@ -0,0 +1,117 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const Valve = require('../../src/specificClass');
|
||||
const supplierCurve = require('../../../generalFunctions/datasets/assetData/curves/ECDV.json');
|
||||
|
||||
function buildValve({ asset = {}, runtimeOptions = {} } = {}) {
|
||||
return new Valve(
|
||||
{
|
||||
general: {
|
||||
name: 'valve-test',
|
||||
logging: { enabled: false, logLevel: 'error' },
|
||||
},
|
||||
asset: {
|
||||
supplier: 'binder',
|
||||
category: 'valve',
|
||||
type: 'control',
|
||||
model: 'ECDV',
|
||||
unit: 'm3/h',
|
||||
...asset,
|
||||
},
|
||||
functionality: {
|
||||
positionVsParent: 'atEquipment',
|
||||
},
|
||||
},
|
||||
{
|
||||
general: {
|
||||
logging: { enabled: false, logLevel: 'error' },
|
||||
},
|
||||
movement: { speed: 1 },
|
||||
time: { starting: 0, warmingup: 0, stopping: 0, coolingdown: 0 },
|
||||
},
|
||||
runtimeOptions
|
||||
);
|
||||
}
|
||||
|
||||
test('valve selects supplier curve and predicts Kv from supplier data', () => {
|
||||
const valve = buildValve();
|
||||
|
||||
valve.updatePressure('measured', 500, 'downstream', 'mbar');
|
||||
valve.updateFlow('predicted', 100, 'downstream', 'm3/h');
|
||||
valve.state.movementManager.currentPosition = 50;
|
||||
valve.updatePosition();
|
||||
|
||||
const expectedKv = supplierCurve['1.204']['125'].y[5];
|
||||
assert.equal(valve.curveSelection.densityKey, 1.204);
|
||||
assert.equal(valve.curveSelection.diameterKey, 125);
|
||||
assert.ok(Math.abs(valve.kv - expectedKv) < 0.01, `expected Kv ${expectedKv}, got ${valve.kv}`);
|
||||
|
||||
valve.destroy();
|
||||
});
|
||||
|
||||
test('valve deltaP math uses converted flow units in formula path', () => {
|
||||
const valve = buildValve();
|
||||
|
||||
valve.kv = 10;
|
||||
valve.rho = 1.204;
|
||||
valve.T = 293.15;
|
||||
valve.updatePressure('measured', 500, 'downstream', 'mbar');
|
||||
|
||||
// 10 l/s equals 36 m3/h; formula path should use 36 m3/h.
|
||||
valve.updateFlow('predicted', 10, 'downstream', 'l/s');
|
||||
|
||||
const downstreamAbsBar = (500 / 1000) + 1.01325;
|
||||
const qM3h = 36;
|
||||
const expectedDeltaPMbar = ((qM3h ** 2 * valve.rho * valve.T) / (514 ** 2 * valve.kv ** 2 * downstreamAbsBar)) * 1000;
|
||||
const actualDeltaP = valve.measurements
|
||||
.type('pressure')
|
||||
.variant('predicted')
|
||||
.position('delta')
|
||||
.getCurrentValue('mbar');
|
||||
|
||||
assert.ok(Number.isFinite(actualDeltaP), 'deltaP should be finite');
|
||||
assert.ok(Math.abs(actualDeltaP - expectedDeltaPMbar) < 0.05, `expected ${expectedDeltaPMbar}, got ${actualDeltaP}`);
|
||||
|
||||
valve.destroy();
|
||||
});
|
||||
|
||||
test('valve liquid mode uses liquid Kv equation through update loop', () => {
|
||||
const valve = buildValve({ runtimeOptions: { serviceType: 'liquid', fluidDensity: 998 } });
|
||||
|
||||
valve.kv = 50;
|
||||
valve.updatePressure('measured', 500, 'downstream', 'mbar');
|
||||
valve.updateFlow('predicted', 100, 'downstream', 'm3/h');
|
||||
|
||||
const expectedDeltaPMbar = (((100 / 50) ** 2) * (998 / 1000)) * 1000;
|
||||
const actualDeltaP = valve.measurements
|
||||
.type('pressure')
|
||||
.variant('predicted')
|
||||
.position('delta')
|
||||
.getCurrentValue('mbar');
|
||||
|
||||
assert.ok(Math.abs(actualDeltaP - expectedDeltaPMbar) < 0.01, `expected ${expectedDeltaPMbar}, got ${actualDeltaP}`);
|
||||
valve.destroy();
|
||||
});
|
||||
|
||||
test('valve gas mode applies choked cap in update loop', () => {
|
||||
const valve = buildValve({ runtimeOptions: { serviceType: 'gas', gasChokedRatioLimit: 0.2 } });
|
||||
|
||||
valve.kv = 1;
|
||||
valve.rho = 1.204;
|
||||
valve.T = 293.15;
|
||||
valve.updatePressure('measured', 500, 'downstream', 'mbar');
|
||||
valve.updateFlow('predicted', 1000, 'downstream', 'm3/h');
|
||||
|
||||
const downstreamAbsBar = (500 / 1000) + 1.01325;
|
||||
const expectedDeltaPMbar = downstreamAbsBar * 0.2 * 1000;
|
||||
const actualDeltaP = valve.measurements
|
||||
.type('pressure')
|
||||
.variant('predicted')
|
||||
.position('delta')
|
||||
.getCurrentValue('mbar');
|
||||
|
||||
assert.ok(Math.abs(actualDeltaP - expectedDeltaPMbar) < 0.0001, `expected ${expectedDeltaPMbar}, got ${actualDeltaP}`);
|
||||
assert.equal(valve.hydraulicDiagnostics?.isChoked, true);
|
||||
valve.destroy();
|
||||
});
|
||||
Reference in New Issue
Block a user