Replies: 2 comments 8 replies
-
Here is an example using SPM and the Chen 2020 parameter set (DFN works too). Note:
import pbeis
import pybamm
import numpy as np
import matplotlib.pyplot as plt
import time as timer
from scipy.fft import fft
# Set up
model = pybamm.lithium_ion.SPM(options={"surface form": "differential"}, name="SPM")
parameter_values = pybamm.ParameterValues("Chen2020")
frequencies = np.logspace(-4, 2, 30)
# Time domain
I = 50 * 1e-3
number_of_periods = 20
samples_per_period = 16
def current_function(t):
return I * pybamm.sin(2 * np.pi * pybamm.InputParameter("Frequency [Hz]") * t)
parameter_values["Current function [A]"] = current_function
start_time = timer.time()
sim = pybamm.Simulation(
model, parameter_values=parameter_values, solver=pybamm.ScipySolver()
)
impedances_time = []
for frequency in frequencies:
# Solve
period = 1 / frequency
dt = period / samples_per_period
t_eval = np.array(range(0, 1 + samples_per_period * number_of_periods)) * dt
sol = sim.solve(t_eval, inputs={"Frequency [Hz]": frequency})
# Extract final two periods of the solution
time = sol["Time [s]"].entries[-3 * samples_per_period - 1 :]
current = sol["Current [A]"].entries[-3 * samples_per_period - 1 :]
voltage = sol["Terminal voltage [V]"].entries[-3 * samples_per_period - 1 :]
# FFT
current_fft = fft(current)
voltage_fft = fft(voltage)
# Get index of first harmonic
idx = np.argmax(np.abs(current_fft))
impedance = -voltage_fft[idx] / current_fft[idx]
impedances_time.append(impedance)
end_time = timer.time()
time_elapsed = end_time - start_time
print("Time domain method: ", time_elapsed, "s")
# Frequency domain
methods = ["direct"]
impedances_freqs = []
for method in methods:
start_time = timer.time()
eis_sim = pbeis.EISSimulation(model, parameter_values=parameter_values)
impedances_freq = eis_sim.solve(frequencies, method)
end_time = timer.time()
time_elapsed = end_time - start_time
print(f"Frequency domain ({method}): ", time_elapsed, "s")
impedances_freqs.append(impedances_freq)
# Compare
_, ax = plt.subplots()
ax = pbeis.nyquist_plot(impedances_time, ax=ax, label="Time", alpha=0.7)
for i, method in enumerate(methods):
ax = pbeis.nyquist_plot(
impedances_freqs[i], ax=ax, label=f"Frequency ({method})", alpha=0.7
)
ax.legend()
plt.suptitle(f"{model.name}")
plt.show() |
Beta Was this translation helpful? Give feedback.
8 replies
-
Hi @rtimms if I need to validate this results what would be possibility to check the results from PyBaMM is accurate enough? Could you please guide me. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi All,
I am trying to obtain the EIS for a given AC input current signal as indicated in the attached file. Can anyone suggested how to extract the terminal voltage and current entries from this in order to obtain the EIS.
I tried to obtain the EIS via #pbeis but it seems the results are wrong.
Please provide your insights in this regard.
impoting libraries
import pybamm
import numpy as np
from functools import partial
import matplotlib.pyplot as plt
import pbeis
Define the DFN model
model = pybamm.lithium_ion.DFN()
params = pybamm.ParameterValues("Chen2020")
def current(t, amp=100e-3, freq=1):
return amp * pybamm.sin(2 * np.pi * freq * t)
set up frequency params
freq = 2e3
samples_per_period = 128
num_periods = 3
set up t_eval
dt = 1 / freq / samples_per_period
t_eval = np.array(range(0, samples_per_period*num_periods)) * dt
Update current function with 2 kHz sinusoid
params['Current function [A]'] = partial(current, amp=100e-3, freq=freq)
Set up solvers
solver_fast = pybamm.CasadiSolver(mode='fast')
Get different solutions
sol_fast = pybamm.Simulation(model=model, parameter_values=params).
solve(t_eval=t_eval, solver=solver_fast)
sim_step = pybamm.Simulation(model=model, parameter_values=params, solver=solver_fast)
t, t_max, dt = 0, t_eval[-1], t_eval[1]
while t < t_max:
sol_step = sim_step.step(dt)
t += dt
fig, axs = plt.subplots(2, 1, figsize=(10, 10/1.6), sharex=True,
constrained_layout=True)
for y_key, ax in zip(['Current [A]', 'Terminal voltage [V]'], axs):
for sol, label in zip([sol_fast],
['fast']):
ax.plot(sol['Time [s]'].data, sol[y_key].data, lw=2, label=label)
ax.legend(bbox_to_anchor=(1, 0.5), loc='center left', fontsize=16)
ax.set_ylabel(y_key, fontsize=18)
ax.tick_params(labelsize=16)
ax.grid('on')
axs[1].set_xlabel('Time [s]', fontsize=18)
fig.suptitle('Solver diffs', fontsize=20)
fig.show()
EIS plot
eis_sim = pbeis.EISSimulation(model=model, parameter_values=params)
eis_sim.solve(pbeis.logspace(-4,4,100)) # calculate impedance at log-spaced frequencies
eis_sim.nyquist_plot()
Beta Was this translation helpful? Give feedback.
All reactions