Skip to content

Commit

Permalink
Fix bug of constant cache assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
enekomartinmartinez committed Aug 11, 2022
1 parent dbf96dd commit 8bb4365
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Bug fixes
~~~~~~~~~
- Fix bug generated when :EXCEPT: keyword is used with subscript subranges (:issue:`352`).
- Fix bug of precision error for :py:func:`pysd.py_backend.allocation.allocate_by_priority` (:issue:`353`).
- Fix bug of constant cache assignment.

Documentation
~~~~~~~~~~~~~
Expand Down
23 changes: 18 additions & 5 deletions pysd/py_backend/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ def __init__(self, py_model_file, params=None, return_func=None,
super().__init__()
self.time = time
self.time_initialization = time_initialization
# Initialize the cache object
self.cache = Cache()
# Python name of the object (for Macros)
self.py_name = py_name
# Booleans to avoid loading again external data or lookups
self.external_loaded = False
self.lookups_loaded = False
# Functions with constant cache
self._constant_funcs = set()
# Load model/macro from file and save in components
self.components = Components(str(py_model_file), self.set_components)

if __version__.split(".")[0]\
Expand All @@ -86,6 +92,7 @@ def __init__(self, py_model_file, params=None, return_func=None,
+ "Please translate again the model with the function"
+ " read_vensim or read_xmile.")

# Assing some protected attributes for easier access
self._namespace = self.components._components.component.namespace
self._dependencies =\
self.components._components.component.dependencies.copy()
Expand Down Expand Up @@ -135,13 +142,17 @@ def __init__(self, py_model_file, params=None, return_func=None,
if isinstance(getattr(self.components, name), HardcodedLookups)
]

# Load data files
if data_files:
self._get_data(data_files)

# Assign the cache type to each variable
self._assign_cache_type()
# Get the initialization order of Stateful elements
self._get_initialize_order()

if return_func is not None:
# Assign the return value of Macros
self.return_func = getattr(self.components, return_func)
else:
self.return_func = lambda: 0
Expand Down Expand Up @@ -297,21 +308,20 @@ def _get_full_dependencies(self, element, dep_set, stateful_deps):
self._get_full_dependencies(dep, dep_set, stateful_deps)

def _add_constant_cache(self):
self.constant_funcs = set()
for element, cache_type in self.cache_type.items():
if cache_type == "run":
self.components._set_component(
element,
constant_cache(getattr(self.components, element))
)
self.constant_funcs.add(element)
self._constant_funcs.add(element)

def _remove_constant_cache(self):
for element in self.constant_funcs:
for element in self._constant_funcs:
self.components._set_component(
element,
getattr(self.components, element).function)
self.constant_funcs = set()
self._constant_funcs.clear()

def _assign_cache_type(self):
"""
Expand Down Expand Up @@ -1149,8 +1159,11 @@ def run(self, params=None, return_columns=None, return_timestamps=None,
element: 1 for element in capture_elements["step"]
}
if cache_output:
# udate the cache type taking into account the outputs
self._assign_cache_type()
self._add_constant_cache()

# add constant cache to thosa variable that are constants
self._add_constant_cache()

# Run the model
self.time.stage = 'Run'
Expand Down

0 comments on commit 8bb4365

Please sign in to comment.