|
| 1 | +######################################################################################## |
| 2 | +## |
| 3 | +## TESTS FOR |
| 4 | +## 'process.heater.py' |
| 5 | +## |
| 6 | +######################################################################################## |
| 7 | + |
| 8 | +# IMPORTS ============================================================================== |
| 9 | + |
| 10 | +import unittest |
| 11 | + |
| 12 | +from pathsim_chem.process import Heater |
| 13 | + |
| 14 | + |
| 15 | +# TESTS ================================================================================ |
| 16 | + |
| 17 | +class TestHeater(unittest.TestCase): |
| 18 | + """Test the duty-specified heater/cooler block.""" |
| 19 | + |
| 20 | + def test_init_default(self): |
| 21 | + """Test default parameters.""" |
| 22 | + H = Heater() |
| 23 | + self.assertEqual(H.rho, 1000.0) |
| 24 | + self.assertEqual(H.Cp, 4184.0) |
| 25 | + |
| 26 | + def test_init_custom(self): |
| 27 | + """Test custom parameters.""" |
| 28 | + H = Heater(rho=800.0, Cp=3000.0) |
| 29 | + self.assertEqual(H.rho, 800.0) |
| 30 | + self.assertEqual(H.Cp, 3000.0) |
| 31 | + |
| 32 | + def test_init_validation(self): |
| 33 | + """Test input validation.""" |
| 34 | + with self.assertRaises(ValueError): |
| 35 | + Heater(rho=0) |
| 36 | + with self.assertRaises(ValueError): |
| 37 | + Heater(rho=-1) |
| 38 | + with self.assertRaises(ValueError): |
| 39 | + Heater(Cp=0) |
| 40 | + with self.assertRaises(ValueError): |
| 41 | + Heater(Cp=-1) |
| 42 | + |
| 43 | + def test_port_labels(self): |
| 44 | + """Test port label definitions.""" |
| 45 | + self.assertEqual(Heater.input_port_labels["F"], 0) |
| 46 | + self.assertEqual(Heater.input_port_labels["T_in"], 1) |
| 47 | + self.assertEqual(Heater.input_port_labels["Q"], 2) |
| 48 | + self.assertEqual(Heater.output_port_labels["F_out"], 0) |
| 49 | + self.assertEqual(Heater.output_port_labels["T_out"], 1) |
| 50 | + |
| 51 | + def test_heating(self): |
| 52 | + """Positive Q should increase temperature.""" |
| 53 | + H = Heater(rho=1000.0, Cp=4184.0) |
| 54 | + F = 0.1 # m³/s |
| 55 | + T_in = 300.0 |
| 56 | + Q = 41840.0 # W |
| 57 | + |
| 58 | + H.inputs[0] = F |
| 59 | + H.inputs[1] = T_in |
| 60 | + H.inputs[2] = Q |
| 61 | + |
| 62 | + H.update(None) |
| 63 | + |
| 64 | + # dT = Q / (F * rho * Cp) = 41840 / (0.1 * 1000 * 4184) = 0.1 K |
| 65 | + expected_T = T_in + Q / (F * 1000.0 * 4184.0) |
| 66 | + self.assertAlmostEqual(H.outputs[1], expected_T, places=6) |
| 67 | + |
| 68 | + def test_cooling(self): |
| 69 | + """Negative Q should decrease temperature.""" |
| 70 | + H = Heater(rho=1000.0, Cp=4184.0) |
| 71 | + H.inputs[0] = 0.1 |
| 72 | + H.inputs[1] = 350.0 |
| 73 | + H.inputs[2] = -41840.0 |
| 74 | + |
| 75 | + H.update(None) |
| 76 | + |
| 77 | + self.assertLess(H.outputs[1], 350.0) |
| 78 | + |
| 79 | + def test_zero_duty(self): |
| 80 | + """Q=0 should pass temperature through unchanged.""" |
| 81 | + H = Heater() |
| 82 | + H.inputs[0] = 0.1 |
| 83 | + H.inputs[1] = 350.0 |
| 84 | + H.inputs[2] = 0.0 |
| 85 | + |
| 86 | + H.update(None) |
| 87 | + |
| 88 | + self.assertAlmostEqual(H.outputs[1], 350.0) |
| 89 | + |
| 90 | + def test_flow_passthrough(self): |
| 91 | + """F_out should equal F_in.""" |
| 92 | + H = Heater() |
| 93 | + H.inputs[0] = 0.5 |
| 94 | + H.inputs[1] = 300.0 |
| 95 | + H.inputs[2] = 10000.0 |
| 96 | + |
| 97 | + H.update(None) |
| 98 | + |
| 99 | + self.assertAlmostEqual(H.outputs[0], 0.5) |
| 100 | + |
| 101 | + def test_zero_flow(self): |
| 102 | + """With zero flow, temperature should remain unchanged.""" |
| 103 | + H = Heater() |
| 104 | + H.inputs[0] = 0.0 |
| 105 | + H.inputs[1] = 350.0 |
| 106 | + H.inputs[2] = 10000.0 |
| 107 | + |
| 108 | + H.update(None) |
| 109 | + |
| 110 | + self.assertAlmostEqual(H.outputs[0], 0.0) |
| 111 | + self.assertAlmostEqual(H.outputs[1], 350.0) |
| 112 | + |
| 113 | + |
| 114 | +# RUN TESTS LOCALLY ==================================================================== |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + unittest.main(verbosity=2) |
0 commit comments