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

Terminate simulation based on probe

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 2 Post By Bernhard

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 11, 2012, 11:25
Default Terminate simulation based on probe
  #1
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
I am running a specific a large set of parameter variations of a specific case (go pyFoam!). The case is using a transient solver, but will eventually result in a steady state simulation. I want to keep that this way.

Based on some probes, I can a posteriori conclude when the steady state was reached. Now, I want to terminate the simulation (with writing output), based on the probes during runTime. This will save me a lot of computational time.

I am not sure how to proceed with this. I am thinking in the direction of some functionObject, but I wonder if anything in this respect has been done already.

I am not afraid to implement things, so any hints or guidance in that direction is very welcome. Also other remarks, hints, tips, suggestions, comments, notes and answers are highly appreciated!
Bernhard is offline   Reply With Quote

Old   December 11, 2012, 16:03
Default
  #2
Member
 
Ganesh Vijayakumar
Join Date: Jan 2010
Posts: 44
Rep Power: 16
ganeshv is on a distinguished road
Take a look at the panicDump functionObject

http://scicomp.stackexchange.com/que...-a-given-range

http://openfoamwiki.net/index.php/Co...unctionObjects
ganeshv is offline   Reply With Quote

Old   December 11, 2012, 16:41
Default
  #3
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
Thanks, I will have a look into it. I think it is not completely what I need, but definitely a good starting point for further implementation.
Bernhard is offline   Reply With Quote

Old   December 12, 2012, 11:06
Default hi
  #4
Member
 
Simon Arne
Join Date: May 2012
Posts: 42
Rep Power: 14
simpomann is on a distinguished road
Hi,

I did something similiar using a bash script.
Basically it reads out the lines of the probe alpha file, creates an average (out of the three probes i used) and terminates interFoam if a condition is met (in this case average=0.7). openFoam is started via a script called Allrun in background and every 5 second there is a check for the condition...
Unluckily I cant publish the code as I created it for my employer

Some hints:

- set a killswitch variable that changes from 0 to 1 when a condition is met (in my case was the average of three probe alpha values >=0.7)
- a basic structure

initialising killswitch, condition etc
start openfoam application in background, e.g. "sh Allrun &"
while [condition is not met]
while read inputline #useful function to grab values from probe file

time="$(echo $inputline | cut -d' ' -f1)"
alpha1="$(echo $inputline | cut -d' ' -f2)"
alpha2="$(echo $inputline | cut -d' ' -f3)"
alpha3="$(echo $inputline | cut -d' ' -f4)"


some stuff about string/integer conversion

if
condition not met : killswitch stays 0
condition is met: killswitch to 1
killall "solvername"
fi

sleep 5
done < path/to/probe/file
done


A useful function for mathematics in bash is |bc
e.g.

final=$(echo "scale=4; ($a+$b+$c)/$nrOfProbes"|bc -l)

the variable final is the average of variables a+b+c, divided by the nrOfProbes (another variable).

Well sorry for not showing up with real code, but I can't. Anyway not sure if this is what you are looking for because i dont know about pyFoam.
Good luck!
Simon

Something that came to my mind: This will leave you with the output from the last normally processed timestep... If you want the real last moment captured, I guess you have to do the following
When the condition is met:
- change writeInterval in controlDict to something super small, what will cause openFoam to write a folder with the timestep data immediately
- terminate process than with killall (I know, its like opening a coke can with a chainsaw, but hey; after the solver termination the rest of your script will run through, e.g. yPlus calculation, foamToVTK or whatever is following after the solution)

Another hint:
- This also will create average values from the header lines of your probe file, for me was no problem as coordinates were super small (so it never met my cancel-condition), but depending on your probe's coordinates you have to find a solution for this (I created an emergency switch that put all read values >1 to 0, because I was processing the alpha field what made it fairly easy)
simpomann is offline   Reply With Quote

Old   December 12, 2012, 11:51
Default
  #5
Senior Member
 
Kent Wardle
Join Date: Mar 2009
Location: Illinois, USA
Posts: 219
Rep Power: 21
kwardle is on a distinguished road
There is also the runTime.writeAndEnd() function which will immediately write the data fields and stop the simulation when called. This may be useful but you will have to trigger it in the solver based on your probe analysis somehow. If you can live with runTimeModifiable on (I cannot) then a flag in controlDict is an option. Just a few thoughts.
kwardle is offline   Reply With Quote

Old   December 12, 2012, 12:16
Default
  #6
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
Dear Simon, thanks for this extensive answer. I did not think about a solution like this, but I think it can work. Did you know about the writeNext or stopNow options for stopAt? That may simplify your scripts a bit.

But I am actually looking for a robust approach. With your approach, if I just rerun the solver, it will not check for this criteria again right?
Also, if I have multiple instances of the same solver running, then I would kill them all, which is also undesired.
Bernhard is offline   Reply With Quote

Old   December 12, 2012, 12:22
Default
  #7
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
Quote:
Originally Posted by kwardle View Post
There is also the runTime.writeAndEnd() function which will immediately write the data fields and stop the simulation when called. This may be useful but you will have to trigger it in the solver based on your probe analysis somehow. If you can live with runTimeModifiable on (I cannot) then a flag in controlDict is an option. Just a few thoughts.
The trick is in 'somehow', I still think functionObjects are the way to go here, but I am experiencing some coding issues here
Bernhard is offline   Reply With Quote

Old   December 13, 2012, 11:27
Default
  #8
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
For future reference, I was able to reproduce the panicDump behavior with codedFunctionObjects

Code:
    codedTest
    {
        type coded;
        functionObjectLibs
        (
            "libutilityFunctionObjects.so"
        );
        redirectType    probeFinishControl;
        outputControl   timeStep;
        minP            0.04;

        code
        #{
           const volScalarField& p = mesh().lookupObject<volScalarField>("p");
           Info << mesh().time().value() << " " << average(p).value() << endl;
           if ( average(p).value() < $minP )
           {
               bool result=const_cast<Time&>(mesh().time()).writeNow();
               FatalErrorIn("Stopped")
                   << "Bla" << endl
                   << exit(FatalError);
           }
        #};
    }
Of course probing is necessary, but this is a starting point

Also, I would like to advertise this related topic: http://www.cfd-online.com/Forums/ope...ionobject.html
chenhu and RomainBou like this.
Bernhard is offline   Reply With Quote

Old   December 13, 2012, 12:36
Default
  #9
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
To continue my monologue. For the cavity-case and icoFoam I was able to get to the converged solution with the function I posted below.

Please note that this is only working for monotic increasing or decreasing convergence at a point, so it is far from a solid check, but, it is a starting point

Code:
   probeFinishControl
    {
        type coded;
        functionObjectLibs
        (
            "libutilityFunctionObjects.so"
        );
        redirectType    probeFinishControl;
        outputControl   timeStep;
        fieldName       p;
        code
        #{
           const volScalarField& p = mesh().lookupObject<volScalarField>("$fieldName");
           
           scalar probe0(p.oldTime()[1]);
           scalar probe1(p[1]);
           scalar diff(probe0-probe1);
           Info << probe0 << endl << probe1 << endl;
           Info << fabs(diff) << endl;
           Info << mesh().time().timeIndex() << endl;

           if ( fabs(diff) < 1.e-7 && mesh().time().timeIndex() > 10 )
           {
               const_cast<Time&>(mesh().time()).writeNow();
               FatalErrorIn("probeFinishControl")
                   << "Probe on testpoint for $fieldName not changing" << endl
                   << exit(FatalError);
           }
        #};
    }
Bernhard is offline   Reply With Quote

Old   December 23, 2012, 07:11
Default
  #10
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by Bernhard View Post
To continue my monologue. For the cavity-case and icoFoam I was able to get to the converged solution with the function I posted below.

Please note that this is only working for monotic increasing or decreasing convergence at a point, so it is far from a solid check, but, it is a starting point

Code:
   probeFinishControl
    {
        type coded;
        functionObjectLibs
        (
            "libutilityFunctionObjects.so"
        );
        redirectType    probeFinishControl;
        outputControl   timeStep;
        fieldName       p;
        code
        #{
           const volScalarField& p = mesh().lookupObject<volScalarField>("$fieldName");
           
           scalar probe0(p.oldTime()[1]);
           scalar probe1(p[1]);
           scalar diff(probe0-probe1);
           Info << probe0 << endl << probe1 << endl;
           Info << fabs(diff) << endl;
           Info << mesh().time().timeIndex() << endl;

           if ( fabs(diff) < 1.e-7 && mesh().time().timeIndex() > 10 )
           {
               const_cast<Time&>(mesh().time()).writeNow();
               FatalErrorIn("probeFinishControl")
                   << "Probe on testpoint for $fieldName not changing" << endl
                   << exit(FatalError);
           }
        #};
    }
Just one possible way to get the sampling done: in swak there is a variant of the coded-functionObject called swakCoded that can be "fed" with the values of "swak global variables". So you could create the probes with swak, feed their values into a global variable and then investigate that in the (swak)Coded-FO. For the other problem that you have: that you can't have "member variables" in the coded-FO that keep their values between timesteps: have a look at what happens to local variables when they are declared static
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   December 23, 2012, 07:44
Default
  #11
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 22
Bernhard is on a distinguished road
Thanks for your suggestions. Using a swak kind of probe would allow for a much nicer control of the probing location.

