Fixed bug in NaN assertion

This commit is contained in:
2025-07-04 15:48:05 +02:00
parent c6b0cab067
commit 4b49d10763

View File

@@ -13,8 +13,14 @@ const math = create(all, config)
* @param {string} label * @param {string} label
*/ */
function assertNoNaN(arr, label = "array") { function assertNoNaN(arr, label = "array") {
if (math.isNaN(arr)) { if (Array.isArray(arr)) {
throw new Error("NaN detected in ${label}!"); for (const el of arr) {
assertNoNaN(el, label);
}
} else {
if (Number.isNaN(arr)) {
throw new Error(`NaN detected in ${label}!`);
}
} }
} }
@@ -80,7 +86,7 @@ class Reactor {
*/ */
_arrayClip2Zero(arr) { _arrayClip2Zero(arr) {
if (Array.isArray(arr)) { if (Array.isArray(arr)) {
return arr.map(clipToZero); return arr.map(x => this._arrayClip2Zero(x));
} else { } else {
return arr < 0 ? 0 : arr; return arr < 0 ? 0 : arr;
} }
@@ -166,6 +172,9 @@ class Reactor_PFR extends Reactor {
this.D_op = this._makeDoperator(true, true); this.D_op = this._makeDoperator(true, true);
this.D2_op = this._makeD2operator(); this.D2_op = this._makeD2operator();
assertNoNaN(this.D_op, "Derivative operator");
assertNoNaN(this.D2_op, "Second derivative operator");
} }
/** /**
@@ -184,7 +193,7 @@ class Reactor_PFR extends Reactor {
return { topic: "Fluent", payload: { inlet: 0, F: math.sum(this.Fs), C: this.state.at(-1) }, timestamp: this.currentTime }; return { topic: "Fluent", payload: { inlet: 0, F: math.sum(this.Fs), C: this.state.at(-1) }, timestamp: this.currentTime };
} }
_applyBoundaryConditions(newState) { _applyBoundaryConditions(state) {
// apply boundary conditions // apply boundary conditions
if (math.sum(this.Fs) > 0) { // Danckwerts BC 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_C_in = math.multiply(1 / math.sum(this.Fs), [this.Fs], this.Cs_in)[0];
@@ -192,14 +201,13 @@ class Reactor_PFR extends Reactor {
BC_gradient[0] = -1; BC_gradient[0] = -1;
BC_gradient[1] = 1; BC_gradient[1] = 1;
let Pe = this.length * math.sum(this.Fs) / (this.D * this.A) let Pe = this.length * math.sum(this.Fs) / (this.D * this.A)
const BC_dispersion = math.multiply((1 - (1 + 4 * this.volume / math.sum(this.Fs) / Pe) ^ 0.5) / Pe, [BC_gradient], stateNew)[0]; const BC_dispersion = math.multiply((1 - (1 + 4 * this.volume / math.sum(this.Fs) / Pe) ^ 0.5) / Pe, [BC_gradient], state)[0];
newState[0] = math.add(BC_C_in, BC_dispersion).map(val => val < 0 ? 0 : val); state[0] = math.add(BC_C_in, BC_dispersion).map(val => val < 0 ? 0 : val);
} else { // Neumann BC (no flux) } else { // Neumann BC (no flux)
newState[0] = newState[1]; state[0] = state[1];
} }
// Neumann BC (no flux) // Neumann BC (no flux)
newState[this.n_x - 1] = newState[this.n_x - 2] state[this.n_x - 1] = state[this.n_x - 2]
return newState
} }
/** /**
@@ -224,11 +232,11 @@ class Reactor_PFR extends Reactor {
} }
const dC_total = math.multiply(math.add(dispersion, advection, reaction, transfer), time_step); const dC_total = math.multiply(math.add(dispersion, advection, reaction, transfer), time_step);
let stateNew = math.add(this.state, dC_total); const stateNew = math.add(this.state, dC_total);
assertNoNaN(stateNew, "new state"); assertNoNaN(stateNew, "new state");
stateNew = this._applyBoundaryConditions(stateNew); this._applyBoundaryConditions(stateNew);
assertNoNaN(stateNew, "new state post BC"); assertNoNaN(stateNew, "new state post BC");