Flow123d  jenkins-Flow123d-windows-release-multijob-285
field_set.cc
Go to the documentation of this file.
1 /*
2  * field_set.cc
3  *
4  * Created on: Mar 8, 2014
5  * Author: jb
6  */
7 
8 #include "fields/field_set.hh"
9 #include "system/sys_profiler.hh"
10 
11 
12 
13 FieldSet &FieldSet::operator +=(FieldCommon &add_field) {
14  FieldCommon *found_field = field(add_field.name());
15  if (found_field) {
16  ASSERT(&add_field==found_field, "Another field of the same name exists when adding field: %s\n",
17  add_field.name().c_str());
18  } else {
19  field_list.push_back(&add_field);
20  }
21  return *this;
22 }
23 
24 
25 
26 FieldSet &FieldSet::operator +=(const FieldSet &other) {
27  for(auto field_ptr : other.field_list) this->operator +=(*field_ptr);
28  return *this;
29 }
30 
31 
32 
33 FieldSet FieldSet::subset(std::vector<std::string> names) const {
34  FieldSet set;
35  for(auto name : names) set += (*this)[name];
36  return set;
37 }
38 
39 
40 
41 FieldSet FieldSet::subset( FieldFlag::Flags::Mask mask) const {
42  FieldSet set;
43  for(auto field : field_list)
44  if (field->flags().match(mask)) set += *field;
45  return set;
46 }
47 
48 
49 
50 Input::Type::Record FieldSet::make_field_descriptor_type(const std::string &equation_name) const {
51  Input::Type::Record rec = FieldCommon::field_descriptor_record(equation_name + "_Data");
52  for(auto field : field_list) {
53  if ( field->flags().match(FieldFlag::declare_input) ) {
54  string description = field->description() + " $[" + field->units().format_latex() + "]$";
55 
56  // Adding units is not so simple.
57  // 1) It must be correct for Latex.
58  // 2) It should be consistent with rest of documentation.
59  // 3) Should be specified for all fields.
60  //if (units != "") description+= " [" +field->units() + "]";
61 
62  // TODO: temporary solution, see FieldCommon::multifield_
63  if (field->is_multifield()) {
65  } else {
66  rec.declare_key(field->input_name(), field->get_input_type(), description);
67  }
68  }
69 
70  }
71  return rec;
72 }
73 
74 
75 
76 Input::Type::Selection FieldSet::make_output_field_selection(const string &name, const string &desc)
77 {
78  namespace IT=Input::Type;
79  IT::Selection sel(name, desc);
80  int i=0;
81  // add value for each field excluding boundary fields
82  for( auto field : field_list)
83  {
84  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
85  {
86  string desc = "Output of the field " + field->name() + " $[" + field->units().format_latex()+"]$";
87  if (field->description().length() > 0)
88  desc += " (" + field->description() + ").";
89  else
90  desc += ".";
91  sel.add_value(i, field->name(), desc);
92  i++;
93  }
94  }
95 
96  return sel;
97 }
98 
99 
100 
101 void FieldSet::set_field(const std::string &dest_field_name, FieldCommon &source)
102 {
103  auto &field = (*this)[dest_field_name];
104  field.copy_from(source);
105 }
106 
107 
108 
109 FieldCommon *FieldSet::field(const std::string &field_name) const {
110  for(auto field : field_list)
111  if (field->name() ==field_name) return field;
112  return nullptr;
113 }
114 
115 
116 
117 FieldCommon &FieldSet::operator[](const std::string &field_name) const {
118  FieldCommon *found_field=field(field_name);
119  if (found_field) return *found_field;
120 
121  THROW(ExcUnknownField() << FieldCommon::EI_Field(field_name));
122  return *field_list[0]; // formal to prevent compiler warning
123 }
124 
125 
126 
127 bool FieldSet::changed() const {
128  bool changed_all=false;
129  for(auto field : field_list) changed_all = changed_all || field->changed();
130  return changed_all;
131 }
132 
133 
134 
135 bool FieldSet::is_constant(Region reg) const {
136  bool const_all=true;
137  for(auto field : field_list) const_all = const_all && field->is_constant(reg);
138  return const_all;
139 }
140 
141 
142 
144  START_TIMER("Fill OutputData");
145  for(auto field : field_list)
146  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
147  field->output(stream);
148 }
149 
150 
151 
152 // OBSOLETE method
153 FieldCommon &FieldSet::add_field( FieldCommon *field, const string &name,
154  const string &desc, const string & d_val) {
155  *this += field->name(name).description(desc).input_default(d_val);
156  return *field;
157 }
158 
159 
160 
161 std::ostream &operator<<(std::ostream &stream, const FieldSet &set) {
162  for(FieldCommon * field : set.field_list) {
163  stream << *field
164  << std::endl;
165  }
166  return stream;
167 }
std::vector< FieldCommon * > field_list
List of all fields.
Definition: field_set.hh:234
virtual IT::Record & get_multifield_input_type()=0
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:47
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:51
virtual void copy_from(const FieldCommon &other)=0
FieldCommon & operator[](const std::string &field_name) const
Definition: field_set.cc:117
static constexpr Mask allow_output
The field can output. Is part of generated output selection. (default on)
Definition: field_flag.hh:27
virtual bool is_constant(Region reg)=0
virtual void output(OutputTime *stream)=0
std::ostream & operator<<(std::ostream &stream, const FieldSet &set)
Definition: field_set.cc:161
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
FieldCommon * field(const std::string &field_name) const
Definition: field_set.cc:109
FieldCommon & add_field(FieldCommon *field, const string &name, const string &desc, const string &d_val="")
Definition: field_set.cc:153
#define ASSERT(...)
Definition: global_defs.h:121
FieldCommon & input_default(const string &input_default)
Definition: field_common.hh:92
Selection & add_value(const int value, const std::string &key, const std::string &description="")
static IT::Record field_descriptor_record(const string &record_name)
Definition: field_common.cc:34
#define START_TIMER(tag)
Starts a timer with specified tag.
void set_field(const std::string &dest_field_name, FieldCommon &source)
Definition: field_set.cc:101
bool is_multifield() const
The class for outputting data during time.
Definition: output_time.hh:32
FieldCommon & description(const string &description)
Definition: field_common.hh:80
void output(OutputTime *stream)
Definition: field_set.cc:143
bool is_constant(Region reg) const
Definition: field_set.cc:135
Input::Type::Selection make_output_field_selection(const string &name, const string &desc="")
Definition: field_set.cc:76
FieldCommon & name(const string &name)
Definition: field_common.hh:73
virtual IT::AbstractRecord & get_input_type()=0
bool changed() const
Record type proxy class.
Definition: type_record.hh:169
FieldCommon & flags(FieldFlag::Flags::Mask mask)
const std::string & input_name() const
bool is_bc() const
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
bool changed() const
Definition: field_set.cc:127
Template for classes storing finite set of named values.
Input::Type::Record make_field_descriptor_type(const std::string &equation_name) const
Definition: field_set.cc:50
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:25
Record & declare_key(const string &key, const KeyType &type, const Default &default_value, const string &description)
Definition: type_record.cc:430