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 | from __future__ import print_function |
---|
11 | import sys |
---|
12 | import os |
---|
13 | from puq import NormalParameter, UniformParameter, Sweep, RapptureHost, Smolyak |
---|
14 | import numpy as np |
---|
15 | from puq.jpickle import unpickle |
---|
16 | |
---|
17 | # Redirect stdout and stderr to files for debugging. |
---|
18 | sys.stdout = open("uq_debug.out", 'w') |
---|
19 | sys.stderr = open("uq_debug.err", 'w') |
---|
20 | |
---|
21 | print(sys.argv) |
---|
22 | pid, varlist, uq_type, args = sys.argv[1:] |
---|
23 | |
---|
24 | dfile = "driver%s.xml" % pid |
---|
25 | cvsname = "params%s.csv" % pid |
---|
26 | hname = "puq_%s.hdf5" % pid |
---|
27 | |
---|
28 | varlist = unpickle(varlist) |
---|
29 | print("varlist=", varlist) |
---|
30 | |
---|
31 | v = {} |
---|
32 | units = {} |
---|
33 | for 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) |
---|
49 | if uq_type == "smolyak": |
---|
50 | uq = Smolyak(v.values(), args) |
---|
51 | else: |
---|
52 | print("ERROR: Unknown UQ type: %s" % uq_type) |
---|
53 | os.chdir('..') |
---|
54 | sys.exit(1) |
---|
55 | |
---|
56 | # save parameter values to CSV file |
---|
57 | with 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. |
---|
64 | host = RapptureHost('puq', dfile) |
---|
65 | sw = Sweep(uq, host, None) |
---|
66 | host.psweep = sw.psweep |
---|
67 | sw.run(hname, overwrite=True) |
---|
68 | |
---|
69 | print("Finished with get_params.py\n") |
---|