/home/cern/BDSIM_new/parser/parser.h

00001 /*
00002  *  parser.h
00003  *
00004  *    GMAD parser functions
00005  *    Ilya Agapov 2005-2006
00006  *    bdsim v.0.3
00007  */
00008 
00009 #ifndef __PARSER_H
00010 #define __PARSER_H
00011 
00012 #include "sym_table.h"
00013 #ifndef _WIN32
00014 #include <unistd.h>
00015 #endif
00016 #include <cstdlib>
00017 #include <cstdio>
00018 #include <cstring>
00019 #include <cmath>
00020 #include <list>
00021 #include <vector>
00022 
00023 #include <iostream>
00024 
00025 #include "gmad.h"
00026 
00027 //using namespace std;
00028 
00029 //double pow(double x, double y) {return exp( y * log(x));}
00030 
00031 int yyerror(char *);
00032 
00033 extern FILE* yyin;
00034 extern int yylex();
00035 
00036 
00037 const int MAX_EXPAND_ITERATIONS = 50;
00038 const int MAX_MULTIPOLE_ORDER = 5;
00039 
00040 const int _undefined = 0;
00041 
00042 std::list<double> _tmparray;  // for reading of arrays
00043 std::list<char*> _tmpstring;
00044 
00045 // representation of arrays used in tokens
00046 struct Array {
00047   std::vector<char*> symbols;
00048   double *data;
00049   int size;
00050 };
00051 
00052 const char *typestr(int type) {
00053   switch(type){
00054   case _MARKER : 
00055     return "marker";
00056   case _DRIFT :
00057     return "drift";
00058   case _RF :
00059    return "rf";
00060   case _SBEND : 
00061     return "sbend";
00062   case _RBEND :
00063     return "rbend";
00064   case _QUAD :
00065     return "quadrupole";
00066   case _SEXTUPOLE :
00067     return "sextupole";
00068   case _OCTUPOLE :
00069     return "octupole";
00070   case _MULT :
00071     return "multipole";
00072   case _SOLENOID : 
00073     return "solenoid";
00074   case _ECOL : 
00075     return "ecol";
00076   case _VKICK :
00077     return "vkick";
00078   case _HKICK :
00079     return "hkick";
00080   case _RCOL : 
00081     return "rcol";
00082   case _LINE :
00083     return "line";
00084   case _REV_LINE :
00085     return "rev_line";
00086   case _SAMPLER :
00087     return "sampler";
00088   case _CSAMPLER:
00089     return "csampler";
00090   case _GAS:
00091     return "gas";
00092   case _TUNNEL:
00093     return "tunnel";
00094   case _MATERIAL:
00095     return "material";
00096   case _ATOM:
00097     return "atom";
00098   case _LASER:
00099     return "laser";
00100   case _ELEMENT :
00101     return "element";
00102   case _TRANSFORM3D :
00103     return "transform3d";
00104   default:
00105     return "none";
00106   }
00107 };
00108 struct Parameters params;
00109 struct Options options;
00110 struct Element element;
00111 
00112 void print(struct Parameters params)
00113 {
00114   printf("printing parameters:\n");
00115   std::list<double>::iterator it;
00116   for(it = params.knl.begin();it!=params.knl.end();it++)
00117     printf(" %f ", (*it));
00118   printf("\n");
00119 };
00120 
00121 void flush(struct Element& e )
00122 {
00123   e.l = 0;
00124   e.B = 0;
00125   e.ks = 0;
00126   e.k0 = 0;
00127   e.k1 = 0;
00128   e.k2 = 0;
00129   e.k3 = 0;
00130   e.angle = 0;
00131   e.tilt = 0;
00132   e.phi = 0;
00133   e.psi = 0;
00134   e.theta = 0;
00135 
00136   e.gradient = 0;
00137 
00138   e.hgap = 0;
00139   e.aper = 0;
00140   e.outR = 0;
00141   e.waveLength = 0;
00142 
00143   e.xdir = 0;
00144   e.ydir = 0;
00145   e.zdir = 0;
00146 
00147   e.name = NULL;
00148   e.type = _NONE;
00149 
00150   e.A = 0;
00151   e.Z = 0;
00152   e.density = 0;      //g*cm-3
00153   e.temper = 300;     //kelvin
00154   e.pressure = 0;     //atm
00155   e.state = "";  //allowed values: "solid", "liquid", "gas"
00156 
00157   /*  
00158       e.knl = std::list<double>(0);
00159       e.ksl = std::list<double>(0);
00160       
00161       geometryFile
00162       bmapFile
00163       material;
00164   */
00165 
00166   //e.material = "";
00167   e.spec = "";
00168 };
00169 
00170 void copy_properties(std::list<struct Element>::iterator dest, std::list<struct Element>::iterator src)
00171 {
00172 
00173   if(DEBUG) printf("%s %s \n",typestr((*dest).type),typestr((*src).type));
00174 
00175   (*dest).type = (*src).type;  
00176  
00177   (*dest).l = (*src).l;
00178 
00179   (*dest).angle = (*src).angle; 
00180   (*dest).xsize = (*src).xsize; 
00181   (*dest).ysize = (*src).ysize; 
00182 
00183   (*dest).xdir = (*src).xdir; 
00184   (*dest).ydir = (*src).ydir; 
00185   (*dest).zdir = (*src).zdir; 
00186   (*dest).phi = (*src).phi; 
00187   (*dest).theta = (*src).theta; 
00188   (*dest).psi = (*src).psi; 
00189   (*dest).waveLength = (*src).waveLength; 
00190  
00191   (*dest).aper = (*src).aper; 
00192   (*dest).outR = (*src).outR; 
00193   (*dest).tilt = (*src).tilt; 
00194   (*dest).B = (*src).B; 
00195   (*dest).ks = (*src).ks;
00196   (*dest).k0 = (*src).k0;
00197   (*dest).k1 = (*src).k1;
00198   (*dest).k2 = (*src).k2;
00199   (*dest).k3 = (*src).k3;
00200   (*dest).knl = (*src).knl;
00201   (*dest).ksl = (*src).ksl;
00202   (*dest).hgap = (*src).hgap;
00203 
00204   (*dest).gradient = (*src).gradient; 
00205 
00206   (*dest).A = (*src).A;
00207   (*dest).Z = (*src).Z;
00208   (*dest).density = (*src).density;
00209   (*dest).temper = (*src).temper; 
00210   (*dest).pressure = (*src).pressure; 
00211   (*dest).state = (*src).state; 
00212   (*dest).components = (*src).components;
00213   (*dest).componentsWeights = (*src).componentsWeights;
00214   (*dest).componentsFractions = (*src).componentsFractions;
00215   (*dest).symbol = (*src).symbol;
00216 
00217   (*dest).geometryFile = (*src).geometryFile;
00218 
00219   (*dest).bmapFile = (*src).bmapFile;
00220 
00221   (*dest).material = (*src).material;
00222 
00223   (*dest).spec = (*src).spec;
00224 
00225   return;
00226 }; 
00227 
00228 void inherit_properties(struct Element e)
00229 {
00230   // copy parameters into temporary buffer params from element e
00231   // parameters already set in params have priority and are not overridden
00232   
00233   if(!params.lset) { params.l = e.l; params.lset = 1; }
00234   if(!params.Bset) { params.B = e.B; params.Bset = 1; }
00235   if(!params.ksset) { params.ks = e.ks; params.ksset = 1; }
00236   if(!params.k0set) { params.k0 = e.k0; params.k0set = 1; }
00237   if(!params.k1set) { params.k1 = e.k1; params.k1set = 1; }
00238   if(!params.k2set) { params.k2 = e.k2; params.k2set = 1; }
00239   if(!params.k3set) { params.k3 = e.k3; params.k3set = 1; }
00240   if(!params.angleset) { params.angle = e.angle; params.angleset = 1; }
00241   if(!params.xsizeset) { params.xsize = e.xsize; params.xsizeset = 1; }
00242   if(!params.ysizeset) { params.ysize = e.ysize; params.ysizeset = 1; }
00243  
00244   if(!params.xdirset) { params.xdir = e.xdir; params.xdirset = 1; }
00245   if(!params.ydirset) { params.ydir = e.ydir; params.ydirset = 1; }
00246   if(!params.zdirset) { params.zdir = e.zdir; params.zdirset = 1; }
00247   if(!params.waveLength) { params.waveLength = e.waveLength; params.waveLengthset = 1; }
00248   if(!params.phiset) { params.phi = e.phi; params.phiset = 1; }
00249   if(!params.psiset) { params.psi = e.psi; params.psiset = 1; }
00250   if(!params.thetaset) { params.theta = e.theta; params.thetaset = 1; }
00251   if(!params.hgapset) { params.hgap = e.hgap; params.hgapset = 1; }
00252 
00253   //materials
00254   if(!params.Aset) { params.A = e.A; params.Aset = 1; }
00255   if(!params.Zset) { params.Z = e.Z; params.Zset = 1; }
00256   if(!params.densityset) { params.density = e.density; params.densityset = 1; }
00257   if(!params.temperset) { params.temper = e.temper; params.temperset = 1; }
00258   if(!params.pressureset) { params.pressure = e.pressure; params.pressureset = 1; }
00259   if(!params.stateset) { strncpy(params.state, e.state.c_str(),64); params.stateset = 1; }
00260   if(!params.symbolset) { strncpy(params.symbol, e.symbol.c_str(),64); params.symbolset = 1; }
00261   if(!params.componentsset) 
00262     { params.components = e.components; params.componentsset = 1; }
00263   if(!params.componentsWeightsset) 
00264     { params.componentsWeights = e.componentsWeights; params.componentsWeightsset = 1; }
00265   if(!params.componentsFractionsset) 
00266     { params.componentsFractions = e.componentsFractions; params.componentsFractionsset = 1; }
00267 
00268   if(!params.aperset) { params.aper = e.aper; params.aperset = 1; }
00269   if(!params.outRset) { params.outR = e.outR; params.outRset = 1; }
00270 
00271   if(!params.gradientset) { params.gradient = e.gradient; params.gradientset = 1; }
00272 
00273   if(!params.tiltset) { params.tilt = e.tilt; params.tiltset = 1; }
00274   if(!params.knlset) { params.knl = e.knl; params.knlset = 1; }
00275   if(!params.kslset) { params.ksl = e.ksl; params.kslset = 1; }
00276 
00277   if(!params.specset) { strncpy(params.spec,e.spec.c_str(),1024); params.specset = 1; }
00278   if(!params.materialset) { strncpy(params.material,e.spec.c_str(),64); params.materialset = 1; }
00279 
00280 
00281 
00282 };
00283 
00284 void set_vector(std::list<double>& dst, struct Array *src)
00285 {
00286   for(int i=0; i< src->size;i++){
00287     dst.push_back(src->data[i]);
00288     if(DEBUG) std::cout << src->data[i] << " ";
00289   }
00290   if(DEBUG) std::cout << std::endl;
00291   
00292 };
00293 
00294 
00295 void set_vector(std::list<char*>& dst, struct Array *src)
00296 {
00297   for(int i=0; i< src->size;i++){
00298     dst.push_back(src->symbols[i]);
00299     if(DEBUG) std::cout << src->symbols[i] << " ";
00300   }
00301   if(DEBUG) std::cout << std::endl;
00302 };
00303 
00304 void set_vector(std::list<int>& dst, struct Array *src)
00305 {
00306   for(int i=0; i< src->size;i++){
00307     dst.push_back((int)(src->data[i]));
00308     if(DEBUG) std::cout << (int)(src->data[i]) << " ";
00309   }
00310   if(DEBUG) std::cout << std::endl;
00311 };
00312 
00313 
00314 // list of all encountered elements
00315 std::list<struct Element> element_list;
00316 
00317 // temporary list
00318 std::list<struct Element> tmp_list;
00319 
00320 std::list<struct Element> beamline_list;
00321 std::list<struct Element> material_list;
00322 std::list<struct Element> atom_list;
00323 
00324 char* current_line = "";
00325 char* current_start = "";
00326 char* current_end = "";
00327 
00328 struct symtab *symtab; 
00329 
00330 extern struct symtab * symlook(char *s);
00331 
00332 std::list<struct Element>::iterator element_lookup(char *name);
00333 std::list<struct Element>::iterator element_lookup(char *name, std::list<struct Element>& el);
00334 int write_table(struct Parameters params,char* name, int type, std::list<struct Element> *lst=NULL);
00335 int expand_line(char *name, char *start, char *end);
00336 void print(std::list<struct Element> l, int ident=0);
00337 
00338 // *********************
00339 // functions
00340 // *********************
00341 
00342 
00343 void help()
00344 {
00345   printf("helping...\n");
00346 }
00347 
00348 void quit()
00349 {
00350   printf("parsing complete...\n");
00351   exit(0);
00352 }
00353 
00354 int write_table(struct Parameters params,char* name, int type, std::list<struct Element> *lst)
00355 {
00356   if(DEBUG) printf("k1=%.10g, k2=%.10g, k3=%.10g, type=%d, lset = %d\n", params.k1, params.k2, params.k3, type, params.lset);
00357   
00358   struct Element e;
00359   flush(e);
00360   // common parameters for all elements
00361   e.name = name;
00362   e.lst = NULL;
00363   e.aper = params.aper;
00364   e.outR = params.outR;
00365   e.xsize = params.xsize;
00366   e.ysize = params.ysize;
00367   e.material = params.material;  
00368   
00369   //specific parameters
00370   switch(type) {
00371 
00372   case _MARKER :
00373     e.type= _MARKER;
00374     break;
00375 
00376   case _DRIFT:
00377     e.type = _DRIFT;
00378     e.l = params.l;
00379     break;
00380 
00381   case _RF:
00382     e.type = _RF;
00383     e.l = params.l;
00384     e.gradient = params.gradient;
00385     break;
00386 
00387   case _SBEND:
00388     e.type = _SBEND;
00389     e.l = params.l;
00390     e.B = params.B;
00391     e.angle = params.angle;
00392     e.hgap = params.hgap;
00393     e.k1 = params.k1;
00394     if(params.tiltset) e.tilt = params.tilt;
00395     break;
00396 
00397   case _RBEND:
00398     e.type = _RBEND;
00399     e.l = params.l;
00400     e.B = params.B;
00401     e.angle = params.angle;
00402     e.hgap = params.hgap;
00403     e.k1 = params.k1;
00404     if(params.tiltset) e.tilt = params.tilt;
00405     break;
00406 
00407   case _VKICK:
00408     e.type = _VKICK;
00409     e.l = params.l;
00410     e.B = params.B;
00411     e.angle = params.angle;
00412     if(params.tiltset) e.tilt = params.tilt;
00413     break;
00414 
00415   case _HKICK:
00416     e.type = _HKICK;
00417     e.l = params.l;
00418     e.B = params.B;
00419     e.angle = params.angle;
00420     if(params.tiltset) e.tilt = params.tilt;
00421     break;
00422 
00423   case _QUAD:
00424     e.type = _QUAD;      
00425     e.l = params.l;
00426     if(params.k0set) {
00427       if(VERBOSE)
00428         printf("Warning: k0 will not be set for element %s of type QUADRUPOLE\n",name);
00429     }
00430     if(params.k1set) {
00431       e.k1 = params.k1;
00432     }
00433     if(params.k2set) {
00434       if(VERBOSE)
00435         printf("Warning: k2 will not be set for element %s of type QUADRUPOLE\n",name);
00436     }
00437     if(params.k3set) {
00438       if(VERBOSE)
00439         printf("Warning: k3 will not be set for element %s of type QUADRUPOLE\n",name);
00440     }
00441     if(params.tiltset) {
00442       e.tilt = params.tilt;
00443     }
00444     e.spec = std::string(params.spec); 
00445     break;
00446 
00447   case _SEXTUPOLE:
00448     e.type = _SEXTUPOLE;
00449     e.l = params.l;
00450     if(params.k0set) {
00451       if(VERBOSE)
00452         printf("Warning: k0 will not be set for element %s of type SEXTUPOLE\n",name);
00453     }
00454     if(params.k1set) {
00455       if(VERBOSE)
00456         printf("Warning: k1 will not be set for element %s of type SEXTUPOLE\n",name);
00457     }
00458     if(params.k2set) {
00459       e.k2 = params.k2;
00460     }
00461     if(params.k3set) {
00462       if(VERBOSE)
00463         printf("Warning: k3 will not be set for element %s of type SEXTUPOLE\n",name);
00464     }
00465     break;
00466 
00467   case _OCTUPOLE:
00468     e.type = _OCTUPOLE;
00469     e.l = params.l;
00470     if(params.k0set) {
00471       if(VERBOSE)
00472         printf("Warning: k0 will not be set for element %s of type OCTUPOLE\n",name);
00473     }
00474     if(params.k1set) {
00475       if(VERBOSE)
00476         printf("Warning: k1 will not be set for element %s of type OCTUPOLE\n",name);
00477     }
00478     if(params.k2set) {
00479       if(VERBOSE)
00480         printf("Warning: k2 will not be set for element %s of type OCTUPOLE\n",name);
00481     }
00482     if(params.k3set) {
00483       e.k3 = params.k3;
00484     }
00485     break;
00486 
00487   case _MULT:
00488     e.type = _MULT;
00489     e.l = params.l;
00490     if(params.knlset)
00491       e.knl = params.knl;
00492     if(params.kslset)
00493       e.ksl = params.ksl;
00494     if(params.k0set) {
00495       if(VERBOSE)
00496         printf("Warning: k0 will not be set for element %s of type MULTIPOLE\n",name);
00497     }
00498     if(params.k1set) {
00499       if(VERBOSE)
00500         printf("Warning: k1 will not be set for element %s of type MULTIPOLE\n",name);
00501     }
00502     if(params.k2set) {
00503       if(VERBOSE)
00504         printf("Warning: k2 will not be set for element %s of type MULTIPOLE\n",name);
00505     }
00506     if(params.k3set) {
00507       if(VERBOSE)
00508         printf("Warning: k3 will not be set for element %s of type MULTIPOLE\n",name);
00509     }
00510     break;
00511 
00512   case _SOLENOID:
00513     e.type = _SOLENOID;
00514     e.l = params.l;
00515     e.ks = params.ks;
00516     e.B = params.B;
00517     break;
00518 
00519   case _ECOL:
00520     e.type = _ECOL;
00521     e.l = params.l;
00522     e.material = std::string(params.material);
00523     break;
00524 
00525   case _RCOL:
00526     e.type = _RCOL;
00527     e.l = params.l;
00528     e.material = std::string(params.material);
00529     break;
00530 
00531   case _LASER:
00532     e.type = _LASER;
00533     e.l = params.l;
00534     e.xdir = params.xdir;
00535     e.ydir = params.ydir;
00536     e.zdir = params.zdir;
00537     e.waveLength = params.waveLength;
00538     break;
00539 
00540   case _ELEMENT:
00541     e.type = _ELEMENT;
00542     e.l = params.l;
00543     e.geometryFile = std::string(params.geometry);
00544     e.bmapFile = std::string(params.bmap);
00545     break;
00546 
00547   case _LINE:
00548     e.lst = lst;
00549     e.type = _LINE;
00550     break;
00551 
00552   case _REV_LINE:
00553     e.lst = lst;
00554     e.type = _REV_LINE;
00555     break;
00556 
00557   case _SAMPLER:
00558     e.type = _SAMPLER;
00559     break;
00560     
00561   case _TRANSFORM3D:
00562     e.type = _TRANSFORM3D;
00563     e.xdir = params.xdir;
00564     e.ydir = params.ydir;
00565     e.zdir = params.zdir;
00566     e.theta = params.theta;
00567     e.phi = params.phi;
00568     e.psi = params.psi;
00569     break;
00570 
00571   case _MATERIAL:
00572     e.type = _MATERIAL;
00573     e.A = params.A;
00574     e.Z = params.Z;
00575     e.density = params.density;
00576     e.temper = params.temper;
00577     e.pressure = params.pressure;
00578     e.state = params.state;
00579     e.components = params.components;
00580     e.componentsWeights = params.componentsWeights;
00581     e.componentsFractions = params.componentsFractions;
00582     material_list.push_back(e);
00583     return 0;
00584 
00585   case _ATOM:
00586     e.type = _ATOM;
00587     e.A = params.A;
00588     e.Z = params.Z;
00589     e.symbol = params.symbol;
00590     atom_list.push_back(e);
00591     return 0;
00592 
00593   case _TUNNEL:
00594     e.type = _TUNNEL;
00595     e.l = -1;
00596     e.geometryFile = std::string(params.geometry);
00597     break;
00598 
00599   default:
00600     break;  
00601   }
00602 
00603   element_list.push_back(e);
00604 
00605   return 0;
00606 
00607 }
00608 
00609 int expand_line(char *name, char *start, char* end)
00610 {
00611   std::list<struct Element>::const_iterator iterNULL = element_list.end(); //bugfix for gcc 4.1.2 - cannot compare iterator to int(NULL). SPM
00612   std::list<struct Element>::iterator it;
00613   
00614   struct Element e;
00615   
00616   it = element_lookup(name);
00617   
00618 //  if( (it!=NULL) && ((*it).type == _LINE || (*it).type == _REV_LINE) ) 
00619   if((*it).type == _LINE || (*it).type == _REV_LINE ) 
00620 
00621     {
00622       
00623       // delete the previous beamline
00624       
00625       beamline_list.clear();
00626       
00627       // expand the desired beamline
00628       
00629       e.type = (*it).type;
00630       e.name = name;
00631       e.l = 0;
00632       e.lst = NULL;
00633       
00634       beamline_list.push_back(e);
00635       
00636       if(VERBOSE) printf("expanding line %s, range = %s/%s\n",name,start,end);
00637       
00638       if(!(*it).lst) return 0; //list empty
00639 
00640       
00641       // first expand the whole range 
00642       std::list<struct Element>::iterator sit = (*it).lst->begin();
00643       std::list<struct Element>::iterator eit = (*it).lst->end();
00644 
00645       // copy the list into the resulting list
00646       switch((*it).type){
00647         case _LINE:
00648           beamline_list.insert(beamline_list.end(),(*it).lst->begin(),(*it).lst->end());
00649           break;
00650         case _REV_LINE:
00651           beamline_list.insert(beamline_list.end(),(*it).lst->rbegin(),(*it).lst->rend());
00652           break;
00653         default:
00654           beamline_list.insert(beamline_list.end(),(*it).lst->begin(),(*it).lst->end());
00655         }
00656       bool is_expanded = false;
00657       
00658       // insert material entries.
00659       // TODO:::
00660 
00661 
00662       // parse starting from the second element until the list is expanded
00663       int iteration = 0;
00664       while(!is_expanded)
00665         {
00666           is_expanded = true;
00667           for(it = ++beamline_list.begin();it!=beamline_list.end();it++ )
00668             {
00669               if(DEBUG) printf("%s , %s \n",(*it).name,typestr((*it).type));
00670               
00671               if((*it).type == _LINE || (*it).type == _REV_LINE)  // list - expand further        
00672                 {
00673                   is_expanded = false;
00674                   // lookup the line in main list
00675                   std::list<struct Element>::iterator tmpit = element_lookup((*it).name);
00676 
00677                   if( (tmpit != iterNULL) && ( (*tmpit).lst != NULL) ) { // sublist found and not empty
00678                     
00679                     if(DEBUG)
00680                       printf("inserting sequence for %s - %s ...",(*it).name,(*tmpit).name);
00681                     if((*it).type == _LINE)
00682                       beamline_list.insert(it,(*tmpit).lst->begin(),(*tmpit).lst->end());
00683                     else if((*it).type == _REV_LINE){
00684                       //iterate over list and invert any sublines contained within. SPM
00685                       std::list<struct Element> tmpList;
00686                       tmpList.insert(tmpList.end(),(*tmpit).lst->begin(),(*tmpit).lst->end());
00687                       for(std::list<struct Element>::iterator 
00688                         itLineInverter = tmpList.begin();
00689                         itLineInverter != tmpList.end(); itLineInverter++){
00690                           if((*itLineInverter).type == _LINE ||
00691                             (*itLineInverter).type == _REV_LINE)
00692                               (*itLineInverter).type *= -1;}
00693                       beamline_list.insert(it,tmpList.rbegin(),tmpList.rend());
00694                     }
00695                     if(DEBUG) printf("inserted\n");
00696                     
00697                     // delete the list pointer
00698                     beamline_list.erase(it--);
00699                     
00700                   } else if ( tmpit != iterNULL ) // entry points to a scalar element type -
00701                     //transfer properties from the main list
00702                     { 
00703                       if(DEBUG) printf("keeping element...%s\n",(*it).name);
00704                       copy_properties(it,tmpit);
00705                       if(DEBUG) printf("done\n");
00706 
00707                     } else  // element of undefined type - neglecting
00708                       {
00709                         if(VERBOSE)
00710                           printf("Warning : Expanding line %s : element %s has not been \
00711                                defined , skipping \n",name,(*it).name);
00712                         beamline_list.erase(it--);
00713                       }
00714                   
00715                 } else  // element - keep as it is 
00716                   {
00717                     // do nothing
00718                   }
00719               
00720             }
00721           iteration++;
00722           if( iteration > MAX_EXPAND_ITERATIONS )
00723             {
00724               printf("Error : Line expansion of '%s' seems to loop, \
00725                      \n possible recursive line definition,quitting \n",name);
00726               exit(0);
00727             }
00728           
00729         }// while
00730       
00731       
00732       // leave only the desired range
00733       //
00734       // rule - from first occurence of 'start' till first 'end' coming after 'start'
00735 
00736 
00737       if( (start!=NULL)) // determine the start element
00738         {
00739           sit = element_lookup(start,beamline_list);
00740           
00741           if(sit==iterNULL)
00742             {
00743               sit = beamline_list.begin();
00744             }
00745           
00746           if(!strcmp(start,"#s")) sit = beamline_list.begin(); 
00747           
00748           beamline_list.erase(beamline_list.begin(),sit);
00749 
00750         }
00751 
00752       if( (end!=NULL)) // determine the end element
00753         {
00754           eit = element_lookup(end,beamline_list);
00755           
00756           if(eit==iterNULL)
00757             {
00758               eit = beamline_list.end();
00759             }
00760           
00761           if(!strcmp(end,"#e")) eit = beamline_list.end();        
00762 
00763 
00764           beamline_list.erase(++eit,beamline_list.end());
00765         }
00766 
00767 
00768       // insert the tunnel if present
00769 
00770       it = element_lookup("tunnel");
00771       if(it!=iterNULL)
00772         beamline_list.push_back(*it);
00773       
00774       return 0;
00775     }
00776   
00777   
00778   printf("line '%s' not found",name);
00779   return 1;
00780   
00781 }
00782 
00783 std::list<struct Element>::iterator element_lookup(char *name)
00784 {
00785    std::list<struct Element>::iterator it;
00786 
00787    for(it=element_list.begin();it!=element_list.end();it++)
00788      {
00789        if(!strcmp((*it).name,name) )
00790          return it;
00791      }
00792    return element_list.end();
00793 }
00794 
00795 std::list<struct Element>::iterator element_lookup(char *name,std::list<struct Element>& el)
00796 {
00797    std::list<struct Element>::iterator it;
00798 
00799    for(it=el.begin();it!=el.end();it++)
00800      {
00801        if(!strcmp((*it).name,name) )
00802          return it;
00803      }
00804    return element_list.end();
00805 }
00806 
00807 
00808 // insert a sampler into beamline_list
00809 void add_sampler(char *name, char *before, int before_count)
00810 {
00811   if(DEBUG) std::cout<<"inserting sampler before "<<before<<"["<<before_count<<"]"<<std::endl;
00812 
00813   std::list<struct Element>::iterator it;
00814 
00815   int element_count = 1;  // count from 1 like in goddam FORTRAN -- for range parsing
00816   struct Element e;
00817   e.type = _SAMPLER;
00818   e.name = name;
00819   e.lst = NULL;
00820 
00821   for(it = beamline_list.begin();it != beamline_list.end(); ++it)
00822     {
00823       if(DEBUG) std::cout<<"-->"<<(*it).name<<std::endl;
00824 
00825       if( !strcmp((*it).name, before)) 
00826         {
00827 
00828           if( before_count == element_count)
00829             {
00830               beamline_list.insert(it,e);
00831               return;
00832             }
00833 
00834           element_count++;
00835         }
00836 
00837     }
00838 
00839   std::cout<<"current beamline doesn't contain element "<<before<<" with number "<<before_count<<std::endl;
00840 
00841 }
00842 
00843 // insert a cylindrical sampler into beamline_list
00844 void add_csampler(char *name, char *before, int before_count, double length, double rad)
00845 {
00846   if(DEBUG) std::cout<<"inserting sampler before "<<before<<"["<<before_count<<"]"<<std::endl;
00847 
00848   std::list<struct Element>::iterator it;
00849 
00850   int element_count = 1;  // count from 1 like in goddam FORTRAN -- for range parsing
00851   struct Element e;
00852   e.type = _CSAMPLER;
00853   e.l = length;
00854   e.r = rad;
00855   e.name = name;
00856   e.lst = NULL;
00857 
00858   for(it = beamline_list.begin();it != beamline_list.end(); ++it)
00859     {
00860       if(DEBUG) std::cout<<"-->"<<(*it).name<<std::endl;
00861 
00862       if( !strcmp((*it).name, before)) 
00863         {
00864 
00865           if( before_count == element_count)
00866             {
00867               beamline_list.insert(it,e);
00868               return;
00869             }
00870 
00871           element_count++;
00872         }
00873 
00874     }
00875 
00876   std::cout<<"current beamline doesn't contain element "<<before<<" with number "<<before_count<<std::endl;
00877 
00878 }
00879 
00880 // insert a beam dumper into beamline_list
00881 void add_dump(char *name, char *before, int before_count)
00882 {
00883   if(DEBUG) std::cout<<"inserting dump before "<<before<<"["<<before_count<<"]"<<std::endl;
00884 
00885   std::list<struct Element>::iterator it;
00886 
00887   int element_count = 1;  // count from 1 like in goddam FORTRAN -- for range parsing
00888   struct Element e;
00889   e.type = _DUMP;
00890   e.name = name;
00891   e.lst = NULL;
00892 
00893   for(it = beamline_list.begin();it != beamline_list.end(); ++it)
00894     {
00895       if(DEBUG) std::cout<<"-->"<<(*it).name<<std::endl;
00896 
00897       if( !strcmp((*it).name, before))
00898         {
00899 
00900           if( before_count == element_count)
00901             {
00902               beamline_list.insert(it,e);
00903               return;
00904             }
00905 
00906 
00907           element_count++;
00908         }
00909 
00910     }
00911 
00912   std::cout<<"current beamline doesn't contain element "<<before<<" with number "<<before_count<<std::endl;
00913 
00914 }
00915 
00916 // insert beam gas                                             
00917 void add_gas(char *name, const char *before, int before_count, const char *material)
00918 {
00919   printf("gas %s will be inserted into %s number %d\n",material,before,before_count);
00920   struct Element e;
00921   e.type = _GAS;
00922   e.name = name;
00923   e.lst = NULL;
00924   element_list.insert(beamline_list.end(),e);
00925  
00926 }
00927 
00928 
00929 void print(std::list<struct Element> l, int ident)
00930 {
00931 
00932   if(VERBOSE) if(ident == 0) printf("using line %s\n",current_line);
00933 
00934   std::list<struct Element>::iterator it;
00935   std::list<double>::iterator it2;
00936 
00937   for(it=l.begin();it!=l.end();it++)
00938     {
00939       for(int i=0;i<ident;i++)
00940         printf("--");
00941 
00942       printf("->%s : %s",(*it).name,typestr((*it).type));
00943 
00944       switch((*it).type) {
00945       case _DRIFT:
00946       case _SBEND:
00947       case _RBEND:
00948       case _QUAD:
00949       case _SEXTUPOLE:
00950       case _OCTUPOLE:
00951         printf(", l=%.10g, k0=%.10g, k1=%.10g, k2=%.10g, k3=%.10g, angle=%.10g,tilt=%.10g ",
00952                (*it).l,(*it).k0,(*it).k1,(*it).k2,(*it).k3,(*it).angle,(*it).tilt);
00953         break;
00954 
00955       case _SOLENOID:
00956         printf(", l=%.10g, ks=%.10g ", (*it).l, (*it).ks);
00957         break;
00958 
00959       case _MULT:
00960         printf(" , knl={");
00961         for(it2=(*it).knl.begin();it2!=(*it).knl.end();it2++)
00962           printf("%.10g, ",(*it2));
00963         printf("},  ksl={");
00964         for(it2=(*it).ksl.begin();it2!=(*it).ksl.end();it2++)
00965           printf("%.10g, ",(*it2));
00966         printf("}");
00967         break;
00968 
00969       case _ELEMENT:
00970         printf("\ngeometry file : %s\n",(*it).geometryFile.c_str());
00971         printf("B map file : %s\n",(*it).bmapFile.c_str());
00972         //printf("E map driver : %s\n",(*it).geometryFile);
00973         //printf("E map file : %s\n",(*it).geometryFile);
00974         break;
00975 
00976       case _CSAMPLER:
00977         printf(" length=%.10g, radius=%.10g",(*it).l, (*it).r);
00978         break;
00979 
00980       case _TRANSFORM3D:
00981         printf(" xdir=%.10g, ydir=%.10g, zdir=%.10g,  phi=%.10g, theta=%.10g,psi=%.10g",
00982                (*it).xdir, (*it).ydir, (*it).zdir, (*it).phi, (*it).theta, (*it).psi);
00983         break;
00984       case _MATERIAL:
00985         printf(" A=%.10g, Z=%.10g, density=%.10g,  temper=%.10g, pressure=%.10g",
00986                (*it).A, (*it).Z, (*it).density, (*it).temper, (*it).pressure);
00987         break;
00988       default:
00989         break;
00990       }
00991 
00992       printf("\n");
00993 
00994       if((*it).lst != NULL)
00995         {
00996           print(*(*it).lst,++ident);
00997           ident--;
00998         }
00999     }
01000 }
01001 
01002 void print(struct Options opt)
01003 {
01004   std::cout<<"Options : "<<std::endl;
01005   std::cout<<"particle : "<<opt.particleName<<std::endl;
01006   std::cout<<"energy : "<<opt.beamEnergy<<std::endl;
01007   std::cout<<"n particles : "<<opt.numberOfParticles<<std::endl;
01008   std::cout<<"n macroparticles : "<<opt.numberToGenerate<<std::endl;
01009   std::cout<<"sigmaX           : "<<opt.sigmaX<<std::endl;
01010   std::cout<<"interactions on           : "<<opt.turnOnInteractions<<std::endl;
01011 }
01012 
01013 
01014 void set_value(std::string name, double value )
01015 {
01016   //
01017   // numeric options for the "beam" command
01018   //
01019   if(name == "energy" ) options.beamEnergy = value;
01020   if(name == "nparticles" ) options.numberOfParticles = (int)value; //not used
01021   if(name == "X0" ) options.X0 = value;
01022   if(name == "Y0" ) options.Y0 = value;
01023   if(name == "Z0" ) options.Z0 = value;
01024   if(name == "T0" ) options.T0 = value;
01025   if(name == "Xp0" ) options.Xp0 = value;
01026   if(name == "Yp0" ) options.Yp0 = value;
01027   if(name == "Zp0" ) options.Zp0 = value;
01028 
01029   if(name == "sigmaT" ) options.sigmaT = value;
01030   if(name == "sigmaE" ) options.sigmaE = value;
01031 
01032   // options for beam distrType="gauss"
01033   if(name == "sigmaX" ) options.sigmaX = value;
01034   if(name == "sigmaY" ) options.sigmaY = value;
01035   if(name == "sigmaXp" ) options.sigmaXp = value;
01036   if(name == "sigmaYp" ) options.sigmaYp = value;
01037 
01038   // options for beam distrType="eshell"
01039   if(name == "shellX" ) options.shellX = value;
01040   if(name == "shellY" ) options.shellY = value;
01041   if(name == "shellXp" ) options.shellXp = value;
01042   if(name == "shellYp" ) options.shellYp = value;
01043 
01044   // options for beam distrType="ring"
01045   if(name == "Rmin" ) options.Rmin = value;
01046   if(name == "Rmax" ) options.Rmax = value;
01047 
01048   //
01049   // numeric options for the"option" command
01050   //
01051 
01052   // options which influence the geometry
01053   if(name == "boxSize" ) {options.componentBoxSize = value;}
01054   if(name == "tunnelRadius" ) options.tunnelRadius = value;
01055   if(name == "beampipeThickness" ) options.beampipeThickness = value;
01056   if(name == "beampipeRadius" ) options.beampipeRadius = value;
01057 
01058   // options which influence tracking 
01059   if(name == "deltaChord") options.deltaChord = value;
01060   if(name == "deltaIntersection") options.deltaIntersection = value;
01061   if(name == "chordStepMinimum") options.chordStepMinimum = value;
01062   if(name == "lengthSafety") options.lengthSafety = value;
01063   if(name == "minimumEpsilonStep" ) options.minimumEpsilonStep = value;
01064   if(name == "maximumEpsilonStep" ) options.maximumEpsilonStep = value;
01065   if(name == "deltaOneStep" ) options.deltaOneStep = value;
01066 
01067   // physics processes
01068   if(name == "turnInteractions") 
01069     {
01070       if(value == 0) options.turnOnInteractions = false;
01071       else options.turnOnInteractions = true;
01072     }
01073   if(name == "thresholdCutCharged" ) options.thresholdCutCharged = value;
01074   if(name == "thresholdCutPhotons" ) options.thresholdCutPhotons = value;
01075   if(name == "useEMHadronic" ) options.useEMHadronic = (int) value;
01076 
01077   if(name == "stopTracks") options.stopTracks = (int) value; 
01078 
01079   if(name == "synchRadOn") options.synchRadOn = (int) value;
01080   if(name == "srMeanFreeFactor") options.synchMeanFreeFactor = (int) value;
01081   if(name == "srRescale") options.synchRescale = (int) value;
01082   if(name == "srLowX") options.synchLowX = value;
01083   if(name == "srLowGamE") options.synchLowGamE = value;
01084   if(name == "srMultiplicity") options.synchPhotonMultiplicity = (int) value;
01085   if(name == "srTrackPhotons") options.synchTrackPhotons = (int) value;
01086 
01087   if(name == "prodCutPhotons" ) options.prodCutPhotons = value;
01088   if(name == "prodCutPhotonsP" ) options.prodCutPhotonsP = value;
01089   if(name == "prodCutElectrons" ) options.prodCutElectrons = value;
01090   if(name == "prodCutElectronsP" ) options.prodCutElectronsP = value;
01091   if(name == "prodCutPositrons" ) options.prodCutPositrons = value;
01092   if(name == "prodCutPositronsP" ) options.prodCutPositronsP = value;
01093 
01094   // twiss parameters
01095   if(name == "betx" ) options.betx = value;
01096   if(name == "bety" ) options.bety = value;
01097   if(name == "alfx" ) options.alfx = value;
01098   if(name == "alfy" ) options.alfy = value;
01099   if(name == "emitx" ) options.emitx = value;
01100   if(name == "emity" ) options.emity = value;
01101   if(name == "doTwiss" ) options.doTwiss = (int) value;
01102 
01103   if(name == "storeTrajectory") options.storeTrajectory = (int) value; 
01104   if(name == "storeMuonTrajectory") options.storeMuonTrajectories = (int) value; 
01105   if(name == "storeNeutronTrajectory") options.storeNeutronTrajectories = (int) value; 
01106 
01107 
01108   // options for generation and storage
01109   if(name == "randomSeed") options.randomSeed = (int) value;
01110   if(name == "ngenerate" ) options.numberToGenerate = (int)value;
01111   if(name == "nperfile" ) options.numberOfEventsPerNtuple = (int)value;
01112   if(name == "eventNumberOffset" ) options.eventNumberOffset = (int)value;
01113   if(name == "nlinesIgnore") options.nlinesIgnore = (int) value;
01114 }
01115 
01116 
01117 void set_value(std::string name, std::string value )
01118 {
01119   // 
01120   // string options for the "beam" command
01121   //
01122   if(name == "particle") options.particleName = value;
01123   if(name == "distrType" ) options.distribType = value;
01124   if(name == "distrFile" ) options.distribFile = value;  
01125 
01126 
01127   //
01128   // string options for the "option" command
01129   //
01130 
01131   // options which influence the geometry
01132   if(name == "beampipeMaterial" ) options.pipeMaterial = value;
01133   if(name == "vacMaterial" ) options.vacMaterial = value;
01134 
01135   // options which influence the tracking
01136   if(name == "physicsList" ) options.physicsList = value; 
01137 
01138   //?
01139   if(name == "fifo") options.fifo = value;
01140 }
01141 
01142 double property_lookup(char *element_name, char *property_name)
01143 {
01144    std::list<struct Element>::iterator it = element_lookup(element_name);
01145    std::list<struct Element>::const_iterator iterNULL = element_list.end();
01146 
01147    if(it == iterNULL) return 0;
01148    
01149    if(!strcmp(property_name,"l")) return (*it).l;
01150    if(!strcmp(property_name,"B")) return (*it).B;
01151    if(!strcmp(property_name,"ks")) return (*it).ks;
01152    if(!strcmp(property_name,"k0")) return (*it).k0;
01153    if(!strcmp(property_name,"k1")) return (*it).k1;
01154    if(!strcmp(property_name,"k2")) return (*it).k2;
01155    if(!strcmp(property_name,"k3")) return (*it).k3;
01156    if(!strcmp(property_name,"aper")) return (*it).aper;
01157    if(!strcmp(property_name,"outR")) return (*it).outR;
01158    if(!strcmp(property_name,"xsize")) return (*it).xsize;
01159    if(!strcmp(property_name,"ysize")) return (*it).ysize;
01160    if(!strcmp(property_name,"xdir")) return (*it).xdir;
01161    if(!strcmp(property_name,"ydir")) return (*it).ydir;
01162    if(!strcmp(property_name,"zdir")) return (*it).zdir;
01163    if(!strcmp(property_name,"phi")) return (*it).phi;
01164    if(!strcmp(property_name,"psi")) return (*it).psi;
01165    if(!strcmp(property_name,"theta")) return (*it).theta;
01166    if(!strcmp(property_name,"waveLength")) return (*it).waveLength;
01167    if(!strcmp(property_name,"tilt")) return (*it).tilt;
01168    if(!strcmp(property_name,"gradient")) return (*it).gradient;
01169    if(!strcmp(property_name,"hgap")) return (*it).hgap;
01170 
01171    if(!strcmp(property_name,"A")) return (*it).A;
01172    if(!strcmp(property_name,"Z")) return (*it).Z;
01173    if(!strcmp(property_name,"density")) return (*it).density;
01174    if(!strcmp(property_name,"T")) return (*it).temper;
01175    if(!strcmp(property_name,"P")) return (*it).pressure;
01176 
01177    return 0;
01178 
01179    //what about property_lookup for attributes of type string, like material?
01180 }
01181 
01182 // ******************************************************
01183 // parser functions
01184 // ******************************************************
01185 
01186 
01187 int add_func(char *name, double (*func)(double))
01188 {
01189   struct symtab *sp=symlook(name);
01190   sp->funcptr=func;
01191   return 0;
01192 }
01193 
01194 int add_var(char *name, double value, int is_reserved = 0)
01195 {
01196   struct symtab *sp=symlook(name);
01197   sp->value=value;
01198   sp->is_reserved = is_reserved;
01199   return 0;
01200 }
01201 
01202 #endif

Generated on Wed Mar 5 17:25:22 2008 for BDSIM by  doxygen 1.5.3