// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_IOHELPERS_HPP #define MUELU_IOHELPERS_HPP #include #include #include namespace MueLu { using Teuchos::RCP; using Teuchos::rcp; using Teuchos::rcp_dynamic_cast; template struct IOhelpers { static Teuchos::RCP > Read(const std::string& filename, const RCP > rowMap, RCP > colMap, const RCP > domainMap = Teuchos::null, const RCP > rangeMap = Teuchos::null, const bool callFillComplete = true, const bool binary = false, const bool readLocal = false) { using IO = Xpetra::IO; Teuchos::RCP > A; if (readLocal) A = IO::ReadLocal(filename, rowMap, colMap, domainMap, rangeMap, callFillComplete, binary); else A = IO::Read(filename, rowMap, colMap, domainMap, rangeMap, callFillComplete, binary); return A; } static Teuchos::RCP > Read(std::string& filename, RCP >& comm) { Teuchos::ParameterList hierarchicalParams; Teuchos::updateParametersFromXmlFileAndBroadcast(filename, Teuchos::Ptr(&hierarchicalParams), *comm); auto op = Read(hierarchicalParams, comm); return op; } static Teuchos::RCP > Read(Teuchos::ParameterList& hierarchicalParams, RCP >& comm) { using HOp = Xpetra::HierarchicalOperator; using blocked_matrix_type = typename HOp::blocked_matrix_type; using blocked_map_type = typename blocked_matrix_type::blocked_map_type; using matrix_type = typename HOp::matrix_type; using map_type = typename HOp::map_type; using lo_vec_type = typename blocked_map_type::lo_vec_type; auto lib = Xpetra::UseTpetra; RCP op; RCP map, near_colmap, clusterCoeffMap, ghosted_clusterCoeffMap, clusterMap, ghosted_clusterMap; RCP nearField, basisMatrix, kernelApproximations, kernelBlockGraph; std::vector > transferMatrices; RCP clusterSizes; RCP blockedClusterMap, ghosted_blockedClusterMap; RCP blockKernelApproximations; const bool readBinary = hierarchicalParams.get("read binary", false); const bool readLocal = hierarchicalParams.get("read local", false); using IO = Xpetra::IO; // row, domain and range map of the operator map = IO::ReadMap(hierarchicalParams.get("map"), lib, comm, readBinary); // colmap of near field near_colmap = IO::ReadMap(hierarchicalParams.get("near colmap"), lib, comm, readBinary); if (hierarchicalParams.isType("coefficient map")) { // 1-to-1 map for the cluster coefficients clusterCoeffMap = IO::ReadMap(hierarchicalParams.get("coefficient map"), lib, comm, readBinary); // overlapping map for the cluster coefficients ghosted_clusterCoeffMap = IO::ReadMap(hierarchicalParams.get("ghosted coefficient map"), lib, comm, readBinary); // 1-to-1 map for the clusters clusterMap = IO::ReadMap(hierarchicalParams.get("cluster map"), lib, comm, readBinary); // overlapping map for the clusters ghosted_clusterMap = IO::ReadMap(hierarchicalParams.get("ghosted cluster map"), lib, comm, readBinary); // blocked cluster map clusterSizes = Xpetra::IO::ReadMultiVectorLO(hierarchicalParams.get("gid_cluster_to_gid_coeff"), clusterMap)->getVectorNonConst(0); blockedClusterMap = rcp(new blocked_map_type(clusterCoeffMap, clusterSizes)); } // near field interactions nearField = Read(hierarchicalParams.get("near field matrix"), map, near_colmap, map, map, true, readBinary, readLocal); if (hierarchicalParams.isType("coefficient map")) { // far field basis expansion coefficients basisMatrix = IOhelpers::Read(hierarchicalParams.get("basis expansion coefficient matrix"), map, clusterCoeffMap, clusterCoeffMap, map, true, readBinary, readLocal); // far field interactions kernelApproximations = IOhelpers::Read(hierarchicalParams.get("far field interaction matrix"), clusterCoeffMap, ghosted_clusterCoeffMap, clusterCoeffMap, clusterCoeffMap, true, readBinary, readLocal); // block graph of far field interactions kernelBlockGraph = IOhelpers::Read(hierarchicalParams.get("far field interaction matrix") + ".block", clusterMap, ghosted_clusterMap, clusterMap, clusterMap, true, readBinary, readLocal); { auto import = kernelBlockGraph->getCrsGraph()->getImporter(); RCP ghosted_clusterSizes = Xpetra::VectorFactory::Build(ghosted_clusterMap); ghosted_clusterSizes->doImport(*clusterSizes, *import, Xpetra::INSERT); ghosted_blockedClusterMap = rcp(new blocked_map_type(ghosted_clusterCoeffMap, ghosted_clusterSizes)); } blockKernelApproximations = rcp(new blocked_matrix_type(kernelApproximations, kernelBlockGraph, blockedClusterMap, ghosted_blockedClusterMap)); // Transfer matrices auto transfersList = hierarchicalParams.sublist("shift coefficient matrices"); for (int i = 0; i < transfersList.numParams(); i++) { std::string filename = transfersList.get(std::to_string(i)); auto transferPoint = IOhelpers::Read(filename, clusterCoeffMap, clusterCoeffMap, clusterCoeffMap, clusterCoeffMap, true, readBinary, readLocal); auto transferBlock = IOhelpers::Read(filename + ".block", clusterMap, clusterMap, clusterMap, clusterMap, true, readBinary, readLocal); auto transfer = rcp(new blocked_matrix_type(transferPoint, transferBlock, blockedClusterMap)); transferMatrices.push_back(transfer); } } RCP params; if (hierarchicalParams.isSublist("params")) { params = rcp(new Teuchos::ParameterList(hierarchicalParams.sublist("params"))); } if (hierarchicalParams.isType("coefficient map")) { op = rcp(new HOp(nearField, blockKernelApproximations, basisMatrix, transferMatrices, params)); return op; } else return nearField; } }; } // namespace MueLu #endif