Effect of varying the smoothing factor
[1]:
import pymultieis as pym
import numpy as np
import torch
[2]:
# Load the file containing the frequencies
F_her = torch.as_tensor(np.load('../../../data/her_50/freq_50.npy'))
# Load the file containing the admittances (a set of 50 spectra)
Y_her = torch.as_tensor(np.load('../../../data/her_50/Y_50.npy'))
[3]:
print(F_her.shape)
print(Y_her.shape)
torch.Size([35])
torch.Size([35, 50])
[4]:
# Define model
def her(p, f):
w = 2*torch.pi*f
s = 1j * w
Rs = p[0]
Qh = p[1]
nh = p[2]
Rad = p[3]
Cad= p[4]
nad= p[5]
Rct= p[6]
Wct=p[7]
Zw = Wct/torch.sqrt(w) * (1-1j)
Zct = Rad + Zw
Yca=(((s**nad)*Cad) + Rct**-1)
Z1=(Yca**-1 +Zct)
Y1=(Z1**-1)
Ydl= ((s**nh)*Qh)
Z = (Rs + (Ydl + Y1)**-1)
Y = 1/Z
return torch.cat((Y.real, Y.imag), dim = 0)
[5]:
p0 = torch.as_tensor([4.72774187e+02, 9.70785283e-07, 6.51261304e-01,
1.04825669e+03, 7.27786796e-07, 8.33955442e-01,
5.98926963e+04, 2.03231984e+04], dtype=torch.float64)
bounds = [[1e-3, 1e6], [1e-9, 1e-1], [1e-1, 1], [1e-5, 1e8], [1e-9, 1e-1], [1e-1, 1], [1e-5, 1e8], [1e-5, 1e8]]
smf_modulus = torch.ones(len(p0)) # Smoothing factor used with the modulus
1. Fit simultaneous without a smoothing factor of zero
[6]:
eis_her = pym.Multieis(p0, F_her, Y_her, bounds, smf_modulus, her, weight= 'modulus', immittance='admittance')
popt, perr, chisqr, chitot, AIC = eis_her.fit_simultaneous_zero()
eis_her.plot_nyquist(10)
eis_her.plot_params()
eis_her.plot_params(show_errorbar=True)
Using initial
Iteration : 812, Loss : 6.41675e-04
Optimization complete
total time is 0:00:47.431561
2. Fit sequential (i.e. fit individual spectra using least squares)
[7]:
eis_her = pym.Multieis(p0, F_her, Y_her, bounds, smf_modulus, her, weight= 'modulus', immittance='admittance')
popt, perr, chisqr, chitot, AIC = eis_her.fit_sequential()
eis_her.plot_nyquist(10)
eis_her.plot_params()
eis_her.plot_params(show_errorbar=True)
Using initial
fitting spectra 0
fitting spectra 10
fitting spectra 20
fitting spectra 30
fitting spectra 40
Optimization complete
total time is 0:00:39.317994
3. Use a smoothing factor to obtain reasonable initial values before setting the smoothing factor to zero
[8]:
eis_her = pym.Multieis(p0, F_her, Y_her, bounds, smf_modulus, her, weight= 'modulus', immittance='admittance')
popt, perr, chisqr, chitot, AIC = eis_her.fit_simultaneous(method='l-bfgs-b')
popt, perr, chisqr, chitot, AIC = eis_her.fit_simultaneous_zero()
eis_her.plot_nyquist(10)
eis_her.plot_params()
eis_her.plot_params(show_errorbar=True)
Using initial
Iteration : 0, Loss : 5.45579e-02
Iteration : 1000, Loss : 1.81920e-04
Iteration : 2000, Loss : 3.16394e-05
Iteration : 3000, Loss : 6.45592e-06
Iteration : 3734, Loss : 4.88288e-06
Optimization complete
total time is 0:00:28.575768
Using prefit
Iteration : 356, Loss : 4.54891e-06
Optimization complete
total time is 0:00:09.680184
4. Use a smoothing factor to obtain reasonable initial values before fitting each spectra individually
[9]:
eis_her_simultaneous = pym.Multieis(p0, F_her, Y_her, bounds, smf_modulus, her, weight= 'modulus', immittance='admittance')
popt, perr, chisqr, chitot, AIC = eis_her.fit_simultaneous()
popt, perr, chisqr, chitot, AIC = eis_her.fit_sequential()
eis_her.plot_nyquist(10)
eis_her.plot_params()
eis_her.plot_params(show_errorbar=True)
Using prefit
Iteration : 0, Loss : 7.93998e-06
Iteration : 321, Loss : 4.87949e-06
Optimization complete
total time is 0:00:20.279947
Using prefit
fitting spectra 0
fitting spectra 10
fitting spectra 20
fitting spectra 30
fitting spectra 40
Optimization complete
total time is 0:00:11.003994
5. Repeating a single spectra (What to do when you have a single spectra to fit)
[10]:
Y_her_single_spectra = Y_her[:, 41]
Y_her_single_spectra.shape
# torch.Size([35])
[10]:
torch.Size([35])
[11]:
Y_her_repeated = torch.tile(Y_her_single_spectra[:,None], (1, 10))
Y_her_repeated.shape
# torch.Size([35, 10])
[11]:
torch.Size([35, 10])
[12]:
smf = torch.full((len(p0),), torch.inf)
eis_her = pym.Multieis(p0, F_her, Y_her_repeated, bounds, smf, her, weight= 'modulus', immittance='admittance')
# popt, perr, chisqr, chitot, AIC = eis_her.fit_simultaneous()
popt, perr, chisqr, chitot, AIC = eis_her.fit_stochastic()
popt, perr, chisqr, chitot, AIC = eis_her.fit_sequential()
eis_her.plot_nyquist(2)
eis_her.plot_params()
eis_her.plot_params(show_errorbar=True)
Using initial
0: loss=9.370e-02
10000: loss=4.670e-04
20000: loss=3.162e-06
30000: loss=3.162e-06
40000: loss=3.164e-06
50000: loss=3.188e-06
60000: loss=3.187e-06
70000: loss=3.162e-06
80000: loss=3.162e-06
90000: loss=3.162e-06
Optimization complete
total time is 0:03:36.581355
Using prefit
fitting spectra 0
Optimization complete
total time is 0:00:00.977091