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/)
-   -   Edit controlDict at runtime (https://www.cfd-online.com/Forums/openfoam-programming-development/137564-edit-controldict-runtime.html)

menonshyam June 18, 2014 12:50

Edit controlDict at runtime
 
Hello all,

I am trying to edit fields in the controlDict file during the course of my simulation. The code below works just fine if i run my application on a single core but if i use multiple cores through mpirun, I am unable to get the field to update. Any ideas on this!?

IOdictionary controlDict(IOobject("controlDict", runTime.system(),mesh,IOobject::MUST_READ,IOobject ::AUTO_WRITE));
controlDict.set("endTime",50);
controlDict.Foam::regIOobject::write();

Thanks!

menonshyam June 18, 2014 13:04

RE:
 
Actually this does work..seems like there was some issue in decomposePar but the code works!

menonshyam July 15, 2014 19:55

Trouble with editing controldict in parallel run
 
Hello,

So i am still having trouble getting a parallel run to stop based on a condition in the code by editing the control dict file. This is what i have right now:

If condition {
controlDict.set("stopAt","noWriteNow");
controlDict.Foam::regIOobject::write();
}

However when i decompose and run this code on multiple processors, in the processors where this condition is satisfied, a new 'system' folder is created with the updated controlDict as per the code. But the remaining processors and the job itself still appear to keep on running. How do i fix this so that the job ends on all the processors!?

Thanks!

alexeym July 16, 2014 04:14

Hi,

it depends.

If you need to stop simulation when criterion is satisfied on every processor, then you can use reduce operation (http://foam.sourceforge.net/docs/cpp/a07342.html) with 'and' binary operation. So criterion is true only when it is true on every processor.

If you need to stop simulation when criterion is satisfied on any processor, then you can use scatter operation (http://foam.sourceforge.net/docs/cpp/a01997.html) to notify every processor that it's time to stop.

Tom Lauriks February 15, 2021 04:23

Hello, this is an old post, but I ran into the same issue (openfoam v6).

I've read another post on the forum on the same topic, where someone said that in parallel runs, it would be very complicated to update the controlDict from within openfoam. This is probably right, because I tried this and at a certain point, I'm pretty sure I got an error message stating that I was trying to modify the registry object controlDict, which is in fact declared as constant.

However, the controlDict can manually be modified during a parallel run without problems. So what I did was fall back on the basic c++ functionalities, and changed purgeWrite 3 to purgeWrite 0 directly in the file without going through OF functionalities, which indeed delivered the desired result.

I'm just starting with coding in c++, so this might not be the best way to do this, but below I will paste the code that achieved this:
Code:

if (currentTime > 20.0 && currentTime < 21.0)
    {
        Info << type() << name() << ": "
                << "switch time detected" << nl << endl;

        string controlDict(obr_.time().rootPath()/obr_.time().globalCaseName() + "/system/controlDict");
    system("sed -i 's/purgeWrite 3/purgeWrite 0/g' " + controlDict);

    }

This if statement was placed in my function object, in the execute function. To my understanding, the system command allows you to execute bash commands from within a c++ program. Since it is very easy to paste strings together, as demonstrated in the construction of the string containing the path to the controldict and the sed command passed onto the linux bash, I presume this method can be quite versatile.


All times are GMT -4. The time now is 03:34.