Skip to content
Snippets Groups Projects
soltracy.cc 7.01 KiB
Newer Older
zhang's avatar
zhang committed
#define ORDER 1          

int no_tps = ORDER; // arbitrary TPSA order is defined locally

extern bool  freq_map;

#include "tracy_lib.h"
#include "time.h"
#include <sys/time.h>

//***************************************************************************************
//
//  MAIN CODE
//
//****************************************************************************************
 int main(int argc, char *argv[])
{
  const long  seed = 1121;
  const bool All = TRUE;
  iniranf(seed); setrancut(2.0);

  // turn on globval.Cavity_on and globval.radiation to get proper synchr radiation damping
  // IDs accounted too if: wiggler model and symplectic integrator (method = 1)
  globval.H_exact     = false; 
  globval.quad_fringe = false;                // quadrupole fringe field
  
  globval.radiation   = false;                // synchrotron radiation
  globval.emittance   = false;                 // emittance
  globval.pathlength  = false; 
//  globval.bpm         = 0;
  
  const double  x_max_FMA = 10e-3, delta_FMA = 10e-2;
  const int     n_x = 80, n_dp = 80, n_tr = 2048;
  
  double nux=0, nuz=0, ksix=0, ksiz=0;
  
  bool chroma;
  double dP = 0.0;
  long lastpos = -1L;
  char str1[S_SIZE];
  
  /************************************************************************
      start read in files and flags
  *************************************************************************/
  read_script(argv[1], true);


 /************************************************************************
    end  read in files and flags
  *************************************************************************/
  prtmfile("flat_file.dat"); // writes flat file   /* very important file for debug*/
  printlatt();  /* SOLEIL print out lattice functions */
  printglob();
  
  
  
    // Flag factory
    
    // Chamber factory
  if (ChamberFlag == false)
     ChamberOff(); // turn off vacuum chamber setting, use the default one
  else if (ChamberNoU20Flag == true)
     DefineChNoU20();  // using vacuum chamber setting but without undulator U20
  else if (ReadChamberFlag == true)
     ReadCh(chamber_file); /* read vacuum chamber from a file "Apertures.dat" , soleil version*/
  PrintCh();
 
 
  // compute tunes by tracking (should be the same as by DA)
  if (TuneTracFlag == true) {
    GetTuneTrac(1026L, 0.0, &nux, &nuz);
    fprintf(stdout,"From tracking: nux = % f nuz = % f \n",nux,nuz);
  }

  // compute chromaticities by tracking (should be the same as by DA)
  if (ChromTracFlag == true){
    GetChromTrac(2L, 1026L, 1e-5, &ksix, &ksiz);
    fprintf(stdout,"From tracking: ksix= % f ksiz= % f \n",ksix,ksiz);
  }


  if (FitTuneFlag == true){
    fprintf(stdout, "\n Fitting tunes\n");
    FitTune(ElemIndex("qp7"),ElemIndex("qp9"), targetnux, targetnuz);
    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
    printglob();                      /* print parameter list */
  }

  if (FitChromFlag == true){
    fprintf(stdout, "\n Fitting chromaticities\n");
    FitChrom(ElemIndex("sx9"),ElemIndex("sx10"), targetksix, targetksiz);
    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
    printglob();                      /* print parameter list */
  }

    //SetKLpar(ElemIndex("QT"), 1, 2L,   0.001026770838382);
  
  // coupling calculation
   if (CouplingFlag == true){
     Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
     printlatt();                      /* dump linear lattice functions into "linlat.dat" */
  //   Coupling_Edwards_Teng();
     printglob();   /* print parameter list */
   }

  // add coupling by random rotating of the quadrupoles
  if (ErrorCouplingFlag == true){
    SetErr();
    Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
    printlatt();                      /* dump linear lattice functions into "linlat.dat" */
//    Coupling_Edwards_Teng();
    printglob();   /* print parameter list */
  }

  // WARNING Fit tunes and chromaticities before applying errors !!!!
  //set multipoles in all magnets
  if (MultipoleFlag == true ){
    if (ThinsextFlag ==true){
      fprintf(stdout, "\n Setting Multipoles for lattice with thin sextupoles \n");
      Multipole_thinsext();  /* for thin sextupoles */
      
      Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
      printglob(); 
     }
    else{
      fprintf(stdout, "\n Setting Multipoles for lattice with thick sextupoles \n");
      Multipole_thicksext();  /* for thick sextupoles */
      
      Ring_GetTwiss(chroma=true, 0.0);  /* Compute and get Twiss parameters */
      printglob(); 
    }
  }
 // Computes FMA
  if (FmapFlag == true){
    if (ChamberFlag == true ){
      if (ExperimentFMAFlag == true)
         fmap(40,12,258,-20e-3,5e-3,0.0,true); // for experimental
      if (DetailedFMAFlag == true)
        fmap(100,50,1026,20e-3,5e-3,0.0,true);
      }
      else{
        if (ExperimentFMAFlag == true)
          fmap(40,12,258,-32e-3,5e-3,0.0,true);
        if (DetailedFMAFlag == true)
          fmap(200,100,1026,32e-3,7e-3,0.0,true);
      }
  }
  
  if (CodeComparaisonFlag){
          fmap(200,100,1026,-32e-3,7e-3,0.0,true);
  }

  if (MomentumAccFlag == true){
    MomentumAcceptance(1L, 108L, 0.01, 0.05, 100L, -0.01, -0.05, 100L);
  }

  // computes Tuneshift with amplitudes
  if (TuneShiftFlag == true){
    if (ChamberFlag == true ){
      NuDx(31L,21L,516L,0.025,0.005,dP);
      NuDp(31L,1026L,0.06);
      }
      else{
        NuDx(50L,30L,516L,0.035,0.02,dP);
        NuDp(31L,1026L,0.06);
      }

  }

  // induced amplitude 
  if (InducedAmplitudeFlag == true){
      InducedAmplitude(193L);  
  }
  
  if (EtaFlag == true){
  // compute cod and twiss parameters for different energy offsets    
    for (int ii=0; ii<=40; ii++) { 
    dP = -0.02+ 0.001*ii;
    Ring_GetTwiss(chroma=false, dP);   /* Compute and get Twiss parameters */
    printlatt();                      /* dump linear lattice functions into "linlat.dat" */
    getcod (dP, lastpos);
//     printcod();
    prt_cod("cod.out", globval.bpm, true);
    //system("mv linlat.out linlat_ooo.out"); 
    sprintf(str1, "mv cod.out cod_%02d.out", ii); 
    system(str1);
    sprintf(str1, "mv linlat.out linlat_%02d.out", ii); 
    system(str1);
    }
}    
  

  if (PhaseSpaceFlag == true){
     int   t_sec,t_usec,t_msec; 
     struct timeval tv,tv1;
     struct timezone tz,tz1 ;
     struct tm *tm,*tm1;
     gettimeofday(&tv, &tz);
     tm=localtime(&tv.tv_sec);
     
     t_sec = - tm->tm_sec;
     printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
             tm->tm_sec, tv.tv_usec);

      Phase(0.001,0,0.001,0,0.001,0, 1);
      
       gettimeofday(&tv1, &tz1);
       tm1=localtime(&tv1.tv_sec);
     
       t_sec += tm->tm_sec;
       t_usec = tv1.tv_usec -  tv.tv_usec; 
       t_msec = t_sec*1000 + t_usec/1000;
         
       
      printf(" %d:%02d:%02d %d \n", tm1->tm_hour, tm1->tm_min,
             tm1->tm_sec, tv1.tv_usec);
      
     
       printf("%d %d %d \n",t_sec*1000,t_usec/1000, t_msec);
     printf("the simulation time for phase space in tracy 3 is %ld milliseconds\n",t_msec);  
   
  }
}