コード例 #1
0
    def test_save(self):
        model = pybamm.BaseModel()
        # create both 1D and 2D variables
        c = pybamm.Variable("c")
        d = pybamm.Variable("d", domain="negative electrode")
        model.rhs = {c: -c, d: 1}
        model.initial_conditions = {c: 1, d: 2}
        model.variables = {"c": c, "d": d, "2c": 2 * c}

        disc = get_discretisation_for_testing()
        disc.process_model(model)
        solution = pybamm.ScipySolver().solve(model, np.linspace(0, 1))

        # test save data
        with self.assertRaises(ValueError):
            solution.save_data("test.pickle")
        # set variables first then save
        solution.update(["c", "d"])
        solution.save_data("test.pickle")
        data_load = pybamm.load("test.pickle")
        np.testing.assert_array_equal(solution.data["c"], data_load["c"])
        np.testing.assert_array_equal(solution.data["d"], data_load["d"])

        # to matlab
        solution.save_data("test.mat", to_format="matlab")
        data_load = loadmat("test.mat")
        np.testing.assert_array_equal(solution.data["c"],
                                      data_load["c"].flatten())
        np.testing.assert_array_equal(solution.data["d"], data_load["d"])

        # to csv
        with self.assertRaisesRegex(ValueError,
                                    "only 0D variables can be saved to csv"):
            solution.save_data("test.csv", to_format="csv")
        # only save "c" and "2c"
        solution.save_data("test.csv", ["c", "2c"], to_format="csv")
        # read csv
        df = pd.read_csv("test.csv")
        np.testing.assert_array_almost_equal(df["c"], solution.data["c"])
        np.testing.assert_array_almost_equal(df["2c"], solution.data["2c"])

        # raise error if format is unknown
        with self.assertRaisesRegex(ValueError,
                                    "format 'wrong_format' not recognised"):
            solution.save_data("test.csv", to_format="wrong_format")

        # test save whole solution
        solution.save("test.pickle")
        solution_load = pybamm.load("test.pickle")
        self.assertEqual(solution.model.name, solution_load.model.name)
        np.testing.assert_array_equal(solution["c"].entries,
                                      solution_load["c"].entries)
        np.testing.assert_array_equal(solution["d"].entries,
                                      solution_load["d"].entries)
コード例 #2
0
    def test_run_experiment(self):
        experiment = pybamm.Experiment([(
            "Discharge at C/20 for 1 hour",
            "Charge at 1 A until 4.1 V",
            "Hold at 4.1 V until C/2",
            "Discharge at 2 W for 1 hour",
        )])
        model = pybamm.lithium_ion.DFN()
        sim = pybamm.Simulation(model, experiment=experiment)
        sol = sim.solve()
        self.assertEqual(sol.termination, "final time")
        self.assertEqual(len(sol.cycles), 1)

        for i, step in enumerate(sol.cycles[0].steps[:-1]):
            len_rhs = sol.all_models[0].concatenated_rhs.size
            y_left = step.all_ys[-1][:len_rhs, -1]
            if isinstance(y_left, casadi.DM):
                y_left = y_left.full()
            y_right = sol.cycles[0].steps[i + 1].all_ys[0][:len_rhs, 0]
            if isinstance(y_right, casadi.DM):
                y_right = y_right.full()
            np.testing.assert_array_equal(y_left.flatten(), y_right.flatten())

        # Solve again starting from solution
        sol2 = sim.solve(starting_solution=sol)
        self.assertEqual(sol2.termination, "final time")
        self.assertGreater(sol2.t[-1], sol.t[-1])
        self.assertEqual(sol2.cycles[0], sol.cycles[0])
        self.assertEqual(len(sol2.cycles), 2)
        # Check starting solution is unchanged
        self.assertEqual(len(sol.cycles), 1)

        # save
        sol2.save("test_experiment.sav")
        sol3 = pybamm.load("test_experiment.sav")
        self.assertEqual(len(sol3.cycles), 2)
        os.remove("test_experiment.sav")
コード例 #3
0
ファイル: simulation.py プロジェクト: tobykirk/PyBaMM
def load_sim(filename):
    """Load a saved simulation"""
    return pybamm.load(filename)