CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   Operations during iterations (

latvietis December 30, 2012 21:27

Operations during iterations
Greetings, FOAMers!

I have magnetic problem solver


1.    B=fvc::curl(A);

2.    B2=B&B;

3.    //--> calculate reluctivity (viR) using some algorithm

4.    solve (viconst*viR*fvm::laplacian(A)==-fvc::curl(Hc));

5.    // move to point 1. using new A values

but I can't get this code to be iterative. All it solves iterative in different time steps is A (that is what it should do written as this I guess). What I want is that from initial A is calculated B, then B2, then viR, then NEW set of A values. Initial fields are A, viR, and Hc. First two are changing with every iteration. And process should repeat until there is achieved some kind of tolerance. How can I do this in OpenFOAM?

Basically I want that these 5 steps are done during each iteration, so I could get an output similar to this


DICPCG:  Solving for Ax, Initial residual = 1, Final residual = 8.67698e-06, No Iterations 96
DICPCG:  Solving for Ay, Initial residual = 1, Final residual = 9.81058e-06, No Iterations 101
DICPCG:  Solving for Az, Initial residual = 0, Final residual = 0, No Iterations 0

which now informs that I have successfully calculated A, but that would tell me that I have successfully calculated A by being trough all 5 steps.

Any hint would be much appreciated!


Bernhard January 1, 2013 07:15

I do not fully understand what sets of equations you are trying to solve. Is it a time-dependent problem?

Short answer: A while loop does the job.

Long answer:
In case of a steady-state problem, you can just use the time-iteration as iteration over your set of equations. This is what is done in the simpleFoam solver. You can look at some of these cases and check how iterative convergence is controlled in these examples. (using residualControl). (Now rechecking the code, and simpleFoam in 2.1.1 does not explicitly call it a time-loop anymore, although the output does)

If your problem is transient, you cannot misuse the time loop for that, and you should build an additional loop around your five lines of code. This what is done in the pimpleFoam solver.

latvietis January 4, 2013 13:24

Sweet, thanks Bernhard! I tried this before but calculation was blowing up so I was looking for different ways. Just had to rearrange order of equations and then follow your advice.

I don't want to start a new thread for a new simple question as I hope someone will reply in here.

How can I write a field? But not simply like


volScalarField T (    IOobject    (        "T",        runTime.timeName(),        mesh,        IOobject::MUST_READ,        IOobject::AUTO_WRITE    ),    mesh );
but something like


volScalarField T (    IOobject    (        "T",        runTime.timeName(),        mesh    ) );

Sadly it doesn't work this simple as I understand there is need for some argument (equation).

volScalarField T (    IOobject    (        "T",        runTime.timeName(),        mesh    )
b+a (or what ever)

The problem is that I can't write a simple equation and solution is calculated by a rather long C code, so what I want is to simply define a field in createFields.H and that I will use later.

Bernhard January 4, 2013 14:28

I don't understand your follow-up question. Also, writing all on one line doesn't make it clearer to read. I thought it was possible to do just something like T.write() or something like that (see e.g. the source code of potentialFoam)

latvietis January 5, 2013 16:23

Sorry, was a bit tired when I wrote this question.

Lets say I define a field B in createFields.H.


volVectorField B

This rather straightforward definition. I simply write in my C file a line

B = fvc::curl(A)

and solver knows that here I want to calculate my field and afterwards it creates a nice file with all values. But I can't do this:


volVectorField A
          //and put nothing here

It doesn't compile and puts error message "No matching function to call to...... (and some details after)." What should I do if I need to define a field that will be calculated at some point, but I don't have a simple formula to put in there.

Sorry if question seems too trivial, and I hope I explained it better.


olivierG January 7, 2013 04:46


The value in createFields, like your "fvc::curl(A)" are only for initialisation, so you can put what you want (should be dimensioned correctly through). So try to provide a "good" initial guess.


All times are GMT -4. The time now is 15:45.