Flow123d  release_2.2.0-41-g0958a8d
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 "input/input_exception.hh"
25 #include "io/observe.hh"
26 
27 namespace it = Input::Type;
28 
29 /******************************************************************************************
30  * Implementation of MultiField<...>
31  */
32 
33 template<int spacedim, class Value>
35 : FieldCommon(),
36  no_check_control_field_(nullptr)
37 {
38 // static_assert(Value::NRows_ == 1 && Value::NCols_ == 1, "");
39  this->multifield_ = true;
40  this->shared_->bc_ = bc;
41 }
42 
43 
44 
45 template<int spacedim, class Value>
47 : FieldCommon(other),
48  sub_fields_(other.sub_fields_),
51 {
52  this->multifield_ = true;
53 }
54 
55 
56 
57 template<int spacedim, class Value>
59 {
60  //OLD_ASSERT( flags().match( FieldFlag::input_copy ) , "Try to assign to non-copy field '%s' from the field '%s'.", this->name().c_str(), other.name().c_str());
61  OLD_ASSERT(other.shared_->mesh_, "Must call set_mesh before assign to other field.\n");
62  OLD_ASSERT( !shared_->mesh_ || (shared_->mesh_==other.shared_->mesh_),
63  "Assignment between multi fields with different meshes.\n");
64  OLD_ASSERT( shared_->comp_names_.size(), "Vector of component names can't be empty!\n");
65  OLD_ASSERT( shared_->comp_names_.size()==other.shared_->comp_names_.size(),
66  "Both multi fields must have same size of vectors of component names.\n");
67 
68  // check for self assignement
69  if (&other == this) return *this;
70 
71  // class members derived from FieldCommon
72  std::vector< std::string > comp_names = shared_->comp_names_; // keep component names
73  shared_ = other.shared_;
74  shared_->comp_names_ = comp_names;
75  shared_->is_fully_initialized_ = false;
77  last_time_ = other.last_time_;
81  this->multifield_ = true;
82 
83  // class members of Field class
84  if ( size() == 0 ) {
85  // move subfields from other, set correct names
86  sub_fields_.clear();
87  sub_fields_.reserve( other.size() );
88  for (unsigned int i=0; i<other.size(); ++i) {
89  sub_fields_.push_back( other.sub_fields_[i] );
90  if (this->shared_->comp_names_[i].length() == 0)
91  THROW( Input::ExcInputMessage() << EI_Message("The field " + this->input_name()
92  + " has set empty name of component.") );
93  else {
94  sub_fields_[i].name_ = this->shared_->comp_names_[i] + "_" + name();
95  }
96  }
97  } else {
98  THROW( ExcMessage() << EI_Message("Internal error. Assignment operator can't be used after call setup_component() method.") );
99  }
102 
103  return *this;
104 }
105 
106 
107 
108 template<int spacedim, class Value>
110  OLD_ASSERT(false, "This method can't be used for MultiField");
111 
112  it::Abstract abstract = it::Abstract();
114  return inst;
115 }
116 
117 
118 template<int spacedim, class Value>
120  it::Array type = it::Array( SubFieldBaseType::get_input_type_instance(shared_->input_element_selection_), 1);
121  return type;
122 }
123 
124 
125 template<int spacedim, class Value>
127  const MultiField<spacedim, typename FieldValue<spacedim>::Enum > &control_field,
128  const vector<FieldEnum> &value_list) -> MultiField &
129 {
130  no_check_control_field_=&control_field;
131  shared_->no_check_values_=value_list;
132  return *this;
133 }
134 
135 
136 template<int spacedim, class Value>
138  const TimeStep &time, LimitSide limit_side)
139 {
140  // initialization of Multifield for first call
141  if (sub_fields_.size() == 0) {
143  }
144 
145  // set time for sub fields
147  is_jump_time_=false;
148  for( SubFieldType &field : sub_fields_) {
149  if (field.set_time(time, limit_side))
151  is_jump_time_ = is_jump_time_ || field.is_jump_time();
152  }
154 }
155 
156 
157 
158 template<int spacedim, class Value>
160  // test if mesh is not set
161  if (shared_->mesh_ && shared_->mesh_ != &mesh) {
162  THROW(ExcFieldMeshDifference() << EI_Field(name()) );
163  }
164 
165  shared_->mesh_ = &mesh;
166 }
167 
168 
169 template<int spacedim, class Value>
171  ASSERT( flags().match(FieldFlag::equation_input))(other.name().c_str())(this->name().c_str())
172  .error("Can not copy to the non-copy field.");
173 
174  // do not use copy if the field have its own input
176  && this->shared_->input_list_.size() != 0 ) return;
177 
178  if (typeid(other) == typeid(*this)) {
179  auto const &other_field = dynamic_cast< MultiField<spacedim, Value> const &>(other);
180  this->operator=(other_field);
181  } else if (typeid(other) == typeid(SubFieldType)) {
182  auto const &other_field = dynamic_cast< SubFieldType const &>(other);
183  sub_fields_.resize(1);
184  sub_fields_[0] = other_field;
185  }
186 }
187 
188 
189 
190 template<int spacedim, class Value>
191 void MultiField<spacedim, Value>::field_output(std::shared_ptr<OutputTime> stream)
192 {
193  // currently we cannot output boundary fields
194  if (!is_bc()) {
195  const OutputTime::DiscreteSpace type = this->output_type();
196 
198 
200  for (unsigned long index=0; index < this->size(); index++)
201  for(unsigned int ids=0; ids < OutputTime::N_DISCRETE_SPACES; ids++)
202  if (flags & (1 << ids))
203  sub_fields_[index].compute_field_data( OutputTime::DiscreteSpace(ids), stream );
204  }
205 }
206 
207 
208 
209 template<int spacedim, class Value>
210 void MultiField<spacedim, Value>::observe_output(std::shared_ptr<Observe> observe)
211 {
212  for(auto &field : sub_fields_) field.observe_output(observe);
213 }
214 
215 
216 
217 
218 template<int spacedim, class Value>
220  bool const_all=true;
221  for(auto &field : sub_fields_) const_all = const_all && field.is_constant(reg);
222  return const_all;
223 }
224 
225 template<int spacedim, class Value>
227 {
228  ASSERT_DBG(true).error("Not used yet. Test it.");
229 
230  FieldResult result_all = result_none;
231  for(auto &field : sub_fields_) {
232  FieldResult sub_result = field.field_result(region_set);
233  if (sub_result == result_none) return result_none;
234 
235  if (result_all == result_none) // first subfield
236  result_all = sub_result;
237  else if (sub_result == result_other || result_all == result_other)
238  result_all = result_other;
239  else if (sub_result != result_all)
240  result_all = result_constant; // all subfields are (possibly different) constants
241  }
242 
243  return result_all;
244 
245 }
246 
247 
248 template<int spacedim, class Value>
250 {
251  int nrows = Value::NRows_;
252  int ncols = Value::NCols_;
253  string type = "Integer";
255  type = "Double";
256 
257  return fmt::format("{{ \"subfields\": true, \"shape\": [ {}, {} ], \"type\": \"{}\", \"limit\": [ {}, {} ] }}",
258  nrows, ncols, type, this->limits().first, this->limits().second);
259 }
260 
261 
262 template<int spacedim, class Value>
264  unsigned int comp_size = this->shared_->comp_names_.size();
265  string full_name;
266  OLD_ASSERT(comp_size, "Vector of component names is empty!\n");
267  OLD_ASSERT(this->shared_->mesh_, "Mesh is not set!\n");
268 
269  sub_fields_.reserve( comp_size );
270  for(unsigned int i_comp=0; i_comp < comp_size; i_comp++)
271  {
272  if (this->shared_->comp_names_[i_comp].length() == 0)
273  full_name = name();
274  else {
275  full_name = this->shared_->comp_names_[i_comp] + "_" + name();
276  }
277 
278  sub_fields_.push_back( SubFieldType(i_comp, name(), full_name, is_bc()) );
279  sub_fields_[i_comp].units( units() );
280  if (no_check_control_field_ != nullptr && no_check_control_field_->size() == sub_fields_.size())
281  sub_fields_[i_comp].disable_where((*no_check_control_field_)[i_comp], shared_->no_check_values_);
282  sub_fields_[i_comp].set_mesh( *(shared_->mesh_) );
283 // sub_fields_[i_comp].set_limit_side(this->limit_side_);
284  sub_fields_[i_comp].input_selection(shared_->input_element_selection_);
285  sub_fields_[i_comp].add_factory( std::make_shared<MultiFieldFactory>(i_comp) );
286 
287  if (this->shared_->input_default_!="") {
288  sub_fields_[i_comp].shared_->input_default_ = this->shared_->input_default_;
289  }
290 
291  sub_fields_[i_comp].flags_ = this->flags_;
292  sub_fields_[i_comp].set_input_list(this->full_input_list_);
293  }
294 }
295 
296 
297 
298 template<int spacedim, class Value>
300  if (! flags().match(FieldFlag::declare_input)) return;
301 
302  // Check sizes of Arrays defined MultiField in field descriptors
304  it != list.end();
305  ++it) {
306  Input::Array mf_array;
307  if ( it->opt_val(this->input_name(), mf_array) ) {
308  unsigned int comp_size = this->shared_->comp_names_.size();
309  if (mf_array.size() != 1 && mf_array.size() != comp_size)
310  THROW( Exc_InvalidMultiFieldSize() << EI_MultiFieldName(this->input_name())
311  << EI_Size(mf_array.size()) << EI_ExpectedSize(comp_size) << list.ei_address() );
312  }
313  }
314 
315  this->full_input_list_ = list;
316 
317  // Save the full array for future use in FieldCommon::mark_input_times().
318  list.copy_to(shared_->input_list_);
319 }
320 
321 
322 // template<int spacedim, class Value>
323 // typename MultiField<spacedim, Value>::MultiFieldValue::return_type MultiField<spacedim, Value>::value(const Point &p, const ElementAccessor<spacedim> &elm) const {
324 // typename MultiFieldValue::return_type ret(size(), 1);
325 // for (unsigned int i_comp=0; i_comp < size(); i_comp++) {
326 // ret(i_comp, 0) = sub_fields_[i_comp].value(p,elm);
327 // }
328 //
329 // return ret;
330 // }
331 //
332 //
333 //
334 // template<int spacedim, class Value>
335 // void MultiField<spacedim, Value>::value_list(const std::vector< Point > &point_list, const ElementAccessor<spacedim> &elm,
336 // std::vector<typename MultiFieldValue::return_type> &value_list) const {
337 // OLD_ASSERT_EQUAL( point_list.size(), value_list.size() );
338 // for(unsigned int i=0; i< point_list.size(); i++) {
339 // value_list[i]=this->value(point_list[i], elm);
340 // }
341 // }
342 
343 
344 
345 template<int spacedim, class Value>
347  Input::Array multifield_arr;
348  if (descriptor_rec.opt_val(field.input_name(), multifield_arr))
349  {
350  //OLD_ASSERT(multifield_arr.size() == 1 || multifield_arr.size() == field.n_comp(),
351  // "Invalid size of Array defined for MultiField '%s'!\n", field.input_name().c_str());
352  unsigned int position = 0;
353  auto it = multifield_arr.begin<Input::AbstractRecord>();
354  if (multifield_arr.size() > 1)
355  while (index_ != position) {
356  ++it; ++position;
357  }
358 
359  FieldAlgoBaseInitData init_data(field.input_name(), field.n_comp(), field.units(), field.limits());
361  field_algo_base->set_component_idx(index_);
362  return field_algo_base;
363  }
364 
365  return NULL;
366 }
367 
368 
369 
370 template<int spacedim, class Value>
372  return in_rec.find<Input::Array>(input_name);
373 }
374 
375 
376 
377 #endif /* MULTI_FIELD_IMPL_HH_ */
void set_input_list(const Input::Array &list) override
IT::Instance get_input_type() override
Iterator< ValueType > begin() const
std::string get_value_attribute() const override
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:60
bool is_jump_time_
Accessor to input data conforming to declared Array.
Definition: accessors.hh:567
EI_Address ei_address() const
Definition: accessors.cc:314
auto disable_where(const MultiField< spacedim, typename FieldValue< spacedim >::Enum > &control_field, const vector< FieldEnum > &value_list) -> MultiField &
unsigned int size() const
Number of subfields that compose the multi-field.
Definition: multi_field.hh:174
static const Input::Type::Instance & get_input_type_instance(Input::Type::Selection value_selection=Input::Type::Selection())
bool is_active_field_descriptor(const Input::Record &in_rec, const std::string &input_name) override
void copy_from(const FieldCommon &other) override
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:62
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
unsigned int component_index_
std::pair< double, double > limits() const
Field< spacedim, Value > SubFieldType
Definition: multi_field.hh:58
Definition: mesh.h:97
const MultiField< spacedim, typename FieldValue< spacedim >::Enum > * no_check_control_field_
Definition: multi_field.hh:232
Iterator< Ret > find(const string &key) const
Helper class that stores data of generic types.
Definition: type_generic.hh:89
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
double last_time_
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
MultiField & operator=(const MultiField &other)
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
Input::Array full_input_list_
Full list of input field descriptors from which the subfields of MultiField are set.
Definition: multi_field.hh:223
Class for declaration of inputs sequences.
Definition: type_base.hh:345
static constexpr bool value
Definition: json.hpp:87
constexpr bool match(Mask mask) const
Definition: flag_array.hh:163
std::shared_ptr< SharedData > shared_
IteratorBase end() const
unsigned int DiscreteSpaceFlags
Definition: output_time.hh:106
OutputTime::DiscreteSpace output_type() const
#define OLD_ASSERT(...)
Definition: global_defs.h:131
void setup_components()
bool opt_val(const string &key, Ret &value) const
double time() const
const std::string & name() const
std::vector< SubFieldType > sub_fields_
Subfields (items) of MultiField.
Definition: multi_field.hh:220
FieldFlag::Flags & flags()
virtual Field< spacedim, Value >::FieldBasePtr create_field(Input::Record rec, const FieldCommon &field)
const UnitSI & units() const
FieldFlag::Flags flags_
Field flags. Default setting is "an equation input field, that can read from user input...
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
static const unsigned int N_DISCRETE_SPACES
Definition: output_time.hh:95
bool set_time(const TimeStep &time, LimitSide limit_side) override
LimitSide last_limit_side_
FieldResult field_result(RegionSet region_set) const override
Indicates special field states.
MultiField(bool bc=false)
Class for declaration of polymorphic Record.
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:459
FieldResult
void field_output(std::shared_ptr< OutputTime > stream) override
static std::shared_ptr< FieldAlgorithmBase< spacedim, Value > > function_factory(const Input::AbstractRecord &rec, const struct FieldAlgoBaseInitData &init_data)
std::shared_ptr< FieldBaseType > FieldBasePtr
Definition: field.hh:66
void copy_to(Container &out) const
bool is_constant(Region reg) override
IT::Array get_multifield_input_type() override
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:295
#define ASSERT_DBG(expr)
Definition: asserts.hh:349
const Mesh * mesh() const
FieldCommon & name(const string &name)
Definition: field_common.hh:97
unsigned int n_comp() const
unsigned int size() const
Class for representation of a vector of fields of the same physical quantity.
Definition: multi_field.hh:55
const std::string & input_name() const
static constexpr Mask equation_input
The field is data parameter of the owning equation. (default on)
Definition: field_flag.hh:33
bool is_bc() const
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
Representation of one time step..
TimeStatus set_time_result_
void observe_output(std::shared_ptr< Observe > observe) override
void set_mesh(const Mesh &mesh) override
LimitSide
Definition: field_common.hh:47
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