예제 #1
0
 def test_bad_scenario_state(self):
     mock_plant = {
         "plant_id": ["A", "B", "C", "D"],
         "ramp_30": [2.5, 5, 10, 25],
     }
     mock_scenario = MockScenario({"plant": mock_plant})
     mock_scenario.state = "Create"
     with self.assertRaises(ValueError):
         _check_scenario_is_in_analyze_state(mock_scenario)
예제 #2
0
def test_get_net_demand_profile_argument_value():
    scenario_create = MockScenario()
    scenario_create.state = "Create"
    arg = (
        ("Spain", scenario, None),
        ("WA", scenario_create, None),
        ("CA", None, None),
    )
    for a in arg:
        with pytest.raises(ValueError):
            get_net_demand_profile(a[0], a[1], a[2])
예제 #3
0
 def test_mock_profile(self, mock_hydro, mock_solar, mock_wind):
     scenario = MockScenario(
         grid_attrs={"plant": mock_plant},
         hydro=mock_hydro,
         solar=mock_solar,
         wind=mock_wind,
     )
     pd.testing.assert_frame_equal(scenario.get_profile("hydro"),
                                   mock_hydro)
     pd.testing.assert_frame_equal(scenario.get_profile("solar"),
                                   mock_solar)
     pd.testing.assert_frame_equal(scenario.get_profile("wind"), mock_wind)
def test_calculate_congestion_surplus_single_time(monkeypatch):
    """Congested case from Kirschen & Strbac Section 5.3.2.4"""
    def mock_get_data(*args, **kwargs):
        return demand

    # Override default InputData.get_data method to avoid profile csv lookup
    monkeypatch.setattr(InputData, "get_data", mock_get_data)

    demand = pd.DataFrame({"UTC": ["t1"], 1: [410], 2: [0]})
    lmp = pd.DataFrame({"UTC": ["t1"], 1: [7.5], 2: [11.25], 3: [10], 4: [0]})
    pg = pd.DataFrame({
        "UTC": ["t1"],
        "A": [50],
        "B": [285],
        "C": [0],
        "D": [75]
    })
    for df in (demand, lmp, pg):
        df.set_index("UTC", inplace=True)
    mock_scenario = MockScenario(grid_attrs, demand=demand, lmp=lmp, pg=pg)

    expected_return = pd.Series(
        data=[787.5],
        index=pd.date_range(start="2016-01-01", periods=1, freq="H"),
    )
    expected_return.rename_axis("UTC")

    surplus = congestion.calculate_congestion_surplus(mock_scenario)
    _check_return(expected_return, surplus)
예제 #5
0
 def test_good_scenario(self):
     mock_plant = {
         "plant_id": ["A", "B", "C", "D"],
         "ramp_30": [2.5, 5, 10, 25],
     }
     mock_scenario = MockScenario({"plant": mock_plant})
     _check_scenario_is_in_analyze_state(mock_scenario)
예제 #6
0
 def setUp(self):
     mock_plant = {
         "plant_id": ["A", "B", "C", "D"],
         "ramp_30": [2.5, 5, 10, 25],
     }
     grid_attrs = {"plant": mock_plant}
     mock_pg = pd.DataFrame(
         {
             "A": [100, 104, (99 + 1e-4), (104 + 1e-4 - 1e-7)],
             "B": [50, 45, 50, 45],
             "C": [20, 40, 60, 80],
             "D": [200, 150, 100, 50],
         }
     )
     self.mock_scenario = MockScenario(grid_attrs, pg=mock_pg)
     self.default_expected = pd.DataFrame(
         {
             "UTC": pd.date_range(start="2016-01-01", periods=4, freq="H"),
             "A": [False, False, True, True],
             "B": [False, False, False, False],
             "C": [False, True, True, True],
             "D": [False, True, True, True],
         }
     )
     self.default_expected.set_index("UTC", inplace=True)
