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)

wyldckat August 19, 2013 12:45

Hi Izna,

You apparently have not yet installed swak4Foam: http://openfoamwiki.net/index.php/Contrib/swak4Foam

Best regards,
Bruno

izna August 19, 2013 12:57

Oh Brunos..:(
Again errors..:(

Quote:


Plotting the following cases:
['../Bump2D_run_1.06_10.0_1500.0_200.0_200.0_0.5_2000']
Traceback (most recent call last):
File "./runBump2D.py", line 211, in <module>
plot2DBump.main(target,hSample)
File "/home/izna/Desktop/benmartinez/2DBumpTemplate/plot2DBump.py", line 38, in main
m = max(lastRun)
ValueError: max() arg is an empty sequence



wyldckat August 21, 2013 10:48

Hi Izna,

That error does not seem to be PyFoam's nor swak4Foam's fault.
Please start a new thread about this particular topic and please describe where you got those cases/scripts and how you ran the case and/or scripts.

Best regards,
Bruno

zaynah04 August 21, 2013 10:52

I will post one thing for this..

I have checked Hanan case, and i think there is soem errors.. i will post mne.. and we can aall compare

francois April 8, 2014 11:27

Hi all,

I've an easy one: "How to tell pyFoam to use matplotlib instead of gnuplot by default ?" :D

I've an already converged case and want to plot for example the residuals with matplotlib. What's the best procedure to achieve this ?

Same kind of question for example for pyFoamSamplePlot.py.

I'm sorry if it looks too trivial but I've tried to find some help in the forum and the doc without success.

Thanks for your help.
Regards
François

gschaider April 11, 2014 09:02

Quote:

Originally Posted by francois (Post 484701)
Hi all,

I've an easy one: "How to tell pyFoam to use matplotlib instead of gnuplot by default ?" :D

I've an already converged case and want to plot for example the residuals with matplotlib. What's the best procedure to achieve this ?

Same kind of question for example for pyFoamSamplePlot.py.

I'm sorry if it looks too trivial but I've tried to find some help in the forum and the doc without success.

Thanks for your help.
Regards
François

If you have the suspicion that something SHOULD work in pyFoam try something like "pyFoamPlotWatcher.py --help" and have a look. In this special case the section "Plot Behaviour" and especially the --implementation-option might be of interest. Also have a look at pyFoamRedoPlot.py (this uses Matplotlib by default)

pyFoamSamplePlot.py only supports Gnuplot but you can write out the data to a CSV-file and then visualize it with whatever you like

Also some utilities (but not SamplePlot) support reading the data and then dropping you to an interactive python-shell. See for instance http://openfoamwiki.net/staticPages/...tml#/sec-3-2-2

My current policy is "Support GNUplot by default because it is an order of magnitude faster than Matplotlib. Also is the API of matplotlib changing and what works on one version might stop working on another. Start offering the possibility to easily export data into the Numpy/Pandas/Matplotlib-stack and then everyone can visualize the way he likes"

francois April 11, 2014 09:10

Thanks Bernhard for your exhaustive and helpful answer :)
It's clearer for me now

Regards,
François

DAPHNE June 5, 2014 05:09

Extracting the values from the patchAverage utility through python script
 
Quote:

Originally Posted by gschaider (Post 193944)
Hi Fabian!

The lines you want to change are:

pUtil.add("deltaP","Average of p over patch (.+) = (%f%)",idNr=1)

In order to get added as data the pattern has to be enclosed in (). The first one is used as the ID (as specified by the option). The %f% is replaced with the regular expression that matches a floating point number (just a shorthand in the PyFoam-classes that use regular expressions)

The second part took me longer (I thought my classes were broken, but in fact in your example the [2] was missing):

deltaPressure=pUtil.get("deltaP",ID="einlass:4[2]")[0]

Now deltaPressure has the string 0.557439 (which is what you wanted?)

Bernhard

Hi Bernhard and All,
I tried to extract the values from the patchAverage utility through python script.
I used patchAverage utility to find the pressure field of cavity case:
################################################## ##########
tutorials/incompressible/icoFoam/cavity$ patchAverage p movingWall
Create time

Create mesh for time = 0

Time = 0
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0

Time = 0.1
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597376

Time = 0.2
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597292

Time = 0.3
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597295

Time = 0.4
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597294

Time = 0.5
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597292

End
################################################## ##################

The script I used is:
###############################################
solver="icoFoam"
case="cavity"
pCmd="patchAverage"
patch="movingWall"

pUtil=UtilityRunner(argv=[pCmd,"p",patch],silent=True,logname="patchAverage1")
pUtil.add("deltaPp","Average of volScalarField over patch movingWall[0](.+) =(%f%)",idNr=1)
pUtil.start()
deltaP=pUtil.get("deltaP",ID="movingWall[0]") [0]
deltaPressure=pUtil.get("deltaP")[0]
print deltaPressure
############################################
Error
/ParameterVariation/cavity$ python utilityrunner.py
Traceback (most recent call last):
File "utilityrunner.py", line 22, in <module>
deltaP=pUtil.get("deltaP")[0]
TypeError: 'NoneType' object has no attribute '__getitem__'

################################################## ##########

So I tried the following:
################################################## #########
from PyFoam.Execution.ConvergenceRunner import ConvergenceRunner
from PyFoam.Execution.UtilityRunner import UtilityRunner
from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
from PyFoam.LogAnalysis.UtilityAnalyzer import UtilityAnalyzer


solver="icoFoam"
case="cavity"
pCmd="patchAverage"
patch="movingWall"

pUtil=UtilityRunner(argv=[pCmd,"p",patch],silent=True,logname="patchAverage1")
pUtil.add("volScalarFieldp","Average of volScalarField over patch movingWall[0](.+) =(%f%)",idNr=1)
pUtil.start()
deltaP=pUtil.get("volScalarFieldp",ID="movingWall" )
print UtilityRunner.get(pUtil,"volScalarField p")
deltaPressure=pUtil.get("volScalarField")

print deltaPressure

##############################################
Output:
None
None
##############################################
Still the values are not extracted..

Thank you.

Regards,
Daphne

gschaider June 5, 2014 05:28

Quote:

Originally Posted by DAPHNE (Post 495662)
Hi Bernhard and All,
I tried to extract the values from the patchAverage utility through python script.
I used patchAverage utility to find the pressure field of cavity case:
################################################## ##########
tutorials/incompressible/icoFoam/cavity$ patchAverage p movingWall
Create time

Create mesh for time = 0

Time = 0
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0

Time = 0.1
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597376

Time = 0.2
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597292

Time = 0.3
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597295

Time = 0.4
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597294

Time = 0.5
Reading volScalarField p
Average of volScalarField over patch movingWall[0] = 0.0597292

End
################################################## ##################

The script I used is:
###############################################
solver="icoFoam"
case="cavity"
pCmd="patchAverage"
patch="movingWall"

pUtil=UtilityRunner(argv=[pCmd,"p",patch],silent=True,logname="patchAverage1")
pUtil.add("deltaPp","Average of volScalarField over patch movingWall[0](.+) =(%f%)",idNr=1)
pUtil.start()
deltaP=pUtil.get("deltaP",ID="movingWall[0]") [0]
deltaPressure=pUtil.get("deltaP")[0]
print deltaPressure
############################################
Error
/ParameterVariation/cavity$ python utilityrunner.py
Traceback (most recent call last):
File "utilityrunner.py", line 22, in <module>
deltaP=pUtil.get("deltaP")[0]
TypeError: 'NoneType' object has no attribute '__getitem__'

################################################## ##########

So I tried the following:
################################################## #########
from PyFoam.Execution.ConvergenceRunner import ConvergenceRunner
from PyFoam.Execution.UtilityRunner import UtilityRunner
from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
from PyFoam.LogAnalysis.UtilityAnalyzer import UtilityAnalyzer


solver="icoFoam"
case="cavity"
pCmd="patchAverage"
patch="movingWall"

pUtil=UtilityRunner(argv=[pCmd,"p",patch],silent=True,logname="patchAverage1")
pUtil.add("volScalarFieldp","Average of volScalarField over patch movingWall[0](.+) =(%f%)",idNr=1)
pUtil.start()
deltaP=pUtil.get("volScalarFieldp",ID="movingWall" )
print UtilityRunner.get(pUtil,"volScalarField p")
deltaPressure=pUtil.get("volScalarField")

print deltaPressure

##############################################
Output:
None
None
##############################################
Still the values are not extracted..

Thank you.

Regards,
Daphne

There may be other problems, but I think it is the [] in the regular expression. That has a special meaning in regular expression. Replace it with "\[0\]"

For your first steps with regular expressions try something like http://regex101.com/#python (mind: it will NOT understand the %f%. That is PyFoam-specific)

alexeym June 5, 2014 05:31

Hi,

Quote:

Originally Posted by DAPHNE (Post 495662)
The script I used is:
###############################################
solver="icoFoam"
case="cavity"
pCmd="patchAverage"
patch="movingWall"

pUtil=UtilityRunner(argv=[pCmd,"p",patch],silent=True,logname="patchAverage1")
pUtil.add("deltaPp","Average of volScalarField over patch movingWall[0](.+) =(%f%)",idNr=1)
pUtil.start()
deltaP=pUtil.get("deltaP",ID="movingWall[0]") [0]
deltaPressure=pUtil.get("deltaP")[0]
print deltaPressure
############################################
Error
/ParameterVariation/cavity$ python utilityrunner.py
Traceback (most recent call last):
File "utilityrunner.py", line 22, in <module>
deltaP=pUtil.get("deltaP")[0]
TypeError: 'NoneType' object has no attribute '__getitem__'

################################################## ##########

It's just common sense, maybe I'm missing something, but the error usually means that pUtil.get("deltaP") returned None. You've used deltaPp in pUtil.add and you've used deltaP in pUtil.get. It looks rather strange to me.

The same thing is in the second script.

DAPHNE June 5, 2014 05:48

Dear Bernhard and Alexeym,

Firstly, thank you so much for your quick reply.

@Bernhard
The link you suggested to try Regular expressions is much useful. I tried that and made changes as you suggested. But still the same error persisits. Will keep trying and update.

@Alexeym
Sorry for the inconvenience.[made mistake while copy and paste]By mistake I posted as
pUtil.add("deltaPp","Average of volScalarField over patch movingWall[0](.+) =(%f%)",idNr=1)

But actually I used only the following:
pUtil.add("deltaP","Average of volScalarField over patch movingWall[0](.+) =(%f%)",idNr=1)

DAPHNE June 5, 2014 05:55

Hi Alexeym,

Here is the corrected version of the code I used:


pUtil=UtilityRunner(argv=[pCmd,"p",patch],silent=True,logname="patchAverage1")
pUtil.add("deltaP","Average of volScalarField over patch movingWall \[0\] (.+) =(%f%)",idNr=1)
pUtil.start()
deltaP=pUtil.get("deltaP")
print UtilityRunner.get(pUtil,"deltaP")
deltaPressure=pUtil.get("deltaP",ID="movingWall \[0\]")
print deltaPressure

Regards,
Daphne

DAPHNE June 12, 2014 03:30

Hi All,

I managed to correct the code to extract the values from the patch Average utility.

Code:
pUtil1=UtilityRunner(argv=[pCmd,"p",patch],silent=True,logname="patchAverage")
pUtil1.add("PressureDifference","Average of volScalarField over patch (.+)= (%f%)",idNr=1)
pUtil1.start()
a=UtilityRunner.get(pUtil1,"PressureDifference")
print a

Regards,
Daphne

Kojirion June 11, 2015 20:53

Hello,

I am using pyFoam version 0.6.4 with python 3.4.3, which the wiki suggests is fine. It generally works, however I have encountered an error that I 'm almost certain is due to incompatibility between python versions. Line 291 of Benchmark.py reads

Code:

args=string.split("%s %s %s %s" % (pre,workDir,caseDir,post))
I believe that would work with python 2, but does not on 3 (" AttributeError: 'module' object has no attribute 'split'")
(Line 293 suffers from the same problem)

I have also encountered a couple more issues when parsing a dictionary with
ParsedParameterFile that I haven't dug in to find the cause of:

- the value "none" is incorrectly read as "no" and has to be manually fixed
- whitespace is not parsed correctly, for example "(air in water)" when reading the phaseProperties of twoPhaseEulerFoam bubbleColumn tutorial: it results in a list rather than a dictionary.


jensi_t January 27, 2016 07:33

Hello,

I use fluent3DMeshToFoam and fluentDataToFoam to start my calculation from an initial solution (with nonuniform profiles on inlets). Then I apply a changeDictionaryDict to the case to set the right boundary conditions (probably also possible with pyFoam).

One inlet condition for U is for example:
Code:

        farfield_inlet
        {
        type            temperatureDirectedInletOutletVelocity;
        phi            phi;
        T              T;
        T0              uniform 255;
        inletDirection  uniform (1 0 0);
        cylindricalCCS  no;
        omega          ( 0 0 0 );
        value          uniform (245 0 0);
        }

My question is how I can copy the nonuniform Ttot Field of farfield_inlet into the U boundary condition. Is pyFoam my friend in such a case? :)

