33
25回オープンCAE勉強会@ 関西 2013. 9.21 片山 達也 Using PyFoam as library

Using PyFoam as library(第25回オープンCAE勉強会@関西)

Embed Size (px)

Citation preview

  • 1. 25CAE@ 2013. 9.21 Using PyFoam as library

2. 2013/9/21 25@ 2 / Python PyFoam cavity tutorial 3. / 2013/9/21 25@ 3 OpenMDAO OpenFOAMpython PyFoampython python 4. 2013/9/21 25@ 4 python ( 2.7.3) OpenFOAM ( 2.2.x) PyFoam ( 0.6.1) numpy python() ipython,matplotlib,scipy etc MPI eclipse (+PyDev) 5. Python(1) 2013/9/21 25@ 5 pythonor ipython shell python >>>print Hello World! Hello World! python code > python (or ipython) shell command 6. Python(2) 2013/9/21 25@ 6 >>> a=1 >>> type(a) int >>> b=1.0 >>> type(b) float >>> type(a+b) float >>> a+a Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str' 1 2 3 4 5 6 python code 7. Python(3) 2013/9/21 25@ 7 >>> l=[1,2,san] >>> l[2] san >>> l[2]=3 >>> l[2] 3 >>> d={east:Mac,west:uMakudo} >>> d[east] Makudo >>> t=(l,a) >>> t[1]=1 Traceback TypeError: tuple 1 2 3 4 5 6 7 8 python code 8. Python(4) 2013/9/21 25@ 8 if >>> a=1 >>> if a==1: . . . print a is ,a . . . else: . . . print a is not ,a . . . a is 1 >>> if a==1: . . . print a is ,a IndentationError: expected an indented block 1 2 3 4 5 6 7 8 python code 9. Python(5) 2013/9/21 25@ 9 for >>> for i in l: . . . print 2**i . . . 2 4 8 >>> for i in xrange(0,1): . . . print i . . . 0 1 1 2 3 4 5 6 python code 10. Python(6) 2013/9/21 25@ 10 >>> def myfunc(val): . . . return val+10 . . . >>> myfunc(1) 11 1 2 3 4 python code 11. Python(7) 2013/9/21 25@ 11 >>> import numpy >>> matA=numpy.array([[1,2],[2,3]]) >>> matA array([[1, 2], [ 2, 3]]) >>> import numpy as np >>> x=np.array([1,2]) >>> from numpy import dot >>> y=dot(matA,x) >>> y array([5,8]) >>> exit() 1 2 3 4 5 6 7 8 9 python code 12. Python(8) 2013/9/21 25@ 12 shellpython > echo print Hello > test.py > python test.py Hello shell command 13. PyFoam 2013/9/21 25@ 13 PyFoam tar file(subversion)API PYTHONPATH prefix PYTHONPATH > cd PyFoam > python setup.py install --prefix=~/PyFoam > export PYTHONPATH=~/PyFoam/lib/python2.7/site- packages:$PYTHONPATH shell command 14. Cavity tutorial 2013/9/21 25@ 14 cavity 23 [m] [m] [m/s] 1 0.1 0.1 0.5 2 0.2 0.2 1.0 L H U 15. Cavity tutorial(1) 2013/9/21 25@ 15 SolutionDirectory (Case) >>> import PyFoam.FoaInformation >>> cavitydir= PyFoam.FoamInformation.foamTutorials() + "/incompressible/icoFoam/cavity" >>> from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory >>> cavityTut=SolutionDirectory(PyFoam.FoamInformation. foamTutorials() +"/incompressible/icoFoam/cavity") >>> cavityCase=cavityTut.cloneCase("./cavity") >>> cavityCase.name 1 2 3 4 5 6 python code 16. Cavity tutorial(2) 2013/9/21 25@ 16 template > cd cavity > cp constant/polyMesh/blockMeshDict constant/polyMesh/blockMeshDict.template > gedit constant/polyMesh/blockMeshDict.template shell command blockMeshDict.tem plate 17. Cavity tutorial(3) 2013/9/21 25@ 17 TemplateFile blockMeshDict $L$,$H$ Dict TemplateFile >>> from PyFoam.Basics.TemplateFile import TemplateFile >>> bMDictTemp=TemplateFile(cavityCase.blockMesh() + ".template",expressionDelimiter="$") >>> bMDictTemp.writeToFile(cavityCase.blockMesh(), {"L":1,"H":2}) 1 2 3 python code 18. Cavity tutorial(4) 2013/9/21 25@ 18 ParsedParameterFile >>> from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile >>> U=ParsedParameterFile(cavityCase.initialDir() + "/U") >>> U['boundaryField']['movingWall']['value'] 'uniform (1 0 0)' >>> from PyFoam.Basics.DataStructures import Vector >>> U['boundaryField']['movingWall']['value'].setUniform( Vector(0.5,0,0)) >>> U.writeFile() 1 2 3 4 5 python code 19. Cavity tutorial(5) 2013/9/21 25@ 19 BasicRunner OpenFOAM >>> from PyFoam.Execution.BasicRunner import BasicRunner >>> import shlex >>> args=shlex.split("blockMesh -case " + cavityCase.name) >>> block=BasicRunner(args,silent=True) >>> summary=block.start() >>> summary {OK: True, casefullname: >>>block.runOK() True 1 2 3 4 5 6 7 python code 20. Cavity tutorial(6) 2013/9/21 25@ 20 LogAnalyzerLineAnalyzer LineAnalyzerlog LogAnalyzerLineAnalyzer log >>> from PyFoam.LogAnalysis.FoamLogAnalyzer import FoamLogAnalyzer >>> from PyFoam.LogAnalysis.ContinuityLineAnalyzer import GeneralContinuityLineAnalyzer >>> analyzer=FoamLogAnalyzer(progress=False) >>> analyzer.addAnalyzer("Continuity", GeneralContinuityLineAnalyzer(doTimelines=True, doFiles=False, singleFile=False, startTime=None, endTime=None)) 1 2 3 4 python code 21. Cavity tutorial(7) 2013/9/21 25@ 21 AnalyzedRunner LogAnalyzer >>> from PyFoam.Execution.AnalyzedRunner import AnalyzedRunner >>> args=shlex.split(icoFoam -case " + cavityCase.name) >>> ico=AnalyzedRunner(analyzer,args,silent=True) >>> summary=ico.start() >>> summary {'OK': True, 'analyzed': {'Continuity': >>>ico.runOK() True 1 2 3 4 5 6 python code 22. Cavity tutorial(8) 2013/9/21 25@ 22 AnalyzedRunner AnalyzedRunnerPyFoam.LogAnalysis. LogAnalyzerApplicationlog FoamLogAnalyzer LineAnalyzer LineAnalyzerfile LineAnalyzer LineAnalyzer( )API 23. Cavity tutorial(9) 2013/9/21 25@ 23 SolutionDirectory.clear() cavityCase >>> cavityCase.clear()1 python code 24. Cavity tutorial(10) 2013/9/21 25@ 24 MeshmovingWall 0.5 [s] p cavityautoRun.py #!/usr/bin/env python import shlex,sys,json import numpy as np autoRun.py 25. Cavity tutorial(11) 2013/9/21 25@ 25 () from PyFoam.RunDictionary.SolutionDirectory import SolutionDirectory from PyFoam.Basics.TemplateFile import TemplateFile from PyFoam.Basics.DataStructures import Vector from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile from PyFoam.Execution.BasicRunner import BasicRunner from PyFoam.Error import error autoRun.py () 26. Cavity tutorial(12) 2013/9/21 25@ 26 () def main(): #cavity case set cavityCase=SolutionDirectory("./cavity") #result list results=[] #blockMeshTemplate bMDictTemp=TemplateFile(cavityCase.blockMesh() + ".template",expressionDelimiter="$") #0/U U=ParsedParameterFile(cavityCase.initialDir() + "/U") autoRun.py () 27. Cavity tutorial(13) 2013/9/21 25@ 27 () for blockL in [1,2]: for blockH in [1,2]: for Uwall in [0.5, 1.0]: #clear case cavityCase.clear() autoRun.py () #edit files bMDictTemp.writeToFile(cavityCase.blockMesh(),{"L":blockL,"H" :blockH}) U['boundaryField']['movingWall']['value'].setUniform(Vector(Uw all,0,0)) U.writeFile() () 28. Cavity tutorial(14) 2013/9/21 25@ 28 () #blockMesh args=shlex.split("blockMesh -case " + cavityCase.name) block=BasicRunner(args,silent=True) block.start() if not block.runOK(): error("There was a problem with blockMesh") sys.exit(1) autoRun.py () () 29. Cavity tutorial(15) 2013/9/21 25@ 29 () #icoFoam args=shlex.split("icoFoam -case " + cavityCase.name) ico=BasicRunner(args,silent=True) ico.start() if not ico.runOK(): error("There was a problem with icoFoam") sys.exit(1) autoRun.py () () 30. Cavity tutorial(16) 2013/9/21 25@ 30 () #post p_latest=ParsedParameterFile(cavityCase.latestDir() + '/p') pmax=np.array(p_latest['internalField'].val).max() #append results results.append({"L":blockL,"H":blockH,"Uwall":Uwall,"pmax":p max}) autoRun.py () () 31. Cavity tutorial(17) 2013/9/21 25@ 31 () #print results print json.dumps(results,indent=4) if __name__ == '__main__': main() autoRun.py () (1) 32. Cavity tutorial(18) 2013/9/21 25@ 32 autoRun.py >python autoRun.py [ { "H": 1, "Uwall": 0.5, "L": 1, "pmax": 2.3693599999999999 }, { "H": 1, "Uwall": 1.0, shell command 33. / 2013/9/21 25@ 33 OpenFOAMPyFoam shellsed OpenMDAO http://en.sourceforge.jp/projects/sfnet_openfoam- extend/downloads/OpenFOAM_Workshops/OFW5_2010_Gothenburg/Advanced_Training /pyFoamAdvanced.pdf