示例#1
0
def _check_xyuv(*args, **kwargs):
    nargs = len(args)
    if nargs == 2:
        x, y = [None] * 2
        u, v = [np.asarray(_) for _ in args]
    elif nargs == 4:
        x, y, u, v = [np.asarray(_) for _ in args]
    else:
        raise TypeError('_check_xyuv: wrong number of arguments')

    indexing = kwargs.get('indexing', 'ij')

    us = u.shape
    assert us == v.shape, '_check_xyuv: u and v must be of same shape'

    if len(us) == 1:
        if x is None and y is None:
            x = list(range(us[0]))
            y = list(range(us[0]))
        else:
            assert x.shape == us, '_check_xyuv: x has shape %s, expected %s' % (x.shape, us)
            assert y.shape == us, '_check_xyuv: y has shape %s, expected %s' % (y.shape, us)
    elif len(us) == 2:
        nx, ny = us
        if x is None and y is None:
            if indexing == 'ij':
                x = seq(nx - 1)
                y = seq(ny - 1)
            else:
                x = seq(ny - 1)
                y = seq(nx - 1)
        else:
            if indexing == 'ij':
                assert (
                    x.shape == (nx, ny) or
                    x.shape == (nx, 1) or
                    x.shape == (nx,)
                ), '_check_xyuv: x has shape %s, expected %s, %s, or %s' % (x.shape, (nx, ny), (nx, 1), (nx,))
                assert (
                    y.shape == (nx, ny) or
                    y.shape == (1, ny) or
                    y.shape == (ny,)
                ), '_check_xyuv: y has shape %s, expected %s, %s, or %s' % (y.shape, (nx, ny), (1, ny), (ny,))
            else:
                assert (
                    x.shape == (nx, ny) or
                    x.shape == (1, ny) or
                    x.shape == (ny,)
                ), '_check_xyuv: x has shape %s, expected %s, %s, or %s' % (x.shape, (nx, ny), (1, ny), (ny,))
                assert (
                    y.shape == (nx, ny) or
                    y.shape == (nx, 1) or
                    y.shape == (nx,)
                ), '_check_xyuv: y has shape %s, expected %s, %s, or %s' % (y.shape, (nx, ny), (nx, 1), (nx,))
    else:
        raise ValueError('_check_xyuv: u must be 1D or 2D, not %dD' % len(us))

    return x, y, u, v
示例#2
0
文件: misc.py 项目: Nhyiraba/scitools
def _check_xyuv(*args, **kwargs):
    nargs = len(args)
    if nargs == 2:
        x, y = [None]*2
        u, v = [asarray(a) for a in args]
    elif nargs == 4:
        x, y, u, v = [asarray(a) for a in args]
    else:
        raise TypeError("_check_xyuv: wrong number of arguments")

    indexing = kwargs.get('indexing', 'ij')

    us = shape(u)
    assert us == shape(v), "_check_xyuv: u and v must be of same shape"

    if len(us) == 1:
        if x is None and y is None:
            x = list(range(us[0]))
            y = list(range(us[0]))
        else:
            assert shape(x) == us, \
                   "_check_xyuv: x has shape %s, expected %s" % (shape(x), us)
            assert shape(y) == us, \
                   "_check_xyuv: y has shape %s, expected %s" % (shape(y), us)
    elif len(us) == 2:
        nx, ny = us
        if x is None and y is None:
            if indexing == 'ij':
                x = seq(nx-1)
                y = seq(ny-1)
            else:
                x = seq(ny-1)
                y = seq(nx-1)
        else:
            if indexing == 'ij':
                assert shape(x)==(nx,ny) or shape(x)==(nx,1) or \
                       shape(x)==(nx,), \
                       "_check_xyuv: x has shape %s, expected %s, %s, " \
                       "or %s" % (shape(x), (nx,ny), (nx,1), (nx,))
                assert shape(y)==(nx,ny) or shape(y)==(1,ny) or \
                       shape(y)==(ny,), \
                       "_check_xyuv: y has shape %s, expected %s, %s, " \
                       "or %s" % (shape(y), (nx,ny), (1,ny), (ny,))
            else:
                assert shape(x)==(nx,ny) or shape(x)==(1,ny) or \
                       shape(x)==(ny,), \
                       "_check_xyuv: x has shape %s, expected %s, %s, " \
                       "or %s" % (shape(x), (nx,ny), (1,ny), (ny,))
                assert shape(y)==(nx,ny) or shape(y)==(nx,1) or \
                       shape(y)==(nx,), \
                       "_check_xyuv: y has shape %s, expected %s, %s, " \
                       "or %s" % (shape(y), (nx,ny), (nx,1), (nx,))
    else:
        raise ValueError("_check_xyuv: u must be 1D or 2D, not %dD" % len(us))

    return x, y, u, v
