Skip to content

Commit

Permalink
Merge pull request #12271 from KratosMultiphysics/RomApp_SplitBasisGe…
Browse files Browse the repository at this point in the history
…nerationProcess

[RomApp] Separate Basis Generation and File Writing Processes
  • Loading branch information
SADPR authored Apr 16, 2024
2 parents 2937f5c + f1ee84d commit 5571927
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,15 @@ def _GetSnapshotsMatrix(self):
return numpy.block(self.snapshots_data_list)


def _PrintRomBasis(self, snapshots_matrix):
def _ComputeSVD(self, snapshots_matrix):

# Calculate the randomized SVD of the snapshots matrix
u,_,_,_= RandomizedSingularValueDecomposition().Calculate(snapshots_matrix, self.svd_truncation_tolerance)

return u


def _PrintRomBasis(self, u):
# Initialize the Python dictionary with the default settings
# Note that this order is kept if Python 3.6 onwards is used
rom_basis_dict = {
Expand All @@ -154,9 +162,6 @@ def _PrintRomBasis(self, snapshots_matrix):
rom_basis_dict["hrom_settings"]["hrom_format"] = self.rom_basis_output_format
n_nodal_unknowns = len(self.snapshot_variables_list)

# Calculate the randomized SVD of the snapshots matrix
u,_,_,_= RandomizedSingularValueDecomposition().Calculate(snapshots_matrix, self.svd_truncation_tolerance)

# Save the nodal basis
rom_basis_dict["rom_settings"]["nodal_unknowns"] = [var.Name() for var in self.snapshot_variables_list]
rom_basis_dict["rom_settings"]["number_of_rom_dofs"] = numpy.shape(u)[1] #TODO: This is way misleading. I'd call it number_of_basis_modes or number_of_rom_modes
Expand Down Expand Up @@ -203,7 +208,8 @@ def ExecuteFinalize(self):
self.n_nodal_unknowns = len(self.snapshot_variables_list)

if not self.rom_manager:
self._PrintRomBasis(self._GetSnapshotsMatrix())
u = self._ComputeSVD(self._GetSnapshotsMatrix())
self._PrintRomBasis(u)

def __GetPrettyFloat(self, number):
float_format = "{:.12f}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def GetJacobianPhiMultiplication(self, computing_model_part):

def CalculateAndSaveBasis(self, snapshots_matrix = None):
# Calculate the new basis and save
snapshots_basis = self.__CalculateResidualBasis(snapshots_matrix)
self.__AppendNewBasisToRomParameters(snapshots_basis)
snapshots_basis = self._CalculateResidualBasis(snapshots_matrix)
self._AppendNewBasisToRomParameters(snapshots_basis)


@classmethod
Expand All @@ -118,7 +118,7 @@ def __GetPetrovGalerkinTrainingDefaultSettings(cls):
}""")
return default_settings

def __CalculateResidualBasis(self, snapshots_matrix):
def _CalculateResidualBasis(self, snapshots_matrix):
if snapshots_matrix is None:
snapshots_matrix = self._GetSnapshotsMatrix()
u_left,s_left,_,_ = RandomizedSingularValueDecomposition(COMPUTE_V=False).Calculate(
Expand All @@ -135,7 +135,7 @@ def __CalculateResidualBasis(self, snapshots_matrix):

return u

def __AppendNewBasisToRomParameters(self, u):
def _AppendNewBasisToRomParameters(self, u):
petrov_galerkin_number_of_rom_dofs= np.shape(u)[1]
n_nodal_unknowns = len(self.rom_settings["nodal_unknowns"].GetStringArray())
petrov_galerkin_nodal_modes = {}
Expand All @@ -161,11 +161,6 @@ def __AppendNewBasisToRomParameters(self, u):

if self.echo_level > 0 : KratosMultiphysics.Logger.PrintInfo("PetrovGalerkinTrainingUtility","\'RomParameters.json\' file updated with HROM weights.")

def __GetPrettyFloat(self, number):
float_format = "{:.12f}"
pretty_number = float(float_format.format(number))
return pretty_number

def __GetGalerkinBasis(self):
if self.rom_format == "json":
with open(self.rom_basis_output_folder / self.rom_basis_output_name.with_suffix(".json"), 'r') as f:
Expand Down Expand Up @@ -195,10 +190,3 @@ def _GetSnapshotsMatrix(self):
snapshots_matrix = np.c_[snapshots_matrix,self.time_step_snapshots_matrix_container[0]]
return snapshots_matrix


@classmethod
def __OrthogonalProjector(self, A, B):
# A - B @(B.T @ A)
BtA = B.T@A
A -= B @ BtA
return A
3 changes: 2 additions & 1 deletion applications/RomApplication/python_scripts/rom_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ def __LaunchTrainROM(self, mu_train):
BasisOutputProcess = process
SnapshotsMatrix.append(BasisOutputProcess._GetSnapshotsMatrix()) #TODO add a CustomMethod() as a standard method in the Analysis Stage to retrive some solution
SnapshotsMatrix = np.block(SnapshotsMatrix)
BasisOutputProcess._PrintRomBasis(SnapshotsMatrix) #Calling the RomOutput Process for creating the RomParameter.json
u = BasisOutputProcess._ComputeSVD(SnapshotsMatrix)
BasisOutputProcess._PrintRomBasis(u) #Calling the RomOutput Process for creating the RomParameter.json

return SnapshotsMatrix

Expand Down

0 comments on commit 5571927

Please sign in to comment.