Compare commits
1 Commits
460b872053
...
2c69a5a0c1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c69a5a0c1 |
@@ -39,6 +39,7 @@
|
|||||||
X_TS_init: { value: 125.0009, required: true },
|
X_TS_init: { value: 125.0009, required: true },
|
||||||
|
|
||||||
timeStep: { value: 1, required: true },
|
timeStep: { value: 1, required: true },
|
||||||
|
speedUpFactor: { value: 1 },
|
||||||
|
|
||||||
enableLog: { value: false },
|
enableLog: { value: false },
|
||||||
logLevel: { value: "error" },
|
logLevel: { value: "error" },
|
||||||
@@ -115,6 +116,10 @@
|
|||||||
type:"num",
|
type:"num",
|
||||||
types:["num"]
|
types:["num"]
|
||||||
})
|
})
|
||||||
|
$("#node-input-speedUpFactor").typedInput({
|
||||||
|
type:"num",
|
||||||
|
types:["num"]
|
||||||
|
})
|
||||||
// Set initial visibility on dialog open
|
// Set initial visibility on dialog open
|
||||||
const initialType = $("#node-input-reactor_type").typedInput("value");
|
const initialType = $("#node-input-reactor_type").typedInput("value");
|
||||||
if (initialType === "CSTR") {
|
if (initialType === "CSTR") {
|
||||||
@@ -243,6 +248,10 @@
|
|||||||
<label for="node-input-timeStep"><i class="fa fa-tag"></i> Time step [s]</label>
|
<label for="node-input-timeStep"><i class="fa fa-tag"></i> Time step [s]</label>
|
||||||
<input type="text" id="node-input-timeStep" placeholder="s">
|
<input type="text" id="node-input-timeStep" placeholder="s">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-speedUpFactor"><i class="fa fa-tag"></i> Speed-up factor</label>
|
||||||
|
<input type="text" id="node-input-speedUpFactor" placeholder="1 = real-time">
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Logger fields injected here -->
|
<!-- Logger fields injected here -->
|
||||||
<div id="logger-fields-placeholder"></div>
|
<div id="logger-fields-placeholder"></div>
|
||||||
|
|||||||
@@ -112,7 +112,8 @@ class nodeClass {
|
|||||||
parseFloat(uiConfig.X_A_init),
|
parseFloat(uiConfig.X_A_init),
|
||||||
parseFloat(uiConfig.X_TS_init)
|
parseFloat(uiConfig.X_TS_init)
|
||||||
],
|
],
|
||||||
timeStep: parseFloat(uiConfig.timeStep)
|
timeStep: parseFloat(uiConfig.timeStep),
|
||||||
|
speedUpFactor: Number(uiConfig.speedUpFactor) || 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,6 +160,10 @@ class nodeClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_tick(){
|
_tick(){
|
||||||
|
const gridProfile = this.source.getGridProfile;
|
||||||
|
if (gridProfile) {
|
||||||
|
this.node.send([{ topic: "GridProfile", payload: gridProfile }, null, null]);
|
||||||
|
}
|
||||||
this.node.send([this.source.getEffluent, null, null]);
|
this.node.send([this.source.getEffluent, null, null]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class Reactor {
|
|||||||
|
|
||||||
this.currentTime = Date.now(); // milliseconds since epoch [ms]
|
this.currentTime = Date.now(); // milliseconds since epoch [ms]
|
||||||
this.timeStep = 1 / (24*60*60) * this.config.timeStep; // time step in seconds, converted to days.
|
this.timeStep = 1 / (24*60*60) * this.config.timeStep; // time step in seconds, converted to days.
|
||||||
this.speedUpFactor = 60; // speed up factor for simulation, 60 means 1 minute per simulated second
|
this.speedUpFactor = config.speedUpFactor ?? 1; // speed up factor for simulation
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,6 +91,8 @@ class Reactor {
|
|||||||
return { topic: "Fluent", payload: { inlet: 0, F: math.sum(this.Fs), C: this.state }, timestamp: this.currentTime };
|
return { topic: "Fluent", payload: { inlet: 0, F: math.sum(this.Fs), C: this.state }, timestamp: this.currentTime };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get getGridProfile() { return null; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the oxygen transfer rate (OTR) based on the dissolved oxygen concentration and temperature.
|
* Calculate the oxygen transfer rate (OTR) based on the dissolved oxygen concentration and temperature.
|
||||||
* @param {number} S_O - Dissolved oxygen concentration [g O2 m-3].
|
* @param {number} S_O - Dissolved oxygen concentration [g O2 m-3].
|
||||||
@@ -275,6 +277,18 @@ class Reactor_PFR extends Reactor {
|
|||||||
assertNoNaN(this.D2_op, "Second derivative operator");
|
assertNoNaN(this.D2_op, "Second derivative operator");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get getGridProfile() {
|
||||||
|
return {
|
||||||
|
grid: this.state.map(row => row.slice()),
|
||||||
|
n_x: this.n_x,
|
||||||
|
d_x: this.d_x,
|
||||||
|
length: this.length,
|
||||||
|
species: ['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'],
|
||||||
|
timestamp: this.currentTime
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setter for axial dispersion.
|
* Setter for axial dispersion.
|
||||||
* @param {object} input - Input object (msg) containing payload with dispersion value [m2 d-1].
|
* @param {object} input - Input object (msg) containing payload with dispersion value [m2 d-1].
|
||||||
|
|||||||
45
test/basic/grid-profile.basic.test.js
Normal file
45
test/basic/grid-profile.basic.test.js
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
|
||||||
|
const { Reactor_CSTR, Reactor_PFR } = require('../../src/specificClass');
|
||||||
|
const { makeReactorConfig } = require('../helpers/factories');
|
||||||
|
|
||||||
|
test('CSTR getGridProfile returns null', () => {
|
||||||
|
const reactor = new Reactor_CSTR(makeReactorConfig({ reactor_type: 'CSTR' }));
|
||||||
|
assert.equal(reactor.getGridProfile, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PFR getGridProfile returns state matrix with correct dimensions', () => {
|
||||||
|
const n_x = 8;
|
||||||
|
const length = 40;
|
||||||
|
const reactor = new Reactor_PFR(
|
||||||
|
makeReactorConfig({ reactor_type: 'PFR', resolution_L: n_x, length }),
|
||||||
|
);
|
||||||
|
|
||||||
|
const profile = reactor.getGridProfile;
|
||||||
|
assert.notEqual(profile, null);
|
||||||
|
assert.equal(profile.n_x, n_x);
|
||||||
|
assert.equal(profile.d_x, length / n_x);
|
||||||
|
assert.equal(profile.length, length);
|
||||||
|
assert.equal(profile.grid.length, n_x, 'grid should have n_x rows');
|
||||||
|
assert.equal(profile.grid[0].length, 13, 'each row should have 13 species');
|
||||||
|
assert.ok(Array.isArray(profile.species), 'species list should be an array');
|
||||||
|
assert.equal(profile.species.length, 13);
|
||||||
|
assert.equal(profile.species[3], 'S_NH');
|
||||||
|
assert.equal(typeof profile.timestamp, 'number');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PFR getGridProfile is mutation-safe', () => {
|
||||||
|
const reactor = new Reactor_PFR(
|
||||||
|
makeReactorConfig({ reactor_type: 'PFR', resolution_L: 5, length: 10 }),
|
||||||
|
);
|
||||||
|
|
||||||
|
const profile = reactor.getGridProfile;
|
||||||
|
const originalValue = reactor.state[0][3]; // S_NH at cell 0
|
||||||
|
|
||||||
|
// Mutate the returned grid
|
||||||
|
profile.grid[0][3] = 999;
|
||||||
|
|
||||||
|
// Reactor internal state should be unchanged
|
||||||
|
assert.equal(reactor.state[0][3], originalValue, 'mutating grid copy must not affect reactor state');
|
||||||
|
});
|
||||||
68
test/basic/speedup-factor.basic.test.js
Normal file
68
test/basic/speedup-factor.basic.test.js
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
|
||||||
|
const { Reactor_CSTR } = require('../../src/specificClass');
|
||||||
|
const nodeClass = require('../../src/nodeClass');
|
||||||
|
const { makeReactorConfig, makeUiConfig, makeNodeStub, makeREDStub } = require('../helpers/factories');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Smoke tests for Fix 3: configurable speedUpFactor on Reactor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
test('specificClass defaults speedUpFactor to 1 when not in config', () => {
|
||||||
|
const config = makeReactorConfig();
|
||||||
|
const reactor = new Reactor_CSTR(config);
|
||||||
|
assert.equal(reactor.speedUpFactor, 1, 'speedUpFactor should default to 1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('specificClass accepts speedUpFactor from config', () => {
|
||||||
|
const config = makeReactorConfig();
|
||||||
|
config.speedUpFactor = 10;
|
||||||
|
const reactor = new Reactor_CSTR(config);
|
||||||
|
assert.equal(reactor.speedUpFactor, 10, 'speedUpFactor should be read from config');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('specificClass accepts speedUpFactor = 60 for accelerated simulation', () => {
|
||||||
|
const config = makeReactorConfig();
|
||||||
|
config.speedUpFactor = 60;
|
||||||
|
const reactor = new Reactor_CSTR(config);
|
||||||
|
assert.equal(reactor.speedUpFactor, 60, 'speedUpFactor=60 should be accepted');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('nodeClass passes speedUpFactor from uiConfig to reactor config', () => {
|
||||||
|
const uiConfig = makeUiConfig({ speedUpFactor: 5 });
|
||||||
|
const node = makeNodeStub();
|
||||||
|
const RED = makeREDStub();
|
||||||
|
|
||||||
|
const nc = new nodeClass(uiConfig, RED, node, 'test-reactor');
|
||||||
|
assert.equal(nc.source.speedUpFactor, 5, 'nodeClass should pass speedUpFactor=5 to specificClass');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('nodeClass defaults speedUpFactor to 1 when not in uiConfig', () => {
|
||||||
|
const uiConfig = makeUiConfig();
|
||||||
|
// Ensure speedUpFactor is not set
|
||||||
|
delete uiConfig.speedUpFactor;
|
||||||
|
|
||||||
|
const node = makeNodeStub();
|
||||||
|
const RED = makeREDStub();
|
||||||
|
|
||||||
|
const nc = new nodeClass(uiConfig, RED, node, 'test-reactor');
|
||||||
|
assert.equal(nc.source.speedUpFactor, 1, 'nodeClass should default speedUpFactor to 1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('updateState with speedUpFactor=1 advances roughly real-time', () => {
|
||||||
|
const config = makeReactorConfig();
|
||||||
|
config.speedUpFactor = 1;
|
||||||
|
config.n_inlets = 1;
|
||||||
|
const reactor = new Reactor_CSTR(config);
|
||||||
|
|
||||||
|
// Set a known start time
|
||||||
|
const t0 = reactor.currentTime;
|
||||||
|
// Advance by 2 seconds real time
|
||||||
|
reactor.updateState(t0 + 2000);
|
||||||
|
|
||||||
|
// With speedUpFactor=1, simulation should have advanced ~2 seconds worth
|
||||||
|
// (not 120 seconds like with the old hardcoded 60x factor)
|
||||||
|
const elapsed = reactor.currentTime - t0;
|
||||||
|
assert.ok(elapsed < 5000, `Elapsed ${elapsed}ms should be close to 2000ms, not 120000ms (old 60x factor)`);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user