Page MenuHomec4science

greedy.py
No OneTemporary

File Metadata

Created
Sun, May 5, 05:12

greedy.py

import numpy as np
from all_forcing_engine import AllForcingEngine
from rrompy.reduction_methods.greedy import RationalInterpolantGreedy as RI
from rrompy.reduction_methods.greedy import ReducedBasisGreedy as RB
from rrompy.solver.fenics import L2NormMatrix
from rrompy.parameter.parameter_sampling import QuadratureSampler as QS
verb = 5
timed = False
algo = "RI"
#algo = "RB"
polyBasis = "LEGENDRE"
#polyBasis = "CHEBYSHEV"
#polyBasis = "MONOMIAL"
if timed: verb = 0
z0s = np.linspace(-3., 3., 100)
z0 = np.mean(z0s)
zl, zr = min(z0s), max(z0s)
n = 30
solver = AllForcingEngine(mu0 = z0, n = n, degree_threshold = 8, verbosity = 0)
params = {'sampler':QS([zl, zr], "UNIFORM"), 'nTestPoints':500,
'greedyTol':1e-2, 'S':2, 'polybasis':polyBasis,
# 'errorEstimatorKind':'DIAGONAL'}
# 'errorEstimatorKind':'INTERPOLATORY'}
'errorEstimatorKind':'AFFINE'}
if algo == "RI":
approx = RI(solver, mu0 = solver.mu0, approxParameters = params,
verbosity = verb)
else:
params.pop("polybasis")
params.pop("errorEstimatorKind")
approx = RB(solver, mu0 = solver.mu0, approxParameters = params,
verbosity = verb)
approx.initEstimatorNormEngine(L2NormMatrix(solver.V))
if timed:
from time import clock
start_time = clock()
approx.greedy()
print("Time: ", clock() - start_time)
else:
approx.greedy(True)
polesApp = approx.getPoles()
print("Poles:\n", polesApp)
approx.samplingEngine.verbosity = 0
approx.trainedModel.verbosity = 0
approx.verbosity = 0
zl, zr = np.real(zl), np.real(zr)
from matplotlib import pyplot as plt
normApp = np.zeros(len(z0s))
norm = np.zeros_like(normApp)
res = np.zeros_like(normApp)
err = np.zeros_like(normApp)
for j in range(len(z0s)):
normApp[j] = approx.normApprox(z0s[j])
norm[j] = approx.normHF(z0s[j])
res[j] = (approx.estimatorNormEngine.norm(approx.getRes(z0s[j]))
/ approx.estimatorNormEngine.norm(approx.getRHS(z0s[j])))
err[j] = approx.normErr(z0s[j]) / norm[j]
resApp = approx.errorEstimator(z0s)
plt.figure()
plt.semilogy(z0s, norm)
plt.semilogy(z0s, normApp, '--')
plt.semilogy(np.real(approx.mus.data),
1.05*np.max(norm)*np.ones_like(approx.mus.data, dtype = float),
'rx')
plt.xlim([zl, zr])
plt.grid()
plt.show()
plt.close()
plt.figure()
plt.semilogy(z0s, res)
plt.semilogy(z0s, resApp, '--')
plt.semilogy(np.real(approx.mus.data),
approx.greedyTol*np.ones_like(approx.mus.data, dtype = float),
'rx')
plt.xlim([zl, zr])
plt.grid()
plt.show()
plt.close()
plt.figure()
plt.semilogy(z0s, err)
plt.xlim([zl, zr])
plt.grid()
plt.show()
plt.close()

Event Timeline