%%%%%%% subroutine for calculating magnetic field components at (Ax,Ay,Az)

function[Bmat]=clovermagcomp(k,Rin,Rout,offset,z1,Axx,Ayy,Azz,intsteps,symetry,N,tempN); 

Bmat=zeros(intsteps,3);
extra =0;
   % calculate corrections for off axis coils
   offset2=sqrt(Rin^2-offset^2);
   offset3=sqrt(Rout^2-offset^2);
   arc1=abs(asin(offset/Rin));
   arc2=abs(asin(offset/Rout));
   
   if tempN == N
      extra =1;
   end
   
      % cloverleaf 1
x1start=-offset3;,x1stop=-offset2;,y1start=offset;,y1stop=offset+1e-12;,z1start=-z1/2;...
,z1stop=-z1/2+1e-12;,x2start=-offset;,x2stop=-offset+1e-12;,y2start=offset2;,y2stop=offset3...
;,z2start=-z1/2;,z2stop=-z1/2+1e-12;,thetainstart=pi-arc1;,thetainstop=(pi/2)+arc1;...
,thetaoutstart=(pi/2)+arc2;,thetaoutstop=pi-arc2;
[startx1,starty1,startz1,stopx1,stopy1,stopz1,startx2,starty2,startz2,stopx2,stopy2,...
      stopz2,Rxi,Ryi,Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2]=makevector(x1start,...
   x1stop,y1start,y1stop,z1start,z1stop,x2start,x2stop,y2start,y2stop,z2start,z2stop,...
Rin,Rout,thetaoutstart,thetaoutstop,thetainstart,thetainstop,intsteps); 

  Bmat=clmagmat(k,startx1,starty1,startz1,stopx1,stopy1,...
   stopz1,startx2,starty2,startz2,stopx2,stopy2,stopz2,Rxi,Ryi,...
   Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2,Axx,Ayy,Azz,extra);

x1start=-offset3;,x1stop=-offset2;,y1start=-offset;,y1stop=-offset+1e-12;,z1start=-z1/2;...
,z1stop=-z1/2+1e-12;x2start=-offset;,x2stop=-offset+1e-12;,y2start=-offset2;,y2stop=-offset3;...
,z2start=-z1/2;,z2stop=-z1/2+1e-12;,thetainstart=pi+arc1;,thetainstop=(3*pi/2)-arc1;...
,thetaoutstart=(3*pi/2)-arc2;,thetaoutstop=pi+arc2;
[startx1,starty1,startz1,stopx1,stopy1,stopz1,startx2,starty2,startz2,stopx2,stopy2,...
      stopz2,Rxi,Ryi,Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2]=makevector(x1start,...
   x1stop,y1start,y1stop,z1start,z1stop,x2start,x2stop,y2start,y2stop,z2start,z2stop,...
   Rin,Rout,thetaoutstart,thetaoutstop,thetainstart,thetainstop,intsteps); 

  Bmat=Bmat+clmagmat(k,startx1,starty1,startz1,stopx1,stopy1,...
   stopz1,startx2,starty2,startz2,stopx2,stopy2,stopz2,Rxi,Ryi,...
   Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2,Axx,Ayy,Azz,0);

x1start=offset3;,x1stop=offset2;,y1start=offset;,y1stop=offset+1e-12;,z1start=-z1/2;...
,z1stop=-z1/2+1e-12;x2start=offset;,x2stop=offset+1e-12;,y2start=offset2;,y2stop=offset3;...
,z2start=-z1/2;,z2stop=-z1/2+1e-12;thetainstart=arc1;,thetainstop=(pi/2)-arc1;...
,thetaoutstart=(pi/2)-arc2;,thetaoutstop=arc2;
[startx1,starty1,startz1,stopx1,stopy1,stopz1,startx2,starty2,startz2,stopx2,stopy2,...
      stopz2,Rxi,Ryi,Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2]=makevector(x1start,...
   x1stop,y1start,y1stop,z1start,z1stop,x2start,x2stop,y2start,y2stop,z2start,z2stop,...
Rin,Rout,thetaoutstart,thetaoutstop,thetainstart,thetainstop,intsteps); 

   Bmat=Bmat+clmagmat(k,startx1,starty1,startz1,stopx1,stopy1,...
   stopz1,startx2,starty2,startz2,stopx2,stopy2,stopz2,Rxi,Ryi,...
   Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2,Axx,Ayy,Azz,0);

 if tempN == N
      extra =1;
   end
  

x1start=offset3;,x1stop=offset2;,y1start=-offset;,y1stop=-offset+1e-12;,z1start=-z1/2;...
,z1stop=-z1/2+1e-12;,x2start=offset;,x2stop=offset+1e-12;,y2start=-offset2;...
,y2stop=-offset3;,z2start=-z1/2;,z2stop=-z1/2+1e-12;,thetainstart=(2*pi)-arc1;...
,thetainstop=(3*pi/2)+arc1;,thetaoutstart=(3*pi/2)+arc2;,thetaoutstop=(2*pi)-arc2;
[startx1,starty1,startz1,stopx1,stopy1,stopz1,startx2,starty2,startz2,stopx2,stopy2,...
      stopz2,Rxi,Ryi,Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2]=makevector(x1start,...
   x1stop,y1start,y1stop,z1start,z1stop,x2start,x2stop,y2start,y2stop,z2start,z2stop,...
Rin,Rout,thetaoutstart,thetaoutstop,thetainstart,thetainstop,intsteps); 

  Bmat=Bmat+clmagmat(k,startx1,starty1,startz1,stopx1,stopy1,...
 stopz1,startx2,starty2,startz2,stopx2,stopy2,stopz2,Rxi,Ryi,...
 Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2,Axx,Ayy,Azz,extra);

