Ejemplo n.º 1
0
def test_neg():
    mv1 = MultiVector()
    mis1 = MultiindexSet.createCompleteOrderSet(3, 4)
    mv1.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv = -mv1
    assert_equal(mv[Multiindex(mis1[-1])], FlatVector([-3, -4, -5]))
    assert_equal(mv1[Multiindex(mis1[-1])], FlatVector([3, 4, 5]))
Ejemplo n.º 2
0
def test_set_defaults():
    mv = MultiVector()
    mis = MultiindexSet.createCompleteOrderSet(3, 4)
    mv.set_defaults(mis, FlatVector([3, 4, 5]))
    assert_equal(mv[Multiindex([1, 2, 1])], FlatVector([3, 4, 5]))
    assert_equal(mv[Multiindex()], FlatVector([3, 4, 5]))
    assert_raises(KeyError, mv.__getitem__, Multiindex([1, 2, 2]))
Ejemplo n.º 3
0
def test_equality():
    mv1 = MultiVector()
    mv2 = MultiVector()
    mv3 = MultiVector()
    mv4 = MultiVector()
    mis1 = MultiindexSet.createCompleteOrderSet(3, 4)
    mis2 = MultiindexSet.createCompleteOrderSet(3, 5)
    mv1.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv2.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv3.set_defaults(mis2, FlatVector([3, 4, 5]))
    mv4.set_defaults(mis1, FlatVector([3, 4, 6]))
    assert_true(mv1 == mv2)
    assert_false(mv1 != mv2)
    assert_true(mv1 != mv3)
    assert_false(mv1 == mv3)
    assert_true(mv1 != mv4)
    assert_false(mv1 == mv4)
Ejemplo n.º 4
0
def test_add():
    mv1 = MultiVector()
    mv2 = MultiVector()
    mv3 = MultiVector()
    mis1 = MultiindexSet.createCompleteOrderSet(3, 4)
    mv1.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv2.set_defaults(mis1, FlatVector([6, 8, 12]))
    mv3.set_defaults(mis1, FlatVector([9, 12, 17]))
    assert_equal(mv1 + mv2, mv3)
Ejemplo n.º 5
0
def prepare_stochastic_operators(M, p1, p2=0, type='L'):
    I = MultiindexSet.createCompleteOrderSet(M, p1).arr
    if type == 'L':
        print "Legendre triple", I.shape
        polysys = LegendrePolynomials()
        L = evaluate_triples(polysys, I, I)
    elif type == 'H':
        assert False
        #J = MultiindexSet.createCompleteOrderSet(M, p2).arr
        #H = evaluate_Hermite_triple(I, I, J)
        #L = [L[:, :, k] for k in range(L.shape[2])]
    return L
Ejemplo n.º 6
0
def initialise_multivector(mv, M):
    # initial multiindices
    mis = [Multiindex(mis) for mis in MultiindexSet.createCompleteOrderSet(M, 1)]
    # intialise fem vectors
    N = 15
    mesh = UnitSquareMesh(N, N)
    V = FunctionSpace(mesh, 'CG', 1)
    ex = Expression('sin(2*pi*A*x[0])', A=0)
    for i, mi in enumerate(mis):
        ex.A = i+1
        f = interpolate(ex, V)
        mv[mi] = FEniCSVector(f)
Ejemplo n.º 7
0
def test_sub():
    mv1 = MultiVector()
    mv2 = MultiVector()
    mi1 = Multiindex([1, 2, 1])
    mi2 = Multiindex([3, 2, 1, 7])
    mis1 = MultiindexSet.createCompleteOrderSet(3, 4)
    mv1.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv2.set_defaults(mis1, FlatVector([6, 8, 10]))
    mv1[mi2] = FlatVector([7, 10, 6])
    mv2[mi2] = FlatVector([2, 6, 13])
    mv3 = mv1 - mv2
    assert_equal(mv3[mi1], FlatVector([-3, -4, -5]))
    assert_equal(mv3[mi2], FlatVector([5, 4, -7]))
Ejemplo n.º 8
0
def test_mul():
    mv1 = MultiVector()
    mv2 = MultiVector()
    mis1 = MultiindexSet.createCompleteOrderSet(3, 4)
    #mis2 = MultiindexSet.createCompleteOrderSet(3, 5)
    mv1.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv2.set_defaults(mis1, FlatVector([6, 8, 10]))
    assert_equal(2 * mv1, mv2)
    assert_equal(mv1 * 2, mv2)
    assert_equal(2.0 * mv1, mv2)
    assert_equal(mv1 * 2.0, mv2)
    assert_equal(mv1[Multiindex()], FlatVector([3, 4, 5]))
    assert_equal(mv1[Multiindex([1, 2, 1])], FlatVector([3, 4, 5]))
    mv1 *= 2.0
    assert_equal(mv1, mv2)
