Skip to content

Commit 337c9b5

Browse files
committed
fix simulation command arguments
* all commands now take model registries, progress hook, args and kwargs * remove layers mod and pgk attr from meta class, not used anymore * add command layer name class attribute to Model - used to denote which class takes commands * use _listify * also fix layer declared in model as classes instead of strings in _initialize(), loop over values and make new value dictionary with __name__ and __module__ * add cmd_layer and commands properties *
1 parent e18ea2a commit 337c9b5

3 files changed

Lines changed: 51 additions & 23 deletions

File tree

carousel/core/models.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ class attributes.
3939
"""
4040
_path_attr = 'modelpath'
4141
_file_attr = 'modelfile'
42-
_layers_mod_attr = 'layers_mod'
43-
_layers_pkg_attr = 'layers_pkg'
4442
_layers_cls_attr = 'layer_cls_names'
4543

4644
def __new__(mcs, name, bases, attr):
@@ -88,6 +86,8 @@ class Model(object):
8886
layers_mod = LAYERS_MOD
8987
#: package with layers module
9088
layers_pkg = LAYERS_PKG
89+
#: simulation layer
90+
cmd_layer_name = 'simulations'
9191

9292
def __init__(self, modelfile=None):
9393
# check for modelfile in meta class, but use argument if not None
@@ -148,7 +148,7 @@ def _load(self, layer=None):
148148
self.model = _model
149149
else:
150150
# convert non-sequence to tuple
151-
layers = layer if isinstance(layer, (list, tuple)) else (layer, )
151+
layers = _listify(layer)
152152
# update/load layers
153153
for layer in layers:
154154
self.model[layer] = _model[layer]
@@ -182,6 +182,26 @@ def _initialize(self):
182182
# from layers module get the layer's class definition
183183
layer_cls = getattr(mod, self.layer_cls_names[layer]) # class def
184184
self.layers[layer] = layer_cls # add layer class def to model
185+
# check if model layers are classes
186+
src_value = {} # layer value generated from source classes
187+
for src in value:
188+
# skip if not a source class
189+
if isinstance(src, basestring):
190+
continue
191+
# check if source has keyword arguments
192+
try:
193+
src, kwargs = src
194+
except TypeError:
195+
kwargs = {} # no key work arguments
196+
# generate layer value from source class
197+
src_value[src.__name__] = {'module': src.__module__,
198+
'package': None}
199+
# update layer keyword arguments
200+
src_value[src.__name__].update(kwargs)
201+
# use layer values generated from source class
202+
if src_value:
203+
value = src_value
204+
# FIXME: update model with dictionary, can't save
185205
# set layer attribute with model data
186206
setattr(self, layer, layer_cls(value))
187207
self._update()
@@ -285,9 +305,17 @@ def save(self, modelfile, layer=None):
285305

286306
@property
287307
def registries(self):
288-
return {('%s_reg' % layer): getattr(self, '%s' % layer).reg
308+
return {layer: getattr(self, layer).reg
289309
for layer in self.layers}
290310

311+
@property
312+
def cmd_layer(self):
313+
return getattr(self, self.cmd_layer_name, NotImplemented)
314+
315+
@property
316+
def commands(self):
317+
return self.cmd_layer.reg.commands
318+
291319
def command(self, cmd, progress_hook=None, *args, **kwargs):
292320
"""
293321
Execute a model command.
@@ -298,8 +326,7 @@ def command(self, cmd, progress_hook=None, *args, **kwargs):
298326
cmds = cmd.split(None, 1) # split commands and simulations
299327
sim_names = cmds[1:] # simulations
300328
if not sim_names:
301-
sim_names = self.model['simulations'].iterkeys()
302-
kwargs.update(self.registries, progress_hook=progress_hook)
329+
sim_names = self.cmd_layer.reg.iterkeys()
303330
for sim_name in sim_names:
304-
sim_cmd = getattr(self.simulations.reg[sim_name], cmd)
305-
sim_cmd(*args, **kwargs)
331+
sim_cmd = getattr(self.cmd_layer.reg[sim_name], cmd)
332+
sim_cmd(self.registries, progress_hook=progress_hook, *args, **kwargs)

carousel/core/simulations.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,28 @@ def index_iterator(self):
162162

163163
# TODO: change start to run
164164

165-
def start(self, data_reg, formula_reg, out_reg, calc_reg,
166-
progress_hook=None):
165+
def start(self, registries, progress_hook=None):
167166
"""
168167
Start the simulation from time zero.
169168
170-
:param data_reg: Data registry.
171-
:type data_reg:
172-
:class:`~carousel.core.data_sources.DataRegistry`
173-
:param formula_reg: Formula registry.
174-
:type formula_reg:
175-
:class:`~carousel.core.formulas.FormulaRegistry`
176-
:param out_reg: Outputs registry.
177-
:type out_reg:
178-
:class:`~carousel.core.outputs.OutputRegistry`
179-
:param calc_reg: Calculation registry.
180-
:type calc_reg:
181-
:class:`~carousel.core.calculation.CalcRegistry`
169+
:param registries: Model registries with layer parameters.
170+
:type: dict
182171
:param progress_hook: A function that receives either a string or a
183172
list containing the index followed by tuples of the data or outputs
184173
names and values specified by ``write_fields`` in the simfile.
185174
:type progress_hook: function
175+
176+
177+
The model registries should contain the following layer registries:
178+
* :class:`~carousel.core.data_sources.DataRegistry`,
179+
* :class:`~carousel.core.formulas.FormulaRegistry`,
180+
* :class:`~carousel.core.outputs.OutputRegistry`,
181+
* :class:`~carousel.core.calculation.CalcRegistry`
186182
"""
183+
data_reg = registries['data']
184+
formula_reg = registries['formulas']
185+
out_reg = registries['outputs']
186+
calc_reg = registries['calculations']
187187
# initialize
188188
if not self.isinitialized:
189189
self.initialize(calc_reg)
@@ -338,7 +338,7 @@ def format_write(self, data_reg, out_reg, idx=None):
338338
out_args = [out_reg[f][:idx] for f in out_fields]
339339
return np.concatenate(data_args + out_args, axis=1)
340340

341-
def pause(self):
341+
def pause(self, **_):
342342
"""
343343
Pause the simulation. How is this different from stopping it? Maintain
344344
info sufficient to restart simulation. Sets ``is_paused`` to True.

carousel/tests/test_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@ class PVPowerSAPM3(Model):
122122
if __name__ == '__main__':
123123
m1 = PVPowerSAPM1()
124124
m2 = PVPowerSAPM2()
125+
m3 = PVPowerSAPM3()

0 commit comments

Comments
 (0)