示例#3
0
def _check_xyuv(*args, **kwargs):
    nargs = len(args)
    if nargs == 2:
        x, y = [None] * 2
        u, v = [asarray(a) for a in args]
    elif nargs == 4:
        x, y, u, v = [asarray(a) for a in args]
    else:
        raise TypeError("_check_xyuv: wrong number of arguments")

    indexing = kwargs.get('indexing', 'ij')

    us = shape(u)
    assert us == shape(v), "_check_xyuv: u and v must be of same shape"

    if len(us) == 1:
        if x is None and y is None:
            x = list(range(us[0]))
            y = list(range(us[0]))
        else:
            assert shape(x) == us, \
                   "_check_xyuv: x has shape %s, expected %s" % (shape(x), us)
            assert shape(y) == us, \
                   "_check_xyuv: y has shape %s, expected %s" % (shape(y), us)
    elif len(us) == 2:
        nx, ny = us
        if x is None and y is None:
            if indexing == 'ij':
                x = seq(nx - 1)
                y = seq(ny - 1)
            else:
                x = seq(ny - 1)
                y = seq(nx - 1)
        else:
            if indexing == 'ij':
                assert shape(x)==(nx,ny) or shape(x)==(nx,1) or \
                       shape(x)==(nx,), \
                       "_check_xyuv: x has shape %s, expected %s, %s, " \
                       "or %s" % (shape(x), (nx,ny), (nx,1), (nx,))
                assert shape(y)==(nx,ny) or shape(y)==(1,ny) or \
                       shape(y)==(ny,), \
                       "_check_xyuv: y has shape %s, expected %s, %s, " \
                       "or %s" % (shape(y), (nx,ny), (1,ny), (ny,))
            else:
                assert shape(x)==(nx,ny) or shape(x)==(1,ny) or \
                       shape(x)==(ny,), \
                       "_check_xyuv: x has shape %s, expected %s, %s, " \
                       "or %s" % (shape(x), (nx,ny), (1,ny), (ny,))
                assert shape(y)==(nx,ny) or shape(y)==(nx,1) or \
                       shape(y)==(nx,), \
                       "_check_xyuv: y has shape %s, expected %s, %s, " \
                       "or %s" % (shape(y), (nx,ny), (nx,1), (nx,))
    else:
        raise ValueError("_check_xyuv: u must be 1D or 2D, not %dD" % len(us))

    return x, y, u, v
示例#4
0
文件: Grid2D.py 项目: arnabkd/inf3331
    def __init__(self,
                 xmin=0, xmax=1, dx=0.5,
                 ymin=0, ymax=1, dy=0.5):
        # coordinates in each space direction:
        self.xcoor = seq(xmin, xmax, dx)
        self.ycoor = seq(ymin, ymax, dy)

        # store for convenience:
        self.dx = dx;  self.dy = dy  
        self.nx = self.xcoor.size;  self.ny = self.ycoor.size

        # make two-dim. versions of the coordinate arrays:
        # (needed for vectorized function evaluations)
        self.xcoorv = self.xcoor[:, newaxis]
        self.ycoorv = self.ycoor[newaxis, :]
示例#5
0
    def __init__(self, xmin=0, xmax=1, dx=0.5, ymin=0, ymax=1, dy=0.5):
        # coordinates in each space direction:
        self.xcoor = seq(xmin, xmax, dx)
        self.ycoor = seq(ymin, ymax, dy)

        # store for convenience:
        self.dx = dx
        self.dy = dy
        self.nx = self.xcoor.size
        self.ny = self.ycoor.size

        # make two-dim. versions of the coordinate arrays:
        # (needed for vectorized function evaluations)
        self.xcoorv = self.xcoor[:, newaxis]
        self.ycoorv = self.ycoor[newaxis, :]
示例#6
0
def heat_equation_numpy(t0, t1, dt, n, m, u, f, nu, verbose=None):
    """
	t0: starting time
	t1: ending time
	dt: time step
	n, m: dimensions of rectangle
	u: space- and time-dependent temperature within the material
	f: heat source
	nu: material-specific thermal diffusivity
	"""
    if verbose:
        print "Starting numpy implementation..."

    v = zeros((n * m)).reshape(n, m)

    for time in seq(t0, t1, dt):
        if verbose > 1:
            print "Calculating time-step for time...", time
        v[1:-1, 1:-1] = u[1:-1, 1:-1] + dt * (
            nu * u[:-2, 1:-1] + nu * u[1:-1, :-2] - 4 * nu * u[1:-1, 1:-1] +
            nu * u[1:-1, 2:] + nu * u[2:, 1:-1] + f[1:-1, 1:-1])
        u, v = v, u

    if verbose:
        print "Returning results..."
    return u
def run(nsteps):
    global step, time, y
    if step+nsteps > maxsteps:
        print 'no more memory available in y'; return

    y, step, time = oscillator.timeloop2(y, step, time, nsteps)
    print 'run %d steps up to step %d; current time=%g' % (nsteps,step,time)

    t = seq(0.0, time, dt)
    y1 = y[0,0:step+1]
    y2 = y[1,0:step+1]
    if len(t) != len(y1):
        raise ValueError, 'length of y1 (%d) and t (%d) are different' % \
              (len(y1), len(t))
    g1.plot(Gnuplot.Data(y1,y2, with='lines'))
示例#8
0
文件: SciPy.py 项目: eddienko/SamPy
    def solve(self):
        """Solve ODE system."""
        # mapping: name of f(y) to Python function for f(y):
        self._fy = {'y': lambda y: y, 'siny': lambda y: sin(y),
                    'y3': lambda y: y - y**3/6.0}
        # set initial conditions:
        self.y0 = [self.p['y0'], 0.0]
        # call SciPy solver:
        from scitools.numpyutils import seq
        self.t = seq(0, self.p['tstop'], self.p['dt'])

        from scipy.integrate import odeint
        self.yvec = odeint(self.f, self.y0, self.t)

        self.y = self.yvec[:,0]  # y(t)
        # write t and y(t) to sim.dat file:
        f = open('sim.dat', 'w')
        for y, t in zip(self.y, self.t):
            f.write('%g %g\n' % (t, y))
        f.close()
示例#9
0
文件: SciPy.py 项目: RainW7/SamPy
    def solve(self):
        """Solve ODE system."""
        # mapping: name of f(y) to Python function for f(y):
        self._fy = {
            'y': lambda y: y,
            'siny': lambda y: sin(y),
            'y3': lambda y: y - y**3 / 6.0
        }
        # set initial conditions:
        self.y0 = [self.p['y0'], 0.0]
        # call SciPy solver:
        from scitools.numpyutils import seq
        self.t = seq(0, self.p['tstop'], self.p['dt'])

        from scipy.integrate import odeint
        self.yvec = odeint(self.f, self.y0, self.t)

        self.y = self.yvec[:, 0]  # y(t)
        # write t and y(t) to sim.dat file:
        f = open('sim.dat', 'w')
        for y, t in zip(self.y, self.t):
            f.write('%g %g\n' % (t, y))
        f.close()
