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

Go to the source code of this file.

Functions

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

Function Documentation

◆ ptwXY_applyFunction()

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

Definition at line 165 of file ptwXY_misc.c.

166 {
167
168 int64_t i, originalLength = ptwXY1->length, notFirstPass = 0;
169 double y1, y2 = 0;
170 nfu_status status;
171 ptwXYPoint p1, p2;
172
173 checkForRoots = checkForRoots && ptwXY1->biSectionMax;
174
175 if( ptwXY1->interpolation == ptwXY_interpolationOther ) {
176 smr_setReportError2p( smr, nfu_SMR_libraryID, nfu_otherInterpolation, "Other interpolation not allowed." );
177 return( ptwXY1->status = nfu_otherInterpolation );
178 }
179 if( ptwXY1->interpolation == ptwXY_interpolationFlat ) {
180 smr_setReportError2p( smr, nfu_SMR_libraryID, nfu_flatInterpolation, "Flat interpolation not allowed." );
181 return( ptwXY1->status = nfu_flatInterpolation );
182 }
183
184 if( ptwXY_simpleCoalescePoints( smr, ptwXY1 ) != nfu_Okay ) goto Err;
185 for( i = originalLength - 1; i >= 0; i-- ) {
186 y1 = ptwXY1->points[i].y;
187 if( ( status = func( smr, &(ptwXY1->points[i]), argList ) ) != nfu_Okay ) {
188 if( ptwXY1->status == nfu_Okay ) ptwXY1->status = status;
189 return( status );
190 }
191 p1 = ptwXY1->points[i];
192 if( notFirstPass ) {
193 if( ptwXY_applyFunction2( smr, ptwXY1, y1, y2, &p1, &p2, func, argList, 0, checkForRoots ) != nfu_Okay ) goto Err;
194 }
195 notFirstPass = 1;
196 p2 = p1;
197 y2 = y1;
198 }
199 ptwXY_update_biSectionMax( ptwXY1, (double) originalLength );
200 return( status );
201
202Err:
204 if( ptwXY1->status == nfu_Okay ) ptwXY1->status = nfu_Error;
205 return( nfu_Error );
206}
@ nfu_Okay
@ nfu_flatInterpolation
@ nfu_Error
@ nfu_otherInterpolation
enum nfu_status_e nfu_status
int nfu_SMR_libraryID
@ ptwXY_interpolationFlat
Definition ptwXY.h:38
@ ptwXY_interpolationOther
Definition ptwXY.h:38
struct ptwXYPoint_s ptwXYPoint
nfu_status ptwXY_simpleCoalescePoints(statusMessageReporting *smr, ptwXYPoints *ptwXY)
Definition ptwXY_core.c:734
void ptwXY_update_biSectionMax(ptwXYPoints *ptwXY1, double oldLength)
Definition ptwXY_misc.c:37
#define smr_setReportError2p(smr, libraryID, code, fmt)
double y
Definition ptwXY.h:64
ptwXYPoint * points
Definition ptwXY.h:93
ptwXY_interpolation interpolation
Definition ptwXY.h:81
double biSectionMax
Definition ptwXY.h:84
int64_t length
Definition ptwXY.h:87
nfu_status status
Definition ptwXY.h:80

Referenced by ptwXY_pow().

◆ ptwXY_createFromFunction()

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

Definition at line 46 of file ptwXY_misc.c.

