Compare commits

...

11 Commits

6 changed files with 315 additions and 32 deletions

View File

@@ -28,6 +28,8 @@
X_A_init: { value: 0.001, required: true },
X_TS_init: { value: 125.0009, required: true },
timeStep: { value: 1, required: true },
enableLog: { value: false },
logLevel: { value: "error" },
@@ -99,6 +101,10 @@
type:"num",
types:["num"]
})
$("#node-input-timeStep").typedInput({
type:"num",
types:["num"]
})
// Set initial visibility on dialog open
const initialType = $("#node-input-reactor_type").typedInput("value");
if (initialType === "CSTR") {
@@ -222,6 +228,11 @@
<label for="node-input-X_TS_init"><i class="fa fa-tag"></i> Initial total suspended solids [g TSS m-3]</label>
<input type="text" id="node-input-X_TS_init" class="concentrations">
</div>
<h2> Simulation parameters </h2>
<div class="form-row">
<label for="node-input-timeStep"><i class="fa fa-tag"></i> Time step [s]</label>
<input type="text" id="node-input-timeStep" placeholder="s">
</div>
<!-- Logger fields injected here -->
<div id="logger-fields-placeholder"></div>

24
package-lock.json generated
View File

@@ -9,14 +9,14 @@
"version": "0.0.1",
"license": "SEE LICENSE",
"dependencies": {
"generalFunctions": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git",
"generalFunctions": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git#fix-missing-references",
"mathjs": "^14.5.2"
}
},
"node_modules/@babel/runtime": {
"version": "7.28.2",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.28.2.tgz",
"integrity": "sha512-KHp2IflsnGywDjBWDkR9iEqiWSpc8GIi0lgTT3mOElT0PP1tG26P4tmFI2YvAdzgq9RGyoHZQEIEdZy6Ec5xCA==",
"version": "7.28.3",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.28.3.tgz",
"integrity": "sha512-9uIQ10o0WGdpP6GDhXcdOJPJuDgFtIDtN/9+ArJQ2NAfAmiuhTQdzkaTGR33v43GYS2UrSA0eX2pPPHoFVvpxA==",
"license": "MIT",
"engines": {
"node": ">=6.9.0"
@@ -48,12 +48,12 @@
"license": "MIT"
},
"node_modules/fraction.js": {
"version": "5.2.2",
"resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-5.2.2.tgz",
"integrity": "sha512-uXBDv5knpYmv/2gLzWQ5mBHGBRk9wcKTeWu6GLTUEQfjCxO09uM/mHDrojlL+Q1mVGIIFo149Gba7od1XPgSzQ==",
"version": "5.3.4",
"resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-5.3.4.tgz",
"integrity": "sha512-1X1NTtiJphryn/uLQz3whtY6jK3fTqoE3ohKs0tT+Ujr1W59oopxmoEh7Lu5p6vBaPbgoM0bzveAW4Qi5RyWDQ==",
"license": "MIT",
"engines": {
"node": ">= 12"
"node": "*"
},
"funding": {
"type": "github",
@@ -62,7 +62,7 @@
},
"node_modules/generalFunctions": {
"version": "1.0.0",
"resolved": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git#f13ee68938ea9d4b3a17ad90618c72634769c777",
"resolved": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git#302e12238745766a679ef11ca6ed5f4ea1548f87",
"license": "SEE LICENSE"
},
"node_modules/javascript-natural-sort": {
@@ -72,9 +72,9 @@
"license": "MIT"
},
"node_modules/mathjs": {
"version": "14.6.0",
"resolved": "https://registry.npmjs.org/mathjs/-/mathjs-14.6.0.tgz",
"integrity": "sha512-5vI2BLB5GKQmiSK9BH6hVkZ+GgqpdnOgEfmHl7mqVmdQObLynr63KueyYYLCQMzj66q69mV2XZZGQqqxeftQbA==",
"version": "14.7.0",
"resolved": "https://registry.npmjs.org/mathjs/-/mathjs-14.7.0.tgz",
"integrity": "sha512-RaMhb+9MSESjDZNox/FzzuFpIUI+oxGLyOy1t3BMoW53pGWnTzZtlucJ5cvbit0dIMYlCq00gNbW1giZX4/1Rg==",
"license": "Apache-2.0",
"dependencies": {
"@babel/runtime": "^7.26.10",

View File

@@ -27,7 +27,7 @@
}
},
"dependencies": {
"generalFunctions": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git",
"generalFunctions": "git+https://gitea.centraal.wbd-rd.nl/p.vanderwilt/generalFunctions.git#fix-missing-references",
"mathjs": "^14.5.2"
}
}

View File

@@ -49,7 +49,7 @@ class nodeClass {
this.source.setDispersion = msg;
break;
case 'registerChild':
// Register this node as a child of the parent node
// Register this node as a parent of the child node
const childId = msg.payload;
const childObj = this.RED.nodes.getNode(childId);
this.source.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent);
@@ -104,7 +104,8 @@ class nodeClass {
parseFloat(uiConfig.X_STO_init),
parseFloat(uiConfig.X_A_init),
parseFloat(uiConfig.X_TS_init)
]
],
timeStep: parseFloat(uiConfig.timeStep)
}
}

