Skip to content

Commit

Permalink
Merge pull request #3 from CIRED/featureBranch
Browse files Browse the repository at this point in the history
Retrofitting obligation
  • Loading branch information
lucas-vivier authored May 16, 2022
2 parents e17dcc7 + 2903af1 commit faf999c
Show file tree
Hide file tree
Showing 92 changed files with 22,319 additions and 900 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,21 @@ dmypy.json
/project/input/test/
/project/input/scenarios/
/project/nb_tutorials/_old/
/project/input/phebus/config_files/
/project/input/phebus_30/config_files/
/project/input/backtesting/data_preparation_1984/
/project/nb_tutorials/
/project/input/_old/
/project/input/abattement_curve/data_preparation.xlsx
/project/cost_curve/input/data_preparation.xlsx
/documentation/development/
/project/input/phebus/data_preparation_phebus.xlsx
/project/input/phebus_30/data_preparation_phebus.xlsx
/project/nb_tutorials/abattement_curve_comparison.ipynb
/project/nb_examples/
/project/input/sdes/
/project/input/consumption_ceren.csv
/project/input/backtesting/
/project/input/abattement_curve/
/project/cost_curve/input/

/.DS_Store/

/project/input/phebus_31/
/project/input/graph_adhoc/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ A simple user interface is available on http://resirf.pythonanywhere.com/ to giv

**Step 1**: Git **clone Res-IRF folder** in your computer.
- Use your terminal and go to a location where you want to store the Res-IRF project.
- `git clone https://github.com/lucas-vivier/Res-IRF.git`
- `git clone https://github.com/CIRED/Res-IRF.git`

**Step 2**: **Create a conda environment** from the environment.yml file:
- The environment.yml file is in the Res-IRF folder.
Expand Down
1,028 changes: 636 additions & 392 deletions project/buildings.py

Large diffs are not rendered by default.

89 changes: 52 additions & 37 deletions project/cost_curve/cost_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
from multiprocessing import Process
import datetime
from itertools import product
import sys

from graphs import cumulated_emission_cost_plot, cost_cumulated_emission_plots

sys.path.append('project')

from buildings import HousingStock
from parse_input import parse_json, apply_linear_rate, final2consumption
from utils import reindex_mi
from ui_abattement_curve import *


def prepare_input(config):
Expand Down Expand Up @@ -112,24 +116,23 @@ def select_final_state(df, energy_performance, dict_replace=None):
return ds_status_quo


def to_result(carbon_cost, emission_final_end, emission_trend_end, potential_emission_saving, emission_ini, stock,
def to_result(carbon_cost, emission_scenario_end, emission_baseline, potential_emission_saving, emission_ini, stock,
path, yr, name='', dict_replace=None, energy_performance='A', horizon=30, private_carbon_cost=None,
health_carbon_cost=None, lost_carbon_cost=None):
health_carbon_cost=None, lost_carbon_cost=None, lcc_saving=None):
"""Formatting results.
Parameters
----------
carbon_cost: pd.DataFrame
Social carbon cost by agent archetype and possible final state.
emission_final_end: pd.DataFrame
Emission (tCO2) by agent archetype in the prospective scenario scenario to the horizon (final year) and
possible final state.
emission_trend_end: pd.Series
Emission (tCO2) by agent archetype in the trend scenario scenario to the horizon (final year).
emission_scenario_end: pd.DataFrame
Last year emission (tCO2/year) for all renovation scenario (performance final, energy final).
emission_baseline: pd.DataFrame
Emission (tCO2/year) in the baseline scenario.
potential_emission_saving
Emission (tCO2) by agent archetype in the trend scenario scenario to the horizon.
Emission (MtCO2) by agent archetype in the trend scenario to the horizon.
emission_ini
Initial Emission (tCO2) by agent archetype (first year).
First year emission (tCO2/year).
stock: pd.Series
path: str
Scenario folder path.
Expand All @@ -142,6 +145,7 @@ def to_result(carbon_cost, emission_final_end, emission_trend_end, potential_emi
private_carbon_cost : pd.DataFrame, optional
health_carbon_cost : pd.DataFrame, optional
lost_carbon_cost : pd.DataFrame, optional
lcc_saving : pd.DataFrame, optional
Returns
-------
Expand All @@ -151,19 +155,24 @@ def to_result(carbon_cost, emission_final_end, emission_trend_end, potential_emi
name = '{}_{}_{}.csv'.format(energy_performance, name.lower(), yr)
path = os.path.join(path, name)

emission_final_end = select_final_state(emission_final_end, energy_performance, dict_replace=dict_replace)
emission_baseline = emission_baseline / 10**6
emission_baseline_total = emission_baseline.sum().sum()

emission_scenario_end = select_final_state(emission_scenario_end, energy_performance, dict_replace=dict_replace)

# gCO2/m2 -> tCO2
emission_final_end_ref = emission_final_end.sum()
emission_diff = (emission_ini - emission_final_end) / 10**6
emission_total_reference = emission_ini.sum() / 10**6
emission_scenario_end_ref = emission_scenario_end.sum()
emission_diff = (emission_ini - emission_scenario_end) / 10**6
emission_total_ini = emission_ini.sum() / 10**6

output = dict()
output['Carbon cost (euro/tCO2)'] = select_final_state(carbon_cost, energy_performance, dict_replace=dict_replace)

if private_carbon_cost is not None:
output['Private carbon cost (euro/tCO2)'] = select_final_state(private_carbon_cost, energy_performance,
dict_replace=dict_replace)
output['LCC saving (euro/m2)'] = select_final_state(lcc_saving, energy_performance,
dict_replace=dict_replace)

if health_carbon_cost is not None:
output['Health carbon cost (euro/tCO2)'] = select_final_state(health_carbon_cost, energy_performance,
Expand All @@ -173,29 +182,33 @@ def to_result(carbon_cost, emission_final_end, emission_trend_end, potential_emi
output['Opportunity carbon cost (euro/tCO2)'] = select_final_state(lost_carbon_cost, energy_performance,
dict_replace=dict_replace)

output['Potential emission saving (tCO2)'] = select_final_state(potential_emission_saving, energy_performance, dict_replace=dict_replace)
output['Potential emission saving (tCO2/yr)'] = output['Potential emission saving (tCO2)'] / horizon
output['Emission difference (tCO2/yr)'] = emission_diff
output['Emission final horizon (tCO2/yr)'] = emission_final_end
output['Emission trend horizon (tCO2/yr)'] = emission_trend_end
output['Potential emission saving (MtCO2)'] = select_final_state(potential_emission_saving, energy_performance,
dict_replace=dict_replace)
output['Potential emission saving (MtCO2/yr)'] = output['Potential emission saving (MtCO2)'] / horizon
output['Emission initial (MtCO2/yr)'] = emission_ini / 10**6
output['Emission baseline end (MtCO2/yr)'] = emission_baseline.iloc[:, -1]
output['Emission scenario end (MtCO2/yr)'] = emission_scenario_end / 10**6
output['Emission difference (MtCO2/yr)'] = emission_diff

output['Dwelling number'] = stock

output = pd.DataFrame(output)

# output = output[output['Potential emission saving (tCO2)'] > 0]
output.loc[output['Potential emission saving (tCO2)'] <= 0, 'Carbon cost (euro/tCO2)'] = float('nan')
output.loc[output['Potential emission saving (MtCO2)'] <= 0, 'Carbon cost (euro/tCO2)'] = float('nan')

output = output.reorder_levels(
['Occupancy status', 'Housing type', 'Income class', 'Energy performance', 'Heating energy'])
output = output.sort_values('Carbon cost (euro/tCO2)')

output['Cumulated potential emission saving (tCO2)'] = output['Potential emission saving (tCO2/yr)'].cumsum()
output['Cumulated potential emission saving (%)'] = output[
'Cumulated potential emission saving (tCO2)'] / emission_total_reference
output['Cumulated potential emission saving (MtCO2/yr)'] = output['Potential emission saving (MtCO2/yr)'].cumsum()
output['Cumulated potential emission saving (%/2018)'] = output['Cumulated potential emission saving (MtCO2/yr)'] / emission_total_ini

output['Cumulated potential emission saving (MtCO2)'] = output['Potential emission saving (MtCO2)'].cumsum()
output['Cumulated potential emission saving (%/baseline)'] = output['Cumulated potential emission saving (MtCO2)'] / emission_baseline_total

output['Cumulated emission difference (tCO2)'] = output['Emission difference (tCO2/yr)'].cumsum()
output['Cumulated emission difference (%)'] = output[
'Cumulated emission difference (tCO2)'] / emission_total_reference
output['Cumulated emission difference (MtCO2/yr)'] = output['Emission difference (MtCO2/yr)'].cumsum()
output['Cumulated emission difference (%/2018)'] = output['Cumulated emission difference (MtCO2/yr)'] / emission_total_ini

output['Cumulated dwelling number'] = output['Dwelling number'].cumsum()
output['Cumulated dwelling number (%)'] = output['Cumulated dwelling number'] / stock.sum()
Expand Down Expand Up @@ -347,12 +360,12 @@ def to_carbon_cost(config, path, calibration_year):

for t in transition:
emission_final = emission_final.stack('{} final'.format(t))
emission_final_end = emission_final.iloc[:, -1]
emission_scenario_end = emission_final.iloc[:, -1]
for t in transition:
emission_final_end = emission_final_end.unstack('{} final'.format(t))
emission_scenario_end = emission_scenario_end.unstack('{} final'.format(t))

# emission flow during last year on the baseline scenario
emission_trend_end = emission.iloc[:, -1] * buildings.stock * buildings.to_area()
emission_baseline = (emission.T * (buildings.stock * buildings.to_area())).T

list_energy_transition = [
{'2Power': {'Natural gas': 'Power', 'Oil fuel': 'Power', 'Wood fuel': 'Power'}},
Expand All @@ -366,11 +379,11 @@ def to_carbon_cost(config, path, calibration_year):

name = list(energy_transition.keys())[0]

to_result(carbon_cost, emission_final_end, emission_trend_end, potential_emission_saving, emission_ini,
to_result(carbon_cost, emission_scenario_end, emission_baseline, potential_emission_saving, emission_ini,
buildings.stock, path, calibration_year, name=name, dict_replace=energy_transition[name],
energy_performance=performance_transition, horizon=horizon,
private_carbon_cost=private_carbon_cost, health_carbon_cost=health_carbon_cost,
lost_carbon_cost=lost_carbon_cost)
lost_carbon_cost=lost_carbon_cost, lcc_saving=lcc_saving)


if __name__ == '__main__':
Expand All @@ -380,11 +393,10 @@ def to_carbon_cost(config, path, calibration_year):

args = parser.parse_args()

name_file = os.path.join(args.name)
with open(name_file) as file:
with open(os.path.join(args.name)) as file:
config = json.load(file)

path = os.path.join('project/output', datetime.datetime.today().strftime('%Y%m%d_%H%M%S'))
path = os.path.join('project/cost_curve/output', datetime.datetime.today().strftime('%Y%m%d_%H%M%S'))
if not os.path.isdir(path):
os.mkdir(path)

Expand All @@ -409,10 +421,13 @@ def to_carbon_cost(config, path, calibration_year):

cost_cumulated_emission_plots(scenarios_dict,
save=os.path.join(path_key, 'cost_emission_end_{}.png'.format(key.lower())),
graph='Cumulated emission difference (%)')
graph='Cumulated emission difference (%/2018)')
cost_cumulated_emission_plots(scenarios_dict,
save=os.path.join(path_key, 'cost_potential_emission_ini_{}.png'.format(key.lower())),
graph='Cumulated potential emission saving (%/2018)')
cost_cumulated_emission_plots(scenarios_dict,
save=os.path.join(path_key, 'cost_potential_emission_{}.png'.format(key.lower())),
graph='Cumulated potential emission saving (%)')
save=os.path.join(path_key, 'cost_potential_emission_baseline_{}.png'.format(key.lower())),
graph='Cumulated potential emission saving (%/baseline)')
cost_cumulated_emission_plots(scenarios_dict,
save=os.path.join(path_key, 'cost_dwelling_{}.png'.format(key.lower())),
graph='Cumulated dwelling number (%)')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ def cumulated_emission_cost_plot(data, graph, add_title=''):
fig.suptitle(title, fontweight='bold')
fig.subplots_adjust(top=0.85)

if graph == 'Cumulated potential emission saving (%)':
if graph == 'Cumulated potential emission saving (%/2018)':
xlabel = "Potentiel de réduction\n % Émission secteur résidentiel 2018"
x = data[graph]
elif graph == 'Cumulated potential emission saving (%/baseline)':
xlabel = "Potentiel de réduction\n % Émission secteur résidentiel"
x = data[graph]
elif graph == 'Cumulated dwelling number (%)':
xlabel = 'Potentiel\n Millions logements'
x = data[graph]
Expand All @@ -50,7 +53,7 @@ def cumulated_emission_cost_plot(data, graph, add_title=''):
plt.show()


def cost_cumulated_emission_plots(scenarios_dict, addtitle=None, legtitle=None, save=None, xlim=(0, 2000),
def cost_cumulated_emission_plots(scenarios_dict, addtitle='', legtitle=None, save=None, xlim=(0, 2000),
graph='Cumulated emission difference (%)'):
"""
Cumulated potential reduction of emission function of CO2 cost (sorted from lower to higher cost)
Expand All @@ -63,8 +66,8 @@ def cost_cumulated_emission_plots(scenarios_dict, addtitle=None, legtitle=None,
----------
scenarios_dict: dict
dict that contains one pd.DataFrame by scenarios
graph: {'Cumulated potential emission saving (%)', 'Cumulated emission difference (%)',
'Cumulated dwelling number (%)'}
graph: {'Cumulated potential emission saving (%/2018)', 'Cumulated emission difference (%/2018)',
Cumulated potential emission saving (%/baseline)', 'Cumulated dwelling number (%)'}
addtitle: str
legtitle: str
"""
Expand Down Expand Up @@ -92,7 +95,8 @@ def cost_cumulated_emission_plots(scenarios_dict, addtitle=None, legtitle=None,
if xlim is not None:
ax.set_xlim(xlim[0], xlim[1])

if graph in ['Cumulated potential emission saving (%)', 'Cumulated emission difference (%)']:
if graph in ['Cumulated potential emission saving (%/2018)', 'Cumulated emission difference (%/2018)',
'Cumulated potential emission saving (%/baseline)']:
ylabel = "Potentiel de réduction\n % Émission secteur résidentiel 2018"
elif graph == 'Cumulated dwelling number (%)':
ylabel = 'Potentiel\n Millions logements'
Expand Down
36 changes: 36 additions & 0 deletions project/elasticity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

import pandas as pd
from numpy.random import normal
from itertools import product

energy_prices = pd.read_csv('project/input/sdes_40/energy_prices_2018.csv', index_col=[0], header=[0])
year = 2020

lambda_1 = [0.6, 0.65, 0.7, 0.75]
lambda_2 = [0.85, 0.9, 0.95, 0.97]

scale = (energy_prices.loc[year, :] / 10)
mean = pd.Series(0, index=scale.index)
epsilon = pd.DataFrame(normal(loc=mean, scale=scale, size=(10, 4)), columns=scale.index)

idx = epsilon.index
scenarios = list(product(lambda_1, lambda_2, idx))

price = dict()
for scenario in scenarios:
l_1 = scenario[0]
l_2 = scenario[1]

nu = energy_prices.loc[year, :] - l_1 * energy_prices.loc[year-1, :] - l_2 * energy_prices.loc[year-2, :]

eps = epsilon.loc[scenario[2]]

temp = energy_prices.loc[[year - 2, year - 1, year], :]
for y in range(year + 1, 2081):
temp.loc[y, :] = l_1 * temp.loc[y - 1, :] + l_2 * temp.loc[y - 2, :] + nu + eps


price[scenario] = temp
break

print('break')
Loading

0 comments on commit faf999c

Please sign in to comment.