diff --git a/src/Algorithms/ParallelCyclotronTracker.cpp b/src/Algorithms/ParallelCyclotronTracker.cpp index 253773b85225819c5e1d9d64d816c9cc1fc7dd94..06496a758e6a7b634c9715a15f19cdd5150f045c 100644 --- a/src/Algorithms/ParallelCyclotronTracker.cpp +++ b/src/Algorithms/ParallelCyclotronTracker.cpp @@ -5,7 +5,7 @@ // Copyright (c) 2007 - 2014, Jianjun Yang, Andreas Adelmann and Matthias Toggweiler, // Paul Scherrer Institut, Villigen PSI, Switzerland // Copyright (c) 2014, Daniel Winklehner, MIT, Cambridge, MA, USA -// Copyright (c) 2012 - 2022, Paul Scherrer Institut, Villigen PSI, Switzerland +// Copyright (c) 2012 - 2023, Paul Scherrer Institut, Villigen PSI, Switzerland // All rights reserved // // Implemented as part of the PhD thesis @@ -161,8 +161,6 @@ ParallelCyclotronTracker::ParallelCyclotronTracker(const Beamline& beamline, * */ ParallelCyclotronTracker::~ParallelCyclotronTracker() { - if (bgf_m) - lossDs_m->save(); for (Component* component : myElements) { delete(component); } @@ -1234,6 +1232,9 @@ void ParallelCyclotronTracker::execute() { for (auto fd : FieldDimensions) { ((fd->second).second)->finalise(); } + if (bgf_m && lossDs_m) { + lossDs_m->save(); + } *gmsg << "* ------------------------------------------------------------------------ *" << endl; } diff --git a/src/Classic/Structure/LossDataSink.cpp b/src/Classic/Structure/LossDataSink.cpp index ca62f49ad38f797dfc0f07fec26d74ba3eaf4ff4..853fdc7888b647008e43f81b945829d97954aadf 100644 --- a/src/Classic/Structure/LossDataSink.cpp +++ b/src/Classic/Structure/LossDataSink.cpp @@ -24,6 +24,7 @@ #include "Utilities/GeneralClassicException.h" #include "Utilities/Options.h" #include "Utilities/Util.h" + #include "Utility/IpplInfo.h" #include <boost/filesystem.hpp> @@ -115,7 +116,7 @@ extern Inform* gmsg; } namespace { - void f64transform(const std::vector<OpalParticle> &particles, + void f64transform(const std::vector<OpalParticle>& particles, unsigned int startIdx, unsigned int numParticles, h5_float64_t *buffer, @@ -124,7 +125,7 @@ namespace { particles.begin() + startIdx + numParticles, buffer, select); } - void i64transform(const std::vector<OpalParticle> &particles, + void i64transform(const std::vector<OpalParticle>& particles, unsigned int startIdx, unsigned int numParticles, h5_int64_t *buffer, @@ -134,7 +135,7 @@ namespace { buffer, select); } - void cminmax(double &min, double &max, double val) { + void cminmax(double& min, double& max, double val) { if (-val > min) { min = -val; } else if (val > max) { @@ -168,7 +169,7 @@ SetStatistics::SetStatistics(): fac_m(0.0) { } -LossDataSink::LossDataSink(std::string outfn, bool hdf5Save, CollectionType collectionType): +LossDataSink::LossDataSink(const std::string& outfn, bool hdf5Save, CollectionType collectionType): h5hut_mode_m(hdf5Save), H5file_m(0), outputName_m(outfn), @@ -193,7 +194,7 @@ LossDataSink::LossDataSink(std::string outfn, bool hdf5Save, CollectionType coll } } -LossDataSink::LossDataSink(const LossDataSink &rhs): +LossDataSink::LossDataSink(const LossDataSink& rhs): h5hut_mode_m(rhs.h5hut_mode_m), H5file_m(rhs.H5file_m), outputName_m(rhs.outputName_m), @@ -227,7 +228,6 @@ void LossDataSink::openH5(h5_int32_t mode) { void LossDataSink::writeHeaderH5() { - // Write file attributes to describe phase space to H5 file. std::stringstream OPAL_version; OPAL_version << OPAL_PROJECT_NAME << " " << OPAL_PROJECT_VERSION << " # git rev. " << Util::getGitRevision(); @@ -297,12 +297,11 @@ void LossDataSink::writeHeaderASCII() { } } -void LossDataSink::addReferenceParticle(const Vector_t &x, - const Vector_t &p, +void LossDataSink::addReferenceParticle(const Vector_t& x, + const Vector_t& p, double time, double spos, - long long globalTrackStep - ) { + long long globalTrackStep) { RefPartR_m.push_back(x); RefPartP_m.push_back(p); globalTrackStep_m.push_back((h5_int64_t)globalTrackStep); @@ -310,7 +309,8 @@ void LossDataSink::addReferenceParticle(const Vector_t &x, refTime_m.push_back(time); } -void LossDataSink::addParticle(const OpalParticle &particle, const boost::optional<std::pair<int, short>> &turnBunchNumPair) { +void LossDataSink::addParticle(const OpalParticle& particle, + const boost::optional<std::pair<int, short>>& turnBunchNumPair) { if (turnBunchNumPair) { if (!particles_m.empty() && turnNumber_m.empty()) { throw GeneralClassicException("LossDataSink::addParticle", @@ -517,7 +517,6 @@ void LossDataSink::saveH5(unsigned int setIdx) { } void LossDataSink::saveASCII() { - /* ASCII output */ diff --git a/src/Classic/Structure/LossDataSink.h b/src/Classic/Structure/LossDataSink.h index 306e448d195c78ec55af059b718a7c670b434675..d9e1ad61e49b6e9d92fa0dac0eaa62bd26320f80 100644 --- a/src/Classic/Structure/LossDataSink.h +++ b/src/Classic/Structure/LossDataSink.h @@ -18,30 +18,29 @@ #ifndef LOSSDATASINK_H_ #define LOSSDATASINK_H_ -////////////////////////////////////////////////////////////// -#include "Algorithms/Vektor.h" -#include "Algorithms/OpalParticle.h" #include "AbsBeamline/ElementBase.h" #include "AbstractObjects/OpalData.h" +#include "Algorithms/Vektor.h" +#include "Algorithms/OpalParticle.h" + +#include "H5hut.h" #include <boost/optional.hpp> -#include <string> #include <fstream> -#include <vector> #include <functional> #include <set> - -#include "H5hut.h" +#include <string> +#include <vector> struct SetStatistics { SetStatistics(); std::string outputName_m; double spos_m; - double refTime_m; // ns - double tmean_m; // ns - double trms_m; // ns + double refTime_m; + double tmean_m; + double trms_m; unsigned long nTotal_m; Vector_t RefPartR_m; Vector_t RefPartP_m; @@ -79,26 +78,26 @@ enum class CollectionType: unsigned short { - h5hut_mode_m defines h5hut or ASCII */ class LossDataSink { - public: +public: LossDataSink() = default; - LossDataSink(std::string outfn, bool hdf5Save, CollectionType = CollectionType::TEMPORAL); + LossDataSink(const std::string& outfn, bool hdf5Save, CollectionType = CollectionType::TEMPORAL); - LossDataSink(const LossDataSink &rsh); + LossDataSink(const LossDataSink& rsh); ~LossDataSink() noexcept(false); bool inH5Mode() { return h5hut_mode_m;} void save(unsigned int numSets = 1, OpalData::OpenMode openMode = OpalData::OpenMode::UNDEFINED); - void addReferenceParticle(const Vector_t &x, - const Vector_t &p, + void addReferenceParticle(const Vector_t& x, + const Vector_t& p, double time, double spos, long long globalTrackStep); - void addParticle(const OpalParticle &, const boost::optional<std::pair<int, short int>> &turnBunchNumPair = boost::none); + void addParticle(const OpalParticle&, const boost::optional<std::pair<int, short int>>& turnBunchNumPair = boost::none); size_t size() const; @@ -106,7 +105,7 @@ class LossDataSink { private: void openASCII() { - if(Ippl::myNode() == 0) { + if (Ippl::myNode() == 0) { os_m.open(fileName_m.c_str(), std::ios::out); } } @@ -138,7 +137,6 @@ private: void splitSets(unsigned int numSets); SetStatistics computeSetStatistics(unsigned int setIdx); - // filename without extension std::string fileName_m; // write either in ASCII or H5hut format @@ -150,6 +148,7 @@ private: /// used to write out data in H5hut mode h5_file_t H5file_m; + // filename without extension std::string outputName_m; /// Current record, or time step, of H5 file.