コード例 #1
0
def test_model_without_update(ms3):
    m1 = Model(ms3)
    n_m = m1.generate_compiled_model(0, 1)
    assert n_m.path_variables["S3_nm.1.t1.R"] == 10
    assert n_m.path_variables["S3_nm.2.t1.R"] == 5
    assert n_m.path_variables["S3_nm.3.t1.R"] == 3
    assert n_m.path_variables["S3_nm.4.t1.R"] == 2
コード例 #2
0
def test_model_with_update(ms3):
    m1 = Model(ms3)
    n_m = m1.generate_numba_model(0, 1)
    n_m.run_callbacks_with_updates(0.1)
    assert n_m.path_variables["S3_nm.1.t1.R"] == 10
    assert n_m.path_variables["S3_nm.2.t1.R"] == 5
    assert n_m.path_variables["S3_nm.3.t1.R"] == 3
    assert n_m.path_variables["S3_nm.4.t1.R"] == 2
コード例 #3
0
ファイル: test_mapping_order.py プロジェクト: amipy/numerous
def test_deriv_order(solver, use_llvm):
    m = Main()
    model = Model(m, use_llvm=use_llvm)
    import numpy as np
    expected = np.array([2.5, 3., 1., 1.])
    assert approx(model.compiled_compute(np.array([0., 0., 0.,
                                                   0.]))) == expected
    expected_2 = [3.3, 3.6, 1.6, 1.4]
    assert approx(model.compiled_compute(np.array([1., 2., 3.,
                                                   4.]))) == expected_2
コード例 #4
0
def test_start_stop_model(filename, solver):
    def stop_callback(t, _):
        if t > 1:
            raise ValueError("Overflow of state2")

    hdf = HistoryDataFrame()
    m1 = Model(S3('S3_sl'), historian=hdf)

    c1 = _SimulationCallback("test")
    c1.add_callback_function(stop_callback)
    m1.callbacks.append(c1)
    m1.save_variables_schedule(0.1, filename)

    s1 = Simulation(m1, t_start=0, t_stop=2, num=100, solver_type=solver)
    with pytest.raises(ValueError, match=r".*Overflow of state2.*"):
        s1.solve()
    hdf2 = HistoryDataFrame.load(filename)
    m2 = Model(S3('S3_sl2'), historian=hdf2)
    m2.restore_state()

    assert approx(m2.states_as_vector, rel=0.1) == [89.8, 3.9, 0.7, 3.3]

    s2 = Simulation(m2, t_start=0, t_stop=1, num=50, solver_type=solver)
    s2.solve()
    m3 = Model(S3('S3_sl3'), historian=hdf)

    s3 = Simulation(m3, t_start=0, t_stop=2, num=100, solver_type=solver)
    s3.solve()
    print(m3.info)
    assert approx(m3.states_as_vector, rel=0.1) == m2.states_as_vector
コード例 #5
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_model_save_only_aliases2(ms3, solver):
    of = OutputFilter(only_aliases=True)
    m1 = Model(ms3, historian_filter=of)
    item = m1.search_items('2')[0]
    columns_number = 0
    for i, var in enumerate(item.get_variables()):
        var[0].alias = str(i)
        columns_number += 1

    s1 = Simulation(m1, t_start=0, t_stop=1000, num=10, solver_type=solver)
    s1.solve()
    assert m1.historian_df.columns.size == columns_number
コード例 #6
0
def test_var_not_used(ms4, solver, use_llvm):
    m1 = Model(ms4, use_llvm=use_llvm)
    s1 = Simulation(m1, t_start=0, t_stop=100, num=10, solver_type=solver)
    s1.solve()
    df = s1.model.historian_df
    assert approx(np.array(df['S1_var.test_item.t1.P2'])) == np.repeat(
        11, (11))
コード例 #7
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_chain_item_binding_model_nested(ms3, solver, use_llvm):
    ms4 = Subsystem('new_s')
    ms4.register_item(ms3)
    m1 = Model(ms4, use_llvm=use_llvm)
    s1 = Simulation(m1, t_start=0, t_stop=1000, num=10, solver_type=solver)
    s1.solve()
    assert approx(m1.states_as_vector, rel=0.01) == [2010, 1010, 510, 210]
