Beispiel #1
0
def test_creation():
    """Test of waveform creation"""
    def check(w):
        for x, xref in zip(w.x, [array([1, 2])]):
            assert_array_equal(x, xref)
        assert_array_equal(w.y, array([3, 4]))

    ## Create from arrays
    check(Waveform([array([1, 2])], array([3, 4])))

    ## Create from single x-array
    check(Waveform(array([1, 2]), array([3, 4])))

    ## Create from lists
    check(Waveform([[1, 2]], [3, 4]))

    ## Create from tuples
    check(Waveform(((1, 2), ), (3, 4)))

    ## Check meta-data of testdata waveforms
    for x, xref in zip(testdata2.x, [np.array([1, 2]), np.array([1, 2, 3])]):
        assert_array_equal(x, xref)

    assert_array_equal(testdata2.y, array([[3, 4, 5], [5, 4, 2]]))

    assert_equal(testdata2.xlabels, ['v1', 'v2'])
    assert_equal(testdata2.xunits, ['V', 'V'])
    assert_equal(testdata2.ylabel, 'i3')
    assert_equal(testdata2.yunit, 'A')
    assert_equal(testdata2.ndim, 2)

    empty = Waveform()
    assert_array_equal(empty.y, array([]))
    assert_equal(empty.ndim, 1)
Beispiel #2
0
def test_unary_operations():
    x = testdata1[0]
    check_func(abs,
               lambda w: Waveform(w.x, abs(get_y(w))), (x, ),
               preserve_yunit=True)
    check_func(lambda w: -w,
               lambda w: Waveform(w.x, -get_y(w)), (x, ),
               preserve_yunit=True,
               ref_ylabel='-%s')
Beispiel #3
0
def test_xval():
    w = Waveform([[5,2],[2,3,4]], array([[3,9,7], [4,6,6]]))

    wref = Waveform([[5,2],[2,3,4]], array([[2,3,4], [2,3,4]]))
    assert_waveform_almost_equal(w.xval(), wref)
    assert_waveform_almost_equal(w.xval(-1), wref)
    assert_waveform_almost_equal(w.xval(1), wref)

    wref = Waveform([[5,2],[2,3,4]], array([[5,5,5], [2,2,2]]))
    assert_waveform_almost_equal(w.xval(0), wref)
Beispiel #4
0
def test_indexing():
    """Test taking slices of a waveform"""
    
    w = testdata2
    
    w_sliced = Waveform(([1,2,3],), array([3,4,5]),
                          xlabels = ('v2',),
                          xunits = ('V',),
                          ylabel = 'i3',
                          yunit = 'A')

    assert_waveform_almost_equal(w[0,:], w_sliced)
    assert_waveform_almost_equal(w[0], w_sliced)

    w_sliced2 = Waveform(([1,2],), array([3,5]),
                         xlabels = ('v1',),
                         xunits = ('V',),
                         ylabel = 'i3',
                         yunit = 'A')
    
    assert_waveform_almost_equal(w[:,0], w_sliced2)

    w_sliced3 = Waveform(([1], [1,2,3],), array([[3,4,5]]),
                          xlabels = ('v1', 'v2',),
                          xunits = ('V', 'V',),
                          ylabel = 'i3',
                          yunit = 'A')
    
    assert_waveform_almost_equal(w[0:1,:], w_sliced3)
    assert_equal(w[0,0], 3)

    w_sliced = Waveform(([1,2],), array([5,2]),
                          xlabels = ('v1',),
                          xunits = ('V',),
                          ylabel = 'i3',
                          yunit = 'A')

    assert_waveform_almost_equal(w[:,-1], w_sliced)

    w_sliced = Waveform([[1,2], [1,2,3]], array([[1,4,5],[4,4,3]]),
                        xlabels = ('v1', 'v2'),
                        xunits = ('V', 'V'),
                        ylabel = 'i3',
                        yunit = 'A')

    assert_waveform_almost_equal(testdata4[:,:, 0], w_sliced)
    assert_waveform_almost_equal(testdata4[...], testdata4)
    assert_waveform_almost_equal(testdata4[..., 0], w_sliced)
