Flow123d  build_with_4.0.3-01443f6
multi_field.impl.hh
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 multi_field.impl.hh
15  * @brief
16  */
17 
18 #ifndef MULTI_FIELD_IMPL_HH_
19 #define MULTI_FIELD_IMPL_HH_
20 
21 
22 #include "multi_field.hh"
24 #include "fields/eval_subset.hh"
25 #include "fields/eval_points.hh"
27 #include "fields/field_set.hh"
28 #include "input/input_exception.hh"
29 #include "io/observe.hh"
30 
31 namespace it = Input::Type;
32 
33 /******************************************************************************************
34  * Implementation of MultiField<...>
35  */
36 
37 template<int spacedim, class Value>
39 : FieldCommon(),
40  no_check_control_field_(nullptr)
41 {
42 // static_assert(Value::NRows_ == 1 && Value::NCols_ == 1, "");
43  this->multifield_ = true;
44  this->set_shape( Value::NRows_, Value::NCols_ );
45 }
46 
47 
48 
49 template<int spacedim, class Value>
51 : FieldCommon(other),
52  sub_fields_(other.sub_fields_),
53  full_input_list_(other.full_input_list_),
54  no_check_control_field_(other.no_check_control_field_)
55 {
56  this->multifield_ = true;
57  this->set_shape( Value::NRows_, Value::NCols_ );
58 }
59 
60 
61 
62 template<int spacedim, class Value>
64 {
65  //ASSERT( flags().match( FieldFlag::input_copy )(this->name())(other.name()).error("Try to assign to non-copy field.");
66  ASSERT_PTR(other.shared_->mesh_).error("Must call set_mesh before assign to other field.\n");
67  ASSERT( !shared_->mesh_ || (shared_->mesh_==other.shared_->mesh_))
68  .error("Assignment between multi fields with different meshes.\n");
69  ASSERT_GT( shared_->comp_names_.size(), 0).error("Vector of component names can't be empty!\n");
70  ASSERT_EQ( shared_->comp_names_.size(), other.shared_->comp_names_.size())
71  .error("Both multi fields must have same size of vectors of component names.\n");
72 
73  // check for self assignement
74  if (&other == this) return *this;
75 
76  // class members derived from FieldCommon
77  std::vector< std::string > comp_names = shared_->comp_names_; // keep component names
78  shared_ = other.shared_;
79  shared_->comp_names_ = comp_names;
80  shared_->is_fully_initialized_ = false;
81  set_time_result_ = other.set_time_result_;
82  last_time_ = other.last_time_;
83  last_limit_side_ = other.last_limit_side_;
84  is_jump_time_ = other.is_jump_time_;
85  component_index_ = other.component_index_;
86  this->multifield_ = true;
87  this->shape_ = other.shape_;
88 
89  // class members of Field class
90  if ( size() == 0 ) {
91  // move subfields from other, set correct names
92  sub_fields_.clear();
93  sub_fields_.reserve( other.size() );
94  for (unsigned int i=0; i<other.size(); ++i) {
95  sub_fields_.push_back( other.sub_fields_[i] );
96  if (this->shared_->comp_names_[i].length() == 0)
97  THROW( Input::ExcInputMessage() << EI_Message("The field " + this->input_name()
98  + " has set empty name of component.") );
99  else {
100  sub_fields_[i].name_ = this->shared_->comp_names_[i] + "_" + name();
101  }
102  }
103  } else {
104  THROW( ExcMessage() << EI_Message("Internal error. Assignment operator can't be used after call setup_component() method.") );
105  }
106  full_input_list_ = other.full_input_list_;
107  no_check_control_field_ = other.no_check_control_field_;
108 
109  return *this;
110 }
111 
112 
113 
114 template<int spacedim, class Value>
116  ASSERT_PERMANENT(false).error("This method can't be used for MultiField");
117 
118  it::Abstract abstract = it::Abstract();
120  return inst;
121 }
122 
123 
124 template<int spacedim, class Value>
126  it::Array type = it::Array( SubFieldBaseType::get_input_type_instance(shared_->input_element_selection_), 1);
127  return type;
128 }
129 
130 
131 template<int spacedim, class Value>
133  const MultiField<spacedim, typename FieldValue<spacedim>::Enum > &control_field,
134  const vector<FieldEnum> &value_list) -> MultiField &
135 {
136  no_check_control_field_=&control_field;
137  shared_->no_check_values_=value_list;
138  return *this;
139 }
140 
141 
142 template<int spacedim, class Value>
144  const TimeStep &time, LimitSide limit_side)
145 {
146  // initialization of Multifield for first call
147  if (sub_fields_.size() == 0) {
148  setup_components();
149  }
150 
151  // set time for sub fields
152  set_time_result_ = TimeStatus::constant;
153  is_jump_time_=false;
154  for( SubFieldType &field : sub_fields_) {
155  if (field.set_time(time, limit_side))
156  set_time_result_ = TimeStatus::changed;
157  is_jump_time_ = is_jump_time_ || field.is_jump_time();
158  }
159  return (set_time_result_ == TimeStatus::changed);
160 }
161 
162 
163 
164 template<int spacedim, class Value>
166  // test if mesh is not set
167  if (shared_->mesh_ && shared_->mesh_ != &mesh) {
168  THROW(ExcFieldMeshDifference() << EI_Field(name()) );
169  }
170 
171  shared_->mesh_ = &mesh;
172 }
173 
174 
175 template<int spacedim, class Value>
177  ASSERT( flags().match(FieldFlag::equation_input))(other.name())(this->name())
178  .error("Can not copy to the non-copy field.");
179 
180  // do not use copy if the field have its own input
181  if ( flags().match(FieldFlag::declare_input)
182  && this->shared_->input_list_.size() != 0 ) return;
183 
184  if (typeid(other) == typeid(*this)) {
185  auto const &other_field = dynamic_cast< MultiField<spacedim, Value> const &>(other);
186  this->operator=(other_field);
187  } else if (typeid(other) == typeid(SubFieldType)) {
188  auto const &other_field = dynamic_cast< SubFieldType const &>(other);
189  sub_fields_.resize(1);
190  sub_fields_[0] = other_field;
191  }
192 }
193 
194 
195 
196 template<int spacedim, class Value>
197 void MultiField<spacedim, Value>::field_output(std::shared_ptr<OutputTime> stream, OutputTime::DiscreteSpace type)
198 {
199  ASSERT_LT( type, OutputTime::N_DISCRETE_SPACES ).error();
200 
201  for (unsigned long index=0; index < this->size(); index++) {
202  sub_fields_[index].compute_field_data( type, stream );
203  }
204 }
205 
206 
207 
208 
209 template<int spacedim, class Value>
211  bool const_all=true;
212  for(auto &field : sub_fields_) const_all = const_all && field.is_constant(reg);
213  return const_all;
214 }
215 
216 template<int spacedim, class Value>
218 {
219  ASSERT(true).error("Not used yet. Test it.");
220 
221  FieldResult result_all = result_none;
222  for(auto &field : sub_fields_) {
223  FieldResult sub_result = field.field_result(region_set);
224  if (sub_result == result_none) return result_none;
225 
226  if (result_all == result_none) // first subfield
227  result_all = sub_result;
228  else if (sub_result == result_other || result_all == result_other)
229  result_all = result_other;
230  else if (sub_result != result_all)
231  result_all = result_constant; // all subfields are (possibly different) constants
232  }
233 
234  return result_all;
235 
236 }
237 
238 
239 template<int spacedim, class Value>
241 {
242  int nrows = Value::NRows_;
243  int ncols = Value::NCols_;
244  string type = "Integer";
246  type = "Double";
247 
248  return fmt::format("{{ \"subfields\": true, \"shape\": [ {}, {} ], \"type\": \"{}\", \"limit\": [ {}, {} ] }}",
249  nrows, ncols, type, this->limits().first, this->limits().second);
250 }
251 
252 
253 template<int spacedim, class Value>
255  unsigned int comp_size = this->shared_->comp_names_.size();
256  ASSERT_GT(comp_size, 0).error("Vector of component names is empty!\n");
257  ASSERT_PTR(this->shared_->mesh_).error("Mesh is not set!\n");
258 
259  sub_fields_.reserve( comp_size );
260  for(unsigned int i_comp=0; i_comp < comp_size; i_comp++)
261  {
262  sub_fields_.push_back( SubFieldType(i_comp, name(), this->full_comp_name(i_comp)) );
263  sub_fields_[i_comp].units( units() );
264  if (no_check_control_field_ != nullptr && no_check_control_field_->size() == sub_fields_.size())
265  sub_fields_[i_comp].disable_where((*no_check_control_field_)[i_comp], shared_->no_check_values_);
266  sub_fields_[i_comp].set_mesh( *(shared_->mesh_) );
267 // sub_fields_[i_comp].set_limit_side(this->limit_side_);
268  sub_fields_[i_comp].input_selection(shared_->input_element_selection_);
269  sub_fields_[i_comp].add_factory( std::make_shared<MultiFieldFactory>(i_comp) );
270 
271  if (this->shared_->input_default_!="") {
272  sub_fields_[i_comp].shared_->input_default_ = this->shared_->input_default_;
273  }
274 
275  sub_fields_[i_comp].flags_ = this->flags_;
276  sub_fields_[i_comp].set_input_list(this->full_input_list_, *tg_);
277  sub_fields_[i_comp].set_default_fieldset( *(this->shared_->default_fieldset_) );
278  }
279 }
280 
281 
282 
283 template<int spacedim, class Value>
285  if (! flags().match(FieldFlag::declare_input)) return;
286 
287  // Check sizes of Arrays defined MultiField in field descriptors
289  it != list.end();
290  ++it) {
291  Input::Array mf_array;
292  if ( it->opt_val(this->input_name(), mf_array) ) {
293  unsigned int comp_size = this->shared_->comp_names_.size();
294  if (mf_array.size() != 1 && mf_array.size() != comp_size)
295  THROW( Exc_InvalidMultiFieldSize() << EI_MultiFieldName(this->input_name())
296  << EI_Size(mf_array.size()) << EI_ExpectedSize(comp_size) << list.ei_address() );
297  }
298  }
299 
300  this->full_input_list_ = list;
301  this->tg_ = &tg;
302 
303  // Save the full array for future use in FieldCommon::mark_input_times().
304  list.copy_to(shared_->input_list_);
305 }
306 
307 
308 
309 template<int spacedim, class Value>
311  Input::Array multifield_arr;
312  if (descriptor_rec.opt_val(field.input_name(), multifield_arr))
313  {
314  //ASSERT_PERMANENT(multifield_arr.size() == 1 || multifield_arr.size() == field.n_comp() )(field.input_name())
315  // .error("Invalid size of Array defined for MultiField!\n";
316  unsigned int position = 0;
317  auto it = multifield_arr.begin<Input::AbstractRecord>();
318  if (multifield_arr.size() > 1)
319  while (index_ != position) {
320  ++it; ++position;
321  }
322 
323  FieldAlgoBaseInitData init_data(field.input_name(), field.n_comp(), field.units(), field.limits(), field.get_flags());
325  field_algo_base->set_component_idx(index_);
326  return field_algo_base;
327  }
328 
329  return NULL;
330 }
331 
332 
333 
334 template<int spacedim, class Value>
336  return in_rec.find<Input::Array>(input_name);
337 }
338 
339 
340 template<int spacedim, class Value>
342  ASSERT_PERMANENT(false).error("Set dependency of MultiField should be performed by individual components!\n");
344 }
345 
346 
347 
348 template<int spacedim, class Value>
349 void MultiField<spacedim, Value>::cache_reallocate(FMT_UNUSED const ElementCacheMap &cache_map, FMT_UNUSED unsigned int region_idx) const {
350  //for(auto &field : sub_fields_) field.cache_reallocate(cache_map);
351  ASSERT_PERMANENT(false).error("Cache reallocate of MultiField should be performed by individual components!\n");
352 }
353 
354 
355 template<int spacedim, class Value>
357  FMT_UNUSED unsigned int region_patch_idx) const {
358  ASSERT_PERMANENT(false).error("Cache update of MultiField should be performed by individual components!\n");
359 }
360 
361 
362 template<int spacedim, class Value>
365  double time,
366  std::vector<std::string> region_set_names)
367 {
368  unsigned int comp_size = this->shared_->comp_names_.size();
369  ASSERT_GT(comp_size, 0).error("Vector of component names is empty!\n");
370  ASSERT_EQ(comp_size, field_vec.size());
371  ASSERT_PTR(this->shared_->mesh_).error("Mesh is not set!\n");
372 
373  sub_fields_.reserve( comp_size );
374  for(unsigned int i_comp=0; i_comp < comp_size; i_comp++)
375  {
376  sub_fields_.push_back( SubFieldType(i_comp, name(), "") );
377  sub_fields_[i_comp].set_mesh( *(shared_->mesh_) );
378  sub_fields_[i_comp].flags_ = this->flags_;
379  sub_fields_[i_comp].set(field_vec[i_comp], time, region_set_names);
380  }
381 }
382 
383 
384 template<int spacedim, class Value>
387  double time,
388  std::vector<std::string> region_set_names)
389 {
390  ASSERT_EQ(this->shared_->comp_names_.size(), 1).error("Size of component names vector must be 1!\n");
391  ASSERT_PTR(this->shared_->mesh_).error("Mesh is not set!\n");
392 
393  sub_fields_.reserve(1);
394  sub_fields_.push_back( SubFieldType(0, name(), "") );
395  sub_fields_[0].set_mesh( *(shared_->mesh_) );
396  sub_fields_[0].flags_ = this->flags_;
397  sub_fields_[0].set(field, time, region_set_names);
398 }
399 
400 
401 
402 #endif /* MULTI_FIELD_IMPL_HH_ */
#define ASSERT(expr)
Definition: asserts.hh:351
#define ASSERT_PERMANENT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:348
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:301
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:333
#define ASSERT_GT(a, b)
Definition of comparative assert macro (Greater Than) only for debug mode.
Definition: asserts.hh:317
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
Directing class of FieldValueCache.
static std::shared_ptr< FieldAlgorithmBase< spacedim, Value > > function_factory(const Input::AbstractRecord &rec, const struct FieldAlgoBaseInitData &init_data)
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:77
std::shared_ptr< SharedData > shared_
TimeStatus set_time_result_
unsigned int component_index_
const std::string & input_name() const
FieldFlag::Flags flags_
Field flags. Default setting is "an equation input field, that can read from user input,...
const std::string & name() const
LimitSide last_limit_side_
std::vector< uint > shape_
double last_time_
FieldCommon & name(const string &name)
std::pair< double, double > limits() const
FieldFlag::Flags get_flags() const
double time() const
void set_shape(uint n_rows, uint n_cols)
bool is_jump_time_
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
unsigned int n_comp() const
static constexpr Mask declare_input
The field can be set from input. The key in input field descriptor is declared. (default on)
Definition: field_flag.hh:35
static constexpr Mask equation_input
The field is data parameter of the owning equation. (default on)
Definition: field_flag.hh:33
Class template representing a field with values dependent on: point, element, and region.
Definition: field.hh:92
std::shared_ptr< FieldBaseType > FieldBasePtr
Definition: field.hh:96
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
Iterator< ValueType > begin() const
unsigned int size() const
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
bool opt_val(const string &key, Ret &value) const
Iterator< Ret > find(const string &key) const
Class for declaration of polymorphic Record.
Class for declaration of inputs sequences.
Definition: type_base.hh:339
Helper class that stores data of generic types.
Definition: type_generic.hh:89
Definition: mesh.h:362
virtual Field< spacedim, Value >::FieldBasePtr create_field(Input::Record rec, const FieldCommon &field)
bool is_active_field_descriptor(const Input::Record &in_rec, const std::string &input_name) override
Class for representation of a vector of fields of the same physical quantity.
Definition: multi_field.hh:87
std::string get_value_attribute() const override
Input::Array full_input_list_
Full list of input field descriptors from which the subfields of MultiField are set.
Definition: multi_field.hh:283
void copy_from(const FieldCommon &other) override
std::vector< const FieldCommon * > set_dependency(unsigned int i_reg) const override
void field_output(std::shared_ptr< OutputTime > stream, OutputTime::DiscreteSpace type) override
bool set_time(const TimeStep &time, LimitSide limit_side) override
void setup_components()
MultiField & operator=(const MultiField &other)
void cache_update(ElementCacheMap &cache_map, unsigned int region_patch_idx) const override
Implements FieldCommon::cache_update.
unsigned int size() const
Number of subfields that compose the multi-field.
Definition: multi_field.hh:200
IT::Instance get_input_type() override
auto disable_where(const MultiField< spacedim, typename FieldValue< spacedim >::Enum > &control_field, const vector< FieldEnum > &value_list) -> MultiField &
std::vector< SubFieldType > sub_fields_
Subfields (items) of MultiField.
Definition: multi_field.hh:280
Field< spacedim, Value > SubFieldType
Definition: multi_field.hh:90
void set_input_list(const Input::Array &list, const TimeGovernor &tg) override
void cache_reallocate(const ElementCacheMap &cache_map, unsigned int region_idx) const override
Implements FieldCommon::cache_reallocate.
IT::Array get_multifield_input_type() override
void set_mesh(const Mesh &mesh) override
const MultiField< spacedim, typename FieldValue< spacedim >::Enum > * no_check_control_field_
Definition: multi_field.hh:295
void set(std::vector< typename Field< spacedim, Value >::FieldBasePtr > field_vec, double time, std::vector< std::string > region_set_names={"ALL"})
FieldResult field_result(RegionSet region_set) const override
Indicates special field states.
bool is_constant(Region reg) override
static const unsigned int N_DISCRETE_SPACES
Definition: output_time.hh:107
Basic time management functionality for unsteady (and steady) solvers (class Equation).
Representation of one time step..
FieldResult
@ result_other
@ result_constant
@ result_none
LimitSide
Definition: field_common.hh:64
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
static constexpr bool value
Definition: json.hpp:87
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
#define FMT_UNUSED
Definition: posix.h:75
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.