Animation and Advanced Python Features Phase I Python

























- Slides: 25
Animation and Advanced Python Features
Phase I Python is the best thing ever!
3 D Visualization: Positions particle configuration: “pos”
3 D Visualization: Scalars particle configuration: “pos” velocity configuration: “vel” velocity squared: “v 2” on “pos” is like a scalar field.
3 D Visualization: Vectors particle configuration: “pos” velocity configuration: “vel” on “pos” is like a vector field.
3 D Visualization: Vectors + Scalars Composition is easy. Import from existing scripts. Protect main loop with if __name__ == '__main__':
3 D Animation: Positions all particle positions: “all_pos”
3 D Animation: Positions + Velocities all particle positions: “all_pos” all particle velocities: “all_vel”
Summary: 3 D Visualization Snapshot Animation Positions: • plot • scatter Positions: • update Line 3 D artist Scalar field: • scatter with color Scalar field: • update Path 3 DCollection artist Vector field: • plot with quiver Vector field: • update Line 3 DCollection artist Don’t know how to update a particular artist? Use Artist. Animation.
2 D Visualization: Scalars spin lattice: “lat”
Output to html spin lattice: “lat” We can upload your html videos to the course website under projects.
Phase II Python is the worst thing ever!
For loop: O(1000) times slower than fortran Autocorrelation function pyanswer, fanswer 15. 694856 pytime, ftime, py/f 0. 039499 2. 8 e-05 1411 formula translation (fortran) was built with scientific computation in mind. Python implementation fortran implementation
For loop: O(1000) times slower than fortran Distance table N=256 Python implementation pytime, ftime, py/f 0. 199822 0. 000391 511 fortran implementation
Vectorize: O(10) times slower than fortran Autocorrelation function Vectorized Python implementation vecanswer, fanswer 15. 694856 vectime, ftime, vec/f 0. 000368 2. 6 e-05 14 fortran implementation
Vectorize: O(10) times slower than fortran Distance table N=256 Vectorized Python implementation vectime, ftime, vec/f 0. 004272 0. 000387 11 fortran implementation
Vectorize: Q/ Where to put conditionals? Distance table Vectorized Python implementation fortran implementation
Phase III Embracing Python
Vectorize: Q/ Where to put conditionals? A/ Boolean Selector. Distance table Vectorized Python implementation fortran implementation
Boolean selector: array of True and False of same shape
List Comprehension: flatten for loop, remove append Cubic Pos natom=64, 000 For implementation pytime, itertime, py/iter 0. 0173 0. 0062 2. 8 Iter implementation Main loop
np. meshgrid: np. array instead of lists Cubic Pos natom=64, 000 For implementation pytime, mtime, py/m 0. 0173 0. 0011 15. 7 meshgrid implementation Main loop
np. einsum: general tensor contraction (Einstein notation) pos cdot kvecs pytime, eintime, py/ein 0. 005 0. 0006 83
f 2 py: compile fortran modules and import makefile Type “make” to create the library file md. so Then import into Python md. f 90
Conclusion: Pick the right tool for the job Python is a versatile language. It excels at: • Text parsing • Graphics • Composing available packaged functions • Interface with other codes Python is not efficient at: • For loops • Memory management • Multiple Instruction Multiple Data (MIMD) Parallelization In high-performance computing (HPC), consider Python as an interface between backend performance code and the user. C, Fortran Data Structures f 2 py Python Next step: analysis, graphics, ML, etc.