Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
xData_matrix.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, [email protected].
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 <string.h>
38#include <limits.h>
39#include <ctype.h>
40#include "xData.h"
41
42#if defined __cplusplus
43namespace GIDI {
44using namespace GIDI;
45#endif
46
47//char const * const xData_matrix_ID = "matrix";
48
49static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text );
50static char *toString( statusMessageReporting *smr, xDataType *xDT );
51static int release( statusMessageReporting *smr, xDataType *xDT );
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*/
71int xData_is_matrix( statusMessageReporting *smr, xDataType *xDT, int setMsg ) {
72
73 return( xData_is_xDataType( smr, xDT, xData_matrix_ID, setMsg ) );
74}
75/*
76************************************************************
77*/
79
80 return( xData_is_matrix( smr, &(element->xDataTypeInfo), setMsg ) );
81}
82/*
83************************************************************
84*/
86
87 xData_Int i, n;
88 xDataType *xDT = &(element->xDataTypeInfo);
89 xData_matrix *oldMatrix = (xData_matrix *) xDT->data, *newMatrix;
90 double *oldP, *newP;
91
92 if( !xData_isElement_matrix( smr, element, 1 ) ) return( NULL );
93 n = oldMatrix->rows * oldMatrix->columns;
94 if( ( newMatrix = (xData_matrix *) xData_malloc2( smr, sizeof( xData_matrix ) + xDT->length * sizeof( xData_matrix_rowStartEnd ) +
95 n * sizeof( double ), 0, "data" ) ) == NULL ) return( NULL );
96 newMatrix->rows = oldMatrix->rows;
97 newMatrix->columns = oldMatrix->columns;
98 newMatrix->rowStartEnds = (xData_matrix_rowStartEnd *) &(newMatrix[1]);
99 newMatrix->values = (double *) &(newMatrix->rowStartEnds[xDT->length]);
100 for( i = 0; i < xDT->length; i++ ) newMatrix->rowStartEnds[i] = oldMatrix->rowStartEnds[i];
101 for( i = 0, oldP = oldMatrix->values, newP = newMatrix->values; i < n; i++, oldP++, newP++ ) *newP = *oldP;
102 return( newMatrix );
103}
104/*
105************************************************************
106*/
108
109 xData_free( smr, data );
110 return( 0 );
111}
112/*
113************************************************************
114*/
115static int toData( statusMessageReporting *smr, xDataType *xDT, xData_attributionList *attributes, const char *text ) {
116
117 xData_Int i, j, row, start, end, rows, columns, status = 0;
118 char *e;
119 const char *s, *size;
120 double *p;
121 xData_matrix *matrix;
122 void *smrUser = xData_get_smrUserInterfaceFromElement( xDT->element );
123
124 if( xDT->status != xData_xDataType_Ok ) return( xData_setMessageError_ReturnInt( 1, smr, smrUser, __FILE__, __LINE__, 1, "bad xDataType instance" ) );
125 release( smr, xDT );
126
127 if( ( size = xData_getAttributesValue( attributes, "size" ) ) == NULL ) return( xData_setMessageError_ReturnInt( 1, smr,
128 smrUser, __FILE__, __LINE__, 1, "xData missing \"size\" attribute" ) );
129 if( xData_stringTo_xData_Int( smr, smrUser, size, &rows, " ,", &e ) ) return( 1 );
130 while( isspace( *e ) ) e++;
131 if( *e != ',' ) {
132 smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "matrix size attribute missing \",\" separator" );
133 return( 1 );
134 }
135 s = (const char *) ++e;
136 if( xData_stringTo_xData_Int( smr, smrUser, s, &columns, "", &e ) ) return( 1 );
137 if( ( xDT->data = (xData_matrix *) xData_malloc2( NULL, sizeof( xData_matrix ) + xDT->length * sizeof( xData_matrix_rowStartEnd ) +
138 rows * columns * sizeof( double ), 0, "xDT->data" ) ) == NULL ) return( 1 );
139 matrix = (xData_matrix *) xDT->data;
140 matrix->rows = rows;
141 matrix->columns = columns;
142 matrix->rowStartEnds = (xData_matrix_rowStartEnd *) &(matrix[1]);
143 matrix->values = (double *) &(matrix->rowStartEnds[xDT->length]);
144
145 for( i = 0, s = text; i < xDT->length; i++ ) {
146 if( xData_stringTo_xData_Int( smr, smrUser, s, &row, " \n", &e ) ) { status = 1; break; }
147 if( ( row < 0 ) || ( row >= rows ) ) {
148 status = 1;
149 smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "row = %lld out-of-range (valid range is 0 <= row <= %lld)", row, rows );
150 break;
151 }
152 if( xData_stringTo_xData_Int( smr, smrUser, e, &start, " \n", &e ) ) { status = 1; break; }
153 if( ( start < 0 ) || ( start > columns ) ) {
154 status = 1;
155 smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "start = %lld out-of-range (valid range is 0 <= start <= %lld)", start, columns );
156 break;
157 }
158 if( xData_stringTo_xData_Int( smr, smrUser, e, &end, " \n", &e ) ) { status = 1; break; }
159 if( ( end < start ) || ( end > columns ) ) {
160 status = 1;
161 smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "end = %lld out-of-range (valid range is %lld <= end <= %lld)", end, start, columns );
162 break;
163 }
164 matrix->rowStartEnds[i].row = row;
165 matrix->rowStartEnds[i].start = start;
166 matrix->rowStartEnds[i].end = end;
167 p = &(matrix->values[row * columns]);
168 for( j = 0; j < start; j++ ) *(p++) = 0.;
169 for( s = e; j < end; j++, p++, s = e ) {
170 if( xData_stringTo_double( smr, smrUser, s, p, " \n", &e ) ) { status = 1; break; }
171 }
172 if( status != 0 ) break;
173 for( ; j < columns; j++ ) *(p++) = 0.;
174 }
175 if( status == 0 ) {
176 while( isspace( *e ) ) e++;
177 if( *e != 0 ) {
178 smr_setMessageError( smr, smrUser, __FILE__, __LINE__, 1, "matrix contains extra data = %s", e );
179 status = 1;
180 }
181 }
182 if( status != 0 ) release( smr, xDT );
183 return( status );
184}
185/*
186************************************************************
187*/
188//static char *toString( statusMessageReporting *smr, xDataType *xDT ) {
189static char *toString( statusMessageReporting *, xDataType *xDT ) {
190
191 xData_Int i, n = 1, start, end, iRow, iColumn;
192 char *str, *p;
193 xData_matrix *matrix = (xData_matrix *) xDT->data;
194 double *row;
195
196 for( iRow = 0; iRow < matrix->rows; iRow++ ) {
197 row = &(matrix->values[iRow * matrix->columns]);
198 for( iColumn = 0; iColumn < matrix->columns; iColumn++ ) if( row[iColumn] != 0. ) break;
199 start = iColumn;
200 for( end = iColumn; iColumn < matrix->columns; iColumn++ ) if( row[iColumn] != 0. ) end = iColumn + 1;
201 if( start < end ) n += 10 * 3 + 17 * ( end - start + 1 );
202 }
203 if( ( str = (char *) xData_malloc2( NULL, n, 0, "str" ) ) == NULL ) return( NULL );
204 for( iRow = 0, p = str; iRow < matrix->rows; iRow++ ) {
205 row = &(matrix->values[iRow * matrix->columns]);
206 for( iColumn = 0; iColumn < matrix->columns; iColumn++ ) if( row[iColumn] != 0. ) break;
207 start = iColumn;
208 for( end = iColumn; iColumn < matrix->columns; iColumn++ ) if( row[iColumn] != 0. ) end = iColumn + 1;
209 if( start < end ) {
210 sprintf( p, "%3d %3d %3d", iRow, start, end );
211 p += strlen( p );
212 for( i = start; i < end; i++, p += 17 ) sprintf( p, " %16.9e", row[i] );
213 sprintf( p, "\n" );
214 p++;
215 }
216 *p = 0;
217 }
218 return( str );
219}
220/*
221************************************************************
222*/
223static int release( statusMessageReporting *smr, xDataType *xDT ) {
224
225 if( xDT->data != NULL ) xDT->data = xData_free( smr, xDT->data );
226 return( xDT->status = xData_xDataType_Ok );
227}
228/*
229************************************************************
230*/
232
233 int status = 0;
234 xData_matrix *matrix = (xData_matrix *) xDT->data;
235
236 if( !xData_is_matrix( smr, xDT, 1 ) ) return( 1 );
237 if( ( index < 0 ) || ( index >= xDT->length ) ) {
238 smr_setMessageError( smr, xData_get_smrUserInterfaceFromElement( xDT->element ), __FILE__, __LINE__, 1,
239 "index = %lld out of range (valid range 0 <= index < %lld)", index, xDT->length );
240 status = 1; }
241 else {
242 *row = matrix->rowStartEnds[index].row;
243 *start = matrix->rowStartEnds[index].start;
244 *end = matrix->rowStartEnds[index].end;
245 }
246 return( status );
247}
248
249#if defined __cplusplus
250}
251#endif
int smr_setMessageError(statusMessageReporting *smr, void *userInterface, const char *file, int line, int code, const char *fmt,...)
xData_Int length
Definition: xData.h:162
xData_element * element
Definition: xData.h:157
enum xData_xDataType status
Definition: xData.h:155
xDT_releaseFunction release
Definition: xData.h:160
const char * typeString
Definition: xData.h:156
xDT_toStringFunction toString
Definition: xData.h:159
void * data
Definition: xData.h:163
xDT_toDataFunction toData
Definition: xData.h:158
xDataType xDataTypeInfo
Definition: xData.h:187
xData_Int columns
Definition: xData.h:111
xData_matrix_rowStartEnd * rowStartEnds
Definition: xData.h:112
double * values
Definition: xData.h:113
xData_Int rows
Definition: xData.h:111
xData_matrix * xData_matrix_copyData(statusMessageReporting *smr, xData_element *element)
Definition: xData_matrix.cc:85
void * xData_free(statusMessageReporting *smr, void *p)
Definition: xDataMisc.cc:89
int xData_stringTo_double(statusMessageReporting *smr, void *smrUserInterface, char const *c, double *value, char const *endings, char **e)
Definition: xData.cc:1044
void * xData_get_smrUserInterfaceFromElement(xData_element *element)
Definition: xData.cc:952
int xData_isElement_matrix(statusMessageReporting *smr, xData_element *element, int setMsg)
Definition: xData_matrix.cc:78
int getRowStartEndAtIndex(statusMessageReporting *smr, xDataType *xDT, xData_Int index, xData_Int *row, xData_Int *start, xData_Int *end)
int xData_is_xDataType(statusMessageReporting *smr, xDataType *xDT, char const *const type, int setMsg)
Definition: xData.cc:900
int xData_init_matrix(statusMessageReporting *smr, xData_element *element)
Definition: xData_matrix.cc:55
char const *const xData_matrix_ID
Definition: xData.h:75
int xData_stringTo_xData_Int(statusMessageReporting *smr, void *smrUserInterface, char const *c, xData_Int *value, char const *endings, char **e)
Definition: xData.cc:1010
int xData_matrix_free_copyData(statusMessageReporting *smr, void *data)
int xData_is_matrix(statusMessageReporting *smr, xDataType *xDT, int setMsg)
Definition: xData_matrix.cc:71
int xData_xDataTypeConvertAttributes(statusMessageReporting *smr, xData_element *element)
Definition: xData.cc:668
int xData_setMessageError_ReturnInt(int value, statusMessageReporting *smr, void *userData, const char *file, int line, int code, const char *fmt,...)
Definition: xDataMisc.cc:160
char * xData_getAttributesValue(xData_attributionList *attributes, const char *name)
Definition: xData.cc:530
#define xData_malloc2(smr, size, zero, forItem)
Definition: xData.h:313
int xData_Int
Definition: xData.h:50
@ xData_xDataType_Ok
Definition: xData.h:81