ECSHackWeek / impedance.py

A Python package for working with electrochemical impedance data
MIT License
217 stars 103 forks source link

Allow simulation of the initial circuit model without having to fit it to experimental data #202

Closed banjaxedben closed 2 years ago

banjaxedben commented 2 years ago

I have just found this project and thought it might be very useful in a slightly different field: I test piezo-electric transducers and find the equivalent circuit by fitting to various models like "Butterworth-VanDyke": p(C0,R1-C1-L1).

Please correct me if I'm wrong, but as far as I can see, it is NOT possible to plot the Bode or Nyquist representation of a circuit that has not yet been fitted to some data?

Circuit string: p(C0,R1-C1-L1)
Fit: False

Initial guesses:
     C0 = 1.30e-09 [F]
     R1 = 6.35e+01 [Ohm]
     C1 = 1.86e-10 [F]
     L1 = 9.00e-02 [H]

Traceback (most recent call last):
  File "C:\Users\BenKinsella\GitLab\impedance\test.py", line 21, in <module>
    plot_nyquist(ax, circuit)
  File "C:\Users\BenKinsella\GitLab\impedance\env3.9\lib\site-packages\impedance\visualization.py", line 33, in plot_nyquist
    ax.plot(np.real(Z), -np.imag(Z), fmt, **kwargs)
  File "C:\Users\BenKinsella\GitLab\impedance\env3.9\lib\site-packages\matplotlib\axes\_axes.py", line 1634, in plot
    self.add_line(line)
  File "C:\Users\BenKinsella\GitLab\impedance\env3.9\lib\site-packages\matplotlib\axes\_base.py", line 2283, in add_line
    self._update_line_limits(line)
  File "C:\Users\BenKinsella\GitLab\impedance\env3.9\lib\site-packages\matplotlib\axes\_base.py", line 2306, in _update_line_limits
    path = line.get_path()
  File "C:\Users\BenKinsella\GitLab\impedance\env3.9\lib\site-packages\matplotlib\lines.py", line 999, in get_path
    self.recache()
  File "C:\Users\BenKinsella\GitLab\impedance\env3.9\lib\site-packages\matplotlib\lines.py", line 652, in recache
    x = _to_unmasked_float_array(xconv).ravel()
  File "C:\Users\BenKinsella\GitLab\impedance\env3.9\lib\site-packages\matplotlib\cbook\__init__.py", line 1298, in _to_unmasked_float_array
    return np.asarray(x, float)
TypeError: float() argument must be a string or a number, not 'CustomCircuit'

At the moment I have to use SPICE to simulate the circuit model (AC sweep). I feel like this should be possible in impedance.py?

banjaxedben commented 2 years ago

Sorry, my mistake. I somehow missed circuit.predict() in my code. It works fine, with a helpful warning

warnings.warn("Simulating circuit based on initial parameters")