Ejemplo n.º 9
0
    def setupCF2(cls, functype, amptype, rvtype='uniform', gamma=0.9, decayexp=2, freqscale=1, freqskip=0, N=1, scale=1, dim=2, secondparam=None):
        try:
            rvs = cls.rv_defs[rvtype]
        except KeyError:
            raise ValueError("Unknown RV type %s", rvtype)

        try:
            func = cls.func_defs[(functype, dim)]
        except KeyError:
            raise ValueError("Unknown function type %s for dim %s", functype, dim)
            

        if amptype == "decay-inf":
            start = SampleProblem.get_decay_start(decayexp, gamma)
            amp = gamma / zeta(decayexp, start)
            ampfunc = lambda i: amp / (float(i) + start) ** decayexp
            logger.info("type is decay_inf with start = " + str(start) + " and amp = " + str(amp))
        elif amptype == "constant": 
            amp = gamma / N
            ampfunc = lambda i: gamma * (i < N)
        else:
            raise ValueError("Unknown amplitude type %s", amptype)

        logger.info("amp function: %s", str([ampfunc(i) for i in range(10)]))
        element = FiniteElement('Lagrange', ufl.triangle, 1)
        # NOTE: the explicit degree of the expression should influence the quadrature order during assembly
        degree = 3

        mis = MultiindexSet.createCompleteOrderSet(dim)
        for i in range(freqskip + 1):
            mis.next()

        a0 = Expression("B", element=element, B=scale)
        if dim == 1:
            a = (Expression(func, freq=freqscale, A=ampfunc(i), B=scale,
                            m=int(mu[0]), degree=degree, element=element) for i, mu in enumerate(mis))
        else:
            a = (Expression(func, freq=freqscale, A=ampfunc(i), B=scale,
                            m=int(mu[0]), n=int(mu[1]),
                            degree=degree, element=element) for i, mu in enumerate(mis))

        if secondparam is not None:
            from itertools import izip
            a0 = (a0, secondparam[0])
            a = ((am, bm) for am, bm in izip(a, secondparam[1]))
        return ParametricCoefficientField(a0, a, rvs)
Ejemplo n.º 10
0
def test_mvwp_copy():
    # compares equal to copied MultiVectorWP but not to MultiVector 
    mv0 = MultiVector()
    mv1 = MultiVectorWithProjection()
    mis1 = MultiindexSet.createCompleteOrderSet(3, 4)
    mv0.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv1.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv2 = mv1.copy()
    assert_equal(mv2, mv1)
    assert_not_equal(mv2, mv0)

    # compares equal if project methods match, make sure project method is copied 
    mv3 = MultiVectorWithProjection(project=lambda : None)
    mv3.set_defaults(mis1, FlatVector([3, 4, 5]))
    mv4 = mv3.copy()
    assert_not_equal(mv2, mv3)
    assert_equal(mv4, mv3)
Ejemplo n.º 11
0
# initial mesh elements
initial_mesh_N = CONF_initial_mesh_N

# plotting flag
PLOT_SOLUTION = True

# use alternate mayavi plotting
MAYAVI_PLOTTING = True


# ============================================================
# PART B: Problem Setup
# ============================================================
    
# define initial multiindices
mis = [Multiindex(mis) for mis in MultiindexSet.createCompleteOrderSet(CONF_initial_Lambda, 1)]
#mis = [mis[0], mis[2]]
#mis = [mis[0]]

# setup domain and meshes
mesh0, boundaries, dim = SampleDomain.setupDomain(CONF_domain, initial_mesh_N=initial_mesh_N)
#meshes = SampleProblem.setupMeshes(mesh0, len(mis), num_refine=10, randref=(0.4, 0.3))
meshes = SampleProblem.setupMeshes(mesh0, len(mis), num_refine=0)

# define coefficient field
# NOTE: for proper treatment of corner points, see elasticity_residual_estimator
coeff_types = ("EF-square-cos", "EF-square-sin", "monomials", "constant")
from itertools import count
muparam = (CONF_mu, (0 for _ in count())) 
coeff_field = SampleProblem.setupCF(coeff_types[CONF_coeff_type], decayexp=CONF_decay_exp, gamma=CONF_gamma,
                                    freqscale=CONF_freq_scale, freqskip=CONF_freq_skip, rvtype="uniform", scale=CONF_coeff_scale, secondparam=muparam)
