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

Go to the source code of this file.

Macros

#define minEps   5e-16
 

Typedefs

typedef nfu_status(* interpolation_func) (ptwXYPoints *desc, double x1, double y1, double x2, double y2, int depth)
 

Functions

nfu_status ptwXY_interpolatePoint (ptwXY_interpolation interpolation, double x, double *y, double x1, double y1, double x2, double y2)
 
ptwXYPointsptwXY_flatInterpolationToLinear (ptwXYPoints *ptwXY, double lowerEps, double upperEps, nfu_status *status)
 
ptwXYPointsptwXY_toOtherInterpolation (ptwXYPoints *ptwXY, ptwXY_interpolation interpolationTo, double accuracy, nfu_status *status)
 
ptwXYPointsptwXY_toUnitbase (ptwXYPoints *ptwXY, nfu_status *status)
 
ptwXYPointsptwXY_fromUnitbase (ptwXYPoints *ptwXY, double xMin, double xMax, nfu_status *status)
 
ptwXYPointsptwXY_unitbaseInterpolate (double w, double w1, ptwXYPoints *ptwXY1, double w2, ptwXYPoints *ptwXY2, nfu_status *status)
 

Macro Definition Documentation

◆ minEps

#define minEps   5e-16

Typedef Documentation

◆ interpolation_func

typedef nfu_status(* interpolation_func) (ptwXYPoints *desc, double x1, double y1, double x2, double y2, int depth)

Definition at line 19 of file ptwXY_interpolation.cc.

Function Documentation

◆ ptwXY_flatInterpolationToLinear()

ptwXYPoints * ptwXY_flatInterpolationToLinear ( ptwXYPoints ptwXY,
double  lowerEps,
double  upperEps,
nfu_status status 
)

Definition at line 74 of file ptwXY_interpolation.cc.

74 {
75
76 int64_t i, length;
77 double x;
79 ptwXYPoint *p1 = NULL, *p2 = NULL, *p3;
80
81#define minEps 5e-16
82
83 if( ( *status = ptwXY_simpleCoalescePoints( ptwXY ) ) != nfu_Okay ) return( NULL );
85 if( ptwXY->interpolation != ptwXY_interpolationFlat ) return( NULL );
86 *status = nfu_badInput;
87 if( ( lowerEps < 0 ) || ( upperEps < 0 ) || ( ( lowerEps == 0 ) && ( upperEps == 0 ) ) ) return( NULL );
88 if( ( lowerEps != 0 ) && ( lowerEps < minEps ) ) lowerEps = minEps;
89 if( ( upperEps != 0 ) && ( upperEps < minEps ) ) upperEps = minEps;
90
91 length = ptwXY->length * ( 1 + ( lowerEps == 0 ? 0 : 1 ) + ( lowerEps == 0 ? 0 : 1 ) );
92 if( ( n = ptwXY_new( ptwXY_interpolationLinLin, NULL, ptwXY->biSectionMax, ptwXY->accuracy, length, ptwXY->overflowLength, status, ptwXY->userFlag ) ) == NULL ) return( NULL );
93
94 p3 = ptwXY->points;
95 if( ptwXY->length > 0 ) ptwXY_setValueAtX( n, p3->x, p3->y );
96 for( i = 0; i < ptwXY->length; i++, p3++ ) {
97 if( i > 1 ) {
98 if( lowerEps > 0 ) {
99 x = ptwXY_flatInterpolationToLinear_eps( p2->x, -lowerEps );
100 if( x > p1->x ) {
101 if( ( *status = ptwXY_setValueAtX( n, x, p1->y ) ) != nfu_Okay ) goto Err;
102 }
103 }
104 if( lowerEps == 0 ) if( ( *status = ptwXY_setValueAtX( n, p2->x, p1->y ) ) != nfu_Okay ) goto Err;
105 if( upperEps == 0 ) if( ( *status = ptwXY_setValueAtX( n, p2->x, p2->y ) ) != nfu_Okay ) goto Err;
106 if( upperEps > 0 ) {
107 x = ptwXY_flatInterpolationToLinear_eps( p2->x, upperEps );
108 if( x < p3->x ) {
109 if( ( *status = ptwXY_setValueAtX( n, x, p2->y ) ) != nfu_Okay ) goto Err;
110 }
111 }
112 }
113 p1 = p2;
114 p2 = p3;
115 }
116 if( ptwXY->length > 1 ) {
117 if( ( lowerEps != 0 ) && ( p1->y != p2->y ) ) {
118 x = ptwXY_flatInterpolationToLinear_eps( p2->x, -lowerEps );
119 if( x > p1->x ) {
120 if( ( *status = ptwXY_setValueAtX( n, x, p1->y ) ) != nfu_Okay ) goto Err;
121 }
122 }
123 if( ( *status = ptwXY_setValueAtX( n, p2->x, p2->y ) ) != nfu_Okay ) goto Err;
124 }
125
126 return( n );
127
128Err:
129 ptwXY_free( n );
130 return( NULL );
131
132#undef minEps
133}
@ nfu_invalidInterpolation
Definition: nf_utilities.h:27
@ nfu_Okay
Definition: nf_utilities.h:25
@ nfu_badInput
Definition: nf_utilities.h:29
nfu_status ptwXY_setValueAtX(ptwXYPoints *ptwXY, double x, double y)
Definition: ptwXY_core.cc:876
nfu_status ptwXY_simpleCoalescePoints(ptwXYPoints *ptwXY)
Definition: ptwXY_core.cc:529
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
@ ptwXY_interpolationFlat
Definition: ptwXY.h:36
@ ptwXY_interpolationLinLin
Definition: ptwXY.h:35
ptwXYPoints * ptwXY_free(ptwXYPoints *ptwXY)
Definition: ptwXY_core.cc:574
#define minEps
double y
Definition: ptwXY.h:62
double x
Definition: ptwXY.h:62
int userFlag
Definition: ptwXY.h:89
ptwXYPoint * points
Definition: ptwXY.h:99
ptwXY_interpolation interpolation
Definition: ptwXY.h:87
double biSectionMax
Definition: ptwXY.h:90
double accuracy
Definition: ptwXY.h:91
int64_t length
Definition: ptwXY.h:93
int64_t overflowLength
Definition: ptwXY.h:95

