Flow123d  release_3.0.0-973-g92f55e826
field_constant.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 field_constant.cc
15  * @brief
16  */
17 
18 #include "fields/field_constant.hh"
20 #include "fields/field_instances.hh" // for instantiation macros
21 #include "input/input_type.hh"
22 
23 
24 /// Implementation.
25 
26 namespace it = Input::Type;
27 
28 FLOW123D_FORCE_LINK_IN_CHILD(field_constant)
29 
30 
31 template <int spacedim, class Value>
33 {
34  return it::Record("FieldConstant", FieldAlgorithmBase<spacedim,Value>::template_name()+" Field constant in space.")
37  .declare_key("value", Value::get_input_type(), it::Default::obligatory(),
38  "Value of the constant field. "
39  "For vector values, you can use scalar value to enter constant vector. "
40  "For square (($N\\times N$))-matrix values, you can use: "
41  " - vector of size (($N$)) to enter diagonal matrix\n\n"
42  " - vector of size (($\\frac12N(N+1)$)) to enter symmetric matrix (upper triangle, row by row)\n"
43  " - scalar to enter multiple of the unit matrix." )
44  //.declare_key("unit", FieldAlgorithmBase<spacedim, Value>::get_input_type_unit_si(), it::Default::optional(),
45  // "Definition of unit.")
46  .allow_auto_conversion("value")
47  .close();
48 }
49 
50 template <int spacedim, class Value>
52  Input::register_class< FieldConstant<spacedim, Value>, unsigned int >("FieldConstant") +
54 
55 
56 template <int spacedim, class Value>
58 : FieldAlgorithmBase<spacedim, Value>(n_comp)
59 {
60  this->is_constant_in_space_ = true;
61 }
62 
63 
64 template <int spacedim, class Value>
66 {
67  this->r_value_ = val;
68 
69  return *this;
70 }
71 
72 
73 template <int spacedim, class Value>
75  this->init_unit_conversion_coefficient(rec, init_data);
76 
77 
78  this->value_.init_from_input( rec.val<typename Value::AccessType>("value") );
79  this->value_.scale(this->unit_conversion_coefficient_);
80  this->check_field_limits(rec, init_data);
81 
82  typename Value::return_type tmp_value;
83  Value tmp_field_value(tmp_value);
84  tmp_field_value.set_n_comp(this->n_comp());
85 
86  tmp_field_value.zeros();
87  if ( this->value_.equal_to(tmp_value) ) {
88  this->field_result_ = result_zeros;
89  return;
90  }
91 
92 
93  tmp_field_value.ones();
94  if ( this->value_.equal_to(tmp_value) ) {
95  this->field_result_ = result_ones;
96  return;
97  }
98 
99  // This check must be the last one, since for scalar and vector values ones() == eye().
100  // For vector, eye() does nothing. So, the value of tmp_value remains equal to ones().
101  tmp_field_value.eye();
102  if ( this->value_.equal_to(tmp_value) ) {
103  this->field_result_ = result_eye;
104  return;
105  }
106 
107 
108  this->field_result_ = result_constant;
109 }
110 
111 
112 
113 /**
114  * Returns one value in one given point. ResultType can be used to avoid some costly calculation if the result is trivial.
115  */
116 template <int spacedim, class Value>
117 typename Value::return_type const & FieldConstant<spacedim, Value>::value(const Point &p, const ElementAccessor<spacedim> &elm)
118 {
119  return this->r_value_;
120 }
121 
122 
123 
124 /**
125  * Returns std::vector of scalar values in several points at once.
126  */
127 template <int spacedim, class Value>
130 {
131  OLD_ASSERT_EQUAL( point_list.size(), value_list.size() );
132 
133  for(unsigned int i=0; i< point_list.size(); i++) {
134  OLD_ASSERT( Value(value_list[i]).n_rows()==this->value_.n_rows(),
135  "value_list[%d] has wrong number of rows: %d; should match number of components: %d\n",
136  i, Value(value_list[i]).n_rows(),this->value_.n_rows());
137 
138 
139  value_list[i]=this->r_value_;
140  }
141 }
142 
143 
144 template <int spacedim, class Value>
146 {
147  if (Value::is_scalable())
148  for( unsigned int row=0; row<this->value_.n_rows(); row++)
149  for( unsigned int col=0; col<this->value_.n_cols(); col++) {
150  if ( (this->value_(row,col) < init_data.limits_.first) || (this->value_(row,col) > init_data.limits_.second) ) {
151  WarningOut().fmt("Value '{}' of Field '{}' at address '{}' is out of limits: <{}, {}>\nUnit of the Field: [{}]\n",
152  this->value_(row,col), init_data.field_name_, rec.address_string(),
153  init_data.limits_.first, init_data.limits_.second, init_data.unit_si_.format_text() );
154  }
155  }
156 }
157 
158 
159 
160 template <int spacedim, class Value>
162 }
163 
164 
165 // Instantiations of FieldConstant
167 
168 // temporary solution for computing more fields at once in python
169 template class FieldConstant<3, FieldValue<0>::Vector >; // Necessary due to default value of the abstract.
170 
OLD_ASSERT_EQUAL
#define OLD_ASSERT_EQUAL(a, b)
Definition: global_defs.h:133
result_zeros
@ result_zeros
Definition: field_algo_base.hh:72
field_constant.hh
UnitSI::format_text
std::string format_text() const
Definition: unit_si.cc:127
FieldAlgoBaseInitData::field_name_
std::string field_name_
Definition: field_algo_base.hh:89
Input::Record::val
const Ret val(const string &key) const
Definition: accessors_impl.hh:31
FieldAlgoBaseInitData::unit_si_
const UnitSI & unit_si_
Definition: field_algo_base.hh:91
FieldConstant
Definition: field_constant.hh:43
FLOW123D_FORCE_LINK_IN_CHILD
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180
FieldAlgorithmBase::is_constant_in_space_
bool is_constant_in_space_
Flag detects that field is only dependent on time.
Definition: field_algo_base.hh:271
std::vector
Definition: doxy_dummy_defs.hh:7
ElementAccessor
Definition: fe_value_handler.hh:29
FieldAlgorithmBase::Point
Space< spacedim >::Point Point
Definition: field_algo_base.hh:113
Input::Record::address_string
string address_string() const
Definition: accessors.cc:184
FieldConstant::init_from_input
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Definition: field_constant.cc:74
result_constant
@ result_constant
Definition: field_algo_base.hh:71
Input::Type::Record::derive_from
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:195
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
FieldAlgoBaseInitData::limits_
std::pair< double, double > limits_
Definition: field_algo_base.hh:92
FieldAlgoBaseInitData
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
Definition: field_algo_base.hh:79
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:132
FieldConstant::FieldConstant
FieldConstant(unsigned int n_comp=0)
Definition: field_constant.cc:57
Input::Type::Default::obligatory
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
result_ones
@ result_ones
Definition: field_algo_base.hh:73
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:501
field_algo_base.impl.hh
Input::Type::Record::close
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:303
Input::Type
Definition: balance.hh:38
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
Value
@ Value
Definition: finite_element.hh:47
input_type.hh
Input::Type::Record::copy_keys
Record & copy_keys(const Record &other)
Copy keys from other record.
Definition: type_record.cc:215
FieldAlgorithmBase
Definition: field_algo_base.hh:110
OLD_ASSERT
#define OLD_ASSERT(...)
Definition: global_defs.h:131
FieldConstant::get_input_type
static const Input::Type::Record & get_input_type()
Implementation.
Definition: field_constant.cc:32
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:246
INSTANCE_ALL
#define INSTANCE_ALL(field)
Definition: field_instances.hh:43
field_instances.hh
FieldConstant::value_list
virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list)
Definition: field_constant.cc:128
FieldConstant::check_field_limits
void check_field_limits(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Compare field value with given minimal and maximal limits.
Definition: field_constant.cc:145
FieldConstant::value
virtual const Value::return_type & value(const Point &p, const ElementAccessor< spacedim > &elm)
Definition: field_constant.cc:117
FieldConstant::set_value
FieldConstant< spacedim, Value > & set_value(const typename Value::return_type &val)
Definition: field_constant.cc:65
FieldConstant::~FieldConstant
virtual ~FieldConstant()
Definition: field_constant.cc:161
result_eye
@ result_eye
Definition: field_algo_base.hh:74