示例#10
0
print 'zeros(n, Float):', type(a), type(a[0]), a
a = zeros(n) # becomes array of integers
print 'zeros(n)', type(a), a

x = linspace(-5, 5, 11)
print 'linspace(-5, 5, 11)', type(x), type(x[0]), x

# arange is possible but not recommended
x = arange(-5, 5, 1, Float)
# note: round-off errors may prevent the last element
# in x from being equal to the upper bound 5
x = arange(-5, 5.1, 1)  # ensures that 5 is in x
print 'arange(-5, 5.1, 1)', type(x), type(x[0]), x
# better:
from scitools.numpyutils import seq
x = seq(-5, 5, 1)
print 'seq(-5, 5, 1)', type(x), type(x[0]), x

# it is trivial to make accompanying y values:
y = sin(x/2.0)*3.0
print 'y = sin(x/2.0)*3.0:', type(y), type(y[0]), y

# create a NumPy array of a Python list:
pl = [0, 1.2, 4, -9.1, 5, 8]
a = array(pl, Float)
print 'pl = [0, 1.2, 4, -9.1, 5, 8]; '\
      'array(pl, Float)', type(a), a
# from nested Python list to NumPy arrays and back again:
x = [0, 0.5, 1]; y = [-6.1, -2, 1.2]  # lists
a = array([x, y])  # form 2x3 array (x and y as rows)
# turn 1st row to Python list and use index to locate an entry:
示例#11
0
文件: misc.py 项目: Nhyiraba/scitools
def _check_xyzuvw(*args, **kwargs):
    nargs = len(args)
    if nargs == 4:
        x, y = [None]*2
        z, u, v, w = [asarray(a) for a in args]
    elif nargs == 6:
        x, y, z, u, v, w = [asarray(a) for a in args]
    else:
        raise TypeError("_check_xyzuvw: wrong number of arguments")

    indexing = kwargs.get('indexing', 'xy')

    us = shape(u)
    assert us == shape(v) == shape(w), \
           "_check_xyzuvw: u, v, and w must be of same shape"

    if len(us) == 1:
        if x is None and y is None:
            x = seq(us[0]-1)
            y = seq(us[0]-1)
        else:
            assert shape(x) == us, \
                   "_check_xyuv: x has shape %s, expected %s" % (shape(x), us)
            assert shape(y) == us, \
                   "_check_xyuv: y has shape %s, expected %s" % (shape(y), us)
        assert shape(z) == us, \
               "_check_xyuv: z has shape %s, expected %s" % (shape(z), us)
    elif len(us) == 2:
        nx, ny = us
        if x is None and y is None:
            x, y, z = _check_xyz(z, indexing=indexing)
        else:
            x, y, z = _check_xyz(x, y, z, indexing=indexing)
        assert shape(z) == us, \
               "_check_xyzuvw: z, u, v, and w must be of same shape"
    elif len(us) == 3:
        nx, ny, nz = us
        if x is None and y is None:
            if indexing == 'ij':
                nx, ny = ny, nx  # swap
            x, y, junk = meshgrid(seq(ny-1), seq(nx-1), seq(nz-1))
        else:
            if indexing == 'ij':
                assert shape(x)==us or shape(x)==(nx,1,1) or shape(x)==(nx,), \
                       "_check_xyzuvw: x has shape %s, expected %s, %s, or %s"\
                       % (shape(x), us, (nx,1,1), (nx,))
                assert shape(y)==us or shape(y)==(1,ny,1) or shape(y)==(ny,), \
                       "_check_xyzuvw: y has shape %s, expected %s, %s, or %s"\
                       % (shape(y), us, (1,ny,1), (ny,))
            else:
                assert shape(x)==us or shape(x)==(1,ny,1) or shape(x)==(ny,), \
                       "_check_xyzuvw: x has shape %s, expected %s, %s, or %s"\
                       % (shape(x), us, (1,ny,1), (ny,))
                assert shape(y)==us or shape(y)==(nx,1,1) or shape(y)==(nx,), \
                       "_check_xyzuvw: y has shape %s, expected %s, %s, or %s"\
                       % (shape(y), us, (nx,1,1), (nx,))
        assert shape(z) == us or shape(z) == (1,1,nz) or shape(z) == (nz,), \
               "_check_xyzuvw: z has shape %s, expected %s, %s, or %s" % \
               (shape(z), us, (1,1,nz), (nz,))
    else:
        raise ValueError(
            "_check_xyzuvw: u must be 1D, 2D, or 3D, not %dD" % len(us))

    return x, y, z, u, v, w