◆ ptwXY_fromUnitbase()

ptwXYPoints * ptwXY_fromUnitbase ( ptwXYPoints ptwXY,
double  xMin,
double  xMax,
nfu_status status 
)

Definition at line 331 of file ptwXY_interpolation.cc.

331 {
332
333 int64_t i, length;
334 ptwXYPoints *n;
335 ptwXYPoint *p, *p2;
336 double dx, inverseDx, xLast = 0.;
337
338 *status = nfu_tooFewPoints;
339 if( ptwXY->length < 2 ) return( NULL );
340 if( ( n = ptwXY_clone( ptwXY, status ) ) == NULL ) return( NULL );
341
342 dx = xMax - xMin;
343 inverseDx = 1. / dx;
344 length = n->length;
345 for( i = 0, p2 = p = n->points; i < length; ++i, ++p ) {
346 p2->x = p->x * dx + xMin;
347 if( i > 0 ) {
348 if( std::fabs( p2->x - xLast ) <= 10. * DBL_EPSILON * ( std::fabs( p2->x ) + std::fabs( xLast ) ) ) {
349 --(n->length);
350 continue;
351 }
352 }
353 p2->y = p->y * inverseDx;
354 xLast = p2->x;
355 ++p2;
356 }
357 n->points[n->length-1].x = xMax; /* Make sure last point is realy xMax. */
358 return( n );
359}
@ nfu_tooFewPoints
Definition: nf_utilities.h:28
ptwXYPoints * ptwXY_clone(ptwXYPoints *ptwXY, nfu_status *status)
Definition: ptwXY_core.cc:208
#define DBL_EPSILON
Definition: templates.hh:66

Referenced by ptwXY_unitbaseInterpolate().

◆ ptwXY_interpolatePoint()

nfu_status ptwXY_interpolatePoint ( ptwXY_interpolation  interpolation,
double  x,
double *  y,
double  x1,
double  y1,
double  x2,
double  y2 
)

Definition at line 30 of file ptwXY_interpolation.cc.

