Skip to content

About

Open book of 36 executed Jupyter notebooks on computational rheology: constitutive models, Brownian dynamics, log-conformation viscoelastic solvers, yield-stress flow, shear thickening and microrheology, written from scratch in NumPy and checked against exact solutions.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Computational rheology in Python

Constitutive models, Brownian dynamics, log-conformation solvers, yield-stress flow and suspensions, from scratch

An open book of executed notebooks on how the flow of polymer solutions, melts, gels and suspensions is computed: each model and method is written out in NumPy and checked against an exact solution or an independent calculation.

tests book Python Notebooks Code: MIT Text: CC BY 4.0

Left: stretch of the polymer and streamlines of an Oldroyd-B fluid in a lid-driven cavity as the Weissenberg number increases. Right: velocity of a Bingham fluid in a square duct as the Bingham number increases, with the unyielded regions hatched

Left: the trace of the conformation tensor (colour, logarithmic) and the streamlines of an Oldroyd-B fluid in a lid-driven cavity as the Weissenberg number increases; the vortex moves upstream (Lecture 13). Right: axial velocity of a Bingham fluid in a square duct as the Bingham number increases; the hatched regions are unyielded, and the flow stops near $2/(2+\sqrt{\pi}) \approx 0.53$ (Lecture 10). (scripts/make_readme_animation.py)

The flow of a polymer solution, a melt, a gel or a dense suspension cannot be computed with a viscosity alone: the stress depends on the history of deformation, on a microstructure that the flow stretches and orients, or on a yield stress. This book works through the models that describe such fluids and the numerical methods that make them computable, from the kinematics of a rotating frame to a two-dimensional viscoelastic flow solver. The book has 18 lectures in six parts, each an executed notebook with captioned figures and three exercises, worked solutions in solutions/, a small tested Python package (engcomprheo) whose core needs NumPy alone, a list of the 24 models and methods with the function that implements each one, and a list of suggested references.

Start in 60 seconds: open Lecture 0 here on GitHub (the notebooks are stored with their outputs): a stressed sample at rest on a turntable, and the shear stress that a constitutive equation written with a partial time derivative wrongly predicts for it.

Status: version 1.0.0. All 18 lectures are written, each with executed worked solutions.

Why this book?

  • No black boxes. Convected derivatives, the log-conformation decomposition, the semi-implicit FENE step, the Rotne–Prager–Yamakawa mobility, the Rolie-Poly and Pom-Pom models, the augmented Lagrangian method for Bingham flow, Brownian configuration fields, a projection solver with DEVSS-G, and dissipative particle dynamics with Lees–Edwards boundaries are each written out in NumPy in the engcomprheo package, and each lecture repeats by hand the calculation of a function it uses.
  • Every method is checked. Each result is compared with something independent: an exact solution such as that of Waters and King for the start-up of channel flow, an identity such as $N_2/N_1 = -\xi/2$, a limit such as the critical Bingham number of a square duct, or a second implementation.
  • Each lecture looks for a failure. The second exercise of every lecture finds a way in which a method or a common shortcut goes wrong: a time derivative that is not frame indifferent, an explicit step that stretches a FENE spring beyond its length, a control variate driven by independent random numbers, a regularized yield stress that creeps, a flow curve without stress diffusion, a thermostat that damps the flow.
  • From molecules to flows. The same fluid is described by stochastic dumbbells, by a closed constitutive equation and inside a flow solver, and the lectures compare the three.
  • Honest about what the numbers are. The material parameters are assumed values of the order of those reported for fluids of each kind, not fits to data, and are labelled so; data that stand for measurements are synthetic.

What the checks show

Centreline velocity and polymer stresses of the finite-volume solver for start-up of channel flow against the exact solution of Waters and King

Start-up of plane Poiseuille flow of an Oldroyd-B fluid: the finite-volume solver engcomprheo.viscoelastic_fvm_2d.solve_1d_channel_viscoelastic_startup against the exact solution of Waters and King (1970); the relative error of the centreline velocity is 0.08 % (Lecture 12). The table collects checks of this kind from the lectures and their solutions.