示例#12
0
def _check_xyzuvw(*args, **kwargs):
    nargs = len(args)
    if nargs == 4:
        x, y = [None] * 2
        z, u, v, w = [asarray(a) for a in args]
    elif nargs == 6:
        x, y, z, u, v, w = [asarray(a) for a in args]
    else:
        raise TypeError("_check_xyzuvw: wrong number of arguments")

    indexing = kwargs.get('indexing', 'xy')

    us = shape(u)
    assert us == shape(v) == shape(w), \
           "_check_xyzuvw: u, v, and w must be of same shape"

    if len(us) == 1:
        if x is None and y is None:
            x = seq(us[0] - 1)
            y = seq(us[0] - 1)
        else:
            assert shape(x) == us, \
                   "_check_xyuv: x has shape %s, expected %s" % (shape(x), us)
            assert shape(y) == us, \
                   "_check_xyuv: y has shape %s, expected %s" % (shape(y), us)
        assert shape(z) == us, \
               "_check_xyuv: z has shape %s, expected %s" % (shape(z), us)
    elif len(us) == 2:
        nx, ny = us
        if x is None and y is None:
            x, y, z = _check_xyz(z, indexing=indexing)
        else:
            x, y, z = _check_xyz(x, y, z, indexing=indexing)
        assert shape(z) == us, \
               "_check_xyzuvw: z, u, v, and w must be of same shape"
    elif len(us) == 3:
        nx, ny, nz = us
        if x is None and y is None:
            if indexing == 'ij':
                nx, ny = ny, nx  # swap
            x, y, junk = meshgrid(seq(ny - 1), seq(nx - 1), seq(nz - 1))
        else:
            if indexing == 'ij':
                assert shape(x)==us or shape(x)==(nx,1,1) or shape(x)==(nx,), \
                       "_check_xyzuvw: x has shape %s, expected %s, %s, or %s"\
                       % (shape(x), us, (nx,1,1), (nx,))
                assert shape(y)==us or shape(y)==(1,ny,1) or shape(y)==(ny,), \
                       "_check_xyzuvw: y has shape %s, expected %s, %s, or %s"\
                       % (shape(y), us, (1,ny,1), (ny,))
            else:
                assert shape(x)==us or shape(x)==(1,ny,1) or shape(x)==(ny,), \
                       "_check_xyzuvw: x has shape %s, expected %s, %s, or %s"\
                       % (shape(x), us, (1,ny,1), (ny,))
                assert shape(y)==us or shape(y)==(nx,1,1) or shape(y)==(nx,), \
                       "_check_xyzuvw: y has shape %s, expected %s, %s, or %s"\
                       % (shape(y), us, (nx,1,1), (nx,))
        assert shape(z) == us or shape(z) == (1,1,nz) or shape(z) == (nz,), \
               "_check_xyzuvw: z has shape %s, expected %s, %s, or %s" % \
               (shape(z), us, (1,1,nz), (nz,))
    else:
        raise ValueError("_check_xyzuvw: u must be 1D, 2D, or 3D, not %dD" %
                         len(us))

    return x, y, z, u, v, w
 def __call__(self,dim):
     if isinstance(self.max[dim],float):
         return scinp.seq( self.min[dim],self.max[dim],self.step[dim] )
     else:
         return scinp.iseq( self.min[dim],self.max[dim],self.step[dim] )
 def __call__(self, dim):
     if isinstance(self.max[dim], float):
         return scinp.seq(self.min[dim], self.max[dim], self.step[dim])
     else:
         return scinp.iseq(self.min[dim], self.max[dim], self.step[dim])