Beispiel #5
0
def check_alongaxes_func(func, reference_func, w, keep_yunit=False):
    for axis in range(w.ndim):
        res = func(w, axis=axis)

        ref_x = list(w.x)
        ref_xlabels = list(w.xlabels)
        ref_xunits = list(w.xlabels)
        del ref_x[axis]
        del ref_xlabels[axis]
        del ref_xunits[axis]

        if keep_yunit:
            yunit = w.yunit
        else:
            yunit = ''

        y = np.apply_along_axis(reference_func, axis, w.y)

        ref = Waveform(ref_x,
                       y,
                       xlabels=ref_xlabels,
                       xunits=ref_xunits,
                       ylabel=func.__name__ + '(' + w.ylabel + ')',
                       yunit=yunit)

        assert_waveform_almost_equal(res, ref)
Beispiel #6
0
 def func():
     return Waveform([[1, 2], [1, 2, 3]],
                     (testdata2.y.T + (testdata2.y.T)[0]).T,
                     xlabels=('v1', 'v1'),
                     xunits=('V', 'V'),
                     ylabel='i3',
                     yunit='A')
Beispiel #7
0
def check_binary_op(op, a, b, preserve_yunit=False):
    exec 'res = a' + op + 'b'

    ref_yunit = ''

    for arg in a, b:
        print str(a) + ' ' + op + ' ' + str(b)
        if iswave(arg):
            ref_x = arg.x
            ref_xlabels = arg.xlabels
            ref_xunits = arg.xunits
            if preserve_yunit:
                ref_yunit == arg.yunit
            break

    ref_ylabel = get_ylabel(a) + op + get_ylabel(b)

    exec 'ref_y = get_y(a) ' + op + ' get_y(b)'

    assert_waveform_almost_equal(
        res,
        Waveform(ref_x,
                 ref_y,
                 xlabels=ref_xlabels,
                 xunits=ref_xunits,
                 ylabel=ref_ylabel,
                 yunit=ref_yunit))
Beispiel #8
0
    def parse(self, s, xlabels, sweep_dimensions=1):
        lines = s.split('\n')

        header = lines[0][1:]
        names = re.split("\s+", header.rstrip())
        data = {}

        if xlabels == None:
            xlabels = names[:sweep_dimensions]
        else:
            xlabels = list(xlabels)

        logging.debug('Result header: ' + header)

        for name in names:
            data[name] = []

        for line in lines[1:]:
            vals = map(sp2float, line.split())

            for name, val in zip(names,vals):
                data[name].append(val)

        logging.debug('Result data items: ' + str(data))

        ## Store result in ResultDict
        xlist = [v for k, v in data.items() if k in xlabels]
 
        for k, v in data.items():
            if k not in xlabels:
                self[k] = Waveform(xlist, np.array(data[k]),
                                   xlabels = xlabels, ylabel=k)
Beispiel #9
0
def test_reversed_broadcasting():
    assert_waveform_equal(
        testdata2 + testdata2[:, 0],
        Waveform([[1, 2], [1, 2, 3]], (testdata2.y.T + (testdata2.y.T)[0]).T,
                 xlabels=('v1', 'v2'),
                 xunits=('V', 'V'),
                 ylabel='i3',
                 yunit='A'))
Beispiel #10
0
def test_xval():
    w = Waveform([[5, 2], [2, 3, 4]], array([[3, 9, 7], [4, 6, 6]]))

    wref = Waveform([[5, 2], [2, 3, 4]], array([[2, 3, 4], [2, 3, 4]]))
    assert_waveform_almost_equal(w.xval(), wref)
    assert_waveform_almost_equal(w.xval(-1), wref)
    assert_waveform_almost_equal(w.xval(1), wref)

    wref = Waveform([[5, 2], [2, 3, 4]], array([[5, 5, 5], [2, 2, 2]]))
    assert_waveform_almost_equal(w.xval(0), wref)
Beispiel #11
0
    def solve(self, freqs, complexfreq=False, refnode=gnd):
        toolkit = self.toolkit

        circuit_noloop = copy(self.cir)

        circuit_noloop[self.device] = LoopBreaker(circuit_noloop[self.device],
                                                  self.inp,
                                                  self.inn,
                                                  self.outp,
                                                  self.outn,
                                                  parent=circuit_noloop,
                                                  instance_name=self.device)

        x = self.toolkit.zeros(
            self.cir.n)  ## FIXME, this should come from the DC analysis

        epar = self.epar
        G = self.cir.G(x, epar)
        G_noloop = circuit_noloop.G(x, epar)

        C = self.cir.C(x, epar)
        C_noloop = circuit_noloop.C(x, epar)

        ## Refer the voltages to the reference node by removing
        ## the rows and columns that corresponds to this node
        irefnode = self.cir.get_node_index(refnode)
        G, G_noloop, C, C_noloop = remove_row_col((G, G_noloop, C, C_noloop),
                                                  irefnode, self.toolkit)
        if complexfreq:
            slist = freqs
        else:
            slist = 2j * self.toolkit.pi * freqs

        ## Calculate return difference
        def Ffunc(s):
            Y = toolkit.toMatrix(G + s * C)
            Y_noloop = toolkit.toMatrix(G_noloop + s * C_noloop)
            return toolkit.det(Y) / toolkit.det(Y_noloop)

        if isiterable(slist):
            F = Waveform(self.toolkit.array(slist),
                         self.toolkit.array([Ffunc(s) for s in slist]),
                         xlabels=('frequency', ),
                         xunits=('Hz', ),
                         ylabel='F')
        else:
            F = Ffunc(slist)

        ## Calculate loop-gain
        T = F - 1

        result = InternalResultDict()
        result['F'] = F
        result['T'] = T
        result['loopgain'] = -T

        return result
Beispiel #12
0
def test_simple_broadcasting():
    assert_waveform_equal(
        testdata2 + testdata2[0],
        Waveform([[1, 2], [1, 2, 3]],
                 testdata2.y + testdata2.y[0],
                 xlabels=('v1', 'v2'),
                 xunits=('V', 'V'),
                 ylabel='i3',
                 yunit='A'))
Beispiel #13
0
def test_getitem():
    w = testdata1[0]
    assert_equal(w[0], w.y[0])

    wsliced = Waveform(array([1]),
                       array([complex(-1, 0)]),
                       xlabels=('freq', ),
                       xunits=('Hz', ),
                       ylabel='amplitude',
                       yunit='V')
    assert_equal(w[0:1], wsliced)
Beispiel #14
0
    def test_transient(self):
        cir = gnucap.Circuit(simple_netlist)
        sim = gnucap.Simulation(cir, direct=self.direct)

        res = sim.run_transient(t=LinSweep(0, 1, n=6))

        refv2 = Waveform(np.array([0., 0.2, 0.4, 0.6, 0.8, 1.]),
                         np.array([0.75, 0.75, 0.75, 0.75, 0.75, 0.75]))

        assert_waveform_equal(res.v(2), refv2)

        assert_equal(res.v(2).xlabels, ['Time'])
Beispiel #15
0
def test_deriv():
    check_nonscalar_function(deriv)

    n = 100
    f = np.array([1.0, 0.5])
    t = np.linspace(0, 1.0, num=n)

    x = Waveform([f, t],
                 np.array([np.sin(2 * np.pi * freq * t) for freq in f]))
    xdot_ref = Waveform([f, t],
                        np.array([
                            2 * np.pi * freq * np.cos(2 * np.pi * freq * t)
                            for freq in f
                        ]))

    xdot = deriv(x)

    error = abs(xdot - xdot_ref[:, :-1])

    maxerror = Waveform(f, 2 * np.pi * f * 5e-2)

    assert ymax(error) < maxerror
Beispiel #16
0
def test_value():
    w1 = Waveform(array([1,2,3]),array([3,5,6]))
    assert_almost_equal(w1.value(1.5), 4.0)

    ## 2-d waveform
    w2 = Waveform([[1,2],[2,3,4]], array([[3,5,6], [4,6,7]]))
    assert_waveform_almost_equal(w2.value(2.5), Waveform([[1, 2]], array([ 4.,  5.])))
    assert_waveform_almost_equal(w2.value(1.5, axis=0), 
                          Waveform([[2, 3, 4]], array([ 3.5, 5.5, 6.5])))
    ## x is a waveform
    w2 = Waveform([[1,2],[2,3,4]], array([[3,5,6], [4,6,7]]))
    assert_waveform_almost_equal(w2.value(Waveform(array([1, 2]), array([2.5, 3.5]))), 
                          Waveform(array([1, 2]),array([ 4.,   6.5])))
Beispiel #17
0
def test_clip():
    ## 1D waveforms
    w1 = Waveform([[1.,2.,3.]], array([8., 6., 1.]))

    assert_waveform_almost_equal(w1.clip(2,3), 
                          Waveform([[ 2.,  3.]], array([ 6.,  1.])))
    assert_waveform_almost_equal(w1.clip(1.5, 3),
                          Waveform([[ 1.5, 2.,  3.]],array([ 7., 6.,  1.])))
    assert_waveform_almost_equal(w1.clip(1., 2.5),
                          Waveform([[ 1., 2.,  2.5]],array([ 8., 6.,  3.5])))
    
    ## 2D waveforms
    w = Waveform([[2,5],[2,3,4,5]], array([[3,9,7,6], [4,6,6,3]]),
                 xlabels = ('x1','x2'), xunits = ('V', 'V'),
                 ylabel = 'i', yunit = 'A')

    assert_waveform_almost_equal(w.clip(3, 4), 
                          Waveform([[2,5],[3,4]], 
                                   array([[9,7], [6,6]]),
                                   xlabels = ('x1','x2'), xunits = ('V', 'V'),
                                   ylabel = 'i', yunit = 'A'))

    w = Waveform([[2.,5.],[2.,3.,4.]], array([[3.,9.,7.], [4.,6.,6.]]),
                 xlabels = ('x1','x2'), xunits = ('V', 'V'),
                 ylabel = 'i', yunit = 'A')

    ## Clip at non-existing x-value from left and right
    assert_waveform_almost_equal(w.clip(2.5, 3.5), 
                          Waveform([[2,5],[2.5,3,3.5]], 
                                   array([[6,9,8], [5,6,6]]),
                                   xlabels = ('x1','x2'), xunits = ('V', 'V'),
                                   ylabel = 'i', yunit = 'A'))

    ## Clip at non-existing x-value from left and right
    assert_waveform_almost_equal(w.clip(2.5, 3.5, axis=0), 
                          Waveform([[2.5,3.5],[2.,3.,4.]], 
                                   array([[3.166667, 8.5, 6.833333], 
                                          [3.5, 7.5, 6.5]]),
                                   xlabels = ('x1','x2'), xunits = ('V', 'V'),
                                   ylabel = 'i', yunit = 'A'))
Beispiel #18
0
def test_shooting():
    circuit.default_toolkit = circuit.numeric

    cir = SubCircuit()

    N = 10
    period = 1e-3

    cir['vs'] = VSin(1, gnd, vac=2.0, va=2.0, freq=1 / period, phase=20)
    cir['R'] = R(1, 2, r=1e4)
    cir['C'] = C(2, gnd, c=1e-8)

    ac = PSS(cir)
    resac = AC(cir).solve(1 / period)

    pss = PSS(cir)

    res = pss.solve(period=period, timestep=period / N)

    v2ac = resac.v(2, gnd)
    v2pss = res['tpss'].v(2, gnd)

    t, dt = numeric.linspace(0, period, num=N, endpoint=True, retstep=True)

    v2ref = numeric.imag(v2ac * numeric.exp(2j * numeric.pi * 1 / period * t))

    w2ref = Waveform(t,
                     v2ref,
                     ylabel='reference',
                     yunit='V',
                     xunits=('s', ),
                     xlabels=('vref(2,gnd!)', ))

    ## Check amplitude error
    v2rms_ac = np.abs(v2ac) / np.sqrt(2)
    v2rms_pss = np.abs(res['fpss'].v(2, gnd)).value(1 / period)
    assert_almost_equal(v2rms_ac, v2rms_pss)

    ## Check error of waveform
    rmserror = np.sqrt(average((v2pss - w2ref)**2))
    assert rmserror < 1e-3, 'rmserror=%f too high' % rmserror
