# A Framework for Flexible and Cost-Efficient Retrofit Measures of Heat Exchanger Networks

Date: 20 Februray 2020  
Authors: Christian Langner*, Elin Svensson, Simon Harvey  
*Correspondance: `christian.langner@chalmers.se`

Source code of the automated determiniation of critical operating points for multi-period heat exchanger networks proposed in Christian Langner, Elin Svensson, Simon Harvey, 
"A Framework for Flexible and Cost-Efficient Retrofit Measures of Heat Exchanger Networks". 
The mathematical formulations to determine critical operating points for multi-period heat exchanger networks had been proposed by Pintarič and Kravanja (2008).


This repository contains: 
1) two main scripts to execute a) the automated KKT-formulation on a provided instance "Main_KKT.py" b) the automated two-level formulation on a provided instance "Main_2_level.py" (using Python 3.7.3, Pyomo 5.6.6, BARON 19.3.24 and CONOPT/IPOPT via GAMS),
2) several scripts containing functions to determine the final set of critial points (accessiable in directory \modules):
	- "CalcA.py" (to calculate matrices A and T compare Pintarič and Kravanja (2008) (p. 1620-1621))
	- "ExtendedSetCovAlgorithm.py" (extended set covering algorithm)
	- "Lower_Control_Design.py" (solves lower level problem of current iteration for two-level formulation compare Pintarič and Kravanja (2008))
	- "SetCovAlgorithm.py" (set covering algorithm compare Pintarič and Kravanja (2008) (p. 1620 ff.))
	- "Upper_Uncertainty.py" (solves upper level problem of current iteration for two-level formulation compare Pintarič and Kravanja (2008))
