CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Community Contributions (https://www.cfd-online.com/Forums/openfoam-community-contributions/)
-   -   [PyFoam] First discussion thread about PyFoam (https://www.cfd-online.com/Forums/openfoam-community-contributions/60683-first-discussion-thread-about-pyfoam.html)

gschaider June 14, 2010 08:02

Quote:

Originally Posted by maddalena (Post 262905)
... so I will try the 2nd solution, it seems the easiest to implement.
Or...
  • is there any already implemented function to read only the last line of a file?
  • Or maybe I can search in the forceCoeffs.dat the line starting with the latestTime and extract the coefficient values.
The point is that frm me is difficult to understand what is implemented and how to use it. I am really a newbie in pyFoam!
Thanks,

mad

Reading the last line of a file is a one-liner (no pun intended) in Python

line=open("filename").readlines()[-1]

If you know that the line consists of floats separated by whitespaces you can get an array with the number with

data=map(float,open("filename").readlines()[-1].split())

If furthermore you happen to know that the data will be "<time> <lift> <drag>" then

time,lift,drag=map(float,open("filename").readline s()[-1].split())

will give you these numbers. But of course this expression is the easiest to break

Bernhard

maddalena June 14, 2010 08:48

Ok, I know that this may be not "elegant", but this is the easiest way to get what I want!
Quote:

Originally Posted by gschaider (Post 262907)
If furthermore you happen to know that the data will be "<time> <lift> <drag>" then

time,lift,drag=map(float,open("filename").readline s()[-1].split())

will give you these numbers. But of course this expression is the easiest to break

Thanks a lot!

mad

bastil August 30, 2010 07:50

Trying to use PyFOAM
 
Hi Bernhard,

I try to use PyFOAM for some time now and I have to say it is a great tool alowing me to do some changes in my files easily. However, I have one minor issue:

I have written a Python-Skript using PyFOAM to modify some boundaries. Everything works fine except the fact that I get two files in my directory afterwards:

PlyParser_FoamFileParser_parsetab.py and
PlyParser_FoamFileParser_parsetab.pyc

I guess these are temporal files generated by the ply-Parser but I don't really know why they are not deleted. As soon as I use one of the default PyFOAM scripts this does not occur. Do I have to do something like closing files?

Thanks Bastian

gschaider August 30, 2010 11:34

Quote:

Originally Posted by bastil (Post 273342)
Hi Bernhard,

I try to use PyFOAM for some time now and I have to say it is a great tool alowing me to do some changes in my files easily. However, I have one minor issue:

I have written a Python-Skript using PyFOAM to modify some boundaries. Everything works fine except the fact that I get two files in my directory afterwards:

PlyParser_FoamFileParser_parsetab.py and
PlyParser_FoamFileParser_parsetab.pyc

I guess these are temporal files generated by the ply-Parser but I don't really know why they are not deleted. As soon as I use one of the default PyFOAM scripts this does not occur. Do I have to do something like closing files?

If I remember it correctly they are saved as a sort-of caching mechanism: if the grammar is unchanged and it finds those file they are not rebuilt. But I may be wrong. Anyway: it never bothered too much so I accepted them. But if there is sufficient interest (which woud manifest itself in a bug-report on the OF-extend-Mantis) I will have a look at it

Bernhard

alfa_8C February 3, 2011 02:40

pyFoamPlotWatcher - customRegExp
 
Hy all,

does anybody know, why this expression in customRegExp . file doesn't work?

This is how the string appears on the screen or log file respectively:

volAverage(heatSource) for T = 293
 
And this is my expression:

{"expr":"volAverage(heatSource) for T = (%f%)","name":"Source","title":["Temp"]}

There is neither an error message nor a plot is being shown as I would expect it.

Thank you in advance,
Toni

gschaider February 3, 2011 08:34

Quote:

Originally Posted by alfa_8C (Post 293416)
Hy all,

does anybody know, why this expression in customRegExp . file doesn't work?

This is how the string appears on the screen or log file respectively:

volAverage(heatSource) for T = 293
 
And this is my expression:

{"expr":"volAverage(heatSource) for T = (%f%)","name":"Source","title":["Temp"]}

There is neither an error message nor a plot is being shown as I would expect it.

Thank you in advance,
Toni

At first: I asssume you have a customRegexp-file (case is important)

The problem here is that for regular expressions (not PyFoams fault not even Pythons. That is the way god created regular expressions) the brackets () have a special meaning. So in order to match a ( you're got to write \( in your expression. Same goes for ) but is left to the reader as an exercise

Bernhard

alfa_8C February 4, 2011 06:54

ok thanx - now it works properly ;-)

one further question.

how must the expression look like, if I want to plot more than one value from log file in the same plot-window?

gschaider February 4, 2011 09:51

Quote:

Originally Posted by alfa_8C (Post 293628)
ok thanx - now it works properly ;-)

one further question.

how must the expression look like, if I want to plot more than one value from log file in the same plot-window?

Find a short but crisp example at http://openfoamwiki.net/index.php/Co...oam#New_format (the master/slave-thing)

alfa_8C February 4, 2011 10:00

I'm still using the old format. As I understand this type: slave; issue is only possible by using the new "openFoam-like" format right?
I can't make out how to apply this to my old format... sorry for the inconvenience...

gschaider February 4, 2011 15:27

Quote:

Originally Posted by alfa_8C (Post 293679)
I'm still using the old format. As I understand this type: slave; issue is only possible by using the new "openFoam-like" format right?
I can't make out how to apply this to my old format... sorry for the inconvenience...

Won't work in the old format. Internally everything (old and new format) is converted to the same data structures but the young format knows more than the old format (that is where it differs from real life ;) ). The good news (not exactly new: it is there since I moved to the new format and it was always "hidden" in the online help) is that every utility that uses customRegex has the option --dump-custom-regegex that prints out these data-structures in the new format. So run the PlotWatcher with this option on a case with the old format and replace the customRegex with the output. Story ends with "and he lived happily ever after with the new format"

sakro May 11, 2011 09:04

Hello, I faced really annoying problem and will be very thankful if you help me =)

I'm using pyFoamCaseBuilder.py for generating cases from Star-ccm+. Everything works well until boundaries are not named using character '-', in this case pyFoamCaseBuilder.py complains

Quote:

Building case testCase
Error in /usr/local/bin/pyFoamCaseBuilder.py : Illegal character '-'
Since main script for Star-ccm+ uses naming-convention for boundaries
like:

Quote:

Patch:000-DOMAIN_INLET
Patch:000-DOMAIN_OUTLET
it is essential for me to be able to handle these situations,
thanks in advance

gschaider May 11, 2011 09:24

Quote:

Originally Posted by sakro (Post 307198)
Hello, I faced really annoying problem and will be very thankful if you help me =)

I'm using pyFoamCaseBuilder.py for generating cases from Star-ccm+. Everything works well until boundaries are not named using character '-', in this case pyFoamCaseBuilder.py complains

Since main script for Star-ccm+ uses naming-convention for boundaries
like:

it is essential for me to be able to handle these situations,
thanks in advance

Which version of PyFoam are you using? Chech with pyFoamVersion.py

Is there a longer stack-trace?

Does pyFoamEchoDictionary.py work for the file in question?

Please write a bug-report providing this information and if possible example files to reproduce it (you find a link to the Mantis at the Wiki-page of PyFoam) and I'll have a look at it

sakro May 11, 2011 10:07

I'm using 0.5.5;

well, I figured out, that this is generally inappropriate name for a boundary that begins with a digit (So boundary name should be an identifier, it is not the same in Star-ccm+). Didn't know that :)

since I have names for boundaries like "000-DOMAIN_INLET" pyFoamEchoDictionary.py complains

Quote:

Error in /usr/local/bin/pyFoamEchoDictionary.py : Illegal character '-'
So it is not a PyFoam issue, sorry for interruption

gschaider May 11, 2011 17:31

Quote:

Originally Posted by sakro (Post 307216)
I'm using 0.5.5;

well, I figured out, that this is generally inappropriate name for a boundary that begins with a digit (So boundary name should be an identifier, it is not the same in Star-ccm+). Didn't know that :)

