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)
def teXXXst_fenics_vector(): # quad_degree = 13 # dolfin.parameters["form_compiler"]["quadrature_degree"] = quad_degree pi = 3.14159265358979323 k1, k2 = 2, 3 EV = pi * pi * (k1 * k1 + k2 * k2) N = 11 degree = 1 mesh = UnitSquare(N, N) fs = FunctionSpace(mesh, "CG", degree) ex = Expression("A*sin(k1*pi*x[0])*sin(k2*pi*x[1])", k1=k1, k2=k2, A=1.0) x = FEniCSVector(interpolate(ex, fs)) # print "x.coeff", x.coeffs.array() ex.A = EV b_ex = assemble_rhs(ex, fs) bexg = interpolate(ex, fs) # print b_ex.array() # print b_ex.array() / (2 * pi * pi * x.coeffs.array()) Afe = assemble_lhs(Expression('1'), fs) # apply discrete operator on (interpolated) x A = FEniCSOperator(Afe, x.basis) b = A * x # evaluate solution for eigenfunction rhs if False: b_num = Function(fs) solve(A, b_num.vector(), b_ex) bnv = A * b_num.vector() b3 = Function(fs, bnv / EV) np.set_printoptions(threshold='nan', suppress=True) print b.coeffs.array() print np.abs((b_ex.array() - b.coeffs.array()) / np.max(b_ex.array())) print np.max(np.abs((b_ex.array() - b.coeffs.array()) / np.max(b_ex.array()))) #print b_ex.array() / (M * interpolate(ex1, fs).vector()).array() # #assert_array_almost_equal(b.coeffs, b_ex.coeffs) b2 = Function(fs, b_ex.copy()) bg = Function(fs, b_ex.copy()) b2g = Function(fs, b_ex.copy()) G = assemble_gramian(x.basis) dolfin.solve(G, bg.vector(), b.coeffs) dolfin.solve(G, b2g.vector(), b2.vector()) # # compute eigenpairs numerically # eigensolver = evaluate_evp(FEniCSBasis(fs)) # # Extract largest (first) eigenpair # r, c, rx, cx = eigensolver.get_eigenpair(0) # print "Largest eigenvalue: ", r # # Initialize function and assign eigenvector # ef0 = Function(fs) # ef0.vector()[:] = rx if False: # export out_b = dolfin.File(__name__ + "_b.pvd", "compressed") out_b << b._fefunc out_b_ex = dolfin.File(__name__ + "_b_ex.pvd", "compressed") out_b_ex << b2 out_b_num = dolfin.File(__name__ + "_b_num.pvd", "compressed") out_b_num << b_num #dolfin.plot(x._fefunc, title="interpolant x", rescale=False, axes=True, legend=True) dolfin.plot(bg, title="b", rescale=False, axes=True, legend=True) dolfin.plot(b2g, title="b_ex (ass/G)", rescale=False, axes=True, legend=True) dolfin.plot(bexg, title="b_ex (dir)", rescale=False, axes=True, legend=True) #dolfin.plot(b_num, title="b_num", rescale=False, axes=True, legend=True) # dolfin.plot(b3, title="M*b_num", rescale=False, axes=True, legend=True) #dolfin.plot(ef0, title="ef0", rescale=False, axes=True, legend=True) print dolfin.errornorm(u=b._fefunc, uh=b2) #, norm_type, degree, mesh) dolfin.interactive()
from spuq.fem.fenics.fenics_vector import FEniCSVector from spuq.application.egsz.multi_vector import MultiVector, MultiVectorWithProjection from spuq.math_utils.multiindex import Multiindex from spuq.math_utils.multiindex_set import MultiindexSet from dolfin import UnitSquare, FunctionSpace, Function, refine, Expression, plot, interactive from math import pi import pickle mis = [Multiindex(mis) for mis in MultiindexSet.createCompleteOrderSet(3, 1)] N = len(mis) meshes = [UnitSquare(10 + 2 * i, 10 + 2 * i) for i in range(N)] functions = [Function(FunctionSpace(m, "CG", d + 1)) for d, m in enumerate(meshes)] ex = Expression('sin(A*x[0])*sin(A*x[1])', A=1) for i, f in enumerate(functions): ex.A = 2 * pi * (i + 1) f.interpolate(ex) # test FEniCSVector # ================= vectors = [FEniCSVector(f) for f in functions] with open('test-vector.pkl', 'wb') as f: pickle.dump(vectors[1], f) with open('test-vector.pkl', "rb") as f: v1 = pickle.load(f) # test MultiVector # ================ #mv1 = MultiVector()