Merge pull request 'dev-Rene' (#1) from dev-Rene into main

Reviewed-on: RnD/machineGroupControl#1
This commit is contained in:
2025-10-06 14:15:42 +00:00
3 changed files with 453 additions and 588 deletions

View File

@@ -1,548 +1,288 @@
// ...existing code...
const MachineGroup = require('./specificClass.js'); const MachineGroup = require('./specificClass.js');
const Machine = require('../../rotatingMachine/src/specificClass'); const Machine = require('../../rotatingMachine/src/specificClass');
const Measurement = require('../../measurement/src/specificClass'); const Measurement = require('../../measurement/src/specificClass');
const specs = require('../../generalFunctions/datasets/assetData/curves/hidrostal-H05K-S03R.json'); const specs = require('../../generalFunctions/datasets/assetData/curves/hidrostal-H05K-S03R.json');
function createBaseMachineConfig(machineNum, name, specs) { const stateConfig = { time:{starting:0,warmingup:0,stopping:0,coolingdown:0}, movement:{speed:1000,mode:"staticspeed"} };
return {
general: {
logging: { enabled: true, logLevel: "warn" },
name: name,
id: machineNum,
unit: "m3/h"
},
functionality: {
softwareType: "machine",
role: "rotationaldevicecontroller"
},
asset: {
category: "pump",
type: "centrifugal",
model: "hidrostal-h05k-s03r",
supplier: "hydrostal",
machineCurve: specs
},
mode: {
current: "auto",
allowedActions: {
auto: ["execSequence", "execMovement", "statusCheck"],
virtualControl: ["execMovement", "statusCheck"],
fysicalControl: ["statusCheck"]
},
allowedSources: {
auto: ["parent", "GUI"],
virtualControl: ["GUI"],
fysicalControl: ["fysical"]
}
},
sequences: {
startup: ["starting", "warmingup", "operational"],
shutdown: ["stopping", "coolingdown", "idle"],
emergencystop: ["emergencystop", "off"],
boot: ["idle", "starting", "warmingup", "operational"]
}
};
}
function createBaseMachineGroupConfig(name) {
return {
general: {
logging: { enabled: true, logLevel: "debug" },
name: name
},
functionality: {
softwareType: "machinegroup",
role: "groupcontroller"
},
scaling: {
current: "normalized"
},
mode: {
current: "optimalcontrol"
}
};
}
const ptConfig = { const ptConfig = {
general: { general:{ logging:{enabled:false,logLevel:"warn"}, name:"testpt", id:"pt-1", unit:"mbar" },
logging: { enabled: true, logLevel: "debug" }, functionality:{ softwareType:"measurement", role:"sensor" },
name: "testpt", asset:{ category:"sensor", type:"pressure", model:"testmodel", supplier:"vega", unit:"mbar" },
id: "0", scaling:{ absMin:0, absMax:4000 }
unit: "mbar", };
const testSuite = [];
const efficiencyComparisons = [];
function logPass(name, details="") {
const entry = { name, status:"PASS", details };
testSuite.push(entry);
console.log(`${name}${details ? `${details}` : ""}`);
}
function logFail(name, error) {
const entry = { name, status:"FAIL", details:error?.message || error };
testSuite.push(entry);
console.error(`${name}${entry.details}`);
}
function approxEqual(actual, expected, tolerancePct=1) {
const tolerance = (expected * tolerancePct) / 100;
return actual >= expected - tolerance && actual <= expected + tolerance;
}
async function sleep(ms){ return new Promise(resolve => setTimeout(resolve, ms)); }
function createMachineConfig(id,label) {
return {
general:{ logging:{enabled:false,logLevel:"warn"}, name:label, id, unit:"m3/h" },
functionality:{ softwareType:"machine", role:"rotationaldevicecontroller" },
asset:{ category:"pump", type:"centrifugal", model:"hidrostal-h05k-s03r", supplier:"hydrostal", machineCurve:specs },
mode:{
current:"auto",
allowedActions:{
auto:["execSequence","execMovement","flowMovement","statusCheck"],
virtualControl:["execMovement","statusCheck"],
fysicalControl:["statusCheck"]
},
allowedSources:{
auto:["parent","GUI"],
virtualControl:["GUI"],
fysicalControl:["fysical"]
}
}, },
functionality: { sequences:{
softwareType: "measurement", startup:["starting","warmingup","operational"],
role: "sensor" shutdown:["stopping","coolingdown","idle"],
}, emergencystop:["emergencystop","off"],
asset: { boot:["idle","starting","warmingup","operational"]
category: "sensor",
type: "pressure",
model: "testmodel",
supplier: "vega"
},
scaling: {
absMin: 0,
absMax: 4000,
} }
};
} }
const stateConfig = { async function bootstrapGroup() {
time:{starting:0, warmingup:0, stopping:0, coolingdown:0}, const groupCfg = {
movement:{speed:1000, mode:"staticspeed"}, general:{ logging:{enabled:false,logLevel:"warn"}, name:"testmachinegroup" },
functionality:{ softwareType:"machinegroup", role:"groupcontroller" },
scaling:{ current:"normalized" },
mode:{ current:"optimalcontrol" }
};
const mg = new MachineGroup(groupCfg);
const pt = new Measurement(ptConfig);
for (let idx=1; idx<=2; idx++){
const machine = new Machine(createMachineConfig(String(idx),`machine-${idx}`), stateConfig);
mg.childRegistrationUtils.registerChild(machine,"downstream");
machine.childRegistrationUtils.registerChild(pt,"downstream");
}
pt.calculateInput(1000);
await sleep(10);
return { mg, pt };
} }
async function sleep(ms) { function captureState(mg,label){
return new Promise(resolve => setTimeout(resolve, ms)); return {
label,
machines: Object.entries(mg.machines).map(([id,machine]) => ({
id,
state: machine.state.getCurrentState(),
position: machine.state.getCurrentPosition(),
predictedFlow: machine.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0,
predictedPower: machine.measurements.type("power").variant("predicted").position("upstream").getCurrentValue() || 0
})),
totals: {
flow: mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0,
power: mg.measurements.type("power").variant("predicted").position("upstream").getCurrentValue() || 0,
efficiency: mg.measurements.type("efficiency").variant("predicted").position("downstream").getCurrentValue() || 0
}
};
} }
function logMachineStates(mg, testName) { async function testNormalizedScaling(mg,pt){
console.log(`\n=== ${testName} ===`); const label = "Normalized scaling tracks expected flow";
console.log(`scaling: ${mg.scaling}, mode: ${mg.mode}`); try{
console.log(`flow range: ${mg.dynamicTotals.flow.min.toFixed(2)} - ${mg.dynamicTotals.flow.max.toFixed(2)} m3/h`);
Object.entries(mg.machines).forEach(([id, machine]) => {
const state = machine.state.getCurrentState();
const flow = machine.measurements?.type("flow")?.variant("predicted")?.position("downstream")?.getCurrentValue() || 0;
const power = machine.measurements?.type("power")?.variant("predicted")?.position("upstream")?.getCurrentValue() || 0;
const position = machine.state?.getCurrentPosition();
console.log(`machine ${id}: state=${state}, position=${position.toFixed(2)}, flow=${flow.toFixed(2)}, power=${power.toFixed(2)}`);
});
const totalFlow = mg.measurements?.type("flow")?.variant("predicted")?.position("downstream")?.getCurrentValue() || 0;
const totalPower = mg.measurements?.type("power")?.variant("predicted")?.position("upstream")?.getCurrentValue() || 0;
console.log(`total: flow=${totalFlow.toFixed(2)}, power=${totalPower.toFixed(2)}`);
// ADD THIS RETURN STATEMENT - this is what was missing!
return {
totalFlow,
totalPower,
efficiency: totalPower > 0 ? totalFlow / totalPower : 0
};
}
async function testPriorityVsOptimalEfficiency(mg, pt1) {
const demandIncrement = 1; // Test every 1% for detailed comparison
console.log("\n🔬 PRIORITY vs OPTIMAL CONTROL EFFICIENCY COMPARISON");
console.log("=".repeat(80));
const results = [];
console.log("\n📊 Testing OPTIMAL CONTROL (every 10% for speed)...");
mg.setScaling("normalized"); mg.setScaling("normalized");
mg.setMode("optimalcontrol"); const dynamic = mg.calcDynamicTotals();
const checkpoints = [0,10,25,50,75,100];
// Test every 10% for speed and give machines time to start for (const demand of checkpoints){
for (let demand = 0; demand <= 100; demand += demandIncrement) { await mg.handleInput("parent", demand);
try { pt.calculateInput(1400);
console.log(`\n🔄 Setting optimal demand to ${demand}%`); await sleep(20);
await mg.handleInput("parent", demand);
pt1.calculateInput(1400); const totals = mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0;
const expected = dynamic.flow.min + (demand/100)*(dynamic.flow.max - dynamic.flow.min);
const data = logMachineStates(mg, `optimal ${demand}%`); if(!approxEqual(totals, expected, 2)){
throw new Error(`Flow ${totals.toFixed(2)} outside expectation ${expected.toFixed(2)} @ ${demand}%`);
results.push({ }
demand,
optimal: {
flow: data.totalFlow,
power: data.totalPower,
efficiency: data.efficiency
}
});
console.log(`✅ optimal ${demand}%: flow=${data.totalFlow.toFixed(2)}, power=${data.totalPower.toFixed(2)}, eff=${data.efficiency.toFixed(4)}`);
} catch (err) {
console.error(`❌ error at optimal ${demand}%:`, err.message);
}
} }
logPass(label);
console.log("\n📊 Testing PRIORITY CONTROL (every 10% for speed)..."); }catch(err){ logFail(label, err); }
mg.setMode("prioritycontrol");
let resultIndex = 0;
for (let demand = 0; demand <= 100; demand += demandIncrement) {
try {
console.log(`\n🔄 Setting priority demand to ${demand}%`);
await mg.handleInput("parent", demand);
pt1.calculateInput(1400);
const data = logMachineStates(mg, `priority ${demand}%`);
// Add priority data to existing result
if (results[resultIndex]) {
results[resultIndex].priority = {
flow: data.totalFlow,
power: data.totalPower,
efficiency: data.efficiency
};
}
console.log(`✅ priority ${demand}%: flow=${data.totalFlow.toFixed(2)}, power=${data.totalPower.toFixed(2)}, eff=${data.efficiency.toFixed(4)}`);
resultIndex++;
} catch (err) {
console.error(`❌ error at priority ${demand}%:`, err.message);
resultIndex++;
}
}
// Generate comparison report
generateEfficiencyReport(results);
} }
// Add this report generation function async function testAbsoluteScaling(mg,pt){
function generateEfficiencyReport(results) { const label = "Absolute scaling accepts direct flow targets";
console.log("\n" + "=".repeat(100)); try{
console.log("📈 EFFICIENCY COMPARISON REPORT");
console.log("=".repeat(100));
// Filter complete results with actual data
const completeResults = results.filter(r =>
r.optimal && r.priority &&
r.optimal.power > 0 && r.priority.power > 0 &&
r.optimal.flow > 0 && r.priority.flow > 0
);
if (completeResults.length === 0) {
console.log("❌ No complete results with active machines to compare");
console.log("💡 This might indicate machines are not starting properly");
// Show what data we do have
console.log("\n🔍 DEBUGGING DATA:");
results.forEach(r => {
if (r.optimal || r.priority) {
console.log(`${r.demand}%: optimal=${r.optimal?.power || 'missing'}, priority=${r.priority?.power || 'missing'}`);
}
});
return;
}
console.log(`\n📊 Successfully analyzed ${completeResults.length} test points with active machines`);
// Calculate summary statistics
let totalPowerDiff = 0;
let totalEffDiff = 0;
let validComparisons = 0;
console.log("\n📋 DETAILED BREAKDOWN:");
console.log("Demand | Optimal Power | Priority Power | Power Diff | Optimal Eff | Priority Eff | Eff Diff");
console.log("-------|---------------|----------------|------------|-------------|--------------|----------");
completeResults.forEach(r => {
const powerDiff = r.priority.power - r.optimal.power;
const effDiff = r.priority.efficiency - r.optimal.efficiency;
totalPowerDiff += powerDiff;
totalEffDiff += effDiff;
validComparisons++;
console.log(
`${r.demand}% | ${r.optimal.power.toFixed(3).padStart(11)} | ${r.priority.power.toFixed(3).padStart(12)} | ` +
`${powerDiff.toFixed(3).padStart(8)} | ${r.optimal.efficiency.toFixed(4).padStart(9)} | ` +
`${r.priority.efficiency.toFixed(4).padStart(10)} | ${effDiff.toFixed(4).padStart(7)}`
);
});
if (validComparisons > 0) {
const avgPowerDiff = totalPowerDiff / validComparisons;
const avgEffDiff = totalEffDiff / validComparisons;
console.log("\n📊 SUMMARY:");
console.log(`Valid comparisons: ${validComparisons}`);
console.log(`Average power difference: ${avgPowerDiff.toFixed(3)} kW`);
console.log(`Average efficiency difference: ${avgEffDiff.toFixed(4)} m3/h per kW`);
console.log("\n💡 RECOMMENDATION:");
if (avgEffDiff > 0.001) {
console.log(`✅ Priority Control shows ${avgEffDiff.toFixed(4)} better efficiency on average`);
} else if (avgEffDiff < -0.001) {
console.log(`✅ Optimal Control shows ${Math.abs(avgEffDiff).toFixed(4)} better efficiency on average`);
} else {
console.log(`⚖️ Both control methods show similar efficiency`);
}
}
}
async function testNormalizedScaling(mg, pt1) {
console.log("\n🧪 testing normalized scaling (0-100%)...");
mg.setScaling("normalized");
//fetch ranges
const maxflow = mg.dynamicTotals.flow.max;
console.log(`max group flow capacity: ${maxflow.toFixed(2)} m3/h`);
const minFlow = mg.dynamicTotals.flow.min;
console.log(`min group flow capacity: ${minFlow.toFixed(2)} m3/h`);
const testPoints = [0, 10, 25, 50, 75, 90, 100];
const testPressurePoints = [800, 1200, 1600, 2000];
for (const pressure of testPressurePoints) {
try {
console.log(`\n--- testing at ${pressure} mbar ---`);
pt1.calculateInput(pressure);
logMachineStates(mg, `${pressure} mbar, before demand tests`);
for (const demand of testPoints) {
try {
console.log(`\n--- normalized demand: ${demand}% ---`);
await mg.handleInput("parent", demand);
logMachineStates(mg, `normalized ${demand}%`);
//check if total flow is within expected range
const totalFlow = mg.measurements?.type("flow")?.variant("predicted")?.position("downstream")?.getCurrentValue() || 0;
const expectedFlow = minFlow + (demand / 100) * (maxflow - minFlow);
const percentTolerance = 0.1 ; // % tolerance of expected flow
const tolerance = (expectedFlow * percentTolerance) / 100;
if (totalFlow < expectedFlow - tolerance || totalFlow > expectedFlow + tolerance) {
console.warn(`⚠️ Total flow (${totalFlow.toFixed(2)} m3/h) is outside expected range (${(expectedFlow - tolerance).toFixed(2)} - ${(expectedFlow + tolerance).toFixed(2)} m3/h)`);
}
else {
console.log( `Difference between expected and actual flow: ${(totalFlow - expectedFlow).toFixed(2)} m3/h`);
console.log(`✅ Total flow (${totalFlow.toFixed(2)} m3/h) is within expected range (${(expectedFlow - tolerance).toFixed(2)} - ${(expectedFlow + tolerance).toFixed(2)} m3/h)`);
}
} catch (err) {
console.error(`❌ error at ${demand}%:`, err.message);
}
}
} catch (err) {
console.error(`❌ error setting pressure to ${pressure}:`, err.message);
}
}
}
async function testAbsoluteScaling(mg, pt1) {
console.log("\n🧪 testing absolute scaling...");
mg.setScaling("absolute"); mg.setScaling("absolute");
mg.setMode("optimalcontrol");
const absMin = mg.dynamicTotals.flow.min; const absMin = mg.dynamicTotals.flow.min;
const absMax = mg.dynamicTotals.flow.max; const absMax = mg.dynamicTotals.flow.max;
const testPoints = [ const demandPoints = [absMin, absMin+20, (absMin+absMax)/2, absMax-20];
absMin,
absMin + 20, for(const setpoint of demandPoints){
(absMin + absMax) / 2, await mg.handleInput("parent", setpoint);
absMax - 20, pt.calculateInput(1400);
absMax await sleep(20);
]; const flow = mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0;
if(!approxEqual(flow, setpoint, 2)){
for (const demand of testPoints) { throw new Error(`Flow ${flow.toFixed(2)} != demand ${setpoint.toFixed(2)}`);
try { }
console.log(`\n--- absolute demand: ${demand.toFixed(2)} m3/h ---`);
await mg.handleInput("parent", demand);
pt1.calculateInput(1400);
logMachineStates(mg, `absolute ${demand.toFixed(2)} m3/h`);
} catch (err) {
console.error(`❌ error at ${demand.toFixed(2)}:`, err.message);
}
} }
logPass(label);
}catch(err){ logFail(label, err); }
} }
async function testControlModes(mg, pt1) { async function testModeTransitions(mg,pt){
console.log("\n🧪 testing different control modes..."); const label = "Mode transitions keep machines responsive";
const modes = ["optimalcontrol", "prioritycontrol", "prioritypercentagecontrol"]; try{
const testDemand = 50; // 50% demand const modes = ["optimalcontrol","prioritycontrol","prioritypercentagecontrol"];
mg.setScaling("normalized"); mg.setScaling("normalized");
for(const mode of modes){
for (const mode of modes) { mg.setMode(mode);
try { await mg.handleInput("parent", 50);
console.log(`\n--- testing ${mode} ---`); pt.calculateInput(1300);
mg.setMode(mode); await sleep(20);
await mg.handleInput("parent", testDemand); const snapshot = captureState(mg, mode);
pt1.calculateInput(1400); const active = snapshot.machines.filter(m => m.state !== "idle");
if(active.length === 0){
logMachineStates(mg, `${mode} at ${testDemand}%`); throw new Error(`No active machines after switching to ${mode}`);
}
} catch (err) {
console.error(`❌ error testing mode ${mode}:`, err.message);
}
} }
logPass(label);
}catch(err){ logFail(label, err); }
} }
async function testRampUpDown(mg, pt1) { async function testRampBehaviour(mg,pt){
console.log("\n🧪 testing ramp up and down..."); const label = "Ramp up/down keeps monotonic flow";
mg.setScaling("normalized"); try{
mg.setMode("optimalcontrol"); mg.setMode("optimalcontrol");
mg.setScaling("normalized");
// Ramp up const upDemands = [0,20,40,60,80,100];
console.log("\n--- ramp up test ---"); let lastFlow = 0;
for (let demand = 0; demand <= 100; demand += 20) { for(const demand of upDemands){
try { await mg.handleInput("parent", demand);
console.log(`ramping up to ${demand}%`); pt.calculateInput(1500);
await mg.handleInput("parent", demand); await sleep(15);
pt1.calculateInput(1400); const flow = mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0;
if(flow < lastFlow - 1){
if (demand % 40 === 0) { // Log every other step throw new Error(`Flow decreased during ramp up: ${flow.toFixed(2)} < ${lastFlow.toFixed(2)}`);
logMachineStates(mg, `ramp up ${demand}%`); }
} lastFlow = flow;
} catch (err) {
console.error(`❌ error ramping up to ${demand}%:`, err.message);
}
} }
const downDemands = [100,80,60,40,20,0];
// Ramp down lastFlow = Infinity;
console.log("\n--- ramp down test ---"); for(const demand of downDemands){
for (let demand = 100; demand >= 0; demand -= 20) { await mg.handleInput("parent", demand);
try { pt.calculateInput(1200);
console.log(`ramping down to ${demand}%`); await sleep(15);
await mg.handleInput("parent", demand); const flow = mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0;
pt1.calculateInput(1400); if(flow > lastFlow + 1){
throw new Error(`Flow increased during ramp down: ${flow.toFixed(2)} > ${lastFlow.toFixed(2)}`);
if (demand % 40 === 0) { // Log every other step }
logMachineStates(mg, `ramp down ${demand}%`); lastFlow = flow;
}
} catch (err) {
console.error(`❌ error ramping down to ${demand}%:`, err.message);
}
} }
logPass(label);
}catch(err){ logFail(label, err); }
} }
async function testPressureResponse(mg, pt1) { async function testPressureAdaptation(mg,pt){
console.log("\n🧪 testing pressure response..."); const label = "Pressure changes update predictions";
mg.setScaling("normalized"); try{
mg.setMode("optimalcontrol"); mg.setMode("optimalcontrol");
mg.setScaling("normalized");
const pressures = [800, 1200, 1600, 2000]; const pressures = [800,1200,1600,2000];
const demand = 50; let previousFlow = null;
for(const p of pressures){
pt.calculateInput(p);
await mg.handleInput("parent", 50);
await sleep(20);
const flow = mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0;
if(previousFlow !== null && Math.abs(flow - previousFlow) < 0.5){
throw new Error(`Flow did not react to pressure shift (${previousFlow.toFixed(2)} -> ${flow.toFixed(2)})`);
}
previousFlow = flow;
}
logPass(label);
}catch(err){ logFail(label, err); }
}
async function comparePriorityVsOptimal(mg, pt){
const label = "Priority vs Optimal efficiency comparison";
try{
mg.setScaling("normalized");
const pressures = [800, 1100, 1400, 1700];
const demands = [...Array(21)].map((_, idx) => idx * 5);
for (const pressure of pressures) { for (const pressure of pressures) {
try { pt.calculateInput(pressure);
console.log(`\n--- testing at ${pressure} mbar ---`); await sleep(15);
pt1.calculateInput(pressure);
await mg.handleInput("parent", demand);
for (const demand of demands) {
logMachineStates(mg, `${pressure} mbar, ${demand}%`); mg.setMode("optimalcontrol");
await mg.handleInput("parent", demand);
} catch (err) { pt.calculateInput(pressure);
console.error(`❌ error at pressure ${pressure}:`, err.message); await sleep(20);
} const optimalTotals = captureState(mg, `optimal-${pressure}-${demand}`).totals;
mg.setMode("prioritycontrol");
await mg.handleInput("parent", demand);
pt.calculateInput(pressure);
await sleep(20);
const priorityTotals = captureState(mg, `priority-${pressure}-${demand}`).totals;
efficiencyComparisons.push({
pressure,
demandPercent: demand,
optimalFlow: Number(optimalTotals.flow.toFixed(3)),
optimalPower: Number(optimalTotals.power.toFixed(3)),
optimalEfficiency: Number((optimalTotals.efficiency || 0).toFixed(4)),
priorityFlow: Number(priorityTotals.flow.toFixed(3)),
priorityPower: Number(priorityTotals.power.toFixed(3)),
priorityEfficiency: Number((priorityTotals.efficiency || 0).toFixed(4)),
efficiencyDelta: Number(((priorityTotals.efficiency || 0) - (optimalTotals.efficiency || 0)).toFixed(4)),
powerDelta: Number((priorityTotals.power - optimalTotals.power).toFixed(3))
});
}
} }
logPass(label, "efficiencyComparisons array populated");
} catch (err) {
logFail(label, err);
}
} }
async function testEdgeCases(mg, pt1) {
console.log("\n🧪 testing edge cases...");
mg.setScaling("normalized");
mg.setMode("optimalcontrol");
const edgeCases = [
{ demand: -10, name: "negative demand" },
{ demand: 0, name: "zero demand" },
{ demand: 0.5, name: "fractional demand" },
{ demand: 110, name: "over 100%" },
{ demand: 999, name: "extreme demand" }
];
for (const testCase of edgeCases) {
try {
console.log(`\n--- testing ${testCase.name}: ${testCase.demand} ---`);
await mg.handleInput("parent", testCase.demand);
pt1.calculateInput(1400);
logMachineStates(mg, testCase.name); async function run(){
console.log("🚀 Starting machine-group integration tests...");
} catch (err) { const { mg, pt } = await bootstrapGroup();
console.error(`❌ error testing ${testCase.name}:`, err.message);
} await testNormalizedScaling(mg, pt);
} await testAbsoluteScaling(mg, pt);
await testModeTransitions(mg, pt);
await testRampBehaviour(mg, pt);
await testPressureAdaptation(mg, pt);
await comparePriorityVsOptimal(mg, pt);
console.log("\n📋 TEST SUMMARY");
console.table(testSuite);
console.log("\n📊 efficiencyComparisons:");
console.dir(efficiencyComparisons, { depth:null });
console.log("✅ All tests completed.");
} }
async function testPerformanceMetrics(mg, pt1) { run().catch(err => {
console.log("\n🧪 testing performance metrics..."); console.error("💥 Test harness crashed:", err);
mg.setScaling("normalized"); });
mg.setMode("optimalcontrol"); // ...existing code...
const demands = [25, 50, 75];
const results = [];
for (const demand of demands) {
try {
const startTime = Date.now();
await mg.handleInput("parent", demand);
pt1.calculateInput(1400);
const endTime = Date.now();
const totalFlow = mg.measurements?.type("flow")?.variant("predicted")?.position("downstream")?.getCurrentValue() || 0;
const totalPower = mg.measurements?.type("power")?.variant("predicted")?.position("upstream")?.getCurrentValue() || 0;
const efficiency = totalFlow > 0 ? (totalFlow / totalPower).toFixed(3) : 0;
results.push({
demand,
flow: totalFlow.toFixed(2),
power: totalPower.toFixed(2),
efficiency,
responseTime: endTime - startTime
});
} catch (err) {
console.error(`❌ error testing performance at ${demand}%:`, err.message);
}
}
console.log("\n=== performance summary ===");
console.log("demand | flow | power | efficiency | response(ms)");
console.log("-------|--------|--------|-----------|---------");
results.forEach(r => {
console.log(`${r.demand}% | ${r.flow} | ${r.power} | ${r.efficiency} | ${r.responseTime}`);
});
}
async function runAllTests() {
console.log("🚀 starting comprehensive machinegroup tests...\n");
try {
// Setup
const machineGroupConfig = createBaseMachineGroupConfig("testmachinegroup");
const machineConfigs = {};
machineConfigs[1] = createBaseMachineConfig(1, "testmachine1", specs);
machineConfigs[2] = createBaseMachineConfig(2, "testmachine2", specs);
const mg = new MachineGroup(machineGroupConfig);
const pt1 = new Measurement(ptConfig);
const numofMachines = 2;
// Register machines
for (let i = 1; i <= numofMachines; i++) {
const machine = new Machine(machineConfigs[i],stateConfig);
mg.childRegistrationUtils.registerChild(machine, "downstream");
}
mg.machines[1].childRegistrationUtils.registerChild(pt1, "downstream");
mg.machines[2].childRegistrationUtils.registerChild(pt1, "downstream");
console.log(`✅ setup complete: ${Object.keys(mg.machines).length} machines registered`);
console.log(`flow range: ${mg.dynamicTotals.flow.min.toFixed(2)} - ${mg.dynamicTotals.flow.max.toFixed(2)} m3/h\n`);
// Run test suites
//await testPriorityVsOptimalEfficiency(mg, pt1);
await testNormalizedScaling(mg, pt1);
await testAbsoluteScaling(mg, pt1);
await testControlModes(mg, pt1);
await testRampUpDown(mg, pt1);
await testPressureResponse(mg, pt1);
await testEdgeCases(mg, pt1);
await testPerformanceMetrics(mg, pt1);
console.log("\n🎉 all tests completed successfully!");
} catch (err) {
console.error("💥 test suite failed:", err.message);
console.error("stack trace:", err.stack);
}
}
// Run all tests // Run all tests
runAllTests(); run();

View File

@@ -58,6 +58,7 @@ class nodeClass {
} }
_updateNodeStatus() { _updateNodeStatus() {
//console.log('Updating node status...');
const mg = this.source; const mg = this.source;
const mode = mg.mode; const mode = mg.mode;
const scaling = mg.scaling; const scaling = mg.scaling;
@@ -72,7 +73,7 @@ class nodeClass {
const totalPower = mg.measurements const totalPower = mg.measurements
?.type("power") ?.type("power")
?.variant("predicted") ?.variant("predicted")
?.position("upstream") ?.position("atEquipment")
?.getCurrentValue() || 0; ?.getCurrentValue() || 0;
// Calculate total capacity based on available machines with safety checks // Calculate total capacity based on available machines with safety checks

View File

@@ -50,12 +50,6 @@ class MachineGroup {
} }
// when a child gets updated do something
handleChildChange() {
this.absoluteTotals = this.calcAbsoluteTotals();
//for reference and not to recalc these values continiously
this.dynamicTotals = this.calcDynamicTotals();
}
registerChild(child,softwareType) { registerChild(child,softwareType) {
this.logger.debug('Setting up childs specific for this class'); this.logger.debug('Setting up childs specific for this class');
@@ -67,17 +61,27 @@ class MachineGroup {
//listen for machine pressure changes //listen for machine pressure changes
this.logger.debug(`Listening for pressure changes from machine ${child.config.general.id}`); this.logger.debug(`Listening for pressure changes from machine ${child.config.general.id}`);
child.measurements.emitter.on("pressure.measured.differential", (eventData) => { child.measurements.emitter.on("pressure.measured.differential", (eventData) => {
this.logger.debug(`Pressure update from ${child.config.general.id}: ${eventData.value} ${eventData.unit}`); this.logger.debug(`Pressure update from ${child.config.general.id}: ${eventData.value} ${eventData.unit}`);
this.handleChildChange(); this.handlePressureChange();
}); });
child.measurements.emitter.on("pressure.measured.downstream", (eventData) => {
this.logger.debug(`Pressure update from ${child.config.general.id}: ${eventData.value} ${eventData.unit}`);
this.handlePressureChange();
});
child.measurements.emitter.on("flow.predicted.downstream", (eventData) => {
this.logger.debug(`Flow prediction update from ${child.config.general.id}: ${eventData.value} ${eventData.unit}`);
//later change to this.handleFlowPredictionChange();
this.handlePressureChange();
});
} }
} }
calcAbsoluteTotals() { calcAbsoluteTotals() {
const absoluteTotals = { flow: { min: Infinity, max: 0 }, power: { min: Infinity, max: 0 } }; const absoluteTotals = { flow: { min: Infinity, max: 0 }, power: { min: Infinity, max: 0 } };
@@ -99,6 +103,7 @@ class MachineGroup {
if( maxPower > totals.power.max ){ totals.power.max = maxPower; } if( maxPower > totals.power.max ){ totals.power.max = maxPower; }
}); });
//surplus machines for max flow and power //surplus machines for max flow and power
if( totals.flow.min < absoluteTotals.flow.min ){ absoluteTotals.flow.min = totals.flow.min; } if( totals.flow.min < absoluteTotals.flow.min ){ absoluteTotals.flow.min = totals.flow.min; }
if( totals.power.min < absoluteTotals.power.min ){ absoluteTotals.power.min = totals.power.min; } if( totals.power.min < absoluteTotals.power.min ){ absoluteTotals.power.min = totals.power.min; }
@@ -107,6 +112,29 @@ class MachineGroup {
}); });
if(absoluteTotals.flow.min === Infinity) {
this.logger.warn(`Flow min ${absoluteTotals.flow.min} is Infinity. Setting to 0.`);
absoluteTotals.flow.min = 0;
}
if(absoluteTotals.power.min === Infinity) {
this.logger.warn(`Power min ${absoluteTotals.power.min} is Infinity. Setting to 0.`);
absoluteTotals.power.min = 0;
}
if(absoluteTotals.flow.max === -Infinity) {
this.logger.warn(`Flow max ${absoluteTotals.flow.max} is -Infinity. Setting to 0.`);
absoluteTotals.flow.max = 0;
}
if(absoluteTotals.power.max === -Infinity) {
this.logger.warn(`Power max ${absoluteTotals.power.max} is -Infinity. Setting to 0.`);
absoluteTotals.power.max = 0;
}
// Place data in object for external use
this.absoluteTotals = absoluteTotals;
return absoluteTotals; return absoluteTotals;
} }
@@ -114,9 +142,10 @@ class MachineGroup {
//max and min current flow and power based on their actual pressure curve //max and min current flow and power based on their actual pressure curve
calcDynamicTotals() { calcDynamicTotals() {
const dynamicTotals = { flow: { min: Infinity, max: 0 }, power: { min: Infinity, max: 0 }, NCog : 0 }; const dynamicTotals = { flow: { min: Infinity, max: 0, act: 0 }, power: { min: Infinity, max: 0, act: 0 }, NCog : 0 };
this.logger.debug(`\n --------- Calculating dynamic totals for ${Object.keys(this.machines).length} machines. @ current pressure settings : ----------`); this.logger.debug(`\n --------- Calculating dynamic totals for ${Object.keys(this.machines).length} machines. @ current pressure settings : ----------`);
Object.values(this.machines).forEach(machine => { Object.values(this.machines).forEach(machine => {
this.logger.debug(`Processing machine with id: ${machine.config.general.id}`); this.logger.debug(`Processing machine with id: ${machine.config.general.id}`);
this.logger.debug(`Current pressure settings: ${JSON.stringify(machine.predictFlow.currentF)}`); this.logger.debug(`Current pressure settings: ${JSON.stringify(machine.predictFlow.currentF)}`);
@@ -125,18 +154,27 @@ class MachineGroup {
const maxFlow = machine.predictFlow.currentFxyYMax; const maxFlow = machine.predictFlow.currentFxyYMax;
const minPower = machine.predictPower.currentFxyYMin; const minPower = machine.predictPower.currentFxyYMin;
const maxPower = machine.predictPower.currentFxyYMax; const maxPower = machine.predictPower.currentFxyYMax;
const actFlow = machine.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue();
const actPower = machine.measurements.type("power").variant("predicted").position("atEquipment").getCurrentValue();
this.logger.debug(`Machine ${machine.config.general.id} - Min Flow: ${minFlow}, Max Flow: ${maxFlow}, Min Power: ${minPower}, Max Power: ${maxPower}, NCog: ${machine.NCog}`);
if( minFlow < dynamicTotals.flow.min ){ dynamicTotals.flow.min = minFlow; } if( minFlow < dynamicTotals.flow.min ){ dynamicTotals.flow.min = minFlow; }
if( minPower < dynamicTotals.power.min ){ dynamicTotals.power.min = minPower; } if( minPower < dynamicTotals.power.min ){ dynamicTotals.power.min = minPower; }
dynamicTotals.flow.max += maxFlow; dynamicTotals.flow.max += maxFlow;
dynamicTotals.power.max += maxPower; dynamicTotals.power.max += maxPower;
dynamicTotals.flow.act += actFlow;
dynamicTotals.power.act += actPower;
//fetch total Normalized Cog over all machines //fetch total Normalized Cog over all machines
dynamicTotals.NCog += machine.NCog; dynamicTotals.NCog += machine.NCog;
}); });
// Place data in object for external use
this.dynamicTotals = dynamicTotals;
return dynamicTotals; return dynamicTotals;
} }
@@ -166,10 +204,16 @@ class MachineGroup {
} }
handlePressureChange() { handlePressureChange() {
this.logger.info("Pressure change detected."); this.logger.info("---------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>Pressure change detected.");
this.calcDynamicTotals(); // Recalculate totals
const { flow, power } = this.calcDynamicTotals();
this.logger.debug(`Dynamic Totals after pressure change - Flow: Min ${flow.min}, Max ${flow.max}, Act ${flow.act} | Power: Min ${power.min}, Max ${power.max}, Act ${power.act}`);
this.measurements.type("flow").variant("predicted").position("downstream").value(flow.act);
this.measurements.type("power").variant("predicted").position("atEquipment").value(power.act);
const { maxEfficiency, lowestEfficiency } = this.calcGroupEfficiency(this.machines); const { maxEfficiency, lowestEfficiency } = this.calcGroupEfficiency(this.machines);
const efficiency = this.measurements.type("efficiency").variant("predicted").position("downstream").getCurrentValue(); const efficiency = this.measurements.type("efficiency").variant("predicted").position("atEquipment").getCurrentValue();
this.calcDistanceBEP(efficiency,maxEfficiency,lowestEfficiency); this.calcDistanceBEP(efficiency,maxEfficiency,lowestEfficiency);
} }
@@ -232,7 +276,6 @@ class MachineGroup {
// Generate all possible subsets of machines (power set) // Generate all possible subsets of machines (power set)
Object.keys(machines).forEach(machineId => { Object.keys(machines).forEach(machineId => {
//machineId = parseInt(machineId);
const state = machines[machineId].state.getCurrentState(); const state = machines[machineId].state.getCurrentState();
const validActionForMode = machines[machineId].isValidActionForMode("execSequence", "auto"); const validActionForMode = machines[machineId].isValidActionForMode("execSequence", "auto");
@@ -334,7 +377,6 @@ class MachineGroup {
} }
// -------- Mode and Input Management -------- // // -------- Mode and Input Management -------- //
isValidActionForMode(action, mode) { isValidActionForMode(action, mode) {
const allowedActionsSet = this.config.mode.allowedActions[mode] || []; const allowedActionsSet = this.config.mode.allowedActions[mode] || [];
return allowedActionsSet.has(action); return allowedActionsSet.has(action);
@@ -346,8 +388,18 @@ class MachineGroup {
this.logger.debug(`Scaling set to: ${scaling}`); this.logger.debug(`Scaling set to: ${scaling}`);
} }
async abortActiveMovements(reason = "new demand") {
await Promise.all(Object.values(this.machines).map(async machine => {
this.logger.warn(`Aborting active movements for machine ${machine.config.general.id} due to: ${reason}`);
if (typeof machine.abortMovement === "function") {
await machine.abortMovement(reason);
}
}));
}
//handle input from parent / user / UI //handle input from parent / user / UI
async optimalControl(Qd, powerCap = Infinity) { async optimalControl(Qd, powerCap = Infinity) {
try{ try{
//we need to force the pressures of all machines to be equal to the highest pressure measured in the group //we need to force the pressures of all machines to be equal to the highest pressure measured in the group
// this is to ensure a correct evaluation of the flow and power consumption // this is to ensure a correct evaluation of the flow and power consumption
@@ -361,20 +413,25 @@ class MachineGroup {
const maxDownstream = Math.max(...pressures.map(p => p.downstream)); const maxDownstream = Math.max(...pressures.map(p => p.downstream));
const minUpstream = Math.min(...pressures.map(p => p.upstream)); const minUpstream = Math.min(...pressures.map(p => p.upstream));
this.logger.debug(`Max downstream pressure: ${maxDownstream}, Min upstream pressure: ${minUpstream}`);
//set the pressures //set the pressures
Object.entries(this.machines).forEach(([machineId, machine]) => { Object.entries(this.machines).forEach(([machineId, machine]) => {
if(machine.state.getCurrentState() !== "operational" && machine.state.getCurrentState() !== "accelerating" && machine.state.getCurrentState() !== "decelerating"){ if(machine.state.getCurrentState() !== "operational" && machine.state.getCurrentState() !== "accelerating" && machine.state.getCurrentState() !== "decelerating"){
//Equilize pressures over all machines so we can make a proper calculation
machine.measurements.type("pressure").variant("measured").position("downstream").value(maxDownstream); machine.measurements.type("pressure").variant("measured").position("downstream").value(maxDownstream);
machine.measurements.type("pressure").variant("measured").position("upstream").value(minUpstream); machine.measurements.type("pressure").variant("measured").position("upstream").value(minUpstream);
// after updating the measurement directly we need to force the update of the value OLIFANT this is not so clear now in the code // after updating the measurement directly we need to force the update of the value OLIFANT this is not so clear now in the code
// we need to find a better way to do this but for now it works // we need to find a better way to do this but for now it works
machine.getMeasuredPressure(); machine.getMeasuredPressure();
} }
}); });
//fetch dynamic totals
const dynamicTotals = this.dynamicTotals;
//update dynamic totals
const dynamicTotals = this.calcDynamicTotals();
const machineStates = Object.entries(this.machines).reduce((acc, [machineId, machine]) => { const machineStates = Object.entries(this.machines).reduce((acc, [machineId, machine]) => {
acc[machineId] = machine.state.getCurrentState(); acc[machineId] = machine.state.getCurrentState();
return acc; return acc;
@@ -396,48 +453,48 @@ class MachineGroup {
} }
// fetch all valid combinations that meet expectations // fetch all valid combinations that meet expectations
const combinations = this.validPumpCombinations(this.machines, Qd, powerCap); const combinations = this.validPumpCombinations(this.machines, Qd, powerCap);
//
const bestResult = this.calcBestCombination(combinations, Qd); const bestResult = this.calcBestCombination(combinations, Qd);
if(bestResult.bestCombination === null){ if(bestResult.bestCombination === null){
this.logger.warn(`Demand: ${Qd.toFixed(2)} -> No valid combination found => not updating control `); this.logger.warn(`Demand: ${Qd.toFixed(2)} -> No valid combination found => not updating control `);
return; return;
} }
const debugInfo = bestResult.bestCombination.map(({ machineId, flow }) => `${machineId}: ${flow.toFixed(2)} units`).join(" | "); const debugInfo = bestResult.bestCombination.map(({ machineId, flow }) => `${machineId}: ${flow.toFixed(2)} units`).join(" | ");
this.logger.debug(`Moving to demand: ${Qd.toFixed(2)} -> Pumps: [${debugInfo}] => Total Power: ${bestResult.bestPower.toFixed(2)}`); this.logger.debug(`Moving to demand: ${Qd.toFixed(2)} -> Pumps: [${debugInfo}] => Total Power: ${bestResult.bestPower.toFixed(2)}`);
//store the total delivered power //store the total delivered power
this.measurements.type("power").variant("predicted").position("upstream").value(bestResult.bestPower); this.measurements.type("power").variant("predicted").position("atEquipment").value(bestResult.bestPower);
this.measurements.type("flow").variant("predicted").position("downstream").value(bestResult.bestFlow); this.measurements.type("flow").variant("predicted").position("downstream").value(bestResult.bestFlow);
this.measurements.type("efficiency").variant("predicted").position("downstream").value(bestResult.bestFlow / bestResult.bestPower); this.measurements.type("efficiency").variant("predicted").position("atEquipment").value(bestResult.bestFlow / bestResult.bestPower);
this.measurements.type("Ncog").variant("predicted").position("downstream").value(bestResult.bestCog); this.measurements.type("Ncog").variant("predicted").position("atEquipment").value(bestResult.bestCog);
await Promise.all(Object.entries(this.machines).map(async ([machineId, machine]) => { await Promise.all(Object.entries(this.machines).map(async ([machineId, machine]) => {
// Find the flow for this machine in the best combination
const pumpInfo = bestResult.bestCombination.find(item => item.machineId == machineId); this.logger.debug(`Searching for machine ${machineId} with state ${machineStates[machineId]} in best combination.`);
const pumpInfo = bestResult.bestCombination.find(item => item.machineId == machineId);
let flow; let flow;
if(pumpInfo !== undefined){ if(pumpInfo !== undefined){
flow = pumpInfo.flow; flow = pumpInfo.flow;
} else { } else {
this.logger.debug(`Machine ${machineId} not in best combination, setting flow to 0`); this.logger.debug(`Machine ${machineId} not in best combination, setting flow control to 0`);
flow = 0; flow = 0;
} }
if( (flow <= 0 ) && ( machineStates[machineId] === "operational" || machineStates[machineId] === "accelerating" || machineStates[machineId] === "decelerating" ) ){ if( (flow <= 0 ) && ( machineStates[machineId] === "operational" || machineStates[machineId] === "accelerating" || machineStates[machineId] === "decelerating" ) ){
await machine.handleInput("parent", "execSequence", "shutdown"); await machine.handleInput("parent", "execSequence", "shutdown");
} }
else if(machineStates[machineId] === "idle" && flow > 0){
if(machineStates[machineId] === "idle" && flow > 0){
await machine.handleInput("parent", "execSequence", "startup"); await machine.handleInput("parent", "execSequence", "startup");
}
else if(machineStates[machineId] === "operational" && flow > 0 ){
await machine.handleInput("parent", "flowMovement", flow); await machine.handleInput("parent", "flowMovement", flow);
} }
if(machineStates[machineId] === "operational" && flow > 0 ){
await machine.handleInput("parent", "flowMovement", flow);
}
})); }));
} }
catch(err){ catch(err){
this.logger.error(err); this.logger.error(err);
@@ -499,7 +556,7 @@ class MachineGroup {
.map(id => ({ id, machine: this.machines[id] })); .map(id => ({ id, machine: this.machines[id] }));
} else { } else {
machinesInPriorityOrder = Object.entries(this.machines) machinesInPriorityOrder = Object.entries(this.machines)
.map(([id, machine]) => ({ id: parseInt(id), machine })) .map(([id, machine]) => ({ id: id, machine }))
.sort((a, b) => a.id - b.id); .sort((a, b) => a.id - b.id);
} }
return machinesInPriorityOrder; return machinesInPriorityOrder;
@@ -545,14 +602,6 @@ class MachineGroup {
// Update dynamic totals // Update dynamic totals
const dynamicTotals = this.calcDynamicTotals(); const dynamicTotals = this.calcDynamicTotals();
// Handle zero demand by shutting down all machines early exit
if (Qd <= 0) {
await Promise.all(Object.entries(this.machines).map(async ([machineId, machine]) => {
if (this.isMachineActive(machineId)) { await machine.handleInput("parent", "execSequence", "shutdown"); }
}));
return;
}
// Cap flow demand to min/max possible values // Cap flow demand to min/max possible values
Qd = this.capFlowDemand(Qd,dynamicTotals); Qd = this.capFlowDemand(Qd,dynamicTotals);
@@ -646,14 +695,16 @@ class MachineGroup {
this.logger.debug(`Priority control for demand: ${totalFlow.toFixed(2)} -> Active pumps: [${debugInfo}] => Total Power: ${totalPower.toFixed(2)}`); this.logger.debug(`Priority control for demand: ${totalFlow.toFixed(2)} -> Active pumps: [${debugInfo}] => Total Power: ${totalPower.toFixed(2)}`);
// Store measurements // Store measurements
this.measurements.type("power").variant("predicted").position("upstream").value(totalPower); this.measurements.type("power").variant("predicted").position("atEquipment").value(totalPower);
this.measurements.type("flow").variant("predicted").position("downstream").value(totalFlow); this.measurements.type("flow").variant("predicted").position("downstream").value(totalFlow);
this.measurements.type("efficiency").variant("predicted").position("downstream").value(totalFlow / totalPower); this.measurements.type("efficiency").variant("predicted").position("atEquipment").value(totalFlow / totalPower);
this.measurements.type("Ncog").variant("predicted").position("downstream").value(totalCog); this.measurements.type("Ncog").variant("predicted").position("atEquipment").value(totalCog);
this.logger.debug(`Flow distribution: ${JSON.stringify(flowDistribution)}`);
// Apply the flow distribution to machines // Apply the flow distribution to machines
await Promise.all(flowDistribution.map(async ({ machineId, flow }) => { await Promise.all(flowDistribution.map(async ({ machineId, flow }) => {
const machine = this.machines[machineId]; const machine = this.machines[machineId];
this.logger.debug(this.machines[machineId].state);
const currentState = this.machines[machineId].state.getCurrentState(); const currentState = this.machines[machineId].state.getCurrentState();
if (flow <= 0 && (currentState === "operational" || currentState === "accelerating" || currentState === "decelerating")) { if (flow <= 0 && (currentState === "operational" || currentState === "accelerating" || currentState === "decelerating")) {
@@ -758,8 +809,10 @@ class MachineGroup {
// fetch and store measurements // fetch and store measurements
Object.entries(this.machines).forEach(([machineId, machine]) => { Object.entries(this.machines).forEach(([machineId, machine]) => {
const powerValue = machine.measurements.type("power").variant("predicted").position("upstream").getCurrentValue();
const powerValue = machine.measurements.type("power").variant("predicted").position("atEquipment").getCurrentValue();
const flowValue = machine.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue(); const flowValue = machine.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue();
if (powerValue !== null) { if (powerValue !== null) {
totalPower.push(powerValue); totalPower.push(powerValue);
} }
@@ -768,10 +821,11 @@ class MachineGroup {
} }
}); });
this.measurements.type("power").variant("predicted").position("upstream").value(totalPower.reduce((a, b) => a + b, 0)); this.measurements.type("power").variant("predicted").position("atEquipment").value(totalPower.reduce((a, b) => a + b, 0));
this.measurements.type("flow").variant("predicted").position("downstream").value(totalFlow.reduce((a, b) => a + b, 0)); this.measurements.type("flow").variant("predicted").position("downstream").value(totalFlow.reduce((a, b) => a + b, 0));
if(totalPower.reduce((a, b) => a + b, 0) > 0){ if(totalPower.reduce((a, b) => a + b, 0) > 0){
this.measurements.type("efficiency").variant("predicted").position("downstream").value(totalFlow.reduce((a, b) => a + b, 0) / totalPower.reduce((a, b) => a + b, 0)); this.measurements.type("efficiency").variant("predicted").position("atEquipment").value(totalFlow.reduce((a, b) => a + b, 0) / totalPower.reduce((a, b) => a + b, 0));
} }
} }
@@ -780,43 +834,81 @@ class MachineGroup {
} }
} }
async handleInput(source, Qd, powerCap = Infinity, priorityList = null) { async handleInput(source, demand, powerCap = Infinity, priorityList = null) {
//abort current movements
await this.abortActiveMovements("new demand received");
const scaling = this.scaling; const scaling = this.scaling;
const mode = this.mode; const mode = this.mode;
let rawInput = Qd; const dynamicTotals = this.calcDynamicTotals();
const demandQ = parseFloat(demand);
let demandQout = 0; // keep output Q by default 0 for safety
this.logger.debug(`Handling input from ${source}: Demand = ${demand}, Power Cap = ${powerCap}, Priority List = ${priorityList}`);
switch (scaling) { switch (scaling) {
case "absolute": case "absolute":
// No scaling needed but cap range if (isNaN(demandQ)) {
if (Qd < this.absoluteTotals.flow.min) { this.logger.warn(`Invalid absolute flow demand: ${demand}. Must be a number.`);
this.logger.warn(`Flow demand ${Qd} is below minimum possible flow ${this.absoluteTotals.flow.min}. Capping to minimum flow.`); demandQout = 0;
Qd = this.absoluteTotals.flow.min; return;
} else if (Qd > this.absoluteTotals.flow.max) { }
this.logger.warn(`Flow demand ${Qd} is above maximum possible flow ${this.absoluteTotals.flow.max}. Capping to maximum flow.`);
Qd = this.absoluteTotals.flow.max; if (demandQ < absoluteTotals.flow.min) {
this.logger.warn(`Flow demand ${demandQ} is below minimum possible flow ${absoluteTotals.flow.min}. Capping to minimum flow.`);
demandQout = this.absoluteTotals.flow.min;
} else if (demandQout > absoluteTotals.flow.max) {
this.logger.warn(`Flow demand ${demandQ} is above maximum possible flow ${absoluteTotals.flow.max}. Capping to maximum flow.`);
demandQout = absoluteTotals.flow.max;
}else if(demandQout <= 0){
this.logger.debug(`Turning machines off`);
demandQout = 0;
//return early and turn all machines off
this.turnOffAllMachines();
return;
} }
break; break;
case "normalized": case "normalized":
// Scale demand to 0-100% linear between min and max flow this is auto capped
Qd = this.interpolation.interpolate_lin_single_point(Qd, 0, 100, this.dynamicTotals.flow.min, this.dynamicTotals.flow.max); this.logger.debug(`Normalizing flow demand: ${demandQ} with min: ${dynamicTotals.flow.min} and max: ${dynamicTotals.flow.max}`);
if(demand < 0){
this.logger.debug(`Turning machines off`);
demandQout = 0;
//return early and turn all machines off
this.turnOffAllMachines();
return;
}
else{
// Scale demand to 0-100% linear between min and max flow this is auto capped
demandQout = this.interpolation.interpolate_lin_single_point(demandQ, 0, 100, dynamicTotals.flow.min, dynamicTotals.flow.max );
this.logger.debug(`Normalized flow demand ${demandQ}% to: ${demandQout} Q units`);
}
break; break;
} }
// Execute control based on mode
switch(mode) { switch(mode) {
case "prioritycontrol": case "prioritycontrol":
await this.equalFlowControl(Qd,powerCap,priorityList); this.logger.debug(`Calculating prio control. Input flow demand: ${demandQ} scaling : ${scaling} -> ${demandQout}`);
await this.equalFlowControl(demandQout,powerCap,priorityList);
break; break;
case "prioritypercentagecontrol": case "prioritypercentagecontrol":
this.logger.debug(`Calculating prio percentage control. Input flow demand: ${demandQ} scaling : ${scaling} -> ${demandQout}`);
if(scaling !== "normalized"){ if(scaling !== "normalized"){
this.logger.warn("Priority percentage control is only valid with normalized scaling."); this.logger.warn("Priority percentage control is only valid with normalized scaling.");
return; return;
} }
await this.prioPercentageControl(rawInput,priorityList); await this.prioPercentageControl(demandQout,priorityList);
break; break;
case "optimalcontrol": case "optimalcontrol":
await this.optimalControl(Qd,powerCap); this.logger.debug(`Calculating optimal control. Input flow demand: ${demandQ} scaling : ${scaling} -> ${demandQout}`);
await this.optimalControl(demandQout,powerCap);
break; break;
default: default:
@@ -831,6 +923,12 @@ class MachineGroup {
} }
async turnOffAllMachines(){
await Promise.all(Object.entries(this.machines).map(async ([machineId, machine]) => {
if (this.isMachineActive(machineId)) { await machine.handleInput("parent", "execSequence", "shutdown"); }
}));
}
setMode(mode) { setMode(mode) {
this.mode = mode; this.mode = mode;
} }
@@ -845,6 +943,7 @@ class MachineGroup {
this.measurements.getVariants(type).forEach(variant => { this.measurements.getVariants(type).forEach(variant => {
const downstreamVal = this.measurements.type(type).variant(variant).position("downstream").getCurrentValue(); const downstreamVal = this.measurements.type(type).variant(variant).position("downstream").getCurrentValue();
const atEquipmentVal = this.measurements.type(type).variant(variant).position("atEquipment").getCurrentValue();
const upstreamVal = this.measurements.type(type).variant(variant).position("upstream").getCurrentValue(); const upstreamVal = this.measurements.type(type).variant(variant).position("upstream").getCurrentValue();
if (downstreamVal != null) { if (downstreamVal != null) {
@@ -853,6 +952,9 @@ class MachineGroup {
if (upstreamVal != null) { if (upstreamVal != null) {
output[`upstream_${variant}_${type}`] = upstreamVal; output[`upstream_${variant}_${type}`] = upstreamVal;
} }
if (atEquipmentVal != null) {
output[`atEquipment_${variant}_${type}`] = atEquipmentVal;
}
if (downstreamVal != null && upstreamVal != null) { if (downstreamVal != null && upstreamVal != null) {
const diffVal = this.measurements.type(type).variant(variant).difference().value; const diffVal = this.measurements.type(type).variant(variant).difference().value;
output[`differential_${variant}_${type}`] = diffVal; output[`differential_${variant}_${type}`] = diffVal;
@@ -876,17 +978,17 @@ class MachineGroup {
} }
module.exports = MachineGroup; module.exports = MachineGroup;
/* /*
const Machine = require('../../rotatingMachine/src/specificClass'); const Machine = require('../../rotatingMachine/src/specificClass');
const Measurement = require('../../measurement/src/specificClass'); const Measurement = require('../../measurement/src/specificClass');
const specs = require('../../generalFunctions/datasets/assetData/curves/hidrostal-H05K-S03R.json'); const specs = require('../../generalFunctions/datasets/assetData/curves/hidrostal-H05K-S03R.json');
const { number } = require("../../generalFunctions/src/convert/lodash/lodash._objecttypes"); const { max } = require("mathjs");
function createBaseMachineConfig(machineNum, name,specs) { function createBaseMachineConfig(machineNum, name,specs) {
return { return {
general: { general: {
logging: { enabled: true, logLevel: "warn" }, logging: { enabled: true, logLevel: "debug" },
name: name, name: name,
id: machineNum, id: machineNum,
unit: "m3/h" unit: "m3/h"
@@ -924,6 +1026,23 @@ function createBaseMachineConfig(machineNum, name,specs) {
}; };
} }
function createStateConfig(){
return {
time:{
starting: 1,
stopping: 1,
warmingup: 1,
coolingdown: 1,
emergencystop: 1
},
movement:{
mode:"dynspeed",
speed:100,
maxSpeed: 1000
}
}
};
function createBaseMachineGroupConfig(name) { function createBaseMachineGroupConfig(name) {
return { return {
general: { general: {
@@ -944,9 +1063,13 @@ function createBaseMachineGroupConfig(name) {
} }
const machineGroupConfig = createBaseMachineGroupConfig("testmachinegroup"); const machineGroupConfig = createBaseMachineGroupConfig("testmachinegroup");
const stateConfigs = {};
const machineConfigs = {}; const machineConfigs = {};
machineConfigs[1]= createBaseMachineConfig(1,"testmachine",specs); stateConfigs[1] = createStateConfig();
machineConfigs[2] = createBaseMachineConfig(2,"testmachine2",specs); stateConfigs[2] = createStateConfig();
machineConfigs[1]= createBaseMachineConfig("asdfkj;asdf","testmachine",specs);
machineConfigs[2] = createBaseMachineConfig("asdfkj;asdf2","testmachine2",specs);
const ptConfig = { const ptConfig = {
general: { general: {
@@ -976,14 +1099,16 @@ async function makeMachines(){
const pt1 = new Measurement(ptConfig); const pt1 = new Measurement(ptConfig);
const numofMachines = 2; const numofMachines = 2;
for(let i = 1; i <= numofMachines; i++){ for(let i = 1; i <= numofMachines; i++){
const machine = new Machine(machineConfigs[i]); const machine = new Machine(machineConfigs[i],stateConfigs[i]);
//mg.machines[i] = machine; //mg.machines[i] = machine;
mg.childRegistrationUtils.registerChild(machine, "downstream"); mg.childRegistrationUtils.registerChild(machine, "downstream");
} }
mg.machines[1].childRegistrationUtils.registerChild(pt1, "downstream");
mg.machines[2].childRegistrationUtils.registerChild(pt1, "downstream");
//mg.setMode("prioritycontrol"); Object.keys(mg.machines).forEach(machineId => {
mg.machines[machineId].childRegistrationUtils.registerChild(pt1, "downstream");
});
mg.setMode("prioritycontrol");
mg.setScaling("normalized"); mg.setScaling("normalized");
const absMax = mg.dynamicTotals.flow.max; const absMax = mg.dynamicTotals.flow.max;
@@ -992,14 +1117,13 @@ async function makeMachines(){
const percMax = 100; const percMax = 100;
try{ try{
/* /*
for(let demand = mg.dynamicTotals.flow.min ; demand <= mg.dynamicTotals.flow.max ; demand += 2){ for(let demand = mg.dynamicTotals.flow.min ; demand <= mg.dynamicTotals.flow.max ; demand += 2){
//set pressure //set pressure
console.log("------------------------------------"); console.log("------------------------------------");
await mg.handleInput("parent",demand); await mg.handleInput("parent",demand);
pt1.calculateInput(1400); pt1.calculateInput(1400);
console.log("Waiting for 0.2 sec ");
//await new Promise(resolve => setTimeout(resolve, 200)); //await new Promise(resolve => setTimeout(resolve, 200));
console.log("------------------------------------"); console.log("------------------------------------");
@@ -1012,24 +1136,24 @@ async function makeMachines(){
await mg.handleInput("parent",demand); await mg.handleInput("parent",demand);
pt1.calculateInput(1400); pt1.calculateInput(1400);
console.log("Waiting for 0.2 sec ");
//await new Promise(resolve => setTimeout(resolve, 200)); //await new Promise(resolve => setTimeout(resolve, 200));
console.log("------------------------------------"); console.log("------------------------------------");
} }
//*/ //*//*
/*
for(let demand = 0 ; demand <= 100 ; demand += 1){ for(let demand = 0 ; demand <= 50 ; demand += 1){
//set pressure //set pressure
console.log(`processing demand of ${demand}`); console.log(`TESTING: processing demand of ${demand}`);
await mg.handleInput("parent",demand); await mg.handleInput("parent",demand);
console.log(mg.machines[1].state.getCurrentState()); Object.keys(mg.machines).forEach(machineId => {
console.log(mg.machines[2].state.getCurrentState()); console.log(mg.machines[machineId].state.getCurrentState());
});
console.log(`updating pressure to 1400 mbar`);
pt1.calculateInput(1400); pt1.calculateInput(1400);
console.log("Waiting for 0.2 sec ");
//await new Promise(resolve => setTimeout(resolve, 200));
console.log("------------------------------------"); console.log("------------------------------------");
} }