BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSPhysicsVectorLinear.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 "BDSDebug.hh"
19#include "BDSException.hh"
20#include "BDSPhysicsVectorLinear.hh"
21
22#include "G4Types.hh"
23
24#include <algorithm>
25#include <iterator>
26#include <vector>
27
28
29BDSPhysicsVectorLinear::BDSPhysicsVectorLinear(const std::vector<G4double>& kineticEnergyIn,
30 const std::vector<G4double>& valuesIn):
31 kineticEnergy(kineticEnergyIn),
32 values(valuesIn),
33 eKMin(kineticEnergy[0]),
34 eKMax(kineticEnergy.back()),
35 eKMinValue(values[0]),
36 eKMaxValue(values.back())
37{
38 if (kineticEnergy.size() != valuesIn.size())
39 {throw BDSException(__METHOD_NAME__, "kinetic energy vector and values vector mismatch in size.");}
40 if (kineticEnergy.size() < 2)
41 {throw BDSException(__METHOD_NAME__, "must be at least 2 points in Ek vs value vectors.");}
42}
43
44G4double BDSPhysicsVectorLinear::Value(G4double eK) const
45{
46 if (eK < eKMin)
47 {return eKMinValue;}
48 else if (eK > eKMax)
49 {return eKMaxValue;}
50 else
51 {
52 auto indexGreaterThanOrEqual = std::lower_bound(kineticEnergy.begin(), kineticEnergy.end(), eK);
53 G4int index2 = (G4int)std::distance(kineticEnergy.begin(), indexGreaterThanOrEqual);
54 G4int index1 = index2 - 1;
55 G4double dx = (eK - kineticEnergy[index1]) / (kineticEnergy[index2] - kineticEnergy[index1]);
56 G4double result = values[index1]*(1.0 - dx) + values[index2]*dx;
57 return result;
58 }
59}
General exception with possible name of object and message.
G4double Value(G4double eK) const
Get the linearly interpolated value for a given kinetic energy.