Advertisement
mabruzzo

comparison-script

Feb 6th, 2023
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. import matplotlib
  2. matplotlib.use('Agg')
  3. import yt
  4.  
  5. def scan_blocklist(ds):
  6.     leaf_blocks = {}
  7.     other_blocks = {}
  8.     for g in ds.index.grids:
  9.         tmp = (g.block_name, g.filename)
  10.         if (g.Level < 0) or (len(g.Children) > 1):
  11.             other_blocks[g.block_name] = (g.filename, g.id)
  12.         else:
  13.             leaf_blocks[g.block_name] = (g.filename, g.id)
  14.        
  15.     return leaf_blocks, other_blocks
  16.  
  17.  
  18. def walk_block_pairs(ds1, ds2):
  19.     # this actually seems like a fundamental datatype
  20.  
  21.     leaf_blocks_1, _ = scan_blocklist(ds1)
  22.     leaf_blocks_2, _ = scan_blocklist(ds2)
  23.  
  24.     num = len(leaf_blocks_1)
  25.     assert num == len(leaf_blocks_2)
  26.  
  27.     for i, (key, val_1) in enumerate(leaf_blocks_1.items()):
  28.         print(f"{i+1}/{num}: {key}")
  29.         assert key in leaf_blocks_2
  30.         val_2 = leaf_blocks_2[key]
  31.         yield key, val_1, val_2
  32.  
  33. def get_fields_to_compare(blocklist_path):
  34.     ds = yt.load(blocklist_path)
  35.     particle_section = ds.parameters['Particle']
  36.     if 'list' in ds.parameters['Particle']:
  37.         particle_types = ds.parameters['Particle']['list']
  38.     else:
  39.         particle_types = []
  40.  
  41.     fields = []
  42.     for field in ds.field_list:
  43.         if field[0] in ['enzoe', 'enzop']:
  44.             fields.append(field)
  45.         elif field[0] in particle_types:
  46.             fields.append(field)
  47.     return fields
  48.  
  49. def compare_leafs(blocklist_path_ref, blocklist_path_other):
  50.  
  51.     fields_to_compare = get_fields_to_compare(blocklist_path_ref)
  52.  
  53.     ds1 = yt.load(blocklist_path_ref)
  54.     ds2 = yt.load(blocklist_path_other)
  55.  
  56.     for name, (fname1,id1), (fname2,id2) in walk_block_pairs(ds1, ds2):
  57.         g1 = ds1.index.grids[id1]
  58.         g2 = ds2.index.grids[id2]
  59.  
  60.         for field in fields_to_compare:
  61.             if not (g1[field] == g2[field]).all():
  62.                 raise RuntimeError(f'mismatch of {field}')
  63.         g1.clear_data()
  64.         g2.clear_data()
  65.  
  66. if __name__ == '__main__':
  67.     blocklist_path_ref = #path/to/reference/output
  68.     blocklist_path_other = #path/to/alt/output
  69.     compare_leafs(blocklist_path_ref, blocklist_path_other)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement