Skip to content

Instantly share code, notes, and snippets.

@gehrer
Created December 25, 2016 11:45
Show Gist options
  • Save gehrer/12c8228b245b21eeaa0a0071127c5e1e to your computer and use it in GitHub Desktop.
Save gehrer/12c8228b245b21eeaa0a0071127c5e1e to your computer and use it in GitHub Desktop.
import ahkab
class NLResistor(ahkab.devices.Component):
def __init__(self, part_id, n1, n2, value):
self.part_id = part_id
self.value = value
self.is_nonlinear = True
self.is_symbolic = False
self.n1 = n1
self.n2 = n2
def get_ports(self):
return ((n1,n2))
def i(self, v, time=0):
return (v/self.value)**0.5
def g(self, v, time=0):
return 1/(2*(self.value*v)**0.5)
def get_op_info(self, ports_v):
vn1n2 = float(ports_v[0][0])
in1n2 = float((ports_v[0][0]/self.value)**0.5)
power = float(ports_v[0][0]**1.5/self.value**0.5)
op_keys = ['Part ID', u"R [\u2126]", "V(n1,n2) [V]", "I(n1->n2) [A]", "P [W]"]
op_info = [self.part_id.upper(), self.value, vn1n2, in1n2, power]
return op_keys, op_info
cir = ahkab.Circuit('test')
n1=cir.add_node('n1')
n2=cir.add_node('n2')
gnd=cir.add_node(cir.gnd)
elem1 = ahkab.devices.Resistor(part_id='R1',n1=n1,n2=n2,value=100)
cir.append(elem1)
elem2 = NLResistor(part_id='Z2',n1=n2,n2=gnd,value=100)
cir.append(elem2)
cir.add_vsource('Vref','n1',cir.gnd,dc_value=10)
op = ahkab.new_op()
res = ahkab.run(cir,op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment