Skip to content
Snippets Groups Projects
soltracy.cc 21.7 KiB
Newer Older
nadolski's avatar
nadolski committed
/*
 Current revision $Revision$
 On branch $Name$
 Latest change $Date$ by $Author$
*/
zhang's avatar
zhang committed
#define ORDER 1          

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

nadolski's avatar
nadolski committed
extern bool freq_map;
zhang's avatar
zhang committed

#include "tracy_lib.h"
zhang's avatar
zhang committed
//***************************************************************************************
//
//  MAIN CODE
//
//****************************************************************************************
nadolski's avatar
nadolski committed
int main(int argc, char *argv[]) {
zhang's avatar
zhang committed
  
  /*set the random value for random generator*/
  const long seed = 1121; //the default random seed number
  iniranf(seed); //initialize the seed
  setrancut(2.0); //default value of the normal cut for the normal distribution

zhang's avatar
zhang committed

  // 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)
nadolski's avatar
nadolski committed
  globval.H_exact = false;
  /*The following values are set in the Read_lattice( ) in soleilcommon.cc*/
  //  globval.quad_fringe = false;              // quadrupole fringe field
  // globval.radiation   = false;             // synchrotron radiation
  // globval.emittance   = false;             // emittance
  // globval.pathlength  = false;
  //  globval.bpm         = 0;

zhang's avatar
zhang committed
 
zhang's avatar
zhang committed
  long i=0L; //initilize the for loop to read command string
  char CommandStr[max_str];
nadolski's avatar
nadolski committed
  double nux = 0, nuy = 0, ksix = 0, ksiy = 0;
zhang's avatar
zhang committed
  bool chroma;
  double dP = 0.0;
  long lastpos = -1L;
  char str1[S_SIZE];
zhang's avatar
zhang committed
  
zhang's avatar
zhang committed
  // paramters to read the command line in user input script
  long CommandNo = -1L;  //the number of commands, since this value is also
                         // the index of array UserCommandFlag[], so this
			 // value is always less than 1 in the really case. 
  UserCommand UserCommandFlag[500];
zhang's avatar
zhang committed
 

  // for time handling
nadolski's avatar
nadolski committed
  uint32_t start, stop;

zhang's avatar
zhang committed
 
nadolski's avatar
nadolski committed
  /************************************************************************
   read in files and flags
   *************************************************************************/
zhang's avatar
zhang committed

   
nadolski's avatar
nadolski committed
  if (argc > 1) {
zhang's avatar
zhang committed
    read_script(argv[1], true,CommandNo, UserCommandFlag);
nadolski's avatar
nadolski committed
  } else {
nadolski's avatar
nadolski committed
    fprintf(stdout, "Not enough parameters\nSyntax is program parameter file\n");
nadolski's avatar
nadolski committed
    exit_(1);
nadolski's avatar
nadolski committed
  /************************************************************************
zhang's avatar
zhang committed
    print files, very important file for debug
nadolski's avatar
nadolski committed
   *************************************************************************/
zhang's avatar
zhang committed
  //print flat file with all the design values of the lattice,
nadolski's avatar
nadolski committed
  prtmfile("flat_file.dat");
zhang's avatar
zhang committed
  // print close orbit to a file
nadolski's avatar
nadolski committed
  getcod(dP, lastpos);
  prt_cod("cod.out", globval.bpm, true);

zhang's avatar
zhang committed
  
  //get_matching_params_scl(); // get tunes and beta functions at entrance
  // compute up to 3rd order momentum compact factor
  get_alphac2(); 
  //cout << endl << "computing tune shifts" << endl;
  //dnu_dA(10e-3, 5e-3, 0.002);
  //get_ksi2(0.0); // this gets the chromas and writes them into chrom2.out
  
  
  
   // Flag factory
zhang's avatar
zhang committed
  for(i=0; i<=CommandNo; i++){//read user defined command by sequence
zhang's avatar
zhang committed
   //assign user defined command
zhang's avatar
zhang committed
    strcpy(CommandStr,UserCommandFlag[i].CommandStr); 
zhang's avatar
zhang committed
    
zhang's avatar
zhang committed
    //print the twiss paramters in a file defined by the name
    if(strcmp(CommandStr,"PrintTwissFlag") == 0) {
      cout << "\n";
      cout << "print the twiss parameters to file: "<< twiss_file << "\n";
      printlatt(twiss_file);  
   }
zhang's avatar
zhang committed
   //print the close orbit
   else if(strcmp(CommandStr,"PrintCODFlag") == 0) {
      cout << "\n";
      cout << "print the close orbit to file: "<< cod_file << "\n";
      getcod(dP, lastpos);
      prt_cod(cod_file, globval.bpm, true);
   }
    
zhang's avatar
zhang committed
    
zhang's avatar
zhang committed
    //turn on flag for quadrupole fringe field
zhang's avatar
zhang committed
   else if(strcmp(CommandStr,"QuadFringeOnFlag") == 0) {
zhang's avatar
zhang committed
      globval.quad_fringe = true;
      cout << "\n";
      cout << "globval.quad_fringe is " << globval.quad_fringe << "\n";
   }
    
zhang's avatar
zhang committed
  //set RF voltage  
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"RFvoltageFlag") == 0) {
nadolski's avatar
nadolski committed
    printf("\nSetting RF voltage:\n");
    printf("    Old RF voltage is: %lf [MV]\n", get_RFVoltage(ElemIndex("cav"))
        / 1e6);
zhang's avatar
zhang committed
    set_RFVoltage(ElemIndex("cav"), UserCommandFlag[i].RFvolt);
nadolski's avatar
nadolski committed
    printf("    New RF voltage is: %lf [MV]\n", get_RFVoltage(ElemIndex("cav"))
        / 1e6);
  }

  // Chamber factory
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"ReadChamberFlag") == 0) {
nadolski's avatar
nadolski committed
    ReadCh(chamber_file); /* read vacuum chamber from a file "Apertures.dat" , soleil version*/
  PrintCh(); // print chamber into chamber.out
zhang's avatar
zhang committed
  }
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
 
