-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
357 lines (314 loc) · 12.1 KB
/
Copy pathsetup.py
File metadata and controls
357 lines (314 loc) · 12.1 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""Common setup routines for training and evaluation"""
import sys
import tomllib
import itertools
import torch
from peds.datasets import SavedDataset
from peds.diffusion_model_1d import DiffusionModel1d
from peds.diffusion_model_2d import DiffusionModel2d
from peds.distributions_lognormal import (
LogNormalDistribution1d,
LogNormalDistribution2d,
)
from peds.distributions_fibres import FibreRadiusDistribution, FibreDistribution2d
from peds.quantity_of_interest import QoISampling1d, QoISampling2d
from peds.interpolation_1d import (
VertexToVolumeInterpolator1d,
VolumeToVertexInterpolator1d,
)
from peds.interpolation_2d import (
VertexToVolumeInterpolator2d,
VolumeToVertexInterpolator2d,
)
__all__ = [
"read_config",
"get_distribution",
"get_physics_model",
"get_qoi",
"get_downsampler",
"get_nn_model",
"get_coarse_model",
"get_pure_nn_model",
"get_datasets",
]
def read_config():
"""Parse command line arguments and read configuration file"""
if len(sys.argv) < 2:
print(f"Usage: python {sys.argv[0]} CONFIGFILE")
sys.exit(0)
config_file = sys.argv[1]
print(f"reading parameters from {config_file}")
with open(config_file, "rb") as f:
config = tomllib.load(f)
print()
print("==== parameters ====")
print()
with open(config_file, "r", encoding="utf8") as f:
for line in f.readlines():
print(line.strip())
print()
return config
def get_distribution(config):
"""Initialise the distribution based on configuration
Returns distribution object that can be used to generate samples for the logarithm of
the diffusion coefficient.
:arg config: configuration dictionary
"""
n = config["discretisation"]["n"]
domain_size = config["discretisation"]["domain_size"]
dim = config["model"]["dimension"]
if dim == 1:
if config["data"]["distribution"] == "lognormal":
return LogNormalDistribution1d(
n,
domain_size,
config["distribution"]["lognormal"]["Lambda"],
config["distribution"]["lognormal"]["a_power"],
)
else:
raise RuntimeError("Only log-normal distribution supported in 1d")
elif dim == 2:
if config["data"]["distribution"] == "lognormal":
return LogNormalDistribution2d(
n, domain_size, config["distribution"]["lognormal"]["Lambda"]
)
elif config["data"]["distribution"] == "fibre":
r_fibre_dist = FibreRadiusDistribution(
r_avg=config["distribution"]["fibre"]["r_fibre_avg"],
r_min=config["distribution"]["fibre"]["r_fibre_min"],
r_max=config["distribution"]["fibre"]["r_fibre_max"],
sigma=config["distribution"]["fibre"]["r_fibre_sigma"],
gaussian=config["distribution"]["fibre"]["gaussian"],
)
return FibreDistribution2d(
n,
domain_size,
volume_fraction=config["distribution"]["fibre"]["volume_fraction"],
r_fibre_dist=r_fibre_dist,
kdiff_background=config["distribution"]["fibre"]["kdiff_background"],
kdiff_fibre=config["distribution"]["fibre"]["kdiff_fibre"],
)
else:
raise RuntimeError(f"invalid dimension: {dim}")
def get_physics_model(config):
"""Construct the high-resolution physics model based on the configuration
Returns physics model
:arg config: configuration dictionary
"""
n = config["discretisation"]["n"]
domain_size = config["discretisation"]["domain_size"]
dim = config["model"]["dimension"]
if dim == 1:
f_rhs = torch.ones(size=(n,), dtype=torch.float) * config["model"]["f_rhs"]
return DiffusionModel1d(f_rhs, domain_size)
elif dim == 2:
f_rhs = torch.ones(size=(n, n), dtype=torch.float) * config["model"]["f_rhs"]
return DiffusionModel2d(f_rhs, domain_size)
else:
raise RuntimeError(f"invalid dimension: {dim}")
def get_qoi(config):
"""Initialise the QoI based on the configuration
Returns QoI object that can be used to sample the quantity of interest
"""
dim = config["model"]["dimension"]
if dim == 1:
return QoISampling1d(config["qoi"]["sample_points"])
elif dim == 2:
return QoISampling2d(config["qoi"]["sample_points"])
else:
raise RuntimeError(f"invalid dimension: {dim}")
def get_downsampler(config):
"""Initialise the downsampler based on the configuration
Returns downsampler object that can be used to downsample the high-resolution field
:arg config: configuration dictionary
"""
n = config["discretisation"]["n"]
dim = config["model"]["dimension"]
if dim == 1:
return torch.nn.Sequential(
torch.nn.Unflatten(-1, (1, n + 1)),
VertexToVolumeInterpolator1d(),
torch.nn.AvgPool1d(1, stride=config["discretisation"]["scaling_factor"]),
VolumeToVertexInterpolator1d(),
torch.nn.Flatten(-2, -1),
)
elif dim == 2:
return torch.nn.Sequential(
torch.nn.Unflatten(-2, (1, n + 1)),
VertexToVolumeInterpolator2d(),
torch.nn.AvgPool2d(1, stride=config["discretisation"]["scaling_factor"]),
VolumeToVertexInterpolator2d(),
torch.nn.Flatten(-3, -2),
)
else:
raise RuntimeError(f"invalid dimension: {dim}")
def get_nn_model(config):
"""Initialise the NN model based on the configuration
Returns neural network model object
:arg config: configuration dictionary
"""
n = config["discretisation"]["n"]
if config["model"]["dimension"] == 1:
return torch.nn.Sequential(
torch.nn.Unflatten(-1, (1, n + 1)),
VertexToVolumeInterpolator1d(),
torch.nn.Conv1d(1, 4, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool1d(2, ceil_mode=True),
torch.nn.Conv1d(4, 4, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool1d(2, ceil_mode=True),
torch.nn.Conv1d(4, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool1d(2, ceil_mode=True),
torch.nn.Conv1d(8, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool1d(2, ceil_mode=True),
torch.nn.Conv1d(8, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.Conv1d(8, 1, 3, padding=1),
VolumeToVertexInterpolator1d(),
torch.nn.Flatten(-2, -1),
)
elif config["model"]["dimension"] == 2:
return torch.nn.Sequential(
torch.nn.Unflatten(-2, (1, n + 1)),
VertexToVolumeInterpolator2d(),
torch.nn.Conv2d(1, 4, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, ceil_mode=True),
torch.nn.Conv2d(4, 4, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, ceil_mode=True),
torch.nn.Conv2d(4, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, ceil_mode=True),
torch.nn.Conv2d(8, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, ceil_mode=True),
torch.nn.Conv2d(8, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(8, 1, 3, padding=1),
VolumeToVertexInterpolator2d(),
torch.nn.Flatten(-3, -2),
)
else:
dim = config["model"]["dimension"]
raise RuntimeError(f"invalid dimension: {dim}")
def get_coarse_model(physics_model_highres, scaling_factor, qoi):
"""Construct coarsened model based on scaling factor
This model coarsens the diffusion coefficient and then applies a low-resolution
physics model on which the quantity of interest is evaluated
:arg physics_model_highres: high-resolution physics model
:arg scaling_factor: scaling factor for the coarsening
:arg qoi: quantity of interest object
"""
f_rhs = physics_model_highres.metadata["f_rhs"]
n = f_rhs.shape[-1]
dim = f_rhs.ndim
if dim == 1:
return torch.nn.Sequential(
torch.nn.Unflatten(-1, (1, n + 1)),
VertexToVolumeInterpolator1d(),
torch.nn.AvgPool1d(1, stride=scaling_factor),
VolumeToVertexInterpolator1d(),
torch.nn.Flatten(-2, -1),
physics_model_highres.coarsen(scaling_factor),
qoi,
)
elif dim == 2:
return torch.nn.Sequential(
torch.nn.Unflatten(-2, (1, n + 1)),
VertexToVolumeInterpolator2d(),
torch.nn.AvgPool2d(1, stride=scaling_factor),
VolumeToVertexInterpolator2d(),
torch.nn.Flatten(-3, -2),
physics_model_highres.coarsen(scaling_factor),
qoi,
)
else:
raise RuntimeError(f"invalid dimension: {dim}")
def get_pure_nn_model(config):
"""Initialise the naive NN model
Returns neural network model object
:arg config: configuration dictionary
"""
qoi = get_qoi(config)
n = config["discretisation"]["n"]
if config["model"]["dimension"] == 1:
return torch.nn.Sequential(
torch.nn.Unflatten(-1, (1, n + 1)),
VertexToVolumeInterpolator1d(),
torch.nn.Conv1d(1, 4, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool1d(2, ceil_mode=True),
torch.nn.Conv1d(4, 4, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool1d(2, ceil_mode=True),
torch.nn.Conv1d(4, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool1d(2, ceil_mode=True),
torch.nn.Conv1d(8, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool1d(2, ceil_mode=True),
torch.nn.Conv1d(8, 8, 3, padding=1),
torch.nn.Flatten(-2, -1),
torch.nn.ReLU(),
torch.nn.Linear(n // 2, 16),
torch.nn.ReLU(),
torch.nn.Linear(16, 16),
torch.nn.ReLU(),
torch.nn.Linear(16, qoi.dim),
)
elif config["model"]["dimension"] == 2:
return torch.nn.Sequential(
torch.nn.Unflatten(-2, (1, n + 1)),
VertexToVolumeInterpolator2d(),
torch.nn.Conv2d(1, 4, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, ceil_mode=True),
torch.nn.Conv2d(4, 4, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, ceil_mode=True),
torch.nn.Conv2d(4, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, ceil_mode=True),
torch.nn.Conv2d(8, 8, 3, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(2, ceil_mode=True),
torch.nn.Conv2d(8, 8, 3, padding=1),
torch.nn.Flatten(-3, -1),
torch.nn.ReLU(),
torch.nn.Linear(8 * (n // 16) ** 2, 16),
torch.nn.ReLU(),
torch.nn.Linear(16, 16),
torch.nn.ReLU(),
torch.nn.Linear(16, qoi.dim),
)
else:
dim = config["model"]["dimension"]
raise RuntimeError(f"invalid dimension: {dim}")
def get_datasets(config):
"""Return the training, validation and test datasets
The datasets are created from the saved dataset file.
:arg config: configuration dictionary
"""
n_samples_train = config["data"]["n_samples_train"]
n_samples_valid = config["data"]["n_samples_valid"]
n_samples_test = config["data"]["n_samples_test"]
n_samples = n_samples_train + n_samples_valid + n_samples_test
data_filename = config["data"]["filename"]
dataset = SavedDataset(data_filename)
assert len(dataset) == n_samples
train_dataset = list(itertools.islice(dataset, 0, n_samples_train))
valid_dataset = list(
itertools.islice(dataset, n_samples_train, n_samples_train + n_samples_valid)
)
test_dataset = list(
itertools.islice(
dataset,
n_samples_train + n_samples_valid,
n_samples_train + n_samples_valid + n_samples_test,
)
)
return train_dataset, valid_dataset, test_dataset