Page MenuHomec4science

sampling_engine_krylov.py
No OneTemporary

File Metadata

Created
Wed, May 8, 18:48

sampling_engine_krylov.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 numpy as np
from rrompy.sampling.base.sampling_engine_base import SamplingEngineBase
from rrompy.utilities.base.types import Np1D, Np2D
from rrompy.utilities.base import verbosityDepth
from rrompy.utilities.exception_manager import RROMPyException
__all__ = ['SamplingEngineKrylov']
class SamplingEngineKrylov(SamplingEngineBase):
"""HERE"""
def preprocesssamples(self):
if self.samples is None: return
return self.samples[:, : self.nsamples]
def preprocessb(self, mu:complex, overwrite : bool = False,
homogeneized : bool = False):
return self.HFEngine.b(mu, self.nsamples, homogeneized = homogeneized)
def postprocessu(self, u:Np1D, overwrite : bool = False):
return u
def nextSample(self, mu:complex, overwrite : bool = False,
homogeneized : bool = False) -> Np1D:
ns = self.nsamples
if self.verbosity >= 10:
verbosityDepth("INIT", ("Setting up computation of {}-th Taylor "
"coefficient.").format(ns),
timestamp = self.timestamp)
samplesOld = self.preprocesssamples()
RHS = self.preprocessb(mu, overwrite = overwrite,
homogeneized = homogeneized)
for i in range(1, ns + 1):
RHS -= self.HFEngine.A(mu, i).dot(samplesOld[:, - i])
if self.verbosity >= 10:
verbosityDepth("DEL", "Done setting up for Taylor coefficient.",
timestamp = self.timestamp)
u = self.postprocessu(self.solveLS(mu, RHS = RHS,
homogeneized = homogeneized),
overwrite = overwrite)
if overwrite:
self.samples[:, ns] = u
self.mus[ns] = mu
else:
if ns == 0:
self.samples = u[:, None]
else:
self.samples = np.hstack((self.samples, u[:, None]))
self.mus = self.mus + [mu]
self.nsamples += 1
return u
def iterSample(self, mu:complex, n:int,
homogeneized : bool = False) -> Np2D:
if self.verbosity >= 5:
verbosityDepth("INIT", ("Starting sampling iterations at mu = "
"{}.").format(mu),
timestamp = self.timestamp)
if n <= 0:
raise RROMPyException(("Number of Krylov iterations must be "
"positive."))
self.resetHistory()
u = self.nextSample(mu, homogeneized = homogeneized)
if n > 1:
self.preallocateSamples(u, mu, n)
for _ in range(1, n):
self.nextSample(mu, overwrite = True,
homogeneized = homogeneized)
if self.verbosity >= 5:
verbosityDepth("DEL", "Finished sampling iterations.",
timestamp = self.timestamp)
return self.samples

Event Timeline