Fieldmap.hh 5.14 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8
#ifndef CLASSIC_FIELDMAP_HH
#define CLASSIC_FIELDMAP_HH

#define READ_BUFFER_LENGTH 256

#include <string>
#include <map>
#include <vector>
9
#include "Algorithms/Vektor.h"
gsell's avatar
gsell committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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

enum MapType {
    UNKNOWN = 0,
    T1DDynamic,
    TAstraDynamic,
    T1DElectroStatic,
    TAstraElectroStatic,
    T1DMagnetoStatic,
    TAstraMagnetoStatic,
    T1DProfile1,
    T1DProfile2,
    T2DDynamic,
    T2DDynamic_cspline,
    T2DElectroStatic,
    T2DElectroStatic_cspline,
    T2DMagnetoStatic,
    T2DMagnetoStatic_cspline,
    T3DDynamic,
    T3DElectroStatic,
    T3DMagnetoStatic,
    T3DMagnetoStaticH5Block,
    T3DDynamicH5Block
};

enum SwapType {

    XZ = 0,
    ZX,
    XYZ = 10,
    XZMY,
    XMYMZ,
    XMZY,
    YMXZ,
    MXMYZ,
    MYXZ,
    ZYMX,
    MXYMZ,
    MZYX
};

enum DiffDirection {
    DX = 0,
    DY,
    DZ
};

class Fieldmap {

public:

    static Fieldmap *getFieldmap(std::string Filename, bool fast = false);
    static std::vector<std::string> getListFieldmapNames();
    static void deleteFieldmap(std::string Filename);
63
    static void clearDictionary();
gsell's avatar
gsell committed
64 65 66 67 68 69 70
    static MapType readHeader(std::string Filename);
    static void readMap(std::string Filename);
    static void freeMap(std::string Filename);

    static std::string typeset_msg(const std::string &msg, const std::string &title);

    virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const = 0;
71
    virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const = 0;
gsell's avatar
gsell committed
72
    virtual void getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const = 0;
73
    virtual void getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const = 0;
gsell's avatar
gsell committed
74 75 76 77 78 79
    virtual void swap() = 0;
    virtual void getInfo(Inform *msg) = 0;
    virtual double getFrequency() const = 0;
    virtual void setFrequency(double freq) = 0;
    virtual void setEdgeConstants(const double &bendAngle, const double &entranceAngle, const double &exitAngle);
    virtual void setFieldLength(const double &);
80 81 82 83 84 85 86 87 88 89 90

    virtual void Get1DProfile1EngeCoeffs(std::vector<double> &engeCoeffsEntry,
                                         std::vector<double> &engeCoeffsExit);
    virtual void Get1DProfile1EntranceParam(double &entranceParameter1,
                                           double &entranceParameter2,
                                           double &entranceParameter3);
    virtual void Get1DProfile1ExitParam(double &exitParameter1,
                                       double &exitParameter2,
                                       double &exitParameter3);
    virtual double GetFieldGap();
    virtual void SetFieldGap(double gap);
gsell's avatar
gsell committed
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    MapType getType() { return Type;}

    virtual void getOnaxisEz(std::vector<std::pair<double, double> > & onaxis);

protected:
    Fieldmap(const std::string &aFilename);
    virtual ~Fieldmap() { ;};
    MapType Type;

    std::string Filename_m;
    int lines_read_m;

    void getLine(std::ifstream &in, std::string &buffer);
    static void getLine(std::ifstream &in, int &lines_read, std::string &buffer);
    template<class S>
    bool interpreteLine(std::ifstream &in, S &value, const bool &file_length_known = true);
    template<class S, class T>
    bool interpreteLine(std::ifstream &in, S &value1, T &value2, const bool &file_length_known = true);
    template<class S, class T, class U>
    bool interpreteLine(std::ifstream &in, S &value1, T &value2, U &value3, const bool &file_length_known = true);
    template<class S, class T, class U, class V>
    bool interpreteLine(std::ifstream &in, S &value1, T &value2, U &value3, V &value4, const bool &file_length_known = true);
    template<class S>
    bool interpreteLine(std::ifstream &in, S &value1, S &value2, S &value3, S &value4, S &value5, S &value6, const bool &file_length_known = true);

    bool interpreteEOF(std::ifstream &in);

    void interpreteWarning(const std::string &error_msg, const std::string &expecting, const std::string &found);
    void interpreteWarning(const std::ios_base::iostate &state,
                           const bool &read_all,
                           const std::string &error_msg,
                           const std::string &found);
    void missingValuesWarning();
    void exceedingValuesWarning();

    void disableFieldmapWarning();
    void noFieldmapWarning();

public:
    virtual void readMap() = 0;
    virtual void freeMap() = 0;
private:
    template<typename T>
    struct TypeParseTraits {
        static const char *name;
    };

    static char buffer_m[READ_BUFFER_LENGTH];
    static std::string alpha_numeric;

    struct FieldmapDescription {
        MapType Type;
        Fieldmap *Map;
        unsigned int RefCounter;
        unsigned int FreeCounter;
        bool read;
        FieldmapDescription(MapType aType, Fieldmap *aMap) {
            Type = aType;
            Map = aMap;
            RefCounter = 1;
	    FreeCounter = 0;// fixme: chuan add this because in multipacting simulation FreeCounter can not be properly initialized.
            read = false;
        }
        //     increaseCounter
    };

    static std::map<std::string, FieldmapDescription> FieldmapDictionary;

};

#endif