Flow123d  3.9.0-31af398cc
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 "tools/time_governor.hh"
20 #include "input/input_type.hh"
21 #include "system/logger.hh"
22 
23 namespace it = Input::Type;
24 
25 template <class Value>
27 {
28  /*
29  * Table function is now fixed for representation of time value. It can be changed
30  * to independent value. It needs only replace type of 't' with generic type
31  * (Input::Type::Parameter).
32  */
33  return it::Tuple("IndependentValue", "Value of Field for time variable.")
34  //"Value of Field for independent variable."
36  "Time stamp." )
37  //"Independent variable of stamp."
38  .declare_key("value", Value::get_input_type(), it::Default::obligatory(),
39  "Value of the field in given stamp." )
40  .close();
41 }
42 
43 template <class Value>
45 {
46  return it::Record("TableFunction", "Allow set variable series initialization of Fields.")
48  "Initizaliation values of Field." )
49  .allow_auto_conversion("values")
50  .close();
51 
52 }
53 
54 
55 template <class Value>
57 : last_t_(-1.0),
58  value_(r_value_)
59 {}
60 
61 
62 template <class Value>
64 {
65  ASSERT( !this->initialized() ).error("TableFunction can't be initialized more than once.");
66 
67  Input::Array data_array = rec.val<Input::Array>("values");
68  double last_t = -1.0;
69  for (Input::Iterator<Input::Tuple> it = data_array.begin<Input::Tuple>(); it != data_array.end(); ++it) {
70  double t = time.read_time(it->find<Input::Tuple>("t"));
71  if (last_t >= t) {
72  WarningOut().fmt("Nonascending order of declared stamps in TableFunction at address {}.\nStamp {} will be skipped.",
73  rec.address_string(), t);
74  } else {
75  last_t = t;
76 
77  typename Value::return_type r_value;
78  Value value(r_value);
79  value.init_from_input( it->val<typename Value::AccessType>("value") );
80  table_values_.push_back( TableValue(t, value) );
81  }
82  }
83 }
84 
85 template <class Value>
87 {
88  return ( table_values_.size() > 0 );
89 }
90 
91 template <class Value>
93 {
94  ASSERT( this->initialized() ).error("Compute value of uninitialized TableFunction.");
95 
96  if (t != last_t_) {
97  unsigned int last_idx = table_values_.size() - 1;
98  if (t < table_values_[0].t_) {
99  WarningOut().fmt("Value of stamp {} is out of range of TableFunction: <{}, {}>. Extrapolation of minimal value will be used.",
100  t, table_values_[0].t_, table_values_[last_idx].t_);
101  this->interpolated(0.0, 0);
102  } else if (t > table_values_[last_idx].t_) {
103  WarningOut().fmt("Value of stamp {} is out of range of TableFunction: <{}, {}>. Extrapolation of maximal value will be used.",
104  t, table_values_[0].t_, table_values_[last_idx].t_);
105  this->interpolated(1.0, last_idx-1);
106  } else {
107  for (unsigned int i=0; i<last_idx; ++i) {
108  if (t >= table_values_[i].t_ && t <= table_values_[i+1].t_) {
109  double coef = (t - table_values_[i].t_) / (table_values_[i+1].t_ - table_values_[i].t_);
110  this->interpolated(coef, i);
111  break;
112  }
113  }
114  }
115  }
116 
117  return this->r_value_;
118 }
119 
120 template <class Value>
121 void TableFunction<Value>::interpolated(double coef, unsigned int idx)
122 {
123  ASSERT(coef >= 0 && coef <= 1)(coef).error();
124  ASSERT(idx >= 0 && idx <= table_values_.size()-2)(idx).error();
125 
126  Value val_0(table_values_[idx].r_value_);
127  Value val_1(table_values_[idx+1].r_value_);
128  if (Value::is_scalable())
129  for( unsigned int row=0; row<value_.n_rows(); row++)
130  for( unsigned int col=0; col<value_.n_cols(); col++) {
131  value_(row,col) = val_0(row,col) + coef * (val_1(row,col) - val_0(row,col));
132  }
133 }
134 
135 
136 // Instantiation of TableFunction class
137 template class TableFunction<FieldValue<0>::Enum >;
138 template class TableFunction<FieldValue<0>::Integer >;
139 template class TableFunction<FieldValue<0>::Scalar >;
140 template class TableFunction<FieldValue<0>::Vector >; // temporary solution for computing more fields at once in python
TableFunction::initialized
bool initialized()
Return true if TableFunction is initialized (method init_from_input was called).
Definition: table_function.cc:86
time_governor.hh
Basic time management class.
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
Input::Record::val
const Ret val(const string &key) const
Definition: accessors_impl.hh:31
value
static constexpr bool value
Definition: json.hpp:87
Input::Type::Tuple
Tuple type proxy class.
Definition: type_tuple.hh:45
TimeStep::read_time
double read_time(Input::Iterator< Input::Tuple > time_it, double default_time=std::numeric_limits< double >::quiet_NaN()) const
Definition: time_governor.cc:262
TableFunction::interpolated
void interpolated(double coef, unsigned int idx)
Definition: table_function.cc:121
Input::Array::begin
Iterator< ValueType > begin() const
Definition: accessors_impl.hh:145
Input::Record::address_string
string address_string() const
Definition: accessors.cc:184
table_function.hh
Input::Iterator
Definition: accessors.hh:143
TableFunction
Definition: table_function.hh:38
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
TableFunction::TableValue
Store value in one t stamp.
Definition: table_function.hh:44
Input::Type::Tuple::close
const Tuple & close() const
Close the Tuple for further declarations of keys.
Definition: type_tuple.cc:70
Input::Type::Record::allow_auto_conversion
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:133
TimeStep
Representation of one time step..
Definition: time_governor.hh:123
Input::Type::Default::obligatory
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
Input::Type::Tuple::declare_key
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:173
Input::Type::Record::declare_key
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:503
TableFunction::return_type
Value::return_type return_type
Definition: table_function.hh:41
logger.hh
Input::Type::Record::close
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
Input::Type
Definition: balance.hh:41
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
Value
@ Value
Definition: finite_element.hh:43
input_type.hh
TableFunction::get_input_type
static const Input::Type::Record & get_input_type()
Definition: table_function.cc:44
Input::Type::Array
Class for declaration of inputs sequences.
Definition: type_base.hh:339
Input::Array
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
Input::Tuple
Accessor to the data with type Type::Tuple.
Definition: accessors.hh:411
TableFunction::value
const return_type & value(double t)
Definition: table_function.cc:92
TableFunction::init_from_input
void init_from_input(const Input::Record &rec, const TimeStep &time)
Initialize actual values of the field given from the given Input::Record rec.
Definition: table_function.cc:63
TableFunction::TableFunction
TableFunction()
Default constructor.
Definition: table_function.cc:56
TimeGovernor::get_input_time_type
static const Input::Type::Tuple & get_input_time_type(double lower_bound=-std::numeric_limits< double >::max(), double upper_bound=std::numeric_limits< double >::max())
Definition: time_governor.cc:47
TableFunction::get_input_type_val
static const Input::Type::Tuple & get_input_type_val()
Definition: table_function.cc:26
Input::Array::end
IteratorBase end() const
Definition: accessors_impl.hh:157