OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
LBalWriter.cpp
Go to the documentation of this file.
1 //
2 // Class LBalWriter
3 // This class writes a SDDS file with MPI load balancing information.
4 //
5 // Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // Christof Metzger-Kraus, Open Sourcerer
7 // All rights reserved
8 //
9 // This file is part of OPAL.
10 //
11 // OPAL is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18 //
19 #include "Structure/LBalWriter.h"
20 
22 #ifdef ENABLE_AMR
24 #endif
26 #include "OPALconfig.h"
27 #include "Physics/Units.h"
28 #include "Utilities/Timer.h"
29 #include "Utilities/Util.h"
30 
31 
32 LBalWriter::LBalWriter(const std::string& fname, bool restart)
33  : SDDSWriter(fname, restart)
34 { }
35 
36 
37 #ifdef ENABLE_AMR
39 #else
41 #endif
42  if (this->hasColumns()) {
43  return;
44  }
45 
46  columns_m.addColumn("t", "double", "ns", "Time");
47 
48  for (int p = 0; p < Ippl::getNodes(); ++p) {
49  std::stringstream tmp1;
50  tmp1 << "\"processor-" << p << "\"";
51 
52  std::stringstream tmp2;
53  tmp2 << "Number of particles of processor " << p;
54 
55  columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
56  }
57 
58 #ifdef ENABLE_AMR
59  if ( AmrPartBunch* amrbeam = dynamic_cast<AmrPartBunch*>(beam) ) {
60 
61  int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
62 
63  for (int lev = 0; lev < nLevel; ++lev) {
64  std::stringstream tmp1;
65  tmp1 << "\"level-" << lev << "\"";
66 
67  std::stringstream tmp2;
68  tmp2 << "Number of particles at level " << lev;
69  columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
70  }
71  }
72 #endif
73 
74  if ( mode_m == std::ios::app ) {
75  return;
76  }
77 
78  OPALTimer::Timer simtimer;
79  std::string dateStr(simtimer.date());
80  std::string timeStr(simtimer.time());
81 
82  std::stringstream ss;
83  ss << "Processor statistics '"
84  << OpalData::getInstance()->getInputFn() << "' "
85  << dateStr << "" << timeStr;
86 
87  this->addDescription(ss.str(), "lbal parameters");
88 
89  this->addDefaultParameters();
90 
91  this->addInfo("ascii", 1);
92 }
93 
95 #ifdef ENABLE_AMR
96  if ( AmrPartBunch* amrbeam = dynamic_cast<AmrPartBunch*>(beam) ) {
97  amrbeam->gatherLevelStatistics();
98  }
99 #endif
100  if ( Ippl::myNode() != 0 ) {
101  return;
102  }
103 #ifdef ENABLE_AMR
104  this->fillHeader(beam);
105 #else
106  this->fillHeader();
107 #endif
108 
109  this->open();
110 
111  this->writeHeader();
112 
113  columns_m.addColumnValue("t", beam->getT() * Units::s2ns);
114 
115  size_t nProcs = Ippl::getNodes();
116  for (size_t p = 0; p < nProcs; ++ p) {
117  std::stringstream ss;
118  ss << "\"processor-" << p << "\"";
119  columns_m.addColumnValue(ss.str(), beam->getLoadBalance(p));
120  }
121 
122 #ifdef ENABLE_AMR
123  if ( AmrPartBunch* amrbeam = dynamic_cast<AmrPartBunch*>(beam) ) {
124  int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
125  for (int lev = 0; lev < nLevel; ++lev) {
126  std::stringstream ss;
127  ss << "\"level-" << lev << "\"";
128  columns_m.addColumnValue(ss.str(), amrbeam->getLevelStatistics(lev));
129  }
130  }
131 #endif
132 
133  this->writeRow();
134 
135  this->close();
136 }
static OpalData * getInstance()
Definition: OpalData.cpp:196
void open()
Definition: SDDSWriter.cpp:132
bool hasColumns() const
Definition: SDDSWriter.h:200
void addDescription(const std::string &text, const std::string &content)
Definition: SDDSWriter.h:159
LBalWriter(const std::string &fname, bool restart)
Definition: LBalWriter.cpp:32
static int myNode()
Definition: IpplInfo.cpp:691
SDDSColumnSet columns_m
Definition: SDDSWriter.h:122
void fillHeader(PartBunchBase< double, 3 > *beam)
Definition: LBalWriter.cpp:38
std::string getInputFn()
get opals input filename
Definition: OpalData.cpp:670
void addDefaultParameters()
Definition: SDDSWriter.cpp:209
static int getNodes()
Definition: IpplInfo.cpp:670
double getT() const
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition: SDDSWriter.h:178
void writeRow()
Definition: SDDSWriter.h:185
size_t getLoadBalance(int p) const
void writeHeader()
Write SDDS header.
Definition: SDDSWriter.cpp:149
constexpr double s2ns
Definition: Units.h:44
void addColumn(const std::string &name, const std::string &type, const std::string &unit, const std::string &desc, std::ios_base::fmtflags flags=std::ios_base::scientific, unsigned short precision=15)
void write(PartBunchBase< double, 3 > *beam) override
Definition: LBalWriter.cpp:94
std::string time() const
Return time.
Definition: Timer.cpp:42
std::string date() const
Return date.
Definition: Timer.cpp:35
void close()
Definition: SDDSWriter.cpp:142
void addColumnValue(const std::string &name, const T &val)
Definition: SDDSColumnSet.h:63
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition: SDDSWriter.h:120