Loading...
Searching...
No Matches
ParserChemReaction.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26/// \file scavenger/src/ParserChemReaction.cc
27/// \brief Implementation of the scavenger::ParserChemReaction class
28
29#include "ParserChemReaction.hh"
30#include "G4SystemOfUnits.hh"
31#include <fstream>
32
33namespace scavenger
34{
35
36//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.....
37
39 const G4String &to) {
40 if (G4StrUtil::contains(aString, from)) {
41 size_t startPosition = 0;
42 while ((startPosition = aString.find(from, startPosition))
43 != std::string::npos) {
44 aString.replace(startPosition, from.length(), to);
45 startPosition += to.length();
46 }
47 }
48}
49
50//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
51
53 const G4String &reactant2,
54 const std::vector<G4String> &product,
55 const G4double &reactionRate,
56 const G4String &type) {
57 if (type == "I") {
58 fListReactant1[0].push_back(reactant1);
59 fListReactant2[0].push_back(reactant2);
60 fListProduct[0].push_back(product);
61 fListRate[0].push_back(reactionRate);
62 } else if (type == "II") {
63 fListReactant1[1].push_back(reactant1);
64 fListReactant2[1].push_back(reactant2);
65 fListProduct[1].push_back(product);
66 fListRate[1].push_back(reactionRate);
67 } else if (type == "III") {
68 fListReactant1[2].push_back(reactant1);
69 fListReactant2[2].push_back(reactant2);
70 fListProduct[2].push_back(product);
71 fListRate[2].push_back(reactionRate);
72 } else if (type == "IV") {
73 fListReactant1[3].push_back(reactant1);
74 fListReactant2[3].push_back(reactant2);
75 fListProduct[3].push_back(product);
76 fListRate[3].push_back(reactionRate);
77 } else if (type == "VI") {
78 fListReactant1[4].push_back(reactant1);
79 fListReactant2[4].push_back(reactant2);
80 fListProduct[4].push_back(product);
81 fListRate[4].push_back(reactionRate);
82 }
83}
84
85//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
86
87void ParserChemReaction::ReadReaction(const G4String &reactionString,
88 std::vector<G4String> &reactant,
89 std::vector<G4String> &product,
90 G4double &reactionRate) {
91 reactant.clear();
92 product.clear();
93
94 G4bool readReaction = true;
95 G4bool readReactant = true;
96 G4bool readProduct = false;
97 G4bool readRate = false;
98
99 std::stringstream aStream;
100 aStream << reactionString;
101
102 while (!aStream.eof() && readReaction) {
103 G4String aString;
104 aStream >> aString;
105
106 if (G4StrUtil::contains(aString,"#")) {
107 readReaction = false;
108 } else if (readReactant) {
109 if (aString == G4String("->")) {
110 readReactant = false;
111 readProduct = true;
112 } else if (aString != G4String("+")) {
113 ReplaceString(aString, G4String("+"), G4String("p"));
114 ReplaceString(aString, G4String("-"), G4String("m"));
115
116 if (reactant.size() < 2) {
117 reactant.push_back(aString);
118 }
119 }
120 } else if (readProduct) {
121 if (aString == G4String(",")) {
122 readProduct = false;
123 readRate = true;
124 } else if (aString != G4String("+") && !G4StrUtil::contains(aString,"[")
125 && !G4StrUtil::contains(aString,"]")) {
126 ReplaceString(aString, G4String("+"), G4String("p"));
127 ReplaceString(aString, G4String("-"), G4String("m"));
128 product.push_back(aString);
129 }
130 } else if (readRate) {
131 std::stringstream aStreamTmp;
132 aStreamTmp << aString;
133 aStreamTmp >> reactionRate;
134
135 if (reactant.size() == 1) {
136 // For first-order reactions
137 reactionRate *= (1 * 1 / s);
138 } else {
139 reactionRate *= (1e-3 * m3 / (mole * s));
140 }
141
142 readRate = false;
143 readReaction = false;
144 }
145 }
146
147 // For first-order reactions
148 if (reactant.size() == 1) {
149 reactant.emplace_back("NoneM");
150 }
151}
152
153//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
154
156 G4double concentration = -1.;
157
158 std::map<G4String, G4double>::iterator it;
159 it = fReservoirConcentrationMap.find(name);
160
161 if (it != fReservoirConcentrationMap.end()) {
162 concentration = it->second;
163 } else {
164 G4ExceptionDescription exception;
165 exception << "Scavenger is not defined: "
166 << "reaction will not be registered!";
167 G4Exception("ParserChemReaction::GetScavengerConcentration", "parchem01",
168 JustWarning, exception);
169 }
170
171 return concentration;
172}
173
174//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
175
176void ParserChemReaction::ReadReservoir(const G4String &reservoirString) {
177 G4double concentration = 0.;
178 G4String name = "";
179
180 G4bool readScavenger = true;
181 G4bool readName = false;
182 G4bool readConcentration = false;
183
184 std::stringstream aStream;
185 aStream << reservoirString;
186
187 while (!aStream.eof() && readScavenger) {
188 G4String aString;
189 aStream >> aString;
190
191 if (G4StrUtil::contains(aString,"#")) {
192 readScavenger = false;
193 } else if (aString == G4String("scavenger:")) {
194 readName = true;
195 } else if (readName) {
196 name = G4String(aString);
197 ReplaceString(name, G4String("+"), G4String("p"));
198 ReplaceString(name, G4String("-"), G4String("m"));
199
200 readName = false;
201 readConcentration = true;
202 } else if (readConcentration) {
203 std::stringstream aStreamTmp;
204 aStreamTmp << aString;
205 aStreamTmp >> concentration;
206 concentration *= (mole / (1e-3 * m3));
207 readConcentration = false;
208 readScavenger = false;
209 }
210 }
211
212 if (concentration > 0.) {
213 if (fReservoirConcentrationMap.count(name) < 1) {
214 fReservoirConcentrationMap[name] = concentration;
215 } else {
216 G4ExceptionDescription exception;
217 exception << "Scavenger already defined previously:\n"
218 << "scavenger will not be registered!";
219 G4Exception("ParserChemReaction::ReadReservoir", "parchem02",
220 JustWarning, exception);
221 }
222 } else {
223 G4ExceptionDescription exception;
224 exception << "Null or negative scavenger concentration:\n"
225 << "scavenger will not be registered!";
226 G4Exception("ParserChemReaction::ReadReservoir", "parchem03",
227 JustWarning, exception);
228 }
229}
230
231//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
232
233void ParserChemReaction::AddReaction(const G4String &reactionString,
234 const G4String &type) {
235 std::vector<G4String> reactant;
236 std::vector<G4String> product;
237 G4double reactionRate = -1;
238
239 G4bool reservoir = false;
240
241 if (type == "VI") {
242 reservoir = true;
243 }
244
245 ReadReaction(reactionString, reactant, product, reactionRate);
246
247 if (!reactant.empty() && (reactionRate <= 0)) {
248 G4ExceptionDescription exception;
249 exception << "Null or negative reaction rate: "
250 << "reaction will not be registered!";
251 G4Exception("ParserChemReaction::AddReaction", "parchem04",
252 JustWarning, exception);
253 return;
254 }
255
256 G4double concentration;
257
258 if (reservoir && (reactant.size() >= 2)) {
259 if (G4StrUtil::contains(reactant[0],"[") && G4StrUtil::contains(reactant[0],"]")) {
260 ReplaceString(reactant[0], G4String("["), G4String(""));
261 ReplaceString(reactant[0], G4String("]"), G4String(""));
262
263 concentration = GetScavengerConcentration(reactant[0]);
264
265 if (concentration != -1) {
266 reactionRate *= concentration;
267 reactant[0].append("(B)");
268 ImplementReaction(reactant[1], reactant[0], product,
269 reactionRate, type);
270 }
271 } else if (G4StrUtil::contains(reactant[1],"[") && G4StrUtil::contains(reactant[1],"]")) {
272 ReplaceString(reactant[1], G4String("["), G4String(""));
273 ReplaceString(reactant[1], G4String("]"), G4String(""));
274
275 concentration = GetScavengerConcentration(reactant[1]);
276
277 if (concentration != -1) {
278 reactionRate *= concentration;
279 reactant[1].append("(B)");
280 ImplementReaction(reactant[0], reactant[1], product,
281 reactionRate, type);
282 }
283 } else if (reactant[1] == "NoneM") {
284 // First-order reaction
285 ImplementReaction(reactant[0], reactant[1], product, reactionRate, type);
286 } else {
287 G4ExceptionDescription exception;
288 exception << "Missing or unsuitable square brackets:\n"
289 << "reaction will not be registered.\n"
290 << "Verify the writing of chemical reactions!";
291 G4Exception("ParserChemReaction::AddReaction", "parchem05",
292 JustWarning, exception);
293 }
294 } else if (reactant.size() >= 2) {
295 if (!G4StrUtil::contains(reactant[0],"[") && !G4StrUtil::contains(reactant[0],"]")
296 && !G4StrUtil::contains(reactant[1],"[") && !G4StrUtil::contains(reactant[1],"]")
297 && (reactant[1] != "NoneM")) {
298 ImplementReaction(reactant[0], reactant[1], product, reactionRate, type);
299 } else if (reactant[1] == "NoneM") {
300 G4ExceptionDescription exception;
301 exception << "Unsuitable reaction type: "
302 << "reaction will not be registered.\n"
303 << "For first-order reaction, use reaction type 6.";
304 G4Exception("ParserChemReaction::AddReaction", "parchem06",
305 JustWarning, exception);
306 } else {
307 G4ExceptionDescription exception;
308 exception << "Unsuitable square brackets: "
309 << "reaction will not be registered.\n"
310 << "Verify the writing of chemical reactions!";
311 G4Exception("ParserChemReaction::AddReaction", "parchem07",
312 JustWarning, exception);
313 }
314 }
315}
316
317//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
318
320 G4String line;
321 std::ifstream myFile(fileName);
322
323 if (myFile.is_open()) {
324 while (getline(myFile, line)) {
325 if (G4StrUtil::contains(line,"type_1")) {
326 AddReaction(line, "I");
327 } else if (G4StrUtil::contains(line,"type_2")) {
328 AddReaction(line, "II");
329 } else if (G4StrUtil::contains(line,"type_3")) {
330 AddReaction(line, "III");
331 } else if (G4StrUtil::contains(line,"type_4")) {
332 AddReaction(line, "IV");
333 } else if (G4StrUtil::contains(line,"type_6")) {
334 AddReaction(line, "VI");
335 } else if (G4StrUtil::contains(line,"scavenger:")) {
336 ReadReservoir(line);
337 } else if (!G4StrUtil::contains(line,"#") && !line.empty()) {
338 G4ExceptionDescription exception;
339 exception << "Unknown declaration: "
340 << "reaction or scavenger will not be registered.\n"
341 << "Verify the writing of chemical reactions or scavengers!";
342 G4Exception("ParserChemReaction::ReadReactionFile", "parchem08",
343 JustWarning, exception);
344 }
345 }
346
347 myFile.close();
348 } else {
349 G4ExceptionDescription exception;
350 exception << "Chemical reaction file not found.";
351 G4Exception("ParserChemReaction::ReadReactionFile", "parchem09",
352 JustWarning, exception);
353 }
354}
355
356//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
357
358}
Definition of the scavenger::ParserChemReaction class.
void ReadReactionFile(const G4String &fileName)
std::array< std::vector< G4String >, 5 > fListReactant2
void ReadReservoir(const G4String &reservoirString)
static void ReadReaction(const G4String &reactionString, std::vector< G4String > &reactant, std::vector< G4String > &product, G4double &reactionRate)
void AddReaction(const G4String &reactionString, const G4String &type)
G4double GetScavengerConcentration(const G4String &name)
std::map< G4String, G4double > fReservoirConcentrationMap
std::array< std::vector< G4double >, 5 > fListRate
std::array< std::vector< G4String >, 5 > fListReactant1
static void ReplaceString(G4String &aString, const G4String &from, const G4String &to)
void ImplementReaction(const G4String &reactant1, const G4String &reactant2, const std::vector< G4String > &product, const G4double &reactionRate, const G4String &type)
std::array< std::vector< std::vector< G4String > >, 5 > fListProduct
G4bool contains(const G4String &str, std::string_view ss)

Applications | User Support | Publications | Collaboration