Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xData_2d_xindex_y.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 <stdlib.h>
37 #include <limits.h>
38 #include <ctype.h>
39 #include "xData.h"
40 
41 #if defined __cplusplus
42 namespace GIDI {
43 using namespace GIDI;
44 #endif
45 
46 //char const * const xData_twod_xindex_y_ID = "2d.xindex_y";
47 
48 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text );
49 static char *toString( statusMessageReporting *smr, xDataType *xDT );
50 static int release( statusMessageReporting *smr, xDataType *xDT );
51 static double *xData_2d_xindex_y_toFilled( statusMessageReporting *smr, xData_element *element, double *Xs, int size );
52 /*
53 ************************************************************
54 */
56 
57  xDataType *xDT = &(element->xDataTypeInfo);
58 
61  xDT->element = element;
62  xDT->toData = toData;
63  xDT->toString = toString;
64  xDT->release = release;
65  xDT->data = NULL;
66  return( xData_xDataTypeConvertAttributes( smr, element ) );
67 }
68 /*
69 ************************************************************
70 */
71 int xData_is_2d_xindex_y( statusMessageReporting *smr, xDataType *xDT, int setMsg ) {
72 
73  return( xData_is_xDataType( smr, xDT, xData_twod_xindex_y_ID, setMsg ) );
74 }
75 /*
76 ************************************************************
77 */
79 
80  return( xData_is_2d_xindex_y( smr, &(element->xDataTypeInfo), setMsg ) );
81 }
82 /*
83 ************************************************************
84 */
85 //static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
86 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *, const char *text ) {
87 
88  xData_Int i, status = 0;
89  char *e;
90  const char *s;
92  void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
93 
94  if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__, 1, "bad xDataType instance" ) );
95  release( smr, xDT );
96  if( ( xDT->data = xData_malloc2( smr, 2 * xDT->length * sizeof( xData_2d_xindex_y ), 0, "data" ) ) == NULL ) return( 1 );
97  for( i = 0, s = text, p = (xData_2d_xindex_y *) xDT->data; i < xDT->length; i++, p++, s = e ) {
98  if( xData_stringTo_xData_Int( smr, smrUser, s, &(p->index), " \n", &e ) ) { status = 1; break; }
99  s = e;
100  if( xData_stringTo_double( smr, smrUser, s, &(p->value), " \n", &e ) ) { status = 1; break; }
101  }
102  if( status == 0 ) {
103  while( isspace( *e ) ) e++;
104  if( *e != 0 ) {
105  smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "2d_xindex_y contains extra data = %s", e );
106  status = 1;
107  }
108  }
109  if( status != 0 ) release( smr, xDT );
110  return( status );
111 }
112 /*
113 ************************************************************
114 */
115 //static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
116 static char *toString( statusMessageReporting *, xDataType *xDT ) {
117 
118  xData_Int i, n = xDT->length, recordSize = 5 + 16 + 1, indexMax = 9;
119  int iFmt = 4;
120  char *str, *p, fmt[32] = " %99d %15.7e\n";
122 
123  if( n < 0 ) n = 0;
124  for( i = 0; i < n; i++, data++ ) {
125  while( ( data->index > indexMax ) && ( indexMax > 0 ) ) {
126  indexMax = 10 * indexMax + 9;
127  recordSize++;
128  iFmt++;
129  }
130  }
131  sprintf( fmt, " %%%dld %%15.7e\n", iFmt );
132  if( ( str = (char *) malloc( recordSize * ( n + 1 ) ) ) == NULL ) return( NULL );
133  for( i = 0, p = str, data = (xData_2d_xindex_y *) xDT->data; i < n; i++, p += recordSize, data++ ) {
134  sprintf( p, fmt, data->index, data->value );
135  }
136  *p = 0;
137  return( str );
138 }
139 /*
140 ************************************************************
141 */
142 static int release( statusMessageReporting *smr, xDataType *xDT ) {
143 
144  if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
145  return( xDT->status = xData_xDataType_Ok );
146 }
147 /*
148 ************************************************************
149 */
151 /*
152 * Returns NULL if length is 0 or memory could not be allocated.
153 */
154  xDataType *xDT = &(element->xDataTypeInfo);
155  xData_Int i, index = xDT->start, *values;
156  xData_2d_xindex_y *data = (xData_2d_xindex_y *) xDT->data;
157 
158  if( xDT->length == 0 ) return( NULL );
159  //if( ( values = xData_malloc2( smr, xDT->length * sizeof( xData_Int ), 0, "values" ) ) == NULL ) return( NULL );
160  if( ( values = (xData_Int*) xData_malloc2( smr, xDT->length * sizeof( xData_Int ), 0, "values" ) ) == NULL ) return( NULL );
161  for( i = 0; i < xDT->length; i++ ) {
162  values[i] = index;
163  index += data[i].index;
164  }
165  return( values );
166 }
167 /*
168 ************************************************************
169 */
171 
172  xData_free( smr, data );
173  return( 0 );
174 }
175 /*
176 ************************************************************
177 */
178 double *xData_2d_xindex_y_toXYs( statusMessageReporting *smr, xData_element *element, double *Xs ) {
179 /*
180 * Returns NULL if length is 0 or memory could not be allocated.
181 */
182  xDataType *xDT = &(element->xDataTypeInfo);
183  xData_Int i, index = xDT->start;
184  double *values = NULL, *p;
185  xData_2d_xindex_y *data = (xData_2d_xindex_y *) xDT->data;
186 
187  if( xDT->length == 0 ) return( NULL );
188  //if( ( values = xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
189  if( ( values = (double*) xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
190  p = values;
191  for( i = 0; i < xDT->length; i++, p++ ) {
192  index += data[i].index;
193  *p = Xs[index];
194  p++;
195  *p = data[i].value;
196  }
197  return( values );
198 }
199 /*
200 ************************************************************
201 */
203 
204  return( xData_2d_xindex_y_toFilled( smr, element, Xs, 1 ) );
205 }
206 /*
207 ************************************************************
208 */
210 
211  xData_free( smr, data );
212  return( 0 );
213 }
214 /*
215 ************************************************************
216 */
218 
219  return( xData_2d_xindex_y_toFilled( smr, element, Xs, 2 ) );
220 }
221 /*
222 ************************************************************
223 */
224 static double *xData_2d_xindex_y_toFilled( statusMessageReporting *smr, xData_element *element, double *Xs, int size ) {
225 /*
226 * Returns NULL if length is 0 or memory could not be allocated.
227 */
228  xDataType *xDT = &(element->xDataTypeInfo);
229  xData_Int i, j, index = xDT->start, length = xDT->end - xDT->start;
230  double x1, x2, *x, y1, y2, *values = NULL, *p;
231  xData_2d_xindex_y *data = (xData_2d_xindex_y *) xDT->data;
232 
233  if( xDT->length == 0 ) return( NULL );
234  //if( ( values = xData_malloc2( smr, size * length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
235  if( ( values = (double*) xData_malloc2( smr, size * length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
236  p = values;
237  x = &(Xs[xDT->start]);
238  x2 = 0.; /* Dummy initializations, as x1 and y1 (set by x2 and y2) are not used first time thru loop. */
239  y2 = 0.;
240  for( i = 0; i < xDT->length; i++, x++, p++ ) {
241  index += data[i].index; /* Note, data[0].index is 0; otherwise, following logic would not work. */
242  x1 = x2;
243  x2 = Xs[index];
244  y1 = y2;
245  y2 = data[i].value;
246  for( j = data[i].index; j > 1; j--, x++, p++ ) {
247  if( size == 2 ) {
248  *p = *x;
249  p++;
250  }
251  *p = ( y1 * ( x2 - *x ) + y2 * ( *x - x1 ) ) / ( x2 - x1 );
252  }
253  if( size == 2 ) *(p++) = *x;
254  *p = y2;
255  }
256  return( values );
257 }
258 
259 #if defined __cplusplus
260 }
261 #endif
xData_Int length
Definition: xData.h:162
int xData_2d_xindex_y_free_toFilledYs(statusMessageReporting *smr, void *data)
double * xData_2d_xindex_y_toFilledXYs(statusMessageReporting *smr, xData_element *element, double *Xs)
int xData_xDataTypeConvertAttributes(statusMessageReporting *smr, xData_element *element)
Definition: xData.cc:668
void * xData_get_smrUserInterfaceFromElement(xData_element *element)
Definition: xData.cc:952
xData_Int start
Definition: xData.h:162
const XML_Char * s
int xData_isElement_2d_xindex_y(statusMessageReporting *smr, xData_element *element, int setMsg)
const char * p
Definition: xmltok.h:285
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
int xData_is_xDataType(statusMessageReporting *smr, xDataType *xDT, char const *const type, int setMsg)
Definition: xData.cc:900
#define xData_malloc2(smr, size, zero, forItem)
Definition: xData.h:313
int xData_init_2d_xindex_y(statusMessageReporting *smr, xData_element *element)
const char *const xData_twod_xindex_y_ID
Definition: xData.h:73
void * xData_free(statusMessageReporting *smr, void *p)
Definition: xDataMisc.cc:89
xDT_toStringFunction toString
Definition: xData.h:159
enum xData_xDataType status
Definition: xData.h:155
xData_Int * xData_2d_xindex_y_rawIndices(statusMessageReporting *smr, xData_element *element)
double * xData_2d_xindex_y_toFilledYs(statusMessageReporting *smr, xData_element *element, double *Xs)
double * xData_2d_xindex_y_toXYs(statusMessageReporting *smr, xData_element *element, double *Xs)
xDataType xDataTypeInfo
Definition: xData.h:187
xDT_releaseFunction release
Definition: xData.h:160
const G4int n
int status
Definition: tracer.cxx:24
int xData_2d_xindex_y_free_rawIndices(statusMessageReporting *smr, void *data)
xData_element * element
Definition: xData.h:157
int xData_setMessageError_ReturnInt(int value, statusMessageReporting *smr, void *userData, const char *file, int line, int code, const char *fmt,...)
Definition: xDataMisc.cc:160
int xData_Int
Definition: xData.h:50
xData_Int index
Definition: xData.h:102
int xData_stringTo_xData_Int(statusMessageReporting *smr, void *smrUserInterface, char const *c, xData_Int *value, char const *endings, char **e)
Definition: xData.cc:1010
const char * typeString
Definition: xData.h:156
int xData_is_2d_xindex_y(statusMessageReporting *smr, xDataType *xDT, int setMsg)
void * malloc(size_t __size)
Definition: hjmalloc.cc:30
xDT_toDataFunction toData
Definition: xData.h:158
const XML_Char const XML_Char * data
int xData_stringTo_double(statusMessageReporting *smr, void *smrUserInterface, char const *c, double *value, char const *endings, char **e)
Definition: xData.cc:1044
xData_Int end
Definition: xData.h:162
void * data
Definition: xData.h:163