CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   How to obtain fluid shear stress (not only wall shear stress) in openFOAM (

sourav90 July 12, 2020 14:33

How to obtain fluid shear stress (not only wall shear stress) in openFOAM
Dear All,

I want to calculate/visualise shear stress inside the fluid domain. I came through wallShearStress utility but as you know, it only calculates the shear stress at the wall.

My model is very simple: a long pipe, with a non-Newtonian fluid simulated at a certain pressure difference between the inlet and the outlet, solved using simpleFoam.

Surprisingly, I found no relevant documentation, although there is plenty of discussion regarding the shear stress at the wall. I came through this helpful post, which enabled me to calculate shear strain in the fluid domain but finding it hard to adopt the approach to obtain shear stress.

I tried with the approach mentioned there, by making files named fluidShearStressSM.C etc. At the wmake stage it throws a lot of errors such as :


g++ -std=c++11 -m64 -Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -Wno-attributes -O3  -DNoRepository -ftemplate-depth-100 -I/opt/openfoam7/src/finiteVolume/lnInclude -I/opt/openfoam7/src/meshTools/lnInclude -I/opt/openfoam7/src/lagrangian/basic/lnInclude -I/opt/openfoam7/src/fileFormats/lnInclude -I/opt/openfoam7/src/sampling/lnInclude -I/opt/openfoam7/src/surfMesh/lnInclude -I/opt/openfoam7/src/thermophysicalModels/basic/lnInclude -I/opt/openfoam7/src/thermophysicalModels/solidThermo/lnInclude -I/opt/openfoam7/src/transportModels -I/opt/openfoam7/src/transportModels/compressible/lnInclude -I/opt/openfoam7/src/TurbulenceModels/turbulenceModels/lnInclude -I/opt/openfoam7/src/TurbulenceModels/incompressible/lnInclude -I/opt/openfoam7/src/TurbulenceModels/compressible/lnInclude -IlnInclude -I. -I/opt/openfoam7/src/OpenFOAM/lnInclude -I/opt/openfoam7/src/OSspecific/POSIX/lnInclude  -fPIC -c fluidShearStressSM/fluidShearStressSM.C -o /opt/openfoam7/platforms/linux64GccDPInt32Opt/src/functionObjects/field/fluidShearStressSM/fluidShearStressSM.o
fluidShearStressSM/fluidShearStressSM.C: In member function ‘virtual bool Foam::functionObjects::fluidShearStressSM::calc()’:
fluidShearStressSM/fluidShearStressSM.C:65:13: error: expected primary-expression before ‘const’
  65 |            const volTensorField& gradU = fvc::grad(U);
      |            ^~~~~
fluidShearStressSM/fluidShearStressSM.C:68:43: error: ‘mu’ was not declared in this scope
  68 |            const volTensorField& tau = - mu * (gradU + gradU.T());
      |                                          ^~
fluidShearStressSM/fluidShearStressSM.C:68:49: error: ‘gradU’ was not declared in this scope
  68 |            const volTensorField& tau = - mu * (gradU + gradU.T());
      |                                                ^~~~~
fluidShearStressSM/fluidShearStressSM.C:69:9: error: expected primary-expression before ‘)’ token
  69 |        );
      |        ^
fluidShearStressSM/fluidShearStressSM.C:68:35: warning: unused variable ‘tau’ [-Wunused-variable]
  68 |            const volTensorField& tau = - mu * (gradU + gradU.T());
      |                                  ^~~
fluidShearStressSM/fluidShearStressSM.C: In constructor ‘Foam::functionObjects::fluidShearStressSM::fluidShearStressSM(const Foam::word&, const Foam::Time&, const Foam::dictionary&)’:
fluidShearStressSM/fluidShearStressSM.C:90:5: error: type ‘Foam::functionObjects::fvMeshFunctionObject’ is not a direct base of ‘Foam::functionObjects::fluidShearStressSM’
  90 |    fvMeshFunctionObject(name, runTime, dict),
      |    ^~~~~~~~~~~~~~~~~~~~
fluidShearStressSM/fluidShearStressSM.C:91:5: error: class ‘Foam::functionObjects::fluidShearStressSM’ does not have any field named ‘logFiles’
  91 |    logFiles(obr_, name),
      |    ^~~~~~~~
fluidShearStressSM/fluidShearStressSM.C:92:5: error: class ‘Foam::functionObjects::fluidShearStressSM’ does not have any field named ‘writeLocalObjects’
  92 |    writeLocalObjects(obr_, log),
      |    ^~~~~~~~~~~~~~~~~
fluidShearStressSM/fluidShearStressSM.C:93:1: error: expected identifier before ‘{’ token
  93 | {
      | ^
fluidShearStressSM/fluidShearStressSM.C:93:1: error: no matching function for call to ‘Foam::functionObjects::fieldExpression::fieldExpression()’
In file included from fluidShearStressSM/fluidShearStressSM.H:42,
                from fluidShearStressSM/fluidShearStressSM.C:26:
lnInclude/fieldExpression.H:88:9: note: candidate: ‘Foam::functionObjects::fieldExpression::fieldExpression(const Foam::word&, const Foam::Time&, const Foam::dictionary&, const Foam::word&, const Foam::word&)’
  88 |        fieldExpression
      |        ^~~~~~~~~~~~~~~
lnInclude/fieldExpression.H:88:9: note:  candidate expects 5 arguments, 0 provided
fluidShearStressSM/fluidShearStressSM.C:95:5: error: ‘resetName’ was not declared in this scope; did you mean ‘resultName_’?
  95 |    resetName(typeName);
      |    ^~~~~~~~~
      |    resultName_
fluidShearStressSM/fluidShearStressSM.C:96:5: error: ‘resetLocalObjectName’ was not declared in this scope
  96 |    resetLocalObjectName(typeName);
      |    ^~~~~~~~~~~~~~~~~~~~
make: *** [/opt/openfoam7/wmake/rules/General/transform:26: /opt/openfoam7/platforms/linux64GccDPInt32Opt/src/functionObjects/field/fluidShearStressSM/fluidShearStressSM.o] Error 1

I would like to have the code usable both as a runtime or a post-processing tool.

I am new to OpenFOAM & have no prior exposure to OOC++ coding. Any suggestion or pointers in the right direction would help me greatly in my research, with this nice open-source software.

fanta July 13, 2020 03:13

-Move to $FOAM_SRC/etc/caseDicts/postProcessing/fields
-make a copy of vorticity to strainRate, replace every occurance of vorticity with strainRate in the files
-Move to $FOAM_SRC/functionObjects/field/make. Edit "files", add strainRate/strainRate.C after vorticity
-copy the folder vorticity in $FOAM_SRC/functionObjects/field to strainRate
-replace vorticity with strainRate in the copied filenames
-in strainRate.C replace #fvcCurl.H with #fvcGrad.H, replace function calc() with


bool Foam::functionObjects::strainRate::calc()
    if (foundObject<volVectorField>(fieldName_))
        return store

    return false;

For non-Newtonian Liquids this formula is generally wrong. I could not find a better approach, yet.

sourav90 August 6, 2020 13:41


Originally Posted by fanta (Post 777611)

For non-Newtonian Liquids this formula is generally wrong. I could not find a better approach, yet.

Why is the formula generally wrong?

BTW, the problem I am facing isn't with implementing strain rate (it works as that link says), rather with the shear stress in the entire fluid domain. Sorry, if I wasn't clear enough...

sourav90 August 6, 2020 13:56

Using swak4FOAM, but shear stress & wall shear stress do not match
2 Attachment(s)
I found a workaround while fiddling with Swak4Foam. He files or commands that I used are as follow:
  • Contents of `funkySetFieldsDict`, to be placed inside `system/`

    version        2.0;
    format          ascii;
    instance        "system";
    local          "";
    class          dictionary;
    object          funkySetFieldsDict;

expressions (
    setTau {
        field          Tau;
        create          true ;
        expression      "mag(grad(U))*nu";

  • After the simulation was over, I ran

    funkySetFields -time '500:3000'

The simulation is as such that the highest shear stress would be at the wall. So, I compare the maximum values of shear stress and wall shear stress (using the wallShearStress utility), it's hugely different. I am not sure now, which one is correct. Can anybody help me inferring where is the error?

Bodo1993 November 17, 2020 15:32

Hi Sourav,
I am wondering if you managed to get a way to extract the correct stress tensor from the openFoam simulations. Thanks.

sourav90 December 10, 2020 12:44

Version 8 of OpenFOAM has natively implemented this
Hello Bodo1993, if you haven't found it already there are two ways to achieve this.

First, with OpenFOAM version 8, you can just add these lines at the end of your controldict file:


    #includeFunc            shearStress;


Copying the commit notes from here


"MomentumTransportModels: Update of the TurbulenceModels library for a…
…ll flow types

providing the shear-stress term in the momentum equation for incompressible and
compressible Newtonian, non-Newtonian and visco-elastic laminar flow as well as
Reynolds averaged and large-eddy simulation of turbulent flow.

The general deviatoric shear-stress term provided by the MomentumTransportModels
library is named divDevTau for compressible flow and divDevSigma (sigma =
tau/rho) for incompressible flow, the spherical part of the shear-stress is
assumed to be either included in the pressure or handled separately. The
corresponding stress function sigma is also provided which in the case of
Reynolds stress closure returns the effective Reynolds stress (including the
laminar contribution) or for other Reynolds averaged or large-eddy turbulence
closures returns the modelled Reynolds stress or sub-grid stress respectively.
For visco-elastic flow the sigma function returns the effective total stress
including the visco-elastic and Newtonian contributions.

For thermal flow the heat-flux generated by thermal diffusion is now handled by
the separate ThermophysicalTransportModels library allowing independent run-time
selection of the heat-flux model.

During the development of the MomentumTransportModels library significant effort
has been put into rationalising the components and supporting libraries,
removing redundant code, updating names to provide a more logical, consistent
and extensible interface and aid further development and maintenance. All
solvers and tutorials have been updated correspondingly and backward
compatibility of the input dictionaries provided.

Henry G. Weller
CFD Direct Ltd."

Second, iff you are curious, have time & want to have fun, then you can implement this yourself, with the cue from here Function object for strain rate tensor? However, this leads to some naming issue with the recent versions, due to the changes described here If anyone try this with the new versions & it works, it would be great if you share it :)

All times are GMT -4. The time now is 02:57.