Skip to content

Commit 4bc0191

Browse files
authored
Merge pull request #158 from zhaozhiwen/fix/112-optical-validation-once
Validate optical-property vector sizes once, after parsing, not per token
2 parents af79a28 + 9de2ee3 commit 4bc0191

1 file changed

Lines changed: 47 additions & 44 deletions

File tree

gemc/gsystem/gmaterial.cc

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -139,51 +139,54 @@ void GMaterial::getMaterialPropertyFromString(const std::string &parameter, cons
139139
} else if (propertyName == "rayleigh") {
140140
rayleigh.push_back(gutilities::getG4Number(trimmedComponent));
141141
}
142+
}
142143

143-
// validation triggered by rayleigh
144-
if (propertyName == "rayleigh") {
145-
// Rayleigh is loaded last: at this point we can validate that all other optical vectors
146-
// either are empty (not specified) or match the photonEnergy vector length.
147-
//
148-
// If they do not match, behavior is undefined because properties would be evaluated
149-
// at inconsistent energy grids.
150-
unsigned long photonEnergyVectorSize = photonEnergy.size();
151-
152-
if (!indexOfRefraction.empty() && indexOfRefraction.size() != photonEnergyVectorSize) {
153-
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
154-
"indexOfRefraction size ", indexOfRefraction.size(), " mismatch: photonEnergy has size ",
155-
photonEnergyVectorSize);
156-
}
157-
if (!absorptionLength.empty() && absorptionLength.size() != photonEnergyVectorSize) {
158-
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
159-
"absorptionLength size ", absorptionLength.size(), " mismatch: photonEnergy has size ",
160-
photonEnergyVectorSize);
161-
}
162-
if (!reflectivity.empty() && reflectivity.size() != photonEnergyVectorSize) {
163-
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
164-
"reflectivity size ", reflectivity.size(), " mismatch: photonEnergy has size ",
165-
photonEnergyVectorSize);
166-
}
167-
if (!efficiency.empty() && efficiency.size() != photonEnergyVectorSize) {
168-
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
169-
"efficiency size ", efficiency.size(), " mismatch: photonEnergy has size ",
170-
photonEnergyVectorSize);
171-
}
172-
if (!fastcomponent.empty() && fastcomponent.size() != photonEnergyVectorSize) {
173-
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
174-
"fastcomponent size ", fastcomponent.size(), " mismatch: photonEnergy has size ",
175-
photonEnergyVectorSize);
176-
}
177-
if (!slowcomponent.empty() && slowcomponent.size() != photonEnergyVectorSize) {
178-
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
179-
"slowcomponent size ", slowcomponent.size(), " mismatch: photonEnergy has size ",
180-
photonEnergyVectorSize);
181-
}
182-
if (!rayleigh.empty() && rayleigh.size() != photonEnergyVectorSize) {
183-
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
184-
"rayleigh size ", rayleigh.size(), " mismatch: photonEnergy has size ",
185-
photonEnergyVectorSize);
186-
}
144+
// Validation runs once, after all tokens for this property have been parsed.
145+
// Rayleigh is loaded last, so every other optical vector is fully populated and can be
146+
// cross-checked against photonEnergy here. (Running this inside the token loop fired on
147+
// the very first rayleigh value, before the vector was complete.)
148+
if (propertyName == "rayleigh") {
149+
// Rayleigh is loaded last: at this point we can validate that all other optical vectors
150+
// either are empty (not specified) or match the photonEnergy vector length.
151+
//
152+
// If they do not match, behavior is undefined because properties would be evaluated
153+
// at inconsistent energy grids.
154+
unsigned long photonEnergyVectorSize = photonEnergy.size();
155+
156+
if (!indexOfRefraction.empty() && indexOfRefraction.size() != photonEnergyVectorSize) {
157+
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
158+
"indexOfRefraction size ", indexOfRefraction.size(), " mismatch: photonEnergy has size ",
159+
photonEnergyVectorSize);
160+
}
161+
if (!absorptionLength.empty() && absorptionLength.size() != photonEnergyVectorSize) {
162+
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
163+
"absorptionLength size ", absorptionLength.size(), " mismatch: photonEnergy has size ",
164+
photonEnergyVectorSize);
165+
}
166+
if (!reflectivity.empty() && reflectivity.size() != photonEnergyVectorSize) {
167+
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
168+
"reflectivity size ", reflectivity.size(), " mismatch: photonEnergy has size ",
169+
photonEnergyVectorSize);
170+
}
171+
if (!efficiency.empty() && efficiency.size() != photonEnergyVectorSize) {
172+
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
173+
"efficiency size ", efficiency.size(), " mismatch: photonEnergy has size ",
174+
photonEnergyVectorSize);
175+
}
176+
if (!fastcomponent.empty() && fastcomponent.size() != photonEnergyVectorSize) {
177+
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
178+
"fastcomponent size ", fastcomponent.size(), " mismatch: photonEnergy has size ",
179+
photonEnergyVectorSize);
180+
}
181+
if (!slowcomponent.empty() && slowcomponent.size() != photonEnergyVectorSize) {
182+
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
183+
"slowcomponent size ", slowcomponent.size(), " mismatch: photonEnergy has size ",
184+
photonEnergyVectorSize);
185+
}
186+
if (!rayleigh.empty() && rayleigh.size() != photonEnergyVectorSize) {
187+
log->error(ERR_GMATERIALOPTICALPROPERTYMISMATCH,
188+
"rayleigh size ", rayleigh.size(), " mismatch: photonEnergy has size ",
189+
photonEnergyVectorSize);
187190
}
188191
}
189192
}

0 commit comments

Comments
 (0)