source: branches/1.5/puq/get_params.py @ 6036

Last change on this file since 6036 was 5191, checked in by mmh, 8 years ago

append units to uq parameters sent to submit

  • Property svn:executable set to *
File size: 2.1 KB
Line 
1#!/usr/bin/env python
2"""
3This script takes some uq parameters from the command line
4and starts up a PUQ session using them.  The RapptureHost()
5class will not actually execute any jobs.  Job parameters
6are instead saved to a CSV file named params[pid].csv.
7PUQ status is saved into an hdf5 file, as usual.
8"""
9
10from __future__ import print_function
11import sys
12import os
13from puq import NormalParameter, UniformParameter, Sweep, RapptureHost, Smolyak
14import numpy as np
15from puq.jpickle import unpickle
16
17# Redirect stdout and stderr to files for debugging.
18sys.stdout = open("uq_debug.out", 'w')
19sys.stderr = open("uq_debug.err", 'w')
20
21print(sys.argv)
22pid, varlist, uq_type, args = sys.argv[1:]
23
24dfile = "driver%s.xml" % pid
25cvsname = "params%s.csv" % pid
26hname = "puq_%s.hdf5" % pid
27
28varlist = unpickle(varlist)
29print("varlist=", varlist)
30
31v = {}
32units = {}
33for p in varlist:
34    name, _units, dist = p
35    name = str(name)
36    units[name] = str(_units)
37    if dist[0] == u'gaussian':
38        try:
39            kwargs = dist[3]
40        except:
41            kwargs = {}
42        v[name] = NormalParameter(name, name, mean=dist[1], dev=dist[2], **kwargs)
43        print(v[name])
44    elif dist[0] == u'uniform':
45        v[name] = UniformParameter(name, name, min=dist[1], max=dist[2])
46    else:
47        print("ERROR: Unknown distribution type: %s" % dist[0])
48        sys.exit(1)
49if uq_type == "smolyak":
50    uq = Smolyak(v.values(), args)
51else:
52    print("ERROR: Unknown UQ type: %s" % uq_type)
53    os.chdir('..')
54    sys.exit(1)
55
56# save parameter values to CSV file
57with open(cvsname, 'w') as f:
58    print(','.join(['@@'+str(p.name) for p in uq.params]), file=f)
59    for i in range(len(uq.params[0].values)):
60        print(','.join(['%.16g%s' % (p.values[i], units[p.name]) for p in uq.params]), file=f)
61
62# This just saves PUQ state into HDF5 file, so later we can have PUQ analyze
63# the results and it knows about the input parameters, UQ method, etc.
64host = RapptureHost('puq', dfile)
65sw = Sweep(uq, host, None)
66host.psweep = sw.psweep
67sw.run(hname, overwrite=True)
68
69print("Finished with get_params.py\n")
Note: See TracBrowser for help on using the repository browser.