Flow123d  JS_before_hm-1623-gd361259
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 "field.hh"
22 #include "field_algo_base.impl.hh"
23 #include "field_fe.hh"
24 #include "fields/eval_subset.hh"
25 #include "fields/eval_points.hh"
27 #include "fields/field_set.hh"
28 #include "mesh/region.hh"
30 #include "input/accessors.hh"
31 #include "io/observe.hh"
32 #include "io/output_mesh.hh"
33 #include "io/output_element.hh"
34 
35 
36 /******************************************************************************************
37  * Implementation of Field<...>
38  */
39 
40 template<int spacedim, class Value>
42 : data_(std::make_shared<SharedData>()),
43  value_cache_( FieldValueCache<typename Value::element_type>(Value::NRows_, Value::NCols_) )
44 {
45  // n_comp is nonzero only for variable size vectors Vector, VectorEnum, ..
46  // this invariant is kept also by n_comp setter
47  shared_->n_comp_ = (Value::NRows_ ? 0 : 1);
48  this->add_factory( std::make_shared<FactoryBase>() );
49 
50  unsigned int cache_size = 1.1 * CacheMapElementNumber::get();
51  value_cache_.reinit(cache_size);
52  value_cache_.resize(cache_size);
53 
54  this->multifield_ = false;
55  this->set_shape( Value::NRows_, Value::NCols_ );
56 }
57 
58 
59 template<int spacedim, class Value>
60 Field<spacedim,Value>::Field(const string &name, bool bc)
61 : data_(std::make_shared<SharedData>()),
62  value_cache_( FieldValueCache<typename Value::element_type>(Value::NRows_, Value::NCols_) )
63 {
64  // n_comp is nonzero only for variable size vectors Vector, VectorEnum, ..
65  // this invariant is kept also by n_comp setter
66  shared_->n_comp_ = (Value::NRows_ ? 0 : 1);
67  shared_->bc_=bc;
68  this->name( name );
69  this->add_factory( std::make_shared<FactoryBase>() );
70  unsigned int cache_size = 1.1 * CacheMapElementNumber::get();
71  value_cache_.reinit(cache_size);
72  value_cache_.resize(cache_size);
73 
74  this->multifield_ = false;
75  this->set_shape( Value::NRows_, Value::NCols_ );
76 }
77 
78 
79 
80 template<int spacedim, class Value>
81 Field<spacedim,Value>::Field(unsigned int component_index, string input_name, string name, bool bc)
82 : data_(std::make_shared<SharedData>()),
83  value_cache_( FieldValueCache<typename Value::element_type>(Value::NRows_, Value::NCols_) )
84 {
85  // n_comp is nonzero only for variable size vectors Vector, VectorEnum, ..
86  // this invariant is kept also by n_comp setter
87  shared_->n_comp_ = (Value::NRows_ ? 0 : 1);
88  this->set_component_index(component_index);
89  this->name_ = (name=="") ? input_name : name;
90  this->shared_->input_name_ = input_name;
91  shared_->bc_ = bc;
92 
93  unsigned int cache_size = 1.1 * CacheMapElementNumber::get();
94  value_cache_.reinit(cache_size);
95  value_cache_.resize(cache_size);
96 
97  this->multifield_ = false;
98  this->set_shape( Value::NRows_, Value::NCols_ );
99 }
100 
101 
102 template<int spacedim, class Value>
104 : FieldCommon(other),
105  data_(other.data_),
107  factories_(other.factories_),
109 {
110  if (other.no_check_control_field_)
111  no_check_control_field_ = make_shared<ControlField>(*other.no_check_control_field_);
112 
113  this->multifield_ = false;
114  this->set_shape( Value::NRows_, Value::NCols_ );
115 }
116 
117 
118 template<int spacedim, class Value>
120 {
121  //ASSERT( flags().match(FieldFlag::input_copy) )(this->name())(other.name()).error("Try to assign to non-copy field from the field.");
122  ASSERT(other.shared_->mesh_).error("Must call set_mesh before assign to other field.\n");
123  ASSERT( !shared_->mesh_ || (shared_->mesh_==other.shared_->mesh_) ).error("Assignment between fields with different meshes.\n");
124 
125  // check for self assignement
126  if (&other == this) return *this;
127 
128  // class members derived from FieldCommon
129  shared_ = other.shared_;
130  shared_->is_fully_initialized_ = false;
132  last_time_ = other.last_time_;
136  this->multifield_ = false;
137 
138  // class members of Field class
139  data_ = other.data_;
140  factories_ = other.factories_;
142  value_cache_ = other.value_cache_;
143  this->shape_ = other.shape_;
144 
145  if (other.no_check_control_field_) {
146  no_check_control_field_ = make_shared<ControlField>(*other.no_check_control_field_);
147  }
148 
149  return *this;
150 }
151 
152 
153 
154 template<int spacedim, class Value>
155 typename Value::return_type Field<spacedim,Value>::operator() (BulkPoint &p) {
157 }
158 
159 
160 
161 template<int spacedim, class Value>
162 typename Value::return_type Field<spacedim,Value>::operator() (EdgePoint &p) {
164 }
165 
166 
167 
168 template<int spacedim, class Value>
169 typename Value::return_type Field<spacedim,Value>::operator() (CouplingPoint &p) {
171 }
172 
173 
174 
175 template<int spacedim, class Value>
176 typename Value::return_type Field<spacedim,Value>::operator() (BoundaryPoint &p) {
178 }
179 
180 
181 
182 template<int spacedim, class Value>
183 typename Value::return_type
184 Field<spacedim,Value>::operator[] (unsigned int i_cache_point) const
185 {
186  return Value::get_from_array( this->value_cache_, i_cache_point );
187 }
188 
189 
190 
191 template<int spacedim, class Value>
193  return FieldBaseType::get_input_type_instance(shared_->input_element_selection_);
194 }
195 
196 
197 
198 template<int spacedim, class Value>
200  ASSERT(false).error("This method can't be used for Field");
201 
202  it::Array arr = it::Array( it::Integer() );
203  return arr;
204 }
205 
206 
207 
208 template<int spacedim, class Value>
210  const Field<spacedim, typename FieldValue<spacedim>::Enum > &control_field,
211  const vector<FieldEnum> &value_list) -> Field &
212 {
213  no_check_control_field_=std::make_shared<ControlField>(control_field);
214  shared_->no_check_values_=value_list;
215  return *this;
216 }
217 
218 template<int spacedim, class Value>
219 void Field<spacedim,Value>::set_mesh(const Mesh &in_mesh) {
220  // since we allow copy of fields before set_mesh is called
221  // we have to check that all copies set the same mesh
222  if (shared_->mesh_ && shared_->mesh_ != &in_mesh) {
223  THROW(ExcFieldMeshDifference() << EI_Field(name()) );
224  }
225 
226  shared_->mesh_ = &in_mesh;
227 
228  // initialize table if it is empty, we assume that the RegionDB is closed at this moment
229  region_fields_.resize( mesh()->region_db().size() );
230  RegionHistory init_history(history_length_limit_); // capacity
231  data_->region_history_.resize( mesh()->region_db().size(), init_history );
232 
233  if (no_check_control_field_) no_check_control_field_->set_mesh(in_mesh);
234 }
235 
236 
237 /*
238 template<int spacedim, class Value>
239 std::shared_ptr< typename Field<spacedim,Value>::FieldBaseType >
240 Field<spacedim,Value>::operator[] (Region reg)
241 {
242  ASSERT_LT(reg.idx(), this->region_fields_.size());
243  return this->region_fields_[reg.idx()];
244 }
245 */
246 
247 
248 template <int spacedim, class Value>
250  ASSERT(this->set_time_result_ != TimeStatus::unknown).error("Unknown time status.\n");
251  ASSERT_LT(reg.idx(), this->region_fields_.size());
252  FieldBasePtr region_field = this->region_fields_[reg.idx()];
253  return ( region_field && region_field->is_constant_in_space() );
254 }
255 
256 
257 template<int spacedim, class Value>
259  FieldBasePtr field,
260  double time,
261  std::vector<std::string> region_set_names)
262 {
263  ASSERT_PTR(field).error("Null field pointer.\n");
264 
265  ASSERT_PTR( mesh() ).error("Null mesh pointer, set_mesh() has to be called before set().\n");
266  ASSERT_EQ( field->n_comp() , shared_->n_comp_);
267  field->set_mesh( mesh() , is_bc() );
268 
269  for (auto set_name : region_set_names) {
270  RegionSet domain = mesh()->region_db().get_region_set(set_name);
271  if (domain.size() == 0) continue;
272 
273  HistoryPoint hp = HistoryPoint(time, field);
274  for(const Region &reg: domain) {
275  RegionHistory &region_history = data_->region_history_[reg.idx()];
276  // insert hp into descending time sequence
277  ASSERT( region_history.size() == 0 || region_history[0].first < hp.first)(hp.first)(region_history[0].first)
278  .error("Can not insert smaller time then last time in field's history.\n");
279  region_history.push_front(hp);
280  }
281  }
283 }
284 
285 
286 
287 template<int spacedim, class Value>
289  const Input::AbstractRecord &a_rec,
290  double time,
291  std::vector<std::string> region_set_names)
292 {
293  FieldAlgoBaseInitData init_data(input_name(), n_comp(), units(), limits(), flags());
294  set(FieldBaseType::function_factory(a_rec, init_data), time, region_set_names);
295 }
296 
297 
298 
299 template<int spacedim, class Value>
300 bool Field<spacedim, Value>::set_time(const TimeStep &time_step, LimitSide limit_side)
301 {
302  ASSERT_PTR( mesh() )( name() ).error("NULL mesh pointer of field with given name. set_mesh must be called before.\n");
303 
304  // Skip setting time if the new time is equal to current time of the field
305  // and if either the field is continuous in that time or the current limit side is same as the new one.
306  if (time_step.end() == last_time_) {
307  if ( ! is_jump_time() ||
308  limit_side == last_limit_side_) {
309  last_limit_side_ = limit_side;
310  return changed();
311  }
312  }
313 
314  last_time_=time_step.end();
315  last_limit_side_ = limit_side;
316 
317  // possibly update our control field
319  no_check_control_field_->set_time(time_step, limit_side);
320  }
321 
323 
324  // read all descriptors satisfying time.ge(input_time)
325  update_history(time_step);
327 
328  //
329  is_jump_time_=false;
330  // set time_step on all regions
331  // for regions that match type of the field domain
332  for(const Region &reg: mesh()->region_db().get_region_set("ALL") ) {
333  auto rh = data_->region_history_[reg.idx()];
334 
335  // skip regions with no matching BC flag
336  if (reg.is_boundary() != is_bc()) continue;
337 
338  // Check regions with empty history, possibly set default.
339  if ( rh.empty()) continue;
340 
341  double last_time_in_history = rh.front().first;
342  unsigned int history_size=rh.size();
343  unsigned int i_history;
344  ASSERT( time_step.ge(last_time_in_history) ).error("Setting field time back in history not fully supported yet!");
345 
346  // set history index
347  if ( time_step.gt(last_time_in_history) ) {
348  // in smooth time_step
349  i_history=0;
350  } else {
351  // time_step .eq. input_time; i.e. jump time
352  is_jump_time_=true;
353  if (limit_side == LimitSide::right) {
354  i_history=0;
355  } else {
356  i_history=1;
357  }
358  }
359  i_history=min(i_history, history_size - 1);
360  ASSERT(i_history >= 0).error("Empty field history.");
361  // possibly update field pointer
362 
363  auto new_ptr = rh.at(i_history).second;
364  if (new_ptr != region_fields_[reg.idx()]) {
365  region_fields_[reg.idx()]=new_ptr;
367  }
368  // let FieldBase implementation set the time
369  if ( new_ptr->set_time(time_step) ) set_time_result_ = TimeStatus::changed;
370 
371  }
372 
373  return changed();
374 }
375 
376 
377 template<int spacedim, class Value>
379  ASSERT( flags().match(FieldFlag::equation_input))(other.name().c_str())(this->name().c_str())
380  .error("Can not copy to the non-input field.");
381 
382  // do not use copy if the field have its own input
384  && this->shared_->input_list_.size() != 0 ) return;
385 
386  if (typeid(other) == typeid(*this)) {
387  auto const &other_field = dynamic_cast< Field<spacedim, Value> const &>(other);
388  this->operator=(other_field);
389  }
390 }
391 
392 
393 
394 template<int spacedim, class Value>
395 void Field<spacedim, Value>::field_output(std::shared_ptr<OutputTime> stream)
396 {
397  // currently we cannot output boundary fields
398  if (!is_bc()) {
399  const OutputTime::DiscreteSpace type = this->get_output_type();
400 
402  this->compute_field_data( type, stream);
403  }
404 }
405 
406 
407 template<int spacedim, class Value>
408 void Field<spacedim, Value>::observe_output(std::shared_ptr<Observe> observe)
409 {
410  typedef typename Value::element_type ElemType;
411 
412  if (observe->point_ds()->size() == 0) return;
413 
414  ElementDataCache<ElemType> &output_data = observe->prepare_compute_data<ElemType>(this->name(), this->time(),
415  (unsigned int)Value::NRows_, (unsigned int)Value::NCols_);
416 
417  unsigned int loc_point_time_index, ele_index;
418  for(ObservePointAccessor op_acc : observe->local_range()) {
419  loc_point_time_index = op_acc.loc_point_time_index();
420  ele_index = op_acc.observe_point().element_idx();
421  const Value &obs_value =
422  Value( const_cast<typename Value::return_type &>(
423  this->value(op_acc.observe_point().global_coords(),
424  ElementAccessor<spacedim>(this->mesh(), ele_index)) ));
425  ASSERT_EQ(output_data.n_comp(), obs_value.n_rows()*obs_value.n_cols()).error();
426  output_data.store_value(loc_point_time_index, obs_value.mem_ptr());
427  }
428 }
429 
430 
431 
432 template<int spacedim, class Value>
434 
435  FieldResult result_all = result_none;
436  for(Region &reg : region_set) {
437  auto f = region_fields_[reg.idx()];
438  if (f) {
439  FieldResult fr = f->field_result();
440  if (result_all == result_none) // first region
441  result_all = fr;
442  else if (fr != result_all)
443  result_all = result_other; // if results from individual regions are different
444  } else return result_none; // if field is undefined on any region of the region set
445  }
446 
447  if (result_all == result_constant && region_set.size() > 1)
448  return result_other; // constant result for individual regions could be non-constant on the whole region set
449 
450  return result_all;
451 
452 }
453 
454 
455 template<int spacedim, class Value>
457 {
458  int nrows = Value::NRows_;
459  int ncols = Value::NCols_;
460  string type = "Integer";
462  type = "Double";
463 
464  return fmt::format("{{ \"shape\": [ {}, {} ], \"type\": \"{}\", \"limit\": [ {}, {} ] }}",
465  nrows, ncols, type, this->limits().first, this->limits().second);
466 }
467 
468 
469 template<int spacedim, class Value>
471  ASSERT_PTR( mesh() ).error("Null mesh pointer, set_mesh() has to be called before.\n");
472 
473  // read input up to given time
474  double input_time;
475  if (shared_->input_list_.size() != 0) {
476  while( shared_->list_idx_ < shared_->input_list_.size()
477  && time.ge( input_time = time.read_time( shared_->input_list_[shared_->list_idx_].find<Input::Tuple>("time") ) ) ) {
478 
479  const Input::Record & actual_list_item = shared_->input_list_[shared_->list_idx_];
480  // get domain specification
481  RegionSet domain;
482  Input::Array domain_name_array;
483  unsigned int id;
484  if (actual_list_item.opt_val("region", domain_name_array)) {
485  std::vector<string> domain_names = mesh()->region_db().get_and_check_operands(domain_name_array);
486  domain = mesh()->region_db().union_set(domain_names);
487 
488  } else if (actual_list_item.opt_val("rid", id)) {
489  Region region;
490  try {
491  region = mesh()->region_db().find_id(id);
492  } catch (RegionDB::ExcUniqueRegionId &e) {
493  e << actual_list_item.ei_address();
494  throw;
495  }
496  if (region.is_valid())
497  domain.push_back(region);
498  else
499  THROW(RegionDB::ExcUnknownRegion() << RegionDB::EI_ID(id) );
500  } else {
501  THROW(ExcMissingDomain()
502  << actual_list_item.ei_address() );
503  }
504 
505  // get field instance
506  for(auto rit = factories_.rbegin() ; rit != factories_.rend(); ++rit) {
507  FieldBasePtr field_instance = (*rit)->create_field(actual_list_item, *this);
508  if (field_instance) // skip descriptors without related keys
509  {
510  // add to history
511  ASSERT_EQ( field_instance->n_comp() , shared_->n_comp_);
512  field_instance->set_mesh( mesh() , is_bc() );
513  for(const Region &reg: domain) {
514  // if region history is empty, add new field
515  // or if region history is not empty and the input_time is higher, add new field
516  // otherwise (region history is not empty and the input_time is the same),
517  // rewrite the region field
518  if( data_->region_history_[reg.idx()].size() == 0
519  || data_->region_history_[reg.idx()].back().first < input_time)
520  {
521  data_->region_history_[reg.idx()].push_front(
522  HistoryPoint(input_time, field_instance));
523  //DebugOut() << "Update history" << print_var(this->name()) << print_var(reg.label()) << print_var(input_time);
524  }
525  else
526  {
527  data_->region_history_[reg.idx()].back() =
528  HistoryPoint(input_time, field_instance);
529  }
530  }
531  break;
532  }
533  }
534 
535  ++shared_->list_idx_;
536  }
537  }
538 }
539 
540 template<int spacedim, class Value>
542  ASSERT_PTR(mesh()).error("Null mesh pointer.");
543  //if (shared_->is_fully_initialized_) return;
544 
545  // check there are no empty field pointers, collect regions to be initialized from default value
546  RegionSet regions_to_init; // empty vector
547 
548  for(const Region &reg : mesh()->region_db().get_region_set("ALL") )
549  if (reg.is_boundary() == is_bc()) { // for regions that match type of the field domain
550  RegionHistory &rh = data_->region_history_[reg.idx()];
551  if ( rh.empty() || ! rh[0].second) // empty region history
552  {
553  // test if check is turned on and control field is FieldConst
554  if (no_check_control_field_ && no_check_control_field_->is_constant(reg) ) {
555  // get constant enum value
556  auto elm = ElementAccessor<spacedim>(mesh(), reg);
557  FieldEnum value = no_check_control_field_->value(elm.centre(),elm);
558  // check that the value is in the disable list
559  if ( std::find(shared_->no_check_values_.begin(), shared_->no_check_values_.end(), value)
560  != shared_->no_check_values_.end() )
561  continue; // the field is not needed on this region
562  }
563  if (shared_->input_default_ != "") { // try to use default
564  regions_to_init.push_back( reg );
565  } else {
566  THROW( ExcMissingFieldValue() << EI_FieldInputName(input_name()) << EI_FieldName(name())
567  << EI_RegId(reg.id()) << EI_RegLabel(reg.label()) );
568  }
569  }
570  }
571 
572  // possibly set from default value
573  if ( regions_to_init.size() ) {
574  std::string region_list;
575  // has to deal with fact that reader can not deal with input consisting of simple values
576  string default_input=input_default();
577  auto input_type = get_input_type().make_instance().first;
578  Input::ReaderToStorage reader( default_input, *input_type, Input::FileFormat::format_JSON );
579 
580  auto a_rec = reader.get_root_interface<Input::AbstractRecord>();
581  FieldAlgoBaseInitData init_data(input_name(), n_comp(), units(), limits(), flags());
582  auto field_ptr = FieldBaseType::function_factory( a_rec , init_data );
583  field_ptr->set_mesh( mesh(), is_bc() );
584  for(const Region &reg: regions_to_init) {
585  data_->region_history_[reg.idx()]
586  .push_front(HistoryPoint( 0.0, field_ptr) );
587  region_list+=" "+reg.label();
588  }
589  FieldCommon::messages_data_.push_back( MessageData(input_default(), name(), region_list) );
590 
591  }
592  //shared_->is_fully_initialized_ = true;
593 }
594 
595 
596 template<int spacedim, class Value>
597 void Field<spacedim,Value>::add_factory(const std::shared_ptr<FactoryBase> factory) {
598  factories_.push_back( factory );
599 }
600 
601 
602 template<int spacedim, class Value>
604  Input::AbstractRecord field_record;
605  if (rec.opt_val(field.input_name(), field_record)) {
606  FieldAlgoBaseInitData init_data(field.input_name(), field.n_comp(), field.units(), field.limits(), field.get_flags());
607  return FieldBaseType::function_factory(field_record, init_data );
608  }
609  else
610  return FieldBasePtr();
611 }
612 
613 
614 template<int spacedim, class Value>
616  return in_rec.find<Input::AbstractRecord>(input_name);
617 }
618 
619 
620 
621 
622 template<int spacedim, class Value>
624  if (! flags().match(FieldFlag::declare_input)) return;
625 
626  // check that times forms ascending sequence
627  double time,last_time=0.0;
628 
630  it != list.end();
631  ++it) {
632  for(auto rit = factories_.rbegin() ; rit != factories_.rend(); ++rit) {
633  if ( (*rit)->is_active_field_descriptor( (*it), this->input_name() ) ) {
634  shared_->input_list_.push_back( Input::Record( *it ) );
635  time = tg.read_time( it->find<Input::Tuple>("time") );
636  if (time < last_time) {
637  THROW( ExcNonascendingTime()
638  << EI_Time(time)
639  << EI_Field(input_name())
640  << it->ei_address());
641  }
642  last_time = time;
643 
644  break;
645  }
646  }
647  }
648 
649 }
650 
651 
652 
653 template<int spacedim, class Value>
654 void Field<spacedim,Value>::compute_field_data(OutputTime::DiscreteSpace space_type, std::shared_ptr<OutputTime> stream) {
655  typedef typename Value::element_type ElemType;
656 
657  OutputTime::OutputDataPtr output_data_base = stream->prepare_compute_data<ElemType>(this->name(), space_type,
658  (unsigned int)Value::NRows_, (unsigned int)Value::NCols_);
659 
660  try{
661  // try casting actual ElementDataCache
662  if( ! output_data_base->is_dummy()){
663  auto output_data = std::dynamic_pointer_cast<ElementDataCache<ElemType>>(output_data_base);
664  fill_data_cache(space_type, stream, output_data);
665  }
666 
667  } catch(const std::bad_cast& e){
668  // skip
669  }
670 
671  /* Set the last time */
672  stream->update_time(this->time());
673 
674 }
675 
676 template<int spacedim, class Value>
678  std::shared_ptr<OutputTime> stream,
679  std::shared_ptr<ElementDataCache<typename Value::element_type>> data_cache)
680 {
681  std::shared_ptr<OutputMeshBase> output_mesh = stream->get_output_mesh_ptr();
682  ASSERT(output_mesh);
683 
684  /* Copy data to array */
685  switch(space_type) {
688  unsigned int node_index = 0;
689  for(const auto & ele : *output_mesh )
690  {
691  std::vector<Space<3>::Point> vertices = ele.vertex_list();
692  for(unsigned int i=0; i < ele.n_nodes(); i++)
693  {
694  const Value &node_value =
695  Value( const_cast<typename Value::return_type &>(
696  this->value(vertices[i],
697  ElementAccessor<spacedim>(ele.orig_mesh(), ele.orig_element_idx()) ))
698  );
699  ASSERT_EQ(data_cache->n_comp(), node_value.n_rows()*node_value.n_cols()).error();
700  data_cache->store_value(node_index, node_value.mem_ptr() );
701  ++node_index;
702  }
703  }
704  }
705  break;
706  case OutputTime::ELEM_DATA: {
707  for(const auto & ele : *output_mesh )
708  {
709  unsigned int ele_index = ele.idx();
710  const Value &ele_value =
711  Value( const_cast<typename Value::return_type &>(
712  this->value(ele.centre(),
713  ElementAccessor<spacedim>(ele.orig_mesh(), ele.orig_element_idx()))
714  )
715  );
716  ASSERT_EQ(data_cache->n_comp(), ele_value.n_rows()*ele_value.n_cols()).error();
717  data_cache->store_value(ele_index, ele_value.mem_ptr() );
718  }
719  }
720  break;
722  std::shared_ptr< FieldFE<spacedim, Value> > field_fe_ptr = this->get_field_fe();
723 
724  if (field_fe_ptr) {
725  auto native_output_data_base = stream->prepare_compute_data<double>(this->name(), space_type,
726  (unsigned int)Value::NRows_, (unsigned int)Value::NCols_);
727  // try casting actual ElementDataCache
728  auto native_output_data = std::dynamic_pointer_cast<ElementDataCache<double>>(native_output_data_base);
729  field_fe_ptr->native_data_to_cache(*native_output_data);
730  } else {
731  WarningOut().fmt("Field '{}' of native data space type is not of type FieldFE. Output will be skipped.\n", this->name());
732  }
733  }
734  break;
737  //should not happen
738  break;
739  }
740 }
741 
742 template<int spacedim, class Value>
743 std::shared_ptr< FieldFE<spacedim, Value> > Field<spacedim,Value>::get_field_fe() {
744  ASSERT_EQ_DBG(this->mesh()->region_db().size(), region_fields_.size()).error();
745  ASSERT(!this->shared_->bc_).error("FieldFE output of native data is supported only for bulk fields!");
746 
747  std::shared_ptr< FieldFE<spacedim, Value> > field_fe_ptr;
748 
749  bool is_fe = (region_fields_.size()>0); // indicate if FieldFE is defined on all bulk regions
750  is_fe = is_fe && region_fields_[1] && (typeid(*region_fields_[1]) == typeid(FieldFE<spacedim, Value>));
751  for (unsigned int i=3; i<2*this->mesh()->region_db().bulk_size(); i+=2)
752  if (!region_fields_[i] || (region_fields_[i] != region_fields_[1])) {
753  is_fe = false;
754  break;
755  }
756  if (is_fe) {
757  field_fe_ptr = std::dynamic_pointer_cast< FieldFE<spacedim, Value> >( region_fields_[1] );
758  }
759 
760  return field_fe_ptr;
761 }
762 
763 
764 template<int spacedim, class Value>
765 void Field<spacedim, Value>::cache_reallocate(const ElementCacheMap &cache_map, unsigned int region_idx) const {
766  // Call cache_reinit of FieldAlgoBase descendant on appropriate region
767  if (region_fields_[region_idx] != nullptr)
768  region_fields_[region_idx]->cache_reinit(cache_map);
769 }
770 
771 
772 template<int spacedim, class Value>
773 void Field<spacedim, Value>::cache_update(ElementCacheMap &cache_map, unsigned int region_patch_idx) const {
774  unsigned int region_idx = cache_map.region_idx_from_chunk_position(region_patch_idx);
775  if (region_fields_[region_idx] != nullptr) // skips bounadry regions for bulk fields and vice versa
776  region_fields_[region_idx]->cache_update(value_cache_, cache_map, region_patch_idx);
777 }
778 
779 
780 template<int spacedim, class Value>
782  if (region_fields_[i_reg] != nullptr) return region_fields_[i_reg]->set_dependency(field_set);
783  else return std::vector<const FieldCommon *>();
784 }
785 
786 
787 template<int spacedim, class Value>
789  return &value_cache_;
790 }
791 
792 
793 template<>
795  return nullptr;
796 }
797 
798 template<>
800  return nullptr;
801 }
802 
803 
804 template<int spacedim, class Value>
806  return &value_cache_;
807 }
808 
809 
810 template<>
812  return nullptr;
813 }
814 
815 template<>
817  return nullptr;
818 }
819 
820 
821 
822 
823 #endif /* FIELD_IMPL_HH_ */
void check_initialized_region_fields_()
Definition: field.impl.hh:541
Classes for auxiliary output mesh.
Iterator< ValueType > begin() const
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:74
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:159
virtual bool is_active_field_descriptor(const Input::Record &in_rec, const std::string &input_name)
Definition: field.impl.hh:615
pair< double, FieldBasePtr > HistoryPoint
Pair: time, pointer to FieldBase instance.
Definition: field.hh:387
RegionSet get_region_set(const std::string &set_name) const
Definition: region.cc:328
#define ASSERT_EQ_DBG(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:332
bool is_jump_time_
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
Point accessor allow iterate over bulk quadrature points defined in local element coordinates...
Definition: eval_subset.hh:96
unsigned int eval_point_idx() const
Return index in EvalPoints object.
Definition: eval_subset.hh:459
Value::return_type get_value(const FieldValueCache< typename Value::element_type > &field_cache, unsigned int elem_patch_idx, unsigned int eval_points_idx) const
Return value of evaluation point given by idx of element in patch and local point idx in EvalPoints f...
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Point accessor allow iterate over quadrature points of given side defined in local element coordinate...
Definition: eval_subset.hh:154
FieldFlag::Flags get_flags() const
static const Input::Type::Instance & get_input_type_instance(Input::Type::Selection value_selection=Input::Type::Selection())
unsigned int bulk_size() const
Definition: region.cc:268
Reader for (slightly) modified input files.
std::string get_value_attribute() const override
Definition: field.impl.hh:456
static unsigned int get()
Return number of stored elements.
void fill_data_cache(OutputTime::DiscreteSpace space_type, std::shared_ptr< OutputTime > stream, std::shared_ptr< ElementDataCache< typename Value::element_type >> data_cache)
Fills acutally the data cache with field values, used in compute_field_data.
Definition: field.impl.hh:677
OutputTime::DiscreteSpace get_output_type() const
std::string name_
void cache_reallocate(const ElementCacheMap &cache_map, unsigned int region_idx) const override
Implements FieldCommon::cache_allocate.
Definition: field.impl.hh:765
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:95
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
FieldValueCache< double > * value_cache() override
Implements FieldCommon::value_cache.
Definition: field.impl.hh:788
unsigned int component_index_
std::pair< double, double > limits() const
Store data of one initialization message.
Definition: field_common.hh:96
void store_value(unsigned int idx, const T *value)
Directing class of FieldValueCache.
Definition: mesh.h:77
Iterator< Ret > find(const string &key) const
void update_history(const TimeStep &time)
Definition: field.impl.hh:470
std::shared_ptr< ElementDataCacheBase > OutputDataPtr
Definition: output_time.hh:126
Helper class that stores data of generic types.
Definition: type_generic.hh:89
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
void observe_output(std::shared_ptr< Observe > observe) override
Definition: field.impl.hh:408
double last_time_
unsigned int eval_point_idx() const
Return index in EvalPoints object.
Definition: eval_subset.hh:450
RegionSet union_set(std::vector< std::string > set_names) const
Definition: region.cc:481
const std::string & input_default() const
bool set_time(const TimeStep &time, LimitSide limit_side) override
Definition: field.impl.hh:300
const RegionDB & region_db() const
Definition: mesh.h:160
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
void field_output(std::shared_ptr< OutputTime > stream) override
Definition: field.impl.hh:395
Class for declaration of the integral input data.
Definition: type_base.hh:483
Value::return_type operator[](unsigned int i_cache_point) const
Return item of value_cache_ given by i_cache_point.
Definition: field.impl.hh:184
Basic time management functionality for unsteady (and steady) solvers (class Equation).
virtual FieldBasePtr create_field(Input::Record rec, const FieldCommon &field)
Definition: field.impl.hh:603
IT::Instance get_input_type() override
Definition: field.impl.hh:192
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
std::vector< const FieldCommon * > set_dependency(FieldSet &field_set, unsigned int i_reg) const override
Definition: field.impl.hh:781
Point accessor allow iterate over quadrature points of given side defined in local element coordinate...
Definition: eval_subset.hh:184
Field()
Definition: field.impl.hh:41
Class for declaration of inputs sequences.
Definition: type_base.hh:339
unsigned int region_idx_from_chunk_position(unsigned int chunk_pos) const
Return begin position of region chunk specified by position in map.
std::shared_ptr< SharedData > data_
Definition: field.hh:401
static constexpr bool value
Definition: json.hpp:87
std::vector< uint > shape_
constexpr bool match(Mask mask) const
Definition: flag_array.hh:163
std::shared_ptr< SharedData > shared_
IteratorBase end() const
EI_Address ei_address() const
Definition: accessors.cc:178
bool opt_val(const string &key, Ret &value) const
void set(FieldBasePtr field, double time, std::vector< std::string > region_set_names={"ALL"})
Definition: field.impl.hh:258
unsigned int eval_point_idx() const
Return index in EvalPoints object.
Definition: eval_subset.hh:114
double time() const
std::shared_ptr< FieldFE< spacedim, Value > > get_field_fe()
Definition: field.impl.hh:743
const std::string & name() const
bool ge(double other_time) const
void cache_update(ElementCacheMap &cache_map, unsigned int region_patch_idx) const override
Implements FieldCommon::cache_update.
Definition: field.impl.hh:773
unsigned int FieldEnum
Definition: field_values.hh:56
FieldFlag::Flags & flags()
Region find_id(unsigned int id, unsigned int dim) const
Definition: region.cc:180
const UnitSI & units() const
std::vector< std::string > get_and_check_operands(const Input::Array &operands) const
Definition: region.cc:312
T get_root_interface() const
Returns the root accessor.
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
IT::Array get_multifield_input_type() override
Definition: field.impl.hh:199
std::shared_ptr< ControlField > no_check_control_field_
Definition: field.hh:411
static const unsigned int N_DISCRETE_SPACES
Definition: output_time.hh:107
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm) const
Definition: field.hh:452
const ElementCacheMap * elm_cache_map() const
Definition: eval_subset.hh:60
LimitSide last_limit_side_
void set_shape(uint n_rows, uint n_cols)
double end() const
virtual void value_list(const Armor::array &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list) const
Definition: field.hh:466
double read_time(Input::Iterator< Input::Tuple > time_it, double default_time=std::numeric_limits< double >::quiet_NaN()) const
std::vector< FieldBasePtr > region_fields_
Definition: field.hh:416
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
double read_time(Input::Iterator< Input::Tuple > time_it, double default_time=std::numeric_limits< double >::quiet_NaN()) const
FieldResult
FieldResult field_result(RegionSet region_set) const override
Indicates special field states.
Definition: field.impl.hh:433
bool is_jump_time()
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:99
std::vector< std::shared_ptr< FactoryBase > > factories_
Definition: field.hh:418
unsigned int eval_point_idx() const
Return index in EvalPoints object.
Definition: eval_subset.hh:471
Point accessor allow iterate over quadrature points of given side defined in local element coordinate...
Definition: eval_subset.hh:213
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
Definition: asserts.hh:336
auto disable_where(const Field< spacedim, typename FieldValue< spacedim >::Enum > &control_field, const vector< FieldEnum > &value_list) -> Field &
Definition: field.impl.hh:209
static std::vector< MessageData > messages_data_
Vector of data of initialization messages.
boost::circular_buffer< HistoryPoint > RegionHistory
Nearest history of one region.
Definition: field.hh:389
void set_component_index(unsigned int idx)
bool is_constant(Region reg) override
Definition: field.impl.hh:249
void add_factory(std::shared_ptr< FactoryBase > factory)
Definition: field.impl.hh:597
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:296
Class OutputElement and its iterator OutputElementIterator on the output mesh.
bool gt(double other_time) const
const Mesh * mesh() const
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:270
FieldCommon & name(const string &name)
unsigned int n_comp() const
bool changed() const
void set_mesh(const Mesh &mesh) override
Definition: field.impl.hh:219
void set_history_changed()
Point accessor allow iterate over local Observe points.
Definition: observe.hh:325
Accessor to the data with type Type::Tuple.
Definition: accessors.hh:411
bool is_valid() const
Returns false if the region has undefined/invalid value.
Definition: region.hh:78
const std::string & input_name() const
void copy_from(const FieldCommon &other) override
Definition: field.impl.hh:378
unsigned int n_comp() 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
void compute_field_data(OutputTime::DiscreteSpace space_type, std::shared_ptr< OutputTime > stream)
Definition: field.impl.hh:654
#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_
Field & operator=(const Field &other)
Definition: field.impl.hh:119
static const unsigned int history_length_limit_
LimitSide
Definition: field_common.hh:61
void set_input_list(const Input::Array &list, const TimeGovernor &tg) override
Definition: field.impl.hh:623
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual)
Definition: asserts.hh:328
unsigned int elem_patch_idx() const
Definition: eval_subset.hh:65
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
Value::return_type operator()(BulkPoint &p)
Return appropriate value to BulkPoint in FieldValueCache.
Definition: field.impl.hh:155
Definition: field.hh:63
FieldValueCache< typename Value::element_type > value_cache_
Definition: field.hh:429
unsigned int idx() const
Returns a global index of the region.
Definition: region.hh:82