thermopt.optimize_cycle module#

class thermopt.optimize_cycle.ThermodynamicCycleOptimization(config_file, out_dir=None)[source]#

Bases: object

Methods

create_animation([format, fps])

Creates an animation from optimization history.

load_config(config_dict)

Load a new configuration and update the problem and solver objects.

plot_cycle_callback(x, iter)

Plot the thermodynamic cycle during optimization.

read_config(config_file)

Loads configuration from a YAML file.

run_optimization([x0])

Executes the optimization process.

save_config_callback(x, iter)

A callback function to save the current configuration during optimization iterations.

save_plot_callback(x, iter)

Save the thermodynamic cycle figure during optimization.

save_report_callback(x, iter)

A callback function to save the current configuration during optimization iterations.

save_results()

Saves the results of the optimization, including configurations and output files.

save_solver_pickle()

Sanitize solver (including deeply nested problem) and save it to a pickle file.

set_config_value(path, value[, reload])

Updates a value in the nested config dictionary using a dot-separated path.

set_config_values(updates)

Updates multiple config values in one call, triggering a single reload.

set_constraint(variable[, type, value, ...])

Adds or updates a constraint for the given variable name.

setup_problem()

Sets up the ThermodynamicCycleProblem based on the loaded configuration.

setup_solver()

Configures and returns the optimization solver.

plot_convergence_history

print_convergence_history

print_optimization_report

create_animation(format='both', fps=1)[source]#

Creates an animation from optimization history.

Parameters:
formatstr, optional

Format of animation (“gif”, “mp4”, or “both”), default is “both”.

durationfloat, optional

Duration of each frame in GIF (default is 0.5 sec).

fpsint, optional

Frames per second for MP4 (default is 10).

load_config(config_dict)[source]#

Load a new configuration and update the problem and solver objects.

Parameters:

config_obj (dict): A dictionary-like configuration object.

plot_convergence_history(savefile=False, showfig=True)[source]#
plot_cycle_callback(x, iter)[source]#

Plot the thermodynamic cycle during optimization.

print_convergence_history(savefile=False)[source]#
print_optimization_report(savefile=False)[source]#
read_config(config_file)[source]#

Loads configuration from a YAML file.

run_optimization(x0=None)[source]#

Executes the optimization process.

save_config_callback(x, iter)[source]#

A callback function to save the current configuration during optimization iterations.

Parameters: - x : The current solution vector from the optimizer. - iter : The current optimization iteration count.

This function acts as a bridge between the optimizer callback requirements and the existing save_current_configuration function.

save_plot_callback(x, iter)[source]#

Save the thermodynamic cycle figure during optimization.

save_report_callback(x, iter)[source]#

A callback function to save the current configuration during optimization iterations.

Parameters: - x : The current solution vector from the optimizer. - iter : The current optimization iteration count.

This function acts as a bridge between the optimizer callback requirements and the existing save_current_configuration function.

save_results()[source]#

Saves the results of the optimization, including configurations and output files.

save_solver_pickle()[source]#

Sanitize solver (including deeply nested problem) and save it to a pickle file.

set_config_value(path, value, reload=True)[source]#

Updates a value in the nested config dictionary using a dot-separated path. Optionally refreshes problem and solver.

Example:

set_config_value(“problem_formulation.fixed_parameters.expander.efficiency”, 0.8)

set_config_values(updates: dict)[source]#

Updates multiple config values in one call, triggering a single reload.

set_constraint(variable, type=None, value=None, normalize=None)[source]#

Adds or updates a constraint for the given variable name.

Parameters:

variable (str): Full variable path, e.g., “$components.cooler_charge.temperature_difference” type (str, optional): Constraint type, e.g., “>”, “<”, “=” value (float, optional): Target value normalize (bool or float, optional): Normalization value or flag

setup_problem()[source]#

Sets up the ThermodynamicCycleProblem based on the loaded configuration.

