Skip to content

Commit

Permalink
splitting energySupplyComponents from newComponents.. Energy Supply m…
Browse files Browse the repository at this point in the history
…odels are not very useful for tespy I believe
  • Loading branch information
mrk committed Nov 2, 2023
1 parent 537c7f5 commit bed22e1
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 367 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import matplotlib.pyplot as plt

from tespy.components import Separator,Merge,CycleCloser,Valve,Splitter
from tespy.components.newcomponents import *
from tespy.components.energySupplyComponents import *

import logging
logging.basicConfig(level=logging.DEBUG)
Expand Down
3 changes: 2 additions & 1 deletion src/tespy/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
from .turbomachinery.turbine import Turbine # noqa: F401

# New components
from .newcomponents import *
from .newcomponents import *
from .energySupplyComponents import *
372 changes: 372 additions & 0 deletions src/tespy/components/energySupplyComponents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,372 @@
import logging

from tespy.components import Merge, Splitter
from tespy.tools.data_containers import ComponentProperties as dc_cp


# Fictious Energy Supply models (energy flows modelled as mass flows)
# No real use for tespy I guess

class MassFactorVCC(Splitter):

@staticmethod
def component():
return 'mass factor vapor compression cycle using COP for converting electricity to heat and cooling (energy flows modelled using tespy mass balances)'

def get_parameters(self):
variables = super().get_parameters()
variables["COP"] = dc_cp(
min_val=0,
deriv=self.COP_deriv,
func=self.COP_func,
latex=self.mass_flow_func_doc,
num_eq=1
)
return variables

def get_mandatory_constraints(self):
return {
'energy_balance_constraints': {
'func': self.energy_balance_func,
'deriv': self.energy_balance_deriv,
'constant_deriv': True, 'latex': self.energy_balance_func_doc,
'num_eq': self.num_o},
'pressure_constraints': {
'func': self.pressure_equality_func,
'deriv': self.pressure_equality_deriv,
'constant_deriv': True,
'latex': self.pressure_equality_func_doc,
'num_eq': self.num_i + self.num_o - 1}
}


def COP_func(self):
r"""
Equation for COP.
Returns
-------
residual : float
Residual value of equation.
.. math::
0 = p_\mathrm{in,1} \cdot pr - p_\mathrm{out,1}
"""
return self.inl[0].m.val_SI * self.COP.val - self.outl[0].m.val_SI

def COP_deriv(self, increment_filter, k):
r"""
Calculate the partial derivatives for combustion pressure ratio.
Parameters
----------
increment_filter : ndarray
Matrix for filtering non-changing variables.
k : int
Position of equation in Jacobian matrix.
"""
inl = self.inl[0]
outl = self.outl[0]
if inl.m.is_var:
self.jacobian[k, inl.m.J_col] = self.COP.val
if outl.m.is_var:
self.jacobian[k, outl.m.J_col] = -1

def calc_parameters(self):
super().calc_parameters()
self.COP.val = self.outl[0].m.val_SI / (self.outl[0].m.val_SI - (-self.outl[1].m.val_SI))



class MassFactorVCCWithPressureLoss(MassFactorVCC):

@staticmethod
def component():
return 'mass factor vapor compression cycle using COP for converting electricity to heat and cooling (energy flows modelled using tespy mass balances)'

def get_parameters(self):
variables = super().get_parameters()
variables["pr"] = dc_cp(
min_val=0,
deriv=self.pr_deriv,
func=self.pr_func,
latex=self.pr_func_doc,
num_eq=1
)
return variables

def pr_func(self):
r"""
Equation for pressure drop.
Returns
-------
residual : float
Residual value of equation.
.. math::
0 = p_\mathrm{in,1} \cdot pr - p_\mathrm{out,1}
"""
return self.inl[0].p.val_SI * self.pr.val - self.outl[0].p.val_SI

def pr_deriv(self, increment_filter, k):
r"""
Calculate the partial derivatives for combustion pressure ratio.
Parameters
----------
increment_filter : ndarray
Matrix for filtering non-changing variables.
k : int
Position of equation in Jacobian matrix.
"""
self.jacobian[k, 0, 1] = self.pr.val
self.jacobian[k, self.num_i, 1] = -1

def get_mandatory_constraints(self):
return {
'mass_flow_constraints': {
'func': self.mass_flow_func, 'deriv': self.mass_flow_deriv,
'constant_deriv': True, 'latex': self.mass_flow_func_doc,
'num_eq': 1},
'fluid_constraints': {
'func': self.fluid_func, 'deriv': self.fluid_deriv,
'constant_deriv': True, 'latex': self.fluid_func_doc,
'num_eq': self.num_o * self.num_nw_fluids},
'energy_balance_constraints': {
'func': self.energy_balance_func,
'deriv': self.energy_balance_deriv,
'constant_deriv': True, 'latex': self.energy_balance_func_doc,
'num_eq': self.num_o},
}

def calc_parameters(self):
super().calc_parameters()
self.pr.val = self.outl[0].p.val_SI / self.inl[0].p.val_SI
for i in range(self.num_i):
if self.inl[i].p.val < self.outl[0].p.val:
msg = (
f"The pressure at inlet {i + 1} is lower than the pressure "
f"at the outlet of component {self.label}."
)
logging.warning(msg)




class MassFactorLossModel(Splitter):

@staticmethod
def component():
return 'mass factor loss model for splitting energy flows (modelled using tespy mass balances)'

def get_parameters(self):
variables = super().get_parameters()
variables["Loss"] = dc_cp(
min_val=0,
deriv=self.Loss_deriv,
func=self.Loss_func,
latex=self.mass_flow_func_doc,
num_eq=1
)
return variables

def get_mandatory_constraints(self):
return {
'mass_flow_constraints': {
'func': self.mass_flow_func, 'deriv': self.mass_flow_deriv,
'constant_deriv': True, 'latex': self.mass_flow_func_doc,
'num_eq': 1},
'fluid_constraints': {
'func': self.fluid_func, 'deriv': self.fluid_deriv,
'constant_deriv': True, 'latex': self.fluid_func_doc,
'num_eq': self.num_o * self.num_nw_fluids},
'energy_balance_constraints': {
'func': self.energy_balance_func,
'deriv': self.energy_balance_deriv,
'constant_deriv': True, 'latex': self.energy_balance_func_doc,
'num_eq': self.num_o},
'pressure_constraints': {
'func': self.pressure_equality_func,
'deriv': self.pressure_equality_deriv,
'constant_deriv': True,
'latex': self.pressure_equality_func_doc,
'num_eq': self.num_i + self.num_o - 1}
}


def Loss_func(self):
return self.inl[0].m.val_SI * (1-self.Loss.val) - self.outl[0].m.val_SI

def Loss_deriv(self, increment_filter, k):
self.jacobian[k , 0, 0] = (1-self.Loss.val)
self.jacobian[k , self.num_i, 0] = -1

def calc_parameters(self):
super().calc_parameters()
self.Loss.val = (self.inl[0].m.val_SI - self.outl[0].m.val_SI)/self.inl[0].m.val_SI



class MassFactorLossModelWithPressureLoss(MassFactorLossModel):

@staticmethod
def component():
return 'mass factor loss model for splitting energy flows (modelled using tespy mass balances)'

def get_parameters(self):
variables = super().get_parameters()
variables["pr"] = dc_cp(
min_val=0,
deriv=self.pr_deriv,
func=self.pr_func,
latex=self.pr_func_doc,
num_eq=1
)
return variables

def pr_func(self):
r"""
Equation for pressure drop.
Returns
-------
residual : float
Residual value of equation.
.. math::
0 = p_\mathrm{in,1} \cdot pr - p_\mathrm{out,1}
"""
return self.inl[0].p.val_SI * self.pr.val - self.outl[0].p.val_SI

