/** Compute the bounding box of a boundary discretization. */ Box<SHAPE_DIMENSION> compute_bounding_box() const { ublas::fixed_vector<float_t, SHAPE_DIMENSION> lower_corner, upper_corner, point; if(n_points() == 0) return Box<SHAPE_DIMENSION>(); lower_corner = (*this)(0); upper_corner = (*this)(0); for(size_t i = 1; i < n_points(); ++i) { point = (*this)(i); for(size_t j = 0; j < SHAPE_DIMENSION; ++j) { if(point(j) < lower_corner(j)) lower_corner(j) = point(j); if(point(j) > upper_corner(j)) upper_corner(j) = point(j); } } return Box<SHAPE_DIMENSION>(lower_corner, upper_corner); }
typename const_accessort_t::data_t integrate(const const_accessort_t & image) const { typename const_accessort_t::data_t result = 0.0; ublas::fixed_vector<float_t, SHAPE_DIMENSION> point, normal; for(size_t i = 0; i < n_points(); ++i) { point = (*this)(i, normal); // explicit cast of floating point values in "vertex" to pixel position! ublas::fixed_vector<size_t, const_accessort_t::dimension> pixel_position = ublas::fixed_vector<size_t, const_accessort_t::dimension>(point); bool is_valid_pixel = true; for(size_t i = 0; i < const_accessort_t::dimension; ++i) { if( ! ( pixel_position(i) < image.size()(i) ) ) { is_valid_pixel = false; break; } } if(is_valid_pixel) result += image[pixel_position] * norm_2(normal); } return result; }
/** Compute the <em>(n-1)</em>-dimensional boundary area of a boundary discretization. */ float_t compute_boundary_area() const { float_t result = 0.0; ublas::fixed_vector<float_t, SHAPE_DIMENSION> point, normal; for(size_t i = 0; i < n_points(); ++i) { point = (*this)(i, normal); result += norm_2(normal); } return result; }
float_t integrate_vector_field (const const_vector_accessor_t & vector_field) const { float_t result = 0.0; ublas::fixed_vector<float_t, SHAPE_DIMENSION> point, normal; for(size_t i = 0; i < n_points(); ++i) { point = (*this)(i, normal); // explicit cast of floating point values in "vertex" to pixel position! ublas::fixed_vector<size_t, const_vector_accessor_t::dimension> pixel_position = ublas::fixed_vector<size_t, const_vector_accessor_t::dimension>(point); bool is_valid_pixel = true; for(size_t i = 0; i < const_vector_accessor_t::dimension; ++i) { if( ! ( pixel_position(i) < vector_field.size()(i) ) ) { is_valid_pixel = false; break; } } if(is_valid_pixel) // Note: unit normal * tangential speed = normal result += inner_prod(vector_field[pixel_position], normal); else { ublas::fixed_vector<float_t, SHAPE_DIMENSION> value; boundary_discretizer_impl::compute_zero_extension(vector_field, point, value); result += inner_prod(value, normal); } } return result; }
int main() { std::vector<unsigned int> n_points(5); n_points[0] = 11; n_points[1] = 21; n_points[2] = 31; n_points[3] = 41; n_points[4] = 51; std::vector<unsigned int> indices(5); indices[0] = 1; indices[1] = 2; indices[2] = 3; indices[3] = 4; indices[4] = 5; unsigned int global_exact = indices[0] + indices[1]*n_points[0] + indices[2]*n_points[0]*n_points[1] + indices[3]*n_points[0]*n_points[1]*n_points[2] + indices[4]*n_points[0]*n_points[1]*n_points[2]*n_points[3]; int return_flag = 0; unsigned int global = QUESO::InterpolationSurrogateHelper::coordToGlobal( indices, n_points ); // This is integer arithmetic so it should be exactly zero error if( (global - global_exact) != 0 ) { std::cerr << "ERROR: mismatch in InterpolationSurrogateHelper::coordToGlobal test." << std::endl << " test = " << global << std::endl << " exact = " << global_exact << std::endl; return_flag = 1; } std::vector<unsigned int> indices_test; QUESO::InterpolationSurrogateHelper::globalToCoord( global, n_points, indices_test ); unsigned int test_indices = 0; for( unsigned int d = 0; d < 5; d++ ) { if( indices[d] != indices_test[d] ) test_indices = 1; } if( test_indices == 1 ) { std::cerr << "ERROR: mismatch in InterpolationSurrogateHelper::globalToCoord test." << std::endl << " test = "; for( unsigned int d = 0; d < 5; d++ ) { std::cerr << indices_test[d] << " "; } std::cerr << std::endl; std::cerr << " exact = "; for( unsigned int d = 0; d < 5; d++ ) { std::cerr << indices[d] << " "; } std::cerr << std::endl; return_flag = 1; } return return_flag; }
int main(int argc, char ** argv) { MPI_Init(&argc, &argv); QUESO::FullEnvironment env(MPI_COMM_WORLD, "test_InterpolationSurrogate/queso_input.txt", "", NULL); int return_flag = 0; std::string vs_prefix = "param_"; // Filename for writing/reading surrogate data std::string filename1 = "test_write_InterpolationSurrogateBuilder_1.dat"; std::string filename2 = "test_write_InterpolationSurrogateBuilder_2.dat"; QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> paramSpace(env,vs_prefix.c_str(), 4, NULL); // Point at which we will test the surrogate evaluation QUESO::GslVector domainVector(paramSpace.zeroVector()); domainVector[0] = -0.4; domainVector[1] = 3.0; domainVector[2] = 1.5; domainVector[3] = 1.65; double exact_val_1 = four_d_fn_1(domainVector[0],domainVector[1],domainVector[2],domainVector[3]); double exact_val_2 = four_d_fn_2(domainVector[0],domainVector[1],domainVector[2],domainVector[3]); double tol = 2.0*std::numeric_limits<double>::epsilon(); // First test surrogate build directly from the computed values { QUESO::GslVector paramMins(paramSpace.zeroVector()); paramMins[0] = -1; paramMins[1] = -0.5; paramMins[2] = 1.1; paramMins[3] = -2.1; QUESO::GslVector paramMaxs(paramSpace.zeroVector()); paramMaxs[0] = 0.9; paramMaxs[1] = 3.14; paramMaxs[2] = 2.1; paramMaxs[3] = 4.1; QUESO::BoxSubset<QUESO::GslVector, QUESO::GslMatrix> paramDomain("param_", paramSpace, paramMins, paramMaxs); std::vector<unsigned int> n_points(4); n_points[0] = 11; n_points[1] = 51; n_points[2] = 31; n_points[3] = 41; // One dataset for each of the two functions const unsigned int n_datasets = 2; QUESO::InterpolationSurrogateDataSet<QUESO::GslVector, QUESO::GslMatrix> data(paramDomain,n_points,n_datasets); MyInterpolationBuilder<QUESO::GslVector,QUESO::GslMatrix> builder( data ); builder.build_values(); QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix> four_d_surrogate_1( data.get_dataset(0) ); QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix> four_d_surrogate_2( data.get_dataset(1) ); double test_val_1 = four_d_surrogate_1.evaluate(domainVector); double test_val_2 = four_d_surrogate_2.evaluate(domainVector); return_flag = return_flag || test_val( test_val_1, exact_val_1, tol, "test_build_1" ) || test_val( test_val_2, exact_val_2, tol, "test_build_2" ); // Write the output to test reading next QUESO::InterpolationSurrogateIOASCII<QUESO::GslVector,QUESO::GslMatrix> data_writer; data_writer.write( filename1, data.get_dataset(0) ); data_writer.write( filename2, data.get_dataset(1) ); } // Now read the data and test { QUESO::InterpolationSurrogateIOASCII<QUESO::GslVector,QUESO::GslMatrix> data_reader_1, data_reader_2; data_reader_1.read( filename1, env, vs_prefix.c_str() ); data_reader_2.read( filename2, env, vs_prefix.c_str() ); // Build a new surrogate QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix> four_d_surrogate_1( data_reader_1.data() ); QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix> four_d_surrogate_2( data_reader_2.data() ); double test_val_1 = four_d_surrogate_1.evaluate(domainVector); double test_val_2 = four_d_surrogate_2.evaluate(domainVector); return_flag = return_flag || test_val( test_val_1, exact_val_1, tol, "test_read_1" ) || test_val( test_val_2, exact_val_2, tol, "test_read_2" ); } return return_flag; }
int main(int argc, char ** argv) { std::string inputFileName = "test_InterpolationSurrogate/queso_input.txt"; const char * test_srcdir = std::getenv("srcdir"); if (test_srcdir) inputFileName = test_srcdir + ('/' + inputFileName); #ifdef QUESO_HAS_MPI MPI_Init(&argc, &argv); QUESO::FullEnvironment env(MPI_COMM_WORLD, inputFileName, "", NULL); #else QUESO::FullEnvironment env(inputFileName, "", NULL); #endif int return_flag = 0; QUESO::VectorSpace<QUESO::GslVector, QUESO::GslMatrix> paramSpace(env,"param_", 3, NULL); QUESO::GslVector paramMins(paramSpace.zeroVector()); paramMins[0] = -1; paramMins[1] = -0.5; paramMins[2] = 1.1; QUESO::GslVector paramMaxs(paramSpace.zeroVector()); paramMaxs[0] = 0.9; paramMaxs[1] = 3.14; paramMaxs[2] = 2.1; QUESO::BoxSubset<QUESO::GslVector, QUESO::GslMatrix> paramDomain("param_", paramSpace, paramMins, paramMaxs); std::vector<unsigned int> n_points(3); n_points[0] = 101; n_points[1] = 51; n_points[2] = 31; QUESO::InterpolationSurrogateData<QUESO::GslVector, QUESO::GslMatrix> data(paramDomain,n_points); std::vector<double> values(n_points[0]*n_points[1]*n_points[2]); double spacing_x = (paramMaxs[0] - paramMins[0])/(n_points[0]-1); double spacing_y = (paramMaxs[1] - paramMins[1])/(n_points[1]-1); double spacing_z = (paramMaxs[2] - paramMins[2])/(n_points[2]-1); for( unsigned int i = 0; i < n_points[0]; i++ ) { for( unsigned int j = 0; j < n_points[1]; j++ ) { for( unsigned int k = 0; k < n_points[2]; k++ ) { unsigned int n = i + j*n_points[0] + k*n_points[0]*n_points[1]; double x = paramMins[0] + i*spacing_x; double y = paramMins[1] + j*spacing_y; double z = paramMins[2] + k*spacing_z; values[n] = three_d_fn(x,y,z); } } } data.set_values( values ); QUESO::LinearLagrangeInterpolationSurrogate<QUESO::GslVector,QUESO::GslMatrix> three_d_surrogate( data ); QUESO::GslVector domainVector(paramSpace.zeroVector()); domainVector[0] = -0.4; domainVector[1] = 3.0; domainVector[2] = 1.5; double test_val = three_d_surrogate.evaluate(domainVector); double exact_val = three_d_fn(domainVector[0],domainVector[1],domainVector[2]); double tol = 2.0*std::numeric_limits<double>::epsilon(); double rel_error = (test_val - exact_val)/exact_val; if( std::fabs(rel_error) > tol ) { std::cerr << "ERROR: Tolerance exceeded for 3D Lagrange interpolation test." << std::endl << " test_val = " << test_val << std::endl << " exact_val = " << exact_val << std::endl << " rel_error = " << rel_error << std::endl << " tol = " << tol << std::endl; return_flag = 1; } #ifdef QUESO_HAS_MPI MPI_Finalize(); #endif return return_flag; }