Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tpia_target_heated.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 <tpia_misc.h>
39 
40 #if defined __cplusplus
41 namespace GIDI {
42 using namespace GIDI;
43 #endif
44 
45 static void _tpia_target_heated_getEnergyGridAndAllocateTotalCrossSections( statusMessageReporting *smr, tpia_target_heated *target, xData_element *element );
46 static int _tpia_target_heated_releaseAndReturnOne( statusMessageReporting *smr, xData_document *doc, tpia_target_heated *target );
47 static int _tpia_target_heated_checkElememtsForAccess( statusMessageReporting *smr, xData_document *doc );
48 static int _tpia_target_heated_checkElememtsForAccess2( statusMessageReporting *smr, xData_element *element );
49 /*
50 ************************************************************
51 */
53 
55 
56  //if( ( target = xData_malloc2( smr, sizeof( tpia_target_heated ), 0, "target" ) ) == NULL ) return( NULL );
57  if( ( target = (tpia_target_heated*) xData_malloc2( smr, sizeof( tpia_target_heated ), 0, "target" ) ) == NULL ) return( NULL );
58  //if( tpia_target_heated_initialize( smr, target ) ) target = xData_free( smr, target );
59  if( tpia_target_heated_initialize( smr, target ) ) target = (tpia_target_heated*) xData_free( smr, target );
60  return( target );
61 }
62 /*
63 ************************************************************
64 */
65 //int tpia_target_heated_initialize( statusMessageReporting *smr, tpia_target_heated *target ) {
67 
68  memset( target, 0, sizeof( tpia_target_heated ) );
69  return( 0 );
70 }
71 /*
72 ************************************************************
73 */
74 tpia_target_heated *tpia_target_heated_createRead( statusMessageReporting *smr, const char *fileName, int checkElememtsForAccess ) {
75 
77 
78  if( ( target = tpia_target_heated_create( smr ) ) == NULL ) return( NULL );
79  //if( tpia_target_heated_read( smr, target, fileName, checkElememtsForAccess ) != 0 ) target = xData_free( smr, target );
80  if( tpia_target_heated_read( smr, target, fileName, checkElememtsForAccess ) != 0 ) target = (tpia_target_heated*) xData_free( smr, target );
81  return( target );
82 }
83 /*
84 ************************************************************
85 */
87 
88  tpia_target_heated_release( smr, target );
89  xData_free( smr, target );
90  return( NULL );
91 }
92 /*
93 ************************************************************
94 */
96 
97  int i;
98 
99  //target->path = xData_free( smr, target->path );
100  target->path = (char*) xData_free( smr, target->path );
101  //target->absPath = xData_free( smr, target->absPath );
102  target->absPath = (char*) xData_free( smr, target->absPath );
103  target->energyGridLength = 0;
104  //target->energyGrid = xData_free( smr, target->energyGrid );
105  target->energyGrid = (double*) xData_free( smr, target->energyGrid );
106  //target->kerma = xData_free( smr, target->kerma );
107  target->kerma = (double*) xData_free( smr, target->kerma );
108  //target->totalCrossSectionPointwise.data = xData_free( smr, target->totalCrossSectionPointwise.data );
109  target->totalCrossSectionPointwise.data = (double*) xData_free( smr, target->totalCrossSectionPointwise.data );
110  //target->totalCrossSectionGrouped.data = xData_free( smr, target->totalCrossSectionGrouped.data );
111  target->totalCrossSectionGrouped.data = (double*) xData_free( smr, target->totalCrossSectionGrouped.data );
112  xData_releaseAttributionList( smr, &(target->attributes) );
113  for( i = 0; i < target->nChannels; i++ ) tpia_channel_free( smr, target->channels[i] );
114  target->nChannels = 0;
115  //target->channels = xData_free( smr, target->channels );
116  target->channels = (tpia_channel**) xData_free( smr, target->channels );
117  for( i = 0; i < target->nProductionChannels; i++ ) tpia_channel_free( smr, target->productionChannels[i] );
118  target->nProductionChannels = 0;
119  //target->productionChannels = xData_free( smr, target->productionChannels );
120  target->productionChannels = (tpia_channel**) xData_free( smr, target->productionChannels );
121  return( 0 );
122 }
123 /*
124 ************************************************************
125 */
126 int tpia_target_heated_read( statusMessageReporting *smr, tpia_target_heated *target, const char *fileName, int checkElememtsForAccess ) {
127 /*
128 * If a target has already been read into this target, user must have called tpia_target_heated_release before calling this routine.
129 * Otherwise, there will be memory leaks.
130 */
131  xData_document *doc = NULL;
132  xData_element *element, *channelElement, *channels;
133  int nChannels;
134  tpia_channel *channel;
135  char *name;
136  xData_Int i, j;
137 
138  tpia_target_heated_initialize( smr, target );
139  if( ( target->path = xDataMisc_allocateCopyString2( smr, fileName, "path" ) ) == NULL ) return( 1 );
140  if( ( target->absPath = xDataMisc_getAbsPath( smr, fileName ) ) == NULL ) return( _tpia_target_heated_releaseAndReturnOne( smr, doc, target ) );
141  if( ( doc = xData_parseReadFile( smr, fileName, NULL, NULL ) ) == NULL ) return( _tpia_target_heated_releaseAndReturnOne( smr, doc, target ) );
142  element = xData_getDocumentsElement( doc );
143  xData_addToAccessed( smr, element, 1 );
144  if( xData_convertAttributeTo_xData_Int( smr, element, "nGroups", &i ) != 0 ) return( _tpia_target_heated_releaseAndReturnOne( smr, doc, target ) );
145  target->nGroups = (int) i;
146  if( strcmp( element->name, "xTargetHeated" ) != 0 ) {
147  tpia_misc_setMessageError_Element( smr, NULL, element, __FILE__, __LINE__, 1, "input file's top element must be xTargetHeated and not %s",
148  element->name ); }
149  else {
150  xData_copyAttributionList( smr, &(target->attributes), &(element->attributes) );
151  if( smr_isOk( smr ) ) target->contents = xData_getAttributesValue( &(target->attributes), "contents" );
152  if( ( name = tpia_misc_pointerToAttributeIfAllOk3( smr, target->absPath, 1, &(target->attributes), "projectile" ) ) != NULL )
153  target->projectileID = tpia_particle_getInternalID( smr, name );
154  if( ( name = tpia_misc_pointerToAttributeIfAllOk3( smr, target->absPath, 1, &(target->attributes), "target" ) ) != NULL )
155  target->targetID = tpia_particle_getInternalID( smr, name );
156  if( smr_isOk( smr ) ) _tpia_target_heated_getEnergyGridAndAllocateTotalCrossSections( smr, target, element );
157  if( smr_isOk( smr ) ) { /* Get channels. */
158  //if( ( channels = xData_getOneElementByTagName( smr, element, "channels", 1 ) ) == NULL )
159  if( ( channels = xData_getOneElementByTagName( smr, element, (char*)"channels", 1 ) ) == NULL )
160  return( _tpia_target_heated_releaseAndReturnOne( smr, doc, target ) );
161  xData_addToAccessed( smr, channels, 1 );
162  if( ( nChannels = xData_numberOfElementsByTagName( smr, channels, "channel" ) ) > 0 ) {
163  //if( ( target->channels = xData_malloc2( smr, nChannels * sizeof( tpia_channel * ), 1, "channels" ) ) == NULL )
164  if( ( target->channels = (tpia_channel**) xData_malloc2( smr, nChannels * sizeof( tpia_channel * ), 1, "channels" ) ) == NULL )
165  return( _tpia_target_heated_releaseAndReturnOne( smr, doc, target ) );
166  for( channelElement = xData_getFirstElement( channels ); channelElement != NULL; channelElement = xData_getNextElement( channelElement ) ) {
167  if( !strcmp( channelElement->name, "channel" ) ) {
168  if( ( channel = tpia_channel_createGetFromElement( smr, target, channelElement, 1 ) ) == NULL ) break;
169  target->channels[target->nChannels] = channel;
170  target->nChannels++;
171  for( i = channel->crossSectionPointwise.start, j = 0; i < channel->crossSectionPointwise.end; i++, j++ )
172  target->totalCrossSectionPointwise.data[i] += channel->crossSectionPointwise.data[j];
173  for( i = channel->crossSectionGrouped.start, j = 0; i < channel->crossSectionGrouped.end; i++, j++ )
174  target->totalCrossSectionGrouped.data[i] += channel->crossSectionGrouped.data[j];
175  }
176  }
177  }
178  }
179  if( smr_isOk( smr ) ) { /* Get production channels. */
180  //if( ( channels = xData_getOneElementByTagName( smr, element, "productionChannels", 0 ) ) == NULL ) {
181  if( ( channels = xData_getOneElementByTagName( smr, element, (char*) "productionChannels", 0 ) ) == NULL ) {
182  if( !smr_isOk( smr ) ) return( _tpia_target_heated_releaseAndReturnOne( smr, doc, target ) ); }
183  else {
184  xData_addToAccessed( smr, channels, 1 );
185  if( ( nChannels = xData_numberOfElementsByTagName( smr, channels, "channel" ) ) > 0 ) {
186  //if( ( target->productionChannels = xData_malloc2( smr, nChannels * sizeof( tpia_channel * ), 1, "channels" ) ) != NULL ) {
187  if( ( target->productionChannels = (tpia_channel**) xData_malloc2( smr, nChannels * sizeof( tpia_channel * ), 1, "channels" ) ) != NULL ) {
188  for( channelElement = xData_getFirstElement(channels); channelElement != NULL; channelElement = xData_getNextElement(channelElement) ) {
189  if( !strcmp( channelElement->name, "channel" ) ) {
190  channel = tpia_channel_createGetFromElement( smr, target, channelElement, 1 );
191  if( channel == NULL ) break;
192  target->productionChannels[target->nProductionChannels] = channel;
193  target->nProductionChannels++;
194  }
195  }
196  }
197  }
198  }
199  }
200  }
201  if( smr_isOk( smr ) && checkElememtsForAccess ) _tpia_target_heated_checkElememtsForAccess( smr, doc );
202  xData_parseFree( smr, doc );
203  if( !smr_isOk( smr ) ) tpia_target_heated_release( smr, target );
204  return( !smr_isOk( smr ) );
205 }
206 /*
207 ************************************************************
208 */
209 static void _tpia_target_heated_getEnergyGridAndAllocateTotalCrossSections( statusMessageReporting *smr, tpia_target_heated *target, xData_element *element ) {
210 
211  xData_Int i, energyGridIndex, energyGridStart, energyGridEnd, energyGridLength;
212  xData_element *energyGrid, *kerma;
213 
214  if( !smr_isOk( smr ) ) return;
215  //if( ( energyGrid = xData_getOneElementByTagName( smr, element, "energyGrid", 1 ) ) == NULL ) return;
216  if( ( energyGrid = xData_getOneElementByTagName( smr, element, (char*) "energyGrid", 1 ) ) == NULL ) return;
217  xData_addToAccessed( smr, energyGrid, 1 );
218  if( ( energyGrid = xData_getElements_xDataElement( smr, energyGrid ) ) == NULL ) return;
219  xData_addToAccessed( smr, energyGrid, 1 );
220  xData_getCommonData( smr, energyGrid, &energyGridIndex, &energyGridStart, &energyGridEnd, &energyGridLength );
221  if( ( target->energyGrid = xData_1d_x_allocateCopyData( smr, energyGrid ) ) == NULL ) return;
222  target->energyGridLength = energyGridLength;
223  target->totalCrossSectionPointwise.start = 0;
224  target->totalCrossSectionPointwise.end = energyGridLength;
225  target->totalCrossSectionPointwise.length = energyGridLength;
226  //if( ( target->totalCrossSectionPointwise.data = xData_malloc2( smr, energyGridLength * sizeof( double ), 0, "totalCrossSectionPointwise" ) ) == NULL )
227  if( ( target->totalCrossSectionPointwise.data = (double*) xData_malloc2( smr, energyGridLength * sizeof( double ), 0, "totalCrossSectionPointwise" ) ) == NULL )
228  return;
229  for( i = 0; i < energyGridLength; i++ ) target->totalCrossSectionPointwise.data[i] = 0.;
230  target->totalCrossSectionGrouped.start = 0;
231  target->totalCrossSectionGrouped.end = energyGridLength;
232  target->totalCrossSectionGrouped.length = energyGridLength;
233  //if( ( target->totalCrossSectionGrouped.data = xData_malloc2( smr, target->nGroups * sizeof( double ), 0, "totalCrossSectionGrouped" ) ) == NULL ) return;
234  if( ( target->totalCrossSectionGrouped.data = (double*) xData_malloc2( smr, target->nGroups * sizeof( double ), 0, "totalCrossSectionGrouped" ) ) == NULL ) return;
235  for( i = 0; i < target->nGroups; i++ ) target->totalCrossSectionGrouped.data[i] = 0.;
236  //if( ( kerma = xData_getOneElementByTagName( smr, element, "kerma", 1 ) ) != NULL ) {
237  if( ( kerma = xData_getOneElementByTagName( smr, element, (char*) "kerma", 1 ) ) != NULL ) {
238  xData_addToAccessed( smr, kerma, 1 );
239  if( ( kerma = xData_getElements_xDataElement( smr, kerma ) ) == NULL ) return;
240  xData_addToAccessed( smr, kerma, 1 );
241  if( ( target->kerma = xData_1d_x_allocateCopyData( smr, kerma ) ) == NULL ) return;
242  }
243 }
244 /*
245 ************************************************************
246 */
247 //int tpia_target_heated_numberOfChannels( statusMessageReporting *smr, tpia_target_heated *target ) {
249 
250  return( target->nChannels );
251 }
252 /*
253 ************************************************************
254 */
255 //int tpia_target_heated_numberOfProductionChannels( statusMessageReporting *smr, tpia_target_heated *target ) {
257 
258  return( target->nProductionChannels );
259 }
260 /*
261 ************************************************************
262 */
264 
265  tpia_channel *channel = NULL;
266 
267  if( ( index >= 0 ) && ( index < target->nChannels ) ) channel = target->channels[index];
268  return( channel );
269 }
270 /*
271 ************************************************************
272 */
274 
275  tpia_channel *channel = tpia_target_heated_getChannelAtIndex( target, index );
276 
277  if( channel == NULL ) {
278  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad channel index = %d for %s + %s", index,
279  target->projectileID->name, target->targetID->name );
280  }
281  return( channel );
282 }
283 /*
284 ************************************************************
285 */
287 
288  tpia_channel *channel = NULL;
289 
290  if( ( index >= 0 ) && ( index < target->nProductionChannels ) ) channel = target->productionChannels[index];
291  return( channel );
292 }
293 /*
294 ************************************************************
295 */
296 //xData_Int tpia_target_heated_getEnergyGrid( statusMessageReporting *smr, tpia_target_heated *target, double **energyGrid ) {
298 
299  if( energyGrid != NULL ) *energyGrid = target->energyGrid;
300  return( target->energyGridLength );
301 }
302 /*
303 ************************************************************
304 */
306 
307  return( tpia_misc_binarySearch( target->energyGridLength, target->energyGrid, e_in ) );
308 }
309 /*
310 ************************************************************
311 */
312 //double tpia_target_heated_getTotalCrossSectionAtE( statusMessageReporting *smr, tpia_target_heated *target, xData_Int iEg, double e_in,
314  int crossSectionType ) {
315 
316  double xsec = 0.;
317 
318  if( crossSectionType == tpia_crossSectionType_grouped ) {
319  xsec = 0; }
320  else if( crossSectionType == tpia_crossSectionType_pointwise ) {
322  tpia_target_heated_getEIndex( target, e_in ), e_in );
323  }
324  return( xsec );
325 }
326 /*
327 ************************************************************
328 */
330  int crossSectionType ) {
331 
332  double xsec = 0.;
333  tpia_channel *channel = tpia_target_heated_getChannelAtIndex_smr( smr, target, index );
334 
335  if( channel != NULL ) xsec = tpia_channel_getCrossSectionAtE( smr, channel, iEg, e_in, crossSectionType );
336  return( xsec );
337 }
338 /*
339 ************************************************************
340 */
342  tpia_decaySamplingInfo *decaySamplingInfo, int nProductData, tpia_productOutgoingData *productDatas ) {
343 
344  tpia_channel *channel = tpia_target_heated_getChannelAtIndex_smr( smr, target, index );
345 
346  if( channel == NULL ) return( -1 );
347  return( tpia_decayChannel_sampleProductsAtE( smr, &(channel->decayChannel), decaySamplingInfo, nProductData, productDatas ) );
348 }
349 /*
350 ************************************************************
351 */
352 static int _tpia_target_heated_releaseAndReturnOne( statusMessageReporting *smr, xData_document *doc, tpia_target_heated *target ) {
353 
354  tpia_target_heated_release( smr, target );
355  if( doc != NULL ) xData_parseFree( smr, doc );
356  return( 1 );
357 }
358 /*
359 ************************************************************
360 */
361 static int _tpia_target_heated_checkElememtsForAccess( statusMessageReporting *smr, xData_document *doc ) {
362 
363  xData_element *element = xData_getDocumentsElement( doc );
364 
365  _tpia_target_heated_checkElememtsForAccess2( smr, element );
366  return( 0 );
367 }
368 /*
369 ************************************************************
370 */
371 static int _tpia_target_heated_checkElememtsForAccess2( statusMessageReporting *smr, xData_element *element ) {
372 
373  xData_element *child;
374 
375  if( xData_getAccessed( smr, element ) != 1 ) printf( "%3d %s\n", xData_getAccessed( smr, element ), element->fullName );
376  for( child = xData_getFirstElement( element ); child != NULL; child = xData_getNextElement( child ) )
377  _tpia_target_heated_checkElememtsForAccess2( smr, child );
378  return( 0 );
379 }
380 
381 #if defined __cplusplus
382 }
383 #endif