// ============================================================================ // == == // == AMDiS - Adaptive multidimensional simulations == // == == // == http://www.amdis-fem.org == // == == // ============================================================================ // // Software License for AMDiS // // Copyright (c) 2010 Dresden University of Technology // All rights reserved. // Authors: Simon Vey, Thomas Witkowski et al. // // This file is part of AMDiS // // See also license.opensource.txt in the distribution. /** \file NonLinSolver.h */ #ifndef AMDIS_NONLINSOLVER_H #define AMDIS_NONLINSOLVER_H #include #include "Global.h" #include "CreatorInterface.h" #include "MatrixVector.h" #include "DOFMatrix.h" #include "OEMSolver.h" #include "ProblemStat.h" #ifdef HAVE_PARALLEL_DOMAIN_AMDIS #include "parallel/PetscProblemStat.h" #endif namespace AMDiS { /** * \ingroup Solver * * \brief * Pure virtual base class for specific non linear solvers. */ class NonLinSolver { public: /** \brief * Constructor * * \param name Name of this solver */ NonLinSolver(const std::string &name, OEMSolver *solver) : name(name), linSolver(solver), tolerance(1.e-8), maxIter(50), info(8), initialResidual(0.0), residual(0.0), usedNorm(NO_NORM) { Parameters::get(name + "->tolerance", tolerance); Parameters::get(name + "->max iteration", maxIter); Parameters::get(name + "->info", info); Parameters::get(name + "->norm", usedNorm); } /// Destructor virtual ~NonLinSolver() {} /** \brief * solves the non linear system. Uses sub class methods */ inline int solve(SolverMatrix >& mat, SystemVector &solution, SystemVector &rhs, AdaptInfo *adaptInfo, ProblemStat *prob) { init(); solution.set(0.0); int result = nlsolve(mat, solution, rhs, adaptInfo, prob); exit(); return result; } inline void setTolerance(double tol) { tolerance = tol; } inline double getTolerance() { return tolerance; } inline OEMSolver* getLinearSolver() { return linSolver; } protected: /// Allocates needed memory. Must be overriden in sub classes. virtual void init() = 0; /// Solves the non linear system. Must be overriden in sub classes. virtual int nlsolve(SolverMatrix >& matVec, SystemVector& x, SystemVector& rhs, AdaptInfo *adaptInfo, ProblemStat *prob) = 0; /// Frees needed memory. Must be overriden in sub classes. virtual void exit() = 0; virtual int solveLinearSystem(SolverMatrix >& mat, SystemVector &x, SystemVector &b) { FUNCNAME("NonLinSolver::solveLinearSystem()"); TEST_EXIT(linSolver)("no solver\n"); return linSolver->solveSystem(mat, x, b); } protected: /// Name of the solver. std::string name; /// Linear solver object. OEMSolver *linSolver; /// Solver tolerance. double tolerance; /// Maximal number of iterations. int maxIter; /// Info level. int info; /// Initial residual. double initialResidual; /// Current residual. double residual; /// Used norm for convergent test. Norm usedNorm; }; /// Interface for creators of concrete NonLinSolvers. class NonLinSolverCreator : public CreatorInterface { public: virtual ~NonLinSolverCreator() {} void setName(std::string n) { name = n; } void setLinearSolver(OEMSolver *solver) { linearSolver = solver; } protected: std::string name; OEMSolver *linearSolver; }; } #include "Newton.h" #endif // AMDIS_NONLINSOLVER_H