# Residual definition of OpenFoam

 Register Blogs Members List Search Today's Posts Mark Forums Read

February 9, 2011, 06:04
Residual definition of OpenFoam
#1
New Member

......
Join Date: Jan 2011
Posts: 2
Rep Power: 0
Hi Dear Foamers,

I am a new guy of OpenFOAM, and I just run the test case of pitzDaily in the OpenFoam tutorial, but with 20000 iterations, Because it does not reach the tolerance 1e-6 yet. Anyway, I found that the initial residual has huge oscillation before it reach the tolerance(actually somewhere about 3e-5). So I want to figure out i) what is the type of the initial residual ii) why it behaves like this.

The first thing I do is to check the code (instead of searching answers from the form......). I went directly to the solver PCG and find the definition of the initial residual and finial residual, which will be like this:

solverPerf.initialResidual() = gSumMag(rA)/normFactor;
solverPerf.finalResidual() = solverPerf.initialResidual();

Then I found the definition of the norm Factor:

//-------------------------------------------------------
Foam::scalar Foam::lduMatrix::solver::normFactor
(
const scalarField& psi,
const scalarField& source,
const scalarField& Apsi,
scalarField& tmpField
) const
{
// --- Calculate A dot reference value of psi
matrix_.sumA(tmpField, interfaceBouCoeffs_, interfaces_);
tmpField *= gAverage(psi);
return gSum(mag(Apsi - tmpField) + mag(source - tmpField)) + matrix_.small_;
// At convergence this simpler method is equivalent to the above
// return 2*gSumMag(source) + matrix_.small_;
}
//-------------------------------------------------------------

and also the MACRO definitioin of gSumMag:

//-------------------------------------------------------------

#define G_UNARY_FUNCTION(returnType, gFunc, func, rFunc) \
\
template<template<class> class Field, class Type> \
returnType gFunc(const FieldField<Field, Type>& f) \
{ \
returnType res = func(f); \
reduce(res, rFunc##Op<Type>()); \
return res; \
} \
TMP_UNARY_FUNCTION(returnType, gFunc)
G_UNARY_FUNCTION(Type, gMax, max, max)
G_UNARY_FUNCTION(Type, gMin, min, min)
G_UNARY_FUNCTION(Type, gSum, sum, sum)
G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum)
#undef G_UNARY_FUNCTION

//----------------------------------------------------------------

Actually I got stuck here....... literally speaking, what I want is just to figure out what kind of norm does PCG (or other solvers ) use, and now I messed all the things up...... I GUESS it was L2 norm or Root Mean Square (RMS) because to my knowledge, only L2 norm is defined as SumFunc()/OtherSumFunc, but I do not know if it was correct.

So my question will be what is the initial residual does PCG use? And why after 20000 iterations, oscillation occurs?

Of couse I also want to understand what does these codes do, especially the macro part, which is really hard for me to understand. If someone can explain sth. about this, I'd really appreciate it!

Thanks for any scanner forward!

BR,
------------
bodyouareboy
Attached Images
 residual.png (5.4 KB, 342 views)

 March 13, 2011, 14:30 #2 New Member   Join Date: Feb 2011 Posts: 20 Rep Power: 9 Hi all! Does anyone has an answer regarding the question of boddyouareboy? I'm also interested to figure out what type of residual is actually being calculated by OF. Thank you! L1011

March 14, 2011, 05:39
#3
Senior Member

Anton Kidess
Join Date: May 2009
Location: Germany
Posts: 1,280
Rep Power: 23
Quote:
 Originally Posted by boddyouareboy [...] and also the MACRO definitioin of gSumMag: //------------------------------------------------------------- #define G_UNARY_FUNCTION(returnType, gFunc, func, rFunc) \ \ template class Field, class Type> \ returnType gFunc(const FieldField& f) \ { \ returnType res = func(f); \ reduce(res, rFunc##Op()); \ return res; \ } \ TMP_UNARY_FUNCTION(returnType, gFunc) G_UNARY_FUNCTION(Type, gMax, max, max) G_UNARY_FUNCTION(Type, gMin, min, min) G_UNARY_FUNCTION(Type, gSum, sum, sum) G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum) #undef G_UNARY_FUNCTION //---------------------------------------------------------------- [...] Of couse I also want to understand what does these codes do, especially the macro part, which is really hard for me to understand. If someone can explain sth. about this, I'd really appreciate it!
The macro definition is just to save some coding effort for the authors. gXXX is the parallel safe version of all the sum, min, max functions (the only difference is an additional call to MPI_REDUCE). So gSumMag calls sumMag, and then accumulates the result on one CPU. sumMag itself will just sum up the magnitudes of the field components. So if I didn't miss anything your code snippet calculates an L1 norm.

 March 15, 2011, 04:08 #4 New Member   Join Date: Feb 2010 Posts: 7 Rep Power: 10 Hello together, as far as I understood it, it is a the L1-nrom since the magnitudes of the residual-vector are summed up. But what still is a kind of miracle to me is the role of the normfactor. I don't see what it is necessary for.

 May 13, 2011, 08:24 #5 Senior Member   Dr. Alexander Vakhrushev Join Date: Mar 2009 Posts: 230 Rep Power: 12 The normalization is required, because you variables can vary in different ranges, so it is a kind of unifying the residuals for different scales in your equations. But actually a found an issue, when the tol parameter is not so representative than relTol. For example in my case, for RANS simulation when I switch to the 2nd order, for some equations a strong source terms are produced due to non-orthogonal corrections in sqewed cells. If you analyze the line: Code: ```// At convergence this simpler method is equivalent to the above // return 2*gSumMag(source) + matrix_.small_;``` it gives you impression, that residuals a scaled actually to the magnitudes of the source term. And, as I described before, it can cause for some equation for under estimation of the domain residuals. I tried to get from Code: `gSum(mag(Apsi - tmpField) + mag(source - tmpField)) + matrix_.small_;` what is stated for the moment when we get convergence Code: ```// At convergence this simpler method is equivalent to the above // return 2*gSumMag(source) + matrix_.small_;``` but can not find the transition! Would be someone so kind to clarify this statement? The solution is either to set tol to something around 1e-8...1e-10 with relTol 0, or use tol 1e-12 for all equations tuning the level of accuracy by e.g. relTol 0.001. So thus you define to improve your residuals for 3 orders. relTol can vary base are you making steady or transient calculations. Tushar@cfd and mm.abdollahzadeh like this. __________________ Best regards, Dr. Alexander VAKHRUSHEV Christian Doppler Laboratory for "Advanced Process Simulation of Solidification and Melting" Simulation and Modelling of Metallurgical Processes Department of Metallurgy University of Leoben http://smmp.unileoben.ac.at Last edited by makaveli_lcf; May 13, 2011 at 11:38.

March 16, 2017, 09:28
#6
Member

Fredi Cenci
Join Date: Dec 2016
Posts: 33
Rep Power: 3
Hey guys,

I know the post is old but I would like to clarify some questions about residuals.

1- As I understood residual is the difference between a field (phi) from one iteration to another, right? so is it Initial Residual? Final Residual?

2 -Can someone tell me how to get L1, L2 and L infinity from the log file? Equations are attached.

3 -If I decrease the tolerance of p,U then the final residual will be lower, right?

Is it possible? I dont know what type of residuals openfoam ii giving me on log file..

Code:
```Time = 0.02

Courant Number mean: 0.107298 max: 1.36175
DILUPBiCG:  Solving for Ux, Initial residual = 0.959413, Final residual = 5.4585e-06, No Iterations 2
DILUPBiCG:  Solving for Uy, Initial residual = 0.317289, Final residual = 8.53016e-07, No Iterations 3
DICPCG:  Solving for p, Initial residual = 0.0109834, Final residual = 0.00109738, No Iterations 112
DICPCG:  Solving for p, Initial residual = 0.336017, Final residual = 0.0308665, No Iterations 17
time step continuity errors : sum local = 0.000179841, global = 1.53469e-07, cumulative = 1.16262e-07
DICPCG:  Solving for p, Initial residual = 0.260022, Final residual = 0.0227987, No Iterations 23
DICPCG:  Solving for p, Initial residual = 0.133745, Final residual = 8.55957e-07, No Iterations 180
time step continuity errors : sum local = 2.53382e-09, global = -4.25928e-11, cumulative = 1.16219e-07```
If openfoam is giving me L1, how do I get L_infinity ? L_infinity = max |Delta (Phi_i)|, 1<i<Np

delta (Phi) is the local change of the flow quantity Phi. Np is the mesh total nodes.

Thanks

[Moderator note: merged 3 posts, one moved from L0, L1, L2 ,Linf error norms]
Attached Images
 resi.png (14.7 KB, 53 views)

Last edited by wyldckat; April 30, 2017 at 11:36. Reason: merged 3 posts on the same topic

 March 18, 2017, 13:07 function #7 Member   Fredi Cenci Join Date: Dec 2016 Posts: 33 Rep Power: 3 I didnt find any function to calculate the change of a variable (U,p,omega,k..) between consecutive time steps in all cells.

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Unseen OpenFOAM Running, Solving & CFD 7 April 16, 2014 03:38 xiuying OpenFOAM Running, Solving & CFD 8 August 27, 2013 15:33 Alan OpenFOAM Running, Solving & CFD 10 April 6, 2012 14:02 sunnysun OpenFOAM Running, Solving & CFD 5 March 16, 2009 04:32 carsten OpenFOAM Bugs 11 September 12, 2008 11:16

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