Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
ptwXY_misc.cc File Reference
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <float.h>
#include "ptwXY.h"

Go to the source code of this file.

Functions

void ptwXY_update_biSectionMax (ptwXYPoints *ptwXY1, double oldLength)
 
ptwXYPointsptwXY_createFromFunction (int n, double *xs, ptwXY_createFromFunction_callback func, void *argList, double accuracy, int checkForRoots, int biSectionMax, nfu_status *status)
 
ptwXYPointsptwXY_createFromFunction2 (ptwXPoints *xs, ptwXY_createFromFunction_callback func, void *argList, double accuracy, int checkForRoots, int biSectionMax, nfu_status *status)
 
nfu_status ptwXY_applyFunction (ptwXYPoints *ptwXY1, ptwXY_applyFunction_callback func, void *argList, int checkForRoots)
 
ptwXYPointsptwXY_fromString (char const *str, ptwXY_interpolation interpolation, ptwXY_interpolationOtherInfo const *interpolationOtherInfo, double biSectionMax, double accuracy, char **endCharacter, nfu_status *status)
 
void ptwXY_showInteralStructure (ptwXYPoints *ptwXY, FILE *f, int printPointersAsNull)
 
void ptwXY_simpleWrite (ptwXYPoints *ptwXY, FILE *f, char *format)
 
void ptwXY_simplePrint (ptwXYPoints *ptwXY, char *format)
 

Function Documentation

◆ ptwXY_applyFunction()

nfu_status ptwXY_applyFunction ( ptwXYPoints ptwXY1,
ptwXY_applyFunction_callback  func,
void *  argList,
int  checkForRoots 
)

Definition at line 146 of file ptwXY_misc.cc.

146 {
147
148 int64_t i, originalLength = ptwXY1->length, notFirstPass = 0;
149 double y1, y2 = 0;
150 nfu_status status;
151 ptwXYPoint p1, p2;
152
153 checkForRoots = checkForRoots && ptwXY1->biSectionMax;
154 if( ptwXY1->status != nfu_Okay ) return( ptwXY1->status );
157 if( ( status = ptwXY_simpleCoalescePoints( ptwXY1 ) ) != nfu_Okay ) return( status );
158 for( i = originalLength - 1; i >= 0; i-- ) {
159 y1 = ptwXY1->points[i].y;
160 if( ( status = func( &(ptwXY1->points[i]), argList ) ) != nfu_Okay ) return( status );
161 p1 = ptwXY1->points[i];
162 if( notFirstPass ) {
163 if( ( status = ptwXY_applyFunction2( ptwXY1, y1, y2, &p1, &p2, func, argList, 0, checkForRoots ) ) != nfu_Okay ) return( status );
164 }
165 notFirstPass = 1;
166 p2 = p1;
167 y2 = y1;
168 }
169 ptwXY_update_biSectionMax( ptwXY1, (double) originalLength );
170 return( status );
171}
@ nfu_invalidInterpolation
Definition: nf_utilities.h:27
@ nfu_Okay
Definition: nf_utilities.h:25
@ nfu_otherInterpolation
Definition: nf_utilities.h:29
enum nfu_status_e nfu_status
nfu_status ptwXY_simpleCoalescePoints(ptwXYPoints *ptwXY)
Definition: ptwXY_core.cc:529
@ ptwXY_interpolationFlat
Definition: ptwXY.h:36
@ ptwXY_interpolationOther
Definition: ptwXY.h:36
void ptwXY_update_biSectionMax(ptwXYPoints *ptwXY1, double oldLength)
Definition: ptwXY_misc.cc:31
double y
Definition: ptwXY.h:62
ptwXYPoint * points
Definition: ptwXY.h:99
ptwXY_interpolation interpolation
Definition: ptwXY.h:87
double biSectionMax
Definition: ptwXY.h:90
int64_t length
Definition: ptwXY.h:93
nfu_status status
Definition: ptwXY.h:85

Referenced by ptwXY_pow().

◆ ptwXY_createFromFunction()

