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

How to lock the fvSolution file ?

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By alexeym
  • 1 Post By alexeym

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 15, 2019, 03:51
Default How to lock the fvSolution file ?
  #1
New Member
 
Aaron Pumnul
Join Date: May 2019
Posts: 9
Rep Power: 7
aaron99 is on a distinguished road
Hello,

I'm very new to OpenFoam but at this time I work with an older version of it (2.3), I'm sure though it won't affect my question.

I would like to lock the fvSolution file because I have another process (created in python) which is writing to it causing it to be modified and then
reread by OpenFoam again.(their access is not synchronized and I want to synchronize it)

my ugly solution is to check explicitly when fvSolution is read:
In regIOobjectRead.C to override
Code:
bool Foam::regIOobject::read()
void Foam::regIOobject::close()
Code:
bool Foam::regIOobject::read() {
   if objPath == "fvSolution" {
     // lock the file to prevent it from being opened by other processes
     flock(fvSolution, LOCK_EX);
   ...
  }
}

 
void Foam::regIOobject::close() {
  ...
  flock(fvSolution, LOCK_UN); //unlock
}
preferred solution:
fvSolution.H also inherits from regIOobject.C so I could override
Code:
bool Foam::regIOobject::read() {//put lock and then call regIOobject::read()}
void Foam::regIOobject::close() {//release lock}
but these don't even get called, I ran the laplacian basic tutorial and
I received no output from these 2 methods. (is fvSolution.H used at all ?)

So how can I prevent an external process from writing to fvSolution when it's being read ?
I don't understand why changing fvSolution.H produces no output.

Last edited by aaron99; May 16, 2019 at 07:07.
aaron99 is offline   Reply With Quote

Old   May 15, 2019, 09:25
Default
  #2
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 38
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

It is not quite clear what you want to achieve.
- Prohibit modification of fvSolution file?
- Ignore modifications of fvSolution file made by another process?
alexeym is offline   Reply With Quote

Old   May 15, 2019, 11:46
Default
  #3
New Member
 
Aaron Pumnul
Join Date: May 2019
Posts: 9
Rep Power: 7
aaron99 is on a distinguished road
Hello Alexey,


when fvSolution file is being read by OpenFoam(call it p1), I want to put a lock on it, such that other external process (call it p2) can't open the file (nor to read or write to it), that is p2 must wait for p1 to finish its job(aka close the stream) and only then p2 will proceed to do with fvSolution whatever p2 intends to.

With other words I want to give p1 exclusive rights to the file, so other processes will wait on p1 to finish its reading/writing and only then will other processes proceed to the fvSolution file.

Thank you!
aaron99 is offline   Reply With Quote

Old   May 16, 2019, 11:43
Default
  #4
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 38
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

Could you elaborate on how you have overridden read method in fvSolution class? For example, I have done the following:

1. Added to fvSolution.H

Code:
        virtual bool read();
just after constructor.

2. Created fvSolution.C with the following code:

Code:
#include "fvSolution.H"

bool Foam::fvSolution::read() {
    Info << "Enter fvSolution::read()" << endl;
    solution::read();
    Info << "Exit fvSolution::read()" << endl;
    return true;
}
3. Added fvSolution.C to Make/files in finiteVolume folder.

4. Recompiled OpenFOAM.

Started simulation, modified fvSolution file and saw:

Code:
...
DILUPBiCGStab:  Solving for k, Initial residual = 4.31815290661e-05, Final residual = 1.53001780603e-13, No Iterations 1
ExecutionTime = 58.26 s  ClockTime = 59 s

regIOobject::readIfModified() : 
    Re-reading object fvSolution from file ".../system/fvSolution"
Enter fvSolution::read()
Exit fvSolution::read()
fieldAverage averages write:
    Calculating averages

Courant Number mean: 0.00120237595803 max: 0.00120238118572
deltaT = 1.72796544044e-06
Time = 4.36792704089e-06

PIMPLE: Iteration 1
DILUPBiCGStab:  Solving for Ux, Initial residual = 0.00128745430922, Final residual = 6.81778088848e-12, No Iterations 1
...
Instead of useless printing you can put there locking and unlocking calls.

Though, I tested it on OpenFOAM-6 (compilation of 2.3.x with gcc-8 is rather unpleasant experience).
aaron99 likes this.

Last edited by alexeym; May 16, 2019 at 11:44. Reason: Forgotten step
alexeym is offline   Reply With Quote

Old   May 17, 2019, 08:33
Default
  #5
New Member
 
Aaron Pumnul
Join Date: May 2019
Posts: 9
Rep Power: 7
aaron99 is on a distinguished road
Thanks for the repply.

the code you provided works fine but only in the case if fvSolution was modified, this triggers
the Foam::fvSolution::read() method which you overrode. (it is fine for the purpose)

But I thought that Foam::fvSolution::read() would be called also for the first time when fvSolution is read, but it's not.

So I would prefer to put a lock as well when fvSolution file is being read for the first time, can I achieve that without checking explicitly in regIOobjectRead.C ?
aaron99 is offline   Reply With Quote

Old   May 17, 2019, 11:44
Default
  #6
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 38
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

During construction of fvSolution class solution::read method is called (from solution constructor). So, you can either modify constructor of solution class (so it locks file), or you can try to modify fvSolution.

In case of solution class modification, you can add bool parameter to constructor with default false value. In fvSolution constructor you set this parameter to true. Locking/unlocking should be added to solution's read method.

In case of fvSolution modification, it is a little bit more complicated. Currently fvSolution is a child of the above mentioned solution, make it a child of fileLocker and solution. fileLocker is a class, which locks a file. To the fileLocker constructor you pass objectRegistry and file name, object constructs full path and lock the file. unlock method of fileLocker releases lock, created during construction.

So, after the modification, constructor of fvSolution will look like:

Code:
        fvSolution(const objectRegistry& obr)
        :
            fileLocker(obr, "fvSolution"),
            solution(obr, "fvSolution")
        {
            unlock();
        }
aaron99 likes this.

Last edited by alexeym; May 17, 2019 at 11:45. Reason: Typo
alexeym is offline   Reply With Quote

Reply

Tags
solution control


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
how to calculate mass flow rate on patches and summation of that during the run? immortality OpenFOAM Post-Processing 104 February 16, 2021 08:46
OpenFoam "Permission denied" and "command not found" problems. iyidaniel@yahoo.co.uk OpenFOAM Running, Solving & CFD 11 January 2, 2018 06:47
[foam-extend.org] problem when installing foam-extend-1.6 Thomas pan OpenFOAM Installation 7 September 9, 2015 21:53
"parabolicVelocity" in OpenFoam 2.1.0 ? sawyer86 OpenFOAM Running, Solving & CFD 21 February 7, 2012 11:44
ParaView Compilation jakaranda OpenFOAM Installation 3 October 27, 2008 11:46


All times are GMT -4. The time now is 10:27.