250 likes | 269 Views
Discover the power of Python for 3D visualization with animation and advanced features. Learn to plot positions, scatter data, visualize scalar and vector fields, update artists, and more. Enhance your skills through examples and tutorials. Explore Python's capabilities and consider the right tools for different tasks. Embrace Python for data analysis, graphics, machine learning, and beyond. Navigate through Python's strengths and inefficiencies, and optimize your code for better performance.
E N D
Phase I Python is the best thing ever!
3D Visualization: Positions particle configuration: “pos”
3D Visualization: Scalars particle configuration: “pos” velocity configuration: “vel” velocity squared: “v2” “v2” on “pos” is like a scalar field.
3D Visualization: Vectors particle configuration: “pos” velocity configuration: “vel” “vel” on “pos” is like a vector field.
3D Visualization: Vectors + Scalars Composition is easy. Import from existing scripts. Protect main loop with if __name__ == '__main__':
3D Animation: Positions all particle positions: “all_pos”
3D Animation: Positions + Velocities all particle positions: “all_pos” all particle velocities: “all_vel”
Summary: 3D Visualization Snapshot Animation • Positions: • plot • scatter • Scalar field: • scatter with color • Vector field: • plot with quiver • Positions: • update Line3D artist • Scalar field: • update Path3DCollection artist • Vector field: • update Line3DCollection artist Don’t know how to update a particular artist? Use ArtistAnimation.
2D 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 15.694856 pytime, ftime, py/f 0.039499 2.8e-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 pytime, ftime, py/f 0.199822 0.000391 511 Python implementation fortran implementation
Vectorize: O(10) times slower than fortran Autocorrelation function vecanswer, fanswer 15.694856 15.694856 vectime, ftime, vec/f 0.000368 2.6e-05 14 Vectorized Python implementation fortran implementation
Vectorize: O(10) times slower than fortran Distance table N=256 vectime, ftime, vec/f 0.004272 0.000387 11 Vectorized Python implementation 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
List Comprehension: flatten for loop, remove append Cubic Posnatom=64,000 pytime, itertime, py/iter 0.0173 0.0062 2.8 For implementation Iter implementation Main loop
np.meshgrid: np.array instead of lists Cubic Posnatom=64,000 pytime, mtime, py/m 0.0173 0.0011 15.7 For implementation meshgrid implementation Main loop
np.einsum: general tensor contraction (Einstein notation) pos \cdotkvecs pytime, eintime, py/ein 0.005 0.0006 83
f2py: compile fortran modules and import md.f90 makefile Type “make” to create the library file md.so Then import into Python
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 Python Next step: analysis, graphics, ML, etc. f2py