ptwXYPoints * ptwXY_createFromFunction ( int  n,
double *  xs,
ptwXY_createFromFunction_callback  func,
void *  argList,
double  accuracy,
int  checkForRoots,
int  biSectionMax,
nfu_status status 
)

Definition at line 40 of file ptwXY_misc.cc.

41 {
42
43 int64_t i;
44 double x1, y1, x2 = 0., y2, eps = ClosestAllowXFactor * DBL_EPSILON;
45 ptwXYPoints *ptwXY;
46 ptwXYPoint *p1, *p2;
47
48 *status = nfu_Okay;
49 if( n < 2 ) { *status = nfu_tooFewPoints; return( NULL ); }
50 for( i = 1; i < n; i++ ) {
51 if( xs[i-1] >= xs[i] ) *status = nfu_XNotAscending;
52 }
53 if( *status == nfu_XNotAscending ) return( NULL );
54
55 x1 = xs[0];
56 if( ( *status = func( x1, &y1, argList ) ) != nfu_Okay ) return( NULL );
57 if( ( ptwXY = ptwXY_new( ptwXY_interpolationLinLin, NULL, biSectionMax, accuracy, 500, 50, status, 0 ) ) == NULL ) return( NULL );
58 for( i = 1; i < n; i++ ) {
59 if( ( *status = ptwXY_setValueAtX_overrideIfClose( ptwXY, x1, y1, eps, 0 ) ) != nfu_Okay ) goto err;
60 x2 = xs[i];
61 if( ( *status = func( x2, &y2, argList ) ) != nfu_Okay ) goto err;
62 if( ( *status = ptwXY_createFromFunctionBisect( ptwXY, x1, y1, x2, y2, func, argList, 0, checkForRoots, eps ) ) != nfu_Okay ) goto err;
63 x1 = x2;
64 y1 = y2;
65 }
66 if( ( *status = ptwXY_setValueAtX_overrideIfClose( ptwXY, x2, y2, eps, 1 ) ) != nfu_Okay ) goto err;
67
68 if( checkForRoots ) {
69 if( ( *status = ptwXY_simpleCoalescePoints( ptwXY ) ) != nfu_Okay ) goto err;
70 for( i = ptwXY->length - 1, p2 = NULL; i >= 0; i--, p2 = p1 ) { /* Work backward so lower points are still valid if a new point is added. */
71 p1 = &(ptwXY->points[i]);
72 if( p2 != NULL ) {
73 if( ( p1->y * p2->y ) < 0. ) {
74 if( ( *status = ptwXY_createFromFunctionZeroCrossing( ptwXY, p1->x, p1->y, p2->x, p2->y, func, argList, eps ) ) != nfu_Okay ) goto err;
75 }
76 }
77 }
78 }
79
80 return( ptwXY );
81
82err:
83 ptwXY_free( ptwXY );
84 return( NULL );
85}
@ nfu_XNotAscending
Definition: nf_utilities.h:26
@ nfu_tooFewPoints
Definition: nf_utilities.h:28
ptwXYPoints * ptwXY_new(ptwXY_interpolation interpolation, ptwXY_interpolationOtherInfo const *interpolationOtherInfo, double biSectionMax, double accuracy, int64_t primarySize, int64_t secondarySize, nfu_status *status, int userFlag)
Definition: ptwXY_core.cc:29
nfu_status ptwXY_setValueAtX_overrideIfClose(ptwXYPoints *ptwXY, double x, double y, double eps, int override)
Definition: ptwXY_core.cc:883
@ ptwXY_interpolationLinLin
Definition: ptwXY.h:35
#define ClosestAllowXFactor
Definition: ptwXY.h:25
ptwXYPoints * ptwXY_free(ptwXYPoints *ptwXY)
Definition: ptwXY_core.cc:574
double x
Definition: ptwXY.h:62
#define DBL_EPSILON
Definition: templates.hh:66

Referenced by nf_Legendre_to_ptwXY(), and ptwXY_createFromFunction2().

◆ ptwXY_createFromFunction2()

ptwXYPoints * ptwXY_createFromFunction2 ( ptwXPoints xs,
ptwXY_createFromFunction_callback  func,
void *  argList,
double  accuracy,
int  checkForRoots,
int  biSectionMax,
nfu_status status 
)

