Refactor documentation in nodeClass and reactor_class for clarity and consistency

This commit is contained in:
2025-07-04 13:52:28 +02:00
parent c239b71ad8
commit 09e7072d16
2 changed files with 90 additions and 47 deletions

View File

@@ -3,9 +3,9 @@ const { Reactor_CSTR, Reactor_PFR } = require('./reactor_class.js');
class nodeClass {
/**
* Construct ReactorNode.
* Node-RED node class for advanced-reactor.
* @param {object} uiConfig - Node-RED node configuration
* @param {object} RED - Node-RED runtime API
* @param {object} RED - Node-RED runtime API
* @param {object} nodeInstance - Node-RED node instance
* @param {string} nameOfNode - Name of the node
*/

View File

@@ -8,7 +8,10 @@ const config = {
const math = create(all, config)
class Reactor {
/**
* Reactor base class.
* @param {object} config - Configuration object containing reactor parameters.
*/
constructor(config){
this.asm = new ASM3();
@@ -18,62 +21,91 @@ class Reactor {
this.Cs_in = Array.from(Array(config.n_inlets), () => new Array(13).fill(0.0)); // composition influents
this.OTR = 0.0; // oxygen transfer rate [g O2 d-1]
this.kla = config.kla; // if NaN, use external OTR [d-1]
this.kla = config.kla; // if NaN, use externaly provided OTR [d-1]
this.currentTime = Date.now(); // milliseconds since epoch [ms]
this.timeStep = 1/(24*60*15); // time step [d]
this.speedUpFactor = 60;
this.speedUpFactor = 60; // speed up factor for simulation, 60 means 1 minute per simulated second
}
set setInfluent(input) { // setter for C_in (WIP)
/**
* Setter for influent data.
* @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;
// DEBUG
// console.log("Pe total " + this.length*math.sum(this.Fs)/(this.D*this.A));
// console.log("Pe local " + this.d_x*math.sum(this.Fs)/(this.D*this.A));
// console.log("Co ad " + math.sum(this.Fs)*this.timeStep/(this.A*this.d_x));
// console.log("Co D " + this.D*this.timeStep/(this.d_x*this.d_x));
}
set setOTR(input) { // setter for OTR (WIP) [g O2 d-1]
/**
* Setter for OTR (Oxygen Transfer Rate).
* @param {object} input - Input object (msg) containing payload with OTR value [g O2 d-1].
*/
set setOTR(input) {
this.OTR = input.payload;
}
/**
*
* @param {number} S_O - Dissolved oxygen concentration [g O2 m-3].
* @param {number} T - Temperature in Celsius, default to 20 C.
* @returns
*/
calcOTR(S_O, T=20.0) { // caculate the OTR using basic correlation, default to temperature: 20 C
let S_O_sat = 14.652 - 4.1022e-1*T + 7.9910e-3*T*T + 7.7774e-5*T*T*T;
return this.kla * (S_O_sat - S_O);
}
}
class Reactor_CSTR extends Reactor {
constructor(config) {
super(config);
this.state = config.initialState;
}
get getEffluent() { // getter for Effluent, defaults to inlet 0
return {topic: "Fluent", payload: {inlet: 0, F: math.sum(this.Fs), C:this.state}, timestamp: this.currentTime};
}
// expect update with timestamp
updateState(newTime) {
/**
* Update the reactor state based on the new time.
* @param {number} newTime - New time to update reactor state to, in milliseconds since epoch.
*/
updateState(newTime) { // expect update with timestamp
const day2ms = 1000 * 60 * 60 * 24;
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_fe(this.timeStep);
this.tick(this.timeStep);
n += 1;
}
this.currentTime += n_iter * this.timeStep * day2ms / this.speedUpFactor;
}
}
tick_fe(time_step) { // tick reactor state using forward Euler method
}
class Reactor_CSTR extends Reactor {
/**
* Reactor_CSTR class for Continuous Stirred Tank Reactor.
* @param {object} config - Configuration object containing reactor parameters.
*/
constructor(config) {
super(config);
this.state = config.initialState;
}
/**
* Getter for effluent data.
* @returns {object} Effluent data object (msg), defaults to inlet 0.
*/
get getEffluent() { // getter for Effluent, defaults to inlet 0
return {topic: "Fluent", payload: {inlet: 0, F: math.sum(this.Fs), C:this.state}, timestamp: this.currentTime};
}
/**
* Tick the reactor state using the forward Euler method.
* @param {number} time_step - Time step for the simulation [d].
* @returns {Array} - New reactor state.
*/
tick(time_step) { // tick reactor state using forward Euler method
const r = this.asm.compute_dC(this.state);
const dC_in = math.multiply(math.divide([this.Fs], this.Vl), this.Cs_in)[0];
const dC_out = math.multiply(-1*math.sum(this.Fs)/this.Vl, this.state);
@@ -89,7 +121,10 @@ class Reactor_CSTR extends Reactor {
}
class Reactor_PFR extends Reactor {
/**
* Reactor_PFR class for Plug Flow Reactor.
* @param {object} config - Configuration object containing reactor parameters.
*/
constructor(config) {
super(config);
@@ -109,31 +144,29 @@ class Reactor_PFR extends Reactor {
this.D_op = this.makeDoperator(true, true);
this.D2_op = this.makeD2operator();
}
set setDispersion(input) { // setter for Axial dispersion [m2 d-1]
/**
* Setter for axial dispersion.
* @param {object} input - Input object (msg) containing payload with dispersion value [m2 d-1].
*/
set setDispersion(input) {
this.D = input.payload;
}
get getEffluent() { // getter for Effluent, defaults to inlet 0
/**
* Getter for effluent data.
* @returns {object} Effluent data object (msg), defaults to inlet 0.
*/
get getEffluent() {
return {topic: "Fluent", payload: {inlet: 0, F: math.sum(this.Fs), C:this.state.at(-1)}, timestamp: this.currentTime};
}
// expect update with timestamp
updateState(newTime) {
const day2ms = 1000 * 60 * 60 * 24;
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_fe(this.timeStep);
n += 1;
}
this.currentTime += n_iter * this.timeStep * day2ms / this.speedUpFactor;
}
}
tick_fe(time_step) { // tick reactor state using forward Euler method
/**
* Tick the reactor state using explicit finite difference method.
* @param {number} time_step - Time step for the simulation [d].
* @returns {Array} - New reactor state.
*/
tick(time_step) {
const dispersion = math.multiply(this.D / (this.d_x*this.d_x), this.D2_op, this.state);
const advection = math.multiply(-1*math.sum(this.Fs)/(this.A*this.d_x), this.D_op, this.state);
const reaction = this.state.map((state_slice) => this.asm.compute_dC(state_slice));
@@ -185,6 +218,12 @@ class Reactor_PFR extends Reactor {
return new_state;
}
/**
* Create finite difference first derivative operator.
* @param {boolean} central - Use central difference scheme if true, otherwise use upwind scheme.
* @param {boolean} higher_order - Use higher order scheme if true, otherwise use first order scheme.
* @returns {Array} - First derivative operator matrix.
*/
makeDoperator(central=false, higher_order=false) { // create gradient operator
if (higher_order) {
if (central) {
@@ -218,6 +257,10 @@ class Reactor_PFR extends Reactor {
}
}
/**
* Create central finite difference second derivative operator.
* @returns {Array} - Second derivative operator matrix.
*/
makeD2operator() { // create the central second derivative operator
const I = math.diag(Array(this.n_x).fill(-2), 0);
const A = math.resize(math.diag(Array(this.n_x).fill(1), 1), [this.n_x, this.n_x]);
@@ -230,7 +273,7 @@ class Reactor_PFR extends Reactor {
}
// testing stuff
// DEBUG
// state: 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
// let initial_state = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1];
// const Reactor = new Reactor_PFR(200, 10, 10, 1, 100, initial_state);
@@ -239,7 +282,7 @@ class Reactor_PFR extends Reactor {
// Reactor.D = 0.01;
// let N = 0;
// while (N < 5000) {
// console.log(Reactor.tick_fe(0.001));
// console.log(Reactor.tick(0.001));
// N += 1;
// }