From 7850592065c2e9a312c22e60ae8818c26ca1bff3 Mon Sep 17 00:00:00 2001 From: "christoph.von.oy" <christoph.von.oy@rwth-aachen.de> Date: Wed, 15 May 2024 13:34:26 +0200 Subject: [PATCH] extract_results uses resample_into --- optimization_model.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/optimization_model.py b/optimization_model.py index 316a2fe..a529d6a 100644 --- a/optimization_model.py +++ b/optimization_model.py @@ -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 -- GitLab