Flow123d  3.9.0-3aaaea010
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>
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) ) {
89  this->field_result_ = result_zeros;
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 
109  this->field_result_ = result_constant;
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  ASSERT_EQ( point_list.size(), value_list.size() );
133  ASSERT(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  ASSERT_EQ( Value(value_list[i]).n_rows(), this->value_.n_rows() )(i)
137  .error("value_list[i] has wrong number of rows\n");
138 
139 
140  value_list[i]=this->r_value_;
141  }
142 }
143 
144 
145 template <int spacedim, class Value>
147  ElementCacheMap &cache_map, unsigned int region_patch_idx)
148 {
149  unsigned int reg_chunk_begin = cache_map.region_chunk_begin(region_patch_idx);
150  unsigned int reg_chunk_end = cache_map.region_chunk_end(region_patch_idx);
151  Armor::ArmaMat<typename Value::element_type, Value::NRows_, Value::NCols_> mat_value( const_cast<typename Value::element_type*>(this->value_.mem_ptr()) );
152  for (unsigned int i_cache = reg_chunk_begin; i_cache < reg_chunk_end; ++i_cache)
153  data_cache.set(i_cache) = mat_value;
154 }
155 
156 
157 template <int spacedim, class Value>
159 {
160  if (Value::is_scalable())
161  for( unsigned int row=0; row<this->value_.n_rows(); row++)
162  for( unsigned int col=0; col<this->value_.n_cols(); col++) {
163  if ( (this->value_(row,col) < init_data.limits_.first) || (this->value_(row,col) > init_data.limits_.second) ) {
164  WarningOut().fmt("Value '{}' of Field '{}' at address '{}' is out of limits: <{}, {}>\nUnit of the Field: [{}]\n",
165  this->value_(row,col), init_data.field_name_, rec.address_string(),
166  init_data.limits_.first, init_data.limits_.second, init_data.unit_si_.format_text() );
167  }
168  }
169 }
170 
171 
172 
173 template <int spacedim, class Value>
175 }
176 
177 
178 // Instantiations of FieldConstant
180 
181 // temporary solution for computing more fields at once in python
182 template class FieldConstant<3, FieldValue<0>::Vector >; // Necessary due to default value of the abstract.
183 
result_zeros
@ result_zeros
Definition: field_algo_base.hh:74
armor.hh
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:91
FieldConstant::cache_update
void cache_update(FieldValueCache< typename Value::element_type > &data_cache, ElementCacheMap &cache_map, unsigned int region_patch_idx) override
Definition: field_constant.cc:146
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
ElementCacheMap
Directing class of FieldValueCache.
Definition: field_value_cache.hh:151
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:93
FieldConstant
Definition: field_constant.hh:43
FLOW123D_FORCE_LINK_IN_CHILD
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:104
FieldAlgorithmBase::is_constant_in_space_
bool is_constant_in_space_
Flag detects that field is only dependent on time.
Definition: field_algo_base.hh:289
std::vector
Definition: doxy_dummy_defs.hh:7
ElementAccessor
Definition: dh_cell_accessor.hh:32
FieldAlgorithmBase::Point
Space< spacedim >::Point Point
Definition: field_algo_base.hh:115
FieldConstant::init_from_input
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data) override
Definition: field_constant.cc:75
Input::Record::address_string
string address_string() const
Definition: accessors.cc:184
result_constant
@ result_constant
Definition: field_algo_base.hh:73
Armor::Array::n_rows
uint n_rows() const
Definition: armor.hh:715
ElementCacheMap::region_chunk_end
unsigned int region_chunk_end(unsigned int region_patch_idx) const
Return end position of region chunk in FieldValueCache.
Definition: field_value_cache.hh:257
Input::Type::Record::derive_from
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:196
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:94
FieldAlgoBaseInitData
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
Definition: field_algo_base.hh:81
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
FieldConstant::FieldConstant
FieldConstant(unsigned int n_comp=0)
Definition: field_constant.cc:58
Input::Type::Default::obligatory
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
ASSERT_EQ
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:333
result_ones
@ result_ones
Definition: field_algo_base.hh:75
FieldConstant::value_list
virtual void value_list(const Armor::array &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list) override
Definition: field_constant.cc:129
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
field_algo_base.impl.hh
Armor::Array::n_cols
uint n_cols() const
Definition: armor.hh:720
Armor::Array::set
ArrayMatSet set(uint index)
Definition: armor.hh:838
Input::Type::Record::close
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
Armor::Array::size
unsigned int size() const
Definition: armor.hh:728
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
ElementCacheMap::region_chunk_begin
unsigned int region_chunk_begin(unsigned int region_patch_idx) const
Return begin position of region chunk in FieldValueCache.
Definition: field_value_cache.hh:251
Input::Type::Record::copy_keys
Record & copy_keys(const Record &other)
Copy keys from other record.
Definition: type_record.cc:216
FieldAlgorithmBase
Definition: field_algo_base.hh:112
FieldConstant::get_input_type
static const Input::Type::Record & get_input_type()
Implementation.
Definition: field_constant.cc:33
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
INSTANCE_ALL
#define INSTANCE_ALL(field)
Definition: field_instances.hh:43
field_instances.hh
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:158
Armor::Array< double >
FieldConstant::set_value
FieldConstant< spacedim, Value > & set_value(const typename Value::return_type &val)
Definition: field_constant.cc:66
FieldConstant::~FieldConstant
virtual ~FieldConstant()
Definition: field_constant.cc:174
Armor::ArmaMat
typename arma::Mat< Type >::template fixed< nr, nc > ArmaMat
Definition: armor.hh:502
result_eye
@ result_eye
Definition: field_algo_base.hh:76
FieldConstant::value
virtual const Value::return_type & value(const Point &p, const ElementAccessor< spacedim > &elm) override
Definition: field_constant.cc:118
FMT_UNUSED
#define FMT_UNUSED
Definition: posix.h:75