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
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 equilibrium 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
71Real
73{
74 RealGradient diff1 =
75 _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _grad_u[_qp];
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
99Real
101{
102 RealGradient diff1_1 =
103 _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _grad_phi[_j][_qp];
104 RealGradient diff1_2 = _phi[_j][_qp] * _sto_u * (_sto_u - 1.0) * _gamma_u[_qp] * _gamma_u[_qp] *
105 std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 2.0) * _grad_u[_qp];
106 for (unsigned int i = 0; i < _vals.size(); ++i)
107 {
108 diff1_1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
109 diff1_2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
110 }
111
112 RealGradient diff1 = diff1_1 + diff1_2;
113 Real d_val =
114 _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _phi[_j][_qp];
115 RealGradient diff2_sum(0.0, 0.0, 0.0);
116 for (unsigned int i = 0; i < _vals.size(); ++i)
117 {
118 RealGradient diff2 = d_val * _sto_v[i] * (*_gamma_v[i])[_qp] *
119 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
120 (*_grad_vals[i])[_qp];
121 for (unsigned int j = 0; j < _vals.size(); ++j)
122 if (j != i)
123 diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
124
125 diff2_sum += diff2;
126 }
127
128 return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
129 (diff1 + diff2_sum) / _gamma_eq[_qp];
130}
131
132Real
134{
135 // If no coupled species, return 0
136 if (_vals.size() == 0)
137 return 0.0;
138
139 // If jvar is not one of the coupled species, return 0
140 if (std::find(_vars.begin(), _vars.end(), jvar) == _vars.end())
141 return 0.0;
142
143 RealGradient diff1 =
144 _sto_u * _gamma_u[_qp] * std::pow(_gamma_u[_qp] * _u[_qp], _sto_u - 1.0) * _grad_u[_qp];
145 for (unsigned int i = 0; i < _vals.size(); ++i)
146 {
147 if (jvar == _vars[i])
148 diff1 *= _sto_v[i] * (*_gamma_v[i])[_qp] *
149 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) * _phi[_j][_qp];
150 else
151 diff1 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
152 }
153
154 Real val_u = std::pow(_gamma_u[_qp] * _u[_qp], _sto_u);
155
156 RealGradient diff2_1(1.0, 1.0, 1.0);
157 RealGradient diff2_2(1.0, 1.0, 1.0);
158
159 for (unsigned int i = 0; i < _vals.size(); ++i)
160 if (jvar == _vars[i])
161 {
162 diff2_1 = _sto_v[i] * (_sto_v[i] - 1.0) * (*_gamma_v[i])[_qp] * (*_gamma_v[i])[_qp] *
163 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 2.0) * _phi[_j][_qp] *
164 (*_grad_vals[i])[_qp];
165 diff2_2 = _sto_v[i] * (*_gamma_v[i])[_qp] *
166 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
167 _grad_phi[_j][_qp];
168 }
169
170 RealGradient diff2 = val_u * (diff2_1 + diff2_2);
171
172 for (unsigned int i = 0; i < _vals.size(); ++i)
173 if (jvar != _vars[i])
174 diff2 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i]);
175
176 RealGradient diff3;
177 RealGradient diff3_sum(0.0, 0.0, 0.0);
178 Real val_jvar = 0.0;
179 unsigned int var = 0;
180
181 for (unsigned int i = 0; i < _vals.size(); ++i)
182 if (jvar == _vars[i])
183 {
184 var = i;
185 val_jvar = val_u * _sto_v[i] * (*_gamma_v[i])[_qp] *
186 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) * _phi[_j][_qp];
187 }
188
189 for (unsigned int i = 0; i < _vals.size(); ++i)
190 if (i != var)
191 {
192 diff3 = val_jvar * _sto_v[i] * (*_gamma_v[i])[_qp] *
193 std::pow((*_gamma_v[i])[_qp] * (*_vals[i])[_qp], _sto_v[i] - 1.0) *
194 (*_grad_vals[i])[_qp];
195
196 for (unsigned int j = 0; j < _vals.size(); ++j)
197 if (j != var && j != i)
198 diff3 *= std::pow((*_gamma_v[i])[_qp] * (*_vals[j])[_qp], _sto_v[j]);
199
200 diff3_sum += diff3;
201 }
202
203 return _weight * std::pow(10.0, _log_k[_qp]) * _diffusivity[_qp] * _grad_test[_i][_qp] *
204 (diff1 + diff2 + diff3_sum) / _gamma_eq[_qp];
205}
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