47 {
48
49 int64_t i;
50 double x1, y1, x2, y2, eps = ClosestAllowXFactor * DBL_EPSILON;
51 ptwXYPoints *ptwXY;
52 ptwXYPoint *p1, *p2;
53
54 if( n < 2 ) {
55 smr_setReportError2( smr, nfu_SMR_libraryID, nfu_tooFewPoints, "Too few point = %d.", (int) n );
56 return( NULL );
57 }
58 for( i = 1; i < n; i++ ) {
59 if( xs[i-1] >= xs[i] ) {
61 "Non-ascending domain values: xs[%d] = %.17e >= xs[%d] = %.17e.",
62 (int) (i-1), xs[i-1], (int) i, xs[i] );
63 return( NULL );
64 }
65 }
66
67 x1 = xs[0];
68 if( func( smr, x1, &y1, argList ) != nfu_Okay ) {
69 return( NULL );
70 }
71 if( ( ptwXY = ptwXY_new( smr, ptwXY_interpolationLinLin, NULL, biSectionMax, accuracy, 500, 50, 0 ) ) == NULL ) goto Err;
72 for( i = 1; i < n; i++ ) {
73 if( ptwXY_setValueAtX_overrideIfClose( smr, ptwXY, x1, y1, eps, 0 ) != nfu_Okay ) goto Err;
74 x2 = xs[i];
75 if( func( smr, x2, &y2, argList ) != nfu_Okay ) goto Err;
76 if( ptwXY_createFromFunctionBisect( smr, ptwXY, x1, y1, x2, y2, func, argList, 0, checkForRoots, eps ) != nfu_Okay ) goto Err;
77 x1 = x2;
78 y1 = y2;
79 }
80 if( ptwXY_setValueAtX_overrideIfClose( smr, ptwXY, x2, y2, eps, 1 ) != nfu_Okay ) goto Err;
81
82 if( checkForRoots ) {
83 if( ptwXY_simpleCoalescePoints( NULL, ptwXY ) != nfu_Okay ) goto Err;
84 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. */
85 p1 = &(ptwXY->points[i]);
86 if( p2 != NULL ) {
87 if( ( p1->y * p2->y ) < 0. ) {
88 if( ptwXY_createFromFunctionZeroCrossing( smr, ptwXY, p1->x, p1->y, p2->x, p2->y, func, argList, eps ) != nfu_Okay ) goto Err;
89 }
90 }
91 }
92 }
93
94 return( ptwXY );
95
96Err:
98 if( ptwXY != NULL ) ptwXY_free( ptwXY );
99 return( NULL );
100}
@ nfu_XNotAscending
@ nfu_tooFewPoints
@ ptwXY_interpolationLinLin
Definition ptwXY.h:37
struct ptwXYPoints_s ptwXYPoints
ptwXYPoints * ptwXY_new(statusMessageReporting *smr, ptwXY_interpolation interpolation, char const *interpolationString, double biSectionMax, double accuracy, int64_t primarySize, int64_t secondarySize, int userFlag)
Definition ptwXY_core.c:28
#define ClosestAllowXFactor
Definition ptwXY.h:28
ptwXYPoints * ptwXY_free(ptwXYPoints *ptwXY)
Definition ptwXY_core.c:782
nfu_status ptwXY_setValueAtX_overrideIfClose(statusMessageReporting *smr, ptwXYPoints *ptwXY, double x, double y, double eps, int override)
#define smr_setReportError2(smr, libraryID, code, fmt,...)
double x
Definition ptwXY.h:64
#define DBL_EPSILON
Definition templates.hh:66

Referenced by GIDI::Functions::Polynomial1d::asXYs1d(), MCGIDI::Probabilities::NBodyPhaseSpace2d::NBodyPhaseSpace2d(), nf_Legendre_to_ptwXY(), and ptwXY_createFromFunction2().

◆ ptwXY_createFromFunction2()

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

Definition at line 104 of file ptwXY_misc.c.

105 {
106
107 ptwXYPoints *ptwXY = ptwXY_createFromFunction( smr, (int) xs->length, xs->points, func, argList, accuracy,
108 checkForRoots, biSectionMax );
109
110 if( ptwXY == NULL ) smr_setReportError2p( smr, nfu_SMR_libraryID, nfu_Error, "Via." );
111 return( ptwXY );
112}
ptwXYPoints * ptwXY_createFromFunction(statusMessageReporting *smr, int n, double *xs, ptwXY_createFromFunction_callback func, void *argList, double accuracy, int checkForRoots, int biSectionMax)
Definition ptwXY_misc.c:46
int64_t length
Definition ptwX.h:29
double * points
Definition ptwX.h:32

◆ ptwXY_fromString()

ptwXYPoints * ptwXY_fromString ( statusMessageReporting * smr,
char const * str,
char sep,
ptwXY_interpolation interpolation,
char const * interpolationString,
double biSectionMax,
double accuracy,
char ** endCharacter,
int useSystem_strtod )

Definition at line 275 of file ptwXY_misc.c.

276 {
277
278 int64_t numberConverted;
279 double *doublePtr;
280 ptwXYPoints *ptwXY = NULL;
281
282 if( ( doublePtr = nfu_stringToListOfDoubles( smr, str, sep, &numberConverted, endCharacter, useSystem_strtod ) ) == NULL ) {
284 return( NULL );
285 }
286 if( ( numberConverted % 2 ) == 0 ) {
287 ptwXY = ptwXY_create( smr, interpolation, interpolationString, biSectionMax, accuracy, numberConverted / 2, 10, numberConverted / 2, doublePtr, 0 ); }
288 else {
289 smr_setReportError2( smr, nfu_SMR_libraryID, nfu_oddNumberOfValues, "Odd number = %d of float for ptwXY.", (int) numberConverted );
290 }
291 smr_freeMemory2( doublePtr );
292 return( ptwXY );
293}
double * nfu_stringToListOfDoubles(statusMessageReporting *smr, char const *str, char sep, int64_t *numberConverted, char **endCharacter, int useSystem_strtod)
@ nfu_oddNumberOfValues
ptwXYPoints * ptwXY_create(statusMessageReporting *smr, ptwXY_interpolation interpolation, char const *interpolationString, double biSectionMax, double accuracy, int64_t primarySize, int64_t secondarySize, int64_t length, double const *xy, int userFlag)
Definition ptwXY_core.c:110
#define smr_freeMemory2(p)

◆ ptwXY_limitAccuracy()

double ptwXY_limitAccuracy ( double accuracy)

Definition at line 28 of file ptwXY_misc.c.

28 {
29
30 if( accuracy < ptwXY_minAccuracy ) accuracy = ptwXY_minAccuracy;
31 if( accuracy > 1 ) accuracy = 1.;
32 return( accuracy );
33}
#define ptwXY_minAccuracy
Definition ptwXY.h:26

