~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to bin/cs_run.py

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
#-------------------------------------------------------------------------------
 
4
 
 
5
# This file is part of Code_Saturne, a general-purpose CFD tool.
 
6
#
 
7
# Copyright (C) 1998-2011 EDF S.A.
 
8
#
 
9
# This program is free software; you can redistribute it and/or modify it under
 
10
# the terms of the GNU General Public License as published by the Free Software
 
11
# Foundation; either version 2 of the License, or (at your option) any later
 
12
# version.
 
13
#
 
14
# This program is distributed in the hope that it will be useful, but WITHOUT
 
15
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
16
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
17
# details.
 
18
#
 
19
# You should have received a copy of the GNU General Public License along with
 
20
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
21
# Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
22
 
 
23
#-------------------------------------------------------------------------------
 
24
 
 
25
"""
 
26
This module describes the script used to run a study/case for Code_Saturne.
 
27
 
 
28
This module defines the following functions:
 
29
- process_cmd_line
 
30
- main
 
31
"""
 
32
 
 
33
#===============================================================================
 
34
# Import required Python modules
 
35
#===============================================================================
 
36
 
 
37
import datetime
 
38
import os, sys, pwd
 
39
import types, string, re, fnmatch
 
40
from optparse import OptionParser
 
41
import ConfigParser
 
42
 
 
43
import cs_exec_environment
 
44
import cs_case_domain
 
45
import cs_case
 
46
 
 
47
#-------------------------------------------------------------------------------
 
48
# Process the command line arguments
 
49
#-------------------------------------------------------------------------------
 
50
 
 
51
def process_cmd_line(argv, pkg):
 
52
    """
 
53
    Process the passed command line arguments.
 
54
    """
 
55
 
 
56
    parser = OptionParser(usage="usage: %prog [options]")
 
57
 
 
58
    parser.add_option("-p", "--param", dest="param", type="string",
 
59
                      metavar="<param>",
 
60
                      help="path or name of the parameters file")
 
61
 
 
62
    parser.add_option("--case", dest="case", type="string",
 
63
                      metavar="<case>",
 
64
                      help="path to the case's directory")
 
65
 
 
66
    parser.add_option("--id", dest="id", type="string",
 
67
                      metavar="<id>",
 
68
                      help="use the given run id")
 
69
 
 
70
    parser.add_option("--suggest-id", dest="suggest_id",
 
71
                      action="store_true",
 
72
                      help="suggest a run id for the next run")
 
73
 
 
74
    parser.add_option("--initialize", dest="initialize",
 
75
                      action="store_true",
 
76
                      help="run the data preparation stage")
 
77
 
 
78
    parser.add_option("--execute", dest="execute",
 
79
                      action="store_true",
 
80
                      help="run the execution stage")
 
81
 
 
82
    parser.add_option("--finalize", dest="finalize",
 
83
                      action="store_true",
 
84
                      help="run the results copy/cleanup stage")
 
85
 
 
86
    parser.set_defaults(suggest_id=False)
 
87
    parser.set_defaults(initialize=False)
 
88
    parser.set_defaults(execute=False)
 
89
    parser.set_defaults(finalize=False)
 
90
    parser.set_defaults(param=None)
 
91
    parser.set_defaults(domain=None)
 
92
    parser.set_defaults(id=None)
 
93
 
 
94
    # Note: we could use args to pass a calculation status file as an argument,
 
95
    # which would allow pursuing the later calculation stages.
 
96
 
 
97
    (options, args) = parser.parse_args(argv)
 
98
 
 
99
    # Try to determine case directory
 
100
 
 
101
    casedir = None
 
102
    param = None
 
103
 
 
104
    if options.param:
 
105
        param = os.path.basename(options.param)
 
106
        if param != options.param:
 
107
            datadir = os.path.split(os.path.realpath(options.param))[0]
 
108
            (casedir, data) = os.path.split(datadir)
 
109
            if data != 'DATA': # inconsistent paramaters location.
 
110
                casedir = None
 
111
 
 
112
    if options.case:
 
113
        casedir = os.path.realpath(options.case)
 
114
 
 
115
    if not casedir:
 
116
        casedir = os.getcwd()
 
117
        while os.path.basename(casedir):
 
118
            data = os.path.join(casedir, 'DATA')
 
119
            src = os.path.join(casedir, 'SRC')
 
120
            if os.path.isdir(data) and os.path.isdir(src):
 
121
                break
 
122
            casedir = os.path.split(casedir)[0]
 
123
 
 
124
    if not casedir:
 
125
        casedir = None
 
126
 
 
127
    # Stages to run (if no filter given, all are done).
 
128
 
 
129
    run_id = options.id
 
130
 
 
131
    suggest_id = options.suggest_id
 
132
 
 
133
    prepare_data = options.initialize
 
134
    run_solver = options.execute
 
135
    save_results = options.finalize
 
136
 
 
137
    if not (prepare_data or run_solver or save_results):
 
138
        prepare_data = True
 
139
        run_solver = True
 
140
        save_results = True
 
141
 
 
142
    return  (casedir, run_id, param, suggest_id,
 
143
             prepare_data, run_solver, save_results)
 
144
 
 
145
#===============================================================================
 
146
# Run the calculation
 
147
#===============================================================================
 
148
 
 
149
def main(argv, pkg):
 
150
    """
 
151
    Main function.
 
152
    """
 
153
 
 
154
    (casedir, run_id, param, suggest_id,
 
155
     prepare_data, run_solver, save_results) = process_cmd_line(argv, pkg)
 
156
 
 
157
    if suggest_id:
 
158
        now = datetime.datetime.now()
 
159
        run_id = now.strftime('%Y%m%d-%H%M')
 
160
        print(run_id)
 
161
        return 0
 
162
 
 
163
    # Use alternate compute (back-end) package if defined
 
164
 
 
165
    config = ConfigParser.ConfigParser()
 
166
    config.read([pkg.get_configfile()])
 
167
 
 
168
    pkg_compute = None
 
169
    if config.has_option('install', 'compute_versions'):
 
170
        compute_versions = config.get('install', 'compute_versions').split(':')
 
171
        if compute_versions[0]:
 
172
            pkg_compute = pkg.get_alternate_version(compute_versions[0])
 
173
 
 
174
    # Values in case and associated domain set from parameters
 
175
 
 
176
    d = cs_case_domain.domain(pkg, package_compute=pkg_compute, param=param)
 
177
 
 
178
    # Now handle case for the corresponding calculation domain(s).
 
179
 
 
180
    c = cs_case.case(pkg,
 
181
                     package_compute=pkg_compute,
 
182
                     case_dir=casedir,
 
183
                     domains=d)
 
184
 
 
185
    # Now run case
 
186
 
 
187
    retval = c.run(mpi_environment=None,
 
188
                   run_id=run_id,
 
189
                   prepare_data=prepare_data,
 
190
                   run_solver=run_solver,
 
191
                   save_results=save_results)
 
192
 
 
193
    return retval
 
194
 
 
195
#-------------------------------------------------------------------------------
 
196
 
 
197
if __name__ == '__main__':
 
198
 
 
199
    # Retrieve package information (name, version, installation dirs, ...)
 
200
    from cs_package import package
 
201
    pkg = package()
 
202
 
 
203
    retval = main(sys.argv[1:], pkg)
 
204
 
 
205
    sys.exit(retval)
 
206
 
 
207
#-------------------------------------------------------------------------------
 
208
# End
 
209
#-------------------------------------------------------------------------------
 
210