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) Royal Holloway,
3University of London 2001 - 2023.
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["cuh2o"] = defines["copper"];
67 defines["iron"] = c->GetColour("iron");
68 defines["fe"] = defines["iron"];
69 defines["gold"] = c->GetColour("gold:220 176 71");
70 defines["au"] = defines["gold"];
71 defines["kapton"] = c->GetColour("kapton");
72 defines["lead"] = c->GetColour("lead");
73 defines["pb"] = defines["lead"];
74 defines["lyso"] = c->GetColour("lyso: 230 210 235 0.3");
75 defines["lso"] = defines["lyso"];
76 defines["yso"] = defines["lyso"];
77 defines["lysoce"] = defines["lyso"];
78 defines["marble"] = c->GetColour("marble:228 228 228 1.0");
79 defines["stainlesssteel"] = c->GetColour("beampipe");
80 defines["stainless-steel"] = defines["stainlesssteel"];
81 defines["sulphur"] = c->GetColour("yellow");
82 defines["s"] = defines["sulphur"];
83 defines["vacuum"] = defines["air"];
84 defines["water"] = c->GetColour("water:0 102 204 0.5");
85
86 // for older versions of Geant4 < V11 we have to use G4DataVector which
87 // can't use list initialisation. In V11 onwards, G4PhysicsFreeVector
88 // changed to take std::vector<G4double> as arguments but G4DataVector
89 // inherits this so it's ok. Order must be ascending.
90 G4DataVector densities(5);
91 for (auto v : {1e-4, 0.01, 0.1, 1.0, 1e2})
92 {densities.emplace_back(v * CLHEP::g / CLHEP::cm3);}
93 G4DataVector values(5);
94 for (auto v : {210, 180, 150, 120, 100})
95 {values.emplace_back(v);}
96 generalDensity = new G4PhysicsFreeVector(densities, values);
97}
98
99G4Colour* BDSColourFromMaterial::GetColour(const G4Material* material,
100 const G4String& prefixToStripFromName)
101{
102 G4String materialName = material->GetName();
103 materialName = BDS::LowerCase(materialName);
104 //G4cout << "original material name " << materialName << G4endl;
105
106 // strip off g4 so we don't have to define duplicates of everything
107 std::string toErase = "g4_";
108 size_t pos = materialName.find(toErase);
109 if (pos != std::string::npos)
110 {materialName.erase(pos, toErase.length());}
111
112 // note, not all material are prefixed by preprocessor and might not contain that prefix string
113 G4String prefixToStripFromNameLower = BDS::LowerCase(prefixToStripFromName);
114 if (!prefixToStripFromName.empty() && BDS::StrContains(materialName, prefixToStripFromNameLower))
115 {materialName.erase(0, prefixToStripFromName.size());}
116
117 auto search = defines.find(materialName);
118 if (search != defines.end())
119 {return search->second;}
120 else
121 {
122 G4double alpha = 1;
123 G4State state = material->GetState();
124 switch (state)
125 {
126 case G4State::kStateGas:
127 {alpha = 0.05; break;}
128 case G4State::kStateSolid:
129 {alpha = 1.0; break;}
130 case G4State::kStateLiquid:
131 {alpha = 0.5; break;}
132 default:
133 {alpha = 1.0; break;}
134 }
135 G4double density = material->GetDensity();
136 G4double value = generalDensity->Value(density);
137 G4String vs = G4String(std::to_string(value));
138 G4Colour* result = BDSColours::Instance()->GetColour(materialName + ":" +
139 vs + " " +
140 vs + " " +
141 vs + " " +
142 std::to_string(alpha));
143 defines[materialName] = result; // cache it as we'd always produce the same result
144 return result;
145 }
146}
147
148G4Colour* BDSColourFromMaterial::GetColourWithDefault(const G4Material* material,
149 G4Colour* defaultIn) const
150{
151 G4String materialName = material->GetName();
152 materialName = BDS::LowerCase(materialName);
153
154 // strip off g4 so we don't have to define duplicates of everything
155 std::string toErase = "g4_";
156 size_t pos = materialName.find(toErase);
157 if (pos != std::string::npos)
158 {materialName.erase(pos, toErase.length());}
159
160 auto search = defines.find(materialName);
161 return search != defines.end() ? search->second : defaultIn;
162}
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:204
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
Definition: BDSUtilities.cc:66
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.