Viscosity

visco.py calculates viscosity using components of the pressure tensor obtained from a canonical ensemble (NVT) molecular dynamics (MD) simulation. Employs the Einstein or Green-Kubo relation, where viscosity is determined from the integral of the pressure tensor elements or their auto-correlation function, respectively.

The viscosity, \(\eta\), is calculated from the integral of the pressure tensor auto-correlation function over time following the Green–Kubo approach

\[\eta = \frac{V}{k_B T} \int_0^\infty \left\langle P_{\alpha \beta} \left( t \right) \cdot P_{\alpha \beta} \left( t_0 \right) \right\rangle dt ,\]

or the Einstein approach

\[\eta = \lim_{t \to \infty} \frac{V}{2 t k_B T} \left\langle \left( \int_0^\infty P_{\alpha \beta}(t') dt' \right)^2 \right\rangle\]

where \(V\) is the simulation box volume, \(k_B\) is the Boltzmann constant, \(T\) is temperature, \(P_{\alpha \beta}\) denotes the off-diagonal element \(\alpha \beta\) of the pressure tensor, and the brackets indicate that average must be taken over all time origins \(t_0\).

Usage:

python visco.py -h

Tip

An example data file, press.data, is available in the example directory. The required values to run the example can be found in the md.param file.

visco.einstein(P, time_array, timestep, volume, temperature)

Calculates the viscosity using the Einstein relation by integrating the components of the pressure tensor.

Parameters:
  • P (np.ndarray) – A 2D array of shape (6, N), where N is the number of time steps. It contains the six independent components of the pressure tensor. P[0] to P[5] correspond to Pxx, Pyy, Pzz, Pxy, Pxz, and Pyz, respectively.

  • time_array (np.ndarray) – A 1D array of time points corresponding to the pressure tensor data.

  • timestep (float) – The time step between consecutive data points.

  • volume (float) – The volume of the simulation box.

  • temperature (float) – The temperature of the simulation.

Returns:

A 1D array of viscosity values over time, in units of Pascal-seconds (Pa·s).

Return type:

np.ndarray

visco.green_kubo(P, timestep, volume, temperature, use_diag)

Calculate the viscosity from the Green-Kubo relation by integrating the autocorrelation of components of the pressure tensor.

Parameters:
  • P (np.ndarray) – A 2D array of shape (6, N), where N is the number of time steps. It contains the six independent components of the pressure tensor. P[0] to P[5] correspond to Pxx, Pyy, Pzz, Pxy, Pxz, and Pyz, respectively.

  • timestep (float) – The time step between consecutive data points.

  • volume (float) – The volume of the simulation box.

  • temperature (float) – The temperature of the simulation.

  • use_diag (bool) – If True, includes the off-diagonal shear components derived from the diagonal pressure tensor components in the calculation.

Returns:

  • avg_acf (np.ndarray) – The averaged autocorrelation function of the pressure tensor components.

  • viscosity (np.ndarray) – The calculated viscosity over time in Pascal-seconds (Pa·s).