Check Computed Reference Agreement
Upper-convected derivative in a spinning frame (Lecture 0) rotated derivative of the fixed frame $8\times10^{-16}$
$N_2/N_1$ of the Johnson–Segalman model (Lecture 0) $-\xi/2$ $1.1\times10^{-16}$
Oldroyd-B Trouton ratio for $\mathrm{Wi} \le 0.42$ (Lecture 1) $3/[(1-2\mathrm{Wi})(1+\mathrm{Wi})]$ $2\times10^{-6}$ relative
Order of the log-conformation RK2 scheme (Lecture 2) 1.94 2
Root of the cubic of the semi-implicit FENE step (Lecture 3) np.roots $2.7\times10^{-15}$
$\langle Q^2\rangle_{\mathrm{eq}}$ of the FENE dumbbell, $b = 6$ to 200 (solutions to Lecture 3) $3b/(b+5)$ $2\times10^{-14}$
Exponent of the longest Zimm relaxation time, $N = 16$ to 36 (Lecture 5) 1.503 3/2 0.2 %
Divergence of the Rotne–Prager–Yamakawa mobility (Lecture 5) $7\times10^{-12}$ 0
Turning point of the coil–stretch curve (solutions to Lecture 6) 0.29752 $2\kappa/(1+\kappa)^2$ 5 digits
Monomer conservation in the two-species micelle model (Lecture 8) $n_A + n_B/2$ $10^{-13}$
Newtonian flow rate in a square duct, $64 \times 64$ (Lecture 10) 0.5622256 0.5623081 (series) 0.015 %, order 1.99
Critical Bingham number of a square duct (solutions to Lecture 10) 0.53015890 $2/(2+\sqrt{\pi})$ 8 digits
Strain of the Saramito model below the yield stress (Lecture 11) 0.078947 $\sigma_0/G_0$ 6 digits
Start-up of channel flow, finite volumes (Lecture 12) Waters–King solution 0.08 %
Pakdel–McKinley number in Taylor–Couette flow (Lecture 14) scaling of Larson, Shaqfeh and Muller $7\times10^{-15}$
Total momentum in dissipative particle dynamics (Lecture 15) $4\times10^{-15}$ 0
Moduli of a power-law medium from Mason's relation (Lecture 16) exact moduli $3\times10^{-15}$ Pa

The package is also compared with 180 exact solutions, identities and independent calculations in docs/validation.md, which scripts/validate.py generates and the tests repeat. Differences at the level of rounding error vary with the versions of Python and NumPy; the stored outputs are from Python 3.13 and NumPy 2.5, and the tests and all notebooks were also run with Python 3.10, NumPy 1.24 and Matplotlib 3.7.

Gallery

One figure from each lecture.

