OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 
23 #include "Utilities/Timer.h"
24 
25 #include <sstream>
26 
28 
29 GridLBalWriter::GridLBalWriter(const std::string& fname, bool restart)
30  : SDDSWriter(fname, restart)
31 { }
32 
33 
35 
36  if (this->hasColumns()) {
37  return;
38  }
39 
40  columns_m.addColumn("t", "double", "ns", "Time");
41 
42  const AmrPartBunch* amrbeam = dynamic_cast<const AmrPartBunch*>(beam);
43 
44  if ( !amrbeam )
45  throw OpalException("GridLBalWriter::fillHeader()",
46  "Can not write grid load balancing for non-AMR runs.");
47  int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
48 
49  for (int lev = 0; lev < nLevel; ++lev) {
50  std::stringstream tmp1;
51  tmp1 << "\"level-" << lev << "\"";
52 
53  std::stringstream tmp2;
54  tmp2 << "Number of boxes at level " << lev;
55 
56  columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
57  }
58 
59  for (int p = 0; p < Ippl::getNodes(); ++p) {
60  std::stringstream tmp1;
61  tmp1 << "\"processor-" << p << "\"";
62 
63  std::stringstream tmp2;
64  tmp2 << "Number of grid points per processor " << p;
65 
66  columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
67  }
68 
69  if ( mode_m == std::ios::app )
70  return;
71 
72  OPALTimer::Timer simtimer;
73 
74  std::string dateStr(simtimer.date());
75  std::string timeStr(simtimer.time());
76 
77  std::stringstream ss;
78  ss << "Grid load balancing statistics '"
79  << OpalData::getInstance()->getInputFn() << "' "
80  << dateStr << "" << timeStr;
81 
82  this->addDescription(ss.str(), "grid lbal parameters");
83 
84  this->addDefaultParameters();
85 
86  this->addInfo("ascii", 1);
87 }
88 
89 
91  AmrPartBunch* amrbeam = dynamic_cast<AmrPartBunch*>(beam);
92 
93  if ( !amrbeam )
94  throw OpalException("GridLBalWriter::write()",
95  "Can not write grid load balancing for non-AMR runs.");
96 
97  std::map<int, long> gridPtsPerCore;
98  std::vector<int> gridsPerLevel;
99 
100  amrbeam->getAmrObject()->getGridStatistics(gridPtsPerCore, gridsPerLevel);
101 
102  if ( Ippl::myNode() != 0 )
103  return;
104 
105  this->fillHeader(beam);
106 
107  this->open();
108 
109  this->writeHeader();
110 
111  columns_m.addColumnValue("t", beam->getT() * 1e9); // 1
112 
113  int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
114 
115  for (int lev = 0; lev < nLevel; ++lev) {
116  std::stringstream ss;
117  ss << "\"level-" << lev << "\"";
118  // we need to cast due to boost::variant
119  columns_m.addColumnValue(ss.str(), toString(gridsPerLevel[lev]));
120  }
121 
122  int nProcs = Ippl::getNodes();
123  for (int p = 0; p < nProcs; ++p) {
124  std::stringstream ss;
125  ss << "\"processor-" << p << "\"";
126  // we need to cast due to boost::variant
127  columns_m.addColumnValue(ss.str(), toString(gridPtsPerCore[p]));
128  }
129 
130  this->writeRow();
131 
132  this->close();
133 }
double getT() const
std::string getInputFn()
get opals input filename
Definition: OpalData.cpp:654
static OpalData * getInstance()
Definition: OpalData.cpp:195
virtual void getGridStatistics(std::map< int, long > &gridPtsPerCore, std::vector< int > &gridsPerLevel) const =0
const AmrObject * getAmrObject() const
Definition: AmrPartBunch.h:82
void write(PartBunchBase< double, 3 > *beam)
GridLBalWriter(const std::string &fname, bool restart)
void fillHeader(const PartBunchBase< double, 3 > *beam)
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 addColumnValue(const std::string &name, const T &val)
Definition: SDDSColumnSet.h:63
std::string toString(const T &val)
Definition: SDDSWriter.h:198
SDDSColumnSet columns_m
Definition: SDDSWriter.h:126
bool hasColumns() const
Definition: SDDSWriter.h:207
void addDefaultParameters()
Definition: SDDSWriter.cpp:211
void addDescription(const std::string &text, const std::string &content)
Definition: SDDSWriter.h:164
void open()
Definition: SDDSWriter.cpp:134
void writeHeader()
Write SDDS header.
Definition: SDDSWriter.cpp:151
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition: SDDSWriter.h:124
void close()
Definition: SDDSWriter.cpp:144
void writeRow()
Definition: SDDSWriter.h:192
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition: SDDSWriter.h:185
The base class for all OPAL exceptions.
Definition: OpalException.h:28
std::string date() const
Return date.
Definition: Timer.cpp:35
std::string time() const
Return time.
Definition: Timer.cpp:42
static int getNodes()
Definition: IpplInfo.cpp:670
static int myNode()
Definition: IpplInfo.cpp:691