Compare commits

..

14 Commits

Author SHA1 Message Date
681856104d Merge pull request 'changed colours, description based on s88' (#2) from dev-Rene into main
Reviewed-on: #2
2025-10-16 13:22:56 +00:00
znetsixe
e0526250c2 changed colours, description based on s88 2025-10-14 13:52:18 +02:00
c0e4539b50 Merge pull request 'dev-Rene' (#1) from dev-Rene into main
Reviewed-on: #1
2025-10-06 14:15:42 +00:00
znetsixe
426d45890f ok 2025-10-05 07:56:35 +02:00
znetsixe
8c59a921d5 syncing 2025-10-05 07:55:23 +02:00
Rene De ren
15501e8b1d updates from laptop 2025-10-03 15:33:37 +02:00
znetsixe
b4364094c6 Stable version of machinegroup control 2025-10-02 17:08:41 +02:00
znetsixe
a55c6bdbea fixed pressure updates from machines. Everything seems to be working again. 2025-09-23 15:50:40 +02:00
znetsixe
ac9d1b4fdd added test file 2025-09-23 15:12:01 +02:00
znetsixe
cbc0840f0c added testfile fixing bugs 2025-09-23 15:03:57 +02:00
znetsixe
c62071992d working on a stable version 2025-09-23 11:19:22 +02:00
znetsixe
ffab553f7e physicalPosition 1D update 2025-09-05 16:20:22 +02:00
znetsixe
078a0d80dc updated function for registration of machines 2025-09-04 17:07:18 +02:00
znetsixe
dc1fb500c0 license update 2025-08-07 13:52:56 +02:00
5 changed files with 711 additions and 216 deletions

102
LICENSE
View File

@@ -1,15 +1,97 @@
MIT License
OPENBARE LICENTIE VAN DE EUROPESE UNIE v. 1.2.
EUPL © Europese Unie 2007, 2016
Deze openbare licentie van de Europese Unie („EUPL”) is van toepassing op het werk (zoals hieronder gedefinieerd) dat onder de voorwaarden van deze licentie wordt verstrekt. Elk gebruik van het werk dat niet door deze licentie is toegestaan, is verboden (voor zover dit gebruik valt onder een recht van de houder van het auteursrecht op het werk). Het werk wordt verstrekt onder de voorwaarden van deze licentie wanneer de licentiegever (zoals hieronder gedefinieerd), direct volgend op de kennisgeving inzake het auteursrecht op het werk, de volgende kennisgeving opneemt:
In licentie gegeven krachtens de EUPL
of op een andere wijze zijn bereidheid te kennen heeft gegeven krachtens de EUPL in licentie te geven.
Copyright (c) 2025 Rene De Ren
1.Definities
In deze licentie wordt verstaan onder:
— „de licentie”:de onderhavige licentie;
— „het oorspronkelijke werk”:het werk dat of de software die door de licentiegever krachtens deze licentie wordt verspreid of medegedeeld, en dat/die beschikbaar is als broncode en, in voorkomend geval, ook als uitvoerbare code;
— „bewerkingen”:de werken of software die de licentiehouder kan creëren op grond van het oorspronkelijke werk of wijzigingen ervan. In deze licentie wordt niet gedefinieerd welke mate van wijziging of afhankelijkheid van het oorspronkelijke werk vereist is om een werk als een bewerking te kunnen aanmerken; dat wordt bepaald conform het auteursrecht dat van toepassing is in de in artikel 15 bedoelde staat;
— „het werk”:het oorspronkelijke werk of de bewerkingen ervan;
— „de broncode”:de voor mensen leesbare vorm van het werk, die het gemakkelijkste door mensen kan worden bestudeerd en gewijzigd;
— „de uitvoerbare code”:elke code die over het algemeen is gecompileerd en is bedoeld om door een computer als een programma te worden uitgevoerd;
— „de licentiegever”:de natuurlijke of rechtspersoon die het werk krachtens de licentie verspreidt of mededeelt;
— „bewerker(s)”:elke natuurlijke of rechtspersoon die het werk krachtens de licentie wijzigt of op een andere wijze bijdraagt tot de totstandkoming van een bewerking;
— „de licentiehouder” of „u”:elke natuurlijke of rechtspersoon die het werk onder de voorwaarden van de licentie gebruikt; — „verspreiding” of „mededeling”:het verkopen, geven, uitlenen, verhuren, verspreiden, mededelen, doorgeven, of op een andere wijze online of offline beschikbaar stellen van kopieën van het werk of het verlenen van toegang tot de essentiële functies ervan ten behoeve van andere natuurlijke of rechtspersonen.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to use,
copy, modify, merge, publish, and distribute the Software for **personal, scientific, or educational purposes**, subject to the following conditions:
2.Draagwijdte van de uit hoofde van de licentie verleende rechten
De licentiegever verleent u hierbij een wereldwijde, royaltyvrije, niet-exclusieve, voor een sublicentie in aanmerking komende licentie, om voor de duur van het aan het oorspronkelijke werk verbonden auteursrecht, het volgende te doen:
— het werk in alle omstandigheden en voor ongeacht welk doel te gebruiken;
— het werk te verveelvoudigen;
— het werk te wijzigen en op grond van het werk bewerkingen te ontwikkelen;
— het werk aan het publiek mede te delen, waaronder het recht om het werk of kopieën ervan aan het publiek ter beschikking te stellen of te vertonen, en het werk, in voorkomend geval, in het openbaar uit te voeren;
— het werk of kopieën ervan te verspreiden;
— het werk of kopieën ervan uit te lenen en te verhuren;
— de rechten op het werk of op kopieën ervan in sublicentie te geven.
Deze rechten kunnen worden uitgeoefend met gebruikmaking van alle thans bekende of nog uit te vinden media, dragers en formaten, voor zover het toepasselijke recht dit toestaat. In de landen waar immateriële rechten van toepassing zijn, doet de licentiegever afstand van zijn recht op uitoefening van zijn immateriële rechten in de mate die door het toepasselijke recht wordt toegestaan teneinde een doeltreffende uitoefening van de bovenvermelde in licentie gegeven economische rechten mogelijk te maken. De licentiegever verleent de licentiehouder een royaltyvrij, niet-exclusief gebruiksrecht op alle octrooien van de licentiegever, voor zover dit noodzakelijk is om de uit hoofde van deze licentie verleende rechten op het werk te gebruiken.
**Commercial use of the Software or any derivative work is explicitly prohibited without prior written consent from the authors.**
This includes but is not limited to resale, inclusion in paid products or services, and monetized distribution.
Any commercial usage must be governed by a shared license or explicit contractual agreement with the authors.
3.Mededeling van de broncode
De licentiegever kan het werk verstrekken in zijn broncode of als uitvoerbare code. Indien het werk als uitvoerbare code wordt verstrekt, verstrekt de licentiegever bij elke door hem verspreide kopie van het werk tevens een machinaal leesbare kopie van de broncode van het werk of geeft hij in een mededeling, volgende op de bij het werk gevoegde auteursrechtelijke kennisgeving, de plaats aan waar de broncode gemakkelijk en vrij toegankelijk is, zolang de licentiegever het werk blijft verspreiden of mededelen.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4.Beperkingen van het auteursrecht
Geen enkele bepaling in deze licentie heeft ten doel de licentiehouder het recht te ontnemen een beroep te doen op een uitzondering op of een beperking van de exclusieve rechten van de rechthebbenden op het werk, of op de uitputting van die rechten of andere toepasselijke beperkingen daarvan.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED...
5.Verplichtingen van de licentiehouder
De verlening van de bovenvermelde rechten is onderworpen aan een aantal aan de licentiehouder opgelegde beperkingen en verplichtingen. Het gaat om de onderstaande verplichtingen.
Attributierecht: de licentiehouder moet alle auteurs-, octrooi- of merkenrechtelijke kennisgevingen onverlet laten alsook alle kennisgevingen die naar de licentie en de afwijzing van garanties verwijzen. De licentiehouder moet een afschrift van deze kennisgevingen en een afschrift van de licentie bij elke kopie van het werk voegen die hij verspreidt of mededeelt. De licentiehouder moet in elke bewerking duidelijk aangeven dat het werk is gewijzigd, en eveneens de datum van wijziging vermelden.
Copyleftclausule: wanneer de licentiehouder kopieën van het oorspronkelijke werk of bewerkingen verspreidt of mededeelt, geschiedt die verspreiding of mededeling onder de voorwaarden van deze licentie of van een latere versie van deze licentie, tenzij het oorspronkelijke werk uitdrukkelijk alleen onder deze versie van de licentie wordt verspreid — bijvoorbeeld door de mededeling „alleen EUPL v. 1.2”. De licentiehouder (die licentiegever wordt) kan met betrekking tot het werk of de bewerkingen geen aanvullende bepalingen of voorwaarden opleggen of stellen die de voorwaarden van de licentie wijzigen of beperken.
Verenigbaarheidsclausule: wanneer de licentiehouder bewerkingen of kopieën ervan verspreidt of mededeelt die zijn gebaseerd op het werk en op een ander werk dat uit hoofde van een verenigbare licentie in licentie is gegeven, kan die verspreiding of mededeling geschieden onder de voorwaarden van deze verenigbare licentie. Voor de toepassing van deze clausule wordt onder „verenigbare licentie” verstaan, de licenties die in het aanhangsel bij deze licentie zijn opgesomd. Indien de verplichtingen van de licentiehouder uit hoofde van de verenigbare licentie in strijd zijn met diens verplichtingen uit hoofde van deze licentie, hebben de verplichtingen van de verenigbare licentie voorrang.
Verstrekking van de broncode: bij de verspreiding of mededeling van kopieën van het werk verstrekt de licentiehouder een machinaal leesbare kopie van de broncode of geeft hij aan waar deze broncode gemakkelijk en vrij toegankelijk is, zolang de licentiehouder het werk blijft verspreiden of mededelen.
Juridische bescherming: deze licentie verleent geen toestemming om handelsnamen, handelsmerken, dienstmerken of namen van de licentiegever te gebruiken, behalve wanneer dit op grond van een redelijk en normaal gebruik noodzakelijk is om de oorsprong van het werk te beschrijven en de inhoud van de auteursrechtelijke kennisgeving te herhalen.
6.Auteursketen
De oorspronkelijke licentiegever garandeert dat hij houder is van het hierbij verleende auteursrecht op het oorspronkelijke werk dan wel dat dit hem in licentie is gegeven en dat hij de bevoegdheid heeft de licentie te verlenen. Elke bewerker garandeert dat hij houder is van het auteursrecht op de door hem aan het werk aangebrachte wijzigingen dan wel dat dit hem in licentie is gegeven en dat hij de bevoegdheid heeft de licentie te verlenen. Telkens wanneer u de licentie aanvaardt, verlenen de oorspronkelijke licentiegever en de opeenvolgende bewerkers u een licentie op hun bijdragen aan het werk onder de voorwaarden van deze licentie.
7.Uitsluiting van garantie
Het werk is een werk in ontwikkeling, dat voortdurend door vele bewerkers wordt verbeterd. Het is een onvoltooid werk, dat bijgevolg nog tekortkomingen of programmeerfouten („bugs”) kan vertonen, die onlosmakelijk verbonden zijn met dit soort ontwikkeling. Om die reden wordt het werk op grond van de licentie verstrekt „zoals het is” en zonder enige garantie met betrekking tot het werk te geven, met inbegrip van, maar niet beperkt tot garanties met betrekking tot de verhandelbaarheid, de geschiktheid voor een specifiek doel, de afwezigheid van tekortkomingen of fouten, de nauwkeurigheid, de eerbiediging van andere intellectuele-eigendomsrechten dan het in artikel 6 van deze licentie bedoelde auteursrecht. Deze uitsluiting van garantie is een essentieel onderdeel van de licentie en een voorwaarde voor de verlening van rechten op het werk.
8.Uitsluiting van aansprakelijkheid
Behoudens in het geval van een opzettelijke fout of directe schade aan natuurlijke personen, is de licentiegever in geen enkel geval aansprakelijk voor ongeacht welke directe of indirecte, materiële of immateriële schade die voortvloeit uit de licentie of het gebruik van het werk, met inbegrip van, maar niet beperkt tot schade als gevolg van het verlies van goodwill, verloren werkuren, een computerdefect of computerfout, het verlies van gegevens, of enige andere commerciële schade, zelfs indien de licentiegever werd gewezen op de mogelijkheid van dergelijke schade. De licentiegever is echter aansprakelijk op grond van de wetgeving inzake productaansprakelijkheid, voor zover deze wetgeving op het werk van toepassing is.
9.Aanvullende overeenkomsten
Bij de verspreiding van het werk kunt u ervoor kiezen een aanvullende overeenkomst te sluiten, waarin de verplichtingen of diensten overeenkomstig deze licentie worden omschreven. Indien deze verplichtingen worden aanvaard, kunt u echter alleen in eigen naam en onder eigen verantwoordelijkheid handelen, en dus niet in naam van de oorspronkelijke licentiegever of een bewerker, en kunt u voorts alleen handelen indien u ermee instemt alle bewerkers schadeloos te stellen, te verdedigen of te vrijwaren met betrekking tot de aansprakelijkheid van of vorderingen tegen deze bewerkers op grond van het feit dat u een garantie of aanvullende aansprakelijkheid hebt aanvaard.
10.Aanvaarding van de licentie
De bepalingen van deze licentie kunnen worden aanvaard door te klikken op het pictogram „Ik ga akkoord”, dat zich bevindt onderaan het venster waarin de tekst van deze licentie is weergegeven, of door overeenkomstig de toepasselijke wetsbepalingen op een soortgelijke wijze met de licentie in te stemmen. Door op dat pictogram te klikken geeft u aan dat u deze licentie en alle voorwaarden ervan ondubbelzinnig en onherroepelijk aanvaardt. Evenzo aanvaardt u onherroepelijk deze licentie en alle voorwaarden ervan door uitoefening van de rechten die u in artikel 2 van deze licentie zijn verleend, zoals het gebruik van het werk, het creëren door u van een bewerking of de verspreiding of mededeling door u van het werk of kopieën ervan.
11.Voorlichting van het publiek
Indien u het werk verspreidt of mededeelt door middel van elektronische communicatiemiddelen (bijvoorbeeld door voor te stellen het werk op afstand te downloaden), moet het distributiekanaal of het medium (bijvoorbeeld een website) het publiek ten minste de gegevens verschaffen die door het toepasselijke recht zijn voorgeschreven met betrekking tot de licentiegever, de licentie en de wijze waarop deze kan worden geraadpleegd, gesloten, opgeslagen en gereproduceerd door de licentiehouder.
12.Einde van de licentie
De licentie en de uit hoofde daarvan verleende rechten eindigen automatisch bij elke inbreuk door de licentiehouder op de voorwaarden van de licentie. Dit einde beëindigt niet de licenties van personen die het werk van de licentiehouder krachtens de licentie hebben ontvangen, mits deze personen zich volledig aan de licentie houden.
13.Overige
Onverminderd artikel 9 vormt de licentie de gehele overeenkomst tussen de partijen met betrekking tot het werk. Indien een bepaling van de licentie volgens het toepasselijke recht ongeldig is of niet uitvoerbaar is, doet dit geen afbreuk aan de geldigheid of uitvoerbaarheid van de licentie in haar geheel. Deze bepaling dient zodanig te worden uitgelegd of gewijzigd dat zij geldig en uitvoerbaar wordt. De Europese Commissie kan, voor zover dit noodzakelijk en redelijk is, versies in andere talen of nieuwe versies van deze licentie of geactualiseerde versies van dit aanhangsel publiceren, zonder de draagwijdte van de uit hoofde van de licentie verleende rechten te beperken. Nieuwe versies van de licentie zullen worden gepubliceerd met een uniek versienummer. Alle door de Europese Commissie goedgekeurde taalversies van deze licentie hebben dezelfde waarde. De partijen kunnen zich beroepen op de taalversie van hun keuze.
14.Bevoegd gerecht
Onverminderd specifieke overeenkomsten tussen de partijen,
— vallen alle geschillen tussen de instellingen, organen en instanties van de Europese Unie, als licentiegeefster, en een licentiehouder in verband met de uitlegging van deze licentie onder de bevoegdheid van het Hof van Justitie van de Europese Unie, conform artikel 272 van het Verdrag betreffende de werking van de Europese Unie,
— vallen alle geschillen tussen andere partijen in verband met de uitlegging van deze licentie onder de uitsluitende bevoegdheid van het bevoegde gerecht van de plaats waar de licentiegever is gevestigd of zijn voornaamste activiteit uitoefent.
15.Toepasselijk recht
Onverminderd specifieke overeenkomsten tussen de partijen,
— wordt deze licentie beheerst door het recht van de lidstaat van de Europese Unie waar de licentiegever zijn statutaire zetel, verblijfplaats of hoofdkantoor heeft,
— wordt deze licentie beheerst door het Belgische recht indien de licentiegever geen statutaire zetel, verblijfplaats of hoofdkantoor heeft in een lidstaat van de Europese Unie.
Aanhangsel
„Verenigbare licenties” in de zin van artikel 5 EUPL zijn:
— GNU General Public License (GPL) v. 2, v. 3
— GNU Affero General Public License (AGPL) v. 3
— Open Software License (OSL) v. 2.1, v. 3.0
— Eclipse Public License (EPL) v. 1.0
— CeCILL v. 2.0, v. 2.1
— Mozilla Public Licence (MPL) v. 2
— GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
— Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) voor andere werken dan software
— European Union Public Licence (EUPL) v. 1.1, v. 1.2
— Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) of Strong Reciprocity (LiLiQ-R+).
De Europese Commissie kan dit aanhangsel actualiseren in geval van latere versies van de bovengenoemde licenties zonder dat er een nieuwe EUPL-versie wordt ontwikkeld, zolang die versies de uit hoofde van artikel 2 van deze licentie verleende rechten verlenen en ze de betrokken broncode beschermen tegen exclusieve toe-eigening.
Voor alle andere wijzigingen van of aanvullingen op dit aanhangsel is de ontwikkeling van een nieuwe EUPL-versie vereist.

