1 | |
---|
2 | #include "softwareMadx.h" |
---|
3 | #include "dataManager.h" |
---|
4 | |
---|
5 | softwareMadx::softwareMadx() : abstractSoftware() |
---|
6 | { |
---|
7 | nameOfSoftware_ = nomDeLogiciel("madx"); |
---|
8 | } |
---|
9 | |
---|
10 | softwareMadx::softwareMadx(string inputFileName,sectorParameters* lattice,dataManager* dt) : abstractSoftware(inputFileName,lattice,dt) |
---|
11 | { |
---|
12 | nameOfSoftware_ = nomDeLogiciel("madx"); |
---|
13 | |
---|
14 | registerElement(nomdElements::drift,TBoolOk); |
---|
15 | registerElement(nomdElements::mpole,TBoolOk); |
---|
16 | } |
---|
17 | |
---|
18 | bool softwareMadx::createInputFile(particleBeam* beamBefore,unsigned int numeroDeb,unsigned int numeroFin,string workingDir) |
---|
19 | { |
---|
20 | cout << "***********************************" << endl; |
---|
21 | cout << " softwareMadx::createInputFile(...) " << endl << endl; |
---|
22 | |
---|
23 | dataManager_->consoleMessage(" softwareMadx::createInputFile"); |
---|
24 | if (!initComputationLimits(numeroDeb,numeroFin)) return false; |
---|
25 | |
---|
26 | string name = workingDir + inputFileName_; |
---|
27 | ofstream outfile; |
---|
28 | outfile.open(name.c_str(),ios::out); |
---|
29 | if(!outfile) { |
---|
30 | dataManager_->consoleMessage(" softwareMadx::createInputFile : error opening output stream "); |
---|
31 | cerr << " error opening output stream " << name << endl; |
---|
32 | return false; |
---|
33 | } |
---|
34 | |
---|
35 | outfile << endl; // saut de ligne |
---|
36 | ostringstream os; |
---|
37 | os << "L: " << "line=("; |
---|
38 | |
---|
39 | abstractElement* elPtr; |
---|
40 | for(unsigned k = numeroDeb_; k <= numeroFin_; k++) |
---|
41 | { |
---|
42 | elPtr = dataManager_->getElementPointerFromNumero(k); |
---|
43 | cout << "debug:: element [" << k << "] " << elPtr->getNomdElement().getExpandedName() << endl; |
---|
44 | vector<statements> v= elPtr->parametersToSoftware(); |
---|
45 | string label= v.at(0).second.at(1); |
---|
46 | outfile << inputFormat(v); //<< endl; |
---|
47 | os << label <<","; |
---|
48 | }//k; |
---|
49 | |
---|
50 | os << ");" << endl; |
---|
51 | int nCells= sectParamPtr_->getNumberOfCells(); |
---|
52 | os << "all: line=(" << nCells << "*L);" << endl; |
---|
53 | outfile << endl; // saut de ligne |
---|
54 | outfile << os.str() << endl; // beam line definition p34 |
---|
55 | |
---|
56 | outfile << "beam;" << endl; // beam data p46 |
---|
57 | outfile << "use, period = all;" << endl << endl; // action commands p45 |
---|
58 | |
---|
59 | outfile << "select,flag = twiss,column = name,s,betx,bety;" << endl; //p48 |
---|
60 | outfile << "twiss,save,centre,file = twiss.out;" << endl; //p51 |
---|
61 | outfile << "stop;" << endl; |
---|
62 | |
---|
63 | outfile.close(); |
---|
64 | dataManager_->consoleMessage("input file done for madx"); |
---|
65 | return true; |
---|
66 | } |
---|
67 | |
---|
68 | string softwareMadx::inputFormat(const vector<statements>& v) const |
---|
69 | { |
---|
70 | // v.at(0).first = "labelsGenericSpecific" |
---|
71 | // v.at(0).second = vector<string> de 2 elements (type de l'element,label) |
---|
72 | string keyword= v.at(0).second.at(0); |
---|
73 | string label= v.at(0).second.at(1); |
---|
74 | ostringstream os; |
---|
75 | if(keyword == "drift") { |
---|
76 | double length = atof(v.at(1).second.at(0).c_str()); |
---|
77 | os << label << ":" << " drift, l=" << 0.01*length<< ";" << endl; |
---|
78 | |
---|
79 | } else if(keyword == "mpole") { |
---|
80 | double x[9]= {0.0}; // 0 <= n <= 9 in user's manual of MAD program |
---|
81 | int n= atoi(v.at(1).second.at(0).c_str()); |
---|
82 | x[n]= atof(v.at(1).second.at(1).c_str()); |
---|
83 | os << label << ":" << " multipole, knl={" << x[0]; |
---|
84 | for(int i = 1; i <= n; i++) { |
---|
85 | os << "," << x[i]; |
---|
86 | } |
---|
87 | os <<"};" << endl; |
---|
88 | |
---|
89 | } else { |
---|
90 | cout << "softwareMadx::inputFormat ERROR : element type= " << keyword << " not defined" << endl; |
---|
91 | } |
---|
92 | |
---|
93 | return os.str(); |
---|
94 | } |
---|
95 | |
---|
96 | bool softwareMadx::execute(string workingDir) |
---|
97 | { |
---|
98 | cout << "***********************************" << endl; |
---|
99 | cout << " softwareMadx::execute(...) " << endl << endl; |
---|
100 | |
---|
101 | dataManager_->consoleMessage(" softwareMadx::execute"); |
---|
102 | bool status= true; |
---|
103 | |
---|
104 | ostringstream sortie; |
---|
105 | sortie << " run madx from " << numeroDeb_ << " to " << numeroFin_ << endl; |
---|
106 | |
---|
107 | string mjob = workingDir + "madx64"; |
---|
108 | mjob += string(" < "); |
---|
109 | mjob += workingDir + inputFileName_; |
---|
110 | cout << " job madx = " << mjob << endl; |
---|
111 | |
---|
112 | string resultOfRun; |
---|
113 | bool success = launchJob(mjob,resultOfRun); |
---|
114 | // xx sortie << resultOfRun << endl; |
---|
115 | if ( !success ) { |
---|
116 | //sortie << " launching of madx failed " << endl; |
---|
117 | status = false; |
---|
118 | } else { |
---|
119 | sortie << " madx finished normally" << endl; |
---|
120 | string nameOut = workingDir + "madx.output"; |
---|
121 | ofstream outfile; |
---|
122 | outfile.open(nameOut.c_str(),ios::out); |
---|
123 | if (!outfile) { |
---|
124 | sortie << " error in opening madx output stream " << nameOut << endl; |
---|
125 | status = false; |
---|
126 | } else { |
---|
127 | // on copie la sortie dans un fichier 'madx.out' |
---|
128 | outfile << resultOfRun << endl; |
---|
129 | outfile.close(); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | dataManager_->consoleMessage(sortie.str()); |
---|
134 | return status; |
---|
135 | } |
---|
136 | |
---|
137 | bool softwareMadx::buildBeamAfterElements(string workingDir) |
---|
138 | { |
---|
139 | cout << "***********************************" << endl; |
---|
140 | cout << " softwareMadx::buildBeamAfterElements(...) " << endl << endl; |
---|
141 | |
---|
142 | dataManager_->consoleMessage(" softwareMadx::buildBeamAfterElements"); |
---|
143 | return false; |
---|
144 | } |
---|