Flow123d  release_1.8.2-1603-g0109a2b
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 field.impl.hh
15  * @brief
16  */
17 
18 #ifndef FIELD_IMPL_HH_
19 #define FIELD_IMPL_HH_
20 
21 #include <boost/foreach.hpp>
22 
23 #include "field.hh"
24 #include "mesh/region.hh"
26 #include "input/accessors.hh"
27 
28 
29 /******************************************************************************************
30  * Implementation of Field<...>
31  */
32 
33 template<int spacedim, class Value>
35 : data_(std::make_shared<SharedData>())
36 {
37  // n_comp is nonzero only for variable size vectors Vector, VectorEnum, ..
38  // this invariant is kept also by n_comp setter
39  shared_->n_comp_ = (Value::NRows_ ? 0 : 1);
40  this->add_factory( std::make_shared<FactoryBase>() );
41 
42  this->multifield_ = false;
43 }
44 
45 
46 template<int spacedim, class Value>
47 Field<spacedim,Value>::Field(const string &name, bool bc)
48 : data_(std::make_shared<SharedData>())
49 
50 {
51  // n_comp is nonzero only for variable size vectors Vector, VectorEnum, ..
52  // this invariant is kept also by n_comp setter
53  shared_->n_comp_ = (Value::NRows_ ? 0 : 1);
54  shared_->bc_=bc;
55  this->name( name );
56  this->add_factory( std::make_shared<FactoryBase>() );
57  this->multifield_ = false;
58 }
59 
60 
61 
62 template<int spacedim, class Value>
63 Field<spacedim,Value>::Field(unsigned int component_index, string input_name, string name, bool bc)
64 : data_(std::make_shared<SharedData>())
65 {
66  // n_comp is nonzero only for variable size vectors Vector, VectorEnum, ..
67  // this invariant is kept also by n_comp setter
68  shared_->n_comp_ = (Value::NRows_ ? 0 : 1);
69  this->set_component_index(component_index);
70  this->name_ = (name=="") ? input_name : name;
71  this->shared_->input_name_ = input_name;
72  shared_->bc_ = bc;
73 
74  this->multifield_ = false;
75 }
76 
77 
78 template<int spacedim, class Value>
80 : FieldCommon(other),
81  data_(other.data_),
83  factories_(other.factories_)
84 {
85  if (other.no_check_control_field_)
86  no_check_control_field_ = make_shared<ControlField>(*other.no_check_control_field_);
87 
88  this->multifield_ = false;
89 }
90 
91 
92 template<int spacedim, class Value>
94 {
95  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());
96  OLD_ASSERT(other.shared_->mesh_, "Must call set_mesh before assign to other field.\n");
97  OLD_ASSERT( !shared_->mesh_ || (shared_->mesh_==other.shared_->mesh_),
98  "Assignment between fields with different meshes.\n");
99 
100  // check for self assignement
101  if (&other == this) return *this;
102 
103  // class members derived from FieldCommon
104  shared_ = other.shared_;
105  shared_->is_fully_initialized_ = false;
107  last_time_ = other.last_time_;
111  this->multifield_ = false;
112 
113  // class members of Field class
114  data_ = other.data_;
115  factories_ = other.factories_;
117 
118  if (other.no_check_control_field_) {
119  no_check_control_field_ = make_shared<ControlField>(*other.no_check_control_field_);
120  }
121 
122  return *this;
123 }
124 
125 
126 
127 template<int spacedim, class Value>
129  return FieldBaseType::get_input_type_instance(shared_->input_element_selection_);
130 }
131 
132 
133 
134 template<int spacedim, class Value>
136  OLD_ASSERT(false, "This method can't be used for Field");
137 
138  it::Array arr = it::Array( it::Integer() );
139  return arr;
140 }
141 
142 
143 
144 template<int spacedim, class Value>
146  const Field<spacedim, typename FieldValue<spacedim>::Enum > &control_field,
147  const vector<FieldEnum> &value_list) -> Field &
148 {
149  no_check_control_field_=std::make_shared<ControlField>(control_field);
150  shared_->no_check_values_=value_list;
151  return *this;
152 }
153 
154 template<int spacedim, class Value>
155 void Field<spacedim,Value>::set_mesh(const Mesh &in_mesh) {
156  // since we allow copy of fields before set_mesh is called
157  // we have to check that all copies set the same mesh
158  if (shared_->mesh_ && shared_->mesh_ != &in_mesh) {
159  THROW(ExcFieldMeshDifference() << EI_Field(name()) );
160  }
161 
162  shared_->mesh_ = &in_mesh;
163 
164  // initialize table if it is empty, we assume that the RegionDB is closed at this moment
165  region_fields_.resize( mesh()->region_db().size() );
166  RegionHistory init_history(history_length_limit_); // capacity
167  data_->region_history_.resize( mesh()->region_db().size(), init_history );
168 
169  if (no_check_control_field_) no_check_control_field_->set_mesh(in_mesh);
170 }
171 
172 
173 /*
174 template<int spacedim, class Value>
175 boost::shared_ptr< typename Field<spacedim,Value>::FieldBaseType >
176 Field<spacedim,Value>::operator[] (Region reg)
177 {
178  OLD_ASSERT_LESS(reg.idx(), this->region_fields_.size());
179  return this->region_fields_[reg.idx()];
180 }
181 */
182 
183 
184 template <int spacedim, class Value>
186  OLD_ASSERT(this->set_time_result_ != TimeStatus::unknown, "Unknown time status.\n");
187  OLD_ASSERT_LESS(reg.idx(), this->region_fields_.size());
188  FieldBasePtr region_field = this->region_fields_[reg.idx()];
189  return (region_field && typeid(*region_field) == typeid(FieldConstant<spacedim, Value>));
190 }
191 
192 
193 template<int spacedim, class Value>
195  const RegionSet &domain,
196  FieldBasePtr field,
197  double time)
198 {
199  OLD_ASSERT(field, "Null field pointer.\n");
200 
201  OLD_ASSERT( mesh(), "Null mesh pointer, set_mesh() has to be called before set_field().\n");
202  if (domain.size() == 0) return;
203 
204  OLD_ASSERT_EQUAL( field->n_comp() , n_comp());
205  field->set_mesh( mesh() , is_bc() );
206 
207  HistoryPoint hp = HistoryPoint(time, field);
208  for(const Region &reg: domain) {
209  RegionHistory &region_history = data_->region_history_[reg.idx()];
210  // insert hp into descending time sequence
211  OLD_ASSERT( region_history.size() == 0 || region_history[0].first < hp.first, "Can not insert smaller time %g then last time %g in field's history.\n",
212  hp.first, region_history[0].first );
213  region_history.push_front(hp);
214  }
216 }
217 
218 
219 
220 template<int spacedim, class Value>
222  const RegionSet &domain,
223  const Input::AbstractRecord &a_rec,
224  double time)
225 {
226  set_field(domain, FieldBaseType::function_factory(a_rec, n_comp()), time);
227 }
228 
229 
230 
231 
232 template<int spacedim, class Value>
233 bool Field<spacedim, Value>::set_time(const TimeStep &time_step, LimitSide limit_side)
234 {
235  OLD_ASSERT( mesh() , "NULL mesh pointer of field '%s'. set_mesh must be called before.\n",name().c_str());
236 
237  // Skip setting time if the new time is equal to current time of the field
238  // and if either the field is continuous in that time or the current limit side is same as the new one.
239  if (time_step.end() == last_time_) {
240  if ( ! is_jump_time() ||
241  limit_side == last_limit_side_) {
242  last_limit_side_ = limit_side;
243  return changed();
244  }
245  }
246 
247  last_time_=time_step.end();
248  last_limit_side_ = limit_side;
249 
250  // possibly update our control field
252  no_check_control_field_->set_time(time_step, limit_side);
253  }
254 
256 
257  // read all descriptors satisfying time.ge(input_time)
258  update_history(time_step);
260 
261  //
262  is_jump_time_=false;
263  // set time_step on all regions
264  // for regions that match type of the field domain
265  for(const Region &reg: mesh()->region_db().get_region_set("ALL") ) {
266  auto rh = data_->region_history_[reg.idx()];
267 
268  // skip regions with no matching BC flag
269  // skipping regions with empty history - no_check regions
270  if (reg.is_boundary() == is_bc() && !rh.empty() ) {
271  double last_time_in_history = rh.front().first;
272  unsigned int history_size=rh.size();
273  unsigned int i_history;
274  OLD_ASSERT(time_step.ge(last_time_in_history), "Setting field time back in history not fully supported yet!");
275 
276  // set history index
277  if ( time_step.gt(last_time_in_history) ) {
278  // in smooth time_step
279  i_history=0;
280  } else {
281  // time_step .eq. input_time; i.e. jump time
282  is_jump_time_=true;
283  if (limit_side == LimitSide::right) {
284  i_history=0;
285  } else {
286  i_history=1;
287  }
288  }
289  i_history=min(i_history, history_size - 1);
290  OLD_ASSERT(i_history >= 0, "Empty field history.");
291  // possibly update field pointer
292  auto new_ptr = rh.at(i_history).second;
293  if (new_ptr != region_fields_[reg.idx()]) {
294  region_fields_[reg.idx()]=new_ptr;
296  }
297  // let FieldBase implementation set the time
298  if ( new_ptr->set_time(time_step) ) set_time_result_ = TimeStatus::changed;
299 
300  }
301  }
302 
303  return changed();
304 }
305 
306 
307 template<int spacedim, class Value>
309  OLD_ASSERT( flags().match(FieldFlag::input_copy), "Try to call copy from the field '%s' to the non-copy field '%s'.",
310  other.name().c_str(), this->name().c_str());
311  if (typeid(other) == typeid(*this)) {
312  auto const &other_field = dynamic_cast< Field<spacedim, Value> const &>(other);
313  this->operator=(other_field);
314  }
315 }
316 
317 
318 
319 template<int spacedim, class Value>
320 void Field<spacedim, Value>::output(std::shared_ptr<OutputTime> stream)
321 {
322  // currently we cannot output boundary fields
323  if (!is_bc())
324  stream->register_data(this->output_type(), *this);
325 }
326 
327 
328 
329 template<int spacedim, class Value>
331 
332  FieldResult result_all = result_none;
333  for(Region &reg : region_set) {
334  auto f = region_fields_[reg.idx()];
335  if (f) {
336  FieldResult fr = f->field_result();
337  if (result_all == result_none) // first region
338  result_all = fr;
339  else if (fr != result_all)
340  return result_other; // if results from individual regions are different
341  } else return result_none; // if field is undefined on any region of the region set
342  }
343 
344  if (result_all == result_constant && region_set.size() > 1)
345  return result_other; // constant result for individual regions could be non-constant on the whole region set
346 
347  return result_all;
348 
349 }
350 
351 
352 
353 template<int spacedim, class Value>
355  OLD_ASSERT( mesh(), "Null mesh pointer, set_mesh() has to be called before.\n");
356 
357  // read input up to given time
358  double input_time;
359  if (shared_->input_list_.size() != 0) {
360  while( shared_->list_idx_ < shared_->input_list_.size()
361  && time.ge( input_time = shared_->input_list_[shared_->list_idx_].val<double>("time") ) ) {
362 
363  const Input::Record & actual_list_item = shared_->input_list_[shared_->list_idx_];
364  // get domain specification
365  RegionSet domain;
366  Input::Array domain_name_array;
367  unsigned int id;
368  if (actual_list_item.opt_val("region", domain_name_array)) {
369  std::vector<string> domain_names = mesh()->region_db().get_and_check_operands(domain_name_array);
370  domain = mesh()->region_db().union_set(domain_names);
371 
372  } else if (actual_list_item.opt_val("rid", id)) {
373  Region region;
374  try {
375  region = mesh()->region_db().find_id(id);
376  } catch (RegionDB::ExcUniqueRegionId &e) {
377  e << actual_list_item.ei_address();
378  throw;
379  }
380  if (region.is_valid())
381  domain.push_back(region);
382  else
383  THROW(RegionDB::ExcUnknownRegion() << RegionDB::EI_ID(id) );
384  } else {
385  THROW(ExcMissingDomain()
386  << actual_list_item.ei_address() );
387  }
388 
389  // get field instance
390  for(auto rit = factories_.rbegin() ; rit != factories_.rend(); ++rit) {
391  FieldBasePtr field_instance = (*rit)->create_field(actual_list_item, *this);
392  if (field_instance) // skip descriptors without related keys
393  {
394  // add to history
395  OLD_ASSERT_EQUAL( field_instance->n_comp() , n_comp());
396  field_instance->set_mesh( mesh() , is_bc() );
397  for(const Region &reg: domain) {
398  data_->region_history_[reg.idx()].push_front(
399  HistoryPoint(input_time, field_instance)
400  );
401  }
402  break;
403  }
404  }
405 
406  ++shared_->list_idx_;
407  }
408  }
409 }
410 
411 template<int spacedim, class Value>
413  OLD_ASSERT(mesh(), "Null mesh pointer.");
414  if (shared_->is_fully_initialized_) return;
415 
416  // check there are no empty field pointers, collect regions to be initialized from default value
417  RegionSet regions_to_init; // empty vector
418 
419  for(const Region &reg : mesh()->region_db().get_region_set("ALL") )
420  if (reg.is_boundary() == is_bc()) { // for regions that match type of the field domain
421  RegionHistory &rh = data_->region_history_[reg.idx()];
422  if ( rh.empty() || ! rh[0].second) // empty region history
423  {
424  // test if check is turned on and control field is FieldConst
425  if (no_check_control_field_ && no_check_control_field_->is_constant(reg) ) {
426  // get constant enum value
427  auto elm = ElementAccessor<spacedim>(mesh(), reg);
428  FieldEnum value = no_check_control_field_->value(elm.centre(),elm);
429  // check that the value is in the disable list
430  if ( std::find(shared_->no_check_values_.begin(), shared_->no_check_values_.end(), value)
431  != shared_->no_check_values_.end() )
432  continue; // the field is not needed on this region
433  }
434  if (shared_->input_default_ != "") { // try to use default
435  regions_to_init.push_back( reg );
436  } else {
437  xprintf(UsrErr, "Missing value of the input field '%s' ('%s') on region ID: %d label: %s.\n",
438  input_name().c_str(), name().c_str(), reg.id(), reg.label().c_str() );
439  }
440  }
441  }
442 
443  // possibly set from default value
444  if ( regions_to_init.size() ) {
445  std::string region_list;
446  // has to deal with fact that reader can not deal with input consisting of simple values
447  string default_input=input_default();
448  auto input_type = get_input_type().make_instance().first;
449  Input::ReaderToStorage reader( default_input, *input_type, Input::FileFormat::format_JSON );
450 
451  auto a_rec = reader.get_root_interface<Input::AbstractRecord>();
452  auto field_ptr = FieldBaseType::function_factory( a_rec , n_comp() );
453  field_ptr->set_mesh( mesh(), is_bc() );
454  for(const Region &reg: regions_to_init) {
455  data_->region_history_[reg.idx()]
456  .push_front(HistoryPoint( 0.0, field_ptr) );
457  region_list+=" "+reg.label();
458  }
459  xprintf(Warn, "Using default value '%s' for part of the input field '%s' ('%s').\n"
460  "regions: %s\n",
461  input_default().c_str(), input_name().c_str(), name().c_str(), region_list.c_str());
462 
463  }
464  shared_->is_fully_initialized_ = true;
465 }
466 
467 
468 template<int spacedim, class Value>
469 void Field<spacedim,Value>::add_factory(const std::shared_ptr<FactoryBase> factory) {
470  factories_.push_back( factory );
471 }
472 
473 
474 template<int spacedim, class Value>
476  Input::AbstractRecord field_record;
477  if (rec.opt_val(field.input_name(), field_record))
478  return FieldBaseType::function_factory(field_record, field.n_comp() );
479  else
480  return FieldBasePtr();
481 }
482 
483 
484 template<int spacedim, class Value>
486  return in_rec.find<Input::AbstractRecord>(input_name);
487 }
488 
489 
490 
491 
492 template<int spacedim, class Value>
494  if (! flags().match(FieldFlag::declare_input)) return;
495 
496  // check that times forms ascending sequence
497  double time,last_time=0.0;
498 
500  it != list.end();
501  ++it) {
502  for(auto rit = factories_.rbegin() ; rit != factories_.rend(); ++rit) {
503  if ( (*rit)->is_active_field_descriptor( (*it), this->input_name() ) ) {
504  shared_->input_list_.push_back( Input::Record( *it ) );
505  time = it->val<double>("time");
506  if (time < last_time) {
507  THROW( ExcNonascendingTime()
508  << EI_Time(time)
509  << EI_Field(input_name())
510  << it->ei_address());
511  }
512  last_time = time;
513 
514  break;
515  }
516  }
517  }
518 
519 }
520 
521 
522 
523 #endif /* FIELD_IMPL_HH_ */
void check_initialized_region_fields_()
Definition: field.impl.hh:412
Iterator< ValueType > begin() const
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:57
virtual bool is_active_field_descriptor(const Input::Record &in_rec, const std::string &input_name)
Definition: field.impl.hh:485
pair< double, FieldBasePtr > HistoryPoint
Pair: time, pointer to FieldBase instance.
Definition: field.hh:286
bool is_jump_time_
Accessor to input data conforming to declared Array.
Definition: accessors.hh:552
unsigned int size() const
Number of subfields that compose the multi-field.
Definition: multi_field.hh:156
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
RegionSet union_set(std::vector< string > set_names) const
Definition: region.cc:466
Reader for (slightly) modified input files.
std::string name_
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:62
FieldResult field_result(RegionSet region_set) const
Indicates special field states.
Definition: field.impl.hh:330
unsigned int component_index_
Definition: mesh.h:99
Iterator< Ret > find(const string &key) const
void update_history(const TimeStep &time)
Definition: field.impl.hh:354
Helper class that stores data of generic types.
Definition: type_generic.hh:88
void set_input_list(const Input::Array &list) override
Definition: field.impl.hh:493
void output(std::shared_ptr< OutputTime > stream) override
Definition: field.impl.hh:320
double last_time_
const std::string & input_default() const
bool set_time(const TimeStep &time, LimitSide limit_side) override
Definition: field.impl.hh:233
const RegionDB & region_db() const
Definition: mesh.h:156
Class for declaration of the integral input data.
Definition: type_base.hh:460
virtual FieldBasePtr create_field(Input::Record rec, const FieldCommon &field)
Definition: field.impl.hh:475
IT::Instance get_input_type() override
Definition: field.impl.hh:128
virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list) const
Definition: field.hh:351
Field()
Definition: field.impl.hh:34
Class for declaration of inputs sequences.
Definition: type_base.hh:316
std::shared_ptr< SharedData > data_
Definition: field.hh:300
static const Input::Type::Instance & get_input_type_instance(const Input::Type::Selection *value_selection=NULL)
std::shared_ptr< SharedData > shared_
IteratorBase end() const
OutputTime::DiscreteSpace output_type() const
EI_Address ei_address() const
Definition: accessors.cc:193
#define OLD_ASSERT(...)
Definition: global_defs.h:128
bool opt_val(const string &key, Ret &value) const
double time() const
Definition: system.hh:59
const std::string & name() const
bool ge(double other_time) const
unsigned int FieldEnum
Definition: field_values.hh:41
static constexpr Mask input_copy
A field that is input of its equation and can not read from input, thus must be set by copy...
Definition: field_flag.hh:39
FieldFlag::Flags & flags()
Region find_id(unsigned int id, unsigned int dim) const
Definition: region.cc:173
std::vector< string > get_and_check_operands(const Input::Array &operands) const
Definition: region.cc:305
T get_root_interface() const
Returns the root accessor.
Accessor to the data with type Type::Record.
Definition: accessors.hh:277
IT::Array get_multifield_input_type() override
Definition: field.impl.hh:135
#define xprintf(...)
Definition: system.hh:87
std::shared_ptr< ControlField > no_check_control_field_
Definition: field.hh:310
#define OLD_ASSERT_LESS(a, b)
Definition: global_defs.h:131
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm) const
Definition: field.hh:337
static std::shared_ptr< FieldAlgorithmBase< spacedim, Value > > function_factory(const Input::AbstractRecord &rec, unsigned int n_comp=0)
LimitSide last_limit_side_
double end() const
std::vector< FieldBasePtr > region_fields_
Definition: field.hh:315
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:444
FieldResult
Definition: system.hh:59
bool is_jump_time()
std::shared_ptr< FieldBaseType > FieldBasePtr
Definition: field.hh:66
std::vector< std::shared_ptr< FactoryBase > > factories_
Definition: field.hh:317
auto disable_where(const Field< spacedim, typename FieldValue< spacedim >::Enum > &control_field, const vector< FieldEnum > &value_list) -> Field &
Definition: field.impl.hh:145
boost::circular_buffer< HistoryPoint > RegionHistory
Nearest history of one region.
Definition: field.hh:288
void set_component_index(unsigned int idx)
bool is_constant(Region reg) override
Definition: field.impl.hh:185
void add_factory(std::shared_ptr< FactoryBase > factory)
Definition: field.impl.hh:469
void set_field(const RegionSet &domain, FieldBasePtr field, double time=0.0)
Definition: field.impl.hh:194
bool gt(double other_time) const
const Mesh * mesh() const
FieldCommon & name(const string &name)
Definition: field_common.hh:83
unsigned int n_comp() const
#define OLD_ASSERT_EQUAL(a, b)
Definition: global_defs.h:130
bool changed() const
void set_mesh(const Mesh &mesh) override
Definition: field.impl.hh:155
void set_history_changed()
bool is_valid() const
Returns false if the region has undefined/invalid value.
Definition: region.hh:77
const std::string & input_name() const
void copy_from(const FieldCommon &other) override
Definition: field.impl.hh:308
bool is_bc() const
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:44
Representation of one time step..
TimeStatus set_time_result_
Field & operator=(const Field &other)
Definition: field.impl.hh:93
static const unsigned int history_length_limit_
LimitSide
Definition: field_common.hh:44
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
unsigned int idx() const
Returns a global index of the region.
Definition: region.hh:81