Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
scavenger::ParserChemReaction Class Reference

Parser to read user files defining chemical reactions and scavengers (reaction with background) More...

#include <Doxymodules_medical.h>

Public Member Functions

 ParserChemReaction ()=default
 
 ~ParserChemReaction ()=default
 
void ReadReactionFile (const G4String &fileName)
 
auto GetListReactant1 ()
 
auto GetListReactant2 ()
 
auto GetListProduct ()
 
auto GetListRate ()
 

Private Member Functions

void AddReaction (const G4String &reactionString, const G4String &type)
 
void ReadReservoir (const G4String &reservoirString)
 
G4double GetScavengerConcentration (const G4String &name)
 
void ImplementReaction (const G4String &reactant1, const G4String &reactant2, const std::vector< G4String > &product, const G4double &reactionRate, const G4String &type)
 

Static Private Member Functions

static void ReadReaction (const G4String &reactionString, std::vector< G4String > &reactant, std::vector< G4String > &product, G4double &reactionRate)
 
static void ReplaceString (G4String &aString, const G4String &from, const G4String &to)
 

Private Attributes

std::array< std::vector< G4String >, 5 > fListReactant1
 
std::array< std::vector< G4String >, 5 > fListReactant2
 
std::array< std::vector< std::vector< G4String > >, 5 > fListProduct
 
std::array< std::vector< G4double >, 5 > fListRate
 
std::map< G4String, G4double > fReservoirConcentrationMap
 

Detailed Description

Parser to read user files defining chemical reactions and scavengers (reaction with background)

Definition at line 347 of file Doxymodules_medical.h.

Constructor & Destructor Documentation

◆ ParserChemReaction()

scavenger::ParserChemReaction::ParserChemReaction ( )
default

◆ ~ParserChemReaction()

scavenger::ParserChemReaction::~ParserChemReaction ( )
default

Member Function Documentation

◆ ReadReactionFile()

void scavenger::ParserChemReaction::ReadReactionFile ( const G4String fileName)

Definition at line 319 of file ParserChemReaction.cc.

319 {
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}
void ReadReservoir(const G4String &reservoirString)
void AddReaction(const G4String &reactionString, const G4String &type)
G4bool contains(const G4String &str, std::string_view ss)

◆ GetListReactant1()

auto scavenger::ParserChemReaction::GetListReactant1 ( )
inline

Definition at line 51 of file ParserChemReaction.hh.

51{ return fListReactant1; }
std::array< std::vector< G4String >, 5 > fListReactant1

◆ GetListReactant2()

auto scavenger::ParserChemReaction::GetListReactant2 ( )
inline

Definition at line 53 of file ParserChemReaction.hh.

53{ return fListReactant2; }
std::array< std::vector< G4String >, 5 > fListReactant2

◆ GetListProduct()

auto scavenger::ParserChemReaction::GetListProduct ( )
inline

Definition at line 55 of file ParserChemReaction.hh.

55{ return fListProduct; }
std::array< std::vector< std::vector< G4String > >, 5 > fListProduct

◆ GetListRate()

auto scavenger::ParserChemReaction::GetListRate ( )
inline

Definition at line 57 of file ParserChemReaction.hh.

57{ return fListRate; }
std::array< std::vector< G4double >, 5 > fListRate

◆ AddReaction()

void scavenger::ParserChemReaction::AddReaction ( const G4String reactionString,
const G4String type 
)
private

Definition at line 233 of file ParserChemReaction.cc.

234 {
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}
static void ReadReaction(const G4String &reactionString, std::vector< G4String > &reactant, std::vector< G4String > &product, G4double &reactionRate)
G4double GetScavengerConcentration(const G4String &name)
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)

◆ ReadReservoir()

void scavenger::ParserChemReaction::ReadReservoir ( const G4String reservoirString)
private

Definition at line 176 of file ParserChemReaction.cc.

176 {
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}
std::map< G4String, G4double > fReservoirConcentrationMap

◆ GetScavengerConcentration()

G4double scavenger::ParserChemReaction::GetScavengerConcentration ( const G4String name)
private

Definition at line 155 of file ParserChemReaction.cc.

155 {
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}

◆ ReadReaction()

void scavenger::ParserChemReaction::ReadReaction ( const G4String reactionString,
std::vector< G4String > &  reactant,
std::vector< G4String > &  product,
G4double &  reactionRate 
)
staticprivate

Definition at line 87 of file ParserChemReaction.cc.

90 {
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}

◆ ImplementReaction()

void scavenger::ParserChemReaction::ImplementReaction ( const G4String reactant1,
const G4String reactant2,
const std::vector< G4String > &  product,
const G4double &  reactionRate,
const G4String type 
)
private

Definition at line 52 of file ParserChemReaction.cc.

56 {
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}

◆ ReplaceString()

void scavenger::ParserChemReaction::ReplaceString ( G4String aString,
const G4String from,
const G4String to 
)
staticprivate

Definition at line 38 of file ParserChemReaction.cc.

39 {
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}

Member Data Documentation

◆ fListReactant1

std::array<std::vector<G4String>, 5> scavenger::ParserChemReaction::fListReactant1
private

Definition at line 81 of file ParserChemReaction.hh.

◆ fListReactant2

std::array<std::vector<G4String>, 5> scavenger::ParserChemReaction::fListReactant2
private

Definition at line 82 of file ParserChemReaction.hh.

◆ fListProduct

std::array<std::vector<std::vector<G4String> >, 5> scavenger::ParserChemReaction::fListProduct
private

Definition at line 83 of file ParserChemReaction.hh.

◆ fListRate

std::array<std::vector<G4double>, 5> scavenger::ParserChemReaction::fListRate
private

Definition at line 84 of file ParserChemReaction.hh.

◆ fReservoirConcentrationMap

std::map<G4String, G4double> scavenger::ParserChemReaction::fReservoirConcentrationMap
private

Definition at line 85 of file ParserChemReaction.hh.


The documentation for this class was generated from the following files:

Applications | User Support | Publications | Collaboration