Runtime modification of a volScalarField
Hey all,
I would like to modify a volScalarField called "modifyS" during runtime of a simulation. "modifyS" is a uniform field and located in the "constant" directory of my case. In "createFields.H" I specify this field by using following IOobject definition: ---------------------- volScalarField modifyS ( IOobject ( "modifyS", runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE ), mesh ); ---------------------- My problem: If I modify "modifyS" during runtime, the modification is not read in. Even if I insert the lookup function at the beginning of the time loop, a modification is not read in: ---------------------- const volScalarField& modifyS = mesh.lookupObject<volScalarField>("modifyS"); ----------------------- Does anyone know how to modify a volScalarField during runtime? Thanks in advance! Regards, Daniel |
Quote:
when you are writing ---------------------- const volScalarField& modifyS = mesh.lookupObject<volScalarField>("modifyS"); ----------------------- you are not asking OpenFOAM to re-read field. This string means only: "find object of type volScalarField with the name modifyS and return constant reference to it to me" Second note. IOobject only defines flags for I/O, but method readIfModified() of the class regIOobject (which inherits from IOobject and inherited by volScalarField) do the task what you want. So, you can try to insert line: --- modifyS.readIfModified(); --- at the start of each time step: while (runTime.run()) { modifyS.readIfModified(); // do something else } Third note. You can always check whether dictionary (field) was modified by the method modifyS.modified(). Than, you can run method modifyS.read() See documentation here Hope, this helped to you |
Thanks a lot mkraposhin for your response. I really appreciate that.
Unfortunately the volScalarField "modifyS" ist still not updated when modified. I have implemented it this way: In createFields.H: ------------------------------------- Info<< "Reading field modifyS\n" << endl; volScalarField modifyS ( IOobject ( "modifyS", runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE ), mesh ); ----------------------------------- In shallowFoam.C: ---------------------------------------- ... while (runTime.run()) { #include "readTimeControls.H" #include "CourantNo.H" #include "setDeltaT.H" modifyS.readIfModified(); runTime++; Info<< "modifyS = " << modifyS << nl << endl; //... something else } ----------------------------------- I have deleted the lookup- function. Do you or does someone else have an idea what else i can try? I have another problem: As soon as I define in createFields.H the IOobject "modifyS" as "MUST_READ_IF_MODIFIED", a modification of "modifyS" sometimes causes an abortion of the simulation with this error: -------------------------------- regIOobject::readIfModified() : Re-reading object modifyS from file "/home/BAUINFORMATIK/OpenFOAM/run/interactivity/PflegerTestModifyS/constant/modifyS" --> FOAM FATAL IO ERROR: cannot open file file: /home/BAUINFORMATIK/OpenFOAM/run/interactivity/PflegerTestModifyS/constant/modifyS at line 0. From function regIOobject::readStream() in file db/regIOobject/regIOobjectRead.C at line 87. FOAM exiting ---------------------------------------- Does someone know the reason for this error and why it occurs just sometimes? Remarks: - When I define the IOobject modifyS at the beginning of the time loop (with MUST_READ or MUST_READ_IF _MODIFIED), the modification actually works. But I also get sometimes this error that the corresponding file can't be opended. So if I can get rid of this error, I can at least use this unconventional solution. - I have tested to modify the IOdictionary "transportProperties", and there a modification is always read in and the simulation is never stopped by an error. I don't know why it does not work for a volScalarField. Thanks a lot in advance! |
1 Attachment(s)
Hi, Scofield
By inspecting the OpenFOAM source code i found, that you cannot update volScalarField only by specifying MUST_READ_IF_MODIFIED at the construction. The problem is that only IOdictionary class knows how to read data from file when it was modified. More over, when object was modified, OpenFOAM object runTime reads it (by executing function ::read()) and sets the modified flag() to false. You have three ways: 1) Use IOdictionary to store additional flags which will show that field modifyS was modified. If this flag was triggered, then, you need to re-init your field 2) Derive interface class from regIOobject and dictionary to read field when it was modified and then, substitute to volScalarField as a construction parameter 3) Derive you own field class from the GeometricField. I will explain to you path #2 1) Create class IOfieldDictionary, which is derived from regIOobject and dictionary Code:
Code:
IOfieldDictionary modifySDict Code:
while (runTime.loop()) when you finished with changing of the field in modifyS, you must set flag "modified" to true: Code:
modified true; |
Thanks a lot mkraposhin for your help. I have integrated it in my code and it works, your response was the solution for my problem. Great!
In another thread I have posted a similar problem: http://www.cfd-online.com/Forums/ope...tml#post454451 |
All times are GMT -4. The time now is 10:09. |