OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
PeakFinder.cpp
Go to the documentation of this file.
1 #include "PeakFinder.h"
2 
3 #include <algorithm>
4 #include <cmath>
5 #include <iterator>
6 
8 #include "Ippl.h"
9 
10 PeakFinder::PeakFinder(std::string elem, double min,
11  double max, double binWidth, bool singlemode)
12  : element_m(elem)
13  , binWidth_m(binWidth)
14  , min_m(min)
15  , max_m(max)
16  , turn_m(0)
17  , peakRadius_m(0.0)
18  , registered_m(0)
19  , singlemode_m(singlemode)
20  , first_m(true)
21  , finished_m(false)
22 {
23  if (min_m > max_m) {
24  std::swap(min_m, max_m);
25  }
26  // calculate bins, round up so that histogram is large enough (add one for safety)
27  nBins_m = static_cast<unsigned int>(std::ceil(( max_m - min_m ) / binWidth_m)) + 1;
28 }
29 
30 
32 
33  double radius = std::hypot(R(0),R(1));
34  radius_m.push_back(radius);
35 
36  peakRadius_m += radius;
37  ++registered_m;
38 }
39 
40 
41 void PeakFinder::evaluate(const int& turn) {
42 
43  if ( first_m ) {
44  turn_m = turn;
45  first_m = false;
46  }
47 
48  if ( turn_m != turn ) {
49  finished_m = true;
50  }
51 
52  bool globFinished = false;
53 
54  if ( !singlemode_m )
55  allreduce(finished_m, globFinished, 1, std::logical_and<bool>());
56  else
57  globFinished = finished_m;
58 
59  if ( globFinished ) {
60 
61  this->computeCentroid_m();
62 
63  turn_m = turn;
64 
65  // reset
66  peakRadius_m = 0.0;
67  registered_m = 0;
68  finished_m = false;
69  }
70 }
71 
72 
74 
76 
77  // last turn is not yet computed
78  this->computeCentroid_m();
79 
80  if ( !peaks_m.empty() ) {
81  // only rank 0 will go in here
82 
83  fn_m = element_m + std::string(".peaks");
84  hist_m = element_m + std::string(".hist");
85 
86  INFOMSG("Save " << fn_m << " and " << hist_m << endl);
87 
88  if(OpalData::getInstance()->inRestartRun())
89  this->append_m();
90  else
91  this->open_m();
92 
93  this->saveASCII_m();
94 
95  this->close_m();
96 
97  }
98 
99  radius_m.clear();
100  globHist_m.clear();
101 }
102 
103 
105  double globPeakRadius = 0.0;
106  int globRegister = 0;
107 
108  //FIXME inefficient
109  if ( !singlemode_m ) {
110  reduce(peakRadius_m, globPeakRadius, 1, std::plus<double>());
111  reduce(registered_m, globRegister, 1, std::plus<int>());
112  } else {
113  globPeakRadius = peakRadius_m;
114  globRegister = registered_m;
115  }
116 
117  if ( Ippl::myNode() == 0 ) {
118  if ( globRegister > 0 )
119  peaks_m.push_back(globPeakRadius / double(globRegister));
120  }
121 }
122 
124  /*
125  * create local histograms
126  */
127 
128  globHist_m.resize(nBins_m);
129  container_t locHist(nBins_m,0.0);
130 
131  double invBinWidth = 1.0 / binWidth_m;
132  for(container_t::iterator it = radius_m.begin(); it != radius_m.end(); ++it) {
133  int bin = static_cast<int>(std::abs(*it - min_m ) * invBinWidth);
134  if (bin < 0 || (unsigned int)bin >= nBins_m) continue; // Probe might save particles outside its boundary
135  ++locHist[bin];
136  }
137 
138  /*
139  * create global histograms
140  */
141  if ( !singlemode_m ) {
142  reduce(&(locHist[0]), &(locHist[0]) + locHist.size(),
143  &(globHist_m[0]), OpAddAssign());
144  } else {
145  globHist_m.swap(locHist);
146  }
147 
148 // reduce(locHist.data(), globHist_m.data(), locHist.size(), std::plus<double>());
149 }
150 
151 
153  os_m.open(fn_m.c_str(), std::ios::out);
154  hos_m.open(hist_m.c_str(), std::ios::out);
155 }
156 
157 
159  os_m.open(fn_m.c_str(), std::ios::app);
160  hos_m.open(hist_m.c_str(), std::ios::app);
161 }
162 
163 
165  os_m.close();
166  hos_m.close();
167 }
168 
169 
171  os_m << "# Peak Radii (mm)" << std::endl;
172  for (auto &radius : peaks_m)
173  os_m << radius << std::endl;
174 
175  hos_m << "# Histogram bin counts (min, max, nbins, binsize) "
176  << min_m << " mm "
177  << max_m << " mm "
178  << nBins_m << " "
179  << binWidth_m << " mm" << std::endl;
180  for (auto binCount : globHist_m)
181  hos_m << binCount << std::endl;
182 }
std::list< double > peaks_m
Definition: PeakFinder.h:99
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
std::string hist_m
histogram filename with extension (.hist)
Definition: PeakFinder.h:77
void computeCentroid_m()
Definition: PeakFinder.cpp:104
PeakFinder()=delete
std::vector< double > container_t
Definition: PeakFinder.h:28
double peakRadius_m
Definition: PeakFinder.h:97
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
double min_m
histogram size
Definition: PeakFinder.h:94
int registered_m
Definition: PeakFinder.h:98
PETE_TUTree< FnCeil, typename T::PETE_Expr_t > ceil(const PETE_Expr< T > &l)
Definition: PETE.h:811
double max_m
Definition: PeakFinder.h:94
void addParticle(const Vector_t &R)
Definition: PeakFinder.cpp:31
static int myNode()
Definition: IpplInfo.cpp:794
bool finished_m
Definition: PeakFinder.h:102
void save()
Definition: PeakFinder.cpp:73
std::ofstream hos_m
used to write out the histrogram
Definition: PeakFinder.h:83
void evaluate(const int &turn)
Definition: PeakFinder.cpp:41
unsigned int nBins_m
Number of bins.
Definition: PeakFinder.h:90
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
Definition: GlobalComm.hpp:55
static OpalData * getInstance()
Definition: OpalData.cpp:209
void saveASCII_m()
Write to output file.
Definition: PeakFinder.cpp:170
Find peaks of radial profile.
void allreduce(const T *input, T *output, int count, Op op)
Definition: GlobalComm.hpp:510
void createHistogram_m()
Definition: PeakFinder.cpp:123
#define INFOMSG(msg)
Definition: IpplInfo.h:397
std::ofstream os_m
used to write out the data
Definition: PeakFinder.h:80
bool singlemode_m
Definition: PeakFinder.h:100
container_t globHist_m
global histogram values
Definition: PeakFinder.h:71
double binWidth_m
Bin width in mm.
Definition: PeakFinder.h:92
container_t radius_m
Definition: PeakFinder.h:69
void open_m()
Open output file.
Definition: PeakFinder.cpp:152
void close_m()
Close output file.
Definition: PeakFinder.cpp:164
std::string fn_m
filename with extension (.peaks)
Definition: PeakFinder.h:74
std::string::iterator iterator
Definition: MSLang.h:16
int turn_m
Definition: PeakFinder.h:96
bool first_m
Definition: PeakFinder.h:101
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:95
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
void append_m()
Open output file in append mode.
Definition: PeakFinder.cpp:158
std::string element_m
Element/probe name, for name output file.
Definition: PeakFinder.h:86