Merge pull request 'Recirculation Integration' (#4) from recirculation-integration into main

Reviewed-on: #4
This commit is contained in:
2025-11-06 13:55:42 +00:00
3 changed files with 48 additions and 44 deletions

View File

@@ -11,7 +11,6 @@
length: { value: 0.},
resolution_L: { value: 0.},
alpha: {value: 0},
n_inlets: { value: 1, required: true},
kla: { value: null },
S_O_init: { value: 0., required: true },
@@ -58,10 +57,6 @@
type:"num",
types:["num"]
});
$("#node-input-n_inlets").typedInput({
type:"num",
types:["num"]
});
$("#node-input-length").typedInput({
type:"num",
types:["num"]
@@ -128,10 +123,6 @@
if (isNaN(volume) || volume <= 0) {
RED.notify("Fluid volume not set correctly", {type: "error"});
}
let n_inlets = parseInt($("#node-input-n_inlets").typedInput("value"));
if (isNaN(n_inlets) || n_inlets < 1) {
RED.notify("Number of inlets not set correctly", {type: "error"});
}
}
});
</script>
@@ -165,10 +156,6 @@
<input type="text" id="node-input-alpha">
</div>
</div>
<div class="form-row">
<label for="node-input-n_inlets"><i class="fa fa-tag"></i> Number of inlets</label>
<input type="text" id="node-input-n_inlets" placeholder="#">
</div>
<h3> Internal mass transfer calculation (optional) </h3>
<div class="form-row">
<label for="node-input-kla"><i class="fa fa-tag"></i> kLa [d-1]</label>

View File

