OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
GridLBalWriter.cpp
Go to the documentation of this file.
1 //
2 // Class GridLBalWriter
3 // This class writes a SDDS file with AMR grid load balancing information.
4 //
5 // Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // Implemented as part of the PhD thesis
9 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #include "GridLBalWriter.h"
22 
24 #include "Physics/Units.h"
25 #include "Utilities/Timer.h"
26 
27 #include <sstream>
28 
29 
30 GridLBalWriter::GridLBalWriter(const std::string& fname, bool restart)
31  : SDDSWriter(fname, restart)
32 { }
33 
34 
36 
37  if (this->hasColumns()) {
38  return;
39  }
40 
41  columns_m.addColumn("t", "double", "ns", "Time");
42 
43  const AmrPartBunch* amrbeam = dynamic_cast<const AmrPartBunch*>(beam);
44 
45  if ( !amrbeam )
46  throw OpalException("GridLBalWriter::fillHeader()",
47  "Can not write grid load balancing for non-AMR runs.");
48  int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
49 
50  for (int lev = 0; lev < nLevel; ++lev) {
51  std::stringstream tmp1;
52  tmp1 << "\"level-" << lev << "\"";
53 
54  std::stringstream tmp2;
55  tmp2 << "Number of boxes at level " << lev;
56 
57  columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
58  }
59 
60  for (int p = 0; p < Ippl::getNodes(); ++p) {
61  std::stringstream tmp1;
62  tmp1 << "\"processor-" << p << "\"";
63 
64  std::stringstream tmp2;
65  tmp2 << "Number of grid points per processor " << p;
66 
67  columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
68  }
69 
70  if ( mode_m == std::ios::app )
71  return;
72 
73  OPALTimer::Timer simtimer;
74 
75  std::string dateStr(simtimer.date());
76  std::string timeStr(simtimer.time());
77 
78  std::stringstream ss;
79  ss << "Grid load balancing statistics '"
80  << OpalData::getInstance()->getInputFn() << "' "
81  << dateStr << "" << timeStr;
82 
83  this->addDescription(ss.str(), "grid lbal parameters");
84 
85  this->addDefaultParameters();
86 
87  this->addInfo("ascii", 1);
88 }
89 
90 
92  AmrPartBunch* amrbeam = dynamic_cast<AmrPartBunch*>(beam);
93 
94  if ( !amrbeam )
95  throw OpalException("GridLBalWriter::write()",
96  "Can not write grid load balancing for non-AMR runs.");
97 
98  std::map<int, long> gridPtsPerCore;
99  std::vector<int> gridsPerLevel;
100 
101  amrbeam->getAmrObject()->getGridStatistics(gridPtsPerCore, gridsPerLevel);
102 
103  if ( Ippl::myNode() != 0 )
104  return;
105 
106  this->fillHeader(beam);
107 
108  this->open();
109 
110  this->writeHeader();
111 
112  columns_m.addColumnValue("t", beam->getT() * Units::s2ns); // 1
113 
114  int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
115 
116  for (int lev = 0; lev < nLevel; ++lev) {
117  std::stringstream ss;
118  ss << "\"level-" << lev << "\"";
119  // we need to cast due to boost::variant
120  columns_m.addColumnValue(ss.str(), toString(gridsPerLevel[lev]));
121  }
122 
123  int nProcs = Ippl::getNodes();
124  for (int p = 0; p < nProcs; ++p) {
125  std::stringstream ss;
126  ss << "\"processor-" << p << "\"";
127  // we need to cast due to boost::variant
128  columns_m.addColumnValue(ss.str(), toString(gridPtsPerCore[p]));
129  }
130 
131  this->writeRow();
132 
133  this->close();
134 }
static OpalData * getInstance()
Definition: OpalData.cpp:196
void open()
Definition: SDDSWriter.cpp:132
void write(PartBunchBase< double, 3 > *beam)
bool hasColumns() const
Definition: SDDSWriter.h:200
void addDescription(const std::string &text, const std::string &content)
Definition: SDDSWriter.h:159
virtual void getGridStatistics(std::map< int, long > &gridPtsPerCore, std::vector< int > &gridsPerLevel) const =0
static int myNode()
Definition: IpplInfo.cpp:691
SDDSColumnSet columns_m
Definition: SDDSWriter.h:122
void fillHeader(const PartBunchBase< double, 3 > *beam)
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
The base class for all OPAL exceptions.
Definition: OpalException.h:28
std::string toString(const T &val)
Definition: SDDSWriter.h:191
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)
std::string time() const
Return time.
Definition: Timer.cpp:42
std::string date() const
Return date.
Definition: Timer.cpp:35
const AmrObject * getAmrObject() const
Definition: AmrPartBunch.h:80
void close()
Definition: SDDSWriter.cpp:142
void addColumnValue(const std::string &name, const T &val)
Definition: SDDSColumnSet.h:63
GridLBalWriter(const std::string &fname, bool restart)
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition: SDDSWriter.h:120