forked from adaptive-cfd/python-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwabbit_tools.py
1539 lines (1260 loc) · 64.4 KB
/
wabbit_tools.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 28 15:41:48 2017
@author: engels
"""
import os
import numpy as np
from scipy.interpolate import RegularGridInterpolator
import h5py
import bcolors
import copy
from inifile_tools import *
from wabbit_dense_error_tools import *
from analytical_functions import *
import logging
#------------------------------------------------------------------------------
# compute some keyvalues to compare the files
#------------------------------------------------------------------------------
def keyvalues(domain_size, dx, data):
max1, min1 = np.max(data), np.min(data)
mean1, squares1 = 0.0, 0.0
# loop over all blocks, ignore last point to only use unique grid
for i in range( data.shape[0] ):
if len(data.shape) == 3: sum_block = np.sum(data[i,:-1,:-1]) # 2D
else: sum_block = np.sum(data[i,:-1,:-1, :-1]) ## 3D
mean1 = mean1 + np.prod(dx[i,:]) * sum_block
squares1 = squares1 + np.prod(dx[i,:]) * sum_block**2
# divide integrals by area to get mean value
mean1 /= np.prod(domain_size)
squares1 /= np.prod(domain_size)
return(max1, min1, mean1, squares1 )
# this object contains all important details about a read in wabbit-file
# everything is neat and at one position and simplifies things
# A grid is uniquely defined by its dimension, block size, domain size
# The individual grid partition is uniquely defined by the number of blocks, treecode and level arrays
class WabbitHDF5file:
# lets define all objects it can possess
# first all attributes, those will always be set
# should be consistent with write attribute list in saveHDF5_tree
periodic_BC = symmetry_BC = version = block_size = time = \
iteration = total_number_blocks = max_level = dim = domain_size = []
# lets define all fields
blocks = np.array([])
coords_origin = np.array([])
coords_spacing = np.array([])
block_treecode_num = np.array([])
block_treecode = np.array([])
procs = np.array([])
refinement_status = np.array([])
level = np.array([])
lgt_ids = np.array([])
# some helping constructs
tc_dict = []
orig_file = ""
"""
python has no init overloading so I keep this empty. Always make sure to call read or set vars afterwards!
"""
def __init__(self):
pass
"""
reads in variables, we can either define to read in "all", "meta" (all but blocks) or only a single variable
"""
def read(self, file, read_var='all', verbose=True):
if verbose:
print(f"Reading {read_var} of file= {file}")
fid = h5py.File(file,'r')
dset_id = fid.get('blocks')
# read attributes - always read all attributes
version = dset_id.attrs.get('version', default=[0])
if isinstance(version, (list, np.ndarray)):
self.version = version[0]
else:
self.version = version
self.periodic_BC = dset_id.attrs.get('periodic_BC')
self.symmetry_BC = dset_id.attrs.get('symmetry_BC')
self.block_size = dset_id.attrs.get('block-size')
# for all scalar entries they can be read in as scalars or lists
self.time = dset_id.attrs.get('time', default=[None])[0]
iteration = dset_id.attrs.get('iteration', default=[None])
if isinstance(iteration, (list, np.ndarray)):
self.iteration = iteration[0]
else:
self.iteration = iteration
total_number_blocks = dset_id.attrs.get('total_number_blocks')
if isinstance(total_number_blocks, (list, np.ndarray)):
self.total_number_blocks = total_number_blocks[0]
else:
self.total_number_blocks = total_number_blocks
self.max_level = dset_id.attrs.get('max_level', default=[None])[0]
self.dim = dset_id.attrs.get('dim', default=[None])[0]
self.domain_size = dset_id.attrs.get('domain-size')
# read fields, only if requested
if read_var in ["coords_origin", "all", "meta"]:
self.coords_origin = np.array(fid['coords_origin'][:])
if read_var in ["coords_spacing", "all", "meta"]:
self.coords_spacing = np.array(fid['coords_spacing'][:])
if read_var in ["blocks", "all"]:
self.blocks = np.array(fid['blocks'], dtype=np.float64)
# very old versions do not have fancy variables
if self.version <= 20200902:
self.refinement_status = [0]*self.total_number_blocks
self.procs = [-1]*self.total_number_blocks
self.lgt_ids = [-1]*self.total_number_blocks
self.block_size = self.blocks.shape[1:]
else:
if read_var in ["refinement_status", "all", "meta"]:
self.refinement_status = np.array(fid['refinement_status'])
if read_var in ["procs", "all", "meta"]:
self.procs = np.array(fid['procs'])
if read_var in ["lgt_ids", "all", "meta"]:
self.lgt_ids = np.array(fid['lgt_ids'])
# read in treecode - dependent on version
# older version - treecode array, create dim, max_level and fields level and block_treecode_num
if self.version < 20240410:
# fill in attributes
try:
self.dim = 3 - (self.block_size[2] == 1) # 2D if only one point in z-direction
except IndexError:
self.dim = 2 # self.block_size[2] is not set
self.block_size = np.array(list(self.block_size) + [1])
if read_var in ["block_treecode", "all", "meta"]:
self.block_treecode = np.array(fid['block_treecode'])
self.max_level = self.block_treecode.shape[1]
if read_var in ["block_treecode_num", "all", "meta"]:
block_treecode = np.array(fid['block_treecode'])
self.max_level = self.block_treecode.shape[1]
self.block_treecode_num = tca_2_tcb(block_treecode, dim=self.dim, max_level=self.max_level)
if read_var in ["level", "all", "meta"]:
block_treecode = np.array(fid['block_treecode'])
self.max_level = self.block_treecode.shape[1]
self.level = tca_2_level(block_treecode).astype(int)
# new version - reconstruct block_treecode for compatibility
else:
if read_var in ["block_treecode", "all", "meta"]:
block_treecode_num = np.array(fid['block_treecode_num'])
level = np.array(fid['level'])
self.block_treecode = tcb_level_2_tcarray(block_treecode_num, level, self.max_level, self.dim)
if read_var in ["block_treecode_num", "all", "meta"]:
self.block_treecode_num = np.array(fid['block_treecode_num'])
if read_var in ["level", "all", "meta"]:
self.level = np.array(fid['level'])
# watch for block_size
if self.version == 20200408 or self.version >= 20231602:
self.block_size[:self.dim] += 1
#print("!!!Warning old (old branch: newGhostNodes) version of wabbit format detected!!!")
else:
print("This file includes redundant points")
# close and we are happy
fid.close()
# create objects which are handy to have
# dictionary to quickly check if a block exists
self.tc_dict = {(self.block_treecode_num[j], self.level[j]): True for j in range(self.total_number_blocks)}
self.orig_file = file
# init a wabbit state by data
# A grid is uniquely defined by its dimension (from blocks), block size (from blocks), domain size
# The individual grid partition is uniquely defined by the number of blocks (from blocks), treecode and level arrays
# for time knowledge we set the time and iteration as well
def fill_vars(self, domain_size, blocks, treecode, level, time, iteration, max_level=21):
self.dim = len(blocks.shape[1:])
self.block_size = np.array(blocks.shape[1:])
if self.dim==2: self.block_size = np.append(self.block_size, 1)
self.domain_size = np.array(domain_size)
self.blocks = blocks
self.block_treecode_num = treecode
self.level = level
self.total_number_blocks = blocks.shape[0]
self.time = time
self.iteration = iteration
# compute attrs which are not set
self.version = 20240410
self.periodic_BC = np.array([1, 1, 1])
self.symmetry_BC = np.array([0, 0, 0])
self.max_level = max_level # for now set to max
# set fields for meta data of blocks
self.lgt_ids = np.arange(self.total_number_blocks)
self.procs = np.zeros(self.total_number_blocks)
self.refinement_status = np.zeros(self.total_number_blocks)
self.block_treecode = tcb_level_2_tcarray(treecode, level, self.max_level, self.dim)
self.coords_origin = np.zeros([self.total_number_blocks, self.dim])
self.coords_spacing = np.zeros([self.total_number_blocks, self.dim])
for i_b in range(self.total_number_blocks):
self.coords_origin[i_b, :] = treecode2origin(self.block_treecode_num[i_b], self.max_level, self.dim, self.domain_size)
self.coords_spacing[i_b, :] = level2spacing(self.level[i_b], self.dim, self.block_size, self.domain_size)
# init values from a matrix and set them into a grid on uniform level
def fill_from_matrix(self, block_values, bs, domain_size=[1,1,1], dim=3, max_level=21, time=0.0, iteration=0):
# extract level from size of array
level_num = np.log2(block_values.shape[0]/bs[0])
if int(level_num) != level_num:
print(f"Input array has wrong size: {block_values.shape[0]}. Ensure size/bs is a power of 2")
return
level_num = int(level_num)
# alter values, we need to copy first line as we have redundant setting - this assumes periodicity
block_red = np.zeros(np.array(block_values.shape)+1)
if dim == 2:
block_red[:-1,:-1] = block_values[:,:] # copy interior
block_red[ -1,:-1] = block_values[0,:] # copy x-line
block_red[:-1, -1] = block_values[:,0] # copy y-line
block_red[ -1, -1] = block_values[0,0] # copy last corner
else:
block_red[:-1,:-1,:-1] = block_values[:,:,:] # copy interior
block_red[ -1,:-1,:-1] = block_values[0,:,:] # copy x-face
block_red[:-1, -1,:-1] = block_values[:,0,:] # copy y-face
block_red[:-1,:-1, -1] = block_values[:,:,0] # copy z-face
block_red[ -1, -1,:-1] = block_values[0,0,:] # copy xy-edge
block_red[ -1,:-1, -1] = block_values[0,:,0] # copy xz-edge
block_red[:-1, -1, -1] = block_values[:,0,0] # copy yz-edge
block_red[ -1, -1, -1] = block_values[0,0,0] # copy last corner
number_blocks = 2**(level_num*dim)
treecode = np.zeros(number_blocks)
level = np.ones(number_blocks)*level_num
if dim == 2:
blocks = np.zeros([number_blocks, bs[0]+1, bs[1]+1])
else:
blocks = np.zeros([number_blocks, bs[0]+1, bs[1]+1, bs[2]+1])
# prepare treecode
for i_b in range(number_blocks):
# encoding is 1-based
if dim == 2:
ix, iy = i_b//(2**level_num)+1, i_b%(2**level_num)+1
tc = tc_encoding([ix, iy], level=level_num, max_level=max_level, dim=dim)
else:
ix, iy, iz = i_b//(2**(2*level_num))+1, (i_b//(2**level_num))%(2**level_num)+1, i_b%(2**(level_num))+1
tc = tc_encoding([ix, iy, iz], level=level_num, max_level=max_level, dim=dim)
treecode[i_b] = int(tc)
# fill blocks array by transcribing part of array
for i_b in range(number_blocks):
if dim == 2:
ix, iy = i_b//(2**level_num), i_b%(2**level_num)
ix, iy = ix*bs[0], iy*bs[1]
blocks[i_b, :, :] = block_red[ix:ix+bs[0]+1, iy:iy+bs[1]+1].transpose(1, 0)
else:
ix, iy, iz = i_b//(2**(2*level_num)), (i_b//(2**level_num))%(2**level_num), i_b%(2**(level_num))
ix, iy, iz = ix*bs[0], iy*bs[1], iz*bs[2]
blocks[i_b, :, :, :] = block_red[ix:ix+bs[0]+1, iy:iy+bs[1]+1, iz:iz+bs[2]+1].transpose(2, 1, 0)
self.fill_vars(domain_size, blocks, treecode, level, time, iteration, max_level)
# let it write itself
def write(self, file, verbose=True):
""" Write data from wabbit to an HDF5 file
Note: hdf5 saves the arrays in [Nz, Ny, Nx] order!
So: data.shape = Nblocks, Bs[3], Bs[2], Bs[1]
"""
if verbose:
print("~~~~~~~~~~~~~~~~~~~~~~~~~")
print(f"Writing file= {file}")
fid = h5py.File( file, 'w')
# those are necessary for wabbit
fid.create_dataset( 'blocks', data=self.blocks, dtype=np.float64)
fid.create_dataset( 'block_treecode_num', data=self.block_treecode_num, dtype=np.int64)
fid.create_dataset( 'level', data=self.level, dtype=np.int32)
# those are optional and not read in from wabbit
fid.create_dataset( 'coords_origin', data=self.coords_origin, dtype=np.float64)
fid.create_dataset( 'coords_spacing', data=self.coords_spacing, dtype=np.float64)
fid.create_dataset( 'block_treecode', data=self.block_treecode, dtype=np.int32)
fid.create_dataset( 'refinement_status', data=self.refinement_status, dtype=np.int32)
fid.create_dataset( 'procs', data=self.procs, dtype=np.int32)
fid.create_dataset( 'lgt_ids', data=self.lgt_ids, dtype=np.int32)
fid.close()
# watch for block_size
if self.version == 20200408 or self.version >= 20231602:
self.block_size[:self.dim] -= 1
# write attributes
# those are necessary for wabbit
fid = h5py.File(file,'a')
dset_id = fid.get( 'blocks' )
dset_id.attrs.create( "version", [20240410], dtype=np.int32) # this is used to distinguish wabbit file formats
dset_id.attrs.create( "block-size", self.block_size, dtype=np.int32) # this is used to distinguish wabbit file formats
dset_id.attrs.create('time', [self.time], dtype=np.float64)
dset_id.attrs.create('iteration', [self.iteration], dtype=np.int32)
dset_id.attrs.create('max_level', [self.max_level], dtype=np.int32)
dset_id.attrs.create('dim', [self.dim], dtype=np.int32)
dset_id.attrs.create('domain-size', self.domain_size, dtype=np.float64)
dset_id.attrs.create('total_number_blocks', [self.total_number_blocks], dtype=np.int32)
dset_id.attrs.create('periodic_BC', self.periodic_BC, dtype=np.int32)
dset_id.attrs.create('symmetry_BC', self.symmetry_BC, dtype=np.int32)
# repair block_size
if self.version == 20200408 or self.version >= 20231602:
self.block_size[:self.dim] += 1
# those are optional and not read in from wabbit
# currently none
fid.close()
# for large data, we do not want to read all block values at once, as it is simply not feasible
# however, we might still want to read or write single blocks, so those functions deal with that
def block_read(self, i_b, file=None):
# some safety checks
file_read = self.orig_file if not file else file
if not file_read:
print("Tried to access a single block but no file is given")
return None
if not self.total_number_blocks:
print("Tried to access a single block before the WabbitStateOject was initialized?")
return None
if i_b < 0 or i_b >= self.total_number_blocks:
print("Tried to access a block outside block range")
return None
fid = h5py.File(file_read,'r')
block = fid['blocks'][i_b, :]
fid.close()
return block
def block_write(self, i_b, block, file=None):
# some safety checks
file_write = self.orig_file if not file else file
if not file_write:
print("Tried to access a single block but no file is given")
return None
if not self.total_number_blocks:
print("Tried to access a single block before the WabbitStateOject was initialized?")
return None
if i_b < 0 or i_b >= self.total_number_blocks:
print("Tried to access a block outside block range")
return None
fid = h5py.File(file_write,'r')
fid['blocks'][i_b, :] = block # we assume here that block sizes are equal
fid.close()
return
# define the == operator for objects
# this is only true if objects are 100% similar
# this is not the case for different simulations so use other function for that
def __eq__(self, other):
# check if both are wabbit objects
isWabbit = isinstance(other, self.__class__)
if not isWabbit:
return False
# literally check attributes
if not np.all(self.block_size == other.block_size): return False
if not self.time == other.time: return False
if not self.iteration == other.iteration: return False
if not self.max_level == other.max_level: return False
if not self.dim == other.dim: return False
if not np.all(self.domain_size == other.domain_size): return False
if not self.total_number_blocks == other.total_number_blocks: return False
if not np.all(self.periodic_BC == other.periodic_BC): return False
if not np.all(self.symmetry_BC == other.symmetry_BC): return False
# check important fields
if np.linalg.norm(self.blocks - other.blocks) != 0: return False
if np.linalg.norm(self.block_treecode_num - other.block_treecode_num) != 0: return False
if np.linalg.norm(self.level - other.level) != 0: return False
# puhhh everything is passed so both are equal
return True
# overwrite + operator, can handle other wabbitstatefiles or scalars as integers/floats
def __add__(self, other):
new_obj = copy.deepcopy(self)
if isinstance(other, WabbitHDF5file):
equal_grid = self.compareGrid(other)
equal_attr = self.compareAttr(other)
if not equal_grid:
print(bcolors.FAIL + f"WARNING: Grids are not equal, operation interpolated for non-consistent blocks- This might take a while" + bcolors.ENDC)
grid_interpolator = other.create_interpolator()
if not equal_attr:
print(bcolors.WARNING + f"ERROR: Attributes are not equal" + bcolors.ENDC)
return None
# blocks are not structured similarly so we have to apply blockwise
for i_blocks in range(new_obj.total_number_blocks):
i_other = other.get_block_id(self.block_treecode_num[i_blocks], self.level[i_blocks])
if (i_other != -1):
new_obj.blocks[i_blocks, :] = new_obj.blocks[i_blocks, :] + other.blocks[i_other, :]
else:
new_obj.blocks[i_blocks, :] = new_obj.blocks[i_blocks, :] + \
other.interpolate_block(new_obj.blocks[i_blocks, :], new_obj.coords_origin[i_blocks], new_obj.coords_spacing[i_blocks], grid_interpolator)
elif isinstance(other, (int, float, np.integer, np.floating)):
new_obj.blocks[:] += other
return new_obj
# overwrite - operator, can handle other wabbitstatefiles or scalars as integers/floats
def __sub__(self, other):
new_obj = copy.deepcopy(self)
if isinstance(other, WabbitHDF5file):
equal_grid = self.compareGrid(other)
equal_attr = self.compareAttr(other)
if not equal_grid:
print(bcolors.FAIL + f"WARNING: Grids are not equal, operation interpolated for non-consistent blocks- This might take a while" + bcolors.ENDC)
grid_interpolator = other.create_interpolator()
if not equal_attr:
print(bcolors.WARNING + f"ERROR: Attributes are not equal" + bcolors.ENDC)
return None
# blocks are not structured similarly so we have to apply blockwise
for i_blocks in range(new_obj.total_number_blocks):
i_other = other.get_block_id(self.block_treecode_num[i_blocks], self.level[i_blocks])
if (i_other != -1):
new_obj.blocks[i_blocks, :] = new_obj.blocks[i_blocks, :] - other.blocks[i_other, :]
else:
new_obj.blocks[i_blocks, :] = new_obj.blocks[i_blocks, :] - \
other.interpolate_block(new_obj.blocks[i_blocks, :], new_obj.coords_origin[i_blocks], new_obj.coords_spacing[i_blocks], grid_interpolator)
elif isinstance(other, (int, float, np.integer, np.floating)):
new_obj.blocks[:] -= other
return new_obj
# overwrite * operator, can handle other wabbitstatefiles or scalars as integers/floats
def __mul__(self, other):
new_obj = copy.deepcopy(self)
if isinstance(other, WabbitHDF5file):
equal_grid = self.compareGrid(other)
equal_attr = self.compareAttr(other)
if not equal_grid:
print(bcolors.FAIL + f"WARNING: Grids are not equal, operation interpolated for non-consistent blocks- This might take a while" + bcolors.ENDC)
grid_interpolator = other.create_interpolator()
if not equal_attr:
print(bcolors.WARNING + f"ERROR: Attributes are not equal" + bcolors.ENDC)
return None
new_obj = copy.deepcopy(self)
# blocks are not structured similarly so we have to apply blockwise
for i_blocks in range(new_obj.total_number_blocks):
i_other = other.get_block_id(self.block_treecode_num[i_blocks], self.level[i_blocks])
if (i_other != -1):
new_obj.blocks[i_blocks, :] = new_obj.blocks[i_blocks, :] * other.blocks[i_other, :]
else:
new_obj.blocks[i_blocks, :] = new_obj.blocks[i_blocks, :] * \
other.interpolate_block(new_obj.blocks[i_blocks, :], new_obj.coords_origin[i_blocks], new_obj.coords_spacing[i_blocks], grid_interpolator)
elif isinstance(other, (int, float, np.integer, np.floating)):
new_obj.blocks[:] *= other
return new_obj
# overwrite / operator, can handle other wabbitstatefiles or scalars as integers/floats
def __truediv__(self, other):
new_obj = copy.deepcopy(self)
if isinstance(other, WabbitHDF5file):
equal_grid = self.compareGrid(other)
equal_attr = self.compareAttr(other)
if not equal_grid:
print(bcolors.FAIL + f"WARNING: Grids are not equal, operation interpolated for non-consistent blocks- This might take a while" + bcolors.ENDC)
grid_interpolator = other.create_interpolator()
if not equal_attr:
print(bcolors.WARNING + f"ERROR: Attributes are not equal" + bcolors.ENDC)
return None
new_obj = copy.deepcopy(self)
# blocks are not structured similarly so we have to apply blockwise
for i_blocks in range(new_obj.total_number_blocks):
i_other = other.get_block_id(self.block_treecode_num[i_blocks], self.level[i_blocks])
if (i_other != -1):
new_obj.blocks[i_blocks, :] = new_obj.blocks[i_blocks, :] / other.blocks[i_other, :]
else:
new_obj.blocks[i_blocks, :] = new_obj.blocks[i_blocks, :] / \
other.interpolate_block(new_obj.blocks[i_blocks, :], new_obj.coords_origin[i_blocks], new_obj.coords_spacing[i_blocks], grid_interpolator)
elif isinstance(other, (int, float, np.integer, np.floating)):
new_obj.blocks[:] /= other
return new_obj
# given a level and treecode, give me the block ID
def get_block_id(self, treecode, level):
if (treecode, level) not in self.tc_dict: return -1
return list(zip(self.block_treecode_num, self.level)).index((treecode, level))
# check if logically two objects are considered to be close to equal
def isClose(self, other, verbose=True, logger=None, return_norm=False):
# check if grid attributes are equal
attr_similarity = self.compareAttr(other, logger=logger)
if not attr_similarity:
if verbose:
text_now = bcolors.FAIL + f"ERROR: Grid attributes are note qual" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
return False
# check if grids are equal
grid_similarity = self.compareGrid(other, logger=logger)
grid_interpolator = ()
if not grid_similarity:
if verbose:
text_now = bcolors.FAIL + f"ERROR: Grid is not equal, interpolating the difference. This might take a while" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
grid_interpolator = other.create_interpolator()
# return False
# check key values of data
max1, min1, mean1, squares1 = keyvalues(self.domain_size, self.coords_spacing, self.blocks)
max2, min2, mean2, squares2 = keyvalues(other.domain_size, other.coords_spacing, other.blocks)
#------------------------------------------------------------------------------
# compute L2 norm of difference, but only if the grids are identical
#------------------------------------------------------------------------------
diff_L2 = 0.0
diff_LInf = 0.0
norm_L2 = 0.0
error_L2 = np.nan
error_LInf = np.nan
for i in range(self.total_number_blocks):
# normalization is norm of data1
norm_L2 = norm_L2 + np.linalg.norm( np.ndarray.flatten(self.blocks[i,:]) )
# L2 and Linfty difference, last point is ignored as this is a redundant point
j = other.get_block_id(self.block_treecode_num[i], self.level[i])
if j != -1:
if self.dim==2:
diff_L2 = diff_L2 + np.linalg.norm( np.ndarray.flatten(self.blocks[i,:-1,:-1]-other.blocks[j,:-1,:-1]) )
diff_LInf = np.max([diff_LInf, np.linalg.norm( np.ndarray.flatten(self.blocks[i,:-1,:-1]-other.blocks[j,:-1,:-1]) , ord=np.inf)])
else:
diff_L2 = diff_L2 + np.linalg.norm( np.ndarray.flatten(self.blocks[i,:-1,:-1,:-1]-other.blocks[j,:-1,:-1,:-1]) )
diff_LInf = np.max([diff_LInf, np.linalg.norm( np.ndarray.flatten(self.blocks[i,:-1,:-1,:-1]-other.blocks[j,:-1,:-1,:-1]) , ord=np.inf)])
else:
if self.dim==2:
diff_block = self.blocks[i, :-1,:-1] - other.interpolate_block(self.blocks[i, :-1,:-1], self.coords_origin[i], self.coords_spacing[i], grid_interpolator)
else:
diff_block = self.blocks[i, :-1,:-1,:-1] - other.interpolate_block(self.blocks[i, :-1,:-1,:-1], self.coords_origin[i], self.coords_spacing[i], grid_interpolator)
diff_L2 = diff_L2 + np.linalg.norm( np.ndarray.flatten(diff_block) )
diff_LInf = np.max([diff_LInf, np.linalg.norm( np.ndarray.flatten(diff_block) , ord=np.inf)])
if norm_L2 >= 1.0e-10:
# relative error
error_L2 = diff_L2 / norm_L2
error_LInf = diff_LInf # not normed
else:
# absolute error
error_L2 = diff_L2
error_LInf = diff_LInf
if verbose:
text_now = f"First : max={max1:12.5e}, min ={min1:12.5e}, mean={mean1:12.5e}, squares={squares1:12.5e}"
if logger==None: print(text_now)
else: logger.info(text_now)
text_now = f"Second: max={max2:12.5e}, min ={min2:12.5e}, mean={mean2:12.5e}, squares={squares2:12.5e}"
if logger==None: print(text_now)
else: logger.info(text_now)
text_now = f"Error : L2 ={error_L2:12.5e}, LInfty={error_LInf:12.5e}"
if logger==None: print(text_now)
else: logger.info(text_now)
if error_L2 <= 1.0e-13:
text_now = "GREAT: The files can be deemed as equal"
if logger==None: print(text_now)
else: logger.info(text_now)
else:
text_now = bcolors.FAIL + "ERROR: The files do not match" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
if return_norm: return error_L2 <= 1.0e-13, error_L2, error_LInf
return error_L2 <= 1.0e-13
# check if grid is equal or not, with fractional we compute the fraction of treecodes which are different
def compareGrid(self, other, fractional=False, verbose=True, logger=None):
if self.total_number_blocks != other.total_number_blocks:
if verbose:
text_now = bcolors.FAIL + f"ERROR: We have a different number of blocks - {self.total_number_blocks} vs {other.total_number_blocks}" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
return False
mismatch_count = 0
# Iterate through self once, checking against the dictionary
for i in range(self.block_treecode_num.shape[0]):
if (self.block_treecode_num[i], self.level[i]) not in other.tc_dict:
mismatch_count += 1
if not fractional:
if verbose:
text_now = bcolors.FAIL + f"ERROR: treecode not matching" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
return False # Early exit if not computing fractional and a mismatch is found
if fractional:
return 1 - mismatch_count / self.block_treecode_num.shape[0]
else:
return True
# check if position and other details about the grid are equal
# A grid is uniquely defined by its dimension, block size, domain size
# The individual grid partition is uniquely defined by the number of blocks, treecode and level arrays
def compareAttr(self, other, verbose=True, logger=None):
# check global grid attributes
if self.dim != other.dim:
if verbose:
text_now = bcolors.FAIL + f"ERROR: Grids are not in the same dimension, we have to leave the matrix - {self.dim} vs {other.dim}" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
return False
if not np.all(self.block_size[1:self.dim] == other.block_size[1:other.dim]):
if verbose:
text_now = bcolors.FAIL + f"ERROR: Block sizes are different - {self.block_size} vs {other.block_size}" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
return False
if np.any(self.domain_size[1:self.dim] != other.domain_size[1:other.dim]):
if verbose:
text_now = bcolors.FAIL + f"ERROR: Domain size is different - {self.domain_size} vs {other.domain_size}" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
return False
return True
# check if objects are at the same time instant, pretty simple but why not have a function for it
# round_digits is needed as floating points do not like direct comparisons
def compareTime(self, other, verbose=True, round_digits=12, logger=None):
similar_time = (np.round(self.time, round_digits) == np.round(other.time, round_digits))
if not similar_time and verbose:
text_now = bcolors.FAIL + f"ERROR: times are not equal" + bcolors.ENDC
if logger==None: print(text_now)
else: logger.info(text_now)
return similar_time
# try to parse the variable name from the file it was read in with
def var_from_filename(self, verbose=True):
fname = self.orig_file
# only get actualy filename not path
if "/" in fname: fname = fname.split("/")[-1]
# var name is in beginning
fname = fname.split("_")[0]
# basic fail-safe: check if there was any splitting done at all
if fname == self.orig_file:
if verbose: print(bcolors.FAIL + f"ERROR: I do not know how to parse the variable name from this file-name - {self.orig_file}" + bcolors.ENDC)
return []
else: return fname
# try to parse the variable name from the file it was read in with
def time_from_filename(self, out_str=True, verbose=True):
fname = self.orig_file
# only get actualy filename not path
if "/" in fname: fname = fname.split("/")[-1]
# var name is in beginning
if not "_" in fname:
if verbose: print(bcolors.FAIL + f"ERROR: I do not know how to parse the variable name from this file-name - {self.orig_file}" + bcolors.ENDC)
return
fname = fname.split("_")[1]
# basic fail-safe: check if there was any splitting done at all
if fname == self.orig_file:
if verbose: print(bcolors.FAIL + f"ERROR: I do not know how to parse the variable name from this file-name - {self.orig_file}" + bcolors.ENDC)
return
# output either as str or number, convention is that last 6 digits are after dot
if out_str: return fname
else: return int(fname) / 1e6
# some informations
def get_max_min_level(self):
return np.min(self.level), np.max(self.level)
# interpolate the values of a block given by its position and spacing to compute norm difference with different grids
def interpolate_block(self, block, coords_origin, coords_spacing, interpolator):
block_out = np.zeros_like(block)
# compute ends of blocks to find in which block a point lays
self_coords_end = self.coords_origin + self.coords_spacing * (np.array(self.blocks.shape[1:]) -1)
for i_x in range(block.shape[0]):
for i_y in range(block.shape[1]):
if len(block.shape) == 3: range_z = range(block.shape[2])
else: range_z = [0]
for i_z in range_z:
# coord of this point
if len(block.shape) == 3: i_xyz = np.array((i_x, i_y, i_z))
else: i_xyz = np.array((i_x, i_y))
coords_point = coords_origin + i_xyz * coords_spacing
# find corresponding block
b_id = np.where(np.all(coords_point >= self.coords_origin, axis=1) & np.all(coords_point <= self_coords_end, axis=1))[0]
# interpolate in this block
if len(block.shape) == 3: block_out[i_x, i_y, i_z] = interpolator[b_id[0]](coords_point)
else: block_out[i_x, i_y] = interpolator[b_id[0]](coords_point)
return block_out
# create regular grid interpolators for every single block so that we can reuse it
def create_interpolator(self):
# compute ends of blocks to find in which block a point lays
self_coords_end = self.coords_origin + self.coords_spacing * (np.array(self.blocks.shape[1:]) -1)
interpolators = []
for i_block in range(self.total_number_blocks):
x_coords = []
for i_dim in range(self.dim):
x_coords.append(np.linspace(self.coords_origin[i_block, i_dim], self_coords_end[i_block, i_dim], self.blocks.shape[1+i_dim]))
interpolators.append(RegularGridInterpolator(x_coords, self.blocks[i_block, :]))
return interpolators
# in order to create analytical results on the same grid we might want to replace all values with that to study it
def replace_values_with_function(self, function):
for i_block in range(self.total_number_blocks):
block = self.blocks[i_block, :]
for i_x in range(block.shape[0]):
for i_y in range(block.shape[1]):
if len(block.shape) == 3: range_z = range(block.shape[2])
else: range_z = [0]
for i_z in range_z:
# coord of this point
if len(block.shape) == 3: i_xyz = np.array((i_x, i_y, i_z))
else: i_xyz = np.array((i_x, i_y))
coords_point = self.coords_origin[i_block] + i_xyz * self.coords_spacing[i_block]
# replace function values
fun_val = function(coords_point)
if len(block.shape) == 4: self.blocks[i_block, i_x, i_y, i_z] = fun_val
else: self.blocks[i_block, i_x, i_y] = fun_val
#
def block_level_distribution( wabbit_obj: WabbitHDF5file ):
""" Read a 2D/3D wabbit file and return a list of how many blocks are at the different levels
"""
counter = np.zeros(wabbit_obj.max_level)
# fetch level for each block and count
for i_level in range(1, wabbit_obj.max_level+1):
counter[i_level - 1] = np.sum(wabbit_obj.level == i_level)
return counter.astype(int)
def block_proc_level_distribution( wabbit_obj: WabbitHDF5file ):
""" Read a 2D/3D wabbit file and return a 2D list of how many blocks are at the different levels for each proc
"""
counter = np.zeros([np.max(wabbit_obj.procs), wabbit_obj.max_level])
# loop over all blocks and add counter of proc and level
for i_block in range(1, wabbit_obj.total_number_blocks):
counter[wabbit_obj.procs[i_block]-1, wabbit_obj.level[i_block]-1] += 1
return counter.astype(int)
def read_wabbit_hdf5_dir(dir):
""" Read all h5 files in directory dir.
Return time, x0, dx, box, data, treecode.
Use data["phi"][it] to reference quantity phi at iteration it
"""
import numpy as np
import re
import ntpath
import os
it=0
w_obj_list, var_list = [], []
# we loop over all files in the given directory
for file in os.listdir(dir):
# filter out the good ones (ending with .h5)
if file.endswith(".h5"):
# from the file we can get the fieldname
fieldname=re.split('_',file)[0]
print(fieldname)
wabbit_obj = WabbitHDF5file()
wabbit_obj.read(os.path.join(dir, file))
w_obj_list.append(wabbit_obj)
var_list.append(wabbit_obj.var_from_filename())
return w_obj_list, var_list
def add_convergence_labels(dx, er, ax=None):
"""
This generic function adds the local convergence rate as nice labels between
two datapoints of a convergence rate (see https://arxiv.org/abs/1912.05371 Fig 3E)
Input:
------
dx: np.ndarray
The x-coordinates of the plot
dx: np.ndarray
The y-coordinates of the plot
Output:
-------
Print to figure.
"""
import numpy as np
import matplotlib.pyplot as plt
if ax is None:
ax = plt.gca()
for i in range(len(dx)-1):
x = 10**( 0.5 * ( np.log10(dx[i]) + np.log10(dx[i+1]) ) )
y = 10**( 0.5 * ( np.log10(er[i]) + np.log10(er[i+1]) ) )
order = "%2.1f" % ( convergence_order(dx[i:i+1+1],er[i:i+1+1]) )
ax.text(x, y, order, horizontalalignment='center', verticalalignment='center',
bbox=dict(facecolor='w', alpha=0.75, edgecolor='none'), fontsize=7 )
def convergence_order(N, err):
""" This is a small function that returns the convergence order, i.e. the least
squares fit to the log of the two passed lists.
"""
import numpy as np
if len(N) != len(err):
raise ValueError('Convergence order args do not have same length')
A = np.ones([len(err), 2])
B = np.ones([len(err), 1])
# ERR = A*N + B
for i in range( len(N) ) :
A[i,0] = np.log(N[i])
B[i] = np.log(err[i])
x, residuals, rank, singval = np.linalg.lstsq(A, B, rcond=None)
return x[0]
def logfit(N, err):
""" This is a small function that returns the logfit, i.e. the least
squares fit to the log of the two passed lists.
"""
import numpy as np
if len(N) != len(err):
raise ValueError('Convergence order args do not have same length')
A = np.ones([len(err), 2])
B = np.ones([len(err), 1])
# ERR = A*N + B
for i in range( len(N) ) :
A[i,0] = np.log10(N[i])
B[i] = np.log10(err[i])
x, residuals, rank, singval = np.linalg.lstsq(A, B, rcond=None)
return x
def linfit(N, err):
""" This is a small function that returns the linfit, i.e. the least
squares fit to the log of the two passed lists.
"""
import numpy as np
if len(N) != len(err):
raise ValueError('Convergence order args do not have same length')
A = np.ones([len(err), 2])
B = np.ones([len(err), 1])
# ERR = A*N + B
for i in range( len(N) ) :
A[i,0] = N[i]
B[i] = err[i]
x, residuals, rank, singval = np.linalg.lstsq(A, B, rcond=None)
return x
def plot_wabbit_dir(d, **kwargs):
import glob
files = glob.glob(os.path.join(d,'*.h5'))
files.sort()
for file in files:
plot_wabbit_file(file, **kwargs)
# extract from numerical treecode the digit at a specific level
def tc_get_digit_at_level(tc_b, level, max_level=21, dim=3):
result = (tc_b // (2**(dim*(max_level - level))) % (2**dim))
if isinstance(tc_b, np.ndarray):
return result.astype(int)
else:
return int(result)
# set for numerical treecode the digit at a specific level
def tc_set_digit_at_level(tc_b, digit, level, max_level=21, dim=3):
# compute digit that is currently there
result = (tc_b // (2**(dim*(max_level - level))) % (2**dim))
# subtract old digit, add new one
tc_b += (-result + digit) * (2**(dim*(max_level - level)))
if isinstance(tc_b, np.ndarray):
return tc_b.astype(int)
else:
return int(tc_b)
# similar to encoding function in wabbit, ixyz are one-based ([1,1] or [1,1,1] give TC 0)
def tc_encoding(ixyz, level=21, max_level=21, dim=3):
if np.any(np.array(ixyz) < 1) or np.any(np.array(ixyz) > 2**level):
print(f"Invalid coordinates {ixyz}, ensure they are 1 <= ixyz <= {2**level}")
return -1
# one-based encoding, so subtract the 1
ixyz_n = np.array(ixyz) - 1
tc = 0
p_arr = [1, 0, 2] # y and x are interchanged for tc encoding
# Loop over all bits set in index
for i_dim in range(len(ixyz_n)):
p_dim = p_arr[i_dim]
for i_level in range(int(ixyz_n[i_dim]).bit_length()):
bit = (ixyz_n[i_dim] >> i_level) & 1
if bit:
# max for if one forgets to set the level
tc += bit << ((i_level) * dim + p_dim + max(max_level-level, 0)*dim)
return tc
def tc_decoding(treecode, level=None, dim=3, max_level=21):
"""
Obtain block position coordinates from numerical binary treecode.
Works for 2D and 3D. Considers each digit and adds their level-shift to each coordinate.
Parameters:
- treecode: int, treecode value
- level: int, level at which to encode, can be negative to set from max_level
- dim: int, dimension (2 or 3), defaults to 3
- max_level: int, max level possible, should be set after params%Jmax
Returns:
- ix: list of int, block position coordinates
"""
n_level = max_level if level is None else level
if n_level < 0: n_level = max_level + n_level + 1
# 1-based
ix = [1] * dim
for i_level in range(n_level):
for i_dim in range(dim):
shift = (i_level + max_level - n_level) * dim + i_dim
bit = (treecode >> shift) & 1
ix[i_dim] += bit << i_level
ix[0], ix[1] = ix[1], ix[0]
return ix
# get string representation of binary treecode
def tc_to_str(tc_b, level, max_level=21, dim=3):
tc_str = ""
for i_level in np.arange(level)+1:
tc_str += str(tc_get_digit_at_level(tc_b, i_level, max_level, dim))
return tc_str
# take level and numerical treecode and convert to treecode array
def tcb_level_2_tcarray(tc_b, level, max_level=21, dim=3):
tc_array = np.zeros((tc_b.shape[0], max_level))
# extract number of each level
# level <= i_level ensures -1 values are inserted for unset levels
for i_level in np.arange(max_level)+1:
tc_array[:, i_level-1] = tc_get_digit_at_level(tc_b, i_level, max_level=max_level, dim=dim) - (level <= i_level)
return tc_array
# extract level from treecode array, assume field
def tca_2_level(tca):
level = np.zeros(tca.shape[0]).astype(int)
# increase level by one if number is not -1
for i_level in range(0, tca.shape[1]):
level[tca[:, i_level] != -1] += 1
return level