예제 #7
0
 def setUp(self):
     mock_plant = {
         "plant_id": ["A", "B", "C", "D"],
         "Pmax": [50, 75, 100, 200],
     }
     grid_attrs = {"plant": mock_plant}
     mock_pg = pd.DataFrame(
         {
             "A": [50, 50],
             "B": [(75 - 1e-4), 70],
             "C": [90, (100 - 1e-7)],
             "D": [150, 175],
         }
     )
     self.mock_scenario = MockScenario(grid_attrs, pg=mock_pg)
     self.default_expected = pd.DataFrame(
         {
             "UTC": pd.date_range(start="2016-01-01", periods=2, freq="H"),
             "A": [True, True],
             "B": [True, False],
             "C": [False, True],
             "D": [False, False],
         }
     )
     self.default_expected.set_index("UTC", inplace=True)
예제 #8
0
 def setUp(self):
     mock_plant = {
         "plant_id": ["A", "B", "C", "D"],
         "Pmin": [0, 10, 20, 30],
     }
     grid_attrs = {"plant": mock_plant}
     mock_pg = pd.DataFrame(
         {
             "A": [0, 0],
             "B": [(10 + 1e-4), 15],
             "C": [25, (20 + 1e-7)],
             "D": [35, 40],
         }
     )
     self.mock_scenario = MockScenario(grid_attrs, pg=mock_pg)
     self.default_expected = pd.DataFrame(
         {
             "UTC": pd.date_range(start="2016-01-01", periods=2, freq="H"),
             "A": [True, True],
             "B": [True, False],
             "C": [False, True],
             "D": [False, False],
         }
     )
     self.default_expected.set_index("UTC", inplace=True)
예제 #9
0
def test_calculate_congestion_surplus_single_time():
    """Congested case from Kirschen & Strbac Section 5.3.2.4"""

    bus_demand = pd.DataFrame({
        "UTC": ["t1"],
        1: [50],
        2: [60],
        3: [300],
        4: [0]
    })
    lmp = pd.DataFrame({"UTC": ["t1"], 1: [7.5], 2: [11.25], 3: [10], 4: [0]})
    pg = pd.DataFrame({
        "UTC": ["t1"],
        "A": [50],
        "B": [285],
        "C": [0],
        "D": [75]
    })
    for df in (bus_demand, lmp, pg):
        df.set_index("UTC", inplace=True)
    mock_scenario = MockScenario(grid_attrs,
                                 bus_demand=bus_demand,
                                 lmp=lmp,
                                 pg=pg)

    expected_return = pd.Series(
        data=[787.5],
        index=pd.date_range(start="2016-01-01", periods=1, freq="H"),
    )
    expected_return.rename_axis("UTC")

    surplus = congestion.calculate_congestion_surplus(mock_scenario)
    _check_return(expected_return, surplus)
예제 #10
0
def mock_scenario():
    return MockScenario(
        grid_attrs={"plant": mock_plant},
        demand=mock_demand,
        hydro=mock_hydro,
        solar=mock_solar,
        wind=mock_wind,
    )
 def test_error_no_solar(self):
     no_solar_mock_plant = {
         "plant_id": ["C", "D"],
         "type": ["wind", "wind"]
     }
     no_solar_grid_attrs = {"plant": no_solar_mock_plant}
     no_solar_scenario = MockScenario(no_solar_grid_attrs)
     with self.assertRaises(ValueError):
         calculate_curtailment_time_series_by_resources(
             no_solar_scenario, resources=("solar", ))
 def setUp(self):
     scenario = MockScenario(
         grid_attrs={"plant": mock_plant},
         demand=mock_demand,
         pg=mock_pg,
         solar=mock_solar,
         wind=mock_wind,
         hydro=mock_hydro,
     )
     scenario.state.grid.zone2id = {"Oregon": 202, "Arizona": 209}
     self.scenario_info = ScenarioInfo(scenario)
