Identify where you do your best thinking and make it a habit. I find that a good cup of coffee or a good workout bring out the best in my productivity. With some luck, there's a late night coffeeshop nearby you can frequent. Being around people alone can help keep your energy up.
The gym helps me decompress and gather thoughts - all the clutter of the current project or the events of that day can be reflected on and next moves can be put together. My regular gym has late hours so it's not unusual for me to get a midnight workout in followed by some more work. I can almost feel my brain sorting through the clutter from a workload as I work up a sweat. Best part - it's a natural way to boost energy and it works.
More than anything, being predisposed to that type of lifestyle helps (i.e. insomnia). I have instances where I'm dead tired but my mind is still racing - these are cases where it's hard to make myself fall asleep.
To sum it up, find a system that suits you. There's a strong possibility that that method requires you to be active (rather than passive) as you navigate through your day. Integrate it into a routine so when those occasions pop-up where you need to pull an all-nighter, the ramp-up isn't as dramatic.