gsystem
Loading...
Searching...
No Matches
gmaterial.cc
Go to the documentation of this file.
1
7
8#include "gutilities.h"
9//using namespace gutilities;
10
11
12// gsystem
13#include "gmaterial.h"
14#include "gsystemConventions.h"
15
16// c++
17#include <iostream>
18#include <sstream>
19
20
21GMaterial::GMaterial(const std::string &s, std::vector<std::string> pars,
22 const std::shared_ptr<GLogger> &logger) : GBase(logger),
23 system(s) {
24 if (pars.size() != gsystem::GMATERIALNUMBEROFPARS) {
25 log->error(gsystem::ERR_GWRONGNUMBEROFPARS,
26 "Incorrect number of material parameters for ", pars[0], ". Expected ",
27 gsystem::GMATERIALNUMBEROFPARS, " but we got ", pars.size());
28 } else {
29 // The parameter vector is a serialized DB/ASCII row. Parsing is positional.
30 size_t i = 0;
31
32 name = gutilities::removeAllSpacesFromString(pars[i++]);
33 density = stod(gutilities::removeAllSpacesFromString(pars[i++]));
34
35 // The "composition" field is tokenized into (component, amount) pairs.
36 setComponentsFromString(pars[i++]);
37
38 // Human-readable description (kept verbatim).
39 description = pars[i++];
40
41 // Optical properties: serialized null fields are skipped at this input boundary.
42 getMaterialPropertyFromString(pars[i++], "photonEnergy");
43 getMaterialPropertyFromString(pars[i++], "indexOfRefraction");
44 getMaterialPropertyFromString(pars[i++], "absorptionLength");
45 getMaterialPropertyFromString(pars[i++], "reflectivity");
46 getMaterialPropertyFromString(pars[i++], "efficiency");
47
48 // Scintillation properties (spectra first, then scalars).
49 getMaterialPropertyFromString(pars[i++], "fastcomponent");
50 getMaterialPropertyFromString(pars[i++], "slowcomponent");
51
52 // assign_if_set increment index
53 scintillationyieldSet = assign_if_set(pars, i, scintillationyield);
54 resolutionscaleSet = assign_if_set(pars, i, resolutionscale);
55 fasttimeconstantSet = assign_if_set(pars, i, fasttimeconstant);
56 slowtimeconstantSet = assign_if_set(pars, i, slowtimeconstant);
57 yieldratioSet = assign_if_set(pars, i, yieldratio);
58 birksConstantSet = assign_if_set(pars, i, birksConstant);
59
60 // Other optical processes: the final vector load triggers cross-vector size validation.
61 getMaterialPropertyFromString(pars[i++], "rayleigh");
62 }
63}
64
65
66std::ostream &operator<<(std::ostream &stream, const GMaterial &gMat) {
67 stream << std::endl;
68 stream << " - Material: " << gMat.name << " in system " << gMat.system << ": " << std::endl;
69 stream << " Density: " << gMat.density << std::endl;
70 if (!gMat.components.empty()) {
71 stream << " Composition: " << std::endl;
72 for (unsigned m = 0; m < gMat.components.size(); m++) {
73 std::string quantity = gMat.amounts[m] > 1 ? " atoms " : " fractional mass";
74 stream << " ・ " << gMat.components[m] << quantity << " " << gMat.amounts[m] << std::endl;
75 }
76 }
77 stream << " Description: " << gMat.description << std::endl;
78 stream << std::endl;
79
80 return stream;
81}
82
83
84// sets components and amounts
85void GMaterial::setComponentsFromString(const std::string &composition) {
86 std::vector<std::string> allComponents = gutilities::getStringVectorFromString(composition);
87
88 // Interpret alternating tokens as (component, amount) pairs.
89 for (unsigned e = 0; e < allComponents.size() / 2; e++) {
90 components.push_back(allComponents[e * 2]);
91 amounts.push_back(stod(allComponents[e * 2 + 1]));
92 }
93}
94
95
96// load property from DB entry based on its name
97void GMaterial::getMaterialPropertyFromString(const std::string &parameter, const std::string &propertyName) {
98 // Nothing to do if the parameter is not assigned.
100 return;
101 }
102
103 // Tokenize the string and parse each component to a numeric value with units.
104 std::stringstream parameterComponents(parameter);
105
106 while (!parameterComponents.eof()) {
107 std::string component;
108 parameterComponents >> component;
109
110 // Removing whitespaces.
111 std::string trimmedComponent = gutilities::removeLeadingAndTrailingSpacesFromString(component);
112 // Non set properties
113
114 // Vector-valued properties append one value per token.
115 if (propertyName == "photonEnergy") {
116 photonEnergy.push_back(gutilities::getG4Number(trimmedComponent));
117 } else if (propertyName == "indexOfRefraction") {
118 indexOfRefraction.push_back(gutilities::getG4Number(trimmedComponent));
119 } else if (propertyName == "absorptionLength") {
120 absorptionLength.push_back(gutilities::getG4Number(trimmedComponent));
121 } else if (propertyName == "reflectivity") {
122 reflectivity.push_back(gutilities::getG4Number(trimmedComponent));
123 } else if (propertyName == "efficiency") {
124 efficiency.push_back(gutilities::getG4Number(trimmedComponent));
125 } else if (propertyName == "fastcomponent") {
126 fastcomponent.push_back(gutilities::getG4Number(trimmedComponent));
127 } else if (propertyName == "slowcomponent") {
128 slowcomponent.push_back(gutilities::getG4Number(trimmedComponent));
129 }
130 // Scalar-valued properties overwrite with the last parsed token.
131 else if (propertyName == "scintillationyield") {
132 scintillationyield = gutilities::getG4Number(trimmedComponent);
133 } else if (propertyName == "resolutionscale") {
134 resolutionscale = gutilities::getG4Number(trimmedComponent);
135 } else if (propertyName == "fasttimeconstant") {
136 fasttimeconstant = gutilities::getG4Number(trimmedComponent);
137 } else if (propertyName == "slowtimeconstant") {
138 slowtimeconstant = gutilities::getG4Number(trimmedComponent);
139 } else if (propertyName == "yieldratio") {
140 yieldratio = gutilities::getG4Number(trimmedComponent);
141 } else if (propertyName == "rayleigh") {
142 rayleigh.push_back(gutilities::getG4Number(trimmedComponent));
143 }
144 }
145
146 // Validation runs once, after all tokens for this property have been parsed.
147 // Rayleigh is loaded last, so every other optical vector is fully populated and can be
148 // cross-checked against photonEnergy here. (Running this inside the token loop fired on
149 // the very first rayleigh value, before the vector was complete.)
150 if (propertyName == "rayleigh") {
151 // Rayleigh is loaded last: at this point we can validate that all other optical vectors
152 // either are empty (not specified) or match the photonEnergy vector length.
153 //
154 // If they do not match, behavior is undefined because properties would be evaluated
155 // at inconsistent energy grids.
156 unsigned long photonEnergyVectorSize = photonEnergy.size();
157
158 if (!indexOfRefraction.empty() && indexOfRefraction.size() != photonEnergyVectorSize) {
160 "indexOfRefraction size ", indexOfRefraction.size(), " mismatch: photonEnergy has size ",
161 photonEnergyVectorSize);
162 }
163 if (!absorptionLength.empty() && absorptionLength.size() != photonEnergyVectorSize) {
165 "absorptionLength size ", absorptionLength.size(), " mismatch: photonEnergy has size ",
166 photonEnergyVectorSize);
167 }
168 if (!reflectivity.empty() && reflectivity.size() != photonEnergyVectorSize) {
170 "reflectivity size ", reflectivity.size(), " mismatch: photonEnergy has size ",
171 photonEnergyVectorSize);
172 }
173 if (!efficiency.empty() && efficiency.size() != photonEnergyVectorSize) {
175 "efficiency size ", efficiency.size(), " mismatch: photonEnergy has size ",
176 photonEnergyVectorSize);
177 }
178 if (!fastcomponent.empty() && fastcomponent.size() != photonEnergyVectorSize) {
180 "fastcomponent size ", fastcomponent.size(), " mismatch: photonEnergy has size ",
181 photonEnergyVectorSize);
182 }
183 if (!slowcomponent.empty() && slowcomponent.size() != photonEnergyVectorSize) {
185 "slowcomponent size ", slowcomponent.size(), " mismatch: photonEnergy has size ",
186 photonEnergyVectorSize);
187 }
188 if (!rayleigh.empty() && rayleigh.size() != photonEnergyVectorSize) {
190 "rayleigh size ", rayleigh.size(), " mismatch: photonEnergy has size ",
191 photonEnergyVectorSize);
192 }
193 }
194}
195
196
197bool GMaterial::assign_if_set(const std::vector<std::string> &pars, size_t &i, double &out) {
198 if (i >= pars.size()) return false;
199
200 const std::string trimmed =
202
203 std::string_view sv(trimmed);
204 if (gutilities::is_unset(sv)) return false;
205
206 try {
207 out = std::stod(trimmed);
208 return true;
209 } catch (const std::exception &) {
210 // Do not error here: malformed optional scalars are treated as "not set".
211 return false;
212 }
213}
GBase(const std::shared_ptr< GOptions > &gopt, std::string logger_name="")
std::shared_ptr< GLogger > log
GMaterial(const std::string &system, std::vector< std::string > pars, const std::shared_ptr< GLogger > &logger)
Construct a material from a serialized parameter list.
Definition gmaterial.cc:21
std::ostream & operator<<(std::ostream &stream, const GMaterial &gMat)
Definition gmaterial.cc:66
Conventions and shared constants for the detector-system module.
constexpr int ERR_GMATERIALOPTICALPROPERTYMISMATCH
constexpr int GMATERIALNUMBEROFPARS
Number of database parameters defining a gmaterial entry.
double getG4Number(const string &v, bool warnIfNotUnit=false)
bool is_unset(std::string_view s)
string removeLeadingAndTrailingSpacesFromString(const std::string &input)
vector< std::string > getStringVectorFromString(const std::string &input)