www.mooseframework.org
AddCoupledSolidKinSpeciesAction.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
8 #include "MooseUtils.h"
9 #include "FEProblem.h"
10 #include "Factory.h"
11 #include "MooseError.h"
12 #include "Parser.h"
13 #include <algorithm>
14 
15 // Regular expression includes
16 #include "pcrecpp.h"
17 
18 template <>
19 InputParameters
21 {
22  InputParameters params = validParams<Action>();
23  params.addRequiredParam<std::vector<NonlinearVariableName>>("primary_species",
24  "The list of primary species to add");
25  params.addParam<std::vector<AuxVariableName>>(
26  "secondary_species", "The list of solid kinetic species to be output as aux variables");
27  params.addRequiredParam<std::string>("kin_reactions", "The list of solid kinetic reactions");
28  params.addRequiredParam<std::vector<Real>>("log10_keq",
29  "The list of equilibrium constants for all reactions");
30  params.addRequiredParam<std::vector<Real>>(
31  "specific_reactive_surface_area",
32  "The list of specific reactive surface area for all minerals (m^2/L)");
33  params.addRequiredParam<std::vector<Real>>(
34  "kinetic_rate_constant", "The list of kinetic rate constant for all reactions (mol/m^2/s)");
35  params.addRequiredParam<std::vector<Real>>(
36  "activation_energy", "The list of activation energy values for all reactions (J/mol)");
37  params.addParam<Real>("gas_constant", 8.314, "Gas constant. Default is 8.314 (J/mol/K)");
38  params.addRequiredParam<std::vector<Real>>(
39  "reference_temperature", "The list of reference temperatures for all reactions (K)");
40  params.addRequiredCoupledVar("system_temperature",
41  "The system temperature for all reactions (K)");
42  params.addClassDescription("Adds solid kinetic Kernels and AuxKernels for primary species");
43  return params;
44 }
45 
47  : Action(params),
48  _primary_species(getParam<std::vector<NonlinearVariableName>>("primary_species")),
49  _secondary_species(getParam<std::vector<AuxVariableName>>("secondary_species")),
50  _kinetic_species_involved(_primary_species.size()),
51  _weights(_primary_species.size()),
52  _input_reactions(getParam<std::string>("kin_reactions")),
53  _logk(getParam<std::vector<Real>>("log10_keq")),
54  _r_area(getParam<std::vector<Real>>("specific_reactive_surface_area")),
55  _ref_kconst(getParam<std::vector<Real>>("kinetic_rate_constant")),
56  _e_act(getParam<std::vector<Real>>("activation_energy")),
57  _gas_const(getParam<Real>("gas_constant")),
58  _ref_temp(getParam<std::vector<Real>>("reference_temperature")),
59  _sys_temp(getParam<std::vector<VariableName>>("system_temperature"))
60 {
61  // Note: as the reaction syntax has changed, check to see if the old syntax has
62  // been used and throw an informative error. The number of = signs should be one
63  // more than the number of commas, while the smallest number of spaces possible is 2
64  bool old_syntax = false;
65  if (std::count(_input_reactions.begin(), _input_reactions.end(), '=') !=
66  std::count(_input_reactions.begin(), _input_reactions.end(), ',') + 1)
67  old_syntax = true;
68 
69  if (std::count(_input_reactions.begin(), _input_reactions.end(), ' ') < 2)
70  old_syntax = true;
71 
72  if (old_syntax)
73  mooseError("Old solid kinetic reaction syntax present.\nReactions should now be comma "
74  "separated, and must have spaces between species and +/-/= operators.\n"
75  "See #9972 for details");
76 
77  // Parse the kinetic reactions
78  pcrecpp::RE re_reactions("(.+?)" // A single reaction (any character until the comma delimiter)
79  "(?:,\\s*|$)" // comma or end of string
80  ,
81  pcrecpp::RE_Options().set_extended(true));
82 
83  pcrecpp::RE re_terms("(\\S+)");
84  pcrecpp::RE re_coeff_and_species("(?: \\(? (.*?) \\)? )" // match the leading coefficent
85  "([A-Za-z].*)" // match the species
86  ,
87  pcrecpp::RE_Options().set_extended(true));
88 
89  pcrecpp::StringPiece input(_input_reactions);
90  pcrecpp::StringPiece single_reaction, term;
91  std::string single_reaction_str;
92 
93  // Parse reaction network to extract each individual reaction
94  while (re_reactions.FindAndConsume(&input, &single_reaction_str))
95  _reactions.push_back(single_reaction_str);
96 
97  _num_reactions = _reactions.size();
98 
99  if (_num_reactions == 0)
100  mooseError("No solid kinetic reaction provided!");
101 
102  // Start parsing each reaction
103  for (unsigned int i = 0; i < _num_reactions; ++i)
104  {
105  single_reaction = _reactions[i];
106 
107  // Capture all of the terms
108  std::string species, coeff_str;
109  Real coeff;
110  int sign = 1;
111  bool secondary = false;
112 
113  std::vector<Real> local_stos;
114  std::vector<VariableName> local_species_list;
115 
116  // Find every single term in this reaction (species and operators)
117  while (re_terms.FindAndConsume(&single_reaction, &term))
118  {
119  // Separating the stoichiometric coefficients from species
120  if (re_coeff_and_species.PartialMatch(term, &coeff_str, &species))
121  {
122  if (coeff_str.length())
123  coeff = std::stod(coeff_str);
124  else
125  coeff = 1.0;
126 
127  coeff *= sign;
128 
129  if (secondary)
130  _solid_kinetic_species.push_back(species);
131  else
132  {
133  local_stos.push_back(coeff);
134  local_species_list.push_back(species);
135  }
136  }
137  // Finding the operators and assign value of -1.0 to "-" sign
138  else if (term == "+" || term == "=" || term == "-")
139  {
140  if (term == "-")
141  {
142  sign = -1;
143  term = "+";
144  }
145 
146  if (term == "=")
147  secondary = true;
148  }
149  else
150  mooseError("Error parsing term: ", term.as_string());
151  }
152 
153  _stos.push_back(local_stos);
154  _primary_species_involved.push_back(local_species_list);
155  }
156 
157  // Start picking out primary species and coupled primary species and assigning
158  // corresponding stoichiomentric coefficients
159  for (unsigned int i = 0; i < _primary_species.size(); ++i)
160  for (unsigned int j = 0; j < _num_reactions; ++j)
161  {
162  for (unsigned int k = 0; k < _primary_species_involved[j].size(); ++k)
164  {
165  _weights[i].push_back(_stos[j][k]);
167  }
168  }
169 
170  // Print out details of the solid kinetic reactions to the console
171  _console << "Solid kinetic reactions:\n";
172  for (unsigned int i = 0; i < _num_reactions; ++i)
173  _console << " Reaction " << i + 1 << ": " << _reactions[i] << "\n";
174  _console << "\n";
175 
176  // Check that all secondary species read from the reaction network have been added
177  // as AuxVariables. Note: can't sort the _solid_kinetic_species vector as it throws
178  // out the species and coefficient vectors so use std::is_permutation
179  if (!std::is_permutation(
181  mooseError("All solid kinetic species must be added as secondary species");
182 
183  // Check that the size of property vectors is equal to the number of reactions
184  if (_logk.size() != _num_reactions)
185  mooseError("The number of values entered for log10_keq is not equal to the number of solid "
186  "kinetic reactions");
187  if (_r_area.size() != _num_reactions)
188  mooseError("The number of values entered for specific_reactive_surface_area is not equal to "
189  "the number of solid kinetic reactions");
190  if (_ref_kconst.size() != _num_reactions)
191  mooseError("The number of values entered for kinetic_rate_constant is not equal to the number "
192  "of solid kinetic reactions");
193  if (_e_act.size() != _num_reactions)
194  mooseError("The number of values entered for activation_energy is not equal to the number of "
195  "solid kinetic reactions");
196  if (_ref_temp.size() != _num_reactions)
197  mooseError("The number of values entered for reference_temperature is not equal to the number "
198  "of solid kinetic reactions");
199 }
200 
201 void
203 {
204  if (_current_task == "add_kernel")
205  {
206  // Add Kernels for each primary species
207  for (unsigned int i = 0; i < _primary_species.size(); ++i)
208  {
209  InputParameters params_kin = _factory.getValidParams("CoupledBEKinetic");
210  params_kin.set<NonlinearVariableName>("variable") = _primary_species[i];
211  params_kin.set<std::vector<Real>>("weight") = _weights[i];
212  params_kin.set<std::vector<VariableName>>("v") = _kinetic_species_involved[i];
213  _problem->addKernel("CoupledBEKinetic", _primary_species[i] + "_" + "_kin", params_kin);
214  }
215  }
216 
217  if (_current_task == "add_aux_kernel")
218  {
219  // Add AuxKernels for each solid kinetic species
220  for (unsigned int i = 0; i < _num_reactions; ++i)
221  {
222  InputParameters params_kin = _factory.getValidParams("KineticDisPreConcAux");
223  params_kin.set<AuxVariableName>("variable") = _solid_kinetic_species[i];
224  params_kin.set<Real>("log_k") = _logk[i];
225  params_kin.set<Real>("r_area") = _r_area[i];
226  params_kin.set<Real>("ref_kconst") = _ref_kconst[i];
227  params_kin.set<Real>("e_act") = _e_act[i];
228  params_kin.set<Real>("gas_const") = _gas_const;
229  params_kin.set<Real>("ref_temp") = _ref_temp[i];
230  params_kin.set<std::vector<VariableName>>("sys_temp") = _sys_temp;
231  params_kin.set<std::vector<Real>>("sto_v") = _stos[i];
232  params_kin.set<std::vector<VariableName>>("v") = _primary_species_involved[i];
233  _problem->addAuxKernel(
234  "KineticDisPreConcAux", "aux_" + _solid_kinetic_species[i], params_kin);
235  }
236  }
237 }
std::vector< std::vector< Real > > _stos
Stoichiometric coefficients for each primary species in each reaction.
const std::vector< Real > _ref_kconst
Reference kinetic rate constant.
Real sign(Real x)
Definition: MathUtils.h:24
std::vector< std::vector< VariableName > > _primary_species_involved
Primary species involved in the ith kinetic reaction.
const std::vector< Real > _logk
Log10 of equilibrium constant.
InputParameters validParams< AddCoupledSolidKinSpeciesAction >()
const std::vector< VariableName > _sys_temp
Actual system temperature.
AddCoupledSolidKinSpeciesAction(const InputParameters &params)
const std::vector< Real > _ref_temp
Reference temperature.
std::vector< std::string > _reactions
Vector of parsed reactions.
unsigned int _num_reactions
Number of reactions.
std::vector< std::vector< VariableName > > _kinetic_species_involved
Secondary solid species involved the ith primary species.
const std::vector< Real > _r_area
Specific reactive surface area, m^2/L solution.
const Real _gas_const
Gas constant, (Default 8.314 J/mol/K)
const std::vector< AuxVariableName > _secondary_species
Secondary species added as AuxVariables.
const std::vector< NonlinearVariableName > _primary_species
Basis set of primary species.
const std::vector< Real > _e_act
Activation energy.
std::string _input_reactions
Reaction network read from input file.
std::vector< VariableName > _solid_kinetic_species
Secondary solid species read by the parser.
std::vector< std::vector< Real > > _weights
Weight of each primary species in each reaction.