nadolski's avatar
nadolski committed
  // compute tunes by tracking (should be the same as by tps)
zhang's avatar
zhang committed
  else if (strcmp(CommandStr,"TuneTracFlag") == 0) {
nadolski's avatar
nadolski committed
    GetTuneTrac(1026L, 0.0, &nux, &nuy);
    fprintf(stdout, "From tracking: nux = % f nuz = % f \n", nux, nuy);
zhang's avatar
zhang committed
    }
zhang's avatar
zhang committed

  // compute chromaticities by tracking (should be the same as by DA)
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"ChromTracFlag") == 0) {
nadolski's avatar
nadolski committed
    start = stampstart();
    GetChromTrac(2L, 1026L, 1e-5, &ksix, &ksiy);
    stop = stampstop(start);
    fprintf(stdout, "From tracking: ksix= % f ksiz= % f \n", ksix, ksiy);
zhang's avatar
zhang committed
    }
    
zhang's avatar
zhang committed

nadolski's avatar
nadolski committed
  //generic function, to fit tunes using 1 family of quadrupoles
zhang's avatar
zhang committed
 // if (FitTuneFlag == true) {
  else if(strcmp(CommandStr,"FitTuneFlag") == 0) {
nadolski's avatar
nadolski committed
    double qf_bn = 0.0, qd_bn = 0.0;
    double qf_bn_fit = 0.0, qd_bn_fit = 0.0;
zhang's avatar
zhang committed

zhang's avatar
zhang committed
    /* quadrupole field strength before fitting*/
zhang's avatar
zhang committed
    qf_bn = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qf), 1)].Elem.M->PB[HOMmax + 2];
    qd_bn = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qd), 1)].Elem.M->PB[HOMmax + 2];
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
    /* fitting tunes*/
nadolski's avatar
nadolski committed
    fprintf(stdout,
zhang's avatar
zhang committed
        "\n Fitting tunes: %s %s, targetnux = %f, targetnuz = %f \n", UserCommandFlag[i].qf, 
	UserCommandFlag[i].qd,UserCommandFlag[i].targetnux, UserCommandFlag[i].targetnuz);
    FitTune(ElemIndex(UserCommandFlag[i].qf), ElemIndex(UserCommandFlag[i].qd), 
            UserCommandFlag[i].targetnux, UserCommandFlag[i].targetnuz);
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
    /* integrated field strength after fitting*/
zhang's avatar
zhang committed
    qf_bn_fit = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qf), 1)].Elem.M->PB[HOMmax + 2];
    qd_bn_fit = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qd), 1)].Elem.M->PB[HOMmax + 2];
zhang's avatar
zhang committed
    /* print out the quadrupole strengths before and after the fitting*/
nadolski's avatar
nadolski committed
    printf("Before the fitting, the quadrupole field strengths are: \n");
zhang's avatar
zhang committed
    printf(" %s = %f,    %s = %f\n", UserCommandFlag[i].qf, qf_bn, UserCommandFlag[i].qd, qd_bn);
nadolski's avatar
nadolski committed
    printf("After the fitting, the quadrupole field strengths are: \n");
zhang's avatar
zhang committed
    printf(" %s = %f,    %s = %f\n", UserCommandFlag[i].qf, qf_bn_fit, UserCommandFlag[i].qd, qd_bn_fit);
nadolski's avatar
nadolski committed

    /* Compute and get Twiss parameters */
    Ring_GetTwiss(chroma = true, 0.0);
    printglob(); /* print parameter list */
zhang's avatar
zhang committed
  }
nadolski's avatar
nadolski committed

  /* specific for soleil ring in which the quadrupole is cut into 2 parts*/
zhang's avatar
zhang committed
  //if (FitTune4Flag == true) {
  else if(strcmp(CommandStr,"FitTune4Flag") == 0) {
nadolski's avatar
nadolski committed
    double qf1_bn = 0.0, qf2_bn = 0.0, qd1_bn = 0.0, qd2_bn = 0.0;
    double qf1_bn_fit = 0.0, qf2_bn_fit = 0.0, qd1_bn_fit = 0.0, qd2_bn_fit =
        0.0;

zhang's avatar
zhang committed
    /* quadrupole field strength before fitting*/
zhang's avatar
zhang committed
    qf1_bn = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qf1), 1)].Elem.M->PB[HOMmax + 2];
    qf2_bn = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qf2), 1)].Elem.M->PB[HOMmax + 2];
    qd1_bn = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qd1), 1)].Elem.M->PB[HOMmax + 2];
    qd2_bn = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qd2), 1)].Elem.M->PB[HOMmax + 2];
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
    /* fitting tunes*/
nadolski's avatar
nadolski committed
    fprintf(
        stdout,
        "\n Fitting tunes for Soleil ring: %s %s %s %s, targetnux = %f, targetnuz = %f \n",
zhang's avatar
zhang committed
        UserCommandFlag[i].qf1, UserCommandFlag[i].qf2, UserCommandFlag[i].qd1, UserCommandFlag[i].qd2, 
	UserCommandFlag[i].targetnux, UserCommandFlag[i].targetnuz);
    FitTune4(ElemIndex(UserCommandFlag[i].qf1), ElemIndex(UserCommandFlag[i].qf2),
             ElemIndex(UserCommandFlag[i].qd1), ElemIndex(UserCommandFlag[i].qd2),
             UserCommandFlag[i].targetnux, UserCommandFlag[i].targetnuz);
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
    /* integrated field strength after fitting*/
zhang's avatar
zhang committed
    qf1_bn_fit = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qf1), 1)].Elem.M->PB[HOMmax + 2];
    qf2_bn_fit = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qf2), 1)].Elem.M->PB[HOMmax + 2];
    qd1_bn_fit = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qd1), 1)].Elem.M->PB[HOMmax + 2];
    qd2_bn_fit = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].qd2), 1)].Elem.M->PB[HOMmax + 2];
zhang's avatar
zhang committed
    /* print out the quadrupole strengths before and after the fitting*/
nadolski's avatar
nadolski committed
    printf("Before the fitting, the quadrupole field strengths are: \n");
zhang's avatar
zhang committed
    printf("    %s = %f,    %s = %f,    %s = %f,    %s = %f\n", UserCommandFlag[i].qf1, qf1_bn,
        UserCommandFlag[i].qf2, qf2_bn, UserCommandFlag[i].qd1, qd1_bn, UserCommandFlag[i].qd2, qd2_bn);
nadolski's avatar
nadolski committed
    printf("After the fitting, the quadrupole field strengths are: \n");
