Flow123d  jenkins-Flow123d-windows32-release-multijob-51
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 description = field->description() + " " + field->units().format();
54 
55  // Adding units is not so simple.
56  // 1) It must be correct for Latex.
57  // 2) It should be consistent with rest of documentation.
58  // 3) Should be specified for all fields.
59  //if (units != "") description+= " [" +field->units() + "]";
60  rec.declare_key(field->input_name(), field->get_input_type(), description);
61  }
62 
63  }
64  return rec;
65 }
66 
67 
68 
69 Input::Type::Selection FieldSet::make_output_field_selection(const string &name, const string &desc)
70 {
71  namespace IT=Input::Type;
72  IT::Selection sel(name, desc);
73  int i=0;
74  // add value for each field excluding boundary fields
75  for( auto field : field_list)
76  {
77  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
78  {
79  string desc = "Output of the field " + field->name() + " " + field->units().format();
80  if (field->description().length() > 0)
81  desc += " (" + field->description() + ").";
82  else
83  desc += ".";
84  sel.add_value(i, field->name(), desc);
85  i++;
86  }
87  }
88 
89  return sel;
90 }
91 
92 
93 
94 void FieldSet::set_field(const std::string &dest_field_name, FieldCommon &source)
95 {
96  auto &field = (*this)[dest_field_name];
97  field.copy_from(source);
98 }
99 
100 
101 
102 FieldCommon *FieldSet::field(const std::string &field_name) const {
103  for(auto field : field_list)
104  if (field->name() ==field_name) return field;
105  return nullptr;
106 }
107 
108 
109 
110 FieldCommon &FieldSet::operator[](const std::string &field_name) const {
111  FieldCommon *found_field=field(field_name);
112  if (found_field) return *found_field;
113 
114  THROW(ExcUnknownField() << FieldCommon::EI_Field(field_name));
115  return *field_list[0]; // formal to prevent compiler warning
116 }
117 
118 
119 
120 bool FieldSet::changed() const {
121  bool changed_all=false;
122  for(auto field : field_list) changed_all = changed_all || field->changed();
123  return changed_all;
124 }
125 
126 
127 
128 bool FieldSet::is_constant(Region reg) const {
129  bool const_all=true;
130  for(auto field : field_list) const_all = const_all && field->is_constant(reg);
131  return const_all;
132 }
133 
134 
135 
137  for(auto field : field_list)
138  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
139  field->output(stream);
140 }
141 
142 
143 
144 // OBSOLETE method
145 FieldCommon &FieldSet::add_field( FieldCommon *field, const string &name,
146  const string &desc, const string & d_val) {
147  *this += field->name(name).description(desc).input_default(d_val);
148  return *field;
149 }
150 
151 
152 
153 std::ostream &operator<<(std::ostream &stream, const FieldSet &set) {
154  for(FieldCommon * field : set.field_list) {
155  stream << *field
156  << std::endl;
157  }
158  return stream;
159 }
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:110
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:153
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
FieldCommon * field(const std::string &field_name) const
Definition: field_set.cc:102
FieldCommon & add_field(FieldCommon *field, const string &name, const string &desc, const string &d_val="")
Definition: field_set.cc:145
#define ASSERT(...)
Definition: global_defs.h:121
FieldCommon & input_default(const string &input_default)
Definition: field_common.hh:90
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:94
The class for outputing data during time.
Definition: output_time.hh:37
FieldCommon & description(const string &description)
Definition: field_common.hh:78
void output(OutputTime *stream)
Definition: field_set.cc:136
bool is_constant(Region reg) const
Definition: field_set.cc:128
Input::Type::Selection make_output_field_selection(const string &name, const string &desc="")
Definition: field_set.cc:69
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:120
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:386