def refine_uniform_from_tetgen(filebase,refinementLevels,index_base=0,EB=False): from proteus import MeshTools,Archiver mesh = MeshTools.TetrahedralMesh() mesh.generateFromTetgenFiles(filebase,index_base,skipGeometricInit=False) MLMesh = MeshTools.MultilevelTetrahedralMesh(0,0,0,skipInit=True) MLMesh.generateFromExistingCoarseMesh(mesh,refinementLevels) MLMesh.meshList[-1].writeTetgenFiles(filebase+'_out',index_base) ar = Archiver.XdmfArchive('.',filebase+'_out') import xml.etree.ElementTree as ElementTree ar.domain = ElementTree.SubElement(ar.tree.getroot(),"Domain") mesh.writeMeshXdmf(ar,'mesh_coarse'+'_out',init=True,EB=EB,tCount=0) MLMesh.meshList[-1].writeMeshXdmf(ar,'mesh_fine'+'_out',init=True,EB=EB,tCount=1) ar.close()
def test_svd_soln(): """ test SVD decomposition of solution by generating SVD, saving to file and reloading """ from proteus.deim_utils import read_snapshots, generate_svd_decomposition ns = get_burgers_ns("test_svd_soln", T=0.1, nDTout=10, archive_pod_res=True) failed = ns.calculateSolution("run_svd_soln") assert not failed from proteus import Archiver archive = Archiver.XdmfArchive(".", "test_svd_soln", readOnly=True) U, s, V = generate_svd_decomposition(archive, len(ns.tnList), 'u', 'soln') S_svd = np.dot(U, np.dot(np.diag(s), V)) #now load back in and test S = read_snapshots(archive, len(ns.tnList), 'u') npt.assert_almost_equal(S, S_svd)
#!/usr/bin/env python from burgers_init import use_deim from proteus import deim_utils,Archiver from proteus.deim_utils import read_snapshots T = 1.0 nDTout = 100 DT = T/float(nDTout) archive = Archiver.XdmfArchive(".",burgers_init.physics.name,readOnly=True) import numpy as np #generate snapshots for solution S = read_snapshots(archive,nDTout+1,'u',) U, s, V = np.linalg.svd(S, full_matrices=False) print 'SVD for solution done!' np.savetxt('SVD_basis', U, delimiter=' ') np.savetxt('Singular_values', s, delimiter=' ') if use_deim: Sf = read_snapshots(archive,nDTout+1,'spatial_residual0') Uf,sf,Vf = np.linalg.svd(Sf,full_matrices=False) print 'SVD for spatial residual done!' np.savetxt('Fs_SVD_basis', Uf, delimiter=' ') np.savetxt('Fs_Singular_values', sf, delimiter=' ') Sm = read_snapshots(archive,nDTout+1,'mass_residual0') Um,sm,Vm = np.linalg.svd(Sm,full_matrices=False) print 'SVD for mass residual done!' np.savetxt('Fm_SVD_basis', Um, delimiter=' ')
def deim_approx(T=0.1, nDTout=10, m=5, m_mass=5): """ Follow basic setup for DEIM approximation - generate a burgers solution, saving spatial and 'mass' residuals - generate SVDs for snapshots - for both residuals F_s and F_m - pick $m$, dimension for snapshot reduced basis $\mathbf{U}_m$ - call DEIM algorithm to determine $\vec \rho$ and compute projection matrix $\mathbf{P}_F=\mathbf{U}_m(\mathbf{P}^T\mathbf{U}_m)^{-1}$ For selected timesteps - extract fine grid solution from archive, $\vec y$ - for both residuals F=F_s and F_m - evaluate $\vec F(\vec y)$ at indices in $\vec \rho \rightarrow \vec c$ - apply DEIM interpolant $\tilde{\vec F} = \mathbf{P}_F\vec c$ - compute error $\|F-\tilde{\vec F}\| - visualize """ from proteus.deim_utils import read_snapshots, generate_svd_decomposition ##run fine grid problem ns = get_burgers_ns("test_deim_approx", T=T, nDTout=nDTout, archive_pod_res=True) failed = ns.calculateSolution("run_deim_approx") assert not failed from proteus import Archiver archive = Archiver.XdmfArchive(".", "test_deim_approx", readOnly=True) ##perform SVD on spatial residual U, s, V = generate_svd_decomposition(archive, len(ns.tnList), 'spatial_residual0', 'F_s') U_m, s_m, V_m = generate_svd_decomposition(archive, len(ns.tnList), 'mass_residual0', 'F_m') from proteus.deim_utils import deim_alg ##calculate DEIM indices and projection matrix rho, PF = deim_alg(U, m) #also 'mass' term rho_m, PF_m = deim_alg(U_m, m_mass) ##for comparison, grab snapshots of solution and residual Su = read_snapshots(archive, len(ns.tnList), 'u') Sf = read_snapshots(archive, len(ns.tnList), 'spatial_residual0') Sm = read_snapshots(archive, len(ns.tnList), 'mass_residual0') steps_to_test = np.arange(len(ns.tnList)) errors = np.zeros(len(steps_to_test), 'd') errors_mass = errors.copy() F_deim = np.zeros((Sf.shape[0], len(steps_to_test)), 'd') Fm_deim = np.zeros((Sf.shape[0], len(steps_to_test)), 'd') for i, istep in enumerate(steps_to_test): #solution on the fine grid u = Su[:, istep] #spatial residual evaluated from fine grid F = Sf[:, istep] #deim approximation on the fine grid F_deim[:, istep] = np.dot(PF, F[rho]) errors[i] = np.linalg.norm(F - F_deim[:, istep]) #repeat for 'mass residual' Fm = Sm[:, istep] #deim approximation on the fine grid Fm_deim[:, istep] = np.dot(PF_m, Fm[rho]) errors_mass[i] = np.linalg.norm(Fm - Fm_deim[:, istep]) # np.savetxt( "deim_approx_errors_space_test_T={0}_nDT={1}_m={2}.dat".format( T, nDTout, m), errors) np.savetxt( "deim_approx_errors_mass_test_T={0}_nDT={1}_m={2}.dat".format( T, nDTout, m_mass), errors_mass) return errors, errors_mass, F_deim, Fm_deim