# Documentation: MAG files

if the magnetic field is not constant, you have 6 different options to define one component of the B vector using a MAG file. ## By coordinates

The simplest and most often used magnetic field: you define a strength for a list of coordinates, and Synrad will interpolate between them.

MAG file format:

```period dirX dirY dirZ N
coordinate1 value1
coordinate2 value2
...
coordinateN valueN```

The idea is that you have a machine (a dipole, for example) that begins at the starting point of the region, and is orientated in the (dirX,dirY,dirZ) direction, with a defined period. Then to calculate the magnetic field strength, the following is done:

1. The actual position is projected to the direction vector (scalar multiplication of the direction vector and the [actual_position-starting_point] vector

2. The projection is interpreted as the "ccordinate" (distance) and being looked up in the first column

3. It is interpolated between the N fixed coordinated (distances) written in the lines Example: Your MAG file is the following for the Bx component:

```100.0 0.0 0.0 1.0 2
0 2.0
100.0 3.0```

It means the following: we have a machine oriented towards the Z axis (0,0,1). It has 2T field at the beginning (0cm) and 3T field at the end of the period (100cm).

Bx(0,0,50)=2.5Tesla (halfway between the two fixed coordinates)
Bx(0,0,120)=2.2Tesla (The period of 100cm is deducted from the 120cm distance)

The above two calculations assumed that the starting point is (0,0,0).

## Coordinates along the beam

Use the same format as above. The dirX, dirY, dirZ numbers will be ignored. The coordinate will be the L value (distance along beam), with full periods subtracted.

## Sine / cosine

You can define a periodic magnetic field. Format of the BXY file:

```period dirX dirY dirZ N
A1 B1
A2 B2
...
AN BN```

Where period is the period of the sine and cosine function. DirX, dirY and dirZ are the same as before. N is the number of lines that follow.

A1, B1, A2, B2, ... are coefficients, to give the following magnetic field:

B(distance)=
A1*sin(distance*2PI/period) + B1*cos(distance*2PI/period) +
A2*sin^2(distance*2PI/period) + B2*cos^2(distance*2PI/period) +
...
AN*sin^N(distance*2PI/period) + BN*cos^N(distance*2PI/period)

If you choose this mode, one component's MAG file will set all the components of the magnetic vector. Load it as the X component's MAG file.

Format of the MAG file:

```centerX centerY centerZ
alfa beta rot
K L```

(centerX,centerY,centerZ) define where the center point of the beginning of the quadrupole is (ie. the starting point of its axis).

Alfa, beta and rot are 3 angles that define its orientation. The meaning of the angles, in radians, are:

alpha: angle with the XZ plane, positive towards Y
beta: angle with YZ plane, negative towards X
rot: quadrupole rotation around its centerline (that is defined by alpha, beta)

direction.X=-cos(alpha)*sin(beta)
direction.Y=sin(alpha)
direction.Z=cos(alpha)*cos(beta) With the above parameters, you have the starting point and the direction of the axis.

K is the strength of the quadrupole (in Tesla*meters), and L is its length in cm (magnetic field is 0 outside the quadrupole).

You can see the snippet code calulcating the quarupole field here.

## Analytic expression

Allows to set up the Halbach wiggler field model.

This mode also sets the B vector's all components. Load the MAG file for the X component! MAG file format:

```period dirX dirY dirZ N
Kx K2```

Here dirX,dirY, dirZ and N are not used (give any value).

The B vector is calculated the following way:

K=2PI/period
Ky=sqrt(K^2-Kx^2)

Bx(positionX,positionY,positionZ) = Kx/Ky*K2*sinh(Kx*positionX)*sinh(Ky*positionY)*cos(K*positionZ)
By(positionX,positionY,positionZ) = K2*sinh(Kx*positionX)*sinh(Ky*positionY)*cos(K*positionZ)
Bz(positionX,positionY,positionZ) = -K/Ky*K2*sinh(Kx*positionX)*sinh(Ky*positionY)*cos(K*positionZ)

## Helicoidal

A helix. MAG file format:

```period phase dirX dirY dirZ N
X1 Y1
X2 Y2
...
XN YN```

The magnetic component is calculated the following way:

ratio = distance*2PI/period
B(ratio) = X1*sin(ratio)*cos(PI*phase/period) + Y1*cos(ratio)*sin(PI*phase/period)
+ X2*sin(2*ratio)*cos(PI*phase/period) + Y2*cos(2*ratio)*sin(PI*phase/period)
+ ...
+ XN*sin(N*ratio)*cos(PI*phase/period) + YN*cos(N*ratio)*sin(PI*phase/period)

## Rotating dipole

Load it as X component, sets all 3 components of the B vector.

MAG file:

```period dirX dirY dirZ N
A X```

where only period and A have meanings (give any value for others).

ratio = distance*2PI/period
Bx(ratio)=A*sin(ratio)
By(ratio)=A*cos(ratio)
Bz=0

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer