H5PartWrapperForPC.cpp 23.1 KB
Newer Older
1 2 3 4 5 6
//
//  Copyright & License: See Copyright.readme in src directory
//

#include "Structure/H5PartWrapperForPC.h"

7
#include "OPALconfig.h"
8
#include "Algorithms/PartBunchBase.h"
9
#include "AbstractObjects/OpalData.h"
10
#include "Utilities/Options.h"
11
#include "Utilities/Util.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "Physics/Physics.h"

#include <boost/filesystem.hpp>

#include <sstream>
#include <set>

extern Inform *gmsg;

H5PartWrapperForPC::H5PartWrapperForPC(const std::string &fileName, h5_int32_t flags):
    H5PartWrapper(fileName, flags),
    previousH5Local_m(false),
    referenceMomentum_m(0.0),
    referenceLocation_m(0.0),
    meanE_m(0.0),
    meanMomentum_m(0.0),
    azimuth_m(0.0),
    elevation_m(0.0)
{ }

32 33 34 35
H5PartWrapperForPC::H5PartWrapperForPC(const std::string &fileName,
                                       int restartStep,
                                       std::string sourceFile,
                                       h5_int32_t flags):
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
    H5PartWrapper(fileName, restartStep, sourceFile, flags),
    previousH5Local_m(false),
    referenceMomentum_m(0.0),
    referenceLocation_m(0.0),
    meanE_m(0.0),
    meanMomentum_m(0.0),
    azimuth_m(0.0),
    elevation_m(0.0)
{ }

H5PartWrapperForPC::~H5PartWrapperForPC()
{ }

void H5PartWrapperForPC::readHeader() {
    h5_int64_t numFileAttributes = H5GetNumFileAttribs(file_m);

    const h5_size_t lengthAttributeName = 256;
    char attributeName[lengthAttributeName];
    h5_int64_t attributeType;
    h5_size_t numAttributeElements;
    std::set<std::string> attributeNames;

    for (h5_int64_t i = 0; i < numFileAttributes; ++ i) {
        REPORTONERROR(H5GetFileAttribInfo(file_m,
                                          i,
                                          attributeName,
                                          lengthAttributeName,
                                          &attributeType,
                                          &numAttributeElements));

        attributeNames.insert(attributeName);
    }

    if (attributeNames.find("dump frequency") != attributeNames.end()) {
        h5_int64_t dumpfreq;
        READFILEATTRIB(Int64, file_m, "dump frequency", &dumpfreq);
        OpalData::getInstance()->setRestartDumpFreq(dumpfreq);
    }
}

frey_m's avatar
frey_m committed
76
void H5PartWrapperForPC::readStep(PartBunchBase<double, 3>* bunch,
77 78 79
                                  h5_ssize_t firstParticle,
                                  h5_ssize_t lastParticle)
{
80 81 82 83 84 85 86 87
    h5_ssize_t numStepsInSource = H5GetNumSteps(file_m);
    h5_ssize_t readStep = numStepsInSource - 1;
    REPORTONERROR(H5SetStep(file_m, readStep));

    readStepHeader(bunch);
    readStepData(bunch, firstParticle, lastParticle);
}

