Beispiel #1
0
def test_gelatize_2d_4():
    print('============ test_gelatize_2d_4 =============')

    V = H1Space('V', ldim=2)

    v = TestFunction(V, name='v')
    u = TestFunction(V, name='u')

    expr = BilinearForm((v, u), dot(grad(v), grad(u)))

    print('> input     >>> {0}'.format(expr))
    print('> gelatized >>> {0}'.format(gelatize(expr)))
Beispiel #2
0
def test_gelatize_1d_1():
    print('============ test_gelatize_1d_1 =============')

    V = H1Space('V', ldim=1)

    v = TestFunction(V, name='v')
    u = TestFunction(V, name='u')

    nx = symbols('nx', integer=True)
    px = symbols('px', integer=True)
    tx = symbols('tx')

    c1 = Constant('c1')
    c2 = Constant('c2')
    c3 = Constant('c3')
    c4 = Constant('c4')

    # ...
    expected = Mass(px, tx) / nx
    assert (gelatize(BilinearForm((v, u), u * v)) == expected)
    # ...

    # ...
    expected = nx * Stiffness(px, tx)
    assert (gelatize(BilinearForm((v, u), dot(grad(v), grad(u)))) == expected)
    # ...

    # ...
    expected = I * Advection(px, tx)
    assert (gelatize(BilinearForm((v, u), dx(u) * v)) == expected)
    # ...

    # ...
    expected = c1 * Mass(px, tx) / nx + c2 * I * Advection(
        px, tx) - c3 * I * Advection(px, tx) + c4 * nx * Stiffness(px, tx)
    assert (gelatize(
        BilinearForm((v, u), c1 * v * u + c2 * dx(u) * v + c3 * dx(v) * u +
                     c4 * dx(v) * dx(u))) == expected)
Beispiel #3
0
def test_gelatize_2d_3():
    print('============ test_gelatize_2d_3 =============')

    V = H1Space('V', ldim=2, is_block=True, shape=2)

    v = VectorTestFunction(V, name='v')
    u = VectorTestFunction(V, name='u')

    c = Constant('c')

    #    a = BilinearForm((v,u), div(v) * div(u) + rot(v) * rot(u))

    degrees = None

    expr = div(v) * div(u) + c * rot(v) * rot(u)
    expr = BilinearForm((v, u), expr)
    print('> input     >>> {0}'.format(expr))
    print('> gelatized >>> {0}'.format(gelatize(expr, degrees=degrees)))
Beispiel #4
0
def compile_symbol(name,
                   a,
                   degrees,
                   n_elements=None,
                   verbose=False,
                   namespace=globals(),
                   context=None,
                   backend='python',
                   export_pyfile=True):
    """."""
    if not isinstance(a, BilinearForm):
        raise TypeError('Expecting a BilinearForm')

    # TODO: nderiv must be computed from the weak form
    nderiv = 1

    # ... weak form attributs
    dim = a.ldim
    fields = a.fields

    # TODO improve
    is_block = False
    is_vector = False

    if verbose:
        print('> dim    = ', dim)
        print('> Fields = ', fields)
    # ...

    # ... contants
    d_args = arguments_datatypes_as_dict(a.constants)
    args, dtypes = arguments_datatypes_split(d_args)
    # ...

    # TODO check what are the free_symbols of expr,
    #      to make sure the final code will compile
    #      the remaining free symbols must be the trial/test basis functions,
    #      and the coordinates

    # ...
    if is_vector and not (is_block):
        raise NotImplementedError(
            'We only treat the case of a block space, for '
            'which all components have are identical.')
    # ...

    # ...
    if is_block:
        pattern = 'block'
    elif is_vector:
        raise NotImplementedError('TODO.')
    else:
        pattern = 'scalar'
    # ...

    # ... get name of the template to be used
    template_str = '_symbol_{dim}d_{pattern}'.format(dim=dim, pattern=pattern)
    # ...

    # ... import the variable from the templates module
    #     NOTE: THE PATH IS HARD CODED HERE!
    try:
        package = importlib.import_module("gelato.codegen.templates.symbol")

    except:
        raise ImportError('could not import {0}'.format(name))

    template = getattr(package, template_str)
    # ...

    # ...
    expr = gelatize(a, degrees=degrees, n_elements=n_elements)
    # ...

    # ... TODO add an attribute called symbol_expr to the BilinearForm?
    #    setattr(a, 'symbol_expr', expr)
    # ...

    # ... identation (def function body)
    tab_base = ' ' * 4
    tab = tab_base
    # ...

    # ... append n_elements as argument of the generated symbol function
    if n_elements:
        n_elements_str = ''
        n_elements_types_str = ''

    else:
        ns = ['nx', 'ny', 'nz'][:dim]
        n_elements_str = ', '.join(n for n in ns)
        n_elements_str = ', {}'.format(n_elements_str)

        n_elements_types_str = ', '.join('int' for n in ns)
        n_elements_types_str = ', {}'.format(n_elements_types_str)
    # ...

    # ... field coeffs
    if fields:
        raise NotImplementedError()

        field_coeffs = construct_field_coeffs_names(fields)
        field_coeffs_str = print_field_coeffs(field_coeffs)
        field_types_str = print_field_coeffs_types(field_coeffs, dim)

        field_values = construct_field_values_names(expr, fields)

        eval_field_str = print_eval_field(expr,
                                          dim,
                                          fields,
                                          field_coeffs,
                                          field_values,
                                          verbose=verbose)

        # ... update identation to be inside the loop
        if is_bilinear_form:
            for i in range(0, 3 * dim):
                tab += ' ' * 4

        elif is_linear_form:
            for i in range(0, 2 * dim):
                tab += ' ' * 4

        elif is_function_form:
            for i in range(0, dim):
                tab += ' ' * 4

        field_value_str = print_assign_field(expr,
                                             dim,
                                             fields,
                                             field_coeffs,
                                             field_values,
                                             tab,
                                             verbose=verbose)

        tab = tab_base
        # ...

    else:
        field_coeffs_str = ''
        eval_field_str = ''
        field_value_str = ''
        field_types_str = ''
    # ...

    # ...
    if fields:
        raise NotImplementedError()

        # we call normalize a second time, and activate the modification of the
        # fields, this will turn terms like dx(F) into F_x if F is a field
        expr = normalize(expr, enable_fields=True)
    # ...

    # ...
    x_args = construct_x_args_names(dim)
    t_args = construct_t_args_names(dim)

    x_args_str = print_position_args(x_args)
    t_args_str = print_fourier_args(t_args)
    # ...

    # ... TODO be careful of conflict when adding block case
    mat_args_str = print_mat_args()
    # ...

    # ... compute indentation
    # TODO
    #    tab += ' '*4
    # ...

    # ...
    tab = tab_base
    # ...

    # ...
    if is_block:
        raise NotImplementedError('')

        # ... - initializing element matrices
        #     - define arguments
        # test functions and trial functions
        if is_bilinear_form:
            size = 2 * dim

        # test functions
        elif is_linear_form:
            size = dim

        elif is_function_form:
            raise NotImplementedError('')

        n_rows = test_n_components
        n_cols = trial_n_components
        mat_args = construct_element_matrix_names(n_rows, n_cols)
        mat_args_str = print_element_matrix_args(n_rows, n_cols, mat_args)
        mat_init_str = print_element_matrix_init(n_rows, n_cols, mat_args,
                                                 size, tab)

        # ... update identation to be inside the loop
        for i in range(0, size):
            tab += ' ' * 4

        tab_base = tab
        # ...

        # ... initializing accumulation variables
        accum_init_str = print_accumulation_var_init(n_rows, n_cols, tab)
        # ...

        # .. update indentation
        for i in range(0, dim):
            tab += ' ' * 4
        # ...

        # ... accumulation contributions
        accum_str = print_accumulation_var(n_rows, n_cols, expr, tab)
        # ...

        # ... assign accumulated values to element matrix
        if is_bilinear_form:
            accum_assign_str = print_bilinear_accumulation_assign(
                n_rows, n_cols, dim, tab_base)

        elif is_linear_form:
            accum_assign_str = print_linear_accumulation_assign(
                n_rows, n_cols, dim, tab_base)
        # ...

        code = template.format(__KERNEL_NAME__=name,
                               __X_ARGS__=x_args_str,
                               __T_ARGS__=t_args_str,
                               __MAT_ARGS__=mat_args_str,
                               __N_ELEMENTS__=n_elements_str,
                               __FIELD_COEFFS__=field_coeffs_str,
                               __FIELD_EVALUATION__=eval_field_str,
                               __MAT_INIT__=mat_init_str,
                               __ACCUM_INIT__=accum_init_str,
                               __FIELD_VALUE__=field_value_str,
                               __TEST_FUNCTION__=test_function_str,
                               __TRIAL_FUNCTION__=trial_function_str,
                               __ACCUM__=accum_str,
                               __ACCUM_ASSIGN__=accum_assign_str,
                               __ARGS__=args)

    else:
        # we call evalf to avoid having fortran doing the evaluation of rational
        # division

        e = _convert_int_to_float(expr.evalf())
        code = template.format(__SYMBOL_NAME__=name,
                               __SYMBOL_EXPR__=e.evalf(),
                               __X_ARGS__=x_args_str,
                               __T_ARGS__=t_args_str,
                               __MAT_ARGS__=mat_args_str,
                               __N_ELEMENTS__=n_elements_str,
                               __FIELD_COEFFS__=field_coeffs_str,
                               __FIELD_EVALUATION__=eval_field_str,
                               __FIELD_VALUE__=field_value_str,
                               __ARGS__=args)
    # ...

