libMesh
variable.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 
193 
194 
203 class VariableGroup : public Variable
204 {
205 public:
212  std::vector<std::string> var_names,
213  const unsigned int var_number,
214  const unsigned int first_scalar_num,
215  const FEType & var_type) :
216  Variable (sys,
217  "var_group",
218  var_number,
219  first_scalar_num,
220  var_type),
221  _names(std::move(var_names))
222  {}
223 
224 
230  std::vector<std::string> var_names,
231  const unsigned int var_number,
232  const unsigned int first_scalar_num,
233  const FEType & var_type,
234  const std::set<subdomain_id_type> & var_active_subdomains) :
235 
236  Variable (sys,
237  "var_group",
238  var_number,
239  first_scalar_num,
240  var_type,
241  var_active_subdomains),
242  _names(std::move(var_names))
243  {}
244 
248  VariableGroup (const VariableGroup &) = default;
249  VariableGroup & operator= (const VariableGroup &) = default;
250  VariableGroup (VariableGroup &&) = default;
251  VariableGroup & operator= (VariableGroup &&) = default;
252 
257  bool operator== ( const VariableGroup & other) const
258  {
259  return (this->Variable::operator==(other)) &&
260  (_names == other._names);
261  }
262 
266  unsigned int n_variables () const
267  { return cast_int<unsigned int>(_names.size()); }
268 
273  Variable variable (unsigned int v) const
274  {
275  libmesh_assert_less (v, this->n_variables());
276  return Variable (this->system(),
277  this->name(v),
278  this->number(v),
279  this->first_scalar_number(v),
280  this->type(),
281  this->active_subdomains());
282  }
283 
289  Variable operator() (unsigned int v) const
290  { return this->variable(v); }
291 
295  const std::string & name(unsigned int v) const
296  {
297  libmesh_assert_less (v, this->n_variables());
298  return _names[v];
299  }
300 
304  unsigned int number(unsigned int v) const
305  {
306  libmesh_assert_less (v, this->n_variables());
307  return _number + v;
308  }
309 
310  // Don't let number(uint) hide number()
311  using Variable::number;
312 
317  unsigned int first_scalar_number(unsigned int v) const
318  {
319  libmesh_assert_less (v, this->n_variables());
320  return _first_scalar_number+v;
321  }
322 
328  void append (std::string var_name)
329  { _names.push_back (std::move(var_name)); }
330 
331 protected:
332  std::vector<std::string> _names;
333 };
334 
335 } // namespace libMesh
336 
337 #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:229
VariableGroup & operator=(const VariableGroup &)=default
MeshBase & mesh
const std::string & name(unsigned int v) const
Definition: variable.h:295
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:211
This is the MeshBase class.
Definition: mesh_base.h:75
void append(std::string var_name)
Appends a variable to the group.
Definition: variable.h:328
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:266
bool operator==(const VariableGroup &other) const
Definition: variable.h:257
unsigned int n_components() const
Definition: variable.C:23
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
Variable & operator=(const Variable &)=default
std::vector< std::string > _names
Definition: variable.h:332
bool active_on_subdomain(subdomain_id_type sid) const
Definition: variable.h:167
Variable variable(unsigned int v) const
Definition: variable.h:273
This class defines a logically grouped set of variables in the system.
Definition: variable.h:203
Variable operator()(unsigned int v) const
Support vg(v).
Definition: variable.h:289
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:304
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:317
const FEType & type() const
Definition: variable.h:144