-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspinupParameterized.py
executable file
·146 lines (124 loc) · 5.29 KB
/
spinupParameterized.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 5 15:44:07 2020
@author: arthur
"""
"""
Low-resolution, run with NN parameterization
"""
import sys
sys.path.append('/home/ag7531/code')
sys.path.append('/home/ag7531/code/subgrid')
import torch
import logging
import mlflow
import argparse
from shallowwater import ShallowWaterModel
from shallowwaterParameterized import (WaterModelWithDLParameterization,
Parameterization)
from subgrid.models.utils import load_model_cls
from subgrid.utils import select_experiment, select_run
from subgrid.testing.utils import pickle_artifact
from netCDF4 import Dataset
from os.path import join
import tempfile
from utils import BoundaryCondition
# Make temporary dir to save outputs
temp_dir = tempfile.mkdtemp(dir='/scratch/ag7531/temp/')
# Use GPU if available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
parser = argparse.ArgumentParser()
parser.add_argument('nyears', type=int,
help='Number of years the model is spun up for')
parser.add_argument('factor', type=int,
help='Coarse-graining factor. Keep to 4 for now.')
parser.add_argument('--every', type=int, default=4,
help='Parameter passed to the stochastic ' \
'parameterization. Set to 0 for no parameterization.')
parser.add_argument('--every_noise', type=int, default=4,
help='Parameter passed to the stochastic parameterization')
parser.add_argument('--param_amp', type=int, default=1.,
help='Multiplication factor applied to parameterization')
parser.add_argument('--boundary', type=str, default='no-slip',
help='either \'no-slip\' or \'free-slip\'')
parser.add_argument('--force-zero-sum', type=bool, default=False,
help='Whether we enforce that the integrate forcing is '
'zero')
script_args = parser.parse_args()
n_years = script_args.nyears
factor = script_args.factor
every = script_args.every
every_noise = script_args.every_noise
param_amp = script_args.param_amp
boundary_condition = BoundaryCondition.get(script_args.boundary)
force_zero_sum = script_args.force_zero_sum
domain_size = 3840
parameterization = every > 0
if parameterization:
logging.info('Running WITH parameterization')
else:
logging.info('Running with NO parameterization')
mlflow.set_experiment('parameterized')
mlflow.log_params(dict(n_years=n_years, parameterization=parameterization,
factor=factor, boundary=boundary_condition.name))
if parameterization:
mlflow.log_params(dict(param_amp=param_amp, every=every,
every_noise=every_noise, zero_sum=force_zero_sum))
model = ShallowWaterModel(output_path=temp_dir,
Nx=domain_size // 10 // factor,
Ny=domain_size // 10 // factor,
Lx=domain_size * 1e3,
Ly=domain_size * 1e3,
Nt=n_years*360*24*60*60,
dump_freq=1*24*60*60, dump_output=True, tau0=0.12,
model_name='eddy_permitting',
bc=boundary_condition.value)
if parameterization:
# TODO put into separate function, separate utils file
# Prompts the user to select a trained model to be used as parameterization
models_experiment_id, _ = select_experiment()
cols = ['metrics.test loss', 'start_time', 'params.time_indices',
'params.model_cls_name', 'params.source.run_id', 'params.submodel']
model_run = select_run(sort_by='start_time', cols=cols,
experiment_ids=[models_experiment_id, ])
model_module_name = model_run['params.model_module_name']
model_cls_name = model_run['params.model_cls_name']
logging.info('Creating the neural network model')
model_cls = load_model_cls(model_module_name, model_cls_name)
# Load the model's file
client = mlflow.tracking.MlflowClient()
model_file = client.download_artifacts(model_run.run_id,
'models/trained_model.pth')
transformation = pickle_artifact(model_run.run_id, 'models/transformation')
net = model_cls(2, 4, padding='same')
net.final_transformation = transformation
# Load parameters of pre-trained model
logging.info('Loading the neural net parameters')
net.cpu()
net.load_state_dict(torch.load(model_file))
print('*******************')
print(net)
print('*******************')
u, v, eta = model.set_initial_cond( init='rest' )
if parameterization:
parameterization = Parameterization(net, device, param_amp, every,
every_noise, force_zero_sum)
model = WaterModelWithDLParameterization(model, parameterization)
# Time-integration of the model
last_percent = None
for i in range( model.N_iter ) :
percent = int(100.0 * float(i) / model.N_iter)
if percent != last_percent:
print( "{}%".format(percent))
last_percent = percent
u_new, v_new, eta_new = model.integrate_forward(u, v, eta)
if u_new is None :
print( "Integration finished!" )
break
u = u_new
v = v_new
eta = eta_new
# log all outputs
mlflow.log_artifacts(temp_dir)
print('Done.')