3) model instances to test the KKT-formulation and the two-level formulation on each of the 5 different design proposals discussed in Christian Langner, Elin Svensson, Simon Harvey, "A Framework for Flexible and Cost-Efficient Retrofit Measures of Heat Exchanger Networks".
Generally, in all scripts, passages have been marked (enclosed by #####) to indicate where adjustments need to be taken if other than the provided model instances should be investigated. 

## Automatization KKT-formulation
The main script "Main_KKT.py" has been prepared to test the provided model instances of the 5 different design proposals. Licenses must be available for BARON 19.3.24 and GAMS/IPOPT or GAMS/CONOPT. 
Other solvers might be applicable and can be tested. However, reduced cost values of the optimization variables must be available at the obtained solution in order to determine critical points using the provided functions in the directory \modules. 
The results obtained by the KKT-formulation are saved in the "res" list of lists. 
Based on the results, the critical points are determined by executing the functions "calcA()" (returns matrix A and T compare Pintarič and Kravanja (2008) (p. 1620-1621)) 
and critical_points() (executes the set covering algorithm compare Pintarič and Kravanja (2008) (p. 1620 ff.)) 
In order to test the extended set covering algorithm, the respective function needs to be imported from modules.ExtrendedSetCovAlgorithm (execute line 15 insetad of line 14).
The results are saved as text douments in the working directory. The follwing txt documents will be generated:
- A.txt: matrix A (compare Pintarič and Kravanja (2008) (p. 1620-1621)); Read as follows:
	- 0: uncertain parameter has no influence on respective design parameter
	- 1: uncertain parameter at minimum value of uncertainty span for maximization of respective design parameter
	- 2: uncertian parameter at maximum value of uncertainty span for maximization of respective design parameter
	- >= 3: uncertain parameter between minimum and maximum value of uncertainty span for maximization of respective design parameter
- Results.txt: one list for each design parameter containing the following data:
	- maximum value for design parameter obtained by final local solver e.g. GAMS/IPOPT or GAMS/CONOPT
	- lower and upper bound values of design parameter returned by BARON (or other (gobal) pre-solver)
	- values for uncertain parameters obtained by final local solver e.g. GAMS/IPOPT or GAMS/CONOPT
	- reduced cost/marginal values of uncertain parameters obtained by final local solver e.g. GAMS/IPOPT or GAMS/CONOPT
	- relative influence of reduced cost/marginal values of uncertain parameters obtained by final local solver e.g. GAMS/IPOPT or GAMS/CONOPT (compare Pintarič and Kravanja (2008) (p. 1610))
- criticalPoints.txt: set of critical points in integer representation; Read as follows:
	- 0: uncertain parameter has no influence
	- 1: uncertain parameter at minimum value of uncertainty span
	- 2: uncertian parameter at maximum value of uncertainty span
	- >= 3: uncertain parameter between minimum and maximum value of uncertainty span
- CP.txt: set of critical points in float representation (values with corresponding units e.g. °C or kW/°C obtained by KKT-formulation)

## Automatization two-level formulation
The main script "Main_2level.py" has been prepared to test the provided model instances of the 5 different design proposals. Licenses must be available for BARON 19.3.24 (lower level problem) and GAMS/IPOPT or GAMS/CONOPT (upper level problem). 
Other solvers might be applicable and can be tested. However, reduced cost values of the optimization variables must be available at the obtained solution of the upper level problem in order to determine critical points (GAMS/CONOPT was experienced to be most numerically stable). 
The user may adjust the number of acceptable iterations to achieve convergence between the lower and upper level problems (parameter: max_iteration)
Depending on the chosen instance, the control variables must be defined differently. The number of control variables depends on the number of equality constraints (excluding the design constraints) and the number of optimization variables (excluding uncertain and design parameters). 
The scripts for the lower and upper level problems ("Lower_Control_Design.py", "Upper_Uncertainty.py") have been prepared to be appllied to the appended model instances (retrofitting examples). They may be adjusted to fit other problems (see scripts).
The results obtained by the two-level formulation are saved in the "res" list of lists. 
Based on the results, the critical points are determined by executing the functions "calcA()" (returns matrices A and T compare Pintarič and Kravanja (2008) (p. 1620-1621)) 
and critical_points() (executes the set covering algorithm compare Pintarič and Kravanja (2008) (p. 1620 ff.)) 
In order to test the extended set covering algorithm, the respective function needs to be imported from modules.ExtrendedSetCovAlgorithm (execute line 15 insetad of line 14).
The results are saved as text douments in the working directory. The follwing txt documents will be generated:
- A.txt: matrix A (compare Pintarič and Kravanja (2008) (p. 1620-1621)); Read as follows:
	- 0: uncertain parameter has no influence on respective design parameter
	- 1: uncertain parameter at minimum value of uncertainty span for maximization of respective design parameter
	- 2: uncertian parameter at maximum value of uncertainty span for maximization of respective design parameter
	- >= 3: uncertain parameter between minimum and maximum value of uncertainty span for maximization of respective design parameter
- Results.txt: one list for each design parameter containing the following data:
	- maximum value for design parameter obtained by final local solver e.g. GAMS/IPOPT or GAMS/CONOPT
	- lower and upper bound values of design parameter returned by BARON (or other (gobal) pre-solver)
	- values for uncertain parameters obtained by final local solver e.g. GAMS/IPOPT or GAMS/CONOPT
	- reduced cost/marginal values of uncertain parameters obtained by final local solver e.g. GAMS/IPOPT or GAMS/CONOPT
	- relative influence of reduced cost/marginal values of uncertain parameters obtained by final local solver e.g. GAMS/IPOPT or GAMS/CONOPT (compare Pintarič and Kravanja (2008) (p. 1610))
- Termination.txt: for each design parameter the termination condition of the (local) solver in the upper level problem is saved to identify infeasible (intermediate) solutions 
- criticalPoints.txt: set of critical points in integer representation; Read as follows:
	- 0: uncertain parameter has no influence
	- 1: uncertain parameter at minimum value of uncertainty span
	- 2: uncertian parameter at maximum value of uncertainty span
	- >= 3: uncertain parameter between minimum and maximum value of uncertainty span
- CP.txt: set of critical points in float representation (values with corresponding units e.g. °C or kW/°C obtained by KKT-formulation)

## Model instances
The different model instances can be found in the directory \Model_instance . The instances have been prepared using Python 3.7.3 and Pyomo 5.6.6. 
For each design proposal discussed Christian Langner, Elin Svensson, Simon Harvey, "A Framework for Flexible and Cost-Efficient Retrofit Measures of Heat Exchanger Networks" two instances can be found in the directory \Model_instance. 
These instances can be used to determine the critical points via the KKT-formulation and the two-level level formulation, respectivley. The instances are named accordingly.

An instance for the KKT-formulation constains:
- 10 Set Declarations:
	- Q_index: Index set for utility exchanger duties
	- T_indes: Index set for internatl network temperatures
	- numCold : Index set for cold streams
	- numCooler : Index set for utility cooler
	- numFcp : Index set for heat capacity flow rates (of process streams)
	- numFcpU : Index set for uncertain heat capacity flow rates (of process streams) 
	- numHEX : Index set for process-to-process HEXs
	- numHeater : Index set for utility heater
	- numHot : Index set for hot streams
	- numTinit : Index set for uncertain inlet temperatures (of process streams)
	
- 5 (indexed) Variable Declarations:
	- Fcp : Heat capacity flowrates (of process streams)
	- Q : Duties of utility exchangers
	- T : Internal network temperatures
	- Tinit : Uncertain inlet temperatures (of process streams)
	- dUA : Design parameters of process-to-process HEX
	
- 12 (indexed) Constraint Declarations:
	- conArea : Process-to-process HEX design constraints
	- conCooler : Utility HEX (cooler) energy balances
	- conCost : Cost function
	- conHEX : Process-to-process HEX energy balances
	- conHeater : Utility HEX (heater) energy balances
	- conNonNegative : Temperature constraints over process-to-process HEX
	- conNonNegativeCooler : Temperature constraints over utility (cooler) HEX
	- conNonNegativeHeater : Temperature constraints over utility (heater) HEX
	- con_deltaTCooler : delta Tmin constraints (cooler)
	- con_deltaTHeater : delta Tmin constraints (heater)
	- con_deltaTcold : delta Tmin constraints (cold exchanger side)
	- con_deltaThot : delta Tmin constraints (hot exchanger side)
	
An instance for the two-level level formulation constains:
- 10 Set Declarations:
	- Q_index: Index set for utility exchanger duties
	- T_indes: Index set for internatl network temperatures
	- numCold : Index set for cold streams
	- numCooler : Index set for utility cooler
	- numFcp : Index set for heat capacity flow rates (of process streams)
	- numFcpU : Index set for uncertain heat capacity flow rates (of process streams) 
	- numHEX : Index set for process-to-process HEXs
	- numHeater : Index set for utility heater
	- numHot : Index set for hot streams
	- numTinit : Index set for uncertain inlet temperatures (of process streams)
	
- 5 (indexed) Variable Declarations:
	- Fcp : Heat capacity flowrates (of process streams)
	- Q : Duties of utility exchangers
	- T : Internal network temperatures
	- Tinit : Uncertain inlet temperatures (of process streams)
	- dUA : Design parameters of process-to-process HEX
	
- 11 (indexed) Constraint Declarations:
	- conArea : Process-to-process HEX design constraints
	- conCooler : Utility HEX (cooler) energy balances
	- conHEX : Process-to-process HEX energy balances
	- conHeater : Utility HEX (heater) energy balances
	- conNonNegative : Temperature constraints over process-to-process HEX
	- conNonNegativeCooler : Temperature constraints over utility (cooler) HEX
	- conNonNegativeHeater : Temperature constraints over utility (heater) HEX
	- con_deltaTCooler : delta Tmin constraints (cooler)
	- con_deltaTHeater : delta Tmin constraints (heater)
	- con_deltaTcold : delta Tmin constraints (cold exchanger side)
	- con_deltaThot : delta Tmin constraints (hot exchanger side)
	
## References

- Z. N. Pintarič and Z. Kravanja, “Identification of critical points for the design and synthesis of flexible processes,” Comput. Chem. Eng., vol. 32, pp. 1603–1624, 2008, doi: 10.1016/S1570-7946(06)80095-7.
