What decides the position of Hall Sensor in BLDC motor

I know that there will be there hall sensors in a BLDC hub motor. They are placed in such a way that they could detect the poles of the rotor magnets, and two of these sensors always read one pole while the other contradicts. But how is it engineered to place these hall sensors. What is the mathematical calculation behind it?

Or

Is it just trial and error?

The position depends on the design of the motor, but the position of the sensor is not magic.

The following image shows the most simple BLDC motor with just one pair of coils and a rotor with two poles, at the moment when polarity is changed:

From left to right:

1. The e-magnets still attract the the poles the permament magnet of the rotor and create torque
2. The polarity of the e-magnets changes
3. The e-magnets repel the poles of the stator facing them and attract the poles on the opposite side, creating torque

The hall sensors of these BLDC motors often already contain a comparator giving a high or low level, indicating if they see a N or S pole.

In the example above, such a sensor would be placed where the blue triangle is. The electronics behind would make it so that the polarity of the e-magnet on the right is always the same as what the sensor sees.

You'll find this principle in that fans used in computers, though there are two pairs of coils and the rotor has four poles:

(Source)

Note that the sensor is placed so that it sees the transition when the poles of the rotor are exactly aligned with the coils!

Another type of motor uses a rotor with two poles and three pairs of coils:

(Source)

In the most basic mode of operation, each pair of coils changes its polarity when the rotor poles are aligned to it, i.e. pair A1/A2 is just about to change in the picture. (In reality, it's more complex, see the source). So you could use three hall sensors in there, each responsible for one pair of coils.

So, now it depends on the design of the motor - how many coil pairs are used, and how many poled does the rotor have. With some logic or some intelligence of the controller, it is also possible to spare some sensors.