00001 #include "Translate/SADParser.hpp"
00002
00003 using namespace std;
00004 using namespace BasicUtilities;
00005
00006
00007
00008
00009 SADParser::SADParser() : TranslateCore() {
00010 language = "SAD";
00011 continuation_char = "@";
00012 c_style_format = true;
00013 knl_tn_style_multipoles = false;
00014 init_lists_sad();
00015 return;
00016 }
00017
00018
00019
00020
00021 void SADParser::init_lists_sad () {
00022
00023 x_class_to_aml_map["bend"] = "bend";
00024 x_class_to_aml_map["cavi"] = "rf_cavity";
00025 x_class_to_aml_map["quad"] = "quadrupole";
00026 x_class_to_aml_map["sext"] = "sextupole";
00027 x_class_to_aml_map["mult"] = "multipole";
00028 x_class_to_aml_map["mark"] = "marker";
00029 x_class_to_aml_map["drift"] = "";
00030 x_class_to_aml_map["oct"] = "octupole";
00031 x_class_to_aml_map["sol"] = "solenoid";
00032 x_class_to_aml_map["kicker"] = "kicker";
00033
00034
00035
00036 register_attribute("l", "length");
00037
00038
00039
00040
00041 register_attribute("bend:g", "bend:g");
00042
00043 register_attribute("bend:e1", "bend:e1");
00044 register_attribute("bend:e2", "bend:e2");
00045 register_attribute("bend:h_gap1", "bend:h_gap1");
00046 register_attribute("bend:h_gap2", "bend:h_gap2");
00047 register_attribute("bend:f_int1", "bend:f_int1");
00048 register_attribute("bend:f_int2", "bend:f_int2");
00049 register_attribute("bend:h1", "bend:f_h1");
00050 register_attribute("bend:h2", "bend:f_h2");
00051 register_attribute("bend:scaled_multipole", "bend:scaled_multipole");
00052 register_attribute("bend:k1", "bend:scaled_multipole:b_coef(n=1)");
00053 register_attribute("bend:k2", "bend:scaled_multipole:b_coef(n=2)");
00054
00055 register_attribute("sextupole:scaled_multipole:a_coef",
00056 "sextupole:scaled_multipole:a_coef");
00057 register_attribute("sextupole:scaled_multipole:b_coef",
00058 "sextupole:scaled_multipole:b_coef");
00059
00060
00061
00062
00063 register_attribute("rotate", "orientation:tilt");
00064 register_attribute("dx", "orientation:x_offset");
00065 register_attribute("dy", "orientation:y_offset");
00066 register_attribute("rotate", "kicker:orientation:tilt");
00067 register_attribute("dx", "kicker:orientation:x_offset");
00068 register_attribute("dy", "kicker:orientation:y_offset");
00069
00070
00071 register_attribute("cavi:phi", "rf_cavity:phase0", "# * twopi", "# / twopi");
00072 register_attribute("cavi:freq", "rf_cavity:rf_freq", "# * 1e9", "# / 1e9");
00073 register_attribute("cavi:harm", "rf_cavity:harmonic");
00074 register_attribute("cavi:volt", "rf_cavity:gradient", "# * 1e6 / ![length]",
00075 "# * ![l] / 1e6");
00076
00077
00078
00079 register_attribute("k1", "quadrupole:k");
00080 register_attribute("k2", "sextupole:k");
00081 register_attribute("k3", "octupole:k");
00082
00083
00084
00085
00086 register_attribute("ks", "solenoid:ks");
00087
00088
00089
00090 register_attribute("apert", "aperture");
00091 register_attribute("dx1", "aperture:x_limit");
00092 register_attribute("dy1", "aperture:y_limit");
00093
00094 register_attribute("controller", "controller");
00095 register_attribute("controller:design", "controller:design");
00096
00097
00098
00099
00100
00101 register_attribute("kicker:hkick", "kicker:x_kick");
00102 register_attribute("kicker:vkick", "kicker:y_kick");
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 register_attribute("momentum", "beam[total_energy]");
00118 register_attribute("mass", "beam[mass]", "# * 1e9");
00119 register_attribute("gamma", "beam[gamma]");
00120 register_attribute("emitx", "beam[emittance_a]");
00121 register_attribute("emity", "beam[emittance_b]");
00122 register_attribute("sigz", "beam[sig_t]");
00123 register_attribute("sige", "beam[sig_e]");
00124
00125 return;
00126
00127 }
00128
00129
00130
00131
00132 bool SADParser::aml_ele_to_x_class (UAPNode* aml_ele, string& x_class) {
00133
00134
00135
00136
00137
00138
00139 StrList names;
00140 NodeVec children = aml_ele->getChildren();
00141 for (NodeVecCIter it = children.begin(); it != children.end(); it++) {
00142 string name = (*it)->getName();
00143 if (found(aml_class_names, name)) names << name;
00144 }
00145
00146 UAPNode* instrument = aml_ele->getChildByName("instrument");
00147
00148
00149
00150 if (names.size() == 0) {
00151 x_class = "drift";
00152 if (instrument) {
00153 x_class = "moni";
00154 string i_type = instrument->getAttributeString("type");
00155 if (i_type == "hmonitor" || i_type == "vmonitor" || i_type == "monitor")
00156 x_class = "moni";
00157
00158 }
00159 return true;
00160 }
00161
00162
00163
00164
00165 if (names.size() > 1) names.remove("aperture");
00166
00167
00168
00169 if (names.size() == 1) {
00170
00171 x_class = names.front();
00172
00173 if (x_class == "quadrupole") x_class = "quad";
00174
00175 if (x_class == "bend") x_class = "bend";
00176
00177 if (x_class == "rf_cavity") x_class = "cavi";
00178
00179 if (x_class == "sextupole") x_class = "sext";
00180
00181 if (x_class == "multipole") x_class = "mult";
00182
00183 if (x_class == "marker") x_class = "mark";
00184
00185 if (x_class == "solenoid") x_class = "sol";
00186
00187 if (x_class == "octupole") x_class = "oct";
00188
00189 if (x_class == "kicker") x_class = "kicker";
00190
00191 if (x_class == "controller") x_class = "controller";
00192
00193 if (x_class == "instrument") x_class = "moni";
00194
00195
00196
00197
00198
00199 if (x_class == "patch" || x_class == "wiggler" || x_class == "match") {
00200 x_class = "BAD!";
00201 info_out.error ("AML element does not have a corresponding " + language + " type",
00202 aml_ele->toStringTree());
00203 return false;
00204 }
00205 }
00206
00207 return true;
00208
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 void SADParser::custom_aml_rep_to_x (UAPNode* lab, UAPNode* x_rep) {
00220
00221 bool ok;
00222 double kvalue, lvalue, K0value ;
00223 double e1value, e2value, anglevalue ;
00224 double rotatevalue ;
00225 double dx1value,dy1value,dx2value,dy2value, powersupplyvalue;
00226
00227 StrList names;
00228 NodeVec children = x_rep->getChildren();
00229
00230
00231 for (NodeVecCIter it = children.begin(); it != children.end(); it++) {
00232 string name = (*it)->getName();
00233
00234
00235 powersupplyvalue =0;
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 if (name == "element") {
00248
00249 UAPNode* slave = *it;
00250 string attrib = slave->getAttributeString("class");
00251
00252
00253
00254
00255
00256 if (attrib == "quad") {
00257 string k1 = slave->getAttributeString("k1");
00258 string l = slave->getAttributeString("l");
00259
00260 lvalue = BasicUtilities::string_to_double(l, ok);
00261 kvalue = BasicUtilities::string_to_double(k1, ok);
00262 kvalue = kvalue * lvalue ;
00263 k1 = double_to_string (kvalue, ok);
00264 slave->addAttribute("k1", k1);
00265 };
00266
00267 if (attrib == "sext") {
00268 string k2 = slave->getAttributeString("k2");
00269 string l = slave->getAttributeString("l");
00270
00271 lvalue = BasicUtilities::string_to_double(l, ok);
00272 kvalue = BasicUtilities::string_to_double(k2, ok);
00273 kvalue = kvalue * lvalue ;
00274 k2 = double_to_string (kvalue, ok);
00275 slave->addAttribute("k2", k2);
00276 };
00277
00278 if (attrib == "oct") {
00279 string k3 = slave->getAttributeString("k3");
00280 string l = slave->getAttributeString("l");
00281
00282 lvalue = BasicUtilities::string_to_double(l, ok);
00283 kvalue = BasicUtilities::string_to_double(k3, ok);
00284 kvalue = kvalue * lvalue ;
00285 k3 = double_to_string (kvalue, ok);
00286 slave->addAttribute("k3", k3);
00287 };
00288
00289
00290
00291
00292 if (attrib == "bend") {
00293 string l = slave->getAttributeString("l");
00294 lvalue = BasicUtilities::string_to_double(l, ok);
00295 string g = slave->getAttributeString("g");
00296 double gvalue = BasicUtilities::string_to_double(g, ok);
00297 anglevalue = lvalue * gvalue;
00298 string angle = double_to_string (anglevalue, ok);
00299 slave->addAttribute("angle", angle);
00300 slave->removeAttribute("g");
00301
00302 if(slave->getAttribute("e1")) {
00303 string e1 = slave->getAttributeString("e1");
00304 e1value = BasicUtilities::string_to_double(e1, ok);
00305 e1value = e1value / anglevalue ;
00306 if (e1value == 0) {
00307 slave->removeAttribute("e1"); }
00308 else {
00309 e1 = double_to_string (e1value, ok);
00310 slave->addAttribute("e1", e1);
00311 };
00312 };
00313
00314 if(slave->getAttribute("e2")) {
00315 string e2 = slave->getAttributeString("e2");
00316 e2value = BasicUtilities::string_to_double(e2, ok);
00317 e2value = e2value / anglevalue ;
00318 if (e2value == 0) {
00319 slave->removeAttribute("e2"); }
00320 else {
00321 e2 = double_to_string (e2value, ok);
00322 slave->addAttribute("e2", e2);
00323 };
00324 };
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 };
00351
00352
00353
00354
00355
00356 if(slave->getAttribute("rotate")) {
00357
00358 string rotate = slave->getAttributeString("rotate");
00359 if (rotate == "") {
00360 if (attrib == "bend") rotate = "pi/2";
00361 if (attrib == "quad") rotate = "pi/4";
00362 if (attrib == "sext") rotate = "pi/6";
00363 if (attrib == "oct") rotate = "pi/8";
00364 if (attrib == "sol_quad") rotate = "pi/4";
00365 };
00366 rotatevalue = BasicUtilities::string_to_double(rotate, ok);
00367 if (rotatevalue == 0) {
00368 slave->removeAttribute("rotate"); }
00369 else {
00370 rotatevalue = rotatevalue / 3.14159265 * 180.0;
00371 rotate = double_to_string(rotatevalue, ok);
00372 rotate = rotate + " DEG";
00373 slave->addAttribute("rotate", rotate);
00374 };
00375 };
00376
00377
00378
00379
00380 if (slave->getAttribute("apert")) {
00381
00382 if (slave->getAttribute("dx1")) {
00383 string dx1 = slave->getAttributeString("dx1");
00384 dx1value = BasicUtilities::string_to_double(dx1, ok);
00385 if (dx1value > 0) {
00386 dx1value = - dx1value;
00387 dx1 = double_to_string(dx1value, ok);
00388 slave->addAttribute("dx1", dx1);
00389 };
00390 dx2value = fabs(dx1value);
00391 string dx2 = double_to_string(dx2value, ok);
00392 slave->addAttribute("dx2", dx2);
00393 };
00394
00395 if (slave->getAttribute("dy1")) {
00396 string dy1 = slave->getAttributeString("dy1");
00397 dy1value = BasicUtilities::string_to_double(dy1, ok);
00398 if (dy1value > 0) {
00399 dy1value = - dy1value;
00400 dy1 = double_to_string(dy1value, ok);
00401 slave->addAttribute("dy1", dy1);
00402 };
00403 dy2value = fabs(dy1value);
00404 string dy2 = double_to_string(dy2value, ok);
00405 slave->addAttribute("dy2", dy2);
00406 };
00407
00408 };
00409
00410
00411
00412
00413
00414
00415 if (attrib == "kicker") {
00416
00417 if(slave->getAttribute("rotate")) {
00418 string rotate = slave->getAttributeString("rotate");
00419 rotatevalue = BasicUtilities::string_to_double(rotate, ok);
00420
00421
00422
00423
00424 string drotate = double_to_string(rotatevalue, ok);
00425 drotate = drotate + " DEG";
00426 slave->addAttribute("drotate", drotate);
00427 slave->removeAttribute("rotate");
00428 };
00429
00430 if(slave->getAttribute("hkick")) {
00431 string K0 = slave->getAttributeString("hkick");
00432
00433
00434 K0value = BasicUtilities::string_to_double(K0, ok);
00435 K0value = - powersupplyvalue * K0value ;
00436 K0 = double_to_string(K0value, ok);
00437 slave->addAttribute("K0", K0);
00438 slave->removeAttribute("hkick");
00439 };
00440
00441 if(slave->getAttribute("vkick")) {
00442 rotatevalue = 90;
00443 string rotate = double_to_string(rotatevalue, ok);
00444 rotate = rotate + " DEG";
00445 slave->addAttribute("rotate", rotate);
00446 string K0 = slave->getAttributeString("vkick");
00447
00448
00449 K0value = BasicUtilities::string_to_double(K0, ok);
00450 K0value = - powersupplyvalue * K0value ;
00451 K0 = double_to_string(K0value, ok);
00452 slave->addAttribute("K0", K0);
00453 slave->removeAttribute("vkick");
00454 };
00455 };
00456
00457
00458 if (attrib == "kicker") attrib="bend" ;
00459 slave->addAttribute("class", attrib);
00460
00461
00462
00463
00464
00465 if (attrib == "cavi") {
00466
00467 if(slave->getAttribute("volt")) {
00468 string volt = slave->getAttributeString("volt");
00469 double voltvalue = 1e6 * BasicUtilities::string_to_double(volt, ok);
00470 volt = double_to_string (voltvalue, ok);
00471 slave->addAttribute("volt", volt);
00472 };
00473
00474 if(slave->getAttribute("phi")) {
00475 string phi = slave->getAttributeString("phi");
00476 double phivalue = BasicUtilities::string_to_double(phi, ok);
00477 phi = double_to_string (phivalue, ok);
00478 slave->addAttribute("phi", phi);
00479 };
00480
00481 if(slave->getAttribute("freq")) {
00482 string freq = slave->getAttributeString("freq");
00483 double freqvalue = BasicUtilities::string_to_double(freq, ok);
00484 freq = double_to_string (freqvalue, ok);
00485 slave->addAttribute("freq", freq);
00486 };
00487
00488 };
00489
00490
00491
00492
00493 if(slave->getAttribute("dx")) {
00494 string dx = slave->getAttributeString("dx");
00495 double dxvalue = BasicUtilities::string_to_double(dx, ok);
00496 if (dxvalue == 0) {
00497 slave->removeAttribute("dx"); }
00498 else {
00499 dx = double_to_string (dxvalue, ok);
00500 slave->addAttribute("dx", dx);
00501 };
00502 };
00503
00504 if(slave->getAttribute("dy")) {
00505 string dy = slave->getAttributeString("dy");
00506 double dyvalue = BasicUtilities::string_to_double(dy, ok);
00507 if (dyvalue == 0) {
00508 slave->removeAttribute("dy"); }
00509 else {
00510 dy = double_to_string (dyvalue, ok);
00511 slave->addAttribute("dy", dy);
00512 };
00513 };
00514
00515 if(slave->getAttribute("s_offset")) {
00516 string soffset = slave->getAttributeString("s_offset");
00517 double soffsetvalue = BasicUtilities::string_to_double(soffset, ok);
00518 soffset = double_to_string (soffsetvalue, ok);
00519 slave->addAttribute("soffset", soffset);
00520 };
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544 }
00545
00546 }
00547 return;
00548
00549 }
00550
00551
00552
00553
00554 bool SADParser::custom_x_element_to_x_file (UAPNode* x_node, string& comment,
00555 StreamStruct& x_out) {
00556
00557 string x_name = x_node->getName();
00558 AttributeVec x_attribs = x_node->getAttributes();
00559
00560
00561
00562 if (x_name == "element") {
00563 string ele_class = x_node->getAttributeString("class");
00564 if (ele_class == "") {
00565 ele_class = x_node->getAttributeString("inherit");
00566 info_out.error (language + ": element inheritance translation from AML not yet implemented!");
00567 throw;
00568 }
00569 x_out << ele_class << " " << x_node->getAttributeString("name") << " = (";
00570 for (AttributeVecCIter att = x_attribs.begin(); att != x_attribs.end(); att++) {
00571 string a_name = att->getName();
00572 string a_value = att->getValue();
00573 if (a_name == "name") continue;
00574 if (a_name == "comment") continue;
00575 if (a_name == "class") continue;
00576 if (a_name == "inherit") continue;
00577
00578 x_out << a_name;
00579 x_add_quote_marks (a_name, a_value);
00580 if (a_value != "") x_out << " = " << a_value << " ";
00581 }
00582
00583 x_out << ")" << comment << fini;
00584 return true;
00585 }
00586
00587
00588
00589 if (x_name == "line") {
00590 x_out << "line " << x_node->getAttributeString("name") << " = ";
00591 write_line_or_list_to_x_file (x_node, " ", x_out);
00592 x_out << comment << fini;
00593 return true;
00594 }
00595
00596
00597 return false;
00598
00599 }
00600
00601
00602
00603
00604
00605
00606
00608
00609
00610
00611
00612
00613
00614
00615 bool SADParser::custom_aml_node_to_x (UAPNode* aml_ele, UAPNode* x_rep) {
00616
00617
00618
00619 a_vec.resize(21, "");
00620 b_vec.resize(21, "");
00621
00622
00623
00624 return false;
00625
00626 }
00627
00628
00629
00630
00631 bool SADParser::custom_aml_element_attribute_to_x(UAPNode* aml_root, UAPNode* aml_ele,
00632 UAPNode* aml_attrib, UAPNode* x_ele) {
00633
00634 string aml_parent_name = aml_ele->getName();
00635 string ele_name = x_ele->getAttributeString("name");
00636 string aml_attrib_name = aml_attrib->getName();
00637 string aml_attrib_value = aml_attrib->getAttributeString("design");
00638
00639
00640
00641 if (aml_parent_name == "multipole" && (aml_attrib_name == "al" || aml_attrib_name == "bl")) {
00642
00643 string n_str = aml_attrib->getAttributeString("n");
00644 bool ok;
00645 int n = string_to_int(n_str, ok);
00646 if (!ok) {
00647 info_out.error ("NO OR BAD \"n\" ATTRIBUTE FOUND.", aml_attrib->toString());
00648 return true;
00649 }
00650
00651 if (aml_attrib_name == "al") {
00652 a_vec[n] = aml_attrib_value;
00653 } else {
00654 b_vec[n] = aml_attrib_value;
00655 }
00656
00657 return true;
00658
00659 }
00660
00661
00662
00663 return false;
00664
00665 }
00666
00667
00668
00669
00670
00671 void SADParser::custom2_aml_element_attribute_to_x (UAPNode* aml_ele, UAPNode* x_rep) {
00672
00673 string aml_name = aml_ele->getName();
00674 bool ok;
00675
00676
00677
00678 if (x_rep->getAttributeString("class") == "multipole") {
00679
00680 double f = 1;
00681
00682 for (int nn = 1; nn < a_vec.size(); nn++) {
00683 if (nn != 1) f = f * (nn - 1);
00684 if (a_vec[nn] == "" && b_vec[nn] == "") continue;
00685 string nn_str = int_to_string (nn, ok);
00686 string n0_str = int_to_string (nn-1, ok);
00687 string f_str = double_to_string (f, ok);
00688 string a_str = add_parens(a_vec[nn], 1);
00689 string b_str = add_parens(b_vec[nn], 1);
00690
00691 if (a_str == "") {
00692 x_rep->addAttribute("k" + n0_str + "l", f_str + " * " + b_str);
00693
00694 } else if (b_str == "") {
00695 x_rep->addAttribute("k" + n0_str + "l", f_str + " * " + a_str);
00696 x_rep->addAttribute("t" + n0_str, "");
00697
00698 } else {
00699 x_rep->addAttribute("k" + n0_str + "l",
00700 f_str + " * sqrt((" + a_vec[nn] + ")^2 + (" + b_vec[nn] + ")^2)");
00701 x_rep->addAttribute("t" + n0_str,
00702 "-atan(" + a_vec[nn] + ", " + b_vec[nn] + ")");
00703 }
00704
00705 }
00706
00707 }
00708
00709
00710
00711 return;
00712
00713 }