30 {
31
32 nfu_status status = nfu_Okay;
33
34 if( interpolation == ptwXY_interpolationOther ) return( nfu_otherInterpolation );
35 if( ( x1 > x2 ) || ( x < x1 ) || ( x > x2 ) ) return( nfu_invalidInterpolation );
36 if( y1 == y2 ) {
37 *y = y1; }
38 else if( x1 == x2 ) {
39 *y = 0.5 * ( y1 + y2 ); }
40 else if( x == x1 ) {
41 *y = y1; }
42 else if( x == x2 ) {
43 *y = y2; }
44 else {
45 switch( interpolation ) {
47 *y = ( y1 * ( x2 - x ) + y2 * ( x - x1 ) ) / ( x2 - x1 );
48 break;
50 if( ( x <= 0. ) || ( x1 <= 0. ) || ( x2 <= 0. ) ) return( nfu_invalidInterpolation );
51 *y = ( y1 * G4Log( x2 / x ) + y2 * G4Log( x / x1 ) ) / G4Log( x2 / x1 );
52 break;
54 if( ( y1 <= 0. ) || ( y2 <= 0. ) ) return( nfu_invalidInterpolation );
55 *y = G4Exp( ( G4Log( y1 ) * ( x2 - x ) + G4Log( y2 ) * ( x - x1 ) ) / ( x2 - x1 ) );
56 break;
58 if( ( x <= 0. ) || ( x1 <= 0. ) || ( x2 <= 0. ) ) return( nfu_invalidInterpolation );
59 if( ( y1 <= 0. ) || ( y2 <= 0. ) ) return( nfu_invalidInterpolation );
60 *y = G4Exp( ( G4Log( y1 ) * G4Log( x2 / x ) + G4Log( y2 ) * G4Log( x / x1 ) ) / G4Log( x2 / x1 ) );
61 break;
63 *y = y1;
64 break;
65 default :
67 }
68 }
69 return( status );
70}
G4double G4Exp(G4double initial_x)
Exponential Function double precision.
Definition: G4Exp.hh:179
G4double G4Log(G4double x)
Definition: G4Log.hh:226
@ nfu_otherInterpolation
Definition: nf_utilities.h:29
enum nfu_status_e nfu_status
@ ptwXY_interpolationLinLog
Definition: ptwXY.h:35
@ ptwXY_interpolationLogLog
Definition: ptwXY.h:35
@ ptwXY_interpolationOther
Definition: ptwXY.h:36
@ ptwXY_interpolationLogLin
Definition: ptwXY.h:35

Referenced by ptwXY_dullEdges(), ptwXY_getValueAtX(), ptwXY_integrate(), ptwXY_integrateWithWeight_sqrt_x(), ptwXY_integrateWithWeight_x(), ptwXY_thicken(), and ptwXY_union().

◆ ptwXY_toOtherInterpolation()

ptwXYPoints * ptwXY_toOtherInterpolation ( ptwXYPoints ptwXY,
ptwXY_interpolation  interpolationTo,
double  accuracy,
nfu_status status 
)

Definition at line 153 of file ptwXY_interpolation.cc.

153 {
154/*
155* This function only works when 'ptwXY->interpolation == interpolationTo' or when interpolationTo is ptwXY_interpolationLinLin.
156*/
157 ptwXYPoints *n1;
158 interpolation_func func = NULL;
159
160 if( ( *status = ptwXY->status ) != nfu_Okay ) return( NULL );
161 if( ptwXY->interpolation == interpolationTo ) {
162 *status = nfu_Okay;
163 return( ptwXY_clone( ptwXY, status ) ); }
164 else {
165 if( interpolationTo == ptwXY_interpolationLinLin ) {
166 switch( ptwXY->interpolation ) {
168 func = ptwXY_LogLogToLinLin; break;
170 func = ptwXY_LinLogToLinLin; break;
172 func = ptwXY_LogLinToLinLin; break;
174 if( ptwXY->interpolationOtherInfo.getValueFunc != NULL ) func = ptwXY_otherToLinLin;
175 break;
176 case ptwXY_interpolationLinLin : /* Stops compilers from complaining. */
178 break;
179 }
180 }
181 }
183 if( func == NULL ) return( NULL );
184
185 *status = nfu_Okay;
186 if( ( n1 = ptwXY_cloneToInterpolation( ptwXY, interpolationTo, status ) ) == NULL ) return( NULL );
187 if( accuracy < ptwXY->accuracy ) accuracy = ptwXY->accuracy;
188 n1->accuracy = accuracy;
189
192 *status = ptwXY_toOtherInterpolation2( n1, ptwXY, func );
195 if( *status != nfu_Okay ) n1 = ptwXY_free( n1 );
196 return( n1 );
197}
@ nfu_unsupportedInterpolationConversion
Definition: nf_utilities.h:27
ptwXYPoints * ptwXY_cloneToInterpolation(ptwXYPoints *ptwXY, ptwXY_interpolation interpolationTo, nfu_status *status)
Definition: ptwXY_core.cc:215
nfu_status(* interpolation_func)(ptwXYPoints *desc, double x1, double y1, double x2, double y2, int depth)
ptwXY_interpolationOtherInfo interpolationOtherInfo
Definition: ptwXY.h:88
nfu_status status
Definition: ptwXY.h:85
ptwXY_getValue_callback getValueFunc
Definition: ptwXY.h:71