frey_m's avatar
frey_m committed
88
void H5PartWrapperForPC::readStepHeader(PartBunchBase<double, 3>* bunch) {
89 90
    h5_float64_t pathLength;
    READSTEPATTRIB(Float64, file_m, "SPOS", &pathLength);
frey_m's avatar
frey_m committed
91
    bunch->setLPath(pathLength);
92 93 94

    h5_int64_t ltstep;
    READSTEPATTRIB(Int64, file_m, "LocalTrackStep", &ltstep);
frey_m's avatar
frey_m committed
95
    bunch->setLocalTrackStep((long long)ltstep);
96 97 98

    h5_int64_t gtstep;
    READSTEPATTRIB(Int64, file_m, "GlobalTrackStep", &gtstep);
frey_m's avatar
frey_m committed
99
    bunch->setGlobalTrackStep((long long)gtstep);
100 101 102 103 104

    READSTEPATTRIB(Float64, file_m, "ENERGY", &meanE_m);

    double actualT;
    READSTEPATTRIB(Float64, file_m, "TIME", &actualT);
frey_m's avatar
frey_m committed
105
    bunch->setT(actualT);
106 107 108

    h5_int64_t SteptoLastInj;
    READSTEPATTRIB(Int64, file_m, "SteptoLastInj", &SteptoLastInj);
frey_m's avatar
frey_m committed
109
    bunch->setSteptoLastInj((int)SteptoLastInj);
110 111 112

    h5_int64_t numBunch;
    READSTEPATTRIB(Int64, file_m, "NumBunch", &numBunch);
frey_m's avatar
frey_m committed
113
    bunch->setNumBunch((int)numBunch);
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

    if (predecessorOPALFlavour_m == "opal-cycl") {
        READSTEPATTRIB(Float64, file_m, "REFPR", (h5_float64_t*) &referenceMomentum_m[0]);
        READSTEPATTRIB(Float64, file_m, "REFPT", (h5_float64_t*) &referenceMomentum_m[1]);
        READSTEPATTRIB(Float64, file_m, "REFPZ", (h5_float64_t*) &referenceMomentum_m[2]);

        READSTEPATTRIB(Float64, file_m, "REFR", (h5_float64_t*) &referenceLocation_m[0]);
        READSTEPATTRIB(Float64, file_m, "REFTHETA", (h5_float64_t*) &referenceLocation_m[1]);
        READSTEPATTRIB(Float64, file_m, "REFZ", (h5_float64_t*) &referenceLocation_m[2]);

        READSTEPATTRIB(Float64, file_m, "AZIMUTH", &azimuth_m);
        READSTEPATTRIB(Float64, file_m, "ELEVATION", &elevation_m);

        h5_int64_t localDump = 0;
        h5_int64_t rc = H5ReadStepAttribInt64(file_m, "LOCAL", &localDump);
        if(rc != H5_SUCCESS) {

            ERRORMSG("H5 rc= " << rc << " in " << __FILE__ << " @ line " << __LINE__ << endl);

            std::string errormsg =
                "You are trying to restart from a legacy file that doesn't contain"
                "information on local/global frame. We are working on legacy support,"
                "but for now you have to use OPAL 1.3.0!";

            throw OpalException("H5PartWrapperForPC::readStepHeader", errormsg);
        } else {
            if (localDump == 1) previousH5Local_m = true;
        }
    } else {
frey_m's avatar
frey_m committed
143 144
        bunch->setT(0.0);
        bunch->setLocalTrackStep((long long) 0 );
145 146
    }

frey_m's avatar
frey_m committed
147
    double mass = bunch->getM() * 1e-6;
148 149 150
    meanMomentum_m = sqrt(std::pow(meanE_m,2.0) + 2 * meanE_m * mass) / mass;
}

frey_m's avatar
frey_m committed
151
void H5PartWrapperForPC::readStepData(PartBunchBase<double, 3>* bunch,
152 153 154
                                      h5_ssize_t firstParticle,
                                      h5_ssize_t lastParticle)
{
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    h5_ssize_t numParticles = getNumParticles();
    if (lastParticle >= numParticles || firstParticle > lastParticle) {
        throw OpalException("H5PartWrapperForPC::readStepData",
                            "the provided particle numbers don't match the number of particles in the file");
    }
    const unsigned int yIndex = (predecessorOPALFlavour_m == "opal-t"? 2: 1);
    const unsigned int zIndex = (predecessorOPALFlavour_m == "opal-t"? 1: 2);

    REPORTONERROR(H5PartSetView(file_m, firstParticle, lastParticle));

    numParticles = lastParticle - firstParticle + 1;

    std::vector<char> buffer(numParticles * sizeof(h5_float64_t));
    h5_float64_t *f64buffer = reinterpret_cast<h5_float64_t*>(&buffer[0]);

    READDATA(Float64, file_m, "x", f64buffer);
    for(long int n = 0; n < numParticles; ++ n) {
frey_m's avatar
frey_m committed
172 173
        bunch->R[n](0) = f64buffer[n];
        bunch->Bin[n] = 0;
174 175 176 177
    }

    READDATA(Float64, file_m, "y", f64buffer);
    for(long int n = 0; n < numParticles; ++ n) {
frey_m's avatar
frey_m committed
178
        bunch->R[n](yIndex) = f64buffer[n];
179 180 181 182
    }

    READDATA(Float64, file_m, "z", f64buffer);
    for(long int n = 0; n < numParticles; ++ n) {
frey_m's avatar
frey_m committed
183
        bunch->R[n](zIndex) = f64buffer[n];
184 185 186 187
    }

    READDATA(Float64, file_m, "px", f64buffer);
    for(long int n = 0; n < numParticles; ++ n) {
frey_m's avatar
frey_m committed
188
        bunch->P[n](0) = f64buffer[n];
189 190 191 192
    }

    READDATA(Float64, file_m, "py", f64buffer);
    for(long int n = 0; n < numParticles; ++ n) {
frey_m's avatar
frey_m committed
193
        bunch->P[n](yIndex) = f64buffer[n];
194 195 196 197
    }

    READDATA(Float64, file_m, "pz", f64buffer);
    for(long int n = 0; n < numParticles; ++ n) {
frey_m's avatar
frey_m committed
198
        bunch->P[n](zIndex) = f64buffer[n];
199 200 201 202
    }

    READDATA(Float64, file_m, "q", f64buffer);
    for(long int n = 0; n < numParticles; ++ n) {
frey_m's avatar
frey_m committed
203
        bunch->Q[n] = f64buffer[n];
204 205 206 207
    }

    READDATA(Float64, file_m, "mass", f64buffer);
    for(long int n = 0; n < numParticles; ++ n) {
frey_m's avatar
frey_m committed
208
        bunch->M[n] = f64buffer[n];
209 210 211 212 213 214 215
    }

    REPORTONERROR(H5PartSetView(file_m, -1, -1));
}

void H5PartWrapperForPC::writeHeader() {
    std::stringstream OPAL_version;
216
    OPAL_version << PACKAGE_NAME << " " << OPAL_VERSION_STR << " # git rev. " << Util::getGitRevision();
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    WRITESTRINGFILEATTRIB(file_m, "OPAL_version", OPAL_version.str().c_str());

    WRITESTRINGFILEATTRIB(file_m, "tUnit", "s");
    WRITESTRINGFILEATTRIB(file_m, "xUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "yUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "zUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "pxUnit", "#beta#gamma");
    WRITESTRINGFILEATTRIB(file_m, "pyUnit", "#beta#gamma");
    WRITESTRINGFILEATTRIB(file_m, "pzUnit", "#beta#gamma");
    WRITESTRINGFILEATTRIB(file_m, "qUnit", "Cb");

    WRITESTRINGFILEATTRIB(file_m, "idUnit", "1");
    WRITESTRINGFILEATTRIB(file_m, "ptype", "1");
    WRITESTRINGFILEATTRIB(file_m, "lastsection", "1");

    WRITESTRINGFILEATTRIB(file_m, "SPOSUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "TIMEUnit", "s");
    WRITESTRINGFILEATTRIB(file_m, "#gammaUnit", "1");
    WRITESTRINGFILEATTRIB(file_m, "ENERGYUnit", "MeV");
    WRITESTRINGFILEATTRIB(file_m, "#varepsilonUnit", "m rad");
    WRITESTRINGFILEATTRIB(file_m, "#varepsilonrUnit", "m rad");
    WRITESTRINGFILEATTRIB(file_m, "#varepsilon-geomUnit", "m rad");

    WRITESTRINGFILEATTRIB(file_m, "#sigmaUnit", "1");
    WRITESTRINGFILEATTRIB(file_m, "RMSXUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "RMSRUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "RMSPUnit", "#beta#gamma");

    WRITESTRINGFILEATTRIB(file_m, "maxdEUnit", "MeV");
    WRITESTRINGFILEATTRIB(file_m, "max#phiUnit", "deg");

    WRITESTRINGFILEATTRIB(file_m, "phizUnit", "deg");
    WRITESTRINGFILEATTRIB(file_m, "dEUnit", "MeV");

    WRITESTRINGFILEATTRIB(file_m, "MASSUnit", "GeV");
    WRITESTRINGFILEATTRIB(file_m, "CHARGEUnit", "C");

    WRITESTRINGFILEATTRIB(file_m, "spos-headUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "E-headUnit", "MV/m");
    WRITESTRINGFILEATTRIB(file_m, "B-headUnit", "T");
    WRITESTRINGFILEATTRIB(file_m, "spos-refUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "E-refUnit", "MV/m");
    WRITESTRINGFILEATTRIB(file_m, "B-refUnit", "T");
    WRITESTRINGFILEATTRIB(file_m, "spos-tailUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "E-tailUnit", "MV/m");
    WRITESTRINGFILEATTRIB(file_m, "B-tailUnit", "T");

    WRITESTRINGFILEATTRIB(file_m, "StepUnit", "1");
    WRITESTRINGFILEATTRIB(file_m, "LocalTrackStepUnit", "1");
    WRITESTRINGFILEATTRIB(file_m, "GlobalTrackStepUnit", "1");
    WRITESTRINGFILEATTRIB(file_m, "NumBunchUnit", "1");
    WRITESTRINGFILEATTRIB(file_m, "NumPartUnit", "1");

    WRITESTRINGFILEATTRIB(file_m, "RefPartRUnit", "m");
    WRITESTRINGFILEATTRIB(file_m, "RefPartPUnit", "#beta#gamma");
    WRITESTRINGFILEATTRIB(file_m, "SteptoLastInjUnit", "1");

    /// Write file dump frequency.
    h5_int64_t dumpfreq = Options::psDumpFreq;
    WRITEFILEATTRIB(Int64, file_m, "dump frequency", &dumpfreq, 1);
}

frey_m's avatar
frey_m committed
279
void H5PartWrapperForPC::writeStep(PartBunchBase<double, 3>* bunch,
280 281
                                   const std::map<std::string, double> &additionalStepAttributes)
{
frey_m's avatar
frey_m committed
282
    if (bunch->getTotalNum() == 0) return;
283

284 285 286 287 288 289
    writeStepHeader(bunch, additionalStepAttributes);
    writeStepData(bunch);

    ++ numSteps_m;
}

frey_m's avatar
frey_m committed
290
void H5PartWrapperForPC::writeStepHeader(PartBunchBase<double, 3>* bunch,
291 292
                                         const std::map<std::string, double> &additionalStepAttributes)
{
frey_m's avatar
frey_m committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    bunch->calcBeamParameters();

    double   t          = bunch->getT();
    double   pathLength = bunch->getLPath();
    Vector_t rmin       = bunch->get_origin();
    Vector_t rmax       = bunch->get_maxExtent();
    Vector_t centroid   = bunch->get_centroid();

    Vector_t meanR = bunch->get_rmean();
    Vector_t meanP = bunch->get_pmean();
    Vector_t xsigma = bunch->get_rrms();
    Vector_t psigma = bunch->get_prms();
    Vector_t vareps = bunch->get_norm_emit();
    Vector_t geomvareps = bunch->get_emit();

    Vector_t RefPartR = bunch->RefPartR_m;
    Vector_t RefPartP = bunch->RefPartP_m;

    double meanEnergy = bunch->get_meanKineticEnergy();
    double energySpread = bunch->getdE();
    double I_0 = 4.0 * Physics::pi * Physics::epsilon_0 * Physics::c * bunch->getM() / bunch->getQ();
314
    double sigma = ((xsigma[0] * xsigma[0]) + (xsigma[1] * xsigma[1])) /
frey_m's avatar
frey_m committed
315
        (2.0 * bunch->get_gamma() * I_0 * (geomvareps[0] * geomvareps[0] + geomvareps[1] * geomvareps[1]));
316

frey_m's avatar
frey_m committed
317 318
    h5_int64_t localTrackStep = (h5_int64_t)bunch->getLocalTrackStep();
    h5_int64_t globalTrackStep = (h5_int64_t)bunch->getGlobalTrackStep();
319

frey_m's avatar
frey_m committed
320 321
    h5_int64_t numBunch = (h5_int64_t)bunch->getNumBunch();
    h5_int64_t SteptoLastInj = (h5_int64_t)bunch->getSteptoLastInj();
322

frey_m's avatar
frey_m committed
323 324
    double mass = 1.0e-9 * bunch->getM();
    double charge = bunch->getCharge();
325

326
    h5_int64_t localFrame = ( Options::psDumpFrame != Options::GLOBAL );
327 328 329 330 331 332 333

    double sposHead = 0.0;
    double sposRef = 0.0;
    double sposTail = 0.0;

    Vector_t maxP(0.0);
    Vector_t minP(0.0);
frey_m's avatar
frey_m committed
334
    bunch->get_PBounds(minP, maxP);
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

    REPORTONERROR(H5SetStep(file_m, numSteps_m));

    char const *OPALFlavour = "opal-cycl";
    WRITESTRINGSTEPATTRIB(file_m, "OPAL_flavour", OPALFlavour);
    WRITESTEPATTRIB(Float64, file_m, "TIME", &t, 1);
    WRITESTEPATTRIB(Float64, file_m, "SPOS", &pathLength, 1);

    WRITESTEPATTRIB(Float64, file_m, "RefPartR", (h5_float64_t *)&RefPartR, 3);
    WRITESTEPATTRIB(Float64, file_m, "centroid", (h5_float64_t *)&centroid, 3);
    WRITESTEPATTRIB(Float64, file_m, "RMSX", (h5_float64_t *)&xsigma, 3);
    WRITESTEPATTRIB(Float64, file_m, "minX", (h5_float64_t *)&rmin, 3);
    WRITESTEPATTRIB(Float64, file_m, "maxX", (h5_float64_t *)&rmax, 3);

    WRITESTEPATTRIB(Float64, file_m, "RefPartP", (h5_float64_t *)&RefPartP, 3);
    WRITESTEPATTRIB(Float64, file_m, "RMSP", (h5_float64_t *)&psigma, 3);
    WRITESTEPATTRIB(Float64, file_m, "minP", (h5_float64_t *)&minP, 3);
    WRITESTEPATTRIB(Float64, file_m, "maxP", (h5_float64_t *)&maxP, 3);

    WRITESTEPATTRIB(Float64, file_m, "#varepsilon", (h5_float64_t *)&vareps, 3);
    WRITESTEPATTRIB(Float64, file_m, "#varepsilon-geom", (h5_float64_t *)&geomvareps, 3);

    WRITESTEPATTRIB(Int64, file_m, "Step", &numSteps_m, 1);
    WRITESTEPATTRIB(Int64, file_m, "LocalTrackStep", &localTrackStep, 1);
    WRITESTEPATTRIB(Int64, file_m, "GlobalTrackStep", &globalTrackStep, 1);

    WRITESTEPATTRIB(Float64, file_m, "#sigma", &sigma, 1);

    WRITESTEPATTRIB(Float64, file_m, "ENERGY", &meanEnergy, 1);
    WRITESTEPATTRIB(Float64, file_m, "dE", &energySpread, 1);

    /// Write particle mass and charge per particle. (Consider making these file attributes.)
    WRITESTEPATTRIB(Float64, file_m, "MASS", &mass, 1);
    WRITESTEPATTRIB(Float64, file_m, "CHARGE", &charge, 1);

    WRITESTEPATTRIB(Int64, file_m, "NumBunch", &numBunch, 1);

    WRITESTEPATTRIB(Int64, file_m, "SteptoLastInj", &SteptoLastInj, 1);

    WRITESTEPATTRIB(Int64, file_m, "LOCAL", &localFrame, 1);

    try {
        h5_float64_t refpr = additionalStepAttributes.at("REFPR");
        h5_float64_t refpt = additionalStepAttributes.at("REFPT");
        h5_float64_t refpz = additionalStepAttributes.at("REFPZ");
        h5_float64_t refr = additionalStepAttributes.at("REFR");
        h5_float64_t reft = additionalStepAttributes.at("REFTHETA");
        h5_float64_t refz = additionalStepAttributes.at("REFZ");
        h5_float64_t azimuth = additionalStepAttributes.at("AZIMUTH");
        h5_float64_t elevation = additionalStepAttributes.at("ELEVATION");

        Vector_t referenceB(additionalStepAttributes.at("B-ref_x"),
                            additionalStepAttributes.at("B-ref_z"),
                            additionalStepAttributes.at("B-ref_y"));
        Vector_t referenceE(additionalStepAttributes.at("E-ref_x"),
                            additionalStepAttributes.at("E-ref_z"),
                            additionalStepAttributes.at("E-ref_y"));
        Vector_t headB(additionalStepAttributes.at("B-head_x"),
                      additionalStepAttributes.at("B-head_z"),
                      additionalStepAttributes.at("B-head_y"));
        Vector_t headE(additionalStepAttributes.at("E-head_x"),
                       additionalStepAttributes.at("E-head_z"),
                       additionalStepAttributes.at("E-head_y"));
        Vector_t tailB(additionalStepAttributes.at("B-tail_x"),
                      additionalStepAttributes.at("B-tail_z"),
                      additionalStepAttributes.at("B-tail_y"));
        Vector_t tailE(additionalStepAttributes.at("E-tail_x"),
                       additionalStepAttributes.at("E-tail_z"),
                       additionalStepAttributes.at("E-tail_y"));

        WRITESTEPATTRIB(Float64, file_m, "REFPR", &refpr, 1);
        WRITESTEPATTRIB(Float64, file_m, "REFPT", &refpt, 1);
        WRITESTEPATTRIB(Float64, file_m, "REFPZ", &refpz, 1);
        WRITESTEPATTRIB(Float64, file_m, "REFR", &refr, 1);
        WRITESTEPATTRIB(Float64, file_m, "REFTHETA", &reft, 1);
        WRITESTEPATTRIB(Float64, file_m, "REFZ", &refz, 1);
        WRITESTEPATTRIB(Float64, file_m, "AZIMUTH", &azimuth, 1);
        WRITESTEPATTRIB(Float64, file_m, "ELEVATION", &elevation, 1);

        WRITESTEPATTRIB(Float64, file_m, "spos-head", &sposHead, 1);
        WRITESTEPATTRIB(Float64, file_m, "spos-ref", &sposRef, 1);
        WRITESTEPATTRIB(Float64, file_m, "spos-tail", &sposTail, 1);

        WRITESTEPATTRIB(Float64, file_m, "B-ref", (h5_float64_t *)&referenceB, 3);
        WRITESTEPATTRIB(Float64, file_m, "E-ref", (h5_float64_t *)&referenceE, 3);
        WRITESTEPATTRIB(Float64, file_m, "B-head", (h5_float64_t *)&headB, 3);
        WRITESTEPATTRIB(Float64, file_m, "E-head", (h5_float64_t *)&headE, 3);
        WRITESTEPATTRIB(Float64, file_m, "B-tail", (h5_float64_t *)&tailB, 3);
        WRITESTEPATTRIB(Float64, file_m, "E-tail", (h5_float64_t *)&tailE, 3);
    } catch (std::out_of_range & m) {
        ERRORMSG(m.what() << endl);

        throw OpalException("H5PartWrapperForPC::wirteStepHeader",
                            "some additional step attribute not found");
    }
}

frey_m's avatar
frey_m committed
432
void H5PartWrapperForPC::writeStepData(PartBunchBase<double, 3>* bunch) {
433 434 435 436 437
    /*
      find particle with ID==0
      and save index in zID
     */

frey_m's avatar
frey_m committed
438
    size_t IDZero = bunch->getLocalNum();
439
    bool found = false;
440
    for(size_t k = 0; k < IDZero; ++ k) {
frey_m's avatar
frey_m committed
441
        if (bunch->ID[k] == 0) {
442
            found = true;
443
            IDZero = k;
444 445 446
        }
    }

frey_m's avatar
frey_m committed
447
    const size_t numLocalParticles = (found? bunch->getLocalNum() - 1: bunch->getLocalNum());
448 449 450 451 452 453 454 455 456 457
    const size_t skipID = IDZero;

    std::vector<char> buffer(numLocalParticles * sizeof(h5_float64_t));
    h5_float64_t *f64buffer = reinterpret_cast<h5_float64_t*>(&buffer[0]);
    h5_int64_t *i64buffer = reinterpret_cast<h5_int64_t*>(&buffer[0]);


    REPORTONERROR(H5PartSetNumParticles(file_m, numLocalParticles));

    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
458
        f64buffer[i] =  bunch->R[i](0);
459
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
460
        f64buffer[i] = bunch->R[i + 1](0);
461

462 463
    WRITEDATA(Float64, file_m, "x", f64buffer);

464
    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
465
        f64buffer[i] =  bunch->R[i](1);
466
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
467
        f64buffer[i] = bunch->R[i + 1](1);
468

469 470
    WRITEDATA(Float64, file_m, "y", f64buffer);

471
    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
472
        f64buffer[i] =  bunch->R[i](2);
473
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
474
        f64buffer[i] = bunch->R[i + 1](2);
475

476 477
    WRITEDATA(Float64, file_m, "z", f64buffer);

478
    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
479
        f64buffer[i] =  bunch->P[i](0);
480
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
481
        f64buffer[i] = bunch->P[i + 1](0);
482

483 484
    WRITEDATA(Float64, file_m, "px", f64buffer);

485
    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
486
        f64buffer[i] =  bunch->P[i](1);
487
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
488
        f64buffer[i] = bunch->P[i + 1](1);
489

490 491
    WRITEDATA(Float64, file_m, "py", f64buffer);

492
    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
493
        f64buffer[i] =  bunch->P[i](2);
494
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
495
        f64buffer[i] = bunch->P[i + 1](2);
496

497 498
    WRITEDATA(Float64, file_m, "pz", f64buffer);

499
    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
500
        f64buffer[i] =  bunch->Q[i];
501
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
502
        f64buffer[i] = bunch->Q[i + 1];
503

504 505
    WRITEDATA(Float64, file_m, "q", f64buffer);

506
    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
507
        f64buffer[i] =  bunch->M[i];
508
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
509
        f64buffer[i] = bunch->M[i + 1];
510

511 512
    WRITEDATA(Float64, file_m, "mass", f64buffer);

513
    for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
514
        i64buffer[i] =  bunch->ID[i];
515
    for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
516
        i64buffer[i] = bunch->ID[i + 1];
517

518
    WRITEDATA(Int64, file_m, "id", i64buffer);
519

520 521
    if (Options::ebDump) {
        for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
522
            f64buffer[i] =  bunch->Ef[i](0);
523
        for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
524
            f64buffer[i] = bunch->Ef[i + 1](0);
525

526 527
        WRITEDATA(Float64, file_m, "Ex", f64buffer);

528
        for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
529
            f64buffer[i] =  bunch->Ef[i](1);
530
        for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
531
            f64buffer[i] = bunch->Ef[i + 1](1);
532

533 534
        WRITEDATA(Float64, file_m, "Ey", f64buffer);

535
        for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
536
            f64buffer[i] =  bunch->Ef[i](2);
537
        for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
538
            f64buffer[i] = bunch->Ef[i + 1](2);
539

540 541
        WRITEDATA(Float64, file_m, "Ez", f64buffer);

542
        for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
543
            f64buffer[i] =  bunch->Bf[i](0);
544
        for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
545
            f64buffer[i] = bunch->Bf[i + 1](0);
546

547
        WRITEDATA(Float64, file_m, "Bx", f64buffer);
548

549
        for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
550
            f64buffer[i] =  bunch->Bf[i](1);
551
        for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
552
            f64buffer[i] = bunch->Bf[i + 1](1);
553

554 555
        WRITEDATA(Float64, file_m, "By", f64buffer);

556
        for(size_t i = 0; i < skipID; ++ i)
frey_m's avatar
frey_m committed
557
            f64buffer[i] =  bunch->Bf[i](2);
558
        for (size_t i = skipID; i < numLocalParticles; ++ i)
frey_m's avatar
frey_m committed
559
            f64buffer[i] = bunch->Bf[i + 1](2);
560

561 562 563 564 565 566
        WRITEDATA(Float64, file_m, "Bz", f64buffer);

    }

    /// Write space charge field map if asked for.
    if(Options::rhoDump) {
frey_m's avatar
frey_m committed
567
        NDIndex<3> idx = bunch->getFieldLayout().getLocalNDIndex();
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
        NDIndex<3> elem;
        REPORTONERROR(H5Block3dSetView(file_m,
                                       idx[0].min(), idx[0].max(),
                                       idx[1].min(), idx[1].max(),
                                       idx[2].min(), idx[2].max()));

        std::unique_ptr<h5_float64_t[]> data(new h5_float64_t[(idx[0].max() + 1)  * (idx[1].max() + 1) * (idx[2].max() + 1)]);

        int ii = 0;
        // h5block uses the fortran convention of storing data:
        // INTEGER, DIMENSION(2,3) :: a
        // => {a(1,1), a(2,1), a(1,2), a(2,2), a(1,3), a(2,3)}
        for(int i = idx[2].min(); i <= idx[2].max(); ++ i) {
            for(int j = idx[1].min(); j <= idx[1].max(); ++ j) {
                for(int k = idx[0].min(); k <= idx[0].max(); ++ k) {
frey_m's avatar
frey_m committed
583
                    data[ii] = bunch->getRho(k, j, i);
584 585 586 587 588 589 590 591
                    ++ ii;
                }
            }
        }
        REPORTONERROR(H5Block3dWriteScalarFieldFloat64(file_m, "rho", data.get()));

        /// Need this to align particles and fields when writing space charge map.
        REPORTONERROR(H5Block3dSetFieldOrigin(file_m, "rho",
frey_m's avatar
frey_m committed
592 593 594
                                              (h5_float64_t)bunch->get_origin()(0),
                                              (h5_float64_t)bunch->get_origin()(1),
                                              (h5_float64_t)bunch->get_origin()(2)));
595 596

        REPORTONERROR(H5Block3dSetFieldSpacing(file_m, "rho",
frey_m's avatar
frey_m committed
597 598 599
                                               (h5_float64_t)bunch->get_hr()(0),
                                               (h5_float64_t)bunch->get_hr()(1),
                                               (h5_float64_t)bunch->get_hr()(2)));
600
    }
winklehner_d's avatar
-DW  
winklehner_d committed
601
}