carbatpy.helpers.opti_cycle_comp_helpers ======================================== .. py:module:: carbatpy.helpers.opti_cycle_comp_helpers .. autoapi-nested-parse:: Helper functions for optimizations of cycles, which use comp.py. Created on Mon Aug 4 11:39:05 2025 @author: atakan Universität Duisburg-Essen, Germany In the framework of the Priority Programme: "Carnot Batteries: Inverse Design from Markets to Molecules" (SPP 2403) https://www.uni-due.de/spp2403/ https://git.uni-due.de/spp-2403/residuals_weather_storage Functions --------- .. autoapisummary:: carbatpy.helpers.opti_cycle_comp_helpers.optimize_wf_heat_pump carbatpy.helpers.opti_cycle_comp_helpers.optimize_orc carbatpy.helpers.opti_cycle_comp_helpers.optimize_cb carbatpy.helpers.opti_cycle_comp_helpers.extract_cb_conf_from_x carbatpy.helpers.opti_cycle_comp_helpers.extract_optim_data_with_paths carbatpy.helpers.opti_cycle_comp_helpers.insert_optim_data carbatpy.helpers.opti_cycle_comp_helpers.parse_species_string carbatpy.helpers.opti_cycle_comp_helpers.extract_cycle_column_names_from_config carbatpy.helpers.opti_cycle_comp_helpers.extract_column_names_from_config Module Contents --------------- .. py:function:: optimize_wf_heat_pump(dir_name, config_0, bounds_0, **kwargs) Heat pump COP optimization, varying p_high and mixture composition p_low cannot be selected directly, it is determined by the storage temperature and the superheating temperature difference and the minimum approach temperature. In optimization, the low pressure (wanted) from the configuration yaml file is checked. If the calculated low pressure is below that value, the COP is weighted with the ratio. :param dir_name: either the path to the yaml file with all configuration parameters, or a dictionary with all values. :type dir_name: str or dict :param config_0: vales set here and deviating from dir_name. :type config_0: dict :param bounds_0: dictionary with the p_high bounds and the upper bounds of the first three molefractions. The remaining value is the difference to 1. It is checked that the sum is 1 and all vales are positive. :type bounds_0: dictTYPE :param \*\*kwargs: - 'optimize_global': select optimization algorith "dif_evol", "bas_hop", 'shgo', or local minimizer (Nelson Mead). :returns: * **result** (*OptimizeResult*) -- The result of the optimization. * **paths** (*list of lists*) -- from function extract_optim_data_with_paths. Needed to reconstruct the config_0 dictionary from the optimizer x-list. .. py:function:: optimize_orc(dir_name, cop, q_dot_h, config_0, bounds_0, **kwargs) ORC optimization, varying p_high and mixture composition etc. :param dir_name: either the path to the yaml file with all configuration parameters, or a dictionary with all values. :type dir_name: str or dict :param cop: COP of the charging heat pump; is needed to secure steady state discharging. :type cop: float :param q_dot_h: The heat flow from the high temperature storage to the working fluid in the evaporator. :type q_dot_h: float :param config_0: vales set here and deviating from dir_name. :type config_0: dict :param bounds_0: dictionary with the p_high bounds and the upper bounds of the first three molefractions. The remaining value is the difference to 1. It is checked that the sum is 1 and all vales are positive. :type bounds_0: dictTYPE :param \*\*kwargs: - 'optimize_global': select optimization algorith "dif_evol", "bas_hop", or local minimizer (Nelson Mead). :returns: * **result** (*OptimizeResult*) -- The result of the optimization. * **paths** (*list of lists*) -- from function extract_optim_data_with_paths. Needed to reconstruct the config_0 dictionary from the optimizer x-list. .. py:function:: optimize_cb(dir_names, configs, bounds, **kwargs) CB optimization, varying p_high and mixture composition etc. :param dir_name: either the path to the yaml file with all configuration parameters, or a dictionary with all values. :type dir_name: str or dict :param cop: COP of the charging heat pump; is needed to secure steady state discharging. :type cop: float :param q_dot_h: The heat flow from the high temperature storage to the working fluid in the evaporator. :type q_dot_h: float :param config_0: vales set here and deviating from dir_name. :type config_0: dict :param bounds_0: dictionary with the p_high bounds and the upper bounds of the first three molefractions. The remaining value is the difference to 1. It is checked that the sum is 1 and all vales are positive. :type bounds_0: dictTYPE :param \*\*kwargs: - 'optimize_global': select optimization algorith "dif_evol", "bas_hop", or local minimizer (Nelson Mead). - 'workers': the number of workers for the differential evolution algorithm. Integer, optional, default value is 1. - 'maxiter': maximum iterations of optimizers, integer, default value is 200. :returns: * **result** (*OptimizeResult*) -- The result of the optimization. * **paths** (*list of lists*) -- from function extract_optim_data_with_paths. Needed to reconstruct the config_0 dictionary from the optimizer x-list. .. py:function:: extract_cb_conf_from_x(x, configs_ini, paths) .. py:function:: extract_optim_data_with_paths(config, bounds) Extracts all optimizable parameter values, along with their bounds and hierarchical paths, from a (possibly nested) configuration dictionary for optimization (e.g., heat pump application). :param config: The (possibly nested) configuration dictionary containing initial values. :type config: dict :param bounds: Dictionary with bounds for all optimizable parameters, structured like config. :type bounds: dict :returns: * **x0** (*list*) -- A flat list of all initial parameter values to be optimized (e.g., p_high and mole fractions). * **bnds** (*list*) -- List of bounds tuples corresponding to all entries in x0. * **paths** (*list of lists*) -- Each entry is the hierarchical path (list of keys/indices) to the corresponding variable in config. Can be used to reconstruct the config dictionary from an x-list after optimization. .. rubric:: Example >>> x0, bnds, paths = extract_optim_data_with_paths(config, bounds) .. py:function:: insert_optim_data(config, x, paths) Reconstructs a configuration dictionary from a flat list of optimized values and corresponding paths, including special handling for mole fractions. For back conversion after optimization (scipy.optimize). :param config: The original configuration dictionary (will be deep-copied). :type config: dict :param x: The optimizer parameter vector (e.g., for p_high and first n-1 mole fractions). :type x: list or array :param paths: The list of hierarchical paths as produced by extract_optim_data_with_paths; describes where in config each value from x should be inserted. :type paths: list of lists :returns: **conf_new** -- A new dictionary with the updated parameter values from x inserted. For fields called 'fractions', the last value is set to 1 minus the sum of the others (to ensure the fractions sum to 1). :rtype: dict .. rubric:: Example >>> conf_new = insert_optim_data(config, x, paths) .. py:function:: parse_species_string(species_str) Converts a species string in format 'Propane * Butane * Pentane * Hexane' into a list without spaces and asterisks. :param species_str: Species string with asterisk separation :type species_str: str :returns: List of species names :rtype: list .. py:function:: extract_cycle_column_names_from_config(config_dict, paths) Extracts column names for optimization variables based on paths. Returns a single flat list with prefixed names for non-fraction variables. :param config_dict: Dictionary with configuration data (contains species names for fractions) Structure: config_dict["working_fluid"]["species"] = "Propane * Butane * ..." :type config_dict: dict :param paths: Dictionary with paths for each optimization :type paths: list :returns: Single flat list of column names for all optimization variables :rtype: list .. py:function:: extract_column_names_from_config(config_dict, paths_dict) Extracts column names for optimization variables based on paths. Returns a single flat list with prefixed names for non-fraction variables. :param config_dict: Dictionary with configuration data (contains species names for fractions) Structure: config_dict["working_fluid"]["species"] = "Propane * Butane * ..." :type config_dict: dict :param paths_dict: Dictionary with paths for each optimization group (e.g. "hp", "orc") :type paths_dict: dict :returns: Single flat list of column names for all optimization variables :rtype: list