#    print('--------------')
#    print(code)
#    print('--------------')
#    import sys; sys.exit(0)

# ...
    if context:
        from pyccel.epyccel import ContextPyccel

        if isinstance(context, ContextPyccel):
            context = [context]
        elif isinstance(context, (list, tuple)):
            for i in context:
                assert (isinstance(i, ContextPyccel))
        else:
            raise TypeError(
                'Expecting a ContextPyccel or list/tuple of ContextPyccel')

        # append functions to the namespace
        for c in context:
            for k, v in list(c.functions.items()):
                namespace[k] = v[0]
    # ...

    # ...
    exec(code, namespace)
    kernel = namespace[name]
    # ...

    # ... export the python code of the module
    if export_pyfile:
        write_code(name, code, ext='py', folder='.pyccel')
    # ...

    return kernel
Beispiel #5
0
def test_gelatize_3d_1():
    print('============ test_gelatize_3d_1 =============')

    V = H1Space('V', ldim=3)

    v = TestFunction(V, name='v')
    u = TestFunction(V, name='u')

    nx, ny, nz = symbols('nx ny nz', integer=True)
    px, py, pz = symbols('px py pz', integer=True)
    tx, ty, tz = symbols('tx ty tz')

    c = Constant('c')

    bx = Constant('bx')
    by = Constant('by')
    bz = Constant('bz')
    b = Tuple(bx, by, bz)

    # ...
    expected = Mass(px, tx) * Mass(py, ty) * Mass(pz, tz) / (nx * ny * nz)
    assert (gelatize(BilinearForm((v, u), u * v)) == expected)
    # ...

    # ...
    expected = nx * Mass(py, ty) * Mass(pz, tz) * Stiffness(px, tx) / (ny * nz)
    assert (gelatize(BilinearForm((v, u), dx(u) * dx(v))) == expected)
    # ...

    # ...
    expected = I * Advection(py, ty) * Mass(px, tx) * Mass(pz, tz) / (nx * nz)
    assert (gelatize(BilinearForm((v, u), dy(u) * v)) == expected)
    # ...

    # ...
    expected = I * Advection(px, tx) * Mass(py, ty) * Mass(pz, tz) / (ny * nz)
    assert (gelatize(BilinearForm((v, u), dx(u) * v)) == expected)
    # ...

    # ...
    expected = (nx * Mass(py, ty) * Mass(pz, tz) * Stiffness(px, tx) /
                (ny * nz) +
                ny * Mass(px, tx) * Mass(pz, tz) * Stiffness(py, ty) /
                (nx * nz) +
                nz * Mass(px, tx) * Mass(py, ty) * Stiffness(pz, tz) /
                (nx * ny))
    assert (gelatize(BilinearForm((v, u), dot(grad(v), grad(u)))) == expected)
    # ...

    # ...
    expected = (
        nx * Mass(py, ty) * Mass(pz, tz) * Stiffness(px, tx) / (ny * nz) +
        I * Advection(px, tx) * Mass(py, ty) * Mass(pz, tz) / (ny * nz) +
        ny * Mass(px, tx) * Mass(pz, tz) * Stiffness(py, ty) / (nx * nz) +
        I * Advection(py, ty) * Mass(px, tx) * Mass(pz, tz) / (nx * nz) +
        nz * Mass(px, tx) * Mass(py, ty) * Stiffness(pz, tz) / (nx * ny))
    assert (gelatize(
        BilinearForm(
            (v, u),
            dot(grad(v), grad(u)) + dx(u) * v + dy(u) * v)) == expected)
    # ...

    # ...
    expected = (-bx * I * Advection(px, tx) * Mass(py, ty) * Mass(pz, tz) /
                (ny * nz) -
                by * I * Advection(py, ty) * Mass(px, tx) * Mass(pz, tz) /
                (nx * nz) -
                bz * I * Advection(pz, tz) * Mass(px, tx) * Mass(py, ty) /
                (nx * ny))
    assert (gelatize(BilinearForm((v, u), dot(b, grad(v)) * u)) == expected)
    # ...

    # ...
    expected = (bx**2 * nx * Mass(py, ty) * Mass(pz, tz) * Stiffness(px, tx) /
                (ny * nz) + 2 * bx * by * Advection(px, tx) *
                Advection(py, ty) * Mass(pz, tz) / nz + 2 * bx * bz *
                Advection(px, tx) * Advection(pz, tz) * Mass(py, ty) / ny +
                by**2 * ny * Mass(px, tx) * Mass(pz, tz) * Stiffness(py, ty) /
                (nx * nz) + 2 * by * bz * Advection(py, ty) *
                Advection(pz, tz) * Mass(px, tx) / nx +
                bz**2 * nz * Mass(px, tx) * Mass(py, ty) * Stiffness(pz, tz) /
                (nx * ny))
    assert (gelatize(BilinearForm(
        (v, u),
        dot(b, grad(v)) * dot(b, grad(u)))) == expected)
    # ...

    degrees = None
    #    degrees = [2, 1, 1]

    #    evaluate = True
    evaluate = False