예제 #13
0
 def setUp(self):
     self.ct = {
         # These data aren't used, but we make sure they don't get changed.
         "demand": {
             "zone_id": {
                 "Washington": 1.1,
                 "Oregon": 1.2
             }
         },
         "solar": {
             "zone_id": {
                 "Washington": 1.5,
                 "Oregon": 1.7
             }
         },
         "wind": {
             "zone_id": {
                 "Oregon": 1.3,
                 "Washington": 2.1
             }
         },
     }
     self.ref_scenario = MockScenario(
         grid_attrs={
             "branch": mock_branch,
             "bus": mock_bus,
             "plant": mock_plant,
         },
         ct={
             "branch": {
                 "branch_id": {
                     101: 1.5,
                     102: 2.5,
                     103: 2,
                     105: 4
                 }
             },
             # These shouldn't get used
             "coal": {
                 "zone_id": {
                     "Oregon": 2,
                     "Washington": 0
                 }
             },
             "demand": {
                 "zone_id": {
                     "Washington": 0.9,
                     "Oregon": 0.8
                 }
             },
         },
     )
     orig_ct = self.ref_scenario.state.get_ct()
     self.orig_branch_scaling = orig_ct["branch"]["branch_id"]
def sim_gen_result():
    scenario = MockScenario(grid_attrs, pg=mock_pg)
    scenario.state.grid.interconnect = ["Western"]
    scenario.state.grid.plant.zone_id = [201, 202, 203, 204]
    scenario.state.grid.id2zone = {
        201: "Washington",
        202: "Oregon",
        203: "Northern California",
        204: "Bay Area",
    }
    return sum_generation_by_state(scenario)
예제 #15
0
def test_calculate_congestion_surplus_three_times():
    """First: congested. Second: uncongested. Third: uncongested, fuzzy."""

    time_indices = ["t1", "t2", "t3"]
    bus_demand = pd.DataFrame({
        "UTC": time_indices,
        1: [50] * 3,
        2: [60] * 3,
        3: [300] * 3,
        4: [0] * 3
    })
    lmp = pd.DataFrame({
        "UTC": time_indices,
        1: [7.5, 7.5, 7.5],
        2: [11.25, 7.5, 7.5],
        3: [10, 7.5, 7.49],
        4: [0, 0, 0],
    })
    pg = pd.DataFrame({
        "UTC": time_indices,
        "A": [50, 125, 125],
        "B": [285] * 3,
        "C": [0] * 3,
        "D": [75, 0, 0],
    })
    for df in (bus_demand, lmp, pg):
        df.set_index("UTC", inplace=True)
    mock_scenario = MockScenario(grid_attrs,
                                 bus_demand=bus_demand,
                                 lmp=lmp,
                                 pg=pg)

    expected_return = pd.Series(
        data=[787.5, 0, 0],
        index=pd.date_range(start="2016-01-01", periods=3, freq="H"),
    )
    expected_return.rename_axis("UTC")

    surplus = congestion.calculate_congestion_surplus(mock_scenario)
    _check_return(expected_return, surplus)
    def setUp(self):
        # Build dummy congu and congl dataframes, containing barrier cruft
        num_hours = 100
        branch_indices = mock_branch["branch_id"]
        num_branches = len(branch_indices)
        congu_data = np.ones((num_hours, num_branches)) * 1e-9
        congl_data = np.ones((num_hours, num_branches)) * 1e-10
        columns = mock_branch["branch_id"]
        congu = pd.DataFrame(congu_data,
                             index=range(num_hours),
                             columns=columns)
        congl = pd.DataFrame(congl_data,
                             index=range(num_hours),
                             columns=columns)
        # Populate with dummy data, added in different hours for thorough testing
        # Branch 101 will have frequent, low congestion
        congu[101].iloc[-15:] = 1
        # Branch 102 will have less frequent, but greater congestion
        congu[102].iloc[:8] = 6
        # Branch 103 will have only occassional congestion, but very high
        congu[103].iloc[10:13] = 20
        congl[103].iloc[20:23] = 30
        # Branch 105 will have extremely high congestion in only one hour
        congl[105].iloc[49] = 9000
        # Build dummy change table
        ct = {"branch": {"branch_id": {b: 1 for b in branch_indices}}}

        # Finally, combine all of this into a MockScenario
        self.mock_scenario = MockScenario(
            grid_attrs={
                "branch": mock_branch,
                "bus": mock_bus,
                "plant": mock_plant,
            },
            congu=congu,
            congl=congl,
            ct=ct,
        )
