libMesh
variable.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #ifndef LIBMESH_VARIABLE_H
19 #define LIBMESH_VARIABLE_H
20 
21 // Local Includes
22 #include "libmesh/libmesh_common.h"
23 #include "libmesh/fe_type.h"
24 #include "libmesh/id_types.h"
25 
26 // C++ includes
27 #include <set>
28 #include <string>
29 #include <vector>
30 
31 namespace libMesh
32 {
33 
34 // Forward Declaration
35 class System;
36 class MeshBase;
37 
50 class Variable
51 {
52 public:
53 
59  Variable (System * sys,
60  std::string var_name,
61  const unsigned int var_number,
62  const unsigned int first_scalar_num,
63  const FEType & var_type) :
64  _sys(sys),
65  _name(std::move(var_name)),
67  _number(var_number),
68  _first_scalar_number(first_scalar_num),
69  _type(var_type)
70  {}
71 
76  Variable (System * sys,
77  std::string var_name,
78  const unsigned int var_number,
79  const unsigned int first_scalar_num,
80  const FEType & var_type,
81  const std::set<subdomain_id_type> & var_active_subdomains) :
82  _sys(sys),
83  _name(std::move(var_name)),
84  _active_subdomains(var_active_subdomains),
85  _number(var_number),
86  _first_scalar_number(first_scalar_num),
87  _type(var_type)
88  {}
89 
93  Variable (const Variable &) = default;
94  Variable & operator= (const Variable &) = default;
95  Variable (Variable &&) = default;
96  Variable & operator= (Variable &&) = default;
97 
102  bool operator== ( const Variable & other) const
103  {
104  return (_sys == other._sys) &&
105  (_name == other._name) &&
108  (_type == other._type);
109  }
110 
114  System * system() const
115  {
116  return _sys;
117  }
118 
122  const std::string & name() const
123  { return _name; }
124 
128  unsigned int number() const
129  { return _number; }
130 
138  unsigned int first_scalar_number() const
139  { return _first_scalar_number; }
140 
144  const FEType & type() const
145  { return _type; }
146 
152  unsigned int n_components() const;
153 
157  unsigned int n_components(const MeshBase & mesh) const;
158 
168  { return (_active_subdomains.empty() || _active_subdomains.count(sid)); }
169 
175  bool implicitly_active () const
176  { return _active_subdomains.empty(); }
177 
181  const std::set<subdomain_id_type> & active_subdomains() const
182  { return _active_subdomains; }
183 
184 protected:
186  std::string _name;
187  std::set<subdomain_id_type> _active_subdomains;
188  unsigned int _number;
189  unsigned int _first_scalar_number;
191 
192 private:
198  { return _type; }
199 
200  // DofMap can change a VariableGroup type() to disable/enable
201  // p-refinement post-variable-addition.
202  friend class DofMap;
203 };
204 
205 
206 
215 class VariableGroup : public Variable
216 {
217 public:
224  std::vector<std::string> var_names,
225  const unsigned int var_number,
226  const unsigned int first_scalar_num,
227  const FEType & var_type) :
228  Variable (sys,
229  "var_group",
230  var_number,
231  first_scalar_num,
232  var_type),
233  _names(std::move(var_names))
234  {}
235 
236 
242  std::vector<std::string> var_names,
243  const unsigned int var_number,
244  const unsigned int first_scalar_num,
245  const FEType & var_type,
246  const std::set<subdomain_id_type> & var_active_subdomains) :
247 
248  Variable (sys,
249  "var_group",
250  var_number,
251  first_scalar_num,
252  var_type,
253  var_active_subdomains),
254  _names(std::move(var_names))
255  {}
256 
260  VariableGroup (const VariableGroup &) = default;
261  VariableGroup & operator= (const VariableGroup &) = default;
262  VariableGroup (VariableGroup &&) = default;
263  VariableGroup & operator= (VariableGroup &&) = default;
264 
269  bool operator== ( const VariableGroup & other) const
270  {
271  return (this->Variable::operator==(other)) &&
272  (_names == other._names);
273  }
274 
278  unsigned int n_variables () const
279  { return cast_int<unsigned int>(_names.size()); }
280 
285  Variable variable (unsigned int v) const
286  {
287  libmesh_assert_less (v, this->n_variables());
288  return Variable (this->system(),
289  this->name(v),
290  this->number(v),
291  this->first_scalar_number(v),
292  this->type(),
293  this->active_subdomains());
294  }
295 
301  Variable operator() (unsigned int v) const
302  { return this->variable(v); }
303 
307  const std::string & name(unsigned int v) const
308  {
309  libmesh_assert_less (v, this->n_variables());
310  return _names[v];
311  }
312 
316  unsigned int number(unsigned int v) const
317  {
318  libmesh_assert_less (v, this->n_variables());
319  return _number + v;
320  }
321 
322  // Don't let number(uint) hide number()
323  using Variable::number;
324 
329  unsigned int first_scalar_number(unsigned int v) const
330  {
331  libmesh_assert_less (v, this->n_variables());
332  return _first_scalar_number+v;
333  }
334 
340  void append (std::string var_name)
341  { _names.push_back (std::move(var_name)); }
342 
343 protected:
344  std::vector<std::string> _names;
345 };
346 
347 } // namespace libMesh
348 
349 #endif // LIBMESH_VARIABLE_H
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:196
std::set< subdomain_id_type > _active_subdomains
Definition: variable.h:187
std::string _name
Definition: variable.h:186
VariableGroup(System *sys, std::vector< std::string > var_names, const unsigned int var_number, const unsigned int first_scalar_num, const FEType &var_type, const std::set< subdomain_id_type > &var_active_subdomains)
Constructor.
Definition: variable.h:241
VariableGroup & operator=(const VariableGroup &)=default
MeshBase & mesh
const std::string & name(unsigned int v) const
Definition: variable.h:307
The libMesh namespace provides an interface to certain functionality in the library.
Variable(System *sys, std::string var_name, const unsigned int var_number, const unsigned int first_scalar_num, const FEType &var_type)
Constructor.
Definition: variable.h:59
VariableGroup(System *sys, std::vector< std::string > var_names, const unsigned int var_number, const unsigned int first_scalar_num, const FEType &var_type)
Constructor.
Definition: variable.h:223
This is the MeshBase class.
Definition: mesh_base.h:80
FEType & type()
Definition: variable.h:197
void append(std::string var_name)
Appends a variable to the group.
Definition: variable.h:340
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
unsigned int _first_scalar_number
Definition: variable.h:189
unsigned int first_scalar_number() const
Definition: variable.h:138
This class defines the notion of a variable in the system.
Definition: variable.h:50
const std::set< subdomain_id_type > & active_subdomains() const
Definition: variable.h:181
unsigned int n_variables() const
Definition: variable.h:278
bool operator==(const VariableGroup &other) const
Definition: variable.h:269
unsigned int n_components() const
Definition: variable.C:23
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:98
Variable & operator=(const Variable &)=default
std::vector< std::string > _names
Definition: variable.h:344
bool active_on_subdomain(subdomain_id_type sid) const
Definition: variable.h:167
Variable variable(unsigned int v) const
Definition: variable.h:285
This class defines a logically grouped set of variables in the system.
Definition: variable.h:215
Variable operator()(unsigned int v) const
Support vg(v).
Definition: variable.h:301
bool implicitly_active() const
Definition: variable.h:175
System * _sys
Definition: variable.h:185
System * system() const
Definition: variable.h:114
unsigned int number(unsigned int v) const
Definition: variable.h:316
unsigned int _number
Definition: variable.h:188
const std::string & name() const
Definition: variable.h:122
unsigned int number() const
Definition: variable.h:128
Variable(System *sys, std::string var_name, const unsigned int var_number, const unsigned int first_scalar_num, const FEType &var_type, const std::set< subdomain_id_type > &var_active_subdomains)
Constructor.
Definition: variable.h:76
bool operator==(const Variable &other) const
Definition: variable.h:102
unsigned int first_scalar_number(unsigned int v) const
Definition: variable.h:329
const FEType & type() const
Definition: variable.h:144