Refactor reactor class to improve NaN handling and add utility function for NaN assertions

This commit is contained in:
2025-07-04 16:28:35 +02:00
parent 6755f2bd28
commit a2cfb20e2c
2 changed files with 48 additions and 41 deletions

View File

@@ -1,5 +1,6 @@
const ASM3 = require('./asm3_class.js');
const { create, all } = require('mathjs');
const { assertNoNaN } = require('./utils.js');
const config = {
matrix: 'Array' // use Array as the matrix type
@@ -7,26 +8,9 @@ const config = {
const math = create(all, config);
const OXYGEN_INDEX = 0;
const S_O_INDEX = 0;
const NUM_SPECIES = 13;
/**
* Assert that no NaN values are present in an array.
* @param {Array} arr
* @param {string} label
*/
function assertNoNaN(arr, label = "array") {
if (Array.isArray(arr)) {
for (const el of arr) {
assertNoNaN(el, label);
}
} else {
if (Number.isNaN(arr)) {
throw new Error(`NaN detected in ${label}!`);
}
}
}
class Reactor {
/**
* Reactor base class.
@@ -35,10 +19,10 @@ class Reactor {
constructor(config) {
this.asm = new ASM3();
this.Vl = config.volume; // fluid volume reactor [m3]
this.volume = config.volume; // fluid volume reactor [m3]
this.Fs = Array(config.n_inlets).fill(0.0); // fluid debits per inlet [m3 d-1]
this.Cs_in = Array.from(Array(config.n_inlets), () => new Array(NUM_SPECIES).fill(0.0)); // composition influents
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.OTR = 0.0; // oxygen transfer rate [g O2 d-1]
this.kla = config.kla; // if NaN, use externaly provided OTR [d-1]
@@ -139,11 +123,11 @@ class Reactor_CSTR extends Reactor {
* @returns {Array} - New reactor state.
*/
tick(time_step) { // tick reactor state using forward Euler method
const inflow = math.multiply(math.divide([this.Fs], this.Vl), this.Cs_in)[0];
const outflow = math.multiply(-1 * math.sum(this.Fs) / this.Vl, this.state);
const inflow = math.multiply(math.divide([this.Fs], this.volume), this.Cs_in)[0];
const outflow = math.multiply(-1 * math.sum(this.Fs) / this.volume, this.state);
const reaction = this.asm.compute_dC(this.state);
const transfer = Array(NUM_SPECIES).fill(0.0);
transfer[OXYGEN_INDEX] = isNaN(this.kla) ? this.OTR : this._calcOTR(this.state[OXYGEN_INDEX]); // 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]); // 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);
assertNoNaN(dC_total, "change in state");
@@ -166,7 +150,7 @@ class Reactor_PFR extends Reactor {
this.n_x = config.resolution_L; // number of slices
this.d_x = this.length / this.n_x;
this.A = this.Vl / this.length; // crosssectional area [m2]
this.A = this.volume / this.length; // crosssectional area [m2]
this.state = Array.from(Array(this.n_x), () => config.initialState.slice())
@@ -198,11 +182,16 @@ class Reactor_PFR extends Reactor {
return { topic: "Fluent", payload: { inlet: 0, F: math.sum(this.Fs), C: this.state.at(-1) }, timestamp: this.currentTime };
}
/**
* Apply boundary conditions to the reactor state.
* for inlet, apply generalised Danckwerts BC, if there is not flow, apply Neumann BC with no flux
* for outlet, apply regular Danckwerts BC (Neumann BC with no flux)
* @param {Array} state - Current reactor state without enforced BCs.
*/
_applyBoundaryConditions(state) {
// apply boundary conditions
if (math.sum(this.Fs) > 0) { // Danckwerts BC
const BC_C_in = math.multiply(1 / math.sum(this.Fs), [this.Fs], this.Cs_in)[0];
const BC_gradient = Array(this.n_x).fill(0.0);
const BC_gradient = Array(this.n_x).fill(0);
BC_gradient[0] = -1;
BC_gradient[1] = 1;
let Pe = this.length * math.sum(this.Fs) / (this.D * this.A)
@@ -224,16 +213,16 @@ class Reactor_PFR extends Reactor {
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));
const transfer = Array.from(Array(this.n_x), () => new Array(NUM_SPECIES).fill(0.0));
const transfer = Array.from(Array(this.n_x), () => new Array(NUM_SPECIES).fill(0));
assertNoNaN(dispersion, "dispersion");
assertNoNaN(advection, "advection");
assertNoNaN(reaction, "reaction");
if (isNaN(this.kla)) { // calculate OTR if kla is not NaN, otherwise use externally calculated OTR
transfer.forEach((x) => { x[OXYGEN_INDEX] = this.OTR; });
transfer.forEach((x) => { x[S_O_INDEX] = this.OTR; });
} else {
transfer.forEach((x, i) => { x[OXYGEN_INDEX] = this._calcOTR(this.state[i][OXYGEN_INDEX]); });
transfer.forEach((x, i) => { x[S_O_INDEX] = this._calcOTR(this.state[i][S_O_INDEX]); });
}
const dC_total = math.multiply(math.add(dispersion, advection, reaction, transfer), time_step);

18
src/utils.js Normal file
View File

@@ -0,0 +1,18 @@
/**
* Assert that no NaN values are present in an array.
* @param {Array} arr
* @param {string} label
*/
function assertNoNaN(arr, label = "array") {
if (Array.isArray(arr)) {
for (const el of arr) {
assertNoNaN(el, label);
}
} else {
if (Number.isNaN(arr)) {
throw new Error(`NaN detected in ${label}!`);
}
}
}
module.exports = { assertNoNaN };