Fitting
yaeos fitting module.
This module provides classes and functions for fitting binary interaction parameters to experimental data.
- class BinaryFitter(model_setter: Callable, model_setter_args: tuple, data: DataFrame, verbose: bool = False, pressure_error: Callable = None, temperature_error: Callable = None, composition_error: Callable = None)[source]
Bases:
objectBinaryFitter class.
This class is used to fit binary interaction parameters to experimental data. The objective function is defined as the sum of the squared errors between the experimental data and the model predictions.
- Parameters:
model_setter (Callable) – A function that returns a model object. The function should take the optimization parameters as the first argument and any other arguments as the following arguments.
model_setter_args (tuple) – A tuple with the arguments to pass to the model_setter function.
data (pandas.DataFrame) – A DataFrame with the experimental data. The DataFrame should have the following columns: - kind: str, the kind of data point (bubble, dew, liquid-liquid, PT, critical) - x1: float, the mole fraction of component 1 (lightest component) in heavy phase. - y1: float, the mole fraction of component 1 (lightest component) in light phase. - T: float, the temperature in K - P: float, the pressure in bar - weight: float, optional, the weight of the data point (default is 1)
verbose (bool, optional) – If True, print the objective function value and the optimization
pressure_error (Callable, optional) – A function `f(Pexp, Pmodel)`that calculates the pressure error between the experimental and model values. The function should take the experimental and model values as arguments and return the error. If None, the default function is used.
temperature_error (Callable, optional) – A function `f(Texp, Tmodel)`that calculates the temperature error between the experimental and model values. The function should take the experimental and model values as arguments and return the error. If None, the default function is used.
composition_error (Callable, optional) – A function `f(zexp, zmodel)`that calculates the composition error between the experimental and model values. The function should take the experimental and model for mixture composition as arguments and return the error. If None, the default function is used.
- get_model
The function that returns the model object from the optimization parameters and the model_setter_args.
- Type:
Callable
- get_model_args
The arguments to pass to the model_setter function.
- Type:
tuple
- data
The experimental data.
- Type:
pandas.DataFrame
- evaluations
A dictionary with the evaluations of the objective function. The keys are ‘fobj’, and ‘x’. ‘fobj’ is the objective function value, ‘x’ is the optimization parameters.
- Type:
dict
- fit(x0, bounds=None, method='Nelder-Mead', optimizer_options=None)[source]
Fit the model to the data.
Fit the model to the data using the objective function defined in the objective_function method. The optimization is performed using the scipy.optimize.minimize function. The optimization result is stored in the .solution property. Which
- Parameters:
x0 (array-like) – Initial guess for the fitting parameters.
bounds (array-like) – Bounds for the fitting parameters.
method (str, optional) – The optimization method to use. Default is ‘Nelder-Mead’.
- Return type:
None
- property model
Return the model with the fitted parameters.
- objective_function(x_values) float[source]
Objective function to minimize when fitting interaction parameters.
- Parameters:
x_values (array-like) – The interaction parameters to fit, 1D array-like.
- Returns:
The value of the objective function, which is the sum of the squared relative errors between the experimental data and the model predictions.
- Return type:
float
- property solution
Return the optimization solution.
- solve_pt(model: ArModel, pressure: float, temperature: float, kind: str) tuple[float, float][source]
Solve a point at a given P and T.
This function solves a point at a given pressure and temperature. That is, solve the composition of two phases in equilibrium at T and P. The function handles liquid-vapor or liquid-liquid.
- Parameters:
model (ArModel) – yaeos ArModel object.
pressure (float) – Pressure [bar].
temperature (float) – Temperature [K].
kind (str) – Kind of phase equilibrium calculation. Options are: - “liquid-liquid”: liquid-liquid equilibrium calculation. - “PT”: pressure-temperature calculation.
- Returns:
x1, y1: Mole fractions of component 1 (light component) in both phases.
- Return type:
tuple[float, float]