diff --git a/ODElibrary/AbstractSolver.cpp b/ODElibrary/AbstractSolver.cpp index 3da9817..a77df61 100644 --- a/ODElibrary/AbstractSolver.cpp +++ b/ODElibrary/AbstractSolver.cpp @@ -1,78 +1,82 @@ // // Created by lionel on 25.11.19. // #include #include #include "AbstractSolver.h" AbstractSolver::AbstractSolver(){ stepSize=0; initialTime=0; finalTime=0; dimension=0; nSteps=0; }; AbstractSolver::~AbstractSolver(){}; void AbstractSolver::setInitialTime(const double t0) { initialTime = t0; } void AbstractSolver::setFinalTime(const double tf) { finalTime = tf; } -void AbstractSolver::setStepSize(double h) { - stepSize = h; +void AbstractSolver::setStepSize(const double h) { + if (h > 0) { + stepSize = h; + } else { + throw std::domain_error("Step size h must be strictly positive"); + } } void AbstractSolver::setNumberOfSteps(int n){ nSteps=n; } void AbstractSolver::setSpaceDimension(int dim) { assert(dim > 0); dimension=dim; } void AbstractSolver::setInitialValue(Eigen::VectorXd & y0) { initialValue = y0; } void AbstractSolver::setRightHandSide(Eigen::MatrixXd & rhsMatrix, Eigen::VectorXd (*g_)(double t)) { A = rhsMatrix; g = g_; } double AbstractSolver::getInitialTime() const{ return initialTime; } double AbstractSolver::getFinalTime() const{ return finalTime; } double AbstractSolver::getStepSize() const{ return stepSize; } int AbstractSolver::getSpaceDimension() const { return dimension; } void AbstractSolver::setOutputType(outputTypes type) { outputType = type; } void AbstractSolver::sendToOutput(double t, Eigen::VectorXd y) { switch(outputType){ case outputTypes::Terminal: std::cout << t << "\t" << y.transpose() << std::endl; } } diff --git a/ODElibrary/AbstractSolver.h b/ODElibrary/AbstractSolver.h index 4a16192..0dd57b5 100644 --- a/ODElibrary/AbstractSolver.h +++ b/ODElibrary/AbstractSolver.h @@ -1,61 +1,61 @@ // // Created by lionel on 25.11.19. // #ifndef ODELIBRARY_ABSTRACTSOLVER_H #define ODELIBRARY_ABSTRACTSOLVER_H #include #include enum class outputTypes{ Unknown, File, Terminal}; /** This is an abstract class for the solver of differential equations * */ class AbstractSolver { protected: // variables for the setting double stepSize; double initialTime; double finalTime; int dimension; // dimension of x int nSteps; Eigen::VectorXd initialValue; outputTypes outputType; // variables for the rhs of the equation Eigen::MatrixXd A; Eigen::VectorXd (*g)(double t); // function g depending on time public: AbstractSolver(); virtual ~AbstractSolver(); void setInitialTime(double t0); void setFinalTime(double tf); void setInitialValue(Eigen::VectorXd & y0); void setStepSize(double h); void setNumberOfSteps(int n); void setSpaceDimension(int dim); void setRightHandSide(Eigen::MatrixXd & rhsMatrix, Eigen::VectorXd (*g_)(double t)); void setOutputType(outputTypes type); void sendToOutput(double t, Eigen::VectorXd y); double getInitialTime() const; double getFinalTime() const; double getStepSize() const; int getSpaceDimension() const; virtual void solve() = 0; }; -#endif //ODELIBRARY_ABSTRACTSOLVER_H +#endif ODELIBRARY_ABSTRACTSOLVER_H diff --git a/ODElibrary/ForwardEuler.h b/ODElibrary/ForwardEuler.h index f7349b3..b18f8e2 100644 --- a/ODElibrary/ForwardEuler.h +++ b/ODElibrary/ForwardEuler.h @@ -1,11 +1,14 @@ - +#ifndef ODELIBRARY_FORWARDEULER_H +#define ODELIBRARY_FORWARDEULER_H #include "AbstractSolver.h" class ForwardEuler : public AbstractSolver{ public: void solve() override; void solve(std::ofstream& outputFile); }; +#endif ODELIBRARY_FORWARDEULER_H +