Skip to content

Commit

Permalink
Merge pull request #11545 from KratosMultiphysics/Kratos_LSPG_HROM
Browse files Browse the repository at this point in the history
Kratos LSPG update.
  • Loading branch information
SADPR authored Sep 14, 2023
2 parents a4592db + d3857d5 commit 07717fc
Show file tree
Hide file tree
Showing 35 changed files with 326 additions and 632 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
"projection_strategy": "galerkin",
"assembling_strategy": "global",
"rom_format": "json",
"train_petrov_galerkin": {
"train": false,
"basis_strategy": "residuals",
"include_phi": false,
"svd_truncation_tolerance": 1e-06
},
"rom_settings": {
"nodal_unknowns": [
"TEMPERATURE"
],
"number_of_rom_dofs": 1,
"petrov_galerkin_number_of_rom_dofs": 0
"petrov_galerkin_number_of_rom_dofs": 0,
"rom_bns_settings": {}
},
"hrom_settings": {
"hrom_format": "json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@
"projection_strategy": "galerkin",
"assembling_strategy": "global",
"rom_format": "json",
"train_petrov_galerkin": {
"train": false,
"basis_strategy": "residuals",
"include_phi": false,
"svd_truncation_tolerance": 1e-06
},
"rom_settings": {
"nodal_unknowns": [
"TEMPERATURE"
],
"number_of_rom_dofs": 1,
"petrov_galerkin_number_of_rom_dofs": 0
"petrov_galerkin_number_of_rom_dofs": 0,
"rom_bns_settings": {}
},
"hrom_settings": {
"hrom_format": "json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ void AddCustomStrategiesToPython(pybind11::module& m)
py::class_<ROMBuilderAndSolverType, typename ROMBuilderAndSolverType::Pointer, BuilderAndSolverType>(m, "ROMBuilderAndSolver")
.def(py::init< LinearSolverType::Pointer, Parameters>() )
;

typedef LeastSquaresPetrovGalerkinROMBuilderAndSolver<SparseSpaceType, LocalSpaceType, LinearSolverType> LeastSquaresPetrovGalerkinROMBuilderAndSolverType;

py::class_<LeastSquaresPetrovGalerkinROMBuilderAndSolverType, typename LeastSquaresPetrovGalerkinROMBuilderAndSolverType::Pointer, ROMBuilderAndSolverType, BuilderAndSolverType>(m, "LeastSquaresPetrovGalerkinROMBuilderAndSolver")
.def(py::init< LinearSolverType::Pointer, Parameters>() )
;

typedef PetrovGalerkinROMBuilderAndSolver<SparseSpaceType, LocalSpaceType, LinearSolverType> PetrovGalerkinROMBuilderAndSolverType;

Expand All @@ -78,6 +72,15 @@ void AddCustomStrategiesToPython(pybind11::module& m)
.def(py::init< LinearSolverType::Pointer, Parameters>() )
;

typedef LeastSquaresPetrovGalerkinROMBuilderAndSolver<SparseSpaceType, LocalSpaceType, LinearSolverType> LeastSquaresPetrovGalerkinROMBuilderAndSolverType;

py::class_<LeastSquaresPetrovGalerkinROMBuilderAndSolverType, typename LeastSquaresPetrovGalerkinROMBuilderAndSolverType::Pointer, GlobalROMBuilderAndSolverType>(m, "LeastSquaresPetrovGalerkinROMBuilderAndSolver")
.def(py::init< LinearSolverType::Pointer, Parameters>() )
.def("BuildAndApplyDirichletConditions", &LeastSquaresPetrovGalerkinROMBuilderAndSolverType::BuildAndApplyDirichletConditions)
.def("GetRightROMBasis", &LeastSquaresPetrovGalerkinROMBuilderAndSolverType::GetRightROMBasis)
;


}

} // namespace Python.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ void AddCustomUtilitiesToPython(pybind11::module& m)
.def(init<ModelPart&, Parameters, BaseSchemeType::Pointer>()) //
.def("GetProjectedResidualsOntoPhi",&RomResidualsUtility::GetProjectedResidualsOntoPhi) //
.def("GetProjectedResidualsOntoPsi",&RomResidualsUtility::GetProjectedResidualsOntoPsi) //
.def("GetProjectedGlobalLHS", &RomResidualsUtility::GetProjectedGlobalLHS)//
;

class_<RomAuxiliaryUtilities>(m, "RomAuxiliaryUtilities")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa
return mNumberOfRomModes;
}

