1 | #include "softwareParmela.h" |
---|
2 | #include "abstractElement.h" |
---|
3 | #include "parmelaParticle.h" |
---|
4 | #include "mathematicalConstants.h" |
---|
5 | #include "PhysicalConstants.h" |
---|
6 | |
---|
7 | |
---|
8 | softwareParmela::softwareParmela() : abstractSoftware() |
---|
9 | { |
---|
10 | ; |
---|
11 | } |
---|
12 | |
---|
13 | softwareParmela::softwareParmela(globalParameters* globals, elementsCollection* beamLine) : abstractSoftware(globals, beamLine) |
---|
14 | { |
---|
15 | ; |
---|
16 | } |
---|
17 | |
---|
18 | bool softwareParmela::createInputFile(particleBeam* beamBefore, unsigned int numeroDeb, unsigned int numeroFin, string workingDir) |
---|
19 | { |
---|
20 | unsigned int k; |
---|
21 | |
---|
22 | if ( numeroDeb < 1 || numeroFin > beamLinePtr_->size() ) { |
---|
23 | cerr << " index of element out of limits " << endl; |
---|
24 | return false; |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | unsigned indexDeb = numeroDeb - 1; |
---|
29 | unsigned indexFin = numeroFin - 1; |
---|
30 | |
---|
31 | ofstream outfile; |
---|
32 | string name = workingDir + "parmin"; |
---|
33 | outfile.open(name.c_str(), ios::out); |
---|
34 | if (!outfile) { |
---|
35 | cerr << " error opening output stream " << name << endl; |
---|
36 | return false; |
---|
37 | } |
---|
38 | |
---|
39 | abstractElement* elPtr; |
---|
40 | double initalKineticEnergy = 0.0; |
---|
41 | elPtr = beamLinePtr_->getElementPointerFromIndex(indexDeb); |
---|
42 | if ( elPtr->getNomdElement().getElementType() != RFgun ) { |
---|
43 | cerr << " dataManager::createInputFileParmela : the first element should be rfgun" << endl; |
---|
44 | return false; |
---|
45 | } |
---|
46 | else { |
---|
47 | elPtr->setPhaseStep( globParamPtr_->getIntegrationStep() ); |
---|
48 | initalKineticEnergy = elPtr->getInitialKineticEnergy(); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | outfile << "TITLE" << endl; |
---|
53 | outfile << " titre provisoire " << endl; |
---|
54 | outfile << "RUN /n0=1 /ip=999 /freq=" << globParamPtr_->getFrequency() << " /z0=0.0 /W0=" << initalKineticEnergy << " /itype=1" << endl; |
---|
55 | outfile << "OUTPUT 0" << endl; |
---|
56 | |
---|
57 | for ( k = indexDeb; k <= indexFin; k++) |
---|
58 | { |
---|
59 | elPtr = beamLinePtr_->getElementPointerFromIndex(k); |
---|
60 | outfile << elPtr->parmelaOutputFlow() << endl; |
---|
61 | } |
---|
62 | |
---|
63 | outfile << "ZOUT" << endl; |
---|
64 | outfile << "START /wt=0.0 /dwt=" << globParamPtr_->getIntegrationStep() << " /nsteps=" << globParamPtr_->getNbSteps() << " nsc=" << globParamPtr_->getScPeriod() << " /nout=10" << endl; |
---|
65 | outfile << "END" << endl; |
---|
66 | outfile.close(); |
---|
67 | return true; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | bool softwareParmela::execute(vector<particleBeam>& beamAfterElement, unsigned int numeroDeb,unsigned int numeroFin,string workingDir,string& resul) |
---|
72 | { |
---|
73 | ostringstream sortie; |
---|
74 | bool ExecuteStatus = true; |
---|
75 | resul.clear(); |
---|
76 | // if ( !createInputFile(NULL,numeroDeb,numeroFin, workingDir) ) |
---|
77 | // { |
---|
78 | // sortie << " error creating parmela input file " << endl; |
---|
79 | // resul = sortie.str(); |
---|
80 | // return false; |
---|
81 | // } |
---|
82 | |
---|
83 | sortie << " EXECUTION DE PARMELA DE l'ELEMENT " << numeroDeb << " A L'ELEMENT " << numeroFin << endl; |
---|
84 | |
---|
85 | char buf[132]; |
---|
86 | string parmelaJob = workingDir + "parmela"; |
---|
87 | parmelaJob += string(" "); |
---|
88 | parmelaJob += workingDir; |
---|
89 | // cout << " job parmela= " << parmelaJob << endl; |
---|
90 | |
---|
91 | string resultOfRun; |
---|
92 | bool success = launchJob(parmelaJob, resultOfRun); |
---|
93 | sortie << resultOfRun << endl; |
---|
94 | if ( !success) { |
---|
95 | sortie << " launching of parmela failed " << endl; |
---|
96 | ExecuteStatus = false; |
---|
97 | } |
---|
98 | else { |
---|
99 | cout << " execution parmela MARCHE " << endl; |
---|
100 | sortie << resultOfRun; |
---|
101 | string::size_type nn = (resultOfRun).find("normal"); |
---|
102 | if ( nn == string::npos ) |
---|
103 | { |
---|
104 | sortie << " abnormal exit of parmela " << endl; |
---|
105 | ExecuteStatus = false; |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | ExecuteStatus = buildBeamAfterElements(numeroDeb,numeroFin, beamAfterElement, workingDir); |
---|
110 | if ( !ExecuteStatus ) { |
---|
111 | sortie << " reading parmdesz failed " << endl; |
---|
112 | } |
---|
113 | |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | resul = sortie.str(); |
---|
118 | return ExecuteStatus; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | bool softwareParmela::buildBeamAfterElements(unsigned int numeroDeb,unsigned int numeroFin, vector<particleBeam>& beams, string workingDir) { |
---|
123 | bool result = true; |
---|
124 | unsigned k; |
---|
125 | for ( k= numeroDeb; k <= numeroFin; k++) |
---|
126 | { |
---|
127 | beams.push_back(particleBeam()); |
---|
128 | vector<double> centroid; |
---|
129 | bareParticle refPart; |
---|
130 | vector<bareParticle> particles; |
---|
131 | if (!beamFromParmela(workingDir,k, globParamPtr_->getFrequency(), centroid, refPart,particles )) |
---|
132 | { |
---|
133 | abstractElement* elem = beamLinePtr_->getElementPointerFromNumero(k); |
---|
134 | if ( elem->is_accepted_by_software(nomDeLogiciel::parmela) == warning) { |
---|
135 | int avantDernier = beams.size() -2; |
---|
136 | beams.back() = beams.at(avantDernier); |
---|
137 | } else { |
---|
138 | // sortie << " reading parmdesz failed " << endl; |
---|
139 | result = false; |
---|
140 | break; |
---|
141 | } |
---|
142 | } |
---|
143 | beams.back().setWithParticles(centroid, refPart,particles); |
---|
144 | } |
---|
145 | return result; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | bool softwareParmela::beamFromParmela(string workingDir,unsigned numeroElement, double referencefrequency, vector<double>& centroid, bareParticle& refPart,vector<bareParticle>& particles ) { |
---|
152 | unsigned k; |
---|
153 | FILE* filefais; |
---|
154 | string nomfilefais = workingDir + "parmdesz"; |
---|
155 | cout << " nom fichier desz : " << nomfilefais << endl; |
---|
156 | filefais = fopen(nomfilefais.c_str(), "r"); |
---|
157 | |
---|
158 | if ( filefais == (FILE*)0 ) { |
---|
159 | cerr << " particleBeam::setFromParmela() erreur a l'ouverture du fichier" << nomfilefais << endl;; |
---|
160 | return false; |
---|
161 | } |
---|
162 | else cout << " particleBeam::setFromParmela() : ouverture du fichier " << nomfilefais << endl; |
---|
163 | |
---|
164 | parmelaParticle partic; |
---|
165 | std::vector<parmelaParticle> faisceau; |
---|
166 | |
---|
167 | cout << " particleBeam::setFromParmela : numeroElement = " << numeroElement << endl; |
---|
168 | unsigned indexElement = numeroElement-1; |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | int testNombrePartRef =0; |
---|
174 | double phaseRef; |
---|
175 | |
---|
176 | while( partic.readFromParmelaFile(filefais) > 0 ) { |
---|
177 | if ( partic.ne == (int)indexElement ) |
---|
178 | { |
---|
179 | faisceau.push_back(partic); |
---|
180 | |
---|
181 | if ( partic.np == 1 ) { |
---|
182 | // en principe on est sur la particule de reference |
---|
183 | if ( fabs(partic.xx) > EPSILON || fabs(partic.yy) > EPSILON || fabs(partic.xxp) > EPSILON || fabs(partic.yyp) > EPSILON) { |
---|
184 | printf(" ATTENTION part. reference douteuse \n"); |
---|
185 | partic.imprim(); |
---|
186 | } |
---|
187 | phaseRef = partic.phi; |
---|
188 | TRIDVECTOR posRef(partic.xx,partic.yy,0.0); |
---|
189 | TRIDVECTOR betagammaRef(partic.xxp*partic.begamz, partic.yyp*partic.begamz, partic.begamz); |
---|
190 | refPart = bareParticle(posRef, betagammaRef); |
---|
191 | testNombrePartRef++; |
---|
192 | if ( testNombrePartRef != 1 ) { |
---|
193 | cerr << " TROP DE PART. DE REF : " << testNombrePartRef << " !! " << endl; |
---|
194 | return false; |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | if ( faisceau.size() == 0) |
---|
201 | { |
---|
202 | cerr << " particleBeam::setFromParmela echec lecture element " << numeroElement << endl; |
---|
203 | return false; |
---|
204 | } |
---|
205 | |
---|
206 | // facteur c/ 360. pour calculer (c dphi) / (360.freq) |
---|
207 | // avec freq en Mhz et dphi en degres et résultat en cm: |
---|
208 | double FACTEUR = 83.3333; // ameliorer la precision |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | // pour l'instant on choisit un centroid nul; |
---|
213 | centroid.clear(); |
---|
214 | centroid = vector<double>(6,0.0); |
---|
215 | |
---|
216 | particles.clear(); |
---|
217 | particles.resize(faisceau.size(), bareParticle()); |
---|
218 | double x,xp,y,yp; |
---|
219 | double betagammaz; |
---|
220 | double betaz; |
---|
221 | double deltaz; |
---|
222 | double dephas; |
---|
223 | double g; |
---|
224 | TRIDVECTOR pos; |
---|
225 | TRIDVECTOR betagamma; |
---|
226 | // contrairement a ce qu'indique la notice PARMELA, dans parmdesz, les xp et yp |
---|
227 | // sont donnes en radians |
---|
228 | for ( k=0; k < faisceau.size(); k++) { |
---|
229 | x=faisceau.at(k).xx; |
---|
230 | xp=faisceau.at(k).xxp; |
---|
231 | y=faisceau.at(k).yy; |
---|
232 | yp=faisceau.at(k).yyp; |
---|
233 | |
---|
234 | // dephasage par rapport a la reference |
---|
235 | dephas = faisceau.at(k).phi - phaseRef; // degrés |
---|
236 | g = faisceau.at(k).wz/ERESTMeV; |
---|
237 | betagammaz = faisceau.at(k).begamz; |
---|
238 | betaz = betagammaz/(g+1.0); |
---|
239 | deltaz = FACTEUR * betaz * dephas / referencefrequency; |
---|
240 | x += xp * deltaz; |
---|
241 | y += yp * deltaz; |
---|
242 | pos.setComponents(x,y,deltaz); |
---|
243 | betagamma.setComponents(xp*betagammaz, yp*betagammaz, betagammaz); |
---|
244 | particles.at(k) = bareParticle(pos,betagamma); |
---|
245 | } |
---|
246 | return true; |
---|
247 | } |
---|