When this happens I put aside my work (not being productive in these circumstances anyways), go exercise until I'm absolutely exhausted, and then get a good night's sleep. The next day my writing is SO much better. Everyone has their own little rituals for these things, although I firmly believe that one of the best things to do when you're frustrated mentally is some sort of physical activity. Go run, swim, bike, walk your dog, whatever. Just get out of the library/lab for a short time and let your brain digest everything you're doing.