Skip to content

Commit

Permalink
Merge pull request #497 from ProjectDrawdown/scenario
Browse files Browse the repository at this point in the history
Grab bag of changes
  • Loading branch information
denised authored Oct 25, 2021
2 parents e5d26ed + ba3b8ef commit 3a2e4e0
Show file tree
Hide file tree
Showing 297 changed files with 1,894 additions and 17,023 deletions.
File renamed without changes.
File renamed without changes.
9 changes: 6 additions & 3 deletions model/operatingcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ def annual_breakout(
if not has_var_costs and not fixed_oper_cost_per_iunit:
return breakout

# supply a reasonable value if none provided; this matches the requested default in the Excel
if lifetime_replacement == 0:
lifetime_replacement = 100

for year in range(first_year, last_year + 1):
# within the years of interest, assume replacement of worn out equipment.
lifetime = lifetime_replacement
assert lifetime_replacement != 0, 'Cannot have a lifetime replacement of 0 and non-zero operating costs'
while math.ceil(lifetime) < (last_year + 1 - year):
lifetime += lifetime_replacement

Expand Down Expand Up @@ -399,7 +402,7 @@ def soln_vs_conv_single_iunit_cashflow(self):
result.name = 'soln_vs_conv_single_iunit_cashflow'

soln_lifetime = self.ac.soln_lifetime_replacement
if self.ac.soln_avg_annual_use is not None and self.ac.conv_avg_annual_use is not None:
if self.ac.soln_avg_annual_use is not None and self.ac.conv_avg_annual_use is not None and self.ac.conv_avg_annual_use != 0:
conv_usage_mult = self.ac.soln_avg_annual_use / self.ac.conv_avg_annual_use # RRS
else:
conv_usage_mult = 1 # LAND
Expand Down Expand Up @@ -510,7 +513,7 @@ def soln_only_single_iunit_cashflow(self):
result.name = 'soln_only_single_iunit_cashflow'

soln_lifetime = self.ac.soln_lifetime_replacement
if self.ac.soln_avg_annual_use is not None and self.ac.conv_avg_annual_use is not None:
if self.ac.soln_avg_annual_use is not None and self.ac.conv_avg_annual_use is not None and self.ac.conv_avg_annual_use != 0:
conv_usage_mult = self.ac.soln_avg_annual_use / self.ac.conv_avg_annual_use # RRS
else:
conv_usage_mult = 1 # LAND
Expand Down
24 changes: 21 additions & 3 deletions model/tests/test_emissionsfactors.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,63 @@ def test_ElectricityGenOnGrid_conv_ref_grid_CO2eq_per_KWh():
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2025, "OECD90"] == pytest.approx(0.454068989)
assert table.loc[2020, 'World'] == pytest.approx(0.483415642)

ac = advanced_controls.AdvancedControls(
emissions_grid_source="ipcc_only", emissions_grid_range="low")
eg = ef.ElectricityGenOnGrid(ac=ac)
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2020, 'World'] == pytest.approx(0.416520905)

ac = advanced_controls.AdvancedControls(
emissions_grid_source="ipcc_only", emissions_grid_range="high")
eg = ef.ElectricityGenOnGrid(ac=ac)
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2020, 'World'] == pytest.approx(0.952177536)

ac = advanced_controls.AdvancedControls(
emissions_grid_source="meta-analysis", emissions_grid_range="mean")
eg = ef.ElectricityGenOnGrid(ac=ac)
eg = ef.ElectricityGenOnGrid(ac=ac, grid_emissions_version=1)
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2020, 'World'] == pytest.approx(0.581083120)

ac = advanced_controls.AdvancedControls(
emissions_grid_source="meta-analysis", emissions_grid_range="low")
eg = ef.ElectricityGenOnGrid(ac=ac)
eg = ef.ElectricityGenOnGrid(ac=ac, grid_emissions_version=1)
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2020, 'World'] == pytest.approx(0.446005409)

ac = advanced_controls.AdvancedControls(
emissions_grid_source="meta-analysis", emissions_grid_range="high")
eg = ef.ElectricityGenOnGrid(ac=ac)
eg = ef.ElectricityGenOnGrid(ac=ac, grid_emissions_version=1)
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2020, 'World'] == pytest.approx(0.726403172)

ac = advanced_controls.AdvancedControls(
emissions_grid_source="meta-analysis", emissions_grid_range="mean")
eg = ef.ElectricityGenOnGrid(ac=ac, grid_emissions_version=2)
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2033, 'World'] == pytest.approx(0.5546363024)

ac = advanced_controls.AdvancedControls(
emissions_grid_source="meta-analysis", emissions_grid_range="mean")
eg = ef.ElectricityGenOnGrid(ac=ac, grid_emissions_version=3)
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2043, 'World'] == pytest.approx(0.52233785855)

