Skip to content

Commit

Permalink
Merge pull request #45 from PaulTalbot-INL/vp_scaler
Browse files Browse the repository at this point in the history
Scalar Multiplier for Valued Params
  • Loading branch information
dylanjm authored Oct 15, 2020
2 parents 2e84471 + d0f1700 commit 442ca3a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
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

0 comments on commit 442ca3a

Please sign in to comment.