Ejemplo n.º 12
0
def run_SFEM(opts, conf):
    # propagate config values
    for sec in conf.keys():
        if sec == "LOGGING":
            continue
        secconf = conf[sec]
        for key, val in secconf.iteritems():
            print "CONF_" + key + "= secconf['" + key + "'] =", secconf[key]
            exec "CONF_" + key + "= secconf['" + key + "']"

    # setup logging
    print "LOG_LEVEL = logging." + conf["LOGGING"]["level"]
    exec "LOG_LEVEL = logging." + conf["LOGGING"]["level"]
    setup_logging(LOG_LEVEL, logfile=CONF_experiment_name + "_SFEM")
    
    # determine path of this module
    path = os.path.dirname(__file__)
    
    # ============================================================
    # PART A: Simulation Options
    # ============================================================
    
    # flags for residual, projection, new mi refinement 
    REFINEMENT = {"RES":CONF_refine_residual, "PROJ":CONF_refine_projection, "MI":CONF_refine_Lambda}

    
    # ============================================================
    # PART B: Problem Setup
    # ============================================================
    
    # define initial multiindices
    mis = [Multiindex(mis) for mis in MultiindexSet.createCompleteOrderSet(CONF_initial_Lambda, 1)]
    
    # setup domain and meshes
    mesh0, boundaries, dim = SampleDomain.setupDomain(CONF_domain, initial_mesh_N=CONF_initial_mesh_N)
    #meshes = SampleProblem.setupMeshes(mesh0, len(mis), num_refine=10, randref=(0.4, 0.3))
    meshes = SampleProblem.setupMeshes(mesh0, len(mis), num_refine=0)
    
    # define coefficient field
    # NOTE: for proper treatment of corner points, see elasticity_residual_estimator
    coeff_types = ("EF-square-cos", "EF-square-sin", "monomials", "constant")
    from itertools import count
    if CONF_mu is not None:
        muparam = (CONF_mu, (0 for _ in count()))
    else:
        muparam = None 
    coeff_field = SampleProblem.setupCF(coeff_types[CONF_coeff_type], decayexp=CONF_decay_exp, gamma=CONF_gamma,
                                    freqscale=CONF_freq_scale, freqskip=CONF_freq_skip, rvtype="uniform", scale=CONF_coeff_scale, secondparam=muparam)

    # setup boundary conditions and pde
    pde, Dirichlet_boundary, uD, Neumann_boundary, g, f = SampleProblem.setupPDE(CONF_boundary_type, CONF_domain, CONF_problem_type, boundaries, coeff_field)

    # define multioperator
    A = MultiOperator(coeff_field, pde.assemble_operator, pde.assemble_operator_inner_dofs, assembly_type=eval("ASSEMBLY_TYPE." + CONF_assembly_type))

    # setup initial solution multivector
    w = SampleProblem.setupMultiVector(dict([(mu, m) for mu, m in zip(mis, meshes)]), functools.partial(setup_vector, pde=pde, degree=CONF_FEM_degree))
    logger.info("active indices of w after initialisation: %s", w.active_indices())

    sim_stats = None
    w_history = []
    if opts.continueSFEM:
        try:
            logger.info("CONTINUIING EXPERIMENT: loading previous data of %s...", CONF_experiment_name)
            import pickle
            LOAD_SOLUTION = os.path.join(opts.basedir, CONF_experiment_name)
            logger.info("loading solutions from %s" % os.path.join(LOAD_SOLUTION, 'SFEM-SOLUTIONS.pkl'))
            # load solutions
            with open(os.path.join(LOAD_SOLUTION, 'SFEM-SOLUTIONS.pkl'), 'rb') as fin:
                w_history = pickle.load(fin)
            # convert to MultiVectorWithProjection
            for i, mv in enumerate(w_history):
                w_history[i] = MultiVectorWithProjection(cache_active=True, multivector=w_history[i])
            # load simulation data
            logger.info("loading statistics from %s" % os.path.join(LOAD_SOLUTION, 'SIM-STATS.pkl'))
            with open(os.path.join(LOAD_SOLUTION, 'SIM-STATS.pkl'), 'rb') as fin:
                sim_stats = pickle.load(fin)
            logger.info("active indices of w after initialisation: %s", w_history[-1].active_indices())
            w0 = w_history[-1]
        except:
            logger.warn("FAILED LOADING EXPERIMENT %s --- STARTING NEW DATA", CONF_experiment_name)
            w0 = w    
    else:
        w0 = w

    
    # ============================================================
    # PART C: Adaptive Algorithm
    # ============================================================
    
    # refinement loop
    # ===============
    w, sim_stats = AdaptiveSolver(A, coeff_field, pde, mis, w0, mesh0, CONF_FEM_degree, gamma=CONF_gamma, cQ=CONF_cQ, ceta=CONF_ceta,
                        # marking parameters
                        theta_eta=CONF_theta_eta, theta_zeta=CONF_theta_zeta, min_zeta=CONF_min_zeta,
                        maxh=CONF_maxh, newmi_add_maxm=CONF_newmi_add_maxm, theta_delta=CONF_theta_delta,
                        marking_strategy=CONF_marking_strategy, max_Lambda_frac=CONF_max_Lambda_frac,
                        # residual error evaluation
                        quadrature_degree=CONF_quadrature_degree,
                        # projection error evaluation
                        projection_degree_increase=CONF_projection_degree_increase, refine_projection_mesh=CONF_refine_projection_mesh,
                        # pcg solver
                        pcg_eps=CONF_pcg_eps, pcg_maxiter=CONF_pcg_maxiter,
                        # adaptive algorithm threshold
                        error_eps=CONF_error_eps,
                        # refinements
                        max_refinements=CONF_iterations, max_dof=CONF_max_dof,
                        do_refinement=REFINEMENT, do_uniform_refinement=CONF_uniform_refinement,
                        w_history=w_history,
                        sim_stats=sim_stats)
    
    from operator import itemgetter
    active_mi = [(mu, w[mu]._fefunc.function_space().mesh().num_cells()) for mu in w.active_indices()]
    active_mi = sorted(active_mi, key=itemgetter(1), reverse=True)
    logger.info("==== FINAL MESHES ====")
    for mu in active_mi:
        logger.info("--- %s has %s cells", mu[0], mu[1])
    print "ACTIVE MI:", active_mi
    print
    
    # memory usage info
    import resource
    logger.info("\n======================================\nMEMORY USED: " + str(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) + "\n======================================\n")
    
    
    # ============================================================
    # PART D: Export of Solutions and Simulation Data
    # ============================================================
        
    # flag for final solution export
    if opts.saveData:
        import pickle
        SAVE_SOLUTION = os.path.join(opts.basedir, CONF_experiment_name)
        try:
            os.makedirs(SAVE_SOLUTION)
        except:
            pass
        logger.info("saving solutions into %s" % os.path.join(SAVE_SOLUTION, 'SFEM-SOLUTIONS.pkl'))
        # save solutions
        with open(os.path.join(SAVE_SOLUTION, 'SFEM-SOLUTIONS.pkl'), 'wb') as fout:
            pickle.dump(w_history, fout)
        # save simulation data
        sim_stats[0]["OPTS"] = opts
        sim_stats[0]["CONF"] = conf
        logger.info("saving statistics into %s" % os.path.join(SAVE_SOLUTION, 'SIM-STATS.pkl'))
        with open(os.path.join(SAVE_SOLUTION, 'SIM-STATS.pkl'), 'wb') as fout:
            pickle.dump(sim_stats, fout)
    
    
    # ============================================================
    # PART E: Plotting
    # ============================================================
    
    # plot residuals
    if opts.plotEstimator and len(sim_stats) > 1:
        try:
            from matplotlib.pyplot import figure, show, legend
            X = [s["DOFS"] for s in sim_stats]
            print "DOFS", X
            L2 = [s["L2"] for s in sim_stats]
            H1 = [s["H1"] for s in sim_stats]
            errest = [sqrt(s["EST"]) for s in sim_stats]
            res_part = [s["RES-PART"] for s in sim_stats]
            proj_part = [s["PROJ-PART"] for s in sim_stats]
            pcg_part = [s["PCG-PART"] for s in sim_stats]
            _reserrmu = [s["RES-mu"] for s in sim_stats]
            _projerrmu = [s["PROJ-mu"] for s in sim_stats]
            proj_max_zeta = [s["PROJ-MAX-ZETA"] for s in sim_stats]
            proj_max_inactive_zeta = [s["PROJ-MAX-INACTIVE-ZETA"] for s in sim_stats]
            try:
                proj_inactive_zeta = sorted([v for v in sim_stats[-2]["PROJ-INACTIVE-ZETA"].values()], reverse=True)
            except:
                proj_inactive_zeta = None
            mi = [s["MI"] for s in sim_stats]
            num_mi = [len(m) for m in mi]
            time_pcg = [s["TIME-PCG"] for s in sim_stats]
            time_estimator = [s["TIME-ESTIMATOR"] for s in sim_stats]
            time_inactive_mi = [s["TIME-INACTIVE-MI"] for s in sim_stats]
            time_marking = [s["TIME-MARKING"] for s in sim_stats]
            reserrmu = defaultdict(list)
            for rem in _reserrmu:
                for mu, v in rem:
                    reserrmu[mu].append(v)
            projerrmu = defaultdict(list)
            for pem in _projerrmu:
                for mu, v in pem:
                    projerrmu[mu].append(v)
            print "errest", errest
    
            # --------
            # figure 2
            # --------
            fig2 = figure()
            fig2.suptitle("error estimator")
            ax = fig2.add_subplot(111)
            ax.loglog(X, errest, '-g<', label='error estimator')
            legend(loc='upper right')
    
            # --------
            # figure 3a
            # --------
            if opts.plotEstimatorAll:
                max_mu_plotting = 7
                fig3 = figure()
                fig3.suptitle("residual contributions")
                ax = fig3.add_subplot(111)
                for i, muv in enumerate(reserrmu.iteritems()):
                    mu, v = muv
                    if i < max_mu_plotting:
                        mu, v = muv
                        ms = str(mu)
                        ms = ms[ms.find('=') + 1:-1]
                        ax.loglog(X[-len(v):], v, '-g<', label=ms)
                legend(loc='upper right')
    
            # --------
            # figure 3b
            # --------
            if opts.plotEstimatorAll:
                fig3b = figure()
                fig3b.suptitle("projection contributions")
                ax = fig3b.add_subplot(111)
                for i, muv in enumerate(projerrmu.iteritems()):
                    mu, v = muv
                    if max(v) > 1e-10 and i < max_mu_plotting:
                        ms = str(mu)
                        ms = ms[ms.find('=') + 1:-1]
                        ax.loglog(X[-len(v):], v, '-g<', label=ms)
                legend(loc='upper right')
    
            # --------
            # figure 4
            # --------
            if opts.plotEstimatorAll:
                fig4 = figure()
                fig4.suptitle("projection $\zeta$")
                ax = fig4.add_subplot(111)
                ax.loglog(X[1:], proj_max_zeta[1:], '-g<', label='max active $\zeta$')
                ax.loglog(X[1:], proj_max_inactive_zeta[1:], '-b^', label='max inactive $\zeta$')
                legend(loc='upper right')
    
            # --------
            # figure 5
            # --------
            fig5 = figure()
            fig5.suptitle("timings")
            ax = fig5.add_subplot(111)
            ax.loglog(X, time_pcg, '-g<', label='pcg')
            ax.loglog(X, time_estimator, '-b^', label='estimator')
            ax.loglog(X, time_inactive_mi, '-c+', label='inactive_mi')
            ax.loglog(X, time_marking, '-ro', label='marking')
            legend(loc='upper right')
                
            # --------
            # figure 6
            # --------
            if opts.plotEstimatorAll:
                fig6 = figure()
                fig6.suptitle("projection error")
                ax = fig6.add_subplot(111)
                ax.loglog(X[1:], proj_part[1:], '-.m>', label='projection part')
                legend(loc='upper right')
                
            # --------
            # figure 7
            # --------
            if opts.plotEstimatorAll and proj_inactive_zeta is not None:
                fig7 = figure()
                fig7.suptitle("inactive multiindex $\zeta$")
                ax = fig7.add_subplot(111)
                ax.loglog(range(len(proj_inactive_zeta)), proj_inactive_zeta, '-.m>', label='inactive $\zeta$')
                legend(loc='lower right')
                
            # --------
            # figure 1
            # --------
            fig1 = figure()
            fig1.suptitle("residual estimator")
            ax = fig1.add_subplot(111)
            if REFINEMENT["MI"]:
                ax.loglog(X, num_mi, '--y+', label='active mi')
            ax.loglog(X, errest, '-g<', label='error estimator')
            ax.loglog(X, res_part, '-.cx', label='residual part')
            ax.loglog(X[1:], proj_part[1:], '-.m>', label='projection part')
            ax.loglog(X, pcg_part, '-.b>', label='pcg part')
            legend(loc='upper right')
            
            show()  # this invalidates the figure instances...
        except:
            import traceback
            print traceback.format_exc()
            logger.info("skipped plotting since matplotlib is not available...")
    
    # plot final meshes
    if opts.plotMesh:
        USE_MAYAVI = Plotter.hasMayavi() and False
        w = w_history[-1]
        for mu, vec in w.iteritems():
            if USE_MAYAVI:
                # mesh
    #            Plotter.figure(bgcolor=(1, 1, 1))
    #            mesh = vec.basis.mesh
    #            Plotter.plotMesh(mesh.coordinates(), mesh.cells(), representation='mesh')
    #            Plotter.axes()
    #            Plotter.labels()
    #            Plotter.title(str(mu))
                # function
                Plotter.figure(bgcolor=(1, 1, 1))
                mesh = vec.basis.mesh
                Plotter.plotMesh(mesh.coordinates(), mesh.cells(), vec.coeffs)
                Plotter.axes()
                Plotter.labels()
                Plotter.title(str(mu))
            else:
                viz_mesh = plot(vec.basis.mesh, title="mesh " + str(mu), interactive=False)
