Skip to content

Commit

Permalink
Refactor codes
Browse files Browse the repository at this point in the history
- Categorize constraint definitions based on type (co2, cost, demand, generation, hydro, investment, nondispatchable, storage, transmission) for better organization.
- Split `rule.py` class into serveral smaller, focused classes according to categorized constraint definitions.
- Simplify model by replacing intermediate constraints with direct expressions.
- Extract new modules `solver.py`, `output_data.py`, and `set_up.py` from `run.py` and `utils.py`
- Move `parameters.py` into `set_up.py`

This improves readability and maintainability of the codebase.
  • Loading branch information
github-actions[bot] committed Jul 10, 2024
1 parent 2655a75 commit 492cb9e
Show file tree
Hide file tree
Showing 20 changed files with 2,471 additions and 2,270 deletions.
125 changes: 125 additions & 0 deletions prepshot/_model/co2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

""" This module contains constraints related to carbon emissions.
"""

import pyoptinterface as poi
import numpy as np

class AddCo2EmissionConstraints:
"""Class for carbon emission constraints and calculations.
"""
def __init__(self, model):
"""Initialize the class.
Parameters
----------
model : pyoptinterface._src.solver.Model
Model index.
para : dict
Dictionary containing parameters.
"""
self.model = model
model.carbon_breakdown = poi.make_tupledict(
model.year_zone_tech_tuples,
rule=self.carbon_breakdown
)
model.carbon_capacity = poi.make_tupledict(
model.year_zone_tuples,
rule=self.emission_calc_by_zone_rule
)
model.carbon = poi.make_tupledict(
model.year,
rule=self.emission_calc_rule
)
model.emission_limit_cons = poi.make_tupledict(
model.year,
rule=self.emission_limit_rule
)

def emission_limit_rule(self, y):
"""Annual carbon emission limits across all zones and technologies.
Parameters
----------
y : int
Planned year.
Returns
-------
pyoptinterface._src.core_ext.ConstraintIndex
Constraint index of the model.
"""
model = self.model
limit = model.para['carbon_emission_limit']
if limit[y] == np.Inf:
return None
lhs = model.carbon[y] - limit[y]
return model.add_linear_constraint(lhs, poi.Leq, 0)

def emission_calc_rule(self, y):
"""Calculation of annual carbon emission across all zones and
technologies.
Parameters
----------
y : int
Planned year.
Returns
-------
pyoptinterface._src.core_ext.ConstraintIndex
Constraint index of the model.
"""
model = self.model
return poi.quicksum(
model.carbon_capacity[y, z]
for z in model.zone
)

def emission_calc_by_zone_rule(self, y, z):
"""Calculation of annual carbon emissions by zone.
Parameters
----------
y : int
Planned year.
z : str
Zone.
Returns
-------
pyoptinterface._src.core_ext.ConstraintIndex
Constraint index of the model.
"""
model = self.model
return poi.quicksum(
model.carbon_breakdown[y, z, te]
for te in model.tech
)

def carbon_breakdown(self, y, z, te):
"""Carbon emission cost breakdown.
Parameters
----------
y : int
Year.
z : str
Zone.
te : str
Technology.
Returns
-------
pyoptinterface._src.core_ext.ExprBuilder
index of expression of the model.
"""
model = self.model
ef = model.para['emission_factor'][te, y]
dt = model.para['dt']
return poi.quicksum(
ef * model.gen[h, m, y, z, te] * dt
for h, m in model.hour_month_tuples
)
Loading

0 comments on commit 492cb9e

Please sign in to comment.