Hi Arpad,
I would tend to discourage the adoption of an user routine for this task.
On the contrary, I would exploit the (relatively) novel approach based on the magnetic field cards.
You can see the manual: 7.22.47. MGNCREATe — FLUKA Manual
A more detailed explanation is also here: https://indico.cern.ch/event/1200922/contributions/5411821/attachments/2660679/4609170/06_Magnetic_electric_fields_2023_Advanced_ANL.pdf
Starting with your case, I see that you want to perform a linear search to locate the position in the magnetic file you attached. However, your user routine contains several bugs, and the overall effect is just to return 0 in any case.
I think it is more appropiate the usage of a 3D map to model the field. To do so, you first have to create the magnetic field with an MGNCREAT card. Then, you need to provide the field in a flattened tensor containing the magnetic field components.
In easier words, given the magnetic field components (Bx, By, Bz) along the three different carthesian coordinates (x, y, z), you need to create a list of MGNDATA card:
MGNDATA Bx(0, 0, 0) By(0, 0, 0) Bz(0, 0, 0) Bx(1, 0, 0) By(1, 0, 0) ...-> Bz(Nx, 0, 0) Bx(1, 1, 0) ..
And so on, along all the directions, as described in the manual.
What FLUKA does is to take the linear interpolation between those points.
Important: to make it work, the points on which you consider the magnetic field are evenly spaced.
I try to give you a concrete example with your problem.
In your specific case, I notice that when y > 3.5 you have less z coordinates. Therefore I generated the magnetic field between y = -4.9 and y = 3.5. That should anyway cover most of your geometry.
To declare the field, I need the following cards:
MGNCREAT 300 TESTDIP
MGNCREAT 2 85 201 &
MGNCREAT -3 -4.9 10 -2.9 3.5 30 &&
Then, to specify the data points, you need several lines:
MGNDATA -1.902E-01 1.205E-01 2.212E-01-8.664E-02 1.195E-01 9.054E-02TESTDIP
MGNDATA -1.895E-01 1.085E-01 2.375E-01-8.856E-02 1.081E-01 1.066E-01 &
MGNDATA -1.889E-01 9.907E-02 2.498E-01-8.955E-02 9.901E-02 1.188E-01 &&
… 17085 lines in total!
Finally, you need to assign this magnetic field to the corresponding region:
MGNFIELD 1 DIPOLE TESTDIP
The results are here:
Field map data: testdip.inp (1.2 MB)
Inputfile: test_dipole.inp (1.2 KB)
Field in the xy plane at z = 20: