Example #1
0
    def test_reactor_engine_copies(self):
        """Do deepcopy, pickling and repr of ReactorEngine work as expected?"""

        orig = engines.ReactorEngine(
            nsteps=101,
            Tinj=300.0,
            rxnmech="dodecane_lu_nox.cti",
            mdot=0.1,
            max_minj=5e-5,
            reward=rw.Reward(negative_reward=-0.05),
        )
        assert_deepcopy_pickle_repr(orig)
Example #2
0
        negative_reward=rwd_params["negative_reward"].value,
        EOC_reward=rwd_params["EOC_reward"].value,
        randomize=rwd_params["randomize"].value,
        random_updates=rwd_params["random_updates"].value,
    )

    # Initialize the engine
    eng_params = params.inputs["engine"]
    if eng_params["engine"].value == "reactor-engine":
        eng = engines.ReactorEngine(
            Tinj=eng_params["Tinj"].value,
            nsteps=eng_params["nsteps"].value,
            mdot=eng_params["mdot"].value,
            max_minj=eng_params["max_minj"].value,
            injection_delay=eng_params["injection_delay"].value,
            max_pressure=eng_params["max_pressure"].value,
            ename=eng_params["ename"].value,
            reward=reward,
            fuel=eng_params["fuel"].value,
            rxnmech=eng_params["rxnmech"].value,
            observables=eng_params["observables"].value,
            use_qdot=eng_params["use_qdot"].value,
        )
    elif eng_params["engine"].value == "EQ-engine":
        eng = engines.EquilibrateEngine(
            Tinj=eng_params["Tinj"].value,
            nsteps=eng_params["nsteps"].value,
            mdot=eng_params["mdot"].value,
            max_minj=eng_params["max_minj"].value,
            injection_delay=eng_params["injection_delay"].value,
            max_pressure=eng_params["max_pressure"].value,
            ename=eng_params["ename"].value,
Example #3
0
    def test_reactor_engine_with_complex_reward(self):
        """Does the ReactorEngine with complex reward work as expected?"""

        # Initialize engine
        reward = rw.Reward(
            names=["work", "nox", "soot"],
            norms=[1.0, 5e-8, 1e-9],
            weights=[0.34, 0.33, 0.33],
            negative_reward=-100.0,
            randomize=False,
        )
        eng = engines.ReactorEngine(
            nsteps=101,
            Tinj=300.0,
            rxnmech="dodecane_lu_nox.cti",
            mdot=0.1,
            max_minj=5e-5,
            ename="Isooctane_MBT_DI_50C_Summ.xlsx",
            reward=reward,
        )
        env = DummyVecEnv([lambda: eng])
        variables = eng.observables + eng.internals + eng.histories
        df = pd.DataFrame(
            columns=list(
                dict.fromkeys(
                    variables
                    + eng.action.actions
                    + ["rewards"]
                    + eng.reward.get_rewards()
                )
            )
        )

        # Evaluate a dummy agent that injects at a fixed time
        t0 = time.time()
        done = False
        cnt = 0
        obs = env.reset()
        df.loc[cnt, variables] = [eng.current_state[k] for k in variables]
        df.loc[cnt, eng.action.actions] = 0
        rwd = list(
            eng.reward.compute(eng.current_state, eng.nsteps, False, False).values()
        )
        df.loc[cnt, eng.reward.get_rewards()] = rwd
        df.loc[cnt, ["rewards"]] = [sum(rwd)]

        while not done:
            cnt += 1
            # Agent tries to inject twice, but is not allowed the second time
            action = (
                [1]
                if (eng.current_state["ca"] == 0) or eng.current_state["ca"] == 2
                else [0]
            )
            obs, reward, done, info = env.step(action)
            df.loc[cnt, variables] = [info[0]["current_state"][k] for k in variables]
            df.loc[cnt, eng.action.actions] = eng.action.current
            df.loc[cnt, ["rewards"]] = reward
            df.loc[cnt, eng.reward.get_rewards()] = list(info[0]["rewards"].values())

        for rwd in eng.reward.get_rewards() + ["rewards"]:
            df[f"cumulative_{rwd}"] = np.cumsum(df[rwd])

        elapsed = time.time() - t0

        utilities.plot_df(env, df, idx=6, name="reactor")

        # Test
        npt.assert_allclose(np.linalg.norm(df.V), 0.002205916821815495)
        npt.assert_allclose(np.linalg.norm(df.p), 34254670.52877185, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df["T"]), 18668.46491609, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.rewards), 54.47632708, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.r_work), 53.47224436, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.r_nox), 14.10312665, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.w_work), 3.41695771, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.w_nox), 3.31645895, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.w_soot), 3.31645895, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.mdot), 0.14142135623730953)
        print(f"Wall time for ReactorEngine (complex reward) = {elapsed} seconds")
Example #4
0
    def test_reactor_engine(self):
        """Does the ReactorEngine work as expected?"""

        # Initialize engine
        eng = engines.ReactorEngine(
            nsteps=101,
            Tinj=300.0,
            rxnmech="dodecane_lu_nox.cti",
            mdot=0.1,
            max_minj=5e-5,
            ename="Isooctane_MBT_DI_50C_Summ.xlsx",
            reward=rw.Reward(negative_reward=-0.05),
        )
        env = DummyVecEnv([lambda: eng])
        variables = eng.observables + eng.internals + eng.histories
        df = pd.DataFrame(
            columns=list(
                dict.fromkeys(
                    variables
                    + eng.action.actions
                    + ["rewards"]
                    + eng.reward.get_rewards()
                )
            )
        )

        # Evaluate a dummy agent that injects at a fixed time
        t0 = time.time()
        done = False
        cnt = 0
        obs = env.reset()
        df.loc[cnt, variables] = [eng.current_state[k] for k in variables]
        df.loc[cnt, eng.action.actions] = 0
        rwd = list(
            eng.reward.compute(eng.current_state, eng.nsteps, False, False).values()
        )
        df.loc[cnt, eng.reward.get_rewards()] = rwd
        df.loc[cnt, ["rewards"]] = [sum(rwd)]

        while not done:
            cnt += 1
            # Agent tries to inject twice, but is not allowed the second time
            action = (
                [1]
                if (eng.current_state["ca"] == -10) or eng.current_state["ca"] == 10
                else [0]
            )
            obs, reward, done, info = env.step(action)
            df.loc[cnt, variables] = [info[0]["current_state"][k] for k in variables]
            df.loc[cnt, eng.action.actions] = eng.action.current
            df.loc[cnt, ["rewards"]] = reward
            df.loc[cnt, eng.reward.get_rewards()] = list(info[0]["rewards"].values())

        for rwd in eng.reward.get_rewards() + ["rewards"]:
            df[f"cumulative_{rwd}"] = np.cumsum(df[rwd])

        elapsed = time.time() - t0

        utilities.plot_df(env, df, idx=3, name="reactor")

        # Test
        npt.assert_allclose(np.linalg.norm(df.V), 0.002205916821815495)
        npt.assert_allclose(np.linalg.norm(df.p), 35131769.27712808, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df["T"]), 18843.86576749, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.rewards), 156.55524985, rtol=1e-5)
        npt.assert_allclose(np.linalg.norm(df.mdot), 0.14142135623730953)
        print(f"Wall time for ReactorEngine = {elapsed} seconds")