BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSColourFromMaterial.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSColours.hh"
19#include "BDSColourFromMaterial.hh"
20#include "BDSUtilities.hh"
21
22#include "G4Colour.hh"
23#include "G4DataVector.hh"
24#include "G4Material.hh"
25#include "G4PhysicsFreeVector.hh"
26#include "G4String.hh"
27#include "G4Types.hh"
28
29#include "CLHEP/Units/SystemOfUnits.h"
30
31#include <map>
32#include <vector>
33
34BDSColourFromMaterial* BDSColourFromMaterial::instance = nullptr;
35
37{
38 if (!instance)
39 {instance = new BDSColourFromMaterial();}
40 return instance;
41}
42
43BDSColourFromMaterial::~BDSColourFromMaterial()
44{
45 instance = nullptr;
46 delete generalDensity;
47}
48
50{
52 defines["air"] = c->GetColour("air:240 240 240 0.05");
53 defines["airbdsim"] = defines["air"];
54 defines["boron"] = c->GetColour("reallyreallydarkgrey");
55 defines["brass"] = c->GetColour("LHCcoil");
56 defines["carbon"] = c->GetColour("reallyreallydarkgrey");
57 defines["c"] = defines["carbon"];
58 defines["graphite"] = defines["carbon"];
59 defines["chlorine"] = c->GetColour("yellow");
60 defines["cl"] = defines["chlorine"];
61 defines["concrete"] = c->GetColour("tunnel");
62 defines["lhcconcrete"] = defines["concrete"];
63 defines["copper"] = c->GetColour("coil");
64 defines["cu"] = defines["copper"];
65 defines["cuh2o"] = defines["copper"];
66 defines["iron"] = c->GetColour("iron");
67 defines["fe"] = defines["iron"];
68 defines["gold"] = c->GetColour("gold:220 176 71");
69 defines["au"] = defines["gold"];
70 defines["kapton"] = c->GetColour("kapton");
71 defines["lead"] = c->GetColour("lead");
72 defines["pb"] = defines["lead"];
73 defines["lyso"] = c->GetColour("lyso: 230 210 235 0.3");
74 defines["lso"] = defines["lyso"];
75 defines["yso"] = defines["lyso"];
76 defines["lysoce"] = defines["lyso"];
77 defines["marble"] = c->GetColour("marble:228 228 228 1.0");
78 defines["stainlesssteel"] = c->GetColour("beampipe");
79 defines["stainless-steel"] = defines["stainlesssteel"];
80 defines["sulphur"] = c->GetColour("yellow");
81 defines["s"] = defines["sulphur"];
82 defines["vacuum"] = defines["air"];
83 defines["cucrzr"] = defines["copper"];
84
85 // for older versions of Geant4 < V11 we have to use G4DataVector which
86 // can't use list initialisation. In V11 onwards, G4PhysicsFreeVector
87 // changed to take std::vector<G4double> as arguments but G4DataVector
88 // inherits this so it's ok. Order must be ascending.
89 G4DataVector densities(5);
90 for (auto v : {1e-4, 0.01, 0.1, 1.0, 1e2})
91 {densities.emplace_back(v * CLHEP::g / CLHEP::cm3);}
92 G4DataVector values(5);
93 for (auto v : {210, 180, 150, 120, 100})
94 {values.emplace_back(v);}
95 generalDensity = new G4PhysicsFreeVector(densities, values);
96}
97
98G4Colour* BDSColourFromMaterial::GetColour(const G4Material* material,
99 const G4String& prefixToStripFromName)
100{
101 G4String materialName = material->GetName();
102 materialName = BDS::LowerCase(materialName);
103
104 // strip off g4 so we don't have to define duplicates of everything
105 std::string toErase = "g4_";
106 size_t pos = materialName.find(toErase);
107 if (pos != std::string::npos)
108 {materialName.erase(pos, toErase.length());}
109
110 // note, not all material are prefixed by preprocessor and might not contain that prefix string
111 G4String prefixToStripFromNameLower = BDS::LowerCase(prefixToStripFromName);
112 if (!prefixToStripFromName.empty() && BDS::StrContains(materialName, prefixToStripFromNameLower))
113 {materialName.erase(0, prefixToStripFromName.size());}
114
115 auto search = defines.find(materialName);
116 if (search != defines.end())
117 {return search->second;}
118 else
119 {
120 G4double alpha = 1;
121 G4State state = material->GetState();
122 switch (state)
123 {
124 case G4State::kStateGas:
125 {alpha = 0.05; break;}
126 case G4State::kStateSolid:
127 {alpha = 1.0; break;}
128 case G4State::kStateLiquid:
129 {alpha = 0.5; break;}
130 default:
131 {alpha = 1.0; break;}
132 }
133 G4double density = material->GetDensity();
134 G4double value = generalDensity->Value(density);
135 G4String vs = G4String(std::to_string(value));
136 G4Colour* result = BDSColours::Instance()->GetColour(materialName + ":" +
137 vs + " " +
138 vs + " " +
139 vs + " " +
140 std::to_string(alpha));
141 defines[materialName] = result; // cache it as we'd always produce the same result
142 return result;
143 }
144}
145
146G4Colour* BDSColourFromMaterial::GetColourWithDefault(const G4Material* material,
147 G4Colour* defaultIn) const
148{
149 G4String materialName = material->GetName();
150 materialName = BDS::LowerCase(materialName);
151
152 // strip off g4 so we don't have to define duplicates of everything
153 std::string toErase = "g4_";
154 size_t pos = materialName.find(toErase);
155 if (pos != std::string::npos)
156 {materialName.erase(pos, toErase.length());}
157
158 auto search = defines.find(materialName);
159 return search != defines.end() ? search->second : defaultIn;
160}
Automatic colours from materials.
std::map< G4String, G4Colour * > defines
Specially defined material colours.
static BDSColourFromMaterial * Instance()
Singleton pattern.
G4Colour * GetColour(const G4Material *material, const G4String &prefixToStripFromName="")
Get colour from name.
BDSColourFromMaterial()
Private constructor as singleton.
G4Colour * GetColourWithDefault(const G4Material *material, G4Colour *defaultIn) const
Get colour from name - if not found return the supplied default.
Colour class that holds all colours used in BDSIM.
Definition BDSColours.hh:33
G4Colour * GetColour(G4double red, G4double green, G4double blue, G4double alpha=1)
Get a cached anonymous colour by values.
static BDSColours * Instance()
singleton pattern
Definition BDSColours.cc:32
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.