source: trunk/source/materials/src/G4MaterialPropertyVector.cc@ 1330

Last change on this file since 1330 was 1315, checked in by garnier, 15 years ago

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

File size: 9.4 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// $Id: G4MaterialPropertyVector.cc,v 1.18 2010/04/22 21:15:19 gum Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-cand-01 $
29//
30//
31////////////////////////////////////////////////////////////////////////
32// G4MaterialPropertyVector Class Implementation
33////////////////////////////////////////////////////////////////////////
34//
35// File: G4MaterialPropertyVector.cc
36// Version: 1.0
37// Created: 1996-02-08
38// Author: Juliet Armstrong
39// Updated: 1997-03-25 by Peter Gumplinger
40// > cosmetics (only)
41// mail: gum@triumf.ca
42//
43////////////////////////////////////////////////////////////////////////
44
45#include "G4MaterialPropertyVector.hh"
46
47// = operator
48// ----------
49//
50G4MaterialPropertyVector&
51G4MaterialPropertyVector::operator =(const G4MaterialPropertyVector& right)
52{
53 if (this == &right) { return *this; }
54
55 // clear the vector of current contents
56
57 MPV.clear();
58
59 // create an actual copy (instead of the shallow copy that the
60 // assignment operator defaults to for G4RWTPtrSortedVector)
61
62 NumEntries = 0;
63 CurrentEntry = -1;
64
65 for (G4int i = 0 ; i < right.NumEntries; i++)
66 {
67 G4MPVEntry *newElement = new G4MPVEntry(right.GetEntry(i));
68 MPV.push_back(newElement);
69 NumEntries++;
70 }
71
72 return *this;
73}
74
75/////////////////
76// Constructors
77/////////////////
78
79G4MaterialPropertyVector::
80G4MaterialPropertyVector(G4double *PhotonEnergies,
81 G4double *PropertyValues,
82 G4int NumElements)
83{
84 NumEntries = 0;
85 CurrentEntry = -1;
86
87 // create a vector filling it with the values
88 // from PhotonEnergies[] and PropertyValues[]
89
90 for(G4int i = 0; i < NumElements; i++)
91 {
92 AddElement(PhotonEnergies[i], PropertyValues[i]);
93 }
94}
95
96G4MaterialPropertyVector::
97G4MaterialPropertyVector(const G4MaterialPropertyVector &right)
98{
99 // create an actual copy (instead of the shallow copy that the
100 // assignment operator defaults to for G4RWTPtrSortedVector)
101
102 NumEntries = 0;
103 CurrentEntry = -1;
104
105 for (G4int i = 0 ; i < right.NumEntries; i++)
106 {
107 G4MPVEntry *newElement = new G4MPVEntry(right.GetEntry(i));
108 MPV.push_back(newElement);
109 NumEntries++;
110 }
111}
112
113////////////////
114// Destructor
115////////////////
116
117G4MaterialPropertyVector::~G4MaterialPropertyVector()
118{
119 MPV.clear();
120}
121
122////////////
123// Methods
124////////////
125
126void G4MaterialPropertyVector::RemoveElement(G4double aPhotonEnergy)
127{
128 G4MPVEntry *newElement;
129 G4MPVEntry *success=0;
130
131 newElement = new G4MPVEntry(aPhotonEnergy, DBL_MAX);
132
133 std::vector<G4MPVEntry*>::iterator i;
134 for (i = MPV.begin(); i != MPV.end(); i++)
135 {
136 if (**i == *newElement) { success = *i; break; }
137 }
138 // success = MPV.remove(newElement);
139
140 if(success == 0)
141 {
142 G4Exception("G4MaterialPropertyVector::RemoveElement()", "NotFound",
143 FatalException, "Element not found !");
144 return;
145 }
146 else
147 {
148 MPV.erase(i); // remove done here.
149 }
150
151 NumEntries--;
152}
153
154G4double G4MaterialPropertyVector::GetProperty(G4double aPhotonEnergy) const
155{
156 G4int left, right;
157 G4double ratio1, ratio2, pmright, pmleft, InterpolatedValue;
158
159 /////////////////////////
160 // Establish table range
161 /////////////////////////
162
163 G4double PMmin = MPV.front()->GetPhotonEnergy();
164 G4double minProp = MPV.front()->GetProperty();
165 G4double PMmax = MPV.back() ->GetPhotonEnergy();
166 G4double maxProp = MPV.back() ->GetProperty();
167
168 ///////////////////////////////////////////
169 // Does value fall outside range of table?
170 ///////////////////////////////////////////
171
172 if (aPhotonEnergy < PMmin)
173 {
174 G4Exception("G4MaterialPropertyVector::GetProperty()", "OutOfRange",
175 JustWarning, "Attempt to retrieve property below range !");
176 G4cout << " aPhotonEnergy = " << aPhotonEnergy/MeV
177 << " [MeV] PMmin = " << PMmin << " [MeV]" << G4endl;
178 return minProp;
179 }
180
181 if (aPhotonEnergy > PMmax)
182 {
183 G4Exception("G4MaterialPropertyVector::GetProperty()", "OutOfRange",
184 JustWarning, "Attempt to retrieve property above range !");
185 G4cout << " aPhotonEnergy = " << aPhotonEnergy/MeV
186 << " [MeV] PMmax = " << PMmax << " [MeV]" << G4endl;
187 return maxProp;
188 }
189
190 //////////////////////////////
191 // Return interpolated value
192 //////////////////////////////
193
194 GetAdjacentBins(aPhotonEnergy, &left, &right);
195 pmleft = MPV[left]->GetPhotonEnergy();
196 pmright = MPV[right]->GetPhotonEnergy();
197 ratio1 = (aPhotonEnergy-pmleft)/(pmright-pmleft);
198 ratio2 = 1 - ratio1;
199 InterpolatedValue = MPV[left]->GetProperty()*ratio2 +
200 MPV[right]->GetProperty()*ratio1;
201 return InterpolatedValue;
202}
203
204G4double G4MaterialPropertyVector::GetPhotonEnergy(G4double aProperty) const
205{
206 // ***NB***
207 // Assumes that the property is an increasing function of photon energy
208 // (e.g. refraction index)
209 // ***NB***
210 //
211 // Returns the photon energy corresponding to the property value passed in.
212 // If several photon energy values correspond to the value passed in, the
213 // function returns the first photon energy in the vector that corresponds
214 // to that value.
215
216 G4int left, right, mid;
217 G4double ratio1, ratio2, pright, pleft;
218
219 //////////////////////////
220 // Establish Table range
221 //////////////////////////
222
223 G4double PropMin = MPV.front()->GetProperty();
224 G4double PMmin = MPV.front()->GetPhotonEnergy();
225 G4double PropMax = MPV.back() ->GetProperty();
226 G4double PMmax = MPV.back() ->GetPhotonEnergy();
227
228 ///////////////////////////////////////////
229 // Does value fall outside range of table?
230 ///////////////////////////////////////////
231
232 if (aProperty < PropMin)
233 {
234 G4Exception("G4MaterialPropertyVector::GetPhotonEnergy()",
235 "OutOfRange", JustWarning,
236 "Attempt to retrieve photon energy out of range");
237 return PMmin;
238 }
239
240 if (aProperty > PropMax)
241 {
242 G4Exception("G4MaterialPropertyVector::GetPhotonEnergy()",
243 "OutOfRange", JustWarning,
244 "Attempt to retrieve photon energy out of range");
245 return PMmax;
246 }
247
248 //////////////////////////////
249 // Return interpolated value
250 //////////////////////////////
251
252 left = 0;
253 right = MPV.size(); // was .entries()
254
255 // find values in bins on either side of aProperty
256
257 do
258 {
259 mid = (left + right)/2;
260 if (MPV[mid]->GetProperty() == aProperty)
261 {
262 // Get first photon energy value in vector that
263 // corresponds to property value
264
265 while ((mid-1 >= 0) && (MPV[mid-1]->GetProperty() == aProperty))
266 {
267 mid--;
268 }
269 return MPV[mid]->GetPhotonEnergy();
270 }
271 if (MPV[mid]->GetProperty() < aProperty)
272 {
273 left = mid;
274 }
275 else
276 {
277 right = mid;
278 }
279 } while ((right - left) > 1);
280
281 pleft = MPV[left]->GetProperty();
282 pright = MPV[right]->GetProperty();
283 ratio1 = (aProperty - pleft) / (pright - pleft);
284 ratio2 = 1 - ratio1;
285
286 return (MPV[left]->GetPhotonEnergy()*ratio2 +
287 MPV[right]->GetPhotonEnergy()*ratio1);
288}
289
290void G4MaterialPropertyVector::DumpVector()
291{
292 if (MPV.empty())
293 {
294 G4Exception("G4MaterialPropertyVector::DumpVector()", "EmptyVector",
295 JustWarning, "Nothing to dump. Vector is empty !");
296 }
297 else
298 {
299 for (G4int i = 0; i < NumEntries; i++)
300 {
301 G4cout << "MPV["<< i << "]: ";
302 MPV[i]->DumpEntry();
303 }
304 G4cout << " Done DumpVector of " << NumEntries << " entries." << G4endl;
305 }
306}
307
308void G4MaterialPropertyVector::GetAdjacentBins(G4double aPhotonEnergy,
309 G4int *left, G4int *right) const
310{
311 G4int mid;
312
313 *left = 0;
314 *right = (MPV.size() - 1); // was .entries()
315
316 // find values in bins on either side of aPhotonEnergy
317
318 do
319 {
320 mid = (*left + *right)/2;
321 if (MPV[mid]->GetPhotonEnergy() <= aPhotonEnergy)
322 {
323 *left = mid;
324 }
325 else
326 {
327 *right = mid;
328 }
329 } while ((*right - *left) > 1);
330}
Note: See TracBrowser for help on using the repository browser.