I have to test the static declaration, in the coded FO it will not work as far as I understand the C++. I see the possibilities when writing an own derived FO however. I should have thought of it.

Thanks again.
Bernhard is offline   Reply With Quote

Old   December 23, 2012, 08:49
Default
  #12
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by Bernhard View Post
Thanks for your suggestions. Using a swak kind of probe would allow for a much nicer control of the probing location.

I have to test the static declaration, in the coded FO it will not work as far as I understand the C++. I see the possibilities when writing an own derived FO however. I should have thought of it.

Thanks again.
Static local variables in functions should work. That is old-school C-behaviour and works inside of methods too (which basically the snipplet in a coded-FO is)
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   September 8, 2014, 11:49
Default
  #13
New Member
 
Join Date: Jul 2013
Posts: 8
Rep Power: 13
Alex2 is on a distinguished road
Hallo,

I'd like to terminate my simulation based on temperature probes. For this purpose I create some probes with createSampledSet. I want to save these probes in a global variable with swakCoded and write a termination criterion. My problem is up to now, that I don't know how to get the probe values to the global variable. Is there an example?

Thank's for your help in advance
Alex2 is offline   Reply With Quote

Old   September 8, 2014, 15:11
Default
  #14
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by Alex2 View Post
Hallo,

I'd like to terminate my simulation based on temperature probes. For this purpose I create some probes with createSampledSet. I want to save these probes in a global variable with swakCoded and write a termination criterion. My problem is up to now, that I don't know how to get the probe values to the global variable. Is there an example?

Thank's for your help in advance
Example for using global variables would be Examples/groovyBC/fillingTheDam (the variables are used in a groovyBC but set in the controlDict with calculateGlobalVariables)

For your purposed you might not even need the variables. There is (in the simpleSwakFunctionObjects - or was it swakSimpleFunctionObjects. I keep forgetting) a FO called writeAndEndSwakExpression that gracefully ends the run if an expression is true (in your case "valueType set")

For an explained example on global variables see my "Programming swak"-presentation from this years workshop in Zagreb (it is linked from the swak-Wiki-page)
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   September 9, 2014, 08:04
Default
  #15
New Member
 