zhang's avatar
zhang committed
    printf("    %s = %f,    %s = %f,    %s = %f,    %s = %f\n", UserCommandFlag[i].qf1,
        qf1_bn_fit, UserCommandFlag[i].qf2, qf2_bn_fit, UserCommandFlag[i].qd1, qd1_bn_fit, 
	UserCommandFlag[i].qd2, qd2_bn_fit);
nadolski's avatar
nadolski committed

    /* Compute and get Twiss parameters */
    Ring_GetTwiss(chroma = true, 0.0);
    printglob(); /* print parameter list */
zhang's avatar
zhang committed
  }

zhang's avatar
zhang committed
  /* fit the chromaticities*/
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"FitChromFlag") == 0) {
  
zhang's avatar
zhang committed
    double sxm1_bn = 0.0, sxm2_bn = 0.0;
    double sxm1_bn_fit = 0.0, sxm2_bn_fit = 0.0;
zhang's avatar
zhang committed
    sxm1_bn = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].sxm1), 1)].Elem.M->PB[HOMmax + 3];
    sxm2_bn = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].sxm2), 1)].Elem.M->PB[HOMmax + 3];

    fprintf(stdout,"\n Fitting chromaticities: %s %s, targetksix = %f,  targetksiz = %f\n",
        UserCommandFlag[i].sxm1, UserCommandFlag[i].sxm2, UserCommandFlag[i].targetksix,
	 UserCommandFlag[i].targetksiz);
	 
    FitChrom(ElemIndex(UserCommandFlag[i].sxm1), ElemIndex(UserCommandFlag[i].sxm2), 
             UserCommandFlag[i].targetksix, UserCommandFlag[i].targetksiz);

    sxm1_bn_fit = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].sxm1), 1)].Elem.M->PB[HOMmax + 3];
    sxm2_bn_fit = Cell[Elem_GetPos(ElemIndex(UserCommandFlag[i].sxm2), 1)].Elem.M->PB[HOMmax + 3];
zhang's avatar
zhang committed
    /* print out the sextupole strengths before and after the fitting*/
nadolski's avatar
nadolski committed
    printf("Before the fitting, the sextupole field strengths are \n");
zhang's avatar
zhang committed
    printf("    %s = %f,    %s = %f\n", UserCommandFlag[i].sxm1, sxm1_bn, UserCommandFlag[i].sxm2, sxm2_bn);
nadolski's avatar
nadolski committed
    printf("After the fitting, the sextupole field strengths are: \n");
zhang's avatar
zhang committed
    printf("    %s = %f,    %s = %f\n", UserCommandFlag[i].sxm1, sxm1_bn_fit, UserCommandFlag[i].sxm2, sxm2_bn_fit);
nadolski's avatar
nadolski committed

    Ring_GetTwiss(chroma = true, 0.0); /* Compute and get Twiss parameters */
    printglob(); /* print parameter list */
zhang's avatar
zhang committed
  }

  // coupling calculation
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"CouplingFlag") == 0) {
nadolski's avatar
nadolski committed
    Ring_GetTwiss(chroma = true, 0.0); /* Compute and get Twiss parameters */
zhang's avatar
zhang committed
    printlatt("linlat_coupling.out"); /* dump linear lattice functions into "linlat.dat" */
zhang's avatar
zhang committed
   // GetEmittance(ElemIndex("cav"), true);  //only for test
nadolski's avatar
nadolski committed
    Coupling_Edwards_Teng();
zhang's avatar
zhang committed
    Ring_GetTwiss(chroma = true, 0.0); /* Compute and get Twiss parameters */
nadolski's avatar
nadolski committed
    printglob(); /* print parameter list */
  }
zhang's avatar
zhang committed

zhang's avatar
zhang committed
  // add coupling by random rotating of the full quadrupole magnets
