CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Running, Solving & CFD

Possibility to perform two simple.loop inside the same code

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 23, 2018, 17:05
Default Possibility to perform two simple.loop inside the same code
  #1
Member
 
Pablo Alarcón
Join Date: Mar 2018
Location: Liège
Posts: 59
Rep Power: 8
palarcon is on a distinguished road
Good morning everybody

I would like to know if it is possible to perform two simple.loop inside the same code. In simplified words, what I want to do is something like this algorithm

Code:
int main(...)
{
while(it <= maxIter)
{
     ...
     while (simple.loop())     // first loop
     {
          ...
          #include "UEqn.H"
          #include "pEqn.H"
          ...
     }
     ...
     while (simple.loop())     // second loop
     {
          ...
          #include "UEqn2.H"
          #include "pEqn2.H"
          ...
     }
     ...
}
...
}
The problem is that when I code like that, the solver enter the first loop and solve the flow but then doesn't enter the second loop.

My guess is that happens because simple.loop already have an stop signal recorded, so when I try to enter the second loop by means of while (simple.loop()) for the second loop, simply doesn't enter because the while(simple.loop()) condition was already fulfilled in the first loop.

Is there any way to restart or put to zero the simple.loop in order to be able to enter to the second loop?

Any insight, references to documentation or examples, or threads in the forum I may have missed are welcome.
palarcon is offline   Reply With Quote

Old   August 25, 2018, 14:08
Default
  #2
Senior Member
 
Peter Baskovich
Join Date: Jul 2014
Posts: 127
Rep Power: 11
pete20r2 is on a distinguished road
I'm not too strong on c++ and I don't have the source in front of me but I'd say that you are right and the second loop doesn't run because the simple.loop() function is returning 0. I think you should reconsider what exactly you are doing because solving the simple loop again is equivalent to just lowering the solver tolerances so it loops more in the first place. You won't be working on the results of the first simple loop until the timestep (iteration for steady state) is advanced.
pete20r2 is offline   Reply With Quote

Old   August 26, 2018, 16:46
Default
  #3
Senior Member
 
Join Date: Aug 2015
Posts: 494
Rep Power: 14
clapointe is on a distinguished road
If you aren't using the simple controls (ie just running for a fixed number of iterations) a simple fix is to use your own for or while loops instead of simple.loop().

Caelan
clapointe is offline   Reply With Quote

Old   August 27, 2018, 04:13
Default
  #4
Member
 
Pablo Alarcón
Join Date: Mar 2018
Location: Liège
Posts: 59
Rep Power: 8
palarcon is on a distinguished road
Actually and in principle, I need the simple controls (or an equivalent one) because is necessary to converge the flow equations in loop 1 before entering loop 2, which, unfortunately solve equations that are really similar. That is the reason why I'm using simple.loop in both.
palarcon is offline   Reply With Quote

Old   August 27, 2018, 04:43
Default
  #5
Senior Member
 
Pablo Higuera
Join Date: Jan 2011
Location: Auckland
Posts: 627
Rep Power: 19
Phicau is on a distinguished road
Hi Pablo,

how about re-defining the variable simple? You can try adding this line after the first loop has finished:

Code:
simpleControl simple(mesh);
I cannot test it right now, but I imagine that in order for it to work, the variable startFrom in controlDict needs to be set to startTime, and startTime to the value of your starting time, usually 0.

Best,

Pablo
__________________
Check out my new project: olaFlow --> The olaFlow Support Thread
Phicau is offline   Reply With Quote

Old   August 27, 2018, 10:05
Default
  #6
Senior Member
 
Join Date: Aug 2015
Posts: 494
Rep Power: 14
clapointe is on a distinguished road
Another option would be to manually set the time with eg. runTime.setTime() -- you'd set it to zero. I think this would reset the simple controls.

Caelan
clapointe is offline   Reply With Quote

Old   August 28, 2018, 01:11
Default
  #7
Senior Member
 
Peter Baskovich
Join Date: Jul 2014
Posts: 127
Rep Power: 11
pete20r2 is on a distinguished road
You could have a look at how the chtMultiRegionFoam handles multiple meshes and solver loops. It appears to setup custom loops for the different regions neatly.
pete20r2 is offline   Reply With Quote