Definition at line 89 of file ptwXY_misc.cc.

90 {
91
92 return( ptwXY_createFromFunction( (int) xs->length, xs->points, func, argList, accuracy, checkForRoots, biSectionMax, status ) );
93}
ptwXYPoints * ptwXY_createFromFunction(int n, double *xs, ptwXY_createFromFunction_callback func, void *argList, double accuracy, int checkForRoots, int biSectionMax, nfu_status *status)
Definition: ptwXY_misc.cc:40
int64_t length
Definition: ptwX.h:26
double * points
Definition: ptwX.h:29

◆ ptwXY_fromString()

ptwXYPoints * ptwXY_fromString ( char const *  str,
ptwXY_interpolation  interpolation,
ptwXY_interpolationOtherInfo const *  interpolationOtherInfo,
double  biSectionMax,
double  accuracy,
char **  endCharacter,
nfu_status status 
)

Definition at line 236 of file ptwXY_misc.cc.

237 {
238
239 int64_t numberConverted;
240 double *doublePtr;
241 ptwXYPoints *ptwXY = NULL;
242
243 if( ( *status = nfu_stringToListOfDoubles( str, &numberConverted, &doublePtr, endCharacter ) ) != nfu_Okay ) return( NULL );
244 *status = nfu_oddNumberOfValues;
245 if( ( numberConverted % 2 ) == 0 )
246 ptwXY = ptwXY_create( interpolation, interpolationOtherInfo, biSectionMax, accuracy, numberConverted, 10, numberConverted / 2, doublePtr, status, 0 );
247 nfu_free( doublePtr );
248 return( ptwXY );
249}
@ nfu_oddNumberOfValues
Definition: nf_utilities.h:30
void * nfu_free(void *p)
nfu_status nfu_stringToListOfDoubles(char const *str, int64_t *numberConverted, double **doublePtr, char **endCharacter)
ptwXYPoints * ptwXY_create(ptwXY_interpolation interpolation, ptwXY_interpolationOtherInfo const *interpolationOtherInfo, double biSectionMax, double accuracy, int64_t primarySize, int64_t secondarySize, int64_t length, double const *xy, nfu_status *status, int userFlag)
Definition: ptwXY_core.cc:108

◆ ptwXY_showInteralStructure()

void ptwXY_showInteralStructure ( ptwXYPoints ptwXY,
FILE *  f,
int  printPointersAsNull 
)

Definition at line 253 of file ptwXY_misc.cc.