Thanks in advance.

Jens

jf_vt October 8, 2017 02:58

pyfoam ( small ) bug
 
Hello
I am not sure where to report pyfoam bug so I do it here:
pyfoam version 0.6.4 on top of python 2.7.12 and foam extend 4.0

The bug is:
PyFoam change keyword value from "none" to "no" creating a subsequent solver error in the following situation:

Initial pressure file p in 0 directory has an outlet value as :
Quote:

outlet
{
type totalPressure;
rho none;
psi psi;
gamma 1.3;
p0 $internalField;
value $internalField;
}
I change $internal field value in this file with the commands:
Quote:

pFile = ParsedParameterFile(os.path.join(dirCase,caseName, '0','p'))
pFile['internalField'].setUniform(press)
pFile.writeFile()
press being previously defined in my script.
This is the ONLY change made on this file

but after writeFile(), the boundary condition has been overwritten with:
Quote:

outlet
{
type totalPressure;
rho no;
psi psi;
gamma 1.3;
p0 $internalField;
value $internalField;
}
as stated above.

If I did not miss something obvious, It looks like a parser bug as "no" are the first 2 letter of "none" obviously...
easy to change afterward so a small bug in any case.

( with congratulation and thanks to the developper(s) for this amazing piece of software)

JF

gschaider October 8, 2017 17:07

