Ejemplo n.º 1
0
def test_demand_is_scaled(base_grid, raw_demand):
    base_demand = raw_demand[base_grid.id2zone.keys()]

    n_zone = param["n_zone_to_scale"]
    ct = ChangeTable(base_grid)
    ct.scale_demand(
        zone_id={
            z: f
            for z, f in zip(
                np.random.choice([i for i in base_grid.zone2id.values()],
                                 size=n_zone,
                                 replace=False),
                2 * np.random.random(size=n_zone),
            )
        })

    tg = TransformGrid(base_grid, ct.ct)
    transformed_grid = tg.get_grid()

    empty_scenario_info = {
    }  # scenario_info not needed since profile_input is mocked
    tp = TransformProfile(empty_scenario_info, transformed_grid, ct.ct)
    transformed_profile = tp.get_profile("demand")
    assert not base_demand.equals(transformed_profile)

    scaled_zone = list(ct.ct["demand"]["zone_id"].keys())
    unscaled_zone = set(base_grid.id2zone.keys()) - set(scaled_zone)
    factor = list(ct.ct["demand"]["zone_id"].values())
    assert transformed_profile[scaled_zone].equals(
        base_demand[scaled_zone].multiply(factor, axis=1))
    if unscaled_zone:
        assert transformed_profile[list(unscaled_zone)].equals(
            base_demand[list(unscaled_zone)])
Ejemplo n.º 2
0
def _check_plants_are_scaled(ct, base_grid, profile_info, raw_profile, resource):
    plant_id_type = get_plant_with_resource(base_grid, resource)

    base_profile = (
        raw_profile[plant_id_type] * base_grid.plant.loc[plant_id_type, "Pmax"]
    )

    tg = TransformGrid(base_grid, ct)
    transformed_grid = tg.get_grid()

    tp = TransformProfile(profile_info, transformed_grid, ct)
    transformed_profile = tp.get_profile(resource)

    scaled_plant_id = []
    scaling_factor_plant = []
    if "zone_id" in ct[resource].keys():
        for z, f in ct[resource]["zone_id"].items():
            plant_id_zone = (
                base_grid.plant.groupby(["zone_id", "type"])
                .get_group((z, resource))
                .index.tolist()
            )
            scaled_plant_id += plant_id_zone
            scaling_factor_plant += [f] * len(plant_id_zone)
    if "plant_id" in ct[resource].keys():
        for i, f in ct[resource]["plant_id"].items():
            if i in scaled_plant_id:
                scaling_factor_plant[scaled_plant_id.index(i)] *= f
            else:
                scaled_plant_id.append(i)
                scaling_factor_plant.append(f)

    assert not base_profile.equals(transformed_profile)
    assert_almost_equal(
        transformed_profile[scaled_plant_id].values,
        base_profile[scaled_plant_id].multiply(scaling_factor_plant, axis=1).values,
    )

    unscaled_plant_id = set(plant_id_type) - set(scaled_plant_id)
    if unscaled_plant_id:
        assert transformed_profile[unscaled_plant_id].equals(
            base_profile[unscaled_plant_id]
        )
    return transformed_profile
Ejemplo n.º 3
0
def test_flexible_demand_profiles_are_trimmed(base_grid,
                                              raw_demand_flexibility_up,
                                              raw_demand_flexibility_dn,
                                              monkeypatch):
    monkeypatch.setattr(Context, "get_data_access",
                        MockContext().get_data_access)
    data_access = Context.get_data_access()

    # Specify the fake demand flexibility profiles from MockInputData
    zone_keys = [f"zone.{z}" for z in base_grid.id2zone.keys()]
    base_demand_flexibility_up = raw_demand_flexibility_up[zone_keys]
    base_demand_flexibility_dn = raw_demand_flexibility_dn[zone_keys]

    # Create fake files in the expected directory path
    exp_path = f"raw/{base_grid.grid_model}"

    for csv_file in (
            "demand_flexibility_up_Test.csv",
            "demand_flexibility_dn_Test.csv",
    ):
        with data_access.write(exp_path + "/" + csv_file) as f:
            pd.DataFrame().to_csv(f)

    # Specify the change table
    ct = ChangeTable(base_grid)
    ct.add_demand_flexibility({
        "demand_flexibility_up": "Test",
        "demand_flexibility_dn": "Test",
        "demand_flexibility_duration": 6,
    })

    # Transform the grid object accordingly
    tg = TransformGrid(base_grid, ct.ct)
    transformed_grid = tg.get_grid()

    # Test that the demand flexibility profiles are pruned
    empty_scenario_info = {"grid_model": base_grid.grid_model}
    tp = TransformProfile(empty_scenario_info, transformed_grid, ct.ct)
    transformed_demand_flexibility_up = tp.get_profile("demand_flexibility_up")
    transformed_demand_flexibility_dn = tp.get_profile("demand_flexibility_dn")
    assert base_demand_flexibility_up.equals(transformed_demand_flexibility_up)
    assert base_demand_flexibility_dn.equals(transformed_demand_flexibility_dn)
Ejemplo n.º 4
0
def test_demand_is_scaled(base_grid):
    input_data = InputData()
    demand_info = {
        "interconnect": "_".join(interconnect),
        "grid_model": "usa_tamu",
        "base_demand": param["demand"],
    }
    raw_demand = input_data.get_data(demand_info, "demand")
    base_demand = raw_demand[base_grid.id2zone.keys()]

    n_zone = param["n_zone_to_scale"]
    ct = ChangeTable(base_grid)
    ct.scale_demand(
        zone_id={
            z: f
            for z, f in zip(
                np.random.choice(
                    [i for i in base_grid.zone2id.values()], size=n_zone, replace=False
                ),
                2 * np.random.random(size=n_zone),
            )
        }
    )

    tg = TransformGrid(base_grid, ct.ct)
    transformed_grid = tg.get_grid()

    tp = TransformProfile(demand_info, transformed_grid, ct.ct)
    transformed_profile = tp.get_profile("demand")
    assert not base_demand.equals(transformed_profile)

    scaled_zone = list(ct.ct["demand"]["zone_id"].keys())
    unscaled_zone = set(base_grid.id2zone.keys()) - set(scaled_zone)
    factor = list(ct.ct["demand"]["zone_id"].values())
    assert transformed_profile[scaled_zone].equals(
        base_demand[scaled_zone].multiply(factor, axis=1)
    )
    if unscaled_zone:
        assert transformed_profile[unscaled_zone].equals(base_demand[unscaled_zone])
Ejemplo n.º 5
0
def _check_new_plants_are_added(ct, base_grid, raw_profile, resource):
    n_plant = param["n_plant_to_add"]
    plant_id_type = (base_grid.plant.isin(
        profile_type[resource]).query("type == True").index)
    base_profile = (raw_profile[plant_id_type] *
                    base_grid.plant.loc[plant_id_type, "Pmax"])

    tg = TransformGrid(base_grid, ct)
    transformed_grid = tg.get_grid()

    empty_scenario_info = {
    }  # scenario_info not needed since profile_input is mocked
    tp = TransformProfile(empty_scenario_info, transformed_grid, ct)
    transformed_profile = tp.get_profile(resource)

    assert not transformed_profile.equals(base_profile)
    assert not len(base_profile.columns) == len(transformed_profile.columns)
    assert len(set(transformed_profile.columns) -
               set(base_profile.columns)) == n_plant
    assert set(transformed_profile.columns) - set(base_profile.columns) == set(
        transformed_grid.plant.index[-n_plant:])

    return transformed_profile.drop(base_profile.columns, axis=1)