253 {
254
255 int64_t i, n = ptwXY_getNonOverflowLength( ptwXY );
256 ptwXYPoint *point = ptwXY->points;
257 ptwXYOverflowPoint *overflowPoint;
258
259 fprintf( f, "status = %d interpolation = %d length = %d allocatedSize = %d\n",
260 (int) ptwXY->status, (int) ptwXY->interpolation, (int) ptwXY->length, (int) ptwXY->allocatedSize );
261 fprintf( f, "userFlag = %d biSectionMax = %.8e accuracy = %.2e minFractional_dx = %.6e\n",
262 ptwXY->userFlag, ptwXY->biSectionMax, ptwXY->accuracy, ptwXY->minFractional_dx );
263 fprintf( f, "interpolationString = %s\n", ptwXY->interpolationOtherInfo.interpolationString );
264 fprintf( f, "getValueFunc is NULL = %d. argList is NULL = %d.\n",
265 ptwXY->interpolationOtherInfo.getValueFunc == NULL, ptwXY->interpolationOtherInfo.argList == NULL );
266 fprintf( f, " overflowLength = %d overflowAllocatedSize = %d mallocFailedSize = %d\n",
267 (int) ptwXY->overflowLength, (int) ptwXY->overflowAllocatedSize, (int) ptwXY->mallocFailedSize );
268 fprintf( f, " Points data, points = %20p\n", ( printPointersAsNull ? NULL : (void*)ptwXY->points ) );
269 for( i = 0; i < n; i++, point++ ) fprintf( f, " %14.7e %14.7e\n", point->x, point->y );
270 fprintf( f, " Overflow points data; %20p\n", ( printPointersAsNull ? NULL : (void*)&(ptwXY->overflowHeader) ) );
271 for( overflowPoint = ptwXY->overflowHeader.next; overflowPoint != &(ptwXY->overflowHeader); overflowPoint = overflowPoint->next ) {
272 fprintf( f, " %14.7e %14.7e %8d %20p %20p %20p\n", overflowPoint->point.x, overflowPoint->point.y, (int) overflowPoint->index,
273 (void*) ( printPointersAsNull ? NULL : overflowPoint ), (void*) ( printPointersAsNull ? NULL : overflowPoint->prior ),
274 (void*) ( printPointersAsNull ? NULL : overflowPoint->next ) );
275 }
276 fprintf( f, " Points in order\n" );
277 for( i = 0; i < ptwXY->length; i++ ) {
278 point = ptwXY_getPointAtIndex( ptwXY, i );
279 fprintf( f, " %14.7e %14.7e\n", point->x, point->y );
280 }
281}
int64_t ptwXY_getNonOverflowLength(ptwXYPoints const *ptwXY)
Definition: ptwXY_core.cc:590
ptwXYPoint * ptwXY_getPointAtIndex(ptwXYPoints *ptwXY, int64_t index)
Definition: ptwXY_core.cc:675
struct ptwXYOverflowPoint_s * next
Definition: ptwXY.h:78
int64_t index
Definition: ptwXY.h:79
ptwXYPoint point
Definition: ptwXY.h:80
double minFractional_dx
Definition: ptwXY.h:92
ptwXYOverflowPoint overflowHeader
Definition: ptwXY.h:98
int userFlag
Definition: ptwXY.h:89
double accuracy
Definition: ptwXY.h:91
ptwXY_interpolationOtherInfo interpolationOtherInfo
Definition: ptwXY.h:88
int64_t overflowLength
Definition: ptwXY.h:95
int64_t overflowAllocatedSize
Definition: ptwXY.h:96
int64_t mallocFailedSize
Definition: ptwXY.h:97
int64_t allocatedSize
Definition: ptwXY.h:94
char const * interpolationString
Definition: ptwXY.h:70
ptwXY_getValue_callback getValueFunc
Definition: ptwXY.h:71

◆ ptwXY_simplePrint()

void ptwXY_simplePrint ( ptwXYPoints ptwXY,
char *  format 
)

Definition at line 298 of file ptwXY_misc.cc.

298 {
299
300 ptwXY_simpleWrite( ptwXY, stdout, format );
301}
void ptwXY_simpleWrite(ptwXYPoints *ptwXY, FILE *f, char *format)
Definition: ptwXY_misc.cc:285

◆ ptwXY_simpleWrite()

void ptwXY_simpleWrite ( ptwXYPoints ptwXY,
FILE *  f,
char *  format 
)

Definition at line 285 of file ptwXY_misc.cc.

285 {
286
287 int64_t i;
288 ptwXYPoint *point;
289
290 for( i = 0; i < ptwXY->length; i++ ) {
291 point = ptwXY_getPointAtIndex( ptwXY, i );
292 fprintf( f, format, point->x, point->y );
293 }
294}

Referenced by ptwXY_simplePrint().

◆ ptwXY_update_biSectionMax()

void ptwXY_update_biSectionMax ( ptwXYPoints ptwXY1,
double  oldLength 
)

Definition at line 31 of file ptwXY_misc.cc.

31 {
32
33 ptwXY1->biSectionMax = ptwXY1->biSectionMax - 1.442695 * G4Log( ptwXY1->length / oldLength ); /* 1.442695 = 1 / std::log( 2. ) */
34 if( ptwXY1->biSectionMax < 0 ) ptwXY1->biSectionMax = 0;
36}
G4double G4Log(G4double x)
Definition: G4Log.hh:227
#define ptwXY_maxBiSectionMax
Definition: ptwXY.h:22

Referenced by ptwXY_applyFunction(), ptwXY_div_ptwXY(), and ptwXY_mul2_ptwXY().