I used to have very vivid, visual nightmares about maths. Not like I hate maths and it's coming to get me, but similar to what you're describing.
Onto your question though, I struggle with all kinds of bombarding chaotic thinking as I'm trying to sleep. I find it helpful to visualise an infinity symbol. I guess tracing it in my mind. I've always done this since I was very small. Maybe it's my equiv of counting sheep. Doesn't always help though, depends on how chaotic my mind is.
_________________
Diagnosed ASD
AQ: 42 (Scores in the 33-50 range indicate significant Austistic traits)
RAADS-R: 165
RDOS: Your neurodiverse (Aspie) score: 159 of 200
Your neurotypical (non-autistic) score: 44 of 200
You are very likely neurodiverse (Aspie)