From e0a05c9901d6cdf5f4c60f0387b1c415223de22b Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 6 May 2024 15:06:36 +0200 Subject: [PATCH 1/4] Defining list into space --- .../TrilinosApplication/trilinos_space.h | 22 +++++++++++++++++++ kratos/spaces/ublas_space.h | 16 ++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/applications/TrilinosApplication/trilinos_space.h b/applications/TrilinosApplication/trilinos_space.h index 983284afb8ce..cfc3120c13fc 100644 --- a/applications/TrilinosApplication/trilinos_space.h +++ b/applications/TrilinosApplication/trilinos_space.h @@ -862,6 +862,28 @@ class TrilinosSpace return true; } + /** + * @brief Returns a list of the fastest direct solvers. + * @details This function returns a vector of strings representing the names of the fastest direct solvers. The order of the solvers in the list may need to be updated and reordered depending on the size of the equation system. + * @return A vector of strings containing the names of the fastest direct solvers. + */ + inline static std::vector FastestDirectSolverList() + { + // May need to be updated and reordered. In fact I think it depends of the size of the equation system + std::vector faster_direct_solvers({ + "mumps2", // Amesos2 (if compiled with MUMPS-support) + "mumps", // Amesos (if compiled with MUMPS-support) + "super_lu_dist2", // Amesos2 SuperLUDist (if compiled with MPI-support) + "super_lu_dist", // Amesos SuperLUDist (if compiled with MPI-support) + "amesos2", // Amesos2 + "amesos", // Amesos + "klu2", // Amesos2 KLU + "klu", // Amesos KLU + "basker" // Amesos2 Basker + }); + return faster_direct_solvers; + } + /** * @brief This function returns a value from a given vector according to a given index * @param rX The vector from which values are to be gathered diff --git a/kratos/spaces/ublas_space.h b/kratos/spaces/ublas_space.h index 558681879d1e..3a95c15054ad 100644 --- a/kratos/spaces/ublas_space.h +++ b/kratos/spaces/ublas_space.h @@ -891,6 +891,22 @@ class UblasSpace return false; } + /** + * @brief Returns a list of the fastest direct solvers. + * @details This function returns a vector of strings representing the names of the fastest direct solvers. The order of the solvers in the list may need to be updated and reordered depending on the size of the equation system. + * @return A vector of strings containing the names of the fastest direct solvers. + */ + inline static std::vector FastestDirectSolverList() + { + std::vector faster_direct_solvers({ + "pardiso_lu", // LinearSolversApplication (if compiled with Intel-support) + "pardiso_ldlt", // LinearSolversApplication (if compiled with Intel-support) + "sparse_lu", // LinearSolversApplication + "skyline_lu_factorization" // In Core, always available, but slow + }); + return faster_direct_solvers; + } + //*********************************************************************** inline static TDataType GetValue(const VectorType& x, std::size_t I) From b6959d0805111587a37a38f3fc467832f08a1a59 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 6 May 2024 17:26:42 +0200 Subject: [PATCH 2/4] Add to python --- .../add_trilinos_space_to_python.cpp | 2 + kratos/python/add_space_to_python.cpp | 50 +++++++++++++++++++ kratos/python/add_space_to_python.h | 27 ++++++++++ .../python/add_sparse_matrices_to_python.cpp | 1 - kratos/python/kratos_python.cpp | 2 + 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 kratos/python/add_space_to_python.cpp create mode 100644 kratos/python/add_space_to_python.h diff --git a/applications/TrilinosApplication/custom_python/add_trilinos_space_to_python.cpp b/applications/TrilinosApplication/custom_python/add_trilinos_space_to_python.cpp index f058b711b290..a8280e1c0e72 100644 --- a/applications/TrilinosApplication/custom_python/add_trilinos_space_to_python.cpp +++ b/applications/TrilinosApplication/custom_python/add_trilinos_space_to_python.cpp @@ -313,6 +313,8 @@ void AddBasicOperations(pybind11::module& m) .def("SetValue", SetValue) //.def("GetValue", GetValue) //deliberately commented out. Only works for local Ids .def("GatherValues", GatherValues) + .def_static("IsDistributed", &TrilinosSparseSpaceType::IsDistributed) + .def_static("FastestDirectSolverList", &TrilinosSparseSpaceType::FastestDirectSolverList) ; m.def("CreateCommunicator", CreateCommunicator); diff --git a/kratos/python/add_space_to_python.cpp b/kratos/python/add_space_to_python.cpp new file mode 100644 index 000000000000..94b80544fc0d --- /dev/null +++ b/kratos/python/add_space_to_python.cpp @@ -0,0 +1,50 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +// System includes + +// External includes + +// Project includes +#include "includes/define_python.h" +#include "python/add_space_to_python.h" +#include "spaces/ublas_space.h" + +namespace Kratos::Python +{ +void AddSpaceToPython(pybind11::module& m) +{ + namespace py = pybind11; + + using SparseSpaceType = UblasSpace>; + + py::class_(m,"SparseSpace") + .def(py::init<>()) + .def_static("Size", &SparseSpaceType::Size) + .def_static("Size1", &SparseSpaceType::Size1) + .def_static("Size2", &SparseSpaceType::Size2) + .def_static("IsDistributed", &SparseSpaceType::IsDistributed) + .def_static("FastestDirectSolverList", &SparseSpaceType::FastestDirectSolverList) + ; + + using LocalSpaceType = UblasSpace; + + py::class_(m,"LocalSpace") + .def(py::init<>()) + .def_static("Size", &LocalSpaceType::Size) + .def_static("Size1", &LocalSpaceType::Size1) + .def_static("Size2", &LocalSpaceType::Size2) + .def_static("IsDistributed", &LocalSpaceType::IsDistributed) + .def_static("FastestDirectSolverList", &LocalSpaceType::FastestDirectSolverList) + ; +} +} // namespace Kratos::Python. \ No newline at end of file diff --git a/kratos/python/add_space_to_python.h b/kratos/python/add_space_to_python.h new file mode 100644 index 000000000000..f13da6e87fc0 --- /dev/null +++ b/kratos/python/add_space_to_python.h @@ -0,0 +1,27 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +#pragma once + +// System includes + +// External includes +#include + +// Project includes + +namespace Kratos::Python +{ + +void AddSpaceToPython(pybind11::module& m); + +} // namespace Kratos::Python. diff --git a/kratos/python/add_sparse_matrices_to_python.cpp b/kratos/python/add_sparse_matrices_to_python.cpp index db7af8d80847..7159e4c7d93a 100644 --- a/kratos/python/add_sparse_matrices_to_python.cpp +++ b/kratos/python/add_sparse_matrices_to_python.cpp @@ -1,4 +1,3 @@ - // | / | // ' / __| _` | __| _ \ __| // . \ | ( | | ( |\__ ` diff --git a/kratos/python/kratos_python.cpp b/kratos/python/kratos_python.cpp index bc02d503c267..542d60604e99 100644 --- a/kratos/python/kratos_python.cpp +++ b/kratos/python/kratos_python.cpp @@ -77,6 +77,7 @@ #include "add_accessors_to_python.h" #include "add_globals_to_python.h" #include "add_geometry_data_to_python.h" +#include "add_space_to_python.h" namespace Kratos::Python { @@ -156,6 +157,7 @@ PYBIND11_MODULE(Kratos, m) AddRegistryToPython(m); AddContainerExpressionToPython(m); AddGlobalsToPython(m); + AddSpaceToPython(m); m.def("Hello", Hello); } From 4bcc3738a179bf3aa1dc055e53cdec3646cb67ef Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 6 May 2024 17:26:51 +0200 Subject: [PATCH 3/4] Use it --- kratos/python_scripts/python_linear_solver_factory.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/kratos/python_scripts/python_linear_solver_factory.py b/kratos/python_scripts/python_linear_solver_factory.py index 6b5a83d798ea..86f5deed8530 100644 --- a/kratos/python_scripts/python_linear_solver_factory.py +++ b/kratos/python_scripts/python_linear_solver_factory.py @@ -2,7 +2,6 @@ from KratosMultiphysics import kratos_utilities as kratos_utils from importlib import import_module - def ConstructSolver(configuration): if(type(configuration) != KM.Parameters): raise Exception("input is expected to be provided as a Kratos Parameters object") @@ -26,11 +25,7 @@ def CreateFastestAvailableDirectLinearSolver(): if kratos_utils.CheckIfApplicationsAvailable("LinearSolversApplication"): from KratosMultiphysics import LinearSolversApplication - linear_solvers_by_speed = [ - "pardiso_lu", # LinearSolversApplication (if compiled with Intel-support) - "sparse_lu", # LinearSolversApplication - "skyline_lu_factorization" # in Core, always available, but slow - ] + linear_solvers_by_speed = KM.SparseSpace.FastestDirectSolverList() for solver_name in linear_solvers_by_speed: if KM.LinearSolverFactory().Has(solver_name): @@ -41,7 +36,6 @@ def CreateFastestAvailableDirectLinearSolver(): raise Exception("Linear-Solver could not be constructed!") - def __GetSolverTypeAndImportApplication(solver_type): # remove unused "KratosMultiphysics. if solver_type.startswith("KratosMultiphysics."): @@ -57,5 +51,4 @@ def __GetSolverTypeAndImportApplication(solver_type): solver_type = splitted_name[1] import_module("KratosMultiphysics." + app_name) - return solver_type - + return solver_type \ No newline at end of file From cecfd566c80db794e00dad8304adf4907791cf71 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 7 May 2024 08:40:46 +0200 Subject: [PATCH 4/4] Refactor --- kratos/python/add_space_to_python.cpp | 50 ------------------- kratos/python/add_space_to_python.h | 27 ---------- kratos/python/add_strategies_to_python.cpp | 9 +++- kratos/python/kratos_python.cpp | 2 - .../python_linear_solver_factory.py | 2 +- 5 files changed, 9 insertions(+), 81 deletions(-) delete mode 100644 kratos/python/add_space_to_python.cpp delete mode 100644 kratos/python/add_space_to_python.h diff --git a/kratos/python/add_space_to_python.cpp b/kratos/python/add_space_to_python.cpp deleted file mode 100644 index 94b80544fc0d..000000000000 --- a/kratos/python/add_space_to_python.cpp +++ /dev/null @@ -1,50 +0,0 @@ -// | / | -// ' / __| _` | __| _ \ __| -// . \ | ( | | ( |\__ ` -// _|\_\_| \__,_|\__|\___/ ____/ -// Multi-Physics -// -// License: BSD License -// Kratos default license: kratos/license.txt -// -// Main authors: Vicente Mataix Ferrandiz -// - -// System includes - -// External includes - -// Project includes -#include "includes/define_python.h" -#include "python/add_space_to_python.h" -#include "spaces/ublas_space.h" - -namespace Kratos::Python -{ -void AddSpaceToPython(pybind11::module& m) -{ - namespace py = pybind11; - - using SparseSpaceType = UblasSpace>; - - py::class_(m,"SparseSpace") - .def(py::init<>()) - .def_static("Size", &SparseSpaceType::Size) - .def_static("Size1", &SparseSpaceType::Size1) - .def_static("Size2", &SparseSpaceType::Size2) - .def_static("IsDistributed", &SparseSpaceType::IsDistributed) - .def_static("FastestDirectSolverList", &SparseSpaceType::FastestDirectSolverList) - ; - - using LocalSpaceType = UblasSpace; - - py::class_(m,"LocalSpace") - .def(py::init<>()) - .def_static("Size", &LocalSpaceType::Size) - .def_static("Size1", &LocalSpaceType::Size1) - .def_static("Size2", &LocalSpaceType::Size2) - .def_static("IsDistributed", &LocalSpaceType::IsDistributed) - .def_static("FastestDirectSolverList", &LocalSpaceType::FastestDirectSolverList) - ; -} -} // namespace Kratos::Python. \ No newline at end of file diff --git a/kratos/python/add_space_to_python.h b/kratos/python/add_space_to_python.h deleted file mode 100644 index f13da6e87fc0..000000000000 --- a/kratos/python/add_space_to_python.h +++ /dev/null @@ -1,27 +0,0 @@ -// | / | -// ' / __| _` | __| _ \ __| -// . \ | ( | | ( |\__ ` -// _|\_\_| \__,_|\__|\___/ ____/ -// Multi-Physics -// -// License: BSD License -// Kratos default license: kratos/license.txt -// -// Main authors: Vicente Mataix Ferrandiz -// - -#pragma once - -// System includes - -// External includes -#include - -// Project includes - -namespace Kratos::Python -{ - -void AddSpaceToPython(pybind11::module& m); - -} // namespace Kratos::Python. diff --git a/kratos/python/add_strategies_to_python.cpp b/kratos/python/add_strategies_to_python.cpp index f078fd189644..8b1f2035b47b 100644 --- a/kratos/python/add_strategies_to_python.cpp +++ b/kratos/python/add_strategies_to_python.cpp @@ -521,9 +521,16 @@ namespace Kratos:: Python auto sparse_space_binder = CreateSpaceInterface< SparseSpaceType >(m,"UblasSparseSpace"); sparse_space_binder.def("TwoNorm", TwoNorm); - //the dot product of two vectors + // The dot product of two vectors sparse_space_binder.def("Dot", Dot); sparse_space_binder.def("TransposeMult", TransposeMult); + // Size functions + sparse_space_binder.def("Size", &SparseSpaceType::Size); + sparse_space_binder.def("Size1", &SparseSpaceType::Size1); + sparse_space_binder.def("Size2", &SparseSpaceType::Size2); + // Information functions + sparse_space_binder.def("IsDistributed", &SparseSpaceType::IsDistributed); + sparse_space_binder.def("FastestDirectSolverList", &SparseSpaceType::FastestDirectSolverList); auto cplx_sparse_space_binder = CreateSpaceInterface< ComplexSparseSpaceType >(m,"UblasComplexSparseSpace"); diff --git a/kratos/python/kratos_python.cpp b/kratos/python/kratos_python.cpp index 542d60604e99..bc02d503c267 100644 --- a/kratos/python/kratos_python.cpp +++ b/kratos/python/kratos_python.cpp @@ -77,7 +77,6 @@ #include "add_accessors_to_python.h" #include "add_globals_to_python.h" #include "add_geometry_data_to_python.h" -#include "add_space_to_python.h" namespace Kratos::Python { @@ -157,7 +156,6 @@ PYBIND11_MODULE(Kratos, m) AddRegistryToPython(m); AddContainerExpressionToPython(m); AddGlobalsToPython(m); - AddSpaceToPython(m); m.def("Hello", Hello); } diff --git a/kratos/python_scripts/python_linear_solver_factory.py b/kratos/python_scripts/python_linear_solver_factory.py index 86f5deed8530..ab83f20e415c 100644 --- a/kratos/python_scripts/python_linear_solver_factory.py +++ b/kratos/python_scripts/python_linear_solver_factory.py @@ -25,7 +25,7 @@ def CreateFastestAvailableDirectLinearSolver(): if kratos_utils.CheckIfApplicationsAvailable("LinearSolversApplication"): from KratosMultiphysics import LinearSolversApplication - linear_solvers_by_speed = KM.SparseSpace.FastestDirectSolverList() + linear_solvers_by_speed = KM.UblasSparseSpace.FastestDirectSolverList() for solver_name in linear_solvers_by_speed: if KM.LinearSolverFactory().Has(solver_name):