I wasn't able to leverage what you had... So I started with a blank screen in OpenScad.
It turns out, you don't need a Cray Super Computer to do this. I just made 3 different toriods. I made the generic one with n1=1 and n2=1. And then I made a couple more just to make sure the code worked:
Attachment 3785
UPDATE: Those equations might be flawed. Some of the possible toroids only generate 1 quadrant of the shape. For example, n1=2 and n2=1. You get the correct shape, but only 1/4 of the toroid. I think some of these toroid shapes are imaginary. The reason is this: You can't take a fractional power of a negative number. But X is defined by X= cos^n1(theta)*(r0+r1*cos^n2(phi)) The cosine function goes negative as theta and phi sweep through the circle. So some of the coordinates generated by the equations are meaningless. (You are not allowed to raise a negative number to a fractional power --- that is undefined)
FURTHER UPDATE: The equations are wrong. Any time n1 or n2 is an even number, you are squaring the sin() and cos() functions. This causes any negative number they produce into a positive number. There is no way to generate points in the negative quadrants if you have n1^2 or n2^2 positive. n1 & n2 can only be 1,3,5,7,etc.