Python for Rheology: fitting the flow curve with SK-Learn

Dr. Marco Berta
3 min readOct 13, 2021

Code related to this post: GitHub — opsabarsec/flow-curve-fitter: Python script that fits rheological models to viscometer measurements of stress vs. strain rate

Rheology, for who never heard about, is a branch of Physics targeting flow and deformation. In real life, a small community whose popularity is nowhere near that of Sustainable Energy or Artificial Intelligence.

Still, this community has been helping generations of food scientists, materials engineers and chemists. In previous jobs was not uncommon for me to get data or to be assigned the measurement of viscosity or modulus for some materials, solutions, gels… Let alone having to teach a course of rheology in French 10 years ago back in Marseille University *.

Something that you will immediately notice when you approach a book on the subject is the insane amount of differential equations and models associated to each measurement. So much Math was very common back in the early days and it is something that everyday practitioners seldom like. Common problem: how do I fit the Casson Law [1] to a simple measurement of stress/shear rate that I made spinning two plates of an expensive machine called rheometer? Most people use Ms Excel and don’t really care about Matlab simulations from equations of velocity profiles. If you are lucky enough the fit can be provided by some software associated to that instrument. But then you still may have to bring all to a spreadsheet and, worse, to compare this model to that from measurements from a different rheometer.

Here is where Python becomes a big savior. The library Scikit-Learn provides a very convenient curve fitting function. Provided that you define correctly the equation that fits data, the rest is fairly straightforward. This is an example of the Casson equation in Python.

x = strain rate

yc = Casson yield stress

eta = Casson viscosity

def Casson(x ,yc, eta):
return ((np.sqrt(yc) + np.sqrt(eta*x))**2)/1

Then you obtain equations parameters and covariance from the fitting function. Here is a function that fits several models to the data, and we can estimate which one is best to calculate for example the yield stress (minimum stress necessary for the flow to happen)

def yield_stress_fits(x,y):yield_stress_models = [Bingham, Herschel_Bulkley, Casson]model_param_list = []for model in yield_stress_models:param, param_cov = curve_fit(model, x,y , maxfev=5000)model_param_list.append(np.round(param,2))return model_param_list

This returns a list of lists. Each list contains the parameter values for each model. Next problem is to obtain a calculated stress (y) from the model for each strain rate value (x) . This can be done referring to the elements of the corresponding list:

y_cas = (np.sqrt(yield_stress_param_list[2][0]) + np.sqrt(yield_stress_param_list[2][1]*x1))**2# finally the viscosity is simply the stress divided by the shear rateeta_cas = y_cas/x1

In the chocolate industry for example the Casson model is widely used, but something more complicated may be needed sometimes, such as the Carreau model [2].

For this I wrote a code that fits several common models to the experimental data and returns all results (stress values from fit, model parameters, R2 …) conveniently into a MS Excel spreadsheet [3]. Even for who has to measure and fit viscosity data but is not into programming I created a Python script that can be run quickly from command line.

Not the highest tech in ML/AI but something that can save lots of time fiddling with commercial software to model experimental data.

References

[1] Solved Explain what is meant by a Casson fluid. Starting | Chegg.com

[2] Rheological properties of chocolate — New Food Magazine

[3] Python for Rheology: fitting viscosity models to flow curves — YouTube

*

Notes from the course I gave in Marseille University:

Marseille University — Rheology course (giandonet.altervista.org)

--

--

Dr. Marco Berta

Senior Data Scientist @ ZF Wind Power, Ph.D. Materials Science in Manchester University