示例#1
0
def test_basic_codegen():
    """Test whether the code generator returns a working method. The
    generated method always returns 0."""
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=0,
                   component_id="<state>",
                   depends_on=[]))
    cbuild.commit()
    code = create_DAGCode_with_init_and_main_phases(
        init_statements=[], main_statements=cbuild.statements)
    codegen = PythonCodeGenerator(class_name="Method")
    print(codegen(code))
    Method = codegen.get_class(code)  # noqa
    method = Method({})
    method.set_up(t_start=0, dt_start=0, context={})
    hist = [s for s in method.run(max_steps=2)]
    assert len(hist) == 3
    assert isinstance(hist[0], method.StepCompleted)
    assert hist[0].current_phase == "init"
    assert isinstance(hist[1], method.StateComputed)
    assert hist[1].state_component == 0
    assert isinstance(hist[2], method.StepCompleted)
    assert hist[2].current_phase == "main"
示例#2
0
def test_basic_conditional_codegen():
    """Test whether the code generator generates branches properly."""
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        Assign(id="then_branch",
               assignee="<state>y",
               assignee_subscript=(),
               expression=1,
               condition=True),
        Assign(id="else_branch",
               assignee="<state>y",
               assignee_subscript=(),
               expression=0,
               condition=False),
        Nop(id="branch", depends_on=["then_branch", "else_branch"]),
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=var("<state>y"),
                   component_id="<state>",
                   depends_on=["branch"]))
    cbuild.commit()
    code = create_DAGCode_with_init_and_main_phases(
        init_statements=[], main_statements=cbuild.statements)
    codegen = PythonCodeGenerator(class_name="Method")
    Method = codegen.get_class(code)  # noqa
    method = Method({})
    method.set_up(t_start=0, dt_start=0, context={"y": 6})
    hist = [s for s in method.run(max_steps=2)]
    assert len(hist) == 3
    assert isinstance(hist[1], method.StateComputed)
    assert hist[1].state_component == 1
    assert isinstance(hist[2], method.StepCompleted)
示例#3
0
def test_global_name_distinctness():
    """Test whether the code generator gives globals distinct names."""
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        Assign(id="assign_y^",
               assignee="<p>y^",
               assignee_subscript=(),
               expression=1),
        Assign(id="assign_y*",
               assignee="<p>y*",
               assignee_subscript=(),
               expression=0),
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=var("<p>y^") + var("<p>y*"),
                   component_id="y",
                   depends_on=["assign_y^", "assign_y*"]))
    cbuild.commit()
    code = create_DAGCode_with_init_and_main_phases(
        init_statements=[], main_statements=cbuild.statements)
    codegen = PythonCodeGenerator(class_name="Method")
    Method = codegen.get_class(code)  # noqa
    method = Method({})
    method.set_up(t_start=0, dt_start=0, context={})
    hist = list(method.run(max_steps=2))
    assert len(hist) == 3
    assert isinstance(hist[1], method.StateComputed)
    assert hist[1].state_component == 1
示例#4
0
def test_cond_detection():
    """Check that the code generator detects a redefinition of a <cond> variable."""
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        Assign(id="assign1",
               assignee="<cond>c",
               assignee_subscript=(),
               expression=1,
               depends_on=[]),
        Assign(id="assign2",
               assignee="<cond>c",
               assignee_subscript=(),
               expression=2,
               depends_on=["assign1"]),
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=1,
                   component_id="<state>",
                   depends_on=["assign2"]))
    cbuild.commit()
    code = create_DAGCode_with_init_and_main_phases(
        init_statements=[], main_statements=cbuild.statements)
    with pytest.raises(CodeGenerationError):
        verify_code(code)
示例#5
0
def test_circular_dependency_detection():
    """Check that the code generator detects that there is a circular
    dependency."""
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        Assign(id="assign",
               assignee="<state>y",
               assignee_subscript=(),
               expression=1,
               depends_on=["assign2"]),
        Assign(id="assign2",
               assignee="<state>y",
               assignee_subscript=(),
               expression=1,
               depends_on=["assign"]),
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=var("<state>y"),
                   component_id="<state>",
                   depends_on=["assign"]))
    cbuild.commit()
    code = create_DAGCode_with_init_and_main_phases(
        init_statements=[], main_statements=cbuild.statements)
    with pytest.raises(CodeGenerationError):
        verify_code(code)
