Page MenuHomec4science

rational_pade.py
No OneTemporary

File Metadata

Created
Wed, May 1, 10:30

rational_pade.py

# Copyright (C) 2018 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import os
import numpy as np
from matrix_fft import matrixFFT
from rrompy.reduction_methods.centered import RationalPade as RP
def test_rho(capsys):
mu = 1.5
mu0 = 2. + 1.j
solver = matrixFFT()
uh = solver.solve(mu)
params = {"POD": False, "rho": 3., "M": 4, "N": 5, "E": 10,
"robustTol": 1e-6}
approx = RP(solver, mu0, params, verbosity = 0)
approx.setupApprox()
out, err = capsys.readouterr()
assert ("Smallest 2 eigenvalues below tolerance. Reducing N from 5 to 4 "
"and E from 10 to 9.") in out
assert len(err) == 0
if not os.path.isdir("./.pytest_cache"): os.mkdir("./.pytest_cache")
filesOut = [x for x in os.listdir("./.pytest_cache") if
(x[-4:] == ".pkl" and x[:6] == "outRho")]
for fileOut in filesOut: os.remove("./.pytest_cache/" + fileOut)
fileStored = approx.storeTrainedModel(".pytest_cache/outRho")
filesOut = [x for x in os.listdir("./.pytest_cache") if
(x[-4:] == ".pkl" and x[:6] == "outRho")]
assert len(filesOut) == 1
assert filesOut[0] == fileStored[- len(filesOut[0]) :]
uhP1 = approx.getApprox(mu)
errP = approx.getErr(mu)
errNP = approx.normErr(mu)
myerrP = uhP1 - uh
assert np.allclose(np.abs(errP - myerrP), 0., rtol = 1e-3)
assert np.isclose(solver.norm(errP), errNP, rtol = 1e-3)
resP = approx.getRes(mu)
resNP = approx.normRes(mu)
assert np.isclose(solver.norm(resP), resNP, rtol = 1e-3)
assert np.allclose(np.abs(resP - (solver.b(mu) - solver.A(mu).dot(uhP1))),
0., rtol = 1e-3)
del approx
approx = RP(solver, mu0, {"E": 3}, verbosity = 0)
approx.loadTrainedModel(fileStored)
for fileOut in filesOut: os.remove("./.pytest_cache/" + fileOut)
uhP2 = approx.getApprox(mu)
assert np.allclose(np.abs(uhP1 - uhP2), 0., rtol = 1e-3)
def test_E_warn(capsys):
mu = 1.5
mu0 = 2. + 1.j
solver = matrixFFT()
uh = solver.solve(mu)
params = {"POD": True, "rho": 3., "M": 4, "N": 5, "E": 2}
approx = RP(solver, mu0, params, verbosity = 0)
approx.setupApprox()
out, err = capsys.readouterr()
assert "Prescribed E is too small. Updating E to M + N." in out
assert len(err) == 0
uhP = approx.getApprox(mu)
errP = approx.getErr(mu)
errNP = approx.normErr(mu)
assert np.allclose(np.abs(errP - (uhP - uh)), 0., rtol = 1e-3)
assert np.isclose(errNP, 0.1372966, rtol = 1e-1)
poles, ress = approx.getResidues()
condres = np.linalg.cond(solver.innerProduct(ress, ress))
assert np.isclose(condres, 36.63625, rtol = 1e-3)
assert np.isclose(np.min(np.abs(poles - 2.)), 0., atol = 1e-5)
assert np.isclose(np.min(np.abs(poles - 1.)), 0., atol = 1e-3)
assert np.isclose(np.min(np.abs(poles - 3.)), 0., atol = 1e-3)

Event Timeline