Old   August 28, 2018, 03:10
Default
  #8
Member
 
Pablo Alarcón
Join Date: Mar 2018
Location: Liège
Posts: 59
Rep Power: 8
palarcon is on a distinguished road
Hello

Everybody, I'm taking notes of all of your comments and I will try them today.
palarcon is offline   Reply With Quote

Old   August 28, 2018, 03:18
Default
  #9
Member
 
Pablo Alarcón
Join Date: Mar 2018
Location: Liège
Posts: 59
Rep Power: 8
palarcon is on a distinguished road
Hello Phicau

Can you please explain me the effect of

Code:
simpleControl simple(mesh);
It will reset the convergence criteria, the number of iteration or something different?
palarcon is offline   Reply With Quote

Old   August 28, 2018, 03:21
Default
  #10
Member
 
Pablo Alarcón
Join Date: Mar 2018
Location: Liège
Posts: 59
Rep Power: 8
palarcon is on a distinguished road
Quote:
Originally Posted by clapointe View Post
Another option would be to manually set the time with eg. runTime.setTime() -- you'd set it to zero. I think this would reset the simple controls.

Caelan
I tried that and it "worked" but not perfectly

Code:
runTime.setTime(it - 1,it -1);
The idea is that after completing the computation of the loop 1 (with N inner iterations) must enter the second loop, where the inputs are the results of the first loop.
That is the reason why the counter is reset to the value of the global iteration number, because there should be X number of complete solutions of the flow (because loop 2 changes the topology of the design domain).
palarcon is offline   Reply With Quote

Old   August 28, 2018, 03:29
Default
  #11
Senior Member
 
Pablo Higuera
Join Date: Jan 2011
Location: Auckland
Posts: 627
Rep Power: 19
Phicau is on a distinguished road
Quote:
Originally Posted by palarcon View Post
Hello Phicau

Can you please explain me the effect of

Code:
simpleControl simple(mesh);
It will reset the convergence criteria, the number of iteration or something different?
It will make it have the same value it had when the simulation started. Basically it is the line called by the solver to initialize the object simple.
__________________
Check out my new project: olaFlow --> The olaFlow Support Thread
Phicau is offline   Reply With Quote

Old   August 28, 2018, 09:45
Default
  #12
Member
 
Pablo Alarcón
Join Date: Mar 2018
Location: Liège
Posts: 59
Rep Power: 8
palarcon is on a distinguished road
Hi Pablo

Unfortunately that option doesn't work

Code:
error: redeclaration of ‘Foam::simpleControl simple’
         simpleControl simple(mesh);
Quote:
Originally Posted by Phicau View Post
Hi Pablo,

how about re-defining the variable simple? You can try adding this line after the first loop has finished:

Code:
simpleControl simple(mesh);
I cannot test it right now, but I imagine that in order for it to work, the variable startFrom in controlDict needs to be set to startTime, and startTime to the value of your starting time, usually 0.

Best,

Pablo
palarcon is offline   Reply With Quote

Old   August 28, 2018, 21:26
Default
  #13
Senior Member
 
Pablo Higuera
Join Date: Jan 2011
Location: Auckland
Posts: 627
Rep Power: 19
Phicau is on a distinguished road
I see... what about using another variable name?

Code:
simpleControl simpleSecond(mesh);

