OpenBeam
C++ library for static analysis of mechanical structures
print_html_matrix.h
1 /* +---------------------------------------------------------------------------+
2  | OpenBeam - C++ Finite Element Analysis library |
3  | |
4  | Copyright (C) 2010-2021 Jose Luis Blanco Claraco |
5  | University of Malaga |
6  | |
7  | OpenBeam is free software: you can redistribute it and/or modify |
8  | it under the terms of the GNU General Public License as published by |
9  | the Free Software Foundation, either version 3 of the License, or |
10  | (at your option) any later version. |
11  | |
12  | OpenBeam is distributed in the hope that it will be useful, |
13  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15  | GNU General Public License for more details. |
16  | |
17  | You should have received a copy of the GNU General Public License |
18  | along with OpenBeam. If not, see <http://www.gnu.org/licenses/>. |
19  | |
20  +---------------------------------------------------------------------------+
21  */
22 
23 #pragma once
24 
25 #include <openbeam/config.h> // num_t
26 
27 #include <cmath>
28 #include <iosfwd>
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 namespace openbeam
34 {
35 template <class MATRIX, class ARRAY = MATRIX>
36 void print_html_matrix(
37  std::ostream& out, const MATRIX& K,
38  const std::vector<std::string>& row_titles,
39  const std::vector<std::string>& col_titles,
40  const ARRAY* rows_to_use = nullptr, const ARRAY* cols_to_use = nullptr)
41 {
42  const size_t nCols = K.cols();
43  const size_t nRows = K.rows();
44 
45  // Determine common factor to make notation clearer:
46  int common_factor_pow = 0;
47  std::map<int, int> pow10hits;
48  for (size_t i = 0; i < nRows; i++)
49  {
50  if (rows_to_use && !(*rows_to_use)[i]) continue;
51 
52  for (size_t j = 0; j < nCols; j++)
53  {
54  if (cols_to_use && !(*cols_to_use)[j]) continue;
55 
56  const num_t Kijabs = std::abs(K(i, j));
57 
58  if (Kijabs > 0)
59  {
60  const int mag =
61  static_cast<int>(log10(Kijabs)); // Floor to "int"
62  pow10hits[mag]++;
63  }
64  // else: NO, zeroes do not count
65  }
66  }
67  if (!pow10hits.empty())
68  {
69  std::map<int, int>::const_iterator it_max = pow10hits.begin();
70  for (auto it = pow10hits.begin(); it != pow10hits.end(); ++it)
71  {
72  if (it->second > it_max->second) it_max = it;
73  }
74  common_factor_pow = it_max->first;
75  }
76 
77  const num_t common_factor =
78  pow(static_cast<num_t>(10), static_cast<num_t>(common_factor_pow));
79  const num_t common_factor_inv = 1. / common_factor;
80 
81  // Title:
82  out << "<table border=\"1\" cellpadding=\"9\" cellspacing=\"0\">\n";
83 
84  // 1st row:
85  out << "<tr>";
86  out << "<td bgcolor=\"#E0E0E0\"> &nbsp; </td>";
87  for (size_t i = 0; i < nCols; i++)
88  {
89  if (cols_to_use && !(*cols_to_use)[i]) continue;
90 
91  out << col_titles[i];
92  }
93  out << "</tr>\n";
94 
95  // The rest of rows:
96  for (size_t i = 0; i < nRows; i++)
97  {
98  if (rows_to_use && !(*rows_to_use)[i]) continue;
99 
100  out << "<tr>";
101  out << row_titles[i];
102 
103  for (size_t j = 0; j < nCols; j++)
104  {
105  if (cols_to_use && !(*cols_to_use)[j]) continue;
106 
107  const num_t Kij = K(i, j);
108  if (!Kij)
109  out << format("<td align=\"right\"> 0 </td>");
110  else
111  out << format(
112  "<td align=\"right\"> %.3f </td>", Kij * common_factor_inv);
113  }
114 
115  out << "</tr>\n";
116  }
117  out << "</table>\n";
118  out << format("( &times; 10<sup>%i</sup> )<br>\n", common_factor_pow);
119 }
120 
121 } // namespace openbeam