zhang's avatar
zhang committed
  //if (ErrorCouplingFlag == true) {
  else if(strcmp(CommandStr,"ErrorCouplingFlag") == 0) {
zhang's avatar
zhang committed
    prtmfile("flat_file.dat"); //print the elements without rotation errors
zhang's avatar
zhang committed
    SetErr(UserCommandFlag[i].err_seed, UserCommandFlag[i].err_rms);
zhang's avatar
zhang committed
    prtmfile("flat_file_errcoupling_full.dat"); //print the elements with rotation errors
nadolski's avatar
nadolski committed
    Ring_GetTwiss(chroma = true, 0.0); /* Compute and get Twiss parameters */
zhang's avatar
zhang committed
    printlatt("linlat_errcoupling.out"); /* dump linear lattice functions into "linlat.dat" */
nadolski's avatar
nadolski committed
    Coupling_Edwards_Teng();
zhang's avatar
zhang committed
   // GetEmittance(ElemIndex("cav"), true); //only for test
    Ring_GetTwiss(chroma = true, 0.0); /* Compute and get Twiss parameters */
  //  printlatt();
nadolski's avatar
nadolski committed
    printglob(); /* print parameter list */
zhang's avatar
zhang committed
  }
zhang's avatar
zhang committed
  
zhang's avatar
zhang committed
//  add coupling by random rotating of the half quadrupole magnets, delicated for soleil
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"ErrorCoupling2Flag") == 0) {
zhang's avatar
zhang committed
    prtmfile("flat_file.dat"); //print the elements without rotation errors
zhang's avatar
zhang committed
    SetErr2(UserCommandFlag[i].err_seed, UserCommandFlag[i].err_rms);
zhang's avatar
zhang committed
    prtmfile("flat_file_errcoupling_half.dat"); //print the elements with rotation errors
zhang's avatar
zhang committed
    Ring_GetTwiss(chroma = true, 0.0); /* Compute and get Twiss parameters */
zhang's avatar
zhang committed
    printlatt("linlat_errcoupling2.out"); /* dump linear lattice functions into "linlat.dat" */
zhang's avatar
zhang committed
    Coupling_Edwards_Teng();
zhang's avatar
zhang committed
   // GetEmittance(ElemIndex("cav"), true);
   Ring_GetTwiss(chroma = true, 0.0); /* Compute and get Twiss parameters */
zhang's avatar
zhang committed
    printglob(); /* print parameter list */
  }
zhang's avatar
zhang committed

  //set multipoles in all magnets
zhang's avatar
zhang committed
  // read multipole error from a file
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"ReadMultipoleFlag") == 0) {
zhang's avatar
zhang committed
    fprintf(stdout,"\n Read Multipoles file for lattice with thick sextupoles \n");
nadolski's avatar
nadolski committed
    ReadFieldErr(multipole_file);
    //first print the full lattice with error as a flat file
zhang's avatar
zhang committed
    prtmfile("flat_file_errmultipole.dat"); // writes flat file with all element errors  /* very important file for debug*/
nadolski's avatar
nadolski committed
    Ring_GetTwiss(chroma = true, 0.0); /* Compute and get Twiss parameters */
    printglob();
zhang's avatar
zhang committed
  }
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
 
nadolski's avatar
nadolski committed

nadolski's avatar
nadolski committed
  /******************************************************************************************/
  // COMPUTATION PART after setting the model
  /******************************************************************************************/
  // computes TuneShift with amplitudes
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"AmplitudeTuneShiftFlag") == 0) {
zhang's avatar
zhang committed
      TunesShiftWithAmplitude(UserCommandFlag[i]._AmplitudeTuneShift_nxpoint,
                              UserCommandFlag[i]._AmplitudeTuneShift_nypoint,
                              UserCommandFlag[i]._AmplitudeTuneShift_nturn, 
			      UserCommandFlag[i]._AmplitudeTuneShift_xmax,
                              UserCommandFlag[i]._AmplitudeTuneShift_ymax, 
			      UserCommandFlag[i]._AmplitudeTuneShift_delta);
zhang's avatar
zhang committed
    } 
zhang's avatar
zhang committed
  // compute tuneshift with energy
 else if(strcmp(CommandStr,"EnergyTuneShiftFlag") == 0) {
     TunesShiftWithEnergy(UserCommandFlag[i]._EnergyTuneShift_npoint, 
                          UserCommandFlag[i]._EnergyTuneShift_nturn,
                          UserCommandFlag[i]._EnergyTuneShift_deltamax);
   }
nadolski's avatar
nadolski committed

