// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_MATLABSMOOTHER_DEF_HPP #define MUELU_MATLABSMOOTHER_DEF_HPP #include "MueLu_MatlabSmoother_decl.hpp" #include "MueLu_MatlabUtils_decl.hpp" #if defined(HAVE_MUELU_MATLAB) #include "MueLu_Monitor.hpp" namespace MueLu { template MatlabSmoother::MatlabSmoother(const Teuchos::ParameterList& paramList) { SetParameterList(paramList); } template void MatlabSmoother::SetParameterList(const Teuchos::ParameterList& paramList) { Factory::SetParameterList(paramList); ParameterList& pL = const_cast(this->GetParameterList()); setupFunction_ = pL.get("Setup Function", ""); solveFunction_ = pL.get("Solve Function", ""); solveDataSize_ = pL.get("Number of Solver Args", 0); } template void MatlabSmoother::DeclareInput(Level& currentLevel) const { using namespace std; this->Input(currentLevel, "A"); ParameterList& pL = const_cast(this->GetParameterList()); needsSetup_ = pL.get("Needs"); vector needsList = tokenizeList(needsSetup_); for (size_t i = 0; i < needsList.size(); i++) { if (!IsParamMuemexVariable(needsList[i]) && needsList[i] != "Level") this->Input(currentLevel, needsList[i]); } } template void MatlabSmoother::Setup(Level& currentLevel) { using namespace std; FactoryMonitor m(*this, "Setup Smoother", currentLevel); if (this->IsSetup() == true) this->GetOStream(Warnings0) << "MueLu::MatlabSmoother::Setup(): Setup() has already been called"; vector> InputArgs = processNeeds(this, needsSetup_, currentLevel); A_ = Factory::Get>(currentLevel, "A"); RCP AmatArg = rcp_implicit_cast(rcp(new MuemexData>(A_))); // Always add A to the beginning of InputArgs InputArgs.insert(InputArgs.begin(), AmatArg); // Call mex function if (!setupFunction_.length()) throw runtime_error("Invalid matlab function name"); solveData_ = callMatlab(setupFunction_, solveDataSize_, InputArgs); this->GetOStream(Statistics1) << description() << endl; this->IsSetup(true); // mark the smoother as set up } template void MatlabSmoother::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const { TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::MatlabSmoother::Apply(): Setup() has not been called"); using namespace Teuchos; using namespace std; if (InitialGuessIsZero) X.putScalar(0.0); // Push on A as first input vector> InputArgs; InputArgs.push_back(rcp(new MuemexData>(A_))); // Push on LHS & RHS RCP Xrcp(&X, false); MultiVector* BPtrNonConst = (MultiVector*)&B; RCP Brcp = rcp(BPtrNonConst, false); RCP>> XData = rcp(new MuemexData>(Xrcp)); RCP>> BData = rcp(new MuemexData>(Brcp)); InputArgs.push_back(XData); InputArgs.push_back(BData); for (size_t i = 0; i < solveData_.size(); i++) InputArgs.push_back(solveData_[i]); if (!solveFunction_.length()) throw std::runtime_error("Invalid matlab function name"); vector> mexOutput = callMatlab(solveFunction_, 1, InputArgs); RCP>> mydata = Teuchos::rcp_static_cast>>(mexOutput[0]); X = *(mydata->getData()); } template RCP> MatlabSmoother::Copy() const { RCP smoother = rcp(new MatlabSmoother(*this)); smoother->SetParameterList(this->GetParameterList()); return smoother; } template std::string MatlabSmoother::description() const { std::ostringstream out; if (SmootherPrototype::IsSetup()) { out << "Matlab Smoother(" << setupFunction_ << "/" << solveFunction_ << ")"; } else { out << SmootherPrototype::description(); } return out.str(); } template void MatlabSmoother::print(Teuchos::FancyOStream& out, const VerbLevel verbLevel) const { MUELU_DESCRIBE; if (verbLevel & Parameters0) out << "Matlab Smoother(" << setupFunction_ << "/" << solveFunction_ << ")"; if (verbLevel & Parameters1) { out0 << "Parameter list: " << std::endl; Teuchos::OSTab tab2(out); out << this->GetParameterList(); } if (verbLevel & Debug) { out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl; } } // Dummy specializations for GO = long long /*template <> void MatlabSmoother::Setup(Level& currentLevel) { throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long."); } template <> void MatlabSmoother,int,long long>::Setup(Level& currentLevel) { throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long."); } template <> void MatlabSmoother::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const { throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long."); } template <> void MatlabSmoother,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const { throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long."); }*/ } // namespace MueLu #endif // HAVE_MUELU_MATLAB #endif // MUELU_MATLABSMOOTHER_DEF_HPP