コード例 #8
0
def test_overloadaction_sum_multiple(system2, solver, use_llvm):
    model = Model(system2, use_llvm=use_llvm)
    sim = Simulation(model, t_start=0, t_stop=10, num=100, solver_type=solver)
    sim.solve()
    df = sim.model.historian_df
    assert approx(np.array(
        df['system2_overload.subsystem1.item1.t1.x'])) == expected_sol(
            np.linspace(0, 10, 101))
コード例 #9
0
def test_system_link_1_5(system15, solver, use_llvm):
    model = Model(system15, use_llvm=use_llvm)

    sim = Simulation(model, t_start=0, t_stop=100, num=200, solver_type=solver)

    sim.solve()
    df = sim.model.historian_df

    assert approx(np.array(df['root_linktest.linkersubsystem_4.item_0.t1.x'])[1:]) == \
           expected(len(df.index[:-1]), 5, 0.9)
コード例 #10
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_callback_step_item_model(ms1, solver):
    class SimpleCallback(NumbaCallbackBase):
        def finalize(self, var_list):
            pass

        @NumbaCallback(method_type=CallbackMethodType.INITIALIZE)
        def initialize(self, var_count, number_of_timesteps):
            pass

        @NumbaCallback(method_type=CallbackMethodType.UPDATE,
                       run_after_init=True)
        def update(self, time, variables):
            if variables['S1.test_item.t1.T2'] > 1000:
                raise ValueError("Overflow of state2")

    m1 = Model(ms1)
    m1.add_callback(SimpleCallback())
    s1 = Simulation(m1, t_start=0, t_stop=1000, num=100, solver_type=solver)
    with pytest.raises(ValueError, match=r".*Overflow of state2.*"):
        s1.solve()
コード例 #11
0
def test_tuple_to_var_assign(solver, use_llvm):
    m = System(tag='system3', item=TupleToVarAssign(tag='item3'))
    model = Model(m, use_llvm=use_llvm, generate_graph_pdf=False)
    s = simulation.Simulation(model,
                              solver_type=solver,
                              t_start=0,
                              t_stop=2.0,
                              num=2,
                              num_inner=2)
    s.solve()
    historian_df = s.model.historian_df
    assert approx(historian_df['system3.item3.t1.x'][2]) == 2
コード例 #12
0
def test_single_unary_operator(solver, use_llvm):
    m = System(tag='system6', item=SingleUnaryOperator(tag='item6'))
    model = Model(m, use_llvm=use_llvm, generate_graph_pdf=False)
    s = simulation.Simulation(model,
                              solver_type=solver,
                              t_start=0,
                              t_stop=2.0,
                              num=2,
                              num_inner=2)
    s.solve()
    historian_df = s.model.historian_df
    assert approx(historian_df['system6.item6.t1.x'][2]) == 2
コード例 #13
0
def test_llvm_diff_function_simple(ms1):
    m1 = Model(ms1, use_llvm=True)
    cm1 = m1.generate_compiled_model(0, 100)
    m2 = Model(ms1, use_llvm=False)
    cm2 = m2.generate_compiled_model(0, 100)

    assert approx(cm1.func(0, np.array([1.0, 1.0, 1.0, 1.0])), rel=0.01) == cm2.func(0, np.array([1.0, 1.0, 1.0, 1.0]))
コード例 #14
0
def test_system_link_Success2(solver, use_llvm):
    N_inner = 5
    N_outer = 2
    system = Success2(N_outer=N_outer, N_inner=N_inner)
    model = Model(system, use_llvm=use_llvm)

    sim = Simulation(model, t_start=0, t_stop=100, num=200, solver_type=solver)

    sim.solve()
    df = sim.model.historian_df

    assert approx(np.array(df['doesnotwork_deeplink.inlet_2.item_5.item_4.item_3.item_2.item_1.boundary_deeplink.t1.x'])[1:], rel=1) == \
           expected(len(df.index[:-1]), (N_outer-1)*N_inner, 0.9)
コード例 #15
0
def test_race_condition_1(solver, use_llvm):
    omega0 = 0.01
    dt = 10
    s1 = System(item=Link(item1=Item1(omega=omega0)), tag='system_race_1')
    s2 = System(item=Item2(omega=omega0), tag='system_race_2')

    m1 = Model(s1, use_llvm=use_llvm)
    sim1 = Simulation(m1, max_step=dt, num=500, solver_type=solver)

    sim1.solve()

    df1 = sim1.model.historian_df

    m2 = Model(s2, use_llvm=use_llvm)
    sim2 = Simulation(m2, max_step=dt, num=500, solver_type=solver)
    sim2.solve()
    df2 = sim2.model.historian_df

    assert np.all(
        np.isclose(np.array(df1['system_race_1.link.t1.S'])[2:],
                   np.array(df2['system_race_2.item2.t1.S'][2:]),
                   rtol=1e-02,
                   atol=1e-04))