◆ ptwXY_toUnitbase()

ptwXYPoints * ptwXY_toUnitbase ( ptwXYPoints ptwXY,
nfu_status status 
)

Definition at line 306 of file ptwXY_interpolation.cc.

306 {
307
308 int64_t i;
309 ptwXYPoints *n;
310 ptwXYPoint *p;
311 double xMin, xMax, dx, inverseDx;
312
313 *status = nfu_tooFewPoints;
314 if( ptwXY->length < 2 ) return( NULL );
315 if( ( n = ptwXY_clone( ptwXY, status ) ) == NULL ) return( NULL );
316
317 xMin = n->points[0].x;
318 xMax = n->points[n->length-1].x;
319 dx = xMax - xMin;
320 inverseDx = 1. / dx;
321 for( i = 0, p = n->points; i < n->length; i++, p++ ) {
322 p->x = ( p->x - xMin ) * inverseDx;
323 p->y = p->y * dx;
324 }
325 n->points[n->length-1].x = 1.; /* Make sure last point is realy 1. */
326 return( n );
327}

Referenced by ptwXY_unitbaseInterpolate().

◆ ptwXY_unitbaseInterpolate()

ptwXYPoints * ptwXY_unitbaseInterpolate ( double  w,
double  w1,
ptwXYPoints ptwXY1,
double  w2,
ptwXYPoints ptwXY2,
nfu_status status 
)

Definition at line 363 of file ptwXY_interpolation.cc.

363 {
364/*
365* Should we not be checking the interpolation members???????
366*/
367 int64_t i;
368 ptwXYPoints *n1 = NULL, *n2 = NULL, *a = NULL, *r = NULL;
369 ptwXYPoint *p;
370 double f, g, xMin, xMax;
371
372 *status = nfu_XOutsideDomain;
373 if( w <= w1 ) {
374 if( w < w1 ) return( NULL );
375 return( ptwXY_clone( ptwXY1, status ) );
376 }
377 if( w >= w2 ) {
378 if( w > w2 ) return( NULL );
379 return( ptwXY_clone( ptwXY2, status ) );
380 }
381 if( ( n1 = ptwXY_toUnitbase( ptwXY1, status ) ) == NULL ) return( NULL );
382 if( ( n2 = ptwXY_toUnitbase( ptwXY2, status ) ) == NULL ) goto Err;
383 f = ( w - w1 ) / ( w2 - w1 );
384 g = 1. - f;
385 for( i = 0, p = n1->points; i < n1->length; i++, p++ ) p->y *= g;
386 for( i = 0, p = n2->points; i < n2->length; i++, p++ ) p->y *= f;
387 if( ( a = ptwXY_add_ptwXY( n1, n2, status ) ) == NULL ) goto Err;
388
389 xMin = g * ptwXY1->points[0].x + f * ptwXY2->points[0].x;
390 xMax = g * ptwXY1->points[ptwXY1->length-1].x + f * ptwXY2->points[ptwXY2->length-1].x;
391 if( ( r = ptwXY_fromUnitbase( a, xMin, xMax, status ) ) == NULL ) goto Err;
392 ptwXY_free( n1 );
393 ptwXY_free( n2 );
394 ptwXY_free( a );
395 return( r );
396
397Err:
398 if( n1 != NULL ) ptwXY_free( n1 );
399 if( n2 != NULL ) ptwXY_free( n2 );
400 if( a != NULL ) ptwXY_free( a );
401 return( NULL );
402}
@ nfu_XOutsideDomain
Definition: nf_utilities.h:26
ptwXYPoints * ptwXY_add_ptwXY(ptwXYPoints *ptwXY1, ptwXYPoints *ptwXY2, nfu_status *status)
ptwXYPoints * ptwXY_toUnitbase(ptwXYPoints *ptwXY, nfu_status *status)
ptwXYPoints * ptwXY_fromUnitbase(ptwXYPoints *ptwXY, double xMin, double xMax, nfu_status *status)