bool GetMonotonicityPreservingFlag() const noexcept
{
return mMonotonicityPreservingFlag;
}

void ProjectToFineBasis(
const TSystemVectorType& rRomUnkowns,
const ModelPart& rModelPart,
Expand All @@ -251,7 +256,8 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa
}

void BuildRightROMBasis(
const ModelPart& rModelPart)
const ModelPart& rModelPart,
Matrix& rPhiGlobal)
{
const auto& r_dof_set = BaseBuilderAndSolverType::GetDofSet();
block_for_each(r_dof_set, [&](const DofType& r_dof)
Expand All @@ -261,10 +267,10 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa
const Matrix::size_type row_id = mMapPhi.at(r_dof.GetVariable().Key());
if (r_dof.IsFixed())
{
noalias(row(mPhiGlobal, r_dof.EquationId())) = ZeroVector(r_rom_nodal_basis.size2());
noalias(row(rPhiGlobal, r_dof.EquationId())) = ZeroVector(r_rom_nodal_basis.size2());
}
else{
noalias(row(mPhiGlobal, r_dof.EquationId())) = row(r_rom_nodal_basis, row_id);
noalias(row(rPhiGlobal, r_dof.EquationId())) = row(r_rom_nodal_basis, row_id);
}
});
}
Expand Down Expand Up @@ -340,7 +346,7 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa

BuildAndProjectROM(pScheme, rModelPart, A, b, Dx);

SolveROM(rModelPart, A, b, Dx);
SolveROM(rModelPart, mEigenRomA, mEigenRomB, Dx);

KRATOS_CATCH("")
}
Expand Down Expand Up @@ -388,7 +394,9 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa
"name" : "global_rom_builder_and_solver",
"nodal_unknowns" : [],
"number_of_rom_dofs" : 10,
"monotonicity_preserving": false
"rom_bns_settings" : {
"monotonicity_preserving": false
}
})");
default_parameters.AddMissingParameters(BaseBuilderAndSolverType::GetDefaultParameters());

Expand Down Expand Up @@ -446,13 +454,10 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa

SizeType mNodalDofs;
std::unordered_map<Kratos::VariableData::KeyType, Matrix::size_type> mMapPhi;

ElementsArrayType mSelectedElements;
ConditionsArrayType mSelectedConditions;

bool mHromSimulation = false;
bool mHromWeightsInitialized = false;

bool mRightRomBasisInitialized = false;

///@}
Expand All @@ -470,7 +475,7 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa
// Set member variables
mNodalDofs = ThisParameters["nodal_unknowns"].size();
mNumberOfRomModes = ThisParameters["number_of_rom_dofs"].GetInt();
mMonotonicityPreservingFlag = ThisParameters["monotonicity_preserving"].GetBool();
mMonotonicityPreservingFlag = ThisParameters["rom_bns_settings"]["monotonicity_preserving"].GetBool();

// Set up a map with key the variable key and value the correct row in ROM basis
IndexType k = 0;
Expand Down Expand Up @@ -753,7 +758,7 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa
mRightRomBasisInitialized = true;
}

BuildRightROMBasis(rModelPart);
BuildRightROMBasis(rModelPart, mPhiGlobal);

auto a_wrapper = UblasWrapper<double>(rA);
const auto& eigen_rA = a_wrapper.matrix();
Expand All @@ -774,8 +779,8 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa
*/
virtual void SolveROM(
ModelPart &rModelPart,
TSystemMatrixType &rA,
TSystemVectorType &rb,
EigenDynamicMatrix &rEigenRomA,
EigenDynamicVector &rEigenRomB,
TSystemVectorType &rDx)
{
KRATOS_TRY
Expand All @@ -786,7 +791,7 @@ class GlobalROMBuilderAndSolver : public ResidualBasedBlockBuilderAndSolver<TSpa

using EigenDynamicVector = Eigen::Matrix<double, Eigen::Dynamic, 1>;
Eigen::Map<EigenDynamicVector> dxrom_eigen(dxrom.data().begin(), dxrom.size());
dxrom_eigen = mEigenRomA.colPivHouseholderQr().solve(mEigenRomB);
dxrom_eigen = rEigenRomA.colPivHouseholderQr().solve(rEigenRomB);

double time = solving_timer.ElapsedSeconds();
KRATOS_INFO_IF("GlobalROMBuilderAndSolver", (this->GetEchoLevel() > 0)) << "Solve reduced system time: " << time << std::endl;
Expand Down
Loading

0 comments on commit 07717fc

Please sign in to comment.