Commit e1b19431 authored by snuverink_j's avatar snuverink_j
Browse files

move openMode from Options to OpalData and pass as argument for LossDataSink

parent 429c45c9
......@@ -103,21 +103,21 @@ struct OpalDataImpl {
// dump frequency as found in restart file
int restart_dump_freq_m;
/// Mode for writing files
OpalData::OPENMODE openMode_m = OpalData::OPENMODE::WRITE;
// last step of a run
int last_step_m;
bool hasBunchAllocated_m;
bool hasDataSinkAllocated_m;
// The particle bunch to be tracked.
PartBunchBase<double, 3> *bunch_m;
bool hasDataSinkAllocated_m;
DataSink *dataSink_m;
bool hasSLBunchAllocated_m;
// The particle bunch to be tracked.
EnvelopeBunch *slbunch_m;
......@@ -374,6 +374,14 @@ int OpalData::getRestartDumpFreq() const {
return p->restart_dump_freq_m;
}
void OpalData::setOpenMode(OPENMODE openMode) {
p->openMode_m = openMode;
}
OpalData::OPENMODE OpalData::getOpenMode() const {
return p->openMode_m;
}
void OpalData::setLastStep(const int &step) {
p->last_step_m = step;
}
......
......@@ -65,6 +65,13 @@ public:
~OpalData();
/// Enum for writing to files
enum class OPENMODE {
WRITE,
APPEND,
UNDEFINED
};
/// reset object for consecutive runs
void reset();
......@@ -201,6 +208,9 @@ public:
/// get the dump frequency as found in restart file
int getRestartDumpFreq() const;
void setOpenMode(OPENMODE openMode);
OPENMODE getOpenMode() const;
/// set the last step in a run for possible follow-up run
void setLastStep(const int &step);
......
......@@ -48,7 +48,8 @@ OrbitThreader::OrbitThreader(const PartData &ref,
auto opal = OpalData::getInstance();
if (Ippl::myNode() == 0 && !opal->isOptimizerRun()) {
std::string fileName = "data/" + OpalData::getInstance()->getInputBasename() + "_DesignPath.dat";
if (Options::openMode == Options::WRITE || !boost::filesystem::exists(fileName)) {
if (OpalData::getInstance()->getOpenMode() == OpalData::OPENMODE::WRITE ||
!boost::filesystem::exists(fileName)) {
logger_m.open(fileName);
logger_m << "#"
<< std::setw(17) << "1 - s"
......
......@@ -207,7 +207,7 @@ void ParallelTTracker::execute() {
evenlyDistributeParticles();
if (OpalData::getInstance()->hasPriorTrack() || OpalData::getInstance()->inRestartRun()) {
Options::openMode = Options::APPEND;
OpalData::getInstance()->setOpenMode(OpalData::OPENMODE::APPEND);
}
prepareSections();
......
......@@ -199,7 +199,7 @@ void ThickTracker::execute() {
if ( OpalData::getInstance()->hasPriorTrack() ||
OpalData::getInstance()->inRestartRun() )
{
Options::openMode = Options::APPEND;
OpalData::getInstance()->setOpenMode(OpalData::OPENMODE::APPEND);
}
prepareSections();
......
......@@ -32,8 +32,6 @@
#include <fstream>
#include <memory>
extern Inform *gmsg;
using namespace std;
// Class Monitor
......@@ -121,14 +119,13 @@ bool Monitor::applyToReferenceParticle(const Vector_t &R,
RefPartBunch_m->P[i], RefPartBunch_m->ID[i],
time, 0);
}
Options::OPENMODE mode = Options::openMode;
if (numPassages_m > 0) {
Options::openMode = Options::APPEND;
}
lossDs_m->save();
OpalData::OPENMODE openMode;
if (numPassages_m > 0) {
Options::openMode = mode;
openMode = OpalData::OPENMODE::APPEND;
} else {
openMode = OpalData::getInstance()->getOpenMode();
}
lossDs_m->save(1, openMode);
}
++ numPassages_m;
......@@ -153,7 +150,8 @@ void Monitor::initialise(PartBunchBase<double, 3> *bunch, double &startField, do
currentPosition = bunch->get_sPos();
}
if (Options::openMode == Options::WRITE || currentPosition < startField) {
if (OpalData::getInstance()->getOpenMode() == OpalData::OPENMODE::WRITE ||
currentPosition < startField) {
namespace fs = boost::filesystem;
fs::path lossFileName = fs::path(filename_m + ".h5");
......
......@@ -230,13 +230,12 @@ int PluginElement::checkPoint(const double &x, const double &y) const {
}
void PluginElement::save() {
Options::OPENMODE mode = Options::openMode;
OpalData::OPENMODE openMode;
if (numPassages_m > 0) {
Options::openMode = Options::APPEND;
}
lossDs_m->save();
if (numPassages_m > 0) {
Options::openMode = mode;
openMode = OpalData::OPENMODE::APPEND;
} else {
openMode = OpalData::getInstance()->getOpenMode();
}
lossDs_m->save(1, openMode);
numPassages_m++;
}
\ No newline at end of file
......@@ -247,18 +247,22 @@ void LossDataSink::addParticle(const Vector_t &x, const Vector_t &p, const size_
bunchNum_m.push_back(bunchNum);
}
void LossDataSink::save(unsigned int numSets) {
void LossDataSink::save(unsigned int numSets, OpalData::OPENMODE openMode) {
if (element_m == std::string("")) return;
if (hasNoParticlesToDump()) return;
if (openMode == OpalData::OPENMODE::UNDEFINED) {
openMode = OpalData::getInstance()->getOpenMode();
}
namespace fs = boost::filesystem;
if (h5hut_mode_m) {
if (!Options::enableHDF5) return;
fn_m = element_m + std::string(".h5");
INFOMSG(level2 << "Save " << fn_m << endl);
if (Options::openMode == Options::WRITE || !fs::exists(fn_m)) {
if (openMode == OpalData::OPENMODE::WRITE || !fs::exists(fn_m)) {
openH5();
writeHeaderH5();
} else {
......@@ -275,7 +279,7 @@ void LossDataSink::save(unsigned int numSets) {
else {
fn_m = element_m + std::string(".loss");
INFOMSG(level2 << "Save " << fn_m << endl);
if (Options::openMode == Options::WRITE || !fs::exists(fn_m)) {
if (openMode == OpalData::OPENMODE::WRITE || !fs::exists(fn_m)) {
openASCII();
writeHeaderASCII();
} else {
......@@ -335,7 +339,7 @@ void LossDataSink::saveH5(unsigned int setIdx) {
nLoc = startSet_m[setIdx + 1] - startSet_m[setIdx];
}
std::unique_ptr<size_t[]> locN(new size_t[Ippl::getNodes()]);
std::unique_ptr<size_t[]> locN(new size_t[Ippl::getNodes()]);
std::unique_ptr<size_t[]> globN(new size_t[Ippl::getNodes()]);
for(int i = 0; i < Ippl::getNodes(); i++) {
......
......@@ -9,6 +9,7 @@
#include "Utility/IpplInfo.h"
#include "Algorithms/Vektor.h"
#include "AbsBeamline/ElementBase.h"
#include "AbstractObjects/OpalData.h"
#include <string>
#include <fstream>
......@@ -69,7 +70,7 @@ class LossDataSink {
bool inH5Mode() { return h5hut_mode_m;}
void save(unsigned int numSets = 1);
void save(unsigned int numSets = 1, OpalData::OPENMODE openMode = OpalData::OPENMODE::UNDEFINED);
void addReferenceParticle(const Vector_t &x,
const Vector_t &p,
......@@ -102,6 +103,7 @@ private:
void writeHeaderASCII() {
if(Ippl::myNode() == 0) {
//FIXME Issue #45 (Cyclotron units)
os_m << "# Element " << element_m << " x (mm), y (mm), z (mm), px ( ), py ( ), pz ( ), id";
if (time_m.size() != 0) {
os_m << ", turn, bunchNumber, time (ns) ";
......
......@@ -7,11 +7,6 @@ namespace Options {
BUNCH_MEAN=1,
REFERENCE=2
};
enum OPENMODE {
WRITE,
APPEND
};
}
#endif
\ No newline at end of file
......@@ -25,9 +25,6 @@ namespace Options {
bool writeBendTrajectories = false;
OPENMODE openMode = WRITE;
// The global program options.
bool mtrace = false;
bool psDumpEachTurn = false;
......
......@@ -66,8 +66,6 @@ namespace Options {
extern bool writeBendTrajectories;
extern OPENMODE openMode;
extern bool idealized;
/// Trace flag.
......
......@@ -433,7 +433,8 @@ void OpalBeamline::plot3DLattice() {
std::ofstream gpl;
std::string fileName = "data/" + OpalData::getInstance()->getInputBasename() + "_ElementPositions.gpl";
if (Options::openMode == Options::APPEND && boost::filesystem::exists(fileName)) {
if (OpalData::getInstance()->getOpenMode() == OpalData::OPENMODE::APPEND &&
boost::filesystem::exists(fileName)) {
gpl.open(fileName, std::ios_base::app);
} else {
gpl.open(fileName);
......@@ -473,7 +474,8 @@ void OpalBeamline::save3DLattice() {
std::ofstream pos;
std::string fileName = "data/" + OpalData::getInstance()->getInputBasename() + "_ElementPositions.txt";
if (Options::openMode == Options::APPEND && boost::filesystem::exists(fileName)) {
if (OpalData::getInstance()->getOpenMode() == OpalData::OPENMODE::APPEND &&
boost::filesystem::exists(fileName)) {
pos.open(fileName, std::ios_base::app);
} else {
pos.open(fileName);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment