-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcompare.f90
More file actions
296 lines (207 loc) · 8.41 KB
/
Copy pathcompare.f90
File metadata and controls
296 lines (207 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
module compare_h5
!! for safety, this trades efficiency for reliability
!! that is, we repeatedly open, close, allocate to help avoid
!! any weird bugs causing false positive/negative
use, intrinsic :: ieee_arithmetic, only : ieee_is_finite
use, intrinsic :: iso_fortran_env, only : stderr=>error_unit
use phys_consts, only : wp
use timeutils, only : date_filename, dateinc
use config, only : gemini_cfg, read_configfile
use h5fortran, only : hdf5_file
use pathlib, only : get_suffix, parent, file_name
use reader, only : get_simsize3
use assert, only : isclose
implicit none (type, external)
integer, parameter :: lsp=7
real(wp), parameter :: &
rtol = 1e-5_wp, atol = 1e-8_wp, &
rtolJ = 0.01_wp, atolJ = 1e-7_wp, &
rtolV = 1e-5_wp, atolV = 50, &
rtolN = 1e-5_wp, atolN = 1e9_wp, &
rtolT = 1e-5_wp, atolT = 100
private
public :: check_plasma_hdf5
contains
subroutine check_plasma_hdf5(new_path, ref_path, all_ok)
type(gemini_cfg) :: cfg
character(*), intent(in) :: new_path, ref_path
logical, intent(out) :: all_ok
character(:), allocatable :: new_file, ref_file
integer :: i, ymd(3)
real(wp) :: UTsec
character(:), allocatable :: suffix
logical :: exists, ok
all_ok = .true.
cfg%infile = new_path // '/inputs/config.nml'
cfg%outdir = '.'
call read_configfile(cfg)
ymd = cfg%ymd0
UTsec = cfg%UTsec0
suffix = get_suffix(cfg%indatsize)
ref_file = date_filename(ref_path, ymd, UTsec) // suffix
inquire(file=ref_file, exist=exists)
if (.not. exists) then
! skip first file for old sim reference data
call dateinc(cfg%dtout, ymd, UTsec)
ref_file = date_filename(ref_path, ymd, UTsec) // suffix
inquire(file=ref_file, exist=exists)
if (.not. exists) error stop "reference data not found in " // ref_path
endif
do
ref_file = date_filename(ref_path, ymd, UTsec) // suffix
inquire(file=ref_file, exist=exists)
if (.not. exists) exit
!! last output file
new_file = date_filename(new_path, ymd, UTsec) // suffix
ok = checker(cfg, new_file, ref_file)
if(.not. ok) then
write(stderr,*) "gemini3d.compare: MISMATCHED data at", ymd, UTsec
all_ok = .false.
endif
!! next time
call dateinc(cfg%dtout, ymd, UTsec)
end do
end subroutine check_plasma_hdf5
logical function checker(cfg, new_file, ref_file)
type(gemini_cfg), intent(in) :: cfg
character(*), intent(in) :: new_file, ref_file
type(hdf5_file) :: hnew, href
integer :: i, bad
logical :: exists, ok
character(7), parameter :: varsT(2) = [character(7) :: 'Tavgall', 'TEall']
character(8), parameter :: varsV(3) = ['v1avgall', 'v2avgall', 'v3avgall']
character(5), parameter :: varsJ(3) = ['J1all', 'J2all', 'J3all']
real(wp) :: UThour1, UThour2
integer :: ymd1(3), ymd2(3)
integer :: lx1, lx2all, lx3all, Nlx1, Nlx2all, Nlx3all, lx2, lx3
bad = 0
call get_simsize3(parent(ref_file) // "/inputs", lx1, lx2all, lx3all)
call get_simsize3(parent(new_file) // "/inputs", Nlx1, Nlx2all, Nlx3all)
if(lx1 /= Nlx1) error stop 'lx1 not match ref: ' // new_file
if(lx2all /= Nlx2all) error stop 'lx2all not match ref: ' // new_file
if(lx3all /= Nlx3all) error stop 'lx3all not match ref: ' // new_file
if(lx3all /= 1) then
!! not swapped
lx2 = lx2all
lx3 = lx3all
else
!! 2D sim, swapped
lx2 = lx3all
lx3 = lx2all
endif
call hnew%initialize(new_file, status='old',action='r')
call href%initialize(ref_file, status='old',action='r')
call href%read('/time/ymd', ymd1)
call hnew%read('/time/ymd', ymd2)
if (any(ymd1 /= ymd2)) error stop 'dates did not match: ' // new_file
call href%read('/time/UThour', UThour1)
call hnew%read('/time/UThour', UThour2)
call hnew%finalize()
call href%finalize()
if (abs(UThour1 - UThour2) > 0.01) error stop "UThour not match: " // new_file
! print *, "flagoutput:", cfg%flagoutput
if (cfg%flagoutput == 3) then
!! just electron density
bad = bad + check_var('neall', new_file, ref_file, rtolN, atolN, lx1, lx2, lx3)
elseif (cfg%flagoutput == 2) then
bad = bad + check_var('neall', new_file, ref_file, rtolN, atolN, lx1, lx2, lx3)
do i = 1,size(varsT)
bad = bad + check_var(varsT(i), new_file, ref_file, rtolT, atolT, lx1, lx2, lx3)
enddo
do i = 1,size(varsV)
bad = bad + check_var(varsV(i), new_file, ref_file, rtolV, atolV, lx1, lx2, lx3)
enddo
do i = 1,size(varsJ)
bad = bad + check_var(varsJ(i), new_file, ref_file, rtolJ, atolJ, lx1, lx2, lx3)
enddo
elseif(cfg%flagoutput==1) then
do i = 1,size(varsJ)
bad = bad + check_var(varsJ(i), new_file, ref_file, rtolJ, atolJ, lx1, lx2, lx3)
enddo
do i = 2,size(varsV)
bad = bad + check_var(varsV(i), new_file, ref_file, rtolV, atolV, lx1, lx2, lx3)
enddo
!> Ne
bad = bad + check_var('nsall', new_file, ref_file, rtolN, atolN, lx1, lx2, lx3, ionly=lsp, derived_name="ne")
!> Te
bad = bad + check_var('Tsall', new_file, ref_file, rtolT, atolT, lx1, lx2, lx3, ionly=lsp, derived_name="Te")
!> Ti
bad = bad + check_derived('Tsall', "Ti", new_file, ref_file, rtolT, atolT, lx1, lx2, lx3)
!> v1
bad = bad + check_derived('vs1all', "v1", new_file, ref_file, rtolV, atolV, lx1, lx2, lx3)
else
error stop 'unknown flagoutput: ' // file_name(ref_file)
endif
checker = bad == 0
end function checker
integer function check_derived(name, derived_name, new_file, ref_file, rtol, atol, lx1, lx2, lx3) result(bad)
character(*), intent(in) :: new_file, ref_file
character(*), intent(in) :: name, derived_name
real(wp), intent(in) :: rtol, atol
integer, intent(in) :: lx1, lx2, lx3
real, dimension(:,:,:), allocatable :: D_new, D_ref
real, dimension(:,:,:,:), allocatable :: new, ref, ns_new, ns_ref
type(hdf5_file) :: hnew, href
bad = 0
allocate(new(lx1, lx2, lx3, lsp), ref(lx1, lx2, lx3, lsp))
allocate(ns_new(lx1, lx2, lx3, lsp), ns_ref(lx1, lx2, lx3, lsp))
allocate(D_ref(lx1, lx2, lx3), D_new(lx1, lx2, lx3))
call hnew%initialize(new_file, status='old',action='r')
call href%initialize(ref_file, status='old',action='r')
call href%read('nsall', ns_ref)
call hnew%read('nsall', ns_new)
if (.not.all(ieee_is_finite(ns_ref))) error stop "NON-FINITE: " // file_name(ref_file) // " ns"
if (.not.all(ieee_is_finite(ns_new))) error stop "NON-FINITE: " // file_name(new_file) // " ns"
call hnew%read(name, new)
call href%read(name, ref)
if (.not.all(ieee_is_finite(new))) error stop "NON-FINITE: " // file_name(new_file) // " " // name
if (.not.all(ieee_is_finite(ref))) error stop "NON-FINITE: " // file_name(ref_file) // " " // name
call hnew%finalize()
call href%finalize()
D_ref = sum(ns_ref(:,:,:,1:6) * ref(:,:,:,1:6), dim=4) / ns_ref(:,:,:,LSP)
D_new = sum(ns_new(:,:,:,1:6) * new(:,:,:,1:6), dim=4) / ns_new(:,:,:,LSP)
if(all(isclose(D_ref, D_new, rtol, atol))) return
bad = bad + 1
write(stderr,*) "MISMATCH: " // file_name(new_file) // " ", derived_name, maxval(abs(D_ref - D_new))
end function check_derived
integer function check_var(name, new_file, ref_file, rtol, atol, lx1, lx2, lx3, ionly, derived_name) result(bad)
character(*), intent(in) :: new_file, ref_file
character(*), intent(in) :: name
real(wp), intent(in) :: rtol, atol
integer, intent(in) :: lx1, lx2, lx3
integer, intent(in), optional :: ionly
character(*), intent(in), optional :: derived_name
type(hdf5_file) :: hnew, href
real, dimension(:,:,:), allocatable :: new, ref
real, dimension(:,:,:,:), allocatable :: new4, ref4
if(present(ionly)) then
allocate(new4(lx1, lx2, lx3, lsp), ref4(lx1, lx2, lx3, lsp))
endif
allocate(new(lx1, lx2, lx3), ref(lx1, lx2, lx3))
bad = 0
call hnew%initialize(new_file, status='old',action='r')
call href%initialize(ref_file, status='old',action='r')
if(present(ionly)) then
call hnew%read(name, new4)
call href%read(name, ref4)
new = new4(:,:,:,lsp)
ref = ref4(:,:,:,lsp)
else
call hnew%read(name, new)
call href%read(name, ref)
endif
call hnew%finalize()
call href%finalize()
if (.not.all(ieee_is_finite(ref))) error stop "NON-FINITE: " // file_name(ref_file) // " " // name
if (.not.all(ieee_is_finite(new))) error stop "NON-FINITE: " // file_name(new_file) // " " // name
if(all(isclose(ref, new, rtol, atol))) return
bad = 1
if(present(derived_name)) then
write(stderr,'(A,/,A,ES12.3,A,2ES12.3,A,2ES12.3)') "MISMATCH: " // file_name(new_file) // " " // derived_name, &
' max diff:', maxval(abs(ref - new)), ' max & min ref:', maxval(ref), minval(ref), ' max & min new:', maxval(new), minval(new)
else
write(stderr,'(A,/,A,ES12.3,A,2ES12.3,A,2ES12.3)') "MISMATCH: " // file_name(new_file) // " " // name, &
' max diff:', maxval(abs(ref - new)), ' max & min ref:', maxval(ref), minval(ref), ' max & min new:', maxval(new), minval(ref)
endif
end function check_var
end module compare_h5