示例#6
0
def test_isnan(python_method_impl, value):
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        Assign(id="assign_1", assignee="x", assignee_subscript=(),
                         expression=var("<builtin>isnan")(value)),
        YieldState(id="return", time=0, time_id="final",
                   expression=var("x"), component_id="<state>",
                   depends_on=["assign_1"]))
    cbuild.commit()
    code = create_DAGCode_with_steady_phase(cbuild.statements)

    result = execute_and_return_single_result(python_method_impl, code)
    assert result == np.isnan(value)
示例#7
0
def test_function_name_distinctness():
    """Test whether the code generator gives functions distinct names."""
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=var("<func>y^")() + var("<func>y*")(),
                   component_id="y"))
    cbuild.commit()
    code = create_DAGCode_with_init_and_main_phases(
        init_statements=[], main_statements=cbuild.statements)
    codegen = PythonCodeGenerator(class_name="Method")
    Method = codegen.get_class(code)  # noqa
    method = Method({"<func>y^": lambda: 0, "<func>y*": lambda: 1})
    method.set_up(t_start=0, dt_start=0, context={})
    hist = list(method.run(max_steps=2))
    assert len(hist) == 3
    assert isinstance(hist[1], method.StateComputed)
    assert hist[1].state_component == 1
示例#8
0
def test_missing_dependency_detection():
    """Check that the code generator detects that there is a missing
    dependency."""
    statements = {
        Assign(id="assign",
               assignee="<state>y",
               assignee_subscript=(),
               expression=1,
               depends_on=["assign2"]),
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=var("<state>y"),
                   component_id="<state>",
                   depends_on=["assign"])
    }
    code = create_DAGCode_with_init_and_main_phases(init_statements=[],
                                                    main_statements=statements)
    with pytest.raises(CodeGenerationError):
        verify_code(code)
示例#9
0
def test_basic_assign_rhs_codegen():
    """Test whether the code generator generates RHS evaluation code
    properly."""
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        Assign(id="assign_rhs1",
               assignee="<state>y",
               assignee_subscript=(),
               expression=var("y")(t=var("<t>")),
               depends_on=[]),
        Assign(id="assign_rhs2",
               assignee="<state>y",
               assignee_subscript=(),
               expression=var("yy")(t=var("<t>"), y=var("<state>y")),
               depends_on=["assign_rhs1"]),
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=var("<state>y"),
                   component_id="<state>",
                   depends_on=["assign_rhs2"]))
    cbuild.commit()
    code = create_DAGCode_with_init_and_main_phases(
        init_statements=[], main_statements=cbuild.statements)
    codegen = PythonCodeGenerator(class_name="Method")
    Method = codegen.get_class(code)  # noqa

    def y(t):
        return 6

    def yy(t, y):
        return y + 6

    method = Method({"y": y, "yy": yy})
    method.set_up(t_start=0, dt_start=0, context={"y": 0})
    hist = [s for s in method.run(max_steps=2)]
    assert len(hist) == 3
    assert isinstance(hist[1], method.StateComputed)
    assert hist[1].state_component == 12
    assert isinstance(hist[2], method.StepCompleted)
示例#10
0
def test_basic_codegen():
    """Test whether the code generator returns a working method. The
    generated method always returns 0."""
    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        YieldState(id="return",
                   time=0,
                   time_id="final",
                   expression=0,
                   component_id="state",
                   depends_on=[]))
    cbuild.commit()
    code = create_DAGCode_with_steady_phase(cbuild.statements)
    codegen = f.CodeGenerator("simple",
                              user_type_map={
                                  "state":
                                  f.ArrayType(
                                      (200, ),
                                      f.BuiltinType("real (kind=8)"),
                                  )
                              })
    print(codegen(code))
示例#11
0
def test_norm(python_method_impl, order, norm_suffix, test_vector):

    def true_norm(x):
        if np.isscalar(x):
            return abs(x)
        return np.linalg.norm(x, ord=order)

    cbuild = RawCodeBuilder()
    cbuild.add_and_get_ids(
        Assign(id="assign_1", assignee="x", assignee_subscript=(),
                         expression=test_vector),
        Assign(id="assign_2", assignee="n", assignee_subscript=(),
                         expression=(
                             var("<builtin>norm_%s" % norm_suffix)(var("x"))),
                         depends_on=["assign_1"]),
        YieldState(id="return", time=0, time_id="final",
                   expression=var("n"), component_id="<state>",
                   depends_on=["assign_2"]))
    cbuild.commit()
    code = create_DAGCode_with_steady_phase(cbuild.statements)

    result = execute_and_return_single_result(python_method_impl, code)
    assert np.allclose(result, true_norm(test_vector))