I haven't experienced anything as severe as what you've described, but when I have racing thoughts I will get up and write them down. Could be something like a to-do list or an idea I have for a project I've been working on. I might write down questions related to a problem that I've been trying to work out. Sometimes I'll sketch whatever's in my head if that's appropriate.

When I was in grad school I kept a notebook handy on my nightstand, since I had trouble turning my brain off before going to sleep.

Somehow getting it out on paper put me at ease that I would be able to take care of all of these thoughts the next day or later in the week.

Good luck with the doctor, and I hope you are able to figure out a way to get some sleep!