def pr_deriv(self, increment_filter, k):
r"""
Calculate the partial derivatives for combustion pressure ratio.
Parameters
----------
increment_filter : ndarray
Matrix for filtering non-changing variables.
k : int
Position of equation in Jacobian matrix.
"""
self.jacobian[k, 0, 1] = self.pr.val
self.jacobian[k, self.num_i, 1] = -1

def get_mandatory_constraints(self):
return {
'mass_flow_constraints': {
'func': self.mass_flow_func, 'deriv': self.mass_flow_deriv,
'constant_deriv': True, 'latex': self.mass_flow_func_doc,
'num_eq': 1},
'fluid_constraints': {
'func': self.fluid_func, 'deriv': self.fluid_deriv,
'constant_deriv': True, 'latex': self.fluid_func_doc,
'num_eq': self.num_o * self.num_nw_fluids},
'energy_balance_constraints': {
'func': self.energy_balance_func,
'deriv': self.energy_balance_deriv,
'constant_deriv': True, 'latex': self.energy_balance_func_doc,
'num_eq': self.num_o},
}

def calc_parameters(self):
super().calc_parameters()
self.pr.val = self.outl[0].p.val_SI / self.inl[0].p.val_SI
for i in range(self.num_i):
if self.inl[i].p.val < self.outl[0].p.val:
msg = (
f"The pressure at inlet {i + 1} is lower than the pressure "
f"at the outlet of component {self.label}."
)
logging.warning(msg)






class MergeEnergySupply(Merge):

@staticmethod
def component():
return 'merge without pressure/energy constraints'

def get_parameters(self):
variables = super().get_parameters()
return variables

def get_mandatory_constraints(self):
return {
'mass_flow_constraints': {
'func': self.mass_flow_func, 'deriv': self.mass_flow_deriv,
'constant_deriv': True, 'latex': self.mass_flow_func_doc,
'num_eq': 1},
'fluid_constraints': {
'func': self.fluid_func, 'deriv': self.fluid_deriv,
'constant_deriv': False, 'latex': self.fluid_func_doc,
'num_eq': self.num_nw_fluids},
}

class SplitterEnergySupply(Splitter):

@staticmethod
def component():
return 'Splitter without pressure/energy constraints'

def get_parameters(self):
variables = super().get_parameters()
return variables

def get_mandatory_constraints(self):
return {
'mass_flow_constraints': {
'func': self.mass_flow_func, 'deriv': self.mass_flow_deriv,
'constant_deriv': True, 'latex': self.mass_flow_func_doc,
'num_eq': 1},
'fluid_constraints': {
'func': self.fluid_func, 'deriv': self.fluid_deriv,
'constant_deriv': True, 'latex': self.fluid_func_doc,
'num_eq': self.num_o * self.num_nw_fluids},
}



class MassFactorVCCEnergySupply(MassFactorVCC):

@staticmethod
def component():
return 'mass factor vapor compression cycle using COP for converting electricity to heat and cooling (energy flows modelled using tespy mass balances, without pressure/enthalpy constraints)'

def get_parameters(self):
variables = super().get_parameters()
return variables


class MassFactorLossModelEnergySupply(MassFactorLossModel):

@staticmethod
def component():
return 'mass factor loss model for splitting energy flows (modelled using tespy mass balances, without pressure/enthalpy constraints)'

def get_parameters(self):
variables = super().get_parameters()
return variables

def get_mandatory_constraints(self):
return {
'mass_flow_constraints': {
'func': self.mass_flow_func, 'deriv': self.mass_flow_deriv,
'constant_deriv': True, 'latex': self.mass_flow_func_doc,
'num_eq': 1},
'fluid_constraints': {
'func': self.fluid_func, 'deriv': self.fluid_deriv,
'constant_deriv': True, 'latex': self.fluid_func_doc,
'num_eq': self.num_o * self.num_nw_fluids},
}
Loading

0 comments on commit bed22e1

Please sign in to comment.