|
[Sponsors] |
January 20, 2014, 11:02 |
How to extract values with pyFoam?
|
#1 |
Senior Member
Join Date: Oct 2013
Posts: 397
Rep Power: 19 |
I'm using a custom solver coupled with a python script that solves some differential equations for boundary conditions. I use pyFoam to set the BCs. To get the values required for the ODEs I currently use patchIntegrate on a boundary. However, to speed things up I would like to include the functionality in the main solver, to avoid loading the fields again.
I was wondering if it was possible to use pyFoam to extract values from console output and either return them directly in the python script or write them to a csv that can be read easily. I know it supports custom plots with customRegexp files. Right now I'm using the BasicRunner class to run my solver because I use custom plotting and I don't think (or know) that pyFoam plotters could deal with being executed separately for each step. What's the best method here? a) Write file from OpenFOAM instead of console output b) Use pyFoam somehow? c) Parse console output manually in pyFoam On another note, when setting a boundary condition I noticed that I need to set it manually for each file in the processor directories in a parallel run. Is there a different, preferred method for doing this? |
|
January 21, 2014, 08:07 |
|
#2 |
Senior Member
Join Date: Oct 2013
Posts: 397
Rep Power: 19 |
I found the boundary condition externalCoupled (see here: http://foam.sourceforge.net/docs/cpp...0.html#details ), which can apparently be used for coupling with an external program.
This BC doesn't appear to be used in any of the tutorials, does anyone have a sample test case for it, together with a simple external application? I would like to use it as it would provide me with a much better interfacing method, but I would like to avoid spending ages to figure out how it works if there is a working example already. |
|
January 22, 2014, 20:57 |
|
#3 | |
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
Quote:
- do a simulation - do patchIntegrate to get the values - read the values to do the ODE - write the results to apply them to BCs (is this the same simulation as point 1?) To get the values that patchIntegrate spits out into a timeline you could use pyFoamUtilityRunner.py to call it (with an appropriate regular expression). That file could be converted to CSV with pyFoamConvertToCSV.py (or a simple self-written script) What I usually prefer to call patchAverage on the simulation data after it has finished is let a function object do that calculation during the simulation (I use swak4Foam)
__________________
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 |
||
January 23, 2014, 04:02 |
|
#4 |
Senior Member
Join Date: Oct 2013
Posts: 397
Rep Power: 19 |
That's what I'm currently doing. It works, but it's somewhat ugly, as OpenFOAM needs to be restarted after each time step. Right now I'm working on adjusting my setup for the externalCoupled BC, I believe this should give some performance improvements and better precision (because the time steps of ODE and OF can then be the same, possibly using the minimum time step controlled by both programs).
Using a function object would probably give some small performance benefit over the external tool, right now I'm thinking of doing it directly in the solver and outputting the results to console. |
|
January 23, 2014, 09:18 |
|
#5 | |
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
Quote:
- function Object evaluates values on a boundary. Feeds them into "global variables" - function object with Python-interpreter does some calculation (using these variables). Gives back the results into global variables (Python-interpreter keeps its state so you can do some integration during the simulation) - these global variables can then be used by groovyBC on the "target boundary" I haven't done this exact thing yet with it but I don't see why it shouldn't be possible. If you give it a try use the development version of swak4Foam. Python-integration is much better there (compared to the released version). Parallel might be bit trickier because it might be necessary to make the Python-code parallel aware
__________________
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 |
||
January 23, 2014, 09:48 |
|
#6 |
Senior Member
Join Date: Oct 2013
Posts: 397
Rep Power: 19 |
I haven't used swak4Foam yet, until now I have used pyFoam to write the boundary conditions after each time step, and then start OF from there again.
Have you checked out the externalCoupled BC in OF 2.2.2? I have nearly completed modifying my python code for it now so I'll soon see how it works out. It also supports parallel computing apparently. I'm only using it now to supply OF with the new BC values, the other direction, OF -> python will be handled through log parsing. Using a function object might still be a bit faster, but I believe that this should be negligible. |
|
January 23, 2014, 11:41 |
|
#7 | |
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
Quote:
__________________
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 |
||
January 24, 2014, 03:51 |
|
#8 |
Senior Member
Join Date: Oct 2013
Posts: 397
Rep Power: 19 |
Good point, I'll keep this in mind when this happens, I wasn't aware of that. However, using python it should be possible with subprocess.Popen() to monitor stdout without writing to disk, or is this buffered as well?
|
|
January 24, 2014, 06:03 |
|
#9 |
Senior Member
Join Date: Oct 2013
Posts: 397
Rep Power: 19 |
I have encountered a design problem with the externalCoupled condition:
Basic structure of my solver: -Outer electrical circuit described by ODEs that provides a potential -Solver calculates current which is fed back in the ODEs to calculate potential for next time step Now when I try to use the externalCoupled condition for the potential boundary, I was expecting that the locking would occur at the end of a time step. Instead, it happens directly after solving for the potential field. The solved field for the time step is written to data.out, OF deletes the lock file and waits for the external program to create data.in and recreate the lock. However, this makes it impossible to extract the current from this time step, as it is obviously calculated after solving for the potential. Now I think there are two possible methods: - Also add a lock for the current field. I am not sure if this works because the current is not solved for, but calculated explicitly. Also the BC for the current needs to be calculated from the potential and conductivity BCs for correct results. I think this would also require to solve the potential equation twice, so that OF asks the external program for a new BC again after the current was calculated. This should not be much of an issue since the potential field should already be converged and there should not be much overhead. - Manually create an lock in the solver at the end of the time step. I am not quite sure how to do this when the solver needs to run in parallel. Is it as simply as writing a file and checking for its existence? How to keep the cores in sync? I don't really know about the methods used in OF for parallelization. Right now I'm a bit at a loss why this BC was created this way. Atleast for my case it would have been much more practical if the lock would occur at the start/end of a time step. |
|
January 24, 2014, 06:10 |
|
#10 |
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
If the writing process believes that he is writing to a terminal it SHOULD be unbuffered (pyFoamRunner etc rely on this).
__________________
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 |
|
January 24, 2014, 16:14 |
|
#11 | |
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
Quote:
__________________
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 |
||
January 27, 2014, 03:37 |
|
#12 |
Senior Member
Join Date: Oct 2013
Posts: 397
Rep Power: 19 |
I have come up with a method to call the solve() twice for the electric potential equation. It's a bit hackish, but it should work without a performance impact because it's called one time after it is already solved, and so it should return quickly.
FunctionObject would also be a way I guess, I just haven't used it yet so I wasn't sure how to do it. If the current approach works then I'll likely stick to that. |
|
January 27, 2014, 11:10 |
|
#13 |
Senior Member
Join Date: Oct 2013
Posts: 397
Rep Power: 19 |
I wasn't able to use the externalCoupled boundary, as it produced some problems.
The boundaries had two value fields in the dictionaries, causing errors in paraview, and I also got unphysical solutions (though that may be caused by something else). I have switched to a manual locking method in the solver and use a simple file format for exchanging data now. I'm aware that there are better methods (though I don't know exactly which, not too familiar with programming under Linux), but for now this should suffice. This method has the advantage that it removes the need to parse log files to extract the results of the previous step. Also, now that the solver is running continously, I can use pyFoamPlotRunner for plotting! Thanks for your input! |
|
January 27, 2014, 21:16 |
|
#14 |
Assistant Moderator
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51 |
That would always have been possible with a second terminal and pyFoamPlotWatcher.py
__________________
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 |
|
Tags |
pyfoam customregexp |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Values for nu t and nu tilda | Phizz82 | OpenFOAM Running, Solving & CFD | 1 | March 2, 2020 07:00 |
Periodic (rotational)flow: a bit of confusion concerning reference values | Mr.Goodcat | FLUENT | 0 | March 22, 2016 08:31 |
strange u+ values with LES-Smagorinsky | matejmuller | OpenFOAM Running, Solving & CFD | 2 | January 8, 2016 07:08 |
Extract nodal values | kctian | STAR-CCM+ | 1 | December 26, 2013 16:13 |
How to extract values along a line? | inf.vish | OpenFOAM Post-Processing | 2 | August 6, 2013 23:49 |