CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Defining stop criteria within solver (https://www.cfd-online.com/Forums/openfoam-programming-development/232349-defining-stop-criteria-within-solver.html)

Shibi December 10, 2020 11:56

Defining stop criteria within solver
 
Hello to all,


I am using the solver pimpleFoam in openFoam v7 to do some calculations.


After I achieve convergence of the main fields I would like to solve another equation.


So, I place the following:


Code:

if (pimple.finalPimpleIter())
            {
                bool switch_solver(true);
                label counter(0);
                while (switch_solver)
                {
                    #include "solveMyEquation.H" //has the solve statement inside

                    counter+=1;
                    if (counter==3)
                    {
                        switch_solver=false;
                    }

                }


            }



This works fine but I would like to end the loop by a predefined value of the residuals and not the number of cycles.
Something like:

Code:

  while (residue > myPredefinedResidue)
                {
                    #include "solveMyEquation.H"

                }





Does anyone know how to do this?


Best regards

Shibi December 11, 2020 16:02

Update:


in solve statement within #include "solveMyEquation.H" I made


Code:

SymmTensor<scalar> Residue_ = solve(MyEquation).initialResidual();
Outside of the include:




Code:



counter+=1;



SymmTensor<scalar> tolerance_ (1e-05,1e-05,1e-05,1e-05,1e-05,1e-05); 
 
scalar checkConvergence_(1);

forAll(Residue_,i)
{
    checkConvergence_*=pos( tolerance_[i]-Residue_[i]);
}


if (checkConvergence_>0)
{
      switch_solver=false; // if checkConvergence_ is positive, all multiplications will result in the value 1 and the residue is smaller than the tolerance
               
}
else
{
      switch_solver=true;
}



// If solver takes more than 3 iteration to converge, leave the loop
 if ( counter==3)
 {
      end_orientation_loop=false;           

 }


It does the first pimple cycle and everything is Ok. During the 2nd cycle, I get the following error:


Code:

--> FOAM FATAL ERROR:
index 3 out of range 0 ... 2

    From function void Foam::UList<T>::checkIndex(Foam::label) const [with T = Foam::SolverPerformance<Foam::SymmTensor<double> >; Foam::label = int]
      in file /home/pc/OpenFOAM/OpenFOAM-7/src/OpenFOAM/lnInclude/UListI.H at line 106.


Any suggestions on what to do?


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