CosmoLayer#

class cosmolayer.CosmoLayer(interaction_matrices, exponents, area_per_segment, *, reference_temperature=298.15, learn_matrices=False, max_iter=100)[source]#

Differentiable COSMO-type activity coefficient layer.

This class assumes that the interaction energy matrix \(\mathbf{U}\) can depend on the temperature \(T\) through the following relationship:

\[\frac{\mathbf{U}}{RT} = \sum_{n=1}^N \frac{\mathbf{U}_n}{RT^{\alpha_n}},\]

where each \(\mathbf{U}_n\) is a constant interaction energy matrix, and \(\alpha_n\) is a constant exponent.

To instantiate the class, the user must provide a reference temperature \(T_{\rm ref}\), a tuple of exponents \((\alpha_1, \ldots, \alpha_N)\), and a tuple of scaled interaction energy matrices \((\hat{\mathbf{U}}_1, \ldots, \hat{\mathbf{U}}_N)\), defined as:

\[\hat{\mathbf{U}}_n = \frac{\mathbf{U}_n}{RT_{\rm ref}^{\alpha_n}}\]
Parameters:
  • interaction_matrices (Sequence[NDArray[np.float64]]) – The scaled interaction energy matrices at the reference temperature (\(\hat{\mathbf{U}}_1, \ldots, \hat{\mathbf{U}}_N\)). Must be square matrices, all with the same shape.

  • exponents (Sequence[int]) – The temperature exponents \((\alpha_1, \ldots, \alpha_N)\). Must have the same length as the number of interaction energy matrices.

  • area_per_segment (float) – Area of each surface segment.

  • reference_temperature (float, optional) – Reference temperature \(T_{\rm ref}\). Default is 298.15 K.

  • learn_matrices (bool, optional) – Whether to register all scaled interaction energy matrices as trainable parameters. Default is False.

Examples

>>> from importlib.resources import files
>>> from cosmolayer import CosmoLayer
>>> from cosmolayer.cosmosac import CosmoSac2002Model
>>> import torch
>>> T_ref = 298.15  # K
>>> source = files("cosmolayer.data")
>>> components = {
...     "fluoromethane": (source / "CF.cosmo").read_text(),
...     "water": (source / "O.cosmo").read_text(),
... }
>>> mixture = CosmoSac2002Model.create_mixture(components)
>>> interaction_matrices = mixture.interaction_matrices(T_ref)
>>> exponents = mixture.temperature_exponents
>>> area_per_segment = mixture.area_per_segment
>>> cosmo_layer = CosmoLayer(interaction_matrices, exponents, area_per_segment)
>>> cosmo_layer
CosmoLayer(
  reference_temperature=298.15
  area_per_segment=7.50
  exponents=(1,)
  num_segment_types=51
)
>>> T = torch.tensor(373.15)
>>> x = torch.tensor([0.5, 0.5], requires_grad=True)
>>> a = torch.tensor(mixture.areas)
>>> v = torch.tensor(mixture.volumes)
>>> P = torch.tensor(mixture.probabilities)
>>> ln_gamma = cosmo_layer(T, x, a, v, P)
>>> ln_gamma.tolist()
[0.805809..., 0.648071...]
>>> gE_RT = (x * ln_gamma).sum()
>>> gE_RT.item()
0.726940...
>>> gE_RT.backward()
>>> x.grad.tolist()
[0.805809..., 0.648071...]

Methods

extra_repr()[source]#

Return a string representation of the CosmoLayer.

Return type:

str

forward(temp, fracs, areas, volumes, probs)[source]#

Forward pass of the CosmoLayer.

Parameters:
  • temp (torch.Tensor) – Temperature in the same units as the reference temperature. Shape: (…,).

  • fracs (torch.Tensor) – Mole fractions of the components. Must sum to 1. Shape: (…, num_components).

  • areas (torch.Tensor) – Surface areas of the components. Shape: (…, num_components).

  • volumes (torch.Tensor) – Volumes of the components. Shape: (…, num_components).

  • probs (torch.Tensor) – Probabilities of segment types per component. Must sum to 1 along the segment type dimension. Shape: (…, num_components, num_segment_types).

Returns:

Logarithms of the activity coefficients. Shape: (…, num_components).

Return type:

torch.Tensor

log_activity_coefficients(temperature, fracs, areas, volumes, probs)[source]#

