diff --git a/OndselSolver/ASMTAssembly.h b/OndselSolver/ASMTAssembly.h index 4875f82..91b575b 100644 --- a/OndselSolver/ASMTAssembly.h +++ b/OndselSolver/ASMTAssembly.h @@ -83,7 +83,7 @@ namespace MbD { double calcCharacteristicLength(); std::shared_ptr>> connectorList(); std::shared_ptr>>markerMap(); - void deleteMbD(); + void deleteMbD() override; void createMbD(std::shared_ptr mbdSys, std::shared_ptr mbdUnits) override; void outputFile(std::string filename); void storeOnLevel(std::ofstream& os, int level) override; diff --git a/OndselSolver/AccNewtonRaphson.h b/OndselSolver/AccNewtonRaphson.h index 469994d..b304629 100644 --- a/OndselSolver/AccNewtonRaphson.h +++ b/OndselSolver/AccNewtonRaphson.h @@ -22,7 +22,7 @@ namespace MbD { void incrementIterNo() override; void initializeGlobally() override; void logSingularMatrixMessage(); - void passRootToSystem(); + void passRootToSystem() override; void postRun() override; void preRun() override; void handleSingularMatrix() override; diff --git a/OndselSolver/AnyPosICNewtonRaphson.h b/OndselSolver/AnyPosICNewtonRaphson.h index e98b9d8..6ce1a84 100644 --- a/OndselSolver/AnyPosICNewtonRaphson.h +++ b/OndselSolver/AnyPosICNewtonRaphson.h @@ -24,7 +24,7 @@ namespace MbD { void fillY() override; void fillPyPx() override; void passRootToSystem() override; - void assignEquationNumbers() = 0; + void assignEquationNumbers() override = 0; int nqsu = -1; FColDsptr qsuOld; diff --git a/OndselSolver/BasicQuasiIntegrator.h b/OndselSolver/BasicQuasiIntegrator.h index 1c53b1e..2d23891 100644 --- a/OndselSolver/BasicQuasiIntegrator.h +++ b/OndselSolver/BasicQuasiIntegrator.h @@ -15,9 +15,9 @@ namespace MbD { { // public: - void firstStep(); + void firstStep() override; bool isRedoingFirstStep(); - void nextStep(); + void nextStep() override; //void reportStepStats(); //void reportTrialStepStats(); void runInitialConditionTypeSolution() override; diff --git a/OndselSolver/DispCompIeqcJeqcKeqct.h b/OndselSolver/DispCompIeqcJeqcKeqct.h index 40f264a..b555101 100644 --- a/OndselSolver/DispCompIeqcJeqcKeqct.h +++ b/OndselSolver/DispCompIeqcJeqcKeqct.h @@ -21,13 +21,13 @@ namespace MbD { void calcPostDynCorrectorIteration() override; void initialize() override; void initializeGlobally() override; - FRowDsptr ppvaluepXIpt(); - FRowDsptr ppvaluepEIpt(); - FRowDsptr ppvaluepEKpt(); - FRowDsptr ppvaluepXJpt(); - FRowDsptr ppvaluepEJpt(); - double ppvalueptpt(); - double pvaluept(); + FRowDsptr ppvaluepXIpt() override; + FRowDsptr ppvaluepEIpt() override; + FRowDsptr ppvaluepEKpt() override; + FRowDsptr ppvaluepXJpt() override; + FRowDsptr ppvaluepEJpt() override; + double ppvalueptpt() override; + double pvaluept() override; void preAccIC() override; void preVelIC() override; diff --git a/OndselSolver/DispCompIeqctJeqcO.h b/OndselSolver/DispCompIeqctJeqcO.h index 01b1e33..4a56ba5 100644 --- a/OndselSolver/DispCompIeqctJeqcO.h +++ b/OndselSolver/DispCompIeqctJeqcO.h @@ -24,7 +24,7 @@ namespace MbD { double ppvalueptpt() override; void preAccIC() override; void preVelIC() override; - double pvaluept(); + double pvaluept() override; double priIeJeOpt; FRowDsptr ppriIeJeOpEIpt; diff --git a/OndselSolver/DistIeqcJec.h b/OndselSolver/DistIeqcJec.h index 2d3a898..dcc851b 100644 --- a/OndselSolver/DistIeqcJec.h +++ b/OndselSolver/DistIeqcJec.h @@ -20,11 +20,11 @@ namespace MbD { void calcPrivate() override; void initialize() override; - FMatDsptr ppvaluepEIpEI(); - FMatDsptr ppvaluepXIpEI(); - FMatDsptr ppvaluepXIpXI(); - FRowDsptr pvaluepEI(); - FRowDsptr pvaluepXI(); + FMatDsptr ppvaluepEIpEI() override; + FMatDsptr ppvaluepXIpEI() override; + FMatDsptr ppvaluepXIpXI() override; + FRowDsptr pvaluepEI() override; + FRowDsptr pvaluepXI() override; FRowDsptr prIeJepXI, prIeJepEI; FMatDsptr pprIeJepXIpXI, pprIeJepXIpEI, pprIeJepEIpEI, mprIeJeOpEIT; diff --git a/OndselSolver/DistIeqcJeqc.h b/OndselSolver/DistIeqcJeqc.h index e9fc3ab..aea2bf6 100644 --- a/OndselSolver/DistIeqcJeqc.h +++ b/OndselSolver/DistIeqcJeqc.h @@ -20,15 +20,15 @@ namespace MbD { void calcPrivate() override; void initialize() override; - FMatDsptr ppvaluepEIpEJ(); - FMatDsptr ppvaluepEIpXJ(); - FMatDsptr ppvaluepEJpEJ(); - FMatDsptr ppvaluepXIpEJ(); - FMatDsptr ppvaluepXIpXJ(); - FMatDsptr ppvaluepXJpEJ(); - FMatDsptr ppvaluepXJpXJ(); - FRowDsptr pvaluepEJ(); - FRowDsptr pvaluepXJ(); + FMatDsptr ppvaluepEIpEJ() override; + FMatDsptr ppvaluepEIpXJ() override; + FMatDsptr ppvaluepEJpEJ() override; + FMatDsptr ppvaluepXIpEJ() override; + FMatDsptr ppvaluepXIpXJ() override; + FMatDsptr ppvaluepXJpEJ() override; + FMatDsptr ppvaluepXJpXJ() override; + FRowDsptr pvaluepEJ() override; + FRowDsptr pvaluepXJ() override; FRowDsptr prIeJepXJ, prIeJepEJ; FMatDsptr pprIeJepXIpXJ, pprIeJepEIpXJ, pprIeJepXJpXJ, pprIeJepXIpEJ, pprIeJepEIpEJ, pprIeJepXJpEJ, pprIeJepEJpEJ, prIeJeOpEJT; diff --git a/OndselSolver/FullMatrix.cpp b/OndselSolver/FullMatrix.cpp index e2c7dbe..0c870f8 100644 --- a/OndselSolver/FullMatrix.cpp +++ b/OndselSolver/FullMatrix.cpp @@ -10,3 +10,426 @@ using namespace MbD; +template +inline FMatsptr FullMatrix::tildeMatrix(FColDsptr col) +{ + //"tildeMatrix is skew symmetric matrix related to angular velocity and cross product." + if (col->size() != 3) throw std::runtime_error("Column is not of dimension 3"); + auto tilde = std::make_shared>(3, 3); + auto c0 = col->at(0); + auto c1 = col->at(1); + auto c2 = col->at(2); + tilde->atijput(0, 0, 0.0); + tilde->atijput(1, 1, 0.0); + tilde->atijput(2, 2, 0.0); + tilde->atijput(1, 2, -c0); + tilde->atijput(0, 2, c1); + tilde->atijput(0, 1, -c2); + tilde->atijput(1, 0, c2); + tilde->atijput(2, 0, -c1); + tilde->atijput(2, 1, c0); + return tilde; +} +template<> +inline void FullMatrix::zeroSelf() +{ + for (int i = 0; i < this->size(); i++) { + this->at(i)->zeroSelf(); + } +} +template<> +inline void FullMatrix::identity() { + this->zeroSelf(); + for (int i = 0; i < this->size(); i++) { + this->at(i)->at(i) = 1.0; + } +} +template +inline FColsptr FullMatrix::column(int j) { + int n = (int)this->size(); + auto answer = std::make_shared>(n); + for (int i = 0; i < n; i++) { + answer->at(i) = this->at(i)->at(j); + } + return answer; +} +template +inline FMatsptr FullMatrix::timesFullMatrix(FMatsptr fullMat) +{ + int m = this->nrow(); + auto answer = std::make_shared>(m); + for (int i = 0; i < m; i++) { + answer->at(i) = this->at(i)->timesFullMatrix(fullMat); + } + return answer; +} +template +inline FMatsptr FullMatrix::timesTransposeFullMatrix(FMatsptr fullMat) +{ + int nrow = this->nrow(); + auto answer = std::make_shared>(nrow); + for (int i = 0; i < nrow; i++) { + answer->at(i) = this->at(i)->timesTransposeFullMatrix(fullMat); + } + return answer; +} +template<> +inline FMatDsptr FullMatrix::times(double a) +{ + int m = this->nrow(); + auto answer = std::make_shared>(m); + for (int i = 0; i < m; i++) { + answer->at(i) = this->at(i)->times(a); + } + return answer; +} +template +inline FMatsptr FullMatrix::times(T a) +{ + assert(false); +} +template +inline FMatsptr FullMatrix::transposeTimesFullMatrix(FMatsptr fullMat) +{ + return this->transpose()->timesFullMatrix(fullMat); +} +template +inline FMatsptr FullMatrix::plusFullMatrix(FMatsptr fullMat) +{ + int n = (int)this->size(); + auto answer = std::make_shared>(n); + for (int i = 0; i < n; i++) { + answer->at(i) = this->at(i)->plusFullRow(fullMat->at(i)); + } + return answer; +} +template +inline FMatsptr FullMatrix::minusFullMatrix(FMatsptr fullMat) +{ + int n = (int)this->size(); + auto answer = std::make_shared>(n); + for (int i = 0; i < n; i++) { + answer->at(i) = this->at(i)->minusFullRow(fullMat->at(i)); + } + return answer; +} +template +inline FMatsptr FullMatrix::transpose() +{ + int nrow = this->nrow(); + auto ncol = this->ncol(); + auto answer = std::make_shared>(ncol, nrow); + for (int i = 0; i < nrow; i++) { + auto& row = this->at(i); + for (int j = 0; j < ncol; j++) { + answer->at(j)->at(i) = row->at(j); + } + } + return answer; +} +template +inline FMatsptr FullMatrix::negated() +{ + return this->times(-1.0); +} +template +inline void FullMatrix::symLowerWithUpper() +{ + int n = (int)this->size(); + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + this->at(j)->at(i) = this->at(i)->at(j); + } + } +} +template +inline void FullMatrix::atiput(int i, FRowsptr fullRow) +{ + this->at(i) = fullRow; +} +template +inline void FullMatrix::atijput(int i, int j, T value) +{ + this->at(i)->atiput(j, value); +} +template +inline void FullMatrix::atijputFullColumn(int i1, int j1, FColsptr fullCol) +{ + for (int ii = 0; ii < fullCol->size(); ii++) + { + this->at(i1 + ii)->at(j1) = fullCol->at(ii); + } +} +template +inline void FullMatrix::atijplusFullRow(int i, int j, FRowsptr fullRow) +{ + this->at(i)->atiplusFullRow(j, fullRow); +} +template +inline void FullMatrix::atijplusNumber(int i, int j, T value) +{ + auto rowi = this->at(i); + rowi->at(j) += value; +} +template +inline void FullMatrix::atijminusNumber(int i, int j, T value) +{ + auto rowi = this->at(i); + rowi->at(j) -= value; +} +template<> +inline double FullMatrix::sumOfSquares() +{ + double sum = 0.0; + for (int i = 0; i < this->size(); i++) + { + sum += this->at(i)->sumOfSquares(); + } + return sum; +} +template +inline double FullMatrix::sumOfSquares() +{ + assert(false); + return 0.0; +} +template +inline void FullMatrix::zeroSelf() +{ + assert(false); +} +template +inline FMatsptr FullMatrix::copy() +{ + auto m = (int)this->size(); + auto answer = std::make_shared>(m); + for (int i = 0; i < m; i++) + { + answer->at(i) = this->at(i)->copy(); + } + return answer; +} +template +inline FullMatrix FullMatrix::operator+(const FullMatrix fullMat) +{ + int n = (int)this->size(); + auto answer = FullMatrix(n); + for (int i = 0; i < n; i++) { + answer.at(i) = this->at(i)->plusFullRow(fullMat.at(i)); + } + return answer; +} +template +inline FColsptr FullMatrix::transposeTimesFullColumn(FColsptr fullCol) +{ + auto sptr = std::make_shared>(*this); + return fullCol->transpose()->timesFullMatrix(sptr)->transpose(); +} +template +inline void FullMatrix::magnifySelf(T factor) +{ + for (int i = 0; i < this->size(); i++) { + this->at(i)->magnifySelf(factor); + } +} +template +inline std::ostream& FullMatrix::printOn(std::ostream& s) const +{ + s << "FullMat[" << std::endl; + for (int i = 0; i < this->size(); i++) + { + s << *(this->at(i)) << std::endl; + } + s << "]"; + return s; +} +template +inline std::shared_ptr> FullMatrix::asEulerParameters() +{ + //"Given [A], compute Euler parameter." + + auto traceA = this->trace(); + T dum = 0.0; + T dumSq = 0.0; + //auto qE = CREATE>::With(4); //Cannot use CREATE.h in subclasses of std::vector. Why? + auto qE = std::make_shared>(4); + qE->initialize(); + auto OneMinusTraceDivFour = (1.0 - traceA) / 4.0; + for (int i = 0; i < 3; i++) + { + dumSq = this->at(i)->at(i) / 2.0 + OneMinusTraceDivFour; + dum = (dumSq > 0.0) ? std::sqrt(dumSq) : 0.0; + qE->atiput(i, dum); + } + dumSq = (1.0 + traceA) / 4.0; + dum = (dumSq > 0.0) ? std::sqrt(dumSq) : 0.0; + qE->atiput(3, dum); + T max = 0.0; + int maxE = -1; + for (int i = 0; i < 4; i++) + { + auto num = qE->at(i); + if (max < num) { + max = num; + maxE = i; + } + } + + if (maxE == 0) { + auto FourE = 4.0 * qE->at(0); + qE->atiput(1, (this->at(0)->at(1) + this->at(1)->at(0)) / FourE); + qE->atiput(2, (this->at(0)->at(2) + this->at(2)->at(0)) / FourE); + qE->atiput(3, (this->at(2)->at(1) - this->at(1)->at(2)) / FourE); + } + else if (maxE == 1) { + auto FourE = 4.0 * qE->at(1); + qE->atiput(0, (this->at(0)->at(1) + this->at(1)->at(0)) / FourE); + qE->atiput(2, (this->at(1)->at(2) + this->at(2)->at(1)) / FourE); + qE->atiput(3, (this->at(0)->at(2) - this->at(2)->at(0)) / FourE); + } + else if (maxE == 2) { + auto FourE = 4.0 * qE->at(2); + qE->atiput(0, (this->at(0)->at(2) + this->at(2)->at(0)) / FourE); + qE->atiput(1, (this->at(1)->at(2) + this->at(2)->at(1)) / FourE); + qE->atiput(3, (this->at(1)->at(0) - this->at(0)->at(1)) / FourE); + } + else if (maxE == 3) { + auto FourE = 4.0 * qE->at(3); + qE->atiput(0, (this->at(2)->at(1) - this->at(1)->at(2)) / FourE); + qE->atiput(1, (this->at(0)->at(2) - this->at(2)->at(0)) / FourE); + qE->atiput(2, (this->at(1)->at(0) - this->at(0)->at(1)) / FourE); + } + qE->conditionSelf(); + qE->calc(); + return qE; +} +template +inline T FullMatrix::trace() +{ + T trace = 0.0; + for (int i = 0; i < this->size(); i++) + { + trace += this->at(i)->at(i); + } + return trace; +} +template +inline double FullMatrix::maxMagnitude() +{ + double max = 0.0; + for (int i = 0; i < this->size(); i++) + { + double element = this->at(i)->maxMagnitude(); + if (max < element) max = element; + } + return max; +} +template +inline FColsptr FullMatrix::bryantAngles() +{ + auto answer = std::make_shared>(3); + auto sthe1y = this->at(0)->at(2); + T the0x, the1y, the2z, cthe0x, sthe0x, y, x; + if (std::abs(sthe1y) > 0.9999) { + if (sthe1y > 0.0) { + the0x = std::atan2(this->at(1)->at(0), this->at(1)->at(1)); + the1y = M_PI / 2.0; + the2z = 0.0; + } + else { + the0x = std::atan2(this->at(2)->at(1), this->at(2)->at(0)); + the1y = M_PI / -2.0; + the2z = 0.0; + } + } + else { + the0x = std::atan2(-this->at(1)->at(2), this->at(2)->at(2)); + cthe0x = std::cos(the0x); + sthe0x = std::sin(the0x); + y = sthe1y; + if (std::abs(cthe0x) > std::abs(sthe0x)) { + x = this->at(2)->at(2) / cthe0x; + } + else { + x = this->at(1)->at(2) / -sthe0x; + } + the1y = std::atan2(y, x); + the2z = std::atan2(-this->at(0)->at(1), this->at(0)->at(0)); + } + answer->atiput(0, the0x); + answer->atiput(1, the1y); + answer->atiput(2, the2z); + return answer; +} +template +inline bool FullMatrix::isDiagonal() +{ + auto m = this->nrow(); + auto n = this->ncol(); + if (m != n) return false; + for (int i = 0; i < m; i++) + { + auto rowi = this->at(i); + for (int j = 0; j < n; j++) + { + if (i != j && rowi->at(j) != 0) return false; + } + } + return true; +} +template +inline bool FullMatrix::isDiagonalToWithin(double ratio) +{ + double maxMag = this->maxMagnitude(); + auto tol = ratio * maxMag; + auto nrow = this->nrow(); + if (nrow == this->ncol()) { + for (int i = 0; i < 3; i++) + { + for (int j = i + 1; j < 3; j++) + { + if (std::abs(this->at(i)->at(j)) > tol) return false; + if (std::abs(this->at(j)->at(i)) > tol) return false; + } + } + return true; + } + else { + return false; + } +} +template +inline std::shared_ptr> FullMatrix::asDiagonalMatrix() +{ + int nrow = this->nrow(); + auto diagMat = std::make_shared>(nrow); + for (int i = 0; i < nrow; i++) + { + diagMat->atiput(i, this->at(i)->at(i)); + } + return diagMat; +} +template +inline void FullMatrix::conditionSelfWithTol(double tol) +{ + for (auto row : *this) { + row->conditionSelfWithTol(tol); + } +} +template +inline FColsptr FullMatrix::timesFullColumn(FColsptr fullCol) +{ + return this->timesFullColumn(fullCol.get()); +} +template +inline FColsptr FullMatrix::timesFullColumn(FullColumn* fullCol) +{ + //"a*b = a(i,j)b(j) sum j." + auto nrow = this->nrow(); + auto answer = std::make_shared>(nrow); + for (int i = 0; i < nrow; i++) + { + answer->at(i) = this->at(i)->timesFullColumn(fullCol); + } + return answer; +} diff --git a/OndselSolver/FullMatrix.h b/OndselSolver/FullMatrix.h index bc954b5..bdc71c2 100644 --- a/OndselSolver/FullMatrix.h +++ b/OndselSolver/FullMatrix.h @@ -11,22 +11,32 @@ #include "corecrt_math_defines.h" #include +#include "FullRow.h" #include "RowTypeMatrix.h" #include "FullColumn.h" namespace MbD { - template - class FullMatrix; + template + class FullMatrix; + using FMatDsptr = std::shared_ptr>; + template using FMatsptr = std::shared_ptr>; + template class FullColumn; using FColDsptr = std::shared_ptr>; + template class FullRow; + + template + class RowTypeMatrix; + template class EulerParameters; + template class DiagonalMatrix; @@ -85,7 +95,7 @@ namespace MbD { FMatsptr transpose(); FMatsptr negated(); void symLowerWithUpper(); - void atiput(int i, FRowsptr fullRow); + void atiput(int i, FRowsptr fullRow) override; void atijput(int i, int j, T value); void atijputFullColumn(int i, int j, FColsptr fullCol); void atijplusFullRow(int i, int j, FRowsptr fullRow); @@ -313,428 +323,5 @@ namespace MbD { mat->identity(); return mat; } - template - inline FMatsptr FullMatrix::tildeMatrix(FColDsptr col) - { - //"tildeMatrix is skew symmetric matrix related to angular velocity and cross product." - if (col->size() != 3) throw std::runtime_error("Column is not of dimension 3"); - auto tilde = std::make_shared>(3, 3); - auto c0 = col->at(0); - auto c1 = col->at(1); - auto c2 = col->at(2); - tilde->atijput(0, 0, 0.0); - tilde->atijput(1, 1, 0.0); - tilde->atijput(2, 2, 0.0); - tilde->atijput(1, 2, -c0); - tilde->atijput(0, 2, c1); - tilde->atijput(0, 1, -c2); - tilde->atijput(1, 0, c2); - tilde->atijput(2, 0, -c1); - tilde->atijput(2, 1, c0); - return tilde; - } - template<> - inline void FullMatrix::zeroSelf() - { - for (int i = 0; i < this->size(); i++) { - this->at(i)->zeroSelf(); - } - } - template<> - inline void FullMatrix::identity() { - this->zeroSelf(); - for (int i = 0; i < this->size(); i++) { - this->at(i)->at(i) = 1.0; - } - } - template - inline FColsptr FullMatrix::column(int j) { - int n = (int)this->size(); - auto answer = std::make_shared>(n); - for (int i = 0; i < n; i++) { - answer->at(i) = this->at(i)->at(j); - } - return answer; - } - template - inline FMatsptr FullMatrix::timesFullMatrix(FMatsptr fullMat) - { - int m = this->nrow(); - auto answer = std::make_shared>(m); - for (int i = 0; i < m; i++) { - answer->at(i) = this->at(i)->timesFullMatrix(fullMat); - } - return answer; - } - template - inline FMatsptr FullMatrix::timesTransposeFullMatrix(FMatsptr fullMat) - { - int nrow = this->nrow(); - auto answer = std::make_shared>(nrow); - for (int i = 0; i < nrow; i++) { - answer->at(i) = this->at(i)->timesTransposeFullMatrix(fullMat); - } - return answer; - } - template<> - inline FMatDsptr FullMatrix::times(double a) - { - int m = this->nrow(); - auto answer = std::make_shared>(m); - for (int i = 0; i < m; i++) { - answer->at(i) = this->at(i)->times(a); - } - return answer; - } - template - inline FMatsptr FullMatrix::times(T a) - { - assert(false); - } - template - inline FMatsptr FullMatrix::transposeTimesFullMatrix(FMatsptr fullMat) - { - return this->transpose()->timesFullMatrix(fullMat); - } - template - inline FMatsptr FullMatrix::plusFullMatrix(FMatsptr fullMat) - { - int n = (int)this->size(); - auto answer = std::make_shared>(n); - for (int i = 0; i < n; i++) { - answer->at(i) = this->at(i)->plusFullRow(fullMat->at(i)); - } - return answer; - } - template - inline FMatsptr FullMatrix::minusFullMatrix(FMatsptr fullMat) - { - int n = (int)this->size(); - auto answer = std::make_shared>(n); - for (int i = 0; i < n; i++) { - answer->at(i) = this->at(i)->minusFullRow(fullMat->at(i)); - } - return answer; - } - template - inline FMatsptr FullMatrix::transpose() - { - int nrow = this->nrow(); - auto ncol = this->ncol(); - auto answer = std::make_shared>(ncol, nrow); - for (int i = 0; i < nrow; i++) { - auto& row = this->at(i); - for (int j = 0; j < ncol; j++) { - answer->at(j)->at(i) = row->at(j); - } - } - return answer; - } - template - inline FMatsptr FullMatrix::negated() - { - return this->times(-1.0); - } - template - inline void FullMatrix::symLowerWithUpper() - { - int n = (int)this->size(); - for (int i = 0; i < n; i++) { - for (int j = i + 1; j < n; j++) { - this->at(j)->at(i) = this->at(i)->at(j); - } - } - } - template - inline void FullMatrix::atiput(int i, FRowsptr fullRow) - { - this->at(i) = fullRow; - } - template - inline void FullMatrix::atijput(int i, int j, T value) - { - this->at(i)->atiput(j, value); - } - template - inline void FullMatrix::atijputFullColumn(int i1, int j1, FColsptr fullCol) - { - for (int ii = 0; ii < fullCol->size(); ii++) - { - this->at(i1 + ii)->at(j1) = fullCol->at(ii); - } - } - template - inline void FullMatrix::atijplusFullRow(int i, int j, FRowsptr fullRow) - { - this->at(i)->atiplusFullRow(j, fullRow); - } - template - inline void FullMatrix::atijplusNumber(int i, int j, T value) - { - auto rowi = this->at(i); - rowi->at(j) += value; - } - template - inline void FullMatrix::atijminusNumber(int i, int j, T value) - { - auto rowi = this->at(i); - rowi->at(j) -= value; - } - template<> - inline double FullMatrix::sumOfSquares() - { - double sum = 0.0; - for (int i = 0; i < this->size(); i++) - { - sum += this->at(i)->sumOfSquares(); - } - return sum; - } - template - inline double FullMatrix::sumOfSquares() - { - assert(false); - return 0.0; - } - template - inline void FullMatrix::zeroSelf() - { - assert(false); - } - template - inline FMatsptr FullMatrix::copy() - { - auto m = (int)this->size(); - auto answer = std::make_shared>(m); - for (int i = 0; i < m; i++) - { - answer->at(i) = this->at(i)->copy(); - } - return answer; - } - template - inline FullMatrix FullMatrix::operator+(const FullMatrix fullMat) - { - int n = (int)this->size(); - auto answer = FullMatrix(n); - for (int i = 0; i < n; i++) { - answer.at(i) = this->at(i)->plusFullRow(fullMat.at(i)); - } - return answer; - } - template - inline FColsptr FullMatrix::transposeTimesFullColumn(FColsptr fullCol) - { - auto sptr = std::make_shared>(*this); - return fullCol->transpose()->timesFullMatrix(sptr)->transpose(); - } - template - inline void FullMatrix::magnifySelf(T factor) - { - for (int i = 0; i < this->size(); i++) { - this->at(i)->magnifySelf(factor); - } - } - template - inline std::ostream& FullMatrix::printOn(std::ostream& s) const - { - s << "FullMat[" << std::endl; - for (int i = 0; i < this->size(); i++) - { - s << *(this->at(i)) << std::endl; - } - s << "]"; - return s; - } - template - inline std::shared_ptr> FullMatrix::asEulerParameters() - { - //"Given [A], compute Euler parameter." - - auto traceA = this->trace(); - T dum = 0.0; - T dumSq = 0.0; - //auto qE = CREATE>::With(4); //Cannot use CREATE.h in subclasses of std::vector. Why? - auto qE = std::make_shared>(4); - qE->initialize(); - auto OneMinusTraceDivFour = (1.0 - traceA) / 4.0; - for (int i = 0; i < 3; i++) - { - dumSq = this->at(i)->at(i) / 2.0 + OneMinusTraceDivFour; - dum = (dumSq > 0.0) ? std::sqrt(dumSq) : 0.0; - qE->atiput(i, dum); - } - dumSq = (1.0 + traceA) / 4.0; - dum = (dumSq > 0.0) ? std::sqrt(dumSq) : 0.0; - qE->atiput(3, dum); - T max = 0.0; - int maxE = -1; - for (int i = 0; i < 4; i++) - { - auto num = qE->at(i); - if (max < num) { - max = num; - maxE = i; - } - } - - if (maxE == 0) { - auto FourE = 4.0 * qE->at(0); - qE->atiput(1, (this->at(0)->at(1) + this->at(1)->at(0)) / FourE); - qE->atiput(2, (this->at(0)->at(2) + this->at(2)->at(0)) / FourE); - qE->atiput(3, (this->at(2)->at(1) - this->at(1)->at(2)) / FourE); - } - else if (maxE == 1) { - auto FourE = 4.0 * qE->at(1); - qE->atiput(0, (this->at(0)->at(1) + this->at(1)->at(0)) / FourE); - qE->atiput(2, (this->at(1)->at(2) + this->at(2)->at(1)) / FourE); - qE->atiput(3, (this->at(0)->at(2) - this->at(2)->at(0)) / FourE); - } - else if (maxE == 2) { - auto FourE = 4.0 * qE->at(2); - qE->atiput(0, (this->at(0)->at(2) + this->at(2)->at(0)) / FourE); - qE->atiput(1, (this->at(1)->at(2) + this->at(2)->at(1)) / FourE); - qE->atiput(3, (this->at(1)->at(0) - this->at(0)->at(1)) / FourE); - } - else if (maxE == 3) { - auto FourE = 4.0 * qE->at(3); - qE->atiput(0, (this->at(2)->at(1) - this->at(1)->at(2)) / FourE); - qE->atiput(1, (this->at(0)->at(2) - this->at(2)->at(0)) / FourE); - qE->atiput(2, (this->at(1)->at(0) - this->at(0)->at(1)) / FourE); - } - qE->conditionSelf(); - qE->calc(); - return qE; - } - template - inline T FullMatrix::trace() - { - T trace = 0.0; - for (int i = 0; i < this->size(); i++) - { - trace += this->at(i)->at(i); - } - return trace; - } - template - inline double FullMatrix::maxMagnitude() - { - double max = 0.0; - for (int i = 0; i < this->size(); i++) - { - double element = this->at(i)->maxMagnitude(); - if (max < element) max = element; - } - return max; - } - template - inline FColsptr FullMatrix::bryantAngles() - { - auto answer = std::make_shared>(3); - auto sthe1y = this->at(0)->at(2); - T the0x, the1y, the2z, cthe0x, sthe0x, y, x; - if (std::abs(sthe1y) > 0.9999) { - if (sthe1y > 0.0) { - the0x = std::atan2(this->at(1)->at(0), this->at(1)->at(1)); - the1y = M_PI / 2.0; - the2z = 0.0; - } - else { - the0x = std::atan2(this->at(2)->at(1), this->at(2)->at(0)); - the1y = M_PI / -2.0; - the2z = 0.0; - } - } - else { - the0x = std::atan2(-this->at(1)->at(2), this->at(2)->at(2)); - cthe0x = std::cos(the0x); - sthe0x = std::sin(the0x); - y = sthe1y; - if (std::abs(cthe0x) > std::abs(sthe0x)) { - x = this->at(2)->at(2) / cthe0x; - } - else { - x = this->at(1)->at(2) / -sthe0x; - } - the1y = std::atan2(y, x); - the2z = std::atan2(-this->at(0)->at(1), this->at(0)->at(0)); - } - answer->atiput(0, the0x); - answer->atiput(1, the1y); - answer->atiput(2, the2z); - return answer; - } - template - inline bool FullMatrix::isDiagonal() - { - auto m = this->nrow(); - auto n = this->ncol(); - if (m != n) return false; - for (int i = 0; i < m; i++) - { - auto rowi = this->at(i); - for (int j = 0; j < n; j++) - { - if (i != j && rowi->at(j) != 0) return false; - } - } - return true; - } - template - inline bool FullMatrix::isDiagonalToWithin(double ratio) - { - double maxMag = this->maxMagnitude(); - auto tol = ratio * maxMag; - auto nrow = this->nrow(); - if (nrow == this->ncol()) { - for (int i = 0; i < 3; i++) - { - for (int j = i + 1; j < 3; j++) - { - if (std::abs(this->at(i)->at(j)) > tol) return false; - if (std::abs(this->at(j)->at(i)) > tol) return false; - } - } - return true; - } - else { - return false; - } - } - template - inline std::shared_ptr> FullMatrix::asDiagonalMatrix() - { - int nrow = this->nrow(); - auto diagMat = std::make_shared>(nrow); - for (int i = 0; i < nrow; i++) - { - diagMat->atiput(i, this->at(i)->at(i)); - } - return diagMat; - } - template - inline void FullMatrix::conditionSelfWithTol(double tol) - { - for (auto row : *this) { - row->conditionSelfWithTol(tol); - } - } - template - inline FColsptr FullMatrix::timesFullColumn(FColsptr fullCol) - { - return this->timesFullColumn(fullCol.get()); - } - template - inline FColsptr FullMatrix::timesFullColumn(FullColumn* fullCol) - { - //"a*b = a(i,j)b(j) sum j." - auto nrow = this->nrow(); - auto answer = std::make_shared>(nrow); - for (int i = 0; i < nrow; i++) - { - answer->at(i) = this->at(i)->timesFullColumn(fullCol); - } - return answer; - } } diff --git a/OndselSolver/FullRow.cpp b/OndselSolver/FullRow.cpp index 3458277..15cf73b 100644 --- a/OndselSolver/FullRow.cpp +++ b/OndselSolver/FullRow.cpp @@ -9,3 +9,40 @@ #include "FullRow.h" using namespace MbD; + +template +FMatsptr FullRow::transposeTimesFullRow(FRowsptr fullRow) +{ + //"a*b = a(i)b(j)" + auto nrow = (int)this->size(); + auto answer = std::make_shared>(nrow); + for (int i = 0; i < nrow; i++) + { + answer->atiput(i, fullRow->times(this->at(i))); + } + return answer; +} + +template +inline FRowsptr FullRow::timesTransposeFullMatrix(FMatsptr fullMat) +{ + //"a*bT = a(1,j)b(k,j)" + int ncol = fullMat->nrow(); + auto answer = std::make_shared>(ncol); + for (int k = 0; k < ncol; k++) { + answer->at(k) = this->dot(fullMat->at(k)); + } + return answer; +} + +template +inline FRowsptr FullRow::timesFullMatrix(FMatsptr fullMat) +{ + FRowsptr answer = fullMat->at(0)->times(this->at(0)); + for (int j = 1; j < (int) this->size(); j++) + { + answer->equalSelfPlusFullRowTimes(fullMat->at(j), this->at(j)); + } + return answer; + //return FRowsptr(); +} diff --git a/OndselSolver/FullRow.h b/OndselSolver/FullRow.h index b4df0bd..59bd6c9 100644 --- a/OndselSolver/FullRow.h +++ b/OndselSolver/FullRow.h @@ -9,7 +9,7 @@ #pragma once #include "FullVector.h" -//#include "FullColumn.h" +#include "FullMatrix.h" namespace MbD { template @@ -18,14 +18,12 @@ namespace MbD { using FRowsptr = std::shared_ptr>; using FRowDsptr = std::shared_ptr>; template - class FullMatrix; - template - using FMatsptr = std::shared_ptr>; - template class FullColumn; template using FColsptr = std::shared_ptr>; using ListFRD = std::initializer_list; + template + class FullMatrix; template class FullRow : public FullVector @@ -43,14 +41,14 @@ namespace MbD { FRowsptr minusFullRow(FRowsptr fullRow); T timesFullColumn(FColsptr fullCol); T timesFullColumn(FullColumn* fullCol); - FRowsptr timesFullMatrix(FMatsptr fullMat); - FRowsptr timesTransposeFullMatrix(FMatsptr fullMat); + FRowsptr timesFullMatrix(std::shared_ptr> fullMat); + FRowsptr timesTransposeFullMatrix(std::shared_ptr> fullMat); void equalSelfPlusFullRowTimes(FRowsptr fullRow, double factor); void equalFullRow(FRowsptr fullRow); FColsptr transpose(); FRowsptr copy(); void atiplusFullRow(int j, FRowsptr fullRow); - FMatsptr transposeTimesFullRow(FRowsptr fullRow); + std::shared_ptr> transposeTimesFullRow(FRowsptr fullRow); std::ostream& printOn(std::ostream& s) const override; }; @@ -111,17 +109,6 @@ namespace MbD { return answer; } template - inline FRowsptr FullRow::timesTransposeFullMatrix(FMatsptr fullMat) - { - //"a*bT = a(1,j)b(k,j)" - int ncol = fullMat->nrow(); - auto answer = std::make_shared>(ncol); - for (int k = 0; k < ncol; k++) { - answer->at(k) = this->dot(fullMat->at(k)); - } - return answer; - } - template inline void FullRow::equalSelfPlusFullRowTimes(FRowsptr fullRow, double factor) { this->equalSelfPlusFullVectortimes(fullRow, factor); @@ -157,18 +144,6 @@ namespace MbD { } } template - inline FMatsptr FullRow::transposeTimesFullRow(FRowsptr fullRow) - { - //"a*b = a(i)b(j)" - auto nrow = (int)this->size(); - auto answer = std::make_shared>(nrow); - for (int i = 0; i < nrow; i++) - { - answer->atiput(i, fullRow->times(this->at(i))); - } - return answer; - } - template inline std::ostream& FullRow::printOn(std::ostream& s) const { s << "FullRow{"; @@ -180,16 +155,5 @@ namespace MbD { s << "}"; return s; } - template - inline FRowsptr FullRow::timesFullMatrix(FMatsptr fullMat) - { - FRowsptr answer = fullMat->at(0)->times(this->at(0)); - for (int j = 1; j < (int) this->size(); j++) - { - answer->equalSelfPlusFullRowTimes(fullMat->at(j), this->at(j)); - } - return answer; - //return FRowsptr(); - } } diff --git a/OndselSolver/GESpMat.h b/OndselSolver/GESpMat.h index fb24492..517ea82 100644 --- a/OndselSolver/GESpMat.h +++ b/OndselSolver/GESpMat.h @@ -16,7 +16,7 @@ namespace MbD { { //markowitzPivotRowCount markowitzPivotColCount privateIndicesOfNonZerosInPivotRow rowPositionsOfNonZerosInPivotColumn public: - FColDsptr solvewithsaveOriginal(SpMatDsptr spMat, FColDsptr fullCol, bool saveOriginal); + FColDsptr solvewithsaveOriginal(SpMatDsptr spMat, FColDsptr fullCol, bool saveOriginal) override; FColDsptr basicSolvewithsaveOriginal(SpMatDsptr spMat, FColDsptr fullCol, bool saveOriginal) override; FColDsptr basicSolvewithsaveOriginal(FMatDsptr fullMat, FColDsptr fullCol, bool saveOriginal) override; void preSolvewithsaveOriginal(FMatDsptr fullMat, FColDsptr fullCol, bool saveOriginal) override; diff --git a/OndselSolver/IntegratorInterface.h b/OndselSolver/IntegratorInterface.h index 93ff606..b976520 100644 --- a/OndselSolver/IntegratorInterface.h +++ b/OndselSolver/IntegratorInterface.h @@ -20,7 +20,7 @@ namespace MbD { public: void initializeGlobally() override; - virtual void preRun() = 0; + virtual void preRun() override = 0; virtual void checkForDiscontinuity() = 0; void setSystem(Solver* sys) override; diff --git a/OndselSolver/Item.h b/OndselSolver/Item.h index 8ab95f8..79a5690 100644 --- a/OndselSolver/Item.h +++ b/OndselSolver/Item.h @@ -151,13 +151,14 @@ namespace MbD { virtual std::ostream& printOn(std::ostream& s) const; friend std::ostream& operator<<(std::ostream& s, const Item& item) { - if (&item) { + // the following if cannot be false +// if (&item) { return item.printOn(s); - } - else { - s << "NULL"; - } - return s; +// } +// else { +// s << "NULL"; +// } + //return s; } std::string name; diff --git a/OndselSolver/LDUSpMat.h b/OndselSolver/LDUSpMat.h index fe4240c..5965a24 100644 --- a/OndselSolver/LDUSpMat.h +++ b/OndselSolver/LDUSpMat.h @@ -19,7 +19,7 @@ namespace MbD { FColDsptr basicSolvewithsaveOriginal(SpMatDsptr spMat, FColDsptr fullCol, bool saveOriginal) override; void decomposesaveOriginal(FMatDsptr fullMat, bool saveOriginal); void decomposesaveOriginal(SpMatDsptr spMat, bool saveOriginal); - FColDsptr forAndBackSubsaveOriginal(FColDsptr fullCol, bool saveOriginal); + FColDsptr forAndBackSubsaveOriginal(FColDsptr fullCol, bool saveOriginal) override; double getmatrixArowimaxMagnitude(int i) override; void forwardSubstituteIntoL() override; void backSubstituteIntoDU() override; diff --git a/OndselSolver/MBDynMarker.h b/OndselSolver/MBDynMarker.h index 1b3dca7..0d3d95d 100644 --- a/OndselSolver/MBDynMarker.h +++ b/OndselSolver/MBDynMarker.h @@ -14,7 +14,7 @@ namespace MbD { class MBDynMarker : public MBDynItem { public: - void parseMBDyn(std::vector& args); + void parseMBDyn(std::vector& args) override; void createASMT() override; std::string nodeStr; diff --git a/OndselSolver/PosICNewtonRaphson.cpp b/OndselSolver/PosICNewtonRaphson.cpp index b894656..0c84dfc 100644 --- a/OndselSolver/PosICNewtonRaphson.cpp +++ b/OndselSolver/PosICNewtonRaphson.cpp @@ -97,7 +97,7 @@ bool PosICNewtonRaphson::isConverged() void PosICNewtonRaphson::handleSingularMatrix() { nSingularMatrixError++; - if (nSingularMatrixError = 1){ + if (nSingularMatrixError == 1){ this->lookForRedundantConstraints(); matrixSolver = this->matrixSolverClassNew(); } diff --git a/OndselSolver/RowTypeMatrix.h b/OndselSolver/RowTypeMatrix.h index 86b799b..9e0efd5 100644 --- a/OndselSolver/RowTypeMatrix.h +++ b/OndselSolver/RowTypeMatrix.h @@ -21,7 +21,7 @@ namespace MbD { RowTypeMatrix(int m) : Array(m) {} RowTypeMatrix(std::initializer_list list) : Array{ list } {} void copyFrom(std::shared_ptr> x); - virtual void zeroSelf() = 0; + virtual void zeroSelf() override = 0; //double maxMagnitude() override; int numberOfElements() override; diff --git a/OndselSolver/SparseMatrix.h b/OndselSolver/SparseMatrix.h index 44e18c1..22276c2 100644 --- a/OndselSolver/SparseMatrix.h +++ b/OndselSolver/SparseMatrix.h @@ -40,7 +40,7 @@ namespace MbD { this->push_back(row); } } - void atiput(int i, SpRowsptr spRow); + void atiput(int i, SpRowsptr spRow) override; void atijplusDiagonalMatrix(int i, int j, DiagMatDsptr diagMat); void atijminusDiagonalMatrix(int i, int j, DiagMatDsptr diagMat); double sumOfSquares() override; diff --git a/OndselSolver/SystemNewtonRaphson.h b/OndselSolver/SystemNewtonRaphson.h index aea3d36..1cfedd7 100644 --- a/OndselSolver/SystemNewtonRaphson.h +++ b/OndselSolver/SystemNewtonRaphson.h @@ -19,7 +19,7 @@ namespace MbD { // public: void initializeGlobally() override; - virtual void assignEquationNumbers() = 0; + virtual void assignEquationNumbers() override = 0; virtual void createVectorsAndMatrices(); std::shared_ptr matrixSolverClassNew() override; void calcdxNorm() override;