Join Date: Jul 2013
Posts: 8
Rep Power: 13
Alex2 is on a distinguished road
Code:
stopIfNoChange {
                type writeAndEndSwakExpression;
                valueType set;
                logicalExpression "(diffT1 < threshold) || (diffT2 < threshold)";
                logicalAccumulation and;
        setName probePoints;    //my problems start here: how can I define the probes?
        set {
            type cloud;
            axis xyz;
            points (
                (0.25 0.05 0.4)
                (0.25 0.225 0.4)
            );

                variables (
             "T1{set 'probePoints[1]}=sum(T);"   //How can I access the probe values?
            "T1fter{set 'probePoints[1]}=sum(T);"
             "T2{set 'probePoints[2]}=sum(T);"
            "T2fter{set 'probePoints[2]}=sum(T);"
                        "diffT1=mag(T1fter-T1);"
                        "diffT2=mag(T2fter-T2);"
                        "threshold=0.5;"
                );
                delayedVariables (
                    {
                        name T1fter;
                        delay 2500;
                        storeInterval 1;
                        startupValue "1";
                    }
                    {
                        name T2fter;
                        delay 2500;
                        storeInterval 1;
                        startupValue "1";
                    }
                    );
                verbose true;
                outputControlMode timestep;
                outputInterval 1;
                    }
Hallo Bernhard,

thank's for your advise to use writeAndEndSwakExpression FO, but I still have some problems to implement and access the coordinates/values of my probes. I haven't found an example (,yet), how to use writeAndEndSwakExpression FO with valueType set;. Could you please give me a hint or an example?

Thank's in advance
Alex2 is offline   Reply With Quote

Old   September 9, 2014, 17:33
Default
  #16
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by Alex2 View Post
Code:
stopIfNoChange {
                type writeAndEndSwakExpression;
                valueType set;
                logicalExpression "(diffT1 < threshold) || (diffT2 < threshold)";
                logicalAccumulation and;
        setName probePoints;    //my problems start here: how can I define the probes?
        set {
            type cloud;
            axis xyz;
            points (
                (0.25 0.05 0.4)
                (0.25 0.225 0.4)
            );

                variables (
             "T1{set 'probePoints[1]}=sum(T);"   //How can I access the probe values?
            "T1fter{set 'probePoints[1]}=sum(T);"
             "T2{set 'probePoints[2]}=sum(T);"
            "T2fter{set 'probePoints[2]}=sum(T);"
                        "diffT1=mag(T1fter-T1);"
                        "diffT2=mag(T2fter-T2);"
                        "threshold=0.5;"
                );
                delayedVariables (
                    {
                        name T1fter;
                        delay 2500;
                        storeInterval 1;
                        startupValue "1";
                    }
                    {
                        name T2fter;
                        delay 2500;
                        storeInterval 1;
                        startupValue "1";
                    }
                    );
                verbose true;
                outputControlMode timestep;
                outputInterval 1;
                    }
Hallo Bernhard,

thank's for your advise to use writeAndEndSwakExpression FO, but I still have some problems to implement and access the coordinates/values of my probes. I haven't found an example (,yet), how to use writeAndEndSwakExpression FO with valueType set;. Could you please give me a hint or an example?

Thank's in advance
Use the createSampledSet-FO to create the sampled set probePoints separately.

As your value type is already "set" on probePoints you don't need the {set'probePoints} in the variables-section. The variables will then store the values in one array. It is not possible to to access the values separately but all the expressions are "vectorized". But this is not a problem for your application. The logical expression would be something like "TDiff<threshold" and the logicalAccumulation would be "or" (that would give the same semantics your current expression gives: if the difference on ANY point falls below 0.5 -> terminate run)
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   September 10, 2014, 12:43
Default
  #17
New Member
 
Join Date: Jul 2013
Posts: 8
Rep Power: 13
Alex2 is on a distinguished road
Code:
        stopIfNoChange {
                type writeAndEndSwakExpression;
                valueType set;
                logicalExpression "(diffT1 < threshold)"; 
                logicalAccumulation or;  
        setName monitorpoints;
         set {
            type swakRegistryProxy;
            axis y;
            setName probePoints;
            }
                variables (
             "T1=T;" 
            "T1fter=T;"
                        "diffT1=mag(T1fter-T1);"
                        "threshold=0.5;"
                );
                delayedVariables (
                    {
                        name T1fter;
                        delay 2500;
                        storeInterval 1;
                        startupValue "1";
                    }
                    );
                verbose true;
                outputControlMode timeStep;
                outputInterval 1;
                    }

   createProbes
        {
        type createSampledSet;
        outputControl timeStep;
        outputInterval 1;
        setName probePoints;
        set {
            type cloud;
            axis y;
            points (
                (0.25 0.05 0.4)
                (0.25 0.225 0.4)
            );

        }
    }
Hallo Bernhard,

thank's for your help. I try to use the createSampledSet FO now (see code above), but unfortunately it still doesn't work. I get the following error message:
probePoints not found in table. Valid entries: 0()

Can you tell me, what I have to change?
Alex2 is offline   Reply With Quote

Old   September 10, 2014, 13:21
Default
  #18
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by Alex2 View Post
Code:
        stopIfNoChange {
                type writeAndEndSwakExpression;
                valueType set;
                logicalExpression "(diffT1 < threshold)"; 
                logicalAccumulation or;  
        setName monitorpoints;
         set {
            type swakRegistryProxy;
            axis y;
            setName probePoints;
            }
                variables (
             "T1=T;" 
            "T1fter=T;"
                        "diffT1=mag(T1fter-T1);"
                        "threshold=0.5;"
                );
                delayedVariables (
                    {
                        name T1fter;
                        delay 2500;
                        storeInterval 1;
                        startupValue "1";
                    }
                    );
                verbose true;
                outputControlMode timeStep;
                outputInterval 1;
                    }

   createProbes
        {
        type createSampledSet;
        outputControl timeStep;
        outputInterval 1;
        setName probePoints;
        set {
            type cloud;
            axis y;
            points (
                (0.25 0.05 0.4)
                (0.25 0.225 0.4)
            );

        }
    }
Hallo Bernhard,

thank's for your help. I try to use the createSampledSet FO now (see code above), but unfortunately it still doesn't work. I get the following error message:
probePoints not found in table. Valid entries: 0()

Can you tell me, what I have to change?
You don't do much programming, do you? (That is nothing to be ashamed of)

Because for people how do programming the Mantra is "usually things happen in the order in which they are written down". And the functions-entry in OpenFOAM is a bit like programming

Move createProbes in front
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   September 11, 2014, 11:54
Default
  #19
New Member
 
Join Date: Jul 2013
Posts: 8
Rep Power: 13
Alex2 is on a distinguished road
Hallo Bernhard,

thank you very much for your help, it seems to work.
Alex2 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
Strange residuals of the Density Based Solver Pat84 FLUENT 0 October 22, 2012 16:59
Compressible flow simulation at mach .7 mbeals FLUENT 6 April 18, 2009 11:49
About probe result of Wave simulation shiw FLOW-3D 3 March 13, 2009 10:15
strange simulation error Ralf Schmidt FLUENT 2 May 4, 2007 14:02
3-D Contaminant Dispersal Simulation Apple L S Chan Main CFD Forum 1 December 23, 1998 11:06


All times are GMT -4. The time now is 06:44.