示例#15
0
def _check_xyzuvw(*args, **kwargs):
    nargs = len(args)
    if nargs == 4:
        x, y = [None] * 2
        z, u, v, w = [np.asarray(_) for _ in args]
    elif nargs == 6:
        x, y, z, u, v, w = [np.asarray(_) for _ in args]
    else:
        raise TypeError('_check_xyzuvw: wrong number of arguments')

    indexing = kwargs.get('indexing', 'xy')

    us = u.shape
    assert us == v.shape == w.shape, \
        '_check_xyzuvw: u, v, and w must be of same shape'

    if len(us) == 1:
        if x is None and y is None:
            x = seq(us[0] - 1)
            y = seq(us[0] - 1)
        else:
            assert x.shape == us, '_check_xyuv: x has shape %s, expected %s' % (x.shape, us)
            assert y.shape == us, '_check_xyuv: y has shape %s, expected %s' % (y.shape, us)
        assert z.shape == us, '_check_xyuv: z has shape %s, expected %s' % (z.shape, us)
    elif len(us) == 2:
        nx, ny = us
        if x is None and y is None:
            x, y, z = _check_xyz(z, indexing=indexing)
        else:
            x, y, z = _check_xyz(x, y, z, indexing=indexing)
        assert z.shape == us, '_check_xyzuvw: z, u, v, and w must be of same shape'
    elif len(us) == 3:
        nx, ny, nz = us
        if x is None and y is None:
            if indexing == 'ij':
                nx, ny = ny, nx  # swap
            x, y, junk = np.meshgrid(seq(ny - 1), seq(nx - 1), seq(nz - 1))
        else:
            if indexing == 'ij':
                assert (
                    x.shape == us or
                    x.shape == (nx, 1, 1) or
                    x.shape == (nx,)
                ), '_check_xyzuvw: x has shape %s, expected %s, %s, or %s' % (x.shape, us, (nx, 1, 1), (nx,))
                assert (
                    y.shape == us or
                    y.shape == (1, ny, 1) or
                    y.shape == (ny,)
                ), '_check_xyzuvw: y has shape %s, expected %s, %s, or %s' % (y.shape, us, (1, ny, 1), (ny,))
            else:
                assert (
                    x.shape == us or
                    x.shape == (1, ny, 1) or
                    x.shape == (ny,)
                ), '_check_xyzuvw: x has shape %s, expected %s, %s, or %s' % (x.shape, us, (1, ny, 1), (ny,))
                assert (
                    y.shape == us or
                    y.shape == (nx, 1, 1) or
                    y.shape == (nx,)
                ), '_check_xyzuvw: y has shape %s, expected %s, %s, or %s' % (y.shape, us, (nx, 1, 1), (nx,))
        assert (
            z.shape == us or
            z.shape == (1, 1, nz) or
            z.shape == (nz,)
        ), '_check_xyzuvw: z has shape %s, expected %s, %s, or %s' % (z.shape, us, (1, 1, nz), (nz,))
    else:
        raise ValueError('_check_xyzuvw: u must be 1D, 2D, or 3D, not %dD' % len(us))

    return x, y, z, u, v, w