Page MenuHomec4science

AbstractSolver.cpp
No OneTemporary

File Metadata

Created
Mon, May 20, 15:08

AbstractSolver.cpp

//
// Created by lionel on 25.11.19.
//
#include <cassert>
#include <sstream>
#include <iostream>
#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(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::checkSpaceDimension(int dim) {
if (dim < 1) {
throw std::domain_error("Dimension must be greater than 0");
} else{
if (dimension == 0) { // the dimension was never specified if it is 0, so we set it to dim
dimension = dim;
} else if (dimension != dim){ // return an error with the conflicting values of dimension
std::stringstream errorMessage;
errorMessage << "Invalid dimension : " << dim << ", it was already assigned " << dimension << ".";
throw std::length_error(errorMessage.str());
} else { // do nothing if the values of dimension match
;
}
}
}
void AbstractSolver::setInitialValue(Eigen::VectorXd & y0) {
// check it is a valid input
checkSpaceDimension(y0.size());
initialValue = y0;
}
void AbstractSolver::setRightHandSide(Eigen::MatrixXd & rhsMatrix, Eigen::VectorXd (*g_)(double t)){
// check the right hand side matrix is valid before storing it
try {
checkSpaceDimension(rhsMatrix.rows());
checkSpaceDimension(rhsMatrix.cols());
} catch (const std::length_error& e){
std::stringstream errorMessage;
errorMessage << "The linear Matrix has dimension ("<< rhsMatrix.rows() <<","<< rhsMatrix.cols() <<
") which is invalid : "<< e.what();
throw std::length_error(errorMessage.str());
}
A = rhsMatrix;
// check if the function is callable and returns object of desired dimension
try {
auto testVector = g_(initialTime);
} catch(const std::exception& e) {
std::stringstream errorMessage;
errorMessage << "Function g(t) could not be evaluated, an exception was caught with message : " << e.what();
throw std::runtime_error(errorMessage.str());
} catch (...) {
throw std::runtime_error("Function g(t) could not be evaluated. No exception was caught.");
}
// check the output of g is in the required format
try {
checkSpaceDimension(g_(initialTime).size());
} catch (const std::length_error& e){
std::stringstream errorMessage;
errorMessage << "Function g(t) has invalid dimension in output : " << e.what();
throw std::length_error(errorMessage.str());
}
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;
}
}

Event Timeline