-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmultiprocessing_bench.py
More file actions
88 lines (78 loc) · 2.48 KB
/
multiprocessing_bench.py
File metadata and controls
88 lines (78 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cProfile
import os
import time
import numpy as np
import pybamm
import pybop
current_dir = os.path.dirname(os.path.abspath(__file__))
dataset_path = os.path.join(
current_dir, "examples/data/synthetic/dfn_charge_discharge_75.csv"
)
# Import the synthetic dataset
csv_data = np.loadtxt(dataset_path, delimiter=",", skiprows=1)
downsample = 1
dataset = pybop.Dataset(
{
"Time [s]": csv_data[::downsample, 0],
"Current function [A]": csv_data[::downsample, 1],
"Voltage [V]": csv_data[::downsample, 2],
"Bulk open-circuit voltage [V]": csv_data[::downsample, 3],
}
)
# Define model and parameter values
model = pybamm.lithium_ion.DFN()
parameter_values = pybamm.ParameterValues("Chen2020")
parameter_values.set_initial_state(f"{csv_data[0, 2]} V")
# Fitting parameters
parameter_values.update(
{
"Negative electrode active material volume fraction": pybop.Parameter(
pybop.Gaussian(0.68, 0.05, truncated_at=[0.4, 0.9]),
initial_value=0.65,
),
"Positive electrode active material volume fraction": pybop.ParameterDistrbution(
pybop.Gaussian(0.58, 0.05, truncated_at=[0.4, 0.9]),
initial_value=0.65,
),
}
)
# Build the problem
simulator = pybop.pybamm.Simulator(model, parameter_values, protocol=dataset)
target = ["Voltage [V]", "Bulk open-circuit voltage [V]"]
cost = pybop.RootMeanSquaredError(dataset, target=target)
problem = pybop.Problem(simulator, cost)
logger = pybop.Logger(minimising=True)
pop_eval = pybop.PopulationEvaluator(
problem, minimise=True, with_sensitivities=False, logger=logger
)
ser_eval = pybop.ScalarEvaluator(
problem, minimise=True, with_sensitivities=False, logger=logger
)
N = 100
list_inputs = [
problem.parameters.to_dict([(0.9 - 0.4) * i / N + 0.4, (0.9 - 0.4) * i / N + 0.4])
for i in range(N)
]
t0 = time.perf_counter()
print(f"Running multiprocessing with {N} inputs...")
# Evaluate serially
t0 = time.perf_counter()
cProfile.runctx(
"sols = [ser_eval._evaluate(inputs) for inputs in list_inputs]",
globals(),
locals(),
filename="serial_profile.prof",
)
t_serial = time.perf_counter() - t0
# Evaluate in parallel
t0 = time.perf_counter()
cProfile.runctx(
"sols_mp = pop_eval._evaluate(list_inputs)",
globals(),
locals(),
filename="multiprocessing_profile.prof",
)
t_parallel = time.perf_counter() - t0
print(
f"Serial time: {t_serial} s, Parallel time: {t_parallel} s, Speedup: {t_serial / t_parallel}"
)