Compute the logarithms of the activity coefficients.

Parameters:
  • temperature (torch.Tensor) – Temperature in the same units as the reference temperature. Shape: (…,).

  • fracs (torch.Tensor) – Mole fractions of the components. Must sum to 1. Shape: (…, num_components).

  • areas (torch.Tensor) – Surface areas of the components. Shape: (…, num_components).

  • volumes (torch.Tensor) – Volumes of the components. Shape: (…, num_components).

  • probs (torch.Tensor) – Probabilities of segment types per component. Must sum to 1 along the segment type dimension. Shape: (…, num_components, num_segment_types).

Returns:

Logarithms of the activity coefficients. Shape: (…, num_components).

Return type:

torch.Tensor

log_combinatorial_activity_coefficients(fracs, areas, volumes)[source]#

Compute the logarithms of the combinatorial activity coefficients.

Parameters:
  • fracs (torch.Tensor) – Mole fractions of the mixture components. Must sum to 1. Shape: (…, num_components).

  • areas (torch.Tensor) – Surface areas of the mixture components, all in the same units. Shape: (…, num_components).

  • volumes (torch.Tensor) – Volumes of the mixture components, all in the same units. Shape: (…, num_components).

Returns:

Logarithms of the combinatorial activity coefficients. Shape: (…, num_components).

Return type:

torch.Tensor

log_mixture_segment_activity_coefficients(scaled_interactions, fracs, areas, probs)[source]#

Compute the log-activity coefficients of segment types in the mixture.

Parameters:
  • scaled_interactions (torch.Tensor) – Scaled interaction energy matrix. Shape: (…, num_segment_types, num_segment_types).

  • fracs (torch.Tensor) – Mole fractions of the components. Must sum to 1. Shape: (…, num_components).

  • areas (torch.Tensor) – Surface areas of the components. Shape: (…, num_components).

  • probs (torch.Tensor) – Probabilities of segment types per component. Must sum to 1 along the segment type dimension. Shape: (…, num_components, num_segment_types).

Returns:

Log-activity coefficients of segment types in the mixture. Shape: (…, num_segment_types).

Return type:

torch.Tensor

log_pure_segment_activity_coefficients(scaled_interactions, probs)[source]#

Compute the log-activity coefficients of segment types in pure compounds.

Parameters:
  • scaled_interactions (torch.Tensor) – Scaled interaction energy matrix. Shape: (…, num_segment_types, num_segment_types).

  • probs (torch.Tensor) – Probabilities of segment types per component. Must sum to 1 along the segment type dimension. Shape: (…, num_components, num_segment_types).

Returns:

Log-activity coefficients of segment types in pure compounds. Shape: (…, num_components, num_segment_types).

Return type:

torch.Tensor

log_residual_activity_coefficients(temperature, fracs, areas, probs)[source]#

Compute the logarithms of the residual activity coefficients.

Parameters:
  • temperature (torch.Tensor) – Temperature in the same units as the reference temperature. Shape: (…,).

  • fracs (torch.Tensor) – Mole fractions of the components. Must sum to 1. Shape: (…, num_components).

  • areas (torch.Tensor) – Surface areas of the components. Shape: (…, num_components).

  • probs (torch.Tensor) – Probabilities of segment types per component. Must sum to 1 along the segment type dimension. Shape: (…, num_components, num_segment_types).

Returns:

Logarithms of the residual activity coefficients. Shape: (…, num_components).

Return type:

torch.Tensor

mixture_probabilities(fracs, areas, probs)[source]#

Compute the probabilities of segment types in the mixture.

Parameters:
  • fracs (torch.Tensor) – Mole fractions of the components. Must sum to 1. Shape: (…, num_components).

  • areas (torch.Tensor) – Surface areas of the components. Shape: (…, num_components).

  • probs (torch.Tensor) – Probabilities of segment types per component. Must sum to 1 along the segment type dimension. Shape: (…, num_components, num_segment_types).

Returns:

Probabilities of segment types in the mixture. Shape: (…, num_segment_types).

Return type:

torch.Tensor

scaled_interactions(temp)[source]#

Compute the scaled interactions at a given temperature.

Parameters:

temp (torch.Tensor) – Temperature in the same units as the reference temperature. Shape: (…,).

Returns:

The scaled interactions at the given temperature. Shape: (…, num_segment_types, num_segment_types).

Return type:

torch.Tensor