def test(): """Test NL AC analysis (API)""" cir = ahkab.Circuit('CS amplifier') mys = ahkab.time_functions.sin(0, 1, 60) cir.add_vsource('vin', '1', '0', dc_value=3, ac_value=1) cir.add_vsource('vdd', '3', '0', dc_value=30) cir.add_resistor('Rd', '3', '2', 10e3) cir.add_capacitor('Cd', '3', '2', 40e-12) cir.add_resistor('Rs', '4', '0', 1e3) cir.add_capacitor('Cs', '4', '0', 4e-6) cir.add_model('ekv', 'ekv0', {'TYPE': 'n', 'VTO': .4, 'KP': 1e-2}) cir.add_mos('m1', '2', '1', '4', '0', w=100e-6, l=1e-6, model_label='ekv0') print(cir) opa = ahkab.new_op(outfile='acnl', verbose=6) aca = ahkab.new_ac(1, 100e6, 10e3, outfile='acnl', verbose=6) r = ahkab.run(cir, [opa, aca])['ac'] testbench = testing.APITest('acnl', cir, [opa, aca], skip_on_travis=False, er=1e-3, ea=1e-5) testbench.setUp() testbench.test() if not cli: testbench.tearDown()
def test_remove_elem_linear(): """Test circuit.remove_elem with linear elem""" c = ahkab.Circuit('test') c.add_resistor('R1', 'n1', 'n2', 1e3) c.add_resistor('R2', 'n2', c.gnd, 1e3) c.add_resistor('R3', 'n2', 'n3', 1e3) c.add_resistor('R4', 'n3', 'n4', 1e3) # save the internal node reference of n1 to check old_nodes_dict = copy.copy(c.nodes_dict) # remove R1, which should trigger the removal of n1 c.remove_elem('R1') # check assert 'n1' not in c.nodes_dict assert old_nodes_dict['n1'] not in c.nodes_dict old_nodes_dict.pop(old_nodes_dict['n1']) old_nodes_dict.pop('n1') # nothing else got removed assert old_nodes_dict == c.nodes_dict assert 'R1' not in [e.part_id.upper() for e in c] # remove R3, which should trigger the removal of no # nodes # this time we call the method with the element R3 = [e for e in c if e.part_id.upper() == 'R3'][0] c.remove_elem(R3) # nothing got removed, right? assert old_nodes_dict == c.nodes_dict assert R3 not in c assert 'R3' not in [e.part_id for e in c]
def test_check_circuit4(): """Test utilities.check_circuit() 4/4""" c = ahkab.Circuit('New') c.add_resistor('R1', 'n1', 'n2', 1e3) c.add_resistor('R1', 'n2', '0', 1e3) v, _ = ahkab.utilities.check_circuit(c) assert not v
def test_op_solution(): """Test results.op_solution""" ######### CIRCUIT ############## ttn = ahkab.Circuit('Twin-T Notch Stopband filter') ttn.add_vsource('V1', 'in', ttn.gnd, dc_value=1, ac_value=1) # first path ttn.add_capacitor('C1', 'in', 'n1', 2.2e-12) ttn.add_capacitor('C2', 'n1', 'out', 2.2e-12) ttn.add_resistor('R1', 'n1', ttn.gnd, 1e3) # second path ttn.add_resistor('R2', 'in', 'n2', 2e3) ttn.add_resistor('R3', 'n2', 'out', 2e3) ttn.add_capacitor('C3', 'n2', ttn.gnd, 2 * 2.2e-12) ttn.add_vcvs('E1', 'outb', ttn.gnd, 'out', ttn.gnd, 1.) # set up the OP and run opa = ahkab.new_op() r = ahkab.run(ttn, opa)['op'] ####### CHECKS ########### # str representation print(str(r)) r.keys() # 'sd' is not an existing key try: r['sd'] assert False except KeyError: pass # fallback on default assert r.get('sd', 1e3) == 1e3 assert r.get('VN1') == 0 # the important part is not the value np.allclose(r.asmatrix(), np.array([[1.00000000e+00], [0.00000000e+00], [1.00000000e+00], [1.00000000e+00], [1.00000000e+00], [1.01736171e-20], [0.00000000e+00]]), rtol=1e-3) r.print_short() set(list(zip(*r.items()))[0]) == set(r.keys()) set(list(zip(*r.items()))[1]) == set(r.values()) # iterator keys = set() values = set() for k, v in r: keys |= {k} values |= {float(v)} assert keys == set(r.keys()) assert values == set(r.values())
def test_get_nodes_number(): """Test get_nodes_number()""" cir = ahkab.Circuit('Circuit test') n1 = cir.create_node('n1') n2 = cir.create_node('n2') n3 = cir.create_node('n3') n4 = cir.create_node('n4') n5 = cir.create_node('n5') n1 = cir.add_node('n1') gnd = cir.create_node('0') assert cir.get_nodes_number() == 6 #assert cir.nodes_dict[n1] in cir.nodes_dict assert cir.get_nodes_number() == int(len(cir.nodes_dict) / 2)
def setUp(self): ttn = ahkab.Circuit('Twin-T Notch Stopband filter') ttn.add_vsource('V1', 'in', ttn.gnd, dc_value=1, ac_value=1) # first path ttn.add_capacitor('C1', 'in', 'n1', 2.2e-12) ttn.add_capacitor('C2', 'n1', 'out', 2.2e-12) ttn.add_resistor('R1', 'n1', ttn.gnd, 1e3) # second path ttn.add_resistor('R2', 'in', 'n2', 2e3) ttn.add_resistor('R3', 'n2', 'out', 2e3) ttn.add_capacitor('C3', 'n2', ttn.gnd, 2 * 2.2e-12) ttn.add_vcvs('E1', 'outb', ttn.gnd, 'out', ttn.gnd, 1.) aca = ahkab.new_ac(1e7, 1e10, 100, x0=None) self.r = ahkab.run(ttn, aca)['ac']
def setUp(self): ttn = ahkab.Circuit('Twin-T Notch Stopband filter') ttn.add_vsource('V1', 'in', ttn.gnd, dc_value=1) # first path ttn.add_capacitor('C1', 'in', 'n1', 2.2e-12) ttn.add_capacitor('C2', 'n1', 'out', 2.2e-12) ttn.add_resistor('R1', 'n1', ttn.gnd, 1e3) # second path ttn.add_resistor('R2', 'in', 'n2', 2e3) ttn.add_resistor('R3', 'n2', 'out', 2e3) ttn.add_capacitor('C3', 'n2', ttn.gnd, 2 * 2.2e-12) ttn.add_vcvs('E1', 'outb', ttn.gnd, 'out', ttn.gnd, 1.) sa = ahkab.new_symbolic(source=None) self.r = ahkab.run(ttn, sa)['symbolic'][0]
def setUp(self): ttn = ahkab.Circuit('Twin-T Notch Stopband filter') ttn.add_vsource('V1', 'in', ttn.gnd, dc_value=1, ac_value=1) # first path ttn.add_capacitor('C1', 'in', 'n1', 2.2e-12) ttn.add_capacitor('C2', 'n1', 'out', 2.2e-12) ttn.add_resistor('R1', 'n1', ttn.gnd, 1e3) # second path ttn.add_resistor('R2', 'in', 'n2', 2e3) ttn.add_resistor('R3', 'n2', 'out', 2e3) ttn.add_capacitor('C3', 'n2', ttn.gnd, 2 * 2.2e-12) ttn.add_vcvs('E1', 'outb', ttn.gnd, 'out', ttn.gnd, 1.) # setup analysis and simulate dca = ahkab.new_dc(-5, 5, 100, 'V1') self.r = ahkab.run(ttn, dca)['dc']
def _run_test(ref_run=False): MINNODES = 6 MAXNODES = 14 STEP = 1 times = [] x = list(range(max((2, MINNODES)), MAXNODES, STEP)) for circuit_nodes in x: # build the circuit mycir = ahkab.Circuit('R2R symbolic test with %d nodes' % circuit_nodes) n1 = '1' gnd = mycir.gnd mycir.add_vsource('VS', n1, gnd, dc_value=10e3) subs = {} for n in range(1, circuit_nodes): n1 = str(n) n2 = str(n + 1) mycir.add_resistor("R%dh1" % n, n1, n2, value=2.6e3) mycir.add_resistor("R%dh2" % n, n1, n2, value=2.6e3) mycir.add_resistor("R%dv" % n, n2, gnd, value=2.6e3) subs.update({"R%dh1" % n: 'R', "R%dh2" % n: 'R', "R%dv" % n: 'R'}) n1 = str(circuit_nodes) mycir.add_resistor("R%dve" % circuit_nodes, n1, gnd, value=2.6e3) subs.update({"R%dve" % circuit_nodes: 'R'}) # define analysis s = ahkab.new_symbolic(subs=subs) start = time.time() r = ahkab.run(mycir, s)['symbolic'][0] stop = time.time() times.append((stop - start)) print("Solving with %d nodes took %f s" % (circuit_nodes, times[-1])) # check the values too VS = r.as_symbol('VS') out_test = r['V' + str(circuit_nodes)] / VS out_th = 1. / (2**(circuit_nodes - 1)) assert .5 * abs(out_th - out_test) / (out_th + out_test) < 1e-3 x = numpy.array(x, dtype=numpy.int64) times = numpy.array(times, dtype=numpy.float64) if ref_run: numpy.savetxt(os.path.join(reference_path, 'r2r_symbolic_ref.csv'), numpy.concatenate((x.reshape( (-1, 1)), times.reshape((-1, 1))), axis=1), delimiter=',') save_boxid(os.path.join(reference_path, 'r2r_symbolic_ref.boxid')) return x, times
def test_find_vde(): """Test circuit.find_vde()""" c = ahkab.Circuit('test') c.add_resistor('R1', 'n1', 'n2', 1e3) c.add_inductor('L1', 'n2', c.gnd, 1e-9) c.add_vsource('V1', 'n3', c.gnd, 1.) c.add_inductor('L2', 'n3', c.gnd, 1e-9) c.add_resistor('R2', 'n2', 'n3', 1e3) c.add_capacitor('C1', 'n3', 'n4', 1e-13) nnodes = len(c.nodes_dict) / 2 # test find_vde for pi in ('L1', 'L2', 'v1'): e = c.find_vde(nnodes - 1 + c.find_vde_index(pi)) assert e.part_id.lower() == pi.lower()
def setUp(self): ######### CIRCUIT ############## ttn = ahkab.Circuit('Twin-T Notch Stopband filter') ttn.add_vsource('V1', 'in', ttn.gnd, dc_value=1, ac_value=1) # first path ttn.add_capacitor('C1', 'in', 'n1', 2.2e-12) ttn.add_capacitor('C2', 'n1', 'out', 2.2e-12) ttn.add_resistor('R1', 'n1', ttn.gnd, 1e3) # second path ttn.add_resistor('R2', 'in', 'n2', 2e3) ttn.add_resistor('R3', 'n2', 'out', 2e3) ttn.add_capacitor('C3', 'n2', ttn.gnd, 2*2.2e-12) ttn.add_vcvs('E1', 'outb', ttn.gnd, 'out', ttn.gnd, 1.) # set up the OP and run opa = ahkab.new_op() self.r = ahkab.run(ttn, opa)['op']
def take_decision_circuit(observations, parameters): observations = normalize_observations(observations) observations.append(1) # The bias paramter is multiplied by 1 (or -1) # Multiply observations that have indicies in the array negative_weights by -1 to compensate for the paramters by multiplied by -1 for i in range(len(observations)): if i in negative_weights: observations[i] *= -1 # Creating an opamp circuit that adds the input voltages; resistors are used as the paramters (actually 1/parameters) mycir = ahkab.Circuit('Simple Example Circuit') add_op_amp(mycir, "_opamp1") mycir.add_vsource("V1", "v1_r1", mycir.gnd, observations[0]) mycir.add_vsource("V2", "v2_r2", mycir.gnd, observations[1]) mycir.add_vsource("V3", "v3_r3", mycir.gnd, observations[2]) mycir.add_vsource("V4", "v4_r4", mycir.gnd, observations[3]) mycir.add_resistor("R1", "inverting_input_opamp1", "v1_r1", value=1 / parameters[0] * 1000) mycir.add_resistor("R2", "inverting_input_opamp1", "v2_r2", value=1 / parameters[1] * 1000) mycir.add_resistor("R3", "inverting_input_opamp1", "v3_r3", value=1 / parameters[2] * 1000) mycir.add_resistor("R4", "inverting_input_opamp1", "v4_r4", value=1 / parameters[3] * 1000) mycir.add_resistor("over", "inverting_input_opamp1", "output_opamp1", value=1000) opa = ahkab.new_op() r = ahkab.run(mycir, opa)['op'] # output is multiplied by -1 because the opamp inverts the output signal jump_prob = r["VOUTPUT_OPAMP1"][0][0] * -1 if jump_prob > 2.5: return 1 else: return 0
def test_find_vde_index(): """Test circuit.find_vde_index()""" c = ahkab.Circuit('test') c.add_resistor('R1', 'n1', 'n2', 1e3) c.add_inductor('L1', 'n2', c.gnd, 1e-9) c.add_vsource('V1', 'n3', c.gnd, 1.) c.add_inductor('L2', 'n3', c.gnd, 1e-9) c.add_resistor('R2', 'n2', 'n3', 1e3) c.add_capacitor('C1', 'n3', 'n4', 1e-13) # test c.find_vde_index with elements as input Ls = [e for e in c if e.part_id[0].lower() == 'l'] Ls.sort(key=lambda x: x.part_id) for L, i in zip(Ls, (0, 2)): assert c.find_vde_index(L) == i # test find_vde_index with IDs as input assert c.find_vde_index('l1') == 0 assert c.find_vde_index('V1') == 1 assert c.find_vde_index('L2') == 2
def setUp(self): ttn = ahkab.Circuit('Twin-T Notch Stopband filter') rect_wave = ahkab.time_functions.pulse(v1=-1, v2=+1, td=0, tr=10e-6, pw=90e-6, tf=10e-6, per=200e-6) ttn.add_vsource('V1', 'in', ttn.gnd, dc_value=1, ac_value=1, function=rect_wave) # first path ttn.add_capacitor('C1', 'in', 'n1', 2.2e-12) ttn.add_capacitor('C2', 'n1', 'out', 2.2e-12) ttn.add_resistor('R1', 'n1', ttn.gnd, 1e3) # second path ttn.add_resistor('R2', 'in', 'n2', 2e3) ttn.add_resistor('R3', 'n2', 'out', 2e3) ttn.add_capacitor('C3', 'n2', ttn.gnd, 2*2.2e-12) ttn.add_vcvs('E1', 'outb', ttn.gnd, 'out', ttn.gnd, 1.) # create a simulation object and run it! # using no step control, we know EXACTLY what the time # axis looks like. Easier for testing. tra = ahkab.new_tran(0, 50e-6, tstep=5e-6, x0=None, use_step_control=False) self.r = ahkab.run(ttn, tra)['tran']
def setUp(self): ttn = ahkab.Circuit('PSS Linear circuit') sin_wave = ahkab.time_functions.sin(vo=0, va=2, td=0, freq=1e6, theta=0, phi=0.) ttn.add_vsource('VIN', 'in', ttn.gnd, dc_value=5, function=sin_wave) ttn.add_resistor('R1', 'in', 'n1', 10e3) ttn.add_resistor('R2', 'n1', 'out', 20e3) ttn.add_resistor('R3', 'n2', ttn.gnd, 10e3) ttn.add_resistor('R4', 'n3', ttn.gnd, 20e3) ttn.add_resistor('R5', 'n3', 'out', 40e3) ttn.add_capacitor('C1', 'n1', 'n2', 31.83e-12) ttn.add_capacitor('C2', 'n1', 'n2', 15.91e-12) ttn.add_vcvs('E1', 'out', ttn.gnd, 'n2', 'n3', 1e6) # create a simulation object and run it! op = ahkab.new_op() pssa = ahkab.new_pss(1e-6, points=60) self.r = ahkab.run(ttn, [op, pssa])['pss']
def build_lpf(cascade: List[FT], fspecs: List, ro=False, cl=False) -> (ahkab.Circuit, Dict[str, float], List[str]): """ :param cascade: list of filter topologies in the cascade :param fspecs: list of filter specs :param ro: consider ro? :return: filter, dict of subs for symbolic expressions, list noise sources """ subs_dict = {} noise_srcs = [] filt = ahkab.Circuit('LPF') for i, t in enumerate(cascade): filt, s, n = attach_stage(filt, t, fspecs[i], len(cascade), i, ro, cl) subs_dict.update(s) noise_srcs = noise_srcs + n print(filt) print('Component substitutions:' + str(subs_dict)) print('Noise sources:' + str(noise_srcs)) return filt, subs_dict, noise_srcs
def test_pz_solution(): """Test results.pz_solution""" # Numeric test, not to run on PYPY if py3compat.PYPY: raise SkipTest bpf = ahkab.Circuit('RLC bandpass') bpf.add_inductor('L1', 'in', 'n1', 1e-6) bpf.add_capacitor('C1', 'n1', 'out', 2.2e-12) bpf.add_resistor('R1', 'out', bpf.gnd, 13) bpf.add_vsource('V1', 'in', bpf.gnd, dc_value=1, ac_value=1) pza = ahkab.new_pz('V1', ('out', bpf.gnd), x0=None, shift=1e3) r = ahkab.run(bpf, pza)['pz'] str(r) np.allclose(r['p0'] + r['p1'], -1034507 * 2, rtol=1e-3) np.allclose(abs(r['p0'] - r['p1']), 107297253 * 2, rtol=1e-3) np.allclose(r['z0'], 0, rtol=1.) assert set(r.keys()) == {u'p0', u'p1', u'z0'} assert r['p0'] == r.get('p0') for i in {u'p0', u'p1', u'z0'}: assert i in r assert r.has_key(i) set(list(zip(*r.items()))[0]) == {u'p0', u'p1', u'z0'} set(list(zip(*r.items()))[1]) == set(r.values()) i = 0 for k, v in r: i += 1 assert v in r.values() assert k in r.keys() assert i == 3
import ahkab import socket import sys # from ahkab import dc_guess #ahkab --help # # Create a TCP/IP socket # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # # Connect the socket to the port where the server is listening # server_address = ('localhost', 1234) # s.connect(server_address) mycir = ahkab.Circuit('Simple Circuit') mycir.add_resistor('R1', 'n1', mycir.gnd, value=5) mycir.add_vsource('V1', 'n2', 'n1', dc_value=8) mycir.add_resistor('R2', 'n2', mycir.gnd, value=2) mycir.add_vsource('V2', 'n3', 'n2', dc_value=4) mycir.add_resistor('R3', 'n3', mycir.gnd, value=4) mycir.add_resistor('R4', 'n001', 'n3', value=1) mycir.add_vsource('V3', 'n4', mycir.gnd, dc_value=10) mycir.add_resistor('R5', 'n2', 'n4', value=4) mycir.add_resistor('R6','n4','n001',value=10) opa = ahkab.new_op() # Assembles an OP analysis and returns the analysis object. r = ahkab.run(mycir, opa)['op'] #print(r) # print('---------------------------------------------')
import pdb import ahkab import numpy as np from matplotlib import * bpf = ahkab.Circuit('RLC bandpass') bpf.add_inductor('L1', 'in', 'n1', 1e-6) bpf.add_capacitor('C1', 'n1', 'out', 2.2e-12) bpf.add_resistor('R1', 'out', bpf.gnd, 13) # we also give V1 an AC value since we wish to run an AC simulation # in the following bpf.add_vsource('V1', 'in', bpf.gnd, dc_value=1, ac_value=1) print(bpf) print type(bpf) ahkab.new_pz(input_source=None, output_port=None, shift=0.0, MNA=None, outfile=None, x0=u'op', verbose=0) pza = ahkab.new_pz('V1', ('out', bpf.gnd), x0=None, shift=1e3) r = ahkab.run(bpf, pza)['pz'] print('Singularities:') for x, _ in r: print "* %s = %+g %+gj Hz" % (x, np.real(r[x]), np.imag(r[x])) pdb.set_trace()
def test_create_node1(): """Test create_node() rejecting doubles""" cir = ahkab.Circuit('Circuit test') n1 = cir.create_node('n1') cir.add_capacitor('C1', 'n1', 'n2', 1e-6) cir.create_node('n1')
def test_new_internal_node1(): """Check the internal nodes exceptions""" cir = ahkab.Circuit('Circuit test') n = cir.new_internal_node() assert cir.is_int_node_internal_only(n)
def test_create_node5(): """Check the return value of create_node()""" cir = ahkab.Circuit('Circuit test') assert 'n1' == cir.create_node('n1')
def test_add_node4(): """Check create_node() correctly adding nodes 2/2""" cir = ahkab.Circuit('Circuit test') n1 = cir.create_node('n1') assert cir.nodes_dict[n1] in cir.nodes_dict
def test_create_node3(): """Check create_node() correctly adding nodes 1/2""" cir = ahkab.Circuit('Circuit test') n1 = cir.create_node('n1') assert n1 in cir.nodes_dict
def test_add_node2(): """Check the return value of add_node()""" cir = ahkab.Circuit('Circuit test') cir.add_node('n1') assert cir.nodes_dict['n1'] == cir.add_node('n1')
# -*- coding: utf-8 -*- """ @author: Abdelrahman & Tarek & Abdullah & Ayman """ #----imprting modules----# LANG="en_US.UTF-8" import ahkab import cmath import math #-----------------------------------------------------------------------------------------------------------# ac_circuit = ahkab.Circuit("AC Circuit") #create an ahkab circuit File = open("Netlist.txt","r+") #open the net list freq = float(File.readline()) #setting the frecuency print("frequence: ", freq) Circuit = File.readlines() #read all the lines #---------------intializations-----------------# pi = math.pi i=0 element_type = {} node1={} node2={} value={} phase={} ns1={} ns2={} vsource={} resistors = {}
def test_create_node2(): """Test create_node() rejecting nodes of wrong types""" cir = ahkab.Circuit('Circuit test') n1 = cir.create_node(1)
def test_new_internal_node2(): """Check the internal nodes""" cir = ahkab.Circuit('Circuit test') n = cir.new_internal_node() assert cir.is_int_node_internal_only(cir.nodes_dict[n])
def test_add_node1(): """Test add_node() rejecting nodes of wrong types""" cir = ahkab.Circuit('Circuit test') n1 = cir.add_node(1)
def getCircuit(self): if self.circuit is None: self.circuit = ahkab.Circuit(self.name()) self.circuit.add_model('ekv', 'nmos', dict(TYPE='n', VTO=.4, KP=10e-6)) return self.circuit