Difference between revisions of "Contrib/PyFoam"

From OpenFOAMWiki
m
m
Line 10: Line 10:
 
This library was developed to control OpenFOAM-simulations with a decent (sorry <tt>Perl</tt>) scripting language to do parameter-variations and results analysis.
 
This library was developed to control OpenFOAM-simulations with a decent (sorry <tt>Perl</tt>) scripting language to do parameter-variations and results analysis.
  
It is an ongoing effort.
+
It is an ongoing effort. I add features on an As-Needed basis but am open to suggestions.
  
 
== Examples ==
 
== Examples ==
Line 118: Line 118:
 
== Installation ==
 
== Installation ==
  
# Download [[Media:PyFoam-0.2.tar.gz|the tar file with the sources]]
+
# Download [[Media:PyFoam-0.2.1.tar.gz|the tar file with the sources]]
 
# Untar it
 
# Untar it
# Go to the directory PyFoam-0.2
+
# Go to the directory PyFoam-0.2.1
 
# Install it with the command
 
# Install it with the command
 
<bash>
 
<bash>
Line 143: Line 143:
  
 
* 2006-01-13: Version 0.2
 
* 2006-01-13: Version 0.2
 +
 +
* 2006-01-16: Version 0.2.1
  
 
--[[User:Bgschaid|Bgschaid]] 21:50, 15 Aug 2005 (CEST)
 
--[[User:Bgschaid|Bgschaid]] 21:50, 15 Aug 2005 (CEST)

Revision as of 14:36, 16 January 2006

1 Short description

This Python-library can be used to

  • analyze the logs produced by OpenFoam-solvers
  • execute OpenFoam-solvers and utilities and analyze their output simultaneously
  • manipulate the parameter files and the initial-conditions of a run in a non-destructive manner

1.1 Motivation

This library was developed to control OpenFOAM-simulations with a decent (sorry Perl) scripting language to do parameter-variations and results analysis.

It is an ongoing effort. I add features on an As-Needed basis but am open to suggestions.

2 Examples

These examples should demonstrate the possible applications of the library.

2.1 Compact output

This example is a wrapper for solvers. For each time-step only one line is output: the time and the initial residuals of the linear solvers (with the name of the solved variable). The complete output of the solver is written to a log-file. The residuals are written to separate files in a special directory in the simulation-directory (for plotting).

 
import re,sys
 
from PyFoam.LogAnalysis.LogLineAnalyzer import LogLineAnalyzer
from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
from PyFoam.Execution.AnalyzedRunner import AnalyzedRunner
 
class CompactLineAnalyzer(LogLineAnalyzer):
    def __init__(self):
        LogLineAnalyzer.__init__(self)
 
        self.told=""
        self.exp=re.compile("^(.+):  Solving for (.+), Initial residual = (.+), Final residual = (.+), No Iterations (.+)$")
 
    def doAnalysis(self,line):
        m=self.exp.match(line)
        if m!=None:
            name=m.groups()[1]
            resid=m.groups()[2]
            time=self.getTime()
            if time!=self.told:
                self.told=time
                print "\n t = %6g : " % ( float(time) ),
            print " %5s: %6e " % (name,float(resid)),
            sys.stdout.flush()
 
class CompactAnalyzer(BoundingLogAnalyzer):
    def __init__(self):
        BoundingLogAnalyzer.__init__(self)
        self.addAnalyzer("Compact",CompactLineAnalyzer())
 
run=AnalyzedRunner(CompactAnalyzer(),silent=True)
run.start()

2.2 Parameter Variation

This example does 10 runs of the same case. For each case a different velocity at the inlet is set. After the simulation has ended the mass flow (with this utility) and the pressure difference (with this utility) are calculated and written to the file InflowVariationResults. In addition to this the data of the last time-step is saved to a separate directory (for further analysis):

 
from PyFoam.Execution.ConvergenceRunner import ConvergenceRunner
from PyFoam.Execution.UtilityRunner import UtilityRunner
from PyFoam.LogAnalysis.BoundingLogAnalyzer import BoundingLogAnalyzer
from PyFoam.RunDictionary.SolutionFile import SolutionFile
from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory
 
solver="simpleFoam"
case="pseudoPoro"
pCmd="calcPressureDifference"
mCmd="calcMassFlow"
 
dire=SolutionDirectory(case,archive="InletVariation")
dire.clearResults()
dire.addBackup("PyFoamSolve.logfile")
dire.addBackup("PyFoamSolve.analyzed")
dire.addBackup("Pressure.analyzed")
dire.addBackup("MassFlow.analyzed")
 
sol=SolutionFile(dire.initialDir(),"U")
 
maximum=1.
nr=10
 
f=dire.makeFile("InflowVariationResults")
 
for i in range(nr+1):
    val=(maximum*i)/nr
    print "Inlet velocity:",val
    sol.replaceBoundary("rein","(%f 0 0)" %(val))
    run=ConvergenceRunner(BoundingLogAnalyzer(),argv=[solver,".",case],silent=True)
    run.start()
 
    print "Last Time = ",dire.getLast()
 
    pUtil=UtilityRunner(argv=[pCmd,".",case],silent=True,logname="Pressure")
    pUtil.add("deltaP","Pressure at .* Difference .*\] (.+)")
    pUtil.start()
 
    deltaP=pUtil.get("deltaP")[0]
 
    mUtil=UtilityRunner(argv=[mCmd,".",case,"-latestTime"],silent=True,logname="MassFlow")
    mUtil.add("mass","Flux at (.+) = .*\] (.+)",idNr=1)
    mUtil.start()
 
    massFlow=mUtil.get("mass",ID="raus")[0]
 
    dire.lastToArchive("vel=%g" % (val))
 
    dire.clearResults()
 
    print "Vel: ",val,"DeltaP: ",deltaP,"Mass Flow:",massFlow
    f.writeLine( (val,deltaP,massFlow) )
 
sol.purgeFile()

3 Installation

  1. Download the tar file with the sources
  2. Untar it
  3. Go to the directory PyFoam-0.2.1
  4. Install it with the command
 
python setup.py install

4 Installed Utilities

TODO: short description of the 5 Scripts that will be installed (in addition to the library)

5 Library Documentation

The tar-file contains the documentation of the Library as HTML (in the folder doc)

6 Additional stuff

Since Version 0.2 the distribution contains the benchFoam.py-script

7 History

  • 2005-08-15: Version 0.1
  • 2006-01-13: Version 0.2
  • 2006-01-16: Version 0.2.1

--Bgschaid 21:50, 15 Aug 2005 (CEST)