View File

@@ -0,0 +1,211 @@
const math = require('mathjs')
/**
* ASM3 class for the Activated Sludge Model No. 3 (ASM3). Using Koch et al. 2000 parameters.
*/
class ASM3 {
constructor() {
/**
* Kinetic parameters for ASM3 at 20 C. Using Koch et al. 2000 parameters.
* @property {Object} kin_params - Kinetic parameters
*/
this.kin_params = {
// Hydrolysis
k_H: 9., // hydrolysis rate constant [g X_S g-1 X_H d-1]
K_X: 1., // hydrolysis saturation constant [g X_S g-1 X_H]
// Heterotrophs
k_STO: 12., // storage rate constant [g S_S g-1 X_H d-1]
nu_NO: 0.5, // anoxic reduction factor [-]
K_O: 0.2, // saturation constant S_0 [g O2 m-3]
K_NO: 0.5, // saturation constant S_NO [g NO3-N m-3]
K_S: 10., // saturation constant S_s [g COD m-3]
K_STO: 0.1, // saturation constant X_STO [g X_STO g-1 X_H]
mu_H_max: 3., // maximum specific growth rate [d-1]
K_NH: 0.01, // saturation constant S_NH3 [g NH3-N m-3]
K_HCO: 0.1, // saturation constant S_HCO [mole HCO3 m-3]
b_H_O: 0.3, // aerobic respiration rate [d-1]
b_H_NO: 0.15, // anoxic respiration rate [d-1]
b_STO_O: 0.3, // aerobic respitation rate X_STO [d-1]
b_STO_NO: 0.15, // anoxic respitation rate X_STO [d-1]
// Autotrophs
mu_A_max: 1.3, // maximum specific growth rate [d-1]
K_A_NH: 1.4, // saturation constant S_NH3 [g NH3-N m-3]
K_A_O: 0.5, // saturation constant S_0 [g O2 m-3]
K_A_HCO: 0.5, // saturation constant S_HCO [mole HCO3 m-3]
b_A_O: 0.20, // aerobic respiration rate [d-1]
b_A_NO: 0.10 // anoxic respiration rate [d-1]
};
/**
* Stoichiometric and composition parameters for ASM3. Using Koch et al. 2000 parameters.
* @property {Object} stoi_params - Stoichiometric parameters
*/
this.stoi_params = {
// Fractions
f_SI: 0., // fraction S_I from hydrolysis [g S_I g-1 X_S]
f_XI: 0.2, // fraction X_I from decomp X_H [g X_I g-1 X_H]
// Yields
Y_STO_O: 0.80, // aerobic yield X_STO per S_S [g X_STO g-1 S_S]
Y_STO_NO: 0.70, // anoxic yield X_STO per S_S [g X_STO g-1 S_S]
Y_H_O: 0.80, // aerobic yield X_H per X_STO [g X_H g-1 X_STO]
Y_H_NO: 0.65, // anoxic yield X_H per X_STO [g X_H g-1 X_STO]
Y_A: 0.24, // anoxic yield X_A per S_NO [g X_A g-1 NO3-N]
// Composition (COD via DoR)
i_CODN: -1.71, // COD content (DoR) [g COD g-1 N2-N]
i_CODNO: -4.57, // COD content (DoR) [g COD g-1 NO3-N]
// Composition (nitrogen)
i_NSI: 0.01, // nitrogen content S_I [g N g-1 S_I]
i_NSS: 0.03, // nitrogen content S_S [g N g-1 S_S]
i_NXI: 0.04, // nitrogen content X_I [g N g-1 X_I]
i_NXS: 0.03, // nitrogen content X_S [g N g-1 X_S]
i_NBM: 0.07, // nitrogen content X_H / X_A [g N g-1 X_H / X_A]
// Composition (TSS)
i_TSXI: 0.75, // TSS content X_I [g TS g-1 X_I]
i_TSXS: 0.75, // TSS content X_S [g TS g-1 X_S]
i_TSBM: 0.90, // TSS content X_H / X_A [g TS g-1 X_H / X_A]
i_TSSTO: 0.60, // TSS content X_STO (PHB based) [g TS g-1 X_STO]
// Composition (charge)
i_cNH: 1/14, // charge per S_NH [mole H+ g-1 NH3-N]
i_cNO: -1/14 // charge per S_NO [mole H+ g-1 NO3-N]
};
/**
* Temperature theta parameters for ASM3. Using Koch et al. 2000 parameters.
* These parameters are used to adjust reaction rates based on temperature.
* @property {Object} temp_params - Temperature theta parameters
*/
this.temp_params = {
// Hydrolysis
theta_H: 0.04,
// Heterotrophs
theta_STO: 0.07,
theta_mu_H: 0.07,
theta_b_H_O: 0.07,
theta_b_H_NO: 0.07,
theta_b_STO_O: this._compute_theta(0.1, 0.3, 10, 20),
theta_b_STO_NO: this._compute_theta(0.05, 0.15, 10, 20),
// Autotrophs
theta_mu_A: 0.105,
theta_b_A_O: 0.105,
theta_b_A_NO: 0.105
};
this.stoi_matrix = this._initialise_stoi_matrix();
}
/**
* Initialises the stoichiometric matrix for ASM3.
* @returns {Array} - The stoichiometric matrix for ASM3. (2D array)
*/
_initialise_stoi_matrix() { // initialise stoichiometric matrix
const { f_SI, f_XI, Y_STO_O, Y_STO_NO, Y_H_O, Y_H_NO, Y_A, i_CODN, i_CODNO, i_NSI, i_NSS, i_NXI, i_NXS, i_NBM, i_TSXI, i_TSXS, i_TSBM, i_TSSTO, i_cNH, i_cNO } = this.stoi_params;
const stoi_matrix = Array(12);
// 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
stoi_matrix[0] = [0., f_SI, 1.-f_SI, i_NXS-(1.-f_SI)*i_NSS-f_SI*i_NSI, 0., 0., (i_NXS-(1.-f_SI)*i_NSS-f_SI*i_NSI)*i_cNH, 0., -1., 0., 0., 0., -i_TSXS];
stoi_matrix[1] = [-(1.-Y_STO_O), 0, -1., i_NSS, 0., 0., i_NSS*i_cNH, 0., 0., 0., Y_STO_O, 0., Y_STO_O*i_TSSTO];
stoi_matrix[2] = [0., 0., -1., i_NSS, -(1.-Y_STO_NO)/(i_CODNO-i_CODN), (1.-Y_STO_NO)/(i_CODNO-i_CODN), i_NSS*i_cNH + (1.-Y_STO_NO)/(i_CODNO-i_CODN)*i_cNO, 0., 0., 0., Y_STO_NO, 0., Y_STO_NO*i_TSSTO];
stoi_matrix[3] = [-(1.-Y_H_O)/Y_H_O, 0., 0., -i_NBM, 0., 0., -i_NBM*i_cNH, 0., 0., 1., -1./Y_H_O, 0., i_TSBM-i_TSSTO/Y_H_O];
stoi_matrix[4] = [0., 0., 0., -i_NBM, -(1.-Y_H_NO)/(Y_H_NO*(i_CODNO-i_CODN)), (1.-Y_H_NO)/(Y_H_NO*(i_CODNO-i_CODN)), -i_NBM*i_cNH+(1.-Y_H_NO)/(Y_H_NO*(i_CODNO-i_CODN))*i_cNO, 0., 0., 1., -1./Y_H_NO, 0., i_TSBM-i_TSSTO/Y_H_NO];
stoi_matrix[5] = [f_XI-1., 0., 0., i_NBM-f_XI*i_NXI, 0., 0., (i_NBM-f_XI*i_NXI)*i_cNH, f_XI, 0., -1., 0., 0., f_XI*i_TSXI-i_TSBM];
stoi_matrix[6] = [0., 0., 0., i_NBM-f_XI*i_NXI, -(1.-f_XI)/(i_CODNO-i_CODN), (1.-f_XI)/(i_CODNO-i_CODN), (i_NBM-f_XI*i_NXI)*i_cNH+(1-f_XI)/(i_CODNO-i_CODN)*i_cNO, f_XI, 0., -1., 0., 0., f_XI*i_TSXI-i_TSBM];
stoi_matrix[7] = [-1., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., -i_TSSTO];
stoi_matrix[8] = [0., 0., 0., 0., -1./(i_CODNO-i_CODN), 1./(i_CODNO-i_CODN), i_cNO/(i_CODNO-i_CODN), 0., 0., 0., -1., 0., -i_TSSTO];
stoi_matrix[9] = [1.+i_CODNO/Y_A, 0., 0., -1./Y_A-i_NBM, 0., 1./Y_A, (-1./Y_A-i_NBM)*i_cNH+i_cNO/Y_A, 0., 0., 0., 0., 1., i_TSBM];
stoi_matrix[10] = [f_XI-1., 0., 0., i_NBM-f_XI*i_NXI, 0., 0., (i_NBM-f_XI*i_NXI)*i_cNH, f_XI, 0., 0., 0., -1., f_XI*i_TSXI-i_TSBM];
stoi_matrix[11] = [0., 0., 0., i_NBM-f_XI*i_NXI, -(1.-f_XI)/(i_CODNO-i_CODN), (1.-f_XI)/(i_CODNO-i_CODN), (i_NBM-f_XI*i_NXI)*i_cNH+(1-f_XI)/(i_CODNO-i_CODN)*i_cNO, 0., 0., 0., 0., -1., f_XI*i_TSXI-i_TSBM];
return stoi_matrix[0].map((col, i) => stoi_matrix.map(row => row[i])); // transpose matrix
}
/**
* Computes the Monod equation rate value for a given concentration and half-saturation constant.
* @param {number} c - Concentration of reaction species.
* @param {number} K - Half-saturation constant for the reaction species.
* @returns {number} - Monod equation rate value for the given concentration and half-saturation constant.
*/
_monod(c, K) {
return c / (K + c);
}
/**
* Computes the inverse Monod equation rate value for a given concentration and half-saturation constant. Used for inhibition.
* @param {number} c - Concentration of reaction species.
* @param {number} K - Half-saturation constant for the reaction species.
* @returns {number} - Inverse Monod equation rate value for the given concentration and half-saturation constant.
*/
_inv_monod(c, K) {
return K / (K + c);
}
/**
* Adjust the rate parameter for temperature T using simplied Arrhenius equation based on rate constant at 20 degrees Celsius and theta parameter.
* @param {number} k - Rate constant at 20 degrees Celcius.
* @param {number} theta - Theta parameter.
* @param {number} T - Temperature in Celcius.
* @returns {number} - Adjusted rate parameter at temperature T based on the Arrhenius equation.
*/
_arrhenius(k, theta, T) {
return k * Math.exp(theta*(T-20));
}
/**
* Computes the temperature theta parameter based on two rate constants and their corresponding temperatures.
* @param {number} k1 - Rate constant at temperature T1.
* @param {number} k2 - Rate constant at temperature T2.
* @param {number} T1 - Temperature T1 in Celcius.
* @param {number} T2 - Temperature T2 in Celcius.
* @returns {number} - Theta parameter.
*/
_compute_theta(k1, k2, T1, T2) {
return Math.log(k1/k2)/(T1-T2);
}
/**
* Computes the reaction rates for each process reaction based on the current state and temperature.
* @param {Array} state - State vector containing concentrations of reaction species.
* @param {number} [T=20] - Temperature in degrees Celsius (default is 20).
* @returns {Array} - Reaction rates for each process reaction.
*/
compute_rates(state, T = 20) {
// 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
const rates = Array(12);
const [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] = state;
const { k_H, K_X, k_STO, nu_NO, K_O, K_NO, K_S, K_STO, mu_H_max, K_NH, K_HCO, b_H_O, b_H_NO, b_STO_O, b_STO_NO, mu_A_max, K_A_NH, K_A_O, K_A_HCO, b_A_O, b_A_NO } = this.kin_params;
const { theta_H, theta_STO, theta_mu_H, theta_b_H_O, theta_b_H_NO, theta_b_STO_O, theta_b_STO_NO, theta_mu_A, theta_b_A_O, theta_b_A_NO } = this.temp_params;
// Hydrolysis
rates[0] = X_H == 0 ? 0 : this._arrhenius(k_H, theta_H, T) * this._monod(X_S / X_H, K_X) * X_H;
// Heterotrophs
rates[1] = this._arrhenius(k_STO, theta_STO, T) * this._monod(S_O, K_O) * this._monod(S_S, K_S) * X_H;
rates[2] = this._arrhenius(k_STO, theta_STO, T) * nu_NO * this._inv_monod(S_O, K_O) * this._monod(S_NO, K_NO) * this._monod(S_S, K_S) * X_H;
rates[3] = X_H == 0 ? 0 : this._arrhenius(mu_H_max, theta_mu_H, T) * this._monod(S_O, K_O) * this._monod(S_NH, K_NH) * this._monod(S_HCO, K_HCO) * this._monod(X_STO/X_H, K_STO) * X_H;
rates[4] = X_H == 0 ? 0 : this._arrhenius(mu_H_max, theta_mu_H, T) * nu_NO * this._inv_monod(S_O, K_O) * this._monod(S_NO, K_NO) * this._monod(S_NH, K_NH) * this._monod(S_HCO, K_HCO) * this._monod(X_STO/X_H, K_STO) * X_H;
rates[5] = this._arrhenius(b_H_O, theta_b_H_O, T) * this._monod(S_O, K_O) * X_H;
rates[6] = this._arrhenius(b_H_NO, theta_b_H_NO, T) * this._inv_monod(S_O, K_O) * this._monod(S_NO, K_NO) * X_H;
rates[7] = this._arrhenius(b_STO_O, theta_b_STO_O, T) * this._monod(S_O, K_O) * X_H;
rates[8] = this._arrhenius(b_STO_NO, theta_b_STO_NO, T) * this._inv_monod(S_O, K_O) * this._monod(S_NO, K_NO) * X_STO;
// Autotrophs
rates[9] = this._arrhenius(mu_A_max, theta_mu_A, T) * this._monod(S_O, K_A_O) * this._monod(S_NH, K_A_NH) * this._monod(S_HCO, K_A_HCO) * X_A;
rates[10] = this._arrhenius(b_A_O, theta_b_A_O, T) * this._monod(S_O, K_O) * X_A;
rates[11] = this._arrhenius(b_A_NO, theta_b_A_NO, T) * this._inv_monod(S_O, K_A_O) * this._monod(S_NO, K_NO) * X_A;
return rates;
}
/**
* Computes the change in concentrations of reaction species based on the current state and temperature.
* @param {Array} state - State vector containing concentrations of reaction species.
* @param {number} [T=20] - Temperature in degrees Celsius (default is 20).
* @returns {Array} - Change in reaction species concentrations.
*/
compute_dC(state, T = 20) { // compute changes in concentrations
// 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
return math.multiply(this.stoi_matrix, this.compute_rates(state, T));
}
}
module.exports = ASM3;

