#include "dataManager.h" #include "mathematicalConstants.h" #include "PhysicalConstants.h" #include "softwareParmela.h" #include "softwareTransport.h" #include "GWt_pspaApplication.h" #include "GWt_console.h"Ò #include "softwareGenerator.h" #include "softwareTest.h" #include #include #include 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); 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); } void dataManager::addSectionToExecute(abstractElement* debut,int debutIndex, abstractElement* fin, int finIndex, abstractSoftware* prog) { jobList_.push_back(new sectionToExecute(debut, debutIndex, fin, finIndex, prog)); } 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) { if (a<0) return; if (a>= jobList_.size()) return; delete jobList_[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(); 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() { bool success = true; abstractSoftware* softw = NULL; string workingDir = pspa_->getWorkingDir(); for(unsigned k = 0; k < jobList_.size(); k++) { cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->getSoftware()->getName()) << endl; int debut = jobList_[k]->getElementNumberInSection(); int fin = jobList_[k]->getLastElementNumberInSection(); softw = jobList_[k]->getSoftware(); if (softw == NULL) { success = false; consoleMessage("dataManager::executeAll : unknown software"); break; } success = softw->createInputFile(currentBeam_,debut,fin,workingDir); if ( success ) { success = softw->execute(debut,fin,workingDir); if ( success ) { success = softw->buildBeamAfterElements(debut,fin,diagnosticBeam_,workingDir); } } delete softw; if ( success ) { currentBeam_ = &diagnosticBeam_.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; //debug cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl; cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl; for (int j = debut; j <= fin; j++) { abstractElement* elPtr= getElementPointerFromNumero(j); cout << "[" << j << "] " << elPtr->getNomdElement().getElementName() << endl; } } //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 " << name << endl; } outfile << globParam_.FileOutputFlow(); abstractElement* elPtr; for(unsigned k = 0; k < getBeamLineSize() ; k++) { elPtr = pspa_->getBeamLine()->getAbstractElement(k); outfile << elPtr->FileOutputFlow(); } outfile.close(); } bool dataManager::restoreElements( string inputFileName) { cout << "dataManager::restoreElements() fichier " << inputFileName << 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; string globalTitle; if ( infile >> globalTitle ) { if ( globalTitle == string("globals") ) { globParam_.raz(); globParam_.FileInput(infile); } else { cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl; return false; } } else { cerr << " dataManager::restoreElements ERROR : reading data save file" << endl; return false; } pspa_->getBeamLine()->clear(); typedElement elementType; string elementLabel; while (infile >> elementLabel) { elementType = nomdElements::getTypeFromLabel(elementLabel); GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType); if ( nouveau == NULL ) { cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl; return false; } nouveau->FileInput(infile); } infile.close(); // debug // unsigned k; // for(k = 0; k < getBeamLineSize(); k++) { // abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k); // cout << "reupere " << ptr->getLabel() << endl; // } return true; } particleBeam* dataManager::getDiagnosticBeam(unsigned index) { if (index >= diagnosticBeam_.size()) { return NULL; } else { return &diagnosticBeam_.at(index); } } // 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; if ( !diagnosticBeam_.at(i).momentRepresentationOk() ) { diagnosticBeam_.at(i).buildMomentRepresentation(); } longueur += elPtr->getLenghtOfElement(); valeur = diagnosticBeam_.at(i).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) { inputFileName = "parmin"; return new softwareParmela(inputFileName, &globParam_, this); } else if (logi == nomDeLogiciel::transport) { inputFileName = "transport.input"; return new softwareTransport(inputFileName, &globParam_, this); } else if (logi == nomDeLogiciel::generator) { inputFileName = "generator.in"; return new softwareGenerator(inputFileName, &globParam_, this); } else if (logi == nomDeLogiciel::test) { return new softwareTest(inputFileName, &globParam_, this); } else { return NULL; } }