-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11_modelOutput.py
More file actions
executable file
·132 lines (118 loc) · 4.9 KB
/
11_modelOutput.py
File metadata and controls
executable file
·132 lines (118 loc) · 4.9 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import sys, os
sys.path.insert(2, os.getcwd())
import numpy as np
from cfno.data.preprocessing import HDF5Dataset
from cfno.training.pySDC import FourierNeuralOp
from cfno.simulation.post import contourPlot
from cfno.utils import readConfig
varChoices = ["vx", "vz", "b", "p"]
# -----------------------------------------------------------------------------
# Script parameters
# -----------------------------------------------------------------------------
parser = argparse.ArgumentParser(
description='View model output from inputs stored in a HDF5 dataset',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"--dataFile", default="dataset.h5", help="name of the dataset HDF5 file")
parser.add_argument(
"--checkpoint", default="model.pt", help="name of the model checkpoint file")
parser.add_argument(
"--var", default="b", help="variable to view", choices=varChoices)
parser.add_argument(
"--iSample", default=0, help="sample index", type=int)
parser.add_argument(
"--iXBeg", default=0, help="xPatch start index", type=int)
parser.add_argument(
"--iYBeg", default=0, help="yPatch start index", type=int)
parser.add_argument(
"--iXEnd", default=None, help="xPatch end index", type=int)
parser.add_argument(
"--iYEnd", default=None, help="yPatch end index", type=int)
parser.add_argument(
"--outType", default="solution", help="type of output", choices=["solution", "update"])
parser.add_argument(
"--refScales", action="store_true", help="use the same scales as the reference field")
parser.add_argument(
"--saveFig", default="modelView", help="output name for contour figure")
parser.add_argument(
"--config", default="config.yaml", help="configuration file")
parser.add_argument(
"--get_subdomain_output", action="store_true", help="Get subdomain output")
parser.add_argument(
"--use_full_input", action="store_true", help="Use full input")
args = parser.parse_args()
dataFile = args.dataFile
checkpoint = args.checkpoint
var = args.var
iSample = args.iSample
outType = args.outType
refScales = args.refScales
saveFig = args.saveFig
# Need model config to get output of shape different to input
# .i.e when using args.get_subdomain_ouput
if args.get_subdomain_output:
if args.config is not None:
config = readConfig(args.config)
args.__dict__.update(**config["model"])
modelConfig = dict(config.model)
else:
raise ValueError("Model configuration is required for FNO to output subdomain.")
iXBeg = args.iXBeg
iYBeg = args.iYBeg
iXEnd = args.iXEnd
iYEnd = args.iYEnd
print(f'Args: {args}')
# -----------------------------------------------------------------------------
# Script execution
# -----------------------------------------------------------------------------
dataset = HDF5Dataset(dataFile)
nSamples = len(dataset)
assert iSample < nSamples, f"iSample={iSample} to big for {nSamples} samples"
xGrid, yGrid = dataset.grid
u0, uRef_full = dataset.sample(iSample)
print(f'Shape of u0: {u0.shape}, uRef: {uRef_full.shape}')
if args.use_full_input:
uInit = u0[varChoices.index(var)]
input = u0
if args.get_subdomain_output:
uRef = uRef_full[varChoices.index(var), iXBeg:iXEnd, iYBeg:iYEnd].copy()
else:
uRef = uRef_full[varChoices.index(var)].copy()
iXBeg = 0
iYBeg = 0
iXEnd = input.shape[1]
iYEnd = input.shape[2]
else:
uInit = u0[varChoices.index(var), iXBeg:iXEnd, iYBeg:iYEnd]
input = u0[:,iXBeg:iXEnd, iYBeg:iYEnd]
uRef = uRef_full[varChoices.index(var), iXBeg:iXEnd, iYBeg:iYEnd].copy()
if args.get_subdomain_output:
model = FourierNeuralOp(model=modelConfig, checkpoint=checkpoint)
else:
model = FourierNeuralOp(checkpoint=checkpoint)
uPred = model(input)[varChoices.index(var)].copy()
if dataset.outType == "update":
uRef /= dataset.outScaling
if uRef.shape != uInit.shape:
uInit = u0[varChoices.index(var), iXBeg:iXEnd, iYBeg:iYEnd]
print(f'Shape of uInit: {uInit.shape}, uRef:{uRef.shape}, uPred: {uPred.shape}')
if outType == "solution" and dataset.outType == "update":
uRef += uInit
if outType == "update" and dataset.outType == "solution":
uRef -= uInit
if outType == "update":
uPred -= uInit
Y, X = xGrid[iXBeg:iXEnd], yGrid[iYBeg:iYEnd]
contourPlot(
uPred, X, Y, title=f"Model {outType} for {var} using sample {iSample}",
refField=uRef, refTitle=f"Dedalus reference (dt={dataset.infos['dtInput'][()]:1.2g}s)",
saveFig=f'{saveFig}_{outType}.jpg', closeFig=False, refScales=refScales)
print(f" -- saved {var} contour for sample {iSample}")
contourPlot(
np.abs(uPred-uRef), X, Y, title=f"Model {outType} error for {var} using sample {iSample}\nDedalus reference (dt={dataset.infos['dtInput'][()]:1.2g}s)",
refField=None, refTitle=None,
saveFig=f'{saveFig}_{outType}_error.jpg', closeFig=False, refScales=False)
print(f" -- saved {var} contour for sample {iSample}")