@@ -88,7 +88,6 @@ class nodeClass {
length: parseFloat(uiConfig.length),
resolution_L: parseInt(uiConfig.resolution_L),
alpha: parseFloat(uiConfig.alpha),
n_inlets: parseInt(uiConfig.n_inlets),
kla: parseFloat(uiConfig.kla),
initialState: [
parseFloat(uiConfig.S_O_init),

View File

@@ -1,5 +1,5 @@
const ASM3 = require('./reaction_modules/asm3_class.js');
const { create, all, isArray } = require('mathjs');
const { create, all, isArray, i } = require('mathjs');
const { assertNoNaN } = require('./utils.js');
const { childRegistrationUtils, logger, MeasurementContainer } = require('generalFunctions');
const EventEmitter = require('events');
@@ -14,6 +14,7 @@ const S_O_INDEX = 0;
const NUM_SPECIES = 13;
const BC_PADDING = 2;
const DEBUG = false;
const DAY2MS = 1000 * 60 * 60 * 24;
class Reactor {
/**
@@ -28,17 +29,17 @@ class Reactor {
this.measurements = new MeasurementContainer();
this.upstreamReactor = null;
this.childRegistrationUtils = new childRegistrationUtils(this); // Child registration utility
this.parent = []; // Gets assigned via child registration
this.upstreamReactor = null;
this.downstreamReactor = null;
this.returnPump = null;
this.asm = new ASM3();
this.volume = config.volume; // fluid volume reactor [m3]
this.Fs = Array(config.n_inlets).fill(0); // fluid debits per inlet [m3 d-1]
this.Cs_in = Array.from(Array(config.n_inlets), () => new Array(NUM_SPECIES).fill(0)); // composition influents
this.Fs = [0]; // fluid debits per inlet [m3 d-1]
this.Cs_in = [Array(NUM_SPECIES).fill(0)]; // composition influents
this.OTR = 0.0; // oxygen transfer rate [g O2 d-1 m-3]
this.temperature = 20; // temperature [C]
@@ -54,9 +55,15 @@ class Reactor {
* @param {object} input - Input object (msg) containing payload with inlet index, flow rate, and concentrations.
*/
set setInfluent(input) {
let index_in = input.payload.inlet;
this.Fs[index_in] = input.payload.F;
this.Cs_in[index_in] = input.payload.C;
const i_in = input.payload.inlet;
if (this.Fs.length <= i_in) {
this.logger.debug(`Adding new inlet index ${i_in}.`);
this.Fs.push(0);
this.Cs_in.push(Array(NUM_SPECIES).fill(0));
this.setInfluent = input;
}
this.Fs[i_in] = input.payload.F;
this.Cs_in[i_in] = input.payload.C;
}
/**
@@ -72,10 +79,17 @@ class Reactor {
* @returns {object} Effluent data object (msg), defaults to inlet 0.
*/
get getEffluent() { // getter for Effluent, defaults to inlet 0
if (isArray(this.state.at(-1))) {
return { topic: "Fluent", payload: { inlet: 0, F: math.sum(this.Fs), C: this.state.at(-1) }, timestamp: this.currentTime };
const Cs = isArray(this.state.at(-1)) ? this.state.at(-1) : this.state;
const effluent = [{ topic: "Fluent", payload: { inlet: 0, F: math.sum(this.Fs), C: Cs }, timestamp: this.currentTime }];
if (this.returnPump) {
const recirculationFlow = this.returnPump.measurements.type("flow").variant("measured").position("atEquipment").getCurrentValue();
// constrain flow to prevent negatives
const F_main = Math.max(effluent[0].payload.F - recirculationFlow, 0);
const F_sidestream = effluent[0].payload.F < recirculationFlow ? effluent[0].payload.F : recirculationFlow;
effluent[0].payload.F = F_main;
effluent.push({ topic: "Fluent", payload: { inlet: 1, F: F_sidestream, C: Cs }, timestamp: this.currentTime });
}
return { topic: "Fluent", payload: { inlet: 0, F: math.sum(this.Fs), C: this.state }, timestamp: this.currentTime };
return effluent;
}
/**
@@ -103,15 +117,24 @@ class Reactor {
}
registerChild(child, softwareType) {
if(!child) {
this.logger.error(`Invalid ${softwareType} child provided.`);
return;
}
switch (softwareType) {
case "measurement":
this.logger.debug(`Registering measurement child.`);
this.logger.debug(`Registering measurement child...`);
this._connectMeasurement(child);
break;
case "reactor":
this.logger.debug(`Registering reactor child.`);
this.logger.debug(`Registering reactor child...`);
this._connectReactor(child);
break;
case "machine":
this.logger.debug(`Registering machine child...`);
this._connectMachine(child);
break;
default:
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
@@ -119,11 +142,6 @@ class Reactor {
}
_connectMeasurement(measurementChild) {
if (!measurementChild) {
this.logger.warn("Invalid measurement provided.");
return;
}
const position = measurementChild.config.functionality.positionVsParent;
const measurementType = measurementChild.config.asset.type;
const eventName = `${measurementType}.measured.${position}`;
@@ -145,12 +163,7 @@ class Reactor {
_connectReactor(reactorChild) {
if (!reactorChild) {
this.logger.warn("Invalid reactor provided.");
return;
}
if (reactorChild.functionality.positionVsParent != "upstream") {
if (reactorChild.config.functionality.positionVsParent != "upstream") {
this.logger.warn("Reactor children of reactors should always be upstream.");
}
@@ -158,15 +171,22 @@ class Reactor {
this.logger.warn("Significant grid sizing discrepancies between adjacent reactors! Change resolutions to match reactors grid step, or implement boundary value interpolation.");
}
// set upstream and downstream reactor variable in current and child nodes respectively for easy access
this.upstreamReactor = reactorChild;
reactorChild.downstreamReactor = this;
reactorChild.emitter.on("stateChange", (data) => {
reactorChild.emitter.on("stateChange", (eventData) => {
this.logger.debug(`State change of upstream reactor detected.`);
this.updateState(data);
this.updateState(eventData);
});
}
_connectMachine(machineChild) {
if (machineChild.config.functionality.positionVsParent == "downstream") {
machineChild.upstreamSource = this;
this.returnPump = machineChild;
}
}
_updateMeasurement(measurementType, value, position, context) {
this.logger.debug(`---------------------- updating ${measurementType} ------------------ `);
@@ -187,20 +207,18 @@ class Reactor {
* @param {number} newTime - New time to update reactor state to, in milliseconds since epoch.
*/
updateState(newTime = Date.now()) { // expect update with timestamp
const day2ms = 1000 * 60 * 60 * 24;
if (this.upstreamReactor) {
this.setInfluent = this.upstreamReactor.getEffluent;
this.setInfluent = this.upstreamReactor.getEffluent[0]; // grab main effluent upstream reactor
}
let n_iter = Math.floor(this.speedUpFactor * (newTime-this.currentTime) / (this.timeStep*day2ms));
let n_iter = Math.floor(this.speedUpFactor * (newTime-this.currentTime) / (this.timeStep*DAY2MS));
if (n_iter) {
let n = 0;
while (n < n_iter) {
this.tick(this.timeStep);
n += 1;
}
this.currentTime += n_iter * this.timeStep * day2ms / this.speedUpFactor;
this.currentTime += n_iter * this.timeStep * DAY2MS / this.speedUpFactor;
this.emitter.emit("stateChange", this.currentTime);
}
}