/
easyvvuq_gauss_tutorial.py
91 lines (76 loc) · 2.38 KB
/
easyvvuq_gauss_tutorial.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
import os
import easyvvuq as uq
import chaospy as cp
# 0. Setup some variables describing app to be run
#
# gauss.py is in current directory and takes one input file
# and writes to 'output.csv'.
cwd = os.getcwd()
input_filename = 'gauss_in.json'
cmd = f"{cwd}/gauss.py {input_filename}"
out_file = "output.csv"
# Template input to substitute values into for each run
template = f"{cwd}/gauss.template"
# 1. Create campaign
my_campaign = uq.Campaign(name='gauss', work_dir=".")
# 2. Parameter space definition
params = {
"sigma": {
"type": "float",
"min": 0.0,
"max": 100000.0,
"default": 0.25
},
"mu": {
"type": "float",
"min": 0.0,
"max": 100000.0,
"default": 1
},
"num_steps": {
"type": "integer",
"min": 0,
"max": 100000,
"default": 10
},
"out_file": {
"type": "string",
"default": "output.csv"
}
}
# 3. Wrap Application
# - Define a new application (we'll call it 'gauss'), and the encoding/decoding elements it needs
# - Also requires a collation element - his will be responsible for aggregating the results
encoder = uq.encoders.GenericEncoder(template_fname=template,
target_filename=input_filename)
decoder = uq.decoders.SimpleCSV(
target_filename=out_file,
output_columns=['Step', 'Value'])
my_campaign.add_app(name="gauss",
params=params,
encoder=encoder,
decoder=decoder)
# 4. Specify Sampler
# - vary the `mu` parameter only
vary = {
"mu": cp.Uniform(1.0, 100.0),
}
my_sampler = uq.sampling.RandomSampler(vary=vary)
my_campaign.set_sampler(my_sampler)
# 5. Get run parameters
my_campaign.draw_samples(num_samples=3,
replicas=5)
# 6. Create run input directories
my_campaign.populate_runs_dir()
print(my_campaign)
# 7. Run Application
# - gauss is executed for each sample
my_campaign.apply_for_each_run_dir(uq.actions.ExecuteLocal(cmd,
interpret='python3'))
# 8. Collate output
my_campaign.collate()
# 9. Run Analysis
# - Calculate bootstrap statistics for collated data
stats = uq.analysis.EnsembleBoot(groupby=[("mu", 0)], qoi_cols=[("Value", 0)])
my_campaign.apply_analysis(stats)
print("stats:\n", my_campaign.get_last_analysis())