Flow123d  last_with_con_2.0.0-663-gd0e2296
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)
100 {
101  // write size of data
102  unsigned long long int data_byte_size = this->n_values * n_elem_ * sizeof(ElemType);
103  out_stream.write(reinterpret_cast<const char*>(&data_byte_size), sizeof(unsigned long long int));
104  // write data
105  for(unsigned int idx = 0; idx < this->n_values; idx++) {
106  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
107  for(ElemType *ptr = ptr_begin; ptr < ptr_begin + n_elem_; ptr++ ) {
108  out_stream.write(reinterpret_cast<const char*>(ptr), sizeof(ElemType));
109  }
110  }
111 }
112 
113 
114 template <class Value>
115 void OutputData<Value>::print_all_yaml(ostream &out_stream, unsigned int precision)
116 {
117  out_stream << "[ ";
118  for(unsigned int idx = 0; idx < this->n_values; idx++) {
119  if (idx != 0) out_stream << ", ";
120  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
121  typename Value::return_type value;
122  out_stream << field_value_to_yaml( Value::from_raw(value, ptr_begin), precision );
123  }
124  out_stream << " ]";
125 }
126 
127 
128 template <class Value>
129 void OutputData<Value>::get_min_max_range(double &min, double &max)
130 {
131  min = std::numeric_limits<double>::max();
132  max = std::numeric_limits<double>::min();
133  for(unsigned int idx = 0; idx < this->n_values; idx++) {
134  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
135  for(ElemType *ptr = ptr_begin; ptr < ptr_begin + n_elem_; ptr++ ) {
136  if ((double)(*ptr) < min) min = (double)(*ptr);
137  if ((double)(*ptr) > max) max = (double)(*ptr);
138  }
139  }
140 }
141 
142 
143 /**
144  * Store data element of given data value under given index.
145  */
146 template <class Value>
147 void OutputData<Value>::store_value(unsigned int idx, const Value& value) {
148  operate(idx, value, [](ElemType& raw, ElemType val) {raw = val;});
149 };
150 
151 /**
152  * Add value to given index
153  */
154 template <class Value>
155 void OutputData<Value>::add(unsigned int idx, const Value& value) {
156  operate(idx, value, [](ElemType& raw, ElemType val) {raw += val;});
157 };
158 
159 /**
160  * Reset values at given index
161  */
162 template <class Value>
163 void OutputData<Value>::zero(unsigned int idx) {
164  operate(idx, val_aux, [](ElemType& raw, ElemType val) {raw = 0;});
165 };
166 
167 /**
168  * Normalize values at given index
169  */
170 template <class Value>
171 void OutputData<Value>::normalize(unsigned int idx, unsigned int divisor) {
172  operate(idx, val_aux, [divisor](ElemType& raw, ElemType val) {raw /= divisor;});
173 };
174 
175 
176 
177 // Instantiation of OutputData template.
178 template class OutputData< FieldValue<0>::Enum >;
179 template class OutputData< FieldValue<0>::Integer >;
180 template class OutputData< FieldValue<0>::Scalar >;
181 
184 
187 
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
void print_binary_all(ostream &out_stream) override
Print all data stored in output data to appended binary format.
Definition: output_data.cc:99
std::string output_field_name
void print_all_yaml(ostream &out_stream, unsigned int precision) override
Definition: output_data.cc:115
string field_value_to_yaml(const T &mat, unsigned int prec)
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:163
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:129
Value val_aux
Definition: output_data.hh:126
void add(unsigned int idx, const Value &value)
Definition: output_data.cc:155
#define xprintf(...)
Definition: system.hh:87
void normalize(unsigned int idx, unsigned int divisor)
Definition: output_data.cc:171
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:147