|
| 1 | +######################################################################################## |
| 2 | +## |
| 3 | +## TESTS FOR |
| 4 | +## 'process.pfr.py' |
| 5 | +## |
| 6 | +######################################################################################## |
| 7 | + |
| 8 | +# IMPORTS ============================================================================== |
| 9 | + |
| 10 | +import unittest |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +from pathsim_chem.process import PFR |
| 14 | + |
| 15 | +from pathsim.solvers import EUF |
| 16 | + |
| 17 | + |
| 18 | +# TESTS ================================================================================ |
| 19 | + |
| 20 | +class TestPFR(unittest.TestCase): |
| 21 | + """Test the plug flow reactor block.""" |
| 22 | + |
| 23 | + def test_init_default(self): |
| 24 | + """Test default initialization.""" |
| 25 | + P = PFR() |
| 26 | + self.assertEqual(P.N_cells, 5) |
| 27 | + self.assertEqual(P.V, 1.0) |
| 28 | + self.assertEqual(P.F, 0.1) |
| 29 | + self.assertEqual(P.k0, 1e6) |
| 30 | + self.assertEqual(P.Ea, 50000.0) |
| 31 | + self.assertEqual(P.n, 1.0) |
| 32 | + |
| 33 | + def test_init_custom(self): |
| 34 | + """Test custom initialization.""" |
| 35 | + P = PFR(N_cells=10, V=2.0, F=0.5, k0=1e4, Ea=40000.0, n=2.0, |
| 36 | + dH_rxn=-30000.0, rho=800.0, Cp=3000.0, C0=1.5, T0=350.0) |
| 37 | + self.assertEqual(P.N_cells, 10) |
| 38 | + self.assertEqual(P.V, 2.0) |
| 39 | + |
| 40 | + P.set_solver(EUF, parent=None) |
| 41 | + state = P.engine.initial_value |
| 42 | + self.assertEqual(len(state), 20) # 2 * N_cells |
| 43 | + self.assertTrue(np.all(state[0::2] == 1.5)) # concentrations |
| 44 | + self.assertTrue(np.all(state[1::2] == 350.0)) # temperatures |
| 45 | + |
| 46 | + def test_init_validation(self): |
| 47 | + """Test input validation.""" |
| 48 | + with self.assertRaises(ValueError): |
| 49 | + PFR(N_cells=0) |
| 50 | + with self.assertRaises(ValueError): |
| 51 | + PFR(V=-1) |
| 52 | + with self.assertRaises(ValueError): |
| 53 | + PFR(F=0) |
| 54 | + with self.assertRaises(ValueError): |
| 55 | + PFR(rho=-1) |
| 56 | + with self.assertRaises(ValueError): |
| 57 | + PFR(Cp=0) |
| 58 | + |
| 59 | + def test_port_labels(self): |
| 60 | + """Test port label definitions.""" |
| 61 | + self.assertEqual(PFR.input_port_labels["C_in"], 0) |
| 62 | + self.assertEqual(PFR.input_port_labels["T_in"], 1) |
| 63 | + self.assertEqual(PFR.output_port_labels["C_out"], 0) |
| 64 | + self.assertEqual(PFR.output_port_labels["T_out"], 1) |
| 65 | + |
| 66 | + def test_state_size(self): |
| 67 | + """Test that state vector has correct size.""" |
| 68 | + for N in [1, 3, 10]: |
| 69 | + P = PFR(N_cells=N) |
| 70 | + P.set_solver(EUF, parent=None) |
| 71 | + self.assertEqual(len(P.engine.initial_value), 2 * N) |
| 72 | + |
| 73 | + def test_output_initial(self): |
| 74 | + """Test outputs at initial state.""" |
| 75 | + P = PFR(N_cells=3, C0=2.0, T0=350.0) |
| 76 | + P.set_solver(EUF, parent=None) |
| 77 | + P.update(None) |
| 78 | + |
| 79 | + # Last cell values |
| 80 | + self.assertAlmostEqual(P.outputs[0], 2.0) # C_out |
| 81 | + self.assertAlmostEqual(P.outputs[1], 350.0) # T_out |
| 82 | + |
| 83 | + def test_no_reaction(self): |
| 84 | + """With k0=0, no reaction occurs. At steady state C_out = C_in.""" |
| 85 | + P = PFR(N_cells=3, V=1.0, F=1.0, k0=0.0, Ea=0.0, n=1.0, |
| 86 | + dH_rxn=0.0, C0=1.0, T0=350.0) |
| 87 | + P.set_solver(EUF, parent=None) |
| 88 | + |
| 89 | + P.inputs[0] = 1.0 # C_in |
| 90 | + P.inputs[1] = 350.0 # T_in |
| 91 | + |
| 92 | + # When C=C_in and T=T_in everywhere, with no reaction, derivatives = 0 |
| 93 | + x = P.engine.get() |
| 94 | + u = np.array([1.0, 350.0]) |
| 95 | + dx = P.op_dyn(x, u, 0) |
| 96 | + |
| 97 | + self.assertTrue(np.allclose(dx, 0.0, atol=1e-10)) |
| 98 | + |
| 99 | + def test_reaction_direction(self): |
| 100 | + """With reaction, concentration should decrease along the reactor.""" |
| 101 | + R_GAS = 8.314 |
| 102 | + T = 350.0 |
| 103 | + k0, Ea = 1e6, 50000.0 |
| 104 | + k = k0 * np.exp(-Ea / (R_GAS * T)) |
| 105 | + |
| 106 | + P = PFR(N_cells=1, V=1.0, F=1.0, k0=k0, Ea=Ea, n=1.0, |
| 107 | + dH_rxn=0.0, C0=1.0, T0=T) |
| 108 | + P.set_solver(EUF, parent=None) |
| 109 | + |
| 110 | + # Set inlet = current state, so flow terms cancel |
| 111 | + P.inputs[0] = 1.0 # C_in |
| 112 | + P.inputs[1] = T # T_in |
| 113 | + |
| 114 | + x = np.array([1.0, T]) |
| 115 | + u = np.array([1.0, T]) |
| 116 | + dx = P.op_dyn(x, u, 0) |
| 117 | + |
| 118 | + # dC/dt = 0 (flow) - k*C = -k |
| 119 | + self.assertAlmostEqual(dx[0], -k, places=5) |
| 120 | + # dT/dt = 0 since dH_rxn=0 |
| 121 | + self.assertAlmostEqual(dx[1], 0.0, places=5) |
| 122 | + |
| 123 | + def test_energy_conservation_exothermic(self): |
| 124 | + """For exothermic reaction (dH_rxn < 0), temperature should increase.""" |
| 125 | + P = PFR(N_cells=1, V=1.0, F=1.0, k0=1e3, Ea=20000.0, n=1.0, |
| 126 | + dH_rxn=-50000.0, rho=1000.0, Cp=4184.0, |
| 127 | + C0=2.0, T0=350.0) |
| 128 | + P.set_solver(EUF, parent=None) |
| 129 | + |
| 130 | + P.inputs[0] = 2.0 |
| 131 | + P.inputs[1] = 350.0 |
| 132 | + |
| 133 | + x = np.array([2.0, 350.0]) |
| 134 | + u = np.array([2.0, 350.0]) |
| 135 | + dx = P.op_dyn(x, u, 0) |
| 136 | + |
| 137 | + # Concentration should decrease (reaction consuming) |
| 138 | + self.assertLess(dx[0], 0) |
| 139 | + # Temperature should increase (exothermic) |
| 140 | + self.assertGreater(dx[1], 0) |
| 141 | + |
| 142 | + def test_jacobian_stability(self): |
| 143 | + """Diagonal of Jacobian should be negative (stable).""" |
| 144 | + P = PFR(N_cells=2, V=1.0, F=0.1, k0=1e3, Ea=30000.0, n=1.0, |
| 145 | + dH_rxn=0.0, C0=1.0, T0=350.0) |
| 146 | + P.set_solver(EUF, parent=None) |
| 147 | + |
| 148 | + x = P.engine.get() |
| 149 | + u = np.array([1.0, 350.0]) |
| 150 | + J = P.op_dyn.jac_x(x, u, 0) |
| 151 | + |
| 152 | + # All diagonal elements should be negative |
| 153 | + for i in range(len(x)): |
| 154 | + self.assertLess(J[i, i], 0) |
| 155 | + |
| 156 | + |
| 157 | +# RUN TESTS LOCALLY ==================================================================== |
| 158 | + |
| 159 | +if __name__ == '__main__': |
| 160 | + unittest.main(verbosity=2) |
0 commit comments