deal2lkit: A ToolKit library for Deal.II
parsed_zero_average_constraints.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2016 by the deal2lkit authors
4 //
5 // This file is part of the deal2lkit library.
6 //
7 // The deal2lkit library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal2lkit distribution.
13 //
14 //-----------------------------------------------------------
15 
17 #include <deal.II/dofs/dof_tools.h>
18 
19 
20 
21 D2K_NAMESPACE_OPEN
22 
23 template <int dim, int spacedim>
25 ParsedZeroAverageConstraints(const std::string &parsed_name,
26  const unsigned int &n_components,
27  const std::string &parsed_component_names,
28  const std::string &parsed_components,
29  const std::string &parsed_boundary_components):
30  ParameterAcceptor(parsed_name),
31  name (parsed_name),
32  str_components (parsed_components),
33  str_boundary_components (parsed_boundary_components),
34  str_component_names (parsed_component_names),
35  mask(n_components, false),
36  boundary_mask(n_components, false),
37  n_components(n_components)
38 {}
39 
40 template <int dim, int spacedim>
42 {
43  for (unsigned int c=0; c<components.size(); ++c)
44  {
45  if ((std::find(_component_names.begin(), _component_names.end(), components[c]) != _component_names.end()))
46  for (unsigned int j=0; j<_component_names.size(); ++j)
47  mask[j] = (_component_names[j] == components[c] || mask[j]);
48  else
49  {
50  try
51  {
52  unsigned int m = Utilities::string_to_int(components[c]);
53  AssertThrow(m < n_components, ExcWrongComponent(m,n_components));
54  mask[m] = true;
55  }
56  catch (std::exception &exc)
57  AssertThrow(false, ExcWrongVariable(components[c],_component_names));
58  }
59 
60  }
61 
62  // boundary
63  for (unsigned int c=0; c<boundary_components.size(); ++c)
64  {
65 
66  if ((std::find(_component_names.begin(), _component_names.end(), boundary_components[c]) != _component_names.end()))
67  for (unsigned int j=0; j<_component_names.size(); ++j)
69 
70  else
71  {
72  try
73  {
74  unsigned int m = Utilities::string_to_int(boundary_components[c]);
75  AssertThrow(m < n_components, ExcWrongComponent(m,n_components));
76  boundary_mask[m] = true;
77  }
78  catch (std::exception &exc)
79  AssertThrow(false, ExcWrongVariable(boundary_components[c],_component_names));
80  }
81 
82  }
83 
84 }
85 
86 
87 
88 template <int dim, int spacedim>
90 {
91  return ComponentMask(mask);
92 }
93 
94 
95 template <int dim, int spacedim>
97 {
99 }
100 
101 
102 template <int dim, int spacedim>
104 {
105  if (str_component_names != "")
106  add_parameter(prm, &_component_names, "Known component names", str_component_names,
108  "These variables can be used to set the corrisponding component mask, "
109  "instead of specifying each component number");
110 
111  else
112  {
113  std::vector<std::string> cn(n_components, "u");
114  add_parameter(prm, &_component_names, "Known component names", print(cn),
116  "These variables can be used to set the corrisponding component mask, "
117  "instead of specifying each component number");
118 
119  }
120  add_parameter(prm, &components, "Zero average on whole domain", str_components,
122  "Pattern to be used: "
123  "0,2,p \n"
124  "You can specify the components either by numbers "
125  "or by the corrisponding variable name, which are parsed at "
126  "construction time. ");
127 
128  add_parameter(prm, &boundary_components, "Zero average on boundary", str_boundary_components,
130  "Pattern to be used: "
131  "0,2,p \n"
132  "You can specify the components either by numbers "
133  "or by the corrisponding variable name, which are parsed at "
134  "construction time. ");
135 
136 }
137 
138 template <>
141  const ComponentMask,
142  const bool,
143  ConstraintMatrix &) const
144 {
146 }
147 
148 template <>
151  const ComponentMask,
152  const bool,
153  ConstraintMatrix &) const
154 {
156 }
157 
158 template <>
161  const ComponentMask,
162  const bool,
163  ConstraintMatrix &) const
164 {
166 }
167 
168 
169 template <int dim, int spacedim>
172  const ComponentMask mask,
173  const bool at_boundary,
174  ConstraintMatrix &constraints) const
175 {
176  std::vector<bool> constrained_dofs (dof_handler.n_dofs(), false);
177 
178  if (at_boundary)
179  DoFTools::extract_boundary_dofs (dof_handler,
180  mask,
181  constrained_dofs);
182  else
183  DoFTools::extract_dofs (dof_handler,
184  mask,
185  constrained_dofs);
186 
187 
188  const unsigned int first_dof
189  = std::distance (constrained_dofs.begin(),
190  std::find (constrained_dofs.begin(),
191  constrained_dofs.end(),
192  true) );
193 
194  constraints.add_line (first_dof);
195  for (unsigned int i=first_dof+1; i<dof_handler.n_dofs(); ++i)
196  if (constrained_dofs[i] == true)
197  constraints.add_entry(first_dof, i, -1);
198 }
199 
200 
201 template <int dim, int spacedim>
204  ConstraintMatrix &constraints) const
205 {
206  for (unsigned int i=0; i<n_components; ++i)
207  {
208  std::vector<bool> m(n_components,false);
209  std::vector<bool> m_boundary(n_components,false);
210 
211  if (boundary_mask[i])
212  {
213  m_boundary[i] = true;
215  ComponentMask(m_boundary),
216  true,
217  constraints);
218  }
219 
220  if (mask[i])
221  {
222  m[i] = true;
224  ComponentMask(m),
225  false,
226  constraints);
227  }
228  }
229 }
230 
231 
232 D2K_NAMESPACE_CLOSE
233 
234 template class deal2lkit::ParsedZeroAverageConstraints<1,1>;
235 template class deal2lkit::ParsedZeroAverageConstraints<1,2>;
236 template class deal2lkit::ParsedZeroAverageConstraints<1,3>;
237 template class deal2lkit::ParsedZeroAverageConstraints<2,2>;
238 template class deal2lkit::ParsedZeroAverageConstraints<2,3>;
239 template class deal2lkit::ParsedZeroAverageConstraints<3,3>;
virtual void parse_parameters_call_back()
parse_parameters_call_back is inherithed by ParameterAcceptor
virtual void declare_parameters(ParameterHandler &prm)
declare_parameters is inherithed by ParameterAcceptor
A parameter acceptor base class.
static ParameterHandler prm
Static parameter.
ComponentMask get_mask() const
return the ComponentMask
#define AssertThrow(cond, exc)
void add_line(const size_type line)
static::ExceptionBase & ExcImpossibleInDim(int arg1)
void add_entry(const size_type line, const size_type column, const double value)
void apply_zero_average_constraints(const DoFHandler< dim, spacedim > &dof_handler, ConstraintMatrix &constraints) const
Compute the zero average constraints and apply them on the given constraint matrix.
types::global_dof_index n_dofs() const
void internal_zero_average_constraints(const DoFHandler< dim, spacedim > &dof_handler, const ComponentMask mask, const bool at_boundary, ConstraintMatrix &constraints) const
void extract_boundary_dofs(const DoFHandlerType &dof_handler, const ComponentMask &component_mask, std::vector< bool > &selected_dofs, const std::set< types::boundary_id > &boundary_ids=std::set< types::boundary_id >())
std::vector< std::string > boundary_components
void extract_dofs(const DoFHandler< dim, spacedim > &dof_handler, const ComponentMask &component_mask, std::vector< bool > &selected_dofs)
std::string print(const std::vector< Type > &list, const std::string sep=",")
Return a string containing the content of the vector, with elements separated by the @ sep parameter...
Definition: utilities.h:425
int string_to_int(const std::string &s)
void add_parameter(ParameterHandler &prm, T *parameter, const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Add a parameter the given parameter list.
ComponentMask get_boundary_mask() const
return the ComponentMask at boundary
ParsedZeroAverageConstraints(const std::string &name, const unsigned int &n_components=1, const std::string &component_names="", const std::string &default_components="", const std::string &default_boundary_components="")
Constructor.