Control P simulation by code

Or this post: