-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields_2D.py
1782 lines (1502 loc) · 52 KB
/
fields_2D.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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#
# fields_2D.py
# module for 2D fields and associated methods.
#
#
###############################################################################
"""
Module with functions for working with 3D fields.
Layout of a field:
------------------
Fields should be 2D numpy arrays, a[y,x] where y is the Chebyshev
direction and x is a fourier direction.
Functions:
----------
set_constants:
returns a dictionary containing the constants to be passed around the
program.
dy:
First Chebyshev direction derivative of a field using Orszag's method
dyy:
Second Chebyshev direction derivative of a field using Orszag's method
d3y:
Third Chebyshev direction derivative of a field using Orszag's method
d4y:
Third Chebyshev direction derivative of a field using Orszag's method
dx:
First Fourier derivative.
dxx:
Second Fourier derivative.
d3x:
Second Fourier derivative.
d4x:
Second Fourier derivative.
dxy:
Chebyshev + Fourier derivative.
dxyy:
2 Chebyshev + Fourier derivative.
dxxy:
Chebyshev + 2 Fourier derivative.
dxyyy:
3 Chebyshev + Fourier derivative.
dxxyy:
2 Chebyshev + 2 Fourier derivative.
dxxxy:
Chebyshev + 3 Fourier derivative.
biharmonic:
Calculate the biharmonic (d4/dx4 + d4/dy4 + 2 d2/d2x2y) of a field.
to_physical:
Transforms a field to physical space, onto GL + uniform 2D grid ready for
multiplication.
to_spectral:
Transforms a field to spectral space, onto Chebyshev + Fourier coeff.
Unit Tests:
-------------
test_diff:
tests the differentiation methods.
test_prods:
tests the transform methods for products of fields.
Unit Testing Notes:
-------------------
* There looked like there was a problem with the transformation routines, turns
out that the x grid used for the uniform realspace analytic version was not
quite right - it has to end sligntly before 2pi because kx at 0 = that at 2*pi
so the fft doesn't bother including it.
"""
### MODULES ###
from scipy import *
from numpy.random import rand
from scipy import optimize, linalg, special
import numpy as np
from numpy.fft import fftshift, ifftshift
import matplotlib.pyplot as plt
#from scipy.fftpack import dct as dct
import subprocess
import cPickle as pickle
import h5py
# IF YOU WANT TO UNCOMMENT THE TESTS, YOU WILL NEED THIS PACKAGE FROM MY
# BITBUCKET ACCOUNT
#import TobySpectralMethods as tsm
### FUNCTIONS ###
def set_constants(M=16, N=16,
kx=pi, Ly=2,
Re=400, Wi=1e-5, beta=1.0,
epsJ=1e-6,
dealiasing=False):
"""
returns a dictionary containing the constants to be passed around the
program.
"""
if dealiasing:
Mf = (3*M)/2
Nf = (3*N)/2 + 1
else:
Mf = M
Nf = N
Lx = 2*pi / kx
return {'M':M, 'N':N, 'Mf':Mf, 'Nf':Nf, 'Lx':Lx, 'Ly':Ly,
'kx':kx, 'Re':Re, 'Wi':Wi, 'beta':beta,
'epsJ':epsJ, 'dealiasing':dealiasing}
def mk_diff_x(CNSTS):
"""Make matrix to do fourier differentiation wrt x."""
M = CNSTS['M']
N = CNSTS['N']
kx = CNSTS['kx']
MDX = zeros( ((2*N+1)*M, (2*N+1)*M), dtype='complex')
n = -N
for i in range(0, (2*N+1)*M, M):
MDX[i:i+M, i:i+M] = eye(M, M, dtype='complex')*n*kx*1.j
n += 1
del n, i
return MDX
def single_dy(cSpec, CNSTS):
"""
Efficient (ignoring the slowness of loops) way of computing the Chebyshev
derivative of an array of Chebyshev coefficients
"""
M = CNSTS['M']
Ly = CNSTS['Ly']
out = zeros(M, 'complex')
# Use recurrence relation to calculate each mode in turn, given we know that
# m>M modes must all be zero
# m = M-2 special case
out[M-2] = 2*(M-1)*cSpec[M-1]
for i in range(3, M):
m = M - i
out[m] = out[m+2] + 2*(m+1)*cSpec[m+1]
del i
# apply the normal C function for the zeroth mode
out[0] = 0.5*(out[2] + 2*cSpec[1])
return out
def dy(spec2D, CNSTS):
"""
Orszag's method for doing a y derivative.
"""
M = CNSTS['M']
N = CNSTS['N']
outSpec = zeros((M, 2*N+1), 'complex')
# The highest modes calculated separately.
outSpec[M-1, :] = 0
outSpec[M-2, :] = 2*(M-1)*spec2D[M-1, :]
for i in range(M-3, 0, -1):
outSpec[i, :] = 2*(i+1)*spec2D[i+1, :] + outSpec[i+2, :]
# the m = 0 mode is special - the c function.
outSpec[0, :] = spec2D[1, :] + 0.5*outSpec[2, :]
return outSpec
def dyy(spec2D, CNSTS):
"""
Second Chebyshev direction derivative of a field using Orszag's method
"""
M = CNSTS['M']
N = CNSTS['N']
outSpec = zeros((M, 2*N+1), 'complex')
# The highest modes calculated separately.
outSpec[M-1, :] = 0
outSpec[M-2, :] = 0
p = M-1
outSpec[M-3, :] = (p**3 - p*(M-3)**2) * spec2D[p, :]
#tmpo and tmpe are odd and even sums respectively
if M%2 == 0:
tmp1o = p**3 * spec2D[p, :]
tmp2o = p * spec2D[p, :]
tmp1e = 0.0
tmp2e = 0.0
else:
tmp1o = 0.0
tmp2o = 0.0
tmp1e = p**3 * spec2D[p, :]
tmp2e = p * spec2D[p, :]
for i in range(M-4, -1, -1):
p = i+2
if ((M+i) % 2) != 0:
outSpec[i, :] = p * (p**2 - i**2) * spec2D[p, :] \
+ tmp1o - i**2*tmp2o
tmp1o += p**3 * spec2D[p, :]
tmp2o += p * spec2D[p, :]
else:
outSpec[i, :] = p * (p**2 - i**2) * spec2D[p, :] \
+ tmp1e - i**2*tmp2e
tmp1e += p**3 * spec2D[p, :]
tmp2e += p * spec2D[p, :]
# the m = 0 mode is special - the c function.
outSpec[0, :] = 0.5 * outSpec[0, :]
return outSpec
def d3y(spec2D, CNSTS):
"""
Third Chebyshev direction derivative of a field using Orszag's method
TODO:
Think I can spped this up by repeating what I did in dyy case above
and removing the inner loop using a cumulative sum.
"""
M = CNSTS['M']
N = CNSTS['N']
outSpec = zeros((M, 2*N+1), 'complex')
# the m = 0 mode is special - the c function.
for p in range(3, M, 2):
outSpec[0, :] += 0.125*spec2D[p, :] * ( 2*p**3 * (p**2 - 2*p + 1)
- p * ( (p**2 - 2*p)**2 + 1) )
for i in range(1, M):
for p in range(i + 3, M, 2):
outSpec[i, :] += 0.25*spec2D[p, :] * ( 2*p**3 * (p**2 - 2*p - i**2 + 1)
- p * ( (p**2 - 2*p)**2
- (i**2 - 1)**2 ) )
return outSpec
def d4y(spec2D, CNSTS):
"""
Fourth Chebyshev direction derivative of a field using Orszag's method
"""
M = CNSTS['M']
N = CNSTS['N']
outSpec = zeros((M, 2*N+1), 'complex')
# the m = 0 mode is special - the c function.
tmpCon = 1. / 24.
for p in range(4, M, 2):
outSpec[0, :] += 0.5*tmpCon*spec2D[p, :]*p * (p**2 * (p**2 - 4)**2)
for i in range(1, M):
for p in range(i + 4, M, 2):
outSpec[i, :] += tmpCon*spec2D[p, :]*p * ( p**2 * (p**2 - 4)**2
- 3*i**2*p**4 + 3*i**4*p**2
- i**2 * (i**2 - 4)**2 )
return outSpec
def dx(spec2D, CNSTS):
"""
First Fourier derivative.
NEEDS TO BE CHECKED! SEEMS WRONG for conjugate modes.
"""
N = CNSTS['N']
M = CNSTS['M']
kx = CNSTS['kx']
deriv2D = zeros((M, 2*N+1), 'complex')
deriv2D[:,0] = 0
for n in range(1,N+1):
deriv2D[:,n] = 1.j*n*kx*spec2D[:,n]
deriv2D[:,N+n] = -1.j*(N+1-n)*kx*spec2D[:,N+n]
del n
return deriv2D
def dxx(spec2D, CNSTS):
"""
Second Fourier derivative.
"""
N = CNSTS['N']
M = CNSTS['M']
kx = CNSTS['kx']
deriv2D = zeros((M, 2*N+1), 'complex')
for n in range(N):
deriv2D[:,n] = -(n*kx)**2 * spec2D[:,n]
deriv2D[:,N+1+n] = -((N-n)*kx)**2 * spec2D[:,N+1+n]
del n
return deriv2D
def d3x(spec2D, CNSTS):
"""
Second Fourier derivative.
"""
N = CNSTS['N']
M = CNSTS['M']
kx = CNSTS['kx']
deriv2D = zeros((M, 2*N+1), 'complex')
for n in range(N):
deriv2D[:,n] = -1.j*(n*kx)**3 * spec2D[:,n]
deriv2D[:,N+1+n] = 1.j*((N-n)*kx)**3 * spec2D[:,N+1+n]
del n
return deriv2D
def d4x(spec2D, CNSTS):
"""
Second Fourier derivative.
"""
N = CNSTS['N']
M = CNSTS['M']
kx = CNSTS['kx']
deriv2D = zeros((M, 2*N+1), 'complex')
for n in range(N):
deriv2D[:,n] = (n*kx)**4 * spec2D[:,n]
deriv2D[:,N+1+n] = ((N-n)*kx)**4 * spec2D[:,N+1+n]
del n
deriv2D[:,2*N] = kx**4 * spec2D[:,2*N]
return deriv2D
def dxy():
"""
Chebyshev + Fourier derivative.
"""
pass
def dxyy():
"""
2 Chebyshev + Fourier derivative.
"""
pass
def dxxy():
"""
Chebyshev + 2 Fourier derivative.
"""
pass
def dxyyy():
"""
3 Chebyshev + Fourier derivative.
"""
pass
def dxxyy():
"""
2 Chebyshev + 2 Fourier derivative.
"""
pass
def dxxxy():
"""
Chebyshev + 3 Fourier derivative.
"""
pass
def biharmonic():
"""
Calculate the biharmonic (d4/dx4 + d4/dy4 + 2 d2/d2x2y) of a field.
"""
pass
def to_physical(in2D, CNSTS):
"""
Full 2 dimensional transform from spectral to real space.
First use the Fast fourier transform, then use my Chebyshev transform on
the result in the y direction.
I have attempted to minimize the number of 2D arrays created.
Note: dealiasing removes a third of the effective degrees of freedom. The
true resolution is then much lower than that assumed by N,M this ought to
be fixed in future versions as it will be a huge waste of computation.
"""
M = CNSTS['M']
N = CNSTS['N']
Mf = CNSTS['Mf']
Nf = CNSTS['Nf']
tmp = zeros((M, 2*Nf+1), dtype='complex')
tmp[:,:N+1] = in2D[:,:N+1]
tmp[:,2*Nf+1-N:] = in2D[:,N+1:]
# Perform the FFT across the x and z directions
_realtmp = zeros((2*Mf-2, 2*Nf+1), dtype='double')
out2D = zeros((2*Mf-2, 2*Nf+1), dtype='complex')
out2D[:M, :] = np.fft.fftpack.ifft(tmp, axis=-1)
# test imaginary part of the fft is zero
normImag = linalg.norm(imag(out2D))
if normImag > 1e-12:
print "output of ifft in to_physical is not real, norm = ", normImag
print 'highest x,modes:'
print imag(out2D)[0, N-3:N+1]
_realtmp = real(out2D)
# Perform the Chebyshev transformation across the y direction
# The second half contains the vector on the Gauss-Labatto points excluding
# the first and last elements and in reverse order
# do this before filling out the first half!
_realtmp[Mf:, :] = _realtmp[Mf-2:0:-1, :]
# The first half contains the vector on the Gauss-Labatto points * c_k
_realtmp[0, :] = 2*_realtmp[0, :]
_realtmp[Mf-1, :] = 2*_realtmp[Mf-1, :]
# Perform the transformation
out2D = 0.5*np.fft.fftpack.rfft(_realtmp, axis=0 )
normImag = linalg.norm(imag(out2D[0:M, :]))
if normImag > 1e-12:
print "output after Cheb transform in to_physical is not real, norm = ", normImag
out2D = real(out2D)
return out2D[0:Mf, :] * (2*Nf+1)
def to_physical_2(in2D, CNSTS):
"""
Full 2 dimensional transform from spectral to real space using a single 2D
complex fft.
- PROBABLY MUCH SLOWER:
Parallelism might speed it up a bit, but you need to a full rather
than a real transform in y dir, and you need to do twice the number
of x transforms => 4* the cost. so for N = 20000 it is 8e5 rather
than 2e5 flops. Is that a big enough difference?
- PROBABLY MUCH EASIER TO PROGRAM IN C:
don't know how to plan all the necessary transforms otherwise!
To get both transforms to be forward transforms, need to flip Fourier
modes and renormalise.
Note: dealiasing removes a third of the effective degrees of freedom. The
true resolution is then much lower than that assumed by N,M this ought to
be fixed in future versions as it will be a huge waste of computation.
"""
M = CNSTS['M']
N = CNSTS['N']
Mf = CNSTS['Mf']
Nf = CNSTS['Nf']
# Prepare the field.
out2D = zeros((2*Mf-2, 2*Nf+1), dtype='complex')
# take complex conjugate (because actually want to do the inverse FFT) and
# renormalise because only the ifft does renormalisation for you
# move renormalisation to to_spectral. that way we should be able to keep
# the spectra with the same normalisation as the matrix code.
out2D[:M, 0] = conj(in2D[:,0]) #/ (2*Nf+1)
out2D[:M, 1:N+1] = conj(in2D[:,1:N+1]) #/ (2*Nf+1)
out2D[:M, 2*Nf+1-N:] = conj(in2D[:,N+1:]) #/ (2*Nf+1)
#if CNSTS['dealiasing']:
# out2D[:, 2*N/3 + 1 : 2*N+1 - 2*N/3] = 0
# out2D[2*M/3:, :] = 0
#if CNSTS['dealiasing']:
# out2D[:, 2*N/3 + 2 : 2*N+1 - 2*N/3] = 0
#out2D[:M, :] = conj(np.fft.fftpack.fft(out2D[:M, :], axis=-1))
# The second half contains the vector on the Gauss-Labatto points excluding
# the first and last elements and in reverse order
# do this before filling out the first half!
out2D[2*Mf-M:, :] = out2D[M-2:0:-1, :]
# The first half contains the vector on the Gauss-Labatto points * c_k
out2D[0, :] = 2*out2D[0, :]
out2D[Mf-1, :] = 2*out2D[Mf-1, :]
# Perform the FFT across the x and z directions
out2D = 0.5*np.fft.fftpack.fft2(out2D)
#out2D = real(out2D)
return out2D[0:Mf, :]
def to_physical_3(in2D, CNSTS):
"""
Use the ifft this time.
"""
M = CNSTS['M']
N = CNSTS['N']
Mf = CNSTS['Mf']
Nf = CNSTS['Nf']
# Prepare the field.
out2D = zeros((2*Mf-2, 2*Nf+1), dtype='complex')
scratch2D = zeros((2*Mf-2, 2*Nf+1), dtype='complex')
out2D[:M, 0] = in2D[:,0]
out2D[:M, 1:N+1] = in2D[:,1:N+1]
out2D[:M, 2*Nf+1-N:] = in2D[:,N+1:]
# The second half contains the vector on the Chebyshev modes excluding
# the first and last elements and in reverse order
# do this before filling out the first half!
scratch2D[2*Mf-M:, :] = out2D[M-2:0:-1, :]
# The first half contains the vector on the Chebyshev modes * ck/2
scratch2D[0, :] = 2*out2D[0, :]
scratch2D[1:Mf-1, :] = out2D[1:Mf-1, :]
scratch2D[Mf-1, :] = 2*out2D[Mf-1, :]
# Perform the iFFT across the x and z directions
out2D = 0.5*np.fft.fftpack.ifft2(scratch2D)
#out2D = real(out2D)
return out2D[0:Mf, :] * (2*Mf-2) * (2*Nf+1)
def to_spectral(in2D, CNSTS):
"""
Full 2 dimensional transform from real space to spectral space.
Note: dealiasing removes a third of the effective degrees of freedom. The
true resolution is then much lower than that assumed by N,M this ought to
be fixed in future versions as it seems like a waste of computation in the
derivative and addition steps.
"""
M = CNSTS['M']
N = CNSTS['N']
Mf = CNSTS['Mf']
Nf = CNSTS['Nf']
# Perform the FFT across the x direction
_realtmp = zeros((2*Mf-2, 2*Nf+1), dtype='double')
out2D = zeros((M, 2*N+1), dtype='complex')
# The first half contains the vector on the Gauss-Labatto points
_realtmp[:Mf, :] = real(in2D)
# The second half contains the vector on the Gauss-Labatto points excluding
# the first and last elements and in reverse order
_realtmp[Mf:, :] = _realtmp[Mf-2:0:-1, :]
# Perform the transformation on this temporary vector
# TODO: Think about antialiasing here
_realtmp = np.fft.fftpack.rfft(_realtmp, axis=0)
# Renormalise and divide by c_k to convert to Chebyshev polynomials
_realtmp[0, :] = (0.5/(Mf-1.0))*_realtmp[0, :]
_realtmp[1:Mf-1, :] = (1.0/(Mf-1.0))*_realtmp[1:Mf-1, :]
_realtmp[Mf-1, :] = (0.5/(Mf-1.0))*_realtmp[Mf-1, :]
# test imaginary part of the fft is zero
normImag = linalg.norm(imag(_realtmp))
if normImag > 1e-12:
print "output of cheb transform in to_spectral is not real, norm = ", normImag
print 'highest x, z modes:'
print imag(_realtmp)[0, N-3:N+1]
_realtmp[:Mf, :] = np.fft.fftpack.fft(_realtmp[:Mf, :])
out2D[:, :N+1] = _realtmp[:M, :N+1]
out2D[:, N+1:] = _realtmp[:M, 2*Nf+1-N:]
return out2D / (2*Nf+1)
def to_spectral_2(in2D, CNSTS):
"""
Full 2 dimensional transform from real space to spectral space using single
2D transform.
Note: dealiasing removes a third of the effective degrees of freedom. The
true resolution is then much lower than that assumed by N,M this ought to
be fixed in future versions as it seems like a waste of computation in the
derivative and addition steps.
Bear in mind, fftpack renormalises its transforms but fftw does not. This
means the c code will have an extra factor about the place.
"""
M = CNSTS['M']
N = CNSTS['N']
Mf = CNSTS['Mf']
Nf = CNSTS['Nf']
# Perform the FFT across the x direction
tmp = zeros((2*Mf-2, 2*Nf+1), dtype='double')
out2D = zeros((M, 2*N+1), dtype='complex')
# The first half contains the vector on the Gauss-Labatto points
tmp[:Mf, :] = real(in2D)
# The second half contains the vector on the Gauss-Labatto points excluding
# the first and last elements and in reverse order
tmp[Mf:, :] = tmp[Mf-2:0:-1, :]
# Perform the transformation on this temporary vector
# TODO: Think about antialiasing here
#_realtmp = np.fft.fftpack.rfft(_realtmp, axis=0)
tmp = np.fft.fftpack.fft2(tmp)
## Renormalise and divide by c_k to convert to Chebyshev polynomials
tmp[0, :] = (0.5/(Mf-1.))*tmp[0, :]
tmp[1:Mf-1, :] = (1.0/(Mf-1.))*tmp[1:Mf-1, :]
tmp[Mf-1, :] = (0.5/(Mf-1.))*tmp[Mf-1, :]
## remove the aliased modes and copy into output
out2D[:, :N+1] = tmp[:M, :N+1]
out2D[:, N+1:] = tmp[:M, 2*Nf+1-N:]
#print "is the temp matrix spectrum of real space?"
#print allclose(tmp[:Mf, 1:Nf+1], conj(tmp[:Mf, 2*Nf+1:Nf:-1]))
#print "is the output matrix spectrum of real space?",
#print allclose(out2D[:, 1:N+1], conj(out2D[:M, 2*N+1:N:-1]))
return out2D / (2*Nf+1)
def forward_cheb_transform(GLcmplx, CNSTS):
"""
Use a real FFT to transform a single array from the Gauss-Labatto grid to
Chebyshev polynomials.
Note, this uses a real FFT therefore you must apply the transformations in
the other directions before this one, otherwise you will loose the data from
the imaginary parts.
"""
M = CNSTS['M']
Mf = CNSTS['Mf']
dealiasing = CNSTS['dealiasing']
# Define the temporary vector for the transformation
tmp = zeros(2*Mf-2)
# The first half contains the vector on the Gauss-Labatto points
tmp[:Mf] = real(GLcmplx)
# The second half contains the vector on the Gauss-Labatto points excluding
# the first and last elements and in reverse order
tmp[Mf:] = real(GLcmplx[Mf-2:0:-1])
#savez('tmp.npz', tmp=tmp, consts=CNSTS)
# Perform the transformation on this temporary vector
# TODO: Think about antialiasing here
tmp = real(np.fft.fftpack.rfft(tmp))
out = zeros(M, dtype='complex')
# Renormalise and divide by c_k to convert to Chebyshev polynomials
out[0] = (0.5/(Mf-1.0)) * tmp[0]
out[1:M-1] = (1.0/(Mf-1.0)) * tmp[1:M-1]
if dealiasing:
out[M-1] = (1.0/(Mf-1.0)) * tmp[M-1]
else:
out[M-1] = (0.5/(Mf-1.0)) * tmp[M-1]
# Define the temporary vector for the transformation
tmp = zeros(2*Mf-2)
# The first half contains the vector on the Gauss-Labatto points
tmp[:Mf] = imag(GLcmplx)
# The second half contains the vector on the Gauss-Labatto points excluding
# the first and last elements and in reverse order
tmp[Mf:] = imag(GLcmplx[Mf-2:0:-1])
# Perform the transformation on this temporary vector
tmp = real(np.fft.fftpack.rfft(tmp))
# Renormalise and divide by c_k to convert to Chebyshev polynomials
out[0] += 1.j * (0.5/(Mf-1.0)) * tmp[0]
out[1:M-1] += 1.j * (1.0/(Mf-1.0)) * tmp[1:M-1]
if dealiasing:
out[M-1] += 1.j * (1.0/(Mf-1.0)) * tmp[M-1]
else:
out[M-1] += 1.j * (0.5/(Mf-1.0)) * tmp[M-1]
return out
def backward_cheb_transform(cSpec, CNSTS):
M = CNSTS['M']
Mf = CNSTS['Mf']
out = zeros((Mf), dtype='complex')
_realtmp = zeros((2*Mf-2), dtype='double')
_realtmp[:M] = real(cSpec[:])
# Perform the Chebyshev transformation across the y direction
# The second half contains the vector on the Gauss-Labatto points excluding
# the first and last elements and in reverse order
# do this before filling out the first half!
_realtmp[Mf:] = _realtmp[Mf-2:0:-1]
# The first half contains the vector on the Gauss-Labatto points * c_k
_realtmp[0] = 2*_realtmp[0]
_realtmp[Mf-1] = 2*_realtmp[Mf-1]
# Perform the transformation
#print shape(np.fft.fftpack.rfft(r_[1:5]))
out += 0.5*real(np.fft.fftpack.rfft(_realtmp))
_realtmp[:] = 0.0
_realtmp[:M] = imag(cSpec[:])
# Perform the Chebyshev transformation across the y direction
# The second half contains the vector on the Gauss-Labatto points excluding
# the first and last elements and in reverse order
# do this before filling out the first half!
_realtmp[Mf:] = _realtmp[Mf-2:0:-1]
# The first half contains the vector on the Gauss-Labatto points * c_k
_realtmp[0] = 2*_realtmp[0]
_realtmp[Mf-1] = 2*_realtmp[Mf-1]
# Perform the transformation
out += 0.5*1.j*real(np.fft.fftpack.rfft(_realtmp))
return out[0:Mf]
def backward_cheb_transform_2(cSpec, CNSTS):
"""
Use a DCT to transform a single array of Chebyshev polynomials to the
Gauss-Labatto grid.
"""
# cleverer way, now works!
M = CNSTS['M']
Mf = CNSTS['Mf']
# Define the temporary vector for the transformation
tmp = zeros(Mf)
# The first half contains the vector on the Gauss-Labatto points * c_k
tmp[0] = real(cSpec[0])
tmp[1:M] = 0.5*real(cSpec[1:M])
tmp[Mf-1] = 2*tmp[Mf-1]
out = zeros(Mf, dtype='complex')
out = dct(tmp, type=1).astype('complex')
tmp[0] = imag(cSpec[0])
tmp[1:M] = 0.5*imag(cSpec[1:M])
tmp[Mf-1] = 2*tmp[Mf-1]
out += dct(tmp, type=1) * 1.j
return out[0:Mf]
#
# Mf = CNSTS['Mf']
# M = CNSTS['M']
#
# # Define the temporary vector for the transformation
# tmp = zeros(Mf)
# out = zeros(Mf, dtype='complex')
#
# # The first half contains the vector on the Gauss-Labatto points * c_k
# tmp[0] = real(cSpec[0])
# tmp[1:M] = 0.5*real(cSpec[1:M])
# tmp[Mf-1] = 2*tmp[Mf-1]
#
# # Perform the transformation via a dct
# out[:] = real(dct(tmp, type=1))
#
# # Define the temporary vector for the transformation
# tmp = zeros(Mf)
#
# # The first half contains the vector on the Gauss-Labatto points * c_k
# tmp[0] = imag(cSpec[0])
# tmp[1:M] = 0.5*imag(cSpec[1:M])
# tmp[Mf-1] = 2*tmp[Mf-1]
#
# # Perform the transformation for the imaginary part via a dct
# out += 1.j*real(dct(tmp, type=1))
#
# return out[0:Mf]
def increase_resolution(vec, NOld, MOld, CNSTS):
"""increase resolution from Nold, Mold to N, M and return the higher res
vector"""
N = CNSTS["N"]
M = CNSTS["M"]
highMres = zeros((2*NOld+1)*M, dtype ='complex')
for n in range(2*NOld+1):
highMres[n*M:n*M + MOld] = vec[n*MOld:(n+1)*MOld]
del n
fullres = zeros((2*N+1)*M, dtype='complex')
fullres[(N-NOld)*M:(N-NOld)*M + M*(2*NOld+1)] = highMres[0:M*(2*NOld+1)]
return fullres
def decrease_resolution(vec, NOld, MOld, CNSTS):
"""
decrease both the N and M resolutions
"""
N = CNSTS["N"]
M = CNSTS["M"]
lowMvec = zeros((2*NOld+1)*M, dtype='complex')
for n in range(2*NOld+1):
lowMvec[n*M:(n+1)*M] = vec[n*MOld:n*MOld + M]
del n
lowNMvec = zeros((2*N+1)*M, dtype='D')
lowNMvec = lowMvec[(NOld-N)*M:(NOld-N)*M + (2*N+1)*M]
return lowNMvec
def decide_resolution(vec, NOld, MOld, CNSTS):
"""
Choose to increase or decrease resolution depending on values of N,M
NOld,MOld.
"""
N = CNSTS["N"]
M = CNSTS["M"]
if N >= NOld and M >= MOld:
ovec = increase_resolution(vec, NOld, MOld, CNSTS)
elif N <= NOld and M <= MOld:
ovec = decrease_resolution(vec, NOld, MOld, CNSTS)
return ovec
def stupid_transform(GLreal, CNSTS):
"""
apply the Chebyshev transform the stupid way.
"""
M = CNSTS['M']
Ly = CNSTS['Ly']
out = zeros(M)
for i in range(M):
out[i] += (1./(M-1.))*GLreal[0]
for j in range(1,M-1):
out[i] += (2./(M-1.))*GLreal[j]*cos(pi*i*j/(M-1))
out[i] += (1./(M-1.))*GLreal[M-1]*cos(pi*i)
del i,j
out[0] = out[0]/2.
out[M-1] = out[M-1]/2.
return out
def stupid_transform_i(GLspec, CNSTS):
"""
apply the Chebyshev transform the stupid way.
"""
M = CNSTS['M']
Mf = CNSTS['Mf']
Ly = CNSTS['Ly']
out = zeros(Mf)
for i in range(Mf):
out[i] += GLspec[0]
for j in range(1,M-1):
out[i] += GLspec[j]*cos(pi*i*j/(Mf-1))
out[i] += GLspec[M-1]*cos(pi*i)
del i,j
return out
def load_hdf5_state(filename):
f = h5py.File(filename, "r")
inarr = array(f["psi"])
f.close()
return inarr
#def test_roll_profile(CNSTS):
# """
# Use the roll profile from the SSP to check that differentiation and
# transformation are working correctly.
# """
#
# M = CNSTS['M']
# N = CNSTS['N']
# Mf = CNSTS['Mf']
# Nf = CNSTS['Nf']
# Lx = CNSTS['Lx']
# Ly = CNSTS['Ly']
# kx = CNSTS['kx']
#
# gamma = pi / Ly
# p = optimize.fsolve(lambda p: p*tan(p) + gamma*tanh(gamma), 2)
# oneOverC = ones(M)
# oneOverC[0] = 1. / 2.
#
# V = zeros((M, 2*N+1), dtype = 'complex')
#
# for m in range(0,M,2):
# V[m, 1] = 2*oneOverC[m]*( ((-1)**(m/2))*(special.jv(m,p)/cos(p)) -
# special.iv(m,gamma)/cosh(gamma) )
# V[m, 2*N] = 2*oneOverC[m]*( ((-1)**(m/2))*(special.jv(m,p)/cos(p)) -
# special.iv(m,gamma)/cosh(gamma) )
# del m
#
# Normal = ( cos(p)*cosh(gamma) ) / ( cosh(gamma) - cos(p) )
# V = 0.5 * Normal * V
# actualSpec = V
#
# y_points = cos(pi*arange(Mf)/(Mf-1))
# #x_points = linspace(0, 2.-(2./(2*Nf+1)), 2*Nf+1)
# xlen = 2*pi / kx
# x_points = linspace(0, xlen-(xlen/(2*Nf+1)), 2*Nf+1)
#
# GLreal = zeros((Mf, 2*Nf+1), 'complex')
#