-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostics.f90
4100 lines (2748 loc) · 150 KB
/
diagnostics.f90
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
MODULE diagnostics
USE parameters
USE mpi
USE fft
USE files
USE derivatives
USE special
IMPLICIT NONE
CONTAINS
subroutine diag_zentrum(uk,vk,wk,bk,wak,psik,u_rot) !Would be optimal to import 3 scratch for omega (zxk,zyk,zzk) and 3 others for grad b (txk,tyk,tzk)
!In PV subroutine it would be great to send 3 more scratch arrays for grad(b)
double complex, dimension(iktx,ikty,n3h2) :: uk,vk,wk,bk
double complex, dimension(iktx,ikty,n3h1) :: psik
double complex, dimension(iktx,ikty,n3h1) :: u_rot,v_rot,b_rot,wak
double complex, dimension(iktx,ikty,n3h1) :: zxk,zyk,zzk !From scratch arrays?
double precision, dimension(n1d,n2d,n3h1) :: zxr,zyr,zzr
real, dimension(n3h0) :: ks,ku,ps,ps_quad !Staggered (s) and unstaggered (u) energy (k: kinetic, p: potential)
real :: ktot_p,ktot,ptot_p,ptot,ptot_quad,ptot_quad_p
real, dimension(n3h0) :: ks_rot,pu_rot !Staggered (s) and unstaggered (u) energy (k: kinetic, p: potential)
real :: ktot_rot_p,ktot_rot,ptot_rot_p,ptot_rot
equivalence(zxk,zxr)
equivalence(zyk,zyr)
equivalence(zzk,zzr)
!Compute velocity/buoyancy fields
do izh1=1,n3h1
do iky=1,ikty
ky = kya(iky)
do ikx=1,iktx
kx = kxa(ikx)
if (L(ikx,iky).eq.1) then
u_rot(ikx,iky,izh1) = - i*ky*psik(ikx,iky,izh1)
v_rot(ikx,iky,izh1) = i*kx*psik(ikx,iky,izh1)
b_rot(ikx,iky,izh1) = ( psik(ikx,iky,izh1+1) - psik(ikx,iky,izh1) )/(r_1(izh2)*dz) !1/r_1 d psi/dz
else
u_rot(ikx,iky,izh1) = (0.D0,0.D0)
v_rot(ikx,iky,izh1) = (0.D0,0.D0)
b_rot(ikx,iky,izh1) = (0.D0,0.D0)
endif
enddo
enddo
enddo
!Explicit treatment of boundaries: in QG, uz=vz=0 => b=0 at the top
if(mype==(npe-1)) then
do iky=1,ikty
do ikx=1,iktx
b_rot(ikx,iky,iztop1) = (0.D0,0.D0)
end do
end do
end if
!Compute k_H spectra at various heights!
!**************************************!
do iz=1,num_spec
if(out_hspec ==1 .and. mod(iter,freq_hspec) ==0) call hspec(uk,vk,wk,bk,u_rot,v_rot,b_rot,iz)
end do
!Compute vorticity for other routines!
!************************************!
! if((out_ens ==1 .and. mod(iter,freq_ens) ==0) .or. (out_pv ==1 .and. mod(iter,freq_pv) ==0) .or. (iter==0 .and. ( out_ens==1 .or. out_pv==1) ) ) then
! call vort(uk,vk,wk,zxk,zyk,zzk)
! if(out_ens ==1 .and. ( mod(iter,freq_ens)==0 .or. iter==0 )) call enstrophy(zxk,zyk,zzk)
! end if
!Conditions of integrability!
!***************************!
if(out_cond ==1 .and. (mod(iter,freq_cond ) ==0 .or. iter==0 )) call cond_integrability(uk,vk,wk,bk)
if(out_grow ==1 .and. (mod(iter,freq_grow ) ==0 .or. iter==0 )) call compute_growth(uk,vk,bk)
if(out_condwz==1 .and. (mod(iter,freq_condwz) ==0)) call cond_wz(wak)
!Vertical scale of buoyancy and vertical velocity!
!************************************************!
if(out_vbuoy ==1 .and. (mod(iter,freq_vbuoy) ==0)) call buoy_vert_scale(wk,bk) !For full w and staggered buoyancy
if(out_vbuoyr==1 .and. (mod(iter,freq_vbuoyr) ==0)) then
call generate_halo_q(b_rot)
call buoy_vert_scale_rot(wak,b_rot) !For ageo w and unstaggered QG buoyancy
end if
!Integrate over k_H to plot fields as a function of z!
!****************************************************!
! 1/(2pi^)^2 int(int( e^2(x,y,z)/2 dx)dy = 1/2 sum over kx ky |eh|^2(z)
ks=0.
ku=0.
ps=0.
ps_quad=0.
ktot_p=0.
ktot=0.
ptot_p=0.
ptot=0.
ptot_quad_p=0.
ptot_quad=0.
ks_rot=0.
pu_rot=0.
ktot_rot_p=0.
ktot_rot=0.
ptot_rot_p=0.
ptot_rot=0.
!With dealiasing, sum_k 1/2 |u(kx,ky,z)|^2 = sum_k L |u|^2 - 0.5 |u(0,0,z)|^2
do iz=1,n3h0
izh1=iz+1
izh2=iz+2
do iky=1,ikty
do ikx=1,iktx
if(L(ikx,iky)==1) then
ks(iz) = ks(iz) + real( uk(ikx,iky,izh2)*CONJG( uk(ikx,iky,izh2) ) + vk(ikx,iky,izh2)*CONJG( vk(ikx,iky,izh2) ) )
ku(iz) = ku(iz) + real( wk(ikx,iky,izh2)*CONJG( wk(ikx,iky,izh2) ) )*Ar2
ps_quad(iz) = ps_quad(iz) + real( bk(ikx,iky,izh2)*CONJG( bk(ikx,iky,izh2) ) )*(big_F*r_1s(izh2)/r_2s(izh2))
ks_rot(iz) = ks_rot(iz) + real( u_rot(ikx,iky,izh1)*CONJG( u_rot(ikx,iky,izh1) ) + v_rot(ikx,iky,izh1)*CONJG( v_rot(ikx,iky,izh1) ) )
pu_rot(iz) = pu_rot(iz) + real( b_rot(ikx,iky,izh1)*CONJG( b_rot(ikx,iky,izh1) ) )*(big_F*r_1(izh2)/r_2(izh2))
end if
enddo
enddo
ps(iz) = -zash0(iz)*real(bk(1,1,izh2))/Ro
!See diary Feb 10th 2014 or journal Oct 22nd 2013. Adjust sum to 2D dialiasing: substract half the kh=0 mode.
ks(iz) = ks(iz) - 0.5*real( uk(1,1,izh2)*CONJG( uk(1,1,izh2) ) + vk(1,1,izh2)*CONJG( vk(1,1,izh2) ) )
ku(iz) = ku(iz) - 0.5*real( wk(1,1,izh2)*CONJG( wk(1,1,izh2) ) )*Ar2
ps_quad(iz) = ps_quad(iz) - 0.5*real( bk(1,1,izh2)*CONJG( bk(1,1,izh2) ) )*(big_F*r_1s(izh2)/r_2s(izh2))
ks_rot(iz) = ks_rot(iz) - 0.5*real( u_rot(1,1,izh1)*CONJG( u_rot(1,1,izh1) ) + v_rot(1,1,izh1)*CONJG( v_rot(1,1,izh1) ) )
pu_rot(iz) = pu_rot(iz) - 0.5*real( b_rot(1,1,izh1)*CONJG( b_rot(1,1,izh1) ) )*(big_F*r_1(izh2)/r_2(izh2))
end do
!If desired, we can plot energy as a function of z and also the vertical energy spectrum
if(out_ez ==1 .and. ( mod(iter,freq_ez)==0 .or. iter == 0)) call plot_ez(ks,ku,ps)
if(out_rotz ==1 .and. ( mod(iter,freq_rotz)==0 .or. iter==0)) call plot_rotz(ks_rot,pu_rot)
! if(out_specz ==1 .and. ( mod(iter,freq_specz)==0 .or. iter == 0)) call specz(ks,ku,pu)
!Compute the total energy by integrating over z!
!**********************************************!
!1/L int(e(z) dz) becomes just a dummy sum over all grid points without interpolation needed...
!First some locally to each processor
do izh0=1,n3h0
izh2=izh0+2
ktot_p =ktot_p + rho_s(izh2)*ks(izh0) + rho_u(izh2)*ku(izh0)
ptot_p =ptot_p + rho_s(izh2)*ps(izh0)
ptot_quad_p=ptot_quad_p + rho_s(izh2)*ps_quad(izh0)
ktot_rot_p=ktot_rot_p + rho_s(izh2)*ks_rot(izh0)
ptot_rot_p=ptot_rot_p + rho_u(izh2)*pu_rot(izh0)
end do
ktot_p =ktot_p /n3
ptot_p =ptot_p /n3
ptot_quad_p=ptot_quad_p/n3
ktot_rot_p=ktot_rot_p/n3
ptot_rot_p=ptot_rot_p/n3
!Sum results from each processor
call mpi_reduce(ktot_p ,ktot , 1,MPI_REAL, MPI_SUM,0,MPI_COMM_WORLD,ierror)
call mpi_reduce(ptot_p ,ptot , 1,MPI_REAL, MPI_SUM,0,MPI_COMM_WORLD,ierror)
call mpi_reduce(ptot_quad_p,ptot_quad, 1,MPI_REAL, MPI_SUM,0,MPI_COMM_WORLD,ierror)
call mpi_reduce(ktot_rot_p,ktot_rot, 1,MPI_REAL, MPI_SUM,0,MPI_COMM_WORLD,ierror)
call mpi_reduce(ptot_rot_p,ptot_rot, 1,MPI_REAL, MPI_SUM,0,MPI_COMM_WORLD,ierror)
if(mype==0) write(unit=unit_energy ,fmt=*) time,ktot,ptot,ptot_quad
if(mype==0) write(unit=unit_energyr,fmt=*) time,ktot_rot,ptot_rot
end subroutine diag_zentrum
subroutine normalize_trop(uk,vk,wk,bk,psik,qk,wak) !This subroutine normalizes fields with the RMS horizontal velocity at the tropopause
!In PV subroutine it would be great to send 3 more scratch arrays for grad(b)
double complex, dimension(iktx,ikty,n3h2) :: uk,vk,wk,bk
double complex, dimension(iktx,ikty,n3h1) :: wak,psik,qk
real :: horu !Initial U_rms
real :: norm !Norm with which we normalize to get the new rms velo to be URMS.
integer :: which_mype !Which mype contains the level we normalize
integer :: iz0,proc
!Find out which mype contains the grid point on which to compute U_RMS: all processors must know which it is for future broadcast
do proc=1,npe
if( trop_height > (proc-1)*n3h0 .AND. trop_height <= proc*n3h0 ) which_mype=proc-1
end do
if(mype==which_mype) then
iz0=trop_height - mype*n3h0 + 2 !2-halo velocity fields
!Compute U_RMS
horu=0.
do iky=1,ikty
do ikx=1,iktx
if(L(ikx,iky)==1) then
horu = horu + real( uk(ikx,iky,iz0)*CONJG( uk(ikx,iky,iz0) ) + vk(ikx,iky,iz0)*CONJG( vk(ikx,iky,iz0) ) )
end if
enddo
enddo
!See diary Feb 10th 2014 or journal Oct 22nd 2013. Adjust sum to 2D dialiasing: substract half the kh=0 mode.
horu = horu - 0.5*real( uk(1,1,iz0)*CONJG( uk(1,1,iz0) ) + vk(1,1,iz0)*CONJG( vk(1,1,iz0) ) )
horu = sqrt(horu)
norm = horu/URMS
end if
!Broadcast the norm to all mypes
call mpi_bcast(norm,1,MPI_REAL,which_mype,MPI_COMM_WORLD,ierror)
!Not just normalize everybody
uk=uk/norm
vk=vk/norm
wk=wk/norm
bk=bk/norm
psik=psik/norm
qk=qk/norm
wak=wak/norm
end subroutine normalize_trop
subroutine hspec(uk,vk,wk,bk,u_rot,v_rot,b_rot,level)
double complex, dimension(iktx,ikty,n3h2) :: uk,vk,wk,bk
double complex, dimension(iktx,ikty,n3h1) :: u_rot,v_rot,b_rot
integer :: level,proc,izh1s,izh2s
real, dimension(0:ktx,2) :: spz !spectrum for kinetic (1) and potential (2)
real, dimension(0:ktx) :: num_modes !number of modes for a given kh
integer :: mode !Mode no
integer :: unit,unit_ro
double precision :: kh
!To compute Ro_macro: compute the average U_peak=sqrt(u^2+v^2) and L_peak, the scale at which energy peaks for each height (bot mid top)
real, dimension(0:ktx) :: U_peak_mode !u^2 + v^2 at each kh mode.
real :: U_peak !U_peak_mode summed over all modes
real :: L_peak !Actual energy bearing scale
real :: L_peak_mem !Actual energy bearing scale (recalls the max result before regression, incase regression failed)
real :: max_L_peak !Value of the spectrum at a given height (temp variable)
double precision :: vx(npt),vy(npt),poly(3)
!Use the concerned mype
if(height(level)<1 .or. height(level)>n3-1) write(*,*) "Problem with level in horizontal_spectrum" !don't include n3, otherwise need a special treatment of boundary conditions...
proc = (height(level)-1)/n3h0 !which processor
izh1s = height(level) - proc*n3h0 + 1 !position in the processor (valid for n3h2 fields only)
izh2s = height(level) - proc*n3h0 + 2 !position in the processor (valid for n3h1 fields only)
if(mype==proc) then
!Which file to write on
if(level==1) then
unit =unit_h0
unit_ro=unit_ro0
elseif(level==2) then
unit =unit_h1
unit_ro=unit_ro1
elseif(level==3) then
unit =unit_h2
unit_ro=unit_ro2
elseif(level==4) then
unit =unit_h3
unit_ro=unit_ro3
elseif(level==5) then
unit =unit_h4
unit_ro=unit_ro4
elseif(level==6) then
unit =unit_h5
unit_ro=unit_ro5
elseif(level==7) then
unit =unit_h6
unit_ro=unit_ro6
elseif(level==8) then
unit =unit_h7
unit_ro=unit_ro7
elseif(level==9) then
unit =unit_h8
unit_ro=unit_ro8
elseif(level==10) then
unit =unit_h9
unit_ro=unit_ro9
else
write(*,*) "Problem with level... can't find the file..."
end if
! if(level==top_height) then
! unit=unit_htop
! unit_ro=unit_ro_top
! else if(level==mid_height) then
! unit=unit_hmid
! unit_ro=unit_ro_mid
! else if(level==bot_height) then
! unit=unit_hbot
! unit_ro=unit_ro_bot
! else
! write(*,*) "Problem with level... can't find the file..."
! end if
spz=0.
num_modes=0.
U_peak=0.
do iky=1,ikty
ky = kya(iky)
do ikx=1,iktx
kx = kxa(ikx)
kh2 = kx*kx+ky*ky
kh = sqrt(1.D0*kh2)
mode = ifix(real(kh*L1/twopi+0.5))
if (L(ikx,iky).eq.1) then
spz(mode,1) = spz(mode,1) + real( uk(ikx,iky,izh2s)*CONJG(uk(ikx,iky,izh2s)) ) !u on staggered grid
spz(mode,1) = spz(mode,1) + real( vk(ikx,iky,izh2s)*CONJG(vk(ikx,iky,izh2s)) ) !v on staggered grid
U_peak = U_peak + spz(mode,1)
spz(mode,1) = spz(mode,1) + 0.5*Ar2*( real( wk(ikx,iky,izh2s)*CONJG(wk(ikx,iky,izh2s)) ) + real( wk(ikx,iky,izh2s-1)*CONJG(wk(ikx,iky,izh2s-1)) ) ) !w interpol
! spz(mode,2) = spz(mode,2) + 0.5*( real( tk(ikx,iky,izh2s)*CONJG(tk(ikx,iky,izh2s)) ) + real( tk(ikx,iky,izh2s-1)*CONJG(tk(ikx,iky,izh2s-1)) ) )*(big_F*r_1s(izh2s)/r_2s(izh2s)) !t interpol
spz(mode,2) = spz(mode,2) + real( bk(ikx,iky,izh2s)*CONJG(bk(ikx,iky,izh2s)) )*(big_F*r_1s(izh2s)/r_2s(izh2s)) !b is not interpolated (avail at stag points
num_modes(mode) = num_modes(mode) + 2
endif
enddo
enddo
!Finish computing U_peak and L_peak, then print Ro_macro
U_peak = U_peak - 0.5*( real( uk(1,1,izh2s)*CONJG(uk(1,1,izh2s))) + real( vk(1,1,izh2s)*CONJG(vk(1,1,izh2s)) )) !To be very precise, substract half of kh=0 mode...
U_peak = sqrt(U_peak) !Since sum_kh L |u|^2 - 0.5* |u(kh=0)|^2 = 0.5*sum |u|^2
!Method 1: easy but leading to discontinuity in L_peak
L_peak=0.
max_L_peak=0.
do mode=0,ktx-1
if(spz(mode,1)>max_L_peak) then
max_L_peak=spz(mode,1)
L_peak=mode
end if
end do
if(parabolic_L_peak==1) then
!Fits parabola with npt points.
L_peak_mem=L_peak
if(L_peak<((npt-1)/2+1)) then
do mode=1,npt
vx(mode)=mode
end do
elseif(L_peak>=((npt-1)/2+1) ) then
do mode=1,npt
vx(mode)=L_peak-((npt-1)/2+1)+mode
end do
end if
do mode=1,npt
vy(mode)=spz(vx(mode),1)
end do
poly = polyfit(vx,vy,2)
L_peak = -poly(2)/(2.*poly(3)) !Maximum of the parabola a + bx + cx^2 is x_max = -b/2c
!Make sure the value is sensible, that is, it is contained within L_peak_mem +/- 1
if(L_peak>L_peak_mem+((npt-1)/2) .or. L_peak<L_peak_mem-((npt-1)/2)) L_peak=L_peak_mem
end if
L_peak=L1/L_peak !L_peak = 2pi/mode for which spectrum(mode) is max)
!Print Ro_macro = Ro*U_peak/L_peak
write(unit_ro,fmt=*) time,Ro*U_peak/L_peak,U_peak,L_peak
spz=0.5*rho_s(izh2s)*spz
do mode=0,ktx-1
if (num_modes(mode).ne.0) then !mode, kin energy, pot energy, num of modes
write(unit,fmt=*) float(mode),spz(mode,1),spz(mode,2),num_modes(mode) !There was a problem here.[r_1/r_2 multiplied a second time to spz...]
endif
enddo
write(unit,*) ' '
call flush(unit)
if(out_hg == 1) then
!Now, plot the rot/div spectrum
!******************************
!Which file to write on
if(level==1) then
unit =unit_hg0
elseif(level==2) then
unit =unit_hg1
elseif(level==3) then
unit =unit_hg2
elseif(level==4) then
unit =unit_hg3
elseif(level==5) then
unit =unit_hg4
elseif(level==6) then
unit =unit_hg5
elseif(level==7) then
unit =unit_hg6
elseif(level==8) then
unit =unit_hg7
elseif(level==9) then
unit =unit_hg8
elseif(level==10) then
unit =unit_hg9
else
write(*,*) "Problem with level... can't find the file..."
end if
! if(level==top_height) then
! unit=unit_htopg
! else if(level==mid_height) then
! unit=unit_hmidg
! else if(level==bot_height) then
! unit=unit_hbotg
! else
! write(*,*) "Problem with level... can't find the file..."
! end if
spz=0.
num_modes=0.
do iky=1,ikty
ky = kya(iky)
do ikx=1,iktx
kx = kxa(ikx)
kh2 = kx*kx+ky*ky
kh = sqrt(1.D0*kh2)
mode = ifix(real(kh*L1/twopi+0.5))
if (L(ikx,iky).eq.1) then
!Assuming energy_rot goes like u_r^2 + v_r^2 + b_r^2
spz(mode,1) = spz(mode,1) + real( u_rot(ikx,iky,izh1s)*CONJG(u_rot(ikx,iky,izh1s)) ) !u on staggered grid
spz(mode,1) = spz(mode,1) + real( v_rot(ikx,iky,izh1s)*CONJG(v_rot(ikx,iky,izh1s)) ) !v on staggered grid
! spz(mode,1) = spz(mode,1) + 0.5*( real( b_rot(ikx,iky,izh1s)*CONJG(b_rot(ikx,iky,izh1s)) ) + real( b_rot(ikx,iky,izh1s-1)*CONJG(b_rot(ikx,iky,izh1s-1)) ) )*(big_F*r_1s(izh2s)/r_2s(izh2s))
!big_F was forgotten in the line above
!Assuming energy_div goes like u_d^2 + v_d^2 + w_d^2 + b_d^2
spz(mode,2) = spz(mode,2) + real( (uk(ikx,iky,izh2s) - u_rot(ikx,iky,izh1s))*CONJG( uk(ikx,iky,izh2s) - u_rot(ikx,iky,izh1s) ) ) !u on staggered grid
spz(mode,2) = spz(mode,2) + real( (vk(ikx,iky,izh2s) - v_rot(ikx,iky,izh1s))*CONJG( vk(ikx,iky,izh2s) - v_rot(ikx,iky,izh1s) ) ) !v on staggered grid
spz(mode,2) = spz(mode,2) + 0.5*( real( wk(ikx,iky,izh2s)*CONJG(wk(ikx,iky,izh2s)) ) + real( wk(ikx,iky,izh2s-1)*CONJG(wk(ikx,iky,izh2s-1)) ) )*Ar2 !w_div=wk
! spz(mode,2) = spz(mode,2) + 0.5*( real(( tk(ikx,iky,izh2s) - b_rot(ikx,iky,izh1s) )*CONJG( tk(ikx,iky,izh2s) - b_rot(ikx,iky,izh1s) ) ) + real((tk(ikx,iky,izh2s-1) - b_rot(ikx,iky,izh1s-1) )*CONJG( tk(ikx,iky,izh2s-1) - b_rot(ikx,iky,izh1s-1) ) ) )*(big_F*r_1s(izh2s)/r_2s(izh2s)) !t interpolated
num_modes(mode) = num_modes(mode) + 2
endif
enddo
enddo
spz=0.5*rho_s(izh2s)*spz
do mode=0,ktx-1
if (num_modes(mode).ne.0) then !mode, rot kinetic energy, div kinetic energy
write(unit,fmt=*) float(mode),spz(mode,1),spz(mode,2),num_modes(mode)
endif
enddo
write(unit,*) ' '
call flush(unit)
end if! if(out_hg ==1)
end if
end subroutine hspec
SUBROUTINE enstrophy(zxk,zyk,zzk)
!This subroutine computes enstrophy cheaply without any form of interpolation.
!It uses the mid-point (or rectangle rule) for the staggered fields (omega_3)
!And the trapezoidal rule for omega_1,2. There is therefore no interpolation needed.
!Ens = 0.5 * int( omega^2 ) dV
!DIMENSIONAL FORM
!We also use the occasion to compute "small-scale" Ro and Fr numbers, defined as
!Ro_micro = sqrt{<omega_z^2>}/f
!Fr_micro = sqrt{<omega_H^2>}/N
!so that
!Ens = 0.5*[ (f*Ro)^2 + (N*Fr)^2 ]
!NONDIMENSIONAL FORM:
!We also use the occasion to compute "small-scale" Ro and Fr numbers, defined as
!Ro_micro = Ro_macro*sqrt{<omega_z^2>}
!Fr_micro = Fr_macro*sqrt{<omega_H^2>}
!so that
!Ens = 0.5*(U/H)^2 *[<omega_H^2> + Ar2*<omega_z^2> ]
!\___/!
! we omit this prop constant for now !
double complex, dimension(iktx,ikty,n3h1) :: zxk,zyk,zzk
real, dimension(n3h0) :: ws,wu !Staggered (s) and unstaggered (u) omega^2
real :: Ro_micro,Fr_micro,Ens,ws_p,wu_p,ws_tot,wu_tot
!Let's first sum over k_h to get a function of z alone.
! 1/(2pi^)^2 int(int( w^2(x,y,z)/2 dx)dy = 1/2 sum over kx ky |wh|^2(z)
ws=0.
wu=0.
ws_p=0.
wu_p=0.
do iz=1,n3h0
izh1=iz+1
do iky=1,ikty
ky = kya(iky)
do ikx=1,iktx
kx = kxa(ikx)
if(L(ikx,iky)==1) then
wu(iz) = wu(iz) + 2.*real( zxk(ikx,iky,izh1)*CONJG( zxk(ikx,iky,izh1) ) + zyk(ikx,iky,izh1)*CONJG( zyk(ikx,iky,izh1) ) )
ws(iz) = ws(iz) + 2.*real( zzk(ikx,iky,izh1)*CONJG( zzk(ikx,iky,izh1) ) )
! wu(iz) = wu(iz) + real( ( i*ky*wk(ikx,iky,izh2) - (vk(ikx,iky,izh2+1)-vk(ikx,iky,izh2))/dz )*CONJG( i*ky*wk(ikx,iky,izh2) - (vk(ikx,iky,izh2+1)-vk(ikx,iky,izh2))/dz ) )
! wu(iz) = wu(iz) + real( ( (uk(ikx,iky,izh2+1)-uk(ikx,iky,izh2))/dz - i*kx*wk(ikx,iky,izh2) )*CONJG( (uk(ikx,iky,izh2+1)-uk(ikx,iky,izh2))/dz - i*kx*wk(ikx,iky,izh2) ) )
! ws(iz) = ws(iz) + real( ( i*kx*vk(ikx,iky,izh2) - i*ky*uk(ikx,iky,izh2) )*CONJG( i*kx*vk(ikx,iky,izh2) - i*ky*uk(ikx,iky,izh2) ) )
end if
enddo
enddo
!See diary Feb 10th 2014 or journal Oct 22nd 2013. Adjust sum to 2D dialiasing: substract half the kh=0 mode.
wu(iz) = wu(iz) - real( zxk(1,1,izh1)*CONJG( zxk(1,1,izh1) ) + zyk(1,1,izh1)*CONJG( zyk(1,1,izh1) ) )
! wu(iz) = wu(iz) - 0.5*real( ( - (vk(1,1,izh2)-vk(1,1,izh2-1))/dz )*CONJG(- (vk(1,1,izh2)-vk(1,1,izh2-1))/dz ) )
! wu(iz) = wu(iz) - 0.5*real( ( (uk(1,1,izh2)-uk(1,1,izh2-1))/dz )*CONJG( (uk(1,1,izh2)-uk(1,1,izh2-1))/dz ) )
end do
!Explicit treatment of boundary conditions
if(mype==(npe-1)) wu(n3h0) = (0.D0,0.D0)
!Plot as a function of z if desired!
!**********************************!
call plot_ensz(ws,wu)
!1/L int(e(z) dz) becomes just a dummy sum over all grid points without interpolation needed...
!First some locally to each processor
do izh0=1,n3h0
ws_p=ws_p + ws(izh0)
wu_p=wu_p + wu(izh0)
end do
ws_p=ws_p/N3
wu_p=wu_p/N3
!Sum results from each processor
call mpi_reduce(ws_p,ws_tot, 1,MPI_REAL, MPI_SUM,0,MPI_COMM_WORLD,ierror)
call mpi_reduce(wu_p,wu_tot, 1,MPI_REAL, MPI_SUM,0,MPI_COMM_WORLD,ierror)
!This is now just <omega_X^2>. Now get the desired dimensional values.
Ro_micro = Ro*sqrt(ws_tot)
Fr_micro = Fr*sqrt(wu_tot)
Ens = 0.5*(ws_tot + Ar2*wu_tot) !There is a (U/H)^2 constant missing for right scaling.
if(mype==0) write(unit=unit_ens,fmt=*) time,Ens,Ro_micro,Fr_micro
END SUBROUTINE enstrophy
!************************************************************************!
!!!! Subroutines to plot z-dependent horizontally averaged quantities !!!!
!************************************************************************!
SUBROUTINE plot_ez(ks,ku,ps) !Now plots z, HOR KIN, WRMS and BRMS
real, dimension(n3h0) :: ks,ku,ps !Staggered (s) and unstaggered (u) energy
real, dimension(n3h0) :: ks_r,ku_r,ps_r !Copies of eu and es to keep es and eu valid for energy subroutine.
real,dimension(n3) :: kuz,ksz,psz !energy(z) for entire domain
integer :: processor,izp,nrec
ksz=0.
kuz=0.
psz=0.
!Send err_p from other processors to mype = 0
if(mype>0) call mpi_send(ks,n3h0,MPI_REAL,0,tag_kzs,MPI_COMM_WORLD,ierror)
if(mype>0) call mpi_send(ku,n3h0,MPI_REAL,0,tag_kzu,MPI_COMM_WORLD,ierror)
if(mype>0) call mpi_send(ps,n3h0,MPI_REAL,0,tag_pzs,MPI_COMM_WORLD,ierror)
if(mype==0) then
!Copy mype==0 part onto err
do iz=1,n3h0
ksz(iz) = ks(iz)
kuz(iz) = ku(iz)
psz(iz) = ps(iz)
end do
!Receive other parts from other processors
do nrec=1,npe-1
call mpi_recv(ks_r,n3h0,MPI_REAL,MPI_ANY_SOURCE,tag_kzs,MPI_COMM_WORLD,status,ierror)
!Copy received share onto err
processor=status(MPI_SOURCE)
!Add the awaiting half level
do iz=1,n3h0
izp=processor*n3h0+iz
ksz(izp) = ksz(izp) + ks_r(iz)
end do
!Kinetic unstag energy part
call mpi_recv(ku_r,n3h0,MPI_REAL,MPI_ANY_SOURCE,tag_kzu,MPI_COMM_WORLD,status,ierror)
!Copy received share onto err
processor=status(MPI_SOURCE)
!Add the awaiting half level
do iz=1,n3h0
izp=processor*n3h0+iz
kuz(izp) = kuz(izp) + ku_r(iz)
end do
!Potential unstag energy part
call mpi_recv(ps_r,n3h0,MPI_REAL,MPI_ANY_SOURCE,tag_pzs,MPI_COMM_WORLD,status,ierror)
!Copy received share onto err
processor=status(MPI_SOURCE)
!Add the awaiting half level
do iz=1,n3h0
izp=processor*n3h0+iz
psz(izp) = psz(izp) + ps_r(iz)
end do
end do
!PLOT!
do iz=1,n3
write(unit_ez,fmt=*) iz,ksz(iz),kuz(iz),psz(iz) !iz,URMS, WRMS, BRMS
enddo
write(unit_ez,*) ' '
call flush(unit_ez)
end if
END SUBROUTINE plot_ez
SUBROUTINE plot_rotz(ks,pu)
real, dimension(n3h0) :: ks,pu !Staggered (s) and unstaggered (u) energy
real, dimension(n3h0) :: ks_r,pu_r !Copies of eu and es to keep es and eu valid for energy subroutine.
real,dimension(n3) :: kz,pz !energy(z) for entire domain
integer :: processor,izp,nrec
kz=0.
pz=0.
!Send err_p from other processors to mype = 0
if(mype>0) call mpi_send(ks,n3h0,MPI_REAL,0,tag_rzs,MPI_COMM_WORLD,ierror)
if(mype>0) call mpi_send(pu,n3h0,MPI_REAL,0,tag_rzu,MPI_COMM_WORLD,ierror)
if(mype==0) then
!Copy mype==0 part onto err
kz(1) = ks(1)
pz(1) = + 0.5*pu(1)
do iz=2,n3h0
kz(iz) = ks(iz)
pz(iz) = + 0.5*(pu(iz) + pu(iz-1))
end do
pz(n3h0+1) = 0.5*pu(n3h0)
!Receive other parts from other processors
do nrec=1,npe-1
call mpi_recv(ks_r,n3h0,MPI_REAL,MPI_ANY_SOURCE,tag_rzs,MPI_COMM_WORLD,status,ierror)
!Copy received share onto err
processor=status(MPI_SOURCE)
!Add the awaiting half level
do iz=1,n3h0
izp=processor*n3h0+iz
kz(izp) = kz(izp) + ks_r(iz)
end do
!Potential unstag energy part
call mpi_recv(pu_r,n3h0,MPI_REAL,MPI_ANY_SOURCE,tag_rzu,MPI_COMM_WORLD,status,ierror)
!Copy received share onto err
processor=status(MPI_SOURCE)
!Add the awaiting half level !except for last mype
if(processor<(npe-1)) pz((processor+1)*n3h0+1)=pz((processor+1)*n3h0+1) + 0.5*pu_r(n3h0)
!iz=1 case
izp=processor*n3h0+1
pz(izp) = pz(izp) + 0.5* pu_r(1)
do iz=2,n3h0
izp=processor*n3h0+iz
pz(izp) = pz(izp) + 0.5*( pu_r(iz) + pu_r(iz-1) )
end do
end do
!PLOT!
do iz=1,n3
write(unit_rotz,fmt=*) zas(iz),rho_st(iz)*kz(iz),rho_st(iz)*pz(iz)!,rho_st(iz)*(kz(iz)+pz(iz))
enddo
write(unit_rotz,*) ' '
call flush(unit_rotz)
end if
END SUBROUTINE plot_rotz
SUBROUTINE plot_ensz(ks,ku)
real, dimension(n3h0) :: ks,ku !Staggered (s) and unstaggered (u) parts of enstrophy
real, dimension(n3h0) :: ks_r,ku_r !Copies of wu and ws to keep ws and wu valid for enstrophy subroutine.
real,dimension(n3) :: kz !enstrophy(z) for entire domain
integer :: processor,izp,nrec
kz=0.
!Send err_p from other processors to mype = 0 \
if(mype>0) call mpi_send(ks,n3h0,MPI_REAL,0,tag_ezs,MPI_COMM_WORLD,ierror)
if(mype>0) call mpi_send(ku,n3h0,MPI_REAL,0,tag_ezu,MPI_COMM_WORLD,ierror)
if(mype==0) then
!Copy mype==0 part onto err \
kz(1) = ks(1) + 0.5*ku(1)
do iz=2,n3h0
kz(iz) = ks(iz) + 0.5*(ku(iz) + ku(iz-1))
end do
kz(n3h0+1) = 0.5*ku(n3h0)
!Receive other parts from other processors
do nrec=1,npe-1
call mpi_recv(ks_r,n3h0,MPI_REAL,MPI_ANY_SOURCE,tag_ezs,MPI_COMM_WORLD,status,ierror)
!Copy received share onto err
processor=status(MPI_SOURCE)
!Add the awaiting half level
do iz=1,n3h0
izp=processor*n3h0+iz
kz(izp) = kz(izp) + ks_r(iz)
end do
!Kinetic unstag energy part
call mpi_recv(ku_r,n3h0,MPI_REAL,MPI_ANY_SOURCE,tag_ezu,MPI_COMM_WORLD,status,ierror)
!Copy received share onto err
processor=status(MPI_SOURCE)
!Add the awaiting half level !except for last mype
if(processor<(npe-1)) kz((processor+1)*n3h0+1)=kz((processor+1)*n3h0+1) + 0.5*ku_r(n3h0)
!iz=1 case
izp=processor*n3h0+1
kz(izp) = kz(izp) + 0.5* ku_r(1)
do iz=2,n3h0
izp=processor*n3h0+iz
kz(izp) = kz(izp) + 0.5*( ku_r(iz) + ku_r(iz-1) )
end do
end do
!PLOT!
do iz=1,n3
write(unit_ensz,fmt=*) zas(iz),rho_st(iz)*kz(iz)
enddo
write(unit_ensz,*) ' '
call flush(unit_ensz)
end if
END SUBROUTINE plot_ensz
!****************!
!!!!! SLICES !!!!!
!****************!
subroutine slices(uk,vk,wk,bk,wak,u_rot,ur,vr,wr,br,war,u_rotr,id_field)
double complex, dimension(iktx,ikty,n3h2) :: uk,vk,wk,bk
double complex, dimension(iktx,ikty,n3h1) :: zzk,wak,u_rot !COULD BE OPTIMIZED
double precision, dimension(n1d,n2d,n3h2) :: ur,vr,wr,br
double precision, dimension(n1d,n2d,n3h1) :: zzr,war,u_rotr
double complex, dimension(iktx,ikty,n3h2) :: bmem
double complex, dimension(iktx,ikty,n3h1) :: qmem
double precision, dimension(n1d,n2d,n3h0+2*hlvl(id_field)) :: field
real, dimension(n1,n3h0) :: XZ_slice_p !Scratch array for xz slices (divided amongst processors)
real, dimension(n1,n3) :: XZ_slice !Scratch array for xz slices
integer :: unit
integer :: id_field
character(len = 32) :: fname !future file name
integer :: nrec
integer :: processor
equivalence(zzk,zzr)
if(id_field==1) then
bmem=uk
call fft_c2r(uk,ur,n3h2)
field = U_scale*ur
else if(id_field==2) then
call fft_c2r(u_rot,u_rotr,n3h1)
field = U_scale*u_rotr
else if(id_field==3) then
bmem=wk
call fft_c2r(wk,wr,n3h2)
field = U_scale*sqrt(Ar2)*wr !In fact, w_DIM = UH/L w_NDIM (in the code) = UH/L Ro w_NDIM_1 (since w_NDIM_0 =0 in QG) => w_real_life = U Ar Ro w_1_computed
else if(id_field==4) then
qmem=wak
call fft_c2r(wak,war,n3h1)
field = U_scale*sqrt(Ar2)*Ro*war !In fact, w_DIM = UH/L w_NDIM (in the code) = UH/L Ro w_NDIM_1 (since w_NDIM_0 =0 in QG) => w_real_life = U Ar Ro w_1_computed
else if(id_field==5) then !QG streamfunction
bmem=bk
call fft_c2r(bk,br,n3h2)
field = (U_scale*U_scale/(H_scale*Ro))*br !In fact, b_real_life = fUL/H b_computed = U^2/(Ro*H) b_computed
!For theta_1_real, just multiply field (b_real) by H_scale*H_1/cp.
elseif(id_field==6) then !Fr^2/Ro * b_z/r_2 << 1 ? (Limiting QG assumption)
bmem=bk
call fft_c2r(bk,br,n3h2)
if(where_bz==stag) then
do izh1=1,n3h1
izh2=izh1+1
do ix=1,n1d
do iy=1,n2d
if(ix<=n1) then
field(ix,iy,izh1) = (br(ix,iy,izh2+1)-br(ix,iy,izh2-1))/(2.*dz)
else
field(ix,iy,izh1) = 0.
end if
end do
end do
end do
if(mype==0) then
do ix=1,n1d
do iy=1,n2d
if(ix<=n1) then
field(ix,iy,izbot1) = (br(ix,iy,izbot2+1) - br(ix,iy,izbot2) )/(2.D0*dz)
else
field(ix,iy,izbot1) = 0.
end if
end do
end do
elseif(mype==(npe-1)) then
do ix=1,n1d
do iy=1,n2d
if(ix<=n1) then