Flow123d  last_with_con_2.0.0-4-g42e6930
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->field_name = field.name();
19  this->field_units = field.units();
20  this->output_field_name = this->field_name;
21 
22  this->n_values = size;
23 
24  if (val_aux.NCols_ == 1) {
25  if (val_aux.NRows_ == 1) {
26  this->n_elem_ = N_SCALAR;
27  this->n_rows = 1;
28  this->n_cols = 1;
29  } else {
30  if (val_aux.NRows_ > 1) {
31  if (val_aux.NRows_ > 3) {
33  "Do not support output of vectors with fixed size >3. Field: %s\n",
34  this->field_name.c_str());
35  } else {
36  this->n_elem_ = N_VECTOR;
37  this->n_rows = 3;
38  this->n_cols = 1;
39  }
40  } else {
41  THROW(OutputTime::ExcOutputVariableVector() << OutputTime::EI_FieldName(this->field_name));
42  }
43  }
44  } else {
45  this->n_elem_ = N_TENSOR;
46  this->n_rows = 3;
47  this->n_cols = 3;
48  }
49 
50  data_ = new ElemType[this->n_values * this->n_elem_];
51 }
52 
53 /**
54  * \brief Destructor of OutputData
55  */
56 template <class Value>
58 {
59  delete[] this->data_;
60 }
61 
62 
63 /**
64  * Output data element on given index @p idx. Method for writing data
65  * to output stream.
66  *
67  * \note This method is used only by MSH file format.
68  */
69 template <class Value>
70 void OutputData<Value>::print(ostream &out_stream, unsigned int idx)
71 {
72  OLD_ASSERT_LESS(idx, this->n_values);
73  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
74  for(ElemType *ptr = ptr_begin; ptr < ptr_begin + n_elem_; ptr++ )
75  out_stream << *ptr << " ";
76 }
77 
78 /**
79  * \brief Print all data stored in output data
80  *
81  * TODO: indicate if the tensor data are output in column-first or raw-first order
82  * and possibly implement transposition. Set such property for individual file formats.
83  * Class OutputData stores always in raw-first order.
84  */
85 template <class Value>
86 void OutputData<Value>::print_all(ostream &out_stream)
87 {
88  for(unsigned int idx = 0; idx < this->n_values; idx++) {
89  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
90  for(ElemType *ptr = ptr_begin; ptr < ptr_begin + n_elem_; ptr++ )
91  out_stream << *ptr << " ";
92  }
93 }
94 
95 
96 template <class Value>
97 void OutputData<Value>::print_all_yaml(ostream &out_stream, unsigned int precision)
98 {
99  out_stream << "[ ";
100  for(unsigned int idx = 0; idx < this->n_values; idx++) {
101  if (idx != 0) out_stream << ", ";
102  ElemType *ptr_begin = this->data_ + n_elem_ * idx;
103  typename Value::return_type value;
104  out_stream << field_value_to_yaml( Value::from_raw(value, ptr_begin), precision );
105  }
106  out_stream << " ]";
107 }
108 
109 
110 /**
111  * Store data element of given data value under given index.
112  */
113 template <class Value>
114 void OutputData<Value>::store_value(unsigned int idx, const Value& value) {
115  operate(idx, value, [](ElemType& raw, ElemType val) {raw = val;});
116 };
117 
118 /**
119  * Add value to given index
120  */
121 template <class Value>
122 void OutputData<Value>::add(unsigned int idx, const Value& value) {
123  operate(idx, value, [](ElemType& raw, ElemType val) {raw += val;});
124 };
125 
126 /**
127  * Reset values at given index
128  */
129 template <class Value>
130 void OutputData<Value>::zero(unsigned int idx) {
131  operate(idx, val_aux, [](ElemType& raw, ElemType val) {raw = 0;});
132 };
133 
134 /**
135  * Normalize values at given index
136  */
137 template <class Value>
138 void OutputData<Value>::normalize(unsigned int idx, unsigned int divisor) {
139  operate(idx, val_aux, [divisor](ElemType& raw, ElemType val) {raw /= divisor;});
140 };
141 
142 
143 
144 // Instantiation of OutputData template.
145 template class OutputData< FieldValue<0>::Enum >;
146 template class OutputData< FieldValue<0>::Integer >;
147 template class OutputData< FieldValue<0>::Scalar >;
148 
151 
154 
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:84
std::string output_field_name
void print_all_yaml(ostream &out_stream, unsigned int precision) override
Definition: output_data.cc:97
string field_value_to_yaml(const T &mat, unsigned int prec)
void print(ostream &out_stream, unsigned int idx) override
Definition: output_data.cc:70
unsigned int n_values
NumCompValueType n_elem_
This class is used for storing data that are copied from field.
Definition: output_data.hh:23
void zero(unsigned int idx)
Definition: output_data.cc:130
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.
Value::element_type ElemType
Definition: output_data.hh:25
Value val_aux
Definition: output_data.hh:115
void add(unsigned int idx, const Value &value)
Definition: output_data.cc:122
#define xprintf(...)
Definition: system.hh:87
#define OLD_ASSERT_LESS(a, b)
Definition: global_defs.h:134
void normalize(unsigned int idx, unsigned int divisor)
Definition: output_data.cc:138
virtual ~OutputData() override
Destructor of OutputData.
Definition: output_data.cc:57
unsigned int n_rows
Definition: output_data.hh:122
unsigned int n_cols
Definition: output_data.hh:122
ElemType * data_
Definition: output_data.hh:96
std::string field_name
Definition: system.hh:59
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:114
void print_all(ostream &out_stream) override
Print all data stored in output data.
Definition: output_data.cc:86