Ejemplo n.º 1
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))
Ejemplo n.º 2
0
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]
Ejemplo n.º 3
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))
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
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
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
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]
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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))
Ejemplo n.º 10
0
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()
Ejemplo n.º 11
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"
Ejemplo n.º 12
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))
Ejemplo n.º 13
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
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0

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:
        finished = True

sim.model.create_historian_df()

df = sim.model.historian_df
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
Ejemplo n.º 17
0
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]
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
Ejemplo n.º 19
0
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]