while (simpleSecond.loop())     // second loop
     {
...
__________________
Check out my new project: olaFlow --> The olaFlow Support Thread
Phicau is offline   Reply With Quote

Old   August 28, 2018, 22:00
Default
  #14
Senior Member
 
Join Date: Aug 2015
Posts: 494
Rep Power: 14
clapointe is on a distinguished road
Quote:
Originally Posted by palarcon View Post
I tried that and it "worked" but not perfectly

Code:
runTime.setTime(it - 1,it -1);
The idea is that after completing the computation of the loop 1 (with N inner iterations) must enter the second loop, where the inputs are the results of the first loop.
That is the reason why the counter is reset to the value of the global iteration number, because there should be X number of complete solutions of the flow (because loop 2 changes the topology of the design domain).
I see. How many of the outer iterations are you doing? If the secondSimple from above doesn't work, then another option will be to set to zero and then write to zero. Then you'll be at "zero" time but with the data from the previous simple loop.

Caelan
clapointe is offline   Reply With Quote

Old   August 30, 2018, 05:14
Default
  #15
Member
 
Pablo Alarcón
Join Date: Mar 2018
Location: Liège
Posts: 59
Rep Power: 8
palarcon is on a distinguished road
Do you have any idea on how create that second variable simpleSecond.loop in order to be recognized by OpenFOAM?

Quote:
Originally Posted by Phicau View Post
I see... what about using another variable name?

Code:
simpleControl simpleSecond(mesh);

while (simpleSecond.loop())     // second loop
     {
...
palarcon is offline   Reply With Quote

Old   August 30, 2018, 05:15
Default
  #16
Member
 
Pablo Alarcón
Join Date: Mar 2018
Location: Liège
Posts: 59
Rep Power: 8
palarcon is on a distinguished road
I can set the time to zero (or any number) but that doesn't change the convergence criteria.
If the first loop is already converged, then there is no way on entering into the second, that is the reason why I have to reset the convergence criteria and not only the time value.

Quote:
Originally Posted by clapointe View Post
I see. How many of the outer iterations are you doing? If the secondSimple from above doesn't work, then another option will be to set to zero and then write to zero. Then you'll be at "zero" time but with the data from the previous simple loop.

Caelan
palarcon is offline   Reply With Quote

Old   August 30, 2018, 11:02
Default
  #17
Senior Member
 
Join Date: Aug 2015
Posts: 494
Rep Power: 14
clapointe is on a distinguished road
Good point -- I was hoping that writing in addition to changing the time would help. Should have looked at the code first.

What if you just wrote your own conversion criteria? Use two while loops, and when whatever you decide to calculate stops changing, you're converged.

Caelan
clapointe is offline   Reply With Quote

Old   August 30, 2018, 21:13
Default
  #18
Senior Member
 
Pablo Higuera
Join Date: Jan 2011
Location: Auckland
Posts: 627
Rep Power: 19
Phicau is on a distinguished road
Hi Pablo

the way to define the second variable is easy:

Code:
simpleControl simpleSecond(mesh);
I presume that it will need to have the following lines in your controlDict for it to work properly:

Code:
startFrom startTime;
startTime 0;
Pablo
__________________
Check out my new project: olaFlow --> The olaFlow Support Thread
Phicau is offline   Reply With Quote

Old   June 29, 2020, 05:11
Default
  #19
New Member
 
Jesús Miguel Sánchez
Join Date: Jan 2019
Posts: 9
Rep Power: 7
Jesus.M is on a distinguished road
Did the last solution work?
Jesus.M is offline   Reply With Quote

Old   November 15, 2021, 02:22
Post
  #20
New Member
 
JHopman
Join Date: Oct 2021
Posts: 1
Rep Power: 0
Jansois is on a distinguished road
Not sure if it has already been solved but this works for me.


Add an extra constructor for another pimpleControl object in your solver:


Code:
pimpleControl pimple(mesh);    //normal line

pimpleControl pimple2(mesh, "PIMPLE2", true);    //added line
Then you can create additional loops in your solver with:


Code:
while (pimple2.loop())
{
    //do stuff in outer loop

   

    while (pimple2.correct())
    {
         //do stufff in inner loop

    }
}
Now add a dictionary in your system/fvSolution file like this:


Code:
PIMPLE2
{
    nCorrectors         x;
    nOuterCorrectors    y;
}
Hope this helps.
Jansois is offline   Reply With Quote

Reply


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 Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[isoAdvector] Issues with modification of IsoAdvector Code raunakbardia OpenFOAM Community Contributions 16 September 4, 2020 05:36
possibility to perform two simple.loop inside the same code palarcon OpenFOAM Programming & Development 5 August 28, 2018 09:48
Type mismatch: Assigning a scalar to a Field<Type> inside templated BC code karlli OpenFOAM Programming & Development 2 August 28, 2017 01:25
public CFD Code development Heinz Wilkening Main CFD Forum 38 March 5, 1999 11:44
What kind of Cmmercial CFD code you feel well? Lans Main CFD Forum 13 October 27, 1998 10:20


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