def xtest_isotropic_adaptive_sparse_grid( self ): f = lambda x: numpy.sum( x**2+1, axis = 0 ) m = CppModel( f ) num_dims = 2 quadrature_rule_1d = ClosedEquidistantQuadRule1D() basis = ClosedPiecewisePolynomialBasis() basis.degree( 2 ) tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, quadrature_rule_1d, basis ) refinement_manager = HierarchicalSurplusRefinementManager() refinement_manager.tolerance( 0.0 ) domain = numpy.array( [-1.,1,-1.,1], numpy.double ) sg = AdaptiveSparseGrid() max_levels = range( 5, 11 ) num_pts = [145,321,705,1537,3329,7169] for i, max_level in enumerate( max_levels ): refinement_manager.maxLevel( max_level ) sg.initialise( m, domain, tpqr, refinement_manager, 1 ) sg.build() assert sg.num_points() == num_pts[i] sg.clear() num_dims = 3 quadrature_rule_1d = ClosedEquidistantQuadRule1D() basis = ClosedPiecewisePolynomialBasis() basis.degree( 1 ) tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, quadrature_rule_1d, basis ) refinement_manager = HierarchicalSurplusRefinementManager() refinement_manager.tolerance( 0.0 ) domain = numpy.array( [-1.,1,-1.,1,-1.,1.], numpy.double ) sg = AdaptiveSparseGrid() max_levels = range( 1, 8 ) num_pts = [7,25,69,177,441,1073,2561] for i, max_level in enumerate( max_levels ): refinement_manager.maxLevel( max_level ) sg.initialise( m, domain, tpqr, refinement_manager, 1 ) sg.build() assert sg.num_points() == num_pts[i] sg.clear() num_dims = 4 quadrature_rule_1d = ClosedEquidistantQuadRule1D() basis = ClosedPiecewisePolynomialBasis() basis.degree( 1 ) tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, quadrature_rule_1d, basis ) refinement_manager = HierarchicalSurplusRefinementManager() refinement_manager.tolerance( 0.0 ) domain = numpy.array( [-1.,1,-1.,1,-1.,1.,-1.,1.], numpy.double ) sg = AdaptiveSparseGrid() max_levels = range( 1, 7 ) num_pts = [9,41,137,401,1105,2929] for i, max_level in enumerate( max_levels ): refinement_manager.maxLevel( max_level ) sg.initialise( m, domain, tpqr, refinement_manager, 1 ) sg.build() assert sg.num_points() == num_pts[i] sg.clear() num_dims = 5 quadrature_rule_1d = ClosedEquidistantQuadRule1D() basis = ClosedPiecewisePolynomialBasis() basis.degree( 1 ) tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, quadrature_rule_1d, basis ) refinement_manager = HierarchicalSurplusRefinementManager() refinement_manager.tolerance( 0.0 ) domain = numpy.array( [-1.,1,-1.,1,-1.,1.,-1.,1.,-1.,1.], numpy.double ) sg = AdaptiveSparseGrid() max_levels = range( 1, 6 ) num_pts = [11,61,241,801,2433] for i, max_level in enumerate( max_levels ): refinement_manager.maxLevel( max_level ) sg.initialise( m, domain, tpqr, refinement_manager, 1 ) sg.build() assert sg.num_points() == num_pts[i] sg.clear() num_dims = 2 quadrature_rule_1d = OpenEquidistantQuadRule1D() basis = OpenPiecewisePolynomialBasis() basis.degree( 1 ) tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, quadrature_rule_1d, basis ) refinement_manager = HierarchicalSurplusRefinementManager() refinement_manager.tolerance( 0.0 ) domain = numpy.array( [-1.,1,-1.,1], numpy.double ) sg = AdaptiveSparseGrid() max_levels = range( 1, 6 ) num_pts = [5,17,49,129,321,769] for i, max_level in enumerate( max_levels ): refinement_manager.maxLevel( max_level ) sg.initialise( m, domain, tpqr, refinement_manager, 0 ) sg.build() assert sg.num_points() == num_pts[i] sg.clear() num_dims = 6 quadrature_rule_1d = OpenEquidistantQuadRule1D() basis = OpenPiecewisePolynomialBasis() basis.degree( 1 ) tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, quadrature_rule_1d, basis ) refinement_manager = HierarchicalSurplusRefinementManager() refinement_manager.tolerance( 0.0 ) domain = numpy.array( [-1.,1,-1.,1,-1.,1.,-1.,1.,-1.,1.,-1.,1.], numpy.double ) sg = AdaptiveSparseGrid() max_levels = range( 1, 5 ) num_pts = [13,97,545,2561] for i, max_level in enumerate( max_levels ): refinement_manager.maxLevel( max_level ) sg.initialise( m, domain, tpqr, refinement_manager, 0 ) sg.build() assert sg.num_points() == num_pts[i] sg.clear() num_dims = 10 quadrature_rule_1d = OpenEquidistantQuadRule1D() basis = OpenPiecewisePolynomialBasis() basis.degree( 1 ) tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, quadrature_rule_1d, basis ) refinement_manager = HierarchicalSurplusRefinementManager() refinement_manager.tolerance( 0.0 ) domain = numpy.array( [-1.,1,-1.,1,-1.,1.,-1.,1.,-1.,1., -1.,1,-1.,1,-1.,1.,-1.,1.,-1.,1.], numpy.double ) sg = AdaptiveSparseGrid() max_levels = range( 1, 4 ) num_pts = [21,241,2001] for i, max_level in enumerate( max_levels ): refinement_manager.maxLevel( max_level ) sg.initialise( m, domain, tpqr, refinement_manager, 0 ) sg.build() assert sg.num_points() == num_pts[i] sg.clear()
def adaptive_sparse_grid_aposteriori_error_based_refinement(): num_dims = 2 quadrature_rule_1d = ClosedEquidistantQuadRule1D() basis = ClosedPiecewisePolynomialBasis() basis.degree(1) tpqr = get_tensor_product_quadrature_rule_single(num_dims, quadrature_rule_1d, basis) path = "/home/jdjakem/software/matlab/packages/" + "a-posteriori-error-estimation/work_stochastic/" # last / is needed if path called from c code directly # path = '' # initiate model and run in the background # os.system( 'python adjoint_file_model.py &' ) # m = CppAdjointFileModel( path ) # m = AdjointMatlabModel() m = MatlabModel() m.add_matlab_paths(["/home/jdjakem/software/matlab/packages/" + "a-posteriori-error-estimation/ACES/", path]) # path needs to be specified last so that sample_input.m is the one # in path and not one in the directores added earlier # m.capture_matlab_buffer( 100 ) m.initialise() # m.print_matlab_buffer() print "Initialised model" forward_solution_num_DOF, adjoint_solution_num_DOF = m.get_num_dof() print "forward ndof: %d\n adjoint ndof: %d" % (forward_solution_num_DOF, adjoint_solution_num_DOF) # refinement_manager = AposterioriErrorRefinementManager() # refinement_manager.path( path ); # refinement_manager.forward_solution_num_DOF( int(forward_solution_num_DOF) ) # refinement_manager.adjoint_solution_num_DOF( int(adjoint_solution_num_DOF) ) refinement_manager = HierarchicalSurplusRefinementManager() refinement_manager.tolerance(0.0) refinement_manager.maxPoints(2000) refinement_manager.maxLevel(20) domain = numpy.array([0.0, 1.0, 0.0, 1], numpy.double) sg = AdaptiveSparseGrid() from utilities.visualisation import plot_surface_from_function sg.initialise(m, domain, tpqr, refinement_manager, 0) sg.build() print "sparse grid built successfully" # need to set this before evaluate is called or # quantities_of_interest will be whatever internal sg routines # 3 have set it as. todo move this to the end of build sg.quantities_of_interest(numpy.array([0], numpy.int32)) # evaluate_enhanced_sparse_grid = lambda x: (sg.evaluate_set( x ) + \ # refinement_manager.compute_error_estimates( x, sg, m )).squeeze() grid = sg.get_coordinates() rng = numpy.random.RandomState(0) test_pts = rng.uniform(0.0, 1.0, (num_dims, 100)) test_vals = m.evaluate_set(test_pts)[:, 0] print "sparse grid error: ", numpy.linalg.norm(test_vals - sg.evaluate_set(test_pts).squeeze()) / numpy.sqrt( test_pts.shape[1] ) # print 'enhanced sparse grid error: ', numpy.linalg.norm( test_vals - # evaluate_enhanced_sparse_grid( test_pts ) ) / numpy.sqrt( test_pts.shape[1] ) # plot_surface_from_function( evaluate_enhanced_sparse_grid, domain, 30 ) # m.terminate() # plot_surface_from_function( sg.evaluate_set, domain, 30 ) import pylab pylab.plot(grid[0, :], grid[1, :], "o") pylab.xlim([0, 1]) pylab.ylim([0, 1]) pylab.show() from sparse_grid_cpp import AposterioriGeneralisedSparseGrid