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