Referenced by ptwXY_setAccuracy(), ptwXY_thin(), and ptwXY_toOtherInterpolation().

◆ ptwXY_showInteralStructure()

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

Definition at line 297 of file ptwXY_misc.c.

297 {
298
299 int64_t i, n1;
300 ptwXYPoint *point = ptwXY->points;
301 ptwXYOverflowPoint *overflowPoint;
302
303 n1 = ptwXY_getNonOverflowLength( NULL, ptwXY );
304
305 fprintf( f, "status = %d interpolation = %d length = %d allocatedSize = %d\n",
306 (int) ptwXY->status, (int) ptwXY->interpolation, (int) ptwXY->length, (int) ptwXY->allocatedSize );
307 fprintf( f, "userFlag = %d biSectionMax = %.8e accuracy = %.2e minFractional_dx = %.6e\n",
308 ptwXY->userFlag, ptwXY->biSectionMax, ptwXY->accuracy, ptwXY->minFractional_dx );
309 fprintf( f, "interpolationString = %s\n", ptwXY->interpolationString );
310 fprintf( f, " overflowLength = %d overflowAllocatedSize = %d mallocFailedSize = %d\n",
311 (int) ptwXY->overflowLength, (int) ptwXY->overflowAllocatedSize, (int) ptwXY->mallocFailedSize );
312 fprintf( f, " Points data, points = %20p\n", ( printPointersAsNull ? NULL : ptwXY->points ) );
313 for( i = 0; i < n1; i++, point++ ) fprintf( f, " %14.7e %14.7e\n", point->x, point->y );
314 fprintf( f, " Overflow points data; %20p\n", ( printPointersAsNull ? NULL : &(ptwXY->overflowHeader) ) );
315 for( overflowPoint = ptwXY->overflowHeader.next; overflowPoint != &(ptwXY->overflowHeader); overflowPoint = overflowPoint->next ) {
316 fprintf( f, " %14.7e %14.7e %8d %20p %20p %20p\n", overflowPoint->point.x, overflowPoint->point.y, (int) overflowPoint->index,
317 ( printPointersAsNull ? NULL : overflowPoint ), ( printPointersAsNull ? NULL : overflowPoint->prior ),
318 ( printPointersAsNull ? NULL : overflowPoint->next ) );
319 }
320 fprintf( f, " Points in order\n" );
321 for( i = 0; i < ptwXY->length; i++ ) {
322 point = ptwXY_getPointAtIndex_Unsafely( ptwXY, i );
323 fprintf( f, " %14.7e %14.7e\n", point->x, point->y );
324 }
325}
int64_t ptwXY_getNonOverflowLength(statusMessageReporting *smr, ptwXYPoints const *ptwXY)
Definition ptwXY_core.c:805
struct ptwXYOverflowPoint_s ptwXYOverflowPoint
ptwXYPoint * ptwXY_getPointAtIndex_Unsafely(ptwXYPoints const *ptwXY, int64_t index)
struct ptwXYOverflowPoint_s * next
Definition ptwXY.h:73
ptwXYPoint point
Definition ptwXY.h:75
double minFractional_dx
Definition ptwXY.h:86
ptwXYOverflowPoint overflowHeader
Definition ptwXY.h:92
int userFlag
Definition ptwXY.h:83
double accuracy
Definition ptwXY.h:85
int64_t overflowLength
Definition ptwXY.h:89
int64_t overflowAllocatedSize
Definition ptwXY.h:90
int64_t mallocFailedSize
Definition ptwXY.h:91
int64_t allocatedSize
Definition ptwXY.h:88
char const * interpolationString
Definition ptwXY.h:82

◆ ptwXY_simplePrint()

void ptwXY_simplePrint ( ptwXYPoints * ptwXY,
char const * format )

Definition at line 342 of file ptwXY_misc.c.

342 {
343
344 ptwXY_simpleWrite( ptwXY, stdout, format );
345}
void ptwXY_simpleWrite(ptwXYPoints *ptwXY, FILE *f, char const *format)
Definition ptwXY_misc.c:329

◆ ptwXY_simpleWrite()

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

Definition at line 329 of file ptwXY_misc.c.

329 {
330
331 int64_t i;
332 ptwXYPoint *point;
333
334 for( i = 0; i < ptwXY->length; i++ ) {
335 point = ptwXY_getPointAtIndex_Unsafely( ptwXY, i );
336 fprintf( f, format, point->x, point->y );
337 }
338}

Referenced by ptwXY_simplePrint(), and GIDI::Functions::XYs1d::write().

◆ ptwXY_update_biSectionMax()

void ptwXY_update_biSectionMax ( ptwXYPoints * ptwXY1,
double oldLength )

Definition at line 37 of file ptwXY_misc.c.

37 {
38
39 ptwXY1->biSectionMax = ptwXY1->biSectionMax - 1.442695 * log( ptwXY1->length / oldLength ); /* 1.442695 = 1 / log( 2. ) */
40 if( ptwXY1->biSectionMax < 0 ) ptwXY1->biSectionMax = 0;
42}
#define ptwXY_maxBiSectionMax
Definition ptwXY.h:25

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