Posts
Generating VTK files with Python
Paraview is an industrial-strength, open-source 3D visualization program designed to visualize large data sets created by computational fluid dynamics (CFD) simulations. Although its native data file format (.vtu) is designed for reading in point- and cell-based data from CFD simulations, I have successfully used it to read in and visualized discrete particle data from a Brownian Dynamics (BD) simulation. Below, you can download a Python class that is useful for visualizing particles in motion. It should be easily adaptable to any type of discrete data set that involves discrete objects, vectors, and/or scalars. In addition, this class should be useful with any application based on the VTK toolkit because they also read .vtu files.
Numerical software and examples
Here is a brief index to some of the more useful things I have created or modified.
Compiling LAPACK with the Intel Fortran Compiler ODEPACK: a library for numerically solving a system of ordinary differential equations
Scientific Computing
My PhD research is to develop tools that predict the interaction of proteins with engineered surfaces. This work has applications in the development of biomedical devices (such as neural implants and point-of-care diagnostics), biosensors, and hybrid neural systems.
My pages about scientific computing
Optimizing fast Python math with Numpy and Scipy (coming soon!) Calculating the pair correlation function with Python Generating VTK files with Python for visualization in Paraview Visualizing trajectories with Python, VMD, and .vtf files Compiling LAPACK with the Intel Fortran Compiler ODEPACK: a library for numerically solving a system of ordinary differential equations
Simulation
Most of my work has been focused on developing simulations for use in research and development. However, I have worked on several projects with applications in military simulation and civilian training.
A WWII Simulation Using OneSAF Objective Framework Custom scenery development for a pilot training scenario
Open-source tool chain
I use open-source software whenever possible. Although there are cases where a proprietary tool is the right choice, open-source tools have many advantages.
Complete control of your critical tools–the vendor will never stop supporting your platform, an upgrade will never be forced upon you, and the licensing fee will never increase. Open-source tools tend to utilize open standards and exchange data much more fluidly than proprietary tools.
- If necessary, your developers can fix a bug themselves. A bug might be critical to your application, but that doesn’t mean it’s a high priority for the application vendor.
Open-Source Tools
Programming Python for rapid application development Numpy and Scipy for scientific omputing C, C++ and Fortran for optimized code Parallel Computing MPI PETSC pypar and mpi4py (Python bindings for MPI) CUDA and PyCUDA for massively parallel execution on NVIDIA graphics processors (GPUs) Simulation OpenFOAM: Computational Fluid Dynamics libmesh: finite element library Visualization Matplotlib (2D plotting) VTK (3D visualization) Paraview (3D visualization) VMD (Molecular dynamics visualization) Documentation LyX LaTeX Asymptote (technical vector graphics) Office and OpenOffice Version control Subversion Git OpenFOAM
Using Python to generate XML files for visualization in Paraview
VTK is an open-source software system for “3D computer graphics, image processing, and visualization” developed by by Kitware. VTK is the foundation of Paraview, an industrial-strength CFD visualization tool that I have found to be very useful. I generate “second generation” XML-based files from my Python code and import them into Paraview for visualization. I am in the process of creating some Python classes to do, and I hope to publish them soon. Until then, I want to share some useful resources. The VTK file formats are specified in this document. It’s a pretty good specification, but it lacks some examples. Soon I will post an example of a valid unstructured, serial .vtu file. Each VTK file includes data from only one time step, so you have to keep track of time yourself (the filename is an easy solution). Paraview can read in data from multiple time steps, but you have to specify them in a .pvd file. This is also an XML file, with the following format: (reference)
Unexpected integer/float math behavior in Python
I wasted some time today tracking down a bug in one of my programs. It turned out to be “unexpected behavior” rather than a bug. I was aware of this aspect of the language, but I made an assumption and got bit. Read on for a valuable lesson. Python handles integer math differently than floating point math. If you type a number without a decimal point, Python treats it as an integer. All math performed only with integers results in integers.For example, 1/2 evaluates to 0 while 1./2. evaluates to 0.5. If you mix integers and floats, Python will produce a floating point result (1/2.=0.5), but you must be very careful. For example, you might expect the expression 4/3*3.14159 to yield a floating point result. It does yield a floating point number, butnotthe one you were expecting! 4/3*3.14159 yields 3.14159. What happened? Python works from left to right. 4/3 evaluates to the integer “1”. 1*3.14159 evaluates to 3.14159. For comparison, 4./3.*3.14159 evaluates to 4.1887866. Here’s the problem with this particular aspect of Python: according to the rules of math, 4/3*3.14159 is exactly the same expression as 4*3.14159/3, but in Python they yield different results if you forget the decimal points! 4*3.14159 evaluates to a floating point, so (4*3.14159)/3 yields the “correct” floating point value. Lesson Learned: be explicit about specifyingall floats if you are doing floating-point math! Sometimes I get lazy and leave a trailing decimal point off of a number when doing a floating point calculation, knowing that the results are “upcast” into floats. Not any more! Note: this unexpected behavior goes away in Python 3.0
What is a shock solution?
Definition of a shock solution
Definition: “shock solution” is an informal mathematical term used to describe a type of weak solution in the field of partial differential equations. When a nonlinear or quasilinear equation does not have a strict solution, a weak solution may be found that allows useful information to be obtained even though a strict solution cannot be obtained. Reference: J. Kevorkian, Partial Differential Equations, Springer-Verlag, 2000.
Why did I choose the domain shocksolution.com?
The shock solution is a clever method of obtaining useful information from a problem that could be considered unsolvable. It is a reminder that even the most complex and difficult problem can be analyzed and understood using the correct tools. My objective is to develop these tools so that better decisions can be made when dealing with the complex problems found in the world.
Profiling Python code
“Speed” is a complicated term when used in the context of software. Does it mean raw speed of execution, or reducing the amount of time until a correct result is obtained? Python is not the first language that comes to mind when people think of “fast software.” It is true that pure Python will usually not execute as quickly as the same algorithm directly coded in C or Fortran. However, when you define speed as “least amount of time until you get the right answer,” then Python is pretty fast. It is so easy to develop correct code in Python, when compared to low-level compiled languages, that Python is often the fastet route to a correct answer, even if the execution time is longer. Having said that, there aretimes when code has to execute quickly, and that’s why I will introduce you to profiling Python code. Like all things Python, profiling is easier than you think. Python 2.4 has the profile module, and Python 2.5 has both profile and cProfile. cProfile is written in C for lower overhead, and it’s the recommended version. I am stuck with profile, because the cluster that I am working with still uses Python 2.4. You can read the docs for more details, but I will quickly outline what I find to be the most helpful usage. For example, say I want to profile the filerun_sim.py. I use the following command line: