Generate phi with specified flow.

Needs further testing and optimization.