The Lodge integral and the upper-convected Maxwell equation give the same stresses in start-up of shear
The Lodge integral and the upper-convected Maxwell equation give the same stresses in start-up of shear (Lecture 0)
The Oldroyd-B Trouton ratio diverges at Wi = 1/2; FENE-P and Giesekus stay finite
The Oldroyd-B Trouton ratio diverges at Wi = 1/2; FENE-P and Giesekus stay finite (Lecture 1)
With a coarse step the direct update loses positive definiteness; the log-conformation update keeps it
With a coarse step the direct update loses positive definiteness; the log-conformation update keeps it (Lecture 2)
Explicit Euler stretches FENE dumbbells beyond their maximum length; the semi-implicit step does not
Explicit Euler stretches FENE dumbbells beyond their maximum length; the semi-implicit step does not (Lecture 3)
A Hookean control variate removes most of the noise of the stress at a low shear rate
A Hookean control variate removes most of the noise of the stress at a low shear rate (Lecture 4)
The Oseen mobility of two beads loses positive definiteness below r = 1.5a; Rotne–Prager–Yamakawa does not
The Oseen mobility of two beads loses positive definiteness below r = 1.5a; Rotne–Prager–Yamakawa does not (Lecture 5)
Coil–stretch hysteresis of the FENE-P model with drag that grows with extension
Coil–stretch hysteresis of the FENE-P model with drag that grows with extension (Lecture 6)
The Pom-Pom model hardens in extension and thins in shear
The Pom-Pom model hardens in extension and thins in shear (Lecture 7)
Two shear bands at one stress in the diffusive Johnson–Segalman model
Two shear bands at one stress in the diffusive Johnson–Segalman model (Lecture 8)
The Papanastasiou model shears inside the plug of the exact Bingham solution
The Papanastasiou model shears inside the plug of the exact Bingham solution (Lecture 9)
Bingham flow in a square duct: velocity, central plug and dead corners
Bingham flow in a square duct: velocity, central plug and dead corners (Lecture 10)
Stress against strain of the Saramito model in oscillation, below and above the yield strain
Stress against strain of the Saramito model in oscillation, below and above the yield strain (Lecture 11)
Start-up of channel flow overshoots the steady velocity when the elasticity number is large
Start-up of channel flow overshoots the steady velocity when the elasticity number is large (Lecture 12)
Cavity flow at Wi = 0.9: stream function, stretch, pressure and convergence
Cavity flow at Wi = 0.9: stream function, stretch, pressure and convergence (Lecture 13)
Normal stress and Pakdel–McKinley number in a channel with wavy walls
Normal stress and Pakdel–McKinley number in a channel with wavy walls (Lecture 14)
Wyart–Cates flow curves: continuous thickening, an S-shaped curve and jamming
Wyart–Cates flow curves: continuous thickening, an S-shaped curve and jamming (Lecture 15)
A component-wise network is not equivariant under rotation; a tensor-basis model is
A component-wise network is not equivariant under rotation; a tensor-basis model is (Lecture 16)
Feed duct and flow curves of the screening calculation
Feed duct and flow curves of the screening calculation (Lecture 17)

Installation

git clone https://github.com/ktwyw/engcomprheo.git
cd engcomprheo
pip install -e ".[book]"
jupyter lab

The notebooks also run without installation: each one adds the repository root to the path if the package is not installed, and installs it from GitHub when it is opened on Google Colab. The package needs NumPy alone; Matplotlib is used for the figures.

Quick start

import numpy as np
from engcomprheo import (brownian_chains, constants_benchmarks, shear_banding_instabilities,
                         tensors_kinematics, viscoplastic_admm)

# the logarithm of a conformation tensor and back (Lecture 2)
C = np.array([[3.2, 0.9], [0.9, 0.7]])
L = np.array([[0.0, 2.0], [0.0, 0.0]])
d = tensors_kinematics.log_conformation_decompose(C, L)
np.abs(tensors_kinematics.exp_conformation(d["Psi"]) - C).max()                  # 8.9e-16

# the semi-implicit FENE step keeps the connector shorter than sqrt(b) = 6 (Lecture 3)
brownian_chains.solve_fene_cubic_radial(np.array([10.0, 100.0]), b_ext=36.0, coeff_alpha=0.25)
                                                                                 # [5.144, 5.952]

# the two slowest relaxation times of an 8-bead chain: Rouse, then Zimm (Lecture 5)
for h_star, hi in ((0.0, False), (0.25, True)):
    brownian_chains.simulate_rouse_zimm_chain_bd(n_beads=8, h_star=h_star, n_steps=1, n_chains=1,
                                                 use_hi=hi)["relaxation_modes"][:2]
                                                                                 # [3.284, 0.854]; [2.827, 0.933]

# a non-monotonic flow curve and the rates at its maximum and minimum (Lecture 8)
curve = shear_banding_instabilities.steady_johnson_segalman_curve(np.linspace(0.1, 10, 50), G0=1.0,
                                                                  lam=1.0, eta_s=0.05, xi_slip=0.2)
curve["is_non_monotonic"], curve["spinodal_rates"]                               # True, [1.866, 6.820]

# Bingham flow in a square duct below and above the critical Bingham number 0.530 (Lecture 10)
for tau_y in (0.32, 0.58):
    viscoplastic_admm.solve_2d_duct_bingham_admm(nx=24, ny=24, tau_y=tau_y)["flow_rate"]   # 0.1384; 2e-07 (no flow)

# start-up of channel flow: the centreline velocity overshoots its steady value (Lecture 12)
w = constants_benchmarks.waters_king_startup_poiseuille(np.array([0.0]), np.linspace(0, 12, 601),
                                                        eta_s=0.1, eta_p=0.9, lam=1.2)
w["u_centerline"].max() / w["u_steady"][0]                                       # 1.84

What's inside

Part I. Objectivity, the conformation tensor and the log-conformation formulation

Lecture Topics Checked against
0. Frame indifference, convected derivatives and finite strain frame indifference; upper-convected, corotational and Gordon–Schowalter derivatives; the Finger tensor; the Lodge integral the transformation under a rotating frame; $N_2/N_1 = -\xi/2$; the upper-convected Maxwell equation
1. The conformation tensor: positive definiteness, free energy and dissipation the conformation tensor; positive definiteness; free energy and dissipation; Oldroyd-B, FENE-P and Giesekus in extension and shear the power balance; exact Trouton ratios
2. The high Weissenberg number problem and the log-conformation formulation why direct integration fails at high Weissenberg number; the log-conformation decomposition; its limit for equal eigenvalues the identity $\mathbf{L}\mathbf{C} + \mathbf{C}\mathbf{L}^\top$; second-order convergence

Part II. Brownian dynamics of dumbbells and chains

Lecture Topics Checked against
3. Stochastic differential equations and FENE dumbbells stochastic differential equations of dumbbells; Hookean, FENE and wormlike-chain springs; the semi-implicit FENE step np.roots; $\langle Q^2\rangle_{\mathrm{eq}} = 3b/(b+5)$
4. The Kramers stress and variance reduction with control variates the Kramers expression for the stress; statistical error; a Hookean control variate driven by the same random numbers the exact Hookean start-up; $1/(1 - \rho^2)$
5. Rouse and Zimm chains with Rotne–Prager–Yamakawa hydrodynamic interactions Rouse and Zimm relaxation times; the Rotne–Prager–Yamakawa mobility; Brownian dynamics of chains with hydrodynamic interaction eigenvalues of the Rouse matrix; Gaussian chain statistics; zero divergence of the mobility

Part III. Closures, tube models and shear banding

Lecture Topics Checked against
6. Closure approximations: FENE-P, FENE-CR and coil–stretch hysteresis the Peterlin closure; FENE-P and FENE-CR; the closure against Brownian dynamics; coil–stretch hysteresis the cubic of steady shear; the closed-form steady states
7. Tube models: Rolie-Poly and Pom-Pom the Rolie-Poly model with constraint release; the Pom-Pom model of branched melts the linear viscoelastic envelope; $\min(q, (1 - \mathrm{Wi}_s)^{-1})$
8. Non-monotonic flow curves and shear banding in wormlike micelles non-monotonic flow curves of the Johnson–Segalman model; stress diffusion and the selected stress; a two-species model of wormlike micelles the limit $\beta_s = 1/8$; uniform total stress; conservation of $n_A + n_B/2$

Part IV. Yield-stress fluids and elastoviscoplasticity

Lecture Topics Checked against
9. Yield-stress fluids: exact Bingham solutions and the error of regularization exact Bingham and Herschel–Bulkley flow in channels and pipes; the Papanastasiou regularization and its creep below the yield stress the Buckingham–Reiner flow rate; the $1/m$ law
10. The augmented Lagrangian method for Bingham flow in a square duct the augmented Lagrangian method for Bingham flow; plug and dead corners in a square duct; the critical Bingham number the series solution for a Newtonian fluid; $2/(2+\sqrt{\pi})$; the exact plane-channel profile
11. Elastoviscoplasticity: the Saramito model, yielding and recoil the Saramito model: elastic below the yield stress, viscoelastic above; start-up, oscillation and recoil the Kelvin–Voigt solution below the yield stress

