#include #include #include "dataManager.h" #include "mathematicalConstants.h" #include "PhysicalConstants.h" #include "GWt_pspaApplication.h" #include "GWt_console.h" #include "softwareParmela.h" #include "softwareTransport.h" #include "softwareGenerator.h" #include "softwareTest.h" #include "softwareMadx.h" //xx #include "softwareUnknown.h" //xx #include #include "UAP/UAPUtilities.hpp" #include "AML/AMLReader.hpp" #define BAVARD 0 dataManager::dataManager(PspaApplication* pspa) : currentBeam_(NULL), pspa_ (pspa) {} dataManager::~dataManager() { unsigned k; for (k=0; k < jobList_.size();k++) { if ( jobList_[k] != NULL ) delete jobList_[k]; } if ( currentBeam_ == NULL ) delete currentBeam_; } void dataManager::consoleMessage(string message) { GWt_console* console = static_cast (wApp->findWidget ("console")); if (console) console->addConsoleMessage(message + "\n"); pspa_->processEvents(); } string dataManager::getLabelFromElementNumero(int numero) { abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(numero-1); if ( ptr == NULL ) return ""; return ptr->getLabel(); } int dataManager::getNumeroFromElementLabel(string label) { int index = -1; for (int k = 0; k < getBeamLineSize() ; k++) { if (pspa_->getBeamLine()->getAbstractElement(k) != NULL) { if ( pspa_->getBeamLine()->getAbstractElement(k)->getLabel() == label ) { index = (int)k + 1; return index; } } } return index; } abstractElement* dataManager::getElementPointerFromNumero(int k) { return pspa_->getBeamLine()->getAbstractElement(k-1); } sectionToExecute* dataManager::addSectionToExecute(abstractElement* debut,int debutIndex, abstractElement* fin, int finIndex, nomDeLogiciel logiciel) { #if BAVARD > 0 cout << "***********************************" << endl; cout << " dataManager::addSectionToExecute " << endl << endl; #endif abstractSoftware* prog; string inputFileName; if(logiciel == nomDeLogiciel::parmela) { inputFileName = "parmin"; prog = new softwareParmela(inputFileName, &globParam_, this); } else if (logiciel == nomDeLogiciel::transport) { inputFileName = "transport.input"; prog = new softwareTransport(inputFileName, &globParam_, this); } else if (logiciel == nomDeLogiciel::generator) { inputFileName = "generator.in"; prog = new softwareGenerator(inputFileName, &globParam_, this); } else if (logiciel == nomDeLogiciel::test) { prog = new softwareTest(inputFileName, &globParam_, this); } else if (logiciel == nomDeLogiciel::madx) { inputFileName = "madx.input"; prog = new softwareMadx(inputFileName,§orParam_,this); } else { prog = new softwareUnknown(); // xx } jobList_.push_back(new sectionToExecute(debut,debutIndex,fin,finIndex,prog)); return jobList_.back(); } void dataManager::clearSectionToExecute() { unsigned k; for(k = 0; k < jobList_.size(); k++) { if ( jobList_[k] != NULL ) clearSectionToExecute(k); } jobList_.clear(); } void dataManager::clearSectionToExecute(int a) { cout << " dataManager::clearSectionToExecute : effacement de la section d'index = " << a << endl; if (a < 0) return; if (a >= (int)jobList_.size()) return; // lors de la creation de la section on a fait un 'new' d'un // softwareXXXX : on fait ici le 'delete' const abstractSoftware* soft = jobList_.at(a)->getSoftware(); if ( soft != NULL ) delete soft; jobList_.erase (jobList_.begin()+a); } void dataManager::initializeExecution() { string workingDir = pspa_->getWorkingDir(); if (workingDir == "") { return; } removeFile(workingDir + "parmdesz"); removeFile(workingDir + "parmin"); removeFile(workingDir + "parin.input0"); removeFile(workingDir + "transport.input"); removeFile(workingDir + "transport.output"); removeFile(workingDir + "generator.in"); removeFile(workingDir + "faisceau.ini"); removeFile(workingDir + "generator.output"); diagnosticBeam_.clear(); indexElementToIndexDiag_.clear(); currentBeam_ = NULL; firstComputedElemNumero_ = getBeamLineSize(); lastComputedElemNumero_ = 1; clearSectionToExecute(); } void dataManager::removeFile(string nameOfFile) { ifstream fileExists; fileExists.open(nameOfFile.c_str(), ios::in); if (fileExists) { fileExists.close(); remove(nameOfFile.c_str()); } } bool dataManager::executeAll() { #if BAVARD > 0 cout << "***********************************" << endl; cout << " dataManager::executeAll() " << endl << endl; #endif bool success = true; abstractSoftware* softw = NULL; string workingDir = pspa_->getWorkingDir(); // on verifie la consecution des sections int lastel = 0; for(unsigned k = 0; k < jobList_.size(); k++) { if ( jobList_[k]->getElementNumberInSection() != lastel +1 ) { consoleMessage("dataManager::executeAll ERROR : sections should be consecutive "); return false; } else { lastel = jobList_[k]->getLastElementNumberInSection(); } } unsigned debut; unsigned fin; for(unsigned k = 0; k < jobList_.size(); k++) { #if BAVARD > 0 cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->getSoftware()->getName()) << endl; #endif debut = jobList_[k]->getElementNumberInSection(); fin = jobList_[k]->getLastElementNumberInSection(); softw = jobList_[k]->getSoftware(); //cout << "k= " << k << ", de " << jobList_[k]->getFirstElement()->getLabel() << " à " << jobList_[k]->getLastElement()->getLabel() << " avec " << softw->getName() << endl; if (softw == NULL) { success = false; consoleMessage("dataManager::executeAll : unknown software"); break; } success = softw->createInputFile(currentBeam_,debut,fin,workingDir); if ( success ) { success = softw->execute(workingDir); if ( success ) { success = softw->buildBeamAfterElements(workingDir); } } if ( success ) { currentBeam_ = &diagnosticBeam_.at(indexElementToIndexDiag_.back()); cout << " execute termine avec succes " << endl; } else { currentBeam_ = NULL; cout << " execute termine en ECHEC " << endl; } if ( !success ) break; if ( debut < firstComputedElemNumero_ ) firstComputedElemNumero_ = debut; if ( fin > lastComputedElemNumero_ ) lastComputedElemNumero_ = fin; #if BAVARD > 0 cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl; cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl; for (unsigned j = debut; j <= fin; j++) { abstractElement* elPtr= getElementPointerFromNumero(j); cout << "[" << j << "] " << elPtr->getNomdElement().getExpandedName() << endl; } #endif } //k cout << " dataManager::executeAll() : premier element = " << firstComputedElemNumero_ << " dernier = " << lastComputedElemNumero_ << endl; return success; } void dataManager::saveConfiguration(string folder, string nameOfFile) { ofstream outfile; //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save"; cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl; cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl; cout << " et qu'on puisse restaurer normalement" << endl; string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save"; // make dir if not exist boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/"); outfile.open(name.c_str(), ios::out); if (!outfile) { cerr<<" ERROR opening output file for persistency "<getBeamLine()->getAbstractElement(k); outfile << elPtr->FileOutputFlow(); } outfile.close(); } // ecriture sur fichier AML void dataManager::writeToAMLFile(string fileName) { UAPNode* uap_root = NULL; uap_root = new UAPNode("UAP_root"); UAPNode* rep = uap_root->addChild("AML_representation"); // root node in the hierarchy UAPNode* lab = rep->addChild("laboratory"); lab->addAttribute("name","PSPA lab"); // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077 globParam_.InputRep(lab); // accelerator or section of accelerator //UAPNode* acc = lab->addChild("machine"); //acc->addAttribute("name",fileName); //sectorParam_.InputRep(lab); // sequence of elements UAPNode* sect = lab->addChild("sector"); sect->addAttribute("name","a_sect"); abstractElement* elPtr; for(int k = 0; k < getBeamLineSize() ; k++) { elPtr = pspa_->getBeamLine()->getAbstractElement(k); elPtr->InputRep(sect); } cout << "!Create the AML file ---------------------------" << endl; AMLReader reader; string aml_file = pspa_->getWorkingDir() + "/" + fileName + ".aml"; reader.AMLRepToAMLFile (uap_root, aml_file); } bool dataManager::restoreElements(string inputFileName) { cout << "***********************************" << endl; cout << " dataManager::restoreElements() fichier :" << inputFileName << endl << endl; ifstream infile; string name = inputFileName; infile.open(name.c_str(), ios::in); if (!infile) { cerr << " error opening input stream " << name << endl; return false; } else cout << " successful opening input stream : " << name << endl; //NOTE:: je ne sais pas si les "secteurs" seront conservés, aussi pour ne pas trop changer les fichiers je lis les données après "globals" dans la boucle pspa_->getBeamLine()->clear(); nomdElements::typedElement elementType; string elementLabel; while (infile >> elementLabel) { if (elementLabel == string("globals") ) { globParam_.raz(); globParam_.FileInput(infile); } else if (elementLabel == string("sectors") ) { sectorParam_.raz(); sectorParam_.FileInput(infile); } else { elementType = nomdElements::getTypeFromLabel(elementLabel); GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType); if ( nouveau == NULL ) { cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl; return false; } else { nouveau->FileInput(infile); } } }// while #if BAVARD > 1 unsigned k; for(k = 0; k < getBeamLineSize(); k++) { abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k); cout << "recupere " << ptr->getLabel() << endl; } #endif infile.close(); return true; } particleBeam* dataManager::getDiagnosticBeam(unsigned index) { if (index >= indexElementToIndexDiag_.size() ) { return NULL; } else { int indDiag = indexElementToIndexDiag_.at(index); return &diagnosticBeam_.at(indDiag); } } particleBeam* dataManager::getDiagnosticBeam(string elementLabel) { return getDiagnosticBeam( getNumeroFromElementLabel(elementLabel) -1); } // on ne tient pas compte des elements "snapshot" presents dans la beamLine void dataManager::donneesRmsEnveloppe(string type,vector& xcor,vector& ycor, string& titre, string& legendx, string& legendy) { double longueur = 0.0; double valeur = 0.0; xcor.clear(); ycor.clear(); titre.clear(); titre = " rms enveloppe "; legendx.clear(); legendx = " z (m)"; if ( type == "x" ) { unsigned i = 0; cout << " dataManager::donneesRmsEnveloppe BeamLineSize = " << getBeamLineSize() << endl; // for (unsigned k = 0; k < getBeamLineSize(); k++) { for (unsigned k = firstComputedElemNumero_ -1 ; k < lastComputedElemNumero_ ; k++) { abstractElement* elPtr = pspa_->getBeamLine()->getAbstractElement(k); // if(elPtr->getNomdElement().getElementType() == snapshot) continue; // if(elPtr->getNomdElement().getElementType() == fit) continue; particleBeam* beamToDraw = getDiagnosticBeam(i); if ( !beamToDraw->momentRepresentationOk() ) { beamToDraw->buildMomentRepresentation(); } longueur += elPtr->getLenghtOfElement(); valeur = beamToDraw->getXmaxRms(); cout << " dataManager::donneesRmsEnveloppe index = " << k << " longueur = " << longueur << " enveloppe : " << valeur << endl; xcor.push_back(0.01*longueur); // on passe en metres ycor.push_back(valeur); i++; } legendy.clear(); legendy = " x (cm) "; } } int dataManager::getBeamLineSize() { return pspa_->getBeamLine()->getBeamLineSize(); } abstractSoftware* dataManager::createSoftwareConnexion(nomDeLogiciel logi) { string inputFileName; if(logi == nomDeLogiciel::parmela) { cout << " dataManager::createSoftwareConnexion : logiciel identifie : parmela " << endl; inputFileName = "parmin"; return new softwareParmela(inputFileName, &globParam_, this); } else if (logi == nomDeLogiciel::transport) { cout << " dataManager::createSoftwareConnexion : logiciel identifie : transport " << endl; inputFileName = "transport.input"; return new softwareTransport(inputFileName, &globParam_, this); } else if (logi == nomDeLogiciel::generator) { cout << " dataManager::createSoftwareConnexion : logiciel identifie : generator " << endl; inputFileName = "generator.in"; return new softwareGenerator(inputFileName, &globParam_, this); } else if (logi == nomDeLogiciel::test) { cout << " dataManager::createSoftwareConnexion : logiciel identifie : test " << endl; return new softwareTest(inputFileName, &globParam_, this); } else { cout << " dataManager::createSoftwareConnexion : logiciel identifie : null " << endl; return NULL; } }