Flow123d  jenkins-Flow123d-windows32-release-multijob-28
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 
10 
11 
12 FieldSet &FieldSet::operator +=(FieldCommon &add_field) {
13  FieldCommon *found_field = field(add_field.name());
14  if (found_field) {
15  ASSERT(&add_field==found_field, "Another field of the same name exists when adding field: %s\n",
16  add_field.name().c_str());
17  } else {
18  field_list.push_back(&add_field);
19  }
20  return *this;
21 }
22 
23 
24 
25 FieldSet &FieldSet::operator +=(const FieldSet &other) {
26  for(auto field_ptr : other.field_list) this->operator +=(*field_ptr);
27  return *this;
28 }
29 
30 
31 
32 FieldSet FieldSet::subset(std::vector<std::string> names) const {
33  FieldSet set;
34  for(auto name : names) set += (*this)[name];
35  return set;
36 }
37 
38 
39 
40 FieldSet FieldSet::subset( FieldFlag::Flags::Mask mask) const {
41  FieldSet set;
42  for(auto field : field_list)
43  if (field->flags().match(mask)) set += *field;
44  return set;
45 }
46 
47 
48 
49 Input::Type::Record FieldSet::make_field_descriptor_type(const std::string &equation_name) const {
50  Input::Type::Record rec = FieldCommon::field_descriptor_record(equation_name + "_Data");
51  for(auto field : field_list) {
52  if ( field->flags().match(FieldFlag::declare_input) ) {
53  string units = field->units();
54  string description = field->description();
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  rec.declare_key(field->input_name(), field->get_input_type(), description);
62  }
63 
64  }
65  return rec;
66 }
67 
68 
69 
70 Input::Type::Selection FieldSet::make_output_field_selection(const string &name, const string &desc)
71 {
72  namespace IT=Input::Type;
73  IT::Selection sel(name, desc);
74  int i=0;
75  // add value for each field excluding boundary fields
76  for( auto field : field_list)
77  {
78  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
79  {
80  string desc = "Output of the field " + field->name(); // + " [" + field->units() + "]";
81  if (field->description().length() > 0)
82  desc += " (" + field->description() + ").";
83  else
84  desc += ".";
85  sel.add_value(i, field->name(), desc);
86  i++;
87  }
88  }
89 
90  return sel;
91 }
92 
93 
94 
95 void FieldSet::set_field(const std::string &dest_field_name, FieldCommon &source)
96 {
97  auto &field = (*this)[dest_field_name];
98  field.copy_from(source);
99 }
100 
101 
102 
103 FieldCommon *FieldSet::field(const std::string &field_name) const {
104  for(auto field : field_list)
105  if (field->name() ==field_name) return field;
106  return nullptr;
107 }
108 
109 
110 
111 FieldCommon &FieldSet::operator[](const std::string &field_name) const {
112  FieldCommon *found_field=field(field_name);
113  if (found_field) return *found_field;
114 
115  THROW(ExcUnknownField() << FieldCommon::EI_Field(field_name));
116  return *field_list[0]; // formal to prevent compiler warning
117 }
118 
119 
120 
121 bool FieldSet::changed() const {
122  bool changed_all=false;
123  for(auto field : field_list) changed_all = changed_all || field->changed();
124  return changed_all;
125 }
126 
127 
128 
129 bool FieldSet::is_constant(Region reg) const {
130  bool const_all=true;
131  for(auto field : field_list) const_all = const_all && field->is_constant(reg);
132  return const_all;
133 }
134 
135 
136 
138  for(auto field : field_list)
139  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
140  field->output(stream);
141 }
142 
143 
144 
145 // OBSOLETE method
146 FieldCommon &FieldSet::add_field( FieldCommon *field, const string &name,
147  const string &desc, const string & d_val) {
148  *this += field->name(name).description(desc).input_default(d_val);
149  return *field;
150 }
151 
152 
153 
154 std::ostream &operator<<(std::ostream &stream, const FieldSet &set) {
155  for(FieldCommon * field : set.field_list) {
156  stream << *field
157  << std::endl;
158  }
159  return stream;
160 }
std::vector< FieldCommon * > field_list
List of all fields.
Definition: field_set.hh:234
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:45
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:111
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:154
FieldCommon & units(const string &units)
Set basic units of the field.
FieldCommon * field(const std::string &field_name) const
Definition: field_set.cc:103
FieldCommon & add_field(FieldCommon *field, const string &name, const string &desc, const string &d_val="")
Definition: field_set.cc:146
#define ASSERT(...)
Definition: global_defs.h:120
FieldCommon & input_default(const string &input_default)
Definition: field_common.hh:95
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:32
void set_field(const std::string &dest_field_name, FieldCommon &source)
Definition: field_set.cc:95
The class for outputing data during time.
Definition: output_time.hh:37
FieldCommon & description(const string &description)
Definition: field_common.hh:83
void output(OutputTime *stream)
Definition: field_set.cc:137
bool is_constant(Region reg) const
Definition: field_set.cc:129
Input::Type::Selection make_output_field_selection(const string &name, const string &desc="")
Definition: field_set.cc:70
FieldCommon & name(const string &name)
Definition: field_common.hh:71
virtual IT::AbstractRecord & get_input_type()=0
bool changed() const
Record type proxy class.
Definition: type_record.hh:161
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:121
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:49
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:390