Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 /// \file scavenger/src/ParserChemReaction.cc 27 /// \brief Implementation of the scavenger::Pa 28 29 #include "ParserChemReaction.hh" 30 31 #include "G4SystemOfUnits.hh" 32 33 #include <fstream> 34 35 namespace scavenger 36 { 37 38 //....oooOO0OOooo........oooOO0OOooo........oo 39 40 void ParserChemReaction::ReplaceString(G4Strin 41 { 42 if (G4StrUtil::contains(aString, from)) { 43 size_t startPosition = 0; 44 while ((startPosition = aString.find(from, 45 aString.replace(startPosition, from.leng 46 startPosition += to.length(); 47 } 48 } 49 } 50 51 //....oooOO0OOooo........oooOO0OOooo........oo 52 53 void ParserChemReaction::ImplementReaction(con 54 con 55 con 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 } 63 else if (type == "II") { 64 fListReactant1[1].push_back(reactant1); 65 fListReactant2[1].push_back(reactant2); 66 fListProduct[1].push_back(product); 67 fListRate[1].push_back(reactionRate); 68 } 69 else if (type == "III") { 70 fListReactant1[2].push_back(reactant1); 71 fListReactant2[2].push_back(reactant2); 72 fListProduct[2].push_back(product); 73 fListRate[2].push_back(reactionRate); 74 } 75 else if (type == "IV") { 76 fListReactant1[3].push_back(reactant1); 77 fListReactant2[3].push_back(reactant2); 78 fListProduct[3].push_back(product); 79 fListRate[3].push_back(reactionRate); 80 } 81 else if (type == "VI") { 82 fListReactant1[4].push_back(reactant1); 83 fListReactant2[4].push_back(reactant2); 84 fListProduct[4].push_back(product); 85 fListRate[4].push_back(reactionRate); 86 } 87 } 88 89 //....oooOO0OOooo........oooOO0OOooo........oo 90 91 void ParserChemReaction::ReadReaction(const G4 92 std::vec 93 std::vec 94 { 95 reactant.clear(); 96 product.clear(); 97 98 G4bool readReaction = true; 99 G4bool readReactant = true; 100 G4bool readProduct = false; 101 G4bool readRate = false; 102 103 std::stringstream aStream; 104 aStream << reactionString; 105 106 while (!aStream.eof() && readReaction) { 107 G4String aString; 108 aStream >> aString; 109 110 if (G4StrUtil::contains(aString, "#")) { 111 readReaction = false; 112 } 113 else if (readReactant) { 114 if (aString == G4String("->")) { 115 readReactant = false; 116 readProduct = true; 117 } 118 else if (aString != G4String("+")) { 119 ReplaceString(aString, G4String("+"), 120 ReplaceString(aString, G4String("-"), 121 122 if (reactant.size() < 2) { 123 reactant.push_back(aString); 124 } 125 } 126 } 127 else if (readProduct) { 128 if (aString == G4String(",")) { 129 readProduct = false; 130 readRate = true; 131 } 132 else if (aString != G4String("+") && !G4 133 && !G4StrUtil::contains(aString 134 { 135 ReplaceString(aString, G4String("+"), 136 ReplaceString(aString, G4String("-"), 137 product.push_back(aString); 138 } 139 } 140 else if (readRate) { 141 std::stringstream aStreamTmp; 142 aStreamTmp << aString; 143 aStreamTmp >> reactionRate; 144 145 if (reactant.size() == 1) { 146 // For first-order reactions 147 reactionRate *= (1 * 1 / s); 148 } 149 else { 150 reactionRate *= (1e-3 * m3 / (mole * s 151 } 152 153 readRate = false; 154 readReaction = false; 155 } 156 } 157 158 // For first-order reactions 159 if (reactant.size() == 1) { 160 reactant.emplace_back("NoneM"); 161 } 162 } 163 164 //....oooOO0OOooo........oooOO0OOooo........oo 165 166 G4double ParserChemReaction::GetScavengerConce 167 { 168 G4double concentration = -1.; 169 170 std::map<G4String, G4double>::iterator it; 171 it = fReservoirConcentrationMap.find(name); 172 173 if (it != fReservoirConcentrationMap.end()) 174 concentration = it->second; 175 } 176 else { 177 G4ExceptionDescription exception; 178 exception << "Scavenger is not defined: " 179 << "reaction will not be registe 180 G4Exception("ParserChemReaction::GetScaven 181 exception); 182 } 183 184 return concentration; 185 } 186 187 //....oooOO0OOooo........oooOO0OOooo........oo 188 189 void ParserChemReaction::ReadReservoir(const G 190 { 191 G4double concentration = 0.; 192 G4String name = ""; 193 194 G4bool readScavenger = true; 195 G4bool readName = false; 196 G4bool readConcentration = false; 197 198 std::stringstream aStream; 199 aStream << reservoirString; 200 201 while (!aStream.eof() && readScavenger) { 202 G4String aString; 203 aStream >> aString; 204 205 if (G4StrUtil::contains(aString, "#")) { 206 readScavenger = false; 207 } 208 else if (aString == G4String("scavenger:") 209 readName = true; 210 } 211 else if (readName) { 212 name = G4String(aString); 213 ReplaceString(name, G4String("+"), G4Str 214 ReplaceString(name, G4String("-"), G4Str 215 216 readName = false; 217 readConcentration = true; 218 } 219 else if (readConcentration) { 220 std::stringstream aStreamTmp; 221 aStreamTmp << aString; 222 aStreamTmp >> concentration; 223 concentration *= (mole / (1e-3 * m3)); 224 readConcentration = false; 225 readScavenger = false; 226 } 227 } 228 229 if (concentration > 0.) { 230 if (fReservoirConcentrationMap.count(name) 231 fReservoirConcentrationMap[name] = conce 232 } 233 else { 234 G4ExceptionDescription exception; 235 exception << "Scavenger already defined 236 << "scavenger will not be regi 237 G4Exception("ParserChemReaction::ReadRes 238 } 239 } 240 else { 241 G4ExceptionDescription exception; 242 exception << "Null or negative scavenger c 243 << "scavenger will not be regist 244 G4Exception("ParserChemReaction::ReadReser 245 } 246 } 247 248 //....oooOO0OOooo........oooOO0OOooo........oo 249 250 void ParserChemReaction::AddReaction(const G4S 251 { 252 std::vector<G4String> reactant; 253 std::vector<G4String> product; 254 G4double reactionRate = -1; 255 256 G4bool reservoir = false; 257 258 if (type == "VI") { 259 reservoir = true; 260 } 261 262 ReadReaction(reactionString, reactant, produ 263 264 if (!reactant.empty() && (reactionRate <= 0) 265 G4ExceptionDescription exception; 266 exception << "Null or negative reaction ra 267 << "reaction will not be registe 268 G4Exception("ParserChemReaction::AddReacti 269 return; 270 } 271 272 G4double concentration; 273 274 if (reservoir && (reactant.size() >= 2)) { 275 if (G4StrUtil::contains(reactant[0], "[") 276 ReplaceString(reactant[0], G4String("[") 277 ReplaceString(reactant[0], G4String("]") 278 279 concentration = GetScavengerConcentratio 280 281 if (concentration != -1) { 282 reactionRate *= concentration; 283 reactant[0].append("(B)"); 284 ImplementReaction(reactant[1], reactan 285 } 286 } 287 else if (G4StrUtil::contains(reactant[1], 288 ReplaceString(reactant[1], G4String("[") 289 ReplaceString(reactant[1], G4String("]") 290 291 concentration = GetScavengerConcentratio 292 293 if (concentration != -1) { 294 reactionRate *= concentration; 295 reactant[1].append("(B)"); 296 ImplementReaction(reactant[0], reactan 297 } 298 } 299 else if (reactant[1] == "NoneM") { 300 // First-order reaction 301 ImplementReaction(reactant[0], reactant[ 302 } 303 else { 304 G4ExceptionDescription exception; 305 exception << "Missing or unsuitable squa 306 << "reaction will not be regis 307 << "Verify the writing of chem 308 G4Exception("ParserChemReaction::AddReac 309 } 310 } 311 else if (reactant.size() >= 2) { 312 if (!G4StrUtil::contains(reactant[0], "[") 313 && !G4StrUtil::contains(reactant[1], " 314 && (reactant[1] != "NoneM")) 315 { 316 ImplementReaction(reactant[0], reactant[ 317 } 318 else if (reactant[1] == "NoneM") { 319 G4ExceptionDescription exception; 320 exception << "Unsuitable reaction type: 321 << "reaction will not be regis 322 << "For first-order reaction, 323 G4Exception("ParserChemReaction::AddReac 324 } 325 else { 326 G4ExceptionDescription exception; 327 exception << "Unsuitable square brackets 328 << "reaction will not be regis 329 << "Verify the writing of chem 330 G4Exception("ParserChemReaction::AddReac 331 } 332 } 333 } 334 335 //....oooOO0OOooo........oooOO0OOooo........oo 336 337 void ParserChemReaction::ReadReactionFile(cons 338 { 339 G4String line; 340 std::ifstream myFile(fileName); 341 342 if (myFile.is_open()) { 343 while (getline(myFile, line)) { 344 if (G4StrUtil::contains(line, "type_1")) 345 AddReaction(line, "I"); 346 } 347 else if (G4StrUtil::contains(line, "type 348 AddReaction(line, "II"); 349 } 350 else if (G4StrUtil::contains(line, "type 351 AddReaction(line, "III"); 352 } 353 else if (G4StrUtil::contains(line, "type 354 AddReaction(line, "IV"); 355 } 356 else if (G4StrUtil::contains(line, "type 357 AddReaction(line, "VI"); 358 } 359 else if (G4StrUtil::contains(line, "scav 360 ReadReservoir(line); 361 } 362 else if (!G4StrUtil::contains(line, "#") 363 G4ExceptionDescription exception; 364 exception << "Unknown declaration: " 365 << "reaction or scavenger wi 366 << "Verify the writing of ch 367 G4Exception("ParserChemReaction::ReadR 368 } 369 } 370 371 myFile.close(); 372 } 373 else { 374 G4ExceptionDescription exception; 375 exception << "Chemical reaction file not f 376 G4Exception("ParserChemReaction::ReadReact 377 } 378 } 379 380 //....oooOO0OOooo........oooOO0OOooo........oo 381 382 } // namespace scavenger