since I have names for boundaries like "000-DOMAIN_INLET" pyFoamEchoDictionary.py complains



So it is not a PyFoam issue, sorry for interruption

Just to clarify: if OF accepts a patch name 00-DOMAIN_INLET (and I wouldn't be surprised if it does) and PyFoam doesn't then this is a bug in PyFoam (how strange that name may seem. Strange mainly for those who are used to programming languages)

is this name accepted by the usual OF-utilities?

Bernhard

sakro May 12, 2011 05:40

Quote:

Originally Posted by gschaider (Post 307259)
Just to clarify: if OF accepts a patch name 00-DOMAIN_INLET (and I wouldn't be surprised if it does) and PyFoam doesn't then this is a bug in PyFoam (how strange that name may seem. Strange mainly for those who are used to programming languages)

is this name accepted by the usual OF-utilities?

Bernhard


That is how I checked it yesterday:
1. copied pitzDaily tutorial
2. in the directory of copied tutorial
pyFoamChangeBoundaryName.py . inlet 000-DOMAIN_INLET --time-step=0
3. simpleFoam

Quote:

Create time

Create mesh for time = 0

--> FOAM Warning :
From function entry::getKeyword(keyType&, Istream&)
in file db/dictionary/entry/entryIO.C at line 77
Reading /run/incompressible/simpleFoam/pitzDaily/constant/polyMesh/boundary
found on line 14 the doubleScalar 0
expected either } or EOF


--> FOAM FATAL ERROR:
hanging pointer, cannot dereference

From function PtrList::operator[]
in file lnInclude/PtrListI.H at line 122.

FOAM aborting

#0 Foam::error::printStack(Foam::Ostream&) in "/opt/openfoam171/lib/linux64GccDPOpt/libOpenFOAM.so"
#1 Foam::error::abort() in "/opt/openfoam171/lib/linux64GccDPOpt/libOpenFOAM.so"
#2 Foam::polyBoundaryMesh::polyBoundaryMesh(Foam::IOo bject const&, Foam::polyMesh const&) in "/opt/openfoam171/lib/linux64GccDPOpt/libOpenFOAM.so"
#3 Foam::polyMesh::polyMesh(Foam::IOobject const&) in "/opt/openfoam171/lib/linux64GccDPOpt/libOpenFOAM.so"
#4 Foam::fvMesh::fvMesh(Foam::IOobject const&) in "/opt/openfoam171/lib/linux64GccDPOpt/libfiniteVolume.so"
#5
in "/opt/openfoam171/applications/bin/linux64GccDPOpt/simpleFoam"
#6 __libc_start_main in "/lib/libc.so.6"
#7
in "/opt/openfoam171/applications/bin/linux64GccDPOpt/simpleFoam"
Aborted
there is no proper error message in OF about that, but it definitely doesn't work =)

I'd note that although "pyFoamChangeBoundaryName.py . inlet 000-DOMAIN_INLET --time-step=0" done its work, after that I couldn't do, say:


Quote:

pitzDaily$ pyFoamEchoDictionary.py 0/U
Error in /usr/local/bin/pyFoamEchoDictionary.py : Illegal character '-'
ps: just noticed that if you change all "inlet" to "000-INLET_OUTLET" by hands than the error that simpleFoam gives you is different:


Quote:

Create mesh for time = 0

--> FOAM Warning :
From function entry::getKeyword(keyType&, Istream&)
in file db/dictionary/entry/entryIO.C at line 77
Reading /run/incompressible/simpleFoam/pitzDaily/constant/polyMesh/boundary
found on line 26 the doubleScalar 0
expected either } or EOF


--> FOAM FATAL IO ERROR:
Expected a ')' or a '}' while reading PtrList, found on line 44 the word 'frontAndBack'

file: /run/incompressible/simpleFoam/pitzDaily/constant/polyMesh/boundary at line 44.

From function Istream::readEndList(const char*)
in file db/IOstreams/IOstreams/Istream.C at line 151.

FOAM exiting
Alexander

philippose June 3, 2011 08:55

Hello Bernhard,

A Good Day to you! Its been a while, and I hope everything is going great with you!

I have been using PyFoam over the last few days to automate some post-processing of OpenFOAM Simulations, and have the following questions / observations:

1. I use a CIFS (Samba) share mounted on my Linux system as my Simulation folder. When using the "SolutionDictionary" class, the constructor quits with an error that the file-system does not support symbolic links. ..... I found out that I can prevent this by using the "paraviewLink=False" entry when calling the constructor, but maybe it would be more elegant if we detect this in the code and make a copy of controlDict to controlDict.foam instead of a symbolic link?


2. When using the "ParsedParameterFile" class, I noticed that the "macroExpansion" option assumes that the path to the "#include" files are relative to the location of the dictionary. .... There are a couple of issues in this case....:

.... 2[a]. It is not always the case that a relative path is used when specifying the location of the file.... it could also be an absolute path, in which case, the line 511 in ParsedParameterFile.py would not provide a usable path.

.... 2[b]. The path may contain environment variables.... in my case this might prove to be a little more difficult, because I used "$FOAM_CASE" which looks like is only valid when actually running the case. ..... However, in general, maybe the python function "os.path.expandvars" should be used on the specified include file before trying to open it.

.... 2[c]. I am not exactly sure what to do in the case when the environment variable is "$FOAM_CASE"..... I was wondering if it makes sense for ParsedParameterFile to consider such special cases and put in the appropriate path.... what do you think?


3. When using pyFoamReadDictionary.py or pyFoamWriteDictionary.py, if the path to the file to be opened is "<case_root>/processor<n>/0/U" .... a processor directory of a parallel case, the script quits with an error.... I have come to the level where it looks like the error is coming from the "ParsedParameterFile" class, but I have not been able to figure out exactly what is causing the issue..... could you please look into this?


Other than these points, I must say..... PyFoam really does a great job :-)! I somehow always stuck to bash scripts for post-processing, but I think thats going to change really fast now.... :-)!

Thanks a lot for all the effort you and the other contributors have put into PyFoam....

Have a great weekend!

Philippose

gschaider June 6, 2011 12:51

Quote:

Originally Posted by philippose (Post 310377)
Hello Bernhard,

A Good Day to you! Its been a while, and I hope everything is going great with you!

I have been using PyFoam over the last few days to automate some post-processing of OpenFOAM Simulations, and have the following questions / observations:

1. I use a CIFS (Samba) share mounted on my Linux system as my Simulation folder. When using the "SolutionDictionary" class, the constructor quits with an error that the file-system does not support symbolic links. ..... I found out that I can prevent this by using the "paraviewLink=False" entry when calling the constructor, but maybe it would be more elegant if we detect this in the code and make a copy of controlDict to controlDict.foam instead of a symbolic link?

I always considered a file-system that doesn't support symbolic links to be crippled. But obviously I'm wrong.

But this can be done. Please write a bug-report at https://sourceforge.net/apps/mantisb...l_bug_page.php

Quote:

Originally Posted by philippose (Post 310377)
2. When using the "ParsedParameterFile" class, I noticed that the "macroExpansion" option assumes that the path to the "#include" files are relative to the location of the dictionary. .... There are a couple of issues in this case....:

.... 2[a]. It is not always the case that a relative path is used when specifying the location of the file.... it could also be an absolute path, in which case, the line 511 in ParsedParameterFile.py would not provide a usable path.

.... 2[b]. The path may contain environment variables.... in my case this might prove to be a little more difficult, because I used "$FOAM_CASE" which looks like is only valid when actually running the case. ..... However, in general, maybe the python function "os.path.expandvars" should be used on the specified include file before trying to open it.

.... 2[c]. I am not exactly sure what to do in the case when the environment variable is "$FOAM_CASE"..... I was wondering if it makes sense for ParsedParameterFile to consider such special cases and put in the appropriate path.... what do you think?

All this seems serious. Please help me to keep track of these issues by writing a bug report at https://sourceforge.net/apps/mantisb...l_bug_page.php (just copy/paste the description)

Quote:

