bool test_fundamentals(Index const dimension) { bool passed = true; Index const number_components = integer_power(dimension, Tensor::ORDER); std::vector<Scalar> const X = generate_sequence<Scalar>(number_components, 1.0, 1.0); // Test constructor with pointer Tensor const A(dimension, &X[0]); // Test copy constructor Tensor B = A; Tensor C; // Test copy assignment C = B - A; Scalar error = norm_f(C); bool const copy_assigned = error <= machine_epsilon<Scalar>(); passed = passed && copy_assigned; // Test fill with pointer B.fill(&X[0]); C = B - A; error = norm_f(C); bool const filled_pointer = error <= machine_epsilon<Scalar>(); passed = passed && filled_pointer; std::vector<Scalar> const Y = generate_sequence<Scalar>(number_components, -1.0, -1.0); C.fill(&Y[0]); // Test increment C += A; error = norm_f(C); bool const incremented = error <= machine_epsilon<Scalar>(); passed = passed && incremented; C.fill(&X[0]); // Test decrement C -= A; error = norm_f(C); bool const decremented = error <= machine_epsilon<Scalar>(); passed = passed && decremented; return passed; }
void calculate() { int j; init_process(&p); double dstnc = 0.0; double step = 0.01; double gold_epsilon = 0.0000001; double dist_epsilon = 0.0000001; double norm_epsilon = 0.0000001; double J1, J2; do { _seperator(); //printX("t", p.t, p.m); calculate_u(&p); calculate_p(&p); //_printM(p.f, p.m, p.n); puts("---"); //_printM(p.u, p.m, p.n); //for (j=0; j<p.m; j++) //{ // for (i=0; i<p.n; i++) // { // p.u1[j][i] = u(p.dx*i, p.dt*j); // } //} //puts("---"); //_printM(p.u1, p.m, p.n); J1 = _JSum(&p); printf("J1 = %.18f\n", J1); double grad_norm = norm_f(&p); //for (j=0; j<p.m; j++) { for (i=0; i<p.n; i++) { p.p[j][i] = p.p[j][i] / grad_norm; } } if (grad_norm < norm_epsilon) { puts("Iteration breaks. Because norm of gradient is less than given epsilon..."); break; } double argmin(double alpha) { int j; double *_f = (double*) malloc(sizeof(double) * p.m*p.n); memcpy(_f, p.f, sizeof(double) * p.m*p.n); for (j=0; j<p.m*p.n; j++) { p.f[j] = p.f[j] - alpha * p.p[j]; } double sum = _JSum(&p); memcpy(p.f, _f, sizeof(double) * p.m*p.n); free(_f); return sum; } double alpha = R1Minimize(argmin, step, gold_epsilon); dstnc = 0.0; for (j=0; j<p.m*p.n; j++) { double f = p.f[j]- alpha * p.p[j]; double df = f - p.f[j]; p.f[j] = f; dstnc = dstnc + df*df; } dstnc = sqrt(dstnc); J2 = _JSum(&p); if (J2 > J1) { puts("Error accure. Then next value of functional is greater than last value..."); exit(-1); } if (dstnc < dist_epsilon) { puts("Iteration breaks. Because distance between last and current point is less than given epsilon..."); break; } } while (1);
bool test_filling(Index const dimension) { bool passed = true; Index const number_components = integer_power(dimension, Tensor::ORDER); // Test construct with zeros Tensor A(dimension, ZEROS); Real error = norm_f_square(A); bool const zeros_constructed = error <= machine_epsilon<Scalar>(); passed = passed && zeros_constructed; // Test construct with ones Tensor B(dimension, ONES); error = norm_f_square(B) - number_components; bool const ones_constructed = error <= machine_epsilon<Scalar>(); passed = passed && ones_constructed; // Test construct with random entries Tensor C(dimension, RANDOM_UNIFORM); error = norm_f(C); bool const random_constructed = error > 0.0 && error < number_components; passed = passed && random_constructed; // Test fill with random components A.fill(RANDOM_UNIFORM); error = norm_f(A); bool const random_filled = error > 0.0 && error < number_components; passed = passed && random_filled; // Test fill with zeros B.fill(ZEROS); error = norm_f_square(B); bool const zeros_filled = error <= machine_epsilon<Scalar>(); passed = passed && zeros_filled; // Test fill with ones C.fill(ZEROS); error = norm_f_square(C) - number_components; bool const ones_filled = error <= machine_epsilon<Scalar>(); passed = passed && ones_filled; return passed; }
bool test_fundamentals(Index const dimension) { bool passed = true; Index const number_components = integer_power(dimension, Tensor::ORDER); std::vector<Scalar> const X = generate_sequence<Scalar>(number_components, 1.0, 1.0); // Test constructor with pointer Tensor const A(dimension, &X[0]); // Test copy constructor Tensor B = A; Tensor C; // Test copy assignment C = B - A; Scalar error = norm_f(C); bool const copy_assigned = error <= machine_epsilon<Scalar>(); passed = passed && copy_assigned; // Test fill with pointer B.fill(&X[0]); C = B - A; error = norm_f(C); bool const filled_pointer = error <= machine_epsilon<Scalar>(); passed = passed && filled_pointer; std::vector<Scalar> const Y = generate_sequence<Scalar>(number_components, -1.0, -1.0); C.fill(&Y[0]); // Test increment C += A; error = norm_f(C); bool const incremented = error <= machine_epsilon<Scalar>(); passed = passed && incremented; C.fill(&X[0]); // Test decrement C -= A; error = norm_f(C); bool const decremented = error <= machine_epsilon<Scalar>(); passed = passed && decremented; #ifdef HAVE_INTREPID_KOKKOSCORE //test Tensor fill and create for Kokkos data types Kokkos::View<Scalar *, Kokkos::DefaultExecutionSpace> X1("X1_kokkos", dimension); Kokkos::View<Scalar **, Kokkos::DefaultExecutionSpace> X2("X2_kokkos", dimension, dimension); Kokkos::View<Scalar ***, Kokkos::DefaultExecutionSpace> X3("X3_kokkos", dimension, dimension, dimension); Kokkos::View<Scalar ****, Kokkos::DefaultExecutionSpace> X4("X4_kokkos", dimension, dimension, dimension, dimension); Kokkos::deep_copy(X1, 3.1); Kokkos::deep_copy(X2, 3.2); Kokkos::deep_copy(X3, 3.3); Kokkos::deep_copy(X4, 3.4); Tensor Z(dimension); //(X1_k,0); Index rank = 0; Index temp = number_components; while (temp != 1) { temp = temp / dimension; rank = rank + 1; assert(temp > 0); } switch (rank) { default: assert(false); break; case 1: Z.fill(X1, 0); break; case 2: Z.fill(X2, 0, 0); break; case 3: Z.fill(X3, 0, 0, 0); break; case 4: Z.fill(X4, 0, 0, 0, 0); break; } // Test copy constructor. Tensor const U = Z; // Test copy assignment. Tensor V; V = U - Z; error = norm_f(V); bool const tensor_create_from_1d_kokkos = error <= machine_epsilon<Scalar>(); passed = passed && tensor_create_from_1d_kokkos; #endif return passed; }