Flow123d  release_2.1.2-337-g6b7a56b
armadillo_tools.cc
Go to the documentation of this file.
1 /*
2  * armadillo_setup.cc
3  *
4  * Created on: Jul 4, 2016
5  * Author: jb
6  */
7 
8 
9 
10 
11 
12 #include "armadillo_tools.hh"
13 
14 //#include <ostream>
15 //#include <sstream>
16 #include <type_traits>
17 #include "system/exc_common.hh"
18 #include "system/logger.hh"
19 
20 #include <armadillo>
21 
22 using namespace std;
23 
24 /**
25  * Auxiliary exception in order to change standard frames_to_cut_.
26  */
27 class ExcArmadillo : public ExceptionBase {
28 public:
30  {
31  this->frames_to_cut_ = {"arma_stop"};
32  }
33 
34  void print_info(std::ostringstream &out) const override {
35  ::internal::ExcStream estream(out, *this);
36  estream << "Armadillo Message: " << EI_Message::val << "\n";
37  }
38 };
39 
40 /**
41  * Auxiliary output buffer to catch the error messages and report stacktrace.
42  */
43 class ArmaStreamBuf : public std::stringbuf {
44 protected:
45  /**
46  * Override sync to throw on error message.
47  */
48  int sync() override;
49 };
50 
52  if (this->str().find("error") != string::npos) {
53  // Can not throw here, since any exception is cached somewhere between armadillo call point and this method.
54  // Must write out the stack here.
55  auto e = ExcArmadillo();
56  e << EI_Message(this->str());
57  _LOG( Logger::MsgType::message ) << e.what();
58 
59  }
60  std::cout << this->str() << std::endl;
61  this->str().clear();
62  return 1;
63 }
64 
65 
67 {
68  static ArmaStreamBuf stream_buf;
69  static std::ostream arma_stream(&stream_buf);
70  arma::set_stream_err1(arma_stream);
71  arma::set_stream_err2(arma_stream);
72 }
73 
74 
75 
76 
77 // internal implementation template
78 template <class T>
79 string field_value_to_yaml_matrix(const T &mat, unsigned int prec) {
80  stringstream ss;
81  ss << "[ ";
82  for(unsigned int i_row=0; i_row < mat.n_rows; i_row ++ ) {
83  if (i_row != 0) ss << ", ";
84  ss << "[ ";
85  for(unsigned int i_col=0; i_col < mat.n_cols; i_col++) {
86  if (i_col != 0) ss << ", ";
87  if (std::is_floating_point<decltype(mat.at(0,0))>::value)
88  //ss << fmt::format("{0:#.{1}g}", mat.at(i_row, i_col), prec);
89  ss << mat.at(i_row, i_col);
90  else
91  ss << mat.at(i_row, i_col);
92  }
93  ss << " ]";
94  }
95  ss << " ]";
96  return ss.str();
97 }
98 
99 // internal implementation template
100 template <class T>
101 string field_value_to_yaml_vector(const T &vec, unsigned int prec) {
102  stringstream ss;
103  ss << "[ ";
104  for(unsigned int i=0; i < vec.n_elem; i++) {
105  if (i != 0) ss << ", ";
106  if (std::is_floating_point<decltype(vec[0])>::value)
107  //ss << fmt::format("{0:#.{1}g}", vec(i), prec);
108  ss << vec(i);
109  else
110  ss << vec(i);
111  }
112  ss << " ]";
113  return ss.str();
114 }
115 
116 template <class IsScalar>
118 
119 template <>
120 struct field_value_scalar_resolution<std::true_type> {
121  template <class T>
122  inline static string print(const T &mat, unsigned int prec) {
123  stringstream ss;
125  ss << mat;
126  else
127  //ss << fmt::format("{0:#.{1}g}", mat, prec);
128  ss << mat;
129  return ss.str();
130  }
131 };
132 
133 template <>
134 struct field_value_scalar_resolution<std::false_type> {
135  template <class T>
136  inline static string print(const T &mat, unsigned int prec) {
137  if (mat.n_cols == 1 || mat.n_rows == 1) {
138  return field_value_to_yaml_vector(mat, prec);
139  } else {
140  return field_value_to_yaml_matrix(mat, prec);
141  }
142  }
143 };
144 
145 
146 template <class T>
147 inline string field_value_to_yaml(const T &mat, unsigned int prec) {
149 }
150 
151 
152 
153 #define FIELD_VALUE_TO_YAML_DIM(dim) \
154  template string field_value_to_yaml(arma::Mat<double>::fixed<dim,dim> const &mat, unsigned int prec); \
155  template string field_value_to_yaml(const arma::Mat<int>::fixed<dim,dim> &mat, unsigned int prec); \
156  template string field_value_to_yaml(const arma::Mat<unsigned int>::fixed<dim,dim> &mat, unsigned int prec); \
157  template string field_value_to_yaml(const arma::Col<double>::fixed<dim> &mat, unsigned int prec); \
158  template string field_value_to_yaml(const arma::Col<int>::fixed<dim> &mat, unsigned int prec); \
159  template string field_value_to_yaml(const arma::Col<unsigned int>::fixed<dim> &mat, unsigned int prec);
160 
163 
164 template string field_value_to_yaml(const arma::Col<double> &mat, unsigned int prec);
165 template string field_value_to_yaml(const arma::Col<unsigned int> &mat, unsigned int prec);
166 
167 template string field_value_to_yaml(const double &mat, unsigned int prec);
168 template string field_value_to_yaml(const int &mat, unsigned int prec);
169 template string field_value_to_yaml(const unsigned int &mat, unsigned int prec);
170 
171 
172 
173 
static string print(const T &mat, unsigned int prec)
string field_value_to_yaml(const T &mat, unsigned int prec)
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:489
static string print(const T &mat, unsigned int prec)
void print_info(std::ostringstream &out) const override
static constexpr bool value
Definition: json.hpp:87
#define FIELD_VALUE_TO_YAML_DIM(dim)
void armadillo_setup()
#define _LOG(type)
Internal macro defining universal record of log.
Definition: logger.hh:230
int sync() override
string field_value_to_yaml_vector(const T &vec, unsigned int prec)
Base of exceptions used in Flow123d.
Definition: exceptions.hh:75
string field_value_to_yaml_matrix(const T &mat, unsigned int prec)