Part V. Micro-macro methods and two-dimensional viscoelastic flow

Lecture Topics Checked against
12. Start-up of viscoelastic channel flow: the Waters–King solution and Brownian configuration fields start-up of plane Poiseuille flow of an Oldroyd-B fluid; Brownian configuration fields and independent ensembles the Waters–King solution
13. DEVSS-G stabilization and a coupled two-dimensional log-conformation solver projection and a cosine-transform Poisson solver; the two-dimensional log-conformation step; DEVSS-G; cavity flow a manufactured solution; plane Poiseuille flow; independence of $\alpha$
14. Viscoelastic cavity flow, constricted channels and the Pakdel–McKinley criterion vortex shift in cavity flow; regularized lid; a channel with wavy walls; the Pakdel–McKinley criterion the Taylor–Couette scaling of Larson, Shaqfeh and Muller

Part VI. Suspensions, microrheology and data-driven stress models

Lecture Topics Checked against
15. Lees–Edwards shear, dissipative particle dynamics and discontinuous shear thickening Lees–Edwards boundaries; dissipative particle dynamics; the Wyart–Cates model; a frictional particle simulation conservation of momentum; the onset $\phi_{\mathrm{DST}}$ by differentiation
16. Passive microrheology and a frame-invariant, thermodynamically consistent stress model a probe in a viscoelastic medium; Mason's form of the generalized Stokes–Einstein relation; a tensor-basis stress model with non-negative weights the exact mean-square displacement; a power-law medium; rotations
17. Project: screening a contraction and slit-die design across scales a screening calculation for a feed duct and a wavy slit that uses the methods of all six parts (assumed parameters and limits) the lubrication integral; the critical Bingham number

Worked solutions to all 54 exercises are in solutions/, one notebook for each lecture. The 24 models and methods are listed in docs/atlas.md, a plan for a semester is in docs/syllabus.md, and the symbols are collected in docs/notation.md.

The 24 models and methods as tiles coloured by the part of the course, each with its number, its name, the lecture that introduces it and the function of the package that implements it
The 24 models and methods, with the lecture that introduces each one and the function of the package that implements it; the colour is the part of the course. (scripts/make_readme_map.py)

The engcomprheo package

Module Purpose
constants_benchmarks six sets of assumed material parameters, the critical Bingham number of a square duct, the Waters–King start-up solution, and the list of the 24 models and methods
tensors_kinematics convected derivatives, a test of frame indifference, strain tensors, the free energy of the conformation tensor, the log-conformation decomposition, and an integrator for the Oldroyd-B, Giesekus and FENE-P models
brownian_chains the semi-implicit FENE step, Brownian dynamics of Hookean, FENE and wormlike-chain dumbbells with a control variate, the Rotne–Prager–Yamakawa mobility, and Rouse and Zimm chains
entangled_tubes the FENE-P and FENE-CR models, coil–stretch hysteresis, and the Rolie-Poly and Pom-Pom models
shear_banding_instabilities flow curves of the Johnson–Segalman model, the diffusive model in Couette flow, a two-species model of wormlike micelles, and the Pakdel–McKinley number
viscoplastic_admm exact Bingham flow in a channel and a pipe, the Papanastasiou regularization, the augmented Lagrangian method in a square duct, and the Saramito model
viscoelastic_fvm_2d start-up of channel flow with an Oldroyd-B equation, configuration fields or independent ensembles; cavity flow with the log-conformation formulation and DEVSS-G; a channel with wavy walls
mesoscopic_suspensions_dpd Lees–Edwards boundaries, dissipative particle dynamics in shear, the Wyart–Cates model, and a two-dimensional frictional particle simulation
microrheology_rheoml a probe in a viscoelastic medium, Mason's relation for the moduli, a tensor-basis stress model with non-negative weights, and a component-wise network for comparison
plotting the plotting style and the colour palette of the figures (Matplotlib)

Run the tests with pytest -q. They include unit tests of the nine modules, the 180 checks of scripts/validate.py, and checks that all 36 notebooks are stored executed, free of errors and with every figure captioned.

Repository layout

engcomprheo/                                 the Python package
notebooks/part1_kinematics_conformation/     Lectures 0-2, executed
notebooks/part2_brownian_dynamics/           Lectures 3-5
notebooks/part3_closures_tubes_banding/      Lectures 6-8
notebooks/part4_yield_stress/                Lectures 9-11
notebooks/part5_viscoelastic_flow/           Lectures 12-14
notebooks/part6_suspensions_microrheology/   Lectures 15-17
solutions/                                   worked solutions to the exercises, executed
tests/                                       tests of the package and of the stored notebooks
docs/                                        introduction, list of models and methods, syllabus, notation,
                                             validation report and references; images
scripts/                                     the plain-text sources of the notebooks and the script that builds them;
                                             the validation report; the figures of this page
myst.yml                                     Jupyter Book 2 configuration

The notebooks are built from plain-text sources: edit scripts/lectures_*.py or scripts/lecture_figures.py and run python scripts/build_notebooks.py, which executes every notebook in a fresh kernel. See CONTRIBUTING.md.

Suggested references

The notebooks are self-contained and do not follow any book. Original papers are named in the lectures where a model or a method is introduced, and are listed with books for further study in docs/references.md, from Oldroyd's formulation of rheological equations of state, Proc. R. Soc. Lond. A 200, 523 (1950), to the log-conformation formulation of Fattal and Kupferman, J. Non-Newtonian Fluid Mech. 123, 281 (2004).

Companion books

Scope and limitations

  • Assumed parameters. The six parameter sets of BENCHMARK_FLUIDS_DB carry the names of kinds of fluid (a Boger fluid, a polystyrene melt, a branched polyethylene, a wormlike micellar solution, a Carbopol gel, a dense suspension). Their values are assumed and are not fitted to measurements.
  • Lectures 0 to 2. Homogeneous flows only. The integrator is second order and explicit.
  • Lectures 3 to 5. Ensembles are small (hundreds to a few thousand trajectories), so the statistical errors are visible; the chains have at most 100 beads and no excluded volume. The Zimm relaxation times use pre-averaged hydrodynamic interaction.
  • Lectures 6 to 8. Single-mode models, except one exercise. The shear-banding calculations are one-dimensional with 36 to 96 cells, and the selected stress depends slightly on the grid.
  • Lectures 9 to 11. The duct solver is for fully developed flow of a Bingham fluid in a square duct on grids of up to $64 \times 64$; the yield surfaces are resolved to one cell.
  • Lectures 12 to 14. The two-dimensional solver uses first-order upwind advection on uniform grids of 16 to 28 cells in each direction and is limited to Weissenberg numbers of order 1. The channel with wavy walls uses a prescribed velocity field. The Pakdel–McKinley numbers are computed with these fields and are not predictions of instability in a real die.
  • Lectures 15 and 16. The particle simulations are two-dimensional with 36 to 100 particles. The stress model of Lecture 16 is fitted to synthetic data.
  • Lecture 17. The screening calculation is an illustration: its limits on the Pakdel–McKinley number are assumed.
  • The code is written to be read. It is not a replacement for production solvers.

Roadmap

Planned: multi-mode models, a higher-order advection scheme for the two-dimensional solver, flow past a cylinder, and examples with published data. Corrections and suggestions are welcome through the issue tracker.

Author

Yanwei Wang · yanwei.wang@nu.edu.kz · ORCID 0000-0002-8488-9833

Citing

Please cite the material using the metadata in CITATION.cff.

License

Code (engcomprheo/, tests/, scripts/ and the code cells of the notebooks): MIT. Text, figures and notebook outputs: CC BY 4.0.

About

Open book of 36 executed Jupyter notebooks on computational rheology: constitutive models, Brownian dynamics, log-conformation viscoelastic solvers, yield-stress flow, shear thickening and microrheology, written from scratch in NumPy and checked against exact solutions.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages