Flow123d  release_2.1.0-84-g6a13a75
table_function.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file table_function.cc
15  * @brief
16  */
17 
18 #include "fields/table_function.hh"
19 #include "input/input_type.hh"
20 
21 namespace it = Input::Type;
22 
23 template <class Value>
25 {
26  return it::Tuple("IndependentValue", "Value of Field for independent variable.")
28  "Independent variable of stamp." )
29  .declare_key("value", Value::get_input_type(), it::Default::obligatory(),
30  "Value of the field in given stamp." )
31  .close();
32 }
33 
34 template <class Value>
36 {
37  return it::Record("TableFunction", "Allow set variable series initialization of Fields.")
39  "Initizaliation values of Field." )
40  .allow_auto_conversion("values")
41  .close();
42 
43 }
44 
45 
46 template <class Value>
48 : last_t_(-1.0),
49  value_(r_value_)
50 {}
51 
52 
53 template <class Value>
55 {
56  ASSERT( !this->initialized() ).error("TableFunction can't be initialized more than once.");
57 
58  Input::Array data_array = rec.val<Input::Array>("values");
59  double last_t = -1.0;
60  for (Input::Iterator<Input::Tuple> it = data_array.begin<Input::Tuple>(); it != data_array.end(); ++it) {
61  double t = it->val<double>("t");
62  if (last_t >= t) {
63  WarningOut().fmt("Nonascending order of declared stamps in TableFunction at address {}.\nStamp {} will be skipped.",
64  rec.address_string(), t);
65  } else {
66  last_t = t;
67 
68  typename Value::return_type r_value;
69  Value value(r_value);
70  value.init_from_input( it->val<typename Value::AccessType>("value") );
71  table_values_.push_back( TableValue(t, value) );
72  }
73  }
74 }
75 
76 template <class Value>
78 {
79  return ( table_values_.size() > 0 );
80 }
81 
82 template <class Value>
84 {
85  ASSERT( this->initialized() ).error("Compute value of uninitialized TableFunction.");
86 
87  if (t != last_t_) {
88  unsigned int last_idx = table_values_.size() - 1;
89  if (t < table_values_[0].t_) {
90  WarningOut().fmt("Value of stamp {} is out of range of TableFunction: <{}, {}>. Extrapolation of minimal value will be used.",
91  t, table_values_[0].t_, table_values_[last_idx].t_);
92  this->interpolated(0.0, 0);
93  } else if (t > table_values_[last_idx].t_) {
94  WarningOut().fmt("Value of stamp {} is out of range of TableFunction: <{}, {}>. Extrapolation of maximal value will be used.",
95  t, table_values_[0].t_, table_values_[last_idx].t_);
96  this->interpolated(1.0, last_idx-1);
97  } else {
98  for (unsigned int i=0; i<last_idx; ++i) {
99  if (t >= table_values_[i].t_ && t <= table_values_[i+1].t_) {
100  double coef = (t - table_values_[i].t_) / (table_values_[i+1].t_ - table_values_[i].t_);
101  this->interpolated(coef, i);
102  break;
103  }
104  }
105  }
106  }
107 
108  return this->r_value_;
109 }
110 
111 template <class Value>
112 void TableFunction<Value>::interpolated(double coef, unsigned int idx)
113 {
114  ASSERT(coef >= 0 && coef <= 1)(coef).error();
115  ASSERT(idx >= 0 && idx <= table_values_.size()-2)(idx).error();
116 
117  Value val_0(table_values_[idx].r_value_);
118  Value val_1(table_values_[idx+1].r_value_);
119  if (Value::is_scalable())
120  for( unsigned int row=0; row<value_.n_rows(); row++)
121  for( unsigned int col=0; col<value_.n_cols(); col++) {
122  value_(row,col) = val_0(row,col) + coef * (val_1(row,col) - val_0(row,col));
123  }
124 }
125 
126 
127 // Instantiation of TableFunction class
128 template class TableFunction<FieldValue<0>::Enum >;
129 template class TableFunction<FieldValue<0>::Integer >;
130 template class TableFunction<FieldValue<0>::Scalar >;
131 template class TableFunction<FieldValue<0>::Vector >; // temporary solution for computing more fields at once in python
Iterator< ValueType > begin() const
return_type r_value_
bool initialized()
Return true if TableFunction is initialized (method init_from_input was called).
Accessor to input data conforming to declared Array.
Definition: accessors.hh:561
Tuple & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Overrides Record::declare_key(const string &, std::shared_ptr<TypeBase>, const Default &...
Definition: type_tuple.cc:171
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:105
const Tuple & close() const
Close the Tuple for further declarations of keys.
Definition: type_tuple.cc:70
static const Input::Type::Record & get_input_type()
Value value_
Last value, prevents passing large values (vectors) by value.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
double last_t_
Last t stamp of computed value_ (to prevent repetitive calculation)
void init_from_input(const Input::Record &rec)
Initialize actual values of the field given from the given Input::Record rec.
void interpolated(double coef, unsigned int idx)
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:301
Class for declaration of inputs sequences.
Definition: type_base.hh:339
IteratorBase end() const
virtual Record & allow_auto_conversion(const string &from_key)
Allows shorter input of the Record providing only value of the from_key given as the parameter...
Definition: type_record.cc:130
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:534
Accessor to the data with type Type::Record.
Definition: accessors.hh:286
const Ret val(const string &key) const
TableFunction()
Default constructor.
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:488
Tuple type proxy class.
Definition: type_tuple.hh:45
static const Input::Type::Tuple & get_input_type_val()
return_type const & value(double t)
Value::return_type return_type
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:234
std::vector< struct TableValue > table_values_
Vector of values in all stamps.
Record type proxy class.
Definition: type_record.hh:177
Accessor to the data with type Type::Tuple.
Definition: accessors.hh:406
Store value in one t stamp.
string address_string() const
Definition: accessors.cc:184