コード例 #16
0
def test_tuple_to_func_assign(solver, use_llvm):
    m = System(tag='system4', item=TupleToFuncAssign(tag='item4'))
    model = Model(m,
                  use_llvm=use_llvm,
                  generate_graph_pdf=False,
                  imports=[("external_functions", "if_replacement")])
    s = simulation.Simulation(model,
                              solver_type=solver,
                              t_start=0,
                              t_stop=2.0,
                              num=2,
                              num_inner=2)
    s.solve()
    historian_df = s.model.historian_df
    assert approx(historian_df['system4.item4.t1.x'][2]) == 2
コード例 #17
0
def test_single_unary_operator_local_variables_are_not_shared(
        solver, use_llvm):
    m = System(tag='system7', item=SingleUnaryOperator(tag='item7', t=1))
    m.register_item(SingleUnaryOperator(tag='item8', t=-3))
    model = Model(m, use_llvm=use_llvm, generate_graph_pdf=False)
    s = simulation.Simulation(model,
                              solver_type=solver,
                              t_start=0,
                              t_stop=2.0,
                              num=2,
                              num_inner=2)
    s.solve()
    historian_df = s.model.historian_df
    assert approx(historian_df['system7.item7.t1.x'][2]) == 4
    assert approx(historian_df['system7.item8.t1.x'][2]) == 12
コード例 #18
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_chain_item_binding_model_nested2(ms3, solver, use_llvm):
    ms4 = Subsystem('new_s4')
    ms4.register_item(ms3)
    ms5 = Subsystem('new_s5')
    ms5.register_item(ms3)
    ms6 = Subsystem('new_s6')
    ms6.register_item(ms4)
    ms6.register_item(ms5)
    ms7 = Subsystem('new_s7')
    ms7.register_item(ms6)
    m1 = Model(ms7, use_llvm=use_llvm)
    s1 = Simulation(m1, t_start=0, t_stop=1000, num=100, solver_type=solver)
    s1.solve()
    assert len(m1.path_variables) == 50
    assert len(m1.variables) == 25
    assert approx(m1.states_as_vector, rel=0.01) == [2010, 1010, 510, 210]
コード例 #19
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_external_data(solver, use_llvm):
    external_mappings = []

    import pandas as pd
    import numpy as np

    data = {
        'time': np.arange(100),
        'Dew Point Temperature {C}': np.arange(100) + 1,
        'Dry Bulb Temperature {C}': np.arange(100) + 2,
    }

    df = pd.DataFrame(data,
                      columns=[
                          'time', 'Dew Point Temperature {C}',
                          'Dry Bulb Temperature {C}'
                      ])
    index_to_timestep_mapping = 'time'
    index_to_timestep_mapping_start = 0
    dataframe_aliases = {
        'system_external.tm0.test_nm.T1':
        ("Dew Point Temperature {C}", InterpolationType.PIESEWISE),
        'system_external.tm0.test_nm.T2':
        ('Dry Bulb Temperature {C}', InterpolationType.PIESEWISE)
    }
    external_mappings.append(
        ExternalMappingElement("inmemory", index_to_timestep_mapping,
                               index_to_timestep_mapping_start, 1,
                               dataframe_aliases))
    data_loader = InMemoryDataLoader(df)
    s = Simulation(Model(StaticDataSystem('system_external', n=1),
                         use_llvm=use_llvm,
                         external_mappings=external_mappings,
                         data_loader=data_loader),
                   t_start=0,
                   t_stop=100.0,
                   num=100,
                   num_inner=100,
                   max_step=.1,
                   solver_type=solver)
    s.solve()
    assert approx(
        np.array(s.model.historian_df['system_external.tm0.test_nm.T_i1'])
        [1:]) == np.arange(101)[1:]
    assert approx(
        np.array(s.model.historian_df['system_external.tm0.test_nm.T_i2'])
        [1:]) == np.arange(101)[1:] + 1
