コード例 #1
0
ファイル: test_grid_function.py プロジェクト: MiroK/sl-pde
def test_4d_approx(n):
    t, x, y, z = sp.symbols('t x y z')

    t_domain = np.linspace(-1, 4, n)  # Physical
    x_domain = np.linspace(0, 2, n)
    y_domain = np.linspace(0, 1, n)
    z_domain = np.linspace(-2, 0, n)
    T, X, Y, Z = np.meshgrid(t_domain,
                             x_domain,
                             y_domain,
                             z_domain,
                             indexing='ij')

    # I will look at 3 points aroung the center
    pts_indices = [(n / 2 - 1, ) * 4, (n / 2, ) * 4, (n / 2 + 1, ) * 4]
    # Their physical representations
    pts = map(
        lambda
        (i, j, k, l): [t_domain[i], x_domain[j], y_domain[k], z_domain[k]],
        pts_indices)

    symbols = (t, x, y, z)

    functions = [
        sp.sin(sp.pi * t) * sp.exp(-(x**2 + y**3 - z**2)),
        sp.cos(sp.pi * t) * sp.exp(-(x**2 + 3 * y**2 + x * y * z))
    ]

    # Some deriveatives
    derivs = [(0, 0, 3, 0), (1, 0, 1, 1), (0, 1, 0, 2), (2, 1, 0, 3),
              (1, 1, 2, 1)]
    numeric = np.zeros(len(pts))
    errors = defaultdict(list)
    for foo in functions:
        foo_values = sp.lambdify((t, x, y, z), foo, 'numpy')(T, X, Y, Z)
        grid_foo = GridFunction([t_domain, x_domain, y_domain, z_domain],
                                foo_values)

        for d in derivs:
            if d != (0, 0, 0, 0):
                dfoo = foo
                for var, deg in zip(symbols, d):
                    dfoo = dfoo.diff(var, deg)
            else:
                dfoo = foo

            # True
            df = sp.lambdify((t, x, y, z), dfoo)
            exact = np.array([df(*pt) for pt in pts])

            dgrid_foo = diff(grid_foo, d)
            [
                dgrid_foo(pi, numeric[i:i + 1])
                for i, pi in enumerate(pts_indices)
            ]

            error = exact - numeric
            e = np.linalg.norm(error)
            errors[foo].append(e)
    return errors
コード例 #2
0
ファイル: test_grid_function.py プロジェクト: MiroK/sl-pde
def test_3d_poly(n):
    t, x, y = sp.symbols('t x y')

    t_domain = np.linspace(-1, 4, n)  # Physical
    x_domain = np.linspace(0, 2, n)
    y_domain = np.linspace(0, 1, n)
    T, X, Y = np.meshgrid(t_domain, x_domain, y_domain, indexing='ij')

    # I will look at 3 points aroung the center
    pts_indices = [(n / 2 - 1, ) * 3, (n / 2, ) * 3, (n / 2 + 1, ) * 3]
    # Their physical representations
    pts = map(lambda (i, j, k): [t_domain[i], x_domain[j], y_domain[k]],
              pts_indices)

    symbols = (t, x, y)
    functions = [
        2 * t + 1 * x - 2 * y,
        3 * (t**2 - 2 * t + 1) + x**2 + t * x - 4 * t * y + 3 * t * x,
        y * (t**4 - 2 * t**2 + t - 1) + 2 * t**2 * 4 * x**3 - y * x * 4 * t**4
    ]
    # Some deriveatives
    derivs = [(0, 0, 0), (1, 0, 1), (0, 1, 0), (2, 1, 0), (1, 2, 0), (3, 1, 1),
              (1, 3, 1)]

    status = True
    numeric = np.zeros(len(pts))
    for foo in functions:
        print foo
        foo_values = sp.lambdify((t, x, y), foo, 'numpy')(T, X, Y)
        grid_foo = GridFunction([t_domain, x_domain, y_domain], foo_values)

        for d in derivs:
            if d != (0, 0, 0):
                dfoo = foo
                for var, deg in zip(symbols, d):
                    dfoo = dfoo.diff(var, deg)
            else:
                dfoo = foo
            print '\t', d, dfoo,
            # True
            df = sp.lambdify((t, x, y), dfoo)
            exact = np.array([df(*pt) for pt in pts])

            dgrid_foo = diff(grid_foo, d)
            [
                dgrid_foo(pi, numeric[i:i + 1])
                for i, pi in enumerate(pts_indices)
            ]

            error = exact - numeric
            e = np.linalg.norm(error)
            print 'OK' if e < TOL else (exact, numeric)
            status = status and e < TOL
    return status
