|
| 1 | +# |
| 2 | +# Test function control submodel |
| 3 | +# |
| 4 | +import numpy as np |
| 5 | +import pybamm |
| 6 | +import tests |
| 7 | +import unittest |
| 8 | + |
| 9 | + |
| 10 | +class TestFunctionControl(unittest.TestCase): |
| 11 | + def test_constant_current(self): |
| 12 | + # load models |
| 13 | + models = [ |
| 14 | + pybamm.lithium_ion.SPM(), |
| 15 | + pybamm.lithium_ion.SPM({"operating mode": "custom"}), |
| 16 | + ] |
| 17 | + |
| 18 | + # load parameter values and process models and geometry |
| 19 | + params = [model.default_parameter_values for model in models] |
| 20 | + |
| 21 | + # First model: 1A charge |
| 22 | + params[0]["Typical current [A]"] = -1 |
| 23 | + |
| 24 | + # Second model: 1C charge via a function |
| 25 | + def constant_current(I, V): |
| 26 | + return I + 1 |
| 27 | + |
| 28 | + params[1]["External circuit function"] = constant_current |
| 29 | + |
| 30 | + # set parameters and discretise models |
| 31 | + for i, model in enumerate(models): |
| 32 | + # create geometry |
| 33 | + geometry = model.default_geometry |
| 34 | + params[i].process_model(model) |
| 35 | + params[i].process_geometry(geometry) |
| 36 | + mesh = pybamm.Mesh( |
| 37 | + geometry, model.default_submesh_types, model.default_var_pts |
| 38 | + ) |
| 39 | + disc = pybamm.Discretisation(mesh, model.default_spatial_methods) |
| 40 | + disc.process_model(model) |
| 41 | + |
| 42 | + # solve model |
| 43 | + solutions = [None] * len(models) |
| 44 | + t_eval = np.linspace(0, 1, 100) |
| 45 | + for i, model in enumerate(models): |
| 46 | + solutions[i] = model.default_solver.solve(model, t_eval) |
| 47 | + |
| 48 | + V0 = pybamm.ProcessedVariable( |
| 49 | + models[0].variables["Terminal voltage [V]"], |
| 50 | + solutions[0].t, |
| 51 | + solutions[0].y, |
| 52 | + mesh, |
| 53 | + ).entries |
| 54 | + V1 = pybamm.ProcessedVariable( |
| 55 | + models[1].variables["Terminal voltage [V]"], |
| 56 | + solutions[1].t, |
| 57 | + solutions[1].y, |
| 58 | + mesh, |
| 59 | + ).entries |
| 60 | + np.testing.assert_array_equal(V0, V1) |
| 61 | + |
| 62 | + def test_constant_voltage(self): |
| 63 | + # load models |
| 64 | + models = [ |
| 65 | + pybamm.lithium_ion.SPM({"operating mode": "voltage"}), |
| 66 | + pybamm.lithium_ion.SPM({"operating mode": "custom"}), |
| 67 | + ] |
| 68 | + |
| 69 | + # load parameter values and process models and geometry |
| 70 | + params = [model.default_parameter_values for model in models] |
| 71 | + |
| 72 | + # First model: 4.1V charge |
| 73 | + params[0]["Voltage function"] = 4.1 |
| 74 | + |
| 75 | + # Second model: 4.1V charge via a function |
| 76 | + def constant_voltage(I, V): |
| 77 | + return V - 4.1 |
| 78 | + |
| 79 | + params[1]["External circuit function"] = constant_voltage |
| 80 | + |
| 81 | + # set parameters and discretise models |
| 82 | + for i, model in enumerate(models): |
| 83 | + # create geometry |
| 84 | + geometry = model.default_geometry |
| 85 | + params[i].process_model(model) |
| 86 | + params[i].process_geometry(geometry) |
| 87 | + mesh = pybamm.Mesh( |
| 88 | + geometry, model.default_submesh_types, model.default_var_pts |
| 89 | + ) |
| 90 | + disc = pybamm.Discretisation(mesh, model.default_spatial_methods) |
| 91 | + disc.process_model(model) |
| 92 | + |
| 93 | + # solve model |
| 94 | + solutions = [None] * len(models) |
| 95 | + t_eval = np.linspace(0, 1, 100) |
| 96 | + for i, model in enumerate(models): |
| 97 | + solutions[i] = model.default_solver.solve(model, t_eval) |
| 98 | + |
| 99 | + V0 = pybamm.ProcessedVariable( |
| 100 | + models[0].variables["Terminal voltage [V]"], |
| 101 | + solutions[0].t, |
| 102 | + solutions[0].y, |
| 103 | + mesh, |
| 104 | + ).entries |
| 105 | + V1 = pybamm.ProcessedVariable( |
| 106 | + models[1].variables["Terminal voltage [V]"], |
| 107 | + solutions[1].t, |
| 108 | + solutions[1].y, |
| 109 | + mesh, |
| 110 | + ).entries |
| 111 | + np.testing.assert_array_equal(V0, V1) |
| 112 | + |
| 113 | + I0 = pybamm.ProcessedVariable( |
| 114 | + models[0].variables["Current [A]"], solutions[0].t, solutions[0].y, mesh |
| 115 | + ).entries |
| 116 | + I1 = pybamm.ProcessedVariable( |
| 117 | + models[1].variables["Current [A]"], solutions[1].t, solutions[1].y, mesh |
| 118 | + ).entries |
| 119 | + np.testing.assert_array_equal(I0, I1) |
| 120 | + |
| 121 | + def test_constant_power(self): |
| 122 | + # load models |
| 123 | + models = [ |
| 124 | + pybamm.lithium_ion.SPM({"operating mode": "power"}), |
| 125 | + pybamm.lithium_ion.SPM({"operating mode": "custom"}), |
| 126 | + ] |
| 127 | + |
| 128 | + # load parameter values and process models and geometry |
| 129 | + params = [model.default_parameter_values for model in models] |
| 130 | + |
| 131 | + # First model: 4W charge |
| 132 | + params[0]["Power function"] = 4 |
| 133 | + |
| 134 | + # Second model: 4W charge via a function |
| 135 | + def constant_power(I, V): |
| 136 | + return I * V - 4 |
| 137 | + |
| 138 | + params[1]["External circuit function"] = constant_power |
| 139 | + |
| 140 | + # set parameters and discretise models |
| 141 | + for i, model in enumerate(models): |
| 142 | + # create geometry |
| 143 | + geometry = model.default_geometry |
| 144 | + params[i].process_model(model) |
| 145 | + params[i].process_geometry(geometry) |
| 146 | + mesh = pybamm.Mesh( |
| 147 | + geometry, model.default_submesh_types, model.default_var_pts |
| 148 | + ) |
| 149 | + disc = pybamm.Discretisation(mesh, model.default_spatial_methods) |
| 150 | + disc.process_model(model) |
| 151 | + |
| 152 | + # solve model |
| 153 | + solutions = [None] * len(models) |
| 154 | + t_eval = np.linspace(0, 1, 100) |
| 155 | + for i, model in enumerate(models): |
| 156 | + solutions[i] = model.default_solver.solve(model, t_eval) |
| 157 | + |
| 158 | + V0 = pybamm.ProcessedVariable( |
| 159 | + models[0].variables["Terminal voltage [V]"], |
| 160 | + solutions[0].t, |
| 161 | + solutions[0].y, |
| 162 | + mesh, |
| 163 | + ).entries |
| 164 | + V1 = pybamm.ProcessedVariable( |
| 165 | + models[1].variables["Terminal voltage [V]"], |
| 166 | + solutions[1].t, |
| 167 | + solutions[1].y, |
| 168 | + mesh, |
| 169 | + ).entries |
| 170 | + np.testing.assert_array_equal(V0, V1) |
| 171 | + |
| 172 | + I0 = pybamm.ProcessedVariable( |
| 173 | + models[0].variables["Current [A]"], solutions[0].t, solutions[0].y, mesh |
| 174 | + ).entries |
| 175 | + I1 = pybamm.ProcessedVariable( |
| 176 | + models[1].variables["Current [A]"], solutions[1].t, solutions[1].y, mesh |
| 177 | + ).entries |
| 178 | + np.testing.assert_array_equal(I0, I1) |
| 179 | + |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + print("Add -v for more debug output") |
| 183 | + import sys |
| 184 | + |
| 185 | + if "-v" in sys.argv: |
| 186 | + debug = True |
| 187 | + pybamm.settings.debug_mode = True |
| 188 | + unittest.main() |
0 commit comments