forked from Energy-Pathways-Group/GLOceanKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternalWaveModelArbitraryStratification.m
670 lines (584 loc) · 29.8 KB
/
InternalWaveModelArbitraryStratification.m
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
classdef InternalWaveModelArbitraryStratification < InternalWaveModel
% InternalWaveModelArbitraryStratification This implements a linear
% internal wave model with arbitrary stratification.
%
% To initialize the model call,
% wavemodel = InternalWaveModelArbitraryStratification(dims, n, rho, z, nModes, latitude)
% where
% dims a vector containing the length scales of x,y,z
% n a vector containing the number of grid points of x,y,z
% rho a function handle to the density profile valid from -Lz to 0
% z a vector containing the vertical grid point desired
% latitude the latitude of the model.
%
% The number of vertical modes will be determined automatically based
% on the number of resolvable modes. If you want to override this
% behavior, pass the name/value pair ('nModes', nModes).
%
% Two very useful stratification profiles are,
% rho = @(z) -(N0*N0*rho0/g)*z + rho0;
% and
% rho = @(z) rho0*(1 + L_gm*N0*N0/(2*g)*(1 - exp(2*z/L_gm)));
% although, unless you need a special grid for the z-dimension, you
% should probably be using InternalWaveModelConstantStratification
% for the constant stratification case.
%
% The points given in the z-dimension do *not* need to span the
% entire domain, *nor* do they need to be uniform.
%
% This method uses the 'slow' transform (aka, matrix multiplication)
% and therefore the computational cost of the method is dominated by
% O(Nx Ny Nz^3). Restricting nModes to only as many modes as
% necessary can help mitigate this cost.
%
% See also INTERNALWAVEMODEL and
% INTERNALWAVEMODELCONSTANTSTRATIFICATION.
%
% Jeffrey J. Early
properties
K2unique % unique squared-wavenumbers
nK2unique % number of unique squared-wavenumbers
iK2unique % map from 2-dim K2, to 1-dim K2unique
S % The 'G' modes with dimensions [Nz x Nmodes x nK2unique]
Sprime % The 'F' modes with dimensions [Nz x Nmodes x nK2unique]
h_unique % Eigendepth with dimensions [nK2Unique x Nmodes]
F2_unique % value of \int F^2 dz, [nK2Unique x Nmodes]
G2_unique % value of \int G^2 dz, [nK2Unique x Nmodes]
N2G2_unique % value of \int N^2 G^2 dz, [nK2Unique x Nmodes]
NumberOfWellConditionedModes % size() = [nK2unique 1]
didPrecomputedModesForK2unique % size() = [nK2unique 1]
cacheFile % should end with .mat
end
properties (Dependent)
F2 % value of \int F^2 dz, size(self.K2);
G2 % value of \int F^2 dz, size(self.K2);
N2G2 % value of \int N^2 G^2 dz, size(self.K2)
% These convert the coefficients of Amp_plus.*conj(Amp_plus) and
% Amp_minus.*conj(Amp_minus) to their depth-integrated averaged
% values
A0_HKE_factor
Apm_HKE_factor
Apm_VKE_factor
Apm_PE_factor
% Same, but for B
B0_HKE_factor
B_HKE_factor
B_PE_factor
end
methods
function self = InternalWaveModelArbitraryStratification(dims, n, rho, z, latitude, varargin)
if length(dims) ~=3
error('The dimensions must be given as [Lx Ly Lz] where Lz is the depth of the water column.');
end
if length(n) == 2
n(3) = length(z);
end
if length(n) ~=3
error('The number of grid points must be given as [Nx Ny Nz] or [Nx Ny]).');
elseif n(3) ~= length(z)
error('Nz must equal length(z)');
end
nargs = length(varargin);
if mod(nargs,2) ~= 0
error('Arguments must be given as name/value pairs');
end
nModes = [];
cacheFile = [];
extraargs = {}; nExtra = 0;
for k = 1:2:length(varargin)
if strcmp(varargin{k}, 'nModes')
nModes = varargin{k+1};
elseif strcmp(varargin{k}, 'cacheFile')
cacheFile = varargin{k+1};
else
nExtra = nExtra+1; extraargs{nExtra} = varargin{k};
nExtra = nExtra+1; extraargs{nExtra} = varargin{k+1};
end
end
if isa(rho,'numeric') == true
zIn = z;
if abs(((max(z)-min(z))-dims(3))/dims(3)) > 1e-7
error('The given Lz is not consistent with the given z coordinate.')
end
else
if all(z <= 0)
% we can assume that the ocean floor is at -Lz
zIn = [-dims(3) 0];
else
% assume the ocean floor is at 0
zIn = [0 dims(3)];
end
fprintf('Assuming the vertical domain from [%f %f].\n',zIn(1), zIn(2));
end
im = InternalModes(rho,zIn,z,latitude, extraargs{:});
im.nModes = length(z);
if isempty(nModes)
[F,G] = im.ModesAtWavenumber(0);
nGoodModes_F = InternalModes.NumberOfWellConditionedModes(F);
nGoodModes_G = InternalModes.NumberOfWellConditionedModes(G);
nModes = min([nGoodModes_F nGoodModes_G]);
fprintf('nModes was set to %d, based on the number of resolvable modes at k=0. Note that this number would likely be lower for k=k_max.\n',nModes);
end
im.nModes = nModes;
N2 = im.N2;
self@InternalWaveModel(dims, n, z, N2, nModes, latitude);
% We should have this so that if unspecified, it does the right
% number of modes.
self.nModes = nModes;
% Figure out how many unique wavenumbers we have
K2 = self.K2(:,:,1);
[self.K2unique,~,self.iK2unique] = unique(K2);
self.iK2unique = reshape(self.iK2unique,size(K2));
self.nK2unique = length(self.K2unique);
self.S = zeros(self.Nz, self.nModes, self.nK2unique);
self.Sprime = zeros(self.Nz, self.nModes, self.nK2unique);
self.h_unique = ones(self.nK2unique, self.nModes);
self.F2_unique = zeros(self.nK2unique, self.nModes);
self.G2_unique = zeros(self.nK2unique, self.nModes);
self.N2G2_unique = zeros(self.nK2unique, self.nModes);
self.NumberOfWellConditionedModes = zeros(self.nK2unique,1);
self.didPrecomputedModesForK2unique = zeros(self.nK2unique,1);
self.cacheFile = cacheFile;
self.internalModes = im;
self.rho0 = im.rho0;
self.internalModes = im;
self.h = ones(size(self.K2)); % we do this to prevent divide by zero when uninitialized.
self.ReadEigenmodesFromCache();
end
function GenerateWavePhases(self, U_plus, U_minus)
self.ComputeModesForNonzeroWavenumbers( any( (U_plus ~= 0) | (U_minus ~= 0),3) );
GenerateWavePhases@InternalWaveModel(self, U_plus, U_minus );
end
function FillOutWaveSpectrum(self)
self.ComputeModesForNonzeroWavenumbers( 1 );
FillOutWaveSpectrum@InternalWaveModel(self);
end
function [GM3Dint,GM3Dext] = InitializeWithSpectralFunction(self, GM2D_int, varargin)
self.ComputeModesForNonzeroWavenumbers( 1 );
[GM3Dint,GM3Dext] = InitializeWithSpectralFunction@InternalWaveModel(self,GM2D_int,varargin{:});
end
function ComputeModesForNonzeroWavenumbers(self, A)
% We go to great lengths to avoid solving the eigenvalue
% problem, because it's so darned expensive.
% This is algorithm is complicated for 2 reasons:
% 1) We only do the eigenvalue problem for some wavenumber if
% there's a nonzero amplitude associated with it and,
% 2) We only do the computation for unique wavenumbers
K2_ = self.K2(:,:,1);
K2Nyquist = InternalWaveModel.NyquistWavenumbers(K2_);
K2requested = unique(K2_( A & ~K2Nyquist )); % Nonzero amplitudes that we haven't yet computed
K2uncomputed = self.K2unique( ~self.didPrecomputedModesForK2unique );
K2needed = intersect(K2requested,K2uncomputed);
nEVPNeeded = length(K2needed);
if nEVPNeeded == 0
return
elseif nEVPNeeded > 1
fprintf('Solving the EVP for %d unique wavenumbers.\n',length(K2needed));
end
self.internalModes.normalization = Normalization.kConstant;
self.internalModes.nModes = self.nModes;
startTime = datetime('now');
lastSaveTime = startTime;
iSolved = 0; % total number of EVPs solved
for iNeeded=1:length(K2needed)
index = find(self.K2unique==K2needed(iNeeded));
self.ComputeModesForK2UniqueIndex(index);
iSolved = iSolved+1;
if (iSolved == 1 && nEVPNeeded >1) || mod(iSolved,10) == 0
timePerStep = (datetime('now')-startTime)/iSolved;
timeRemaining = (nEVPNeeded-iSolved)*timePerStep;
fprintf('\tsolving EVP %d of %d to file. Estimated finish time %s (%s from now)\n', iSolved, nEVPNeeded, datestr(datetime('now')+timeRemaining), datestr(timeRemaining, 'HH:MM:SS')) ;
end
if seconds(datetime('now')-lastSaveTime) > 300
self.SaveEigenmodesToCache();
lastSaveTime = datetime('now');
end
end
self.SaveEigenmodesToCache();
self.h = self.TransformFromK2UniqueToK2Vector(self.h_unique);
self.SetOmegaFromEigendepths(self.h);
end
function SaveEigenmodesToCache(self)
if isempty(self.cacheFile)
return;
end
K2unique_ = self.K2unique;
nK2unique_ = self.nK2unique;
iK2unique_ = self.iK2unique;
S_ = self.S;
Sprime_ = self.Sprime;
h_unique_ = self.h_unique;
F2_unique_ = self.F2_unique;
G2_unique_ = self.G2_unique;
N2G2_unique_ = self.N2G2_unique;
NumberOfWellConditionedModes_ = self.NumberOfWellConditionedModes;
didPrecomputedModesForK2unique_ = self.didPrecomputedModesForK2unique;
save(self.cacheFile,'K2unique_','nK2unique_','iK2unique_','S_','Sprime_','h_unique_','F2_unique_','G2_unique_','N2G2_unique_','NumberOfWellConditionedModes_','didPrecomputedModesForK2unique_','-v7.3');
fprintf('Saved to cache file %d EVP results.\n',sum(self.didPrecomputedModesForK2unique));
end
function ReadEigenmodesFromCache(self)
if exist(self.cacheFile,'file')
A = load(self.cacheFile);
if self.nModes <= size(A.S_,2)
self.K2unique = A.K2unique_;
self.nK2unique = A.nK2unique_;
self.iK2unique = A.iK2unique_;
self.S = A.S_(:,1:self.nModes,:);
self.Sprime = A.Sprime_(:,1:self.nModes,:);
self.h_unique = A.h_unique_(:,1:self.nModes);
self.F2_unique = A.F2_unique_(:,1:self.nModes);
self.G2_unique = A.G2_unique_(:,1:self.nModes);
self.N2G2_unique = A.N2G2_unique_(:,1:self.nModes);
self.NumberOfWellConditionedModes = A.NumberOfWellConditionedModes_;
self.didPrecomputedModesForK2unique =A.didPrecomputedModesForK2unique_;
else
error('There are not enough modes in this cache!');
end
self.NumberOfWellConditionedModes(self.NumberOfWellConditionedModes>self.nModes) = self.nModes;
self.h = self.TransformFromK2UniqueToK2Vector(self.h_unique);
self.SetOmegaFromEigendepths(self.h);
fprintf('Read from cache file. %d of %d EVPs already solved.\n',sum(self.didPrecomputedModesForK2unique), self.nK2unique);
end
end
function ReadEigenmodesFromNetCDFCache(self,file)
A.K2unique_ = ncread(file,'K2unique');
A.nK2unique_ = length(A.K2unique_);
A.iK2unique_ = ncread(file,'iK2unique');
A.S_ = ncread(file,'S');
A.Sprime_ = ncread(file,'Sprime');
A.h_unique_ = ncread(file,'h_unique');
A.F2_unique_ = ncread(file,'F2_unique');
A.G2_unique_ = ncread(file,'G2_unique');
A.N2G2_unique_ = ncread(file,'N2G2_unique');
A.NumberOfWellConditionedModes_ = ncread(file,'NumberOfWellConditionedModes');
A.didPrecomputedModesForK2unique_ = ncread(file,'didPrecomputedModesForK2unique');
if self.nModes <= size(A.S_,2)
self.K2unique = A.K2unique_;
self.nK2unique = A.nK2unique_;
self.iK2unique = A.iK2unique_;
self.S = A.S_(:,1:self.nModes,:);
self.Sprime = A.Sprime_(:,1:self.nModes,:);
self.h_unique = A.h_unique_(:,1:self.nModes);
self.F2_unique = A.F2_unique_(:,1:self.nModes);
self.G2_unique = A.G2_unique_(:,1:self.nModes);
self.N2G2_unique = A.N2G2_unique_(:,1:self.nModes);
self.NumberOfWellConditionedModes = A.NumberOfWellConditionedModes_;
self.didPrecomputedModesForK2unique =A.didPrecomputedModesForK2unique_;
else
error('There are not enough modes in this cache!');
end
self.NumberOfWellConditionedModes(self.NumberOfWellConditionedModes>self.nModes) = self.nModes;
self.h = self.TransformFromK2UniqueToK2Vector(self.h_unique);
self.SetOmegaFromEigendepths(self.h);
fprintf('Read from cache file. %d of %d EVPs already solved.\n',sum(self.didPrecomputedModesForK2unique), self.nK2unique);
end
function ComputeModesForK2UniqueIndex(self,iUnique)
kk = self.K2unique(iUnique);
[F,G,h,~,F2_,N2G2_,G2_] = self.internalModes.ModesAtWavenumber(sqrt(kk));
h = reshape(h,[1 1 self.nModes]);
N = InternalModes.NumberOfWellConditionedModes(G);
badIndex = find(h>0,1,'last');
if badIndex < self.nModes
warning('Eigenvalue problem returned negative eigenvalue at index %d, try with higher resolution.',badIndex)
end
self.S(:,:,iUnique) = G;
self.Sprime(:,:,iUnique) = F;
self.F2_unique(iUnique,:) = F2_;
self.G2_unique(iUnique,:) = G2_;
self.N2G2_unique(iUnique,:) = N2G2_;
self.h_unique(iUnique,:) = h;
self.NumberOfWellConditionedModes(iUnique) = N;
self.didPrecomputedModesForK2unique(iUnique) = 1;
end
function h_full = TransformFromK2UniqueToK2Vector(self,h_k2unique)
% Converts a vector (like h) to its full matrix format---lots
% of redundant data.
%
% size(h_k2unique) = [K2unique nModes]
% size(h_full) = [Nx Ny Nmodes]
h_full = nan(size(self.K2));
for iUnique=1:length(self.K2unique)
indices = find(self.iK2unique==iUnique);
for iIndex=1:length(indices)
currentIndex = indices(iIndex);
[i,j] = ind2sub([self.Nx self.Ny], currentIndex);
h_full(i,j,:) = h_k2unique(iUnique,:);
end
end
end
function S_full = TransformFromK2UniqueToK2Matrix(self,S_k2unique)
% Converts a vector (like h) to its full matrix format---lots
% of redundant data.
%
% size(S_k2unique) = [Nz Nmodes K2unique]
% size(S_full) = [Nz Nmodes Nx Ny]
S_full = zeros(self.Nz, self.nModes, self.Nx, self.Ny);
for iUnique=1:length(self.K2unique)
indices = find(self.iK2_unique==iUnique);
for iIndex=1:length(indices)
currentIndex = indices(iIndex);
[i,j] = ind2sub([self.Nx self.Ny], currentIndex);
S_full(i,j,:) = S_k2unique(:,:,S_k2unique);
end
end
end
function rho = RhoBarAtDepth(self,z)
rho = interp1(self.internalModes.z,self.internalModes.rho,z,'spline');
end
function N2 = N2AtDepth(self,z)
N2 = interp1(self.internalModes.z,self.internalModes.N2,z,'spline');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function value = get.F2(self)
value = self.TransformFromK2UniqueToK2Vector(self.F2_unique);
end
function value = get.G2(self)
value = self.TransformFromK2UniqueToK2Vector(self.G2_unique);
end
function value = get.N2G2(self)
value = self.TransformFromK2UniqueToK2Vector(self.N2G2_unique);
end
function value = get.A0_HKE_factor(self)
value = self.Lz/2;
end
function value = get.Apm_HKE_factor(self)
% This currently differs from the definition in the manuscript
% by a factor of h. The missing factor of 1/2 is the c.c.
omega = self.Omega;
if abs(self.f0) < 1e-14 % This handles the f=0 case.
omega(omega == 0) = 1;
end
fOverOmega = self.f0 ./ omega;
value = (1 + fOverOmega.*fOverOmega) .* self.F2 ./ (2*self.h);
end
function value = get.Apm_VKE_factor(self)
value = self.K2 .* self.h .* self.G2/2;
end
function value = get.Apm_PE_factor(self)
value = self.K2 .* self.h .* self.N2G2 ./ (2*self.Omega.*self.Omega);
end
function value = get.B0_HKE_factor(self)
value = (self.g^2/(self.f0*self.f0)) * self.K2(:,:,1) * self.Lz/2;
end
function value = get.B_HKE_factor(self)
value = (self.g*self.g/(2*self.f0*self.f0)) .* self.K2 .* self.F2;
end
function value = get.B_PE_factor(self)
value = self.N2G2/2;
end
end
methods %(Access = protected)
function [F,G] = InternalModeAtDepth(self,z,iWave)
% return the normal mode
[k0, l0, j0] = ind2sub([self.Nx self.Ny self.Nz],iWave);
F = interp1(self.z,self.Sprime(:,j0,k0+1,l0+1),z,'spline');
G = interp1(self.z,self.S(:,j0,k0+1,l0+1),z,'spline');
end
function ratio = UmaxGNormRatioForWave(self,k0, l0, j0)
A = zeros(size(self.K2(:,:,1)));
A(k0+1,l0+1) = 1;
self.ComputeModesForNonzeroWavenumbers(A)
myH = self.h(k0+1,l0+1,j0);
myK = self.Kh(k0+1,l0+1,j0);
self.internalModes.normalization = Normalization.uMax;
F = self.internalModes.ModesAtWavenumber(myK);
F_uConst = F(:,j0);
iK2 = self.iK2unique(k0+1,l0+1);
F_Gnorm = self.Sprime(:,j0,iK2);
[~, index] = max(abs(F_uConst));
F_coefficient = F_Gnorm(index)/F_uConst(index);
ratio = sqrt(myH)/F_coefficient;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Called by the superclass when advecting particles spectrally.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function F = InternalUVModeAtDepth(self, z, iMode)
error('broken');
[k0, l0, j0] = ind2sub([self.Nx self.Ny self.Nz],iMode);
F = interp1(self.z,self.Sprime(:,j0,k0+1,l0+1),z,'spline');
end
function G = InternalWModeAtDepth(self, z, iMode)
error('broken');
[k0, l0, j0] = ind2sub([self.Nx self.Ny self.Nz],iMode);
G = interp1(self.z,self.S(:,j0,k0+1,l0+1),z,'spline');
end
function InitializeWithHorizontalVelocityAndDensityPerturbationFields(self, t, u, v, rho_prime)
if length(size(u)) == 2 % deal with 2D data
if self.Ny == 1
u = reshape(u,self.Nx,self.Ny,self.Nz);
v = reshape(u,self.Nx,self.Ny,self.Nz);
rho_prime = reshape(rho_prime,self.Nx,self.Ny,self.Nz);
else
error('Dimensional issues');
end
end
a = self.rho0 * reshape(self.N2,1,1,[])/self.g;
zeta = rho_prime ./ a;
self.InitializeWithHorizontalVelocityAndIsopycnalDisplacementFields(t,u,v,zeta);
end
% function InitializeWithHorizontalVelocityAndIsopycnalDisplacementFields(self, t, u, v, zeta)
% % This function can be used as a wave-vortex decomposition. It
% % will *exactly* recover amplitudes being used the generate the
% % dynamical fields. For the moment I assume assuming no
% % buoyancy perturbation at the boundaries.
%
% if length(size(u)) == 2 % deal with 2D data
% if self.Ny == 1
% u = reshape(u,self.Nx,self.Ny,self.Nz);
% v = reshape(u,self.Nx,self.Ny,self.Nz);
% zeta = reshape(zeta,self.Nx,self.Ny,self.Nz);
% else
% error('Dimensional issues');
% end
% end
%
% ubar = self.TransformFromSpatialDomainWithF( u );
% vbar = self.TransformFromSpatialDomainWithF( v );
% etabar = self.TransformFromSpatialDomainWithG( zeta );
%
% alpha = atan2(self.L,self.K);
% delta = sqrt(self.h).*(cos(alpha) .* ubar + sin(alpha) .* vbar);
% zeta = sqrt(self.h).*(cos(alpha) .* vbar - sin(alpha) .* ubar);
%
% omega = abs(self.Omega);
% isFzero = 0;
% if abs(self.f0) < 1e-14 % This handles the f=0 case.
% omega(omega == 0) = 1;
% isFzero = 1;
% end
% fOverOmega = self.f0 ./ omega;
% KhOverOmega = self.Kh ./ omega;
%
% A_plus = exp(-sqrt(-1)*self.Omega*t).*(-self.g*sqrt(self.h).*etabar.*KhOverOmega + delta - sqrt(-1)*zeta.*fOverOmega)/2;
% A_minus = exp(sqrt(-1)*self.Omega*t).*(self.g*sqrt(self.h).*etabar.*KhOverOmega + delta + sqrt(-1)*zeta.*fOverOmega)/2;
% if isFzero == 1
% self.B = zeros(size(self.Kh));
% else
% self.B = (etabar*self.f0 - sqrt(-1)*zeta.*self.Kh.*sqrt(self.h))*self.f0./(self.Omega.*self.Omega);
% end
%
% % inertial must be solved for separately.
% A_plus(1,1,:) = exp(-sqrt(-1)*self.f0*t)*(ubar(1,1,:) - sqrt(-1)*vbar(1,1,:)).*sqrt(self.h(1,1,:))/2;
% A_minus(1,1,:) = conj(A_plus(1,1,:));
% self.B(1,1,:) = 0;
% self.B = InternalWaveModel.MakeHermitian(self.B);
%
% % B is the geostrophic solution, not yet implemented.
% A_plus = InternalWaveModel.MakeHermitian(A_plus);
% A_minus = InternalWaveModel.MakeHermitian(A_minus);
% self.GenerateWavePhases(A_plus,A_minus);
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Computes the phase information given the amplitudes (internal)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function u = TransformToSpatialDomainWithBarotropicFMode(self, u_bar)
u = self.Nx*self.Ny*ifft(ifft(u_bar,self.Nx,1),self.Ny,2,'symmetric');
u = repmat(u,[1 1 self.Nz]);
end
function u = TransformToSpatialDomainWithF(self, u_bar)
u_temp = zeros(self.Nx,self.Ny,self.Nz);
u_bar = permute(u_bar,[3 1 2]); % Speed optimization: keep matrices adjacent in memory
for i=1:self.Nx
for j=1:self.Ny
iK2 = self.iK2unique(i,j);
u_temp(i,j,:) = self.Sprime(:,:,iK2)*u_bar(:,i,j);
end
end
% Here we use what I call the 'Fourier series' definition of the ifft, so
% that the coefficients in frequency space have the same units in time.
u = self.Nx*self.Ny*ifft(ifft(u_temp,self.Nx,1),self.Ny,2,'symmetric');
end
function w = TransformToSpatialDomainWithG(self, w_bar )
w_temp = zeros(self.Nx,self.Ny,self.Nz);
w_bar = permute(w_bar,[3 1 2]); % Speed optimization: keep matrices adjacent in memory
for i=1:self.Nx
for j=1:self.Ny
iK2 = self.iK2unique(i,j);
w_temp(i,j,:) = self.S(:,:,iK2)*w_bar(:,i,j);
end
end
% Here we use what I call the 'Fourier series' definition of the ifft, so
% that the coefficients in frequency space have the same units in time.
w = self.Nx*self.Ny*ifft(ifft(w_temp,self.Nx,1),self.Ny,2,'symmetric');
end
function u_bar = TransformFromSpatialDomainWithBarotropicFMode(self, u)
% Consistent with the DCT-I, the end points only have half the
% width of the other points.
u(:,:,1) = 0.5*u(:,:,1);
u(:,:,end) = 0.5*u(:,:,end);
u_bar = fft(fft(sum(u,3)/(self.Nz-1),self.Nx,1),self.Ny,2)/self.Nx/self.Ny;
end
function u_bar = TransformFromSpatialDomainWithF(self, u)
if length(size(u)) == 2 % deal with 2D data
if self.Ny == 1
u = reshape(u,self.Nx,self.Ny,self.Nz);
else
error('Dimensional issues');
end
end
% convert to K x L x Z
u_temp = fft(fft(u,self.Nx,1),self.Ny,2)/self.Nx/self.Ny;
%%%% Temp hack %%%%%
% We have to remove the barotropic component before projecting
% with the baroclinic modes
u_rm = u_temp;
u_rm(:,:,1) = 0.5*u_rm(:,:,1);
u_rm(:,:,end) = 0.5*u_rm(:,:,end);
u_rm = sum(u_rm,3)/(self.Nz-1);
u_temp = u_temp - u_rm;
%%%% Temp hack %%%%%
self.ComputeModesForNonzeroWavenumbers( any(u_temp,3) );
RedundantWavenumbers = InternalWaveModel.RedundantHermitianCoefficients(zeros(self.Nx,self.Ny));
% The 'S' and 'Sprime' have dimensions Nz x Nmodes x Nx x Ny
u_temp = permute(u_temp,[3 1 2]); % convert to Nz x Nk x Nl
u_bar = zeros(self.Nx,self.Ny,self.nModes);
zIndices = 1:self.Nz;
for i=1:self.Nx
for j=1:self.Ny
iK2 = self.iK2unique(i,j);
if i == (self.Nx/2 + 1) || j == (self.Ny/2 + 1) || RedundantWavenumbers(i,j) == 1 || ~self.didPrecomputedModesForK2unique(iK2)
continue;
end
N = self.NumberOfWellConditionedModes(iK2);
u_bar(i,j,1:N) = self.Sprime(zIndices,1:N,iK2)\u_temp(zIndices,i,j);
end
end
u_bar = InternalWaveModel.MakeHermitian(u_bar);
end
function w_bar = TransformFromSpatialDomainWithG(self, w)
if length(size(w)) == 2 % deal with 2D data
if self.Ny == 1
w = reshape(w,self.Nx,self.Ny,self.Nz);
else
error('Dimensional issues');
end
end
% convert to K x L x Z
w_temp = fft(fft(w,self.Nx,1),self.Ny,2)/self.Nx/self.Ny;
self.ComputeModesForNonzeroWavenumbers( any(w_temp,3) );
RedundantWavenumbers = InternalWaveModel.RedundantHermitianCoefficients(zeros(self.Nx,self.Ny));
% The 'S' and 'Sprime' have dimensions Nz x Nmodes x Nx x Ny
w_temp = permute(w_temp,[3 1 2]); % convert to Nz x Nk x Nl
w_bar = zeros(self.Nx,self.Ny,self.nModes);
% Chop off the end points, which are zero anyway, given
% boundary conditions
zIndices = 2:(self.Nz-1);
for i=1:self.Nx
for j=1:self.Ny
iK2 = self.iK2unique(i,j);
if i == (self.Nx/2 + 1) || j == (self.Ny/2 + 1) || RedundantWavenumbers(i,j) == 1 || ~self.didPrecomputedModesForK2unique(iK2)
continue;
end
N = self.NumberOfWellConditionedModes(iK2);
w_bar(i,j,1:N) = self.S(zIndices,1:N,iK2)\w_temp(zIndices,i,j);
end
end
w_bar = InternalWaveModel.MakeHermitian(w_bar);
end
end
end