Page MenuHomec4science

sampling_engine_pivoted_pod.py
No OneTemporary

File Metadata

Created
Tue, May 14, 23:10

sampling_engine_pivoted_pod.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 scipy.linalg import block_diag
from rrompy.sampling.base.pod_engine import PODEngine
from .sampling_engine_pivoted import SamplingEnginePivoted
from rrompy.utilities.base.types import Np1D, paramVal, sampList
from rrompy.utilities.base import verbosityManager as vbMng
from rrompy.sampling import sampleList, emptySampleList
__all__ = ['SamplingEnginePivotedPOD']
class SamplingEnginePivotedPOD(SamplingEnginePivoted):
@property
def HFEngine(self):
"""Value of HFEngine. Its assignment resets history."""
return self._HFEngine
@HFEngine.setter
def HFEngine(self, HFEngine):
self._HFEngine = HFEngine
self.resetHistory()
self.PODEngine = PODEngine(self._HFEngine)
def resetHistory(self, j : int = 0):
super().resetHistory(j)
self.samples_full = [emptySampleList() for _ in range(j)]
self.RPOD = [np.zeros((0, 0), dtype = np.complex) for _ in range(j)]
def resetHistoryCoalesced(self):
super().resetHistoryCoalesced()
self.RPODCoalesced = np.zeros((0, 0), dtype = np.complex)
self.samples_fullCoalesced = emptySampleList()
def popSample(self, j:int):
if hasattr(self, "nsamples") and self.nsamples[j] > 1:
self.RPOD[j] = self.RPOD[j][: -1, : -1]
self.samples_full[j].pop()
super().popSample(j)
def coalesceSamples(self):
vbMng(self, "INIT", "Coalescing samples.", 7)
verb = self.verbosity
self.verbosity = 0
super().coalesceSamples()
self.verbosity = verb
self.RPODCoalesced = block_diag(*(self.RPOD))
self.samples_fullCoalesced = emptySampleList()
self.samples_fullCoalesced.reset((self.samples_full[0].shape[0],
self.nsamplesCoalesced),
self.samples_full[0].dtype)
ci = 0
for j, samp_full in enumerate(self.samples_full):
Rheg = samp_full.shape[1]
self.samples_fullCoalesced.data[:, ci : ci + Rheg] = samp_full.data
ci += Rheg
vbMng(self, "DEL", "Done coalescing samples.", 7)
def preprocesssamples(self, idxs:Np1D, j:int) -> sampList:
if self.samples_full[j] is None or len(self.samples_full[j]) == 0:
return
return self.samples_full[j](idxs)
def setsample(self, u:sampList, j:int, overwrite : bool = False):
super().setsample(u, j, overwrite)
if overwrite:
self.samples_full[j][self.nsamples[j]] = u
else:
if self.nsamples[j] == 0:
self.samples_full[j] = sampleList(u)
else:
self.samples_full[j].append(u)
def postprocessu(self, u:sampList, j:int, overwrite : bool = False):
if overwrite:
self.samples_full[j][self.nsamples[j]] = u
else:
if self.nsamples[j] == 0:
self.samples_full[j] = sampleList(u)
else:
self.samples_full[j].append(u)
vbMng(self, "INIT", "Starting orthogonalization.", 20)
u, r, _ = self.PODEngine.GS(u, self.samples[j],
is_state = self.sample_state)
self.RPOD[j] = np.pad(self.RPOD[j], ((0, 1), (0, 1)), 'constant')
self.RPOD[j][:, -1] = r
vbMng(self, "DEL", "Done orthogonalizing.", 20)
super().setsample(u, j, overwrite)
def postprocessuBulk(self, j:int):
vbMng(self, "INIT",
"Starting orthogonalization for marginal no {}.".format(j), 40)
u, self.RPOD[j] = self.PODEngine.generalizedQR(self.samples_full[j],
is_state = self.sample_state)
vbMng(self, "DEL", "Done orthogonalizing.", 40)
self.samples[j] = sampleList(u)
def preallocateSamples(self, u:Np1D, mu:paramVal, n:int, j:int):
super().preallocateSamples(u, mu, n, j)
self.samples_full[j].reset((u.shape[0], n), u.dtype)
self.samples_full[j][0] = u

Event Timeline