- Move existing OndselSolver, GNN ML layer, and tooling into GNN/ directory for integration in later phases - Add Create addon scaffold: package.xml, Init.py - Add expression DAG with eval, symbolic diff, simplification - Add parameter table with fixed/free variable tracking - Add quaternion rotation as polynomial Expr trees - Add RigidBody entity (7 DOF: position + unit quaternion) - Add constraint classes: Coincident, DistancePointPoint, Fixed - Add Newton-Raphson solver with symbolic Jacobian + numpy lstsq - Add pre-solve passes: substitution + single-equation - Add DOF counting via Jacobian SVD rank - Add KindredSolver IKCSolver bridge for kcsolve integration - Add 82 unit tests covering all modules Registers as 'kindred' solver via kcsolve.register_solver() when loaded by Create's addon_loader.
107 lines
3.0 KiB
C++
107 lines
3.0 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()
|
|
{
|
|
throw SimulationStoppingError("To be implemented.");
|
|
}
|
|
|
|
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 {
|
|
throw SimulationStoppingError("To be implemented.");
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|