https://mooseframework.inl.gov
CoupledDiffusionReactionSub.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
11 
13 
14 registerMooseObject("ChemicalReactionsApp", CoupledDiffusionReactionSub);
15 
18 {
20  params.addParam<Real>(
21  "weight",
22  1.0,
23  "Weight of equilibrium species concentration in the primary species concentration");
24  params.addCoupledVar(
25  "log_k", 0.0, "Equilibrium constant of the equilbrium reaction in dissociation form");
26  params.addParam<Real>("sto_u",
27  1.0,
28  "Stoichiometric coef of the primary species this kernel "
29  "operates on in the equilibrium reaction");
30  params.addCoupledVar(
31  "gamma_u", 1.0, "Activity coefficient of primary species that this kernel operates on");
32  params.addParam<std::vector<Real>>(
33  "sto_v", {}, "The stoichiometric coefficients of coupled primary species");
34  params.addCoupledVar("v", "List of coupled primary species in this equilibrium species");
35  params.addCoupledVar("gamma_v", 1.0, "Activity coefficients of coupled primary species");
36  params.addCoupledVar("gamma_eq", 1.0, "Activity coefficient of this equilibrium species");
37  params.addClassDescription("Diffusion of equilibrium species");
38  return params;
39 }
40 
42  : Kernel(parameters),
43  _diffusivity(getMaterialProperty<Real>("diffusivity")),
44  _weight(getParam<Real>("weight")),
45  _log_k(coupledValue("log_k")),
46  _sto_u(getParam<Real>("sto_u")),
47  _sto_v(getParam<std::vector<Real>>("sto_v")),
48  _vars(coupledIndices("v")),
49  _vals(coupledValues("v")),
50  _grad_vals(coupledGradients("v")),
51  _gamma_u(coupledValue("gamma_u")),
52  _gamma_v(isCoupled("gamma_v")
53  ? coupledValues("gamma_v") // have value
54  : std::vector<const VariableValue *>(coupledComponents("v"),
55  &coupledValue("gamma_v"))), // default
56  _gamma_eq(coupledValue("gamma_eq"))
57 {
58  const unsigned int n = coupledComponents("v");
59 
60  // Check that the correct number of coupled values have been provided
61  if (_sto_v.size() != n)
62  mooseError("The number of stoichiometric coefficients in sto_v is not equal to the number of "
63  "coupled species in ",
64  _name);
65 
66  if (isCoupled("gamma_v"))
67  if (coupledComponents("gamma_v") != n)
68  mooseError("The number of activity coefficients in gamma_v is not equal to the number of "
69  "coupled species in ",
70  _name);
71 }
72 
73 Real
75 {
76  RealGradient diff1 =
78  for (unsigned int i = 0; i < _vals.size(); ++i)
79  diff1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
80 
81  RealGradient diff2_sum(0.0, 0.0, 0.0);
82  const Real d_val = std::pow(_gamma_u[_qp] * _u[_qp], _sto_u);
83  for (unsigned int i = 0; i < _vals.size(); ++i)
84  {
85  RealGradient diff2 = d_val * _sto_v[i] * (*_gamma_v[i])[_qp] *
86  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
87  (*_grad_vals[i])[_qp];
88 
89  for (unsigned int j = 0; j < _vals.size(); ++j)
90  if (j != i)
91  diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
92 
93  diff2_sum += diff2;
94  }
95 
96  mooseAssert(_gamma_eq[_qp] > 0.0, "Activity coefficient must be greater than zero");
97  return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
98  (diff1 + diff2_sum) / _gamma_eq[_qp];
99  ;
100 }
101 
102 Real
104 {
105  RealGradient diff1_1 =
107  RealGradient diff1_2 = _phi[_j][_qp] * _sto_u * (_sto_u - 1.0) * _gamma_u[_qp] * _gamma_u[_qp] *
108  std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 2.0) * _grad_u[_qp];
109  for (unsigned int i = 0; i < _vals.size(); ++i)
110  {
111  diff1_1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
112  diff1_2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
113  }
114 
115  RealGradient diff1 = diff1_1 + diff1_2;
116  Real d_val =
117  _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _phi[_j][_qp];
118  RealGradient diff2_sum(0.0, 0.0, 0.0);
119  for (unsigned int i = 0; i < _vals.size(); ++i)
120  {
121  RealGradient diff2 = d_val * _sto_v[i] * (*_gamma_v[i])[_qp] *
122  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
123  (*_grad_vals[i])[_qp];
124  for (unsigned int j = 0; j < _vals.size(); ++j)
125  if (j != i)
126  diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
127 
128  diff2_sum += diff2;
129  }
130 
131  return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
132  (diff1 + diff2_sum) / _gamma_eq[_qp];
133 }
134 
135 Real
137 {
138  // If no coupled species, return 0
139  if (_vals.size() == 0)
140  return 0.0;
141 
142  // If jvar is not one of the coupled species, return 0
143  if (std::find(_vars.begin(), _vars.end(), jvar) == _vars.end())
144  return 0.0;
145 
146  RealGradient diff1 =
147  _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _grad_u[_qp];
148  for (unsigned int i = 0; i < _vals.size(); ++i)
149  {
150  if (jvar == _vars[i])
151  diff1 *= _sto_v[i] * (*_gamma_v[i])[_qp] *
152  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) * _phi[_j][_qp];
153  else
154  diff1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
155  }
156 
157  Real val_u = std::pow(_gamma_u[_qp] * _u[_qp], _sto_u);
158 
159  RealGradient diff2_1(1.0, 1.0, 1.0);
160  RealGradient diff2_2(1.0, 1.0, 1.0);
161 
162  for (unsigned int i = 0; i < _vals.size(); ++i)
163  if (jvar == _vars[i])
164  {
165  diff2_1 = _sto_v[i] * (_sto_v[i] - 1.0) * (*_gamma_v[i])[_qp] * (*_gamma_v[i])[_qp] *
166  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 2.0) * _phi[_j][_qp] *
167  (*_grad_vals[i])[_qp];
168  diff2_2 = _sto_v[i] * (*_gamma_v[i])[_qp] *
169  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
170  _grad_phi[_j][_qp];
171  }
172 
173  RealGradient diff2 = val_u * (diff2_1 + diff2_2);
174 
175  for (unsigned int i = 0; i < _vals.size(); ++i)
176  if (jvar != _vars[i])
177  diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
178 
179  RealGradient diff3;
180  RealGradient diff3_sum(0.0, 0.0, 0.0);
181  Real val_jvar = 0.0;
182  unsigned int var = 0;
183 
184  for (unsigned int i = 0; i < _vals.size(); ++i)
185  if (jvar == _vars[i])
186  {
187  var = i;
188  val_jvar = val_u * _sto_v[i] * (*_gamma_v[i])[_qp] *
189  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) * _phi[_j][_qp];
190  }
191 
192  for (unsigned int i = 0; i < _vals.size(); ++i)
193  if (i != var)
194  {
195  diff3 = val_jvar * _sto_v[i] * (*_gamma_v[i])[_qp] *
196  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
197  (*_grad_vals[i])[_qp];
198 
199  for (unsigned int j = 0; j < _vals.size(); ++j)
200  if (j != var && j != i)
201  diff3 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
202 
203  diff3_sum += diff3;
204  }
205 
206  return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
207  (diff1 + diff2 + diff3_sum) / _gamma_eq[_qp];
208 }
static InputParameters validParams()
const std::vector< const VariableValue * > _gamma_v
Activity coefficients of coupled primary species in the equilibrium species.
virtual bool isCoupled(const std::string &var_name, unsigned int i=0) const
RealVectorValue RealGradient
const VariableGradient & _grad_u
static InputParameters validParams()
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
const VariableValue & _gamma_eq
Activity coefficient of equilibrium species.
const VariablePhiGradient & _grad_phi
const Real _weight
Weight of the equilibrium species concentration in the total primary species concentration.
CoupledDiffusionReactionSub(const InputParameters &parameters)
const VariableValue & _log_k
Equilibrium constant for the equilibrium species in association form.
virtual Real computeQpResidual() override
Diffusion of primary species in given equilibrium species.
const std::vector< const VariableGradient * > _grad_vals
Coupled gradients of primary species concentrations.
const VariableValue & _gamma_u
Activity coefficient of primary species in the equilibrium species.
virtual Real computeQpJacobian() override
const std::vector< Real > _sto_v
Stoichiometric coefficients of the coupled primary species.
const std::vector< const VariableValue * > _vals
Coupled primary species concentrations.
const Real _sto_u
Stoichiometric coefficient of the primary species.
unsigned int _i
const std::string _name
const std::vector< unsigned int > _vars
Coupled primary species variable numbers.
void addCoupledVar(const std::string &name, const std::string &doc_string)
OutputTools< Real >::VariableValue VariableValue
unsigned int _j
unsigned int coupledComponents(const std::string &var_name) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
registerMooseObject("ChemicalReactionsApp", CoupledDiffusionReactionSub)
const VariableTestGradient & _grad_test
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
const MaterialProperty< Real > & _diffusivity
Material property of dispersion-diffusion coefficient.
const VariablePhiValue & _phi
MooseUnits pow(const MooseUnits &, int)
virtual Real computeQpOffDiagJacobian(unsigned int jvar) override
const VariableValue & _u
unsigned int _qp