StatisticalErrors.h 6.37 KB
Newer Older
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#ifndef OPAL_STATISTICALERRORS_H
#define OPAL_STATISTICALERRORS_H

//
//  Copyright & License: See Copyright.readme in src directory
//

/*!
  Class documentation
*/

#define SE_VISITELEMENT(elem) virtual void visit##elem(const elem &) { }

#include "Algorithms/Tracker.h"

#ifdef WITH_UNIT_TESTS
#include <gtest/gtest_prod.h>
#endif

#include <iostream>
#include <list>

class BMultipoleField;
class PartBunch;
class AlignWrapper;
class BeamBeam;
class Collimator;
class Corrector;
class CyclotronValley;
class Degrader;
class Diagnostic;
class Drift;
class ElementBase;
34
class FlexibleCollimator;
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
class Lambertson;
class Marker;
class Monitor;
class Multipole;
class ParallelPlate;
class Probe;
class RBend;
class RFCavity;
class RFQuadrupole;
class SBend;
class Separator;
class Septum;
class Solenoid;
class TravelingWave;
struct OpalInputInterpreter;


class StatisticalErrors: public Tracker {

public:
    /// Constructor.
    explicit StatisticalErrors(const Beamline &beamline,
                               const PartData &reference,
                               bool revBeam,
                               bool revTrack,
                               const std::string &method,
                               unsigned int nodesPerInstance,
                               unsigned int numInstances,
                               const std::vector<std::string> &objectives);

    virtual ~StatisticalErrors();

    SE_VISITELEMENT(AlignWrapper)
    SE_VISITELEMENT(Beamline)
    SE_VISITELEMENT(BeamBeam)
    SE_VISITELEMENT(Collimator)
    SE_VISITELEMENT(Corrector)
    SE_VISITELEMENT(CyclotronValley)
    SE_VISITELEMENT(Degrader)
    SE_VISITELEMENT(Diagnostic)
    SE_VISITELEMENT(Drift)
76
    SE_VISITELEMENT(FlexibleCollimator)
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    SE_VISITELEMENT(Lambertson)
    SE_VISITELEMENT(Marker)
    SE_VISITELEMENT(Monitor)
    SE_VISITELEMENT(Multipole)
    SE_VISITELEMENT(ParallelPlate)
    SE_VISITELEMENT(Probe)
    SE_VISITELEMENT(RBend)
    SE_VISITELEMENT(RFCavity)
    SE_VISITELEMENT(RFQuadrupole)
    SE_VISITELEMENT(SBend)
    SE_VISITELEMENT(Separator)
    SE_VISITELEMENT(Septum)
    SE_VISITELEMENT(Solenoid)
    SE_VISITELEMENT(TravelingWave)

    virtual void execute();

private:

    StatisticalErrors();
    StatisticalErrors(const StatisticalErrors &);

    void operator=(const StatisticalErrors &);

    void runSimulation(const std::string &inputFileName, MPI_Comm comm);
    void stashEnvironment();
    void popEnvironment();

    static std::string getNextDirectoryName(bool reference);
    void createDirectory(const std::string &name);
    void linkFiles(const std::string &path);
    void removeLinks(const std::string &path);

    void calcIdealDivision(unsigned int numNodes);
    void formGroups();
    void shutReplicasDown(int tag,
                          const std::vector<std::string> &currentJobs);
    std::pair<int, int> identifyFreeReplica(int tag);
    std::string assignReplicaNewJob(const OpalInputInterpreter &interpreter,
                                    const std::pair<int, int> &source,
                                    bool referenceRun,
                                    std::string nextDir,
                                    int tag);

    struct PRMessage {
        int status;
        std::string nextDirectory;
    };

    PRMessage getCrunchJobFromPrimary(int tag);
    void collectOutputData(const std::string &directory);
    void processOutputData(const std::string &directory);
129
    // std::string readSDDSFile(const std::string &directory);
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    void writeSDDSFile();
    void writeSDDSHeader(std::ofstream &out,
                         std::vector<std::string> &order);
    void writeSDDSData(std::ofstream &out,
                       const std::map<std::string, std::vector<double> > &data,
                       const std::vector<std::string> &order);
    std::vector<std::pair<double, double> > computeStatistics(const std::string &objective,
                                                              const std::vector<double> &referencePositions);
    std::vector<double> getNextDataset(std::ifstream &dataStream,
                                       std::ifstream &positionStream,
                                       int dataType,
                                       const std::vector<double> &referencePositions);

    std::vector<double > interpolateSDDSData(const std::vector<double> &data,
                                             const std::vector<double> &oldpositions,
                                             const std::vector<double> &newpositions);
    std::vector<double> readFloatData(std::ifstream &in);
    std::vector<double> readDoubleData(std::ifstream &in);
    std::vector<double> readShortData(std::ifstream &in);
    std::vector<double> readLongData(std::ifstream &in);

151
    // SDDS::file parseSDDSFile(std::string & contents);
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    void removeOldDataFiles();
    void removeBinaryDataFiles();
    std::map<std::string, std::vector<double> > distributeProcessingJobs();
    void getProcessingJobFromPrimary();

    std::vector<double> getReferenceSamplingPositions();
    void sendReferenceSamplingPositions(std::vector<double> positionSampling);
    void sendObjectiveForProcessing(int tag,
                                    int source,
                                    unsigned int i,
                                    char* messageBuffer);
    std::pair<int, int> getNextObjectiveToProcess(int tag, char *messageBuffer);
    void sendProcessedDataToPrimary(int tag,
                                    int objectiveID,
                                    unsigned int length,
                                    char *messageBuffer);
    void copyDataToBuffer(char *messageBuffer,
                          const std::vector<std::pair<double, double> > &statistics);

    struct DataSource {
        int source;
        int ID;
        std::vector<double> data;
    };

    DataSource getProcessedDataFromReplica(int tag,
                                           unsigned int i,
                                           unsigned int length,
                                           char* messageBuffer);

    std::string method_m;
    unsigned int nodesPerInstance_m;
    unsigned int numInstances_m;

    MPI_Comm subComm_m;
    MPI_Group subGroup_m;
    std::vector<int> groupIDs_m;
    std::vector<int> groupRanks_m;
    std::vector<int> localMasters_m;
    std::vector<std::string> objectives_m;

    std::string directoryBaseName_m;
};

#endif // OPAL_STATISTICALERRORS_H