g4system
Loading...
Searching...
No Matches
g4world.cc
Go to the documentation of this file.
1// g4world.cc : implementation of the Geant4 world builder and material initialization.
12// gemc
13#include "g4world.h"
14#include "gfactory.h"
15
16// g4system
17#include "g4system_options.h"
18#include "g4systemConventions.h"
19#include "gsystemConventions.h"
22
23
24// c++
25#include <vector>
26
27G4World::G4World(const GWorld *gworld, const std::shared_ptr<GOptions> &gopts)
28 : GBase(gopts, G4SYSTEM_LOGGER) {
29 auto gsystemMap = gworld->getSystemsMap();
30
31 // Phase 1: create and initialize a Geant4 object factory for each system.
32 // The factory provides solid/logical/physical creation for volumes in that system.
33 createG4SystemFactory(gopts,
34 gsystemMap,
35 gopts->getScalarString("useBackupMaterial"),
36 gopts->getScalarInt("check_overlaps")
37 );
38
39 // Phase 2: build all materials across systems, resolving dependencies iteratively.
40 buildMaterials(gsystemMap);
41
42 // Phase 3: ensure common isotopes/elements/materials exist (used by typical configurations).
43 buildDefaultMaterialsElementsAndIsotopes();
44
45 // Phase 4: build volumes. Some volumes depend on mothers that may not exist yet,
46 // so we iterate until the remaining list becomes empty or the dependency resolution stalls.
47 std::vector<GVolume *> thisIterationRemainingVolumes;
48 unsigned long previousRemainingVolumes = 0;
49
50 do {
51 thisIterationRemainingVolumes.clear();
52
53 // Loop over all systems and attempt to build all volumes in each system.
54 for (auto &[systemName, gsystem]: *gsystemMap) {
55 std::string g4Factory = g4FactoryNameFromSystemFactory(gsystem->getFactoryName());
56 auto objectsFactory = get_factory(g4Factory);
57
58 for (auto &[volumeName, gvolumePtr]: gsystem->getGVolumesMap()) {
59 auto *gvolume = gvolumePtr.get();
60
61 // Try to build; if dependencies are missing, remember it for the next iteration.
62 if (!build_g4volume(gvolume, objectsFactory)) {
63 // Only track volumes that are meant to exist; nonexistent volumes are skipped quietly.
64 if (gvolume->getExistence()) {
65 log->info(2, " >> adding volumeName <", volumeName, "> to the list of remaining volumes");
66 thisIterationRemainingVolumes.push_back(gvolume);
67 }
68 }
69 }
70
71 // Diagnostic listing of the volumes that could not be built due to missing mothers.
72 if (!thisIterationRemainingVolumes.empty()) {
73 log->info(2, "G4World: ", systemName, " : ",
74 thisIterationRemainingVolumes.size(),
75 " remaining motherless g4volumes to be built:");
76 for (auto *gvolumeLeft: thisIterationRemainingVolumes) {
77 log->info(2, "G4World: ", gvolumeLeft->getName(),
78 " with mother <", gvolumeLeft->getG4MotherName(), "> ");
79 }
80 }
81 }
82
83 // Dependency-stall detection: error only when the remaining count fails to strictly
84 // decrease across iterations (a strictly smaller count means progress was made).
85 if (previousRemainingVolumes != 0 && !thisIterationRemainingVolumes.empty() &&
86 thisIterationRemainingVolumes.size() >= previousRemainingVolumes) {
87 for (auto *gvolumeLeft: thisIterationRemainingVolumes) {
88 log->warning(" >> ", gvolumeLeft->getName(),
89 " with mother <", gvolumeLeft->getG4MotherName(), "> not built");
90 }
92 "dependencies are not being resolved: their number should diminish. "
93 "Above are the outstanding gvolumes");
94 }
95 previousRemainingVolumes = thisIterationRemainingVolumes.size();
96 } while (!thisIterationRemainingVolumes.empty());
97
98 // Phase 5: build optical surfaces (mirrors), now that all logical/physical volumes exist.
99 buildOpticalSurfaces(gsystemMap);
100
101 // Optional diagnostic output: list known materials from the Geant4 NIST manager.
102 if (gopts->getSwitch("showPredefinedMaterials")) { G4NistManager::Instance()->ListMaterials("all"); }
103
104 // Optional diagnostic output: print materials used in the simulation.
105 if (gopts->getSwitch("printSystemsMaterials")) {
106 auto matTable = (G4MaterialTable *) G4Material::GetMaterialTable();
107 for (auto thisMat: *matTable) {
108 log->info(0, 2, "G4World: GEMC Material: <", thisMat->GetName(), ">, density: ",
109 thisMat->GetDensity() / (CLHEP::g / CLHEP::cm3), "g/cm3");
110
111 // Print each component; negative/zero values mean "fractional mass", positive means "number of atoms".
112 for (auto &[material, component]: thisMat->GetMatComponents()) {
113 if (component > 0.0) {
114 log->info(0, "element", material->GetName(), "number of atoms: ", component);
115 } else { log->info(0, "element", material->GetName(), "fractional mass: ", component); }
116 }
117 }
118 }
119}
120
121/*──────────────────────── look-ups ──────────────────────────*/
122
123const G4Volume *G4World::getG4Volume(const std::string &volumeName) const {
124 auto it = g4volumesMap.find(volumeName);
125 return (it != g4volumesMap.end()) ? it->second : nullptr;
126}
127
128void G4World::setFieldManagerForVolume(const std::string &volumeName,
129 G4FieldManager *fm,
130 bool forceToAllDaughters) {
131 auto it = g4volumesMap.find(volumeName);
132 if (it != g4volumesMap.end()) it->second->setFieldManager(fm, forceToAllDaughters);
133}
134
135// ---- g4FactoryNameFromSystemFactory -----------------------------------------------------------
136std::string G4World::g4FactoryNameFromSystemFactory(const std::string &factory) const {
137 // Map GEMC system factory labels to g4system object factory labels.
138 if (factory == GSYSTEMASCIIFACTORYLABEL ||
139 factory == GSYSTEMSQLITETFACTORYLABEL ||
140 factory == GSYSTEMMYSQLTFACTORYLABEL) { return G4SYSTEMNATFACTORY; } else if (
141 factory == GSYSTEMCADTFACTORYLABEL) { return G4SYSTEMCADFACTORY; } else {
143 "gsystemFactory factory <", factory, "> is not mapped to any G4SystemFactory");
144 }
145}
146
147void G4World::createG4SystemFactory(const std::shared_ptr<GOptions> &gopts,
148 SystemMap *gsystemsMap,
149 const std::string &backup_material,
150 int check_overlaps) {
151 // Instantiate a manager used to register and create factories.
152 GManager manager(gopts);
153
154 // Creating the native factory no matter what (it is the default for ASCII/SQLite/MySQL systems).
155 log->info(2, "G4World: registering default factory <", G4SYSTEMNATFACTORY, ">");
156 manager.RegisterObjectFactory<G4NativeSystemFactory>(G4SYSTEMNATFACTORY, gopts);
157
158 // Register factories based on the system factory label, then create/initialize them lazily.
159 for (auto &[gsystemName, gsystem]: *gsystemsMap) {
160 std::string factory = gsystem->getFactoryName();
161 std::string g4Factory = g4FactoryNameFromSystemFactory(factory);
162
163 log->info(2, "G4World: creating factory <", g4Factory, "> to for system <", gsystemName, ">");
164
165 // Register needed factory types:
166 // this will always be false for the default native label because it was registered above.
167 if (factory == GSYSTEMASCIIFACTORYLABEL || factory == GSYSTEMSQLITETFACTORYLABEL ||
168 factory == GSYSTEMMYSQLTFACTORYLABEL) {
169 if (g4systemFactory.find(g4Factory) == g4systemFactory.end()) {
170 manager.RegisterObjectFactory<G4NativeSystemFactory>(g4Factory, gopts);
171 }
172 } else if (factory == GSYSTEMCADTFACTORYLABEL) {
173 if (g4systemFactory.find(GSYSTEMCADTFACTORYLABEL) == g4systemFactory.end()) {
174 manager.RegisterObjectFactory<G4CadSystemFactory>(g4Factory, gopts);
175 }
176 }
177
178 // Create and initialize the concrete factory instance once per label.
179 if (g4systemFactory.find(g4Factory) == g4systemFactory.end()) {
180 g4systemFactory[g4Factory] = manager.CreateObject<G4ObjectsFactory>(g4Factory);
181 g4systemFactory[g4Factory]->initialize_context(check_overlaps, backup_material);
182 }
183 }
184}
185
186void G4World::buildMaterials(SystemMap *system_map) {
187 // Build materials across all systems. Some materials may depend on other materials/elements,
188 // so we iterate until all dependencies are resolved or the resolution stalls.
189 std::vector<GMaterial *> thisIterationRemainingMaterials;
190 unsigned long previousRemainingMaterials = 0;
191 do {
192 thisIterationRemainingMaterials.clear();
193
194 for (const auto &[systemName, system]: *system_map) {
195 // Loop over the material map in each system and attempt to build each material.
196 for (const auto &[gmaterialName, gmaterialPtr]: system->getGMaterialMap()) {
197 if (createG4Material(gmaterialPtr) == false) {
198 thisIterationRemainingMaterials.push_back(gmaterialPtr.get());
199 }
200 }
201 }
202
203 // Dependency-stall detection for material building: error only when the remaining
204 // count fails to strictly decrease across iterations.
205 if (previousRemainingMaterials != 0 && !thisIterationRemainingMaterials.empty() &&
206 thisIterationRemainingMaterials.size() >= previousRemainingMaterials) {
207 for (auto &gmaterialLeft: thisIterationRemainingMaterials) { log->warning(gmaterialLeft->getName()); }
209 "Dependencies are not being resolved: their number should diminish. Above are the Outstanding gmaterials");
210 }
211 previousRemainingMaterials = thisIterationRemainingMaterials.size();
212 } while (!thisIterationRemainingMaterials.empty());
213}
214
215bool G4World::build_g4volume(const GVolume *s, G4ObjectsFactory *objectsFactory) {
216 log->info(2, "G4World: using factory <", objectsFactory->className(),
217 "> to build g4volume <", s->getG4Name(), ">");
218
219 return objectsFactory->build_g4volume(s, &g4volumesMap);
220}
Factory that converts CAD files (PLY / STL) into Geant4 tessellated solids via CADMesh.
Builds a tessellated solid from CAD files using CADMesh.
Implements solid creation for Geant4 CSG primitives and validates constructor parameter counts.
Base class orchestrating the conversion of a GVolume into a Geant4 representation.
bool build_g4volume(const GVolume *s, std::unordered_map< std::string, G4Volume * > *g4s)
Build (or retrieve) solid, logical, and physical volumes for a given GVolume.
virtual std::string_view className() const =0
Short, human-readable factory name for logging.
Convenience container holding a Geant4 solid, logical, and physical volume.
Definition g4volume.h:52
G4World(const GWorld *gworld, const std::shared_ptr< GOptions > &gopts)
Construct and build the Geant4 world from a GEMC world.
Definition g4world.cc:27
void setFieldManagerForVolume(const std::string &volumeName, G4FieldManager *fm, bool forceToAllDaughters)
Attach a G4FieldManager to the logical volume of a named volume.
Definition g4world.cc:128
G4ObjectsFactory * get_factory(const std::string &factoryName)
Retrieve a registered factory by name.
Definition g4world.h:128
const G4Volume * getG4Volume(const std::string &volumeName) const
Return the G4Volume wrapper for a volume name.
Definition g4world.cc:123
std::shared_ptr< GLogger > log
void warning(Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
std::string getG4Name() const
SystemMap * getSystemsMap() const
Factory that builds Geant4 native primitive solids (G4Box, G4Cons, G4Trap, ...) from GEMC GVolume rec...
Conventions, labels, and error codes used by the g4system geometry/material layer.
#define ERR_G4SYSTEMFACTORYNOTFOUND
A required Geant4 system factory was not found/mapped.
#define ERR_G4DEPENDENCIESNOTSOLVED
Geometry/material dependencies could not be resolved.
#define G4SYSTEMNATFACTORY
#define G4SYSTEMCADFACTORY
Option definitions for the g4system module (geometry/material construction layer).
constexpr const char * G4SYSTEM_LOGGER
Logger name used by the module-level builder (e.g. G4World).
High-level builder that turns a GEMC world description into Geant4 geometry.
std::map< std::string, SystemPtr > SystemMap