CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   How to modify kinematic viscosity "nu" within the time loop? (https://www.cfd-online.com/Forums/openfoam-solving/87674-how-modify-kinematic-viscosity-nu-within-time-loop.html)

 taner April 26, 2011 08:50

How to modify kinematic viscosity "nu" within the time loop?

Dear all,

kinematic viscosity "nu" can be modified during the runtime by changing its value in the "transportProperties" dictionary.
Is there a simple way to change its value within the time loop?
I would like to define it as a function of time/iterations in a SIMPLE solver to improve convergence like:

while (runTime.loop())
{

if ( current_time <= some_fixed_time )
{
nu = nu_desired * ( some_fixed_time / current_time );
}
else
{
nu = nu_desired;
}

...

}

so that it reaches to the desired value after some fixed time. I would appreciate any proposal to implement it in an elegant way.

Taner.

 MartinB April 26, 2011 09:41

Hi Taner,

I propose to modify one of the existing viscosity models. You can find them in OpenFOAM-xxx/src/transportModels/incompressible/viscosityModels/.

Add some new values to the constant/transportProperties file, for example nu_start, nu_desired and some_fixed_time. Read these values into your new viscosity model class and adjust the current nu in dependency of the current time step.

This approach should work directly for simpleFoam or nonNewtonianIcoFoam and every other solver that uses non newtonian material models.

Martin

 MartinB April 26, 2011 14:01

Well, here is a quick implementation. I changed the algorithm a little bit so that it runs more smoothly with my test case.

First, the "constant/transportProperties" file:
Code:

```/*--------------------------------*- C++ -*----------------------------------*\ | =========                |                                                | | \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          | |  \\    /  O peration    | Version:  1.6                                  | |  \\  /    A nd          | Web:      www.OpenFOAM.org                      | |    \\/    M anipulation  |                                                | \*---------------------------------------------------------------------------*/ FoamFile {     version    2.0;     format      ascii;     class      dictionary;     location    "constant";     object      transportProperties; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // transportModel  approachingNu; approachingNuCoeffs {     nu_desired            nu_desired [ 0 2 -1 0 0 0 0 ] 1e-06;     nu_start                  nu_start [ 0 2 -1 0 0 0 0 ] 1.0;     some_fixed_time  some_fixed_time [ 0 0 1 0 0 0 0 ] 200; } // ************************************************************************* //```
Here is the header file "approachingNu.H", written and roughly tested for OpenFOAM-1.6-ext:
Code:

```/*---------------------------------------------------------------------------*\   =========                |   \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox   \\    /  O peration    |     \\  /    A nd          | Copyright held by original author     \\/    M anipulation  | ------------------------------------------------------------------------------- License     This file is part of OpenFOAM.     OpenFOAM is free software; you can redistribute it and/or modify it     under the terms of the GNU General Public License as published by the     Free Software Foundation; either version 2 of the License, or (at your     option) any later version.     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     for more details.     You should have received a copy of the GNU General Public License     along with OpenFOAM; if not, write to the Free Software Foundation,     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Class     Foam::viscosityModels::approachingNu Description     A material model which is approaching the desired viscosity nu_desired,     starting from nu_start.     A blended algorithm is:     if (current_time < some_fixed_time_)     {         scalar alpha = current_time / some_fixed_time;         return (alpha * nu_desired_ + (1 - alpha) * nu_start_);     }     else     {         return nu_desired_;     }     However this one behaves better in my tests:     if (current_time < some_fixed_time_)     {         scalar alpha = (nu_start * 1000 / nu_desired) ^ (1/some_fixed_time)         return (nu_start / (alpha ^ current_time)) + nu_desired;     }     else     {         return nu_desired_;     }     SourceFiles     approachingNu.C \*---------------------------------------------------------------------------*/ #ifndef approachingNu_H #define approachingNu_H #include "viscosityModel.H" #include "dimensionedScalar.H" #include "volFields.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { namespace viscosityModels { /*---------------------------------------------------------------------------*\                           Class approachingNu Declaration \*---------------------------------------------------------------------------*/ class approachingNu :     public viscosityModel {     // Private data         dictionary approachingNuCoeffs_;         dimensionedScalar nu_desired_;         dimensionedScalar nu_start_;         dimensionedScalar some_fixed_time_;         volScalarField nu_;     // Private Member Functions         //- Calculate and return the laminar viscosity         tmp<volScalarField> calcNu() const; public:     //- Runtime type information     TypeName("approachingNu");     // Constructors         //- construct from components         approachingNu         (             const word& name,             const dictionary& viscosityProperties,             const volVectorField& U,             const surfaceScalarField& phi         );     // Destructor         ~approachingNu()         {}     // Member Functions         //- Return the laminar viscosity         const volScalarField& nu() const         {             return nu_;         }         //- Correct the laminar viscosity         void correct()         {             nu_ = calcNu();         }         //- Read transportProperties dictionary         bool read(const dictionary& viscosityProperties); }; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace viscosityModels } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif // ************************************************************************* //```
And the "approachingNu.C" file:
Code:

```/*---------------------------------------------------------------------------*\   =========                |   \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox   \\    /  O peration    |     \\  /    A nd          | Copyright held by original author     \\/    M anipulation  | ------------------------------------------------------------------------------- License     This file is part of OpenFOAM.     OpenFOAM is free software; you can redistribute it and/or modify it     under the terms of the GNU General Public License as published by the     Free Software Foundation; either version 2 of the License, or (at your     option) any later version.     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     for more details.     You should have received a copy of the GNU General Public License     along with OpenFOAM; if not, write to the Free Software Foundation,     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA \*---------------------------------------------------------------------------*/ #include "approachingNu.H" #include "addToRunTimeSelectionTable.H" #include "surfaceFields.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { namespace viscosityModels {     defineTypeNameAndDebug(approachingNu, 0);     addToRunTimeSelectionTable     (         viscosityModel,         approachingNu,         dictionary     ); } } // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * // /* this is the linear blended algorithm... however I do prefer the alternative algorithm below Foam::tmp<Foam::volScalarField> Foam::viscosityModels::approachingNu::calcNu() const {     scalar current_time((U_.time()).value());     Info << "current_time = " << current_time << endl;     Info << "some_fixed_time_ = " << some_fixed_time_.value() << endl;     if (current_time < some_fixed_time_.value())     {         scalar alpha = current_time / some_fixed_time_.value();         Info << "alpha = " << alpha << ", nu = " << (alpha * nu_desired_ + (1 - alpha) * nu_start_) << endl;         return         (             strainRate() * dimensionedScalar("dummy",nu_desired_.dimensions()*dimTime,0.0) // dummy for adding strainRate dependend model             + (alpha * nu_desired_ + (1 - alpha) * nu_start_)         );     }     else     {         Info << "nu = " << nu_desired_ << endl;         return         (             strainRate() * dimensionedScalar("dummy",nu_desired_.dimensions()*dimTime,0.0) // dummy for adding strainRate dependend model             + nu_desired_         );     } } */ Foam::tmp<Foam::volScalarField> Foam::viscosityModels::approachingNu::calcNu() const {     scalar current_time((U_.time()).value());     //Info << "current_time = " << current_time << endl;     //Info << "some_fixed_time_ = " << some_fixed_time_.value() << endl;     if (current_time < some_fixed_time_.value())     {         scalar alpha = pow((nu_start_.value()*1000/nu_desired_.value()), (1/some_fixed_time_.value()));         //Info << "alpha = " << alpha << ", nu = " << (nu_start_ / (pow(alpha, current_time))) + nu_desired_ << endl;         return         (             strainRate() * dimensionedScalar("dummy",nu_desired_.dimensions()*dimTime,0.0) // dummy for adding strainRate dependend model             + (nu_start_ / (pow(alpha, current_time))) + nu_desired_         );     }     else     {         //Info << "nu = " << nu_desired_ << endl;         return         (             strainRate() * dimensionedScalar("dummy",nu_desired_.dimensions()*dimTime,0.0) // dummy for adding strainRate dependend model             + nu_desired_         );     } } // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * // Foam::viscosityModels::approachingNu::approachingNu (     const word& name,     const dictionary& viscosityProperties,     const volVectorField& U,     const surfaceScalarField& phi ) :     viscosityModel(name, viscosityProperties, U, phi),     approachingNuCoeffs_(viscosityProperties.subDict(typeName + "Coeffs")),     nu_desired_(approachingNuCoeffs_.lookup("nu_desired")),     nu_start_(approachingNuCoeffs_.lookup("nu_start")),     some_fixed_time_(approachingNuCoeffs_.lookup("some_fixed_time")),         nu_     (         IOobject         (             "nu",             U_.time().timeName(),             U_.db(),             IOobject::NO_READ,             IOobject::AUTO_WRITE         ),         calcNu()     ) {} // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * // bool Foam::viscosityModels::approachingNu::read (     const dictionary& viscosityProperties ) {     viscosityModel::read(viscosityProperties);     approachingNuCoeffs_ = viscosityProperties.subDict(typeName + "Coeffs");     approachingNuCoeffs_.lookup("nu_desired") >> nu_desired_;     approachingNuCoeffs_.lookup("nu_start") >> nu_start_;     approachingNuCoeffs_.lookup("some_fixed_time") >> some_fixed_time_;     return true; } // ************************************************************************* //```
For non-Extend OpenFOAM versions it should be
Code:

`tmp<volScalarField> nu() const`
in line 126 instead, but I didn't test it with these versions...

Martin

 taner April 28, 2011 03:51

Hi Martin,
I appreciated your quick and accurate support very much.
The model that you implemented works well for the non-extended, official OpenFOAM 1.7.1.
Best regards,
Taner.

 hossein_y62 January 8, 2013 04:12

Hi every body,

I need to change the power law in the OpenFOAM-xxx/src/transportModels/incompressible/viscosityModels/ directory. How can I do that?

the default power law model defines nuMax and nuMin, as I set them to 100000 and 0 respectively, the run time will increase dramatically. I have to notice that I am using interFoam Solver.

It would be very appreciated if someone could help me,

Thanx

 suman91 September 20, 2016 19:04

Cannot access nu value

Hi,

Since this thread talks about kinematic viscosity and changing its value, am posting my doubt here. I am running a flow over backward facing step using LES model and pisoFoam. I can't seem to access the kinematic viscosity by referring to it as "nu";

I tried to just print the value to see if I have access to it:

Code:

`Info<<"laminar viscosity is"<<nu;`
I get the error : " nu was not declared in this scope"

Please suggest where I am going wrong. I am using OpenFOAM 2.4.0

 nero235 October 13, 2016 10:30

You're calling the wrong function. The function name is
Code:

`nu()`
See:
Code:

`singlePhaseTransportModel.H`
.

Best, Sebastian

 suman91 October 26, 2016 09:19

Hi Sebastian,

Thanks for the reply. Really appreciate it. Sorry was not able to reply to this before. I should have posted an update. So, I was able to access the viscosity values in the following manner:

Code:

`volScalarField viscosity = laminarTransport.nu();`
Another way I was able to access it was through the turbulence object

Code:

`volScalarField viscosity = turbulence->nu();`
Thanks again.
Suman

 All times are GMT -4. The time now is 10:13.