#                if SAVE_SOLUTION != '':
#                    viz_mesh.write_png(SAVE_SOLUTION + '/mesh' + str(mu) + '.png')
#                    viz_mesh.write_ps(SAVE_SOLUTION + '/mesh' + str(mu), format='pdf')
    #            vec.plot(title=str(mu), interactive=False)
        if USE_MAYAVI:
            Plotter.show(stop=True)
            Plotter.close(allfig=True)
        else:
            interactive()
    
    # plot sample solution
    if opts.plotSolution:
        w = w_history[-1]
        # get random field sample and evaluate solution (direct and parametric)
        RV_samples = coeff_field.sample_rvs()
        ref_maxm = w_history[-1].max_order
        sub_spaces = w[Multiindex()].basis.num_sub_spaces
        degree = w[Multiindex()].basis.degree
        maxh = min(w[Multiindex()].basis.minh / 4, CONF_max_h)
        maxh = w[Multiindex()].basis.minh
        projection_basis = get_projection_basis(mesh0, maxh=maxh, degree=degree, sub_spaces=sub_spaces)
        sample_sol_param = compute_parametric_sample_solution(RV_samples, coeff_field, w, projection_basis)
        sample_sol_direct = compute_direct_sample_solution(pde, RV_samples, coeff_field, A, ref_maxm, projection_basis)
        sol_variance = compute_solution_variance(coeff_field, w, projection_basis)
    
        # plot
        print sub_spaces
        if sub_spaces == 0:
            viz_p = plot(sample_sol_param._fefunc, title="parametric solution")
            viz_d = plot(sample_sol_direct._fefunc, title="direct solution")
            if ref_maxm > 0:
                viz_v = plot(sol_variance._fefunc, title="solution variance")
    
            # debug---
            if not True:        
                for mu in w.active_indices():
                    for i, wi in enumerate(w_history):
                        if i == len(w_history) - 1 or True:
                            plot(wi[mu]._fefunc, title="parametric solution " + str(mu) + " iteration " + str(i))
    #                        plot(wi[mu]._fefunc.function_space().mesh(), title="parametric solution " + str(mu) + " iteration " + str(i), axes=True)
                    interactive()
            # ---debug
            
#            for mu in w.active_indices():
#                plot(w[mu]._fefunc, title="parametric solution " + str(mu))
        else:
            mesh_param = sample_sol_param._fefunc.function_space().mesh()
            mesh_direct = sample_sol_direct._fefunc.function_space().mesh()
            wireframe = True
            viz_p = plot(sample_sol_param._fefunc, title="parametric solution", mode="displacement", mesh=mesh_param, wireframe=wireframe)#, rescale=False)
            viz_d = plot(sample_sol_direct._fefunc, title="direct solution", mode="displacement", mesh=mesh_direct, wireframe=wireframe)#, rescale=False)
            
#            for mu in w.active_indices():
#                viz_p = plot(w[mu]._fefunc, title="parametric solution: " + str(mu), mode="displacement", mesh=mesh_param, wireframe=wireframe)
        interactive()


    if opts.plotFlux:
        w = w_history[-1]
        # get random field sample and evaluate solution (direct and parametric)
        RV_samples = coeff_field.sample_rvs()
        ref_maxm = w_history[-1].max_order
        sub_spaces = w[Multiindex()].basis.num_sub_spaces
        degree = w[Multiindex()].basis.degree
        maxh = min(w[Multiindex()].basis.minh / 4, CONF_max_h)
        maxh = w[Multiindex()].basis.minh
        projection_basis = get_projection_basis(mesh0, maxh=maxh, degree=degree, sub_spaces=sub_spaces)
        vec_projection_basis = get_projection_basis(mesh0, maxh=maxh, degree=degree, sub_spaces=2)
        sample_sol_param = compute_parametric_sample_solution(RV_samples, coeff_field, w, projection_basis)
        sample_sol_direct = compute_direct_sample_solution(pde, RV_samples, coeff_field, A, ref_maxm, projection_basis)
        sol_variance = compute_solution_variance(coeff_field, w, projection_basis)
    
        sol_param_flux = compute_solution_flux(pde, RV_samples, coeff_field, sample_sol_param, ref_maxm, projection_basis, vec_projection_basis)
        sol_direct_flux = compute_solution_flux(pde, RV_samples, coeff_field, sample_sol_direct, ref_maxm, projection_basis, vec_projection_basis)

        # plot
        if sub_spaces == 0:
            #viz_p = plot(sol_param_flux._fefunc, title="parametric solution flux")
            flux_x, flux_y = sol_param_flux._fefunc.split(deepcopy=True)
            viz_x = plot(flux_x, title="parametric solution flux x")
            viz_y = plot(flux_y, title="parametric solution flux y")

            flux_x, flux_y = sol_direct_flux._fefunc.split(deepcopy=True)
            viz_x = plot(flux_x, title="direct solution flux x")
            viz_y = plot(flux_y, title="direct solution flux y")

        else:
            raise Exception("not implemented");
        interactive()
Ejemplo n.º 13
0
from matplotlib.pyplot import figure, show, spy

from spuq.math_utils.multiindex_set import MultiindexSet
from spuq.polyquad.structure_coefficients import evaluate_triples
from spuq.polyquad.polynomials import LegendrePolynomials


I = MultiindexSet.createCompleteOrderSet(4, 3, reversed=True).arr
print I

#H = evaluate_Hermite_triple(I, I, J)
#print "shape of H:", H.shape
    # create polynomial instance
lp = LegendrePolynomials()

L = evaluate_triples(lp, I, I)
print len(L), L[0].shape

# fig = figure()
# spy(np.sum(H, axis=2))
fig = figure()
#spy(np.sum(L))
spy(L[3])
show()
Ejemplo n.º 14
0
def run_SFEM(opts, conf):
    # propagate config values
    _G = globals()
    for sec in conf.keys():
        if sec == "LOGGING":
            continue
        secconf = conf[sec]
        for key, val in secconf.iteritems():
            print "CONF_" + key + "= secconf['" + key + "'] =", secconf[key]
            _G["CONF_" + key] = secconf[key]

    # setup logging
    _G["LOG_LEVEL"] = eval("logging." + conf["LOGGING"]["level"])
    exec "LOG_LEVEL = logging." + conf["LOGGING"]["level"]
    setup_logging(LOG_LEVEL, logfile=CONF_experiment_name + "_SFEM-P{0}".format(CONF_FEM_degree))
    
    # determine path of this module
    path = os.path.dirname(__file__)
    
    # ============================================================
    # PART A: Simulation Options
    # ============================================================
    
    # flags for residual and tail refinement 
    REFINEMENT = {"RES":CONF_refine_residual, "TAIL":CONF_refine_tail, "OSC":CONF_refine_osc}
    
    # ============================================================
    # PART B: Problem Setup
    # ============================================================
    
    # define initial multiindices
    mis = [Multiindex(mis) for mis in MultiindexSet.createCompleteOrderSet(CONF_initial_Lambda, 1)]
    
    # setup domain and meshes
    mesh0, boundaries, dim = SampleDomain.setupDomain(CONF_domain, initial_mesh_N=CONF_initial_mesh_N)
    #meshes = SampleProblem.setupMesh(mesh0, num_refine=10, randref=(0.4, 0.3))
    mesh0 = SampleProblem.setupMesh(mesh0, num_refine=0)
    
    # define coefficient field
    # NOTE: for proper treatment of corner points, see elasticity_residual_estimator
    coeff_types = ("EF-square-cos", "EF-square-sin", "monomials", "constant")
    from itertools import count
    if CONF_mu is not None:
        muparam = (CONF_mu, (0 for _ in count()))
    else:
        muparam = None 
    coeff_field = SampleProblem.setupCF(coeff_types[CONF_coeff_type], decayexp=CONF_decay_exp, gamma=CONF_gamma,
                                    freqscale=CONF_freq_scale, freqskip=CONF_freq_skip, rvtype="uniform", scale=CONF_coeff_scale, secondparam=muparam)

    # setup boundary conditions and pde
    pde, Dirichlet_boundary, uD, Neumann_boundary, g, f = SampleProblem.setupPDE(CONF_boundary_type, CONF_domain, CONF_problem_type, boundaries, coeff_field)

    # define multioperator
    A = MultiOperator(coeff_field, pde.assemble_operator, pde.assemble_operator_inner_dofs)

    # setup initial solution multivector
    w = SampleProblem.setupMultiVector(mis, pde, mesh0, CONF_FEM_degree)
    logger.info("active indices of w after initialisation: %s", w.active_indices())

    sim_stats = None
    w_history = []
    PATH_SOLUTION = os.path.join(opts.basedir, CONF_experiment_name)
    try:
        os.makedirs(PATH_SOLUTION)
    except:
        pass    
    FILE_SOLUTION = 'SFEM2-SOLUTIONS-P{0}.pkl'.format(CONF_FEM_degree)
    FILE_STATS = 'SIM2-STATS-P{0}.pkl'.format(CONF_FEM_degree)
    
    if opts.continueSFEM:
        try:
            logger.info("CONTINUING EXPERIMENT: loading previous data of %s...", CONF_experiment_name)
            import pickle
            logger.info("loading solutions from %s" % os.path.join(PATH_SOLUTION, FILE_SOLUTION))
            # load solutions
            with open(os.path.join(PATH_SOLUTION, FILE_SOLUTION), 'rb') as fin:
                w_history = pickle.load(fin)
            # convert to MultiVectorWithProjection
            for i, mv in enumerate(w_history):
                w_history[i] = MultiVectorSharedBasis(multivector=w_history[i])
            # load simulation data
            logger.info("loading statistics from %s" % os.path.join(PATH_SOLUTION, FILE_STATS))
            with open(os.path.join(PATH_SOLUTION, FILE_STATS), 'rb') as fin:
                sim_stats = pickle.load(fin)
            logger.info("active indices of w after initialisation: %s", w_history[-1].active_indices())
            w0 = w_history[-1]
        except:
            logger.warn("FAILED LOADING EXPERIMENT %s --- STARTING NEW DATA", CONF_experiment_name)
            w0 = w    
    else:
        w0 = w

    
    # ============================================================
    # PART C: Adaptive Algorithm
    # ============================================================
    
    # refinement loop
    # ===============
    w, sim_stats = AdaptiveSolver(A, coeff_field, pde, mis, w0, mesh0, CONF_FEM_degree,
                        # marking parameters
                        rho=CONF_rho, # tail factor
                        theta_x=CONF_theta_x, # residual marking bulk parameter
                        theta_y=CONF_theta_y, # tail bound marking bulk paramter
                        maxh=CONF_maxh, # maximal mesh width for coefficient maximum norm evaluation
                        add_maxm=CONF_add_maxm, # maximal search length for new new
                        # error estimator evaluation
                        estimator_type=CONF_estimator_type,
                        quadrature_degree=CONF_quadrature_degree,
                        # pcg solver
                        pcg_eps=CONF_pcg_eps, pcg_maxiter=CONF_pcg_maxiter,
                        # adaptive algorithm threshold
                        error_eps=CONF_error_eps,
                        # refinements
                        max_refinements=CONF_iterations, max_dof=CONF_max_dof,
                        do_refinement=REFINEMENT, do_uniform_refinement=CONF_uniform_refinement, refine_osc_factor=CONF_refine_osc_factor,
                        w_history=w_history,
                        sim_stats=sim_stats)
    
    from operator import itemgetter
    active_mi = [(mu, w[mu]._fefunc.function_space().mesh().num_cells()) for mu in w.active_indices()]
    active_mi = sorted(active_mi, key=itemgetter(1), reverse=True)
    logger.info("==== FINAL MESHES ====")
    for mu in active_mi:
        logger.info("--- %s has %s cells", mu[0], mu[1])
    print "ACTIVE MI:", active_mi
    print
    
    # memory usage info
    import resource
    logger.info("\n======================================\nMEMORY USED: " + str(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) + "\n======================================\n")
    
    
    # ============================================================
    # PART D: Export of Solutions and Simulation Data
    # ============================================================
        
    # flag for final solution export
    if opts.saveData:
        import pickle
        try:
            os.makedirs(PATH_SOLUTION)
        except:
            pass
        logger.info("saving solutions into %s" % os.path.join(PATH_SOLUTION, FILE_SOLUTION))
        # save solutions
        with open(os.path.join(PATH_SOLUTION, FILE_SOLUTION), 'wb') as fout:
            pickle.dump(w_history, fout)
        # save simulation data
        sim_stats[0]["OPTS"] = opts
        sim_stats[0]["CONF"] = conf
        logger.info("saving statistics into %s" % os.path.join(PATH_SOLUTION, FILE_STATS))
        with open(os.path.join(PATH_SOLUTION, FILE_STATS), 'wb') as fout:
            pickle.dump(sim_stats, fout)


    # ============================================================
    # PART E: Plotting
    # ============================================================
    
    # plot residuals
    if opts.plotEstimator and len(sim_stats) > 1:
        try:
            from matplotlib.pyplot import figure, show, legend
            X = [s["DOFS"] for s in sim_stats]
            print "DOFS", X
            err_est = [s["ERROR-EST"] for s in sim_stats]
            err_res = [s["ERROR-RES"] for s in sim_stats]
            err_tail = [s["ERROR-TAIL"] for s in sim_stats]
            res_L2 = [s["RESIDUAL-L2"] for s in sim_stats]
            res_H1A = [s["RESIDUAL-H1A"] for s in sim_stats]
            mi = [s["MI"] for s in sim_stats]
            num_mi = [len(m) for m in mi]
            
            # --------
            # figure 1
            # --------
            fig1 = figure()
            fig1.suptitle("residual estimator")
            ax = fig1.add_subplot(111)
            if REFINEMENT["TAIL"]:
                ax.loglog(X, num_mi, '--y+', label='active mi')
            ax.loglog(X, err_est, '-g<', label='error estimator')
            ax.loglog(X, err_res, '-.cx', label='residual')
            ax.loglog(X, err_tail, '-.m>', label='tail')
            legend(loc='upper right')
            
            print "RESIDUAL L2", res_L2
            print "RESIDUAL H1A", res_H1A
            print "EST", err_est
            print "RES", err_res
            print "TAIL", err_tail
            
            show()  # this invalidates the figure instances...
        except:
            import traceback
            print traceback.format_exc()
            logger.info("skipped plotting since matplotlib is not available...")

    # plot final meshes
    if opts.plotMesh:
        w = w_history[-1]
        viz_mesh = plot(w.basis.basis.mesh, title="shared mesh", interactive=False)
        interactive()
    
    # plot sample solution
    if opts.plotSolution:
        w = w_history[-1]
        # get random field sample and evaluate solution (direct and parametric)
        RV_samples = coeff_field.sample_rvs()
        ref_maxm = w_history[-1].max_order
        sub_spaces = w[Multiindex()].basis.num_sub_spaces
        degree = w[Multiindex()].basis.degree
        maxh = min(w[Multiindex()].basis.minh / 4, CONF_maxh)
        maxh = w[Multiindex()].basis.minh
        projection_basis = get_projection_basis(mesh0, maxh=maxh, degree=degree, sub_spaces=sub_spaces)
        sample_sol_param = compute_parametric_sample_solution(RV_samples, coeff_field, w, projection_basis)
        sample_sol_direct = compute_direct_sample_solution(pde, RV_samples, coeff_field, A, ref_maxm, projection_basis)
        sol_variance = compute_solution_variance(coeff_field, w, projection_basis)
    
        # plot
        print sub_spaces
        if sub_spaces == 0:
            viz_p = plot(sample_sol_param._fefunc, title="parametric solution")
            viz_d = plot(sample_sol_direct._fefunc, title="direct solution")
            if ref_maxm > 0:
                viz_v = plot(sol_variance._fefunc, title="solution variance")
        else:
            mesh_param = sample_sol_param._fefunc.function_space().mesh()
            mesh_direct = sample_sol_direct._fefunc.function_space().mesh()
            wireframe = True
            viz_p = plot(sample_sol_param._fefunc, title="parametric solution", mode="displacement", mesh=mesh_param, wireframe=wireframe)#, rescale=False)
            viz_d = plot(sample_sol_direct._fefunc, title="direct solution", mode="displacement", mesh=mesh_direct, wireframe=wireframe)#, rescale=False)
        interactive()
