Skip to content

Instantly share code, notes, and snippets.

@itdaniher
Created December 27, 2016 00:18
Show Gist options
  • Save itdaniher/b595075f30c9db440b0409c9b16f835d to your computer and use it in GitHub Desktop.
Save itdaniher/b595075f30c9db440b0409c9b16f835d 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, op_index, v, time=0):
return (v[0]/self.value)**0.5
def g(self, op_index, v, port_index, time=0):
return 1/(2*(self.value*v[0])**0.5)
def get_output_ports(self):
return ((self.n1, self.n2),)
def get_drive_ports(self, op):
return self.get_output_ports()
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