Hermes2D  3.0
essential_boundary_conditions.cpp
1 // This file is part of Hermes2D
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://www.hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 #include "essential_boundary_conditions.h"
21 #include "exact_solution.h"
22 namespace Hermes
23 {
24  namespace Hermes2D
25  {
26  template<typename Scalar>
28  {
29  current_time = 0.0;
30  value_const = 0.0;
31  }
32 
33  template<typename Scalar>
34  EssentialBoundaryCondition<Scalar>::EssentialBoundaryCondition(std::vector<std::string> markers) : markers(markers)
35  {
36  current_time = 0.0;
37  value_const = 0.0;
38  }
39 
40  template<typename Scalar>
42  {
43  markers.push_back(marker);
44  current_time = 0.0;
45  value_const = 0.0;
46  }
47 
48  template<typename Scalar>
50  {
51  }
52 
53  template<typename Scalar>
55  {
56  this->current_time = time;
57  }
58 
59  template<typename Scalar>
61  {
62  return current_time;
63  }
64 
65  template<typename Scalar>
66  DefaultEssentialBCConst<Scalar>::DefaultEssentialBCConst(std::vector<std::string> markers, Scalar value_const) : EssentialBoundaryCondition<Scalar>(markers)
67  {
68  this->value_const = value_const;
69  }
70 
71  template<typename Scalar>
73  {
74  this->value_const = value_const;
75  this->markers.push_back(marker);
76  }
77 
78  template<typename Scalar>
79  Scalar DefaultEssentialBCConst<Scalar>::value(double x, double y) const
80  {
81  this->warn("EssentialBoundaryCondition::Function used either for a constant condition, or not redefined for nonconstant condition.");
82  return 0.0;
83  }
84 
85  template<typename Scalar>
87  MeshFunctionSharedPtr<Scalar> exact_solution_)
88  : EssentialBoundaryCondition<Scalar>(std::vector<std::string>())
89  {
90  ExactSolutionScalar<Scalar>* exact = dynamic_cast<ExactSolutionScalar<Scalar>*>(exact_solution_.get());
91  if (exact)
92  this->exact_solution = exact;
93  else
94  throw Exceptions::Exception("Wrong mesh function type passed to DefaultEssentialBCNonConst.");
95 
96  for (unsigned int i = 0; i < markers_.size(); i++)
97  this->markers.push_back(markers_[i]);
98  }
99 
100  template<typename Scalar>
101  DefaultEssentialBCNonConst<Scalar>::DefaultEssentialBCNonConst(std::string marker, MeshFunctionSharedPtr<Scalar> exact_solution_)
102  : EssentialBoundaryCondition<Scalar>(std::vector<std::string>())
103  {
104  ExactSolutionScalar<Scalar>* exact = dynamic_cast<ExactSolutionScalar<Scalar>*>(exact_solution_.get());
105  if (exact)
106  this->exact_solution = exact;
107  else
108  throw Exceptions::Exception("Wrong mesh function type passed to DefaultEssentialBCNonConst.");
109  this->markers.push_back(marker);
110  }
111 
112  template<typename Scalar>
113  Scalar DefaultEssentialBCNonConst<Scalar>::value(double x, double y) const
114  {
115  return exact_solution->value(x, y);
116  }
117 
118  template<typename Scalar>
119  EssentialBCs<Scalar>::EssentialBCs() : HermesAnyBC(nullptr)
120  {
121  }
122 
123  template<typename Scalar>
124  EssentialBCs<Scalar>::EssentialBCs(std::vector<EssentialBoundaryCondition<Scalar> *> essential_bcs) : HermesAnyBC(nullptr)
125  {
126  add_boundary_conditions(essential_bcs);
127  }
128 
129  template<typename Scalar>
130  EssentialBCs<Scalar>::EssentialBCs(EssentialBoundaryCondition<Scalar> * boundary_condition) : HermesAnyBC(nullptr)
131  {
132  std::vector<EssentialBoundaryCondition<Scalar> *> boundary_conditions;
133  boundary_conditions.push_back(boundary_condition);
134  add_boundary_conditions(boundary_conditions);
135  }
136 
137  template<typename Scalar>
139  {
140  for (typename std::vector<EssentialBoundaryCondition<Scalar> *>::iterator it = boundary_conditions.begin(); it != boundary_conditions.end(); it++)
141  all.push_back(*it);
142 
143  this->markers.clear();
144  create_marker_cache();
145  }
146 
147  template<typename Scalar>
149  {
150  std::vector<EssentialBoundaryCondition<Scalar> *> boundary_conditions;
151  boundary_conditions.push_back(boundary_condition);
152  add_boundary_conditions(boundary_conditions);
153  }
154 
155  template<typename Scalar>
156  typename std::vector<EssentialBoundaryCondition<Scalar> *>::const_iterator EssentialBCs<Scalar>::begin() const
157  {
158  return all.begin();
159  }
160 
161  template<typename Scalar>
162  typename std::vector<EssentialBoundaryCondition<Scalar> *>::const_iterator EssentialBCs<Scalar>::end() const
163  {
164  return all.end();
165  }
166 
167  template<typename Scalar>
169  {
170  }
171 
172  template<typename Scalar>
174  {
175  bool hermes_any_set = false;
176  this->markers.clear();
177  this->BCs.clear();
178  EssentialBoundaryCondition<Scalar>* any_set = nullptr;
179  for (this->iterator = begin(); iterator != end(); ++iterator)
180  for (std::vector<std::string>::const_iterator it = (*iterator)->markers.begin(); it != (*iterator)->markers.end(); ++it)
181  {
182  if (hermes_any_set)
183  throw Hermes::Exceptions::Exception("Attempt to define a BC on HERMES_ANY together with a BC on a specific part: '%s'.", it->c_str());
184  if ((*it) == HERMES_ANY)
185  {
186  if (any_set != nullptr)
187  throw Hermes::Exceptions::Exception("Attempt to define a BC on HERMES_ANY together with a BC on a specific part: '%s'.", any_set->markers.begin()->c_str());
188  hermes_any_set = true;
189  this->HermesAnyBC = *iterator;
190  }
191  else
192  {
193  any_set = *iterator;
194  for (unsigned short i = 0; i < this->markers.size(); i++)
195  if (this->markers[i] == *it)
196  throw Hermes::Exceptions::Exception("Attempt to define more than one description of the BC on the same part of the boundary with marker '%s'.", it->c_str());
197  this->markers.push_back(*it);
198  this->BCs.push_back(*iterator);
199  }
200  }
201  }
202 
203  template<typename Scalar>
204  const std::vector<std::string>& EssentialBCs<Scalar>::get_markers() const
205  {
206  return this->markers;
207  }
208 
209  template<typename Scalar>
211  {
212  if (this->HermesAnyBC != nullptr)
213  return this->HermesAnyBC;
214  for (unsigned short i = 0; i < this->markers.size(); i++)
215  if (this->markers[i] == marker)
216  return this->BCs[i];
217  return nullptr;
218  }
219 
220  template<typename Scalar>
222  {
223  for (iterator = begin(); iterator != end(); iterator++)
224  (*iterator)->set_current_time(time);
225  }
226 
227  template HERMES_API class EssentialBoundaryCondition < double > ;
228  template HERMES_API class EssentialBoundaryCondition < std::complex<double> > ;
229  template HERMES_API class DefaultEssentialBCConst < double > ;
230  template HERMES_API class DefaultEssentialBCConst < std::complex<double> > ;
231  template HERMES_API class DefaultEssentialBCNonConst < double > ;
232  template HERMES_API class DefaultEssentialBCNonConst < std::complex<double> > ;
233  template HERMES_API class EssentialBCs < double > ;
234  template HERMES_API class EssentialBCs < std::complex<double> > ;
235  }
236 }
Definition: adapt.h:24
Scalar value_const
Special case of a constant function.
EssentialBoundaryCondition< Scalar > * get_boundary_condition(std::string marker)
Return the essential BC on the specified marker.
DefaultEssentialBCConst(std::vector< std::string > markers, Scalar value_const)
Constructors.
::xsd::cxx::tree::time< char, simple_type > time
C++ type corresponding to the time XML Schema built-in type.
double get_current_time() const
Get the current time for time-dependent boundary conditions.
void set_current_time(double time)
Sets the current time for time-dependent boundary conditions.
void add_boundary_conditions(std::vector< EssentialBoundaryCondition< Scalar > * > essential_bcs)
Initializes the class, fills the structures.
void set_current_time(double time)
Set the current time for time-dependent boundary conditions.
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
virtual Scalar value(double x, double y) const
const std::vector< std::string > & get_markers() const
Return all the markers where this set of conditions is defined.
Class representing constant essential boundary condition.