-
Notifications
You must be signed in to change notification settings - Fork 240
/
om.py
588 lines (500 loc) · 23.6 KB
/
om.py
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
"""
A console script wrapper for multiple openmdao functions.
"""
import sys
import os
import argparse
from openmdao import __version__ as version
try:
import pkg_resources
except ImportError:
pkg_resources = None
import openmdao.utils.hooks as hooks
from openmdao.visualization.n2_viewer.n2_viewer import n2
from openmdao.visualization.connection_viewer.viewconns import view_connections
from openmdao.visualization.scaling_viewer.scaling_report import _scaling_setup_parser, \
_scaling_cmd
from openmdao.visualization.dyn_shape_plot import _view_dyn_shapes_setup_parser, \
_view_dyn_shapes_cmd
try:
import bokeh
from openmdao.visualization.meta_model_viewer.meta_model_visualization import view_metamodel
except ImportError:
bokeh = None
from openmdao.components.meta_model_semi_structured_comp import MetaModelSemiStructuredComp
from openmdao.components.meta_model_structured_comp import MetaModelStructuredComp
from openmdao.components.meta_model_unstructured_comp import MetaModelUnStructuredComp
from openmdao.core.component import Component
from openmdao.devtools.debug import config_summary, tree
from openmdao.devtools.itrace import _itrace_exec, _itrace_setup_parser
from openmdao.devtools.iprofile_app.iprofile_app import _iprof_exec, _iprof_setup_parser
from openmdao.devtools.iprofile import _iprof_totals_exec, _iprof_totals_setup_parser
from openmdao.devtools.iprof_mem import _mem_prof_exec, _mem_prof_setup_parser, \
_mempost_exec, _mempost_setup_parser
from openmdao.devtools.iprof_utils import _Options
from openmdao.error_checking.check_config import _check_config_cmd, _check_config_setup_parser
from openmdao.utils.mpi import MPI
from openmdao.utils.find_cite import print_citations
from openmdao.utils.code_utils import _calltree_setup_parser, _calltree_exec
from openmdao.utils.coloring import _total_coloring_setup_parser, _total_coloring_cmd, \
_partial_coloring_setup_parser, _partial_coloring_cmd, \
_view_coloring_setup_parser, _view_coloring_exec
from openmdao.utils.scaffold import _scaffold_setup_parser, _scaffold_exec
from openmdao.utils.file_utils import _load_and_exec, _to_filename
from openmdao.utils.entry_points import _list_installed_setup_parser, _list_installed_cmd, \
split_ep, _compute_entry_points_setup_parser, _compute_entry_points_exec, \
_find_plugins_setup_parser, _find_plugins_exec
from openmdao.utils.general_utils import ignore_errors
from openmdao.utils.om_warnings import warn_deprecation
def _n2_setup_parser(parser):
"""
Set up the openmdao subparser for the 'openmdao n2' command.
Parameters
----------
parser : argparse subparser
The parser we're adding options to.
"""
parser.add_argument('file', nargs=1,
help='Python script or recording containing the model. '
'If metadata from a parallel run was recorded in a separate file, '
'specify both database filenames delimited with a comma.')
parser.add_argument('-o', default='n2.html', action='store', dest='outfile',
help='html output file.')
parser.add_argument('--no_browser', action='store_true', dest='no_browser',
help="don't display in a browser.")
parser.add_argument('--embed', action='store_true', dest='embeddable',
help="create embeddable version.")
parser.add_argument('--title', default=None,
action='store', dest='title', help='diagram title.')
parser.add_argument('--use_declare_partial_info', action='store_true',
dest='use_declare_partial_info',
help="ignored, now always true.")
def _n2_cmd(options, user_args):
"""
Process command line args and call n2 on the specified file.
Parameters
----------
options : argparse Namespace
Command line options.
user_args : list of str
Command line options after '--' (if any). Passed to user script.
"""
filename = _to_filename(options.file[0])
if filename.endswith('.py'):
# the file is a python script, run as a post_setup hook
def _noraise(prob):
prob.model._raise_connection_errors = False
if options.use_declare_partial_info:
warn_deprecation("'--use_declare_partial_info' is now the"
" default and the option is ignored.")
def _viewmod(prob):
n2(prob, outfile=options.outfile, show_browser=not options.no_browser,
title=options.title, embeddable=options.embeddable)
exit() # could make this command line selectable later
hooks._register_hook('setup', 'Problem', pre=_noraise)
hooks._register_hook('final_setup', 'Problem', post=_viewmod)
ignore_errors(True)
_load_and_exec(options.file[0], user_args)
else:
# assume the file is a recording, run standalone
n2(filename, outfile=options.outfile, title=options.title,
show_browser=not options.no_browser, embeddable=options.embeddable)
def _view_connections_setup_parser(parser):
"""
Set up the openmdao subparser for the 'openmdao view_connections' command.
Parameters
----------
parser : argparse subparser
The parser we're adding options to.
"""
parser.add_argument('file', nargs=1, help='Python file containing the model.')
parser.add_argument('-o', default='connections.html', action='store', dest='outfile',
help='html output file.')
parser.add_argument('-t', '--title', action='store', dest='title',
help='title of web page.')
parser.add_argument('--no_browser', action='store_true', dest='no_browser',
help="don't display in a browser.")
parser.add_argument('-v', '--show_values', action='store_true', dest='show_values',
help="Display values.")
parser.add_argument('-p', '--problem', action='store', dest='problem', help='Problem name')
def _view_connections_cmd(options, user_args):
"""
Return the post_setup hook function for 'openmdao view_connections'.
Parameters
----------
options : argparse Namespace
Command line options.
user_args : list of str
Args to be passed to the user script.
"""
def _viewconns(prob):
if options.title:
title = options.title
else:
title = "Connections for %s" % os.path.basename(options.file[0])
view_connections(prob, outfile=options.outfile, show_browser=not options.no_browser,
show_values=options.show_values, title=title)
exit()
# register the hook
if options.show_values:
funcname = 'final_setup'
else:
funcname = 'setup'
hooks._register_hook(funcname, class_name='Problem', inst_id=options.problem, post=_viewconns)
ignore_errors(True)
_load_and_exec(options.file[0], user_args)
def _meta_model_parser(parser):
"""
Set up the openmdao subparser for the 'openmdao meta_model' command.
Parameters
----------
parser : argparse subparser
The parser we're adding options to.
"""
parser.add_argument('file', nargs=1, help='Python file containing the model.')
parser.add_argument('-m', '--metamodel_pathname', action='store', dest='pathname',
help='pathname of the metamodel component.')
parser.add_argument('-r', '--resolution', default=50, type=int,
action='store', dest='resolution',
help='Number of points to create contour grid')
parser.add_argument('-p', '--port_number', default=5007, action='store', dest='port_number',
help='Port number to open viewer')
parser.add_argument('--no_browser', action='store_false', dest='browser',
help='Bokeh server will start server without browser')
def _meta_model_cmd(options, user_args):
"""
Return the post_setup hook function for 'openmdao meta_model'.
Parameters
----------
options : argparse Namespace
Command line options.
user_args : list of str
Args to be passed to the user script.
"""
def _view_metamodel(prob):
if bokeh is None:
print("bokeh must be installed to view a MetaModel. Use the command:\n",
" pip install bokeh")
exit()
hooks._unregister_hook('final_setup', 'Problem')
mm_types = (MetaModelStructuredComp, MetaModelUnStructuredComp,
MetaModelSemiStructuredComp)
pathname = options.pathname
port_number = options.port_number
resolution = options.resolution
browser = options.browser
if pathname:
comp = prob.model._get_subsystem(pathname)
if comp and isinstance(comp, mm_types):
view_metamodel(comp, resolution, port_number, browser)
exit()
else:
comp = None
metamodels = {mm.pathname: mm for
mm in prob.model.system_iter(include_self=True, typ=mm_types)}
mm_names = list(metamodels.keys())
mm_count = len(mm_names)
def _mm_list(mm_names, options):
for mm in mm_names:
print("openmdao view_mm -m {} {}".format(mm, options.file[0]))
if mm_count == 0:
print("No Metamodel components found in model.")
elif mm_count == 1 and not pathname:
comp = metamodels[mm_names[0]]
view_metamodel(comp, resolution, port_number, browser)
else:
if not pathname:
print("\nMetamodel not specified. Try one of the following:\n")
_mm_list(mm_names, options)
elif not comp:
if len(mm_names) > 1:
print("\nMetamodel '{}' not found. Try one of the "
"following:\n".format(pathname))
else:
print("\nMetamodel '{}' not found. Try the "
"following:\n".format(pathname))
_mm_list(mm_names, options)
else:
if len(mm_names) > 1:
print("\n'{}' is not a Metamodel. Try one of the "
"following:\n".format(pathname))
else:
print("\n'{}' is not a Metamodel. Try the following:\n".format(pathname))
_mm_list(mm_names, options)
exit()
hooks._register_hook('final_setup', 'Problem', post=_view_metamodel)
_load_and_exec(options.file[0], user_args)
def _config_summary_setup_parser(parser):
"""
Set up the openmdao subparser for the 'openmdao summary' command.
Parameters
----------
parser : argparse subparser
The parser we're adding options to.
"""
parser.add_argument('file', nargs=1, help='Python file containing the model.')
def _config_summary_cmd(options, user_args):
"""
Return the post_setup hook function for 'openmdao summary'.
Parameters
----------
options : argparse Namespace
Command line options.
user_args : list of str
Args to be passed to the user script.
"""
def summary(prob):
config_summary(prob)
sys.exit(0)
hooks._register_hook('final_setup', 'Problem', post=summary)
ignore_errors(True)
_load_and_exec(options.file[0], user_args)
def _tree_setup_parser(parser):
"""
Set up the openmdao subparser for the 'openmdao tree' command.
Parameters
----------
parser : argparse subparser
The parser we're adding options to.
"""
parser.add_argument('file', nargs=1, help='Python file containing the model.')
parser.add_argument('-o', default=None, action='store', dest='outfile',
help='Output file name. By default, output goes to stdout.')
parser.add_argument('-c', '--colors', action='store_true', dest='show_colors',
help="Display colors if the terminal supports it. Requires 'colorama' "
"python package. Use 'pip install colorama' to install it.")
parser.add_argument('-d', '--depth', action='store', type=int, dest='depth',
default=0, help="Max depth of tree to display.")
parser.add_argument('-a', '--attr', action='append', default=[], dest='attrs',
help='Add an attribute to search for in tree systems.')
parser.add_argument('-v', '--var', action='append', default=[], dest='vecvars',
help='Add a variable to search for in vectors of tree components. '
'Use component relative names.')
parser.add_argument('-r', '--rank', action='store', type=int, dest='rank',
default=0, help="Display the tree on this rank (if MPI is active).")
parser.add_argument('-p', '--problem', action='store', dest='problem', help='Problem name')
parser.add_argument('-s', '--sizes', action='store_true', dest='show_sizes',
help="Display input and output sizes.")
parser.add_argument('--approx', action='store_true', dest='show_approx',
help="Show which components compute approximations.")
def _get_tree_filter(attrs, vecvars):
"""
Pull attributes and input/output vector variables out of a tree System.
Parameters
----------
attrs : list of str
Names of attributes (may contain dots).
vecvars : list of str
Names of variables contained in the input or output vectors. Use component relative
names.
Returns
-------
function
A function that takes a System and returns a list of name value pairs.
"""
def _finder(system):
found = []
for attr in attrs:
parts = attr.split('.') # allow attrs with dots
try:
obj = system
for p in parts:
obj = getattr(obj, p)
found.append((attr, obj))
except AttributeError:
pass
if isinstance(system, Component):
for var in vecvars:
if var in system._var_rel2meta:
if var in system._outputs:
found.append((var, system._outputs[var]))
elif var in system._inputs:
found.append((var, system._inputs[var]))
return found
return _finder
def _tree_cmd(options, user_args):
"""
Return the post_setup hook function for 'openmdao tree'.
Parameters
----------
options : argparse Namespace
Command line options.
user_args : list of str
Args to be passed to the user script.
"""
if options.outfile is None:
out = sys.stdout
else:
out = open(options.outfile, 'w')
if options.attrs or options.vecvars:
filt = _get_tree_filter(options.attrs, options.vecvars)
else:
filt = None
def _tree(prob):
tree(prob, show_colors=options.show_colors, show_sizes=options.show_sizes,
show_approx=options.show_approx, filter=filt, max_depth=options.depth,
rank=options.rank, stream=out)
exit()
# register the hook
if options.vecvars or options.show_sizes or options.show_approx:
funcname = 'final_setup'
else:
funcname = 'setup'
hooks._register_hook(funcname, class_name='Problem', inst_id=options.problem, post=_tree)
ignore_errors(True)
_load_and_exec(options.file[0], user_args)
def _cite_setup_parser(parser):
"""
Set up the openmdao subparser for the 'openmdao cite' command.
Parameters
----------
parser : argparse subparser
The parser we're adding options to.
"""
parser.add_argument('file', nargs=1, help='Python file containing the model.')
parser.add_argument('-o', default=None, action='store', dest='outfile',
help='Name of output file. By default, output goes to stdout.')
parser.add_argument('-c', '--class', action='append', default=[], dest='classes',
help='Find citation for this class.')
def _cite_cmd(options, user_args):
"""
Run the `openmdao cite` command.
Parameters
----------
options : argparse Namespace
Command line options.
user_args : list of str
Args to be passed to the user script.
"""
if options.outfile is None:
out = sys.stdout
else:
out = open(options.outfile, 'w')
if not options.classes:
options.classes = None
def _cite(prob):
if not MPI or MPI.COMM_WORLD.rank == 0:
print_citations(prob, classes=options.classes, out_stream=out)
exit()
hooks._register_hook('setup', 'Problem', post=_cite)
ignore_errors(True)
_load_and_exec(options.file[0], user_args)
# this dict should contain names mapped to tuples of the form:
# (setup_parser_func, executor, description)
_command_map = {
'call_tree': (_calltree_setup_parser, _calltree_exec,
"Display the call tree for the specified class method and all 'self' class "
"methods it calls."),
'check': (_check_config_setup_parser, _check_config_cmd,
'Perform a number of configuration checks on the problem.'),
'cite': (_cite_setup_parser, _cite_cmd, 'Print citations referenced by the problem.'),
'compute_entry_points': (_compute_entry_points_setup_parser, _compute_entry_points_exec,
'Compute entry point declarations to add to the setup.py file.'),
'find_plugins': (_find_plugins_setup_parser, _find_plugins_exec,
'Find openmdao plugins on github.'),
'iprof': (_iprof_setup_parser, _iprof_exec,
'Profile calls to particular object instances.'),
'iprof_totals': (_iprof_totals_setup_parser, _iprof_totals_exec,
'Generate total timings of calls to particular object instances.'),
'list_installed': (_list_installed_setup_parser, _list_installed_cmd,
'List installed types recognized by OpenMDAO.'),
'mem': (_mem_prof_setup_parser, _mem_prof_exec,
'Profile memory used by OpenMDAO related functions.'),
'mempost': (_mempost_setup_parser, _mempost_exec, 'Post-process memory profile output.'),
'n2': (_n2_setup_parser, _n2_cmd, 'Display an interactive N2 diagram of the problem.'),
'partial_coloring': (_partial_coloring_setup_parser, _partial_coloring_cmd,
'Compute coloring(s) for specified partial jacobians.'),
'scaffold': (_scaffold_setup_parser, _scaffold_exec,
'Generate a simple scaffold for a component.'),
'summary': (_config_summary_setup_parser, _config_summary_cmd,
'Print a short top-level summary of the problem.'),
'total_coloring': (_total_coloring_setup_parser, _total_coloring_cmd,
'Compute a coloring for the total jacobian.'),
'trace': (_itrace_setup_parser, _itrace_exec, 'Dump trace output.'),
'tree': (_tree_setup_parser, _tree_cmd, 'Print the system tree.'),
'view_coloring': (_view_coloring_setup_parser, _view_coloring_exec, 'View a colored jacobian.'),
'view_connections': (_view_connections_setup_parser, _view_connections_cmd,
'View connections showing values and source/target units.'),
'view_dyn_shapes': (_view_dyn_shapes_setup_parser, _view_dyn_shapes_cmd,
'View the dynamic shape dependency graph.'),
'view_mm': (_meta_model_parser, _meta_model_cmd, "View a metamodel."),
'scaling': (_scaling_setup_parser, _scaling_cmd, 'View driver scaling report.'),
}
def openmdao_cmd():
"""
Run an 'openmdao' sub-command or list help info for 'openmdao' command or sub-commands.
"""
# pre-parse sys.argv to split between before and after '--'
if '--' in sys.argv:
idx = sys.argv.index('--')
sys_args = sys.argv[:idx]
user_args = sys.argv[idx + 1:]
sys.argv[:] = sys_args
else:
user_args = []
parser = argparse.ArgumentParser(description='OpenMDAO Command Line Tools',
usage='openmdao [-h] [--version] command [command_options] '
'filename',
epilog='Use -h after any sub-command for sub-command help.'
' If using a tool on a script that takes its own command line'
' arguments, place those arguments after a "--". For example:'
' openmdao n2 -o foo.html myscript.py -- -x --myarg=bar')
parser.add_argument('--version', action='version', version=version)
# setting 'dest' here will populate the Namespace with the active subparser name
subs = parser.add_subparsers(title='Tools', metavar='', dest="subparser_name")
for p, (parser_setup_func, executor, help_str) in sorted(_command_map.items()):
subp = subs.add_parser(p, help=help_str)
parser_setup_func(subp)
subp.set_defaults(executor=executor)
if pkg_resources is None:
print("\npkg_resources was not found, so no plugin entry points can be loaded.\n")
else:
# now add any plugin openmdao commands
epdict = {}
for ep in pkg_resources.iter_entry_points(group='openmdao_command'):
cmd, module, target = split_ep(ep)
# don't let plugins override the builtin commands
if cmd in _command_map:
raise RuntimeError("openmdao plugin command '{}' defined in {} conflicts with "
"builtin command '{}'.".format(cmd, module, cmd))
elif cmd in epdict:
raise RuntimeError("openmdao plugin command '{}' defined in {} conflicts with a "
"another plugin command defined in {}."
.format(cmd, module, epdict[cmd][1]))
epdict[cmd] = (ep, module)
# sort commands by module and then by command name so commands from plugins will
# be grouped together.
for cmd, (ep, module) in sorted(epdict.items(), key=lambda x: x[1][1] + x[0]):
func = ep.load()
parser_setup_func, executor, help_str = func()
pkg = module.split('.', 1)[0]
subp = subs.add_parser(cmd, help='(%s plugin) ' % pkg + help_str)
parser_setup_func(subp)
subp.set_defaults(executor=executor)
# handle case where someone just runs `openmdao <script> [dashed-args]`
args = [a for a in sys.argv[1:] if not a.startswith('-')]
cmdargs = [a for a in sys.argv[1:] if a not in ('-h', '--version')]
if not set(args).intersection(subs.choices) and len(args) == 1 and os.path.isfile(cmdargs[0]):
_load_and_exec(args[0], user_args)
else:
hooks.use_hooks = True
# we do a parse_known_args here instead of parse_args so that we can associate errors with
# the correct subparser. Otherwise we would just get a top level error message without any
# sub-command usage info.
options, unknown = parser.parse_known_args()
if unknown:
msg = 'unrecognized arguments: ' + ', '.join(unknown)
try:
sub = subs.choices[options.subparser_name]
except KeyError:
parser.error(msg)
else:
print(sub.format_usage(), file=sys.stderr)
print(msg, file=sys.stderr)
parser.exit(2)
if hasattr(options, 'executor'):
options.executor(options, user_args)
else:
print("\nNothing to do.")
if __name__ == '__main__':
openmdao_cmd()