Diode response

ground 0 

load "diode.osdi"
load "resistor.osdi"

model vsource vsource
model resistor resistor
model d diode is=1e-12 n=2 rs=0.1 cjo=100p vj=1 m=0.5 

v1 (1 0) vsource dc=0
r1 (1 2) resistor r=1
d1 (2 0) d

control
  abort always
  
  // Saves that are applied to all analyses from this point on
  save default p(d1,gd) p(d1,cd)

  sweep is model="d" parameter="is" values=[1e-12, 1e-10, 1e-8, 1e-6] continuation=1
  sweep v1 instance="v1" parameter="dc" from=-50 to=10 mode="lin" points=200 continuation=1
    analysis op1 op

  postprocess(PYTHON, "runme.py")
endc

embed "runme.py" <<<FILE
from rawfile import rawread
from runtest import *
import numpy as np
import sys

raw = rawread('op1.raw')
flat = raw.get()
plot = raw.get(sweeps=1)

tests=[]

status = (np.abs(flat['is'].min()/1e-12-1)<1e-9).all()
tests.append(status)
print("minimum is test", status)

status = (np.abs(flat['is'].max()/1e-6-1)<1e-9).all()
tests.append(status)
print("maximum is test", status)

status = (np.abs(flat['v1'].min()/-50-1)<1e-9).all()
tests.append(status)
print("minimum v1 test", status)

status = (np.abs(flat['v1'].max()/10-1)<1e-9).all()
tests.append(status)
print("maximum v1 test", status)

status = flat['v1'].shape[0]==201*4
tests.append(status)
print("number of points test", status)

if isTest():
    sys.exit(not all(tests))
else:
    import matplotlib.pyplot as plt

    fig1, ax1 = plt.subplots(2, 1)
    fig1.suptitle('Diode characteristic')
    fig1.axes[0].set_title('DC response')
    fig1.axes[0].set_ylabel('Id [A]')
    fig1.axes[1].set_ylabel('gd [A/V]')
    fig1.axes[1].set_xlabel('Ud [V]')

    fig2, ax2 = plt.subplots(1, 1)
    fig2.suptitle('Diode characteristic')
    fig2.axes[0].set_title('Differential capacitance')
    fig2.axes[0].set_ylabel('Cd [F]')
    fig2.axes[0].set_xlabel('Ud [V]')

    for ii in range(plot.sweepGroups):
        sdata = plot.sweepData(ii)
        traceName = 'is=%.2e' % sdata['is']
        fig1.axes[0].plot(plot[ii, '2'], -plot[ii, 'v1:flow(br)'], label=traceName)
        fig1.axes[1].semilogy(plot[ii, '2'], plot[ii, 'd1.gd'], label=traceName)
        ax2.semilogy(plot[ii, '2'], plot[ii, 'd1.cd'], label=traceName)

    fig1.axes[0].legend(loc='upper left')
    fig1.axes[0].set_xlim(-1,2)
    fig1.axes[1].set_xlim(-1,2)
    fig1.axes[1].set_ylim(1e-12,1e3)
    fig1.axes[1].legend(loc='lower right')
    ax2.legend(loc='upper left')

    plt.show()
>>>FILE