if symetry ==1
   B2 =flipud(Bmat);
   Bmat=Bmat+B2;
elseif symetry==0
   
    if tempN == N
      extra =1;
   end
  
 % cloverleaf 2
 x1start=-offset2;,x1stop=-offset3;,y1start=offset;,y1stop=offset+1e-12;,z1start=z1/2;...
,z1stop=z1/2+1e-12;,x2start=-offset;,x2stop=-offset+1e-12;,y2start=offset3;,y2stop=offset2;...
,z2start=z1/2;,z2stop=z1/2+1e-12;,thetainstart=(pi/2)+arc1;,thetainstop=pi-arc1;...
,thetaoutstart=pi-arc2;,thetaoutstop=(pi/2)+arc2;
[startx1,starty1,startz1,stopx1,stopy1,stopz1,startx2,starty2,startz2,stopx2,stopy2,...
      stopz2,Rxi,Ryi,Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2]=makevector(x1start,...
   x1stop,y1start,y1stop,z1start,z1stop,x2start,x2stop,y2start,y2stop,z2start,z2stop,...
Rin,Rout,thetaoutstart,thetaoutstop,thetainstart,thetainstop,intsteps); 

  Bmat=Bmat+clmagmat(k,startx1,starty1,startz1,stopx1,stopy1,...
   stopz1,startx2,starty2,startz2,stopx2,stopy2,stopz2,Rxi,Ryi,...
   Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2,Axx,Ayy,Azz,extra);

x1start=-offset2;,x1stop=-offset3;,y1start=-offset;,y1stop=-offset+1e-12;,z1start=z1/2;...
,z1stop=z1/2+1e-12;,x2start=-offset;,x2stop=-offset+1e-12;,y2start=-offset3;...
,y2stop=-offset2;,z2start=z1/2;,z2stop=z1/2+1e-12;,thetainstart=(3*pi/2)-arc1;,...
thetainstop=pi+arc1;,thetaoutstart=pi+arc2;,thetaoutstop=(3*pi/2)-arc2;
[startx1,starty1,startz1,stopx1,stopy1,stopz1,startx2,starty2,startz2,stopx2,stopy2,...
      stopz2,Rxi,Ryi,Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2]=makevector(x1start,...
   x1stop,y1start,y1stop,z1start,z1stop,x2start,x2stop,y2start,y2stop,z2start,z2stop,...
   Rin,Rout,thetaoutstart,thetaoutstop,thetainstart,thetainstop,intsteps); 

  Bmat=Bmat+clmagmat(k,startx1,starty1,startz1,stopx1,stopy1,...
   stopz1,startx2,starty2,startz2,stopx2,stopy2,stopz2,Rxi,Ryi,...
   Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2,Axx,Ayy,Azz,0);

x1start=offset2;,x1stop=offset3;,y1start=offset;,y1stop=offset+1e-12;,z1start=z1/2;...
,z1stop=z1/2+1e-12;,x2start=offset;,x2stop=offset+1e-12;,y2start=offset3;,y2stop=offset2;...
,z2start=z1/2;,z2stop=z1/2+1e-12;,thetainstart=(pi/2)-arc1;,thetainstop=arc1;...
,thetaoutstart=arc2;,thetaoutstop=(pi/2)-arc2;
[startx1,starty1,startz1,stopx1,stopy1,stopz1,startx2,starty2,startz2,stopx2,stopy2,...
      stopz2,Rxi,Ryi,Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2]=makevector(x1start,...
   x1stop,y1start,y1stop,z1start,z1stop,x2start,x2stop,y2start,y2stop,z2start,z2stop,...
Rin,Rout,thetaoutstart,thetaoutstop,thetainstart,thetainstop,intsteps); 

   Bmat=Bmat+clmagmat(k,startx1,starty1,startz1,stopx1,stopy1,...
   stopz1,startx2,starty2,startz2,stopx2,stopy2,stopz2,Rxi,Ryi,...
   Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2,Axx,Ayy,Azz,0);


 if tempN == N
      extra =1;
   end
  
x1start=offset2;,x1stop=offset3;,y1start=-offset;,y1stop=-offset+1e-12;,z1start=z1/2;...
,z1stop=z1/2+1e-12;,x2start=offset;,x2stop=offset+1e-12;,y2start=-offset3;...
,y2stop=-offset2;,z2start=z1/2;,z2stop=z1/2+1e-12;,thetainstart=(3*pi/2)+arc1;...
,thetainstop=(2*pi)-arc1;,thetaoutstart=(2*pi)-arc2;,thetaoutstop=(3*pi/2)+arc2;
[startx1,starty1,startz1,stopx1,stopy1,stopz1,startx2,starty2,startz2,stopx2,stopy2,...
      stopz2,Rxi,Ryi,Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2]=makevector(x1start,...
   x1stop,y1start,y1stop,z1start,z1stop,x2start,x2stop,y2start,y2stop,z2start,z2stop,...
Rin,Rout,thetaoutstart,thetaoutstop,thetainstart,thetainstop,intsteps); 

  Bmat=Bmat+clmagmat(k,startx1,starty1,startz1,stopx1,stopy1,...
 stopz1,startx2,starty2,startz2,stopx2,stopy2,stopz2,Rxi,Ryi,...
 Rzi,Rxo,Ryo,Rzo,dtheta1,dtheta2,theta1,theta2,Axx,Ayy,Azz,extra);

end
Bmat=sum(Bmat,1);












