File: cs_case_coupling.py

package info (click to toggle)
code-saturne 7.0.2%2Brepack-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 62,868 kB
  • sloc: ansic: 395,271; f90: 100,755; python: 86,746; cpp: 6,227; makefile: 4,247; xml: 2,389; sh: 1,091; javascript: 69
file content (269 lines) | stat: -rw-r--r-- 10,077 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2021 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------
try:
    import ConfigParser  # Python2
    configparser = ConfigParser
except Exception:
    import configparser  # Python3
import os
import os.path
import sys

from code_saturne.cs_case_domain import *
from code_saturne.cs_case import *
from code_saturne import cs_exec_environment
from code_saturne import cs_run_conf
from code_saturne import cs_runcase

#===============================================================================
# Main function for code coupling execution
#===============================================================================

def coupling(package,
             domains,
             casedir,
             dest_dir = None,
             staging_dir = None,
             verbose = True,
             package_compute = None):

    use_saturne = False
    use_syrthes = False
    use_neptune = False
    use_cathare = False
    use_py_code = False

    # Use alternate compute (back-end) package if defined

    config = configparser.ConfigParser()
    config.read(package.get_global_configfile())

    if package_compute == None:
        package_compute = package

    # Initialize code domains
    sat_domains = []
    syr_domains = []
    nep_domains = []
    cat_domains = []
    py_domains = []

    if domains == None:
        raise RunCaseError('No domains defined.')

    for d in domains:

        domain_s = d.get('domain')
        solver_s = d.get('solver').lower()
        script_s = None
        param_s = None

        if (domain_s == None):
            msg = 'Check your coupling definition.\n'
            msg += 'domain key is missing.'
            raise RunCaseError(msg)

        # First, determine parameter file to use for code_saturne
        # or associated modules (ensuring backwards compatibiliy)

        if solver_s in package.config.solver_modules.keys() \
           or solver_s == 'cathare':

            param = None

            script = d.get('script')          # v6.1 and older structure
            if script != None:
                if script[-4:] == '.xml':
                    param = script
            else:
                param = d.get('param')
                if not param:
                    param = d.get('paramfile') # older_name

            s_dir = staging_dir
            if not s_dir:
                s_dir = casedir
            if not s_dir:
                s_dir = os.getcwd()

            if param == None:
                run_conf_path = os.path.join(s_dir,
                                             domain_s,
                                             'DATA',
                                             'run.cfg')
                if os.path.isfile(run_conf_path):
                    run_conf = cs_run_conf.run_conf(run_conf_path)
                    if 'setup' in run_conf.sections:
                        if 'param' in run_conf.sections['setup']:
                            param = run_conf.sections['setup']['param']

            if script and not param: #  v6.1 and older case structure
                runcase_path = os.path.join(s_dir,
                                            domain_s,
                                            'SCRIPTS',
                                            script)
                if os.path.isfile(runcase_path):
                    try:
                        runcase = cs_runcase.runcase(runcase_path)
                        param = runcase.get_parameters()
                    except Exception:
                        err_str = 'Cannot read ' + d.get('solver') \
                                  + ' script: ' + runcase_path
                        raise RunCaseError(err_str)

            # Remark: if param is undefined, the code_saturne domain will
            # default to 'setup.xml' if present.

            d['param'] = param

        # Now build case domain for the different solvers:

        if solver_s in package.config.solver_modules.keys():

            dom = domain(package,
                         package_compute = package_compute,
                         name = domain_s,
                         param = d.get('param'),
                         n_procs_weight = d.get('n_procs_weight'),
                         n_procs_min = d.get('n_procs_min'),
                         n_procs_max = d.get('n_procs_max'))

            if solver_s == 'code_saturne':
                use_saturne = True
                sat_domains.append(dom)
            elif solver_s == 'neptune_cfd':
                use_neptune = True
                nep_domains.append(dom)

        elif solver_s == 'syrthes':

            param_s = d.get('param')
            if param_s == None:
                param_s = d.get('script') # older name

            if (param_s == None):
                msg = 'Check your coupling definition.\n'
                msg += 'parameters file selection is missing for domain: '
                msg += domain_s + '.\n'
                raise RunCaseError(msg)

            try:
                dom = syrthes_domain(package,
                                     cmd_line = d.get('opt'),
                                     name = domain_s,
                                     param = d.get('param'),
                                     n_procs_weight = d.get('n_procs_weight'),
                                     n_procs_min = d.get('n_procs_min'),
                                     n_procs_max = d.get('n_procs_max'),
                                     verbose = verbose)

            except Exception:
                err_str = 'Cannot create SYRTHES domain. Opt = ' + d.get('opt') + '\n'
                err_str += ' domain = ' + domain_s + '\n'
                err_str += ' n_procs_weight = ' + str(d.get('n_procs_weight')) + '\n'
                raise RunCaseError(err_str)

            use_syrthes = True
            syr_domains.append(dom)

        elif solver_s == 'cathare':

            # Current version using Cathare2: the cathare case is converted to a
            # .so library which is opened and launched by a neptune_cfd executable

            dom = cathare_domain(package,
                                 package_compute = package_compute,
                                 name = domain_s,
                                 param = d.get('param'),
                                 n_procs_weight = None,
                                 n_procs_min = 1,
                                 n_procs_max = 1,
                                 cathare_case_file = d.get('cathare_case_file'),
                                 neptune_cfd_dom = d.get('neptune_cfd_domain'))

            use_cathare = True
            cat_domains.append(dom)

        elif solver_s == 'python_code':

            script_s = d.get('script')
            if (script_s == None):
                msg = 'Check your coupling definition.\n'
                msg += 'Python script file selection is missing for domain: '
                msg += domain_s + '.\n'
                raise RunCaseError(msg)

            # Generic Code_Saturne/Python Script coupling
            # The python script can contain any MPI compatible code or supervisor

            try:
                dom = python_domain(package,
                                    name = domain_s,
                                    cmd_line = d.get('command_line'),
                                    script_name = script_s)

            except Exception:
                err_str = 'Cannot create Python code domain.\n'
                err_str += ' domain = ' + domain_s + '\n'
                err_str += ' script = ' + str(d.get('script')) + '\n'
                raise RunCaseError(err_str)

            use_py_code = True
            py_domains.append(dom)

        else:
            err_str = 'Unknown code type : ' + d.get('solver') + '.\n'
            raise RunCaseError(err_str)

    # Now handle case for the corresponding calculation domain(s).

    c = case(package,
             package_compute = package_compute,
             case_dir = casedir,
             dest_dir = dest_dir,
             staging_dir = staging_dir,
             domains = sat_domains + nep_domains + cat_domains,
             syr_domains = syr_domains,
             py_domains = py_domains)

    if verbose:
        msg = ' Coupling execution between: \n'
        if use_saturne == True:
            msg += '   o code_saturne [' + str(len(sat_domains)) + ' domain(s)];\n'
        if use_syrthes == True:
            msg += '   o SYRTHES      [' + str(len(syr_domains)) + ' domain(s)];\n'
        if use_neptune == True:
            msg += '   o neptune_cfd  [' + str(len(nep_domains)) + ' domain(s)];\n'
        if use_cathare == True:
            msg += '   o CATHARE2     [' + str(len(cat_domains)) + ' domain(s)];\n'
        if use_py_code == True:
            msg += '   o Python Script  [' + str(len(py_domains)) + ' domain(s)];\n'
        sys.stdout.write(msg+'\n')

    return c

#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------