Flow123d  JS_before_hm-979-g397e552
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 #include "system/armor.hh"
23 
24 
25 /// Implementation.
26 
27 namespace it = Input::Type;
28 
29 FLOW123D_FORCE_LINK_IN_CHILD(field_constant)
30 
31 
32 template <int spacedim, class Value>
33 const Input::Type::Record & FieldConstant<spacedim, Value>::get_input_type()
34 {
35  return it::Record("FieldConstant", FieldAlgorithmBase<spacedim,Value>::template_name()+" Field constant in space.")
38  .declare_key("value", Value::get_input_type(), it::Default::obligatory(),
39  "Value of the constant field. "
40  "For vector values, you can use scalar value to enter constant vector. "
41  "For square (($N\\times N$))-matrix values, you can use: "
42  " - vector of size (($N$)) to enter diagonal matrix\n\n"
43  " - vector of size (($\\frac12N(N+1)$)) to enter symmetric matrix (upper triangle, row by row)\n"
44  " - scalar to enter multiple of the unit matrix." )
45  //.declare_key("unit", FieldAlgorithmBase<spacedim, Value>::get_input_type_unit_si(), it::Default::optional(),
46  // "Definition of unit.")
47  .allow_auto_conversion("value")
48  .close();
49 }
50 
51 template <int spacedim, class Value>
53  Input::register_class< FieldConstant<spacedim, Value>, unsigned int >("FieldConstant") +
55 
56 
57 template <int spacedim, class Value>
59 : FieldAlgorithmBase<spacedim, Value>(n_comp)
60 {
61  this->is_constant_in_space_ = true;
62 }
63 
64 
65 template <int spacedim, class Value>
67 {
68  this->r_value_ = val;
69 
70  return *this;
71 }
72 
73 
74 template <int spacedim, class Value>
76  this->init_unit_conversion_coefficient(rec, init_data);
77 
78 
79  this->value_.init_from_input( rec.val<typename Value::AccessType>("value") );
80  this->value_.scale(this->unit_conversion_coefficient_);
81  this->check_field_limits(rec, init_data);
82 
83  typename Value::return_type tmp_value;
84  Value tmp_field_value(tmp_value);
85  tmp_field_value.set_n_comp(this->n_comp());
86 
87  tmp_field_value.zeros();
88  if ( this->value_.equal_to(tmp_value) ) {
90  return;
91  }
92 
93 
94  tmp_field_value.ones();
95  if ( this->value_.equal_to(tmp_value) ) {
96  this->field_result_ = result_ones;
97  return;
98  }
99 
100  // This check must be the last one, since for scalar and vector values ones() == eye().
101  // For vector, eye() does nothing. So, the value of tmp_value remains equal to ones().
102  tmp_field_value.eye();
103  if ( this->value_.equal_to(tmp_value) ) {
104  this->field_result_ = result_eye;
105  return;
106  }
107 
108 
110 }
111 
112 
113 
114 /**
115  * Returns one value in one given point. ResultType can be used to avoid some costly calculation if the result is trivial.
116  */
117 template <int spacedim, class Value>
118 typename Value::return_type const & FieldConstant<spacedim, Value>::value(const Point &, const ElementAccessor<spacedim> &)
119 {
120  return this->r_value_;
121 }
122 
123 
124 
125 /**
126  * Returns std::vector of scalar values in several points at once.
127  */
128 template <int spacedim, class Value>
131 {
132  OLD_ASSERT_EQUAL( point_list.size(), value_list.size() );
133  ASSERT_DBG(point_list.n_rows() == spacedim && point_list.n_cols() == 1).error("Invalid point size.\n");
134 
135  for(unsigned int i=0; i< point_list.size(); i++) {
136  OLD_ASSERT( Value(value_list[i]).n_rows()==this->value_.n_rows(),
137  "value_list[%d] has wrong number of rows: %d; should match number of components: %d\n",
138  i, Value(value_list[i]).n_rows(),this->value_.n_rows());
139 
140 
141  value_list[i]=this->r_value_;
142  }
143 }
144 
145 
146 template <int spacedim, class Value>
148  ElementCacheMap &cache_map, unsigned int region_idx)
149 {
150  auto update_cache_data = cache_map.update_cache_data();
151  unsigned int region_in_cache = update_cache_data.region_cache_indices_range_.find(region_idx)->second;
152  unsigned int i_cache_el_begin = update_cache_data.region_value_cache_range_[region_in_cache];
153  unsigned int i_cache_el_end = update_cache_data.region_value_cache_range_[region_in_cache+1];
154  Armor::ArmaMat<typename Value::element_type, Value::NRows_, Value::NCols_> mat_value( const_cast<typename Value::element_type*>(this->value_.mem_ptr()) );
155  for (unsigned int i_cache = i_cache_el_begin; i_cache < i_cache_el_end; ++i_cache)
156  data_cache.data().set(i_cache) = mat_value;
157 }
158 
159 
160 template <int spacedim, class Value>
162 {
163  if (Value::is_scalable())
164  for( unsigned int row=0; row<this->value_.n_rows(); row++)
165  for( unsigned int col=0; col<this->value_.n_cols(); col++) {
166  if ( (this->value_(row,col) < init_data.limits_.first) || (this->value_(row,col) > init_data.limits_.second) ) {
167  WarningOut().fmt("Value '{}' of Field '{}' at address '{}' is out of limits: <{}, {}>\nUnit of the Field: [{}]\n",
168  this->value_(row,col), init_data.field_name_, rec.address_string(),
169  init_data.limits_.first, init_data.limits_.second, init_data.unit_si_.format_text() );
170  }
171  }
172 }
173 
174 
175 
176 template <int spacedim, class Value>
178 }
179 
180 
181 // Instantiations of FieldConstant
183 
184 // temporary solution for computing more fields at once in python
185 template class FieldConstant<3, FieldValue<0>::Vector >; // Necessary due to default value of the abstract.
186 
void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Init value of unit_conversion_coefficient_ from input.
void check_field_limits(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Compare field value with given minimal and maximal limits.
FieldConstant< spacedim, Value > & set_value(const typename Value::return_type &val)
unsigned int size() const
Definition: armor.hh:718
virtual void value_list(const Armor::array &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list) override
std::string format_text() const
Definition: unit_si.cc:127
const UpdateCacheHelper & update_cache_data() const
Return update cache data helper.
FieldResult field_result_
Indicator of particular values (zero, one) constant over space.
Abstract linear system class.
Definition: balance.hh:40
void cache_update(FieldValueCache< typename Value::element_type > &data_cache, ElementCacheMap &cache_map, unsigned int region_idx) override
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
#define INSTANCE_ALL(field)
Directing class of FieldValueCache.
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
uint n_cols() const
Definition: armor.hh:710
Value::return_type r_value_
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm) override
double unit_conversion_coefficient_
Coeficient of conversion of user-defined unit.
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:196
#define OLD_ASSERT(...)
Definition: global_defs.h:131
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
#define FMT_UNUSED
Definition: posix.h:75
const Armor::Array< elm_type > & data() const
Return data vector.
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
const Ret val(const string &key) const
std::unordered_map< unsigned int, unsigned int > region_cache_indices_range_
FieldConstant(unsigned int n_comp=0)
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
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data) override
ArrayMatSet set(uint index)
Definition: armor.hh:821
uint n_rows() const
Definition: armor.hh:705
Space< spacedim >::Point Point
typename arma::Mat< Type >::template fixed< nr, nc > ArmaMat
Definition: armor.hh:502
Record & copy_keys(const Record &other)
Copy keys from other record.
Definition: type_record.cc:216
const UnitSI & unit_si_
virtual ~FieldConstant()
bool is_constant_in_space_
Flag detects that field is only dependent on time.
Value value_
Last value, prevents passing large values (vectors) by value.
#define ASSERT_DBG(expr)
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:258
#define OLD_ASSERT_EQUAL(a, b)
Definition: global_defs.h:133
std::pair< double, double > limits_
Record type proxy class.
Definition: type_record.hh:182
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180
unsigned int n_comp() const
string address_string() const
Definition: accessors.cc:184