Ejemplo n.º 15
0
from spuq.fem.fenics.fenics_basis import FEniCSBasis
from spuq.math_utils.multiindex_set import MultiindexSet
from spuq.math_utils.multiindex import Multiindex

from dolfin import UnitSquareMesh, FunctionSpace
import numpy as np

# setup covariance
GCV = GaussianCovariance(sigma=1, a=1)

# construct discrete function space
N = 3
mesh = UnitSquareMesh(N, N)
V = FunctionSpace(mesh, 'CG', 1)
basis = FEniCSBasis(V)

# evaluate KL expansion
KL = KLexpansion(GCV, basis, M=5)

# evaluate transformed covariance
# initial multiindices
mis = [Multiindex(mis) for mis in MultiindexSet.createCompleteOrderSet(3, 1)]
sigma_, mu_ = 1, 0
phi = lambda gamma: np.exp(sigma_*gamma + mu_)
TCV = TransformedCovariance(mis, phi, KL, N=3)

# evaluate KL of transfor
KL2 = KLexpansion(TCV, basis, M=5)
# get pce coefficients of KL
r = eval_pce_from_KL(mis, KL, TCV._phii)
Ejemplo n.º 16
0
 def test_init(self):
     I = MultiindexSet.createCompleteOrderSet(2, 4)
