Line data Source code
1 : /*
2 : * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Martin Rupp
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #include "histogramm.h"
34 : #include <algorithm> // std::sort
35 : #include <sstream>
36 : #include <iomanip> // setprecision
37 :
38 :
39 : namespace ug
40 : {
41 :
42 : // minimum width is 8
43 0 : std::string scientificStr(double d, size_t width)
44 : {
45 0 : std::stringstream s;
46 0 : if(d < 0)
47 : std::setprecision(width-7);
48 : else
49 0 : s << std::setprecision(width-6);
50 0 : s << std::left << std::scientific << std::setw(width) << d;
51 0 : return s.str();
52 0 : }
53 :
54 0 : std::string cutString(double d, size_t width)
55 : {
56 0 : std::stringstream s;
57 0 : s << std::left << std::setw(width) << d;
58 0 : if(s.str().length() == width)
59 : return s.str();
60 0 : return scientificStr(d, width);
61 0 : }
62 :
63 :
64 :
65 :
66 0 : std::string HistogrammString(std::vector<double> values)
67 : {
68 0 : if(values.size() == 0) return "";
69 0 : std::stringstream ss;
70 0 : std::sort(values.begin(), values.end());
71 :
72 0 : double vmin = values[0];
73 0 : double vmax = values[values.size()-1];
74 :
75 : const size_t N = 80;
76 : const double Nd=80.0;
77 : std::vector<size_t> hist;
78 0 : hist.resize(N, 0);
79 :
80 : size_t histmax=0;
81 0 : for(size_t i=0; i<values.size(); i++)
82 : {
83 0 : size_t idx = (size_t)((N-1)*(values[i]-vmin)/(vmax-vmin));
84 0 : hist[idx] ++;
85 0 : if(histmax < hist[idx]) histmax = hist[idx];
86 : }
87 :
88 0 : ss << "histmax = " << histmax << " vmin = " << vmin << " vmax = " << vmax << "\n";
89 : const int height = 10;
90 0 : for(int h=height; h>=0; h--)
91 : {
92 0 : ss << (histmax*h)/((double)height) << "\t";
93 0 : for(size_t i=0; i<N; i++)
94 : {
95 0 : if(hist[i]*height > histmax*h) ss << "#";
96 0 : else ss << " ";
97 : }
98 0 : ss <<"\n";
99 : }
100 :
101 0 : ss << vmin << "\t";
102 0 : for(size_t i =0; i<N; i+=9)
103 0 : ss << " | ";
104 :
105 0 : ss << "\n" << vmin << "\t";
106 0 : for(size_t i =0; i<N; i+=9)
107 0 : ss << scientificStr((i+4)*(vmax-vmin)/(Nd-1)+vmin, 8) << " ";
108 0 : ss << vmax << "\n";
109 :
110 0 : for(size_t i=0; i<N; i++)
111 0 : ss << "-";
112 0 : ss << "\n";
113 : return ss.str();
114 0 : }
115 :
116 :
117 0 : std::string DistributionPercentage(std::vector<double> values)
118 : {
119 0 : if(values.size() == 0) return "";
120 0 : std::stringstream ss;
121 0 : std::sort(values.begin(), values.end());
122 :
123 0 : double vmin = values[0];
124 0 : double vmax = values[values.size()-1];
125 0 : double vdiff = vmax-vmin;
126 :
127 0 : if(vdiff < 1e-12) return "all equal";
128 :
129 : const size_t M = 90;
130 : size_t N = values.size();
131 :
132 : const int height = 10;
133 0 : for(int h=height; h>=0; h--)
134 : {
135 0 : double hv = (vdiff*h)/((double)height);
136 0 : ss << cutString(hv+vmin, 8) << " ";
137 0 : for(size_t i=0; i<M; i++)
138 : {
139 0 : if(values[(i*(N-1))/M]-vmin >= hv) ss << ".";
140 0 : else ss << "#";
141 :
142 : }
143 0 : ss <<"\n";
144 : }
145 :
146 0 : ss << " ";
147 0 : for(size_t i =0; i<M; i+=9)
148 0 : ss << "| ";
149 :
150 0 : ss << "\n%smaller ";
151 0 : for(size_t i =0; i<M; i+=9)
152 0 : ss << std::setw(9) << std::left << i*100.0/M;
153 :
154 0 : ss << "\n#smaller ";
155 0 : for(size_t i =0; i<M; i+=9)
156 0 : ss << std::setw(9) << std::left << (i*N)/M;
157 :
158 :
159 0 : ss << "\n%bigger ";
160 0 : for(size_t i =0; i<M; i+=9)
161 0 : ss << std::setw(9) << std::left << 100-i*100.0/M;
162 0 : ss << "\n#bigger ";
163 0 : for(size_t i =0; i<M; i+=9)
164 0 : ss << std::setw(9) << std::left << N-(i*N)/M;
165 :
166 :
167 0 : ss << "\nvalue ";
168 0 : for(size_t i =0; i<M; i+=9)
169 0 : ss << cutString(values[(i*(N-1))/M], 8) << " ";
170 :
171 0 : ss << "\n% of max ";
172 0 : for(size_t i =0; i<M; i+=9)
173 0 : ss << std::left << cutString(values[(i*(N-1))/M]*100.0/vmax, 8) << " ";
174 :
175 :
176 0 : ss << "\n---------";
177 0 : for(size_t i=0; i<M; i++)
178 0 : ss << "-";
179 0 : ss << "\n";
180 : return ss.str();
181 0 : }
182 :
183 : }
|