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