def test_specify_support_twice(self): # # TODO: Haven't done it for precision yet # oort = 1 / np.sqrt(2) V = np.array([[0.5, oort, 0, 0.5], [0.5, 0, -oort, -0.5], [0.5, -oort, 0, 0.5], [0.5, 0, oort, -0.5]]) # Eigenvalues d = np.array([4, 0, 2, 0], dtype=float) Lmd = np.diag(d) # Covariance matrix K = V.dot(Lmd.dot(V.T)) # # Restrict subspace in two steps # # Field with predefined subspace u = GaussianField(4, K=K, mode='covariance', support=V[:, 0:3]) # Further restrict subspace (automatically) u.update_support() # # Reduce subspace at once # v = GaussianField(4, K=K, mode='covariance', support=V[:, [0, 2]]) # Check that the supports are the same U = u.support() V = v.support() self.assertTrue(np.allclose(U - V.dot(V.T.dot(U)), np.zeros(U.shape))) self.assertTrue(np.allclose(V - U.dot(U.T.dot(V)), np.zeros(V.shape)))
def test_degenerate_sample(self): """ Test support and reduced covariance """ oort = 1 / np.sqrt(2) V = np.array([[0.5, oort, 0, 0.5], [0.5, 0, -oort, -0.5], [0.5, -oort, 0, 0.5], [0.5, 0, oort, -0.5]]) # Eigenvalues d = np.array([4, 3, 2, 0], dtype=float) Lmd = np.diag(d) # Covariance matrix K = V.dot(Lmd.dot(V.T)) # Zero mean Gaussian field u_ex = GaussianField(4, K=K, mode='covariance', support=V[:, 0:3]) u_im = GaussianField(4, K=K, mode='covariance') u_im.update_support() # Check reduced covariances self.assertTrue( np.allclose(u_ex.covariance().get_matrix(), u_im.covariance().get_matrix().toarray())) # Check supports V_ex = u_ex.support() V_im = u_im.support() # Ensure they have the same sign for i in range(V_ex.shape[1]): if V_ex[0, i] < 0: V_ex[:, i] = -V_ex[:, i] if V_im[0, i] < 0: V_im[:, i] = -V_im[:, i] self.assertTrue(np.allclose(V_ex, V_im)) u_ex.set_support(V_ex) u_im.set_support(V_im) # Compare samples z = u_ex.iid_gauss(n_samples=1) u_ex_smp = u_ex.sample(z=z, decomposition='chol') u_im_smp = u_im.sample(z=z, decomposition='chol') self.assertTrue(np.allclose(u_ex_smp, u_im_smp))
def test02_variance(): """ Compute the variance of J(q) for different mesh refinement levels and compare with MC estimates. """ l_max = 8 for i_res in np.arange(2, l_max): # Computational mesh mesh = Mesh1D(resolution=(2**i_res, )) # Element element = QuadFE(mesh.dim(), 'DQ0') dofhandler = DofHandler(mesh, element) dofhandler.distribute_dofs() # Linear Functional mesh.mark_region('integrate', lambda x: x >= 0.75, entity_type='cell', strict_containment=False) phi = Basis(dofhandler) assembler = Assembler(Form(4, test=phi, flag='integrate')) assembler.assemble() L = assembler.get_vector() # Define Gaussian random field C = Covariance(dofhandler, name='gaussian', parameters={'l': 0.05}) C.compute_eig_decomp() eta = GaussianField(dofhandler.n_dofs(), K=C) eta.update_support() n_samples = 100000 J_paths = L.dot(eta.sample(n_samples=n_samples)) var_mc = np.var(J_paths) lmd, V = C.get_eig_decomp() LV = L.dot(V) var_an = LV.dot(np.diag(lmd).dot(LV.transpose())) print(var_mc, var_an)
u = Nodal(dofhandler=dh_y, data=u_data, dim=1) # # Regularization parameter # #gamma = 0.00001 gamma = 0.1 # # Random diffusion coefficient # n_samples = 200 cov = Covariance(dh_y, name='gaussian', parameters={'l':0.1}) k = GaussianField(ny, K=cov) k.update_support() kfn = Nodal(dofhandler=dh_y, data=k.sample(n_samples=n_samples)) # ============================================================================= # Assembly # ============================================================================= K = Kernel(kfn, F=lambda f:np.exp(f)) # diffusivity problems = [[Form(K, test=phi_x, trial=phi_x)], [Form(test=phi, trial=phi)]] assembler = Assembler(problems, mesh) assembler.assemble() # Mass matrix (for control)
# ============================================================================= # Random field # ============================================================================= n_samples = 10 n_train, n_test = 8, 2 i_train = np.arange(n_train) i_test = np.arange(n_train, n_samples) cov = Covariance(dofhandler, name='gaussian', parameters={'l': 0.1}) cov.compute_eig_decomp() d, V = cov.get_eig_decomp() plt.semilogy(d, '.') plt.show() log_q = GaussianField(n, K=cov) log_q.update_support() qfn = Nodal(dofhandler=dofhandler, data=np.exp(log_q.sample(n_samples=n_samples))) plot = Plot() plot.line(qfn, i_sample=np.arange(n_samples)) # ============================================================================= # Generate Snapshot Set # ============================================================================= phi = Basis(dofhandler, 'u') phi_x = Basis(dofhandler, 'ux') problems = [[Form(kernel=qfn, trial=phi_x, test=phi_x), Form(1, test=phi)], [Form(1, test=phi, trial=phi)]]
CC.compute_eig_decomp() d, V = CC.get_eig_decomp() print(d) lmd = np.arange(len(d)) ax.semilogy(lmd, d, '.-', label='level=%d' % i) plt.legend() plt.show() # # Define random field on the fine mesh # C = Covariance(dofhandler, name='gaussian', parameters={'l': 0.05}) C.compute_eig_decomp() eta = GaussianField(dofhandler.n_dofs(), K=C) eta.update_support() #eta_path = Nodal(data=eta.sample(), basis=phi) eta0 = P.dot(eta.sample()) eg0 = eta.condition(P, eta0, n_samples=100) eg0_paths = Nodal(data=eg0, basis=Basis(dofhandler)) e0_path = Nodal(data=eta0, basis=Basis(dofhandler, subforest_flag=0)) plot = Plot(quickview=False) ax = plt.subplot(111) for i in range(30): ax = plot.line(eg0_paths, axis=ax, mesh=mesh, i_sample=i, plot_kwargs={ 'color': 'k',