www.mooseframework.org
MooseVariableScalar.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "MooseVariableScalar.h"
16 #include "SubProblem.h"
17 #include "SystemBase.h"
18 #include "Assembly.h"
19 #include "SystemBase.h"
20 #include "MooseVariable.h"
21 
22 // libMesh
23 #include "libmesh/numeric_vector.h"
24 #include "libmesh/dof_map.h"
25 
26 #include <limits>
27 
29  const FEType & fe_type,
30  SystemBase & sys,
31  Assembly & assembly,
32  Moose::VarKindType var_kind)
33  : MooseVariableBase(var_num, fe_type, sys, assembly, var_kind)
34 {
35 }
36 
38 {
39  _u.release();
40  _u_old.release();
41  _u_older.release();
42 
43  _u_dot.release();
45 }
46 
47 void
49 {
50  const NumericVector<Real> & current_solution = *_sys.currentSolution();
51  const NumericVector<Real> & solution_old = _sys.solutionOld();
52  const NumericVector<Real> & solution_older = _sys.solutionOlder();
53  const NumericVector<Real> & u_dot = _sys.solutionUDot();
54  const Real & du_dot_du = _sys.duDotDu();
55 
56  _dof_map.SCALAR_dof_indices(_dof_indices, _var_num);
57 
58  unsigned int n = _dof_indices.size();
59  _u.resize(n);
60  _u_old.resize(n);
61  _u_older.resize(n);
62  _u_dot.resize(n);
63 
64  _du_dot_du.clear();
65  _du_dot_du.resize(n, du_dot_du);
66 
67  // If we have an empty partition, or if we have a partition which
68  // does not include any of the subdomains of a subdomain-restricted
69  // variable, then we do not have access to that variable! Hopefully
70  // we won't need the indices we lack.
71  if (_dof_map.all_semilocal_indices(_dof_indices))
72  {
73  current_solution.get(_dof_indices, &_u[0]);
74  solution_old.get(_dof_indices, &_u_old[0]);
75  solution_older.get(_dof_indices, &_u_older[0]);
76  u_dot.get(_dof_indices, &_u_dot[0]);
77  }
78  else
79  {
80  for (std::size_t i = 0; i != n; ++i)
81  {
82  const dof_id_type dof_index = _dof_indices[i];
83  std::vector<dof_id_type> one_dof_index(1, dof_index);
84  if (_dof_map.all_semilocal_indices(one_dof_index))
85  {
86  libmesh_assert_less(i, _u.size());
87 
88  current_solution.get(one_dof_index, &_u[i]);
89  solution_old.get(one_dof_index, &_u_old[i]);
90  solution_older.get(one_dof_index, &_u_older[i]);
91  u_dot.get(one_dof_index, &_u_dot[i]);
92  }
93  else
94  {
95 #ifdef _GLIBCXX_DEBUG
96  // Let's make it possible to catch invalid accesses to these
97  // variables immediately via a thrown exception, if our
98  // libstdc++ compiler flags allow for that.
99  _u.resize(i);
100  _u_old.resize(i);
101  _u_older.resize(i);
102  _u_dot.resize(i);
103 #else
104  // If we can't catch errors at run-time, we can at least
105  // propagate NaN values rather than invalid values, so that
106  // users won't trust the result.
107  _u[i] = std::numeric_limits<Real>::quiet_NaN();
108  _u_old[i] = std::numeric_limits<Real>::quiet_NaN();
109  _u_older[i] = std::numeric_limits<Real>::quiet_NaN();
110  _u_dot[i] = std::numeric_limits<Real>::quiet_NaN();
111 #endif
112  }
113  }
114  }
115 }
116 
117 bool
119 {
120  // scalar variables are never nodal ;-)
121  return false;
122 }
123 
124 void
125 MooseVariableScalar::setValue(unsigned int i, Number value)
126 {
127 // In debug modes, we might have set a "trap" to catch reads of
128 // uninitialized values, but this trap shouldn't prevent setting
129 // values.
130 #ifdef DEBUG
131  if (i >= _u.size())
132  {
133  libmesh_assert_less(i, _dof_indices.size());
134  _u.resize(i + 1);
135  }
136 #endif
137  _u[i] = value; // update variable value
138 }
139 
140 void
142 {
143  unsigned int n = _dof_indices.size();
144 // In debug modes, we might have set a "trap" to catch reads of
145 // uninitialized values, but this trap shouldn't prevent setting
146 // values.
147 #ifdef DEBUG
148  _u.resize(n);
149 #endif
150  for (unsigned int i = 0; i < n; i++)
151  _u[i] = value;
152 }
153 
154 void
155 MooseVariableScalar::insert(NumericVector<Number> & soln)
156 {
157  // We may have redundantly computed this value on many different
158  // processors, but only the processor which actually owns it should
159  // be saving it to the solution vector, to avoid O(N_scalar_vars)
160  // unnecessary communication.
161 
162  const dof_id_type first_dof = _dof_map.first_dof();
163  const dof_id_type end_dof = _dof_map.end_dof();
164  if (_dof_indices.size() > 0 && first_dof <= _dof_indices[0] && _dof_indices[0] < end_dof)
165  soln.insert(&_u[0], _dof_indices);
166 }
MooseVariableScalar(unsigned int var_num, const FEType &fe_type, SystemBase &sys, Assembly &assembly, Moose::VarKindType var_kind)
Keeps track of stuff related to assembling.
Definition: Assembly.h:63
const DofMap & _dof_map
DOF map.
void resize(const unsigned int size)
Change the number of elements the array can store.
Definition: MooseArray.h:205
virtual NumericVector< Number > & solutionOld()=0
VariableValue _u
The value of scalar variable.
VariableValue _u_older
The older value of scalar variable.
Base class for a system (of equations)
Definition: SystemBase.h:91
std::vector< dof_id_type > _dof_indices
DOF indices.
virtual Number & duDotDu()
Definition: SystemBase.h:156
virtual bool isNodal() const override
Is this variable nodal.
virtual NumericVector< Number > & solutionUDot()
Definition: SystemBase.h:157
SystemBase & _sys
System this variable is part of.
VarKindType
Framework-wide stuff.
Definition: MooseTypes.h:156
void setValue(unsigned int i, Number value)
Set the nodal value for this variable (to keep everything up to date.
void setValues(Number value)
Set all of the values of this scalar variable to the same value.
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition: MooseArray.h:250
PetscInt n
void release()
Manually deallocates the data pointer.
Definition: MooseArray.h:56
unsigned int _var_num
variable number (from libMesh)
virtual NumericVector< Number > & solutionOlder()=0
void insert(NumericVector< Number > &soln)
void clear()
Change the number of elements the array can store to zero.
Definition: MooseArray.h:198
VariableValue _u_old
The old value of scalar variable.
virtual const NumericVector< Number > *& currentSolution()=0
The solution vector that is currently being operated on.