Originally Posted by philippose (Post 310377)
3. When using pyFoamReadDictionary.py or pyFoamWriteDictionary.py, if the path to the file to be opened is "<case_root>/processor<n>/0/U" .... a processor directory of a parallel case, the script quits with an error.... I have come to the level where it looks like the error is coming from the "ParsedParameterFile" class, but I have not been able to figure out exactly what is causing the issue..... could you please look into this?

Same as before: https://sourceforge.net/apps/mantisb...l_bug_page.php (sorry. But that way we're not relying on my poor brain to remember these bugs)


Quote:

Originally Posted by philippose (Post 310377)
Other than these points, I must say..... PyFoam really does a great job :-)! I somehow always stuck to bash scripts for post-processing, but I think thats going to change really fast now.... :-)!

Thanks a lot for all the effort you and the other contributors have put into PyFoam....

Have a great weekend!

Philippose

Thanks

philippose June 6, 2011 17:09

Hello Bernhard,

A Good Evening to you!

Thanks a lot for your response.

I just submitted two bug reports to the Mantis bug-tracker to cover the issues [1] and [2].

Before I submit a bug report for the issue [3] which I had mentioned in the post, I would like to look a little deeper into the actual error I get. I dont thing the information I currently have will be enough for a decent bug report.

Let me know if you would like me to try any changes that you make, or if you want me to try and solve the issues in case you are too busy.


One more question..... did you have a specific reason for using system shell commands for operations such as copy (cp) and delete (rm) instead of for example, the python "shutil" module? .... I was trying to think in terms of platform independence (Windows ports of OpenFOAM).

I remember that a long time ago you had mentioned that platform independence was not part of the specifications of PyFoam.... are you considering going down the platform independence route in the future sometime?

Have a great day!

Philippose

gschaider June 7, 2011 06:54

Quote:

Originally Posted by philippose (Post 310752)
Hello Bernhard,

A Good Evening to you!

Thanks a lot for your response.

I just submitted two bug reports to the Mantis bug-tracker to cover the issues [1] and [2].

Before I submit a bug report for the issue [3] which I had mentioned in the post, I would like to look a little deeper into the actual error I get. I dont thing the information I currently have will be enough for a decent bug report.

Let me know if you would like me to try any changes that you make, or if you want me to try and solve the issues in case you are too busy.

Seen those. Thanks. I'll have a look what I can do this week

Quote:

Originally Posted by philippose (Post 310752)
One more question..... did you have a specific reason for using system shell commands for operations such as copy (cp) and delete (rm) instead of for example, the python "shutil" module?

Reason is that when these parts were implemented I was using (because this was the system default) python2.3 and AFAIK shutil was introduced in 2.4

Quote:

Originally Posted by philippose (Post 310752)
.... I was trying to think in terms of platform independence (Windows ports of OpenFOAM).

I remember that a long time ago you had mentioned that platform independence was not part of the specifications of PyFoam.... are you considering going down the platform independence route in the future sometime?

Well I try to be as platform independent as possible (for instance consequent usage of os.path). What I meant is that I never tested it on windows. It is not very likely that I'll do that in the next time (unless a customer asks me too). It's not what I consider an interesting project

Aurel June 25, 2011 15:05

Problem with OF_2.0.x and pyFoamPlotWatcher.py
 
Hi,

the installation of the current pyFOAM version (0.5.5) as a regular user worked fine. I followed the Wiki (http://openfoamwiki.net/index.php/Co...m#Installation).

With the new OF version 2.0.x I've got problems. SimpleFoam doesn't work at all with pitzDaily tutorial.
I receive following message:

"aurel@aurel-desktop:~/OpenFOAM/aurel-2.0.x/run/pitzDaily$ simpleFoam > log & pyFoamPlotWatcher.py log
[1] 5283
Traceback (most recent call last):
File "/home/aurel/OpenFOAM/PyFOAM/bin/pyFoamPlotWatcher.py", line 3, in <module>
from PyFoam.Applications.PlotWatcher import PlotWatcher
ImportError: No module named PyFoam.Applications.PlotWatcher
aurel@aurel-desktop:~/OpenFOAM/aurel-2.0.x/run/pitzDaily$ "

For assistance to find the error I would be very grateful.

Best regards

Marc


All times are GMT -4. The time now is 19:00.