OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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//
22
25#include "Physics/Units.h"
27#include "Utilities/Timer.h"
28
29#include <sstream>
30
31
32GridLBalWriter::GridLBalWriter(const std::string& fname, bool restart)
33 : SDDSWriter(fname, restart)
34{ }
35
37
38 if (this->hasColumns()) {
39 return;
40 }
41
42 columns_m.addColumn("t", "double", "ns", "Time");
43
44 const AmrPartBunch* amrbeam = dynamic_cast<const AmrPartBunch*>(beam);
45
46 if ( !amrbeam )
47 throw OpalException("GridLBalWriter::fillHeader()",
48 "Can not write grid load balancing for non-AMR runs.");
49 int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
50
51 for (int lev = 0; lev < nLevel; ++lev) {
52 std::stringstream tmp1;
53 tmp1 << "\"level-" << lev << "\"";
54
55 std::stringstream tmp2;
56 tmp2 << "Number of boxes at level " << lev;
57
58 columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
59 }
60
61 for (int p = 0; p < Ippl::getNodes(); ++p) {
62 std::stringstream tmp1;
63 tmp1 << "\"processor-" << p << "\"";
64
65 std::stringstream tmp2;
66 tmp2 << "Number of grid points per processor " << p;
67
68 columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
69 }
70
71 if ( mode_m == std::ios::app )
72 return;
73
74 OPALTimer::Timer simtimer;
75
76 std::string dateStr(simtimer.date());
77 std::string timeStr(simtimer.time());
78
79 std::stringstream ss;
80 ss << "Grid load balancing statistics '"
81 << OpalData::getInstance()->getInputFn() << "' "
82 << dateStr << "" << timeStr;
83
84 this->addDescription(ss.str(), "grid lbal parameters");
85
87
88 this->addInfo("ascii", 1);
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 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 columns_m.addColumnValue(ss.str(), toString(gridPtsPerCore[p]));
127 }
128
129 this->writeRow();
130
131 this->close();
132}
constexpr double s2ns
Definition Units.h:44
double getT() const
std::string getInputFn()
get opals input filename
Definition OpalData.cpp:670
static OpalData * getInstance()
Definition OpalData.cpp:196
virtual void getGridStatistics(std::map< int, long > &gridPtsPerCore, std::vector< int > &gridsPerLevel) const =0
const AmrObject * getAmrObject() const
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)
std::string toString(const T &val)
Definition SDDSWriter.h:191
SDDSColumnSet columns_m
Definition SDDSWriter.h:122
bool hasColumns() const
Definition SDDSWriter.h:200
void addDefaultParameters()
void addDescription(const std::string &text, const std::string &content)
Definition SDDSWriter.h:159
void writeHeader()
Write SDDS header.
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition SDDSWriter.h:120
void writeRow()
Definition SDDSWriter.h:185
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition SDDSWriter.h:178
The base class for all OPAL exceptions.
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