I am building a U-shaped magnetic yoke (with N and S pole on left and right side) to study electron trajectories.
The geometry is defined using two large boxes to form the yoke, and a smaller box is subtracted to create a vacuum gap. A dipole magnetic field is applied in the gap.
However, with the current magnetic field direction, the electrons are bending left or right and hitting the side walls of the yoke. I want the electrons to bend up or down, which means I need to change the direction of the magnetic field.
you can actually solve magnetic field geometry for homnogeneous field, in several ways.
First, the approach you propose is functional in general.
Also, you can do it very easily (after you spend some time getting familiar with the code and its compilation) with source routing magfld.
In my opinion (based on Fluka errors) you have a parallel secondary problem, with creating a bunch of low-energy secondary electrons, that get to infinite circular trajectories, so they cannot be computed.
You need to adjust setting of the magnetic field simulation (MGNFIELD card) and cut off irrelevant electrons below a reasonable for you energy threshold.