View File

@@ -1,15 +1,12 @@
<!--
brabantse delta kleuren:
#eaf4f1
#86bbdd
#bad33b
#0c99d9
#a9daee
#0f52a5
#50a8d9
#cade63
#4f8582
#c4cce0
| S88-niveau | Primair (blokkleur) | Tekstkleur |
| ---------------------- | ------------------- | ---------- |
| **Area** | `#0f52a5` | wit |
| **Process Cell** | `#0c99d9` | wit |
| **Unit** | `#50a8d9` | zwart |
| **Equipment (Module)** | `#86bbdd` | zwart |
| **Control Module** | `#a9daee` | zwart |
-->
<script src="/machineGroupControl/menu.js"></script> <!-- Load the menu script for dynamic dropdowns -->
<script src="/machineGroupControl/configData.js"></script> <!-- Load the config script for node information -->
@@ -17,7 +14,7 @@
<script>
RED.nodes.registerType('machineGroupControl',{
category: "EVOLV",
color: "#eaf4f1",
color: "#50a8d9",
defaults: {
// Define default properties
name: { value: "" },
@@ -26,16 +23,20 @@
enableLog: { value: false },
logLevel: { value: "error" },
// Physical aspect
//physicalAspect
positionVsParent: { value: "" },
positionLabel: { value: "" },
positionIcon: { value: "" },
hasDistance: { value: false },
distance: { value: 0 },
distanceUnit: { value: "m" },
distanceDescription: { value: "" }
},
inputs:1,
outputs:3,
inputLabels: ["Input"],
outputLabels: ["process", "dbase", "parent"],
icon: "font-awesome/fa-tachometer",
icon: "font-awesome/fa-cogs",
label: function () {
return this.positionIcon + " " + "machineGroup";

288
src/groupcontrol.test.js Normal file
View File

@@ -0,0 +1,288 @@
// ...existing code...
const MachineGroup = require('./specificClass.js');
const Machine = require('../../rotatingMachine/src/specificClass');
const Measurement = require('../../measurement/src/specificClass');
const specs = require('../../generalFunctions/datasets/assetData/curves/hidrostal-H05K-S03R.json');
const stateConfig = { time:{starting:0,warmingup:0,stopping:0,coolingdown:0}, movement:{speed:1000,mode:"staticspeed"} };
const ptConfig = {
general:{ logging:{enabled:false,logLevel:"warn"}, name:"testpt", id:"pt-1", unit:"mbar" },
functionality:{ softwareType:"measurement", role:"sensor" },
asset:{ category:"sensor", type:"pressure", model:"testmodel", supplier:"vega", unit:"mbar" },
scaling:{ absMin:0, absMax:4000 }
};
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"]
}
},
sequences:{
startup:["starting","warmingup","operational"],
shutdown:["stopping","coolingdown","idle"],
emergencystop:["emergencystop","off"],
boot:["idle","starting","warmingup","operational"]
}
};
}
async function bootstrapGroup() {
const groupCfg = {
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 };
}
function captureState(mg,label){
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
}
};
}
async function testNormalizedScaling(mg,pt){
const label = "Normalized scaling tracks expected flow";
try{
mg.setScaling("normalized");
const dynamic = mg.calcDynamicTotals();
const checkpoints = [0,10,25,50,75,100];
for (const demand of checkpoints){
await mg.handleInput("parent", demand);
pt.calculateInput(1400);
await sleep(20);
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);
if(!approxEqual(totals, expected, 2)){
throw new Error(`Flow ${totals.toFixed(2)} outside expectation ${expected.toFixed(2)} @ ${demand}%`);
}
}
logPass(label);
}catch(err){ logFail(label, err); }
}
async function testAbsoluteScaling(mg,pt){
const label = "Absolute scaling accepts direct flow targets";
try{
mg.setScaling("absolute");
mg.setMode("optimalcontrol");
const absMin = mg.dynamicTotals.flow.min;
const absMax = mg.dynamicTotals.flow.max;
const demandPoints = [absMin, absMin+20, (absMin+absMax)/2, absMax-20];
for(const setpoint of demandPoints){
await mg.handleInput("parent", setpoint);
pt.calculateInput(1400);
await sleep(20);
const flow = mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0;
if(!approxEqual(flow, setpoint, 2)){
throw new Error(`Flow ${flow.toFixed(2)} != demand ${setpoint.toFixed(2)}`);
}
}
logPass(label);
}catch(err){ logFail(label, err); }
}
async function testModeTransitions(mg,pt){
const label = "Mode transitions keep machines responsive";
try{
const modes = ["optimalcontrol","prioritycontrol","prioritypercentagecontrol"];
mg.setScaling("normalized");
for(const mode of modes){
mg.setMode(mode);
await mg.handleInput("parent", 50);
pt.calculateInput(1300);
await sleep(20);
const snapshot = captureState(mg, mode);
const active = snapshot.machines.filter(m => m.state !== "idle");
if(active.length === 0){
throw new Error(`No active machines after switching to ${mode}`);
}
}
logPass(label);
}catch(err){ logFail(label, err); }
}
async function testRampBehaviour(mg,pt){
const label = "Ramp up/down keeps monotonic flow";
try{
mg.setMode("optimalcontrol");
mg.setScaling("normalized");
const upDemands = [0,20,40,60,80,100];
let lastFlow = 0;
for(const demand of upDemands){
await mg.handleInput("parent", demand);
pt.calculateInput(1500);
await sleep(15);
const flow = mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0;
if(flow < lastFlow - 1){
throw new Error(`Flow decreased during ramp up: ${flow.toFixed(2)} < ${lastFlow.toFixed(2)}`);
}
lastFlow = flow;
}
const downDemands = [100,80,60,40,20,0];
lastFlow = Infinity;
for(const demand of downDemands){
await mg.handleInput("parent", demand);
pt.calculateInput(1200);
await sleep(15);
const flow = mg.measurements.type("flow").variant("predicted").position("downstream").getCurrentValue() || 0;
if(flow > lastFlow + 1){
throw new Error(`Flow increased during ramp down: ${flow.toFixed(2)} > ${lastFlow.toFixed(2)}`);
}
lastFlow = flow;
}
logPass(label);
}catch(err){ logFail(label, err); }
}
async function testPressureAdaptation(mg,pt){
const label = "Pressure changes update predictions";
try{
mg.setMode("optimalcontrol");
mg.setScaling("normalized");
const pressures = [800,1200,1600,2000];
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) {
pt.calculateInput(pressure);
await sleep(15);
for (const demand of demands) {
mg.setMode("optimalcontrol");
await mg.handleInput("parent", demand);
pt.calculateInput(pressure);
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 run(){
console.log("🚀 Starting machine-group integration tests...");
const { mg, pt } = await bootstrapGroup();
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.");
}
run().catch(err => {
console.error("💥 Test harness crashed:", err);
});
// ...existing code...
// Run all tests
run();

View File

@@ -58,28 +58,32 @@ class nodeClass {
}
_updateNodeStatus() {
//console.log('Updating node status...');
const mg = this.source;
const mode = mg.mode;
const scaling = mg.scaling;
const totalFlow =
Math.round(
mg.measurements
.type("flow")
.variant("predicted")
.position("downstream")
.getCurrentValue() * 1
) / 1;
const totalPower =
Math.round(
mg.measurements
.type("power")
.variant("predicted")
.position("upstream")
.getCurrentValue() * 1
) / 1;
// Calculate total capacity based on available machines
const availableMachines = Object.values(mg.machines).filter((machine) => {
// Add safety checks for measurements
const totalFlow = mg.measurements
?.type("flow")
?.variant("predicted")
?.position("downstream")
?.getCurrentValue() || 0;
const totalPower = mg.measurements
?.type("power")
?.variant("predicted")
?.position("atEquipment")
?.getCurrentValue() || 0;
// Calculate total capacity based on available machines with safety checks
const availableMachines = Object.values(mg.machines || {}).filter((machine) => {
// Safety check: ensure machine and machine.state exist
if (!machine || !machine.state || typeof machine.state.getCurrentState !== 'function') {
console.warn(`Machine missing or invalid:`, machine?.config?.general?.id || 'unknown');
return false;
}
const state = machine.state.getCurrentState();
const mode = machine.currentMode;
return !(
@@ -89,29 +93,27 @@ class nodeClass {
);
});
const totalCapacity = Math.round(mg.dynamicTotals.flow.max * 1) / 1;
const totalCapacity = Math.round((mg.dynamicTotals?.flow?.max || 0) * 1) / 1;
// Determine overall status based on available machines
const status =
availableMachines.length > 0
const status = availableMachines.length > 0
? `${availableMachines.length} machine(s) connected`
: "No machines";
let scalingSymbol = "";
switch (scaling.toLowerCase()) {
switch ((scaling || "").toLowerCase()) {
case "absolute":
scalingSymbol = "Ⓐ"; // Clear symbol for Absolute mode
scalingSymbol = "Ⓐ";
break;
case "normalized":
scalingSymbol = "Ⓝ"; // Clear symbol for Normalized mode
scalingSymbol = "Ⓝ";
break;
default:
scalingSymbol = mode;
scalingSymbol = mode || "";
break;
}
// Generate status text in a single line
const text = ` ${mode} | ${scalingSymbol}: 💨=${totalFlow}/${totalCapacity} | ⚡=${totalPower} | ${status}`;
const text = ` ${mode || 'Unknown'} | ${scalingSymbol}: 💨=${Math.round(totalFlow)}/${totalCapacity} | ⚡=${Math.round(totalPower)} | ${status}`;
return {
fill: availableMachines.length > 0 ? "green" : "red",
@@ -197,24 +199,36 @@ class nodeClass {
const RED = this.RED;
switch (msg.topic) {
case "registerChild":
//console.log(`Registering child in mgc: ${msg.payload}`);
console.log(`Registering child in mgc: ${msg.payload}`);
const childId = msg.payload;
const childObj = RED.nodes.getNode(childId);
// Debug: Check what we're getting
console.log(`Child object:`, childObj ? 'found' : 'NOT FOUND');
console.log(`Child source:`, childObj?.source ? 'exists' : 'MISSING');
if (childObj?.source) {
console.log(`Child source type:`, childObj.source.constructor.name);
console.log(`Child has state:`, !!childObj.source.state);
}
mg.childRegistrationUtils.registerChild(
childObj.source,
msg.positionVsParent
);
// Debug: Check machines after registration
console.log(`Total machines after registration:`, Object.keys(mg.machines || {}).length);
break;
case "setMode":
const mode = msg.payload;
const source = "parent";
mg.setMode(source, mode);
mg.setMode(mode);
break;
case "setScaling":
const scaling = msg.payload;
mg.setScaling(scaling);
break;
case "Qd":

View File

@@ -1,41 +1,3 @@
/**
* @file machine.js
*
* Permission is hereby granted to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to use it for personal
* or non-commercial purposes, with the following restrictions:
*
* 1. **No Copying or Redistribution**: The Software or any of its parts may not
* be copied, merged, distributed, sublicensed, or sold without explicit
* prior written permission from the author.
*
* 2. **Commercial Use**: Any use of the Software for commercial purposes requires
* a valid license, obtainable only with the explicit consent of the author.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
* OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Ownership of this code remains solely with the original author. Unauthorized
* use of this Software is strictly prohibited.
*
* @summary A class to interact and manipulate machines with a non-euclidian curve
* @description A class to interact and manipulate machines with a non-euclidian curve
* @module machineGroup
* @exports machineGroup
* @version 0.1.0
* @since 0.1.0
*
* Author:
* - Rene De Ren
* Email:
* - r.de.ren@brabantsedelta.nl
*/
//load local dependencies
const EventEmitter = require("events");
const {logger,configUtils,configManager, MeasurementContainer, interpolation , childRegistrationUtils} = require('generalFunctions');
@@ -74,11 +36,36 @@ 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) {
this.logger.debug('Setting up childs specific for this class');
if(softwareType == "machine"){
// Check if the machine is already registered
this.machines[child.config.general.id] === undefined ? this.machines[child.config.general.id] = child : this.logger.warn(`Machine ${child.config.general.id} is already registered.`);
//listen for machine pressure changes
this.logger.debug(`Listening for pressure changes from machine ${child.config.general.id}`);
child.measurements.emitter.on("pressure.measured.differential", (eventData) => {
this.logger.debug(`Pressure update from ${child.config.general.id}: ${eventData.value} ${eventData.unit}`);
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() {
@@ -102,6 +89,7 @@ class MachineGroup {
if( maxPower > totals.power.max ){ totals.power.max = maxPower; }
});
//surplus machines for max flow and power
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; }
@@ -110,6 +98,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;
}
@@ -117,26 +128,39 @@ class MachineGroup {
//max and min current flow and power based on their actual pressure curve
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 : ----------`);
Object.values(this.machines).forEach(machine => {
this.logger.debug(`Processing machine with id: ${machine.config.general.id}`);
this.logger.debug(`Current pressure settings: ${JSON.stringify(machine.predictFlow.currentF)}`);
//fetch min flow ever seen over all machines
const minFlow = machine.predictFlow.currentFxyYMin;
const maxFlow = machine.predictFlow.currentFxyYMax;
const minPower = machine.predictPower.currentFxyYMin;
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( minPower < dynamicTotals.power.min ){ dynamicTotals.power.min = minPower; }
dynamicTotals.flow.max += maxFlow;
dynamicTotals.power.max += maxPower;
dynamicTotals.flow.act += actFlow;
dynamicTotals.power.act += actPower;
//fetch total Normalized Cog over all machines
dynamicTotals.NCog += machine.NCog;
});
// Place data in object for external use
this.dynamicTotals = dynamicTotals;
return dynamicTotals;
}
@@ -166,10 +190,16 @@ class MachineGroup {
}
handlePressureChange() {
this.logger.info("Pressure change detected.");
this.calcDynamicTotals();
this.logger.info("---------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>Pressure change detected.");
// 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 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);
}
@@ -198,6 +228,7 @@ class MachineGroup {
checkSpecialCases(machines, Qd) {
Object.values(machines).forEach(machine => {
const state = machine.state.getCurrentState();
const mode = machine.currentMode;
@@ -231,16 +262,13 @@ class MachineGroup {
// Generate all possible subsets of machines (power set)
Object.keys(machines).forEach(machineId => {
machineId = parseInt(machineId);
const state = machines[machineId].state.getCurrentState();
const validSourceForMode = machines[machineId].isValidSourceForMode("parent", "auto");
const validActionForMode = machines[machineId].isValidActionForMode("execSequence", "auto");
// Reasons why a machine is not valid for the combination
if( state === "off" || state === "coolingdown" || state === "stopping" || state === "emergencystop" || !validSourceForMode || !validActionForMode){
if( state === "off" || state === "coolingdown" || state === "stopping" || state === "emergencystop" || !validActionForMode){
return;
}
@@ -335,23 +363,29 @@ class MachineGroup {
}
// -------- Mode and Input Management -------- //
isValidSourceForMode(source, mode) {
const allowedSourcesSet = this.config.mode.allowedSources[mode] || [];
return allowedSourcesSet.has(source);
}
isValidActionForMode(action, mode) {
const allowedActionsSet = this.config.mode.allowedActions[mode] || [];
return allowedActionsSet.has(action);
}
setScaling(scaling) {
const scalingSet = new Set(defaultConfig.scaling.current.rules.values.map( (value) => value.value));
const scalingSet = new Set(this.defaultConfig.scaling.current.rules.values.map( (value) => value.value));
scalingSet.has(scaling)? this.scaling = scaling : this.logger.warn(`${scaling} is not a valid scaling option.`);
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
async optimalControl(Qd, powerCap = Infinity) {
try{
//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
@@ -365,20 +399,25 @@ class MachineGroup {
const maxDownstream = Math.max(...pressures.map(p => p.downstream));
const minUpstream = Math.min(...pressures.map(p => p.upstream));
this.logger.debug(`Max downstream pressure: ${maxDownstream}, Min upstream pressure: ${minUpstream}`);
//set the pressures
Object.entries(this.machines).forEach(([machineId, machine]) => {
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("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
// we need to find a better way to do this but for now it works
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]) => {
acc[machineId] = machine.state.getCurrentState();
return acc;
@@ -401,7 +440,6 @@ class MachineGroup {
// fetch all valid combinations that meet expectations
const combinations = this.validPumpCombinations(this.machines, Qd, powerCap);
//
const bestResult = this.calcBestCombination(combinations, Qd);
if(bestResult.bestCombination === null){
@@ -413,35 +451,36 @@ class MachineGroup {
this.logger.debug(`Moving to demand: ${Qd.toFixed(2)} -> Pumps: [${debugInfo}] => Total Power: ${bestResult.bestPower.toFixed(2)}`);
//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("efficiency").variant("predicted").position("downstream").value(bestResult.bestFlow / bestResult.bestPower);
this.measurements.type("Ncog").variant("predicted").position("downstream").value(bestResult.bestCog);
this.measurements.type("efficiency").variant("predicted").position("atEquipment").value(bestResult.bestFlow / bestResult.bestPower);
this.measurements.type("Ncog").variant("predicted").position("atEquipment").value(bestResult.bestCog);
await Promise.all(Object.entries(this.machines).map(async ([machineId, machine]) => {
// Find the flow for this machine in the best combination
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;
if(pumpInfo !== undefined){
flow = pumpInfo.flow;
} 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;
}
if( (flow <= 0 ) && ( machineStates[machineId] === "operational" || machineStates[machineId] === "accelerating" || machineStates[machineId] === "decelerating" ) ){
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");
}
else if(machineStates[machineId] === "operational" && flow > 0 ){
await machine.handleInput("parent", "flowMovement", flow);
}
if(machineStates[machineId] === "operational" && flow > 0 ){
await machine.handleInput("parent", "flowMovement", flow);
}
}));
}
catch(err){
this.logger.error(err);
@@ -503,7 +542,7 @@ class MachineGroup {
.map(id => ({ id, machine: this.machines[id] }));
} else {
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);
}
return machinesInPriorityOrder;
@@ -512,10 +551,9 @@ class MachineGroup {
filterOutUnavailableMachines(list) {
const newList = list.filter(({ id, machine }) => {
const state = machine.state.getCurrentState();
const validSourceForMode = machine.isValidSourceForMode("parent", "auto");
const validActionForMode = machine.isValidActionForMode("execSequence", "auto");
return !(state === "off" || state === "coolingdown" || state === "stopping" || state === "emergencystop" || !validSourceForMode || !validActionForMode);
return !(state === "off" || state === "coolingdown" || state === "stopping" || state === "emergencystop" || !validActionForMode);
});
return newList;
}
@@ -550,14 +588,6 @@ class MachineGroup {
// Update dynamic totals
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
Qd = this.capFlowDemand(Qd,dynamicTotals);
@@ -651,14 +681,16 @@ class MachineGroup {
this.logger.debug(`Priority control for demand: ${totalFlow.toFixed(2)} -> Active pumps: [${debugInfo}] => Total Power: ${totalPower.toFixed(2)}`);
// 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("efficiency").variant("predicted").position("downstream").value(totalFlow / totalPower);
this.measurements.type("Ncog").variant("predicted").position("downstream").value(totalCog);
this.measurements.type("efficiency").variant("predicted").position("atEquipment").value(totalFlow / totalPower);
this.measurements.type("Ncog").variant("predicted").position("atEquipment").value(totalCog);
this.logger.debug(`Flow distribution: ${JSON.stringify(flowDistribution)}`);
// Apply the flow distribution to machines
await Promise.all(flowDistribution.map(async ({ machineId, flow }) => {
const machine = this.machines[machineId];
this.logger.debug(this.machines[machineId].state);
const currentState = this.machines[machineId].state.getCurrentState();
if (flow <= 0 && (currentState === "operational" || currentState === "accelerating" || currentState === "decelerating")) {
@@ -763,8 +795,10 @@ class MachineGroup {
// fetch and store measurements
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();
if (powerValue !== null) {
totalPower.push(powerValue);
}
@@ -773,10 +807,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));
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));
}
}
@@ -785,49 +820,85 @@ class MachineGroup {
}
}
async handleInput(source, Qd, powerCap = Infinity, priorityList = null) {
async handleInput(source, demand, powerCap = Infinity, priorityList = null) {
if (!this.isValidSourceForMode(source, this.mode)) {
this.logger.warn(`Invalid source ${source} for mode ${this.mode}`);
return;
}
//abort current movements
await this.abortActiveMovements("new demand received");
const scaling = this.scaling;
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) {
case "absolute":
// No scaling needed but cap range
if (Qd < this.absoluteTotals.flow.min) {
this.logger.warn(`Flow demand ${Qd} is below minimum possible flow ${this.absoluteTotals.flow.min}. Capping to minimum flow.`);
Qd = this.absoluteTotals.flow.min;
} 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 (isNaN(demandQ)) {
this.logger.warn(`Invalid absolute flow demand: ${demand}. Must be a number.`);
demandQout = 0;
return;
}
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;
case "normalized":
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
Qd = this.interpolation.interpolate_lin_single_point(Qd, 0, 100, this.dynamicTotals.flow.min, this.dynamicTotals.flow.max);
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;
}
// Execute control based on mode
switch(mode) {
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;
case "prioritypercentagecontrol":
this.logger.debug(`Calculating prio percentage control. Input flow demand: ${demandQ} scaling : ${scaling} -> ${demandQout}`);
if(scaling !== "normalized"){
this.logger.warn("Priority percentage control is only valid with normalized scaling.");
return;
}
await this.prioPercentageControl(rawInput,priorityList);
await this.prioPercentageControl(demandQout,priorityList);
break;
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;
default:
this.logger.warn(`${mode} is not a valid mode.`);
break;
}
@@ -838,8 +909,14 @@ class MachineGroup {
}
setMode(source,mode) {
this.isValidSourceForMode(source, mode) ? this.mode = mode : this.logger.warn(`Invalid source ${source} for mode ${mode}`);
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) {
this.mode = mode;
}
getOutput() {
@@ -852,6 +929,7 @@ class MachineGroup {
this.measurements.getVariants(type).forEach(variant => {
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();
if (downstreamVal != null) {
@@ -860,6 +938,9 @@ class MachineGroup {
if (upstreamVal != null) {
output[`upstream_${variant}_${type}`] = upstreamVal;
}
if (atEquipmentVal != null) {
output[`atEquipment_${variant}_${type}`] = atEquipmentVal;
}
if (downstreamVal != null && upstreamVal != null) {
const diffVal = this.measurements.type(type).variant(variant).difference().value;
output[`differential_${variant}_${type}`] = diffVal;
@@ -883,31 +964,31 @@ class MachineGroup {
}
module.exports = MachineGroup;
/*
const Machine = require('../../../rotatingMachine/dependencies/machine/machine');
const Measurement = require('../../../measurement/dependencies/measurement/measurement');
const specs = require('../../../generalFunctions/datasets/assetData/pumps/hydrostal/centrifugal pumps/models.json');
const power = require("../../../convert/dependencies/definitions/power");
const { machine } = require("os");
function createBaseMachineConfig(name,specs) {
const Machine = require('../../rotatingMachine/src/specificClass');
const Measurement = require('../../measurement/src/specificClass');
const specs = require('../../generalFunctions/datasets/assetData/curves/hidrostal-H05K-S03R.json');
const { max } = require("mathjs");
function createBaseMachineConfig(machineNum, name,specs) {
return {
general: {
logging: { enabled: true, logLevel: "warn" },
logging: { enabled: true, logLevel: "debug" },
name: name,
id: machineNum,
unit: "m3/h"
},
functionality: {
softwareType: "machine",
role: "RotationalDeviceController"
role: "rotationaldevicecontroller"
},
asset: {
type: "pump",
subType: "Centrifugal",
model: "TestModel",
supplier: "Hydrostal",
machineCurve: specs[0].machineCurve
category: "pump",
type: "centrifugal",
model: "hidrostal-h05k-s03r",
supplier: "hydrostal",
machineCurve: specs
},
mode: {
current: "auto",
@@ -931,6 +1012,23 @@ function createBaseMachineConfig(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) {
return {
general: {
@@ -938,8 +1036,8 @@ function createBaseMachineGroupConfig(name) {
name: name
},
functionality: {
softwareType: "machineGroup",
role: "GroupController"
softwareType: "machinegroup",
role: "groupcontroller"
},
scaling: {
current: "normalized"
@@ -950,22 +1048,30 @@ function createBaseMachineGroupConfig(name) {
};
}
const machineGroupConfig = createBaseMachineGroupConfig("TestMachineGroup");
const machineConfig = createBaseMachineConfig("TestMachine",specs);
const machineGroupConfig = createBaseMachineGroupConfig("testmachinegroup");
const stateConfigs = {};
const machineConfigs = {};
stateConfigs[1] = createStateConfig();
stateConfigs[2] = createStateConfig();
machineConfigs[1]= createBaseMachineConfig("asdfkj;asdf","testmachine",specs);
machineConfigs[2] = createBaseMachineConfig("asdfkj;asdf2","testmachine2",specs);
const ptConfig = {
general: {
logging: { enabled: true, logLevel: "debug" },
name: "TestPT",
name: "testpt",
id: "0",
unit: "mbar",
},
functionality: {
softwareType: "measurement",
role: "Sensor"
role: "sensor"
},
asset: {
type: "sensor",
subType: "pressure",
model: "TestModel",
category: "sensor",
type: "pressure",
model: "testmodel",
supplier: "vega"
},
scaling:{
@@ -978,15 +1084,17 @@ async function makeMachines(){
const mg = new MachineGroup(machineGroupConfig);
const pt1 = new Measurement(ptConfig);
const numofMachines = 2;
for(let i = 0; i < numofMachines; i++){
const machine = new Machine(machineConfig);
for(let i = 1; i <= numofMachines; i++){
const machine = new Machine(machineConfigs[i],stateConfigs[i]);
//mg.machines[i] = machine;
mg.childRegistrationUtils.registerChild(machine, "downstream");
}
mg.machines[1].childRegistrationUtils.registerChild(pt1, "downstream");
mg.machines[2].childRegistrationUtils.registerChild(pt1, "downstream");
mg.setMode("parent","prioritycontrol");
Object.keys(mg.machines).forEach(machineId => {
mg.machines[machineId].childRegistrationUtils.registerChild(pt1, "downstream");
});
mg.setMode("prioritycontrol");
mg.setScaling("normalized");
const absMax = mg.dynamicTotals.flow.max;
@@ -1002,7 +1110,6 @@ async function makeMachines(){
console.log("------------------------------------");
await mg.handleInput("parent",demand);
pt1.calculateInput(1400);
console.log("Waiting for 0.2 sec ");
//await new Promise(resolve => setTimeout(resolve, 200));
console.log("------------------------------------");
@@ -1015,22 +1122,24 @@ async function makeMachines(){
await mg.handleInput("parent",demand);
pt1.calculateInput(1400);
console.log("Waiting for 0.2 sec ");
//await new Promise(resolve => setTimeout(resolve, 200));
console.log("------------------------------------");
}
//*//*
*//*
for(let demand = 0 ; demand <= 100 ; demand += 1){
for(let demand = 0 ; demand <= 50 ; demand += 1){
//set pressure
console.log("------------------------------------");
console.log(`TESTING: processing demand of ${demand}`);
await mg.handleInput("parent",demand);
Object.keys(mg.machines).forEach(machineId => {
console.log(mg.machines[machineId].state.getCurrentState());
});
console.log(`updating pressure to 1400 mbar`);
pt1.calculateInput(1400);
console.log("Waiting for 0.2 sec ");
//await new Promise(resolve => setTimeout(resolve, 200));
console.log("------------------------------------");
}
@@ -1045,4 +1154,5 @@ async function makeMachines(){
makeMachines();
//*/