Flow123d  release_3.0.0-910-g56968e3
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 #include "system/logger.hh"
21 
22 namespace it = Input::Type;
23 
24 template <class Value>
26 {
27  return it::Tuple("IndependentValue", "Value of Field for independent variable.")
29  "Independent variable of stamp." )
30  .declare_key("value", Value::get_input_type(), it::Default::obligatory(),
31  "Value of the field in given stamp." )
32  .close();
33 }
34 
35 template <class Value>
37 {
38  return it::Record("TableFunction", "Allow set variable series initialization of Fields.")
40  "Initizaliation values of Field." )
41  .allow_auto_conversion("values")
42  .close();
43 
44 }
45 
46 
47 template <class Value>
49 : last_t_(-1.0),
50  value_(r_value_)
51 {}
52 
53 
54 template <class Value>
56 {
57  ASSERT( !this->initialized() ).error("TableFunction can't be initialized more than once.");
58 
59  Input::Array data_array = rec.val<Input::Array>("values");
60  double last_t = -1.0;
61  for (Input::Iterator<Input::Tuple> it = data_array.begin<Input::Tuple>(); it != data_array.end(); ++it) {
62  double t = it->val<double>("t");
63  if (last_t >= t) {
64  WarningOut().fmt("Nonascending order of declared stamps in TableFunction at address {}.\nStamp {} will be skipped.",
65  rec.address_string(), t);
66  } else {
67  last_t = t;
68 
69  typename Value::return_type r_value;
70  Value value(r_value);
71  value.init_from_input( it->val<typename Value::AccessType>("value") );
72  table_values_.push_back( TableValue(t, value) );
73  }
74  }
75 }
76 
77 template <class Value>
79 {
80  return ( table_values_.size() > 0 );
81 }
82 
83 template <class Value>
85 {
86  ASSERT( this->initialized() ).error("Compute value of uninitialized TableFunction.");
87 
88  if (t != last_t_) {
89  unsigned int last_idx = table_values_.size() - 1;
90  if (t < table_values_[0].t_) {
91  WarningOut().fmt("Value of stamp {} is out of range of TableFunction: <{}, {}>. Extrapolation of minimal value will be used.",
92  t, table_values_[0].t_, table_values_[last_idx].t_);
93  this->interpolated(0.0, 0);
94  } else if (t > table_values_[last_idx].t_) {
95  WarningOut().fmt("Value of stamp {} is out of range of TableFunction: <{}, {}>. Extrapolation of maximal value will be used.",
96  t, table_values_[0].t_, table_values_[last_idx].t_);
97  this->interpolated(1.0, last_idx-1);
98  } else {
99  for (unsigned int i=0; i<last_idx; ++i) {
100  if (t >= table_values_[i].t_ && t <= table_values_[i+1].t_) {
101  double coef = (t - table_values_[i].t_) / (table_values_[i+1].t_ - table_values_[i].t_);
102  this->interpolated(coef, i);
103  break;
104  }
105  }
106  }
107  }
108 
109  return this->r_value_;
110 }
111 
112 template <class Value>
113 void TableFunction<Value>::interpolated(double coef, unsigned int idx)
114 {
115  ASSERT(coef >= 0 && coef <= 1)(coef).error();
116  ASSERT(idx >= 0 && idx <= table_values_.size()-2)(idx).error();
117 
118  Value val_0(table_values_[idx].r_value_);
119  Value val_1(table_values_[idx+1].r_value_);
120  if (Value::is_scalable())
121  for( unsigned int row=0; row<value_.n_rows(); row++)
122  for( unsigned int col=0; col<value_.n_cols(); col++) {
123  value_(row,col) = val_0(row,col) + coef * (val_1(row,col) - val_0(row,col));
124  }
125 }
126 
127 
128 // Instantiation of TableFunction class
129 template class TableFunction<FieldValue<0>::Enum >;
130 template class TableFunction<FieldValue<0>::Integer >;
131 template class TableFunction<FieldValue<0>::Scalar >;
132 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:567
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:169
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
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:346
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:303
Class for declaration of inputs sequences.
Definition: type_base.hh:346
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:132
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:541
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
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:501
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:246
std::vector< struct TableValue > table_values_
Vector of values in all stamps.
Record type proxy class.
Definition: type_record.hh:182
Accessor to the data with type Type::Tuple.
Definition: accessors.hh:412
Store value in one t stamp.
string address_string() const
Definition: accessors.cc:184