Expand reactor demo telemetry and stability handling
This commit is contained in:
@@ -1,4 +1,21 @@
|
|||||||
const { Reactor_CSTR, Reactor_PFR } = require('./specificClass.js');
|
const { Reactor_CSTR, Reactor_PFR } = require('./specificClass.js');
|
||||||
|
const { outputUtils } = require('generalFunctions');
|
||||||
|
|
||||||
|
const REACTOR_SPECIES = [
|
||||||
|
'S_O',
|
||||||
|
'S_I',
|
||||||
|
'S_S',
|
||||||
|
'S_NH',
|
||||||
|
'S_N2',
|
||||||
|
'S_NO',
|
||||||
|
'S_HCO',
|
||||||
|
'X_I',
|
||||||
|
'X_S',
|
||||||
|
'X_H',
|
||||||
|
'X_STO',
|
||||||
|
'X_A',
|
||||||
|
'X_TS'
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
class nodeClass {
|
class nodeClass {
|
||||||
@@ -18,6 +35,7 @@ class nodeClass {
|
|||||||
|
|
||||||
this._loadConfig(uiConfig)
|
this._loadConfig(uiConfig)
|
||||||
this._setupClass();
|
this._setupClass();
|
||||||
|
this._output = new outputUtils();
|
||||||
|
|
||||||
this._attachInputHandler();
|
this._attachInputHandler();
|
||||||
this._registerChild();
|
this._registerChild();
|
||||||
@@ -164,7 +182,29 @@ class nodeClass {
|
|||||||
if (gridProfile) {
|
if (gridProfile) {
|
||||||
this.node.send([{ topic: "GridProfile", payload: gridProfile }, null, null]);
|
this.node.send([{ topic: "GridProfile", payload: gridProfile }, null, null]);
|
||||||
}
|
}
|
||||||
this.node.send([this.source.getEffluent, null, null]);
|
this.node.send([this.source.getEffluent, this._buildTelemetryMessage(), null]);
|
||||||
|
}
|
||||||
|
|
||||||
|
_buildTelemetryMessage() {
|
||||||
|
const effluent = this.source?.getEffluent;
|
||||||
|
const concentrations = effluent?.payload?.C;
|
||||||
|
if (!Array.isArray(concentrations)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const telemetry = {
|
||||||
|
flow_total: Number(effluent.payload.F),
|
||||||
|
temperature: Number(this.source?.temperature),
|
||||||
|
};
|
||||||
|
|
||||||
|
for (let i = 0; i < Math.min(REACTOR_SPECIES.length, concentrations.length); i += 1) {
|
||||||
|
const value = Number(concentrations[i]);
|
||||||
|
if (Number.isFinite(value)) {
|
||||||
|
telemetry[REACTOR_SPECIES[i]] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._output.formatMsg(telemetry, this.config, 'influxdb');
|
||||||
}
|
}
|
||||||
|
|
||||||
_attachCloseHandler() {
|
_attachCloseHandler() {
|
||||||
|
|||||||
@@ -104,6 +104,29 @@ class Reactor {
|
|||||||
return this.kla * (S_O_sat - S_O);
|
return this.kla * (S_O_sat - S_O);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_calcOxygenSaturation(T = 20.0) {
|
||||||
|
return 14.652 - 4.1022e-1 * T + 7.9910e-3 * T*T + 7.7774e-5 * T*T*T;
|
||||||
|
}
|
||||||
|
|
||||||
|
_capDissolvedOxygen(state) {
|
||||||
|
const saturation = this._calcOxygenSaturation(this.temperature);
|
||||||
|
const capRow = (row) => {
|
||||||
|
if (!Array.isArray(row)) {
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
const next = row.slice();
|
||||||
|
if (Number.isFinite(next[S_O_INDEX])) {
|
||||||
|
next[S_O_INDEX] = Math.max(0, Math.min(next[S_O_INDEX], saturation));
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (Array.isArray(state) && Array.isArray(state[0])) {
|
||||||
|
return state.map(capRow);
|
||||||
|
}
|
||||||
|
return capRow(state);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clip values in an array to zero.
|
* Clip values in an array to zero.
|
||||||
* @param {Array} arr - Array of values to clip.
|
* @param {Array} arr - Array of values to clip.
|
||||||
@@ -241,7 +264,7 @@ class Reactor_CSTR extends Reactor {
|
|||||||
transfer[S_O_INDEX] = isNaN(this.kla) ? this.OTR : this._calcOTR(this.state[S_O_INDEX], this.temperature); // calculate OTR if kla is not NaN, otherwise use externaly calculated OTR
|
transfer[S_O_INDEX] = isNaN(this.kla) ? this.OTR : this._calcOTR(this.state[S_O_INDEX], this.temperature); // calculate OTR if kla is not NaN, otherwise use externaly calculated OTR
|
||||||
|
|
||||||
const dC_total = math.multiply(math.add(inflow, outflow, reaction, transfer), time_step)
|
const dC_total = math.multiply(math.add(inflow, outflow, reaction, transfer), time_step)
|
||||||
this.state = this._arrayClip2Zero(math.add(this.state, dC_total)); // clip value element-wise to avoid negative concentrations
|
this.state = this._capDissolvedOxygen(this._arrayClip2Zero(math.add(this.state, dC_total))); // clip concentrations and enforce physical DO saturation
|
||||||
if(DEBUG){
|
if(DEBUG){
|
||||||
assertNoNaN(dC_total, "change in state");
|
assertNoNaN(dC_total, "change in state");
|
||||||
assertNoNaN(this.state, "new state");
|
assertNoNaN(this.state, "new state");
|
||||||
@@ -348,7 +371,7 @@ class Reactor_PFR extends Reactor {
|
|||||||
assertNoNaN(stateNew, "new state post BC");
|
assertNoNaN(stateNew, "new state post BC");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.state = this._arrayClip2Zero(stateNew);
|
this.state = this._capDissolvedOxygen(this._arrayClip2Zero(stateNew));
|
||||||
return stateNew;
|
return stateNew;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,10 @@ test('CSTR uses kla-based oxygen transfer when kla is finite', () => {
|
|||||||
reactor.OTR = 1;
|
reactor.OTR = 1;
|
||||||
reactor.state = Array(NUM_SPECIES).fill(0);
|
reactor.state = Array(NUM_SPECIES).fill(0);
|
||||||
|
|
||||||
const expected = reactor._calcOTR(0, reactor.temperature);
|
const expected = Math.min(
|
||||||
|
reactor._calcOTR(0, reactor.temperature),
|
||||||
|
reactor._calcOxygenSaturation(reactor.temperature),
|
||||||
|
);
|
||||||
reactor.tick(1);
|
reactor.tick(1);
|
||||||
|
|
||||||
assert.ok(Math.abs(reactor.state[0] - expected) < 1e-9);
|
assert.ok(Math.abs(reactor.state[0] - expected) < 1e-9);
|
||||||
@@ -75,7 +78,10 @@ test('PFR uses kla-based transfer branch when kla is finite', () => {
|
|||||||
reactor.OTR = 0;
|
reactor.OTR = 0;
|
||||||
reactor.state = Array.from({ length: reactor.n_x }, () => Array(NUM_SPECIES).fill(0));
|
reactor.state = Array.from({ length: reactor.n_x }, () => Array(NUM_SPECIES).fill(0));
|
||||||
|
|
||||||
const expected = reactor._calcOTR(0, reactor.temperature) * (reactor.n_x / (reactor.n_x - 2));
|
const expected = Math.min(
|
||||||
|
reactor._calcOTR(0, reactor.temperature) * (reactor.n_x / (reactor.n_x - 2)),
|
||||||
|
reactor._calcOxygenSaturation(reactor.temperature),
|
||||||
|
);
|
||||||
reactor.tick(1);
|
reactor.tick(1);
|
||||||
|
|
||||||
assert.ok(Math.abs(reactor.state[1][0] - expected) < 1e-9);
|
assert.ok(Math.abs(reactor.state[1][0] - expected) < 1e-9);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ test('_tick emits source effluent on process output', () => {
|
|||||||
const node = makeNodeStub();
|
const node = makeNodeStub();
|
||||||
|
|
||||||
inst.node = node;
|
inst.node = node;
|
||||||
|
inst._output = { formatMsg() { return null; } };
|
||||||
inst.source = {
|
inst.source = {
|
||||||
get getEffluent() {
|
get getEffluent() {
|
||||||
return { topic: 'Fluent', payload: { inlet: 0, F: 1, C: [] }, timestamp: 1 };
|
return { topic: 'Fluent', payload: { inlet: 0, F: 1, C: [] }, timestamp: 1 };
|
||||||
@@ -23,6 +24,50 @@ test('_tick emits source effluent on process output', () => {
|
|||||||
assert.equal(node._sent[0][2], null);
|
assert.equal(node._sent[0][2], null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('_tick emits reactor telemetry on influx output', () => {
|
||||||
|
const inst = Object.create(NodeClass.prototype);
|
||||||
|
const node = makeNodeStub();
|
||||||
|
let captured = null;
|
||||||
|
|
||||||
|
inst.node = node;
|
||||||
|
inst.config = { functionality: { softwareType: 'reactor' }, general: { id: 'reactor-node-1' } };
|
||||||
|
inst._output = {
|
||||||
|
formatMsg(output, config, format) {
|
||||||
|
captured = { output, config, format };
|
||||||
|
return { topic: 'reactor_reactor-node-1', payload: { measurement: 'reactor_reactor-node-1', fields: output } };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
inst.source = {
|
||||||
|
temperature: 19.5,
|
||||||
|
get getGridProfile() {
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
get getEffluent() {
|
||||||
|
return {
|
||||||
|
topic: 'Fluent',
|
||||||
|
payload: {
|
||||||
|
inlet: 0,
|
||||||
|
F: 42,
|
||||||
|
C: [2.1, 30, 100, 16, 0, 1, 8, 25, 75, 1500, 0, 15, 2500]
|
||||||
|
},
|
||||||
|
timestamp: 1
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
inst._tick();
|
||||||
|
|
||||||
|
assert.equal(node._sent.length, 1);
|
||||||
|
assert.equal(node._sent[0][0].topic, 'Fluent');
|
||||||
|
assert.equal(node._sent[0][1].topic, 'reactor_reactor-node-1');
|
||||||
|
assert.equal(captured.format, 'influxdb');
|
||||||
|
assert.equal(captured.output.flow_total, 42);
|
||||||
|
assert.equal(captured.output.temperature, 19.5);
|
||||||
|
assert.equal(captured.output.S_O, 2.1);
|
||||||
|
assert.equal(captured.output.S_NH, 16);
|
||||||
|
assert.equal(captured.output.X_TS, 2500);
|
||||||
|
});
|
||||||
|
|
||||||
test('_startTickLoop schedules periodic tick after startup delay', () => {
|
test('_startTickLoop schedules periodic tick after startup delay', () => {
|
||||||
const inst = Object.create(NodeClass.prototype);
|
const inst = Object.create(NodeClass.prototype);
|
||||||
const delays = [];
|
const delays = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user