Ejemplo n.º 17
0
    def setupCF2(cls, functype, amptype, rvtype='uniform', gamma=0.9, decayexp=2, freqscale=1, freqskip=0, N=1, scale=1, dim=2, secondparam=None):
        try:
            rvs = cls.rv_defs[rvtype]
        except KeyError:
            raise ValueError("Unknown RV type %s", rvtype)

        try:
            func = cls.func_defs[(functype, dim)]
        except KeyError:
            raise ValueError("Unknown function type %s for dim %s", functype, dim)
            
        if amptype == "decay-inf":
            start = SampleProblem.get_decay_start(decayexp, gamma)
            amp = gamma / zeta(decayexp, start)
            ampfunc = lambda i: amp / (float(i) + start) ** decayexp
            logger.info("type is decay_inf with start = " + str(start) + " and amp = " + str(amp))
        elif amptype == "constant": 
            amp = gamma / N
            ampfunc = lambda i: gamma * (i < N)
        else:
            raise ValueError("Unknown amplitude type %s", amptype)

        logger.info("amp function: %s", str([ampfunc(i) for i in range(10)]))
        element = FiniteElement('Lagrange', ufl.triangle, 1)
        # NOTE: the explicit degree of the expression should influence the quadrature order during assembly
        # TODO: this needs some serious thoughts!
        degree = 3

        mis = MultiindexSet.createCompleteOrderSet(dim)
        for i in range(freqskip + 1):
            mis.next()

        def generate_expression(func, freq, A, B, m, n, degree, element):
            func, min_val, max_val, max_grad = func
            if n is None:
                ex = Expression(func, freq=freq, A=A, B=B,
                                    m=m, degree=degree, element=element)
            else:
                ex = Expression(func, freq=freqscale, A=A, B=B,
                                    m=m, n=n, degree=degree, element=element)
            try:            
                for k, v in {"freq":freq, "A":A, "B":B, "m":m, "n":n, "pi":"np.pi"}.iteritems():
                    v = str(v)
                    min_val = min_val.replace(k, v)
                    max_val = max_val.replace(k, v)
                    max_grad = max_grad.replace(k, v)
                ex.min_val = eval(min_val)
                ex.max_val = eval(max_val)
                ex.max_grad = eval(max_grad)
            except:
                logger.info("no min/max info available for coefficients")
            return ex
            
        a0 = generate_expression(("B", "B", "B", "1.0"), freq=freqscale, A=1, B=scale, m=0, n=None, degree=0, element=element)
        if dim == 1:
            a = (generate_expression(func=func, freq=freqscale, A=ampfunc(i), B=scale,
                            m=int(mu[0]), n=None, degree=degree, element=element) for i, mu in enumerate(mis))
        else:
            a = (generate_expression(func=func, freq=freqscale, A=ampfunc(i), B=scale,
                            m=int(mu[0]), n=int(mu[1]), degree=degree, element=element) for i, mu in enumerate(mis))

        if secondparam is not None:
            from itertools import izip
            a0 = (a0, secondparam[0])
            a = ((am, bm) for am, bm in izip(a, secondparam[1]))
        return ParametricCoefficientField(a0, a, rvs)