Flow123d  release_2.1.0-87-gfbc1563
output_data.cc
Go to the documentation of this file.
1 /*
2  * output_data.cc
3  *
4  * Created on: Jun 28, 2016
5  * Author: jb
6  */
7 
8 #include "io/output_data.hh"
9 #include "fields/field_values.hh"
10 #include "fields/field_common.hh"
11 #include "io/output_time.hh"
13 
14 template <class Value>
15 OutputData<Value>::OutputData(const FieldCommon &field, unsigned int size)
16 : val_aux(aux)
17 {
18  this->set_vtk_type<ElemType>();
19  this->field_name = field.name();
20  this->field_units = field.units();
21  this->output_field_name = this->field_name;
22 
23  this->n_values = size;
24 
25  if (val_aux.NCols_ == 1) {
26  if (val_aux.NRows_ == 1) {
27  this->n_elem_ = N_SCALAR;
28  this->n_rows = 1;
29  this->n_cols = 1;
30  } else {
31  if (val_aux.NRows_ > 1) {
32  if (val_aux.NRows_ > 3) {
34  "Do not support output of vectors with fixed size >3. Field: %s\n",
35  this->field_name.c_str());
36  } else {
37  this->n_elem_ = N_VECTOR;
38  this->n_rows = 3;
39  this->n_cols = 1;
40  }
41  } else {
42  THROW(OutputTime::ExcOutputVariableVector() << OutputTime::EI_FieldName(this->field_name));
43  }
44  }
45  } else {
46  this->n_elem_ = N_TENSOR;
47  this->n_rows = 3;
48  this->n_cols = 3;
49  }
50 
51  data_ = new ElemType[this->n_values * this->n_elem_];
52 }
53 
54 /**
55  * \brief Destructor of OutputData
56  */
57 template <class Value>
59 {
60  delete[] this->data_;
61 }
62 
63 
64 /**
65  * Output data element on given index @p idx. Method for writing data
66  * to output stream.
67  *
68  * \note This method is used only by MSH file format.
69  */
70 template <class Value>
71 void OutputData<Value>::print_ascii(ostream &out_stream, unsigned int idx)
72 {
73  ASSERT_LT(idx, this->n_values).error();
74  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
75  for(ElemType *ptr = ptr_begin; ptr < ptr_begin + n_elem_; ptr++ )
76  out_stream << *ptr << " ";
77 }
78 
79 /**
80  * \brief Print all data stored in output data
81  *
82  * TODO: indicate if the tensor data are output in column-first or raw-first order
83  * and possibly implement transposition. Set such property for individual file formats.
84  * Class OutputData stores always in raw-first order.
85  */
86 template <class Value>
87 void OutputData<Value>::print_ascii_all(ostream &out_stream)
88 {
89  for(unsigned int idx = 0; idx < this->n_values; idx++) {
90  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
91  for(ElemType *ptr = ptr_begin; ptr < ptr_begin + n_elem_; ptr++ )
92  out_stream << *ptr << " ";
93  }
94 }
95 
96 
97 /// Prints the whole data vector into stream.
98 template <class Value>
99 void OutputData<Value>::print_binary_all(ostream &out_stream, bool print_data_size)
100 {
101  if (print_data_size) {
102  // write size of data
103  unsigned long long int data_byte_size = this->n_values * n_elem_ * sizeof(ElemType);
104  out_stream.write(reinterpret_cast<const char*>(&data_byte_size), sizeof(unsigned long long int));
105  }
106  // write data
107  for(unsigned int idx = 0; idx < this->n_values; idx++) {
108  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
109  for(ElemType *ptr = ptr_begin; ptr < ptr_begin + n_elem_; ptr++ ) {
110  out_stream.write(reinterpret_cast<const char*>(ptr), sizeof(ElemType));
111  }
112  }
113 }
114 
115 
116 template <class Value>
117 void OutputData<Value>::print_all_yaml(ostream &out_stream, unsigned int precision)
118 {
119  out_stream << "[ ";
120  for(unsigned int idx = 0; idx < this->n_values; idx++) {
121  if (idx != 0) out_stream << ", ";
122  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
123  typename Value::return_type value;
124  out_stream << field_value_to_yaml( Value::from_raw(value, ptr_begin), precision );
125  }
126  out_stream << " ]";
127 }
128 
129 
130 template <class Value>
131 void OutputData<Value>::get_min_max_range(double &min, double &max)
132 {
133  min = std::numeric_limits<double>::max();
134  max = std::numeric_limits<double>::min();
135  for(unsigned int idx = 0; idx < this->n_values; idx++) {
136  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
137  for(ElemType *ptr = ptr_begin; ptr < ptr_begin + n_elem_; ptr++ ) {
138  if ((double)(*ptr) < min) min = (double)(*ptr);
139  if ((double)(*ptr) > max) max = (double)(*ptr);
140  }
141  }
142 }
143 
144 
145 /**
146  * Store data element of given data value under given index.
147  */
148 template <class Value>
149 void OutputData<Value>::store_value(unsigned int idx, const Value& value) {
150  operate(idx, value, [](ElemType& raw, ElemType val) {raw = val;});
151 };
152 
153 /**
154  * Add value to given index
155  */
156 template <class Value>
157 void OutputData<Value>::add(unsigned int idx, const Value& value) {
158  operate(idx, value, [](ElemType& raw, ElemType val) {raw += val;});
159 };
160 
161 /**
162  * Reset values at given index
163  */
164 template <class Value>
165 void OutputData<Value>::zero(unsigned int idx) {
166  operate(idx, val_aux, [](ElemType& raw, ElemType val) {raw = 0;});
167 };
168 
169 /**
170  * Normalize values at given index
171  */
172 template <class Value>
173 void OutputData<Value>::normalize(unsigned int idx, unsigned int divisor) {
174  operate(idx, val_aux, [divisor](ElemType& raw, ElemType val) {raw /= divisor;});
175 };
176 
177 
178 
179 // Instantiation of OutputData template.
180 template class OutputData< FieldValue<0>::Enum >;
181 template class OutputData< FieldValue<0>::Integer >;
182 template class OutputData< FieldValue<0>::Scalar >;
183 
186 
189 
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:60
void operate(unsigned int idx, const Value &val, const Func &func)
Definition: output_data.hh:95
std::string output_field_name
void print_all_yaml(ostream &out_stream, unsigned int precision) override
Definition: output_data.cc:117
string field_value_to_yaml(const T &mat, unsigned int prec)
void print_binary_all(ostream &out_stream, bool print_data_size=true) override
Print all data stored in output data to appended binary format.
Definition: output_data.cc:99
void print_ascii(ostream &out_stream, unsigned int idx) override
Definition: output_data.cc:71
unsigned int n_values
NumCompValueType n_elem_
This class is used for storing data that are copied from field.
Definition: output_data.hh:24
void zero(unsigned int idx)
Definition: output_data.cc:165
OutputData(const FieldCommon &field, unsigned int size)
Constructor of templated OutputData.
Definition: output_data.cc:15
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
static constexpr bool value
Definition: json.hpp:87
Value::element_type ElemType
Definition: output_data.hh:26
void get_min_max_range(double &min, double &max) override
Definition: output_data.cc:131
Value val_aux
Definition: output_data.hh:126
void add(unsigned int idx, const Value &value)
Definition: output_data.cc:157
#define xprintf(...)
Definition: system.hh:87
void normalize(unsigned int idx, unsigned int divisor)
Definition: output_data.cc:173
virtual ~OutputData() override
Destructor of OutputData.
Definition: output_data.cc:58
unsigned int n_rows
Definition: output_data.hh:133
unsigned int n_cols
Definition: output_data.hh:133
void print_ascii_all(ostream &out_stream) override
Print all data stored in output data ro ascii format.
Definition: output_data.cc:87
ElemType * data_
Definition: output_data.hh:107
std::string field_name
Definition: system.hh:59
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:296
FieldCommon & name(const string &name)
Definition: field_common.hh:86
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45
void store_value(unsigned int idx, const Value &value)
Definition: output_data.cc:149