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; }
bool test_arithmetic(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); Real const sum_squares = number_components * (number_components + 1) * (2 * number_components + 1) / 6; // Test addition Tensor const A(dimension, &X[0]); Tensor const B = -1.0 * A; Tensor const C = -1.0 * B; Tensor const D = A + B; Real error = norm_f_square(D); bool const added = error <= machine_epsilon<Scalar>(); passed = passed && added; // Test subtraction Tensor const E = A - C; error = norm_f_square(E); bool const subtracted = error <= machine_epsilon<Scalar>(); passed = passed && subtracted; // Test scaling error = norm_f_square(C) - sum_squares; bool const scaled = error <= machine_epsilon<Scalar>(); passed = passed && scaled; Tensor const F = C / -1.0; error = norm_f_square(F) - sum_squares; bool const divided = error <= machine_epsilon<Scalar>(); passed = passed && divided; Tensor const G = 1.0 / C; error = norm_f_square(G) - sum_squares; bool const split = error <= machine_epsilon<Scalar>(); passed = passed && split; return passed; }
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; }
void operator_integer() { int result; char in[USHRT_MAX]; printf("Entre com o 1º valor: "); scanf("%s", in); printf("\n"); result = atoi(in); int keep_going = 1; while (keep_going) { switch (menu_integer()) { case 1: // Add printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_add(&result, atoi(in)); break; case 2: // Subtract printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_subtract(&result, atoi(in)); break; case 3: // Multiply printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_multiply(&result, atoi(in)); break; case 4: // Power printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_power(&result, atoi(in)); break; case 5: // Divide while (1) { printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); if (atoi(in) == 0) printf("Valor inválido!\n\n"); else break; } integer_divide(&result, atoi(in)); break; case 6: // Module printf("Entre com o próximo valor: "); scanf("%s", in); printf("\n"); integer_module(&result, atoi(in)); break; default: keep_going = 0; break; } printf("Resultado: %d\n\n", result); } }
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; }
void calculate_cell_reconstruction_matrices(int n_variables, double *weight_exponent, int *maximum_order, struct FACE *face, int n_cells, struct CELL *cell, struct ZONE *zone) { int c, u, i, j, k, l; int order, n_powers, n_stencil; //find the overall maximum order int maximum_maximum_order = 0; for(u = 0; u < n_variables; u ++) if(maximum_order[u] > maximum_maximum_order) maximum_maximum_order = maximum_order[u]; //cell structure allocation for(c = 0; c < n_cells; c ++) exit_if_false(cell_matrix_new(n_variables, &cell[c]),"allocating cell matrices"); //numerics values double **matrix, *weight; int n_constraints, *constraint; exit_if_false(allocate_double_matrix(&matrix,ORDER_TO_POWERS(maximum_maximum_order),MAX_STENCIL),"allocating matrix"); exit_if_false(allocate_integer_vector(&constraint,MAX_STENCIL),"allocating constraints"); exit_if_false(allocate_double_vector(&weight,MAX_STENCIL),"allocating weights"); //stencil element properties int s_id, s_index; struct ZONE *s_zone; char s_location, *s_condition; double s_area, *s_centroid, s_weight; //integration double x[2]; int differential[2], d; //CV polygon int n_polygon; double ***polygon; exit_if_false(allocate_double_pointer_matrix(&polygon,MAX(MAX_CELL_FACES,4),2),"allocating polygon memory"); for(c = 0; c < n_cells; c ++) { for(u = 0; u < n_variables; u ++) { //problem size order = cell[c].order[u]; n_powers = ORDER_TO_POWERS(order); n_stencil = cell[c].n_stencil[u]; n_constraints = 0; for(i = 0; i < n_stencil; i ++) { //stencil element properties s_id = cell[c].stencil[u][i]; s_index = ID_TO_INDEX(s_id); s_zone = &zone[ID_TO_ZONE(s_id)]; s_location = s_zone->location; s_condition = s_zone->condition; if(s_location == 'f') { s_centroid = face[s_index].centroid; s_area = face[s_index].area; } else if(s_location == 'c') { s_centroid = cell[s_index].centroid; s_area = cell[s_index].area; } else exit_if_false(0,"recognising zone location"); s_weight = (s_centroid[0] - cell[c].centroid[0])*(s_centroid[0] - cell[c].centroid[0]); s_weight += (s_centroid[1] - cell[c].centroid[1])*(s_centroid[1] - cell[c].centroid[1]); s_weight = 1.0/pow(s_weight,0.5*weight_exponent[u]); if(s_location == 'c' && s_index == c) s_weight = 1.0; weight[i] = s_weight; //unknown and dirichlet conditions have zero differentiation differential[0] = differential[1] = 0; //other conditions have differential determined from numbers of x and y-s in the condition string if(s_condition[0] != 'u' && s_condition[0] != 'd') { j = 0; while(s_condition[j] != '\0') { differential[0] += (s_condition[j] == 'x'); differential[1] += (s_condition[j] == 'y'); j ++; } } //index for the determined differential d = differential_index[differential[0]][differential[1]]; //unknowns if(s_condition[0] == 'u') { /*//fit unknowns to centroid points x[0] = s_centroid[0] - cell[c].centroid[0]; x[1] = s_centroid[1] - cell[c].centroid[1]; for(j = 0; j < n_powers; j ++) { matrix[j][i] = polynomial_coefficient[d][j]* integer_power(x[0],polynomial_power_x[d][j])* integer_power(x[1],polynomial_power_y[d][j])* s_weight; }*/ //fit unknowns to CV average n_polygon = generate_control_volume_polygon(polygon, s_index, s_location, face, cell); for(j = 0; j < n_powers; j ++) matrix[j][i] = 0.0; for(j = 0; j <= order; j ++) { for(k = 0; k < n_polygon; k ++) { x[0] = 0.5*polygon[k][0][0]*(1.0 - gauss_x[order][j]) + 0.5*polygon[k][1][0]*(1.0 + gauss_x[order][j]) - cell[c].centroid[0]; x[1] = 0.5*polygon[k][0][1]*(1.0 - gauss_x[order][j]) + 0.5*polygon[k][1][1]*(1.0 + gauss_x[order][j]) - cell[c].centroid[1]; for(l = 0; l < n_powers; l ++) { //[face integral of polynomial integrated wrt x] * [x normal] / [CV area] matrix[l][i] += polynomial_coefficient[d][l] * (1.0 / (polynomial_power_x[d][l] + 1.0)) * integer_power(x[0],polynomial_power_x[d][l]+1) * integer_power(x[1],polynomial_power_y[d][l]) * s_weight * gauss_w[order][j] * 0.5 * (polygon[k][1][1] - polygon[k][0][1]) / s_area; } } } } //knowns else { //known faces fit to face average if(s_location == 'f') { for(j = 0; j < n_powers; j ++) matrix[j][i] = 0.0; for(j = 0; j < order; j ++) { x[0] = 0.5*face[s_index].node[0]->x[0]*(1.0 - gauss_x[order-1][j]) + 0.5*face[s_index].node[1]->x[0]*(1.0 + gauss_x[order-1][j]) - cell[c].centroid[0]; x[1] = 0.5*face[s_index].node[0]->x[1]*(1.0 - gauss_x[order-1][j]) + 0.5*face[s_index].node[1]->x[1]*(1.0 + gauss_x[order-1][j]) - cell[c].centroid[1]; for(k = 0; k < n_powers; k ++) { matrix[k][i] += polynomial_coefficient[d][k] * integer_power(x[0],polynomial_power_x[d][k]) * integer_power(x[1],polynomial_power_y[d][k]) * s_weight*gauss_w[order-1][j]*0.5; } } } //cells need implementing //if(s_location == 'c') //{ //} } //constraints are the centre cell and any dirichlet boundaries if((s_location == 'c' && s_index == c) || s_condition[0] == 'd') constraint[n_constraints++] = i; } //solve if(n_constraints > 0) exit_if_false(constrained_least_squares(n_stencil,n_powers,matrix,n_constraints,constraint) == LS_SUCCESS, "doing CLS calculation"); else exit_if_false(least_squares(n_stencil,n_powers,matrix) == LS_SUCCESS,"doing LS calculation"); //multiply by the weights for(i = 0; i < n_powers; i ++) for(j = 0; j < n_stencil; j ++) matrix[i][j] *= weight[j]; //store in the cell structure for(i = 0; i < n_powers; i ++) for(j = 0; j < n_stencil; j ++) cell[c].matrix[u][i][j] = matrix[i][j]; } } //clean up free_matrix((void**)matrix); free_vector(constraint); free_vector(weight); free_matrix((void**)polygon); }