コード例 #20
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_static_system(solver, use_llvm):
    import numpy as np
    s = Simulation(Model(StaticDataSystem('system_static', n=1),
                         use_llvm=use_llvm),
                   t_start=0,
                   t_stop=100.0,
                   num=100,
                   num_inner=100,
                   max_step=.1,
                   solver_type=solver)
    s.solve()
    assert approx(
        np.array(s.model.historian_df['system_static.tm0.test_nm.T_i1'])
        [1:]) == np.repeat(0, (100))
    assert approx(
        np.array(s.model.historian_df['system_static.tm0.test_nm.T_i2'])
        [1:]) == np.repeat(0, (100))
コード例 #21
0
def test_logger_levels(solver, use_llvm):
    num = 100
    t_stop = 100
    t_start = 0
    sys = TestLogSubsystem1()
    model = Model(sys, logger_level=ALL, use_llvm=use_llvm)
    tvec = np.linspace(t_start, t_stop, num + 1, dtype=np.float64)
    sim = Simulation(model, t_start=t_start, t_stop=t_stop, num=num, num_inner=1, solver_type=solver,
                     rtol=1e-8, atol=1e-8)
    sim.solve()

    df = sim.model.historian_df

    s_analytic = sigmoidlike(tvec)

    prefix = 'testlogsubsystem1.testlogitem1.t1'
    p = f"{prefix}.p"
    v = f"{prefix}.v"
    s = f"{prefix}.s"

    expected_results = {v: tvec, p: np.ones(num + 1), s: s_analytic}

    for k, v in expected_results.items():
        assert pytest.approx(v, abs=1e-5) == df.get(k), "expected results do not match actual results"
コード例 #22
0
def test_single_equation_multiple_variables(ms1, solver, use_llvm):
    m1 = Model(ms1, use_llvm=use_llvm)
    s1 = Simulation(m1, t_start=0, t_stop=1000, num=100, solver_type=solver)
    s1.solve()
    assert m1.historian_df["S1_inh.test1.t1.P"][100] == 5
    assert m1.historian_df["S1_inh.test1.t1.T"][100] == 50
コード例 #23
0
def test_equation_inheritence_3(ms3):
    m1 = Model(ms3)
    assert len(m1.compiled_eq) == 1
コード例 #24
0
        self.t1.add_equations([self])

    @Equation()
    def eval(self, scope):
        scope.x_dot = 0.1


class System(Subsystem):
    def __init__(self, tag='sys'):
        super().__init__(tag)
        i = SuperSimpleStepTest()
        self.register_item(i)


sys = System()
model = Model(sys)
t_stop = 100
sim = Simulation(model,
                 t_start=0,
                 t_stop=t_stop,
                 num=50,
                 max_step=np.inf,
                 method='RK45',
                 solver_type=SolverType.NUMEROUS)

finished = False
dt = 2
while not finished:
    t, info = sim.step_solve(dt)
    print(t)
    if t >= t_stop:
コード例 #25
0
def test_equation_inheritence_2(ms2, solver):
    m1 = Model(ms2)
    s1 = Simulation(m1, t_start=0, t_stop=1000, num=100, solver_type=solver)
    s1.solve()
    assert m1.historian_df["S2_inh.test1.t1.P"][100] == 5
    assert m1.historian_df["S2_inh.test1.t1.L"][100] == 7
コード例 #26
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_model_var_referencing(ms1, solver, use_llvm):
    m1 = Model(ms1, use_llvm=use_llvm)
    s1 = Simulation(m1, t_start=0, t_stop=1000, num=10, solver_type=solver)
    s1.solve()
    assert approx(list(m1.states_as_vector[::-1]),
                  rel=0.01) == [2010, 1010, 510, 210]
コード例 #27
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_chain_item_binding_model(ms3, solver, use_llvm):
    m1 = Model(ms3, use_llvm=use_llvm)
    s1 = Simulation(m1, t_start=0, t_stop=1000, num=100, solver_type=solver)
    s1.solve()
    assert approx(m1.states_as_vector, rel=0.01) == [2010, 1010, 510, 210]
コード例 #28
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_model_save_only_aliases(ms3, solver):
    of = OutputFilter(only_aliases=True)
    m1 = Model(ms3, historian_filter=of)
    s1 = Simulation(m1, t_start=0, t_stop=1000, num=10, solver_type=solver)
    s1.solve()
    assert m1.historian_df.empty
コード例 #29
0
ファイル: test_model.py プロジェクト: fossilfree/numerous
def test_1_item_model(ms1):
    m1 = Model(ms1)
    item = m1.search_items('test_item')[0]
    assert item.t1.P.value == 100