コード例 #3
0
ファイル: test_grid_function.py プロジェクト: MiroK/sl-pde
def test_2d_poly(n):
    '''Error for some approx at [-1, 4]x[0, 2]'''
    t, x = sp.symbols('t x')

    t_domain = np.linspace(-1, 4, n)  # Physical
    x_domain = np.linspace(0, 2, n)  # Physical
    T, X = np.meshgrid(t_domain, x_domain, indexing='ij')

    # I will look at 3 points aroung the center
    pts_indices = [(n / 2 - 1, ) * 2, (n / 2, ) * 2, (n / 2 + 1, ) * 2]
    # Their physical representations
    pts = map(lambda (i, j): [t_domain[i], x_domain[j]], pts_indices)

    symbols = (t, x)
    functions = [
        2 * t + x, 3 * (t**2 - 2 * t + 1) + x**2,
        (t**4 - 2 * t**2 + t - 1) + 2 * t**2 * 4 * x**3 - x * 3 * t**2
    ]
    # Some deriveatives
    derivs = [(0, 0), (1, 0), (0, 1), (1, 1), (2, 1), (3, 1), (1, 3), (2, 2)]

    numeric = np.zeros(len(pts))
    status = True
    for foo in functions:
        print foo
        foo_values = sp.lambdify((t, x), foo, 'numpy')(T, X)
        grid_foo = GridFunction([t_domain, x_domain], foo_values)

        for d in derivs:
            if d != (0, 0):
                dfoo = foo
                for var, deg in zip(symbols, d):
                    dfoo = dfoo.diff(var, deg)
            else:
                dfoo = foo
            print '\t', d, dfoo,
            # True
            df = sp.lambdify((t, x), dfoo)
            exact = np.array([df(*pt) for pt in pts])

            dgrid_foo = diff(grid_foo, d)
            [
                dgrid_foo(pi, numeric[i:i + 1])
                for i, pi in enumerate(pts_indices)
            ]

            error = exact - numeric
            e = np.linalg.norm(error)
            print 'OK' if e < TOL else (exact, numeric)
            status = status and e < TOL
    return status
コード例 #4
0
ファイル: test_grid_function.py プロジェクト: MiroK/sl-pde
def test_2d_approx(n):
    '''Error for some approx at [-1, 4]x[0, 2].Just convergence'''
    t, x = sp.symbols('t x')

    t_domain = np.linspace(-1, 4, n)  # Physical
    x_domain = np.linspace(0, 2, n)  # Physical
    T, X = np.meshgrid(t_domain, x_domain, indexing='ij')

    # I will look at 3 points aroung the center
    pts_indices = [(n / 2 - 1, ) * 2, (n / 2, ) * 2, (n / 2 + 1, ) * 2]
    # Their physical representations
    pts = map(lambda (i, j): [t_domain[i], x_domain[j]], pts_indices)

    symbols = (t, x)
    functions = [
        sp.sin(sp.pi * (t + x)), t * sp.sin(2 * sp.pi * (x - 3 * t * x)),
        sp.cos(2 * sp.pi * x * t)
    ]

    # Some deriveatives
    derivs = [(1, 0), (0, 1), (1, 1), (2, 1), (3, 1), (1, 3), (2, 2)]
    errors = {foo: list() for foo in functions}
    numeric = np.zeros(len(pts))
    for foo in functions:

        foo_values = sp.lambdify((t, x), foo, 'numpy')(T, X)
        grid_foo = GridFunction([t_domain, x_domain], foo_values)

        for d in derivs:
            if d != (0, 0):
                dfoo = foo
                for var, deg in zip(symbols, d):
                    dfoo = dfoo.diff(var, deg)
            else:
                dfoo = foo

            # True
            df = sp.lambdify((t, x), dfoo)
            exact = np.array([df(*pt) for pt in pts])

            dgrid_foo = diff(grid_foo, d)
            [
                dgrid_foo(pi, numeric[i:i + 1])
                for i, pi in enumerate(pts_indices)
            ]

            error = exact - numeric
            e = np.linalg.norm(error)
            errors[foo].append(e)
    return errors
コード例 #5
0
ファイル: test_grid_function.py プロジェクト: MiroK/sl-pde
def test_1d_poly_vec(n):
    '''Error for some approx at [-1, 4]'''
    t = sp.Symbol('t')
    t_domain = np.linspace(-1, 4, n)  # Physical
    t_domain_index = range(len(t_domain))

    # I will look at 3 points aroung the center
    pts_index = [
        t_domain_index[len(t_domain) / 2 - 1],
        t_domain_index[len(t_domain) / 2],
        t_domain_index[len(t_domain) / 2 + 1]
    ]
    pts = t_domain[pts_index]

    symbols = (t, )
    functions = [2 * t, 3 * (t**2 - 2 * t + 1), (t**4 - 2 * t**2 + t - 1)]
    # We look at up to 3 derivatives
    derivs = [(0, ), (1, ), (2, ), (3, )]
    status = True

    numeric = np.zeros((len(pts), 2))
    for foo in functions:
        print foo
        foo_values = np.array(map(sp.lambdify(t, foo), t_domain))
        grid_foo = GridFunction([t_domain], [foo_values, 2 * foo_values])
        for d in derivs:
            if d != (0, ):
                dfoo = foo
                for var, deg in zip(symbols, d):
                    dfoo = dfoo.diff(var, deg)
            else:
                dfoo = foo
            print '\t', d, dfoo,
            # True
            df = sp.lambdify(t, dfoo)
            exact = df(pts)
            exact = np.c_[exact, 2 * exact]

            dgrid_foo = diff(grid_foo, d)

            for row, pi in zip(numeric, pts_index):
                np.put(row, range(2), dgrid_foo((pi, )))

            error = exact - numeric
            e = np.linalg.norm(error)
            print 'OK' if e < TOL else (exact, numeric)

            status = status and e < TOL
    return status
コード例 #6
0
ファイル: test_grid_function.py プロジェクト: MiroK/sl-pde
def test_1d_approx(n, width=7):
    '''Error for some approx at [-1, 4]. Just convergence'''
    t = sp.Symbol('t')
    t_domain = np.linspace(-1, 4, n)  # Physical
    t_domain_index = range(len(t_domain))

    # I will look at 3 points aroung the center
    pts_index = [
        t_domain_index[len(t_domain) / 2 - 1],
        t_domain_index[len(t_domain) / 2],
        t_domain_index[len(t_domain) / 2 + 1]
    ]
    pts = t_domain[pts_index]

    symbols = (t, )
    functions = [2 * sp.sin(sp.pi * t), sp.cos(2 * sp.pi * t**2)]
    # We look at up to 3 derivatives
    derivs = [(1, ), (2, ), (3, ), (4, )]

    numeric = np.zeros(len(pts))
    status = {f: list() for f in functions}
    for foo in functions:
        foo_values = np.array(map(sp.lambdify(t, foo), t_domain))
        grid_foo = GridFunction([t_domain], foo_values)
        for d in derivs:
            if d != (0, ):
                dfoo = foo
                for var, deg in zip(symbols, d):
                    dfoo = dfoo.diff(var, deg)
            else:
                dfoo = foo
            # True
            df = sp.lambdify(t, dfoo)
            exact = np.array(map(df, pts))

            dgrid_foo = diff(grid_foo, d, width=width)
            numeric.ravel()[:] = [dgrid_foo((pi, )) for pi in pts_index]

            error = exact - numeric
            e = np.linalg.norm(error)
            status[foo].append(e)

    return status
コード例 #7
0
if __name__ == '__main__':
    from grid_function import GridFunction
    import numpy as np

    f, g = sp.symbols('f g')

    n = 24
    grid = [
        np.linspace(-1, 4, n),
        np.linspace(0, 2, n),
        np.linspace(0, 1, n),
        np.linspace(-2, 0, n)
    ]
    T, X, Y, Z = np.meshgrid(*grid, indexing='ij')

    f, g, t, x, y, z = sp.symbols('f g t x y z')
    # The one that gives us data
    expr = t**2 + sp.sin(4 * x * y * z) + z**2

    expr_numpy = sp.lambdify((t, x, y, z), expr, 'numpy')
    data = expr_numpy(T, X, Y, Z)

    grid_f = GridFunction(grid, [[data, 2 * data], [3 * data, 4 * data]])

    #y = np.zeros(grid_f.value_shape)
    print grid_f((8, 7, 6, 7))
    #grid_g = GridFunction(grid, [data, -2*data])

    #print expr_value_shape(f * g, {f: grid_f, g: grid_g})
コード例 #8
0
ファイル: learn_advection.py プロジェクト: MiroK/sl-pde
u, wind, x, t = sp.symbols('u w x t')
# Use exact solution to generate the data
u_exact = sp.sin(sp.sin(x) + t)

u_exact = sp.lambdify((t, x), u_exact, 'numpy')

# All data
t_domain = np.linspace(0, T_end, n_samples)
x_domain = np.linspace(-0.5, 0.5, n_samples)
T, X = np.meshgrid(t_domain, x_domain, indexing='ij')
u_data = u_exact(T, X)

# To build the model we sample the data (in interior indices) to get
# the derivative right. For the rhs we keep it simple
u_grid = GridFunction([t_domain, x_domain], u_data)

x_grid = Coordinate([t_domain, x_domain], 1)

lhs_stream = compile_stream((sp.Derivative(u, t), ), {u: u_grid})

foos = polynomials([u], 3)
derivatives = Dx(u, 1, [3])[1:]  # Don't include 0;th
# Combine these guys
columns = [(1./sp.cos(x))*sp.Derivative(u, x)] + \
          list(foos) + \
          list(derivatives) + \
          [reduce(operator.mul, cs) for cs in itertools.product(foos, derivatives)]

rhs_stream = compile_stream(columns, {u: u_grid})
コード例 #9
0
    grid = [
        np.linspace(-1, 4, n),
        np.linspace(0, 2, n),
        np.linspace(0, 1, n),
        np.linspace(-2, 0, n)
    ]
    T, X, Y, Z = np.meshgrid(*grid, indexing='ij')

    f, g, t, x, y, z = sp.symbols('f g t x y z')
    # The one that gives us data
    expr = t**2 + sp.sin(4 * x * y * z) + z**2

    expr_numpy = sp.lambdify((t, x, y, z), expr, 'numpy')
    data = expr_numpy(T, X, Y, Z)

    grid_expr = GridFunction(grid, data)

    grid_expr1 = GridFunction(grid, data + data**2)

    # Check the values at
    i_point = (8, 7, 6, 7)

    # print expand_derivative(sp.Derivative(f**2 + g**2, y, x))

    # exit()
    expression = (1, f, f**2, sp.sin(f) * f, sp.Derivative(f, x),
                  f * sp.Derivative(f, x),
                  f**2 + sp.sqrt(f) * sp.Derivative(f, y),
                  sp.Derivative(f, x, y) + sp.Derivative(g, y, y),
                  sp.Derivative(f**2, y, x) + sp.Derivative(g, y, x))
    subs = {f: grid_expr, g: grid_expr1}
コード例 #10
0
ファイル: learn_odes.py プロジェクト: MiroK/sl-pde
print 'A', A
print 'eigenvalues', eigw

u0 = np.ones(n_eqs)
# This is now a vector valued function
u_exact = lambda t, U=U, eigw=eigw, u0=u0: (
    (U.dot(np.diag([np.exp(ei * t) for ei in eigw]))).dot(U.T)).dot(u0)

# # All data
t_domain = np.linspace(0, T_end, n_samples)
u_data = np.array(map(u_exact, t_domain))

# Let's make one grid function for evvery component of that vector
u_grids = []
for col in range(n_eqs):
    u_grids.append(GridFunction([t_domain], u_data[:, col]))
