Skip to content
Snippets Groups Projects
Commit 78505920 authored by Christoph von Oy's avatar Christoph von Oy
Browse files

extract_results uses resample_into

parent ffc4e400
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from Model_Library.dynamics import Profile
from Model_Library.dynamics import Profile, resample_into
from enum import Enum
import numpy as np
......@@ -150,10 +150,13 @@ class EntityResult:
self.var_kinds[var_name] = ('i_prime_result', n_i_prime_vars)
n_i_prime_vars += 1
self.n_u_vars = n_u_vars
self.u_result = np.empty(n_u_vars, dtype=float)
self.n_i_vars = n_i_vars
self.i_result = np.empty((n_i_vars, dynamic.shape()), dtype=float)
self.n_i_prime_vars = n_i_prime_vars
self.i_prime_result = np.empty((n_i_prime_vars, dynamic.state_shape()), dtype=float)
self.dynamic = dynamic
......@@ -161,17 +164,23 @@ class EntityResult:
self.sub_results = dict()
def extract_result(self, block):
if self.dynamic != block.dynamic:
raise ValueError('Cannot extract results from a block into a EntitResult with a different dynamic!')
# TODO: when the resampling is simple copying, then detect that here and extract in the loop over self.var_kinds the values directly into the EntityResult instead of creating new ndarrays
u_values = np.empty(self.n_u_vars, dtype=float)
i_values = np.empty((self.n_i_vars, block.dynamic.shape()), dtype=float)
i_prime_values = np.empty((self.n_i_prime_vars, block.dynamic.state_shape()), dtype=float)
for var_name, (var_kind, index) in self.var_kinds.items():
if var_kind == 'i_result':
values = block.component_dict[var_name].get_values()
self.i_result[index] = np.fromiter((values[t] for t in block.T), float)
value_object = block.component_dict[var_name].get_values()
i_values[index] = np.fromiter((value_object[t] for t in block.T), float)
elif var_kind == 'u_result':
self.u_result[index] = pyo.value(block.component_dict[var_name])
u_values[index] = pyo.value(block.component_dict[var_name])
elif var_kind == 'i_prime_result':
values = block.component_dict[var_name].get_values()
self.i_prime_result[index] = np.fromiter((values[t] for t in block.T_prime), float)
value_object = block.component_dict[var_name].get_values()
i_prime_values[index] = np.fromiter((value_object[t] for t in block.T_prime), float)
self.u_result = u_values
resample_into(i_values, block.dynamic, self.i_result, self.dynamic)
resample_into(i_prime_values, block.dynamic, self.i_prime_result, self.dynamic)
def add_sub_result(self, key, result):
self.sub_results[key] = result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment