Potential theory in gravity and magnetic applications