source: branches/uq/puq/get_params.py @ 6226

Last change on this file since 6226 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
RevLine 
[5029]1#!/usr/bin/env python
[5168]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
[5191]10from __future__ import print_function
[5148]11import sys
[5168]12import os
[5148]13from puq import NormalParameter, UniformParameter, Sweep, RapptureHost, Smolyak
[5029]14import numpy as np
15from puq.jpickle import unpickle
16
[5168]17# Redirect stdout and stderr to files for debugging.
18sys.stdout = open("uq_debug.out", 'w')
19sys.stderr = open("uq_debug.err", 'w')
[5148]20
[5191]21print(sys.argv)
[5168]22pid, varlist, uq_type, args = sys.argv[1:]
[5148]23
[5168]24dfile = "driver%s.xml" % pid
[5029]25cvsname = "params%s.csv" % pid
26hname = "puq_%s.hdf5" % pid
27
28varlist = unpickle(varlist)
[5191]29print("varlist=", varlist)
[5029]30
31v = {}
[5191]32units = {}
[5029]33for p in varlist:
[5191]34    name, _units, dist = p
[5029]35    name = str(name)
[5191]36    units[name] = str(_units)
[5029]37    if dist[0] == u'gaussian':
[5182]38        try:
39            kwargs = dist[3]
40        except:
41            kwargs = {}
42        v[name] = NormalParameter(name, name, mean=dist[1], dev=dist[2], **kwargs)
[5191]43        print(v[name])
[5029]44    elif dist[0] == u'uniform':
45        v[name] = UniformParameter(name, name, min=dist[1], max=dist[2])
46    else:
[5191]47        print("ERROR: Unknown distribution type: %s" % dist[0])
[5029]48        sys.exit(1)
49if uq_type == "smolyak":
50    uq = Smolyak(v.values(), args)
51else:
[5191]52    print("ERROR: Unknown UQ type: %s" % uq_type)
[5029]53    os.chdir('..')
54    sys.exit(1)
55
[5168]56# save parameter values to CSV file
[5191]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)
[5029]61
[5168]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.
[5169]64host = RapptureHost('puq', dfile)
65sw = Sweep(uq, host, None)
66host.psweep = sw.psweep
[5029]67sw.run(hname, overwrite=True)
[5168]68
[5191]69print("Finished with get_params.py\n")
Note: See TracBrowser for help on using the repository browser.