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