Page MenuHomec4science

trained_model_pivoted_reduced_basis.py
No OneTemporary

File Metadata

Created
Thu, May 2, 20:09

trained_model_pivoted_reduced_basis.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 .trained_model_pivoted_general import TrainedModelPivotedGeneral
from .trained_model_reduced_basis import TrainedModelReducedBasis
from rrompy.utilities.base.types import (Np1D, ListAny, paramVal, paramList,
sampList)
from rrompy.utilities.base import verbosityManager as vbMng, freepar as fp
from rrompy.utilities.poly_fitting.polynomial import (
hashIdxToDerivative as hashI)
from rrompy.utilities.numerical import (eigvalsNonlinearDense,
marginalizePolyList)
from rrompy.utilities.exception_manager import RROMPyException
from rrompy.parameter import checkParameter, checkParameterList
from rrompy.sampling import emptySampleList, sampleList
__all__ = ['TrainedModelPivotedReducedBasis']
class TrainedModelPivotedReducedBasis(TrainedModelPivotedGeneral,
TrainedModelReducedBasis):
"""
ROM approximant evaluation for pivoted RB approximant.
Attributes:
Data: dictionary with all that can be pickled.
"""
def interpolateMarginal(self, mu : paramVal = [],
samples : ListAny = []) -> ListAny:
"""
Evaluate marginal interpolator at arbitrary marginal parameter.
Args:
mu: Target parameter.
samples: Objects to interpolate.
"""
mu = checkParameter(mu, self.data.nparMarginal)
sList = isinstance(samples[0], sampleList)
sEff = [None] * len(samples)
for j in range(len(samples)):
if sList:
sEff[j] = samples[j].data
else:
sEff[j] = samples[j]
try:
dtype = sEff[0].dtype
except:
dtype = sEff[0][0].dtype
vbMng(self, "INIT", "Interpolating marginal at mu = {}.".format(mu),
95)
muC = self.centerNormalizeMarginal(mu)
p = np.zeros_like(sEff[0], dtype = dtype)
for mIj, spj in zip(self.data.marginalInterp, sEff):
p += mIj(muC) * spj
vbMng(self, "DEL", "Done interpolating marginal.", 95)
return p
def getApproxReduced(self, mu : paramList = []) -> sampList:
"""
Evaluate reduced representation of approximant at arbitrary parameter.
Args:
mu: Target parameter.
"""
mu = checkParameterList(mu, self.data.npar)[0]
if (not hasattr(self, "lastSolvedApproxReduced")
or self.lastSolvedApproxReduced != mu):
vbMng(self, "INIT",
"Computing RB solution at mu = {}.".format(mu), 12)
self.uApproxReduced = emptySampleList()
self.uApproxReduced.reset((self.data.ARBsList[0][0].shape[0],
len(mu)), self.data.projMat.dtype)
for i, muPL in enumerate(mu):
muP = checkParameter([muPL(0, x) \
for x in self.data.directionPivot])
muM = [muPL(0, x) for x in self.data.directionMarginal]
vbMng(self, "INIT",
"Assembling reduced model for mu = {}.".format(muPL), 87)
ARBs = self.interpolateMarginal(muM, self.data.ARBsList)
bRBs = self.interpolateMarginal(muM, self.data.bRBsList)
ARBmu = ARBs[0]
bRBmu = bRBs[0]
for j in range(1, max(len(ARBs), len(bRBs))):
theta = np.prod(self.centerNormalizePivot(muP)
** hashI(j, self.data.nparPivot))
if j < len(ARBs):
ARBmu += theta * ARBs[j]
if j < len(bRBs):
bRBmu += theta * bRBs[j]
vbMng(self, "DEL", "Done assembling reduced model.", 87)
vbMng(self, "INIT",
"Solving reduced model for mu = {}.".format(muPL), 75)
self.uApproxReduced[i] = np.linalg.solve(ARBmu, bRBmu)
vbMng(self, "DEL", "Done solving reduced model.", 75)
vbMng(self, "DEL", "Done computing RB solution.", 12)
self.lastSolvedApproxReduced = mu
return self.uApproxReduced
def getPoles(self, marginalVals : ListAny = [fp], jSupp : int = 1,
**kwargs) -> Np1D:
"""
Obtain approximant poles.
Returns:
Numpy complex vector of poles.
"""
if not hasattr(marginalVals, "__len__"): marginalVals = [marginalVals]
marginalVals = list(marginalVals)
try:
rDim = marginalVals.index(fp)
if rDim < len(marginalVals) - 1 and fp in marginalVals[rDim + 1 :]:
raise
except:
raise RROMPyException(("Exactly 1 'freepar' entry in "
"marginalVals must be provided."))
if rDim in self.data.directionMarginal:
raise RROMPyException(("'freepar' entry in marginalVals must be "
"in pivot dimension."))
mValsP = [marginalVals[x] for x in self.data.directionPivot]
rDimP = mValsP.index(fp)
mValsM = [marginalVals[x] for x in self.data.directionMarginal]
ARBs = self.interpolateMarginal(mValsM, self.data.ARBsList)
if self.data.nparPivot > 1:
mValsP[rDimP] = self.data.mu0Pivot(rDimP)
mValsP = self.centerNormalizePivot(mValsP)
mValsP = mValsP.data.flatten()
mValsP[rDimP] = fp
ARBs = marginalizePolyList(ARBs, mValsP, "auto")
ev = eigvalsNonlinearDense(ARBs, jSupp = jSupp, **kwargs)
return np.power(self.data.mu0(rDim) ** self.data.rescalingExp[rDim]
+ ev * self.data.scaleFactor[rDim],
1. / self.data.rescalingExp[rDim])

Event Timeline