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

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

update CVS release candidate geant4.9.3.01

File size: 9.7 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.17 2009/04/21 15:35:45 gcosmo Exp $
28// GEANT4 tag $Name: materials-V09-02-18 $
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  G4MPVEntry *target, *temp; 
157  G4int left, right;
158  G4double ratio1, ratio2, pmright, pmleft, InterpolatedValue;
159 
160  /////////////////////////
161  // Establish table range
162  /////////////////////////
163
164  G4double PMmin   = MPV.front()->GetPhotonEnergy(); 
165  G4double minProp = MPV.front()->GetProperty(); 
166  G4double PMmax   = MPV.back() ->GetPhotonEnergy();
167  G4double maxProp = MPV.back() ->GetProperty();
168
169  ///////////////////////////////////////////
170  // Does value fall outside range of table?
171  ///////////////////////////////////////////
172
173  if (aPhotonEnergy < PMmin) 
174  {
175    G4Exception("G4MaterialPropertyVector::GetProperty()", "OutOfRange",
176                JustWarning, "Attempt to retrieve property below range !");
177    return minProp; 
178  } 
179
180        if (aPhotonEnergy > PMmax)
181  {
182    G4Exception("G4MaterialPropertyVector::GetProperty()", "OutOfRange",
183                JustWarning, "Attempt to retrieve property above range !");
184    return maxProp;
185  } 
186 
187  target = new G4MPVEntry(aPhotonEnergy, 0.0);
188
189  temp = 0;
190  //temp = MPV.find(target);
191  std::vector<G4MPVEntry*>::const_iterator i;
192  for (i = MPV.begin(); i != MPV.end(); i++)
193  {
194    if (**i == *target)  { temp = *i; break; }
195  }
196  if (temp != 0)
197  {
198    ////////////////////////
199    // Return actual value
200    ////////////////////////
201
202    G4double retval = temp->GetProperty();
203    delete target;
204    return retval;
205  }
206  else
207  {
208    //////////////////////////////
209    // Return interpolated value
210    //////////////////////////////
211
212    GetAdjacentBins(aPhotonEnergy, &left, &right);
213
214    pmleft = MPV[left]->GetPhotonEnergy();
215    pmright = MPV[right]->GetPhotonEnergy();
216    ratio1 = (aPhotonEnergy-pmleft)/(pmright-pmleft);
217    ratio2 = 1 - ratio1;
218    InterpolatedValue = MPV[left]->GetProperty()*ratio2 + 
219                        MPV[right]->GetProperty()*ratio1;
220    delete target;
221    return InterpolatedValue;
222  } 
223}
224
225G4double G4MaterialPropertyVector::GetPhotonEnergy(G4double aProperty) const
226{
227  //         ***NB***
228  // Assumes that the property is an increasing function of photon energy
229  // (e.g. refraction index)
230  //         ***NB***
231  //
232  // Returns the photon energy corresponding to the property value passed in.
233  // If several photon energy values correspond to the value passed in, the
234  // function returns the first photon energy in the vector that corresponds
235  // to that value.
236
237  G4int left, right, mid;
238  G4double ratio1, ratio2, pright, pleft;
239
240  //////////////////////////
241  // Establish Table range
242  //////////////////////////
243
244  G4double PropMin = MPV.front()->GetProperty();
245  G4double PMmin   = MPV.front()->GetPhotonEnergy();
246  G4double PropMax = MPV.back() ->GetProperty();
247  G4double PMmax   = MPV.back() ->GetPhotonEnergy();
248
249  ///////////////////////////////////////////
250  // Does value fall outside range of table?
251  ///////////////////////////////////////////
252
253  if (aProperty < PropMin) 
254  {
255    G4Exception("G4MaterialPropertyVector::GetPhotonEnergy()",
256                "OutOfRange", JustWarning,
257                "Attempt to retrieve photon energy out of range");
258    return PMmin;
259  }
260
261  if (aProperty > PropMax)
262  {
263    G4Exception("G4MaterialPropertyVector::GetPhotonEnergy()",
264                "OutOfRange", JustWarning,
265                "Attempt to retrieve photon energy out of range");
266    return PMmax;
267  }
268
269  //////////////////////////////
270  // Return interpolated value
271  //////////////////////////////
272
273  left = 0;
274  right = MPV.size(); // was .entries()
275
276  // find values in bins on either side of aProperty
277 
278  do
279  {
280    mid = (left + right)/2;
281    if (MPV[mid]->GetProperty() == aProperty)
282    {
283      // Get first photon energy value in vector that
284      // corresponds to property value 
285
286      while ((mid-1 >= 0) && (MPV[mid-1]->GetProperty() == aProperty))
287      {
288          mid--;
289      }
290      return MPV[mid]->GetPhotonEnergy();
291    }
292    if (MPV[mid]->GetProperty() < aProperty)
293    {
294      left = mid;
295    }
296    else
297    {
298      right = mid;
299    }
300  } while ((right - left) > 1);
301
302  pleft = MPV[left]->GetProperty();
303  pright = MPV[right]->GetProperty();
304  ratio1 = (aProperty - pleft) / (pright - pleft);
305  ratio2 = 1 - ratio1;
306
307  return  (MPV[left]->GetPhotonEnergy()*ratio2 +
308           MPV[right]->GetPhotonEnergy()*ratio1);
309}
310
311void G4MaterialPropertyVector::DumpVector()
312{
313  if (MPV.empty())
314  {
315    G4Exception("G4MaterialPropertyVector::DumpVector()", "EmptyVector",
316                JustWarning, "Nothing to dump. Vector is empty !");
317  }
318  else
319  {
320    for (G4int i = 0; i < NumEntries; i++)
321    {
322      G4cout << "MPV["<< i << "]: ";
323      MPV[i]->DumpEntry();
324    }
325    G4cout << " Done DumpVector of " << NumEntries << " entries." << G4endl;
326  }
327} 
328
329void G4MaterialPropertyVector::GetAdjacentBins(G4double aPhotonEnergy,
330                                               G4int *left, G4int *right) const
331{
332  G4int mid;
333
334  *left = 0;
335  *right = (MPV.size() - 1); // was .entries()
336
337  // find values in bins on either side of aPhotonEnergy
338
339  do
340  {
341    mid = (*left + *right)/2;
342    if (MPV[mid]->GetPhotonEnergy() < aPhotonEnergy) 
343    {
344      *left = mid;
345    }
346    else
347    {
348      *right = mid;
349    }
350  } while ((*right - *left) > 1);
351}
Note: See TracBrowser for help on using the repository browser.