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