xData_2d_xindex_y.cc

Go to the documentation of this file.
00001 /*
00002 # <<BEGIN-copyright>>
00003 # Copyright (c) 2010, Lawrence Livermore National Security, LLC. 
00004 # Produced at the Lawrence Livermore National Laboratory 
00005 # Written by Bret R. Beck, beck6@llnl.gov. 
00006 # CODE-461393
00007 # All rights reserved. 
00008 #  
00009 # This file is part of GIDI. For details, see nuclear.llnl.gov. 
00010 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov. 
00011 # 
00012 # Redistribution and use in source and binary forms, with or without modification, 
00013 # are permitted provided that the following conditions are met: 
00014 #
00015 #      1) Redistributions of source code must retain the above copyright notice, 
00016 #         this list of conditions and the disclaimer below.
00017 #      2) Redistributions in binary form must reproduce the above copyright notice, 
00018 #         this list of conditions and the disclaimer (as noted below) in the 
00019 #          documentation and/or other materials provided with the distribution.
00020 #      3) Neither the name of the LLNS/LLNL nor the names of its contributors may be 
00021 #         used to endorse or promote products derived from this software without 
00022 #         specific prior written permission. 
00023 #
00024 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
00025 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00026 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT 
00027 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR 
00028 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00029 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00030 # OR SERVICES;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
00031 # AND ON  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00032 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00033 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00034 # <<END-copyright>>
00035 */
00036 #include <stdlib.h>
00037 #include <limits.h>
00038 #include <ctype.h>
00039 #include "xData.h"
00040 
00041 #if defined __cplusplus
00042 namespace GIDI {
00043 using namespace GIDI;
00044 #endif
00045 
00046 //char const * const xData_twod_xindex_y_ID = "2d.xindex_y";
00047 
00048 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text );
00049 static char *toString( statusMessageReporting *smr, xDataType *xDT );
00050 static int release( statusMessageReporting *smr, xDataType *xDT );
00051 static double *xData_2d_xindex_y_toFilled( statusMessageReporting *smr, xData_element *element, double *Xs, int size );
00052 /*
00053 ************************************************************
00054 */
00055 int xData_init_2d_xindex_y( statusMessageReporting *smr, xData_element *element ) {
00056 
00057     xDataType *xDT = &(element->xDataTypeInfo);
00058 
00059     xDT->status = xData_xDataType_Ok;
00060     xDT->typeString = xData_twod_xindex_y_ID;
00061     xDT->element = element;
00062     xDT->toData = toData;
00063     xDT->toString = toString;
00064     xDT->release = release;
00065     xDT->data = NULL;
00066     return( xData_xDataTypeConvertAttributes( smr, element ) );
00067 }
00068 /*
00069 ************************************************************
00070 */
00071 int xData_is_2d_xindex_y( statusMessageReporting *smr, xDataType *xDT, int setMsg ) {
00072 
00073     return( xData_is_xDataType( smr, xDT, xData_twod_xindex_y_ID, setMsg ) );
00074 }
00075 /*
00076 ************************************************************
00077 */
00078 int xData_isElement_2d_xindex_y( statusMessageReporting *smr, xData_element *element, int setMsg ) {
00079 
00080     return( xData_is_2d_xindex_y( smr, &(element->xDataTypeInfo), setMsg ) );
00081 }
00082 /*
00083 ************************************************************
00084 */
00085 //static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
00086 static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *, const char *text ) {
00087 
00088     xData_Int i, status = 0;
00089     char *e;
00090     const char *s;
00091     xData_2d_xindex_y *p;
00092     void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
00093 
00094     if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__, 1, "bad xDataType instance" ) );
00095     release( smr, xDT );
00096     if( ( xDT->data = xData_malloc2( smr, 2 * xDT->length * sizeof( xData_2d_xindex_y ), 0, "data" ) ) == NULL ) return( 1 );
00097     for( i = 0, s = text, p = (xData_2d_xindex_y *) xDT->data; i < xDT->length; i++, p++, s = e ) {
00098         if( xData_stringTo_xData_Int( smr, smrUser, s, &(p->index), " \n", &e ) ) { status = 1; break; }
00099         s = e;
00100         if( xData_stringTo_double( smr, smrUser, s, &(p->value), " \n", &e ) ) { status = 1; break; }
00101     }
00102     if( status == 0 ) {
00103         while( isspace( *e ) ) e++;
00104         if( *e != 0 ) {
00105             smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "2d_xindex_y contains extra data = %s", e );
00106             status = 1;
00107         }
00108     }
00109     if( status != 0 ) release( smr, xDT );
00110     return( status );
00111 }
00112 /*
00113 ************************************************************
00114 */
00115 //static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
00116 static char *toString( statusMessageReporting *, xDataType *xDT ) {
00117 
00118     xData_Int i, n = xDT->length, recordSize = 5 + 16 + 1, indexMax = 9;
00119     int iFmt = 4;
00120     char *str, *p, fmt[32] = " %99d %15.7e\n";
00121     xData_2d_xindex_y *data = (xData_2d_xindex_y *) xDT->data;
00122 
00123     if( n < 0 ) n = 0;
00124     for( i = 0; i < n; i++, data++ ) {
00125         while( ( data->index > indexMax ) && ( indexMax > 0 ) ) {
00126             indexMax = 10 * indexMax + 9;
00127             recordSize++;
00128             iFmt++;
00129         }
00130     }
00131     sprintf( fmt, " %%%dld %%15.7e\n", iFmt );
00132     if( ( str = (char *) malloc( recordSize * ( n + 1 ) ) ) == NULL ) return( NULL );
00133     for( i = 0, p = str, data = (xData_2d_xindex_y *) xDT->data; i < n; i++, p += recordSize, data++ ) {
00134         sprintf( p, fmt, data->index, data->value );
00135     }
00136     *p = 0;
00137     return( str );
00138 }
00139 /*
00140 ************************************************************
00141 */
00142 static int release( statusMessageReporting *smr, xDataType *xDT ) {
00143 
00144     if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
00145     return( xDT->status = xData_xDataType_Ok );
00146 }
00147 /*
00148 ************************************************************
00149 */
00150 xData_Int *xData_2d_xindex_y_rawIndices( statusMessageReporting *smr, xData_element *element ) {
00151 /*
00152 *   Returns NULL if length is 0 or memory could not be allocated.
00153 */
00154     xDataType *xDT = &(element->xDataTypeInfo);
00155     xData_Int i, index = xDT->start, *values;
00156     xData_2d_xindex_y *data = (xData_2d_xindex_y *) xDT->data;
00157 
00158     if( xDT->length == 0 ) return( NULL );
00159     //if( ( values = xData_malloc2( smr, xDT->length * sizeof( xData_Int ), 0, "values" ) ) == NULL ) return( NULL );
00160     if( ( values = (xData_Int*) xData_malloc2( smr, xDT->length * sizeof( xData_Int ), 0, "values" ) ) == NULL ) return( NULL );
00161     for( i = 0; i < xDT->length; i++ ) {
00162         values[i] = index;
00163         index += data[i].index;
00164     }
00165     return( values );
00166 }
00167 /*
00168 ************************************************************
00169 */
00170 int xData_2d_xindex_y_free_rawIndices( statusMessageReporting *smr, void *data ) {
00171 
00172     xData_free( smr, data );
00173     return( 0 );
00174 }
00175 /*
00176 ************************************************************
00177 */
00178 double *xData_2d_xindex_y_toXYs( statusMessageReporting *smr, xData_element *element, double *Xs ) {
00179 /*
00180 *   Returns NULL if length is 0 or memory could not be allocated.
00181 */
00182     xDataType *xDT = &(element->xDataTypeInfo);
00183     xData_Int i, index = xDT->start;
00184     double *values = NULL, *p;
00185     xData_2d_xindex_y *data = (xData_2d_xindex_y *) xDT->data;
00186 
00187     if( xDT->length == 0 ) return( NULL );
00188     //if( ( values = xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
00189     if( ( values = (double*) xData_malloc2( smr, 2 * xDT->length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
00190     p = values;
00191     for( i = 0; i < xDT->length; i++, p++ ) {
00192         index += data[i].index;
00193         *p = Xs[index];
00194         p++;
00195         *p = data[i].value;
00196     }
00197     return( values );
00198 }
00199 /*
00200 ************************************************************
00201 */
00202 double *xData_2d_xindex_y_toFilledYs( statusMessageReporting *smr, xData_element *element, double *Xs ) {
00203 
00204     return( xData_2d_xindex_y_toFilled( smr, element, Xs, 1 ) );
00205 }
00206 /*
00207 ************************************************************
00208 */
00209 int xData_2d_xindex_y_free_toFilledYs( statusMessageReporting *smr, void *data ) {
00210 
00211     xData_free( smr, data );
00212     return( 0 );
00213 }
00214 /*
00215 ************************************************************
00216 */
00217 double *xData_2d_xindex_y_toFilledXYs( statusMessageReporting *smr, xData_element *element, double *Xs ) {
00218 
00219     return( xData_2d_xindex_y_toFilled( smr, element, Xs, 2 ) );
00220 }
00221 /*
00222 ************************************************************
00223 */
00224 static double *xData_2d_xindex_y_toFilled( statusMessageReporting *smr, xData_element *element, double *Xs, int size ) {
00225 /*
00226 *   Returns NULL if length is 0 or memory could not be allocated.
00227 */
00228     xDataType *xDT = &(element->xDataTypeInfo);
00229     xData_Int i, j, index = xDT->start, length = xDT->end - xDT->start;
00230     double x1, x2, *x, y1, y2, *values = NULL, *p;
00231     xData_2d_xindex_y *data = (xData_2d_xindex_y *) xDT->data;
00232 
00233     if( xDT->length == 0 ) return( NULL );
00234     //if( ( values = xData_malloc2( smr, size * length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
00235     if( ( values = (double*) xData_malloc2( smr, size * length * sizeof( double ), 0, "values" ) ) == NULL ) return( NULL );
00236     p = values;
00237     x = &(Xs[xDT->start]);
00238     x2 = 0.;                                        /* Dummy initializations, as x1 and y1 (set by x2 and y2) are not used first time thru loop. */
00239     y2 = 0.;
00240     for( i = 0; i < xDT->length; i++, x++, p++ ) {
00241         index += data[i].index;                     /* Note, data[0].index is 0; otherwise, following logic would not work. */
00242         x1 = x2;
00243         x2 = Xs[index];
00244         y1 = y2;
00245         y2 = data[i].value;
00246         for( j = data[i].index; j > 1; j--, x++, p++ ) {
00247             if( size == 2 ) {
00248                 *p = *x;
00249                 p++;
00250             }
00251             *p = ( y1 * ( x2 - *x ) + y2 * ( *x - x1 ) ) / ( x2 - x1 );
00252         }
00253         if( size == 2 ) *(p++) = *x;
00254         *p = y2;
00255     }
00256     return( values );
00257 }
00258 
00259 #if defined __cplusplus
00260 }
00261 #endif

Generated on Mon May 27 17:50:36 2013 for Geant4 by  doxygen 1.4.7