Files
reactor/dependencies/reactor_class.js

84 lines
3.0 KiB
JavaScript

const ASM3 = require('./asm3_class')
const math = require('mathjs')
class Reactor_CSTR {
constructor(volume, n_inlets, kla, initial_state) {
this.state = initial_state;
console.log(this.state);
this.asm = new ASM3();
this.Vl = volume; // fluid volume reactor [m3]
this.Fs = Array(n_inlets).fill(0.0); // fluid debits per inlet [m3 d-1]
this.Cs_in = Array.from(Array(n_inlets), () => new Array(13).fill(0.0)); // composition influents
this.OTR = 0.0; // oxygen transfer rate [g O2 d-1]
this.kla = kla; // if NaN, use external OTR [d-1]
this.currentTime = Date.now(); // milliseconds since epoch [ms]
this.timeStep = 1/(24*60*15) // time step [d]
}
set setInfluent(input) { // setter for C_in (WIP)
let index_in = input.payload.inlet;
this.Fs[index_in] = input.payload.F;
this.Cs_in[index_in] = input.payload.C;
}
set setOTR(input) { // setter for OTR (WIP) [g O2 d-1]
this.OTR = input.payload;
}
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};
}
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);
}
// expect update with timestamp
updateState(timestamp) {
let newTime = timestamp;
const day2ms = 1000 * 60 * 60 * 24;
let n_iter = Math.floor((newTime - this.currentTime) / (this.timeStep * day2ms));
if (n_iter > 0) {
let n = 0;
while (n < n_iter) {
console.log(this.tick_fe(this.timeStep));
n += 1;
}
this.currentTime += n_iter * this.timeStep * day2ms;
n_iter = 0;
}
}
tick_fe(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(math.sum(this.Fs)/this.Vl, this.state);
const t_O = Array(13).fill(0.0);
t_O[0] = isNaN(this.kla) ? this.OTR : this.calcOTR(this.state[0]); // calculate OTR if kla is not NaN, otherwise use externaly calculated OTR
const dC_total = math.multiply(math.add(dC_in, dC_out, r, t_O), time_step);
this.state = math.add(this.state, dC_total);
return this.state;
}
}
// testing stuff
// 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_CSTR(initial_state);
// Reactor.C_in = [0.0, 30., 100., 16., 0., 0., 5., 25., 75., 30., 0., 0., 125.];
// N = 0;
// while (N < 500) {
// console.log(Reactor.tick_fe(0.001));
// N += 1;
// }
module.exports = Reactor_CSTR;