This commit is contained in:
znetsixe
2025-11-25 15:10:36 +01:00
parent b49f0c3ed2
commit f4cb329597
2 changed files with 353 additions and 314 deletions

View File

@@ -411,10 +411,8 @@ class MachineGroup {
return { bestCombination, bestPower, bestFlow, bestCog };
}
/**
* Estimate the local dP/dQ slopes around the BEP for the provided machine.
* A gentle +/- delta perturbation is used to keep calling code self-contained.
*/
// Estimate the local dP/dQ slopes around the BEP for the provided machine.
estimateSlopesAtBEP(machine, Q_BEP, delta = 1.0) {
const fallback = {
slopeLeft: 0,
@@ -424,65 +422,48 @@ class MachineGroup {
P_BEP: 0
};
try {
if (!machine || !machine.hasCurve || !machine.predictFlow) {
this.logger.warn(`estimateSlopesAtBEP: invalid machine input provided.`);
return fallback;
}
const minFlow = machine.predictFlow.currentFxyYMin;
const maxFlow = machine.predictFlow.currentFxyYMax;
const span = Math.max(0, maxFlow - minFlow);
const normalizedCog = Math.max(0, Math.min(1, machine.NCog || 0));
const targetBEP = Q_BEP ?? (minFlow + span * normalizedCog);
const clampFlow = (flow) => Math.min(maxFlow, Math.max(minFlow, flow)); // ensure within bounds using small helper function
const center = clampFlow(targetBEP);
const deltaSafe = Math.max(delta, 0.01);
const leftFlow = clampFlow(center - deltaSafe);
const rightFlow = clampFlow(center + deltaSafe);
const powerAt = (flow) => machine.inputFlowCalcPower(flow); // helper to get power at a given flow
const P_center = powerAt(center);
const P_left = powerAt(leftFlow);
const P_right = powerAt(rightFlow);
const slopeLeft = (P_center - P_left) / Math.max(1e-6, center - leftFlow);
const slopeRight = (P_right - P_center) / Math.max(1e-6, rightFlow - center);
const alpha = Math.max(1e-6, (Math.abs(slopeLeft) + Math.abs(slopeRight)) / 2);
const minFlow = machine.predictFlow.currentFxyYMin;
const maxFlow = machine.predictFlow.currentFxyYMax;
const span = Math.max(0, maxFlow - minFlow);
const normalizedCog = Math.max(0, Math.min(1, machine.NCog || 0));
const targetBEP = Q_BEP ?? (minFlow + span * normalizedCog);
const clampFlow = (flow) => Math.min(maxFlow, Math.max(minFlow, flow));
const center = clampFlow(targetBEP);
const deltaSafe = Math.max(delta, 0.01);
const leftFlow = clampFlow(center - deltaSafe);
const rightFlow = clampFlow(center + deltaSafe);
const powerAt = (flow) => {
try {
return machine.inputFlowCalcPower(flow);
} catch (error) {
this.logger.warn(`estimateSlopesAtBEP: failed power calc for ${machine.config?.general?.id}: ${error.message}`);
return 0;
}
};
return {
slopeLeft,
slopeRight,
alpha,
Q_BEP: center,
P_BEP: P_center
};
const P_center = powerAt(center);
const P_left = powerAt(leftFlow);
const P_right = powerAt(rightFlow);
const slopeLeft = (P_center - P_left) / Math.max(1e-6, center - leftFlow);
const slopeRight = (P_right - P_center) / Math.max(1e-6, rightFlow - center);
const alpha = Math.max(1e-6, (Math.abs(slopeLeft) + Math.abs(slopeRight)) / 2);
return {
slopeLeft,
slopeRight,
alpha,
Q_BEP: center,
P_BEP: P_center
};
} catch (err) {
this.logger.warn(`estimateSlopesAtBEP failed: ${err.message}`);
return fallback;
}
}
/**
* Redistribute remaining demand using slope-based weights so flatter curves attract more flow.
*/
//Redistribute remaining demand using slope-based weights so flatter curves attract more flow.
redistributeFlowBySlope(pumpInfos, flowDistribution, delta, directional = true) {
const tolerance = 1e-3;
let remaining = delta;
const entryMap = new Map(flowDistribution.map(entry => [entry.machineId, entry]));
const tolerance = 1e-3; // Small tolerance to avoid infinite loops
let remaining = delta; // Remaining flow to distribute
const entryMap = new Map(flowDistribution.map(entry => [entry.machineId, entry])); // Map for quick access
// Loop until remaining flow is within tolerance
while (Math.abs(remaining) > tolerance) {
const increasing = remaining > 0;
const increasing = remaining > 0; // Determine if we are increasing or decreasing flow
// Build candidates with capacity and weight
const candidates = pumpInfos.map(info => {
const entry = entryMap.get(info.id);
if (!entry) { return null; }
const capacity = increasing ? info.maxFlow - entry.flow : entry.flow - info.minFlow;
const capacity = increasing ? info.maxFlow - entry.flow : entry.flow - info.minFlow; // Calculate available capacity based on direction
if (capacity <= tolerance) { return null; }
const slope = increasing
@@ -493,32 +474,31 @@ class MachineGroup {
return { entry, capacity, weight };
}).filter(Boolean);
if (!candidates.length) { break; }
if (!candidates.length) { break; } // No candidates available, exit loop
const weightSum = candidates.reduce((sum, candidate) => sum + candidate.weight * candidate.capacity, 0);
if (weightSum <= 0) { break; }
const weightSum = candidates.reduce((sum, candidate) => sum + candidate.weight * candidate.capacity, 0); // weighted sum of capacities
if (weightSum <= 0) { break; } // Avoid division by zero
let progress = 0;
// Distribute remaining flow among candidates based on their weights and capacities
candidates.forEach(candidate => {
let share = (candidate.weight * candidate.capacity / weightSum) * Math.abs(remaining);
share = Math.min(share, candidate.capacity);
if (share <= 0) { return; }
share = Math.min(share, candidate.capacity); // Ensure we don't exceed capacity
if (share <= 0) { return; } // Skip if no share to allocate
if (increasing) {
candidate.entry.flow += share;
} else {
candidate.entry.flow -= share;
}
progress += share;
progress += share; // Track total progress made in this iteration
});
if (progress <= tolerance) { break; }
remaining += increasing ? -progress : progress;
remaining += increasing ? -progress : progress; // Update remaining flow to distribute
}
}
/**
* BEP-gravitation based combination finder that biases allocation around each pump's BEP.
*/
// BEP-gravitation based combination finder that biases allocation around each pump's BEP.
calcBestCombinationBEPGravitation(combinations, Qd, method = "BEP-Gravitation-Directional") {
let bestCombination = null;
let bestPower = Infinity;
@@ -534,7 +514,7 @@ class MachineGroup {
const maxFlow = machine.predictFlow.currentFxyYMax;
const span = Math.max(0, maxFlow - minFlow);
const NCog = Math.max(0, Math.min(1, machine.NCog || 0));
const estimatedBEP = minFlow + span * NCog;
const estimatedBEP = minFlow + span * NCog; // Estimated BEP flow based on current curve
const slopes = this.estimateSlopesAtBEP(machine, estimatedBEP);
return {
id: machineId,
@@ -547,15 +527,17 @@ class MachineGroup {
};
});
// Skip if no pumps in combination
if (pumpInfos.length === 0) { return; }
// Start at BEP flows
const flowDistribution = pumpInfos.map(info => ({
machineId: info.id,
flow: Math.min(info.maxFlow, Math.max(info.minFlow, info.Q_BEP))
}));
}));
let totalFlow = flowDistribution.reduce((sum, entry) => sum + entry.flow, 0);
const delta = Qd - totalFlow;
let totalFlow = flowDistribution.reduce((sum, entry) => sum + entry.flow, 0); // Initial total flow
const delta = Qd - totalFlow; // Difference to target demand
if (Math.abs(delta) > 1e-6) {
this.redistributeFlowBySlope(pumpInfos, flowDistribution, delta, directional);
}
@@ -1225,8 +1207,8 @@ class MachineGroup {
}
module.exports = MachineGroup;
/*
const {coolprop} = require('generalFunctions');
const Machine = require('../../rotatingMachine/src/specificClass');
const Measurement = require('../../measurement/src/specificClass');
const specs = require('../../generalFunctions/datasets/assetData/curves/hidrostal-H05K-S03R.json');