source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/softwareParmela.cc @ 313

Last change on this file since 313 was 313, checked in by lemeur, 11 years ago

messages sur console

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