LossDataSink.h 2.39 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5
#ifndef OPAL_LOSSOUTPUT_H_
#define OPAL_LOSSOUTPUT_H_

//////////////////////////////////////////////////////////////
#include "Utility/IpplInfo.h"
adelmann's avatar
adelmann committed
6 7
#include "Utilities/Options.h"
#include "AbstractObjects/OpalData.h"
8
#include "Algorithms/Vektor.h"
adelmann's avatar
adelmann committed
9

gsell's avatar
gsell committed
10 11 12 13 14 15 16 17 18
#include <string>
#include <fstream>
#include <vector>

#include <hdf5.h>
#include "H5hut.h"

#include "config.h"

adelmann's avatar
adelmann committed
19 20 21 22
/*
  - In the destructor we do ALL the file handling
  - h5hut_mode_m defines h5hut or ASCII
 */
gsell's avatar
gsell committed
23
class LossDataSink {
adelmann's avatar
adelmann committed
24 25
 public:
    LossDataSink();
gsell's avatar
gsell committed
26

adelmann's avatar
adelmann committed
27
    LossDataSink(std::string elem, bool hdf5Save);
gsell's avatar
gsell committed
28

adelmann's avatar
adelmann committed
29 30
    LossDataSink(const LossDataSink &rsh);
    ~LossDataSink();
gsell's avatar
gsell committed
31

adelmann's avatar
adelmann committed
32
    bool inH5Mode() { return h5hut_mode_m;}
gsell's avatar
gsell committed
33

adelmann's avatar
adelmann committed
34 35 36
    void save();

    void addParticle(const Vector_t x, const Vector_t p, const size_t id);
37 38

    void addParticle(const Vector_t x, const Vector_t p, const size_t  id, const double time, const size_t turn);
gsell's avatar
gsell committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52

private:

    void open() {
        if(Ippl::myNode() == 0) {
            os_m.open(fn_m.c_str(), std::ios::out);
        }
    }

    void append() {
        if(Ippl::myNode() == 0) {
            os_m.open(fn_m.c_str(), std::ios::app);
        }
    }
53

gsell's avatar
gsell committed
54 55 56 57
    void close() {
        if(Ippl::myNode() == 0)
            os_m.close();
    }
58

adelmann's avatar
adelmann committed
59
    void writeHeaderASCII() {
gsell's avatar
gsell committed
60
        if(Ippl::myNode() == 0) {
adelmann's avatar
adelmann committed
61 62 63 64
            if (time_m.size() != 0)
                os_m << "# Element " << element_m << " x (mm),  y (mm),  z (mm),  px ( ),  py ( ),  pz ( ), id,  turn,  time (ns) " << std::endl;
            else
                os_m << "# Element " << element_m << " x (mm),  y (mm),  z (mm),  px ( ),  py ( ),  pz ( ), id " << std::endl;
gsell's avatar
gsell committed
65 66 67
        }
    }

68 69 70 71 72
    //bool hasNoParticlesToDump() {
        //return x_m.size() == 0;
    //}

    bool hasNoParticlesToDump();
gsell's avatar
gsell committed
73

adelmann's avatar
adelmann committed
74
    void writeHeaderH5();
gsell's avatar
gsell committed
75

adelmann's avatar
adelmann committed
76 77 78
    void openH5();

    void saveH5();
79

adelmann's avatar
adelmann committed
80
    void saveASCII();
gsell's avatar
gsell committed
81 82

private:
adelmann's avatar
adelmann committed
83

gsell's avatar
gsell committed
84 85 86 87 88
    // filename without extension
    std::string fn_m;

    // used to write out the data
    std::ofstream os_m;
89

gsell's avatar
gsell committed
90 91
    std::string element_m;

adelmann's avatar
adelmann committed
92
    bool h5hut_mode_m;
gsell's avatar
gsell committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106

    /// %Pointer to H5 file for particle data.
    h5_file_t *H5file_m;

    /// Current record, or time step, of H5 file.
    h5_int64_t H5call_m;

    std::vector<long>   id_m;
    std::vector<double>  x_m;
    std::vector<double>  y_m;
    std::vector<double>  z_m;
    std::vector<double> px_m;
    std::vector<double> py_m;
    std::vector<double> pz_m;
107

108 109
    std::vector<size_t> turn_m;
    std::vector<double> time_m;
gsell's avatar
gsell committed
110 111
};
#endif