Geant4  10.00.p02
tpia_particle.cc
Go to the documentation of this file.
1 /*
2 # <<BEGIN-copyright>>
3 # Copyright (c) 2010, Lawrence Livermore National Security, LLC.
4 # Produced at the Lawrence Livermore National Laboratory
5 # Written by Bret R. Beck, beck6@llnl.gov.
6 # CODE-461393
7 # All rights reserved.
8 #
9 # This file is part of GIDI. For details, see nuclear.llnl.gov.
10 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov.
11 #
12 # Redistribution and use in source and binary forms, with or without modification,
13 # are permitted provided that the following conditions are met:
14 #
15 # 1) Redistributions of source code must retain the above copyright notice,
16 # this list of conditions and the disclaimer below.
17 # 2) Redistributions in binary form must reproduce the above copyright notice,
18 # this list of conditions and the disclaimer (as noted below) in the
19 # documentation and/or other materials provided with the distribution.
20 # 3) Neither the name of the LLNS/LLNL nor the names of its contributors may be
21 # used to endorse or promote products derived from this software without
22 # specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
28 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 # <<END-copyright>>
35 */
36 #include <string.h>
37 #include <tpia_target.h>
38 #include "G4Types.hh"
39 
40 #if defined __cplusplus
41 namespace GIDI {
42 using namespace GIDI;
43 #endif
44 
47 static G4ThreadLocal tpia_particle **particleSortedList = NULL;
48 static G4ThreadLocal tpia_particle *particleList = NULL, *particleListEnd = NULL;
49 /*
50 ************************************************************
51 */
52 tpia_particle *tpia_particle_create( statusMessageReporting *smr ) {
53 
54  //tpia_particle *particle = xData_malloc2( smr, sizeof( tpia_particle ), 0, "particle" );
55  tpia_particle *particle = (tpia_particle*) xData_malloc2( smr, sizeof( tpia_particle ), 0, "particle" );
56 
57  if( particle == NULL ) return( NULL );
58  tpia_particle_initialize( smr, particle );
59  return( particle );
60 }
61 /*
62 ************************************************************
63 */
64 //int tpia_particle_initialize( statusMessageReporting *smr, tpia_particle *particle ) {
65 int tpia_particle_initialize( statusMessageReporting *, tpia_particle *particle ) {
66 
67  memset( particle, 0, sizeof( tpia_particle ) );
68  return( 0 );
69 }
70 /*
71 ************************************************************
72 */
73 tpia_particle *tpia_particle_free( statusMessageReporting *smr, tpia_particle *particle ) {
74 
75  int i, j;
76  tpia_particle **p;
77 
78  for( i = 0, p = particleSortedList; i < numberOfParticles; i++, p++ ) {
79  if( *p == particle ) {
80  numberOfParticles--;
81  for( j = i; j < numberOfParticles; j++, p++ ) *p = p[1];
82  break;
83  }
84  }
85  if( particle == particleListEnd ) particleListEnd = particle->prior;
86  if( particle == particleList ) particleList = particle->next;
87  if( particle->prior != NULL ) particle->prior->next = particle->next;
88  if( particle->next != NULL ) particle->next->prior = particle->prior;
89  tpia_particle_release( smr, particle );
90  xData_free( smr, particle );
91  return( NULL );
92 }
93 /*
94 ************************************************************
95 */
96 int tpia_particle_release( statusMessageReporting *smr, tpia_particle *particle ) {
97 
98  if( particle->spectralID != NULL ) tpi_spectralID_free( smr, particle->spectralID );
99  return( 0 );
100 }
101 /*
102 ************************************************************
103 */
104 int tpia_particle_freeInternalList( statusMessageReporting *smr ) {
105 
106  while( particleList != NULL ) tpia_particle_free( smr, particleList );
107  //particleSortedList = xData_free( smr, particleSortedList );
108  particleSortedList = (tpia_particle**) xData_free( smr, particleSortedList );
109  return( 0 );
110 }
111 /*
112 ************************************************************
113 */
114 tpia_particle *tpia_particle_getInternalID( statusMessageReporting *smr, const char * const name ) {
115 
116  int i, iCmp, min, mid, max, Z, A, m;
117  tpia_particle *particle;
118  char *EOP;
119 
120  iCmp = 0;
121  min = mid = 0;
122  max = numberOfParticles;
123  while( min != max ) {
124  mid = ( min + max ) / 2;
125  iCmp = strcmp( name, particleSortedList[mid]->name );
126  if( iCmp == 0 ) return( particleSortedList[mid] );
127  if( iCmp < 0 ) {
128  max = mid - 1;
129  if( mid == 0 ) max = 0; }
130  else {
131  min = mid + 1;
132  if( min > max ) min = max;
133  }
134  }
135  mid = min;
136  if( numberOfParticles > 0 ) {
137  iCmp = strcmp( name, particleSortedList[mid]->name );
138  if( iCmp == 0 ) return( particleSortedList[mid] );
139  if( ( iCmp < 0 ) && ( mid != 0 ) ) {
140  mid--;
141  iCmp = strcmp( name, particleSortedList[mid]->name );
142  }
143  }
144 
145  if( ( particle = tpia_particle_create( smr ) ) == NULL ) return( NULL );
146  if( ( particle->spectralID = tpi_spectralID_parse( smr, name, &(EOP) ) ) == NULL ) return( tpia_particle_free( smr, particle ) );
147  particle->name = particle->spectralID->name;
148  if( tpia_miscNameToZAm( smr, particle->name, &Z, &A, &m ) != 0 ) return( tpia_particle_free( smr, particle ) );
149  particle->prior = NULL;
150  particle->next = NULL;
151  particle->Z = Z;
152  particle->A = A;
153  particle->m = m;
154  particle->mass = tpia_particleMass_AMU( smr, particle->name );
155  if( !smr_isOk( smr ) ) return( tpia_particle_free( smr, particle ) );
156  particle->fullMass_MeV = tpia_AMU2MeV * particle->mass + particle->spectralID->level;
157 
158  if( sizeOfParticleSortedList < ( numberOfParticles + 1 ) ) {
159  if( sizeOfParticleSortedList == 0 ) {
161  else {
163  }
164  //if( ( particleSortedList = xData_realloc2( smr, particleSortedList, sizeOfParticleSortedList * sizeof( tpia_particle * ),
165  if( ( particleSortedList = (tpia_particle** ) xData_realloc2( smr, particleSortedList, sizeOfParticleSortedList * sizeof( tpia_particle * ),
166  "particleSortedList" ) ) == NULL ) return( tpia_particle_free( smr, particle ) );
167  }
168 
169  if( particleList == NULL ) {
170  particle->ordinal = 0;
171  particleListEnd = particleList = particle; }
172  else {
173  particle->ordinal = particleListEnd->ordinal + 1;
174  particle->prior = particleListEnd;
175  particleListEnd->next = particle;
176  particleListEnd = particle;
177  }
178 
179  if( ( mid != 0 ) || ( iCmp > 0 ) ) mid++;
180  for( i = numberOfParticles; i > mid; i-- ) particleSortedList[i] = particleSortedList[i-1];
181  particleSortedList[mid] = particle;
182  numberOfParticles++;
183 
184  return( particle );
185 }
186 /*
187 ************************************************************
188 */
189 //int tpia_particle_printInternalSortedList( statusMessageReporting *smr ) {
190 int tpia_particle_printInternalSortedList( statusMessageReporting * ) {
191 
192  int i;
193 
194  for( i = 0; i < numberOfParticles; i++ ) printf( "%s\n", particleSortedList[i]->name );
195  return( 0 );
196 }
197 
198 #if defined __cplusplus
199 }
200 #endif
static G4ThreadLocal int incrementalSizeOfList
int tpia_particle_freeInternalList(statusMessageReporting *smr)
G4String name
Definition: TRTMaterials.hh:40
static G4ThreadLocal tpia_particle * particleListEnd
int tpia_particle_printInternalSortedList(statusMessageReporting *)
#define G4ThreadLocal
Definition: tls.hh:52
tpi_spectralID * tpi_spectralID_parse(statusMessageReporting *smr, char const *str, char **EOP)
Definition: tpi_IDs.cc:218
static G4ThreadLocal tpia_particle ** particleSortedList
static G4ThreadLocal tpia_particle * particleList
int smr_isOk(statusMessageReporting *smr)
int tpia_miscNameToZAm(statusMessageReporting *smr, const char *name, int *Z, int *A, int *m)
Definition: tpia_misc.cc:105
static G4ThreadLocal int sizeOfParticleSortedList
static G4ThreadLocal int numberOfParticles
void * tpi_spectralID_free(statusMessageReporting *smr, tpi_spectralID *spectralID)
Definition: tpi_IDs.cc:191
static const G4double A[nN]
int tpia_particle_initialize(statusMessageReporting *, tpia_particle *particle)
T max(const T t1, const T t2)
brief Return the largest of the two arguments
void * xData_free(statusMessageReporting *, void *p)
Definition: xDataMisc.cc:89
static G4ThreadLocal int initialSizeOfList
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
tpia_particle * tpia_particle_create(statusMessageReporting *smr)
static const double m
Definition: G4SIunits.hh:110
int tpia_particle_release(statusMessageReporting *smr, tpia_particle *particle)
double tpia_particleMass_AMU(statusMessageReporting *smr, const char *name)
Definition: tpia_mass.cc:887
tpia_particle * tpia_particle_free(statusMessageReporting *smr, tpia_particle *particle)
tpia_particle * tpia_particle_getInternalID(statusMessageReporting *smr, const char *const name)