Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalar Multiplier for Valued Params #45

Merged
merged 3 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/ValuedParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def get_input_specs(cls, name, disallowed=None):
disallowed = []
spec = InputData.parameterInputFactory(name,
descr=r"""This value can be taken from any \emph{one} of the sources described below.""")
spec.addSub(InputData.parameterInputFactory('multiplier', contentType=InputTypes.FloatType,
descr=r"""Multiplies any value obtained by this parameter by the given value. \default{1}"""))
# for when the value is fixed (takes precedence over "sweep" and "opt") ...
spec.addSub(InputData.parameterInputFactory('fixed_value', contentType=InputTypes.FloatType,
descr=r"""indicates this value is a fixed value, given by the value of this node."""))
Expand Down Expand Up @@ -100,6 +102,7 @@ def __init__(self, name):
self._value = None # used for fixed values
self._growth_val = None # used to grow the value year-by-year
self._growth_mode = None # mode for growth (e.g. exponenetial, linear)
self._scalar = None # scaling for values obtained
self._coefficients = {} # for (linear) coefficents, resource: coefficient

def get_growth(self):
Expand Down Expand Up @@ -173,7 +176,6 @@ def evaluate(self, inputs, target_var=None, aliases=None):
"""
if aliases is None:
aliases = {}
ret = None
if self.type == 'value':
value = {target_var: self._value}
elif self.type == 'ARMA':
Expand All @@ -184,13 +186,14 @@ def evaluate(self, inputs, target_var=None, aliases=None):
value = self._evaluate_linear(inputs, target_var, aliases)
elif self.type == 'Function':
# directly set the return, unlike the other types
ret = self._evaluate_function(inputs, aliases)
value, inputs = self._evaluate_function(inputs, aliases)
else:
raise RuntimeError('Unrecognized data source:', self.type)
# if the return dict wasn't auto-created, create it now
if ret is None:
ret = (value, inputs)
return ret
# apply scaling factor
if self._scalar is not None:
for key in value:
value[key] *= self._scalar
return (value, inputs)

def _load(self, comp_name, item, mode, alias_dict):
"""
Expand All @@ -215,8 +218,13 @@ def _load(self, comp_name, item, mode, alias_dict):
self._growth_val = growth.value
self._growth_mode = growth.parameterValues['mode']

# handle scaling factor
scalar = item.findFirst('multiplier')
if scalar is not None:
self._scalar = scalar.value

# find type of contents provided
given = list(x.getName() for x in item.subparts if x.getName() != 'growth')
given = list(x.getName() for x in item.subparts if x.getName() not in ['growth', 'multiplier'])

has_vals = any(g in self.valued_methods for g in given)
has_arma = any(g == 'ARMA' for g in given)
Expand Down
17 changes: 2 additions & 15 deletions tests/integration_tests/production_flex/heron_input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@
</produces>
<economics>
<lifetime>27</lifetime>
<!--<CashFlow name="capex" type="one-time" taxable='True' inflation='none' mult_target='False'>
<driver>
<Function method="electric_consume">transfers</Function>
</driver>
<reference_price>
<fixed_value>1</fixed_value>
</reference_price>
<reference_driver>
<fixed_value>1</fixed_value>
</reference_driver>
<scaling_factor_x>
<fixed_value>1</fixed_value>
</scaling_factor_x>
</CashFlow>-->
</economics>
</Component>

Expand Down Expand Up @@ -98,7 +84,7 @@
<CashFlow name="e_sales" type="repeating" taxable='True' inflation='none' mult_target='False'>
<driver>
<Function method="electric_consume">transfers</Function>
<!-- <resource multiplier="-1" absolute="True">electricity</resource> -->
<multiplier>-1</multiplier>
</driver>
<reference_price>
<fixed_value>0.5</fixed_value>
Expand All @@ -124,6 +110,7 @@
<CashFlow name="e_sales" type="repeating" taxable='True' inflation='none' mult_target='False'>
<driver>
<Function method="electric_consume">transfers</Function>
<multiplier>-1</multiplier>
</driver>
<reference_price>
<Function method="flex_price">transfers</Function>
Expand Down
27 changes: 16 additions & 11 deletions tests/integration_tests/production_flex/transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@
"""

def electric_consume(data, meta):
## works with generic?
# activity = meta['raven_vars']['HERON_pyomo_model']
# t = meta['t']
# flip sign because we consume the electricity
# E = -1.0 * activity['electricity'][t]

## works with pyomo
# model = meta['HERON']['pyomo_model']
# component = meta['HERON']['component']
"""
Provides the amount of electricity consumed.
@ In, data, dict, request for data
@ In, meta, dict, state information
@ Out, data, dict, filled data
@ In, meta, dict, state information
"""
activity = meta['HERON']['activity']
# TODO a get_activity method for the dispatcher -> returns object-safe activity (expression or value)?
amount = -1 * activity['electricity']
amount = activity['electricity']
# NOTE multiplier is -1 in the input!
data = {'driver': amount}
return data, meta

def flex_price(data, meta):
"""
Determines the price of electricity.
@ In, data, dict, request for data
@ In, meta, dict, state information
@ Out, data, dict, filled data
@ In, meta, dict, state information
"""
sine = meta['HERON']['RAVEN_vars']['Signal']
t = meta['HERON']['time_index']
# DispatchManager
# scale electricity consumed to flex between -1 and 1
amount = - 2 * (sine[t] - 0.5)
data = {'reference_price': amount}
Expand Down