Exemple #1
0
 def test_mock_grid_failures(self):
     with pytest.raises(TypeError):
         MockGrid(grid_attrs="foo")
     with pytest.raises(TypeError):
         MockGrid(grid_attrs={1: "foo"})
     with pytest.raises(ValueError):
         MockGrid(grid_attrs={"foo": "bar"})
Exemple #2
0
    def test_emissions_summarization(self, mock_pg, mock_plant):
        # setup
        pg = pd.DataFrame(mock_pg).iloc[:3, :]
        plant = pd.DataFrame(mock_plant)
        plant.set_index("plant_id", inplace=True)
        input_carbon_values = [
            [0, 0, 6.6998, 13.546000, 11.8475],
            [0, 0, 9.4472, 21.1873333, 20.3100],
            [0, 0, 13.0622, 31.6073333, 32.1575],
        ]
        input_carbon = pd.DataFrame(
            input_carbon_values, index=pg.index, columns=pg.columns
        )
        expected_sum = {
            "coal": {1004: 66.3406666},
            "ng": {1003: 29.2092},
            "dfo": {1005: 64.315},
        }

        # calculation
        summation = summarize_emissions_by_bus(
            input_carbon, MockGrid(grid_attrs={"plant": mock_plant})
        )

        # checks
        err_msg = "summarize_emissions_by_bus didn't return a dict"
        assert isinstance(summation, dict), err_msg
        err_msg = "summarize_emissions_by_bus didn't return the right dict keys"
        assert set(summation.keys()) == expected_sum.keys(), err_msg
        for k in expected_sum.keys():
            err_msg = "summation not correct for fuel " + k
            assert expected_sum[k].keys() == summation[k].keys(), err_msg
            for bus in expected_sum[k]:
                err_msg = "summation not correct for bus " + str(bus)
                assert expected_sum[k][bus] == pytest.approx(summation[k][bus]), err_msg
Exemple #3
0
 def __init__(
     self,
     grid_attrs=None,
     congl=None,
     congu=None,
     ct=None,
     demand=None,
     bus_demand=None,
     lmp=None,
     pf=None,
     pg=None,
     dcline_pf=None,
     storage_e=None,
     storage_pg=None,
     solar=None,
     wind=None,
     hydro=None,
 ):
     """Constructor."""
     self.grid = MockGrid(grid_attrs)
     self.congl = _ensure_ts_index(congl)
     self.congu = _ensure_ts_index(congu)
     self.ct = ct if ct is not None else {}
     self.demand = _ensure_ts_index(demand)
     self.bus_demand = _ensure_ts_index(bus_demand)
     self.lmp = _ensure_ts_index(lmp)
     self.pf = _ensure_ts_index(pf)
     self.dcline_pf = _ensure_ts_index(dcline_pf)
     self.pg = _ensure_ts_index(pg)
     self.storage_e = _ensure_ts_index(storage_e)
     self.storage_pg = _ensure_ts_index(storage_pg)
     self.solar = _ensure_ts_index(solar)
     self.wind = _ensure_ts_index(wind)
     self.hydro = _ensure_ts_index(hydro)
     self.name = "analyze"
Exemple #4
0
 def test_mock_grid_successes(self):
     grid = MockGrid(grid_attrs={"plant": mock_plant})
     assert isinstance(grid, object), "MockGrid should return an object"
     assert hasattr(grid,
                    "plant"), "Plant property should be in the MockGrid"
     assert len(grid.branch
                ) == 0, "Branch dataframe should be empty in the MockGrid"
Exemple #5
0
    def setUp(self):
        def check_expected(upgrades, expected_interstate, expected_intrastate):
            err_msg = "classify_interstate_intrastate should return a dict"
            self.assertIsInstance(upgrades, dict, err_msg)
            err_msg = "dict keys should be 'interstate' and 'intrastate'"
            self.assertEqual(upgrades.keys(), expected_keys, err_msg)
            for v in upgrades.values():
                self.assertIsInstance(v, list, "dict values should be lists")
                for b in v:
                    self.assertIsInstance(b, int, "branch_ids should be ints")
            err_msg = "interstate values not as expected"
            self.assertEqual(set(upgrades["interstate"]), expected_interstate,
                             err_msg)
            err_msg = "intrastate values not as expected"
            self.assertEqual(set(upgrades["intrastate"]), expected_intrastate,
                             err_msg)

        self.check_expected = check_expected
        self.mock_grid = MockGrid(grid_attrs={"branch": mock_branch})
Exemple #6
0
    def __init__(
        self,
        grid_attrs=None,
        congl=None,
        congu=None,
        ct=None,
        demand=None,
        lmp=None,
        pg=None,
        storage_pg=None,
        solar=None,
        wind=None,
        hydro=None,
    ):
        """Constructor.

        :param dict grid_attrs: fields to be added to grid.
        :param pandas.DataFrame congl: dummy congl
        :param pandas.DataFrame congu: dummy congu
        :param dict ct: dummy ct
        :param pandas.DataFrame demand: dummy demand
        :param pandas.DataFrame lmp: dummy lmp
        :param pandas.DataFrame pg: dummy pg
        :param pandas.DataFrame storage_pg: dummy storage_pg
        :param pandas.DataFrame solar: dummy solar
        :param pandas.DataFrame wind: dummy wind
        :param pandas.DataFrame hydro: dummy hydro
        """
        self.grid = MockGrid(grid_attrs)
        self.congl = _ensure_ts_index(congl)
        self.congu = _ensure_ts_index(congu)
        self.ct = ct if ct is not None else {}
        self.demand = _ensure_ts_index(demand)
        self.lmp = _ensure_ts_index(lmp)
        self.pg = _ensure_ts_index(pg)
        self.storage_pg = _ensure_ts_index(storage_pg)
        self.solar = _ensure_ts_index(solar)
        self.wind = _ensure_ts_index(wind)
        self.hydro = _ensure_ts_index(hydro)
        self.name = "analyze"
Exemple #7
0
def test_calculate_substation_capacity():
    mock_sub = {"sub_id": [1, 2, 3, 4]}
    mock_bus2sub = {"bus_id": [10, 20, 21, 30, 40], "sub_id": [1, 2, 2, 3, 4]}
    mock_branch = {
        "branch_id": [200, 400, 420, 600, 601, 1200],
        "from_bus_id": [10, 40, 20, 20, 30, 30],
        "to_bus_id": [20, 10, 21, 30, 20, 40],
        "rateA": [1, 2, 4, 8, 16, 32],
    }

    mock_grid_data = {
        "sub": mock_sub,
        "bus2sub": mock_bus2sub,
        "branch": mock_branch,
    }
    mock_grid = MockGrid(grid_attrs=mock_grid_data)
    substation_capacity = calculate_substation_capacity(mock_grid)
    expected_return = pd.Series({
        1: 3,
        2: 25,
        3: 56,
        4: 34,
    })
    assert substation_capacity.equals(expected_return)
 def setUp(self):
     self.grid = MockGrid(grid_attrs)
def test_get_profile_by_state():
    mock_grid = MockGrid(grid_attrs={"plant": mock_plant_df})
    assert mock_plant_profile.equals(
        get_profile_by_state(mock_total_profile, "CA", mock_grid))
        1100,
        1100,
        1200,
        1400,
        1500,
        1300,
        1100,
        1200,
        1900,
    ],
    "interconnect": ["Western"] * 20,
}

grid_attrs = {"plant": mock_plant, "gencost_before": mock_gencost}

grid = MockGrid(grid_attrs)
grid.interconnect = "Western"
grid.zone2id = {"Utah": 210, "Colorado": 212, "Washington": 201}


def test_get_supply_data():
    supply_df = get_supply_data(grid, 1)
    test_slope = supply_df["slope1"]
    exp_slope = pd.Series(
        [
            31.25,
            30.20,
            40.00,
            30.10,
            25.20,
            25.10,
Exemple #11
0
        "Washington",
        "Washington",
        "Oregon",
    ],
}

mock_plant = {
    "plant_id": ["A", "B", "C", "D", "E", "F", "G"],
    "bus_id": [1, 1, 5, 7, 7, 7, 8],
    "type": ["solar", "coal", "wind", "solar", "solar", "ng", "wind"],
    "Pmax": [15, 30, 10, 12, 8, 20, 15],
}

mock_grid = MockGrid(grid_attrs={
    "branch": mock_branch,
    "bus": mock_bus,
    "plant": mock_plant
})
mock_grid.get_grid_model = lambda: "usa_tamu"


class TestStubTopologyHelpers(unittest.TestCase):
    def setUp(self):
        self.branch = mock_grid.branch
        self.plant = mock_grid.plant

    def test_find_branches_connected_to_bus_1(self):
        branches_connected = _find_branches_connected_to_bus(self.branch, 1)
        self.assertEqual(branches_connected, {101, 106})

    def test_find_branches_connected_to_bus_4(self):
def mock_grid():
    return MockGrid(grid_attrs)
        1100,
        1100,
        1200,
        1400,
        1500,
        1300,
        1100,
        1200,
        1900,
    ],
    "interconnect": ["Western"] * 20,
}

grid_attrs = {"plant": mock_plant, "gencost_before": mock_gencost}

grid = MockGrid(grid_attrs)
grid.interconnect = "Western"
grid.zone2id = {"Utah": 210, "Colorado": 212, "Washington": 201}
grid.get_grid_model = lambda: "usa_tamu"


def test_get_supply_data():
    supply_df = get_supply_data(grid, 1)
    test_slope = supply_df["slope1"]
    exp_slope = pd.Series(
        [
            31.25,
            30.20,
            40.00,
            30.10,
            25.20,
Exemple #14
0
 def setUp(self):
     self.grid = MockGrid(grid_attrs={"branch": mock_branch})
        "Washington",
        "Washington",
        "Oregon",
    ],
}

mock_plant = {
    "plant_id": ["A", "B", "C", "D", "E", "F", "G"],
    "bus_id": [1, 1, 5, 7, 7, 7, 8],
    "type": ["solar", "coal", "wind", "solar", "solar", "ng", "wind"],
    "Pmax": [15, 30, 10, 12, 8, 20, 15],
}

mock_grid = MockGrid(grid_attrs={
    "branch": mock_branch,
    "bus": mock_bus,
    "plant": mock_plant
})


class TestStubTopologyHelpers(unittest.TestCase):
    def setUp(self):
        self.branch = mock_grid.branch
        self.plant = mock_grid.plant

    def test_find_branches_connected_to_bus_1(self):
        branches_connected = _find_branches_connected_to_bus(self.branch, 1)
        self.assertEqual(branches_connected, {101, 106})

    def test_find_branches_connected_to_bus_4(self):
        branches_connected = _find_branches_connected_to_bus(self.branch, 4)
Exemple #16
0
 def grid(self):
     grid = MockGrid(grid_attrs={"plant": mock_plant})
     return grid