Beispiel #6
0
def test_gelatize_2d_1():
    print('============ test_gelatize_2d_1 =============')

    V = H1Space('V', ldim=2)

    v = TestFunction(V, name='v')
    u = TestFunction(V, name='u')

    nx, ny = symbols('nx ny', integer=True)
    px, py = symbols('px py', integer=True)
    tx, ty = symbols('tx ty')

    c = Constant('c')

    bx = Constant('bx')
    by = Constant('by')
    b = Tuple(bx, by)

    # ...
    expected = Mass(px, tx) * Mass(py, ty) / (nx * ny)
    assert (gelatize(BilinearForm((v, u), u * v)) == expected)
    # ...

    # ...
    expected = Mass(py, ty) * nx * Stiffness(px, tx) / ny
    assert (gelatize(BilinearForm((v, u), dx(u) * dx(v))) == expected)
    # ...

    # ...
    expected = I * Advection(py, ty) * Mass(px, tx) / nx
    assert (gelatize(BilinearForm((v, u), dy(u) * v)) == expected)
    # ...

    # ...
    expected = I * Advection(px, tx) * Mass(py, ty) / ny
    assert (gelatize(BilinearForm((v, u), dx(u) * v)) == expected)
    # ...

    # ...
    expected = Mass(px, tx) * ny * Stiffness(py, ty) / nx + Mass(
        py, ty) * nx * Stiffness(px, tx) / ny
    assert (gelatize(BilinearForm((v, u), dot(grad(v), grad(u)))) == expected)
    # ...

    # ...
    expected = (nx * Mass(py, ty) * Stiffness(px, tx) / ny +
                I * Advection(px, tx) * Mass(py, ty) / ny +
                ny * Mass(px, tx) * Stiffness(py, ty) / nx +
                I * Advection(py, ty) * Mass(px, tx) / nx)
    assert (gelatize(
        BilinearForm(
            (v, u),
            dot(grad(v), grad(u)) + dx(u) * v + dy(u) * v)) == expected)
    # ...

    # ...
    expected = -bx * I * Advection(px, tx) * Mass(
        py, ty) / ny - by * I * Advection(py, ty) * Mass(px, tx) / nx
    assert (gelatize(BilinearForm((v, u), dot(b, grad(v)) * u)) == expected)
    # ...

    # ...
    expected = bx**2 * nx * Mass(py, ty) * Stiffness(
        px, tx) / ny + 2 * bx * by * Advection(px, tx) * Advection(
            py, ty) + by**2 * ny * Mass(px, tx) * Stiffness(py, ty) / nx
    assert (gelatize(BilinearForm(
        (v, u),
        dot(b, grad(v)) * dot(b, grad(u)))) == expected)
    # ...

    degrees = None
    #    degrees = [2, 1]

    #    evaluate = True
    evaluate = False