-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenvar_exercises.py
More file actions
executable file
·175 lines (127 loc) · 5.48 KB
/
Copy pathenvar_exercises.py
File metadata and controls
executable file
·175 lines (127 loc) · 5.48 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script to practice the implementation of EnVar methods.
Goal: implement the different B matrices as full matrices and as sequences of sparse operators, and compare them with Dirac tests
Content:
- Exercise 1: pure sampled covariance matrix
- Exercise 2: localized covariance matrix
- Exercise 3: hybrid covariance matrix
Requires the tools available in "utilities.py".
Programming: Benjamin Menetrier (benjamin.menetrier@met.no)
Licensing: this code is distributed under the APACHE 2 license
Copyright (c) 2023-2024 Meteorologisk Institutt
"""
###############################################################################
# Imports
###############################################################################
import numpy as np
from utilities import *
###############################################################################
# General parameters (should not be changed)
###############################################################################
# Domain size
n = 101
# Correlation cut-off length-scale (unit: number of points)
Rcor = 15.0
# Maximum ensemble size
Nmax = 1000
# Set random seed (for reproducibility)
np.random.seed(7)
###############################################################################
# Generate true covariance
###############################################################################
Utrue = define_correlation_sqrt(n, Rcor)
Btrue = np.matmul(Utrue, np.transpose(Utrue))
plot_matrix(Btrue, "True covariance")
###############################################################################
# Generate dirac vector
###############################################################################
dirac = np.zeros((n,1))
dirac[int(n/2)] = 1.0
plot_vector(dirac, "Dirac vector")
###############################################################################
# Generate ensemble
###############################################################################
ens = generate_ensemble(n, Utrue, Nmax)
plot_vector(ens[:,0:10], "Ensemble (first 10 members)")
###############################################################################
###############################################################################
# Exercise 1: pure sampled covariance matrix
###############################################################################
###############################################################################
print("Pure sampled covariance matrix:")
# Exercise 2.1: compute ensemble perturbations and compute pure sampled covariance for the whole ensemble
# Remove mean to get ensemble perturbations
# ...
# Scale ensemble perturbations with 1/sqrt(N-1)
# ...
# Compute pure sampled covariance
# ...
# Plot true and pure sampled covariances
# ...
# Exercise 1.2: play with the ensemble size N
N_list = [10, 50, 250]
#for N in N_list:
# ...
# Exercise 1.3: compute a pure sampled covariance for 20 members (and don't overwrite it afterwards), apply it to the dirac vector and plot the result
# ...
# Exercise 1.4: dirac test on the pure sampled covariance with 20 members, sequence of operators
# 1.4.1: Apply square-root adjoint
# ...
# 1.4.2: Apply square-root
# ...
# 1.4.3: Plot result
# ...
###############################################################################
###############################################################################
# Exercise 2: localized covariance matrix
###############################################################################
###############################################################################
print("Localized covariance matrix:")
# Exercise 2.1: create a localization matrix
Rloc = 25.0
Ul = define_correlation_sqrt(n, Rloc)
L = np.matmul(Ul, np.transpose(Ul))
# Exercise 2.2: apply the localization to the pure sampled covariance (Schur product), plot the localization and the localized covariance
# ...
# Exercise 2.3: play with the localization radius Rloc
Rloc_list = [50.0, 25.0, 10.0, 1.0]
#for Rloc in Rloc_list:
# ...
# Exercise 2.4: dirac test on the localized covariance with Rloc = 25
# ...
# Exercise 2.5: dirac test on the localized covariance with Rloc = 25, sequence of operators
# 2.5.1: Apply square-root adjoint
# ...
# 2.5.2: Apply square-root
# ...
# 2.5.3: Plot result
# ...
###############################################################################
###############################################################################
# Exercise 3: hybrid covariance matrix
###############################################################################
###############################################################################
print("Hybrid covariance matrix:")
# Exercise 3.1: generate localized covariance with localization cut-off length-scale 20.0
# ...
# Exercise 3.2: generate a static B of cut-off length-scale 50.0
Rsta = 50.0
Us = define_correlation_sqrt(n, Rsta)
Bs = np.matmul(Us, np.transpose(Us))
plot_matrix(Bs, "Static covariance")
# Exercise 3.3: define a hybrid covariance matrix with weights 0.7 on the ensemble component and 0.3 on the static component
# ...
# Exercise 3.4: play with the hybrid weights, keeping the total variance constant (gammae + gammas = 1)
gammae_list = [0.0, 0.33, 0.66, 1.0]
#for gammae in gammae_list:
# ...
# Exercise 3.5: dirac test on the localized covariance with weights = 0.6 / 0.4, full matrix
# ...
# Exercise 3.6: dirac test on the localized covariance with weights = 0.6 / 0.4, sequence of operators
# 3.6.1: Apply square-root adjoint
# ...
# 3.6.2: Apply square-root
# ...
# 3.6.3: Plot result