CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   do_while loop for coupling two equations (convergence criteria?) (

mhassani February 26, 2011 12:32

do_while loop for coupling two equations (convergence criteria?)
I have a question regarding coupling two scalar transport equations. the equations are coupled through their source term.

from the programmer's guide, source term can explicitly be added to the equation: solve(fvm::laplacian(phi) == f), f being a vol<type>Field.

I want to solve them sequentially and constructing a loop to reach to converge iteratively. in order to see the convergence, I have one problem, how I can save the previous iteration result and subtract them to see if it meets the convergence tolerance.
solve(equation of C1, with a source term of C2)
solve (equation of C2, with a source term of C1)
error1=C1(n-1)-C1; //n-1 represent previous iteration results
error2=C2(n-1)-C2;//n-1 represent previous iteration results
}while(error1<0.001 && error2<0.001) //0.001 is the convergence tolerance
if you can help me, it means a lot. thanks in forward.
p.s. I have declare the errors as volScalarField and I don't know is it ok, and I have faced errors using declaration of this errors and using relational operators (>=). any suggestion?

santiagomarquezd February 28, 2011 08:56

Muhammad, error1 and error2 will be fields and comparing them with scalar is inappropriate, I think. May be you can take the average of error1 and errors absolute values in order to have only on scalar, then compare it with your error criterion.

volScalarField C1nminus1=C1;

solve(equation of C1, with a source term of C2)
error1=Foam::average(Foam::mag(C1nminus1-C1)); //n-1 represent previous iteration results

Or something like this.


akidess February 28, 2011 12:13

Why not use the residual as done in simpleFoam? Something like:


    eqnResidual = solve

    maxResidual = max(eqnResidual, maxResidual);

Edit: Thinking about it a bit more this might just give r = Ax - b, which wouldn't really help you.

santiagomarquezd February 28, 2011 12:35

Yes, I thought in the same thing, but it supposes you're judging convergence based on residuals. Which I can understand at all is the purpose of this loop, Is this a sort of outer loop in order to better couple the solution in each time-step?


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