www.mooseframework.org
CoupledDiffusionReactionSub.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
12 registerMooseObject("ChemicalReactionsApp", CoupledDiffusionReactionSub);
13 
16 {
18  params.addParam<Real>(
19  "weight",
20  1.0,
21  "Weight of equilibrium species concentration in the primary species concentration");
22  params.addCoupledVar(
23  "log_k", 0.0, "Equilibrium constant of the equilbrium reaction in dissociation form");
24  params.addParam<Real>("sto_u",
25  1.0,
26  "Stoichiometric coef of the primary species this kernel "
27  "operates on in the equilibrium reaction");
28  params.addCoupledVar(
29  "gamma_u", 1.0, "Activity coefficient of primary species that this kernel operates on");
30  params.addParam<std::vector<Real>>(
31  "sto_v", {}, "The stoichiometric coefficients of coupled primary species");
32  params.addCoupledVar("v", "List of coupled primary species in this equilibrium species");
33  params.addCoupledVar("gamma_v", 1.0, "Activity coefficients of coupled primary species");
34  params.addCoupledVar("gamma_eq", 1.0, "Activity coefficient of this equilibrium species");
35  params.addClassDescription("Diffusion of equilibrium species");
36  return params;
37 }
38 
40  : Kernel(parameters),
41  _diffusivity(getMaterialProperty<Real>("diffusivity")),
42  _weight(getParam<Real>("weight")),
43  _log_k(coupledValue("log_k")),
44  _sto_u(getParam<Real>("sto_u")),
45  _sto_v(getParam<std::vector<Real>>("sto_v")),
46  _vars(coupledIndices("v")),
47  _vals(coupledValues("v")),
48  _grad_vals(coupledGradients("v")),
49  _gamma_u(coupledValue("gamma_u")),
50  _gamma_v(isCoupled("gamma_v")
51  ? coupledValues("gamma_v") // have value
52  : std::vector<const VariableValue *>(coupledComponents("v"),
53  &coupledValue("gamma_v"))), // default
54  _gamma_eq(coupledValue("gamma_eq"))
55 {
56  const unsigned int n = coupledComponents("v");
57 
58  // Check that the correct number of coupled values have been provided
59  if (_sto_v.size() != n)
60  mooseError("The number of stoichiometric coefficients in sto_v is not equal to the number of "
61  "coupled species in ",
62  _name);
63 
64  if (isCoupled("gamma_v"))
65  if (coupledComponents("gamma_v") != n)
66  mooseError("The number of activity coefficients in gamma_v is not equal to the number of "
67  "coupled species in ",
68  _name);
69 }
70 
71 Real
73 {
74  RealGradient diff1 =
76  for (unsigned int i = 0; i < _vals.size(); ++i)
77  diff1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
78 
79  RealGradient diff2_sum(0.0, 0.0, 0.0);
80  const Real d_val = std::pow(_gamma_u[_qp] * _u[_qp], _sto_u);
81  for (unsigned int i = 0; i < _vals.size(); ++i)
82  {
83  RealGradient diff2 = d_val * _sto_v[i] * (*_gamma_v[i])[_qp] *
84  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
85  (*_grad_vals[i])[_qp];
86 
87  for (unsigned int j = 0; j < _vals.size(); ++j)
88  if (j != i)
89  diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
90 
91  diff2_sum += diff2;
92  }
93 
94  mooseAssert(_gamma_eq[_qp] > 0.0, "Activity coefficient must be greater than zero");
95  return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
96  (diff1 + diff2_sum) / _gamma_eq[_qp];
97  ;
98 }
99 
100 Real
102 {
103  RealGradient diff1_1 =
105  RealGradient diff1_2 = _phi[_j][_qp] * _sto_u * (_sto_u - 1.0) * _gamma_u[_qp] * _gamma_u[_qp] *
106  std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 2.0) * _grad_u[_qp];
107  for (unsigned int i = 0; i < _vals.size(); ++i)
108  {
109  diff1_1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
110  diff1_2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
111  }
112 
113  RealGradient diff1 = diff1_1 + diff1_2;
114  Real d_val =
115  _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _phi[_j][_qp];
116  RealGradient diff2_sum(0.0, 0.0, 0.0);
117  for (unsigned int i = 0; i < _vals.size(); ++i)
118  {
119  RealGradient diff2 = d_val * _sto_v[i] * (*_gamma_v[i])[_qp] *
120  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
121  (*_grad_vals[i])[_qp];
122  for (unsigned int j = 0; j < _vals.size(); ++j)
123  if (j != i)
124  diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
125 
126  diff2_sum += diff2;
127  }
128 
129  return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
130  (diff1 + diff2_sum) / _gamma_eq[_qp];
131 }
132 
133 Real
135 {
136  // If no coupled species, return 0
137  if (_vals.size() == 0)
138  return 0.0;
139 
140  // If jvar is not one of the coupled species, return 0
141  if (std::find(_vars.begin(), _vars.end(), jvar) == _vars.end())
142  return 0.0;
143 
144  RealGradient diff1 =
145  _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _grad_u[_qp];
146  for (unsigned int i = 0; i < _vals.size(); ++i)
147  {
148  if (jvar == _vars[i])
149  diff1 *= _sto_v[i] * (*_gamma_v[i])[_qp] *
150  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) * _phi[_j][_qp];
151  else
152  diff1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
153  }
154 
155  Real val_u = std::pow(_gamma_u[_qp] * _u[_qp], _sto_u);
156 
157  RealGradient diff2_1(1.0, 1.0, 1.0);
158  RealGradient diff2_2(1.0, 1.0, 1.0);
159 
160  for (unsigned int i = 0; i < _vals.size(); ++i)
161  if (jvar == _vars[i])
162  {
163  diff2_1 = _sto_v[i] * (_sto_v[i] - 1.0) * (*_gamma_v[i])[_qp] * (*_gamma_v[i])[_qp] *
164  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 2.0) * _phi[_j][_qp] *
165  (*_grad_vals[i])[_qp];
166  diff2_2 = _sto_v[i] * (*_gamma_v[i])[_qp] *
167  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
168  _grad_phi[_j][_qp];
169  }
170 
171  RealGradient diff2 = val_u * (diff2_1 + diff2_2);
172 
173  for (unsigned int i = 0; i < _vals.size(); ++i)
174  if (jvar != _vars[i])
175  diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
176 
177  RealGradient diff3;
178  RealGradient diff3_sum(0.0, 0.0, 0.0);
179  Real val_jvar = 0.0;
180  unsigned int var = 0;
181 
182  for (unsigned int i = 0; i < _vals.size(); ++i)
183  if (jvar == _vars[i])
184  {
185  var = i;
186  val_jvar = val_u * _sto_v[i] * (*_gamma_v[i])[_qp] *
187  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) * _phi[_j][_qp];
188  }
189 
190  for (unsigned int i = 0; i < _vals.size(); ++i)
191  if (i != var)
192  {
193  diff3 = val_jvar * _sto_v[i] * (*_gamma_v[i])[_qp] *
194  std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
195  (*_grad_vals[i])[_qp];
196 
197  for (unsigned int j = 0; j < _vals.size(); ++j)
198  if (j != var && j != i)
199  diff3 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
200 
201  diff3_sum += diff3;
202  }
203 
204  return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
205  (diff1 + diff2 + diff3_sum) / _gamma_eq[_qp];
206 }
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
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