# And a symbols for each
us = sp.symbols(' '.join(['u%d' % i for i in range(n_eqs)]))

symbol_foo_map = dict(zip(us, u_grids))
# We are going to build a block structured system d u0/dt @ all_pts, next component
lhs_streams = [
    compile_stream((sp.Derivative(usi, t), ), symbol_foo_map) for usi in us
]

# NOTE: this is reused for each block
columns = polynomials(us, 2)
print columns

rhs_stream = compile_stream(columns, symbol_foo_map)
コード例 #11
0
    grid = [
        np.linspace(-1, 4, n),
        np.linspace(0, 2, n),
        np.linspace(0, 1, n),
        np.linspace(-2, 0, n)
    ]
    T, X, Y, Z = np.meshgrid(*grid, indexing='ij')

    f, t, x, y, z = sp.symbols('f t x y z')
    # The one that gives us data
    expr = t**2 + sp.sin(4 * x * y * z) + z**2

    expr_numpy = sp.lambdify((t, x, y, z), expr, 'numpy')
    data = expr_numpy(T, X, Y, Z)

    grid_expr = GridFunction(grid, data)

    # Check the values at
    i_point = (8, 7, 6, 7)

    point = [grid[i][p] for i, p in enumerate(i_point)]

    compiled_expr = compile_expr(f**3 + f * sp.Derivative(f**2, x, y)**2 +
                                 f / 3 + sp.sin(f), {f: grid_expr},
                                 grid=grid)
    values[:] = compiled_expr(i_point)

    # Now take some derivative
    df_expr = expr**3 + expr * (sp.Derivative(expr, x,
                                              y))**2 + expr / 3 + sp.sin(expr)
コード例 #12
0
ファイル: learn_source.py プロジェクト: MiroK/sl-pde
u, x, t = sp.symbols('u x t')
# Use exact solution to generate the data
u_exact_expr = x**2*t
u_exact = sp.lambdify((t, x), u_exact_expr, 'numpy')

# All data
t_domain = np.linspace(0, T_end, n_samples)
x_domain = np.linspace(0, X_end, n_samples)
T, X = np.meshgrid(t_domain, x_domain, indexing='ij')
u_data = u_exact(T, X)

# To build the model we sample the data (in interior indices) to get
# the derivative right. For the rhs we keep it simple
grid = [t_domain, x_domain]
u_grid = GridFunction(grid, u_data)

equation = sp.Derivative(u, t) - u - sp.Derivative(u, x) - sp.Derivative(u, x, x)

lhs_stream = compile_stream((equation, ), {u: u_grid})

foos = (-t*x**2, -2*t*x, -2*t, x**2)# + (sp.sin(x), sp.cos(x), sp.sin(t), sp.cos(t))
#polynomials([x, t], 3)

# Combine these guys
columns = list(foos)

rhs_stream = compile_stream(columns, {u:u_grid})

# A subset of this data is now used for training; where we can eval deriv
# safely
コード例 #13
0
    np.linspace(0, 1, n),
    np.linspace(-2, 0, n)
]
T, X, Y, Z = np.meshgrid(*grid, indexing='ij')

f, g, t, x, y, z = sp.symbols('f g t x y z')

expressions = (
    x,  #t**2 + sp.sin(4*x*y*z) + z**2,
    y)  #t + sp.exp(-(4*(x**2 + y**2))) + z**3)

grid_fs = []
for f_ in expressions:
    expr_numpy = sp.lambdify((t, x, y, z), f_, 'numpy')
    data = expr_numpy(T, X, Y, Z)
    grid_fs.append(GridFunction(grid, data))

# Not the streams
expression = (1, f, g, f**2 + f * g + g**2, sp.sin(f + g),
              sp.exp(-(f**2 + g**2)), sp.Derivative(f, x),
              sp.Derivative(f**2, y), sp.Derivative(f + g, x),
              2 * f * sp.Derivative(g, y, x), 2 * g * sp.Derivative(f, y, x),
              sp.Derivative(2 * f * g, y, x, y), sp.Derivative(f**2, x, x))

from streams import expand_derivative
gg = expand_derivative(expression[0])

print
subs = {f: grid_fs[0], g: grid_fs[1]}

# Sympy version