FIX: Logic requiring pngmath was broken
[freefoam.git] / bin / freefoam-para.py.in
blobcf01472ac2bfc30324a3609137dce954d78f93b3
1 #!@PYTHON_EXECUTABLE@
2 #-------------------------------------------------------------------------------
3 # ______ _ ____ __ __
4 # | ____| _| |_ / __ \ /\ | \/ |
5 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
6 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
7 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
8 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
10 # FreeFOAM: The Cross-Platform CFD Toolkit
12 # Copyright (C) 2008-2011 Michael Wild <themiwi@users.sf.net>
13 # Gerber van der Graaf <gerber_graaf@users.sf.net>
14 #-------------------------------------------------------------------------------
15 # License
16 # This file is part of FreeFOAM.
18 # FreeFOAM is free software; you can redistribute it and/or modify it
19 # under the terms of the GNU General Public License as published by the
20 # Free Software Foundation; either version 2 of the License, or (at your
21 # option) any later version.
23 # FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
24 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 # for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with FreeFOAM; if not, write to the Free Software Foundation,
30 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 # Script
33 # freefoam-para
35 # Description
36 # Start ParaView with the OpenFOAM plugin
38 #------------------------------------------------------------------------------
40 """Usage: freefoam@PY_SCRIPT_SUFFIX@ para [-case <dir>] [-touch] [-h, -help]
42 Visualize the case <dir> with ParaView.
44 Options
45 -------
46 -case <dir> Specify alternative case or processor directory
47 -touch Only create the .foam file
49 """
51 # want to be future proof
52 from FreeFOAM.compat import *
54 import os
55 import os.path
56 import sys
57 import glob
58 import subprocess
60 # parse options
61 case = '.'
62 touchOnly = False
63 args = sys.argv[1:]
64 while len(args) > 0:
65 a = args[0]
66 if a == '-case':
67 if len(args) < 2:
68 sys.stderr.write('Error: The -case option requires an argument\n')
69 sys.exit(1)
70 case = args[1]
71 del args[0:2]
72 elif a == '-touch':
73 touchOnly = True
74 del args[0]
75 else:
76 sys.stderr.write('Error: unknown option/argument "%s"\n'%a)
77 sys.stderr.write(__doc__+'\n')
78 sys.exit(1)
80 case = os.path.abspath(case)
82 # get a sensible caseName
83 caseName = os.path.basename(case)
84 caseFile = "%s.foam"%caseName
86 if touchOnly:
87 open(caseFile, 'at')
88 echo('created "%s"'%os.path.relpath(caseFile, os.getcwd()))
89 sys.exit(0)
91 # parent directory for normal or parallel results
92 parentDir = case
93 if caseName[:9] == 'processor':
94 parentDir = os.path.dirname(case)
96 # check existence of essential files
97 for check in 'controlDict fvSchemes fvSolution'.split():
98 f = os.path.join(parentDir, 'system', check)
99 if not os.path.isfile(f):
100 sys.stderr.write('Error: File does not exist "%s"\n'%f)
101 sys.exit(1)
103 try:
104 if os.path.exists(caseFile):
105 doDelete = False
106 else:
107 # only create/remove caseFile if it didn't already exist
108 doDelete = True
109 open(caseFile, 'at')
110 echo('created temporary "%s"'%caseFile)
112 # Try to find ParaView:
113 paraview = None
114 if 'FREEFOAM_PARAVIEW_APP' in os.environ:
115 # Using a environment variable the user can set...
116 paraview=os.environ['FREEFOAM_PARAVIEW_APP']
117 elif os.path.exists('@FOAM_PARAVIEW3_APP@'):
118 # Using the CMake configure value...
119 paraview="@FOAM_PARAVIEW3_APP@"
121 if not paraview and os.uname()[0] == 'Darwin':
122 # On Darwin it might also be an app-bundle
123 pv_dirs = []
124 if 'FREEFOAM_PARAVIEW_APP' in os.environ:
125 pv_dirs.append(os.environ['FREEFOAM_PARAVIEW_APP'])
126 pv_dirs.extend('/Applications ~/Applications'.split())
127 for d in pv_dirs:
128 d = os.path.expanduser(d)
129 for n in reversed(glob.glob(os.path.join(d, '[Pp]ara[Vv]iew*.app'))):
130 tmp = os.path.join(n, 'Contents', 'MacOS', 'paraview')
131 if os.path.isfile(tmp):
132 paraview = tmp
133 break
135 if not paraview:
136 # Otherwise notify the user
137 sys.stderr.write('Error: Could not find ParaView 3. Either compile it or point\n' +
138 'FREEFOAM_PARAVIEW_APP to the full path of the paraview application.\n')
139 sys.exit(1)
140 # We want to do nice exit when running paraview to give paraview opportunity
141 # to clean up
142 if 'FOAM_ABORT' in os.environ:
143 del os.environ['FOAM_ABORT']
144 sys.exit(subprocess.call([paraview, '--data=%s'%caseFile]))
145 finally:
146 if doDelete:
147 os.remove(caseFile)
149 # ------------------- vim: set sw=3 sts=3 ft=python et: ------------ end-of-file