Geant4 Cross Reference |
1 /* 1 /* 2 # <<BEGIN-copyright>> 2 # <<BEGIN-copyright>> 3 # <<END-copyright>> 3 # <<END-copyright>> 4 */ 4 */ 5 #include <string.h> 5 #include <string.h> 6 #include "MCGIDI.h" 6 #include "MCGIDI.h" 7 7 8 #if defined __cplusplus 8 #if defined __cplusplus 9 namespace GIDI { 9 namespace GIDI { 10 using namespace GIDI; 10 using namespace GIDI; 11 #endif 11 #endif 12 12 13 static int initialSizeOfList = 1000, increment 13 static int initialSizeOfList = 1000, incrementalSizeOfList = 1000; 14 static int numberOfParticles = 0, sizeOfPartic 14 static int numberOfParticles = 0, sizeOfParticleSortedList = 0; 15 static MCGIDI_particle **particleSortedList = 15 static MCGIDI_particle **particleSortedList = NULL; 16 static MCGIDI_particle *particleList = NULL, * 16 static MCGIDI_particle *particleList = NULL, *particleListEnd = NULL; 17 /* 17 /* 18 ********************************************** 18 ************************************************************ 19 */ 19 */ 20 MCGIDI_particle *MCGIDI_particle_new( statusMe 20 MCGIDI_particle *MCGIDI_particle_new( statusMessageReporting *smr ) { 21 21 22 MCGIDI_particle *particle = (MCGIDI_partic 22 MCGIDI_particle *particle = (MCGIDI_particle *) smr_malloc2( smr, sizeof( MCGIDI_particle ), 0, "particle" ); 23 23 24 if( particle == NULL ) return( NULL ); 24 if( particle == NULL ) return( NULL ); 25 MCGIDI_particle_initialize( smr, particle 25 MCGIDI_particle_initialize( smr, particle ); 26 return( particle ); 26 return( particle ); 27 } 27 } 28 /* 28 /* 29 ********************************************** 29 ************************************************************ 30 */ 30 */ 31 int MCGIDI_particle_initialize( statusMessageR 31 int MCGIDI_particle_initialize( statusMessageReporting * /*smr*/, MCGIDI_particle *particle ) { 32 32 33 memset( particle, 0, sizeof( MCGIDI_partic 33 memset( particle, 0, sizeof( MCGIDI_particle ) ); 34 return( 0 ); 34 return( 0 ); 35 } 35 } 36 /* 36 /* 37 ********************************************** 37 ************************************************************ 38 */ 38 */ 39 MCGIDI_particle *MCGIDI_particle_free( statusM 39 MCGIDI_particle *MCGIDI_particle_free( statusMessageReporting *smr, MCGIDI_particle *particle ) { 40 40 41 int i, j; 41 int i, j; 42 MCGIDI_particle **p; 42 MCGIDI_particle **p; 43 43 44 for( i = 0, p = particleSortedList; i < nu 44 for( i = 0, p = particleSortedList; i < numberOfParticles; i++, p++ ) { 45 if( *p == particle ) { 45 if( *p == particle ) { 46 numberOfParticles--; 46 numberOfParticles--; 47 for( j = i; j < numberOfParticles; 47 for( j = i; j < numberOfParticles; j++, p++ ) *p = p[1]; 48 break; 48 break; 49 } 49 } 50 } 50 } 51 if( particle == particleListEnd ) particle 51 if( particle == particleListEnd ) particleListEnd = particle->prior; 52 if( particle == particleList ) particleLis 52 if( particle == particleList ) particleList = particle->next; 53 if( particle->prior != NULL ) particle->pr 53 if( particle->prior != NULL ) particle->prior->next = particle->next; 54 if( particle->next != NULL ) particle->nex 54 if( particle->next != NULL ) particle->next->prior = particle->prior; 55 MCGIDI_particle_release( smr, particle ); 55 MCGIDI_particle_release( smr, particle ); 56 smr_freeMemory( (void **) &particle ); 56 smr_freeMemory( (void **) &particle ); 57 return( NULL ); 57 return( NULL ); 58 } 58 } 59 /* 59 /* 60 ********************************************** 60 ************************************************************ 61 */ 61 */ 62 int MCGIDI_particle_release( statusMessageRepo 62 int MCGIDI_particle_release( statusMessageReporting * /*smr*/, MCGIDI_particle *particle ) { 63 63 64 smr_freeMemory( (void **) &(particle->name 64 smr_freeMemory( (void **) &(particle->name) ); 65 return( 0 ); 65 return( 0 ); 66 } 66 } 67 /* 67 /* 68 ********************************************** 68 ************************************************************ 69 */ 69 */ 70 int MCGIDI_particle_freeInternalList( statusMe 70 int MCGIDI_particle_freeInternalList( statusMessageReporting *smr ) { 71 71 72 while( particleList != NULL ) MCGIDI_parti 72 while( particleList != NULL ) MCGIDI_particle_free( smr, particleList ); // Loop checking, 11.06.2015, T. Koi 73 particleSortedList = (MCGIDI_particle **) 73 particleSortedList = (MCGIDI_particle **) smr_freeMemory( (void **) &particleSortedList ); 74 return( 0 ); 74 return( 0 ); 75 } 75 } 76 /* 76 /* 77 ********************************************** 77 ************************************************************ 78 */ 78 */ 79 MCGIDI_particle *MCGIDI_particle_getInternalID 79 MCGIDI_particle *MCGIDI_particle_getInternalID( statusMessageReporting *smr, const char * const name, MCGIDI_POPs *pops ) { 80 80 81 int i, iCmp, min, mid, max, Z, A, m, level 81 int i, iCmp, min, mid, max, Z, A, m, level; 82 MCGIDI_particle *particle; 82 MCGIDI_particle *particle; 83 MCGIDI_POP *pop; 83 MCGIDI_POP *pop; 84 84 85 iCmp = 0; 85 iCmp = 0; 86 min = mid = 0; 86 min = mid = 0; 87 max = numberOfParticles; 87 max = numberOfParticles; 88 while( min != max ) { // Loop checking, 1 88 while( min != max ) { // Loop checking, 11.06.2015, T. Koi 89 mid = ( min + max ) / 2; 89 mid = ( min + max ) / 2; 90 iCmp = strcmp( name, particleSortedLis 90 iCmp = strcmp( name, particleSortedList[mid]->name ); 91 if( iCmp == 0 ) return( particleSorted 91 if( iCmp == 0 ) return( particleSortedList[mid] ); 92 if( iCmp < 0 ) { 92 if( iCmp < 0 ) { 93 max = mid - 1; 93 max = mid - 1; 94 if( mid == 0 ) max = 0; } 94 if( mid == 0 ) max = 0; } 95 else { 95 else { 96 min = mid + 1; 96 min = mid + 1; 97 if( min > max ) min = max; 97 if( min > max ) min = max; 98 } 98 } 99 } 99 } 100 mid = min; 100 mid = min; 101 if( numberOfParticles > 0 ) { 101 if( numberOfParticles > 0 ) { 102 iCmp = strcmp( name, particleSortedLis 102 iCmp = strcmp( name, particleSortedList[mid]->name ); 103 if( iCmp == 0 ) return( particleSorted 103 if( iCmp == 0 ) return( particleSortedList[mid] ); 104 if( ( iCmp < 0 ) && ( mid != 0 ) ) { 104 if( ( iCmp < 0 ) && ( mid != 0 ) ) { 105 mid--; 105 mid--; 106 iCmp = strcmp( name, particleSorte 106 iCmp = strcmp( name, particleSortedList[mid]->name ); 107 } 107 } 108 } 108 } 109 109 110 if( ( particle = MCGIDI_particle_new( smr 110 if( ( particle = MCGIDI_particle_new( smr ) ) == NULL ) return( NULL ); 111 if( ( particle->name = smr_allocateCopyStr 111 if( ( particle->name = smr_allocateCopyString( smr, name, "particle->name", __FILE__, __LINE__, __func__ ) ) == NULL ) goto err; 112 if( MCGIDI_miscNameToZAm( smr, name, &Z, & 112 if( MCGIDI_miscNameToZAm( smr, name, &Z, &A, &m, &level ) != 0 ) goto err; 113 particle->prior = NULL; 113 particle->prior = NULL; 114 particle->next = NULL; 114 particle->next = NULL; 115 particle->Z = Z; 115 particle->Z = Z; 116 particle->A = A; 116 particle->A = A; 117 particle->m = m; 117 particle->m = m; 118 if( ( pop = MCGIDI_POPs_findParticle( pops 118 if( ( pop = MCGIDI_POPs_findParticle( pops, name ) ) == NULL ) { /* This should not happend. */ 119 particle->mass_MeV = MCGIDI_AMU2MeV * 119 particle->mass_MeV = MCGIDI_AMU2MeV * MCGIDI_particleMass_AMU( smr, name ); } 120 else { 120 else { 121 particle->mass_MeV = pop->mass_MeV; 121 particle->mass_MeV = pop->mass_MeV; 122 } 122 } 123 if( !smr_isOk( smr ) ) goto err; 123 if( !smr_isOk( smr ) ) goto err; 124 124 125 if( sizeOfParticleSortedList < ( numberOfP 125 if( sizeOfParticleSortedList < ( numberOfParticles + 1 ) ) { 126 if( sizeOfParticleSortedList == 0 ) { 126 if( sizeOfParticleSortedList == 0 ) { 127 sizeOfParticleSortedList = initial 127 sizeOfParticleSortedList = initialSizeOfList; } 128 else { 128 else { 129 sizeOfParticleSortedList += increm 129 sizeOfParticleSortedList += incrementalSizeOfList; 130 } 130 } 131 if( ( particleSortedList = (MCGIDI_par 131 if( ( particleSortedList = (MCGIDI_particle **) smr_realloc2( smr, particleSortedList, sizeOfParticleSortedList * sizeof( MCGIDI_particle * ), 132 "particleSortedList" ) ) == NULL ) 132 "particleSortedList" ) ) == NULL ) goto err; 133 } 133 } 134 134 135 if( particleList == NULL ) { 135 if( particleList == NULL ) { 136 particle->ordinal = 0; 136 particle->ordinal = 0; 137 particleListEnd = particleList = parti 137 particleListEnd = particleList = particle; } 138 else { 138 else { 139 particle->ordinal = particleListEnd->o 139 particle->ordinal = particleListEnd->ordinal + 1; 140 particle->prior = particleListEnd; 140 particle->prior = particleListEnd; 141 particleListEnd->next = particle; 141 particleListEnd->next = particle; 142 particleListEnd = particle; 142 particleListEnd = particle; 143 } 143 } 144 144 145 if( ( mid != 0 ) || ( iCmp > 0 ) ) mid++; 145 if( ( mid != 0 ) || ( iCmp > 0 ) ) mid++; 146 for( i = numberOfParticles; i > mid; i-- ) 146 for( i = numberOfParticles; i > mid; i-- ) particleSortedList[i] = particleSortedList[i-1]; 147 particleSortedList[mid] = particle; 147 particleSortedList[mid] = particle; 148 numberOfParticles++; 148 numberOfParticles++; 149 149 150 return( particle ); 150 return( particle ); 151 151 152 err: 152 err: 153 MCGIDI_particle_free( smr, particle ); 153 MCGIDI_particle_free( smr, particle ); 154 return( NULL ); 154 return( NULL ); 155 } 155 } 156 /* 156 /* 157 ********************************************** 157 ************************************************************ 158 */ 158 */ 159 int MCGIDI_particle_printInternalSortedList( s 159 int MCGIDI_particle_printInternalSortedList( statusMessageReporting * /*smr*/ ) { 160 160 161 int i; 161 int i; 162 162 163 for( i = 0; i < numberOfParticles; i++ ) p 163 for( i = 0; i < numberOfParticles; i++ ) printf( "%s\n", particleSortedList[i]->name ); 164 return( 0 ); 164 return( 0 ); 165 } 165 } 166 166 167 #if defined __cplusplus 167 #if defined __cplusplus 168 } 168 } 169 #endif 169 #endif 170 170 171 171