def test_calculate_congestion_surplus_three_times(monkeypatch):
    """First: congested. Second: uncongested. Third: uncongested, fuzzy."""
    def mock_get_data(*args, **kwargs):
        return demand

    # Override default InputData.get_data method to avoid profile csv lookup
    monkeypatch.setattr(InputData, "get_data", mock_get_data)

    time_indices = ["t1", "t2", "t3"]
    demand = pd.DataFrame({"UTC": time_indices, 1: [410] * 3, 2: [0] * 3})
    lmp = pd.DataFrame({
        "UTC": time_indices,
        1: [7.5, 7.5, 7.5],
        2: [11.25, 7.5, 7.5],
        3: [10, 7.5, 7.49],
        4: [0, 0, 0],
    })
    pg = pd.DataFrame({
        "UTC": time_indices,
        "A": [50, 125, 125],
        "B": [285] * 3,
        "C": [0] * 3,
        "D": [75, 0, 0],
    })
    for df in (demand, lmp, pg):
        df.set_index("UTC", inplace=True)
    mock_scenario = MockScenario(grid_attrs, demand=demand, lmp=lmp, pg=pg)

    expected_return = pd.Series(
        data=[787.5, 0, 0],
        index=pd.date_range(start="2016-01-01", periods=3, freq="H"),
    )
    expected_return.rename_axis("UTC")

    surplus = congestion.calculate_congestion_surplus(mock_scenario)
    _check_return(expected_return, surplus)
예제 #18
0
        1560,
        1752,
        2034,
        2401,
        1352,
        2160,
        2472,
        3217,
    ],
})

scenario = MockScenario(
    grid_attrs={
        "plant": mock_plant,
        "bus": mock_bus,
        "storage_gen": mock_storage
    },
    demand=mock_demand,
    pg=mock_pg,
)
scenario.info["start_date"] = "2016-01-01 00:00:00"
scenario.info["end_date"] = "2016-01-01 10:00:00"
scenario.state.grid.zone2id = {
    "Washington": 201,
    "Oregon": 202,
}


def test_NLDC_calculation_wind_str():
    assert calculate_NLDC(scenario, "wind", 10) == approx(3496.1)
 def test_with_scenario_not_analyze(self):
     test_scenario = MockScenario(grid_attrs, pg=mock_pg)
     test_scenario.state = "Create"
     with self.assertRaises(ValueError):
         sum_generation_by_type_zone(test_scenario)
 def setUp(self):
     self.scenario = MockScenario(grid_attrs, pg=mock_pg)
    "C": [1, 1, 2, 3],
    "D": [1, 3, 5, 7],
})

mock_storage_pg = pd.DataFrame({
    0: [1, 1, 1],
    1: [0, 1, 2],
    2: [2, 2, 2],
})

grid_attrs = {
    "plant": mock_plant,
    "bus": mock_bus,
    "storage_gen": mock_storage
}
scenario = MockScenario(grid_attrs, pg=mock_pg, storage_pg=mock_storage_pg)
scenario.state.grid.zone2id = {
    "Washington": 201,
    "Oregon": 202,
}


class TestSumGenerationByTypeZone(unittest.TestCase):
    def setUp(self):
        self.scenario = MockScenario(grid_attrs, pg=mock_pg)

    def test_sum_generation(self):
        expected_return = pd.DataFrame({
            "type": ["hydro", "solar", "wind"],
            1: [0, 10, 15],
            2: [23, 0, 0]
예제 #22
0
        "1002": [4, 3, 1, 2],
        "1003": [3, 3, 3, 3],
    }
)

grid_attrs = {"plant": mock_plant}

mock_demand = pd.DataFrame(
    {
        201: [1, 2, 3, 4],
        202: [4, 3, 2, 1],
        203: [2, 2, 2, 2],
    }
)

scenario = MockScenario(grid_attrs, pg=mock_pg, demand=mock_demand)
scenario.state.grid.zone2id = {
    "Washington": 201,
    "Oregon": 202,
    "Northern California": 203,
}


def test_get_demand_time_series():
    demand = get_demand_time_series(scenario, "Washington")
    expected_results = [1, 2, 3, 4]
    assert_array_equal(demand.to_numpy(), expected_results)


