Solenoids have many practical implications and they are mainly used to create magnetic fields or as electromagnets. A solenoid is a combination of closely wound loops of wire in the form of helix, and each loop of wire has its own magnetic field.
A large number of such loops allow you combine magnetic fields of each loop to create a greater magnetic field. The combination of magnetic fields means the vector sum of magnetic fields due to individual loops.
We consider a solenoid carrying current \(I\) as shown in Figure 2. The current in each loop of the solenoid creates magnetic field and the combination of such magnetic fields creates a greater magnetic field. The solenoid with current acts as the source of magnetic field. If the solenoid is closely wound, each loop can be approximated as a circle. Here we determine the magnetic field of the solenoid using Ampere's law.
If the coils are closely wound and the length of the solenoid is much greater than it's diameter, the magnetic field lines inside the solenoid approach straight lines and the field is more uniform. There are still magnetic field lines outside the solenoid as the magnetic field lines form closed loops.
What has been found from the careful investigations is that the half of these lines leak out through the windings and half appear through the ends. The magnetic field outside the solenoid is much weaker as the outside volume is much greater than that of the inside and very little field exists around the center of the solenoid (outside).
Furthermore, a solenoid is the windings of wire and each loop is not a perfect circle, you can understand that, if you consider the entire solenoid as a straight wire, and made an amperian loop (closed path in Ampere's law), the loop indeed encloses current flowing through the solenoid which means the solenoid itself acts as a straight wire with magnetic field similar to that of the straight wire.
The magnetic field lines of a solenoid at the ends still spread outside like those of a bar magnet. A properly formed solenoid has magnetic moments associated with each loop and the one end of the solenoid acts as the south pole and another acts as the north pole. For an illustration for a single loop you can revisit magnetic field of a loop.
Now we create a closed path as shown in Figure 3 above. To use Ampere's law we determine the line integral \(\oint \vec B \cdot d\vec l\) over this closed path where \(dl\) is the length element of this closed path. Use the right hand rule to find the direction of integration path. In our case it is in anticlockwise direction, that is along \(abcd\) in the figure. Let the length of the rectangular path is \(L\). The direction of \(d\vec l\) will be the direction of our integration path.
Along path \(ab\), \(\vec B\) and \(d\vec l\) are parallel and \(\int_a^b \vec B \cdot d\vec l = \int_a^b B\,dl = B\int_a^b dl = BL\). Along paths \(bd\) and \(ca\), \(\vec B\) is perpendicular to \(d\vec l\) and the integral along these paths is zero. Along path \(dc\), the magnetic field is negligible and approximated as zero (note the side \(bc\) is far from the edge of the solenoid where magnetic field is much weaker and neglected as zero). Therefore the total line integral over the closed path is
\[\oint \vec B \cdot d\vec l = BL + 0 + 0 + 0 = BL\]
We know from Ampere's law that \(\oint \vec B \cdot d\vec l = \mu_0I\). If \(n\) is the number of turns per unit length, there are \(nL\) turns in length \(L\), therefore the total current enclosed by the closed path is \(nL\) times \(I\), that is \(nLI\). So according to Ampere's law we have,
\[BL = \mu_0\,n\,LI\]
Therefore the magnetic field of the solenoid inside it is
\[B = \mu_0nI \tag{1} \label{1}\]
The above expression of magnetic field of a solenoid is valid near the center of the solenoid. The magnetic field of a solenoid near the ends approaches half of the magnetic field at the center, that is the magnetic field gradually decreases from the center to the ends. The above equation also tells us that the magnetic field is uniform over the cross-section of the solenoid.
What is magnetic field of a toroidal solenoid?
A torus is a shape bounded by a moving circle in a circular path and forms a doughnut like shape. Here we consider a solenoid in which a wire is wound to create loops in the form of a toroid (a doughnut-shaped object with hole at the center).
The Figure 4 below shows a toroidal solenoid with current into and out of the solenoid where a wire is loosely would to form a solenoid in the form of a torus. But here we suppose a torus with closely wound loops of wire, so the magnetic field is more bounded within the solenoid.
In Figure 5, a closely wound solenoid is shown. There are three loops namely 1, 2 and 3. To apply Ampere's law to determine the magnetic field within the solenoid, loop 1 encloses no current, and loop 3 encloses a net current of zero. Note that within the closed path of loop 3 the currents into the screen cancel the current out of the screen (here the screen means your computer screen or smart phone's).
The only loop that encloses current among the three is loop 2 with radius \(r\). If \(N\) is the number of turns in the solenoid. Now, we apply Ampere's law around the loop 2 to determine the magnetic field of toroidal solenoid. As always, use right hand rule to determine the direction of integration path to avoid negative current in the result, that is make \(\vec B\) and \(d\vec l\) parallel at each point of the integration path not antiparallel.
\[\oint \vec B \cdot d\vec l = B\oint dl = B(2\pi\,r) = 2\pi\,r\,B\]
Note that the magnetic field is constant for a constant radius \(r\), and taken out of the integral for a closely wound solenoid. Now the Ampere's law tells us that the line integral over a closed path is \(\mu_0\) times the total current enclosed by the path, that is \(2\pi\,rB = \mu_0NI\), and we find the expression of magnetic field as
\[B = \frac{\mu_0NI}{2\pi\,r} \tag{2} \label{2}\]
The above equation of magnetic field of a toroidal solenoid shows that the field depends on the radius \(r\). It means that the magnetic field is not uniform over the cross-section of the solenoid, but if the cross-sectional radius is small in comparison to \(r\), the magnetic field can be considered as nearly uniform.
In case of toroidal solenoid, the number of turns per unit length is \(N/2\pi\,r\). So, substituting this value for \(n\) in Equation \eqref{1}, you'll get Equation \eqref{2}. So a toroidal solenoid satisfies the equation of magnetic field of closely wound long straight solenoid.
In case of an ideal solenoid, it is approximated that the loops are perfect circles and the windings of loops is compact, that is the solenoid is tightly wound. In such a case we can conclude that the magnetic field outside the solenoid (for path 1 and path 3) is zero also suggested by \(\oint \vec B \cdot d\vec l = 0\).
As warned in Ampere's law, that \(\oint \vec B \cdot d\vec l = 0\) does not mean that \( B\) is zero. Beware! In real situations, however, toroidal solenoid itself acts as a current loop. Similar to the straight solenoid, the toroidal solenoid acts as a single loop of wire with current. If you make a closed path (amperian loop) enclosing that current as shown in Figure 4, the solenoid has magnetic field like that of a single current loop.
You may think for loops 1 and 3, the magnetic field is zero, but that's not true. The field is weak but it exists and the line integral is zero for these loops not because there is no magnetic field but because \(\vec B\) and \(d\vec l\) are perpendicular to each other. Note that the solenoid loops are not completely circles and there is a weak magnetic field similar to that of a circular loop.