View File

@@ -40,22 +40,10 @@ class Reactor {
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.timeStep = 1 / (24*60*60) * this.config.timeStep; // time step [d]
this.speedUpFactor = 60; // speed up factor for simulation, 60 means 1 minute per simulated second
}
updateMeasurement(variant, subType, value, position) {
this.logger.debug(`---------------------- updating ${subType} ------------------ `);
switch (subType) {
case "temperature":
this.temperature = value;
break;
default:
this.logger.error(`Type '${subType}' not recognized for measured update.`);
return;
}
}
/**
* Setter for influent data.
* @param {object} input - Input object (msg) containing payload with inlet index, flow rate, and concentrations.
@@ -109,6 +97,78 @@ class Reactor {
}
}
registerChild(child, softwareType) {
switch (softwareType) {
case "measurement":
this.logger.debug(`Registering measurement child.`);
this._connectMeasurement(child);
break;
case "reactor":
this.logger.debug(`Registering reactor child.`);
this._connectReactor(child);
break;
default:
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
}
}
_connectMeasurement(measurement) {
if (!measurement) {
this.logger.warn("Invalid measurement provided.");
return;
}
const position = measurement.config.functionality.positionVsParent;
const measurementType = measurement.config.asset.type;
const key = `${measurementType}_${position}`;
const eventName = `${measurementType}.measured.${position}`;
// Register event listener for measurement updates
this.measurements.emitter.on(eventName, (eventData) => {
this.logger.debug(`${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
// Store directly in parent's measurement container
this.measurements
.type(measurementType)
.variant("measured")
.position(position)
.value(eventData.value, eventData.timestamp, eventData.unit);
this._updateMeasurement(measurementType, eventData.value, position, eventData);
});
}
_connectReactor(reactor) {
if (!reactor) {
this.logger.warn("Invalid reactor provided.");
return;
}
this.upstreamReactor = reactor;
reactor.emitter.on("stateChange", (data) => {
this.logger.debug(`State change of upstream reactor detected.`);
this.updateState(data);
});
}
_updateMeasurement(measurementType, value, position, context) {
this.logger.debug(`---------------------- updating ${measurementType} ------------------ `);
switch (measurementType) {
case "temperature":
if (position == "atEquipment") {
this.temperature = value;
}
break;
default:
this.logger.error(`Type '${measurementType}' not recognized for measured update.`);
return;
}
}
/**
* Update the reactor state based on the new time.
* @param {number} newTime - New time to update reactor state to, in milliseconds since epoch.
@@ -255,14 +315,14 @@ class Reactor_PFR extends Reactor {
return stateNew;
}
updateMeasurement(variant, subType, value, position) {
switch(subType) {
_updateMeasurement(measurementType, value, position, context) {
switch(measurementType) {
case "oxygen":
grid_pos = Math.round(position * this.n_x);
this.state[grid_pos][S_O_INDEX] = value; // naive approach for reconciling measurements and simulation
return;
break;
}
super.updateMeasurement(variant, subType, value, position);
super._updateMeasurement(measurementType, value, position, context);
}
/**