LossDataSink.h 3.17 KB
Newer Older
gsell's avatar
gsell 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
#ifndef OPAL_LOSSOUTPUT_H_
#define OPAL_LOSSOUTPUT_H_

//////////////////////////////////////////////////////////////
//#include "Ippl.h"
#include "Utility/IpplInfo.h"
#include <string>
//#include <iosfwd>
#include <fstream>
#include <vector>
//#include <iostream>
#include "AbstractObjects/OpalData.h"

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

#include "config.h"

#define MAXLOSS 1000000

//////////////////////////////////////////////////////////////
// specialization of LossDataSink to PPartBunch objects
class LossDataSink {
public:

    LossDataSink(size_t np, bool hdf5Save = false):
        element_m("NULL"),
        doHdf5Save_m(hdf5Save) {
29
        if(!doHdf5Save_m) {
30
            fn_m = OpalData::getInstance()->getInputBasename() + std::string(".loss");
Jianjun Yang's avatar
Jianjun Yang committed
31 32
	    os_m.precision(8);
	    os_m.setf(std::ios::scientific, std::ios::floatfield);
33 34 35 36 37
        if(OpalData::getInstance()->inRestartRun()) 
            append();
        else
            open();
        writeHeader(np);
38
	    close();
gsell's avatar
gsell committed
39 40 41 42 43 44 45
        }
        hdf5FileIsOpen_m = false;
        H5call_m = 0;
    }

    LossDataSink():
        element_m("NULL") {
46
        if(!doHdf5Save_m) {
47
            fn_m = OpalData::getInstance()->getInputBasename() + std::string(".loss");
Jianjun Yang's avatar
Jianjun Yang committed
48 49
	    os_m.precision(8);
	    os_m.setf(std::ios::scientific, std::ios::floatfield);
gsell's avatar
gsell committed
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
            append();
        }
        hdf5FileIsOpen_m = false;
        H5call_m = 0;
    }

    ~LossDataSink() {
        save(element_m);
        if(!doHdf5Save_m) {
            close();
        } else {
            if(hdf5FileIsOpen_m) {
                H5CloseFile(H5file_m);
                Ippl::Comm->barrier();
            }
        }

    }

    bool inH5Mode() { return doHdf5Save_m;}

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);
        }
    }

    void close() {
        if(Ippl::myNode() == 0)
            os_m.close();
    }

    void writeHeader(size_t np) {
        if(Ippl::myNode() == 0) {
92
            os_m << "# Element x (mm),  y (mm),  z (mm),  px ( ),  py ( ),  pz ( ), id,  turn,  time (ns) " << std::endl;
gsell's avatar
gsell committed
93 94 95 96 97 98 99
        }
    }

    void writeH5FileAttributes();

public:

100 101
    void addParticle(const Vector_t x, const Vector_t p, const size_t id);
    void addParticle_time(const Vector_t x, const Vector_t p, const size_t  id, const double time, const size_t turn); 
gsell's avatar
gsell committed
102 103
    void openH5(std::string fn);
    void save(std::string element) ;
104
    void save_time(std::string element) ;
gsell's avatar
gsell committed
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

private:
    // filename without extension
    std::string fn_m;

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

    std::string element_m;

    bool doHdf5Save_m;

    bool hdf5FileIsOpen_m;

    /// %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;
132 133
    std::vector<size_t> turn_m;
    std::vector<double> time_m;
gsell's avatar
gsell committed
134 135 136 137 138
};
#endif