https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
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 equilibrium 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
73Real
75{
76 RealGradient diff1 =
77 _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _grad_u[_qp];
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
101Real
103{
104 RealGradient diff1_1 =
105 _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _grad_phi[_j][_qp];
106 RealGradient diff1_2 = _phi[_j][_qp] * _sto_u * (_sto_u - 1.0) * _gamma_u[_qp] * _gamma_u[_qp] *
107 std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 2.0) * _grad_u[_qp];
108 for (unsigned int i = 0; i < _vals.size(); ++i)
109 {
110 diff1_1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
111 diff1_2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
112 }
113
114 RealGradient diff1 = diff1_1 + diff1_2;
115 Real d_val =
116 _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _phi[_j][_qp];
117 RealGradient diff2_sum(0.0, 0.0, 0.0);
118 for (unsigned int i = 0; i < _vals.size(); ++i)
119 {
120 RealGradient diff2 = d_val * _sto_v[i] * (*_gamma_v[i])[_qp] *
121 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
122 (*_grad_vals[i])[_qp];
123 for (unsigned int j = 0; j < _vals.size(); ++j)
124 if (j != i)
125 diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
126
127 diff2_sum += diff2;
128 }
129
130 return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
131 (diff1 + diff2_sum) / _gamma_eq[_qp];
132}
133
134Real
136{
137 // If no coupled species, return 0
138 if (_vals.size() == 0)
139 return 0.0;
140
141 // If jvar is not one of the coupled species, return 0
142 if (std::find(_vars.begin(), _vars.end(), jvar) == _vars.end())
143 return 0.0;
144
145 RealGradient diff1 =
146 _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _grad_u[_qp];
147 for (unsigned int i = 0; i < _vals.size(); ++i)
148 {
149 if (jvar == _vars[i])
150 diff1 *= _sto_v[i] * (*_gamma_v[i])[_qp] *
151 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) * _phi[_j][_qp];
152 else
153 diff1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
154 }
155
156 Real val_u = std::pow(_gamma_u[_qp] * _u[_qp], _sto_u);
157
158 RealGradient diff2_1(1.0, 1.0, 1.0);
159 RealGradient diff2_2(1.0, 1.0, 1.0);
160
161 for (unsigned int i = 0; i < _vals.size(); ++i)
162 if (jvar == _vars[i])
163 {
164 diff2_1 = _sto_v[i] * (_sto_v[i] - 1.0) * (*_gamma_v[i])[_qp] * (*_gamma_v[i])[_qp] *
165 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 2.0) * _phi[_j][_qp] *
166 (*_grad_vals[i])[_qp];
167 diff2_2 = _sto_v[i] * (*_gamma_v[i])[_qp] *
168 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
169 _grad_phi[_j][_qp];
170 }
171
172 RealGradient diff2 = val_u * (diff2_1 + diff2_2);
173
174 for (unsigned int i = 0; i < _vals.size(); ++i)
175 if (jvar != _vars[i])
176 diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
177
178 RealGradient diff3;
179 RealGradient diff3_sum(0.0, 0.0, 0.0);
180 Real val_jvar = 0.0;
181 unsigned int var = 0;
182
183 for (unsigned int i = 0; i < _vals.size(); ++i)
184 if (jvar == _vars[i])
185 {
186 var = i;
187 val_jvar = val_u * _sto_v[i] * (*_gamma_v[i])[_qp] *
188 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) * _phi[_j][_qp];
189 }
190
191 for (unsigned int i = 0; i < _vals.size(); ++i)
192 if (i != var)
193 {
194 diff3 = val_jvar * _sto_v[i] * (*_gamma_v[i])[_qp] *
195 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
196 (*_grad_vals[i])[_qp];
197
198 for (unsigned int j = 0; j < _vals.size(); ++j)
199 if (j != var && j != i)
200 diff3 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
201
202 diff3_sum += diff3;
203 }
204
205 return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
206 (diff1 + diff2 + diff3_sum) / _gamma_eq[_qp];
207}
registerMooseObject("ChemicalReactionsApp", CoupledDiffusionReactionSub)
unsigned int coupledComponents(const std::string &var_name) const
virtual bool isCoupled(const std::string &var_name, unsigned int i=0) const
Diffusion of primary species in given equilibrium species.
const VariableValue & _gamma_eq
Activity coefficient of equilibrium species.
const std::vector< Real > _sto_v
Stoichiometric coefficients of the coupled primary species.
const VariableValue & _log_k
Equilibrium constant for the equilibrium species in association form.
const Real _sto_u
Stoichiometric coefficient of the primary species.
const VariableValue & _gamma_u
Activity coefficient of primary species in the equilibrium species.
const std::vector< const VariableValue * > _vals
Coupled primary species concentrations.
virtual Real computeQpOffDiagJacobian(unsigned int jvar) override
const MaterialProperty< Real > & _diffusivity
Material property of dispersion-diffusion coefficient.
const Real _weight
Weight of the equilibrium species concentration in the total primary species concentration.
const std::vector< const VariableValue * > _gamma_v
Activity coefficients of coupled primary species in the equilibrium species.
virtual Real computeQpResidual() override
const std::vector< const VariableGradient * > _grad_vals
Coupled gradients of primary species concentrations.
const std::vector< unsigned int > _vars
Coupled primary species variable numbers.
CoupledDiffusionReactionSub(const InputParameters &parameters)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void addCoupledVar(const std::string &name, const std::string &doc_string)
unsigned int _qp
unsigned int _j
unsigned int _i
const VariableGradient & _grad_u
const VariablePhiValue & _phi
static InputParameters validParams()
const VariablePhiGradient & _grad_phi
const VariableTestGradient & _grad_test
const VariableValue & _u
void mooseError(Args &&... args) const
const std::string & _name
VariableValueTempl< false > VariableValue