https://mooseframework.inl.gov
Loading...
Searching...
No Matches
PorousFlowAqueousPreDisChemistry.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{
19 "primary_concentrations",
20 "List of MOOSE Variables that represent the concentrations of the primary species");
21 params.addRequiredParam<unsigned>("num_reactions",
22 "Number of equations in the system of chemical reactions");
23 params.addParam<bool>("equilibrium_constants_as_log10",
24 false,
25 "If true, the equilibrium constants are written in their log10 form, eg, "
26 "-2. If false, the equilibrium constants are written in absolute terms, "
27 "eg, 0.01");
28 params.addRequiredCoupledVar("equilibrium_constants",
29 "Equilibrium constant for each equation (dimensionless). If these "
30 "are temperature dependent AuxVariables, the Jacobian will not be "
31 "exact");
32 params.addRequiredParam<std::vector<Real>>(
33 "primary_activity_coefficients",
34 "Activity coefficients for the primary species (dimensionless) (one for each)");
35 params.addRequiredParam<std::vector<Real>>(
36 "reactions",
37 "A matrix defining the aqueous reactions. The matrix is entered as a long vector: the first "
38 "row is "
39 "entered first, followed by the second row, etc. There should be num_reactions rows. All "
40 "primary species should appear only on the LHS of each reaction (and there should be just "
41 "one secondary species on the RHS, by definition) so they may have negative coefficients. "
42 "Each row should have number of primary_concentrations entries, which are the stoichiometric "
43 "coefficients. The first coefficient must always correspond to the first primary species, "
44 "etc");
45 params.addRequiredParam<std::vector<Real>>("specific_reactive_surface_area",
46 "Specific reactive surface area in m^2/(L solution).");
47 params.addRequiredParam<std::vector<Real>>(
48 "kinetic_rate_constant",
49 "Kinetic rate constant in mol/(m^2 s), at the reference temperature (one for each reaction)");
50 params.addRequiredParam<std::vector<Real>>("molar_volume",
51 "Volume occupied by one mole of the secondary species "
52 "(L(solution)/mol) (one for each reaction)");
53 params.addRequiredParam<std::vector<Real>>("activation_energy",
54 "Activation energy, J/mol (one for each reaction)");
55 params.addParam<Real>("gas_constant", 8.31434, "Gas constant, in J/(mol K)");
56 params.addParam<Real>("reference_temperature", 298.15, "Reference temperature, K");
57 params.addParam<std::vector<Real>>("theta_exponent",
58 "Theta exponent. Defaults to 1. (one for each reaction)");
59 params.addParam<std::vector<Real>>("eta_exponent",
60 "Eta exponent. Defaults to 1. (one for each reaction)");
61 params.addPrivateParam<std::string>("pf_material_type", "chemistry");
62 params.addClassDescription("This Material forms a std::vector of mineralisation reaction rates "
63 "(L(precipitate)/L(solution)/s) appropriate to the aqueous "
64 "precipitation-dissolution system provided. Note: the "
65 "PorousFlowTemperature must be measured in Kelvin.");
66 return params;
67}
68
70 const InputParameters & parameters)
71 : PorousFlowMaterialVectorBase(parameters),
72 _porosity_old(_nodal_material ? getMaterialPropertyOld<Real>("PorousFlow_porosity_nodal")
73 : getMaterialPropertyOld<Real>("PorousFlow_porosity_qp")),
74 _aq_ph(_dictator.aqueousPhaseNumber()),
75 _saturation(_nodal_material
76 ? getMaterialProperty<std::vector<Real>>("PorousFlow_saturation_nodal")
77 : getMaterialProperty<std::vector<Real>>("PorousFlow_saturation_qp")),
78
79 _temperature(_nodal_material ? getMaterialProperty<Real>("PorousFlow_temperature_nodal")
80 : getMaterialProperty<Real>("PorousFlow_temperature_qp")),
81 _dtemperature_dvar(
82 _nodal_material
83 ? getMaterialProperty<std::vector<Real>>("dPorousFlow_temperature_nodal_dvar")
84 : getMaterialProperty<std::vector<Real>>("dPorousFlow_temperature_qp_dvar")),
85
86 _num_primary(coupledComponents("primary_concentrations")),
87 _num_reactions(getParam<unsigned>("num_reactions")),
88 _equilibrium_constants_as_log10(getParam<bool>("equilibrium_constants_as_log10")),
89 _num_equilibrium_constants(coupledComponents("equilibrium_constants")),
90 _equilibrium_constants(_num_equilibrium_constants),
91 _primary_activity_coefficients(getParam<std::vector<Real>>("primary_activity_coefficients")),
92 _reactions(getParam<std::vector<Real>>("reactions")),
93
94 _sec_conc_old(
95 _nodal_material
96 ? getMaterialPropertyOld<std::vector<Real>>("PorousFlow_mineral_concentration_nodal")
97 : getMaterialPropertyOld<std::vector<Real>>("PorousFlow_mineral_concentration_qp")),
98
99 _mineral_sat(_num_reactions),
100 _bounded_rate(_num_reactions),
101 _reaction_rate(
102 _nodal_material
103 ? declareProperty<std::vector<Real>>("PorousFlow_mineral_reaction_rate_nodal")
104 : declareProperty<std::vector<Real>>("PorousFlow_mineral_reaction_rate_qp")),
105 _dreaction_rate_dvar(_nodal_material ? declareProperty<std::vector<std::vector<Real>>>(
106 "dPorousFlow_mineral_reaction_rate_nodal_dvar")
107 : declareProperty<std::vector<std::vector<Real>>>(
108 "dPorousFlow_mineral_reaction_rate_qp_dvar")),
109
110 _r_area(getParam<std::vector<Real>>("specific_reactive_surface_area")),
111 _molar_volume(getParam<std::vector<Real>>("molar_volume")),
112 _ref_kconst(getParam<std::vector<Real>>("kinetic_rate_constant")),
113 _e_act(getParam<std::vector<Real>>("activation_energy")),
114 _gas_const(getParam<Real>("gas_constant")),
115 _one_over_ref_temp(1.0 / getParam<Real>("reference_temperature")),
116 _theta_exponent(isParamValid("theta_exponent") ? getParam<std::vector<Real>>("theta_exponent")
117 : std::vector<Real>(_num_reactions, 1.0)),
118 _eta_exponent(isParamValid("eta_exponent") ? getParam<std::vector<Real>>("eta_exponent")
119 : std::vector<Real>(_num_reactions, 1.0))
120{
121 if (_dictator.numPhases() < 1)
122 mooseError("PorousFlowAqueousPreDisChemistry: The number of fluid phases must not be zero");
123
124 if (_num_primary != _num_components - 1)
125 mooseError("PorousFlowAqueousPreDisChemistry: The number of mass_fraction_vars is ",
127 " which must be one greater than the number of primary concentrations (which is ",
129 ")");
130
131 // correct number of equilibrium constants
133 mooseError("PorousFlowAqueousPreDisChemistry: The number of equilibrium constants is ",
135 " which must be equal to the number of reactions (",
137 ")");
138
139 // correct number of activity coefficients
141 mooseError("PorousFlowAqueousPreDisChemistry: The number of primary activity "
142 "coefficients is ",
144 " which must be equal to the number of primary species (",
146 ")");
147
148 // correct number of stoichiometry coefficients
149 if (_reactions.size() != _num_reactions * _num_primary)
150 mooseError("PorousFlowAqueousPreDisChemistry: The number of stoichiometric "
151 "coefficients specified in 'reactions' (",
152 _reactions.size(),
153 ") must be equal to the number of reactions (",
155 ") multiplied by the number of primary species (",
157 ")");
158
159 if (_r_area.size() != _num_reactions)
160 mooseError("PorousFlowAqueousPreDisChemistry: The number of specific reactive "
161 "surface areas provided is ",
162 _r_area.size(),
163 " which must be equal to the number of reactions (",
165 ")");
166
167 if (_ref_kconst.size() != _num_reactions)
168 mooseError("PorousFlowAqueousPreDisChemistry: The number of kinetic rate constants is ",
169 _ref_kconst.size(),
170 " which must be equal to the number of reactions (",
172 ")");
173
174 if (_e_act.size() != _num_reactions)
175 mooseError("PorousFlowAqueousPreDisChemistry: The number of activation energies is ",
176 _e_act.size(),
177 " which must be equal to the number of reactions (",
179 ")");
180
181 if (_molar_volume.size() != _num_reactions)
182 mooseError("PorousFlowAqueousPreDisChemistry: The number of molar volumes is ",
183 _molar_volume.size(),
184 " which must be equal to the number of reactions (",
186 ")");
187
188 if (_theta_exponent.size() != _num_reactions)
189 mooseError("PorousFlowAqueousPreDisChemistry: The number of theta exponents is ",
190 _theta_exponent.size(),
191 " which must be equal to the number of reactions (",
193 ")");
194
195 if (_eta_exponent.size() != _num_reactions)
196 mooseError("PorousFlowAqueousPreDisChemistry: The number of eta exponents is ",
197 _eta_exponent.size(),
198 " which must be equal to the number of reactions (",
200 ")");
201
202 if (_num_reactions != _dictator.numAqueousKinetic())
203 mooseError("PorousFlowAqueousPreDisChemistry: You have specified the number of "
204 "reactions to be ",
206 " but the Dictator knows that the number of aqueous kinetic "
207 "(precipitation-dissolution) reactions is ",
208 _dictator.numAqueousKinetic());
209
211 _primary.resize(_num_primary);
212 for (unsigned i = 0; i < _num_primary; ++i)
213 {
214 _primary_var_num[i] = coupled("primary_concentrations", i);
215 // A primary concentration need not be a PorousFlow variable (the derivative loop below skips
216 // any that is not), and a prescribed one may be an elemental AuxVariable, which has no nodal
217 // value to read. This matches the treatment of the mass fractions, which are the same kind of
218 // quantity, in PorousFlowMassFraction.
219 _primary[i] = &nodalOrQpValue("primary_concentrations", i);
220 }
221
222 for (unsigned i = 0; i < _num_equilibrium_constants; ++i)
223 {
224 // If equilibrium_constants are elemental AuxVariables (or constants), we want to use
225 // coupledGenericValue() rather than coupledGenericDofValue()
226 const bool is_nodal = isCoupled("equilibrium_constants")
227 ? getFieldVar("equilibrium_constants", i)->isNodal()
228 : false;
229
231 (_nodal_material && is_nodal ? &coupledDofValues("equilibrium_constants", i)
232 : &coupledValue("equilibrium_constants", i));
233 }
234}
235
236void
244
245void
247{
250 for (unsigned r = 0; r < _num_reactions; ++r)
251 _dreaction_rate_dvar[_qp][r].assign(_num_var, 0.0);
252
253 // Compute the reaction rates
255
256 // Compute the derivatives of the reaction rates
257 std::vector<std::vector<Real>> drr(_num_reactions);
258 std::vector<Real> drr_dT(_num_reactions);
259 for (unsigned r = 0; r < _num_reactions; ++r)
260 {
261 dQpReactionRate_dprimary(r, drr[r]);
262 drr_dT[r] = dQpReactionRate_dT(r);
263 }
264
265 // compute _dreaction_rate_dvar[_qp]
266 for (unsigned wrt = 0; wrt < _num_primary; ++wrt)
267 {
268 // derivative with respect to the "wrt"^th primary species concentration
269 if (!_dictator.isPorousFlowVariable(_primary_var_num[wrt]))
270 continue;
271 const unsigned pf_wrt = _dictator.porousFlowVariableNum(_primary_var_num[wrt]);
272
273 // run through the reactions, using drr in the appropriate places
274 for (unsigned r = 0; r < _num_reactions; ++r)
275 _dreaction_rate_dvar[_qp][r][pf_wrt] = drr[r][wrt];
276 }
277
278 // use the derivative wrt temperature
279 for (unsigned r = 0; r < _num_reactions; ++r)
280 for (unsigned v = 0; v < _num_var; ++v)
281 _dreaction_rate_dvar[_qp][r][v] += drr_dT[r] * _dtemperature_dvar[_qp][v];
282}
283
284Real
285PorousFlowAqueousPreDisChemistry::stoichiometry(unsigned reaction_num, unsigned primary_num) const
286{
287 const unsigned index = reaction_num * _num_primary + primary_num;
288 return _reactions[index];
289}
290
291void
293 unsigned & zero_count) const
294{
295 zero_count = 0;
296 for (unsigned i = 0; i < _num_primary; ++i)
297 {
298 if (_primary_activity_coefficients[i] * (*_primary[i])[_qp] <= 0.0)
299 {
300 zero_count += 1;
301 zero_conc_index = i;
302 if (zero_count > 1)
303 return;
304 }
305 }
306 return;
307}
308
309void
311{
312 for (unsigned r = 0; r < _num_reactions; ++r)
313 {
314 _mineral_sat[r] =
315 (_equilibrium_constants_as_log10 ? std::pow(10.0, -(*_equilibrium_constants[r])[_qp])
316 : 1.0 / (*_equilibrium_constants[r])[_qp]);
317 for (unsigned j = 0; j < _num_primary; ++j)
318 {
319 const Real gamp = _primary_activity_coefficients[j] * (*_primary[j])[_qp];
320 if (gamp <= 0.0)
321 {
322 if (stoichiometry(r, j) < 0.0)
323 _mineral_sat[r] = std::numeric_limits<Real>::max();
324 else if (stoichiometry(r, j) == 0.0)
325 _mineral_sat[r] *= 1.0;
326 else
327 {
328 _mineral_sat[r] = 0.0;
329 break;
330 }
331 }
332 else
333 _mineral_sat[r] *= std::pow(gamp, stoichiometry(r, j));
334 }
335 const Real fac = 1.0 - std::pow(_mineral_sat[r], _theta_exponent[r]);
336 // if fac > 0 then dissolution occurs; if fac < 0 then precipitation occurs.
337 const Real sgn = (fac < 0 ? -1.0 : 1.0);
338 const Real unbounded_rr = -sgn * rateConstantQp(r) * _r_area[r] * _molar_volume[r] *
339 std::pow(std::abs(fac), _eta_exponent[r]);
340
341 /*
342 *
343 * Note the use of the OLD value of porosity here.
344 * This strategy, which breaks the cyclic dependency between porosity
345 * and mineral concentration, is used in
346 * Kernel: PorousFlowPreDis
347 * Material: PorousFlowPorosity
348 * Material: PorousFlowAqueousPreDisChemistry
349 * Material: PorousFlowAqueousPreDisMineral
350 *
351 */
352
353 // bound the reaction so _sec_conc lies between zero and unity
354 const Real por_times_rr_dt = _porosity_old[_qp] * _saturation[_qp][_aq_ph] * unbounded_rr * _dt;
355 if (_sec_conc_old[_qp][r] + por_times_rr_dt > 1.0)
356 {
357 _bounded_rate[r] = true;
358 _reaction_rate[_qp][r] =
359 (1.0 - _sec_conc_old[_qp][r]) / _porosity_old[_qp] / _saturation[_qp][_aq_ph] / _dt;
360 }
361 else if (_sec_conc_old[_qp][r] + por_times_rr_dt < 0.0)
362 {
363 _bounded_rate[r] = true;
364 _reaction_rate[_qp][r] =
365 -_sec_conc_old[_qp][r] / _porosity_old[_qp] / _saturation[_qp][_aq_ph] / _dt;
366 }
367 else
368 {
369 _bounded_rate[r] = false;
370 _reaction_rate[_qp][r] = unbounded_rr;
371 }
372 }
373}
374
375void
377 std::vector<Real> & drr) const
378{
379 drr.assign(_num_primary, 0.0);
380
381 // handle corner case
382 if (_bounded_rate[reaction_num])
383 return;
384
385 /*
386 * Form the derivative of _mineral_sat, and store it in drr for now.
387 * The derivatives are straightforward if all primary > 0.
388 *
389 * If more than one primary = 0 then I set the derivatives to zero, even though it could be
390 * argued that with certain stoichiometric coefficients you might have derivative = 0/0 and it
391 * might be appropriate to set this to a non-zero finite value.
392 *
393 * If exactly one primary = 0 and its stoichiometry = 1 then the derivative wrt this one is
394 * nonzero.
395 * If exactly one primary = 0 and its stoichiometry > 1 then all derivatives are zero.
396 * If exactly one primary = 0 and its stoichiometry < 1 then the derivative wrt this one is
397 * infinity
398 */
399
400 unsigned zero_count = 0;
401 unsigned zero_conc_index = 0;
402 findZeroConcentration(zero_conc_index, zero_count);
403 if (zero_count == 0)
404 {
405 for (unsigned i = 0; i < _num_primary; ++i)
406 drr[i] = stoichiometry(reaction_num, i) * _mineral_sat[reaction_num] /
407 std::max((*_primary[i])[_qp], 0.0);
408 }
409 else
410 {
411 if (_theta_exponent[reaction_num] < 1.0)
412 // dfac = infinity (see below) so the derivative may be inf, inf * 0, or inf/inf. I simply
413 // return with drr = 0
414 return;
415
416 // count the number of primary <= 0, and record the one that's zero
417 if (zero_count == 1 && stoichiometry(reaction_num, zero_conc_index) == 1.0)
418 {
419 Real conc_without_zero = (_equilibrium_constants_as_log10
420 ? std::pow(10.0, -(*_equilibrium_constants[reaction_num])[_qp])
421 : 1.0 / (*_equilibrium_constants[reaction_num])[_qp]);
422 for (unsigned i = 0; i < _num_primary; ++i)
423 {
424 if (i == zero_conc_index)
425 conc_without_zero *= _primary_activity_coefficients[i];
426 else
427 conc_without_zero *=
428 std::pow(_primary_activity_coefficients[i] * std::max((*_primary[i])[_qp], 0.0),
429 stoichiometry(reaction_num, i));
430 }
431 drr[zero_conc_index] = conc_without_zero;
432 }
433 else if (zero_count == 0 and stoichiometry(reaction_num, zero_conc_index) < 1.0)
434 drr[zero_conc_index] = std::numeric_limits<Real>::max();
435 else
436 // all other cases have drr = 0, so return without performing any other calculations
437 return;
438 }
439
440 // At the moment _drr = d(mineral_sat)/d(primary)
441 // Multiply by appropriate term to give _drr = d(reaction_rate)/d(primary)
442 const Real fac = 1.0 - std::pow(_mineral_sat[reaction_num], _theta_exponent[reaction_num]);
443 const Real dfac = -_theta_exponent[reaction_num] *
444 std::pow(_mineral_sat[reaction_num], _theta_exponent[reaction_num] - 1.0);
445 const Real multiplier = -rateConstantQp(reaction_num) * _r_area[reaction_num] *
446 _molar_volume[reaction_num] *
447 std::pow(std::abs(fac), _eta_exponent[reaction_num] - 1.0) *
448 _eta_exponent[reaction_num] * dfac;
449 for (unsigned i = 0; i < _num_primary; ++i)
450 drr[i] *= multiplier;
451}
452
453Real
455{
456 return _ref_kconst[reaction_num] * std::exp(_e_act[reaction_num] / _gas_const *
457 (_one_over_ref_temp - 1.0 / _temperature[_qp]));
458}
459
460Real
462{
463 // handle corner case
464 if (_bounded_rate[reaction_num])
465 return 0.0;
466
467 const Real drate_const = rateConstantQp(reaction_num) * _e_act[reaction_num] / _gas_const *
468 std::pow(_temperature[_qp], -2.0);
469 const Real fac = 1.0 - std::pow(_mineral_sat[reaction_num], _theta_exponent[reaction_num]);
470 const Real sgn = (fac < 0 ? -1.0 : 1.0);
471 const Real dkinetic_rate = -sgn * drate_const * _r_area[reaction_num] *
472 _molar_volume[reaction_num] *
473 std::pow(std::abs(fac), _eta_exponent[reaction_num]);
474
475 return dkinetic_rate;
476}
const double v
void mooseError(Args &&... args)
registerMooseObject("PorousFlowApp", PorousFlowAqueousPreDisChemistry)
void addRequiredCoupledVar(const std::string &name, const std::string &doc_string)
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addPrivateParam(const std::string &name, const T &value)
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)
virtual void resize(const std::size_t size) override final
Material designed to form a std::vector of mass fractions of mineral concentrations from primary-spec...
std::vector< unsigned int > _primary_var_num
The variable number of the primary variables.
const std::vector< Real > _r_area
Reactive surface area (m^2/L) for each reaction.
const unsigned int _num_primary
Number of primary species.
const MaterialProperty< std::vector< Real > > & _sec_conc_old
std::vector< bool > _bounded_rate
Whether the reaction rate has to be bounded in order that the precipitate stays inside [0,...
const Real _one_over_ref_temp
1/reference_temperature (1/K)
void findZeroConcentration(unsigned &zero_conc_index, unsigned &zero_count) const
Checks gamp[i] = _primary_activity_coefficients[i] * (*_primary[i])[qp].
std::vector< const VariableValue * > _primary
Values of the primary species' concentrations (dimensionless)
const std::vector< Real > _ref_kconst
Rate constant (mol/(m^2 s)) at reference temperature for each reaction.
const MaterialProperty< std::vector< Real > > & _dtemperature_dvar
d(temperature)/(d porflow variable)
const Real _gas_const
Gas constant (J/(mol K))
MaterialProperty< std::vector< Real > > & _reaction_rate
Reaction rate of mineralisation.
const unsigned int _aq_ph
Aqueous phase number.
const MaterialProperty< std::vector< Real > > & _saturation
Saturation.
const std::vector< Real > _reactions
Stoichiometry defining the aqeuous geochemistry equilibrium reactions.
const MaterialProperty< Real > & _porosity_old
Old values of the porosity.
Real stoichiometry(unsigned reaction_num, unsigned primary_num) const
The stoichiometric coefficient.
virtual Real dQpReactionRate_dT(unsigned reaction_num) const
Computes derivative of the reaction rate with respect to the temperature.
const unsigned int _num_reactions
Number of equations in the aqueous geochemistry system.
const bool _equilibrium_constants_as_log10
Whether the equilibium constants are written in their log10 form, or in absolute terms.
virtual void computeQpReactionRates()
Compute the secondary-species concentration as defined by the chemistry Must be overridden by derived...
virtual void dQpReactionRate_dprimary(unsigned reaction_num, std::vector< Real > &drr) const
Computes derivative of the reaction rate with respect to the primary concentrations.
std::vector< const VariableValue * > _equilibrium_constants
Equilibrium constants (dimensionless)
PorousFlowAqueousPreDisChemistry(const InputParameters &parameters)
const MaterialProperty< Real > & _temperature
Temperature.
const std::vector< Real > _eta_exponent
Eta exponent for the precipitation-dissolution for each reaction.
std::vector< Real > _mineral_sat
Mineral saturation ratio - a useful temporary variable during computeQpProperties.
const std::vector< Real > _primary_activity_coefficients
Activity coefficients for the primary species (dimensionless)
MaterialProperty< std::vector< std::vector< Real > > > & _dreaction_rate_dvar
d(reaction rate of mineralisation)/d(porous flow var)
const std::vector< Real > _theta_exponent
Theta exponent for the precipitation-dissolution for each reaction.
const std::vector< Real > _e_act
Activation energy (J/mol) for each reaction.
Real rateConstantQp(unsigned reaction_num) const
const unsigned _num_equilibrium_constants
Number of equilibrium_constants provided.
const std::vector< Real > _molar_volume
Molar volume (L/mol) for each secondary species.
Base class for all PorousFlow vector materials.
const unsigned int _num_components
Number of fluid components.
const unsigned int _num_var
Number of PorousFlow variables.