DISPATCH
demo.py
1 # -*- coding: utf-8 -*-
2 """
3 Created on Sun Jul 29 20:00:32 2018
4 
5 @author: Aake
6 """
7 
8 # Pythn 2/3 compatibility
9 from __future__ import print_function
10 
11 import os
12 import numpy as np
13 
14 import dispatch
15 import dispatch.select as ds
16 
17 def demo(iout=1,run='',data='../data'):
18  """ Demonstrates the use of packet procedures"""
19  snapfile=os.path.join(data,run,'snapshots.dat')
20  assert os.path.isfile(snapfile), 'the file '+snapfile+' must exist'
21  original = np.get_printoptions()
22  np.set_printoptions(formatter={'float': '{:6.3f}'.format})
23  #
24  def _print(s):
25  e='=================================='
26  l1=len(e)-len(s)/2
27  l2=len(e)*2-len(s)-l1
28  print(e[0:l1],s,e[0:l2])
29  #
30  _print('maxloc:')
31  a=np.zeros((2,3,4))
32  for i in range(a.shape[0]):
33  for j in range(a.shape[1]):
34  for k in range(a.shape[2]):
35  a[i,j,k]=(i-1)**2+(j-2)**3+(k-2)**4
36  i = ds.maxloc(a)
37  print ('maxloc(a) =', i, ', value =', a[i[0],i[1],i[2]])
38  _print('minloc:')
39  i = ds.minloc(a)
40  print ('minloc(a) =', i, ', value =', a[i[0],i[1],i[2]])
41  #
42  _print('patch_at:')
43  pt=[0.51,0.52,0.53]
44  pp=dispatch.snapshot(iout,run,data).patches
45  p1=ds.patch_at(pt,pp)
46  print ('patch_at: id =',p1.id,'position =',p1.position)
47  #
48  _print('corners:')
49  for a in (True,False):
50  print('active =',a)
51  c=ds.corners(pp[0],active=a)
52  print(' one: {} {}'.format(c[0],c[1]))
53  c=ds.corners(pp,active=a)
54  print('many: {} {}'.format(c[0],c[1]))
55  #
56  _print('is_inside')
57  for p in (pp[0],p1):
58  print ('is_inside: id =',p.id,'position =',p.position,'result',
59  ds.is_inside(pt,p))
60  #
61  _print('count_inside')
62  for p in (pp[0],p1):
63  print ('count_inside: id =',p.id,'position =',p.position,'result',
64  ds.count_inside(pt,p))
65  #
66  _print('indices_and_weights')
67  (i,w)=ds.indices_and_weights(pt,p1)
68  print(i,w)
69  #
70  _print('patches_along:')
71  ds.patches_along(pt,pp,verbose=2)
72  _print('patches_along_x:')
73  ds.patches_along_x(pt,pp,verbose=2)
74  _print('patches_along_y:')
75  ds.patches_along_x(pt,pp,verbose=2)
76  _print('patches_along_z:')
77  ds.patches_along_x(pt,pp,verbose=2)
78  #
79  _print('values_along')
80  iv=2
81  print('variable index =',iv)
82  x,v=ds.values_along(pt,pp,dir=0,iv=iv)
83  print('xmin,xmax =',x.min(),x.max())
84  print('vmin,vmax =',v.min(),v.max())
85  #
86  _print('shell_values:')
87  shv=ds.shell_values(pp,verbose=1)
88  shv.radial_components()
89  shv.angles()
90  print('variable min max (in shell)')
91  for key in shv.var.keys():
92  v=np.array(shv.var[key])
93  print('{:>8} {:12.3e} {:12.3e} {}'.format(key,v.min(),v.max(),v.shape))
94  print('average mass flux:',shv.mass_flux())
95  #
96  np.set_printoptions(**original)
97 
98 if __name__ == '__main__':
99  demo()