Skip to content

Commit

Permalink
Merge pull request #73 from TimSiebert1/fix_tests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
TimSiebert1 authored Nov 12, 2024
2 parents fd441a5 + e5dcfeb commit 8393ced
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 93 deletions.
57 changes: 6 additions & 51 deletions ADOL-C/boost-test/traceFixedPointScalarTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ static double norm(double* x, int dim)

static double traceNewtonForSquareRoot(int tapeNumber, int subTapeNumber, double argument)
{
//ax1 = sqrt(ax1);
adouble x = 2.5; // Initial iterate
adouble y;
double out;
trace_on(tapeNumber);
adouble u;
u <<= argument;

//ax1 = sqrt(ax1);
adouble x = 2.5; // Initial iterate
adouble y;


fp_iteration(subTapeNumber,
iteration<double>,
Expand All @@ -63,8 +65,6 @@ static double traceNewtonForSquareRoot(int tapeNumber, int subTapeNumber, double
&y, // [out] Final state of the iteration
1, // Size of the vector x_0
1); // Number of parameters

double out;
y >>= out;
trace_off();

Expand All @@ -76,6 +76,7 @@ static double traceNewtonForSquareRoot(int tapeNumber, int subTapeNumber, double
*/
BOOST_AUTO_TEST_CASE(NewtonScalarFixedPoint_zos_forward)
{
ensureContiguousLocations(5);
// Compute the square root of 2.0
const double argument[1] = {2.0};
double out = traceNewtonForSquareRoot(1, // tape number
Expand All @@ -86,7 +87,6 @@ BOOST_AUTO_TEST_CASE(NewtonScalarFixedPoint_zos_forward)
BOOST_TEST(out == std::sqrt(argument[0]), tt::tolerance(tol));

double value[1];

zos_forward(1, // Tape number
1, // Number of dependent variables
1, // Number of indepdent variables
Expand Down Expand Up @@ -128,50 +128,5 @@ BOOST_AUTO_TEST_CASE(NewtonScalarFixedPoint_fos_forward)
BOOST_TEST(derivative[0] == exactDerivative, tt::tolerance(tol));
}

BOOST_AUTO_TEST_CASE(NewtonForSquareRootFixedPoint_fov_forward)
{
// Compute the square root of 2.0
const double argument[1] = {2.0};
double out = traceNewtonForSquareRoot(1, // tape number
2,
argument[0]);

// Did taping really produce the correct value?
BOOST_TEST(out == std::sqrt(argument[0]), tt::tolerance(tol));

// Use fov_forward to compute the first derivative
int m = 1; // Number of dependent variables
int n = 1; // Number of independent variables
int p = 1; // Number of tangents

double **xd = myalloc2(n, p);
double value[m];
double **yd = myalloc2(m, p);

/* Test partial derivative wrt x1 and x2. */
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
xd[i][j] = (i == j) ? 1.0 : 0.0;
}
}

fov_forward(1, // Tape number
m, // Number of dependent variables
n, // Number of independent variables
p, // Number of tangents
argument, // Where to evaluate the derivative
xd, // The tangents
value, // The compute function value
yd); // The computed derivative

double exactDerivative = 1.0/(2*sqrt(argument[0]));

BOOST_TEST(value[0] == out, tt::tolerance(tol));
BOOST_TEST(yd[0][0] == exactDerivative, tt::tolerance(tol));

myfree2(xd);
myfree2(yd);
}

BOOST_AUTO_TEST_SUITE_END()

44 changes: 2 additions & 42 deletions ADOL-C/src/fixpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ static int fp_fos_forward ( int dim_xu, double *xu, double *xu_dot,
adolc_exit(-1,"",__func__,__FILE__,__LINE__);
}
for (k=1; (k<current->N_max_deriv)|(k<current->N_max); k++) {
if (k > 1){
for (i=0; i<dim_x; i++) xu[i] = x_fix[i];
for (i=0; i<dim_x; i++) xu_dot[i] = x_fix_dot[i];
}
fos_forward ( current->sub_tape_num, dim_x, dim_xu, 0, xu, xu_dot, x_fix, x_fix_dot);
for (i=0; i<dim_x; i++) xu[i] = x_fix[i] - xu[i];
err = (*current->norm)(xu,dim_x);
Expand All @@ -120,46 +122,6 @@ static int fp_fos_forward ( int dim_xu, double *xu, double *xu_dot,
return -1;
}

static int fp_hos_reverse ( int dim_x, double *x_fix_bar, int dim_xu, double *xu_bar, double* /*unused*/, double* /*unused*/) {
// (d x_fix) / (d x_0) = 0 (!)
int i, k;
double err;
ADOLC_OPENMP_THREAD_NUMBER;
ADOLC_OPENMP_GET_THREAD_NUMBER;
locint edf_index = ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index;

// Find fpi_stack element with index 'edf_index'.
auto current = std::find_if(fpi_stack.begin(), fpi_stack.end(), [&](auto&& v){return v.edf_index == edf_index;});

if (current==fpi_stack.end()) {
fprintf(stderr,"ADOL-C Error! No edf found for fixpoint iteration.\n");
adolc_exit(-1,"",__func__,__FILE__,__LINE__);
}
double *U = new double[dim_xu];
double *xi = new double[dim_x];

for (k=1; k<current->N_max_deriv; k++) {
for (i=0; i<dim_x; i++) xi[i] = U[i];
fos_reverse ( current->sub_tape_num, dim_x, dim_xu, xi, U );
for (i=0; i<dim_x; i++) U[i] += x_fix_bar[i];
for (i=0; i<dim_x; i++) xi[i] = U[i] - xi[i];
err = (*current->norm_deriv)(xi,dim_x);
printf(" fp_fos_reverse: k = %d err = %e\n",k,err);
if (err<current->epsilon_deriv) {
for (i=0; i<dim_xu-dim_x; i++) {
xu_bar[dim_x+i] += U[dim_x+i];
}

delete[] xi;
delete[] U;
return k;
}
}
for (i=0; i<dim_xu-dim_x; i++) xu_bar[dim_x+i] += U[dim_x+i];
delete[] xi;
delete[] U;
return -1;
}

static int fp_fos_reverse ( int dim_x, double *x_fix_bar, int dim_xu, double *xu_bar, double* /*unused*/, double* /*unused*/) {
// (d x_fix) / (d x_0) = 0 (!)
Expand All @@ -185,7 +147,6 @@ static int fp_fos_reverse ( int dim_x, double *x_fix_bar, int dim_xu, double *xu
for (i=0; i<dim_x; i++) U[i] += x_fix_bar[i];
for (i=0; i<dim_x; i++) xi[i] = U[i] - xi[i];
err = (*current->norm_deriv)(xi,dim_x);
printf(" fp_fos_reverse: k = %d err = %e\n",k,err);
if (err<current->epsilon_deriv) {
for (i=0; i<dim_xu-dim_x; i++) {
xu_bar[dim_x+i] += U[dim_x+i];
Expand Down Expand Up @@ -224,7 +185,6 @@ ADOLC_DLL_EXPORT int fp_iteration ( int sub_tape_num,
edf_iteration->zos_forward = &fp_zos_forward;
edf_iteration->fos_forward = &fp_fos_forward;
edf_iteration->fos_reverse = &fp_fos_reverse;
edf_iteration->fos_reverse = &fp_hos_reverse;

// add new fp information
fpi_data data;
Expand Down

0 comments on commit 8393ced

Please sign in to comment.