Refactor ASM3 constructor to remove state parameter and update compute_dC method to use state directly; add Reactor_CSTR class for reactor simulation with forward Euler method

This commit is contained in:
2025-06-11 15:17:09 +02:00
parent 333efcda52
commit 341af6db4d
2 changed files with 27 additions and 10 deletions

View File

@@ -61,9 +61,8 @@ class ASM3 {
i_cNO: -1/14 // charge per S_NO [mole H+ g-1 NO3-N]
}
constructor(state) {
constructor() {
// 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
this.state = state;
this.stoi_matrix = this._initialise_stoi_matrix()
}
@@ -96,10 +95,10 @@ class ASM3 {
return K / (K + c);
}
compute_rates(state) { // computes reaction rates. state is optional, if not provided, use class state
compute_rates(state) { // computes reaction rates. state is optional
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 || this.state;
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;
// Hydrolysis
@@ -123,12 +122,9 @@ class ASM3 {
return rates;
}
compute_dC(dt){ // compute change in concentration over time step dt
return math.multiply(math.multiply(this.stoi_matrix, this.compute_rates()), dt);
compute_dC(state){ // compute changes in concentrations
return math.multiply(this.stoi_matrix, this.compute_rates(state));
}
}
// testing stuff
const asm3 = new ASM3([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])
console.log(asm3.compute_rates())
console.log(asm3.compute_dC(0.001))
module.exports = ASM3;

21
dependencies/reactor_class.js vendored Normal file
View File

@@ -0,0 +1,21 @@
const ASM3 = require('./asm3_class')
const math = require('mathjs')
class Reactor_CSTR {
constructor(initial_state){
this.state = initial_state;
this.asm = new ASM3();
}
tick_fe(time_step){ // tick reactor state using forward Euler method
const delta = this.asm.compute_dC(this.state);
this.state = math.add(this.state, math.multiply(delta, time_step));
return this.state;
}
}
// testing stuff
let 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(state);
console.log(Reactor.tick_fe(0.001));