Quote:

Originally Posted by jf_vt (Post 666874)
Hello
I am not sure where to report pyfoam bug so I do it here:
pyfoam version 0.6.4 on top of python 2.7.12 and foam extend 4.0

The bug is:
PyFoam change keyword value from "none" to "no" creating a subsequent solver error in the following situation:

Initial pressure file p in 0 directory has an outlet value as :


I change $internal field value in this file with the commands:


press being previously defined in my script.
This is the ONLY change made on this file

but after writeFile(), the boundary condition has been overwritten with:


as stated above.

If I did not miss something obvious, It looks like a parser bug as "no" are the first 2 letter of "none" obviously...
easy to change afterward so a small bug in any case.

( with congratulation and thanks to the developper(s) for this amazing piece of software)

JF

The Problem seems to be PyFoam. "none" is assumed to be an equivalent for boolean "false" because OpenFOAM defines it this way in Switch.H and Switch.C. "false" is then later written as "no". One of the instances where PyFoam has to chose because the OF-fileformat is not unambigous ... and it choses wrongly.

The best way for you to fix this quickly is to go to PyFoam/Basics/DataStructures.py look for the FalseStrings-list and comment out "none" as well. That way "none" should be parsed as the word "none".

I'll think of a permanent solution (that will probably be the same thing + praying that it doesn't break too many other cases)

jf_vt October 8, 2017 23:20

Quote:

Originally Posted by gschaider (Post 666925)

The best way for you to fix this quickly is to go to PyFoam/Basics/DataStructures.py look for the FalseStrings-list and comment out "none" as well. That way "none" should be parsed as the word "none".

Thanks
I will keep that in mind if this has a broader impact on my scripts down the road and a real fix appear impractical or difficult.

For the time being, I just added
Quote:

pFile['boundaryField']['outlet']['rho']='none'
to the script and it worked as well

JF

gschaider October 9, 2017 07:37

Quote:

Originally Posted by jf_vt (Post 666946)
Thanks
I will keep that in mind if this has a broader impact on my scripts down the road and a real fix appear impractical or difficult.

For the time being, I just added


to the script and it worked as well

JF

I'll remove "none" from the list of possible "false"-representations. I don't think that anyone is even aware that this is possible (OF specifies 6 different ways to write false ) and it breaks valid cases

mbookin January 16, 2018 09:58

Dear Bernard!

I'm trying to make GUI for my OpenFoam solver with PyFoam supposed to be used for parsing dictionaries.
I thought to use the ParsedParameterFile function for taking data from GUI edit-boxes and writing it to the dictionary. The thing that stopped me was that ParsedParameterFile function cant understand dimensioned scalars in the dictionary. I have written here about this problem, but haven't got any answers. Could you please give me a hint of where to look for. How do you think maybe OpenFOAM classes like IOdictionary is more suitable for that?


All times are GMT -4. The time now is 17:38.