eg = ef.ElectricityGenOnGrid(ac=ac, grid_emissions_version=4)
table = eg.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2042, 'World'] == pytest.approx(0.52865037213)

def test_emissions_grid_current():
# Test that default and current both load, and are the same
ac = advanced_controls.AdvancedControls(
emissions_grid_source="meta-analysis", emissions_grid_range="low")
eg_default = ef.ElectricityGenOnGrid(ac=ac)
table = eg_default.conv_ref_grid_CO2eq_per_KWh()

eg_current = ef.ElectricityGenOnGrid(ac=ac,grid_emissions_version="current")
table_2 = eg_current.conv_ref_grid_CO2eq_per_KWh()
assert table.loc[2040,'World'] == table_2.loc[2040,'World']

def test_ElectricityGenOnGrid_conv_ref_grid_CO2eq_per_KWh_invalid_range():
with pytest.raises(ValueError):
Expand Down
12 changes: 7 additions & 5 deletions model/tests/test_helpertables.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,17 +710,19 @@ def test_soln_use_first_pds_datapoint_not_2014():
ht_pds_datapoints = ht_ref_datapoints
ht = helpertables.HelperTables(ac, pds_adoption_data_per_region=custom_scen,
ref_datapoints=ht_ref_datapoints, pds_datapoints=ht_pds_datapoints,
copy_pds_world_too=True)
copy_pds_datapoint=True, copy_pds_world_too=True)
result = ht.soln_pds_funits_adopted()
# even when first datapoint is not 2014, copy_pds_datapoint copies to 2014
for region in regions:
assert int(result.loc[2020, region]) == 1000
assert int(result.loc[2014, region]) == 1000

ht = helpertables.HelperTables(ac, pds_adoption_data_per_region=custom_scen,
ref_datapoints=ht_ref_datapoints, pds_datapoints=ht_pds_datapoints,
copy_pds_world_too=False)
copy_pds_datapoint=True, copy_pds_world_too=False)
result = ht.soln_pds_funits_adopted()
assert int(result.loc[2020, regions[0]]) != 1000
assert int(result.loc[2014, regions[0]]) != 1000
for region in regions[1:]:
assert int(result.loc[2020, region]) == 1000
assert int(result.loc[2014, region]) == 1000


def test_copy_ref_datapoint():
Expand Down
23 changes: 0 additions & 23 deletions model/tests/test_operatingcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,29 +562,6 @@ def test_annual_breakout_zero_cost_zero_lifetime_replacement():
assert (result == 0).all(axis=None)


def test_annual_breakout_nonzero_cost_zero_lifetime_replacement():
soln_net_annual_funits_adopted = pd.DataFrame(soln_net_annual_funits_adopted_list[1:],
columns=soln_net_annual_funits_adopted_list[0]).set_index('Year')
soln_pds_tot_iunits_reqd = pd.DataFrame(soln_pds_tot_iunits_reqd_list[1:],
columns=soln_pds_tot_iunits_reqd_list[0]).set_index('Year')
soln_ref_tot_iunits_reqd = pd.DataFrame(soln_ref_tot_iunits_reqd_list[1:],
columns=soln_ref_tot_iunits_reqd_list[0]).set_index('Year')
ac = advanced_controls.AdvancedControls(report_end_year=2050,
soln_lifetime_capacity=0, soln_avg_annual_use=1725.04615384615,
soln_var_oper_cost_per_funit=0.0, soln_fuel_cost_per_funit=0.0,
soln_fixed_oper_cost_per_iunit=2)
oc = operatingcost.OperatingCost(ac=ac,
soln_net_annual_funits_adopted=soln_net_annual_funits_adopted,
soln_pds_tot_iunits_reqd=soln_pds_tot_iunits_reqd,
soln_ref_tot_iunits_reqd=soln_ref_tot_iunits_reqd,
conv_ref_annual_tot_iunits=None, soln_pds_annual_world_first_cost=None,
soln_ref_annual_world_first_cost=None, conv_ref_annual_world_first_cost=None,
single_iunit_purchase_year=None, soln_pds_install_cost_per_iunit=None,
conv_ref_install_cost_per_iunit=None, conversion_factor=1.0)
with pytest.raises(AssertionError):
result = oc.soln_pds_annual_breakout()


def test_soln_pds_annual_breakout_land():
columns = ['World', 'A']
index = range(2015, 2061)
Expand Down
44 changes: 0 additions & 44 deletions model/tests/test_unitadoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,50 +311,6 @@ def test_soln_pds_cumulative_funits_bug_behavior():
pd.testing.assert_frame_equal(result.iloc[0:5], expected, check_exact=False)


