Flow123d  release_2.2.0-37-g336ee74
field_set.cc
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_set.cc
15  * @brief
16  */
17 
18 #include "fields/field_set.hh"
19 #include "system/sys_profiler.hh"
21 
22 
23 
24 FieldSet &FieldSet::operator +=(FieldCommon &add_field) {
25  FieldCommon *found_field = field(add_field.name());
26  if (found_field) {
27  OLD_ASSERT(&add_field==found_field, "Another field of the same name exists when adding field: %s\n",
28  add_field.name().c_str());
29  } else {
30  field_list.push_back(&add_field);
31  }
32  return *this;
33 }
34 
35 
36 
37 FieldSet &FieldSet::operator +=(const FieldSet &other) {
38  for(auto field_ptr : other.field_list) this->operator +=(*field_ptr);
39  return *this;
40 }
41 
42 
43 
44 FieldSet FieldSet::subset(std::vector<std::string> names) const {
45  FieldSet set;
46  for(auto name : names) set += (*this)[name];
47  return set;
48 }
49 
50 
51 
52 FieldSet FieldSet::subset( FieldFlag::Flags::Mask mask) const {
53  FieldSet set;
54  for(auto field : field_list)
55  if (field->flags().match(mask)) set += *field;
56  return set;
57 }
58 
59 
60 
61 Input::Type::Record FieldSet::make_field_descriptor_type(const std::string &equation_name) const {
62  string rec_name = equation_name + ":Data";
64  Input::Type::Record rec = Input::Type::Record(rec_name, desc)
66 
67  for(auto field : field_list) {
68  if ( field->flags().match(FieldFlag::declare_input) ) {
69  string description = field->description() + " (($[" + field->units().format_latex() + "]$))";
70 
71  // Adding units is not so simple.
72  // 1) It must be correct for Latex.
73  // 2) It should be consistent with rest of documentation.
74  // 3) Should be specified for all fields.
75  //if (units != "") description+= " [" +field->units() + "]";
76 
77  // TODO: temporary solution, see FieldCommon::multifield_
78 
79  std::shared_ptr<Input::Type::TypeBase> field_type_ptr;
80  if (field->is_multifield()) {
81  field_type_ptr = std::make_shared<Input::Type::Array>(field->get_multifield_input_type());
82  } else {
83  field_type_ptr = std::make_shared<Input::Type::Instance>(field->get_input_type());
84  }
85  OLD_ASSERT( field->units().is_def() , "units not def.");
86  rec.declare_key(field->input_name(), field_type_ptr, Input::Type::Default::optional(), description,
87  { {FlowAttribute::field_unit(), field->units().json() },
89  }
90 
91  }
92  return rec.close();
93 }
94 
95 
96 /*
97 Input::Type::Selection FieldSet::make_output_field_selection(const string &name, const string &desc)
98 {
99  namespace IT=Input::Type;
100  IT::Selection sel(name, desc);
101  int i=0;
102  // add value for each field excluding boundary fields
103  for( auto field : field_list)
104  {
105  if ( !field->is_bc() && field->flags().match( FieldFlag::allow_output) )
106  {
107  string desc = "Output of the field " + field->name() + " (($[" + field->units().format_latex()+"]$))";
108  if (field->description().length() > 0)
109  desc += " (" + field->description() + ").";
110  else
111  desc += ".";
112  DebugOut() << field->get_value_attribute();
113 
114  sel.add_value(i, field->name(), desc, { {FlowAttribute::field_value_shape(), field->get_value_attribute()} } );
115  i++;
116  }
117  }
118 
119  return sel;
120 }
121 */
122 
123 
124 void FieldSet::set_field(const std::string &dest_field_name, FieldCommon &source)
125 {
126  auto &field = (*this)[dest_field_name];
127  field.copy_from(source);
128 }
129 
130 
131 
132 FieldCommon *FieldSet::field(const std::string &field_name) const {
133  for(auto field : field_list)
134  if (field->name() ==field_name) return field;
135  return nullptr;
136 }
137 
138 
139 
140 FieldCommon &FieldSet::operator[](const std::string &field_name) const {
141  FieldCommon *found_field=field(field_name);
142  if (found_field) return *found_field;
143 
144  THROW(ExcUnknownField() << FieldCommon::EI_Field(field_name));
145  return *field_list[0]; // formal to prevent compiler warning
146 }
147 
148 
149 bool FieldSet::set_time(const TimeStep &time, LimitSide limit_side) {
150  bool changed_all=false;
151  for(auto field : field_list) changed_all = field->set_time(time, limit_side) || changed_all;
152  return changed_all;
153 }
154 
155 
156 
157 bool FieldSet::changed() const {
158  bool changed_all=false;
159  for(auto field : field_list) changed_all = changed_all || field->changed();
160  return changed_all;
161 }
162 
163 
164 
165 bool FieldSet::is_constant(Region reg) const {
166  bool const_all=true;
167  for(auto field : field_list) const_all = const_all && field->is_constant(reg);
168  return const_all;
169 }
170 
171 
173  bool is_jump = false;
174  for(auto field : field_list) is_jump = is_jump || field->is_jump_time();
175  return is_jump;
176 }
177 
178 
179 
180 // OBSOLETE method
182  const string &desc, const string & d_val) {
183  *this += field->name(name).description(desc).input_default(d_val);
184  return *field;
185 }
186 
187 
188 
189 std::ostream &operator<<(std::ostream &stream, const FieldSet &set) {
190  for(FieldCommon * field : set.field_list) {
191  stream << *field
192  << std::endl;
193  }
194  return stream;
195 }
std::vector< FieldCommon * > field_list
List of all fields.
Definition: field_set.hh:243
bool is_jump_time() const
Definition: field_set.cc:172
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:60
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:61
virtual void copy_from(const FieldCommon &other)=0
FieldCommon & operator[](const std::string &field_name) const
Definition: field_set.cc:140
virtual bool is_constant(Region reg)=0
virtual std::string get_value_attribute() const =0
virtual IT::Instance get_input_type()=0
virtual bool set_time(const TimeStep &time, LimitSide limit_side)=0
static const std::string field_descriptor_record_description(const string &record_name)
Definition: field_common.cc:68
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:303
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:124
#define OLD_ASSERT(...)
Definition: global_defs.h:131
FieldCommon * field(const std::string &field_name) const
Definition: field_set.cc:132
FieldCommon & add_field(FieldCommon *field, const string &name, const string &desc, const string &d_val="")
Definition: field_set.cc:181
FieldCommon & input_default(const string &input_default)
static IT::Record field_descriptor_record(const string &record_name)
Definition: field_common.cc:55
void set_field(const std::string &dest_field_name, FieldCommon &source)
Definition: field_set.cc:124
static string field_value_shape()
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:490
bool is_multifield() const
Record & copy_keys(const Record &other)
Copy keys from other record.
Definition: type_record.cc:215
bool is_jump_time()
FieldCommon & description(const string &description)
virtual IT::Array get_multifield_input_type()=0
bool set_time(const TimeStep &time, LimitSide limit_side)
Definition: field_set.cc:149
bool is_constant(Region reg) const
Definition: field_set.cc:165
FieldCommon & name(const string &name)
Definition: field_common.hh:97
static string field_unit()
bool changed() const
friend std::ostream & operator<<(std::ostream &stream, const FieldSet &set)
Definition: field_set.cc:189
Record type proxy class.
Definition: type_record.hh:182
FieldCommon & flags(FieldFlag::Flags::Mask mask)
const std::string & input_name() const
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
Representation of one time step..
bool changed() const
Definition: field_set.cc:157
LimitSide
Definition: field_common.hh:47
Input::Type::Record make_field_descriptor_type(const std::string &equation_name) const
Definition: field_set.cc:61
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