def test_get_net_demand_time_series():
    net_demand = get_net_demand_time_series(scenario, "all")
예제 #23
0
def test_check_scenario_is_in_analyze_state_argument_value():
    input = MockScenario()
    input.state = "Create"
    with pytest.raises(ValueError):
        _check_scenario_is_in_analyze_state(input)
예제 #24
0
        0.00085,
        0.0009,
        0,
        0,
        0,
        0,
        0.0013,
        0.0011,
        0,
    ],
    "c1": [0, 20, 0, 0, 25, 32, 18, 29, 0, 0, 0, 0, 35, 27, 0],
    "c0": [0, 888, 0, 0, 750, 633, 599, 933, 0, 0, 0, 0, 1247, 1111, 0],
    "interconnect": ["Western"] * 15,
}

scenario = MockScenario({"plant": mock_plant, "gencost_after": mock_gencost})
scenario.info["start_date"] = "2016-01-01 00:00:00"
scenario.info["end_date"] = "2016-01-10 23:00:00"

grid = scenario.state.get_grid()


def test_check_data_frame_argument_type():
    arg = (
        (1, "int"),
        ("homer", "str"),
        ({"homer", "marge", "bart", "lida"}, "set"),
        (pd.DataFrame({"California": [1, 2, 3], "Texas": [4, 5, 6]}), 123456),
    )
    for a in arg:
        with pytest.raises(TypeError):
mock_pg = pd.DataFrame(
    {
        "A": [1, 2, 3, 4],
        "B": [1, 2, 4, 8],
        "C": [1, 1, 2, 3],
        "D": [1, 3, 5, 7],
    }
)

mock_storage = {
    "bus_id": [1, 2, 3],
    "Pmax": [10, 10, 10],
}

grid_attrs = {"plant": mock_plant, "bus": mock_bus, "storage_gen": mock_storage}
scenario = MockScenario(grid_attrs)
scenario.state.grid.zone2id = {
    "Washington": 201,
    "Bay Area": 204,
    "New York City": 7,
}


def check_dataframe_matches(received_return, expected_return):
    assert isinstance(received_return, pd.DataFrame)
    assert_array_equal(
        received_return.index.to_numpy(), expected_return.index.to_numpy()
    )
    assert_array_equal(
        received_return.columns.to_numpy(), expected_return.columns.to_numpy()
    )
예제 #26
0
 def test_mock_hydro_stored_properly(self, mock_hydro):
     scenario = MockScenario(grid_attrs={"plant": mock_plant},
                             hydro=mock_hydro)
     hydro = scenario.state.get_hydro()
     err_msg = "hydro should have dimension (periodNum * len(hydro_plant))"
     assert hydro.shape == mock_hydro.shape, err_msg
예제 #27
0
 def test_mock_wind_stored_properly(self, mock_wind):
     scenario = MockScenario(grid_attrs={"plant": mock_plant},
                             wind=mock_wind)
     wind = scenario.state.get_wind()
     err_msg = "wind should have dimension (periodNum * len(wind_plant))"
     assert wind.shape == mock_wind.shape, err_msg
예제 #28
0
 def test_mock_solar_stored_properly(self, mock_solar):
     scenario = MockScenario(grid_attrs={"plant": mock_plant},
                             solar=mock_solar)
     solar = scenario.state.get_solar()
     err_msg = "solar should have dimension (periodNum * len(solar_plant))"
     assert solar.shape == mock_solar.shape, err_msg
예제 #29
0
def scenario(mock_plant, mock_gencost, mock_pg):
    return MockScenario(
        grid_attrs={"plant": mock_plant, "gencost_before": mock_gencost},
        pg=mock_pg,
    )
예제 #30
0
 def test_mock_pg_stored_properly(self, mock_pg):
     scenario = MockScenario(grid_attrs={"plant": mock_plant}, pg=mock_pg)
     pg = scenario.state.get_pg()
     err_msg = "pg should have dimension (periodNum * len(plant))"
     assert pg.shape == mock_pg.shape, err_msg