Flow123d  JB-rel-int-test-ea53151
equation.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 equation.cc
15  * @brief Abstract base class for equation clasess.
16  * @author Jan Brezina
17  */
18 
19 #include <petscmat.h>
20 #include "tools/time_governor.hh"
21 
22 
23 #include "equation.hh"
24 #include "system/system.hh"
25 #include "input/accessors.hh"
26 #include "fields/field_set.hh"
27 #include "fields/field_common.hh"
28 #include "fields/bc_field.hh"
29 #include "tools/unit_converter.hh"
30 #include "tools/unit_si.hh"
31 
32 
33 
34 
35 /*****************************************************************************************
36  * Implementation of EqBase
37  */
38 
40  return Input::Type::Record("EquationBase_AUX", "Auxiliary record with keys common for equations. Should not be used.")
42  "Time governor setting.")
43  .close();
44 }
45 
47  return Input::Type::Record("EquationBase_user_field_AUX", "Auxiliary record with common key user_field. Should not be used.")
48  .declare_key("user_fields", Input::Type::Array(
49  FieldSet::make_user_field_type(equation_name)),
51  "Input fields of the equation defined by user.")
52  .close();
53 }
54 
56 : equation_empty_(true),
57  mesh_(NULL),
58  time_(NULL),
59  input_record_(),
60  eq_fieldset_(nullptr)
61 {}
62 
63 
64 
66 : equation_empty_(false),
67  mesh_(&mesh),
68  time_(NULL),
69  input_record_(in_rec),
70  eq_fieldset_(nullptr)
71 {}
72 
73 
75 {
76  time_ = &time;
77 }
78 
80 {
81  return time_->t();
82 }
83 
84 void EquationBase::init_user_fields(Input::Array user_fields, FieldSet &output_fields) {
86  it != user_fields.end();
87  ++it) {
88  std::string field_name = it->val<std::string>("name");
89  auto shape_type = it->val<FieldSet::UserFieldShape>("shape_type");
90 
91  // check if field of same name doesn't exist in FieldSet
92  auto * exist_field = eq_fieldset_->field(field_name);
93  if (exist_field!=nullptr) {
94  THROW(FieldSet::ExcFieldExists() << FieldCommon::EI_Field(field_name));
95  }
96 
97  UnitSI units = UnitSI::dimensionless();
98  Input::Record unit_record;
99  if ( it->opt_val("unit", unit_record) ) {
100  std::string unit_str = unit_record.val<std::string>("unit_formula");
101  try {
102  units.convert_unit_from(unit_str);
103  } catch (ExcInvalidUnit &e) {
104  e << it->ei_address();
105  throw;
106  } catch (ExcNoncorrespondingUnit &e) {
107  e << it->ei_address();
108  throw;
109  }
110  }
111 
112  Input::AbstractRecord field_rec = it->val<Input::AbstractRecord>("field");
113  switch (shape_type)
114  {
115  case FieldSet::scalar:
116  Field<3, FieldValue<3>::Scalar> * scalar_field;
117  scalar_field = new Field<3, FieldValue<3>::Scalar>();
118  *eq_fieldset_+=scalar_field
119  ->name(field_name)
120  .description("")
121  .units( units )
123  scalar_field->set_mesh(*mesh_);
124  scalar_field->set( field_rec, time_->t());
125  scalar_field->set_default_fieldset(*eq_fieldset_);
126  output_fields+=*scalar_field;
127  break;
128  case FieldSet::vector:
129  Field<3, FieldValue<3>::VectorFixed> * vector_field;
130  vector_field = new Field<3, FieldValue<3>::VectorFixed>();
131  *eq_fieldset_+=vector_field
132  ->name(field_name)
133  .description("")
134  .units( units )
136  vector_field->set_mesh(*mesh_);
137  vector_field->set( field_rec, time_->t());
138  vector_field->set_default_fieldset(*eq_fieldset_);
139  output_fields+=*vector_field;
140  break;
141  case FieldSet::tensor:
142  Field<3, FieldValue<3>::TensorFixed> * tensor_field;
143  tensor_field = new Field<3, FieldValue<3>::TensorFixed>();
144  *eq_fieldset_+=tensor_field
145  ->name(field_name)
146  .description("")
147  .units( units )
149  tensor_field->set_mesh(*mesh_);
150  tensor_field->set( field_rec, time_->t());
151  tensor_field->set_default_fieldset(*eq_fieldset_);
152  output_fields+=*tensor_field;
153  break;
154  }
155  }
156 }
static Input::Type::Record & record_template()
Template Record with common keys for derived equations.
Definition: equation.cc:39
virtual double solved_time()
Definition: equation.cc:79
std::shared_ptr< FieldSet > eq_fieldset_
Definition: equation.hh:249
virtual void set_time_governor(TimeGovernor &time)
Definition: equation.cc:74
void init_user_fields(Input::Array user_fields, FieldSet &output_fields)
Definition: equation.cc:84
TimeGovernor * time_
Definition: equation.hh:241
static Input::Type::Record & user_fields_template(std::string equation_name)
Template Record with common key user_fields for derived equations.
Definition: equation.cc:46
Mesh * mesh_
Definition: equation.hh:240
TimeGovernor & time()
Definition: equation.hh:151
FieldCommon & description(const string &description)
FieldCommon & flags(FieldFlag::Flags::Mask mask)
void set_default_fieldset(FieldSet &default_fs)
FieldCommon & name(const string &name)
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
static constexpr Mask equation_result
Match result fields. These are never given by input or copy of input.
Definition: field_flag.hh:55
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:159
static const Input::Type::Record & make_user_field_type(const std::string &equation_name)
Declare input record type of field defined by user.
Definition: field_set.cc:41
Class template representing a field with values dependent on: point, element, and region.
Definition: field.hh:92
void set(FieldBasePtr field, double time, std::vector< std::string > region_set_names={"ALL"})
Definition: field.impl.hh:242
void set_mesh(const Mesh &mesh) override
Definition: field.impl.hh:203
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
Iterator< ValueType > begin() const
IteratorBase end() const
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
const Ret val(const string &key) const
Class for declaration of inputs sequences.
Definition: type_base.hh:339
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:124
Record type proxy class.
Definition: type_record.hh:182
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
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:503
Definition: mesh.h:362
Basic time management functionality for unsteady (and steady) solvers (class Equation).
double t() const
static const Input::Type::Record & get_input_type()
Class for representation SI units of Fields.
Definition: unit_si.hh:40
double convert_unit_from(std::string actual_unit) const
Convert and check user-defined unit.
Definition: unit_si.cc:217
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:55
Abstract base class for equation clasess.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
Basic time management class.