nadolski's avatar
nadolski committed
  // Computes FMA
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"FmapFlag") == 0) {
    printf("\n begin Fmap calculation for on momentum particles: \n");
zhang's avatar
zhang committed
    fmap(UserCommandFlag[i]._FmapFlag_nxpoint, 
         UserCommandFlag[i]._FmapFlag_nypoint, 
	 UserCommandFlag[i]._FmapFlag_nturn, 
	 UserCommandFlag[i]._FmapFlag_xmax,
         UserCommandFlag[i]._FmapFlag_ymax, 
	 UserCommandFlag[i]._FmapFlag_delta, 
	 UserCommandFlag[i]._FmapFlag_diffusion);
zhang's avatar
zhang committed
  }

nadolski's avatar
nadolski committed
  // Compute FMA dp
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"FmapdpFlag") == 0) {
    printf("\n begin Fmap calculation for off momentum particles: \n");
zhang's avatar
zhang committed
    fmapdp(UserCommandFlag[i]._FmapdpFlag_nxpoint, 
           UserCommandFlag[i]._FmapdpFlag_nepoint, 
	   UserCommandFlag[i]._FmapdpFlag_nturn,
           UserCommandFlag[i]._FmapdpFlag_xmax, 
	   UserCommandFlag[i]._FmapdpFlag_emax, 
	   UserCommandFlag[i]._FmapdpFlag_z,
           UserCommandFlag[i]._FmapdpFlag_diffusion);
zhang's avatar
zhang committed
  }

zhang's avatar
zhang committed
//  // if (CodeComparaisonFlag) {
//   else if(strcmp(CommandStr,"CodeComparaisonFlag") == 0) {
//     fmap(200, 100, 1026, -32e-3, 7e-3, 0.0, true);
//   }
nadolski's avatar
nadolski committed

  // MOMENTUM ACCEPTANCE
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"MomentumAccFlag") == 0) {
nadolski's avatar
nadolski committed
    bool cavityflag, radiationflag;
    /* record the initial values*/
    cavityflag = globval.Cavity_on;
    radiationflag = globval.radiation;

    /* set the dimension for the momentum tracking*/
zhang's avatar
zhang committed
    if (strncmp("6D", UserCommandFlag[i].TrackDim, 2) == 0) {
nadolski's avatar
nadolski committed
      globval.Cavity_on = true;
      globval.radiation = true;
zhang's avatar
zhang committed
    } else if (strncmp("4D", UserCommandFlag[i].TrackDim, 2) == 0) {
nadolski's avatar
nadolski committed
      globval.Cavity_on = false;
      globval.radiation = false;
    } else {
      printf("MomentumAccFlag: Error!!! DimTrack must be '4D' or '6D'\n");
      exit_(1);
    };

nadolski's avatar
nadolski committed
    /* calculate momentum acceptance*/
zhang's avatar
zhang committed
    printf("\n Calculate momentum acceptance: \n");
zhang's avatar
zhang committed
    MomentumAcceptance(UserCommandFlag[i]._MomentumAccFlag_istart, 
                       UserCommandFlag[i]._MomentumAccFlag_istop,
                       UserCommandFlag[i]._MomentumAccFlag_deltaminp, 
		       UserCommandFlag[i]._MomentumAccFlag_deltamaxp,
                       UserCommandFlag[i]._MomentumAccFlag_nstepp, 
		       UserCommandFlag[i]._MomentumAccFlag_deltaminn,
                       UserCommandFlag[i]._MomentumAccFlag_deltamaxn, 
		       UserCommandFlag[i]._MomentumAccFlag_nstepn);
nadolski's avatar
nadolski committed

    /* restore the initial values*/
    globval.Cavity_on = cavityflag;
    globval.radiation = radiationflag;
  }
zhang's avatar
zhang committed

  // induced amplitude 
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"InducedAmplitudeFlag") == 0) {
   printf("\n Calculate induced amplitude: \n");
nadolski's avatar
nadolski committed
    InducedAmplitude(193L);
zhang's avatar
zhang committed
  }
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed

zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"EtaFlag") == 0) {
nadolski's avatar
nadolski committed
    // compute cod and Twiss parameters for different energy offsets
nadolski's avatar
nadolski committed
    for (int ii = 0; ii <= 40; ii++) {
      dP = -0.02 + 0.001 * ii;
      Ring_GetTwiss(chroma = false, dP); /* Compute and get Twiss parameters */
zhang's avatar
zhang committed
      printlatt("linlat_eta.out"); /* dump linear lattice functions into "linlat.dat" */
nadolski's avatar
nadolski committed
      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);
zhang's avatar
zhang committed
    }
  }
zhang's avatar
zhang committed

zhang's avatar
zhang committed
// track to get phase space
zhang's avatar
zhang committed
  else if(strcmp(CommandStr,"PhaseSpaceFlag") == 0) {
nadolski's avatar
nadolski committed
    bool cavityflag, radiationflag;
    /* record the initial values*/
    cavityflag = globval.Cavity_on;
    radiationflag = globval.radiation;

    /* set the dimension for the momentum tracking*/
zhang's avatar
zhang committed
    if (strncmp("6D", UserCommandFlag[i]._Phase_Dim, 2) == 0) {
nadolski's avatar
nadolski committed
      globval.Cavity_on = true;
zhang's avatar
zhang committed
    } else if (strncmp("4D", UserCommandFlag[i]._Phase_Dim, 2) == 0) {
nadolski's avatar
nadolski committed
      globval.Cavity_on = false;
    } else {
      printf("MomentumAccFlag: Error!!! _Phase_Dim must be '4D' or '6D'\n");
      exit_(1);
    };
    /* setting damping */
zhang's avatar
zhang committed
    if ( UserCommandFlag[i]._Phase_Damping == true) {
nadolski's avatar
nadolski committed
      globval.radiation = true;
    } else  {
      globval.radiation = false;
    }

nadolski's avatar
nadolski committed
    start = stampstart();
zhang's avatar
zhang committed
    Phase(UserCommandFlag[i]._Phase_X, 
          UserCommandFlag[i]._Phase_Px, 
	  UserCommandFlag[i]._Phase_Y, 
	  UserCommandFlag[i]._Phase_Py, 
	  UserCommandFlag[i]._Phase_delta, 
	  UserCommandFlag[i]._Phase_ctau, 
	  UserCommandFlag[i]._Phase_nturn);
nadolski's avatar
nadolski committed
    printf("the simulation time for phase space in tracy 3 is \n");
nadolski's avatar
nadolski committed
    stop = stampstop(start);
nadolski's avatar
nadolski committed

    /* restore the initial values*/
    globval.Cavity_on = cavityflag;
    globval.radiation = radiationflag;
nadolski's avatar
nadolski committed
  }
zhang's avatar
zhang committed
  
zhang's avatar
zhang committed
 
