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/)
-   -   Change solver tolerance depending on solution (https://www.cfd-online.com/Forums/openfoam-programming-development/236446-change-solver-tolerance-depending-solution.html)

msaravia May 30, 2021 23:34

Change solver tolerance depending on solution
 
Hello everyone.

I need to change the solver tolerances while running. More specifically, I need to change the absolute tolerance of the solver after a certain number of iterations.

I know the fvMatrix class has a solver object; but neither of them has a getter implemented to return a reference to the tolerance parameter (which is read from fvSolution).

Do you have any idea about how I can achieve this? Thanks in advance.

Siassei May 31, 2021 01:51

A little hint.
Not tested until yet, but adding runTimeModifiable to fvSolution?

doc:
runTimeModifiable
Switch for whether dictionaries, e.g. controlDict, are re-read during a simulation at the beginning of each time step, allowing the user to modify parameters during a simulation.

msaravia May 31, 2021 10:08

Quote:

Originally Posted by Siassei (Post 804968)
A little hint.
Not tested until yet, but adding runTimeModifiable to fvSolution?

doc:
runTimeModifiable
Switch for whether dictionaries, e.g. controlDict, are re-read during a simulation at the beginning of each time step, allowing the user to modify parameters during a simulation.

Hi Thomas, thank for the hint! I have considered it, specially because if I get to access the fvSolution object I would need to make sure that this option is activated in order to get the dictionary to be read at every iteation.

Apparently I can access the linear solver controls through the mesh as mesh.solverDict(fieldName). fvMesh is derived from fvSolution so that all fields have access to the fvSolution from the mesh reference they hold.

I will keep you posted

msaravia June 1, 2021 11:55

Well, after trying several things. I managed to change the solver settings at runtime without needing to write the dictionary to file.
For anyone needing to do it, here it is my solution.

a) Create a dictionary reading the original solver settings.

Code:

IOdictionary solDict
(
        IOobject
        (
        "fvSolution",
        runTime.caseSystem(),
        runTime,
        IOobject::MUST_READ,
        IOobject::AUTO_WRITE
        )
);

dictionary& solverControl = solDict.subDict("solvers").subDict("A");

b) modify the solver control as needed

Code:

solverControl.set("tolerance", 1E-10);
C) Now, the magic. Just call the solver with the control dictionary as argument.

Code:

AEqn.solve(solverControl);
The solution was so simple that now I feel the dumbest person in the world. You can make fun of me; I take it.

However, I still need to change the number of non-orthogonal correctors on the fly, which apparently has not a simple solution.


All times are GMT -4. The time now is 11:12.