forked from mabarnes/stella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstella_io.fpp
1396 lines (1168 loc) · 51.5 KB
/
stella_io.fpp
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
# include "define.inc"
module stella_io
# ifdef NETCDF
use netcdf, only: nf90_noerr
use netcdf_utils, only: netcdf_error, kind_nf
# endif
implicit none
private
public :: init_stella_io, finish_stella_io
public :: write_time_nc
public :: write_phi2_nc
public :: write_phi_nc
public :: write_gvmus_nc
public :: write_gzvs_nc
public :: write_kspectra_nc
public :: write_moments_nc
public :: write_radial_fluxes_nc
public :: get_nout
public :: sync_nc
# ifdef NETCDF
integer (kind_nf) :: ncid
integer (kind_nf) :: naky_dim, nttot_dim, nmu_dim, nvtot_dim, nspec_dim
integer (kind_nf) :: nakx_dim, ntubes_dim, radgridvar_dim
integer (kind_nf) :: time_dim, char10_dim, char200_dim, ri_dim, nlines_dim, nheat_dim
integer (kind_nf) :: nalpha_dim
integer, dimension (7) :: moment_dim
integer, dimension (6) :: field_dim
integer, dimension (5) :: zvs_dim
integer, dimension (4) :: vmus_dim
integer, dimension (4) :: kykxaz_dim
integer, dimension (3) :: mode_dim, heat_dim, kykxz_dim, flux_x_dim
integer, dimension (2) :: kx_dim, ky_dim, om_dim, flux_dim, nin_dim, fmode_dim
integer, dimension (2) :: flux_surface_dim, rad_grid_dim
integer :: nakx_id, ntubes_id
integer :: naky_id, nttot_id, akx_id, aky_id, zed_id, nspec_id
integer :: nmu_id, nvtot_id, mu_id, vpa_id
integer :: time_id, phi2_id, theta0_id, nproc_id, nmesh_id
integer :: phi_vs_t_id, phi2_vs_kxky_id
integer :: pflux_x_id, vflux_x_id, qflux_x_id
integer :: density_id, upar_id, temperature_id
integer :: gvmus_id, gzvs_id
integer :: input_id
integer :: charge_id, mass_id, dens_id, temp_id, tprim_id, fprim_id
integer :: vnew_id, spec_type_id
integer :: bmag_id, gradpar_id, gbdrift_id, gbdrift0_id
integer :: cvdrift_id, cvdrift0_id, gds2_id, gds21_id, gds22_id
integer :: kperp2_id, rad_grid_id
integer :: grho_id, jacob_id, shat_id, drhodpsi_id, q_id, jtwist_id
integer :: beta_id
integer :: code_id
# endif
real :: zero
! include 'netcdf.inc'
contains
!==============================================
!============ INITIATE STELLA IO ==============
!==============================================
subroutine init_stella_io (restart, write_phi_vs_t, write_kspectra, write_gvmus, &
write_gzvs, write_moments, write_radial_fluxes)
use mp, only: proc0
use file_utils, only: run_name
# ifdef NETCDF
use netcdf, only: nf90_create, nf90_open, nf90_redef
use netcdf, only: nf90_clobber, nf90_noclobber, nf90_write
use netcdf_utils, only: get_netcdf_code_precision, netcdf_real
# endif
implicit none
logical, intent (in) :: restart
logical, intent (in) :: write_phi_vs_t, write_kspectra, write_gvmus, write_gzvs
logical, intent (in) :: write_moments, write_radial_fluxes!, write_symmetry
# ifdef NETCDF
character (300) :: filename
integer :: status
zero = epsilon(0.0)
if (netcdf_real == 0) netcdf_real = get_netcdf_code_precision()
status = nf90_noerr
! The netcdf file has the extension ".out.nc"
filename = trim(trim(run_name)//'.out.nc')
! Only the first processor (proc0) opens the file
if (proc0) then
if (restart) then
status = nf90_create (trim(filename), nf90_noclobber, ncid)
if (status /= nf90_noerr) then
status = nf90_open (trim(filename), nf90_write, ncid)
if (status /= nf90_noerr) call netcdf_error (status, file=filename)
status = nf90_redef (ncid)
endif
else
status = nf90_create (trim(filename), nf90_clobber, ncid)
endif
if (status /= nf90_noerr) call netcdf_error (status, file=filename)
call define_dims
call define_vars (write_phi_vs_t, write_kspectra, write_gvmus, write_gzvs, write_moments, write_radial_fluxes)
call nc_grids
call nc_species
call nc_geo
end if
# endif
end subroutine init_stella_io
subroutine define_dims
use file_utils, only: num_input_lines
use kt_grids, only: naky, nakx, nalpha
use zgrid, only: nzgrid, ntubes
use vpamu_grids, only: nvpa, nmu
use species, only: nspec
use physics_flags, only: radial_variation
# ifdef NETCDF
use netcdf, only: nf90_unlimited
use netcdf, only: nf90_def_dim, nf90_inq_dimid
# endif
# ifdef NETCDF
integer :: status
! Associate the grid variables, e.g. ky, kx, with their size, e.g. naky, nakx,
! and a variable which is later used to store these sizes in the NetCDF file, e.g. naky_dim, nakx_dim
status = nf90_inq_dimid(ncid,'ky',naky_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'ky', naky, naky_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='ky')
endif
status = nf90_inq_dimid(ncid,'kx',nakx_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'kx', nakx, nakx_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='kx')
endif
status = nf90_inq_dimid(ncid,'tube',ntubes_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'tube', ntubes, ntubes_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='tube')
endif
status = nf90_inq_dimid(ncid,'theta0',nakx_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'theta0', nakx, nakx_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='theta0')
endif
status = nf90_inq_dimid(ncid,'zed',nttot_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'zed', 2*nzgrid+1, nttot_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='zed')
endif
status = nf90_inq_dimid(ncid,'alpha',nalpha_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'alpha', nalpha, nalpha_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='alpha')
endif
status = nf90_inq_dimid(ncid,'vpa',nvtot_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'vpa', nvpa, nvtot_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='vpa')
endif
status = nf90_inq_dimid(ncid,'mu',nmu_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'mu', nmu, nmu_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='mu')
endif
status = nf90_inq_dimid(ncid,'species',nspec_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'species', nspec, nspec_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='species')
endif
status = nf90_inq_dimid(ncid,'t',time_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 't', nf90_unlimited, time_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='t')
endif
status = nf90_inq_dimid(ncid,'char10',char10_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'char10', 10, char10_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='char10')
endif
status = nf90_inq_dimid(ncid,'char200',char200_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'char200', 200, char200_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='char200')
endif
status = nf90_inq_dimid(ncid,'nlines',nlines_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'nlines', num_input_lines, nlines_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='nlines')
endif
status = nf90_inq_dimid(ncid,'ri',ri_dim)
if (status /= nf90_noerr) then
status = nf90_def_dim (ncid, 'ri', 2, ri_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='ri')
endif
if(radial_variation) then
status = nf90_inq_dimid(ncid,'radgridvar',radgridvar_dim)
if (status /= nf90_noerr) then
! size 3: x, q/psi, rho
status = nf90_def_dim (ncid, 'radgridvar', 3, radgridvar_dim)
if (status /= nf90_noerr) call netcdf_error (status, dim='radgridvar')
endif
endif
# endif
end subroutine define_dims
subroutine finish_stella_io
use mp, only: proc0
# ifdef NETCDF
use netcdf, only: nf90_close
use netcdf_utils, only: netcdf_error
integer :: status
if (proc0) then
call save_input
status = nf90_close (ncid)
if (status /= nf90_noerr) call netcdf_error (status)
end if
# endif
end subroutine finish_stella_io
subroutine save_input
!<doc> Save the input file in the NetCDF file </doc>
# ifdef NETCDF
use file_utils, only: num_input_lines, get_input_unit
use netcdf, only: nf90_put_var
character(200) line
integer, dimension (2) :: nin_start, nin_count
integer :: status, n, unit
nin_start(1) = 1
nin_start(2) = 1
nin_count(2) = 1
call get_input_unit (unit)
rewind (unit=unit)
do n = 1, num_input_lines
read (unit=unit, fmt="(a)") line
nin_count(1) = len(trim(line))
! status = nf_put_vara_text (ncid, input_id, nin_start, nin_count, line)
status = nf90_put_var (ncid, input_id, line, start=nin_start, count=nin_count)
if (status /= nf90_noerr) call netcdf_error (status, ncid, input_id)
nin_start(2) = nin_start(2) + 1
end do
# endif
end subroutine save_input
subroutine define_vars (write_phi_vs_t, write_kspectra, write_gvmus, &
! write_gzvs, write_symmetry, write_moments)
write_gzvs, write_moments, write_radial_fluxes)
use mp, only: nproc
use species, only: nspec
use run_parameters, only: fphi, fapar, fbpar
use physics_flags, only: radial_variation
# ifdef NETCDF
use netcdf, only: nf90_char, nf90_int, nf90_global
use netcdf, only: nf90_def_var, nf90_inq_varid, nf90_put_att, nf90_enddef, nf90_put_var
use netcdf, only: nf90_inq_libvers
use netcdf_utils, only: netcdf_real
# endif
implicit none
logical, intent(in) :: write_phi_vs_t, write_kspectra, write_gvmus, write_gzvs!, write_symmetry
logical, intent (in) :: write_moments, write_radial_fluxes
# ifdef NETCDF
character (5) :: ci
character (20) :: datestamp, timestamp, timezone
integer :: status
flux_surface_dim(1) = nalpha_dim
flux_surface_dim(2) = nttot_dim
fmode_dim(1) = naky_dim
fmode_dim(2) = nakx_dim
mode_dim (1) = naky_dim
mode_dim (2) = nakx_dim
mode_dim (3) = time_dim
kx_dim (1) = nakx_dim
kx_dim (2) = time_dim
ky_dim (1) = naky_dim
ky_dim (2) = time_dim
om_dim (1) = ri_dim
om_dim (2) = time_dim
nin_dim(1) = char200_dim
nin_dim(2) = nlines_dim
flux_dim (1) = nspec_dim
flux_dim (2) = time_dim
flux_x_dim (1) = nakx_dim
flux_x_dim (2) = nspec_dim
flux_x_dim (3) = time_dim
rad_grid_dim (1) = radgridvar_dim
rad_grid_dim (2) = nakx_dim
heat_dim (1) = nspec_dim
heat_dim (2) = nheat_dim
heat_dim (3) = time_dim
field_dim (1) = ri_dim
field_dim (2) = naky_dim
field_dim (3) = nakx_dim
field_dim (4) = nttot_dim
field_dim (5) = ntubes_dim
field_dim (6) = time_dim
moment_dim (1) = ri_dim
moment_dim (2) = naky_dim
moment_dim (3) = nakx_dim
moment_dim (4) = nttot_dim
moment_dim (5) = ntubes_dim
moment_dim (6) = nspec_dim
moment_dim (7) = time_dim
vmus_dim (1) = nvtot_dim
vmus_dim (2) = nmu_dim
vmus_dim (3) = nspec_dim
vmus_dim (4) = time_dim
zvs_dim (1) = ntubes_dim
zvs_dim (2) = nttot_dim
zvs_dim (3) = nvtot_dim
zvs_dim (4) = nspec_dim
zvs_dim (5) = time_dim
kykxz_dim (1) = naky_dim
kykxz_dim (2) = nakx_dim
kykxz_dim (3) = nttot_dim
kykxaz_dim (1) = naky_dim
kykxaz_dim (2) = nakx_dim
kykxaz_dim (3) = nalpha_dim
kykxaz_dim (4) = nttot_dim
! Write some useful general information such as the website,
! date and time into the NetCDF file
status = nf90_put_att (ncid, nf90_global, 'title', 'stella simulation data')
if (status /= nf90_noerr) call netcdf_error (status, ncid, nf90_global, att='title')
datestamp(:) = ' '
timestamp(:) = ' '
timezone(:) = ' '
call date_and_time (datestamp, timestamp, timezone)
status = nf90_inq_varid(ncid,'code_info',code_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'code_info', nf90_char, char10_dim, code_id)
if (status /= nf90_noerr) call netcdf_error (status, var='code_info')
endif
status = nf90_put_att (ncid, code_id, 'long_name', 'stella')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att='long_name')
ci = 'c1'
status = nf90_put_att (ncid, code_id, trim(ci), 'Date: '//trim(datestamp))
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c2'
status = nf90_put_att (ncid, code_id, trim(ci), &
'Time: '//trim(timestamp)//' '//trim(timezone))
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c3'
status = nf90_put_att (ncid, code_id, trim(ci), &
'netCDF version '//trim(nf90_inq_libvers()))
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c4'
status = nf90_put_att (ncid, code_id, trim(ci), &
'Units are determined with respect to reference temperature (T_ref),')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c5'
status = nf90_put_att (ncid, code_id, trim(ci), &
'reference charge (q_ref), reference mass (mass_ref),')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c6'
status = nf90_put_att (ncid, code_id, trim(ci), &
'reference field (B_ref), and reference length (a_ref)')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c7'
status = nf90_put_att (ncid, code_id, trim(ci), &
'from which one may construct rho_ref and vt_ref/a,')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c8'
status = nf90_put_att (ncid, code_id, trim(ci), &
'which are the basic units of perpendicular length and time.')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c9'
status = nf90_put_att (ncid, code_id, trim(ci), &
'Macroscopic lengths are normalized to the minor radius.')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c10'
status = nf90_put_att (ncid, code_id, trim(ci), &
'The difference between rho (normalized minor radius) and rho (gyroradius)')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
ci = 'c11'
status = nf90_put_att (ncid, code_id, trim(ci), &
'should be clear from the context in which they appear below.')
if (status /= nf90_noerr) call netcdf_error (status, ncid, code_id, att=ci)
! Write lots of input variables (e.g. nproc, nkx, nky)
! into the NetCDF file
status = nf90_inq_varid(ncid,'nproc',nproc_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'nproc', nf90_int, nproc_id)
if (status /= nf90_noerr) call netcdf_error (status, var='nproc')
endif
status = nf90_put_att (ncid, nproc_id, 'long_name', 'Number of processors')
if (status /= nf90_noerr) call netcdf_error (status, ncid, nproc_id, att='long_name')
status = nf90_inq_varid(ncid,'nmesh',nmesh_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'nmesh', netcdf_real, nmesh_id)
if (status /= nf90_noerr) call netcdf_error (status, var='nmesh')
endif
status = nf90_put_att (ncid, nmesh_id, 'long_name', 'Number of meshpoints')
if (status /= nf90_noerr) call netcdf_error (status, ncid, nmesh_id, att='long_name')
status = nf90_inq_varid(ncid,'ntubes',ntubes_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'ntubes', nf90_int, ntubes_id)
if (status /= nf90_noerr) call netcdf_error (status, var='ntubes')
endif
status = nf90_inq_varid(ncid,'nkx',nakx_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'nkx', nf90_int, nakx_id)
if (status /= nf90_noerr) call netcdf_error (status, var='nkx')
endif
status = nf90_inq_varid(ncid,'nky',naky_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'nky', nf90_int, naky_id)
if (status /= nf90_noerr) call netcdf_error (status, var='nky')
endif
status = nf90_inq_varid(ncid,'nzed_tot',nttot_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'nzed_tot', nf90_int, nttot_id)
if (status /= nf90_noerr) call netcdf_error (status, var='nzed_tot')
endif
status = nf90_inq_varid(ncid,'nspecies',nspec_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'nspecies', nf90_int, nspec_id)
if (status /= nf90_noerr) call netcdf_error (status, var='nspecies')
endif
status = nf90_inq_varid(ncid,'nmu',nmu_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'nmu', nf90_int, nmu_id)
if (status /= nf90_noerr) call netcdf_error (status, var='nmu')
endif
status = nf90_inq_varid(ncid,'nvpa_tot',nvtot_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'nvpa_tot', nf90_int, nvtot_id)
if (status /= nf90_noerr) call netcdf_error (status, var='nvpa_tot')
endif
status = nf90_inq_varid(ncid,'t',time_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 't', netcdf_real, time_dim, time_id)
if (status /= nf90_noerr) call netcdf_error (status, var='t')
endif
status = nf90_put_att (ncid, time_id, 'long_name', 'Time')
if (status /= nf90_noerr) call netcdf_error (status, ncid, time_id, att='long_name')
status = nf90_put_att (ncid, time_id, 'units', 'L/vt')
if (status /= nf90_noerr) call netcdf_error (status, ncid, time_id, att='units')
status = nf90_inq_varid(ncid,'charge',charge_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'charge', nf90_int, nspec_dim, charge_id)
if (status /= nf90_noerr) call netcdf_error (status, var='charge')
endif
status = nf90_put_att (ncid, charge_id, 'long_name', 'Charge')
if (status /= nf90_noerr) call netcdf_error (status, ncid, charge_id, att='long_name')
status = nf90_put_att (ncid, charge_id, 'units', 'q')
if (status /= nf90_noerr) call netcdf_error (status, ncid, charge_id, att='units')
status = nf90_inq_varid(ncid,'mass',mass_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'mass', netcdf_real, nspec_dim, mass_id)
if (status /= nf90_noerr) call netcdf_error (status, var='mass')
endif
status = nf90_put_att (ncid, mass_id, 'long_name', 'Atomic mass')
if (status /= nf90_noerr) call netcdf_error (status, ncid, mass_id, att='long_name')
status = nf90_put_att (ncid, mass_id, 'units', 'm')
if (status /= nf90_noerr) call netcdf_error (status, ncid, mass_id, att='units')
status = nf90_inq_varid(ncid,'dens',dens_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'dens', netcdf_real, nspec_dim, dens_id)
if (status /= nf90_noerr) call netcdf_error (status, var='dens')
endif
status = nf90_put_att (ncid, dens_id, 'long_name', 'Density')
if (status /= nf90_noerr) call netcdf_error (status, ncid, dens_id, att='long_name')
status = nf90_put_att (ncid, dens_id, 'units', 'n_e')
if (status /= nf90_noerr) call netcdf_error (status, ncid, dens_id, att='units')
status = nf90_inq_varid(ncid,'temp',temp_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'temp', netcdf_real, nspec_dim, temp_id)
if (status /= nf90_noerr) call netcdf_error (status, var='temp')
endif
status = nf90_put_att (ncid, temp_id, 'long_name', 'Temperature')
if (status /= nf90_noerr) call netcdf_error (status, ncid, temp_id, att='long_name')
status = nf90_put_att (ncid, temp_id, 'units', 'T')
if (status /= nf90_noerr) call netcdf_error (status, ncid, temp_id, att='units')
status = nf90_inq_varid(ncid,'tprim',tprim_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'tprim', netcdf_real, nspec_dim, tprim_id)
if (status /= nf90_noerr) call netcdf_error (status, var='tprim')
endif
status = nf90_put_att (ncid, tprim_id, 'long_name', '-1/rho dT/drho')
if (status /= nf90_noerr) call netcdf_error (status, ncid, tprim_id, att='long_name')
status = nf90_inq_varid(ncid,'fprim',fprim_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'fprim', netcdf_real, nspec_dim, fprim_id)
if (status /= nf90_noerr) call netcdf_error (status, var='fprim')
endif
status = nf90_put_att (ncid, fprim_id, 'long_name', '-1/rho dn/drho')
if (status /= nf90_noerr) call netcdf_error (status, ncid, fprim_id, att='long_name')
status = nf90_inq_varid(ncid,'vnew',vnew_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'vnew', netcdf_real, nspec_dim, vnew_id)
if (status /= nf90_noerr) call netcdf_error (status, var='vnew')
endif
status = nf90_put_att (ncid, vnew_id, 'long_name', 'Collisionality')
if (status /= nf90_noerr) call netcdf_error (status, ncid, vnew_id, att='long_name')
status = nf90_put_att (ncid, vnew_id, 'units', 'v_t/L')
if (status /= nf90_noerr) call netcdf_error (status, ncid, vnew_id, att='units')
status = nf90_inq_varid(ncid,'type_of_species',spec_type_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'type_of_species', nf90_int, nspec_dim, spec_type_id)
if (status /= nf90_noerr) call netcdf_error (status, var='type_of_species')
endif
status = nf90_inq_varid(ncid,'theta0',theta0_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'theta0', netcdf_real, fmode_dim, theta0_id)
if (status /= nf90_noerr) call netcdf_error (status, var='theta0')
endif
status = nf90_put_att (ncid, theta0_id, 'long_name', 'Theta_0')
if (status /= nf90_noerr) call netcdf_error (status, ncid, theta0_id, att='long_name')
status = nf90_inq_varid(ncid,'kx',akx_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'kx', netcdf_real, nakx_dim, akx_id)
if (status /= nf90_noerr) call netcdf_error (status, var='kx')
endif
status = nf90_put_att (ncid, akx_id, 'long_name', 'kx rho')
if (status /= nf90_noerr) call netcdf_error (status, ncid, akx_id, att='long_name')
if(radial_variation) then
status = nf90_inq_varid(ncid,'rad_grid',rad_grid_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'rad_grid', netcdf_real, rad_grid_dim, rad_grid_id)
if (status /= nf90_noerr) call netcdf_error (status, var='rad_grid')
endif
status = nf90_put_att (ncid, rad_grid_id, 'long_name', 'x, q/psi, rho')
if (status /= nf90_noerr) call netcdf_error (status, ncid, rad_grid_id, att='long_name')
endif
status = nf90_inq_varid(ncid,'ky',aky_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'ky', netcdf_real, naky_dim, aky_id)
if (status /= nf90_noerr) call netcdf_error (status, var='ky')
endif
status = nf90_put_att (ncid, aky_id, 'long_name', 'ky rho')
if (status /= nf90_noerr) call netcdf_error (status, ncid, aky_id, att='long_name')
status = nf90_inq_varid(ncid,'mu',mu_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'mu', netcdf_real, nmu_dim, mu_id)
if (status /= nf90_noerr) call netcdf_error (status, var='mu')
endif
status = nf90_inq_varid(ncid,'vpa',vpa_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'vpa', netcdf_real, nvtot_dim, vpa_id)
if (status /= nf90_noerr) call netcdf_error (status, var='vpa')
endif
status = nf90_inq_varid(ncid,'zed',zed_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'zed', netcdf_real, nttot_dim, zed_id)
if (status /= nf90_noerr) call netcdf_error (status, var='zed')
endif
status = nf90_inq_varid(ncid,'bmag',bmag_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'bmag', netcdf_real, flux_surface_dim, bmag_id)
if (status /= nf90_noerr) call netcdf_error (status, var='bmag')
endif
status = nf90_put_att (ncid, bmag_id, 'long_name', '|B|(alpha,zed)')
if (status /= nf90_noerr) call netcdf_error (status, ncid, bmag_id, att='long_name')
status = nf90_put_att (ncid, bmag_id, 'units', 'B_0')
if (status /= nf90_noerr) call netcdf_error (status, ncid, bmag_id, att='units')
status = nf90_inq_varid(ncid,'gradpar',gradpar_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'gradpar', netcdf_real, nttot_dim, gradpar_id)
if (status /= nf90_noerr) call netcdf_error (status, var='gradpar')
endif
status = nf90_inq_varid(ncid,'gbdrift',gbdrift_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'gbdrift', netcdf_real, flux_surface_dim, gbdrift_id)
if (status /= nf90_noerr) call netcdf_error (status, var='gbdrift')
endif
status = nf90_inq_varid(ncid,'gbdrift0',gbdrift0_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'gbdrift0', netcdf_real, flux_surface_dim, gbdrift0_id)
if (status /= nf90_noerr) call netcdf_error (status, var='gbdrift0')
endif
status = nf90_inq_varid(ncid,'cvdrift',cvdrift_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'cvdrift', netcdf_real, flux_surface_dim, cvdrift_id)
if (status /= nf90_noerr) call netcdf_error (status, var='cvdrift')
endif
status = nf90_inq_varid(ncid,'cvdrift0',cvdrift0_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'cvdrift0', netcdf_real, flux_surface_dim, cvdrift0_id)
if (status /= nf90_noerr) call netcdf_error (status, var='cvdrift0')
endif
status = nf90_inq_varid(ncid,'kperp2',kperp2_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'kperp2', netcdf_real, kykxaz_dim, kperp2_id)
if (status /= nf90_noerr) call netcdf_error (status, var='kperp2')
endif
status = nf90_inq_varid(ncid,'gds2',gds2_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'gds2', netcdf_real, flux_surface_dim, gds2_id)
if (status /= nf90_noerr) call netcdf_error (status, var='gds2')
endif
status = nf90_inq_varid(ncid,'gds21',gds21_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'gds21', netcdf_real, flux_surface_dim, gds21_id)
if (status /= nf90_noerr) call netcdf_error (status, var='gds21')
endif
status = nf90_inq_varid(ncid,'gds22',gds22_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'gds22', netcdf_real, flux_surface_dim, gds22_id)
if (status /= nf90_noerr) call netcdf_error (status, var='gds22')
endif
status = nf90_inq_varid(ncid,'grho',grho_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'grho', netcdf_real, flux_surface_dim, grho_id)
if (status /= nf90_noerr) call netcdf_error (status, var='grho')
endif
status = nf90_inq_varid(ncid,'jacob',jacob_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'jacob', netcdf_real, flux_surface_dim, jacob_id)
if (status /= nf90_noerr) call netcdf_error (status, var='jacob')
endif
status = nf90_inq_varid(ncid,'q',q_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'q', netcdf_real, q_id)
if (status /= nf90_noerr) call netcdf_error (status, var='q')
endif
status = nf90_put_att (ncid, q_id, 'long_name', 'local safety factor')
if (status /= nf90_noerr) call netcdf_error (status, ncid, q_id, att='long_name')
status = nf90_inq_varid(ncid,'beta',beta_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'beta', netcdf_real, beta_id)
if (status /= nf90_noerr) call netcdf_error (status, var='beta')
endif
status = nf90_put_att (ncid, beta_id, 'long_name', 'reference beta')
if (status /= nf90_noerr) call netcdf_error (status, ncid, beta_id, att='long_name')
status = nf90_inq_varid(ncid,'shat',shat_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'shat', netcdf_real, shat_id)
if (status /= nf90_noerr) call netcdf_error (status, var='shat')
endif
status = nf90_put_att (ncid, shat_id, 'long_name', '(rho/q) dq/drho')
if (status /= nf90_noerr) call netcdf_error (status, ncid, shat_id, att='long_name')
status = nf90_inq_varid(ncid,'jtwist',jtwist_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'jtwist', netcdf_real, jtwist_id)
if (status /= nf90_noerr) call netcdf_error (status, var='jtwist')
endif
status = nf90_put_att (ncid, jtwist_id, 'long_name', '2*pi*shat*dky/dkx')
if (status /= nf90_noerr) call netcdf_error (status, ncid, jtwist_id, att='long_name')
status = nf90_inq_varid(ncid,'drhodpsi',drhodpsi_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'drhodpsi', netcdf_real, drhodpsi_id)
if (status /= nf90_noerr) call netcdf_error (status, var='drhodpsi')
endif
status = nf90_put_att (ncid, drhodpsi_id, 'long_name', 'drho/dPsi')
if (status /= nf90_noerr) call netcdf_error (status, ncid, drhodpsi_id, att='long_name')
if (fphi > zero) then
status = nf90_inq_varid(ncid,'phi2',phi2_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'phi2', netcdf_real, time_dim, phi2_id)
if (status /= nf90_noerr) call netcdf_error (status, var='phi2')
endif
status = nf90_put_att (ncid, phi2_id, 'long_name', '|Potential**2|')
if (status /= nf90_noerr) &
call netcdf_error (status, ncid, phi2_id, att='long_name')
status = nf90_put_att (ncid, phi2_id, 'units', '(T/q rho/L)**2')
if (status /= nf90_noerr) &
call netcdf_error (status, ncid, phi2_id, att='units')
! status = nf90_def_var &
! (ncid, 'phi2_by_mode', netcdf_real, mode_dim, phi2_by_mode_id)
! if (status /= nf90_noerr) call netcdf_error (status, var='phi2_by_mode')
! if (nakx > 1) then
! status = nf90_def_var &
! (ncid, 'phi2_by_kx', netcdf_real, kx_dim, phi2_by_kx_id)
! if (status /= nf90_noerr) &
! call netcdf_error (status, var='phi2_by_kx')
! end if
! if (naky > 1) then
! status = nf90_def_var &
! (ncid, 'phi2_by_ky', netcdf_real, ky_dim, phi2_by_ky_id)
! if (status /= nf90_noerr) &
! call netcdf_error (status, var='phi2_by_ky')
! end if
if (write_phi_vs_t) then
status = nf90_inq_varid(ncid,'phi_vs_t',phi_vs_t_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'phi_vs_t', netcdf_real, field_dim, phi_vs_t_id)
if (status /= nf90_noerr) call netcdf_error (status, var='phi_vs_t')
endif
status = nf90_put_att (ncid, phi_vs_t_id, 'long_name', 'Electrostatic Potential vs time')
if (status /= nf90_noerr) call netcdf_error (status, ncid, phi_vs_t_id, att='long_name')
end if
if (write_radial_fluxes) then
status = nf90_inq_varid(ncid,'pflux_x',pflux_x_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'pflux_x', netcdf_real, flux_x_dim, pflux_x_id)
if (status /= nf90_noerr) call netcdf_error (status, var='pflux_x')
endif
status = nf90_inq_varid(ncid,'vflux_x',vflux_x_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'vflux_x', netcdf_real, flux_x_dim, vflux_x_id)
if (status /= nf90_noerr) call netcdf_error (status, var='vflux_x')
endif
status = nf90_inq_varid(ncid,'qflux_x',qflux_x_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'qflux_x', netcdf_real, flux_x_dim, qflux_x_id)
if (status /= nf90_noerr) call netcdf_error (status, var='qflux_x')
endif
end if
if (write_kspectra) then
status = nf90_inq_varid(ncid,'phi2_vs_kxky',phi2_vs_kxky_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'phi2_vs_kxky', netcdf_real, mode_dim, phi2_vs_kxky_id)
if (status /= nf90_noerr) call netcdf_error (status, var='phi2_vs_kxky')
endif
status = nf90_put_att (ncid, phi2_vs_kxky_id, 'long_name', 'Electrostatic Potential vs (ky,kx,t)')
if (status /= nf90_noerr) call netcdf_error (status, ncid, phi2_vs_kxky_id, att='long_name')
end if
end if
if (write_moments) then
status = nf90_inq_varid(ncid,'density',density_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'density', netcdf_real, moment_dim, density_id)
if (status /= nf90_noerr) call netcdf_error (status, var='density')
endif
status = nf90_put_att (ncid, density_id, 'long_name', 'perturbed density vs (ky,kx,z,t)')
if (status /= nf90_noerr) call netcdf_error (status, ncid, density_id, att='long_name')
status = nf90_inq_varid(ncid,'upar',upar_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'upar', netcdf_real, moment_dim, upar_id)
if (status /= nf90_noerr) call netcdf_error (status, var='upar')
endif
status = nf90_put_att (ncid, upar_id, 'long_name', 'perturbed parallel flow vs (ky,kx,z,t)')
if (status /= nf90_noerr) call netcdf_error (status, ncid, upar_id, att='long_name')
status = nf90_inq_varid(ncid,'temperature',temperature_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'temperature', netcdf_real, moment_dim, temperature_id)
if (status /= nf90_noerr) call netcdf_error (status, var='temperature')
endif
status = nf90_put_att (ncid, temperature_id, 'long_name', 'perturbed temperature vs (ky,kx,z,t)')
if (status /= nf90_noerr) call netcdf_error (status, ncid, temperature_id, att='long_name')
end if
if (write_gvmus) then
status = nf90_inq_varid(ncid,'gvmus',gvmus_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'gvmus', netcdf_real, vmus_dim, gvmus_id)
if (status /= nf90_noerr) call netcdf_error (status, var='gvmus')
endif
status = nf90_put_att (ncid, gvmus_id, 'long_name', &
'guiding center distribution function averaged over real space')
if (status /= nf90_noerr) call netcdf_error (status, ncid, gvmus_id, att='long_name')
end if
if (write_gzvs) then
status = nf90_inq_varid(ncid,'gzvs',gzvs_id)
if(status /= nf90_noerr) then
status = nf90_def_var &
(ncid, 'gzvs', netcdf_real, zvs_dim, gzvs_id)
if (status /= nf90_noerr) call netcdf_error (status, var='gzvs')
endif
status = nf90_put_att (ncid, gvmus_id, 'long_name', &
'guiding center distribution function averaged over (kx,ky,mu)')
if (status /= nf90_noerr) call netcdf_error (status, ncid, gzvs_id, att='long_name')
end if
! if (write_symmetry) then
! status = nf90_def_var &
! (ncid, 'pflx_zvpa', netcdf_real, zvs_dim, gzvs_id)
! if (status /= nf90_noerr) call netcdf_error (status, var='gzvs')
! status = nf90_put_att (ncid, gvmus_id, 'long_name', &
! 'guiding center distribution function averaged over (kx,ky,mu)')
! if (status /= nf90_noerr) call netcdf_error (status, ncid, gzvs_id, att='long_name')
! end if
status = nf90_inq_varid(ncid,'input_file',input_id)
if(status /= nf90_noerr) then
status = nf90_def_var (ncid, 'input_file', nf90_char, nin_dim, input_id)
if (status /= nf90_noerr) call netcdf_error (status, var='input_file')
endif
status = nf90_put_att (ncid, input_id, 'long_name', 'Input file')
if (status /= nf90_noerr) call netcdf_error (status, ncid, input_id, att='long_name')
status = nf90_enddef (ncid) ! out of definition mode
if (status /= nf90_noerr) call netcdf_error (status)
status = nf90_put_var (ncid, nproc_id, nproc)
if (status /= nf90_noerr) call netcdf_error (status, ncid, nproc_id)
# endif
end subroutine define_vars
subroutine write_time_nc (nout, time)
# ifdef NETCDF
use netcdf, only: nf90_put_var, nf90_sync
# endif
implicit none
integer, intent (in) :: nout
real, intent (in) :: time
# ifdef NETCDF
integer :: status
status = nf90_put_var (ncid, time_id, time, start=(/ nout /))
if (status /= nf90_noerr) call netcdf_error (status, ncid, time_id)
! The two lines below are added to flush buffers to disk
status = NF90_SYNC(ncid)
if (status /= nf90_noerr) call netcdf_error (status, ncid, time_id)
# endif
end subroutine write_time_nc
subroutine write_phi2_nc (nout, phi2)
# ifdef NETCDF
use netcdf, only: nf90_put_var
# endif
implicit none
integer, intent (in) :: nout
real, intent (in) :: phi2
# ifdef NETCDF
integer :: status
status = nf90_put_var (ncid, phi2_id, phi2, start=(/ nout /))
if (status /= nf90_noerr) call netcdf_error (status, ncid, phi2_id)
# endif
end subroutine write_phi2_nc
subroutine write_phi_nc (nout, phi)
use convert, only: c2r
use zgrid, only: nzgrid, ntubes
use kt_grids, only: nakx, naky
# ifdef NETCDF
use netcdf, only: nf90_put_var, nf90_sync
# endif
implicit none
integer, intent (in) :: nout
complex, dimension (:,:,-nzgrid:,:), intent (in) :: phi
# ifdef NETCDF
integer :: status
integer, dimension (6) :: start, count
real, dimension (:,:,:,:,:), allocatable :: phi_ri
start = 1
start(6) = nout
count(1) = 2
count(2) = naky
count(3) = nakx
count(4) = 2*nzgrid+1
count(5) = ntubes
count(6) = 1
allocate (phi_ri(2, naky, nakx, 2*nzgrid+1, ntubes))
call c2r (phi, phi_ri)
status = nf90_put_var (ncid, phi_vs_t_id, phi_ri, start=start, count=count)
if (status /= nf90_noerr) call netcdf_error (status, ncid, phi_vs_t_id)
! Buffers to disk
status = NF90_SYNC(ncid)
if (status /= nf90_noerr) call netcdf_error (status, ncid, phi_vs_t_id)
deallocate (phi_ri)
# endif
end subroutine write_phi_nc
subroutine write_radial_fluxes_nc (nout, pflux, vflux,qflux)
use convert, only: c2r
use kt_grids, only: nakx
use species, only: nspec
# ifdef NETCDF
use netcdf, only: nf90_put_var
# endif
implicit none
integer, intent (in) :: nout
real, dimension (:,:), intent (in) :: pflux, vflux, qflux
# ifdef NETCDF
integer :: status
integer, dimension (3) :: start, count
start = 1
start(3) = nout
count(1) = nakx
count(2) = nspec
count(3) = 1
status = nf90_put_var (ncid, pflux_x_id, pflux, start=start, count=count)
if (status /= nf90_noerr) call netcdf_error (status, ncid, pflux_x_id)
status = nf90_put_var (ncid, vflux_x_id, vflux, start=start, count=count)
if (status /= nf90_noerr) call netcdf_error (status, ncid, vflux_x_id)
status = nf90_put_var (ncid, qflux_x_id, qflux, start=start, count=count)
if (status /= nf90_noerr) call netcdf_error (status, ncid, qflux_x_id)
# endif
end subroutine write_radial_fluxes_nc
subroutine write_kspectra_nc (nout, phi2_vs_kxky)
use kt_grids, only: nakx, naky
# ifdef NETCDF
use netcdf, only: nf90_put_var, nf90_sync