Beispiel #19
0
# -*- coding: latin-1 -*-
# Copyright (c) 2008 Pycircuit Development Team
# See LICENSE for details.

from nose.tools import *
from numpy.testing import assert_array_almost_equal, assert_array_equal

import numpy as np
from pycircuit.post import Waveform
from pycircuit.post.functions import *
from pycircuit.post.testing import *
import unittest

testdata1 = (Waveform(array([1, 10, 100]),
                      array([complex(-1, 0), complex(0, 1), 2]),
                      xlabels=('freq', ),
                      xunits=('Hz', ),
                      ylabel='amplitude',
                      yunit='V'), complex(0, 1), -1.5, 1.5)
testdata1_0_table = """====== ===========
freq   amplitude  
Hz     V          
====== ===========
     1     (-1+0j)
    10          1j
   100      (2+0j)
====== ==========="""

testdata2 = Waveform([[1, 2], [1, 2, 3]],
                     array([[3, 4, 5], [5, 4, 2]]),
                     xlabels=('v1', 'v2'),
                     xunits=('V', 'V'),
Beispiel #20
0
def test_xmin(): 
    w1 = Waveform(array([1,2,3]),array([3,5,6]))
    assert_equal(w1.xmin(), 1)

    w2 = Waveform([[5,2],[2,3,4]], array([[3,5,6], [4,6,7]]))
    assert_equal(w2.xmin(), 2)
Beispiel #21
0
import os

from nose.tools import *

from pycircuit.post.cds import PSFResultSet
from pycircuit.post import Waveform
from pycircuit.post.testing import *

psfresultset_testvectors = {
    'dcop.raw':
        ((('dcOp-dc', 'vout'), 2.5), 
         (('dcOp-dc', 'vin') , 5.0),
         (('designParamVals-info', 'top-level', 'k'), 0.5)),
    'dcsweep.raw':
        ((('dc1-dc', 'out'), 
          Waveform(([  1.        ,   3.66666667,   6.33333333,  9.        ],), 
                    [  2.        ,   4.66666667,   7.33333333,  10.        ])),),
}

withlibpsf = 'withlibpsf'
withoutlibpsf = 'withoutlibpsf'

def test_psfresultset():
    for uselibpsf in False, True:
        for rawdir in psfresultset_testvectors.keys():
            if uselibpsf:
                yield psfresultset, withlibpsf, rawdir
            else:
                yield psfresultset, withoutlibpsf, rawdir

def psfresultset(libpsf, rawdir):
    if libpsf == withlibpsf:
Beispiel #22
0
def test_clip():
    ## 1D waveforms
    w1 = Waveform([[1., 2., 3.]], array([8., 6., 1.]))

    assert_waveform_almost_equal(w1.clip(2, 3),
                                 Waveform([[2., 3.]], array([6., 1.])))
    assert_waveform_almost_equal(
        w1.clip(1.5, 3), Waveform([[1.5, 2., 3.]], array([7., 6., 1.])))
    assert_waveform_almost_equal(
        w1.clip(1., 2.5), Waveform([[1., 2., 2.5]], array([8., 6., 3.5])))

    ## 2D waveforms
    w = Waveform([[2, 5], [2, 3, 4, 5]],
                 array([[3, 9, 7, 6], [4, 6, 6, 3]]),
                 xlabels=('x1', 'x2'),
                 xunits=('V', 'V'),
                 ylabel='i',
                 yunit='A')

    assert_waveform_almost_equal(
        w.clip(3, 4),
        Waveform([[2, 5], [3, 4]],
                 array([[9, 7], [6, 6]]),
                 xlabels=('x1', 'x2'),
                 xunits=('V', 'V'),
                 ylabel='i',
                 yunit='A'))

    w = Waveform([[2., 5.], [2., 3., 4.]],
                 array([[3., 9., 7.], [4., 6., 6.]]),
                 xlabels=('x1', 'x2'),
                 xunits=('V', 'V'),
                 ylabel='i',
                 yunit='A')

    ## Clip at non-existing x-value from left and right
    assert_waveform_almost_equal(
        w.clip(2.5, 3.5),
        Waveform([[2, 5], [2.5, 3, 3.5]],
                 array([[6, 9, 8], [5, 6, 6]]),
                 xlabels=('x1', 'x2'),
                 xunits=('V', 'V'),
                 ylabel='i',
                 yunit='A'))

    ## Clip at non-existing x-value from left and right
    assert_waveform_almost_equal(
        w.clip(2.5, 3.5, axis=0),
        Waveform([[2.5, 3.5], [2., 3., 4.]],
                 array([[3.166667, 8.5, 6.833333], [3.5, 7.5, 6.5]]),
                 xlabels=('x1', 'x2'),
                 xunits=('V', 'V'),
                 ylabel='i',
                 yunit='A'))
Beispiel #23
0
# -*- coding: latin-1 -*-
# Copyright (c) 2008 Pycircuit Development Team
# See LICENSE for details.

from nose.tools import *
from numpy.testing import assert_array_almost_equal, assert_array_equal

import numpy as np
from pycircuit.post import Waveform
from pycircuit.post.functions import *
from pycircuit.post.testing import *

testdata1 = (
    Waveform(array([1,10,100]),array([complex(-1,0),complex(0,1),2]), 
             xlabels = ('freq',),
             xunits = ('Hz', ),
             ylabel = 'amplitude',
             yunit = 'V'
             ),    complex(0,1),
    -1.5,
    1.5)
testdata1_0_table = """====== ===========
freq   amplitude  
Hz     V          
====== ===========
     1     (-1+0j)
    10          1j
   100      (2+0j)
====== ==========="""

testdata2 = Waveform([[1,2], [1,2,3]], array([[3,4,5],[5,4,2]]),
                     xlabels = ('v1', 'v2'),
Beispiel #24
0
def test_value():
    w1 = Waveform(array([1, 2, 3]), array([3, 5, 6]))
    assert_almost_equal(w1.value(1.5), 4.0)

    ## 2-d waveform
    w2 = Waveform([[1, 2], [2, 3, 4]], array([[3, 5, 6], [4, 6, 7]]))
    assert_waveform_almost_equal(w2.value(2.5),
                                 Waveform([[1, 2]], array([4., 5.])))
    assert_waveform_almost_equal(w2.value(1.5, axis=0),
                                 Waveform([[2, 3, 4]], array([3.5, 5.5, 6.5])))
    ## x is a waveform
    w2 = Waveform([[1, 2], [2, 3, 4]], array([[3, 5, 6], [4, 6, 7]]))
    assert_waveform_almost_equal(
        w2.value(Waveform(array([1, 2]), array([2.5, 3.5]))),
        Waveform(array([1, 2]), array([4., 6.5])))
Beispiel #25
0
def test_ymax():
    w = Waveform([[2, 5], [2, 3, 4]], array([[3, 9, 7], [4, 6, 6]]))
    check_alongaxes_func(Waveform.ymax, np.max, w)
Beispiel #26
0
def test_ymin():
    w = Waveform([[5, 2], [2, 3, 4]], array([[3, 9, 7], [4, 6, 6]]))
    check_alongaxes_func(Waveform.ymin, np.min, w)
Beispiel #27
0
def test_xmin():
    w1 = Waveform(array([1, 2, 3]), array([3, 5, 6]))
    assert_equal(w1.xmin(), 1)

    w2 = Waveform([[5, 2], [2, 3, 4]], array([[3, 5, 6], [4, 6, 7]]))
    assert_equal(w2.xmin(), 2)