Files
solver/OndselSolver/SystemNewtonRaphson.cpp
aiksiongkoh 3d6a23a678 Cmake gtest (#72)
* Check rackpin and gear for zero radii

* rebase zero-radii-check (#69)

* contributing

* Update push-freecad.yml

updated actions/checkout to v4

* dragging log for debugging

* fix calcdxNorm crash

* setDebug and remove MBDyn*

* Update cmakelists.txt

* fix includes for gcc-14

gcc-14 is more disciplined about not including <algorithm> transitively.

* fix runDragStep

* backhoe files (#65)

* Mark unused variables to silence compiler warnings. (#64)

* Backhoe issues (#67)

* backhoe issues

* runDragStep edit

* backhoe issues

* runDragStep edit

* Reduce large drag step progressively until convergence.

* Switch to using built-in M_PI, even on MSVC (#68)

---------

Co-authored-by: Brad Collette <bradcollette@pop-os.localdomain>
Co-authored-by: mosfet80 <realeandrea@yahoo.it>
Co-authored-by: PaddleStroke <pierrelouis.boyer@gmail.com>
Co-authored-by: Jed Brown <jed@jedbrown.org>
Co-authored-by: sliptonic <shopinthewoods@gmail.com>
Co-authored-by: Chris Hennes <chennes@pioneerlibrarysystem.org>

* in progress

* Gtest added

---------

Co-authored-by: Brad Collette <bradcollette@pop-os.localdomain>
Co-authored-by: mosfet80 <realeandrea@yahoo.it>
Co-authored-by: PaddleStroke <pierrelouis.boyer@gmail.com>
Co-authored-by: Jed Brown <jed@jedbrown.org>
Co-authored-by: sliptonic <shopinthewoods@gmail.com>
Co-authored-by: Chris Hennes <chennes@pioneerlibrarysystem.org>
2024-07-30 14:45:59 -06:00

107 lines
2.9 KiB
C++

/***************************************************************************
* Copyright (c) 2023 Ondsel, Inc. *
* *
* This file is part of OndselSolver. *
* *
* See LICENSE file for details about copyright. *
***************************************************************************/
#include <fstream>
#include <iomanip>
#include "SystemNewtonRaphson.h"
#include "SystemSolver.h"
#include "SparseMatrix.h"
#include "MatrixSolver.h"
#include "GESpMatParPvMarkoFast.h"
#include "CREATE.h"
#include "GESpMatParPvPrecise.h"
using namespace MbD;
void SystemNewtonRaphson::initializeGlobally()
{
this->assignEquationNumbers();
system->partsJointsMotionsLimitsForcesTorquesDo([&](std::shared_ptr<Item> item) { item->useEquationNumbers(); });
this->createVectorsAndMatrices();
matrixSolver = this->matrixSolverClassNew();
}
void MbD::SystemNewtonRaphson::assignEquationNumbers()
{
assert(false);
}
void SystemNewtonRaphson::createVectorsAndMatrices()
{
x = std::make_shared<FullColumn<double>>(n);
y = std::make_shared<FullColumn<double>>(n);
pypx = std::make_shared <SparseMatrix<double>>(n, n);
}
std::shared_ptr<MatrixSolver> SystemNewtonRaphson::matrixSolverClassNew()
{
return CREATE<GESpMatParPvMarkoFast>::With();
}
void SystemNewtonRaphson::calcdxNorm()
{
VectorNewtonRaphson::calcdxNorm();
std::stringstream ss;
ss << std::setprecision(std::numeric_limits<double>::max_digits10);
ss << "MbD: Convergence = " << dxNorm;
auto str = ss.str();
system->logString(str);
}
void SystemNewtonRaphson::basicSolveEquations()
{
auto debug = false;
if (debug) {
outputSpreadsheet();
}
dx = matrixSolver->solvewithsaveOriginal(pypx, y->negated(), false);
}
void SystemNewtonRaphson::handleSingularMatrix()
{
auto& r = *matrixSolver;
std::string str = typeid(r).name();
if (str.find("GESpMatParPvMarkoFast") != std::string::npos) {
matrixSolver = CREATE<GESpMatParPvPrecise>::With();
this->solveEquations();
}
else {
str = typeid(r).name();
if (str.find("GESpMatParPvPrecise") != std::string::npos) {
str = "MbD: Singular Matrix Error. ";
system->logString(str);
matrixSolver->throwSingularMatrixError("SystemNewtonRaphson");
}
else {
assert(false);
}
}
}
void MbD::SystemNewtonRaphson::outputSpreadsheet()
{
std::ofstream os("../../testapp/spreadsheetcpp.csv");
os << std::setprecision(std::numeric_limits<double>::max_digits10);
for (size_t i = 0; i < pypx->nrow(); i++)
{
auto rowi = pypx->at(i);
for (size_t j = 0; j < pypx->ncol(); j++)
{
if (j > 0) os << '\t';
if (rowi->find(j) == rowi->end()) {
os << 0.0;
}
else {
os << rowi->at(j);
}
}
os << "\t\t" << y->at(i) << std::endl;
}
}