setup_solver()[source]#

Configures and returns the optimization solver.

class thermopt.optimize_cycle.ThermodynamicCycleProblem(configuration, out_dir=None)[source]#

Bases: OptimizationProblem

A class to represent a thermodynamic cycle optimization problem.

This class provides functionalities to load and update the configuration for the thermodynamic cycle, and to perform interactive plotting based on the current configuration.

Attributes:
plot_initializedbool

Flag to indicate if the plot has been initialized.

constraintsdict

Dictionary holding the constraints of the problem.

fixed_parametersdict

Dictionary holding the fixed parameters of the problem.

design_variablesdict

Dictionary holding the current values of the design variables.

lower_boundsdict

Dictionary holding the lower bounds of the design variables.

upper_boundsdict

Dictionary holding the upper bounds of the design variables.

keyslist

List of keys (names) of the design variables.

x0np.ndarray

Initial guess for the optimization problem.

Methods

update_config(configuration):

Update the problem’s configuration based on the provided dictionary.

load_config_from_file(configuration_file):

Load and update the problem’s configuration from a specified file.

plot_cycle_interactive(configuration_file, update_interval=0.20):

Perform interactive plotting, updating the plot based on the configuration file.

fitness(x)[source]#

Evaluate optimization problem

get_bounds()[source]#

Get the bounds for each decision variable (Pygmo format)

Returns:
boundstuple of lists

A tuple of two items where the first item is the list of lower bounds and the second item of the list of upper bounds for the vector of decision variables. For example, ([-2 -1], [2, 1]) indicates that the first decision variable has bounds between -2 and 2, and the second has bounds between -1 and 1.

get_nec()[source]#

Return the number of equality constraints associated with the problem.

Returns:
neqint

Number of equality constraints.

get_nic()[source]#

Return the number of inequality constraints associated with the problem.

Returns:
nineqint

Number of inequality constraints.

load_configuration_file(config_file)[source]#

Load and update the problem’s configuration from a specified file.

Useful to plot the cycle according to the latest version of the configuration file in real time (interactive initial guess generation)

Parameters:
config_filestr

Path to the configuration file.

plot_cycle()[source]#

Plots or updates the thermodynamic cycle diagrams based on current settings, including the option to include a pinch point diagram.

This function is capable of both creating new cycle diagrams and updating existing ones. It’s particularly useful in dynamic scenarios such as during optimization steps, where the plot needs to be refreshed continually with new data. The method also supports real-time updates based on the latest configuration settings.

The function first determines the number of subplots required based on the cycle diagrams specified in the ‘plot_settings’ attribute and whether a pinch point diagram is included. It then either initializes a new figure and axes or updates existing ones. Each thermodynamic diagram (phase diagram and cycle components) and the optional pinch point diagram are plotted or updated accordingly.

The method ensures that the plot reflects the current state of the cycle, including any changes during optimization or adjustments to configuration settings.

The data of the plots can be updated interactively, but the subplot objects created can only be specified upon class initialization. For example, it is not possible to switch on and off the pinch point diagram or to add new thermodynamic diagrams. The class would have to be re-initialized upon those scenarios. The reason for this choice is that re-creating a figure would be too time consuming and not practical for the real-time updating of the plots

plot_cycle_realtime(configuration_file, update_interval=0.1, write_report=False)[source]#

Perform interactive plotting, updating the plot based on the configuration file.

Parameters:
config_filestr

Path to the configuration file.

update_intervalfloat, optional

Time interval in seconds between plot updates (default is 0.1 seconds).

save_current_configuration(filename)[source]#

Save the current configuration to a YAML file.

save_data_to_excel(filename='performance.xlsx')[source]#

Exports the cycle performance data to Excel file

update_problem(configuration)[source]#

Update the problem’s configuration based on the provided dictionary.

Parameters:
configdict

Dictionary containing the new configuration for the thermodynamic cycle problem.