Example #1
0
    def test_genseralised_sparse_grid_aposteriori_error_based_refinement( self ):
        from sparse_grid_cpp import AposterioriGeneralisedSparseGrid

        num_dims = 1

        quadrature_rule_1d = ClenshawCurtisQuadRule1D()
        basis = LagrangePolynomialBasis()

        tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, 
                                                              quadrature_rule_1d,
                                                              basis )
        
        m = AdjointModel()
        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)


        domain = numpy.array( [-1.,1.], numpy.double )
        sg = AposterioriGeneralisedSparseGrid()

        sg.tolerance( 0.0 )
        sg.max_num_points( 40 )
        sg.max_level( 10 )
        sg.forward_solution_num_DOF( int( forward_solution_num_DOF ) )
        sg.adjoint_solution_num_DOF( int( adjoint_solution_num_DOF ) )


        from utilities.visualisation import plot_surface_from_function

        sg.initialise( m, domain, tpqr, 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 ) + \
            sg.compute_error_estimates( x ) ).squeeze()

        grid = sg.get_coordinates() 
        rng = numpy.random.RandomState( 0 )
        test_pts = rng.uniform( -1., 1., ( num_dims, 1000 ) )
        test_vals = m.evaluate_set( test_pts )[:,0]
        sg_error = numpy.linalg.norm( test_vals - 
                                      sg.evaluate_set( test_pts ).squeeze() )
        print 'done1'
        print 'sparse grid error: ', sg_error
        assert sg_error < 4e-10
        enhanced_sg_error = numpy.linalg.norm( test_vals - 
                            evaluate_enhanced_sparse_grid( test_pts ) )
        print 'enhanced sparse grid error: ', enhanced_sg_error
        assert enhanced_sg_error < 8e-15

        print 'done'
        num_dims = 2

        quadrature_rule_1d = ClenshawCurtisQuadRule1D()
        basis = LagrangePolynomialBasis()

        tpqr = self.get_tensor_product_quadrature_rule_single(num_dims, 
                                                              quadrature_rule_1d,
                                                              basis )
        
        m = AdjointModel()
        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)


        domain = numpy.array( [-1.,1.,0.4,0.8], numpy.double )
        sg = AposterioriGeneralisedSparseGrid()

        sg.tolerance( 0.0 )
        sg.max_num_points( 1000 )
        sg.max_level( 10 )
        sg.forward_solution_num_DOF( int( forward_solution_num_DOF ) )
        sg.adjoint_solution_num_DOF( int( adjoint_solution_num_DOF ) )


        from utilities.visualisation import plot_surface_from_function

        sg.initialise( m, domain, tpqr, 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 ) + \
            sg.compute_error_estimates( x ) ).squeeze()

        grid = sg.get_coordinates() 
        rng = numpy.random.RandomState( 0 )
        test_pts = rng.uniform( 0., 1., ( num_dims, 1000 ) )
        from utilities.math_utils import map_from_unit_hypercube
        test_pts = map_from_unit_hypercube( test_pts, domain )
        test_vals = m.evaluate_set( test_pts )[:,0]
        print test_pts.max(axis=1), test_pts.min(axis=1)
        sg_error = numpy.linalg.norm( test_vals - 
                                      sg.evaluate_set( test_pts ).squeeze() )
        print 'sparse grid error: ', sg_error
        assert sg_error < 4e-10
        enhanced_sg_error = numpy.linalg.norm( test_vals - 
                            evaluate_enhanced_sparse_grid( test_pts ) )
        print 'enhanced sparse grid error: ', enhanced_sg_error
        assert enhanced_sg_error < 8e-15

        #plot_surface_from_function( m.evaluate_set, domain, 100 )
        
        import pylab
        import mpl_toolkits.mplot3d.axes3d as p3
        fig = pylab.figure( 1 )
        ax = p3.Axes3D( fig )
        grid = sg.get_coordinates()
        fv = sg.get_function_values()
        ax.scatter3D( grid[0,:], grid[1,:], fv[0,:] )
Example #2
0
def generalised_sparse_grid_aposteriori_error_based_refinement():

    num_dims = 2

    quadrature_rule_1d = ClenshawCurtisQuadRule1D()
    basis = LagrangePolynomialBasis()

    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.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.initialise()
    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)

    domain = numpy.array([0.0, 1.0, 0.0, 1], numpy.double)
    sg = AposterioriGeneralisedSparseGrid()

    # todo this should be set automatically by model. Then can replace
    # pt.getDegreesOfFreedom by grid.num_qoi()
    # sg.quantities_of_interest( numpy.arange( forward_solution_num_DOF +
    #                           adjoint_solution_num_DOF + num_dims + 2,
    #                                         dtype = numpy.int32 ) )

    sg.tolerance(0.0)
    sg.max_num_points(4000)
    sg.max_level(20)
    sg.forward_solution_num_DOF(int(forward_solution_num_DOF))
    sg.adjoint_solution_num_DOF(int(adjoint_solution_num_DOF))

    from utilities.visualisation import plot_surface_from_function

    sg.initialise(m, domain, tpqr, 0)
    sg.build()
    print "sparse grid built successfully"
    print "sparse grid has ", sg.num_points(), " points"

    # 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) + sg.compute_error_estimates(x)).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()