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