def test_soln_pds_cumulative_funits_bug_behavior_ref_base_adoption():
funits = [
['Year', 'World', 'OECD90', 'Eastern Europe', 'Asia (Sans Japan)', 'Middle East and Africa',
'Latin America', 'China', 'India', 'EU', 'USA'],
[2014, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.99],
[2015, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[2016, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[2017, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
soln_pds_funits_adopted = pd.DataFrame(funits[1:], columns=funits[0]).set_index('Year')
ac = advanced_controls.AdvancedControls(solution_category=SOLUTION_CATEGORY.LAND,
ref_base_adoption={ "World": 10.0, "OECD90": 9.0, "Eastern Europe": 8.0,
"Asia (Sans Japan)": 7.0, "Middle East and Africa": 6.0, "Latin America": 5.0,
"China": 4.0, "India": 3.0, "EU": 2.0, "USA": 1.0})
ua = unitadoption.UnitAdoption(ac=ac,
ref_total_adoption_units=None, pds_total_adoption_units=None,
soln_pds_funits_adopted=soln_pds_funits_adopted, soln_ref_funits_adopted=None,
bug_cfunits_double_count=True)
result = ua.soln_pds_cumulative_funits()
assert result.loc[2014, 'World'] == pytest.approx(0.1)
assert result.loc[2014, 'OECD90'] == pytest.approx(9.2)
assert result.loc[2014, 'Eastern Europe'] == pytest.approx(8.3)
assert result.loc[2014, 'Asia (Sans Japan)'] == pytest.approx(7.4)
assert result.loc[2014, 'Middle East and Africa'] == pytest.approx(6.5)
assert result.loc[2014, 'Latin America'] == pytest.approx(5.6)
assert result.loc[2014, 'China'] == pytest.approx(4.7)
assert result.loc[2014, 'India'] == pytest.approx(3.8)
assert result.loc[2014, 'EU'] == pytest.approx(2.9)
assert result.loc[2014, 'USA'] == pytest.approx(1.99)
ua = unitadoption.UnitAdoption(ac=ac,
ref_total_adoption_units=None, pds_total_adoption_units=None,
soln_pds_funits_adopted=soln_pds_funits_adopted, soln_ref_funits_adopted=None,
bug_cfunits_double_count=False)
result = ua.soln_pds_cumulative_funits()
assert result.loc[2014, 'World'] == pytest.approx(0.1)
assert result.loc[2014, 'OECD90'] == pytest.approx(0.2)
assert result.loc[2014, 'Eastern Europe'] == pytest.approx(0.3)
assert result.loc[2014, 'Asia (Sans Japan)'] == pytest.approx(0.4)
assert result.loc[2014, 'Middle East and Africa'] == pytest.approx(0.5)
assert result.loc[2014, 'Latin America'] == pytest.approx(0.6)
assert result.loc[2014, 'China'] == pytest.approx(0.7)
assert result.loc[2014, 'India'] == pytest.approx(0.8)
assert result.loc[2014, 'EU'] == pytest.approx(0.9)
assert result.loc[2014, 'USA'] == pytest.approx(0.99)


def test_soln_pds_cumulative_funits_missing_data():
funits = [
Expand Down
8 changes: 1 addition & 7 deletions model/unitadoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,7 @@ def soln_pds_cumulative_funits(self):
# the 2014 soln_pds_cumulative_funits, which ends up double counting 2014.
# We optionally enable this bug-for-bug compatibility.
# https://docs.google.com/document/d/19sq88J_PXY-y_EnqbSJDl0v9CdJArOdFLatNNUFhjEA/edit#heading=h.z9hqutnbnigx
idx = first_year.first_valid_index()
if self.ac is not None and self.ac.ref_base_adoption is not None:
# solutions updated for Drawdown 2020 have a ref_base_adoption dict
omit_main = pd.DataFrame(self.ac.ref_base_adoption, index=[idx])
else:
# Solutions not yet updated fall back to the original code here
omit_main = self.soln_pds_funits_adopted.iloc[[0], :].fillna(0.0).copy(deep=True)
omit_main = self.soln_pds_funits_adopted.iloc[[0], :].fillna(0.0).copy(deep=True)
omit_main.index.name = 'Year'
main_region = dd.REGIONS[0]
omit_main[main_region] = 0.0
Expand Down
2 changes: 1 addition & 1 deletion solution/afforestation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def __init__(self, scen=None):
pds_adoption_trend_per_region=pds_adoption_trend_per_region,
pds_adoption_is_single_source=pds_adoption_is_single_source)

self.ef = emissionsfactors.ElectricityGenOnGrid(ac=self.ac)
self.ef = emissionsfactors.ElectricityGenOnGrid(ac=self.ac,grid_emissions_version=1)

self.ua = unitadoption.UnitAdoption(ac=self.ac,
ref_total_adoption_units=self.tla_per_region,
Expand Down

This file was deleted.

Loading

0 comments on commit 3a2e4e0

Please sign in to comment.