zhang's avatar
zhang committed
  
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
 else if (strcmp(CommandStr,"TouschekFlag") == 0 ||strcmp(CommandStr,"IBSFlag") == 0 ||
          strcmp(CommandStr,"TousTrackFlag") == 0 ){ 
nadolski's avatar
nadolski committed
  //  ????????????? NSRL version, Check with Soleil version "MomentumAcceptance"
  // IBS & TOUSCHEK
  int k, n_turns;
  double sigma_s, sigma_delta, tau, alpha_z, beta_z, gamma_z, eps[3];
  FILE *outf;
  const double Qb = 5e-9; // e charge in one bunch
zhang's avatar
zhang committed

zhang's avatar
zhang committed
  
zhang's avatar
zhang committed
//  else if(strcmp(CommandStr,"TouschekFlag") == 0) {
nadolski's avatar
nadolski committed
    double sum_delta[globval.Cell_nLoc + 1][2];
    double sum2_delta[globval.Cell_nLoc + 1][2];

zhang's avatar
zhang committed
    GetEmittance(ElemIndex("cav"), true);
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
    // initialize momentum aperture arrays
nadolski's avatar
nadolski committed
    for (k = 0; k <= globval.Cell_nLoc; k++) {
      sum_delta[k][0] = 0.0;
      sum_delta[k][1] = 0.0;
      sum2_delta[k][0] = 0.0;
      sum2_delta[k][1] = 0.0;
zhang's avatar
zhang committed
    }
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
    globval.eps[Y_] = 0.008e-9;
nadolski's avatar
nadolski committed
    n_turns = 40;

zhang's avatar
zhang committed
    // get the twiss parameters
nadolski's avatar
nadolski committed
    alpha_z = -globval.Ascr[ct_][ct_] * globval.Ascr[delta_][ct_]
        - globval.Ascr[ct_][delta_] * globval.Ascr[delta_][delta_];
zhang's avatar
zhang committed
    beta_z = sqr(globval.Ascr[ct_][ct_]) + sqr(globval.Ascr[ct_][delta_]);
nadolski's avatar
nadolski committed
    gamma_z = (1 + sqr(alpha_z)) / beta_z;

    sigma_delta = sqrt(gamma_z * globval.eps[Z_]);
    sigma_s = sqrt(beta_z * globval.eps[Z_]);//50e-3
    beta_z = sqr(sigma_s) / globval.eps[Z_];
    alpha_z = sqrt(beta_z * gamma_z - 1);
zhang's avatar
zhang committed

    // INCLUDE LC (LC changes sigma_s and eps_z, but has no influence on sigma_delta)
    if (false) {
nadolski's avatar
nadolski committed
      double newLength, bunchLengthening;
zhang's avatar
zhang committed
      newLength = 50e-3;
nadolski's avatar
nadolski committed
      bunchLengthening = newLength / sigma_s;
zhang's avatar
zhang committed
      sigma_s = newLength;
nadolski's avatar
nadolski committed
      globval.eps[Z_] = globval.eps[Z_] * bunchLengthening;
      beta_z = beta_z * bunchLengthening;
      gamma_z = gamma_z / bunchLengthening;
      alpha_z = sqrt(beta_z * gamma_z - 1); // this doesn't change
zhang's avatar
zhang committed
    }
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
    Touschek(Qb, globval.delta_RF, globval.eps[X_], globval.eps[Y_],
nadolski's avatar
nadolski committed
        sigma_delta, sigma_s);

zhang's avatar
zhang committed
 // Intra Beam Scattering(IBS)
zhang's avatar
zhang committed
    if (strcmp(CommandStr,"IBSFlag") == 0) {
zhang's avatar
zhang committed
      // initialize eps_IBS with eps_SR
nadolski's avatar
nadolski committed
      for (k = 0; k < 3; k++)
        eps[k] = globval.eps[k];
      for (k = 0; k < 20; k++) //prototype (looping because IBS routine doesn't check convergence)
        IBS(Qb, globval.eps, eps, alpha_z, beta_z);
    }
zhang's avatar
zhang committed

    // TOUSCHEK TRACKING
    // Calculate Touschek lifetime
    // with the momentum acceptance which is determined by 
    // the RF acceptance delta_RF and the momentum aperture 
    // at each element location which is tracked over n turns, 
    //the vacuum chamber is read from the file "chamber_file"  
    // finally, write the momentum acceptance to file "mom_aper.out".
zhang's avatar
zhang committed
    if (strcmp(CommandStr,"TousTrackFlag") == 0) {
zhang's avatar
zhang committed
      globval.Aperture_on = true;
nadolski's avatar
nadolski committed
      ReadCh(chamber_file);
      //      LoadApers("/home/simon/projects/src/lattice/Apertures.dat", 1, 1);
      tau = Touschek(Qb, globval.delta_RF, false, globval.eps[X_],
          globval.eps[Y_], sigma_delta, sigma_s, n_turns, true, sum_delta,
          sum2_delta); //the TRUE flag requires apertures loaded

      printf("Touschek lifetime = %10.3e hrs\n", tau / 3600.0);

zhang's avatar
zhang committed
      outf = file_write("mom_aper.out");
nadolski's avatar
nadolski committed
      for (k = 0; k <= globval.Cell_nLoc; k++)
        fprintf(outf, "%4d %7.2f %5.3f %6.3f\n", k, Cell[k].S, 1e2
            * sum_delta[k][0], 1e2 * sum_delta[k][1]);
zhang's avatar
zhang committed
      fclose(outf);
    }
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
  }
zhang's avatar
zhang committed
  else
    printf("Wrong!!!!!");
 }//end of looking for user defined flag
nadolski's avatar
nadolski committed

zhang's avatar
zhang committed
  
  return 0;
zhang's avatar
zhang committed
}//end of main()